Skip to content

Commit 5b62057

Browse files
committed
RuntimeLibcalls: Add entries for stack probe functions
Stop hardcoding different variants of __chkstk and query the name through RuntimeLibcalls.
1 parent 2bf9278 commit 5b62057

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
@@ -406,6 +406,7 @@ def STACK_SMASH_HANDLER : RuntimeLibcall;
406406
// Safe stack
407407
def SAFESTACK_POINTER_ADDRESS : RuntimeLibcall;
408408

409+
def STACK_PROBE : RuntimeLibcall;
409410
def SECURITY_CHECK_COOKIE : RuntimeLibcall;
410411

411412
// Deoptimization
@@ -1084,6 +1085,15 @@ def __stack_smash_handler : RuntimeLibcallImpl<STACK_SMASH_HANDLER>;
10841085

10851086
def __riscv_flush_icache : RuntimeLibcallImpl<RISCV_FLUSH_ICACHE>;
10861087

1088+
def _chkstk : RuntimeLibcallImpl<STACK_PROBE>;
1089+
def _alloca : RuntimeLibcallImpl<STACK_PROBE>;
1090+
1091+
def __chkstk : RuntimeLibcallImpl<STACK_PROBE>;
1092+
def ___chkstk_ms : RuntimeLibcallImpl<STACK_PROBE>;
1093+
1094+
def __chkstk_ms : RuntimeLibcallImpl<STACK_PROBE>;
1095+
def __chkstk_arm64ec : RuntimeLibcallImpl<STACK_PROBE, "#__chkstk_arm64ec">;
1096+
10871097
def __security_check_cookie : RuntimeLibcallImpl<SECURITY_CHECK_COOKIE>;
10881098
def __security_check_cookie_arm64ec : RuntimeLibcallImpl<SECURITY_CHECK_COOKIE,
10891099
"#__security_check_cookie_arm64ec">;
@@ -1260,6 +1270,9 @@ defvar SecurityCheckCookieIfWinMSVC =
12601270
LibcallImpls<(add __security_check_cookie, __security_cookie),
12611271
isWindowsMSVCOrItaniumEnvironment>;
12621272

1273+
defvar __chkstk_IfWinMSVC =
1274+
LibcallImpls<(add __chkstk), isWindowsMSVCEnvironment>;
1275+
12631276
defvar LibmHasSinCosF32 = LibcallImpls<(add sincosf), hasSinCos>;
12641277
defvar LibmHasSinCosF64 = LibcallImpls<(add sincos), hasSinCos>;
12651278
defvar LibmHasSinCosF80 = LibcallImpls<(add sincosl_f80), hasSinCos>;
@@ -1418,6 +1431,7 @@ def AArch64SystemLibrary : SystemRuntimeLibrary<
14181431
DefaultLibmExp10,
14191432
DefaultStackProtector,
14201433
SecurityCheckCookieIfWinMSVC,
1434+
__chkstk_IfWinMSVC,
14211435
SMEABI_LibCalls_PreserveMost_From_X0,
14221436
SMEABI_LibCalls_PreserveMost_From_X1,
14231437
SMEABI_LibCalls_PreserveMost_From_X2)
@@ -1465,6 +1479,7 @@ def WindowsARM64ECSystemLibrary
14651479
LibcallImpls<(add __security_check_cookie_arm64ec,
14661480
__security_cookie),
14671481
isWindowsMSVCEnvironment>,
1482+
__chkstk_arm64ec,
14681483
ExceptionModelCallsArm64EC)>;
14691484

14701485
//===----------------------------------------------------------------------===//
@@ -1882,6 +1897,7 @@ def ARMSystemLibrary
18821897
WindowARMDivRemCalls,
18831898
WindowARMFPIntCasts,
18841899
SecurityCheckCookieIfWinMSVC,
1900+
LibcallImpls<(add __chkstk), isOSWindows>,
18851901
AEABIDivRemCalls,
18861902
DarwinSinCosStret, DarwinExp10,
18871903
LibmHasSinCosF32, LibmHasSinCosF64, LibmHasSinCosF128,
@@ -2552,6 +2568,19 @@ def isX86_32 : RuntimeLibcallPredicate<"TT.getArch() == Triple::x86">;
25522568
def isX86_64 : RuntimeLibcallPredicate<"TT.getArch() == Triple::x86_64">;
25532569
def isX86 : RuntimeLibcallPredicate<"TT.isX86()">;
25542570

2571+
def isCygwinMinGW64 : RuntimeLibcallPredicate<
2572+
[{TT.isOSCygMing() && TT.getArch() == Triple::x86_64}]>;
2573+
def isCygwinMinGW32 : RuntimeLibcallPredicate<
2574+
[{TT.isOSCygMing() && TT.getArch() == Triple::x86}]>;
2575+
2576+
def isWin32NotCygMing : RuntimeLibcallPredicate<
2577+
[{TT.getArch() == Triple::x86 &&
2578+
(TT.isOSWindows() || TT.isUEFI()) && !TT.isOSCygMing()}]>;
2579+
def isWin64NotCygMing : RuntimeLibcallPredicate<
2580+
[{TT.getArch() == Triple::x86_64 &&
2581+
(TT.isOSWindows() || TT.isUEFI()) && !TT.isOSCygMing()}]>;
2582+
2583+
25552584
// Some darwins have an optimized __bzero/bzero function.
25562585
def darwinHas__bzero : RuntimeLibcallPredicate<"TT.isMacOSX() && !TT.isMacOSXVersionLT(10, 6)">;
25572586

