-
Notifications
You must be signed in to change notification settings - Fork 796
[SYCL] Implement coverage instrumentation for device code #20710
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
0x12CC
wants to merge
3
commits into
intel:sycl
Choose a base branch
from
0x12CC:device_code_coverage
base: sycl
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1385,11 +1385,7 @@ static ArrayRef<options::ID> getUnsupportedOpts() { | |
| options::OPT_fno_profile_generate, // -f[no-]profile-generate | ||
| options::OPT_ftest_coverage, | ||
| options::OPT_fno_test_coverage, // -f[no-]test-coverage | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If there are tests added for these options, please make sure they are removed as well. |
||
| options::OPT_fcoverage_mapping, | ||
| options::OPT_coverage, // --coverage | ||
| options::OPT_fno_coverage_mapping, // -f[no-]coverage-mapping | ||
| options::OPT_fprofile_instr_generate, | ||
| options::OPT_fprofile_instr_generate_EQ, | ||
| options::OPT_coverage, // --coverage | ||
| options::OPT_fprofile_arcs, | ||
| options::OPT_fno_profile_arcs, // -f[no-]profile-arcs | ||
| options::OPT_fno_profile_instr_generate, // -f[no-]profile-instr-generate | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| ; RUN: opt < %s -passes=instrprof -S | FileCheck %s | ||
|
|
||
| target triple = "spir64-unknown-unknown" | ||
|
|
||
| @__profn_foo = private constant [3 x i8] c"foo" | ||
| ; CHECK: @__profc_foo = private global { ptr addrspace(1), i64 } zeroinitializer, section "__llvm_prf_cnts", comdat #0 | ||
| ; CHECK: @__profd_foo = private global { i64, i64, i64, i64, ptr, ptr, i32, [3 x i16], i32 } { i64 {{.*}}, i64 {{.*}}, i64 sub (i64 ptrtoint (ptr @__profc_foo to i64) | ||
| @__profn_bar = private constant [3 x i8] c"bar" | ||
| ; CHECK: @__profc_bar = private global { ptr addrspace(1), i64 } zeroinitializer, section "__llvm_prf_cnts", comdat #1 | ||
| ; CHECK: @__profd_bar = private global { i64, i64, i64, i64, ptr, ptr, i32, [3 x i16], i32 } { i64 {{.*}}, i64 {{.*}}, i64 sub (i64 ptrtoint (ptr @__profc_bar to i64) | ||
|
|
||
| ; CHECK: @__llvm_prf_nm = {{.*}} section "__llvm_prf_names" | ||
|
|
||
| define void @_Z3foov() { | ||
| call void @llvm.instrprof.cover(ptr @__profn_foo, i64 12345678, i32 1, i32 0) | ||
| ; CHECK: %pgocount.addr = load ptr addrspace(1), ptr @__profc_foo, align 8 | ||
| ; CHECK: store i8 0, ptr addrspace(1) %pgocount.addr, align 1 | ||
| ret void | ||
| } | ||
|
|
||
| %class.A = type { ptr } | ||
| define dso_local void @_Z3barv(ptr nocapture nonnull align 8 %0) unnamed_addr #0 align 2 { | ||
| call void @llvm.instrprof.cover(ptr @__profn_bar, i64 87654321, i32 1, i32 0) | ||
| ; CHECK: %pgocount.addr = load ptr addrspace(1), ptr @__profc_bar, align 8 | ||
| ; CHECK: store i8 0, ptr addrspace(1) %pgocount.addr, align 1 | ||
| ret void | ||
| } | ||
|
|
||
| declare void @llvm.instrprof.cover(ptr, i64, i32, i32) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| # Design for Device-side Code Coverage | ||
|
|
||
| ## Overview | ||
|
|
||
| This document describes the design and implementation of device-side code coverage for SYCL, extending Clang's source-based code coverage to support device code. The approach leverages the existing SYCL device global infrastructure, as detailed in the [DeviceGlobal.md](DeviceGlobal.md) design document, to enable collection and aggregation of coverage data from device kernels. | ||
|
|
||
| ## Design Details | ||
|
|
||
| ### Profiling Counter Representation | ||
|
|
||
| Profiling counters for code coverage are lowered by the compiler as device globals. Specifically, the `InstrProfilingLoweringPass` is modified so that, when targeting SPIR-V, coverage counters are represented as pointers to USM buffers, matching the representation of other SYCL device globals. This indirection allows counters to be relocatable and managed consistently with other device-side global variables. | ||
|
|
||
| Each counter is annotated with a unique identifier (`sycl-unique-id`) of the form `__profc_<fn_hash>`, where `<fn_hash>` is a 64-bit unsigned integer uniquely identifying the instrumented function. The counter's size is also recorded via the `sycl-device-global-size` attribute. These attributes ensure that counters are discoverable and manageable by the SYCL runtime and integration headers/footers. | ||
|
|
||
| The profile counter device global is represented as an array of 8-byte integers (`std::uint64_t`). The number of elements in this array corresponds to the number of regions in the function being instrumented, where a region typically represents a distinct code branch or block. The size of the device global variable is therefore determined by multiplying the number of regions by eight bytes, and this value is recorded in the `sycl-device-global-size` attribute for use by the runtime and integration logic. | ||
|
|
||
| ### Integration with Device Global Infrastructure | ||
|
|
||
| The device global infrastructure, as described in [DeviceGlobal.md](DeviceGlobal.md), provides mechanisms for mapping host and device instances of global variables, managing their lifetimes, and facilitating data transfer. Device-side coverage counters are treated as a special class of device globals: | ||
|
|
||
| - They use the shared allocation type rather than the device allocation type for the underlying USM memory. | ||
| - They do not have corresponding `device_global` declarations in host code. | ||
| - Their lifetime and cleanup are managed via the device global map, with integration footer code ensuring registration and deregistration. | ||
|
|
||
| ### Runtime Handling and Data Aggregation | ||
|
|
||
| When a device global entry corresponding to a coverage counter is released (e.g., when a device image is unloaded), the SYCL runtime aggregates the values from the device-side counter into the equivalent host-side counter. Equivalence is determined by matching both the `<fn_hash>` and the number of counter regions. If no matching host-side counter exists—typically due to differences in code between host and device caused by the `__SYCL_DEVICE_ONLY__` macro—the device-side counter values are discarded. | ||
|
|
||
| The aggregation is performed by invoking a new function in the compiler runtime, `__sycl_increment_profile_counters`, which is weakly linked to accommodate optional runtime availability. This function accepts the `<fn_hash>`, the number of regions, and the increment values, and updates the host-side counters accordingly. At program exit, the final profile data reflects the sum of host and device coverage counters. | ||
|
|
||
| ### Compiler and Runtime Changes | ||
|
|
||
| #### Compiler Frontend | ||
|
|
||
| - The lowering pass for coverage counters is updated to emit device globals with the appropriate attributes and indirection. | ||
| - Integration headers and footers are updated to register device global counters with the runtime, using the unique identifier and size. | ||
|
|
||
| #### SYCL Runtime | ||
|
|
||
| - Device globals with IDs matching the `__profc_<fn_hash>` pattern are recognized as coverage counters. | ||
| - USM allocation and management for counters is handled as for other device globals, but without host-side declarations. | ||
| - Upon cleanup, device-side counter values are aggregated into host-side counters via the runtime API. | ||
|
|
||
| #### Compiler Runtime | ||
|
|
||
| - The new function `__sycl_increment_profile_counters` is introduced to update host-side counters. | ||
| - The function is weakly linked to allow for optional inclusion. | ||
|
|
||
| ### Limitations and Considerations | ||
|
|
||
| - The feature is currently implemented only for SPIR-V targets; CUDA and HIP backends are not supported. | ||
| - Devices lacking support for device globals cannot utilize device-side code coverage. | ||
| - Differences in code between host and device (e.g., due to `__SYCL_DEVICE_ONLY__`) may prevent aggregation of coverage data for some functions. | ||
| - The design relies on the robustness of the device global infrastructure for correct mapping and lifetime management. | ||
|
|
||
| ## Relationship to Device Global Design | ||
|
|
||
| This feature is built upon the mechanisms described in [DeviceGlobal.md](DeviceGlobal.md), including: | ||
|
|
||
| - Use of unique string identifiers (`sycl-unique-id`) for mapping and management. | ||
| - USM-based allocation and zero-initialization of device-side storage. | ||
| - Integration header/footer registration for host-device correlation. | ||
| - Runtime database for device global management and lookup. | ||
|
|
||
| The code coverage counters are a specialized use case of device globals, with additional logic for aggregation and profile generation. | ||
|
|
||
| ## References | ||
|
|
||
| - [Implementation design for SYCL device globals](DeviceGlobal.md) | ||
| - [Clang Source-based Code Coverage](https://clang.llvm.org/docs/SourceBasedCodeCoverage.html) | ||
| - [SYCL Specification](https://registry.khronos.org/SYCL/specs/sycl-2020/html/sycl-2020.html) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this option only be passed when instrumentation is enabled? Also, please add/update a test verifying that this option being passed when needed.