Skip to content

Commit d90423c

Browse files
committed
Add -Xfrontend -mergeable-traps as a way to emit mergeable traps
1 parent ed78ae6 commit d90423c

File tree

5 files changed

+37
-2
lines changed

5 files changed

+37
-2
lines changed

include/swift/AST/IRGenOptions.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,9 @@ class IRGenOptions {
478478
// Whether to run the HotColdSplitting pass when optimizing.
479479
unsigned EnableHotColdSplit : 1;
480480

481+
// Whether to emit mergeable or non-mergeable traps.
482+
unsigned MergeableTraps : 1;
483+
481484
/// The number of threads for multi-threaded code generation.
482485
unsigned NumThreads = 0;
483486

@@ -567,7 +570,7 @@ class IRGenOptions {
567570
DisableReadonlyStaticObjects(false), CollocatedMetadataFunctions(false),
568571
ColocateTypeDescriptors(true), UseRelativeProtocolWitnessTables(false),
569572
UseFragileResilientProtocolWitnesses(false),
570-
EnableHotColdSplit(false),
573+
EnableHotColdSplit(false), MergeableTraps(false),
571574
CmdArgs(), SanitizeCoverage(llvm::SanitizerCoverageOptions()),
572575
TypeInfoFilter(TypeInfoDumpFilter::All),
573576
PlatformCCallingConvention(llvm::CallingConv::C), UseCASBackend(false),

include/swift/Option/FrontendOptions.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1317,6 +1317,10 @@ def disable_split_cold_code :
13171317
Flag<["-"], "disable-split-cold-code">,
13181318
HelpText<"Disable splitting of cold code when optimizing">;
13191319

1320+
def mergeable_traps :
1321+
Flag<["-"], "mergeable-traps">,
1322+
HelpText<"Emit mergeable traps even in optimized builds">;
1323+
13201324
def enable_new_llvm_pass_manager :
13211325
Flag<["-"], "enable-new-llvm-pass-manager">,
13221326
HelpText<"Enable the new llvm pass manager">;

lib/Frontend/CompilerInvocation.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3445,6 +3445,11 @@ static bool ParseIRGenArgs(IRGenOptions &Opts, ArgList &Args,
34453445
return true;
34463446
}
34473447

3448+
Opts.MergeableTraps = Opts.shouldOptimize();
3449+
if (Args.hasArg(OPT_mergeable_traps)) {
3450+
Opts.MergeableTraps = true;
3451+
}
3452+
34483453
Opts.EnableObjectiveCProtocolSymbolicReferences =
34493454
Args.hasFlag(OPT_enable_objective_c_protocol_symbolic_references,
34503455
OPT_disable_objective_c_protocol_symbolic_references,

lib/IRGen/IRGenFunction.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ llvm::CallInst *IRBuilder::CreateNonMergeableTrap(IRGenModule &IGM,
503503
}
504504
}
505505

506-
if (IGM.IRGen.Opts.shouldOptimize()) {
506+
if (!IGM.IRGen.Opts.MergeableTraps) {
507507
// Emit unique side-effecting inline asm calls in order to eliminate
508508
// the possibility that an LLVM optimization or code generation pass
509509
// will merge these blocks back together again. We emit an empty asm

test/embedded/traps-mergeable.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// RUN: %target-swift-emit-ir -enable-experimental-feature Extern -enable-experimental-feature Embedded -mergeable-traps -wmo -Xllvm -link-embedded-runtime=0 %s -O | %FileCheck %s
2+
3+
// REQUIRES: swift_in_compiler
4+
// REQUIRES: optimized_stdlib
5+
// REQUIRES: OS=macosx || OS=linux-gnu
6+
7+
@_extern(c)
8+
public func external()
9+
10+
public func test(i: Int, j: Int) {
11+
precondition(i != 0, "precondition 1")
12+
external()
13+
precondition(j != 1, "precondition 2")
14+
}
15+
16+
// CHECK-NOT: call void asm sideeffect ""
17+
18+
// CHECK: define {{.*}}@"$s4main4test1i1jySi_SitF"
19+
// CHECK: tail call void @llvm.trap()
20+
// CHECK: unreachable
21+
// CHECK: tail call void @llvm.trap()
22+
// CHECK: unreachable
23+
// CHECK: }

0 commit comments

Comments
 (0)