Skip to content

codegen: Copy to an alloca when the argument is neither by-val nor by-move for indirect pointer.#155343

Open
dianqk wants to merge 2 commits intorust-lang:mainfrom
dianqk:indirect-by-ref
Open

codegen: Copy to an alloca when the argument is neither by-val nor by-move for indirect pointer.#155343
dianqk wants to merge 2 commits intorust-lang:mainfrom
dianqk:indirect-by-ref

Conversation

@dianqk
Copy link
Copy Markdown
Member

@dianqk dianqk commented Apr 15, 2026

Fixes #155241.

Arguments passed indirectly via a hidden pointer must be copied to an alloca, except for by-val or by-move.

r? RalfJung (since you have already commented a lot)

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 15, 2026
self.codegen_argument(
bx,
op,
// Pass by-move for an explicit tail call, which has been handled above as well.
Copy link
Copy Markdown
Member Author

@dianqk dianqk Apr 15, 2026

Choose a reason for hiding this comment

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

This feels a bit awkward; perhaps the tail call handling can be moved into codegen_argument as well.

View changes since the review

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The issue has nothing to do with tail calls, does it...?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes. #151143 works well.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

But this PR is changing something about tail calls...?

Maybe it's just the comment that is confusing me. Why is it okay to always to by-move for tail call arguments? Cc @folkertdev

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Because #151143 has done a similar thing, we don't need to copy it again. IIUC, tail call is special; the same arguments on the caller must be passed to the callee. Extra copy is UB.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

#151143 says something about

Therefore we store the argument for the callee in the corresponding caller's slot.

So I guess there is code elsewhere establishing an invariant that the argument here is the equivalent slot and we can pass a pointer to it. The comment should explain that and point at that code.

// CHECK: call void @test_simd(<4 x i32> <i32 2, i32 4, i32 6, i32 8>
test_simd(const { Simd::<i32, 4>([2, 4, 6, 8]) });

// CHECK: call void @test_simd_unaligned(%"minisimd::PackedSimd<i32, 3>" %1
Copy link
Copy Markdown
Member Author

@dianqk dianqk Apr 15, 2026

Choose a reason for hiding this comment

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

A redundant memcpy is skipped here.

View changes since the review

@dianqk dianqk marked this pull request as ready for review April 16, 2026 13:24
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 16, 2026

RalfJung is not on the review rotation at the moment.
They may take a while to respond.

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Apr 16, 2026
@dianqk dianqk changed the title [DRAFT] codegen: Copy to an alloca when the argument is neither by-val nor by-move for indirect pointer. codegen: Copy to an alloca when the argument is neither by-val nor by-move for indirect pointer. Apr 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

release-mode heap corruption with non-generic FnOnce(Vec<usize>)

3 participants