Skip to content

Commit 298f0c5

Browse files
authored
Port proflib.c to LLVM-18 INSTR_PROF_RAW_VERSION 9 (#422)
There have been a number of changes to the profile output since LLVM17 that need to be taken account of: * ELF targets no longer do self-registration. * The profile version has been raised to 9. In addition we want the sample to visualize the profile with the run command so that we can more easily be alerted to changes in the profile format. The largest change is the removal of self-registration. When this was in operation each object file has a .init_array entry with a pointer to a registration function that would be called prior to main. This registration function would register the names and counters of all the functions, allowing the program to calculate at run-time the extent of the counter and names section. Without self-registration we need to use linker defined __start_ and __stop_ section symbols to find the extent. The profile version increasing to 9 has added a bitmap section that we need to account for. This section should be empty unless MC/DC coverage is used. The majority of the source code has been adapted from files in llvm-project/compiler-rt/lib/profile and llvm-project/compiler-rt/include/profile To replicate the behaviour of the previous example, we make a C __attribute__((constructor)) function that runs before main to register an atexit handler that writes out the profile after exiting main.
1 parent 46e7450 commit 298f0c5

File tree

4 files changed

+184
-142
lines changed

4 files changed

+184
-142
lines changed

samples/src/cpp-baremetal-semihosting-prof/Makefile

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,12 @@ hello.elf: *.cpp
2828

2929
run: hello.hex
3030
qemu-system-arm -M microbit -semihosting -nographic -device loader,file=$<
31+
$(BIN_PATH)/llvm-profdata merge -sparse default.profraw -o hello.profdata
32+
$(BIN_PATH)/llvm-cov show hello.elf -instr-profile=hello.profdata
3133

3234
debug: hello.hex
3335
qemu-system-arm -M microbit -semihosting -nographic -device loader,file=$< -s -S
3436

35-
prof: run
36-
llvm-profdata merge -sparse default.profraw -o hello.profdata
37-
llvm-cov show hello.elf -instr-profile=hello.profdata
38-
3937
clean:
4038
rm -f *.elf *.hex *.profraw *.profdata *.o
4139

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# Code profiling and coverage sample
22

3-
This sample shows how to use instrumentation to emit profile data
3+
This sample shows how to use instrumentation to emit profile data
44
and use it to show code coverage.
55

66
Use `make run` to build with instrumentation, run and collect the raw profile data,
7-
then `make prof` to visualize the code coverage.
7+
then output a visualization of the code coverage.
88

99
NOTE: The upstream runtime implementation changes regularly,
1010
thus `proflib.c` file needs to be updated to keep in sync.

samples/src/cpp-baremetal-semihosting-prof/make.bat

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
:make
2323
@if [%target%]==[build] goto :build
2424
@if [%target%]==[run] goto :run
25-
@if [%target%]==[prof] goto :prof
2625
@if [%target%]==[clean] goto :clean
2726
@echo Error: unknown target "%target%"
2827
@exit /B 1
@@ -38,9 +37,6 @@
3837
@call :build_fn
3938
:do_run
4039
qemu-system-arm.exe -M microbit -semihosting -nographic -device loader,file=hello.hex
41-
@exit /B
42-
43-
:prof
4440
llvm-profdata.exe merge -sparse default.profraw -o hello.profdata
4541
llvm-cov.exe show hello.elf -instr-profile=hello.profdata
4642
@exit /B

0 commit comments

Comments
 (0)