Skip to content

Instrument only host code in the coverage build - #3193

Merged
antonwolfy merged 6 commits into
masterfrom
fix-coverage-device-counters
Dec 1, 2026
Merged

antonwolfy merged 6 commits into
masterfrom
fix-coverage-device-counters

Conversation

@ndgrigorian

@ndgrigorian ndgrigorian commented Nov 20, 2026 •

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.

The device counts are lost in dpnp anyway: libsycl, loaded by dpctl before any instrumented module, never binds __sycl_increment_profile_counters, through which the profile runtime would add them. So this PR instruments the host side only (-Xarch_host), as the static libtensor sources already are: in the global PROFILE_FLAGS, for all dpnp sources of the libtensor modules and under DPNP_TENSOR_GENERATE_COVERAGE_FOR_PYBIND11_EXTENSIONS. The radix select sources, which #3138 made host only on their own, are covered by that too, so their separate list is gone. 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. This also makes that configuration build: on master the device counters push _tensor_impl, _tensor_elementwise_impl, _tensor_reductions_impl and _tensor_sorting_impl past 2 GiB, and their link fails with relocation R_X86_64_PC32 out of range (found in review).
  • After the rebase on master, all 134 instrumented compile commands of a reconfigured coverage tree have only the -Xarch_host forms; the modules weren't rebuilt locally after it.

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?

ndgrigorian added a commit that referenced this pull request Nov 20, 2026
@github-actions

github-actions Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

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

@github-actions

github-actions Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

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

@coveralls

coveralls commented Nov 20, 2026 •

Copy link
Copy Markdown
Collaborator

Coverage Status

coverage: 84.557%. remained the same — 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 Resolved
Comment thread dpnp/libtensor/source/CMakeLists.txt Resolved

@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.

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. The device counts are lost in dpnp anyway, since
libsycl is loaded by dpctl before any instrumented module and never binds
__sycl_increment_profile_counters, so instrument the host side only, as
the static libtensor sources already are. This makes all dpnp sources of
the libtensor modules host only, so the separate list of radix select
sources is dropped.

@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.

Approving. Everything from the first round is in: the comment names the loading order that drops the device counts, the radix select list is gone after the rebase, and the description and the changelog say that DPNP_TENSOR_GENERATE_COVERAGE_FOR_PYBIND11_EXTENSIONS=ON builds now. Apart from the comment text, the CMake changes are the ones I built and ran in the first round, so those results still apply. The Coveralls build of the previous head has exactly master's totals at its base: 30813 of 35539 lines and 1906 of 3156 branches.

I pushed two commits. One rewords the comment on the static libtensor sources, since the SHELL: target options for the tensor pybind11 coverage build keep the repeated -Xarch_host too. The other merges master and resolves the CHANGELOG.md conflict with #3207 and #3209 by keeping all three entries.

@antonwolfy

Copy link
Copy Markdown
Contributor

The red Build with nightly build of DPC++ toolchain job (Build with Open Source LLVM SYCL compiler) isn't caused by this PR. Since CMake 4.5.1 was published on PyPI on 2026-12-01, the job fails at Build and install dpctl on every PR: CMake 4.5 identifies the open source DPC++ nightly by the new compiler ID DPClang, which IntelSYCLConfig.cmake rejects ("Unsupported compiler family DPClang"). It's handled separately: #3215 pins the job's pip packages to the last green versions (CMake 4.4.5), and #3216 adds DPClang support to dpnp's IntelSYCLConfig.cmake. The job isn't required for merging.

@antonwolfy
antonwolfy merged commit 4be83d0 into master Dec 1, 2026
44 of 46 checks passed
@antonwolfy
antonwolfy deleted the fix-coverage-device-counters branch December 1, 2026 12:02
github-actions Bot added a commit that referenced this pull request Dec 1, 2026
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`.

The device counts are lost in dpnp anyway: libsycl, loaded by dpctl
before any instrumented module, never binds
`__sycl_increment_profile_counters`, through which the profile runtime
would add them. So this PR instruments the host side only
(`-Xarch_host`), as the static libtensor sources already are: in the
global `PROFILE_FLAGS`, for all dpnp sources of the libtensor modules
and under `DPNP_TENSOR_GENERATE_COVERAGE_FOR_PYBIND11_EXTENSIONS`. The
radix select sources, which #3138 made host only on their own, are
covered by that too, so their separate list is gone.
`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. This also makes that
configuration build: on master the device counters push `_tensor_impl`,
`_tensor_elementwise_impl`, `_tensor_reductions_impl` and
`_tensor_sorting_impl` past 2 GiB, and their link fails with `relocation
R_X86_64_PC32 out of range` (found in review).
- After the rebase on master, all 134 instrumented compile commands of a
reconfigured coverage tree have only the `-Xarch_host` forms; the
modules weren't rebuilt locally after it.

The full coverage job wasn't run locally; the CI run of this PR is the
check for that.
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