Skip to content

Commit ff04af4

Browse files
committed
Add -Xfrontend -mergeable-traps as a way to emit mergeable traps
1 parent 0a304e1 commit ff04af4

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
@@ -480,6 +480,9 @@ class IRGenOptions {
480480

481481
unsigned EmitAsyncFramePushPopMetadata : 1;
482482

483+
// Whether to emit mergeable or non-mergeable traps.
484+
unsigned MergeableTraps : 1;
485+
483486
/// The number of threads for multi-threaded code generation.
484487
unsigned NumThreads = 0;
485488

@@ -570,6 +573,7 @@ class IRGenOptions {
570573
ColocateTypeDescriptors(true), UseRelativeProtocolWitnessTables(false),
571574
UseFragileResilientProtocolWitnesses(false),
572575
EnableHotColdSplit(false), EmitAsyncFramePushPopMetadata(false),
576+
MergeableTraps(false),
573577
CmdArgs(), SanitizeCoverage(llvm::SanitizerCoverageOptions()),
574578
TypeInfoFilter(TypeInfoDumpFilter::All),
575579
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
@@ -1331,6 +1331,10 @@ def disable_split_cold_code :
13311331
Flag<["-"], "disable-split-cold-code">,
13321332
HelpText<"Disable splitting of cold code when optimizing">;
13331333

1334+
def mergeable_traps :
1335+
Flag<["-"], "mergeable-traps">,
1336+
HelpText<"Emit mergeable traps even in optimized builds">;
1337+
13341338
def enable_new_llvm_pass_manager :
13351339
Flag<["-"], "enable-new-llvm-pass-manager">,
13361340
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
@@ -3482,6 +3482,11 @@ static bool ParseIRGenArgs(IRGenOptions &Opts, ArgList &Args,
34823482
return true;
34833483
}
34843484

3485+
Opts.MergeableTraps = Opts.shouldOptimize();
3486+
if (Args.hasArg(OPT_mergeable_traps)) {
3487+
Opts.MergeableTraps = true;
3488+
}
3489+
34853490
Opts.EnableObjectiveCProtocolSymbolicReferences =
34863491
Args.hasFlag(OPT_enable_objective_c_protocol_symbolic_references,
34873492
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)