ARM-software / LLVM-embedded-toolchain-for-Arm

A project dedicated to building LLVM toolchain for 32-bit Arm embedded targets.
Apache License 2.0
377 stars 85 forks source link

Port proflib.c to LLVM-18 INSTR_PROF_RAW_VERSION 9 #422

Closed smithp35 closed 3 months ago

smithp35 commented 3 months ago

There have been a number of changes to the profile output since LLVM17 that need to be taken account of:

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.