Skip to content
Closed
Changes from 1 commit
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
Next Next commit
Re-order some blocks in runtimelookup
  • Loading branch information
EgorBo committed Mar 15, 2023
commit 889424325931da53d84710adae699ab7cb371c90
39 changes: 15 additions & 24 deletions src/coreclr/jit/runtimelookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,12 +239,12 @@ PhaseStatus Compiler::fgExpandRuntimeLookups()
// if (*fastPathValue == null)
// goto fallbackBb;
//
// fastPathBb(BBJ_ALWAYS): [weight: 0.8]
// rtLookupLcl = *fastPathValue;
// fallbackBb(BBJ_ALWAYS): [weight: 0.2]
// rtLookupLcl = HelperCall();
// goto block;
//
// fallbackBb(BBJ_NONE): [weight: 0.2]
// rtLookupLcl = HelperCall();
// fastPathBb(BBJ_NONE): [weight: 0.8]
// rtLookupLcl = *fastPathValue;
//
// block(...): [weight: 1.0]
// use(rtLookupLcl);
Expand All @@ -262,15 +262,14 @@ PhaseStatus Compiler::fgExpandRuntimeLookups()
CreateBlockFromTree(this, prevBb, BBJ_COND, gtNewOperNode(GT_JTRUE, TYP_VOID, nullcheckOp),
debugInfo);

// Fast-path basic block
GenTree* asgFastpathValue = gtNewAssignNode(gtClone(rtLookupLcl), fastPathValueClone);
BasicBlock* fastPathBb = CreateBlockFromTree(this, nullcheckBb, BBJ_NONE, asgFastpathValue, debugInfo);

// Fallback basic block
GenTree* asgFallbackValue = gtNewAssignNode(gtClone(rtLookupLcl), call);
BasicBlock* fallbackBb =
CreateBlockFromTree(this, nullcheckBb, BBJ_NONE, asgFallbackValue, debugInfo, true);

// Fast-path basic block
GenTree* asgFastpathValue = gtNewAssignNode(gtClone(rtLookupLcl), fastPathValueClone);
BasicBlock* fastPathBb =
CreateBlockFromTree(this, nullcheckBb, BBJ_ALWAYS, asgFastpathValue, debugInfo);
CreateBlockFromTree(this, nullcheckBb, BBJ_ALWAYS, asgFallbackValue, debugInfo, true);

BasicBlock* sizeCheckBb = nullptr;
if (needsSizeCheck)
Expand All @@ -288,12 +287,12 @@ PhaseStatus Compiler::fgExpandRuntimeLookups()
// if (*fastPathValue == null)
// goto fallbackBb;
//
// fastPathBb(BBJ_ALWAYS): [weight: 0.64]
// rtLookupLcl = *fastPathValue;
// fallbackBb(BBJ_ALWAYS): [weight: 0.36]
// rtLookupLcl = HelperCall();
// goto block;
//
// fallbackBb(BBJ_NONE): [weight: 0.36]
// rtLookupLcl = HelperCall();
// fastPathBb(BBJ_NONE): [weight: 0.64]
// rtLookupLcl = *fastPathValue;
//
// block(...): [weight: 1.0]
// use(rtLookupLcl);
Expand Down Expand Up @@ -322,8 +321,8 @@ PhaseStatus Compiler::fgExpandRuntimeLookups()
fgRemoveRefPred(block, prevBb);
fgAddRefPred(block, fastPathBb);
fgAddRefPred(block, fallbackBb);
nullcheckBb->bbJumpDest = fallbackBb;
fastPathBb->bbJumpDest = block;
nullcheckBb->bbJumpDest = fastPathBb;
fallbackBb->bbJumpDest = block;
Copy link
Contributor

@kunalspathak kunalspathak Mar 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if you mark fallBackBb->bbSetRunRarely()? I had to do similar thing to re-arrange in #80297.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

G_M10050_IG01:
       sub      rsp, 40
                                                ;; size=4 bbWeight=1 PerfScore 0.25
G_M10050_IG02:
       mov      rcx, qword ptr GS:[0x0058]
       mov      rcx, qword ptr [rcx+30H]
       mov      edx, dword ptr [rcx+48H]
       cmp      edx, 2
       jl       SHORT G_M10050_IG04
                                                ;; size=21 bbWeight=1 PerfScore 6.25
G_M10050_IG03:
       mov      rcx, qword ptr [rcx+50H]
       cmp      qword ptr [rcx+10H], 0
       jne      SHORT G_M10050_IG05
                                                ;; size=11 bbWeight=0.90 PerfScore 5.42
G_M10050_IG04:
       mov      rcx, 0xD1FFAB1E
       mov      edx, 29
       mov      r8d, 2
       call     CORINFO_HELP_GETSHARED_NONGCTHREADSTATIC_BASE_NOCTOR_OPTIMIZED
       mov      qword ptr [rsp+20H], rax
                                                ;; size=31 bbWeight=0.09 PerfScore 0.25
G_M10050_IG05:
       mov      rax, qword ptr [rsp+20H]
       mov      eax, dword ptr [rax+8CH]
                                                ;; size=11 bbWeight=1 PerfScore 3.00
G_M10050_IG06:
       add      rsp, 40
       ret
                                                ;; size=5 bbWeight=1 PerfScore 1.25

vs.

G_M10050_IG01:
       sub      rsp, 40
                                                ;; size=4 bbWeight=1 PerfScore 0.25
G_M10050_IG02:
       mov      rax, qword ptr GS:[0x0058]
       mov      rax, qword ptr [rax+30H]
       mov      ecx, dword ptr [rax+48H]
       cmp      ecx, 2
       jl       SHORT G_M10050_IG06
                                                ;; size=21 bbWeight=1 PerfScore 6.25
G_M10050_IG03:
       mov      rax, qword ptr [rax+50H]
       cmp      qword ptr [rax+10H], 0
       je       SHORT G_M10050_IG06
                                                ;; size=11 bbWeight=0.90 PerfScore 5.42
G_M10050_IG04:
       mov      r9, qword ptr [rsp+20H]
       mov      eax, dword ptr [r9+8CH]
                                                ;; size=12 bbWeight=1 PerfScore 3.00
G_M10050_IG05:
       add      rsp, 40
       ret
                                                ;; size=5 bbWeight=1 PerfScore 1.25
G_M10050_IG06:
       mov      rcx, 0xD1FFAB1E
       mov      edx, 29
       mov      r8d, 2
       call     CORINFO_HELP_GETSHARED_NONGCTHREADSTATIC_BASE_NOCTOR_OPTIMIZED
       mov      r9, rax
       mov      qword ptr [rsp+20H], r9
       jmp      SHORT G_M10050_IG04
                                                ;; size=36 bbWeight=0 PerfScore 0.00


if (needsSizeCheck)
{
Expand Down Expand Up @@ -412,13 +411,5 @@ PhaseStatus Compiler::fgExpandRuntimeLookups()
}
}

if (result == PhaseStatus::MODIFIED_EVERYTHING)
{
if (opts.OptimizationEnabled())
{
fgReorderBlocks(/* useProfileData */ false);
fgUpdateChangedFlowGraph(FlowGraphUpdates::COMPUTE_BASICS);
}
}
Comment on lines -451 to -458
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

W.r.t. the confusing diffs, maybe try with this change reverted?

return result;
}