Skip to content

Commit ee57eed

Browse files
committed
IRGen: Annotate LLVM IR of condfail with the condfail's message
`-Xfrontend -enable-cond-fail-message-annotation` LLVM IR produced by the Swift compiler will add the `annotation` metadata attribute to the branch instruction generated for cond_fail builtins.
1 parent 13ef626 commit ee57eed

File tree

5 files changed

+46
-2
lines changed

5 files changed

+46
-2
lines changed

include/swift/AST/IRGenOptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,8 @@ class IRGenOptions {
490490

491491
unsigned ConditionalRuntimeRecords : 1;
492492

493+
unsigned AnnotateCondFailMessage : 1;
494+
493495
unsigned InternalizeAtLink : 1;
494496

495497
/// Internalize symbols (static library) - do not export any public symbols.
@@ -634,6 +636,7 @@ class IRGenOptions {
634636
DisableStandardSubstitutionsInReflectionMangling(false),
635637
EnableGlobalISel(false), VirtualFunctionElimination(false),
636638
WitnessMethodElimination(false), ConditionalRuntimeRecords(false),
639+
AnnotateCondFailMessage(false),
637640
InternalizeAtLink(false), InternalizeSymbols(false),
638641
MergeableSymbols(false), EmitGenericRODatas(true),
639642
NoPreallocatedInstantiationCaches(false),

include/swift/Option/FrontendOptions.td

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1175,6 +1175,12 @@ def enable_arm64_corocc : Flag<["-"], "enable-arm64-corocc">,
11751175
def disable_arm64_corocc : Flag<["-"], "disable-arm64-corocc">,
11761176
HelpText<"Don't use swiftcorocc for yield_once_2 routines on arm64 variants.">;
11771177

1178+
def enable_cond_fail_message_annotation : Flag<["-"], "enable-cond-fail-message-annotation">,
1179+
HelpText<"Enable large loadable types register to memory pass">;
1180+
1181+
def disable_cond_fail_message_annotation : Flag<["-"], "dissable-cond-fail-message-annotation">,
1182+
HelpText<"Disable large loadable types register to memory pass">;
1183+
11781184
let Flags = [FrontendOption, NoDriverOption, HelpHidden, ModuleInterfaceOptionIgnorable] in {
11791185
def enable_pack_metadata_stack_promotion :
11801186
Joined<["-"], "enable-pack-metadata-stack-promotion=">,

lib/Frontend/CompilerInvocation.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3764,6 +3764,11 @@ static bool ParseIRGenArgs(IRGenOptions &Opts, ArgList &Args,
37643764
Opts.EnableLayoutStringValueWitnessesInstantiation = Args.hasFlag(OPT_enable_layout_string_value_witnesses_instantiation,
37653765
OPT_disable_layout_string_value_witnesses_instantiation,
37663766
Opts.EnableLayoutStringValueWitnessesInstantiation);
3767+
Opts.AnnotateCondFailMessage =
3768+
Args.hasFlag(OPT_enable_cond_fail_message_annotation,
3769+
OPT_disable_cond_fail_message_annotation,
3770+
Opts.AnnotateCondFailMessage);
3771+
37673772

37683773
if (Opts.EnableLayoutStringValueWitnessesInstantiation &&
37693774
!Opts.EnableLayoutStringValueWitnesses) {

lib/IRGen/IRGenSIL.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8249,8 +8249,11 @@ void IRGenSILFunction::visitCondFailInst(swift::CondFailInst *i) {
82498249

82508250
llvm::BasicBlock *failBB = llvm::BasicBlock::Create(IGM.getLLVMContext());
82518251
llvm::BasicBlock *contBB = llvm::BasicBlock::Create(IGM.getLLVMContext());
8252-
Builder.CreateCondBr(expectedCond, failBB, contBB);
8253-
8252+
auto br = Builder.CreateCondBr(expectedCond, failBB, contBB);
8253+
8254+
if (IGM.getOptions().AnnotateCondFailMessage && !i->getMessage().empty())
8255+
br->addAnnotationMetadata(i->getMessage());
8256+
82548257
Builder.SetInsertPoint(&CurFn->back());
82558258
Builder.emitBlock(failBB);
82568259
if (IGM.DebugInfo)

test/IRGen/annotated_cond_fail.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// RUN: %target-swift-frontend -module-name A -enable-cond-fail-message-annotation -primary-file %s -O -emit-ir | %FileCheck %s
2+
3+
// REQUIRES: swift_stdlib_no_asserts,optimized_stdlib
4+
// REQUIRES: PTRSIZE=64
5+
6+
public func test(_ a: [Int], _ i: Int) -> Int {
7+
return a[i + 1]
8+
}
9+
10+
// CHECK: define{{.*}} swiftcc i64 @"$s1A4testySiSaySiG_SitF"(ptr{{.*}} %0, i64 %1)
11+
12+
// CHECK: [[ADD:%.*]] = tail call { i64, i1 } @llvm.sadd.with.overflow.i64(i64 %1, i64 1)
13+
// CHECK: [[IDX:%.*]] = extractvalue { i64, i1 } [[ADD]], 0
14+
// CHECK: [[OVERFLOW:%.*]] = extractvalue { i64, i1 } [[ADD]], 1
15+
// CHECK: br i1 [[OVERFLOW]], {{.*}}!annotation ![[ARITH_OVERFLOW:[0-9]+]]
16+
17+
// CHECK: [[C0:%.*]] = icmp slt i64 [[IDX]], 0
18+
// CHECK: br i1 [[C0]], {{.*}}!annotation ![[ARRAY_INDEX_OUT_OF_BOUNDS:[0-9]+]]
19+
20+
// CHECK: [[SIZE_ADDR:%.*]] = getelementptr
21+
// CHECK: [[SIZE:%.*]] = load i64, ptr [[SIZE_ADDR]]
22+
// CHECK: [[C1:%.*]] = icmp ult i64 [[IDX]], [[SIZE]]
23+
// CHECK: br i1 [[C1]], {{.*}}!annotation ![[ARRAY_INDEX_OUT_OF_BOUNDS]]
24+
25+
26+
// CHECK-DAG: ![[ARITH_OVERFLOW]] = !{!"arithmetic overflow"}
27+
// CHECK-DAG: ![[ARRAY_INDEX_OUT_OF_BOUNDS]] = !{!"Index out of range"}

0 commit comments

Comments
 (0)