Skip to content
Closed
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
44 changes: 14 additions & 30 deletions src/coreclr/jit/runtimelookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,6 @@ PhaseStatus Compiler::fgExpandRuntimeLookups()
}

GenTree* ctxTree = call->gtArgs.GetArgByIndex(0)->GetNode();
GenTree* sigNode = call->gtArgs.GetArgByIndex(1)->GetNode();

// Prepare slotPtr tree (TODO: consider sharing this part with impRuntimeLookup)
GenTree* slotPtrTree = gtCloneExpr(ctxTree);
Expand Down Expand Up @@ -275,11 +274,11 @@ PhaseStatus Compiler::fgExpandRuntimeLookups()
// if (*fastPathValue == null)
// goto fallbackBb;
//
// fastPathBb(BBJ_ALWAYS): [weight: 0.8]
// fastPathBb(BBJ_ALWAYS): [weight: 1.0]
// rtLookupLcl = *fastPathValue;
// goto block;
//
// fallbackBb(BBJ_NONE): [weight: 0.2]
// fallbackBb(BBJ_NONE): [weight: 0.0]
// rtLookupLcl = HelperCall();
//
// block(...): [weight: 1.0]
Expand Down Expand Up @@ -320,15 +319,15 @@ PhaseStatus Compiler::fgExpandRuntimeLookups()
// goto fallbackBb;
// ...
//
// nullcheckBb(BBJ_COND): [weight: 0.8]
// nullcheckBb(BBJ_COND): [weight: 1.0]
// if (*fastPathValue == null)
// goto fallbackBb;
//
// fastPathBb(BBJ_ALWAYS): [weight: 0.64]
// fastPathBb(BBJ_ALWAYS): [weight: 1.0]
// rtLookupLcl = *fastPathValue;
// goto block;
//
// fallbackBb(BBJ_NONE): [weight: 0.36]
// fallbackBb(BBJ_NONE): [weight: 0.0]
// rtLookupLcl = HelperCall();
//
// block(...): [weight: 1.0]
Expand Down Expand Up @@ -387,27 +386,15 @@ PhaseStatus Compiler::fgExpandRuntimeLookups()

//
// Re-distribute weights (see '[weight: X]' on the diagrams above)
// TODO: consider marking fallbackBb as rarely-taken
//
block->inheritWeight(prevBb);
if (needsSizeCheck)
{
sizeCheckBb->inheritWeight(prevBb);
// 80% chance we pass nullcheck
nullcheckBb->inheritWeightPercentage(sizeCheckBb, 80);
// 64% (0.8 * 0.8) chance we pass both nullcheck and sizecheck
fastPathBb->inheritWeightPercentage(nullcheckBb, 80);
// 100-64=36% chance we fail either nullcheck or sizecheck
fallbackBb->inheritWeightPercentage(sizeCheckBb, 36);
}
else
{
nullcheckBb->inheritWeight(prevBb);
// 80% chance we pass nullcheck
fastPathBb->inheritWeightPercentage(nullcheckBb, 80);
// 20% chance we fail nullcheck (TODO: Consider making it cold (0%))
fallbackBb->inheritWeightPercentage(nullcheckBb, 20);
}
nullcheckBb->inheritWeight(prevBb);
fastPathBb->inheritWeight(prevBb);
fallbackBb->bbSetRunRarely();

//
// Update loop info if loop table is known to be valid
Expand Down Expand Up @@ -437,6 +424,12 @@ PhaseStatus Compiler::fgExpandRuntimeLookups()
assert(BasicBlock::sameEHRegion(prevBb, sizeCheckBb));
}

// Merge prevBb with nullcheckBb (or sizeBb) if possible to simplify layout
if (fgCanCompactBlocks(prevBb, prevBb->GetUniqueSucc()))
{
fgCompactBlocks(prevBb, prevBb->GetUniqueSucc());
}

// Scan current block again, the current call will be ignored because of ClearExpRuntimeLookup.
// We don't try to re-use expansions for the same lookups in the current block here - CSE is responsible
// for that
Expand All @@ -447,14 +440,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;
}