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

[RyuJIT] Import String.Empty as CNS_STR #44847

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1393,6 +1393,8 @@ bool Compiler::s_dspMemStats = false;
const bool Compiler::Options::compNoPInvokeInlineCB = false;
#endif

unsigned Compiler::s_emptyStringSconCPX = 0;

/*****************************************************************************
*
* One time initialization code
Expand Down
2 changes: 2 additions & 0 deletions src/coreclr/jit/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -9491,6 +9491,8 @@ XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

//-------------------------- Global Compiler Data ------------------------------------

static unsigned s_emptyStringSconCPX;

#ifdef DEBUG
private:
static LONG s_compMethodsCount; // to produce unique label names
Expand Down
13 changes: 10 additions & 3 deletions src/coreclr/jit/importer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14842,9 +14842,16 @@ void Compiler::impImportBlockCode(BasicBlock* block)
{
assert(aflags & CORINFO_ACCESS_GET);

LPVOID pValue;
InfoAccessType iat = info.compCompHnd->emptyStringLiteral(&pValue);
op1 = gtNewStringLiteralNode(iat, pValue);
if (s_emptyStringSconCPX != 0)
{
op1 = gtNewSconNode(s_emptyStringSconCPX, info.compCompHnd->getClassModule(impGetObjectClass()));
}
else
{
LPVOID pValue;
InfoAccessType iat = info.compCompHnd->emptyStringLiteral(&pValue);
op1 = gtNewStringLiteralNode(iat, pValue);
}
goto FIELD_DONE;
}
break;
Expand Down
28 changes: 21 additions & 7 deletions src/coreclr/jit/morph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9270,38 +9270,52 @@ GenTree* Compiler::fgMorphConst(GenTree* tree)
return tree;
}

CORINFO_MODULE_HANDLE scpHnd = tree->AsStrCon()->gtScpHnd;
unsigned sconCPX = tree->AsStrCon()->gtSconCPX;

// TODO-CQ: Do this for compCurBB->isRunRarely(). Doing that currently will
// guarantee slow performance for that block. Instead cache the return value
// of CORINFO_HELP_STRCNS and go to cache first giving reasonable perf.

if (compCurBB->bbJumpKind == BBJ_THROW)
{
CorInfoHelpFunc helper = info.compCompHnd->getLazyStringLiteralHelper(tree->AsStrCon()->gtScpHnd);
CorInfoHelpFunc helper = info.compCompHnd->getLazyStringLiteralHelper(scpHnd);
if (helper != CORINFO_HELP_UNDEF)
{
// For un-important blocks, we want to construct the string lazily

GenTreeCall::Use* args;
if (helper == CORINFO_HELP_STRCNS_CURRENT_MODULE)
{
args = gtNewCallArgs(gtNewIconNode(RidFromToken(tree->AsStrCon()->gtSconCPX), TYP_INT));
args = gtNewCallArgs(gtNewIconNode(RidFromToken(sconCPX), TYP_INT));
}
else
{
args = gtNewCallArgs(gtNewIconNode(RidFromToken(tree->AsStrCon()->gtSconCPX), TYP_INT),
gtNewIconEmbScpHndNode(tree->AsStrCon()->gtScpHnd));
args = gtNewCallArgs(gtNewIconNode(RidFromToken(sconCPX), TYP_INT),
gtNewIconEmbScpHndNode(scpHnd));
}

tree = gtNewHelperCallNode(helper, TYP_REF, args);
return fgMorphTree(tree);
}
}

assert(tree->AsStrCon()->gtScpHnd == info.compScopeHnd || !IsUninitialized(tree->AsStrCon()->gtScpHnd));
assert(scpHnd == info.compScopeHnd || !IsUninitialized(scpHnd));

// Save SconCPX to s_emptyStringSconCPX if it's "" (empty string).
if ((s_emptyStringSconCPX == 0) &&
(scpHnd == info.compCompHnd->getClassModule(impGetObjectClass())))
{
int length = -1;
info.compCompHnd->getStringLiteral(scpHnd, sconCPX, &length);
if (length == 0)
{
s_emptyStringSconCPX = sconCPX;
}
}

LPVOID pValue;
InfoAccessType iat =
info.compCompHnd->constructStringLiteral(tree->AsStrCon()->gtScpHnd, tree->AsStrCon()->gtSconCPX, &pValue);
InfoAccessType iat = info.compCompHnd->constructStringLiteral(scpHnd, sconCPX, &pValue);

tree = gtNewStringLiteralNode(iat, pValue);

Expand Down