Skip to content

Commit 5cb7716

Browse files
committed
Auto merge of #153684 - cuviper:min-llvm-21, r=nikic
Update the minimum external LLVM to 21 With this change, we'll have stable support for LLVM 21 and 22. For reference, the previous increase to LLVM 20 was #145071. cc @rust-lang/wg-llvm r? nikic
2 parents 4efe3dc + 52dfa94 commit 5cb7716

38 files changed

+68
-339
lines changed

compiler/rustc_codegen_llvm/src/abi.rs

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ use crate::attributes::{self, llfn_attrs_from_instance};
2323
use crate::builder::Builder;
2424
use crate::context::CodegenCx;
2525
use crate::llvm::{self, Attribute, AttributePlace, Type, Value};
26-
use crate::llvm_util;
2726
use crate::type_of::LayoutLlvmExt;
2827

2928
trait ArgAttributesExt {
@@ -46,6 +45,12 @@ const OPTIMIZATION_ATTRIBUTES: [(ArgAttribute, llvm::AttributeKind); 4] = [
4645
(ArgAttribute::NoUndef, llvm::AttributeKind::NoUndef),
4746
];
4847

48+
const CAPTURES_ATTRIBUTES: [(ArgAttribute, llvm::AttributeKind); 3] = [
49+
(ArgAttribute::CapturesNone, llvm::AttributeKind::CapturesNone),
50+
(ArgAttribute::CapturesAddress, llvm::AttributeKind::CapturesAddress),
51+
(ArgAttribute::CapturesReadOnly, llvm::AttributeKind::CapturesReadOnly),
52+
];
53+
4954
fn get_attrs<'ll>(this: &ArgAttributes, cx: &CodegenCx<'ll, '_>) -> SmallVec<[&'ll Attribute; 8]> {
5055
let mut regular = this.regular;
5156

@@ -82,18 +87,10 @@ fn get_attrs<'ll>(this: &ArgAttributes, cx: &CodegenCx<'ll, '_>) -> SmallVec<[&'
8287
attrs.push(llattr.create_attr(cx.llcx));
8388
}
8489
}
85-
// captures(...) is only available since LLVM 21.
86-
if (21, 0, 0) <= llvm_util::get_version() {
87-
const CAPTURES_ATTRIBUTES: [(ArgAttribute, llvm::AttributeKind); 3] = [
88-
(ArgAttribute::CapturesNone, llvm::AttributeKind::CapturesNone),
89-
(ArgAttribute::CapturesAddress, llvm::AttributeKind::CapturesAddress),
90-
(ArgAttribute::CapturesReadOnly, llvm::AttributeKind::CapturesReadOnly),
91-
];
92-
for (attr, llattr) in CAPTURES_ATTRIBUTES {
93-
if regular.contains(attr) {
94-
attrs.push(llattr.create_attr(cx.llcx));
95-
break;
96-
}
90+
for (attr, llattr) in CAPTURES_ATTRIBUTES {
91+
if regular.contains(attr) {
92+
attrs.push(llattr.create_attr(cx.llcx));
93+
break;
9794
}
9895
}
9996
} else if cx.tcx.sess.sanitizers().contains(SanitizerSet::MEMORY) {
@@ -508,9 +505,7 @@ impl<'ll, 'tcx> FnAbiLlvmExt<'ll, 'tcx> for FnAbi<'tcx, Ty<'tcx>> {
508505
}
509506
PassMode::Indirect { attrs, meta_attrs: None, on_stack: false } => {
510507
let i = apply(attrs);
511-
if cx.sess().opts.optimize != config::OptLevel::No
512-
&& llvm_util::get_version() >= (21, 0, 0)
513-
{
508+
if cx.sess().opts.optimize != config::OptLevel::No {
514509
attributes::apply_to_llfn(
515510
llfn,
516511
llvm::AttributePlace::Argument(i),

compiler/rustc_codegen_llvm/src/attributes.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -506,15 +506,11 @@ pub(crate) fn llfn_attrs_from_instance<'ll, 'tcx>(
506506
to_add.push(llvm::CreateAllocKindAttr(cx.llcx, AllocKindFlags::Free));
507507
// applies to argument place instead of function place
508508
let allocated_pointer = AttributeKind::AllocatedPointer.create_attr(cx.llcx);
509-
let attrs: &[_] = if llvm_util::get_version() >= (21, 0, 0) {
510-
// "Does not capture provenance" means "if the function call stashes the pointer somewhere,
511-
// accessing that pointer after the function returns is UB". That is definitely the case here since
512-
// freeing will destroy the provenance.
513-
let captures_addr = AttributeKind::CapturesAddress.create_attr(cx.llcx);
514-
&[allocated_pointer, captures_addr]
515-
} else {
516-
&[allocated_pointer]
517-
};
509+
// "Does not capture provenance" means "if the function call stashes the pointer somewhere,
510+
// accessing that pointer after the function returns is UB". That is definitely the case here since
511+
// freeing will destroy the provenance.
512+
let captures_addr = AttributeKind::CapturesAddress.create_attr(cx.llcx);
513+
let attrs = &[allocated_pointer, captures_addr];
518514
attributes::apply_to_llfn(llfn, AttributePlace::Argument(0), attrs);
519515
}
520516
if let Some(align) = codegen_fn_attrs.alignment {

compiler/rustc_codegen_llvm/src/context.rs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -190,17 +190,6 @@ pub(crate) unsafe fn create_module<'ll>(
190190
let mut target_data_layout = sess.target.data_layout.to_string();
191191
let llvm_version = llvm_util::get_version();
192192

193-
if llvm_version < (21, 0, 0) {
194-
if sess.target.arch == Arch::Nvptx64 {
195-
// LLVM 21 updated the default layout on nvptx: https://github.com/llvm/llvm-project/pull/124961
196-
target_data_layout = target_data_layout.replace("e-p6:32:32-i64", "e-i64");
197-
}
198-
if sess.target.arch == Arch::AmdGpu {
199-
// LLVM 21 adds the address width for address space 8.
200-
// See https://github.com/llvm/llvm-project/pull/139419
201-
target_data_layout = target_data_layout.replace("p8:128:128:128:48", "p8:128:128")
202-
}
203-
}
204193
if llvm_version < (22, 0, 0) {
205194
if sess.target.arch == Arch::Avr {
206195
// LLVM 22.0 updated the default layout on avr: https://github.com/llvm/llvm-project/pull/153010
@@ -342,11 +331,6 @@ pub(crate) unsafe fn create_module<'ll>(
342331
// Add "kcfi-arity" module flag if KCFI arity indicator is enabled. (See
343332
// https://github.com/llvm/llvm-project/pull/117121.)
344333
if sess.is_sanitizer_kcfi_arity_enabled() {
345-
// KCFI arity indicator requires LLVM 21.0.0 or later.
346-
if llvm_version < (21, 0, 0) {
347-
tcx.dcx().emit_err(crate::errors::SanitizerKcfiArityRequiresLLVM2100);
348-
}
349-
350334
llvm::add_module_flag_u32(
351335
llmod,
352336
llvm::ModuleFlagMergeBehavior::Override,

compiler/rustc_codegen_llvm/src/errors.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,3 @@ pub(crate) struct MismatchedDataLayout<'a> {
204204
pub(crate) struct FixedX18InvalidArch<'a> {
205205
pub arch: &'a str,
206206
}
207-
208-
#[derive(Diagnostic)]
209-
#[diag("`-Zsanitizer-kcfi-arity` requires LLVM 21.0.0 or later")]
210-
pub(crate) struct SanitizerKcfiArityRequiresLLVM2100;

compiler/rustc_codegen_llvm/src/llvm_util.rs

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -254,10 +254,6 @@ pub(crate) fn to_llvm_features<'a>(sess: &Session, s: &'a str) -> Option<LLVMFea
254254
},
255255

256256
// Filter out features that are not supported by the current LLVM version
257-
Arch::LoongArch32 | Arch::LoongArch64 => match s {
258-
"32s" if major < 21 => None,
259-
s => Some(LLVMFeature::new(s)),
260-
},
261257
Arch::PowerPC | Arch::PowerPC64 => match s {
262258
"power8-crypto" => Some(LLVMFeature::new("crypto")),
263259
s => Some(LLVMFeature::new(s)),
@@ -372,23 +368,12 @@ fn update_target_reliable_float_cfg(sess: &Session, cfg: &mut TargetConfig) {
372368
let (major, _, _) = version;
373369

374370
cfg.has_reliable_f16 = match (target_arch, target_os) {
375-
// LLVM crash without neon <https://github.com/llvm/llvm-project/issues/129394> (fixed in LLVM 20.1.1)
376-
(Arch::AArch64, _)
377-
if !cfg.target_features.iter().any(|f| f.as_str() == "neon")
378-
&& version < (20, 1, 1) =>
379-
{
380-
false
381-
}
382371
// Unsupported <https://github.com/llvm/llvm-project/issues/94434> (fixed in llvm22)
383372
(Arch::Arm64EC, _) if major < 22 => false,
384-
// Selection failure <https://github.com/llvm/llvm-project/issues/50374> (fixed in llvm21)
385-
(Arch::S390x, _) if major < 21 => false,
386373
// MinGW ABI bugs <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115054>
387374
(Arch::X86_64, Os::Windows) if *target_env == Env::Gnu && *target_abi != Abi::Llvm => false,
388375
// Infinite recursion <https://github.com/llvm/llvm-project/issues/97981>
389376
(Arch::CSky, _) if major < 22 => false, // (fixed in llvm22)
390-
(Arch::Hexagon, _) if major < 21 => false, // (fixed in llvm21)
391-
(Arch::LoongArch32 | Arch::LoongArch64, _) if major < 21 => false, // (fixed in llvm21)
392377
(Arch::PowerPC | Arch::PowerPC64, _) if major < 22 => false, // (fixed in llvm22)
393378
(Arch::Sparc | Arch::Sparc64, _) if major < 22 => false, // (fixed in llvm22)
394379
(Arch::Wasm32 | Arch::Wasm64, _) if major < 22 => false, // (fixed in llvm22)
@@ -403,8 +388,6 @@ fn update_target_reliable_float_cfg(sess: &Session, cfg: &mut TargetConfig) {
403388
(Arch::AmdGpu, _) => false,
404389
// Unsupported <https://github.com/llvm/llvm-project/issues/94434>
405390
(Arch::Arm64EC, _) => false,
406-
// Selection bug <https://github.com/llvm/llvm-project/issues/96432> (fixed in LLVM 20.1.0)
407-
(Arch::Mips64 | Arch::Mips64r6, _) if version < (20, 1, 0) => false,
408391
// Selection bug <https://github.com/llvm/llvm-project/issues/95471>. This issue is closed
409392
// but basic math still does not work.
410393
(Arch::Nvptx64, _) => false,
@@ -413,9 +396,6 @@ fn update_target_reliable_float_cfg(sess: &Session, cfg: &mut TargetConfig) {
413396
(Arch::PowerPC | Arch::PowerPC64, _) => false,
414397
// ABI unsupported <https://github.com/llvm/llvm-project/issues/41838>
415398
(Arch::Sparc, _) => false,
416-
// Stack alignment bug <https://github.com/llvm/llvm-project/issues/77401>. NB: tests may
417-
// not fail if our compiler-builtins is linked. (fixed in llvm21)
418-
(Arch::X86, _) if major < 21 => false,
419399
// MinGW ABI bugs <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115054>
420400
(Arch::X86_64, Os::Windows) if *target_env == Env::Gnu && *target_abi != Abi::Llvm => false,
421401
// There are no known problems on other platforms, so the only requirement is that symbols

compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -301,12 +301,7 @@ extern "C" LLVMTargetMachineRef LLVMRustCreateTargetMachine(
301301

302302
std::string Error;
303303
auto Trip = Triple(Triple::normalize(TripleStr));
304-
const llvm::Target *TheTarget =
305-
#if LLVM_VERSION_GE(21, 0)
306-
TargetRegistry::lookupTarget(Trip, Error);
307-
#else
308-
TargetRegistry::lookupTarget(Trip.getTriple(), Error);
309-
#endif
304+
const llvm::Target *TheTarget = TargetRegistry::lookupTarget(Trip, Error);
310305
if (TheTarget == nullptr) {
311306
LLVMRustSetLastError(Error.c_str());
312307
return nullptr;
@@ -367,13 +362,8 @@ extern "C" LLVMTargetMachineRef LLVMRustCreateTargetMachine(
367362

368363
Options.EmitStackSizeSection = EmitStackSizeSection;
369364

370-
#if LLVM_VERSION_GE(21, 0)
371365
TargetMachine *TM = TheTarget->createTargetMachine(Trip, CPU, Feature,
372366
Options, RM, CM, OptLevel);
373-
#else
374-
TargetMachine *TM = TheTarget->createTargetMachine(
375-
Trip.getTriple(), CPU, Feature, Options, RM, CM, OptLevel);
376-
#endif
377367

378368
if (LargeDataThreshold != 0) {
379369
TM->setLargeDataThreshold(LargeDataThreshold);
@@ -701,12 +691,8 @@ extern "C" LLVMRustResult LLVMRustOptimize(
701691
if (LintIR) {
702692
PipelineStartEPCallbacks.push_back([](ModulePassManager &MPM,
703693
OptimizationLevel Level) {
704-
#if LLVM_VERSION_GE(21, 0)
705694
MPM.addPass(
706695
createModuleToFunctionPassAdaptor(LintPass(/*AbortOnError=*/true)));
707-
#else
708-
MPM.addPass(createModuleToFunctionPassAdaptor(LintPass()));
709-
#endif
710696
});
711697
}
712698

@@ -1210,12 +1196,8 @@ LLVMRustCreateThinLTOData(LLVMRustThinLTOModule *modules, size_t num_modules,
12101196
// Convert the preserved symbols set from string to GUID, this is then needed
12111197
// for internalization.
12121198
for (size_t i = 0; i < num_symbols; i++) {
1213-
#if LLVM_VERSION_GE(21, 0)
12141199
auto GUID =
12151200
GlobalValue::getGUIDAssumingExternalLinkage(preserved_symbols[i]);
1216-
#else
1217-
auto GUID = GlobalValue::getGUID(preserved_symbols[i]);
1218-
#endif
12191201
Ret->GUIDPreservedSymbols.insert(GUID);
12201202
}
12211203

@@ -1474,21 +1456,12 @@ extern "C" void LLVMRustComputeLTOCacheKey(RustStringRef KeyOut,
14741456
DenseSet<GlobalValue::GUID> CfiFunctionDecls;
14751457

14761458
// Based on the 'InProcessThinBackend' constructor in LLVM
1477-
#if LLVM_VERSION_GE(21, 0)
14781459
for (auto &Name : Data->Index.cfiFunctionDefs().symbols())
14791460
CfiFunctionDefs.insert(GlobalValue::getGUIDAssumingExternalLinkage(
14801461
GlobalValue::dropLLVMManglingEscape(Name)));
14811462
for (auto &Name : Data->Index.cfiFunctionDecls().symbols())
14821463
CfiFunctionDecls.insert(GlobalValue::getGUIDAssumingExternalLinkage(
14831464
GlobalValue::dropLLVMManglingEscape(Name)));
1484-
#else
1485-
for (auto &Name : Data->Index.cfiFunctionDefs())
1486-
CfiFunctionDefs.insert(
1487-
GlobalValue::getGUID(GlobalValue::dropLLVMManglingEscape(Name)));
1488-
for (auto &Name : Data->Index.cfiFunctionDecls())
1489-
CfiFunctionDecls.insert(
1490-
GlobalValue::getGUID(GlobalValue::dropLLVMManglingEscape(Name)));
1491-
#endif
14921465

14931466
Key = llvm::computeLTOCacheKey(conf, Data->Index, ModId, ImportList,
14941467
ExportList, ResolvedODR, DefinedGlobals,

compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,7 @@ extern "C" void LLVMRustSetLastError(const char *Err) {
137137

138138
extern "C" void LLVMRustSetNormalizedTarget(LLVMModuleRef M,
139139
const char *Target) {
140-
#if LLVM_VERSION_GE(21, 0)
141140
unwrap(M)->setTargetTriple(Triple(Triple::normalize(Target)));
142-
#else
143-
unwrap(M)->setTargetTriple(Triple::normalize(Target));
144-
#endif
145141
}
146142

147143
extern "C" void LLVMRustPrintPassTimings(RustStringRef OutBuf) {
@@ -452,11 +448,7 @@ static Attribute::AttrKind fromRust(LLVMRustAttributeKind Kind) {
452448
case LLVMRustAttributeKind::DeadOnUnwind:
453449
return Attribute::DeadOnUnwind;
454450
case LLVMRustAttributeKind::DeadOnReturn:
455-
#if LLVM_VERSION_GE(21, 0)
456451
return Attribute::DeadOnReturn;
457-
#else
458-
report_fatal_error("DeadOnReturn attribute requires LLVM 21 or later");
459-
#endif
460452
case LLVMRustAttributeKind::CapturesAddress:
461453
case LLVMRustAttributeKind::CapturesReadOnly:
462454
case LLVMRustAttributeKind::CapturesNone:
@@ -514,7 +506,6 @@ extern "C" void LLVMRustEraseInstFromParent(LLVMValueRef Instr) {
514506

515507
extern "C" LLVMAttributeRef
516508
LLVMRustCreateAttrNoValue(LLVMContextRef C, LLVMRustAttributeKind RustAttr) {
517-
#if LLVM_VERSION_GE(21, 0)
518509
if (RustAttr == LLVMRustAttributeKind::CapturesNone) {
519510
return wrap(Attribute::getWithCaptureInfo(*unwrap(C), CaptureInfo::none()));
520511
}
@@ -527,7 +518,6 @@ LLVMRustCreateAttrNoValue(LLVMContextRef C, LLVMRustAttributeKind RustAttr) {
527518
*unwrap(C), CaptureInfo(CaptureComponents::Address |
528519
CaptureComponents::ReadProvenance)));
529520
}
530-
#endif
531521
#if LLVM_VERSION_GE(23, 0)
532522
if (RustAttr == LLVMRustAttributeKind::DeadOnReturn) {
533523
return wrap(Attribute::getWithDeadOnReturnInfo(*unwrap(C),

src/bootstrap/src/core/build_steps/llvm.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -631,11 +631,11 @@ fn check_llvm_version(builder: &Builder<'_>, llvm_config: &Path) {
631631
let version = get_llvm_version(builder, llvm_config);
632632
let mut parts = version.split('.').take(2).filter_map(|s| s.parse::<u32>().ok());
633633
if let (Some(major), Some(_minor)) = (parts.next(), parts.next())
634-
&& major >= 20
634+
&& major >= 21
635635
{
636636
return;
637637
}
638-
panic!("\n\nbad LLVM version: {version}, need >=20\n\n")
638+
panic!("\n\nbad LLVM version: {version}, need >=21\n\n")
639639
}
640640

641641
fn configure_cmake(

src/ci/docker/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ To run a specific CI job locally, you can use the `citool` Rust crate:
1414
cargo run --manifest-path src/ci/citool/Cargo.toml run-local <job-name>
1515
```
1616

17-
For example, to run the `x86_64-gnu-llvm-20-1` job:
17+
For example, to run the `x86_64-gnu-llvm-21-1` job:
1818
```
19-
cargo run --manifest-path src/ci/citool/Cargo.toml run-local x86_64-gnu-llvm-20-1
19+
cargo run --manifest-path src/ci/citool/Cargo.toml run-local x86_64-gnu-llvm-21-1
2020
```
2121

2222
The job will output artifacts in an `obj/<image-name>` dir at the root of a repository. Note
@@ -27,10 +27,10 @@ Docker image executed in the given CI job.
2727
while locally, to the `obj/<image-name>` directory. This is primarily to prevent
2828
strange linker errors when using multiple Docker images.
2929

30-
For some Linux workflows (for example `x86_64-gnu-llvm-20-N`), the process is more involved. You will need to see which script is executed for the given workflow inside the [`jobs.yml`](../github-actions/jobs.yml) file and pass it through the `DOCKER_SCRIPT` environment variable. For example, to reproduce the `x86_64-gnu-llvm-20-3` workflow, you can run the following script:
30+
For some Linux workflows (for example `x86_64-gnu-llvm-21-N`), the process is more involved. You will need to see which script is executed for the given workflow inside the [`jobs.yml`](../github-actions/jobs.yml) file and pass it through the `DOCKER_SCRIPT` environment variable. For example, to reproduce the `x86_64-gnu-llvm-21-3` workflow, you can run the following script:
3131

3232
```
33-
DOCKER_SCRIPT=x86_64-gnu-llvm3.sh ./src/ci/docker/run.sh x86_64-gnu-llvm-20
33+
DOCKER_SCRIPT=x86_64-gnu-llvm3.sh ./src/ci/docker/run.sh x86_64-gnu-llvm-21
3434
```
3535

3636
## Local Development

src/ci/docker/host-aarch64/aarch64-gnu-llvm-20/Dockerfile renamed to src/ci/docker/host-aarch64/aarch64-gnu-llvm-21/Dockerfile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
1515
cmake \
1616
sudo \
1717
gdb \
18-
llvm-20-tools \
19-
llvm-20-dev \
18+
llvm-21-tools \
19+
llvm-21-dev \
2020
libedit-dev \
2121
libssl-dev \
2222
pkg-config \
@@ -42,7 +42,7 @@ ENV EXTERNAL_LLVM="1"
4242

4343
# Using llvm-link-shared due to libffi issues -- see #34486
4444
ENV RUST_CONFIGURE_ARGS="--build=aarch64-unknown-linux-gnu \
45-
--llvm-root=/usr/lib/llvm-20 \
45+
--llvm-root=/usr/lib/llvm-21 \
4646
--enable-llvm-link-shared \
4747
--set rust.randomize-layout=true \
4848
--set rust.thin-lto-import-instr-limit=10"

0 commit comments

Comments
 (0)