Skip to content

Improve Session/CodegenBackend construction - #161432

Open
nnethercote wants to merge 3 commits into
rust-lang:mainfrom
nnethercote:improve-session-backend-building
Open

nnethercote wants to merge 3 commits into
rust-lang:mainfrom
nnethercote:improve-session-backend-building

Conversation

@nnethercote

@nnethercote nnethercote commented Aug 20, 2026

Copy link
Copy Markdown
Contributor

View all comments

The creation and initialization of sessions and codegen backends is intertwined, which is confusing and error prone. This commit detangles things, and also simplifies the types used for the state within the backends. Details in individual commits.

r? @bjorn3

@rustbot

rustbot commented Aug 20, 2026

Copy link
Copy Markdown
Collaborator

miri is developed in its own repository. If the Miri part of this change can be broken out, consider making this change to rust-lang/miri instead. However, if Miri needs adjusting for rustc changes, just ignore this message.

cc @rust-lang/miri

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

cc @bjorn3

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 Aug 20, 2026
@nnethercote

Copy link
Copy Markdown
Contributor Author

This is an opinionated change, see what you all think.

LLM disclosure: some of the ideas came from an analysis done by an LLM. I wrote all the code and text myself.

fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.info.lock().expect("lock").fmt(formatter)
}
#[derive(Clone)]

@antoyo antoyo Aug 20, 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.

Do the cg_gcc changes need to be done in this PR?
I would be more confortable landing this directly in the cg_gcc repo so that the whole test suite can run (some cg_gcc tests do not run here in the Rust repo).

View changes since the review

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.

I think they do, because both commits change the signature of CodegenBackend::init. Doing a local test run in cg_gcc is probably the way forward, if/when there's agreement that this PR is worth merging.

@rust-bors

This comment has been minimized.

@RalfJung RalfJung left a comment

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.

Broadly makes sense to me but I did not check all the details.

View changes since this 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.

FWIW there is some more codegen-backend-related session initialization happening inside add_configuration. And especially the handing of target features is a complete mess (not as bad as it used to be, but still bad). We're calling llvm_util::global_llvm_features like half a dozen times because we need it in various places and we don't have a tcx yet so it can't be a query...

Anyway, not really something for this PR. I just wondered what this PR does with the messy part of codegen backend initialization that I regularly run into, and the answer is "nothing". Which is fine, the cleanup here seems reasonable on its own. Maybe inspiration for a future cleanup PR. :)

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.

Interesting. I looked into add_configuration and found a bug; #161718 fixes it and takes a step towards cleaning things up more. Once that PR merges I will do more in this PR to fix the remaining ordering problems.

I also looked at global_llvm_features. There is a query for it, global_backend_features, but it's not actually necessary. It should be possible to get the features once and store them in the session, which should make things simpler. Not sure yet if I will do that in this PR or a follow-up.

@nnethercote nnethercote Aug 31, 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.

I have successfully removed the global_backend_features query. #161903 needs to merge first.

Comment thread compiler/rustc_codegen_ssa/src/base.rs Outdated
Comment thread compiler/rustc_codegen_llvm/src/lib.rs Outdated
Comment thread compiler/rustc_session/src/session.rs
Comment thread compiler/rustc_session/src/session.rs Outdated
Comment thread compiler/rustc_session/src/session.rs
Comment thread compiler/rustc_codegen_cranelift/src/lib.rs Outdated
nnethercote added a commit to nnethercote/rust that referenced this pull request Aug 25, 2026
Currently, `parse_cfg` calls `build_configuration`, which calls
`default_configuration`, which calls
`sess.target.singlethread(&sess.internal_target_features)`. But
`sess.internal_target_features` hasn't been set at this point and is
empty!

This commit moves the setting of `sess.internal_target_features` before
the `parse_cfg` call to fix this ordering bug. This results in the
`cfg(target_has_threads)` being correctly set on
`wasm32-unknown-unknown` when `-Ctarget-feature=+atomics` is specified.

