Skip to content

Commit c963a90

Browse files
authored
Merge pull request #61046 from hamishknight/cant-pass-this-up
2 parents 90c791e + 00217ae commit c963a90

File tree

3 files changed

+125
-9
lines changed

3 files changed

+125
-9
lines changed

lib/IRGen/IRGen.cpp

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,20 @@ performOptimizationsUsingLegacyPassManger(const IRGenOptions &Opts,
245245
// Set up a pipeline.
246246
PassManagerBuilderWrapper PMBuilder(Opts);
247247

248+
// If we're generating a profile, add the lowering pass now.
249+
if (Opts.GenerateProfile) {
250+
// TODO: Surface the option to emit atomic profile counter increments at
251+
// the driver level.
252+
// Configure the module passes.
253+
legacy::PassManager ModulePasses;
254+
ModulePasses.add(createTargetTransformInfoWrapperPass(
255+
TargetMachine->getTargetIRAnalysis()));
256+
InstrProfOptions Options;
257+
Options.Atomic = bool(Opts.Sanitizers & SanitizerKind::Thread);
258+
ModulePasses.add(createInstrProfilingLegacyPass(Options));
259+
ModulePasses.run(*Module);
260+
}
261+
248262
if (Opts.shouldOptimize() && !Opts.DisableLLVMOptzns) {
249263
PMBuilder.OptLevel = 2; // -Os
250264
PMBuilder.SizeLevel = 1; // -Os
@@ -355,15 +369,6 @@ performOptimizationsUsingLegacyPassManger(const IRGenOptions &Opts,
355369
ModulePasses.add(createTargetTransformInfoWrapperPass(
356370
TargetMachine->getTargetIRAnalysis()));
357371

358-
// If we're generating a profile, add the lowering pass now.
359-
if (Opts.GenerateProfile) {
360-
// TODO: Surface the option to emit atomic profile counter increments at
361-
// the driver level.
362-
InstrProfOptions Options;
363-
Options.Atomic = bool(Opts.Sanitizers & SanitizerKind::Thread);
364-
ModulePasses.add(createInstrProfilingLegacyPass(Options));
365-
}
366-
367372
PMBuilder.populateModulePassManager(ModulePasses);
368373

369374
// The PMBuilder only knows about LLVM AA passes. We should explicitly add

test/Profiler/rdar77217762.sil

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
2+
// rdar://77217762 – The increment_profiler_counter in $foo gets optimized out by
3+
// LLVM function optimization, but make sure we still emit the name data which
4+
// is required by the coverage record.
5+
6+
// Make sure both the legacy and new pass manager work.
7+
// We need to disable SIL optimizations to ensure LLVM optimizes out the
8+
// profiler increment.
9+
// RUN: %target-swift-frontend -emit-ir -profile-generate -profile-coverage-mapping -O -disable-sil-perf-optzns -disable-new-llvm-pass-manager %s | %FileCheck %s
10+
// RUN: %target-swift-frontend -emit-ir -profile-generate -profile-coverage-mapping -O -disable-sil-perf-optzns -enable-new-llvm-pass-manager %s | %FileCheck %s
11+
12+
sil_stage canonical
13+
14+
import Swift
15+
import Builtin
16+
17+
// CHECK-DAG: @__covrec
18+
// CHECK-DAG: @__llvm_coverage_mapping
19+
// CHECK-DAG: @__llvm_prf_nm
20+
21+
sil [ossa] @main : $@convention(c) (Int32, UnsafeMutablePointer<Optional<UnsafeMutablePointer<Int8>>>) -> Int32 {
22+
bb0(%0 : $Int32, %1 : $UnsafeMutablePointer<Optional<UnsafeMutablePointer<Int8>>>):
23+
%2 = integer_literal $Builtin.Int32, 0
24+
%3 = struct $Int32 (%2 : $Builtin.Int32)
25+
return %3 : $Int32
26+
}
27+
28+
sil [ossa] @$foo : $@convention(thin) () -> () {
29+
bb0:
30+
%1 = integer_literal $Builtin.Int1, 0
31+
cond_br %1, bb1, bb2
32+
33+
bb1:
34+
increment_profiler_counter 0, "$foo", num_counters 1, hash 0
35+
br bb3
36+
37+
bb2:
38+
br bb3
39+
40+
bb3:
41+
%10 = tuple ()
42+
return %10 : $()
43+
}
44+
45+
sil_coverage_map "main.swift" "$foo" "$foo" 0 {
46+
10:6 -> 10:11 : 0
47+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// RUN: %empty-directory(%t)
2+
3+
// rdar://77217762 – The increment_profiler_counter in $foo gets optimized out by
4+
// LLVM function optimization, but make sure we still emit the name data which
5+
// is required by the coverage record.
6+
7+
// Make sure both the legacy and new pass manager work.
8+
// We need to disable SIL optimizations to ensure LLVM optimizes out the
9+
// profiler increment.
10+
// RUN: %target-build-swift %s -profile-generate -profile-coverage-mapping -O -Xfrontend -disable-sil-perf-optzns -Xfrontend -disable-new-llvm-pass-manager -o %t/main1
11+
// RUN: %target-build-swift %s -profile-generate -profile-coverage-mapping -O -Xfrontend -disable-sil-perf-optzns -Xfrontend -enable-new-llvm-pass-manager -o %t/main2
12+
13+
// This unusual use of 'sh' allows the path of the profraw file to be
14+
// substituted by %target-run.
15+
// RUN: %target-codesign %t/main1
16+
// RUN: %target-codesign %t/main2
17+
// RUN: %target-run sh -c 'env LLVM_PROFILE_FILE=$1 $2' -- %t/default1.profraw %t/main1
18+
// RUN: %target-run sh -c 'env LLVM_PROFILE_FILE=$1 $2' -- %t/default2.profraw %t/main2
19+
20+
// RUN: %llvm-profdata merge %t/default1.profraw -o %t/default1.profdata
21+
// RUN: %llvm-profdata merge %t/default2.profraw -o %t/default2.profdata
22+
23+
// RUN: %llvm-cov report %t/main1 -instr-profile=%t/default1.profdata
24+
// RUN: %llvm-cov report %t/main2 -instr-profile=%t/default2.profdata
25+
26+
// REQUIRES: profile_runtime
27+
// REQUIRES: executable_test
28+
// REQUIRES: OS=macosx
29+
30+
sil_stage canonical
31+
32+
import Swift
33+
import Builtin
34+
35+
sil [ossa] @main : $@convention(c) (Int32, UnsafeMutablePointer<Optional<UnsafeMutablePointer<Int8>>>) -> Int32 {
36+
bb0(%0 : $Int32, %1 : $UnsafeMutablePointer<Optional<UnsafeMutablePointer<Int8>>>):
37+
increment_profiler_counter 0, "__tlcd_line:12:1", num_counters 1, hash 0
38+
%3 = function_ref @$foo : $@convention(thin) () -> ()
39+
%4 = apply %3() : $@convention(thin) () -> ()
40+
%5 = integer_literal $Builtin.Int32, 0
41+
%6 = struct $Int32 (%5 : $Builtin.Int32)
42+
return %6 : $Int32
43+
}
44+
45+
sil [ossa] @$foo : $@convention(thin) () -> () {
46+
bb0:
47+
%1 = integer_literal $Builtin.Int1, 0
48+
cond_br %1, bb1, bb2
49+
50+
bb1:
51+
increment_profiler_counter 0, "$foo", num_counters 1, hash 0
52+
br bb3
53+
54+
bb2:
55+
br bb3
56+
57+
bb3:
58+
%10 = tuple ()
59+
return %10 : $()
60+
}
61+
62+
sil_coverage_map "main.swift" "$foo" "$foo" 0 {
63+
10:6 -> 10:11 : 0
64+
}

0 commit comments

Comments
 (0)