Skip to content

Commit 6e0d765

Browse files
committed
Add -Xfrontend -mergeable-traps as a way to emit mergeable traps
1 parent 06f6358 commit 6e0d765

File tree

5 files changed

+37
-1
lines changed

5 files changed

+37
-1
lines changed

include/swift/AST/IRGenOptions.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,9 @@ class IRGenOptions {
540540
// variants.
541541
unsigned UseCoroCCArm64 : 1;
542542

543+
// Whether to emit mergeable or non-mergeable traps.
544+
unsigned MergeableTraps : 1;
545+
543546
/// The number of threads for multi-threaded code generation.
544547
unsigned NumThreads = 0;
545548

@@ -646,6 +649,7 @@ class IRGenOptions {
646649
EmitAsyncFramePushPopMetadata(true), EmitTypeMallocForCoroFrame(false),
647650
AsyncFramePointerAll(false), UseProfilingMarkerThunks(false),
648651
UseCoroCCX8664(false), UseCoroCCArm64(false),
652+
MergeableTraps(false),
649653
DebugInfoForProfiling(false), CmdArgs(),
650654
SanitizeCoverage(llvm::SanitizerCoverageOptions()),
651655
TypeInfoFilter(TypeInfoDumpFilter::All),

include/swift/Option/FrontendOptions.td

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

1404+
def mergeable_traps :
1405+
Flag<["-"], "mergeable-traps">,
1406+
HelpText<"Emit mergeable traps even in optimized builds">;
1407+
14041408
def enable_new_llvm_pass_manager :
14051409
Flag<["-"], "enable-new-llvm-pass-manager">,
14061410
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
@@ -3776,6 +3776,11 @@ static bool ParseIRGenArgs(IRGenOptions &Opts, ArgList &Args,
37763776
return true;
37773777
}
37783778

3779+
Opts.MergeableTraps = Opts.shouldOptimize();
3780+
if (Args.hasArg(OPT_mergeable_traps)) {
3781+
Opts.MergeableTraps = true;
3782+
}
3783+
37793784
Opts.EnableObjectiveCProtocolSymbolicReferences =
37803785
Args.hasFlag(OPT_enable_objective_c_protocol_symbolic_references,
37813786
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)