-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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: removed unneeded throw helper blocks #95379
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c995902
JIT: removed unneeded throw helper blocks
AndyAyersMS 3a172aa
add missing cases
AndyAyersMS e2c1d62
restrict fix to certain targets
AndyAyersMS cd54f6e
be more selective in scanning for helper uses
AndyAyersMS 1c2a9d2
add hash lookup; remove old cache
AndyAyersMS 45a8db5
unblock on mac arm64
AndyAyersMS 9e4c8cb
review feedback
AndyAyersMS File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,9 +14,9 @@ StackLevelSetter::StackLevelSetter(Compiler* compiler) | |
, maxStackLevel(0) | ||
, memAllocator(compiler->getAllocator(CMK_CallArgs)) | ||
, putArgNumSlots(memAllocator) | ||
, throwHelperBlocksUsed(comp->fgUseThrowHelperBlocks() && comp->compUsesThrowHelper) | ||
#if !FEATURE_FIXED_OUT_ARGS | ||
, framePointerRequired(compiler->codeGen->isFramePointerRequired()) | ||
, throwHelperBlocksUsed(comp->fgUseThrowHelperBlocks() && comp->compUsesThrowHelper) | ||
#endif // !FEATURE_FIXED_OUT_ARGS | ||
{ | ||
// The constructor reads this value to skip iterations that could set it if it is already set. | ||
|
@@ -59,8 +59,29 @@ PhaseStatus StackLevelSetter::DoPhase() | |
comp->fgSetPtrArgCntMax(maxStackLevel); | ||
CheckArgCnt(); | ||
|
||
// Might want an "other" category for things like this... | ||
return PhaseStatus::MODIFIED_NOTHING; | ||
// When optimizing, check if there are any unused throw helper blocks, | ||
// and if so, remove them. | ||
// | ||
bool madeChanges = false; | ||
|
||
if (comp->opts.OptimizationEnabled()) | ||
{ | ||
for (Compiler::AddCodeDsc* add = comp->fgGetAdditionalCodeDescriptors(); add != nullptr; add = add->acdNext) | ||
{ | ||
if (add->acdUsed) | ||
{ | ||
continue; | ||
} | ||
|
||
BasicBlock* const block = add->acdDstBlk; | ||
JITDUMP("Throw help block " FMT_BB " is unused\n", block->bbNum); | ||
block->bbFlags &= ~BBF_DONT_REMOVE; | ||
comp->fgRemoveBlock(block, /* unreachable */ true); | ||
madeChanges = true; | ||
} | ||
} | ||
|
||
return madeChanges ? PhaseStatus::MODIFIED_EVERYTHING : PhaseStatus::MODIFIED_NOTHING; | ||
} | ||
|
||
//------------------------------------------------------------------------ | ||
|
@@ -71,6 +92,9 @@ PhaseStatus StackLevelSetter::DoPhase() | |
// Nodes in blocks are iterated in the reverse order to memorize GT_PUTARG_STK | ||
// and GT_PUTARG_SPLIT stack sizes. | ||
// | ||
// Also note which (if any) throw helper blocks might end up being used by | ||
// codegen. | ||
// | ||
// Arguments: | ||
// block - the block to process. | ||
// | ||
|
@@ -89,17 +113,6 @@ void StackLevelSetter::ProcessBlock(BasicBlock* block) | |
SubStackLevel(numSlots); | ||
} | ||
|
||
#if !FEATURE_FIXED_OUT_ARGS | ||
// Set throw blocks incoming stack depth for x86. | ||
if (throwHelperBlocksUsed && !framePointerRequired) | ||
{ | ||
if (node->OperMayThrow(comp)) | ||
{ | ||
SetThrowHelperBlocks(node, block); | ||
} | ||
} | ||
#endif // !FEATURE_FIXED_OUT_ARGS | ||
|
||
if (node->IsCall()) | ||
{ | ||
GenTreeCall* call = node->AsCall(); | ||
|
@@ -108,11 +121,45 @@ void StackLevelSetter::ProcessBlock(BasicBlock* block) | |
call->gtArgs.SetStkSizeBytes(usedStackSlotsCount * TARGET_POINTER_SIZE); | ||
#endif // UNIX_X86_ABI | ||
} | ||
|
||
if (!throwHelperBlocksUsed) | ||
{ | ||
continue; | ||
} | ||
|
||
// When optimizing we want to know what throw helpers might be used | ||
// so we can remove the ones that aren't needed. | ||
// | ||
// If we're not optimizing then the helper requests made in | ||
// morph are likely still accurate, so we don't bother checking | ||
// if helpers are indeed used. | ||
// | ||
bool checkForHelpers = comp->opts.OptimizationEnabled(); | ||
|
||
#if !FEATURE_FIXED_OUT_ARGS | ||
// Even if not optimizing, if we have a moving SP frame, a shared helper may | ||
// be reached with mixed stack depths and so force this method to use | ||
// a frame pointer. Once we see that the method will need a frame | ||
// pointer we no longer need to check for this case. | ||
// | ||
checkForHelpers |= !framePointerRequired; | ||
#endif | ||
|
||
if (checkForHelpers) | ||
{ | ||
if (((node->gtFlags & GTF_EXCEPT) != 0) && node->OperMayThrow(comp)) | ||
{ | ||
SetThrowHelperBlocks(node, block); | ||
} | ||
else | ||
{ | ||
// assert(((node->gtFlags & GTF_CALL) != 0) || !node->OperMayThrow(comp)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove or enable |
||
} | ||
} | ||
} | ||
assert(currentStackLevel == 0); | ||
} | ||
|
||
#if !FEATURE_FIXED_OUT_ARGS | ||
//------------------------------------------------------------------------ | ||
// SetThrowHelperBlocks: Set throw helper blocks incoming stack levels targeted | ||
// from the node. | ||
|
@@ -147,6 +194,25 @@ void StackLevelSetter::SetThrowHelperBlocks(GenTree* node, BasicBlock* block) | |
SetThrowHelperBlock(SCK_ARITH_EXCPN, block); | ||
break; | ||
|
||
#if defined(TARGET_ARM64) || defined(TARGET_ARM) || defined(TARGET_LOONGARCH64) || defined(TARGET_RISCV64) | ||
case GT_DIV: | ||
case GT_UDIV: | ||
{ | ||
ExceptionSetFlags exSetFlags = node->OperExceptions(comp); | ||
|
||
if ((exSetFlags & ExceptionSetFlags::DivideByZeroException) != ExceptionSetFlags::None) | ||
{ | ||
SetThrowHelperBlock(SCK_DIV_BY_ZERO, block); | ||
} | ||
|
||
if ((exSetFlags & ExceptionSetFlags::ArithmeticException) != ExceptionSetFlags::None) | ||
{ | ||
SetThrowHelperBlock(SCK_ARITH_EXCPN, block); | ||
} | ||
} | ||
break; | ||
#endif | ||
|
||
default: // Other opers can target throw only due to overflow. | ||
break; | ||
} | ||
|
@@ -171,6 +237,11 @@ void StackLevelSetter::SetThrowHelperBlock(SpecialCodeKind kind, BasicBlock* blo | |
{ | ||
Compiler::AddCodeDsc* add = comp->fgFindExcptnTarget(kind, comp->bbThrowIndex(block)); | ||
assert(add != nullptr); | ||
// We expect we'll actually need this helper. | ||
add->acdUsed = true; | ||
|
||
#if !FEATURE_FIXED_OUT_ARGS | ||
|
||
if (add->acdStkLvlInit) | ||
{ | ||
// If different range checks happen at different stack levels, | ||
|
@@ -217,9 +288,8 @@ void StackLevelSetter::SetThrowHelperBlock(SpecialCodeKind kind, BasicBlock* blo | |
#endif // Debug | ||
add->acdStkLvl = currentStackLevel; | ||
} | ||
} | ||
|
||
#endif // !FEATURE_FIXED_OUT_ARGS | ||
} | ||
|
||
//------------------------------------------------------------------------ | ||
// PopArgumentsFromCall: Calculate the number of stack arguments that are used by the call. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor, but maybe you should follow the pattern set by
GetBlockToEHPreds
and many similar functions, to have a:Instead of having the allocation within
fgAddCodeRef