Skip to content

Refactor the way cg_ssa handles indirect returns (returns via sret) — Take 2 - #160023

Open
antoyo wants to merge 6 commits into
rust-lang:mainfrom
antoyo:ssa-indirect-return
Open

Refactor the way cg_ssa handles indirect returns (returns via sret) — Take 2#160023
antoyo wants to merge 6 commits into
rust-lang:mainfrom
antoyo:ssa-indirect-return

Conversation

@antoyo

@antoyo antoyo commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

This continues the work that @FractalFir done in #144976.

I managed to make this work, so this confirms the original approach works and is ready to merge.

r? workingjubilee

(assigning the same reviewer as the original PR)

The problem

The current way cg_ssa handles PassMode::Indirect is fundamentally incompatible with the requirements of the GCC backend. This is currently worked around in a very brittle way, that breaks on ARM.

In order to function correctly, cg_gcc requires the sret(indirect return) pointer to be treated in a special way.

The solution

This PR separates the sret pointer from all the other arguments, allowing each backend to decide how it wants to handle that pointer. This will allow GCC to do it's own thing, while changing nothing on the LLVM side. Currently, the PR just makes both backends preappend the sret arg, mimicking the previous behaviour.

The PR is based on this zulip suggestion.

Assumptions I made.

The PR makes certain assumptions about the ABI handling.

  1. No LLVM intrinsic returns via sret. According to my sources, this is always true.
  2. Tail calls don't currently return via sret. From asking about, it seems like the tail call handling does not currently support indirect returns. So, I did not add support for that.
  3. TLS shims don't perform indirect returns, and asserts and drops also don't perform indirect returns(since they return nothing) - those assumptions seemed reasonable.
  4. Inline assembly calls don't use indirect returns. I assumed functions like inline_asm_call will not need the sret handling code. I am not 100% sure about this, tough.

@rustbot

rustbot commented Jul 27, 2026

Copy link
Copy Markdown
Collaborator

Some changes occurred in compiler/rustc_codegen_llvm/src/builder/autodiff.rs

cc @ZuseZ4

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

cc @GuillaumeGomez

@rustbot rustbot added A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. F-autodiff `#![feature(autodiff)]` 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 Jul 27, 2026
@rustbot

rustbot commented Jul 27, 2026

Copy link
Copy Markdown
Collaborator

workingjubilee is currently at their maximum review capacity.
They may take a while to respond.

@rust-log-analyzer

This comment has been minimized.

@antoyo
antoyo force-pushed the ssa-indirect-return branch from 8a2ec6b to 25ec648 Compare July 27, 2026 17:46
@antoyo

antoyo commented Jul 27, 2026

Copy link
Copy Markdown
Contributor Author

@bors try

@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Jul 27, 2026
Refactor the way cg_ssa handles indirect returns (returns via `sret`) — Take 2
Comment thread compiler/rustc_codegen_llvm/src/builder.rs Outdated
Comment thread compiler/rustc_codegen_llvm/src/builder.rs Outdated
@rust-bors

rust-bors Bot commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: 3ae8918 (3ae8918f6a7874e42bc8432d0339e0d172a2daa6)
Base parent: 4fefe36 (4fefe3656700e5528edddc2336667818917dfc70)

@rust-bors

rust-bors Bot commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

☔ The latest upstream changes (presumably #159844) made this pull request unmergeable. Please resolve the merge conflicts by rebasing.

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. F-autodiff `#![feature(autodiff)]` 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.

6 participants