Skip to content

Fix CUDA event binding compatibility and arch forwarding - #7

Closed
mohamedsamirx wants to merge 1 commit into
NVIDIA:mainfrom
mohamedsamirx:fix-cuda-event-compat-arch-forwarding
Closed

mohamedsamirx wants to merge 1 commit into
NVIDIA:mainfrom
mohamedsamirx:fix-cuda-event-compat-arch-forwarding

Conversation

@mohamedsamirx

@mohamedsamirx mohamedsamirx commented May 8, 2026 •

Copy link
Copy Markdown
Contributor

Summary

This PR fixes two setup/runtime issues found while validating cuda-oxide on a CUDA 12.4 system with an sm_75 NVIDIA T1200 GPU.

  • Add a CUDA Driver API compatibility wrapper for event elapsed timing.
  • Use cuEventElapsedTime_v2 when the installed CUDA headers expose it.
  • Fall back to cuEventElapsedTime for older CUDA 12.x headers such as CUDA 12.4.
  • Forward explicit cargo oxide --arch values to the normal PTX generation path through CUDA_OXIDE_TARGET, while preserving backend auto-target selection when --arch is omitted.

Motivation

CUDA 12.4 headers generate a cuEventElapsedTime binding, not cuEventElapsedTime_v2, which caused cuda-core to fail to compile.

Also, cargo oxide run vecadd --arch sm_75 accepted the --arch flag but did not apply it for normal PTX output. The generated PTX still targeted the auto-selected architecture, causing PTX JIT failures on sm_75 hardware. With this change, --arch sm_75 correctly generates .target sm_75.

The change preserves the existing default behavior when --arch is omitted: the backend still performs auto-target selection.

Testing

cargo fmt --check
cargo check
cargo clippy -p cuda-bindings -p cuda-core -p cargo-oxide -- -D warnings
cargo test -p cuda-bindings -p cuda-core -p cargo-oxide
cargo oxide doctor
unset CUDA_OXIDE_TARGET && cargo oxide build vecadd
unset CUDA_OXIDE_TARGET && cargo oxide run vecadd --arch sm_75

When --arch is omitted, the generated vecadd.ptx keeps the backend-selected default:

.target sm_80

With --arch sm_75, the generated vecadd.ptx contains:

.target sm_75

and the example prints:

SUCCESS: All 1024 elements correct!

@nihalpasham

Copy link
Copy Markdown
Collaborator

Landed via #15 with a verified maintainer signature while preserving your authorship. For future PRs, it would be great if you could use verified commit signatures so branch protection can merge them directly. Thanks again for the fix!

@nihalpasham nihalpasham closed this May 9, 2026
@mohamedsamirx

Copy link
Copy Markdown
Contributor Author

Sure, thank you

brandonros added a commit to brandonros/cuda-oxide that referenced this pull request May 12, 2026
…a case

Three small fixes to make `cargo oxide` and `cuda-bindings` work on
nix-managed and aarch64 environments without exporting extra env vars
or symlinking toolkit directories:

* `cargo-oxide/src/backend.rs` (NVIDIA#5): when `CARGO_TARGET_DIR` (or
  `CARGO_BUILD_TARGET_DIR`) is set, the child `cargo build` writes
  `librustc_codegen_cuda.so` there, not at
  `crates/rustc-codegen-cuda/target/debug/`. Backend discovery was
  hard-coded to the latter and silently returned a non-existent path,
  forcing users to set `CUDA_OXIDE_BACKEND` manually. Added a
  `codegen_target_dir(...)` helper that honors the env var with a
  fallback to `<crate>/target`, and routed all three call sites
  (`find_or_build_backend`, `build_backend_from_source`,
  `auto_fetch_and_build`) through it.