@@ -2581,8 +2610,12 @@ defvar X86CommonLibcalls =
25812610
// FIXME: MSVCRT doesn't have powi. The f128 case is added as a
25822611
// hack for one test relying on it.
25832612
__powitf2_f128,
2584-
DefaultStackProtector
2585-
);
2613+
DefaultStackProtector,
2614+
LibcallImpls<(add ___chkstk_ms), isCygwinMinGW64>,
2615+
LibcallImpls<(add __chkstk), isWin64NotCygMing>,
2616+
LibcallImpls<(add _alloca), isCygwinMinGW32>,
2617+
LibcallImpls<(add _chkstk), isWin32NotCygMing>
2618+
);
25862619

25872620
defvar Windows32DivRemMulCalls =
25882621
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
@@ -16857,11 +16857,15 @@ AArch64TargetLowering::LowerWindowsDYNAMIC_STACKALLOC(SDValue Op,
1685716857
return DAG.getMergeValues(Ops, DL);
1685816858
}
1685916859

16860+
RTLIB::LibcallImpl ChkStkImpl = getLibcallImpl(RTLIB::STACK_PROBE);
16861+
if (ChkStkImpl == RTLIB::Unsupported)
16862+
return SDValue();
16863+
1686016864
Chain = DAG.getCALLSEQ_START(Chain, 0, 0, DL);
1686116865

1686216866
EVT PtrVT = getPointerTy(DAG.getDataLayout());
16863-
SDValue Callee = DAG.getTargetExternalSymbol(Subtarget->getChkStkName(),
16864-
PtrVT, 0);
16867+
SDValue Callee = DAG.getTargetExternalSymbol(
16868+
getLibcallImplName(ChkStkImpl).data(), PtrVT, 0);
1686516869

1686616870
const AArch64RegisterInfo *TRI = Subtarget->getRegisterInfo();
1686716871
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
@@ -11727,6 +11727,11 @@ ARMTargetLowering::EmitLowered__chkstk(MachineInstr &MI,
1172711727
// -mcmodel=large, alleviating the need for the trampoline which may clobber
1172811728
// IP.
1172911729

11730+
RTLIB::LibcallImpl ChkStkLibcall = getLibcallImpl(RTLIB::STACK_PROBE);
11731+
if (ChkStkLibcall == RTLIB::Unsupported)
11732+
reportFatalUsageError("no available implementation of __chkstk");
11733+
11734+
const char *ChkStk = getLibcallImplName(ChkStkLibcall).data();
1173011735
switch (TM.getCodeModel()) {
1173111736
case CodeModel::Tiny:
1173211737
llvm_unreachable("Tiny code model not available on ARM.");
@@ -11735,7 +11740,7 @@ ARMTargetLowering::EmitLowered__chkstk(MachineInstr &MI,
1173511740
case CodeModel::Kernel:
1173611741
BuildMI(*MBB, MI, DL, TII.get(ARM::tBL))
1173711742
.add(predOps(ARMCC::AL))
11738-
.addExternalSymbol("__chkstk")
11743+
.addExternalSymbol(ChkStk)
1173911744
.addReg(ARM::R4, RegState::Implicit | RegState::Kill)
1174011745
.addReg(ARM::R4, RegState::Implicit | RegState::Define)
1174111746
.addReg(ARM::R12,
@@ -11748,7 +11753,7 @@ ARMTargetLowering::EmitLowered__chkstk(MachineInstr &MI,
1174811753
Register Reg = MRI.createVirtualRegister(&ARM::rGPRRegClass);
1174911754

1175011755
BuildMI(*MBB, MI, DL, TII.get(ARM::t2MOVi32imm), Reg)
11751-
.addExternalSymbol("__chkstk");
11756+
.addExternalSymbol(ChkStk);
1175211757
BuildMI(*MBB, MI, DL, TII.get(gettBLXrOpcode(*MBB->getParent())))
1175311758
.add(predOps(ARMCC::AL))
1175411759
.addReg(Reg, RegState::Kill)

llvm/lib/Target/X86/X86ISelLowering.cpp

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

6243762437
// We need a stack probe to conform to the Windows ABI. Choose the right
6243862438
// symbol.
62439-
if (Subtarget.is64Bit())
62440-
return Subtarget.isTargetCygMing() ? "___chkstk_ms" : "__chkstk";
62441-
return Subtarget.isTargetCygMing() ? "_alloca" : "_chkstk";
62439+
RTLIB::LibcallImpl StackProbeImpl = getLibcallImpl(RTLIB::STACK_PROBE);
62440+
if (StackProbeImpl == RTLIB::Unsupported)
62441+
return "";
62442+
return getLibcallImplName(StackProbeImpl);
6244262443
}
6244362444

6244462445
unsigned

0 commit comments

Comments
 (0)