Skip to content

Instrument only host code in the coverage build - #3193

Open
ndgrigorian wants to merge 3 commits into
masterfrom
fix-coverage-device-counters
Open

ndgrigorian wants to merge 3 commits into
masterfrom
fix-coverage-device-counters

Conversation

@ndgrigorian

Copy link
Copy Markdown
Collaborator

This PR fixes a random segfault of the OpenCL CPU runtime in the coverage build, which fails the coverage job of PRs, such as #3157 in test_sumprod.py, and makes it rely on the retry elsewhere. The crash is always at libintelocl.so+0x2d5f026: a TBB worker in NDRange::Init calls clDevCmdStatusChanged on a freed command.

In a coverage build, -fprofile-instr-generate -fcoverage-mapping also instrument device code, and the device compiler puts every __profc_ counter of a module into each of its kernel images, where SYCL treats them as device globals. This happens for the dpnp sources compiled into the libtensor modules (choose.cpp and putmask.cpp give each _tensor_impl image about 800 counters), and for every target that keeps the global flags: the pybind11 extensions, dpnp_backend_c and the Cython modules. With them, a few dpnp.tensor.concat calls on opencl:cpu segfault in about half the runs; concat copies with _copy_usm_ndarray_into_usm_ndarray of _tensor_impl.

Device counts never reach the profile, so this PR instruments the host side only (-Xarch_host), as the static libtensor sources already are: in the global PROFILE_FLAGS, for the dpnp sources of the libtensor modules and under DPNP_TENSOR_GENERATE_COVERAGE_FOR_PYBIND11_EXTENSIONS. tensor_build_flags.cmake now strips PROFILE_FLAGS as a whole. Release builds are unchanged.

Checked on a local coverage build of master, on opencl:cpu (Core Ultra 7 265U):

  • The reproducer (10 runs of the concat calls) crashes 0 times with the change and 5 times without it; third_party/cupy/math_tests/test_sumprod.py passes 4 runs out of 4, against 3 crashes out of 3 without it.
  • No kernel image of any module has a __profc_ counter left (13261 of 23417 images had them before), and host coverage is still recorded, e.g. 78% of the regions of choose.cpp after one dpnp.choose call.
  • With DPNP_TENSOR_GENERATE_COVERAGE_FOR_PYBIND11_EXTENSIONS=ON, the libtensor sources get only the -Xarch_host forms; that configuration was not built.

The full coverage job wasn't run locally; the CI run of this PR is the check for that.

  • Have you provided a meaningful PR description?
  • Have you added a test, reproducer or referred to an issue with a reproducer?
  • Have you tested your changes locally for CPU and GPU devices?
  • Have you made sure that new changes do not introduce compiler warnings?
  • Have you checked performance impact of proposed changes?
  • Have you added documentation for your changes, if necessary?
  • Have you added your changes to the changelog?

Device-side -fprofile-instr-generate puts every __profc_ counter of a
module into each of its kernel images as a device global. The OpenCL CPU
runtime then segfaults at random in NDRange::Init, calling
clDevCmdStatusChanged on a freed command; dpt.concat in _tensor_impl,
whose images carry the counters of choose.cpp and putmask.cpp, crashes
in about half the runs. Device counts never reach the profile, so
instrument the host side only, as the static libtensor sources already
are.
@github-actions

Copy link
Copy Markdown
Contributor

View rendered docs @ https://intelpython.github.io/dpnp/pull/3193/index.html

@github-actions

Copy link
Copy Markdown
Contributor

Array API standard conformance tests for dpnp=0.22.0dev2=np2py314h8d9cdd5_39 ran successfully.
Passed: 1491
Failed: 0
Skipped: 6

@coveralls

coveralls commented Nov 20, 2026 •

Copy link
Copy Markdown
Collaborator

Coverage Status

No base build to compare — fix-coverage-device-counters into master

@antonwolfy antonwolfy left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested this PR rebased on current master (I resolved the conflict myself, see the inline comment) against master itself. Both were Coverage builds with the CMake options of gen_coverage.py and DPC++ 2026.2.0, run on the CPU device of a Xeon Platinum 8480+ (OpenCL CPU runtime 2026.22.10) and on a Data Center GPU Max 1100 (Level Zero).

