Skip to content

Commit 9047e9c

Browse files
authored
Rollup merge of rust-lang#60038 - michaelwoerister:pgo-updates-2, r=alexcrichton
Add codegen test for PGO instrumentation. This PR adds a codegen test that makes sure that LLVM actually generates instrumentation code when we enable PGO instrumentation in `rustc`. The second commit updates a test case to the new commandline option syntax introduced in rust-lang#59874. Without the fix the test still works, but it confusingly creates a directory called `test.profraw`, which usually is the name of the _file_ where profiling data is collected.
2 parents f38b9c9 + cc77087 commit 9047e9c

File tree

14 files changed

+65
-34
lines changed

14 files changed

+65
-34
lines changed

src/bootstrap/test.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1268,11 +1268,11 @@ impl Step for Compiletest {
12681268
builder.add_rust_test_threads(&mut cmd);
12691269

12701270
if builder.config.sanitizers {
1271-
cmd.env("SANITIZER_SUPPORT", "1");
1271+
cmd.env("RUSTC_SANITIZER_SUPPORT", "1");
12721272
}
12731273

12741274
if builder.config.profiler {
1275-
cmd.env("PROFILER_SUPPORT", "1");
1275+
cmd.env("RUSTC_PROFILER_SUPPORT", "1");
12761276
}
12771277

12781278
cmd.env("RUST_TEST_TMPDIR", builder.out.join("tmp"));
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Test that `-Zpgo-gen` creates expected instrumentation artifacts in LLVM IR.
2+
3+
// needs-profiler-support
4+
// compile-flags: -Z pgo-gen -Ccodegen-units=1
5+
6+
// CHECK: @__llvm_profile_raw_version =
7+
// CHECK: @__profc_{{.*}}pgo_instrumentation{{.*}}some_function{{.*}} = private global
8+
// CHECK: @__profd_{{.*}}pgo_instrumentation{{.*}}some_function{{.*}} = private global
9+
// CHECK: @__profc_{{.*}}pgo_instrumentation{{.*}}main{{.*}} = private global
10+
// CHECK: @__profd_{{.*}}pgo_instrumentation{{.*}}main{{.*}} = private global
11+
// CHECK: @__llvm_profile_filename = {{.*}}"default_%m.profraw\00"{{.*}}
12+
13+
#[inline(never)]
14+
fn some_function() {
15+
16+
}
17+
18+
fn main() {
19+
some_function();
20+
}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
# needs-profiler-support
2+
13
-include ../tools.mk
24

35
all:
4-
ifeq ($(PROFILER_SUPPORT),1)
56
$(RUSTC) -Copt-level=3 -Clto=fat -Z pgo-gen="$(TMPDIR)" test.rs
67
$(call RUN,test) || exit 1
78
[ -e "$(TMPDIR)"/default_*.profraw ] || (echo "No .profraw file"; exit 1)
8-
endif
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1+
# needs-profiler-support
2+
13
-include ../tools.mk
24

35
all:
4-
ifeq ($(PROFILER_SUPPORT),1)
5-
$(RUSTC) -O -Ccodegen-units=1 -Z pgo-gen="$(TMPDIR)/test.profraw" --emit=llvm-ir test.rs
6+
$(RUSTC) -O -Ccodegen-units=1 -Z pgo-gen="$(TMPDIR)" --emit=llvm-ir test.rs
67
# We expect symbols starting with "__llvm_profile_".
78
$(CGREP) "__llvm_profile_" < $(TMPDIR)/test.ll
89
# We do NOT expect the "__imp_" version of these symbols.
910
$(CGREP) -v "__imp___llvm_profile_" < $(TMPDIR)/test.ll # 64 bit
1011
$(CGREP) -v "__imp____llvm_profile_" < $(TMPDIR)/test.ll # 32 bit
11-
endif
+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
# needs-profiler-support
2+
13
-include ../tools.mk
24

35
all:
4-
ifeq ($(PROFILER_SUPPORT),1)
56
$(RUSTC) -g -Z pgo-gen="$(TMPDIR)" test.rs
67
$(call RUN,test) || exit 1
78
[ -e "$(TMPDIR)"/default_*.profraw ] || (echo "No .profraw file"; exit 1)
8-
endif
+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
# needs-profiler-support
2+
13
-include ../tools.mk
24

35
all:
4-
ifeq ($(PROFILER_SUPPORT),1)
56
$(RUSTC) -g -Z profile test.rs
67
$(call RUN,test) || exit 1
78
[ -e "$(TMPDIR)/test.gcno" ] || (echo "No .gcno file"; exit 1)
89
[ -e "$(TMPDIR)/test.gcda" ] || (echo "No .gcda file"; exit 1)
9-
endif

src/test/run-make-fulldeps/sanitizer-address/Makefile

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1+
# needs-sanitizer-support
2+
13
-include ../tools.mk
24

35
LOG := $(TMPDIR)/log.txt
46

57
# NOTE the address sanitizer only supports x86_64 linux and macOS
68

79
ifeq ($(TARGET),x86_64-apple-darwin)
8-
ASAN_SUPPORT=$(SANITIZER_SUPPORT)
910
EXTRA_RUSTFLAG=-C rpath
1011
else
1112
ifeq ($(TARGET),x86_64-unknown-linux-gnu)
12-
ASAN_SUPPORT=$(SANITIZER_SUPPORT)
1313

1414
# Apparently there are very specific Linux kernels, notably the one that's
1515
# currently on Travis CI, which contain a buggy commit that triggers failures in
@@ -23,7 +23,5 @@ endif
2323
endif
2424

2525
all:
26-
ifeq ($(ASAN_SUPPORT),1)
2726
$(RUSTC) -g -Z sanitizer=address -Z print-link-args $(EXTRA_RUSTFLAG) overflow.rs | $(CGREP) librustc_asan
2827
$(TMPDIR)/overflow 2>&1 | $(CGREP) stack-buffer-overflow
29-
endif
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# needs-sanitizer-support
2+
13
-include ../tools.mk
24

35
LOG := $(TMPDIR)/log.txt
@@ -8,15 +10,12 @@ LOG := $(TMPDIR)/log.txt
810
# is correctly detected.
911

1012
ifeq ($(TARGET),x86_64-unknown-linux-gnu)
11-
ASAN_SUPPORT=$(SANITIZER_SUPPORT)
1213

1314
# See comment in sanitizer-address/Makefile for why this is here
1415
EXTRA_RUSTFLAG=-C relocation-model=dynamic-no-pic
1516
endif
1617

1718
all:
18-
ifeq ($(ASAN_SUPPORT),1)
1919
$(RUSTC) -g -Z sanitizer=address --crate-type cdylib --target $(TARGET) $(EXTRA_RUSTFLAG) library.rs
2020
$(RUSTC) -g -Z sanitizer=address --crate-type bin --target $(TARGET) $(EXTRA_RUSTFLAG) -llibrary program.rs
2121
LD_LIBRARY_PATH=$(TMPDIR) $(TMPDIR)/program 2>&1 | $(CGREP) stack-buffer-overflow
22-
endif
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# needs-sanitizer-support
2+
13
-include ../tools.mk
24

35
LOG := $(TMPDIR)/log.txt
@@ -8,15 +10,11 @@ LOG := $(TMPDIR)/log.txt
810
# is correctly detected.
911

1012
ifeq ($(TARGET),x86_64-unknown-linux-gnu)
11-
ASAN_SUPPORT=$(SANITIZER_SUPPORT)
12-
1313
# See comment in sanitizer-address/Makefile for why this is here
1414
EXTRA_RUSTFLAG=-C relocation-model=dynamic-no-pic
1515
endif
1616

1717
all:
18-
ifeq ($(ASAN_SUPPORT),1)
1918
$(RUSTC) -g -Z sanitizer=address --crate-type dylib --target $(TARGET) $(EXTRA_RUSTFLAG) library.rs
2019
$(RUSTC) -g -Z sanitizer=address --crate-type bin --target $(TARGET) $(EXTRA_RUSTFLAG) -llibrary program.rs
2120
LD_LIBRARY_PATH=$(TMPDIR) $(TMPDIR)/program 2>&1 | $(CGREP) stack-buffer-overflow
22-
endif
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
1+
# needs-sanitizer-support
2+
13
-include ../tools.mk
24

35
# NOTE the address sanitizer only supports x86_64 linux and macOS
46

57
ifeq ($(TARGET),x86_64-apple-darwin)
6-
ASAN_SUPPORT=$(SANITIZER_SUPPORT)
78
EXTRA_RUSTFLAG=-C rpath
89
else
910
ifeq ($(TARGET),x86_64-unknown-linux-gnu)
10-
ASAN_SUPPORT=$(SANITIZER_SUPPORT)
1111
EXTRA_RUSTFLAG=
1212
endif
1313
endif
1414

1515
all:
16-
ifeq ($(ASAN_SUPPORT),1)
1716
$(RUSTC) -Z sanitizer=address --crate-type proc-macro --target $(TARGET) hello.rs 2>&1 | $(CGREP) '-Z sanitizer'
18-
endif
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
-include ../tools.mk
22

3+
# needs-sanitizer-support
34
# only-linux
45
# only-x86_64
56
# ignore-test
67
# FIXME(#46126) ThinLTO for libstd broke this test
78

89
all:
9-
ifdef SANITIZER_SUPPORT
1010
$(RUSTC) -C opt-level=1 -g -Z sanitizer=leak -Z print-link-args leak.rs | $(CGREP) librustc_lsan
1111
$(TMPDIR)/leak 2>&1 | $(CGREP) 'detected memory leaks'
12-
endif
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
-include ../tools.mk
22

3+
# needs-sanitizer-support
34
# only-linux
45
# only-x86_64
56

67
all:
7-
ifdef SANITIZER_SUPPORT
88
$(RUSTC) -g -Z sanitizer=memory -Z print-link-args uninit.rs | $(CGREP) librustc_msan
99
$(TMPDIR)/uninit 2>&1 | $(CGREP) use-of-uninitialized-value
10-
endif
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
1+
# needs-sanitizer-support
2+
13
-include ../tools.mk
24

35
# This test builds a staticlib, then an executable that links to it.
4-
# The staticlib and executable both are compiled with address sanitizer,
6+
# The staticlib and executable both are compiled with address sanitizer,
57
# and we assert that a fault in the staticlib is correctly detected.
68

79
ifeq ($(TARGET),x86_64-unknown-linux-gnu)
8-
ASAN_SUPPORT=$(SANITIZER_SUPPORT)
910
EXTRA_RUSTFLAG=
1011
endif
1112

1213
all:
13-
ifeq ($(ASAN_SUPPORT),1)
1414
$(RUSTC) -g -Z sanitizer=address --crate-type staticlib --target $(TARGET) library.rs
1515
$(CC) program.c $(call STATICLIB,library) $(call OUT_EXE,program) $(EXTRACFLAGS) $(EXTRACXXFLAGS)
1616
LD_LIBRARY_PATH=$(TMPDIR) $(TMPDIR)/program 2>&1 | $(CGREP) stack-buffer-overflow
17-
endif
1817

src/tools/compiletest/src/header.rs

+21
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ impl EarlyProps {
8888
}
8989
}
9090

91+
let rustc_has_profiler_support = env::var_os("RUSTC_PROFILER_SUPPORT").is_some();
92+
let rustc_has_sanitizer_support = env::var_os("RUSTC_SANITIZER_SUPPORT").is_some();
93+
9194
iter_header(testfile, None, &mut |ln| {
9295
// we should check if any only-<platform> exists and if it exists
9396
// and does not matches the current platform, skip the test
@@ -116,6 +119,16 @@ impl EarlyProps {
116119
config.parse_needs_matching_clang(ln) {
117120
props.ignore = Ignore::Ignore;
118121
}
122+
123+
if !rustc_has_profiler_support &&
124+
config.parse_needs_profiler_support(ln) {
125+
props.ignore = Ignore::Ignore;
126+
}
127+
128+
if !rustc_has_sanitizer_support &&
129+
config.parse_needs_sanitizer_support(ln) {
130+
props.ignore = Ignore::Ignore;
131+
}
119132
}
120133

121134
if (config.mode == common::DebugInfoGdb || config.mode == common::DebugInfoBoth) &&
@@ -748,6 +761,14 @@ impl Config {
748761
self.parse_name_directive(line, "needs-matching-clang")
749762
}
750763

764+
fn parse_needs_profiler_support(&self, line: &str) -> bool {
765+
self.parse_name_directive(line, "needs-profiler-support")
766+
}
767+
768+
fn parse_needs_sanitizer_support(&self, line: &str) -> bool {
769+
self.parse_name_directive(line, "needs-sanitizer-support")
770+
}
771+
751772
/// Parses a name-value directive which contains config-specific information, e.g., `ignore-x86`
752773
/// or `normalize-stderr-32bit`.
753774
fn parse_cfg_name_directive(&self, line: &str, prefix: &str) -> ParsedNameDirective {

0 commit comments

Comments
 (0)