Skip to content

intrinsics: add globaltimer - #52

Merged
nihalpasham merged 2 commits into
NVIDIA:mainfrom
josephglanville:jpg/intrinsics-globaltimer
May 27, 2026
Merged

nihalpasham merged 2 commits into
NVIDIA:mainfrom
josephglanville:jpg/intrinsics-globaltimer

Conversation

@josephglanville

@josephglanville josephglanville commented May 13, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Adds a CUDA device-side globaltimer() debug intrinsic that reads PTX %globaltimer as a 64-bit timer value. This complements clock64() with a global timer source that is better suited for measurements that may span multiple SMs.

Example

use cuda_device::debug;

let start = debug::globaltimer();

// ... operation to measure ...

let end = debug::globaltimer();
let ticks = debug::elapsed_ticks(start, end);

Implementation

  • Adds cuda_device::debug::globaltimer() -> u64 and cuda_device::debug::elapsed_ticks(start, end) -> u64.
  • Adds the nvvm.read_ptx_sreg_globaltimer op to dialect-nvvm, including registration and result-type verification.
  • Wires cuda_device::debug::globaltimer through MIR intrinsic dispatch.
  • Lowers the NVVM op to convergent inline asm:
mov.u64 $0, %globaltimer;
  • Updates debug and dialect documentation to include the new timer intrinsic.
  • Updates the debug example to exercise globaltimer() alongside clock64().

Validation

  • Adds test_globaltimer_lowers_to_inline_asm, which verifies that ReadPtxSregGlobaltimerOp lowers to the expected inline asm template and is marked convergent

@josephglanville
josephglanville force-pushed the jpg/intrinsics-globaltimer branch from 9cf660b to 9c60086 Compare May 13, 2026 06:01
Signed-off-by: Joseph Glanville <[email protected]>
@josephglanville
josephglanville force-pushed the jpg/intrinsics-globaltimer branch from 9c60086 to 4c23506 Compare May 13, 2026 06:25
Switch `nvvm.read_ptx_sreg_globaltimer` from inline asm to the named
LLVM intrinsic `llvm.nvvm.read.ptx.sreg.globaltimer`, matching how the
sibling SREG reads (clock, clock64, tid, ctaid, ntid, lane, cluster)
already lower. This drops the unnecessary `convergent` attribute (SREG
reads are not warp-synchronous primitives) and lets LLVM model the call
as a typed intrinsic instead of an opaque asm blob.

Emitted PTX is byte-identical (`mov.u64 %rdN, %globaltimer;`), verified
against `llc-21.1.8` (project floor). The `debug` example still runs
end-to-end with correct cycle measurements.

Doc/comment touch-ups for parity with the clock64 entries:
- `cuda-device/src/debug.rs`: refresh the lowering hint in the
  `globaltimer()` stub to reference the LLVM intrinsic.
- `dialect-nvvm/src/ops/debug.rs`: complete the ASCII-table row so it
  reads `%globaltimer / ...globaltimer` like the Clock/Clock64 rows.
- `mir-lower/src/convert/intrinsics/debug.rs`: update the lowering
  table to reflect the new conversion.

The lowering unit test is renamed to
`test_globaltimer_lowers_to_intrinsic_call` and reshaped to mirror
`test_intrinsic_insertion`: it asserts the `llvm_nvvm_read_ptx_sreg_globaltimer`
declaration is emitted, the kernel body holds a direct call to it, and
adds a regression guard that the lowering must not produce inline asm.

Co-Authored-By: Joseph Glanville <[email protected]>
Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>

@nihalpasham nihalpasham left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for adding this

I've layered a few changes on top before merging:

  • Lower nvvm.read_ptx_sreg_globaltimer via the named LLVM intrinsic
    rather than inline asm.
    llvm.nvvm.read.ptx.sreg.globaltimer
    is a real NVPTX intrinsic supported well below our LLVM 21
    (verified with llc-21.1.8: emits exactly the same mov.u64 %rdN, %globaltimer;).
  • Going through the named intrinsic also drops the unnecessary convergent
    attribute; SREG reads don't require warp-synchronous execution, only
    primitives like shfl / vote / barrier / mbarrier / cp.async.bulk do.
  • Updated the unit test to mirror the existing test_intrinsic_insertion
    pattern (assert the declare + the call, plus a regression guard that
    globaltimer must not lower to inline asm).

@nihalpasham
nihalpasham merged commit c7b7fa0 into NVIDIA:main May 27, 2026
20 checks passed
nihalpasham added a commit to josephglanville/cuda-oxide that referenced this pull request May 27, 2026
No conflicts. Brings in 51 commits since the PR's merge-base
(b6a06ff), including PR NVIDIA#44 (embedded IR + binary artifacts) and
PR NVIDIA#52 (globaltimer named intrinsic). Re-runs CI against the
merged state.
nihalpasham added a commit to ur4t/cuda-oxide that referenced this pull request May 27, 2026
Brings in the 20 commits that have landed since the PR's merge-base
(c8b3103), including PR NVIDIA#44, PR NVIDIA#52, and PR NVIDIA#60. Auto-merges cleanly.
meefs pushed a commit to meefs/cuda-oxide that referenced this pull request Sep 30, 2026
…l-actions-856a8fccb2

Bump the all-actions group with 4 updates
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