rustc_{codegen_,}llvm: adapt to new ThreadModel API
LLVM 24 [has moved](e733cebdf7) from TargetOptions::ThreadModel to specifying thread-model as a module flag. This catches us up.
@rustbot label: +llvm-main
Fix initialization cycle in `target_config`
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
`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 #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.
PassWrapper: handle LLVM 24 change in function types
LLVM 24 stopped using llvm::Any here [to avoid heap allocations](fa23198d68) in PassInstrumentation.
@rustbot label: +llvm-main
Pass LargeDataThreshold to LLVM module IR
Before, LargeDataThreshold was only set via `LLVMRustCreateTargetMachine`. When LTO is enabled, this information is required to be passed into the generated LLVM module IR files since the LTO linker spins up its own TargetMachine and otherwise has no knowledge of the LargeDataThreshold.
Currently, `-Z large-data-threshold` has no affect at all when compiling with LTO.
This commit makes sure that if `-Z large-data-threshold` is passed and not 0, it is preserved and stored into the generated LLVM IR.
Before, LargeDataThreshold was only set via `LLVMRustCreateTargetMachine`.
When LTO is enabled, this information is required to be passed into the
generated LLVM module IR files since the LTO linker spins up its own
TargetMachine and otherwise has no knowledge of the LargeDataThreshold.
Currently, `-Z large-data-threshold` has no affect at all when compiling
with LTO.
This commit makes sure that if `-Z large-data-threshold` is passed and
not 0, it is preserved and stored into the generated LLVM IR.
Currently, module summaries are only emitted with thin lto. If we would
link full/fat lto'd rust code against lto'd c++ code built with CFI (or
WPD), those passes would fail during the link step because the
participating rust modules are missing module summaries. Rust code does
not know at compile-time if it would be participating in some special
link which may require module summaries, so this PR ensures module
summaries are unconditionally emitted for full/fat lto, just like with
thin lto.
The WriteBitcodeToFile function just invokes the normal
BitcodeWriterPass under the hood, but doesn't provide a way to set the
argument for emitting module summaries. So this patch just adds the pass
directly and sets that argument.
Finally, now that we're generating module summaries for `-Clto=fat`
without using ThinLTO, we need to update `rustc-metadata` to keep
around a temporary directory that contains the `.rmeta` for a little
longer before we deserialize it.
Add -Zautodiff_post_passes flag to limit which llvm passes to run after enzyme to make autodiff tests more robust
* Add -Zautodiff_post_passes flag to limit which llvm passes to run after enzyme to make autodiff tests more robust
* Change some llvm ir check in codegen_llvm/autodiff tests
r? @ZuseZ4
In LLVM, FeatureKV/SubtargetKV pointers are now private:
https://github.com/llvm/llvm-project/pull/206237
This change fixes compiler errors when building rustc with ToT LLVM
by using the key() and desc() accessors.
disable naked-dead-code-elimination test if no RET mnemonic is available
this test emit x86_64 specific ret asm instruction and should not be compiled on any other arch.
Add infrastructure to query LLVM backend for specific assembly mnemonics
and use it in compiletest to conditionally run tests based on instruction
availability.
This fixes test failures with naked-dead-code-elimination which requires
the `RET` mnemonic.
Co-authored-by: Folkert de Vries <flokkievids@gmail.com>
Always use the ThinLTO pipeline for pre-link optimizations
When using cargo this was already effectively done for all dependencies as cargo passes -Clinker-plugin-lto without -Clto=fat/thin. -Clinker-plugin-lto assumes that ThinLTO will be used. The ThinLTO pre-link pipeline is faster than the fat LTO one. And according to the benchmarks in [^1] there is barely any runtime performance difference between executables that used fat LTO with the fat vs ThinLTO pre-link pipeline.
This also helps avoid having yet another code path if we want to support Unified LTO (that is a single bitcode file that supports being used for both fat LTO and ThinLTO when using linker plugin LTO, we already support it when rustc does LTO as ThinLTO bitcode is enough of a superset of fat LTO bitcode that it happens to work by accident if you don't explicitly have a check preventing mixing of them for the current set of LTO features that rustc exposes.) I'm currently still investigating if rustc would benefit from Unified LTO and how exactly to integrate it.
[^1]: https://discourse.llvm.org/t/rfc-a-unified-lto-bitcode-frontend/61774
When using cargo this was already effectively done for all dependencies
as cargo passes -Clinker-plugin-lto without -Clto=fat/thin.
-Clinker-plugin-lto assumes that ThinLTO will be used. The ThinLTO
pre-link pipeline is faster than the fat LTO one. And according to the
benchmarks in [1] there is barely any runtime performance difference
between executables that used fat LTO with the fat vs ThinLTO pre-link
pipeline.
[1]: https://discourse.llvm.org/t/rfc-a-unified-lto-bitcode-frontend/61774
As far as I can tell it was introduced to allow fat LTO with
-Clinker-plugin-lto. Later a change was made to automatically disable
ThinLTO summary generation when -Clinker-plugin-lto -Clto=fat is used,
so we can safely remove it.
This flag allows specifying the threshold size for placing static data
in large data sections when using the medium code model on x86-64.
When using -Ccode-model=medium, data smaller than this threshold uses
RIP-relative addressing (32-bit offsets), while larger data uses
absolute 64-bit addressing. This allows the compiler to generate more
efficient code for smaller data while still supporting data larger than
2GB.
This mirrors the -mlarge-data-threshold flag available in GCC and Clang.
The default threshold is 65536 bytes (64KB) if not specified, matching
LLVM's default behavior.
PassWrapper: Access GlobalValueSummaryInfo::SummaryList via getter for LLVM 22+
https://github.com/llvm/llvm-project/pull/164355 makes SummaryList private and provides a getter method.
`@rustbot` label llvm-main