* `cuda-bindings/build.rs` (NVIDIA#6): `collect_lib_paths` only probed
  `{toolkit}/lib64{,/stubs}` and `{toolkit}/targets/x86_64-linux/`,
  which miss the aarch64 / nix layout where `libcuda.so` lives at
  `{toolkit}/lib/stubs/`. Now probes `lib{,/stubs}` in addition to
  `lib64{,/stubs}`, plus a per-arch `targets/<arch>-linux/lib{,/stubs}`
  driven by `CARGO_CFG_TARGET_ARCH` (x86_64 / aarch64; aarch64 also
  probes `sbsa-linux` for SBSA redist archives).

* `cuda-bindings/build.rs` (NVIDIA#7): `cuda_toolkit_dir()` only honored
  `CUDA_TOOLKIT_PATH` before defaulting to `/usr/local/cuda`. Most
  environment-management tools (nix, conda, modules) set `CUDA_HOME`
  / `CUDA_PATH` / `CUDA_ROOT` instead, and a missing toolkit detection
  silently slid to v1 of `cuEventElapsedTime` even on CUDA 13.2+ where
  that symbol no longer exists. Fall through the four common names in
  order, and add `rerun-if-env-changed` lines for each.

Verified on this aarch64 nix toolkit: the build script now emits
`rustc-link-search=native=...lib/stubs/` (where libcuda.so actually
lives) where before it would have emitted nothing usable. All
existing examples (vecadd, array_index, str_panic_path, slice_range,
slice_const_idx_write, cross_crate_kernel) still build cleanly.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
brandonros added a commit to brandonros/cuda-oxide that referenced this pull request May 12, 2026
…a case

Three small fixes to make `cargo oxide` and `cuda-bindings` work on
nix-managed and aarch64 environments without exporting extra env vars
or symlinking toolkit directories:

* `cargo-oxide/src/backend.rs` (NVIDIA#5): when `CARGO_TARGET_DIR` (or
  `CARGO_BUILD_TARGET_DIR`) is set, the child `cargo build` writes
  `librustc_codegen_cuda.so` there, not at
  `crates/rustc-codegen-cuda/target/debug/`. Backend discovery was
  hard-coded to the latter and silently returned a non-existent path,
  forcing users to set `CUDA_OXIDE_BACKEND` manually. Added a
  `codegen_target_dir(...)` helper that honors the env var with a
  fallback to `<crate>/target`, and routed all three call sites
  (`find_or_build_backend`, `build_backend_from_source`,
  `auto_fetch_and_build`) through it.

* `cuda-bindings/build.rs` (NVIDIA#6): `collect_lib_paths` only probed
  `{toolkit}/lib64{,/stubs}` and `{toolkit}/targets/x86_64-linux/`,
  which miss the aarch64 / nix layout where `libcuda.so` lives at
  `{toolkit}/lib/stubs/`. Now probes `lib{,/stubs}` in addition to
  `lib64{,/stubs}`, plus a per-arch `targets/<arch>-linux/lib{,/stubs}`
  driven by `CARGO_CFG_TARGET_ARCH` (x86_64 / aarch64; aarch64 also
  probes `sbsa-linux` for SBSA redist archives).

* `cuda-bindings/build.rs` (NVIDIA#7): `cuda_toolkit_dir()` only honored
  `CUDA_TOOLKIT_PATH` before defaulting to `/usr/local/cuda`. Most
  environment-management tools (nix, conda, modules) set `CUDA_HOME`
  / `CUDA_PATH` / `CUDA_ROOT` instead, and a missing toolkit detection
  silently slid to v1 of `cuEventElapsedTime` even on CUDA 13.2+ where
  that symbol no longer exists. Fall through the four common names in
  order, and add `rerun-if-env-changed` lines for each.

Verified on this aarch64 nix toolkit: the build script now emits
`rustc-link-search=native=...lib/stubs/` (where libcuda.so actually
lives) where before it would have emitted nothing usable. All
existing examples (vecadd, array_index, str_panic_path, slice_range,
slice_const_idx_write, cross_crate_kernel) still build cleanly.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
brandonros added a commit to brandonros/cuda-oxide that referenced this pull request May 12, 2026
…a case

Three small fixes to make `cargo oxide` and `cuda-bindings` work on
nix-managed and aarch64 environments without exporting extra env vars
or symlinking toolkit directories:

* `cargo-oxide/src/backend.rs` (NVIDIA#5): when `CARGO_TARGET_DIR` (or
  `CARGO_BUILD_TARGET_DIR`) is set, the child `cargo build` writes
  `librustc_codegen_cuda.so` there, not at
  `crates/rustc-codegen-cuda/target/debug/`. Backend discovery was
  hard-coded to the latter and silently returned a non-existent path,
  forcing users to set `CUDA_OXIDE_BACKEND` manually. Added a
  `codegen_target_dir(...)` helper that honors the env var with a
  fallback to `<crate>/target`, and routed all three call sites
  (`find_or_build_backend`, `build_backend_from_source`,
  `auto_fetch_and_build`) through it.

* `cuda-bindings/build.rs` (NVIDIA#6): `collect_lib_paths` only probed
  `{toolkit}/lib64{,/stubs}` and `{toolkit}/targets/x86_64-linux/`,
  which miss the aarch64 / nix layout where `libcuda.so` lives at
  `{toolkit}/lib/stubs/`. Now probes `lib{,/stubs}` in addition to
  `lib64{,/stubs}`, plus a per-arch `targets/<arch>-linux/lib{,/stubs}`
  driven by `CARGO_CFG_TARGET_ARCH` (x86_64 / aarch64; aarch64 also
  probes `sbsa-linux` for SBSA redist archives).

* `cuda-bindings/build.rs` (NVIDIA#7): `cuda_toolkit_dir()` only honored
  `CUDA_TOOLKIT_PATH` before defaulting to `/usr/local/cuda`. Most
  environment-management tools (nix, conda, modules) set `CUDA_HOME`
  / `CUDA_PATH` / `CUDA_ROOT` instead, and a missing toolkit detection
  silently slid to v1 of `cuEventElapsedTime` even on CUDA 13.2+ where
  that symbol no longer exists. Fall through the four common names in
  order, and add `rerun-if-env-changed` lines for each.

Verified on this aarch64 nix toolkit: the build script now emits
`rustc-link-search=native=...lib/stubs/` (where libcuda.so actually
lives) where before it would have emitted nothing usable. All
existing examples (vecadd, array_index, str_panic_path, slice_range,
slice_const_idx_write, cross_crate_kernel) still build cleanly.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
brandonros added a commit to brandonros/cuda-oxide that referenced this pull request May 13, 2026
…a case

Three small fixes to make `cargo oxide` and `cuda-bindings` work on
nix-managed and aarch64 environments without exporting extra env vars
or symlinking toolkit directories:

* `cargo-oxide/src/backend.rs` (NVIDIA#5): when `CARGO_TARGET_DIR` (or
  `CARGO_BUILD_TARGET_DIR`) is set, the child `cargo build` writes
  `librustc_codegen_cuda.so` there, not at
  `crates/rustc-codegen-cuda/target/debug/`. Backend discovery was
  hard-coded to the latter and silently returned a non-existent path,
  forcing users to set `CUDA_OXIDE_BACKEND` manually. Added a
  `codegen_target_dir(...)` helper that honors the env var with a
  fallback to `<crate>/target`, and routed all three call sites
  (`find_or_build_backend`, `build_backend_from_source`,
  `auto_fetch_and_build`) through it.

* `cuda-bindings/build.rs` (NVIDIA#6): `collect_lib_paths` only probed
  `{toolkit}/lib64{,/stubs}` and `{toolkit}/targets/x86_64-linux/`,
  which miss the aarch64 / nix layout where `libcuda.so` lives at
  `{toolkit}/lib/stubs/`. Now probes `lib{,/stubs}` in addition to
  `lib64{,/stubs}`, plus a per-arch `targets/<arch>-linux/lib{,/stubs}`
  driven by `CARGO_CFG_TARGET_ARCH` (x86_64 / aarch64; aarch64 also
  probes `sbsa-linux` for SBSA redist archives).

* `cuda-bindings/build.rs` (NVIDIA#7): `cuda_toolkit_dir()` only honored
  `CUDA_TOOLKIT_PATH` before defaulting to `/usr/local/cuda`. Most
  environment-management tools (nix, conda, modules) set `CUDA_HOME`
  / `CUDA_PATH` / `CUDA_ROOT` instead, and a missing toolkit detection
  silently slid to v1 of `cuEventElapsedTime` even on CUDA 13.2+ where
  that symbol no longer exists. Fall through the four common names in
  order, and add `rerun-if-env-changed` lines for each.

Verified on this aarch64 nix toolkit: the build script now emits
`rustc-link-search=native=...lib/stubs/` (where libcuda.so actually
lives) where before it would have emitted nothing usable. All
existing examples (vecadd, array_index, str_panic_path, slice_range,
slice_const_idx_write, cross_crate_kernel) still build cleanly.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
nihalpasham pushed a commit that referenced this pull request Nov 27, 2026
Closes #1880.

## What This PR Does

Fixes an oracle-ID aliasing bug in `lean-exporter`
(`translate/program.rs`) where `fallback_rhs_for` accepted an explicit
ID map parameter and two call sites (`mir.extract_array_element` and
`nvvm.clc_query_*`) passed the recycled register map `&val_ids` instead
of the monotonic `&opaque_ids` map:

```text
BEFORE (`main`):
  Block 1:  `ld0 = mir.load(...)`                  ──► `opaque_ids` #7  ──► `OpaqueUint(7, 32)`
  Block 2:  `elem = mir.extract_array_element(..)` ──► `val_ids`    #7  ──► `OpaqueUint(7, 32)`  (COLLISION!)
            Lean treated two unrelated runtime values as equal (`env.opaqueUint t 7`)!

AFTER (This PR):
  `fallback_rhs_for` captures `opaque_ids` directly (and `CtaUint1` on `clc_query_*` also reads
  `opaque_ids`), so every opaque/CTA integer definition receives a strictly increasing ID.
```

---

## Changes

* **`crates/lean-exporter/src/translate/program.rs`**:
* Capture `opaque_ids` directly inside `fallback_rhs_for` and allocate
`clc_query_*` (`CtaUint1` / `OpaqueUint`) IDs from `opaque_ids`.
  * Remove the shadowed duplicate `"mir.array_element_addr"` match arm.
* **`crates/lean-exporter/tests/export.rs`**:
* Add `clc_query_and_array_element_use_distinct_opaque_ids` checking
global uniqueness and RPO monotonicity across a multi-block collision
reproducer and all shipped MIR fixtures.

---

## Verification

* **Lean 4 & Exporter Suite**: `CUDA_OXIDE_REQUIRE_LEAN=1 cargo test -p
lean-exporter --all-targets`: **PASS**
* **Lints & Guards**: `cargo clippy -p lean-exporter --all-targets -- -D
warnings`, `rustdoc -D warnings`, `cargo oxide fmt --check`, and
`check-*.sh` guards: **PASS**

Signed-off-by: hs1167 <[email protected]>
Signed-off-by: nihalpasham <[email protected]>
Co-authored-by: OpenBot <335604065+openbotgit[bot]@users.noreply.github.com>
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.

2 participants