Skip to content

Commit be690f7

Browse files
committed
[WIP] [CodeGen] Enable TrapUnreachable by default for all targets.
1 parent 783bac7 commit be690f7

File tree

106 files changed

+590
-412
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

106 files changed

+590
-412
lines changed

llvm/include/llvm/Target/TargetOptions.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ namespace llvm {
145145
DataSections(false), IgnoreXCOFFVisibility(false),
146146
XCOFFTracebackTable(true), UniqueSectionNames(true),
147147
UniqueBasicBlockSectionNames(false), SeparateNamedSections(false),
148-
TrapUnreachable(false), NoTrapAfterNoreturn(false), TLSSize(0),
148+
TrapUnreachable(true), NoTrapAfterNoreturn(true), TLSSize(0),
149149
EmulatedTLS(false), EnableTLSDESC(false), EnableIPRA(false),
150150
EmitStackSizeSection(false), EnableMachineOutliner(false),
151151
EnableMachineFunctionSplitter(false), SupportsDefaultOutlining(false),

llvm/lib/CodeGen/GlobalISel/IRTranslator.cpp

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3807,16 +3807,11 @@ bool IRTranslator::emitSPDescriptorFailure(StackProtectorDescriptor &SPD,
38073807
return false;
38083808
}
38093809

3810-
// On PS4/PS5, the "return address" must still be within the calling
3811-
// function, even if it's at the very end, so emit an explicit TRAP here.
3812-
// WebAssembly needs an unreachable instruction after a non-returning call,
3813-
// because the function return type can be different from __stack_chk_fail's
3814-
// return type (void).
3815-
const TargetMachine &TM = MF->getTarget();
3816-
if (TM.getTargetTriple().isPS() || TM.getTargetTriple().isWasm()) {
3817-
LLVM_DEBUG(dbgs() << "Unhandled trap emission for stack protector fail\n");
3818-
return false;
3819-
}
3810+
// Emit a trap instruction if we are required to do so.
3811+
const TargetOptions &TargetOpts = TLI->getTargetMachine().Options;
3812+
if (TargetOpts.TrapUnreachable && !TargetOpts.NoTrapAfterNoreturn)
3813+
CurBuilder->buildInstr(TargetOpcode::G_TRAP);
3814+
38203815
return true;
38213816
}
38223817

llvm/lib/CodeGen/LLVMTargetMachine.cpp

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@
3333
#include "llvm/Target/TargetOptions.h"
3434
using namespace llvm;
3535

36-
static cl::opt<bool>
37-
EnableTrapUnreachable("trap-unreachable", cl::Hidden,
38-
cl::desc("Enable generating trap for unreachable"));
36+
cl::opt<bool> EnableTrapUnreachable(
37+
"trap-unreachable", cl::Hidden,
38+
cl::desc("Enable generating trap for unreachable"));
3939

