Skip to content

perf(finalizer): reuse pinned tool provenance - #893

Merged
nihalpasham merged 3 commits into
NVIDIA:mainfrom
lucifer1004:pr/materializer-provenance-handshake
Aug 16, 2026
Merged

nihalpasham merged 3 commits into
NVIDIA:mainfrom
lucifer1004:pr/materializer-provenance-handshake

Conversation

@lucifer1004

@lucifer1004 lucifer1004 commented Aug 15, 2026 •

Copy link
Copy Markdown
Contributor

Problem

  • Every backend materialization recomputed provenance by rehashing the pinned tool binaries (libNVVM, nvJitLink), which are hundreds of MB. Warm builds paid the full SHA-256 cost over and over for tools that had not changed:
Before: every single build
  hash libnvvm.so (~300 MB)  ->  provenance digest     (seconds, every time)

After: first build
  hash libnvvm.so  ->  cache { version, dev/inode/ctime/size, digest }
warm build
  stat libnvvm.so, identity matches  ->  reuse digest  (no hashing)
libnvvm.so touched or replaced
  full rehash; if the content actually changed        ->  ProvenanceMismatch (loud)

Fix

  • a versioned handshake cache at .oxide-artifacts/materializer-handshake/v1.json stores verified tool provenance with explicit, derived-Serde fields
  • each operation revalidates cheaply: handshake version, file identity (device, inode, ctime, size), and the expected semantic digest must all match to reuse the cached hash
  • any mismatch falls back safely to full rehashing, and a genuine content change fails loudly with ProvenanceMismatch
  • review follow-ups: 4a77207 minimizes the isolated backend lockfile churn (only the new serde/serde_json entries, no unrelated bumps), skips digest reuse when the Unix identity fields are absent so non-Unix hosts always rehash, and documents the cache file in the README; 99632bc refreshes all 209 example lockfiles, which went stale because the new serde dependency reaches every example through its cuda-host path dependency

Gotchas

  • version skew is intentional and loud: after updating cargo-oxide, run cargo oxide setup again or materialization fails with MissingToolIdentityHandshake
  • deleting the cache file just forces a full rehash; a corrupted or inconsistent cache is ignored and self-heals on the next build
  • identity is only trusted with complete Unix metadata; anything less rehashes

Testing and validation

  • finalizer, cargo-oxide, and isolated rustc-codegen-cuda materialization suites green, plus four live finalizer integration tests
  • cold vs warm timing measured with byte-identical handshake JSON output
  • cache behavior spot-checked: touch/replace libnvvm between builds triggers a rehash, content change fails with ProvenanceMismatch
  • example license-policy script, root and backend cargo-deny all green
  • CI green (43/43)

@nihalpasham nihalpasham added perf Performance of generated code or of the compiler itself build-related Build scripts, toolchain discovery, or compilation setup cargo-oxide The `cargo oxide` subcommand and build automation host-apis Host-side runtime APIs (cuda-host, cuda-core, cuda-async) labels Aug 16, 2026
- Regenerate the isolated rustc-codegen-cuda Cargo.lock with a minimal
  diff vs main: only the newly required serde, serde_core, serde_derive,
  serde_json, itoa, and zmij entries are added; the unrelated hashbrown
  and foldhash version churn is reverted.
- Skip handshake digest reuse when the Unix identity fields (device,
  inode, change time) are absent, so identities produced on non-Unix
  platforms always fall back to rehashing the opened tool file.
- Document the materializer-handshake cache file and that deleting it
  forces a full rehash.
… dependency

cuda-artifact-finalizer gained a serde dependency in 45b4f97, and every
example workspace resolves that crate through its cuda-host path
dependency, so all 209 committed example Cargo.lock files went stale and
the cargo-deny example-policy job failed --locked resolution on each
representative workspace. Regenerate them with cargo update --workspace
plus a precise pin to serde 1.0.228, matching the root Cargo.lock and
keeping the diff purely additive (no syn 3 churn from serde 1.0.229).
@nihalpasham

nihalpasham commented Aug 16, 2026 •