Note: I have plans to make this kind of ordering bug
difficult/impossible in a follow-up (e.g. rust-lang#161432).
@bjorn3 bjorn3 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 Aug 27, 2026
nnethercote added a commit to nnethercote/rust that referenced this pull request Aug 31, 2026
`llvm::target_config` creates `target_machine` by calling
`create_informational_target_machine`, which calls
`target_machine_factory`, which uses `internal_target_features`. But
this is just before `internal_target_features` is initialized! So we
should move `internal_target_features` initialization before
`target_machine`, right?

But `internal_target_features` initialization involves a closure that
inspects `target_machine`. There is a cyclic dependency. There is enough
function nesting here that it's hard to spot.

In practice this cycle doesn't cause problems because the closure
doesn't inspect the parts of `target_machine` that depend on
`internal_target_features`. But it demonstrates how startup
initialization is all tangled up, and it's blocking some cleanups I am
doing in rust-lang#161432 relating to the dangerous uses of `Session` before it's
fully initialized.

Therefore, this commit changes the first part: instead of creating
an `OwnedTargetMachine` we create an `OwnedMCSubtargetInfo`. This is a
smaller type that has the feature information we need but doesn't depend
on `internal_target_features`. Under the covers we are now using LLVM's
`Target::createMCSubtargetInfo` instead of
`TargetMachine::getMCSubtargetInfo` so that we avoid having to create a
`TargetMachine` at this early stage. This eliminates the cycle.
(`TargetMachine` can still be created later on, once we're past this
fraught initialization.) There are some slight differences between these
two approaches, and the preceding commits fixed up some issues there.

Some details about this commit:
- The new `OwnedMCSubtargetInfo` is similar to the existing
  `OwnedTargetMachine`.
- `create_informational_target_machine` no longer needs a `for_cfg`
  parameter, because the one site where `for_cfg` was true has been
  removed.
- `LLVMRustCreateMCSubtargetInfo` mostly replicates part of
  `LLVMRustCreateTargetMachine`
- `LLVMRustMCSubtargetInfoHasFeature` partly replicates
  `LLVMRustHasFeature`.
- `LLVMRustHasFeature` is no longer needed.
- The error message for `custom-target-invalid-llvm-target.rs` changed.
nnethercote added a commit to nnethercote/rust that referenced this pull request Aug 31, 2026
`llvm::target_config` creates `target_machine` by calling
`create_informational_target_machine`, which calls
`target_machine_factory`, which uses `internal_target_features`. But
this is just before `internal_target_features` is initialized! So we
should move `internal_target_features` initialization before
`target_machine`, right?

But `internal_target_features` initialization involves a closure that
inspects `target_machine`. There is a cyclic dependency. There is enough
function nesting here that it's hard to spot.

In practice this cycle doesn't cause problems because the closure
doesn't inspect the parts of `target_machine` that depend on
`internal_target_features`. But it demonstrates how startup
initialization is all tangled up, and it's blocking some cleanups I am
doing in rust-lang#161432 relating to the dangerous uses of `Session` before it's
fully initialized.

Therefore, this commit changes the first part: instead of creating
and `OwnedTargetMachine` we create an `OwnedMCSubtargetInfo`. This is a
smaller type that has the feature information we need but doesn't depend
on `internal_target_features`. Under the covers we are now using LLVM's
`Target::createMCSubtargetInfo` instead of
`TargetMachine::getMCSubtargetInfo` so that we avoid having to create a
`TargetMachine` at this early stage. This eliminates the cycle.
(`TargetMachine` can still be created later on, once we're past this
fraught initialization.) There are some slight differences between these
two approaches, and the preceding commits fixed up some issues there.

Some details about this commit:
- The new `OwnedMCSubtargetInfo` is similar to the existing
  `OwnedTargetMachine`.
- `create_informational_target_machine` no longer needs a `for_cfg`
  parameter, because the one site where `for_cfg` was true has been
  removed.
- `LLVMRustCreateMCSubtargetInfo` mostly replicates part of
  `LLVMRustCreateTargetMachine`
- `LLVMRustMCSubtargetInfoHasFeature` partly replicates
  `LLVMRustHasFeature`.
- `LLVMRustHasFeature` is no longer needed.
- The error message for `custom-target-invalid-llvm-target.rs` changed.
@nnethercote
nnethercote force-pushed the improve-session-backend-building branch from 1225a52 to dacfcaa Compare August 31, 2026 04:57
@rustbot

rustbot commented Aug 31, 2026

Copy link
Copy Markdown
Collaborator

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

cc @rust-lang/clippy

These commits modify compiler targets.
(See the Target Tier Policy.)

@rustbot rustbot added A-attributes Area: Attributes (`#[…]`, `#![…]`) A-run-make Area: port run-make Makefiles to rmake.rs T-clippy Relevant to the Clippy team. labels Aug 31, 2026
@rustbot

This comment has been minimized.

@rustbot

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

nnethercote added a commit to nnethercote/rust that referenced this pull request Aug 31, 2026
`llvm::target_config` creates `target_machine` by calling
`create_informational_target_machine`, which calls
`target_machine_factory`, which uses `internal_target_features`. But
this is just before `internal_target_features` is initialized! So we
should move `internal_target_features` initialization before
`target_machine`, right?

But `internal_target_features` initialization involves a closure that
inspects `target_machine`. There is a cyclic dependency. There is enough
function nesting here that it's hard to spot.

In practice this cycle doesn't cause problems because the closure
doesn't inspect the parts of `target_machine` that depend on
`internal_target_features`. But it demonstrates how startup
initialization is all tangled up, and it's blocking some cleanups I am
doing in rust-lang#161432 relating to the dangerous uses of `Session` before it's
fully initialized.

Therefore, this commit changes the first part: instead of creating
and `OwnedTargetMachine` we create an `OwnedMCSubtargetInfo`. This is a
smaller type that has the feature information we need but doesn't depend
on `internal_target_features`. Under the covers we are now using LLVM's
`Target::createMCSubtargetInfo` instead of
`TargetMachine::getMCSubtargetInfo` so that we avoid having to create a
`TargetMachine` at this early stage. This eliminates the cycle.
(`TargetMachine` can still be created later on, once we're past this
fraught initialization.) There are some slight differences between these
two approaches, and the preceding commits fixed up some issues there.

Some details about this commit:
- The new `OwnedMCSubtargetInfo` is similar to the existing
  `OwnedTargetMachine`.
- `create_informational_target_machine` no longer needs a `for_cfg`
  parameter, because the one site where `for_cfg` was true has been
  removed.
- `LLVMRustCreateMCSubtargetInfo` mostly replicates part of
  `LLVMRustCreateTargetMachine`
- `LLVMRustMCSubtargetInfoHasFeature` partly replicates
  `LLVMRustHasFeature`.
- `LLVMRustHasFeature` is no longer needed.
- The error message for `custom-target-invalid-llvm-target.rs` changed.
@nnethercote
nnethercote force-pushed the improve-session-backend-building branch from dacfcaa to c8f2df6 Compare August 31, 2026 05:08
@rustbot

This comment has been minimized.

@rust-bors

This comment has been minimized.

nnethercote added a commit to nnethercote/rust that referenced this pull request Sep 1, 2026
`llvm::target_config` creates `target_machine` by calling
`create_informational_target_machine`, which calls
`target_machine_factory`, which uses `internal_target_features`. But
this is just before `internal_target_features` is initialized! So we
should move `internal_target_features` initialization before
`target_machine`, right?

But `internal_target_features` initialization involves a closure that
inspects `target_machine`. There is a cyclic dependency. There is enough
function nesting here that it's hard to spot.

In practice this cycle doesn't cause problems because the closure
doesn't inspect the parts of `target_machine` that depend on
`internal_target_features`. But it demonstrates how startup
initialization is all tangled up, and it's blocking some cleanups I am
doing in rust-lang#161432 relating to the dangerous uses of `Session` before it's
fully initialized.

Therefore, this commit changes the first part: instead of creating
and `OwnedTargetMachine` we create an `OwnedMCSubtargetInfo`. This is a
smaller type that has the feature information we need but doesn't depend
on `internal_target_features`. Under the covers we are now using LLVM's
`Target::createMCSubtargetInfo` instead of
`TargetMachine::getMCSubtargetInfo` so that we avoid having to create a
`TargetMachine` at this early stage. This eliminates the cycle.
(`TargetMachine` can still be created later on, once we're past this
fraught initialization.) There are some slight differences between these
two approaches, and the preceding commits fixed up some issues there.

Some details about this commit:
- The new `OwnedMCSubtargetInfo` is similar to the existing
  `OwnedTargetMachine`.
- `create_informational_target_machine` no longer needs a `for_cfg`
  parameter, because the one site where `for_cfg` was true has been
  removed.
- `LLVMRustCreateMCSubtargetInfo` mostly replicates part of
  `LLVMRustCreateTargetMachine`
- `LLVMRustMCSubtargetInfoHasFeature` partly replicates
  `LLVMRustHasFeature`.
- `LLVMRustHasFeature` is no longer needed.
- The error message for `custom-target-invalid-llvm-target.rs` changed.
@nnethercote
nnethercote force-pushed the improve-session-backend-building branch from c8f2df6 to 6279910 Compare September 1, 2026 00:07
@rustbot

This comment has been minimized.

@rustbot

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-bors

This comment has been minimized.

@nnethercote
nnethercote force-pushed the improve-session-backend-building branch from 6279910 to 08d1a72 Compare September 15, 2026 03:42
@rustbot

rustbot commented Sep 15, 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.

@nnethercote nnethercote 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. S-blocked Status: Blocked on something else such as an RFC or other implementation work. labels Sep 15, 2026
@nnethercote

Copy link
Copy Markdown
Contributor Author

#161903 has merged and I have rebased. This is ready for review now.

@rust-log-analyzer

This comment has been minimized.

Session creation is currently awkward: we build a mostly-initialized
session, then use it to initialize a codegen backend, and then use the
codegen backend to finish initializing the session.

And it's not just awkward: within the Cranelift backend's `init` method
`sess.lto()` is called, which consults `sess.thin_lto_supported`,
*before* that field has been properly set! In practice it had no effect
but it's worth fixing.

This commit cleans up this mess. It introduces `EarlySession`, which
contains just four `Session` fields, the ones that are needed for
codegen backend initialization. It is now a field within `Session`, and
`Session` derefs to `EarlySession` to avoid changing a zillion
`sess.target`/`sess.opts`/etc. occurrences. `EarlySession` is passed to
`init`, which returns a `CodegenBackendInit` that contains the
backend-specific information needed to build a `Session`. (It replaces
the `replaced_intrinsics`, `fallback_intrinsics`, and
`thin_lto_supported` methods.) The `Session` can then be built in a
single step. No more `Session`/`CodegenBackend` initialization
intermingling.

A few functions that previously took a `Session` now take something
else, e.g. a `Target`. Some `Session` methods are now `EarlySession`
methods. And a new `early_lto` method is used for Cranelift's LTO check.
It currently takes `&self`, which is a bit strange for an `init` method.
As a result, the Cranelift and GCC backends have to use types with
interior mutability.

This commit changes it to `&mut self`. Benefits:

- The Cranelift backend can use `Option` instead of `OnceCell` to
  indicate uninit vs. init.

- The GCC backend can avoid `Mutex`, and use `bool` instead of
  `AtomicBool`, which makes things much simpler. The commit also
  restructures `GccCodegenBackend` to mirror `CraneliftCodegenBackend`:
  just contain an `Option<BackendConfig>`, which makes the uninit vs.
  init distinction foolproof. (E.g. no need to set `lto_supported` to
  false and then later overwrite it with the real value.) As part of
  this the `LockedTargetInfo` type is renamed `SharedTargetInfo` because
  that better matches its new internals. (All this compiles both with
  and without the "master" feature set.)
It's now possible to get the backend features (a `Vec<String>`) when the
codegen backend is started, pass it back through `CodegenBackendInit`,
and just store it in the `Session`. This removes the need for the query.

Also:

- `WriteBackendMethods::target_machine_factory` no longer needs the
  `target_features` parameter, because it's now available through the
  `sess` parameter.

- `CodegenContext` no longer needs the `backend_features` field because
  we can use `sess.global_backend_features` instead.

- `CodegenBackend::provide` is now a no-op for all the in-tree backends.
  I haven't removed it because out-of-tree backends still rely on it.
@nnethercote
nnethercote force-pushed the improve-session-backend-building branch from 08d1a72 to 477ab19 Compare September 15, 2026 05:06
@nnethercote

Copy link
Copy Markdown
Contributor Author

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Sep 15, 2026
@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Sep 15, 2026
… r=<try>

Improve `Session`/`CodegenBackend` construction
@rust-bors

rust-bors Bot commented Sep 15, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: a4be199 (a4be199b900861661373793eca2a3783bd283390)
Base parent: c26ce70 (c26ce708de5d14682647895d2f3caf38f70b5aa6)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (a4be199): comparison URL.

Overall result: ❌✅ regressions and improvements - please read:

Benchmarking means the PR may be perf-sensitive. It's automatically marked not fit for rolling up. Overriding is possible but disadvised: it risks changing compiler perf.

Next, please: If you can, justify the regressions found in this try perf run in writing along with @rustbot label: +perf-regression-triaged. If not, fix the regressions and do another perf run. Neutral or positive results will clear the label automatically.

@bors rollup=never rustc-perf
@rustbot label: -S-waiting-on-perf +perf-regression

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

mean range count
Regressions ❌
(primary)
0.2% [0.2%, 0.2%] 1
Regressions ❌
(secondary)
0.4% [0.2%, 0.6%] 3
Improvements ✅
(primary)
-0.2% [-0.3%, -0.2%] 3
Improvements ✅
(secondary)
-0.6% [-0.9%, -0.3%] 11
All ❌✅ (primary) -0.1% [-0.3%, 0.2%] 4

Max RSS (memory usage)

Results (secondary 0.1%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
5.2% [1.9%, 7.9%] 7
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-4.9% [-7.0%, -2.2%] 7
All ❌✅ (primary) - - 0

Cycles

Results (secondary -1.3%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
8.2% [7.7%, 8.8%] 2
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-5.1% [-8.3%, -2.9%] 5
All ❌✅ (primary) - - 0

Binary size

This perf run didn't have relevant results for this metric.

Bootstrap: 499.183s -> 495.997s (-0.64%)
Artifact size: 406.98 MiB -> 406.94 MiB (-0.01%)

@rustbot rustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Sep 15, 2026
BackendConfig::from_opts(&sess.opts.cg.llvm_args)
.unwrap_or_else(|err| sess.dcx().fatal(err)),
);
}

@bjorn3 bjorn3 Sep 15, 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.

This can use let config = self.config.get_or_insert_with(|| ...).

View changes since the review

@bjorn3

bjorn3 commented Sep 15, 2026

Copy link
Copy Markdown
Member

r=me with the above change

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

Labels

A-attributes Area: Attributes (`#[…]`, `#![…]`) 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 perf-regression Performance regression. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-clippy Relevant to the Clippy team. 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.

7 participants