40-
static cl::opt<bool> EnableNoTrapAfterNoreturn(
40+
cl::opt<bool> EnableNoTrapAfterNoreturn(
4141
"no-trap-after-noreturn", cl::Hidden,
4242
cl::desc("Do not emit a trap instruction for 'unreachable' IR instructions "
4343
"after noreturn calls, even if --trap-unreachable is set."));
@@ -96,10 +96,15 @@ LLVMTargetMachine::LLVMTargetMachine(const Target &T,
9696
this->CMModel = CM;
9797
this->OptLevel = OL;
9898

99-
if (EnableTrapUnreachable)
100-
this->Options.TrapUnreachable = true;
101-
if (EnableNoTrapAfterNoreturn)
102-
this->Options.NoTrapAfterNoreturn = true;
99+
if (EnableTrapUnreachable.getNumOccurrences())
100+
this->Options.TrapUnreachable = EnableTrapUnreachable;
101+
102+
if (EnableNoTrapAfterNoreturn.getNumOccurrences())
103+
this->Options.NoTrapAfterNoreturn = EnableNoTrapAfterNoreturn;
104+
105+
// Keep all traps in debug environments.
106+
else if (OL == CodeGenOptLevel::None)
107+
this->Options.NoTrapAfterNoreturn = false;
103108
}
104109

105110
TargetTransformInfo

llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3183,15 +3183,10 @@ SelectionDAGBuilder::visitSPDescriptorFailure(StackProtectorDescriptor &SPD) {
31833183
SDValue Chain = TLI.makeLibCall(DAG, RTLIB::STACKPROTECTOR_CHECK_FAIL,
31843184
MVT::isVoid, {}, CallOptions, getCurSDLoc())
31853185
.second;
3186-
// On PS4/PS5, the "return address" must still be within the calling
3187-
// function, even if it's at the very end, so emit an explicit TRAP here.
3188-
// Passing 'true' for doesNotReturn above won't generate the trap for us.
3189-
if (TM.getTargetTriple().isPS())
3190-
Chain = DAG.getNode(ISD::TRAP, getCurSDLoc(), MVT::Other, Chain);
3191-
// WebAssembly needs an unreachable instruction after a non-returning call,
3192-
// because the function return type can be different from __stack_chk_fail's
3193-
// return type (void).
3194-
if (TM.getTargetTriple().isWasm())
3186+
3187+
// If our current target options require us to add a trap instruction, do so.
3188+
const TargetOptions &TargetOpts = DAG.getTarget().Options;
3189+
if (TargetOpts.TrapUnreachable && !TargetOpts.NoTrapAfterNoreturn)
31953190
Chain = DAG.getNode(ISD::TRAP, getCurSDLoc(), MVT::Other, Chain);
31963191

31973192
DAG.setRoot(Chain);

llvm/lib/Target/AArch64/AArch64TargetMachine.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -360,11 +360,6 @@ AArch64TargetMachine::AArch64TargetMachine(const Target &T, const Triple &TT,
360360
TLOF(createTLOF(getTargetTriple())), isLittle(LittleEndian) {
361361
initAsmInfo();
362362

363-
if (TT.isOSBinFormatMachO()) {
364-
this->Options.TrapUnreachable = true;
365-
this->Options.NoTrapAfterNoreturn = true;
366-
}
367-
368363
if (getMCAsmInfo()->usesWindowsCFI()) {
369364
// Unwinding can get confused if the last instruction in an
370365
// exception-handling region (function, funclet, try block, etc.)
@@ -373,6 +368,7 @@ AArch64TargetMachine::AArch64TargetMachine(const Target &T, const Triple &TT,
373368
// FIXME: We could elide the trap if the next instruction would be in
374369
// the same region anyway.
375370
this->Options.TrapUnreachable = true;
371+
this->Options.NoTrapAfterNoreturn = false;
376372
}
377373

378374
if (this->Options.TLSSize == 0) // default

llvm/lib/Target/AMDGPU/AMDGPUTargetMachine.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,8 @@ static cl::opt<bool>
396396
cl::desc("Enable AMDGPUAttributorPass"),
397397
cl::init(true), cl::Hidden);
398398

399+
extern cl::opt<bool> EnableTrapUnreachable;
400+
399401
extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeAMDGPUTarget() {
400402
// Register the target
401403
RegisterTargetMachine<R600TargetMachine> X(getTheR600Target());
@@ -612,6 +614,10 @@ AMDGPUTargetMachine::AMDGPUTargetMachine(const Target &T, const Triple &TT,
612614
FS, Options, getEffectiveRelocModel(RM),
613615
getEffectiveCodeModel(CM, CodeModel::Small), OptLevel),
614616
TLOF(createTLOF(getTargetTriple())) {
617+
// FIXME: There are some scenarios where targets may not have hardware traps,
618+
// and external calls to `abort` also fail. For now, do a blanket-disable.
619+
if (!EnableTrapUnreachable.getNumOccurrences())
620+
this->Options.TrapUnreachable = false;
615621
initAsmInfo();
616622
if (TT.getArch() == Triple::amdgcn) {
617623
if (getMCSubtargetInfo()->checkFeatures("+wavefrontsize64"))

llvm/lib/Target/ARM/ARMTargetMachine.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -251,11 +251,6 @@ ARMBaseTargetMachine::ARMBaseTargetMachine(const Target &T, const Triple &TT,
251251
this->Options.EABIVersion = EABI::EABI5;
252252
}
253253

254-
if (TT.isOSBinFormatMachO()) {
255-
this->Options.TrapUnreachable = true;
256-
this->Options.NoTrapAfterNoreturn = true;
257-
}
258-
259254
// ARM supports the debug entry values.
260255
setSupportsDebugEntryValues(true);
261256

llvm/lib/Target/BPF/BPFTargetMachine.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
#include <optional>
3535
using namespace llvm;
3636

37+
extern cl::opt<bool> EnableTrapUnreachable;
38+
3739
static cl::
3840
opt<bool> DisableMIPeephole("disable-bpf-peephole", cl::Hidden,
3941
cl::desc("Disable machine peepholes for BPF"));
@@ -74,6 +76,12 @@ BPFTargetMachine::BPFTargetMachine(const Target &T, const Triple &TT,
7476
getEffectiveCodeModel(CM, CodeModel::Small), OL),
7577
TLOF(std::make_unique<TargetLoweringObjectFileELF>()),
7678
Subtarget(TT, std::string(CPU), std::string(FS), *this) {
79+
// FIXME: If the user has not explicitly enabled TrapUnreachable,
80+
// disable it. We do not have an explicit trap opcode and external calls
81+
// to abort are a no-no.
82+
if (!EnableTrapUnreachable.getNumOccurrences())
83+
this->Options.TrapUnreachable = false;
84+
7785
initAsmInfo();
7886

7987
BPFMCAsmInfo *MAI =

llvm/lib/Target/Hexagon/HexagonTargetMachine.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ static cl::opt<bool> EnableInstSimplify("hexagon-instsimplify", cl::Hidden,
146146
cl::init(true),
147147
cl::desc("Enable instsimplify"));
148148

149+
extern cl::opt<bool> EnableTrapUnreachable;
150+
149151
/// HexagonTargetMachineModule - Note that this is used on hosts that
150152
/// cannot link in a library unless there are references into the
151153
/// library. In particular, it seems that it is not possible to get
@@ -284,6 +286,11 @@ HexagonTargetMachine::HexagonTargetMachine(const Target &T, const Triple &TT,
284286
(HexagonNoOpt ? CodeGenOptLevel::None : OL)),
285287
TLOF(std::make_unique<HexagonTargetObjectFile>()),
286288
Subtarget(Triple(TT), CPU, FS, *this) {
289+
// FIXME: If the user has not explicitly enabled TrapUnreachable,
290+
// disable it. We currently seem to have problems with EH_RETURN lowering.
291+
if (!EnableTrapUnreachable.getNumOccurrences())
292+
this->Options.TrapUnreachable = false;
293+
287294
initializeHexagonCopyHoistingPass(*PassRegistry::getPassRegistry());
288295
initializeHexagonExpandCondsetsPass(*PassRegistry::getPassRegistry());
289296
initializeHexagonLoopAlignPass(*PassRegistry::getPassRegistry());

llvm/lib/Target/X86/X86TargetMachine.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,10 +242,11 @@ X86TargetMachine::X86TargetMachine(const Target &T, const Triple &TT,
242242
OL),
243243
TLOF(createTLOF(getTargetTriple())), IsJIT(JIT) {
244244
// On PS4/PS5, the "return address" of a 'noreturn' call must still be within
245-
// the calling function, and TrapUnreachable is an easy way to get that.
246-
if (TT.isPS() || TT.isOSBinFormatMachO()) {
245+
// the calling function, and unsetting NoTrapAfterNoreturn
246+
// is an easy way to get that.
247+
if (TT.isPS()) {
247248
this->Options.TrapUnreachable = true;
248-
this->Options.NoTrapAfterNoreturn = TT.isOSBinFormatMachO();
249+
this->Options.NoTrapAfterNoreturn = false;
249250
}
250251

251252
setMachineOutliner(true);

0 commit comments

Comments
 (0)