The fix works.

  • No kernel image keeps a __profc_ counter. On master, 83 of the 90 images of libdpnp_backend_c, _blas_impl and _rng_impl have them, and stripped copies of _tensor_impl and _tensor_elementwise_impl still hold 4.1 and 5.0 million __profc_ strings (1186 and 2246 counters). With this PR no module has any.
  • The coverage build gets much smaller: all its .so files take 262 MB instead of 1980 MB, and _tensor_impl is 46 MB instead of 749 MB.
  • On opencl:cpu, third_party/cupy/math_tests/test_sumprod.py crashed master in 2 of 3 runs, in an elementwise call under dpnp.gradient, and passed in 3 of 3 with this PR. In the full dpnp and dpnp.tensor suites under xdist, master lost 130 and 141 workers to crashes, this PR none. The concat reproducer didn't crash master on this Xeon in 20 runs.
  • Coverage stays the same. The Coveralls build of this PR has master's totals at its base: 30569 of 35301 lines and 1876 of 3144 branches. On the GPU, the per-line C++ report of the dpnp and dpnp.tensor suites is identical on master and with this PR: 12394 of 16273 lines and 1909 of 3156 branches, with no line different.

DPNP_TENSOR_GENERATE_COVERAGE_FOR_PYBIND11_EXTENSIONS=ON works with this PR and is broken on master. On master the build fails to link _tensor_impl, _tensor_elementwise_impl, _tensor_reductions_impl and _tensor_sorting_impl, with 84 relocation R_X86_64_PC32 out of range errors: the device counters push these modules past 2 GiB. With this PR it builds, the SHELL: forms keep both -Xarch_host in every compile command, and no image has a counter. gen_coverage.py --tensor-cpp-coverage then adds 126 libtensor files with real counts: 24225 of their 29638 lines are covered after the dpnp and dpnp.tensor suites. The description says this configuration wasn't built; it could say that the PR makes it build, and the changelog entry could mention it too.

Device counts can reach a profile, just not dpnp's. The new comment and the description say that the device counts never reach the profile. With DPC++ 2026.2 they do in a standalone program: when the SYCL runtime frees the counters' device globals, it adds them to the host counters through __sycl_increment_profile_counters of the profile runtime. A small kernel built with -fprofile-instr-generate -fcoverage-mapping gets exact device counts on both devices that way. In dpnp they can't arrive:

  • libsycl refers to __sycl_increment_profile_counters as a weak undefined symbol and binds it when it's loaded. dpctl loads libsycl before any instrumented module, so the reference stays null. The same instrumented library, loaded with ctypes after import dpctl, gets no device counts.
  • Even when the reference binds, only the module it bound to gets counts. With two instrumented libraries in one process, the kernel lines of the second one stay at 0.
  • The counts are added only when the device globals are freed, and a queue that lives until exit (as dpctl's cached ones do) gets none.

So the PR loses nothing, and an option to keep device instrumentation wouldn't bring any data back. It's the loading order that drops the counts, not the toolchain, and the inline comment suggests a wording.

No other CMake file needs a change. The link options without -Xarch_host add no device counters (no image has any), the dpnp.tensor Cython modules get no C++ instrumentation with or without this PR, and simplify_iteration_space.cpp, which isn't compiled as SYCL, is still measured.

Comment thread CMakeLists.txt
Comment on lines +378 to +379
# Host only: device counters become device globals in every kernel image of
# their module, which crash the OpenCL CPU runtime, and never reach the profile

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The counts can reach a profile with this toolchain (see the review), so the reason could name what drops them in dpnp:

Suggested change
# Host only: device counters become device globals in every kernel image of
# their module, which crash the OpenCL CPU runtime, and never reach the profile
# Host only: device counters become device globals in every kernel image of
# their module and crash the OpenCL CPU runtime. Their counts are lost anyway:
# libsycl, loaded by dpctl first, never binds __sycl_increment_profile_counters

"${_clang_prefix}-fno-finite-math-only"
)
if(DPNP_GENERATE_COVERAGE)
# host only, as PROFILE_FLAGS in the top-level CMakeLists.txt

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This conflicts with master now, here and in CHANGELOG.md. #3138 made the dpnp sources of the radix select modules host only with _dpnp_radix_select_sources: the AND NOT _src_fn IN_LIST _dpnp_radix_select_sources condition on this line, the list's entry in the set_property below, and the comment there that ends with "The device counts never reach the profile anyway". With this change every source in _dpnp_sources is host only, so the rebase can drop the list, the condition, the entry and that part of the comment. I resolved it that way for my runs, and the rebased build then has no device counter in any module, _statistics_impl and _tensor_sorting_impl included.

@vlad-perevezentsev vlad-perevezentsev left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked the coverage configuration with icpx 2026.1.1: no compile command instruments device code any more, the host counters stay the same, and the device code of the compiled objects has no counters. Agreed with @antonwolfy on the comment wording, on the rebase on master, which now conflicts, and on mentioning the tensor pybind11 coverage build in the changelog.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants