Skip to content

Commit 934b4d3

Browse files
authored
Comgr: add support for coverage collection (llvm#2088)
2 parents 8866630 + a53c3e9 commit 934b4d3

File tree

3 files changed

+78
-26
lines changed

3 files changed

+78
-26
lines changed

amd/comgr/CMakeLists.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,27 @@ if (CMAKE_SIZEOF_VOID_P EQUAL 4)
265265
set_target_properties(amd_comgr PROPERTIES OUTPUT_NAME "amd_comgr32")
266266
endif()
267267

268+
option(LLVM_BUILD_INSTRUMENTED_COVERAGE "Build LLVM and tools with Code Coverage instrumentation" Off)
269+
mark_as_advanced(LLVM_BUILD_INSTRUMENTED_COVERAGE)
270+
if(LLVM_BUILD_INSTRUMENTED_COVERAGE)
271+
if(NOT LLVM_PROFILE_MERGE_POOL_SIZE)
272+
# A pool size of 1-2 is probably sufficient on an SSD. 3-4 should be fine
273+
# for spinning disks. Anything higher may only help on slower mediums.
274+
set(LLVM_PROFILE_MERGE_POOL_SIZE "4")
275+
endif()
276+
if(NOT LLVM_PROFILE_FILE_PATTERN)
277+
if(NOT LLVM_PROFILE_DATA_DIR)
278+
file(TO_NATIVE_PATH "${LLVM_BINARY_DIR}/profiles" LLVM_PROFILE_DATA_DIR)
279+
endif()
280+
file(TO_NATIVE_PATH "${LLVM_PROFILE_DATA_DIR}/%${LLVM_PROFILE_MERGE_POOL_SIZE}m.profraw" LLVM_PROFILE_FILE_PATTERN)
281+
endif()
282+
set(INSTRUMENTED_COVERAGE_FLAGS -O0 -fprofile-instr-generate=${LLVM_PROFILE_FILE_PATTERN} -fcoverage-mapping)
283+
list(APPEND AMD_COMGR_PRIVATE_COMPILE_OPTIONS ${INSTRUMENTED_COVERAGE_FLAGS})
284+
list(APPEND AMD_COMGR_PUBLIC_COMPILE_OPTIONS ${INSTRUMENTED_COVERAGE_FLAGS})
285+
list(APPEND AMD_COMGR_PRIVATE_LINKER_OPTIONS ${INSTRUMENTED_COVERAGE_FLAGS} -L${LLVM_LIBRARY_DIRS})
286+
list(APPEND AMD_COMGR_PUBLIC_LINKER_OPTIONS ${INSTRUMENTED_COVERAGE_FLAGS} -L${LLVM_LIBRARY_DIRS})
287+
endif()
288+
268289
target_compile_options(amd_comgr
269290
PRIVATE "${AMD_COMGR_PRIVATE_COMPILE_OPTIONS}")
270291
target_compile_definitions(amd_comgr

amd/comgr/README.md

Lines changed: 55 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,68 +20,67 @@ branch. LLVM should be built with at least
2020

2121
An example `bash` session to build Comgr on Linux using GNUMakefiles is:
2222

23-
$ LLVM_PROJECT=~/llvm-project
24-
$ DEVICE_LIBS=~/llvm-project/amd/device-libs
25-
$ COMGR=~/llvm-project/amd/comgr
26-
$ mkdir -p "$LLVM_PROJECT/build"
27-
$ cd "$LLVM_PROJECT/build"
23+
$ LLVM_PROJECT=~/llvm-project/build
24+
$ DEVICE_LIBS=~/llvm-project/amd/device-libs/build
25+
$ mkdir -p "$LLVM_PROJECT"
26+
$ cd "$LLVM_PROJECT"
2827
$ cmake \
2928
-DCMAKE_BUILD_TYPE=Release \
3029
-DLLVM_ENABLE_PROJECTS="llvm;clang;lld" \
3130
-DLLVM_TARGETS_TO_BUILD="AMDGPU;X86" \
3231
../llvm
3332
$ make
34-
$ mkdir -p "$DEVICE_LIBS/build"
35-
$ cd "$DEVICE_LIBS/build"
33+
$ mkdir -p "$DEVICE_LIBS"
34+
$ cd "$DEVICE_LIBS"
3635
$ cmake \
3736
-DCMAKE_BUILD_TYPE=Release \
38-
-DCMAKE_PREFIX_PATH="$LLVM_PROJECT/build" \
37+
-DCMAKE_PREFIX_PATH="$LLVM_PROJECT" \
3938
..
4039
$ make
41-
$ mkdir -p "$COMGR/build"
42-
$ cd "$COMGR/build"
40+
$ cd ~/llvm-project/amd/comgr
41+
$ mkdir -p build; cd build;
4342
$ cmake \
4443
-DCMAKE_BUILD_TYPE=Release \
45-
-DCMAKE_PREFIX_PATH="$LLVM_PROJECT/build;$DEVICE_LIBS/build" \
44+
-DCMAKE_PREFIX_PATH="$LLVM_PROJECT;$DEVICE_LIBS" \
4645
..
4746
$ make
4847
$ make test
4948

5049
The equivalent on Windows in `cmd.exe` using Visual Studio project files is:
5150

52-
> set LLVM_PROJECT="%HOMEPATH%\llvm-project"
53-
> set DEVICE_LIBS="%HOMEPATH%\llvm-project\amd\device-libs"
54-
> set COMGR="%HOMEPATH%\llvm-project\amd\comgr"
55-
> mkdir "%LLVM_PROJECT%\build"
56-
> cd "%LLVM_PROJECT%\build"
51+
> set LLVM_PROJECT="%HOMEPATH%\llvm-project\build"
52+
> set DEVICE_LIBS="%HOMEPATH%\llvm-project\amd\device-libs\build"
53+
> mkdir "%LLVM_PROJECT%"
54+
> cd "%LLVM_PROJECT%"
5755
> cmake ^
5856
-DLLVM_ENABLE_PROJECTS="llvm;clang;lld" ^
5957
-DLLVM_TARGETS_TO_BUILD="AMDGPU;X86" ^
6058
..\llvm
6159
> msbuild /p:Configuration=Release ALL_BUILD.vcxproj
62-
> mkdir "%DEVICE_LIBS%\build"
63-
> cd "%DEVICE_LIBS%\build"
60+
> mkdir "%DEVICE_LIBS%"
61+
> cd "%DEVICE_LIBS%"
6462
> cmake ^
65-
-DCMAKE_PREFIX_PATH="%LLVM_PROJECT%\build" ^
63+
-DCMAKE_PREFIX_PATH="%LLVM_PROJECT%" ^
6664
..
6765
> msbuild /p:Configuration=Release ALL_BUILD.vcxproj
68-
> mkdir "%COMGR%\build"
69-
> cd "%COMGR%\build"
66+
> cd "%HOMEPATH%\llvm-project\amd\comgr"
67+
> mkdir build
68+
> cd build
7069
> cmake ^
71-
-DCMAKE_PREFIX_PATH="%LLVM_PROJECT%\build;%DEVICE_LIBS%\build" ^
70+
-DCMAKE_PREFIX_PATH="%LLVM_PROJECT%;%DEVICE_LIBS%" ^
7271
..
7372
> msbuild /p:Configuration=Release ALL_BUILD.vcxproj
7473
> msbuild /p:Configuration=Release RUN_TESTS.vcxproj
7574

76-
Optionally,
75+
**ASAN support:** Optionally,
7776
[AddressSanitizer](https://github.com/google/sanitizers/wiki/AddressSanitizer)
7877
may be enabled during development via `-DADDRESS_SANITIZER=On` during the Comgr
7978
`cmake` step.
8079

81-
Comgr can be built as a static library by passing
80+
**Static Comgr:** Comgr can be built as a static library by passing
8281
`-DCOMGR_BUILD_SHARED_LIBS=OFF` during the Comgr `cmake` step.
8382

84-
To enable SPIRV support, checkout
83+
**SPIRV Support:** To enable SPIRV support, checkout
8584
[SPIRV-LLVM-Translator](https://github.com/ROCm/SPIRV-LLVM-Translator) in
8685
`llvm/projects` or `llvm/tools` and build using the above instructions, with the
8786
exception that the `-DCMAKE_PREFIX_PATH` for llvm-project must be an install
@@ -92,6 +91,37 @@ Comgr SPIRV-related APIs can be disabled by passing
9291
`-DCOMGR_DISABLE_SPIRV=1` during the Comgr `cmake` step. This removes any
9392
dependency on LLVM SPIRV libraries or the llvm-spirv tool.
9493

94+
**Code Coverage Instrumentation:** Comgr supports source-based [code coverage
95+
via clang](https://clang.llvm.org/docs/SourceBasedCodeCoverage.html), and
96+
leverages the same CMake variables as
97+
[LLVM](https://www.llvm.org/docs/CMake.html#llvm-related-variables)
98+
(LLVM_BUILD_INSTRUMENTED_COVERAGE, etc.).
99+
100+
Example of insturmenting with covereage, generating profiles, and creating an
101+
HTML for investigation:
102+
103+
$ cmake -DCMAKE_STRIP="" -DLLVM_PROFILE_DATA_DIR=`pwd`/profiles \
104+
-DLLVM_BUILD_INSTRUMENTED_COVERAGE=On \
105+
-DCMAKE_CXX_COMPILER="$LLVM_PROJECT/bin/clang++" \
106+
-DCMAKE_C_COMPILER="$LLVM_PROJECT/bin/clang" \
107+
-DCMAKE_BUILD_TYPE=Release \
108+
-DCMAKE_PREFIX_PATH="$LLVM_PROJECT;$DEVICE_LIBS" ..
109+
$ make -j
110+
$ make test test-lit
111+
$ cd profiles
112+
# Manually aggregate the data and create text report.
113+
$ $LLVM_PROJECT/bin/llvm-profdata merge -sparse *.profraw -o \
114+
comgr_test.profdata # merge and index data
115+
$ $LLVM_PROJECT/bin/llvm-cov report ../libamd_comgr.so \
116+
-instr-profile=comgr_test.profdata \
117+
-ignore-filename-regex="[cl].*/include/*" # show test report without \
118+
includes
119+
# Or use python script to aggregate the data and create html report.
120+
$ $LLVM_PROJECT/../llvm/utils/prepare-code-coverage-artifact.py \
121+
--preserve-profiles $LLVM_PROJECT/bin/llvm-profdata \
122+
$LLVM_PROJECT/bin/llvm-cov . html ../libamd_comgr.so \
123+
# create html report
124+
95125
Depending on the Code Object Manager
96126
------------------------------------
97127

amd/comgr/docs/ReleaseNotes.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,10 @@ Comgr Testing, Debugging, and Logging Updates
6060
---------------------------------------------
6161
- Removed HIP\_PATH and ROCM\_PATH environment variables. These were used for
6262
now-removed Comgr actions, such as \*COMPILE\_SOURCE\_TO\_FATBIN.
63-
- Added a new Comgr LIT testing infrastrucutre, which can be found in
63+
- Added a new Comgr LIT testing infrastructure, which can be found in
6464
amd/comgr/test-lit. This will allow us to write more in-depth and targeted
6565
tests.
66+
- Added support for source-based code coverage. See README.md for more details.
6667

6768
New Targets
6869
-----------

0 commit comments

Comments
 (0)