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

Allow some intrinsics in Tier0 #77357

Merged
merged 12 commits into from
Feb 13, 2023
25 changes: 24 additions & 1 deletion src/coreclr/jit/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -9037,6 +9037,12 @@ XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
COUNT_OPT_CODE
};

enum OptimizationsKind
EgorBo marked this conversation as resolved.
Show resolved Hide resolved
{
OPT_Lightweight,
OPT_All
};

struct Options
{
JitFlags* jitFlags; // all flags passed from the EE
Expand Down Expand Up @@ -9106,8 +9112,25 @@ XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
{
return MinOpts() || compDbgCode;
}
bool OptimizationEnabled() const

//------------------------------------------------------------------------
// OptimizationEnabled: Are optimizations of the specified kind allowed?
//
// Arguments:
// kind - there are two kinds of optimizations:
// OPT_All: all kinds of optimizations
// OPT_Lightweight: only those which won't regress JIT throughput
//
// Return Value:
// true if JIT is alowed to perform optimizations of the given kind.
//
bool OptimizationEnabled(OptimizationsKind kind = OPT_All) const
{
if (kind == OPT_Lightweight)
{
// Quick optimizations are allowed for all cases except debug code mode
EgorBo marked this conversation as resolved.
Show resolved Hide resolved
return !compDbgCode;
}
return !OptimizationDisabled();
}

Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/jit/importer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3791,7 +3791,7 @@ void Compiler::impImportAndPushBox(CORINFO_RESOLVED_TOKEN* pResolvedToken)
// and the other you get
// *(temp+4) = expr

if (opts.OptimizationDisabled())
if (!opts.OptimizationEnabled(OPT_Lightweight))
{
// For minopts/debug code, try and minimize the total number
// of box temps by reusing an existing temp when possible.
Expand Down
29 changes: 28 additions & 1 deletion src/coreclr/jit/importercalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2476,14 +2476,41 @@ GenTree* Compiler::impIntrinsic(GenTree* newobjThis,
break;
}

// Allow some lighweight intrinsics in Tier0 which can improve throughput
// we introduced betterToExpand here because we're fine if intrinsic decides to not expand itself
// in this case unlike mustExpand.
bool betterToExpand = mustExpand;
if (!mustExpand && opts.OptimizationEnabled(OPT_Lightweight))
{
switch (ni)
{
// HasFlag allocates in Tier0 and produces a fairly large IR tree
case NI_System_Enum_HasFlag:

// This one is just `return true/false`
case NI_System_Runtime_CompilerServices_RuntimeHelpers_IsKnownConstant:

// This is faster to expand than emitting get_Length call
case NI_System_String_get_Length:

betterToExpand = true;
break;

default:
// Unsafe.* are all small enough to prefer expansions.
betterToExpand = ni >= NI_SRCS_UNSAFE_START && ni <= NI_SRCS_UNSAFE_END;
break;
}
}

GenTree* retNode = nullptr;

// Under debug and minopts, only expand what is required.
// NextCallReturnAddress intrinsic returns the return address of the next call.
// If that call is an intrinsic and is expanded, codegen for NextCallReturnAddress will fail.
// To avoid that we conservatively expand only required intrinsics in methods that call
// the NextCallReturnAddress intrinsic.
if (!mustExpand && (opts.OptimizationDisabled() || info.compHasNextCallRetAddr))
if (!betterToExpand && !mustExpand && (opts.OptimizationDisabled() || info.compHasNextCallRetAddr))
EgorBo marked this conversation as resolved.
Show resolved Hide resolved
{
*pIntrinsicName = NI_Illegal;
return retNode;
Expand Down