Repository navigation
mir-importer: read closure upvars from generic suffix - #212
Merged
Merged
Conversation
Signed-off-by: nik <[email protected]>
Collaborator
|
Thanks for the PR. This was an interesting catch - Merging as-is. Tiny heads up: this one and #206 both add a helper at the same spot in types.rs, so whichever lands second will need a trivial keep-both rebase. 👍🏾 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #211.
Problem
rustc suffix-encodes closure substitutions as
[parent_args..., closure_kind, closure_sig, tupled_upvars]. The MIR importer treatedsubsts[2]as the upvars tuple, which is only true when there are no parent generic arguments before the closure suffix.In a generic kernel that constructs a captured closure and passes it through an out-of-line generic helper, the old importer translated the closure environment as a zero-field struct even though closure construction still had two capture operands. On
upstream/mainwith only the regression applied:Fix
Add a small
closure_upvar_tyshelper that reads the tupled-upvars type from the last closure generic argument and verifies that it is a tuple. Use that helper in both places that decode closure captures:is_rust_type_zst, so captured closures in generic contexts are not mistaken for zero-sized closures;translate_type, so the generated MIR struct fields match rustc's closure environment.This keeps the compiler rule in one place instead of duplicating a fixed-index assumption.
Diligence
I checked the sibling closure paths for other fixed-index upvar decoding:
AggregateKind::Closurebuilds the closure value from MIR operands and delegates the closure type totypes::translate_type.fn_sig()and does not inspect upvar substitutions.The initial direct-call regression was stale because rustc optimized the closure environment away before import. The committed regression keeps the closure as a real value by passing it through
#[inline(never)] apply_unary.Tests
Extended
crates/rustc-codegen-cuda/examples/genericwithclosure_capture<T>, a generic kernel that captures two generic values in a closure and passes that closure to an out-of-line generic helper:The host launches the kernel for both
f32andi32, checking that the captured arithmetic is applied correctly for each monomorphization.Validation
Verified the regression fails on
upstream/mainby applying only the example change and running:Verified the patched branch with: