Skip to content

Commit 37449a6

Browse files
committed
RuntimeLibcalls: Add entries for stack probe functions
Stop hardcoding different variants of __chkstk and query the name through RuntimeLibcalls.
1 parent eab23e1 commit 37449a6

File tree

7 files changed

+67
-19
lines changed

7 files changed

+67
-19
lines changed

llvm/include/llvm/IR/RuntimeLibcalls.td

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,7 @@ def STACK_SMASH_HANDLER : RuntimeLibcall;
474474
// Safe stack
475475
def SAFESTACK_POINTER_ADDRESS : RuntimeLibcall;
476476

477+
def STACK_PROBE : RuntimeLibcall;
477478
def SECURITY_CHECK_COOKIE : RuntimeLibcall;
478479

479480
// Deoptimization
@@ -1188,6 +1189,15 @@ def __stack_smash_handler : RuntimeLibcallImpl<STACK_SMASH_HANDLER>;
11881189

11891190
def __riscv_flush_icache : RuntimeLibcallImpl<RISCV_FLUSH_ICACHE>;
11901191

1192+
def _chkstk : RuntimeLibcallImpl<STACK_PROBE>;
1193+
def _alloca : RuntimeLibcallImpl<STACK_PROBE>;
1194+
1195+
def __chkstk : RuntimeLibcallImpl<STACK_PROBE>;
1196+
def ___chkstk_ms : RuntimeLibcallImpl<STACK_PROBE>;
1197+
1198+
def __chkstk_ms : RuntimeLibcallImpl<STACK_PROBE>;
1199+
def __chkstk_arm64ec : RuntimeLibcallImpl<STACK_PROBE, "#__chkstk_arm64ec">;
1200+
11911201
def __security_check_cookie : RuntimeLibcallImpl<SECURITY_CHECK_COOKIE>;
11921202
def __security_check_cookie_arm64ec : RuntimeLibcallImpl<SECURITY_CHECK_COOKIE,
11931203
"#__security_check_cookie_arm64ec">;
@@ -1330,6 +1340,9 @@ defvar SecurityCheckCookieIfWinMSVC =
13301340
LibcallImpls<(add __security_check_cookie, __security_cookie),
13311341
isWindowsMSVCOrItaniumEnvironment>;
13321342

1343+
defvar __chkstk_IfWinMSVC =
1344+
LibcallImpls<(add __chkstk), isWindowsMSVCEnvironment>;
1345+
13331346
defvar LibmHasSinCosF32 = LibcallImpls<(add sincosf), hasSinCos>;
13341347
defvar LibmHasSinCosF64 = LibcallImpls<(add sincos), hasSinCos>;
13351348
defvar LibmHasSinCosF80 = LibcallImpls<(add sincosl_f80), hasSinCos>;
@@ -1488,6 +1501,7 @@ def AArch64SystemLibrary : SystemRuntimeLibrary<
14881501
DefaultLibmExp10,
14891502
DefaultStackProtector,
14901503
SecurityCheckCookieIfWinMSVC,
1504+
__chkstk_IfWinMSVC,
14911505
SMEABI_LibCalls_PreserveMost_From_X0,
14921506
SMEABI_LibCalls_PreserveMost_From_X1,
14931507
SMEABI_LibCalls_PreserveMost_From_X2)
@@ -1535,6 +1549,7 @@ def WindowsARM64ECSystemLibrary
15351549
LibcallImpls<(add __security_check_cookie_arm64ec,
15361550
__security_cookie),
15371551
isWindowsMSVCEnvironment>,
1552+
__chkstk_arm64ec,
15381553
ExceptionModelCallsArm64EC)>;
15391554

15401555
//===----------------------------------------------------------------------===//
@@ -1952,6 +1967,7 @@ def ARMSystemLibrary
19521967
WindowARMDivRemCalls,
19531968
WindowARMFPIntCasts,
19541969
SecurityCheckCookieIfWinMSVC,
1970+
LibcallImpls<(add __chkstk), isOSWindows>,
19551971
AEABIDivRemCalls,
19561972
DarwinSinCosStret, DarwinExp10,
19571973
LibmHasSinCosF32, LibmHasSinCosF64, LibmHasSinCosF128,
@@ -2622,6 +2638,19 @@ def isX86_32 : RuntimeLibcallPredicate<"TT.getArch() == Triple::x86">;
26222638
def isX86_64 : RuntimeLibcallPredicate<"TT.getArch() == Triple::x86_64">;
26232639
def isX86 : RuntimeLibcallPredicate<"TT.isX86()">;
26242640

2641+
def isCygwinMinGW64 : RuntimeLibcallPredicate<
2642+
[{TT.isOSCygMing() && TT.getArch() == Triple::x86_64}]>;
2643+
def isCygwinMinGW32 : RuntimeLibcallPredicate<
2644+
[{TT.isOSCygMing() && TT.getArch() == Triple::x86}]>;
2645+
2646+
def isWin32NotCygMing : RuntimeLibcallPredicate<
2647+
[{TT.getArch() == Triple::x86 &&
2648+
(TT.isOSWindows() || TT.isUEFI()) && !TT.isOSCygMing()}]>;
2649+
def isWin64NotCygMing : RuntimeLibcallPredicate<
2650+
[{TT.getArch() == Triple::x86_64 &&
2651+
(TT.isOSWindows() || TT.isUEFI()) && !TT.isOSCygMing()}]>;
2652+
2653+
26252654
// Some darwins have an optimized __bzero/bzero function.
26262655
def darwinHas__bzero : RuntimeLibcallPredicate<"TT.isMacOSX() && !TT.isMacOSXVersionLT(10, 6)">;
26272656

@@ -2654,8 +2683,12 @@ defvar X86CommonLibcalls =
26542683
// hack for one test relying on it.
26552684
__powitf2_f128,
26562685
DefaultStackProtector,
2657-
LibcallImpls<(add MemChkLibcalls), isNotPS>
2658-
);
2686+
LibcallImpls<(add MemChkLibcalls), isNotPS>,
2687+
LibcallImpls<(add ___chkstk_ms), isCygwinMinGW64>,
2688+
LibcallImpls<(add __chkstk), isWin64NotCygMing>,
2689+
LibcallImpls<(add _alloca), isCygwinMinGW32>,
2690+
LibcallImpls<(add _chkstk), isWin32NotCygMing>
2691+
);
26592692

26602693
defvar Windows32DivRemMulCalls =
26612694
LibcallsWithCC<(add WindowsDivRemMulLibcalls), X86_STDCALL,

llvm/lib/Target/AArch64/AArch64ISelLowering.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17071,11 +17071,15 @@ AArch64TargetLowering::LowerWindowsDYNAMIC_STACKALLOC(SDValue Op,
1707117071
return DAG.getMergeValues(Ops, DL);
1707217072
}
1707317073

17074+
RTLIB::LibcallImpl ChkStkImpl = getLibcallImpl(RTLIB::STACK_PROBE);
17075+
if (ChkStkImpl == RTLIB::Unsupported)
17076+
return SDValue();
17077+
1707417078
Chain = DAG.getCALLSEQ_START(Chain, 0, 0, DL);
1707517079

1707617080
EVT PtrVT = getPointerTy(DAG.getDataLayout());
17077-
SDValue Callee = DAG.getTargetExternalSymbol(Subtarget->getChkStkName(),
17078-
PtrVT, 0);
17081+
SDValue Callee = DAG.getTargetExternalSymbol(
17082+
getLibcallImplName(ChkStkImpl).data(), PtrVT, 0);
1707917083

1708017084
const AArch64RegisterInfo *TRI = Subtarget->getRegisterInfo();
1708117085
const uint32_t *Mask = TRI->getWindowsStackProbePreservedMask();

llvm/lib/Target/AArch64/AArch64PrologueEpilogue.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1135,7 +1135,12 @@ void AArch64PrologueEmitter::emitWindowsStackProbe(
11351135
.setMIFlags(MachineInstr::FrameSetup);
11361136
}
11371137

1138-
const char *ChkStk = Subtarget.getChkStkName();
1138+
const AArch64TargetLowering *TLI = Subtarget.getTargetLowering();
1139+
RTLIB::LibcallImpl ChkStkLibcall = TLI->getLibcallImpl(RTLIB::STACK_PROBE);
1140+
if (ChkStkLibcall == RTLIB::Unsupported)
1141+
reportFatalUsageError("no available implementation of __chkstk");
1142+
1143+
const char *ChkStk = TLI->getLibcallImplName(ChkStkLibcall).data();
11391144
switch (MF.getTarget().getCodeModel()) {
11401145
case CodeModel::Tiny:
11411146
case CodeModel::Small:

llvm/lib/Target/AArch64/AArch64Subtarget.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -450,12 +450,6 @@ class AArch64Subtarget final : public AArch64GenSubtargetInfo {
450450
/// add + cnt instructions.
451451
bool useScalarIncVL() const;
452452

453-
const char* getChkStkName() const {
454-
if (isWindowsArm64EC())
455-
return "#__chkstk_arm64ec";
456-
return "__chkstk";
457-
}
458-
459453
/// Choose a method of checking LR before performing a tail call.
460454
AArch64PAuth::AuthCheckMethod
461455
getAuthenticatedLRCheckMethod(const MachineFunction &MF) const;

llvm/lib/Target/ARM/ARMFrameLowering.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,6 +1138,12 @@ void ARMFrameLowering::emitPrologue(MachineFunction &MF,
11381138
.add(predOps(ARMCC::AL));
11391139
}
11401140

1141+
const ARMTargetLowering *TLI = STI.getTargetLowering();
1142+
RTLIB::LibcallImpl ChkStkLibcall = TLI->getLibcallImpl(RTLIB::STACK_PROBE);
1143+
if (ChkStkLibcall == RTLIB::Unsupported)
1144+
reportFatalUsageError("no available implementation of __chkstk");
1145+
const char *ChkStk = TLI->getLibcallImplName(ChkStkLibcall).data();
1146+
11411147
switch (TM.getCodeModel()) {
11421148
case CodeModel::Tiny:
11431149
llvm_unreachable("Tiny code model not available on ARM.");
@@ -1146,14 +1152,14 @@ void ARMFrameLowering::emitPrologue(MachineFunction &MF,
11461152
case CodeModel::Kernel:
11471153
BuildMI(MBB, MBBI, dl, TII.get(ARM::tBL))
11481154
.add(predOps(ARMCC::AL))
1149-
.addExternalSymbol("__chkstk")
1155+
.addExternalSymbol(ChkStk)
11501156
.addReg(ARM::R4, RegState::Implicit)
11511157
.setMIFlags(MachineInstr::FrameSetup);
11521158
break;
11531159
case CodeModel::Large:
11541160
BuildMI(MBB, MBBI, dl, TII.get(ARM::t2MOVi32imm), ARM::R12)
1155-
.addExternalSymbol("__chkstk")
1156-
.setMIFlags(MachineInstr::FrameSetup);
1161+
.addExternalSymbol(ChkStk)
1162+
.setMIFlags(MachineInstr::FrameSetup);
11571163

11581164
BuildMI(MBB, MBBI, dl, TII.get(ARM::tBLXr))
11591165
.add(predOps(ARMCC::AL))

llvm/lib/Target/ARM/ARMISelLowering.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11480,6 +11480,11 @@ ARMTargetLowering::EmitLowered__chkstk(MachineInstr &MI,
1148011480
// -mcmodel=large, alleviating the need for the trampoline which may clobber
1148111481
// IP.
1148211482

11483+
RTLIB::LibcallImpl ChkStkLibcall = getLibcallImpl(RTLIB::STACK_PROBE);
11484+
if (ChkStkLibcall == RTLIB::Unsupported)
11485+
reportFatalUsageError("no available implementation of __chkstk");
11486+
11487+
const char *ChkStk = getLibcallImplName(ChkStkLibcall).data();
1148311488
switch (TM.getCodeModel()) {
1148411489
case CodeModel::Tiny:
1148511490
llvm_unreachable("Tiny code model not available on ARM.");
@@ -11488,7 +11493,7 @@ ARMTargetLowering::EmitLowered__chkstk(MachineInstr &MI,
1148811493
case CodeModel::Kernel:
1148911494
BuildMI(*MBB, MI, DL, TII.get(ARM::tBL))
1149011495
.add(predOps(ARMCC::AL))
11491-
.addExternalSymbol("__chkstk")
11496+
.addExternalSymbol(ChkStk)
1149211497
.addReg(ARM::R4, RegState::Implicit | RegState::Kill)
1149311498
.addReg(ARM::R4, RegState::Implicit | RegState::Define)
1149411499
.addReg(ARM::R12,
@@ -11501,7 +11506,7 @@ ARMTargetLowering::EmitLowered__chkstk(MachineInstr &MI,
1150111506
Register Reg = MRI.createVirtualRegister(&ARM::rGPRRegClass);
1150211507

1150311508
BuildMI(*MBB, MI, DL, TII.get(ARM::t2MOVi32imm), Reg)
11504-
.addExternalSymbol("__chkstk");
11509+
.addExternalSymbol(ChkStk);
1150511510
BuildMI(*MBB, MI, DL, TII.get(gettBLXrOpcode(*MBB->getParent())))
1150611511
.add(predOps(ARMCC::AL))
1150711512
.addReg(Reg, RegState::Kill)

llvm/lib/Target/X86/X86ISelLowering.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62614,9 +62614,10 @@ X86TargetLowering::getStackProbeSymbolName(const MachineFunction &MF) const {
6261462614

6261562615
// We need a stack probe to conform to the Windows ABI. Choose the right
6261662616
// symbol.
62617-
if (Subtarget.is64Bit())
62618-
return Subtarget.isTargetCygMing() ? "___chkstk_ms" : "__chkstk";
62619-
return Subtarget.isTargetCygMing() ? "_alloca" : "_chkstk";
62617+
RTLIB::LibcallImpl StackProbeImpl = getLibcallImpl(RTLIB::STACK_PROBE);
62618+
if (StackProbeImpl == RTLIB::Unsupported)
62619+
return "";
62620+
return getLibcallImplName(StackProbeImpl);
6262062621
}
6262162622

6262262623
unsigned

0 commit comments

Comments
 (0)