Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JIT: Avoid boxing ArgumentNullException.ThrowIfNull arguments in Tier-0 #104815

Merged
merged 8 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/coreclr/jit/importercalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3256,6 +3256,9 @@ GenTree* Compiler::impIntrinsic(CORINFO_CLASS_HANDLE clsHnd,
// to avoid some unnecessary boxing
case NI_System_Enum_HasFlag:

// This one is made intrinsic specifically to avoid boxing in Tier0
case NI_System_ArgumentNullException_ThrowIfNull:

// Most atomics are compiled to single instructions
case NI_System_Threading_Interlocked_And:
case NI_System_Threading_Interlocked_Or:
Expand Down Expand Up @@ -3786,6 +3789,32 @@ GenTree* Compiler::impIntrinsic(CORINFO_CLASS_HANDLE clsHnd,
break;
}

case NI_System_ArgumentNullException_ThrowIfNull:
{
// void ThrowIfNull(object? argument, string? paramName = null)
// void ThrowIfNull(object? argument, ExceptionArgument paramName)
assert(sig->numArgs == 2);
assert(sig->retType == CORINFO_TYPE_VOID);

// if we see:
//
// ArgumentNullException_ThrowIfNull(GT_BOX(...), ...)
EgorBo marked this conversation as resolved.
Show resolved Hide resolved
//
// We should be able to remove the call (and the box). It is done via intrinsic only
// because Tier0 is not able to remove the box itself.
//
GenTree* arg = impStackTop(1).val;
if (arg->OperIs(GT_BOX) && !opts.compDbgCode && !opts.jitFlags->IsSet(JitFlags::JIT_FLAG_MIN_OPT))
{
impSpillSideEffects(true, CHECK_SPILL_ALL DEBUGARG("spill side effects for ThrowIfNull"));
gtTryRemoveBoxUpstreamEffects(arg, BR_REMOVE_AND_NARROW);
impPopStack();
impPopStack();
retNode = gtNewNothingNode();
}
break;
}

case NI_System_Enum_HasFlag:
{
GenTree* thisOp = impStackTop(1).val;
Expand Down Expand Up @@ -9825,6 +9854,13 @@ NamedIntrinsic Compiler::lookupNamedIntrinsic(CORINFO_METHOD_HANDLE method)
result = NI_System_Activator_DefaultConstructorOf;
}
}
else if (strcmp(className, "ArgumentNullException") == 0)
{
if (strcmp(methodName, "ThrowIfNull") == 0)
{
result = NI_System_ArgumentNullException_ThrowIfNull;
}
}
else if (strcmp(className, "Array") == 0)
{
if (strcmp(methodName, "Clone") == 0)
Expand Down
2 changes: 2 additions & 0 deletions src/coreclr/jit/namedintrinsiclist.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ enum NamedIntrinsic : unsigned short
{
NI_Illegal = 0,

NI_System_ArgumentNullException_ThrowIfNull,

NI_System_Enum_HasFlag,

NI_System_BitConverter_DoubleToInt64Bits,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ protected ArgumentNullException(SerializationInfo info, StreamingContext context
/// <summary>Throws an <see cref="ArgumentNullException"/> if <paramref name="argument"/> is null.</summary>
/// <param name="argument">The reference type argument to validate as non-null.</param>
/// <param name="paramName">The name of the parameter with which <paramref name="argument"/> corresponds.</param>
[Intrinsic] // Tier0 intrinsic to avoid redundant boxing in generics
public static void ThrowIfNull([NotNull] object? argument, [CallerArgumentExpression(nameof(argument))] string? paramName = null)
{
if (argument is null)
Expand All @@ -59,6 +60,7 @@ public static void ThrowIfNull([NotNull] object? argument, [CallerArgumentExpres
}
}

[Intrinsic] // Tier0 intrinsic to avoid redundant boxing in generics
internal static void ThrowIfNull([NotNull] object? argument, ExceptionArgument paramName)
{
if (argument is null)
Expand Down
Loading