-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Move vzeroupper emit back to JIT #115748
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
Merged
Merged
Move vzeroupper emit back to JIT #115748
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -4879,7 +4879,8 @@ void CodeGen::genCodeForShift(GenTree* tree) | |
| { | ||
| int shiftByValue = (int)shiftBy->AsIntConCommon()->IconValue(); | ||
|
|
||
| if (tree->OperIsRotate() && compiler->compOpportunisticallyDependsOn(InstructionSet_BMI2) && !tree->gtSetFlags()) | ||
| if (tree->OperIsRotate() && compiler->compOpportunisticallyDependsOn(InstructionSet_BMI2) && | ||
|
Member
Author
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. Changes from jit format |
||
| !tree->gtSetFlags()) | ||
| { | ||
| // If we have a contained source operand, we must emit rorx. | ||
| // We may also use rorx for 64bit values when a mov would otherwise be required, | ||
|
|
@@ -4906,7 +4907,8 @@ void CodeGen::genCodeForShift(GenTree* tree) | |
| return; | ||
| } | ||
| } | ||
| else if (tree->OperIsShift() && compiler->compOpportunisticallyDependsOn(InstructionSet_BMI2) && !tree->gtSetFlags()) | ||
| else if (tree->OperIsShift() && compiler->compOpportunisticallyDependsOn(InstructionSet_BMI2) && | ||
| !tree->gtSetFlags()) | ||
| { | ||
| // Emit shlx, sarx, shrx if BMI2 is available instead of mov+shl, mov+sar, mov+shr. | ||
| switch (tree->OperGet()) | ||
|
|
@@ -10452,8 +10454,10 @@ void CodeGen::genFnEpilog(BasicBlock* block) | |
| } | ||
| #endif | ||
|
|
||
| genClearAvxStateInEpilog(); | ||
|
|
||
| // Restore float registers that were saved to stack before SP is modified. | ||
| genRestoreCalleeSavedFltRegs(compiler->compLclFrameSize); | ||
| genRestoreCalleeSavedFltRegs(); | ||
tannergooding marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| #ifdef JIT32_GCENCODER | ||
| // When using the JIT32 GC encoder, we do not start the OS-reported portion of the epilog until after | ||
|
|
@@ -10913,6 +10917,8 @@ void CodeGen::genFuncletProlog(BasicBlock* block) | |
|
|
||
| // This is the end of the OS-reported prolog for purposes of unwinding | ||
| compiler->unwindEndProlog(); | ||
|
|
||
| genClearAvxStateInProlog(); | ||
| } | ||
|
|
||
| /***************************************************************************** | ||
|
|
@@ -10933,6 +10939,8 @@ void CodeGen::genFuncletEpilog() | |
|
|
||
| ScopedSetVariable<bool> _setGeneratingEpilog(&compiler->compGeneratingEpilog, true); | ||
|
|
||
| genClearAvxStateInEpilog(); | ||
|
|
||
| inst_RV_IV(INS_add, REG_SPBASE, genFuncletInfo.fiSpDelta, EA_PTRSIZE); | ||
| instGen_Return(0); | ||
| } | ||
|
|
@@ -11030,6 +11038,8 @@ void CodeGen::genFuncletProlog(BasicBlock* block) | |
| // Add a padding for 16-byte alignment | ||
| inst_RV_IV(INS_sub, REG_SPBASE, 12, EA_PTRSIZE); | ||
| #endif | ||
|
|
||
| genClearAvxStateInProlog(); | ||
| } | ||
|
|
||
| /***************************************************************************** | ||
|
|
@@ -11048,6 +11058,8 @@ void CodeGen::genFuncletEpilog() | |
|
|
||
| ScopedSetVariable<bool> _setGeneratingEpilog(&compiler->compGeneratingEpilog, true); | ||
|
|
||
| genClearAvxStateInEpilog(); | ||
|
|
||
| #ifdef UNIX_X86_ABI | ||
| // Revert a padding that was added for 16-byte alignment | ||
| inst_RV_IV(INS_add, REG_SPBASE, 12, EA_PTRSIZE); | ||
|
|
@@ -11337,40 +11349,21 @@ void CodeGen::genZeroInitFrameUsingBlockInit(int untrLclHi, int untrLclLo, regNu | |
| // Save compCalleeFPRegsPushed with the smallest register number saved at [RSP+offset], working | ||
| // down the stack to the largest register number stored at [RSP+offset-(genCountBits(regMask)-1)*XMM_REG_SIZE] | ||
| // Here offset = 16-byte aligned offset after pushing integer registers. | ||
| // | ||
| // Params | ||
| // lclFrameSize - Fixed frame size excluding callee pushed int regs. | ||
| // non-funclet: this will be compLclFrameSize. | ||
| // funclet frames: this will be FuncletInfo.fiSpDelta. | ||
| void CodeGen::genPreserveCalleeSavedFltRegs(unsigned lclFrameSize) | ||
| void CodeGen::genPreserveCalleeSavedFltRegs() | ||
| { | ||
| regMaskTP regMask = compiler->compCalleeFPRegsSavedMask; | ||
|
|
||
| // Only callee saved floating point registers should be in regMask | ||
| assert((regMask & RBM_FLT_CALLEE_SAVED) == regMask); | ||
|
|
||
| if (GetEmitter()->ContainsCallNeedingVzeroupper() && !GetEmitter()->Contains256bitOrMoreAVX()) | ||
| { | ||
| // The Intel optimization manual guidance in `3.11.5.3 Fixing Instruction Slowdowns` states: | ||
| // Insert a VZEROUPPER to tell the hardware that the state of the higher registers is clean | ||
| // between the VEX and the legacy SSE instructions. Often the best way to do this is to insert a | ||
| // VZEROUPPER before returning from any function that uses VEX (that does not produce a VEX | ||
| // register) and before any call to an unknown function. | ||
|
|
||
| // This method contains a call that needs vzeroupper but also doesn't use 256-bit or higher | ||
| // AVX itself. Thus we can optimize to only emitting a single vzeroupper in the function prologue | ||
| // This reduces the overall amount of codegen, particularly for more common paths not using any | ||
| // SIMD or floating-point. | ||
|
|
||
| instGen(INS_vzeroupper); | ||
| } | ||
|
|
||
| // fast path return | ||
| if (regMask == RBM_NONE) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| unsigned lclFrameSize = compiler->compLclFrameSize; | ||
|
|
||
| #ifdef TARGET_AMD64 | ||
| unsigned firstFPRegPadding = compiler->lvaIsCalleeSavedIntRegCountEven() ? REGSIZE_BYTES : 0; | ||
| unsigned offset = lclFrameSize - firstFPRegPadding - XMM_REGSIZE_BYTES; | ||
|
|
@@ -11400,35 +11393,21 @@ void CodeGen::genPreserveCalleeSavedFltRegs(unsigned lclFrameSize) | |
| // Save/Restore compCalleeFPRegsPushed with the smallest register number saved at [RSP+offset], working | ||
| // down the stack to the largest register number stored at [RSP+offset-(genCountBits(regMask)-1)*XMM_REG_SIZE] | ||
| // Here offset = 16-byte aligned offset after pushing integer registers. | ||
| // | ||
| // Params | ||
| // lclFrameSize - Fixed frame size excluding callee pushed int regs. | ||
| // non-funclet: this will be compLclFrameSize. | ||
| // funclet frames: this will be FuncletInfo.fiSpDelta. | ||
| void CodeGen::genRestoreCalleeSavedFltRegs(unsigned lclFrameSize) | ||
| void CodeGen::genRestoreCalleeSavedFltRegs() | ||
| { | ||
| regMaskTP regMask = compiler->compCalleeFPRegsSavedMask; | ||
|
|
||
| // Only callee saved floating point registers should be in regMask | ||
| assert((regMask & RBM_FLT_CALLEE_SAVED) == regMask); | ||
|
|
||
| if (GetEmitter()->Contains256bitOrMoreAVX()) | ||
| { | ||
| // The Intel optimization manual guidance in `3.11.5.3 Fixing Instruction Slowdowns` states: | ||
| // Insert a VZEROUPPER to tell the hardware that the state of the higher registers is clean | ||
| // between the VEX and the legacy SSE instructions. Often the best way to do this is to insert a | ||
| // VZEROUPPER before returning from any function that uses VEX (that does not produce a VEX | ||
| // register) and before any call to an unknown function. | ||
|
|
||
| instGen(INS_vzeroupper); | ||
| } | ||
|
|
||
| // fast path return | ||
| if (regMask == RBM_NONE) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| unsigned lclFrameSize = compiler->compLclFrameSize; | ||
|
|
||
| #ifdef TARGET_AMD64 | ||
| unsigned firstFPRegPadding = compiler->lvaIsCalleeSavedIntRegCountEven() ? REGSIZE_BYTES : 0; | ||
| instruction copyIns = ins_Copy(TYP_FLOAT); | ||
|
|
@@ -11470,6 +11449,45 @@ void CodeGen::genRestoreCalleeSavedFltRegs(unsigned lclFrameSize) | |
| } | ||
| } | ||
|
|
||
| //----------------------------------------------------------------------------------- | ||
| // genClearAvxStateInProlog: Generate vzeroupper instruction to clear AVX state if necessary in a prolog | ||
| // | ||
| void CodeGen::genClearAvxStateInProlog() | ||
| { | ||
| if (GetEmitter()->ContainsCallNeedingVzeroupper() && !GetEmitter()->Contains256bitOrMoreAVX()) | ||
tannergooding marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| // The Intel optimization manual guidance in `3.11.5.3 Fixing Instruction Slowdowns` states: | ||
| // Insert a VZEROUPPER to tell the hardware that the state of the higher registers is clean | ||
| // between the VEX and the legacy SSE instructions. Often the best way to do this is to insert a | ||
| // VZEROUPPER before returning from any function that uses VEX (that does not produce a VEX | ||
| // register) and before any call to an unknown function. | ||
|
|
||
| // This method contains a call that needs vzeroupper but also doesn't use 256-bit or higher | ||
| // AVX itself. Thus we can optimize to only emitting a single vzeroupper in the function prologue | ||
| // This reduces the overall amount of codegen, particularly for more common paths not using any | ||
| // SIMD or floating-point. | ||
|
|
||
| instGen(INS_vzeroupper); | ||
| } | ||
| } | ||
|
|
||
| //----------------------------------------------------------------------------------- | ||
| // genClearAvxStateInEpilog: Generate vzeroupper instruction to clear AVX state if necessary in an epilog | ||
| // | ||
| void CodeGen::genClearAvxStateInEpilog() | ||
| { | ||
| if (GetEmitter()->Contains256bitOrMoreAVX()) | ||
| { | ||
| // The Intel optimization manual guidance in `3.11.5.3 Fixing Instruction Slowdowns` states: | ||
| // Insert a VZEROUPPER to tell the hardware that the state of the higher registers is clean | ||
| // between the VEX and the legacy SSE instructions. Often the best way to do this is to insert a | ||
| // VZEROUPPER before returning from any function that uses VEX (that does not produce a VEX | ||
| // register) and before any call to an unknown function. | ||
|
|
||
| instGen(INS_vzeroupper); | ||
| } | ||
| } | ||
|
|
||
| //----------------------------------------------------------------------------------- | ||
| // instGen_MemoryBarrier: Emit a MemoryBarrier instruction | ||
| // | ||
|
|
||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.