Skip to content

Move va_arg handling to rustc_codegen_ssa - #166298

Open
beetrees wants to merge 1 commit into
rust-lang:mainfrom
beetrees:va-arg-ssa
Open

beetrees wants to merge 1 commit into
rust-lang:mainfrom
beetrees:va-arg-ssa

Conversation

@beetrees

@beetrees beetrees commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

View all comments

This allows the va_arg codegen to be shared between backends. Methods for emitting ptr_mask and phi are added to BuilderMethods, ensuring that there are no changes to the emitted LLVM IR.

r? @folkertdev

@rustbot

rustbot commented Nov 21, 2026

Copy link
Copy Markdown
Collaborator

rustc_codegen_gcc is developed in its own repository. If possible, consider making this change to rust-lang/rustc_codegen_gcc instead.

cc @antoyo, @GuillaumeGomez

@rustbot rustbot added A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. 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. labels Nov 21, 2026

fn start_phi(&mut self, value: Self::Value) -> Self::PhiBuilder;
fn add_phi_input(&mut self, phi: &mut Self::PhiBuilder, value: Self::Value);
fn phi_output(&mut self, phi: Self::PhiBuilder) -> Self::Value;

@bjorn3 bjorn3 Nov 21, 2026 •

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.

I removed all uses of phi from cg_ssa a while ago as Cranelift doesn't use phi nodes, but instead uses basic block params.

View changes since the review

@beetrees beetrees Nov 21, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The builder API exposed in this PR can be implemented by just creating an alloca in start_phi, writing to it in add_phi_input, and reading from it in phi_output, so it should be implementable in any codegen backend regardless of what mechanism is used to transfer values between basic blocks. That said, I think it would be possible to implement this in a Cranelift backend using basic block params - at most the destination basic block would need to be passed as an argument to start_phi. The PhiBuilder would then store the destination block and the index of the basic block parameter. add_phi_input would track the parameters for each destination block in the builder, which the terminating br/cond_br (or similar) would pass as appropriate.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Would it make sense to use other names than names containing phi then?
Or is phi the correct name here?

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 builder API exposed in this PR can be implemented by just creating an alloca in start_phi, writing to it in add_phi_input, and reading from it in phi_output, so it should be implementable in any codegen backend regardless of what mechanism is used to transfer values between basic blocks.

Cranelift isn't capable of optimizing that away currently.

One thing that may work is to require add_phi_input to be called while the builder points to the basic block in which a jump to the block that will contain the phi_output should result in the phi node giving that value. In that case I could use declare_var in start_phi (requires passing in the type rather than a value to start_phi), def_var in add_phi_output and use_var in phi_output. Then the SSA construction algorithm in cranelift-frontend should produce the right basic block parameter.

Or is phi the correct name here?

SSA calls this phi functions/nodes.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

With declare_var/def_var/use_var a Cranelift backend should be able to use an implementation similar to the GCC one, replacing new_temp with declare_var, assign with def_var and to_rvalue with use_var.


// Just to be safe we error out explicitly here, instead of crossing our fingers that
// the default LLVM implementation has the correct behavior for this target.
bug!("c-variadic functions are not currently implemented for custom target {arch}")

@antoyo antoyo Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

If I understand correctly, some GCC targets that used to work will stop working in this PR since they will now hit this bug!. Is this correct?
If yes, could there be a way for the backend to provide its own implementation (like calling self.context.new_va_arg directly)?

View changes since the review

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It seems unlikely that that would come up in practice. This is a match on the architecture, so it would only fail on architectures that the compiler truly has no support for. That will probably fail way before c-variadic becomes a problem?

We can/should just add a variant to the architecture enum for such targets I think?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

By "GCC targets", I meant targets not supported by LLVM (so by official rustc).
So, it's possible other issues come up before variadics.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We currently have Spirv in the enum, which isn't otherwise really supported either. So, there is precedent for adding variants to the enum.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

There's been some chicken-and-egg problem (rustc vs libc) in the past. Do you know if this concerns this enum?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I don't think that'll be a problem here. Also if a convincing exception comes up we can use GCC's va_arg of course, but by default we want to error so that we can explicitly consider it.

@folkertdev

Copy link
Copy Markdown
Contributor

Overall I'm in favor of this change for va_arg, I'll let y'all figure out what exact strategy makes most sense for all codegen backends.

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 22, 2026
@rustbot

rustbot commented Nov 22, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@folkertdev

Copy link
Copy Markdown
Contributor

Also now that bors is back in business maybe there are parts that can be pre-committed if this takes a while?

@rust-bors

This comment has been minimized.

@rustbot rustbot added the A-run-make Area: port run-make Makefiles to rmake.rs label Nov 27, 2026
@rustbot

This comment has been minimized.

@beetrees beetrees 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 Nov 27, 2026
Comment on lines +100 to +103
// Adding inputs to a phi is done as part of the branch as some backends (like
// rustc_codegen_gcc) need to insert statements before the basic block terminator. Having a
// combined `br_with_phi` operation prevents incorrect usage of the API even on backends that
// don't need to insert any statements, like rustc_codegen_llvm.

@antoyo antoyo Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nit: please use a doc-comment here to that it appears in the doc.
I'm good with this API and the cg_gcc part.

View changes since the review

@rust-bors

This comment has been minimized.

@rustbot

rustbot commented Nov 28, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. A-run-make Area: port run-make Makefiles to rmake.rs 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.

5 participants