Skip to content

Possible JIT inlining optimization: removing multiple argument null checks #2156

@mikernet

Description

@mikernet

Would it make sense for the JITer to eliminate redundant "if null then throw" (i.e. if (arg == null) throw Exception();) checks for a given argument if it is not possible for that argument value to have changed between the checks? As far as I can tell this does not currently happen, even for simple cases.

For example:

void M1(string value) => M2(value ?? throw new ArgumentNullException();

[MethodImpl(MethodImplOptions.AggressiveInlining)]
void M2(string value) => M3(value ?? throw new ArgumentNullException();

[MethodImpl(MethodImplOptions.AggressiveInlining)]
void M3(string value) => M4(value ?? throw new ArgumentNullException();

[MethodImpl(MethodImplOptions.AggressiveInlining)]
void M4(string value)
{
    if (value == null)
        throw new ArgumentNullException();
}

// After inlining M1 effectively becomes:

void M1(string value)
{
    if (value == null) throw new ArgumentNullException();
    // These can be removed:
    if (value == null) throw new ArgumentNullException();
    if (value == null) throw new ArgumentNullException();
    if (value == null) throw new ArgumentNullException();
}

Metadata

Metadata

Assignees

Labels

area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions