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 6 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
2 changes: 2 additions & 0 deletions src/coreclr/jit/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -4541,6 +4541,8 @@ class Compiler

GenTree* impDuplicateWithProfiledArg(GenTreeCall* call, IL_OFFSET ilOffset);

GenTree* impThrowIfNull(GenTreeCall* call);

#ifdef DEBUG
var_types impImportJitTestLabelMark(int numArgs);
#endif // DEBUG
Expand Down
8 changes: 7 additions & 1 deletion src/coreclr/jit/gentree.h
Original file line number Diff line number Diff line change
Expand Up @@ -2265,6 +2265,7 @@ struct GenTree
return OperGet() == GT_CALL;
}
inline bool IsHelperCall();
inline bool IsHelperCall(Compiler* compiler, unsigned helper);

bool gtOverflow() const;
bool gtOverflowEx() const;
Expand Down Expand Up @@ -10500,7 +10501,12 @@ inline bool GenTree::IsCnsVec() const

inline bool GenTree::IsHelperCall()
{
return OperGet() == GT_CALL && AsCall()->IsHelperCall();
return IsCall() && AsCall()->IsHelperCall();
}

inline bool GenTree::IsHelperCall(Compiler* compiler, unsigned helper)
{
return IsCall() && AsCall()->IsHelperCall(compiler, helper);
}
jakobbotsch marked this conversation as resolved.
Show resolved Hide resolved

inline var_types GenTree::CastFromType()
Expand Down
119 changes: 119 additions & 0 deletions src/coreclr/jit/importercalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1330,6 +1330,10 @@ var_types Compiler::impImportCall(OPCODE opcode,
}
else
{
if (call->IsCall() && call->AsCall()->IsSpecialIntrinsic(this, NI_System_ArgumentNullException_ThrowIfNull))
{
call = impThrowIfNull(call->AsCall());
}
impAppendTree(call, CHECK_SPILL_ALL, impCurStmtDI);
}
}
Expand Down Expand Up @@ -1528,6 +1532,70 @@ var_types Compiler::impImportCall(OPCODE opcode,
#pragma warning(pop)
#endif

//------------------------------------------------------------------------
// impThrowIfNull: Remove redundandant boxing from ArgumentNullException_ThrowIfNull
// it is done for Tier0 where we can't remove it without inlining otherwise.
//
// We have the following pattern:
//
// ArgumentNullException.ThrowIfNull(CORINFO_HELP_BOX_NULLABLE(classHandle, addr), valueName);
//
// We need to transform it to:
//
// var addrTmp = addr;
// var valueNameTmp = valueName;
// addr->hasValue != 0 ? NOP : ArgumentNullException.ThrowIfNull(null, valueNameTmp)
//
// Arguments:
// call -- call representing ArgumentNullException_ThrowIfNull
//
// Return Value:
// Optimized tree (or the original call tree if we can't optimize it).
//
GenTree* Compiler::impThrowIfNull(GenTreeCall* call)
{
assert(call->IsSpecialIntrinsic(this, NI_System_ArgumentNullException_ThrowIfNull));
assert(call->gtArgs.CountUserArgs() == 2);

GenTree* value = call->gtArgs.GetUserArgByIndex(0)->GetNode();
GenTree* valueName = call->gtArgs.GetUserArgByIndex(1)->GetNode();

if (!value->IsHelperCall(this, CORINFO_HELP_BOX_NULLABLE))
{
// We're not boxing - bail out.
return call;
}

GenTree* boxHelperClsArg = value->AsCall()->gtArgs.GetUserArgByIndex(0)->GetNode();
GenTree* boxHelperAddrArg = value->AsCall()->gtArgs.GetUserArgByIndex(1)->GetNode();

if ((boxHelperClsArg->gtFlags & GTF_SIDE_EFFECT) != 0)
{
// boxHelperClsArg is always just a class handle constant, so we don't bother spilling it.
return call;
}

// Now we need to spill the addr and argName arguments in the correct order
// to preserve possible side effects.
unsigned boxedValTmp = lvaGrabTemp(true DEBUGARG("boxedVal spilled"));
unsigned boxedArgNameTmp = lvaGrabTemp(true DEBUGARG("boxedArg spilled"));
impStoreToTemp(boxedValTmp, boxHelperAddrArg, CHECK_SPILL_ALL);
impStoreToTemp(boxedArgNameTmp, valueName, CHECK_SPILL_ALL);

// Change arguments to 'ThrowIfNull(null, valueNameTmp)'
call->gtArgs.GetUserArgByIndex(0)->EarlyNodeRef() = gtNewNull();
call->gtArgs.GetUserArgByIndex(1)->EarlyNodeRef() = gtNewLclvNode(boxedArgNameTmp, valueName->TypeGet());
EgorBo marked this conversation as resolved.
Show resolved Hide resolved

// This is Tier0 specific, so we create a raw indir node to access Nullable<T>.hasValue field
// which is the first field of Nullable<T> struct and is of type 'bool'.
//
static_assert_no_msg(OFFSETOF__CORINFO_NullableOfT__hasValue == 0);
GenTree* hasValueField = gtNewIndir(TYP_UBYTE, gtNewLclvNode(boxedValTmp, boxHelperAddrArg->TypeGet()));
EgorBo marked this conversation as resolved.
Show resolved Hide resolved
GenTreeOp* cond = gtNewOperNode(GT_NE, TYP_INT, hasValueField, gtNewIconNode(0));

return gtNewQmarkNode(TYP_VOID, cond, gtNewColonNode(TYP_VOID, gtNewNothingNode(), call));
}

//------------------------------------------------------------------------
// impDuplicateWithProfiledArg: duplicates a call with a profiled argument, e.g.:
// Given `Buffer.Memmove(dst, src, len)` call,
Expand Down Expand Up @@ -3256,6 +3324,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 +3857,47 @@ 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 (opts.compDbgCode || opts.jitFlags->IsSet(JitFlags::JIT_FLAG_MIN_OPT))
{
// Don't fold it for debug code or forced MinOpts
break;
}

// 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
// because Tier0 is not able to remove the box itself (it doesn't inline callees).
//
GenTree* arg = impStackTop(1).val;
if (arg->OperIs(GT_BOX))
{
impSpillSideEffects(true, CHECK_SPILL_ALL DEBUGARG("spill side effects for ThrowIfNull"));
gtTryRemoveBoxUpstreamEffects(arg, BR_REMOVE_AND_NARROW);
impPopStack();
impPopStack();
retNode = gtNewNothingNode();
}
EgorBo marked this conversation as resolved.
Show resolved Hide resolved
else
{
// Nullable is a bit more complicated, we need to materialize the actual ThrowIfNull call first
// NOTE: when optimizations are enabled, we generate a better code for this case as is.
if (!opts.OptimizationEnabled() && arg->IsHelperCall(this, CORINFO_HELP_BOX_NULLABLE))
{
isSpecial = true;
}
}
break;
}

case NI_System_Enum_HasFlag:
{
GenTree* thisOp = impStackTop(1).val;
Expand Down Expand Up @@ -9825,6 +9937,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