Copy link
Copy Markdown
Collaborator

Gist: caching design is good: versioned handshake, identity revalidation, and a fail-safe path back to full hashing on any mismatch. Warm builds skip rehashing multi-hundred-MB tool binaries, and a corrupted cache self-heals. I tightened a few edges and cleaned up fallout.

What I checked and did:

  • pushed 4a77207: minimized the isolated backend lockfile churn (only the serde/serde_json additions remain, no unrelated version bumps), added a guard so digest reuse only happens when the full Unix identity (device, inode, ctime) is present (non-Unix always rehashes), and documented the cache file in the README (delete it to force a rehash)
  • pushed 99632bc: refreshed all 209 example lockfiles. Your serde addition to cuda-artifact-finalizer reaches every example through their cuda-host path dependency, so their --locked resolutions went stale and the example-policy CI job failed. The refresh is purely additive and holds serde at the root lock's 1.0.228 so no extra syn churn sneaks in
  • finalizer tests, isolated backend build, root and backend cargo-deny, and the example license-policy script all green; CI fully green

One heads-up on version skew: after updating cargo-oxide, users need cargo oxide setup again or materialization fails with a clear handshake error. Seems like the right trade.

@nihalpasham
nihalpasham merged commit 4429962 into NVIDIA:main Aug 16, 2026
44 of 87 checks passed
nihalpasham added a commit to lucifer1004/cuda-oxide that referenced this pull request Aug 17, 2026
… dependency

cuda-host now depends on ptx-parse, and every example workspace resolves
cuda-host through its path dependency, so each committed example
Cargo.lock (and the isolated rustc-codegen-cuda lock) gains the two
additive ptx-parse entries. Purely additive, matching the re-lock
pattern from NVIDIA#893.
nihalpasham added a commit that referenced this pull request Aug 17, 2026
… (stack #914-#917) (#917)

* feat(ptx): add lossless structural syntax foundation

* refactor(ptx-parse): rename ptx-syntax crate to ptx-parse

Rename the crate directory, package name, workspace entries, and all
dependent imports. Mark the crate publishable since cuda-host (published)
depends on it; the ptx-parse name is free on crates.io.

* refactor(examples): migrate PTX self-checks to structural ptx-parse queries

Extracted from PR #915: ten examples' textual PTX assertions move onto
ptx-parse Document queries (callables, directives), with each example
Cargo.lock refreshed for the new path dependency. The dialect half of
that PR is not included.

* fix(cuda-intrinsics-gen): propagate PTX parse errors instead of matching nothing

instructions_with_matching_head mapped Document::parse errors to an
empty match list, so unparseable PTX read as "no match" and could slip
through the probe validators with a misleading zero-count message.
Return the ParseError instead and propagate it through
matching_instructions, contains_matching_instruction,
InstructionPattern::matches, and selection_matches_policy, matching how
the other migrated call sites surface parse errors.

* chore(examples): re-lock example workspaces for cuda-host's ptx-parse dependency

cuda-host now depends on ptx-parse, and every example workspace resolves
cuda-host through its path dependency, so each committed example
Cargo.lock (and the isolated rustc-codegen-cuda lock) gains the two
additive ptx-parse entries. Purely additive, matching the re-lock
pattern from #893.

* chore(repo): register ptx-parse in the crate inventory and license ledger

* feat(ptx): add structured surface dialect

Extracted from PR #915: crates/dialect-ptx only (the example-migration
half already landed with the ptx-parse foundation). Imports adapted from
the renamed ptx-syntax crate to ptx-parse.

* refactor(dialect-ptx): verify header text agrees with typed callable attrs

ptx.callable carried the same facts twice: typed attrs (callable_name,
callable_kind, callable_external) for queries and the header string for
printing. The emitter prints only the string, so mutating a typed attr
silently desynced the emitted PTX from the queryable truth.

The header stays the print carrier (it holds parameter lists and
performance directives the dialect does not model yet), but Verify now
re-parses it with ptx-parse and rejects any disagreement with the typed
attrs, naming both values in the error. ptx.scope's header gets the
matching check: a non-empty header must not carry its own terminator
and must not spell a callable, which would bypass ptx.callable's typed
attrs. The contract is documented on both ops.

