Skip to content

fix(ptx): internalize non-root device helpers - #387

Merged
nihalpasham merged 5 commits into
NVIDIA:mainfrom
niklebedenko:codex/upstream-internalize-ptx-helpers
Jul 23, 2026
Merged

nihalpasham merged 5 commits into
NVIDIA:mainfrom
niklebedenko:codex/upstream-internalize-ptx-helpers

Conversation

@niklebedenko

Copy link
Copy Markdown
Contributor

Closes #386.

Summary

Make LLVM's optimized PTX pipeline internalize compiler-private device helpers so ordinary inlining and GlobalDCE can remove their unused definitions. Preserve the module's exporter-derived public ABI, including kernel entries, standalone device-function roots, and defined globals.

Before: fully inlined helpers can remain as `.visible .func` definitions in optimized PTX
After:  private helper definitions are removed while public functions and globals remain visible

What changed

Semantic public roots

  • Return rendered LLVM and its public-symbol set together as a typed ExportedModule.
  • Derive roots from exporter state: kernel entries or standalone device-function roots, plus every defined global.
  • Sort and deduplicate the root list before carrying it through the compilation pipeline; rendered LLVM text is never reparsed to recover visibility.

PTX optimization

  • Run internalize,default<O2> with the explicit public API list when roots are available.
  • Retain the existing -O2 path for modules without roots and leave no-opt compilation unchanged.
  • Reject a symbol that cannot be represented in LLVM's comma-separated API-list argument rather than silently optimizing with an incomplete root set.

Correctness hardening

  • Preserve host-resolved constants and other defined globals at their historical external linkage.
  • Add a real optimizer/llc regression proving that a non-root helper is removed only in the optimized artifact while the kernel survives.
  • Exercise constant-memory lookup and execution on the GPU after optimization.

Known separate limitations

  • This change does not add new inlining or loop-unrolling policy; it only permits LLVM to delete private definitions after existing optimization makes them unreachable.
  • The current IR does not distinguish host-exported globals from compiler-private globals, so all defined globals are conservatively retained.

Testing

  • cargo oxide fmt --check
  • cargo test -p llvm-export — 26 unit tests, 46 export integration tests, and 3 operation tests passed
  • cargo test -p cuda-oxide-codegen — 59 unit tests, 10 compile_to_ptx tests, concurrent and spine integrations, and 1 doctest passed
  • cargo clippy -p llvm-export -p cuda-oxide-codegen --all-targets -- -D warnings
  • Real opt/llc artifact regression: optimized PTX retains add_kernel and removes unused_helper; no-opt PTX retains the helper
  • RTX 3080 (sm_86): optimized constant_memory successfully looked up, populated, and read its host-visible constant across two launches
  • RTX 3080 (sm_86): optimized and no-opt array_for_loop runs passed during branch preparation

Checklist

  • Branch rebased onto current upstream/main
  • Correctness claim covered by focused regressions
  • Unsupported root serialization fails clearly
  • Optimized and unoptimized behavior verified
  • Host-visible global behavior verified on GPU

@nihalpasham nihalpasham added codegen Device code-generation pipeline (Rust MIR to IR to PTX) llvm-export Textual LLVM/NVVM IR exporter (llvm-export crate) on-hold Paused: valid but deferred until a prerequisite decision or dependency resolves perf Performance of generated code or of the compiler itself tests Adds or improves test coverage labels Jul 22, 2026
niklebedenko and others added 3 commits July 23, 2026 15:07
…nalization

emit_llvm_used and public_symbols each re-implemented the
kernels-if-any-else-device-functions rule, so the @llvm.used set and the
internalize public-API list could silently drift apart. Compute the root
list once, as the union of kernel entries and #[device] definitions:
a #[device] export now stays visible even in a module that also has
kernels, while anonymous helpers carry neither marker and remain
internalizable.
The NVIDIA#410 libdevice test predates public_symbols; pass the module's
kernel as the internalization root so the test now also covers the
llvm-link + internalize composition.
@nihalpasham nihalpasham removed the on-hold Paused: valid but deferred until a prerequisite decision or dependency resolves label Jul 23, 2026
@nihalpasham
nihalpasham force-pushed the codex/upstream-internalize-ptx-helpers branch from 295575a to 6d1d74d Compare July 23, 2026 10:30
@nihalpasham

Copy link
Copy Markdown
Collaborator

Thanks for the PR

What landed on top of your commit:

Validated: exporter and codegen suites including your spine regression (which fails on unfixed main: a fully inlined helper used to survive opt -O2 into PTX as a dead .visible .func), plus helper_fn, standalone_device_fn, libdevice examples, and the FFI lanes on an RTX 5090.

Threading public_symbols through generate_ptx alongside NVIDIA#410's libdevice
parameter pushed both entry points past clippy's argument limit. The
module-shaped inputs (IR path, PTX output path, symbol roots) already
had a struct inside the impl; take PtxModule at the public boundary too
instead of three loose parameters.
The compile-only gate required the two nested_identity monomorphizations
to survive as .visible .func in the final PTX. Internalizing non-root
helpers deliberately inlines and deletes them there, which is the
optimization this branch adds. What the NVIDIA#358 regression actually needs
pinned is canonical mangling and non-kernel classification, and both
are decided before the middle-end runs: assert them in the pre-opt
LLVM IR instead, and keep the PTX assertions for the single kernel
entry and the absence of _TID_ helper symbols.
@nihalpasham
nihalpasham merged commit f59d9d5 into NVIDIA:main Jul 23, 2026
33 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

codegen Device code-generation pipeline (Rust MIR to IR to PTX) llvm-export Textual LLVM/NVVM IR exporter (llvm-export crate) perf Performance of generated code or of the compiler itself tests Adds or improves test coverage

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PTX optimization emits unreachable device helper definitions

2 participants