Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c836f5a

Browse files
authored
Make FailFast blocks cold (GS cookies) (#93429)
1 parent bf10b61 commit c836f5a

File tree

10 files changed

+29
-33
lines changed

10 files changed

+29
-33
lines changed

src/coreclr/jit/codegenarmarch.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -631,11 +631,8 @@ void CodeGen::genEmitGSCookieCheck(bool pushReg)
631631
// Compare with the GC cookie constant
632632
GetEmitter()->emitIns_R_R(INS_cmp, EA_PTRSIZE, regGSConst, regGSValue);
633633

634-
BasicBlock* gsCheckBlk = genCreateTempLabel();
635-
inst_JMP(EJ_eq, gsCheckBlk);
636-
// regGSConst and regGSValue aren't needed anymore, we can use them for helper call
637-
genEmitHelperCall(CORINFO_HELP_FAIL_FAST, 0, EA_UNKNOWN, regGSConst);
638-
genDefineTempLabel(gsCheckBlk);
634+
Compiler::AddCodeDsc* codeDsc = compiler->fgFindExcptnTarget(SpecialCodeKind::SCK_FAIL_FAST, 0);
635+
inst_JMP(EJ_ne, codeDsc->acdDstBlk);
639636
}
640637

641638
//---------------------------------------------------------------------

src/coreclr/jit/codegenloongarch64.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5192,12 +5192,8 @@ void CodeGen::genEmitGSCookieCheck(bool pushReg)
51925192
GetEmitter()->emitIns_R_S(INS_ld_d, EA_PTRSIZE, regGSValue, compiler->lvaGSSecurityCookie, 0);
51935193

51945194
// Compare with the GC cookie constant
5195-
BasicBlock* gsCheckBlk = genCreateTempLabel();
5196-
GetEmitter()->emitIns_J_cond_la(INS_beq, gsCheckBlk, regGSConst, regGSValue);
5197-
5198-
// regGSConst and regGSValue aren't needed anymore, we can use them for helper call
5199-
genEmitHelperCall(CORINFO_HELP_FAIL_FAST, 0, EA_UNKNOWN, regGSConst);
5200-
genDefineTempLabel(gsCheckBlk);
5195+
Compiler::AddCodeDsc* codeDsc = compiler->fgFindExcptnTarget(SpecialCodeKind::SCK_FAIL_FAST, 0);
5196+
GetEmitter()->emitIns_J_cond_la(INS_bne, codeDsc->acdDstBlk, regGSConst, regGSValue);
52015197
}
52025198

52035199
//---------------------------------------------------------------------

src/coreclr/jit/codegenriscv64.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4958,12 +4958,8 @@ void CodeGen::genEmitGSCookieCheck(bool pushReg)
49584958
GetEmitter()->emitIns_R_S(INS_ld, EA_PTRSIZE, regGSValue, compiler->lvaGSSecurityCookie, 0);
49594959

49604960
// Compare with the GC cookie constant
4961-
BasicBlock* gsCheckBlk = genCreateTempLabel();
4962-
GetEmitter()->emitIns_J_cond_la(INS_beq, gsCheckBlk, regGSConst, regGSValue);
4963-
4964-
// regGSConst and regGSValue aren't needed anymore, we can use them for helper call
4965-
genEmitHelperCall(CORINFO_HELP_FAIL_FAST, 0, EA_UNKNOWN, regGSConst);
4966-
genDefineTempLabel(gsCheckBlk);
4961+
Compiler::AddCodeDsc* codeDsc = compiler->fgFindExcptnTarget(SpecialCodeKind::SCK_FAIL_FAST, 0);
4962+
GetEmitter()->emitIns_J_cond_la(INS_bne, codeDsc->acdDstBlk, regGSConst, regGSValue);
49674963
}
49684964

49694965
//---------------------------------------------------------------------

src/coreclr/jit/codegenxarch.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -195,11 +195,8 @@ void CodeGen::genEmitGSCookieCheck(bool pushReg)
195195
GetEmitter()->emitIns_S_R(INS_cmp, EA_PTRSIZE, regGSCheck, compiler->lvaGSSecurityCookie, 0);
196196
}
197197

198-
BasicBlock* gsCheckBlk = genCreateTempLabel();
199-
inst_JMP(EJ_je, gsCheckBlk);
200-
genEmitHelperCall(CORINFO_HELP_FAIL_FAST, 0, EA_UNKNOWN);
201-
genDefineTempLabel(gsCheckBlk);
202-
198+
Compiler::AddCodeDsc* codeDsc = compiler->fgFindExcptnTarget(SpecialCodeKind::SCK_FAIL_FAST, 0);
199+
inst_JMP(EJ_jne, codeDsc->acdDstBlk);
203200
genPopRegs(pushedRegs, byrefPushedRegs, norefPushedRegs);
204201
}
205202

src/coreclr/jit/compiler.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6117,7 +6117,6 @@ class Compiler
61176117

61186118
AddCodeDsc* fgAddCodeList;
61196119
bool fgAddCodeModf;
6120-
bool fgRngChkThrowAdded;
61216120
AddCodeDsc* fgExcptnTargetCache[SCK_COUNT];
61226121

61236122
BasicBlock* fgRngChkTarget(BasicBlock* block, SpecialCodeKind kind);

src/coreclr/jit/compiler.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3095,6 +3095,7 @@ inline bool Compiler::fgIsThrowHlpBlk(BasicBlock* block)
30953095

30963096
if (!((call->AsCall()->gtCallMethHnd == eeFindHelper(CORINFO_HELP_RNGCHKFAIL)) ||
30973097
(call->AsCall()->gtCallMethHnd == eeFindHelper(CORINFO_HELP_THROWDIVZERO)) ||
3098+
(call->AsCall()->gtCallMethHnd == eeFindHelper(CORINFO_HELP_FAIL_FAST)) ||
30983099
(call->AsCall()->gtCallMethHnd == eeFindHelper(CORINFO_HELP_THROW_ARGUMENTEXCEPTION)) ||
30993100
(call->AsCall()->gtCallMethHnd == eeFindHelper(CORINFO_HELP_THROW_ARGUMENTOUTOFRANGEEXCEPTION)) ||
31003101
(call->AsCall()->gtCallMethHnd == eeFindHelper(CORINFO_HELP_OVERFLOW))))
@@ -3111,7 +3112,7 @@ inline bool Compiler::fgIsThrowHlpBlk(BasicBlock* block)
31113112
if (block == add->acdDstBlk)
31123113
{
31133114
return add->acdKind == SCK_RNGCHK_FAIL || add->acdKind == SCK_DIV_BY_ZERO || add->acdKind == SCK_OVERFLOW ||
3114-
add->acdKind == SCK_ARG_EXCPN || add->acdKind == SCK_ARG_RNG_EXCPN;
3115+
add->acdKind == SCK_ARG_EXCPN || add->acdKind == SCK_ARG_RNG_EXCPN || add->acdKind == SCK_FAIL_FAST;
31153116
}
31163117
}
31173118

@@ -3136,7 +3137,7 @@ inline unsigned Compiler::fgThrowHlpBlkStkLevel(BasicBlock* block)
31363137
// Compute assert cond separately as assert macro cannot have conditional compilation directives.
31373138
bool cond =
31383139
(add->acdKind == SCK_RNGCHK_FAIL || add->acdKind == SCK_DIV_BY_ZERO || add->acdKind == SCK_OVERFLOW ||
3139-
add->acdKind == SCK_ARG_EXCPN || add->acdKind == SCK_ARG_RNG_EXCPN);
3140+
add->acdKind == SCK_ARG_EXCPN || add->acdKind == SCK_ARG_RNG_EXCPN || add->acdKind == SCK_FAIL_FAST);
31403141
assert(cond);
31413142

31423143
// TODO: bbTgtStkDepth is DEBUG-only.

src/coreclr/jit/fgbasic.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,6 @@ void Compiler::fgInit()
109109
/* This global flag is set whenever we remove a statement */
110110
fgStmtRemoved = false;
111111

112-
/* This global flag is set whenever we add a throw block for a RngChk */
113-
fgRngChkThrowAdded = false; /* reset flag for fgIsCodeAdded() */
114-
115112
/* Keep track of whether or not EH statements have been optimized */
116113
fgOptimizedFinally = false;
117114

src/coreclr/jit/flowgraph.cpp

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3584,6 +3584,8 @@ unsigned Compiler::acdHelper(SpecialCodeKind codeKind)
35843584
return CORINFO_HELP_THROWDIVZERO;
35853585
case SCK_ARITH_EXCPN:
35863586
return CORINFO_HELP_OVERFLOW;
3587+
case SCK_FAIL_FAST:
3588+
return CORINFO_HELP_FAIL_FAST;
35873589
default:
35883590
assert(!"Bad codeKind");
35893591
return 0;
@@ -3608,8 +3610,10 @@ BasicBlock* Compiler::fgAddCodeRef(BasicBlock* srcBlk, unsigned refData, Special
36083610
// arg slots on the stack frame if there are no other calls.
36093611
compUsesThrowHelper = true;
36103612

3611-
if (!fgUseThrowHelperBlocks())
3613+
if (!fgUseThrowHelperBlocks() && (kind != SCK_FAIL_FAST))
36123614
{
3615+
// We'll create a throw block in-place then (for better debugging)
3616+
// It's not needed for fail fast, since it's not recoverable anyway.
36133617
return nullptr;
36143618
}
36153619

@@ -3620,6 +3624,7 @@ BasicBlock* Compiler::fgAddCodeRef(BasicBlock* srcBlk, unsigned refData, Special
36203624
BBJ_THROW, // SCK_ARITH_EXCP, SCK_OVERFLOW
36213625
BBJ_THROW, // SCK_ARG_EXCPN
36223626
BBJ_THROW, // SCK_ARG_RNG_EXCPN
3627+
BBJ_THROW, // SCK_FAIL_FAST
36233628
};
36243629

36253630
noway_assert(sizeof(jumpKinds) == SCK_COUNT); // sanity check
@@ -3695,6 +3700,9 @@ BasicBlock* Compiler::fgAddCodeRef(BasicBlock* srcBlk, unsigned refData, Special
36953700
case SCK_ARG_RNG_EXCPN:
36963701
msg = " for ARG_RNG_EXCPN";
36973702
break;
3703+
case SCK_FAIL_FAST:
3704+
msg = " for FAIL_FAST";
3705+
break;
36983706
default:
36993707
msg = " for ??";
37003708
break;
@@ -3712,8 +3720,7 @@ BasicBlock* Compiler::fgAddCodeRef(BasicBlock* srcBlk, unsigned refData, Special
37123720

37133721
/* Remember that we're adding a new basic block */
37143722

3715-
fgAddCodeModf = true;
3716-
fgRngChkThrowAdded = true;
3723+
fgAddCodeModf = true;
37173724

37183725
/* Now figure out what code to insert */
37193726

@@ -3743,6 +3750,10 @@ BasicBlock* Compiler::fgAddCodeRef(BasicBlock* srcBlk, unsigned refData, Special
37433750
helper = CORINFO_HELP_THROW_ARGUMENTOUTOFRANGEEXCEPTION;
37443751
break;
37453752

3753+
case SCK_FAIL_FAST:
3754+
helper = CORINFO_HELP_FAIL_FAST;
3755+
break;
3756+
37463757
default:
37473758
noway_assert(!"unexpected code addition kind");
37483759
return nullptr;
@@ -3759,7 +3770,6 @@ BasicBlock* Compiler::fgAddCodeRef(BasicBlock* srcBlk, unsigned refData, Special
37593770
tree = fgMorphArgs(tree);
37603771

37613772
// Store the tree in the new basic block.
3762-
assert(!srcBlk->isEmpty());
37633773
if (!srcBlk->IsLIR())
37643774
{
37653775
fgInsertStmtAtEnd(newBlk, fgNewStmtFromTree(tree));
@@ -3781,7 +3791,7 @@ BasicBlock* Compiler::fgAddCodeRef(BasicBlock* srcBlk, unsigned refData, Special
37813791

37823792
Compiler::AddCodeDsc* Compiler::fgFindExcptnTarget(SpecialCodeKind kind, unsigned refData)
37833793
{
3784-
assert(fgUseThrowHelperBlocks());
3794+
assert(fgUseThrowHelperBlocks() || (kind == SCK_FAIL_FAST));
37853795
if (!(fgExcptnTargetCache[kind] && // Try the cached value first
37863796
fgExcptnTargetCache[kind]->acdData == refData))
37873797
{

src/coreclr/jit/gentree.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ enum SpecialCodeKind
6060
SCK_OVERFLOW = SCK_ARITH_EXCPN, // target on overflow
6161
SCK_ARG_EXCPN, // target on ArgumentException (currently used only for SIMD intrinsics)
6262
SCK_ARG_RNG_EXCPN, // target on ArgumentOutOfRangeException (currently used only for SIMD intrinsics)
63+
SCK_FAIL_FAST, // target for fail fast exception
6364
SCK_COUNT
6465
};
6566

src/coreclr/jit/gschecks.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ PhaseStatus Compiler::gsPhase()
3030
unsigned const prevBBCount = fgBBcount;
3131
gsGSChecksInitCookie();
3232

33+
fgAddCodeRef(fgLastBB, 0, SCK_FAIL_FAST);
34+
3335
if (compGSReorderStackLayout)
3436
{
3537
gsCopyShadowParams();

0 commit comments

Comments
 (0)