* feat(dialect-ptx): replace instruction prefix string with typed predicate attr

PTX defines exactly one instruction prefix, the guard predicate, so
ptx.instruction no longer carries a free-form instruction_prefix string.
It now carries an optional typed PredicateAttr (register + negation),
populated by the projection from ptx-parse's parsed predicate. The
emitter derives the textual "@%p1" / "@!%p1" guard from the attribute,
and attribute verification rejects registers that are not %-prefixed.

PtxBuilder grows predicated_instruction(); plain instruction() drops the
prefix parameter. Repo-wide grep confirms nothing but predicate text
ever rode in the prefix string.

* docs(dialect-ptx): add demo_pipeline example walking build, print, emit, reparse

Builds a kernel with a counting loop through PtxBuilder, prints the typed
IR (the loop back-edge carries the typed ptx.predicate attribute), emits
canonical PTX, and checks the guard survives a lossless ptx-parse round
trip.

* chore(repo): register ptx-parse and dialect-ptx in repo-wide inventories

The license-inventory, crate-overview, and SPDX CI guards require every
workspace member to be listed. Adds dependency-licenses.csv rows and
README Crate Overview rows for both new crates, and the standard SPDX
header on ptx-parse's audit example.

* feat(ptx): analyze CFG and lexical normalization

* feat(dialect-ptx): reject vector-element uses of renamed registers at plan time

'.' is a word byte, so a vector-element use such as v.x lexes as a
single token and never resolves to the binding v. Renaming a nested
binding would leave v.x behind, silently rebinding it to any
same-named outer register and assembling with wrong semantics.
RegisterAlphaPlan now scans the callable body for tokens of the form
R.suffix whenever R is scheduled for renaming and fails closed with a
typed error naming the token and its byte span.

* feat(dialect-ptx): reject rename targets that capture label or callable names

The occupied-token check only avoids names spelled inside the callable
body. A module callable never referenced in the body, or a label equal
to a bank base name (which the bank availability check skips), can
still end up as a rename target; the rewritten register then captures
branch or call targets and the PTX fails ptxas or assembles with wrong
semantics. RegisterAlphaPlan now cross-checks every chosen target
against the callable's labels and the module's callable names and
fails closed with a typed error naming both symbols.

* feat(dialect-ptx): gate rewrite plans behind the shared PTX version ceiling

The UnsupportedPtxVersion ceiling previously gated only the read-only
CFG recovery. Rewrites are higher risk than reads: an unaudited ISA
could bind or spell registers in ways the resolver does not model.
The .version validation moves to a shared version module with a typed
PtxVersionError, and RegisterAlphaPlan and ScopeFlattenPlan now fail
closed on missing, invalid, or newer-than-9.3 versions exactly like
ControlFlow::analyze.

* feat(ptx): materialize and verify native CFG

* fix(dialect-ptx): treat .pragma as scope-sensitive in scope flattening

Pragma strings are tool-defined with placement-dependent meanings, so
deleting the braces around a block-scoped pragma such as
.pragma "nounroll"; can widen its effect to the whole callable. Scope
flattening now fails closed on nested scopes containing .pragma; only
.loc, whose debug-location effect follows lexical order regardless of
braces, remains scope-neutral.

* docs(dialect-ptx): record first native-CFG consumers and transform conventions

* docs(dialect-ptx): raise demo_pipeline output into the native CFG

The example now re-analyzes its own canonical emission with
NativeCfgPlan, prints the raised module and the guarded loop back-edge
ptx.terminator (typed predicate plus real successor edges), and checks
that canonical emission from the raised CFG is byte-identical to the
surface emission.

---------

Co-authored-by: nihalpasham <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

build-related Build scripts, toolchain discovery, or compilation setup cargo-oxide The `cargo oxide` subcommand and build automation host-apis Host-side runtime APIs (cuda-host, cuda-core, cuda-async) perf Performance of generated code or of the compiler itself

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants