Skip to content

Commit fb55618

Browse files
committed
Address suggestions
Added tests
1 parent 08bc2e1 commit fb55618

File tree

6 files changed

+138
-7
lines changed

6 files changed

+138
-7
lines changed

include/swift/AST/IRGenOptions.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -552,9 +552,9 @@ class IRGenOptions {
552552
/// or the empty string.
553553
std::string UseSampleProfile = "";
554554

555-
/// Path to the context-sensitive profile file to be used for CS-IR PGO,
556-
/// or the default string.
557-
std::string CSProfileGenFile = "default_%m.profraw";
555+
/// Name of the profile file to use as output for -ir-profile-generate,
556+
/// and -cs-profile-generate, or the default string.
557+
std::string InstrProfileOutput = "default_%m.profraw";
558558

559559
/// Whether to enable context-sensitive IR PGO generation.
560560
bool EnableCSIRProfileGen = false;

lib/Driver/ToolChains.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,7 @@ void ToolChain::addCommonFrontendArgs(const OutputInfo &OI,
334334
inputArgs.AddLastArg(arguments, options::OPT_profile_use);
335335
inputArgs.AddLastArg(arguments, options::OPT_ir_profile_generate);
336336
inputArgs.AddLastArg(arguments, options::OPT_cs_profile_generate);
337+
inputArgs.AddLastArg(arguments, options::OPT_cs_profile_generate_EQ);
337338
inputArgs.AddLastArg(arguments, options::OPT_profile_coverage_mapping);
338339
inputArgs.AddAllArgs(arguments, options::OPT_warning_treating_Group);
339340
inputArgs.AddLastArg(arguments, options::OPT_sanitize_EQ);

lib/Frontend/CompilerInvocation.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3501,7 +3501,7 @@ static bool ParseIRGenArgs(IRGenOptions &Opts, ArgList &Args,
35013501
Opts.EnableCSIRProfileGen = Args.hasArg(OPT_cs_profile_generate) ||
35023502
Args.hasArg(OPT_cs_profile_generate_EQ);
35033503
if (auto V = Args.getLastArgValue(OPT_cs_profile_generate_EQ); !V.empty())
3504-
Opts.CSProfileGenFile = V.str();
3504+
Opts.InstrProfileOutput = V.str();
35053505

35063506
Opts.DebugInfoForProfiling |= Args.hasArg(OPT_debug_info_for_profiling);
35073507

lib/IRGen/IRGen.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,8 @@ static void populatePGOOptions(std::optional<PGOOptions> &Out,
234234
if (Opts.EnableCSIRProfileGen) {
235235
const bool hasUse = !Opts.UseProfile.empty();
236236
Out = PGOOptions(
237-
/*ProfileFile=*/ hasUse ? Opts.UseProfile : "",
238-
/*CSProfileGenFile=*/ Opts.CSProfileGenFile,
237+
/*ProfileFile=*/ Opts.UseProfile,
238+
/*CSProfileGenFile=*/ Opts.InstrProfileOutput,
239239
/*ProfileRemappingFile=*/ "",
240240
/*MemoryProfile=*/ "",
241241
/*FS=*/ llvm::vfs::getRealFileSystem(),
@@ -249,7 +249,7 @@ static void populatePGOOptions(std::optional<PGOOptions> &Out,
249249

250250
if (Opts.EnableIRProfileGen) {
251251
Out = PGOOptions(
252-
/*ProfileFile=*/ "",
252+
/*ProfileFile=*/ Opts.InstrProfileOutput,
253253
/*CSProfileGenFile=*/ "",
254254
/*ProfileRemappingFile=*/ "",
255255
/*MemoryProfile=*/ "",

test/IRGen/cs_profile_generate.sil

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// RUN: %target-swift-frontend -cs-profile-generate -O -emit-ir %s | %FileCheck %s
2+
3+
sil_stage canonical
4+
5+
import Builtin
6+
import Swift
7+
import SwiftShims
8+
9+
sil @b : $@convention(thin) () -> ()
10+
11+
sil @c : $@convention(thin) () -> ()
12+
13+
14+
// CHECK: @__llvm_profile_runtime = external hidden global
15+
16+
// counter array (2 counters for the then/else)
17+
// CHECK: @__profc_a = {{.*}} global [2 x i64]
18+
// data record pointing at the counter array and the function
19+
// CHECK: @__profd_a = {{.*}} global {{.*}} ptr @a.local
20+
21+
22+
// CHECK: br i1 {{.*}}, label %[[THEN:[0-9]+]], label %[[ELSE:[0-9]+]]
23+
// THEN: increment counter[0] and call b()
24+
// CHECK: [[THEN]]:
25+
// CHECK: %[[L0:.*]] = load i64, ptr @__profc_a, align 8
26+
// CHECK: %[[A0:.*]] = add i64 %[[L0]], 1
27+
// CHECK: store i64 %[[A0]], ptr @__profc_a, align 8
28+
// CHECK: call swiftcc void @b()
29+
// CHECK: br label %[[MERGE:[0-9]+]]
30+
31+
// ELSE: increment counter[1] and call c()
32+
// CHECK: [[ELSE]]:
33+
// CHECK: %[[L1:.*]] = load i64, ptr getelementptr inbounds (i8, ptr @__profc_a, i64 8), align 8
34+
// CHECK: %[[A1:.*]] = add i64 %[[L1]], 1
35+
// CHECK: store i64 %[[A1]], ptr getelementptr inbounds (i8, ptr @__profc_a, i64 8), align 8
36+
// CHECK: call swiftcc void @c()
37+
// CHECK: br label %[[MERGE]]
38+
39+
// CHECK: [[MERGE]]:
40+
// CHECK: ret void
41+
42+
// CHECK: declare swiftcc void @c()
43+
// CHECK: declare swiftcc void @b()
44+
45+
sil @a : $@convention(thin) (Bool) -> () {
46+
bb0(%0 : $Bool):
47+
%2 = struct_extract %0, #Bool._value
48+
cond_br %2, bb1, bb2
49+
50+
bb1:
51+
// function_ref b()
52+
%4 = function_ref @b : $@convention(thin) () -> ()
53+
%5 = apply %4() : $@convention(thin) () -> ()
54+
br bb3
55+
56+
bb2:
57+
// function_ref c()
58+
%7 = function_ref @c: $@convention(thin) () -> ()
59+
%8 = apply %7() : $@convention(thin) () -> ()
60+
br bb3
61+
62+
bb3:
63+
%10 = tuple ()
64+
return %10
65+
}

test/IRGen/ir_profile_generate.sil

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// RUN: %target-swift-frontend -ir-profile-generate -emit-ir %s | %FileCheck %s
2+
3+
sil_stage canonical
4+
5+
import Builtin
6+
import Swift
7+
import SwiftShims
8+
9+
sil @b : $@convention(thin) () -> ()
10+
11+
sil @c : $@convention(thin) () -> ()
12+
13+
// CHECK: @__llvm_profile_runtime = external hidden global
14+
15+
// counter array (2 counters for the then/else)
16+
// CHECK: @__profc_a = {{.*}} global [2 x i64]
17+
// data record pointing at the counter array and the function
18+
// CHECK: @__profd_a = {{.*}} global {{.*}} ptr @a.local
19+
20+
// CHECK: br i1 {{.*}}, label %[[THEN:[0-9]+]], label %[[ELSE:[0-9]+]]
21+
22+
// THEN: increment counter[0] and call b()
23+
// CHECK: [[THEN]]:
24+
// CHECK: %[[L0:.*]] = load i64, ptr @__profc_a, align 8
25+
// CHECK: %[[A0:.*]] = add i64 %[[L0]], 1
26+
// CHECK: store i64 %[[A0]], ptr @__profc_a, align 8
27+
// CHECK: call swiftcc void @b()
28+
// CHECK: br label %[[MERGE:[0-9]+]]
29+
30+
// ELSE: increment counter[1] and call c()
31+
// CHECK: [[ELSE]]:
32+
// CHECK: %[[L1:.*]] = load i64, ptr getelementptr inbounds ([2 x i64], ptr @__profc_a, i32 0, i32 1), align 8
33+
// CHECK: %[[A1:.*]] = add i64 %[[L1]], 1
34+
// CHECK: store i64 %[[A1]], ptr getelementptr inbounds ([2 x i64], ptr @__profc_a, i32 0, i32 1), align 8
35+
// CHECK: call swiftcc void @c()
36+
// CHECK: br label %[[MERGE]]
37+
38+
// CHECK: [[MERGE]]:
39+
// CHECK: ret void
40+
41+
// CHECK: declare swiftcc void @c()
42+
// CHECK: declare swiftcc void @b()
43+
// CHECK: declare void @llvm.instrprof.increment
44+
45+
sil @a : $@convention(thin) (Bool) -> () {
46+
bb0(%0 : $Bool):
47+
%2 = struct_extract %0, #Bool._value
48+
cond_br %2, bb1, bb2
49+
50+
bb1:
51+
// function_ref b()
52+
%4 = function_ref @b : $@convention(thin) () -> ()
53+
%5 = apply %4() : $@convention(thin) () -> ()
54+
br bb3
55+
56+
bb2:
57+
// function_ref c()
58+
%7 = function_ref @c: $@convention(thin) () -> ()
59+
%8 = apply %7() : $@convention(thin) () -> ()
60+
br bb3
61+
62+
bb3:
63+
%10 = tuple ()
64+
return %10
65+
}

0 commit comments

Comments
 (0)