Skip to content

Fix initialization cycle in target_config - #161903

Open
nnethercote wants to merge 5 commits into
rust-lang:mainfrom
nnethercote:fix-target_config-cycle
Open

Fix initialization cycle in target_config#161903
nnethercote wants to merge 5 commits into
rust-lang:mainfrom
nnethercote:fix-target_config-cycle

Conversation

@nnethercote

Copy link
Copy Markdown
Contributor

There is an initialization cycle in target_config. Details in the final commit. The previous commits are precursors that support the changes in the final commit.

r? @nikic
cc @cuviper @RalfJung @Zalathar @Patryk27

@rustbot

rustbot commented Aug 28, 2026

Copy link
Copy Markdown
Collaborator

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

@rustbot rustbot added 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. labels Aug 28, 2026
@rustbot

This comment has been minimized.

@nnethercote

Copy link
Copy Markdown
Contributor Author

LLM discosure: an LLM suggested the LLVM-related changes to fix the cycle and did extensive review of my changes. I made all the code and text changes myself.

@rust-log-analyzer

This comment has been minimized.

Comment thread compiler/rustc_codegen_llvm/src/back/owned_mc_subtarget_info.rs Outdated
Comment thread compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp Outdated
Comment thread compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp Outdated

@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.

which calls
target_machine_factory, which uses internal_target_features

Good catch! That was not supposed to happen, that's why it receives the target features explicitly as a slice.

This mostly seems to be about using OwnedMCSubtargetInfo rather than an actual target machine to read the feature list off of LLVM. I don't know these LLVM APIs so I can't comment on how to best do that.

View changes since this review

Comment thread compiler/rustc_codegen_llvm/src/back/write.rs Outdated
Comment thread compiler/rustc_codegen_llvm/src/llvm_util.rs Outdated
Comment thread compiler/rustc_codegen_llvm/src/llvm_util.rs Outdated
The `require-explicit-cpu.json` case currently prints a "default target
CPU" line; test for this. (It will change in the next commit.)
Specifically, don't print it when `need_explicit_cpu` is set, because it
doesn't really make sense in that context. Right now among builtin
targets this only affects the `amdgcn-amd-amdhsa` target, but it will
also be relevant for the `avr2` target in the next commit. It also
affects the `require-explicit-cpu.json` case in
`tests/run-make/target-specs/rmake.rs`.
Currently rustc uses LLVM's `TargetMachine::getMCSubtargetInfo` method
to access an `MCSubtargetInfo` to do feature testing. The next commit
will change the feature testing to instead use an alternative pathway,
LLVM's `Target::createMCSubtargetInfo` method. The two pathways have
some slight differences.

One difference relates to the `avr-none` target. Currently its `cpu`
field isn't set so it gets the default "generic" value, which is not a
valid AVR CPU name. This was hidden by the fact that the current LLVM
pathway goes through the `getCPU` function in `AVRTargetMachine.cpp`,
which rewrites "generic" as "avr2". But the alternative LLVM pathway
doesn't rewrite "generic". Without an adjustment, we would get some
behavioural differences with the alternative pathway, such as
"unrecognized processor" errors and empty base feature sets.

Therefore, this commit sets `cpu` to "avr2", a more obviously correct
choice, and what the current LLVM pathway is effectively doing behind
the scenes.

You might think this would change the code generated by default, but
`avr-none` has `need_explicit_cpu` set to true, so that's not the case,
because a missing `-Ctarget-cpu` will trigger a fatal error before
codegen. But `cpu` can still reach non-codegen paths (e.g. feature/cfg
computation in session setup, and `--print`) so we need a valid backend
name.

A consequence of this is that `--print target-spec-json` will emit `cpu:
"avr2"`.

Another consequence is that the `requires_consistent_cpu` check will
compare a crate built without `-Ctarget-cpu` (non-codegen only) against
"avr2" instead of "generic".

The commit also modifies two tests. In both cases, the test passes in
this commit with or without the explicit `cpu` field. But in the next
commit (using the alternative pathway) both tests would fail without the
explicit `cpu` field:

- `tests/ui/abi/avr-sram.rs` would fail with
  ```
  'generic' is not a recognized processor for this target (ignoring processor)
  'generic' is not a recognized processor for this target (ignoring processor)
  warning: target feature `sram` must be enabled to ensure that the ABI of the current target can be implemented correctly
  ```

- `tests/run-make/print-cfg/rmake.rs` would fail because all features
  would be missing.

Finally, the field docs for `TargetOptions` are tweaked to clarify the
interplay between `cpu` and `need_explicit_cpu`.
The `repr(transparent)` isn't necessary: there are no casts or
transmutes involving it, and it's not passed by value across an FFI
boundary.

The `PhantomData` also isn't necessary: the type isn't generic so
variance isn't a factor; the `Drop` impl doesn't involve `may_dangle`;
and the `NonNull` field means the type is `!Send`/`!Sync` with or
without the `PhantomData`.
@nnethercote
nnethercote force-pushed the fix-target_config-cycle branch from 590ecfb to 0107f29 Compare August 31, 2026 02:10
@rustbot

rustbot commented Aug 31, 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.

@rustbot

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

`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
nnethercote force-pushed the fix-target_config-cycle branch from 0107f29 to 7f0581d Compare August 31, 2026 05:09
@rustbot

rustbot commented Aug 31, 2026

Copy link
Copy Markdown
Collaborator

⚠️ Warning ⚠️

  • There are issue links (such as #123) in the commit messages of the following commits.
    Please move them to the PR description, to avoid spamming the issues with references to the commit, and so this bot can automatically canonicalize them to avoid issues with subtree.

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.

6 participants