Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions compiler/rustc_codegen_llvm/src/llvm_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ unsafe fn configure_llvm(sess: &EarlySession) {
let mut llvm_c_strs = Vec::with_capacity(n_args + 1);
let mut llvm_args = Vec::with_capacity(n_args + 1);

let (llvm_major, llvm_minor, llvm_patch) = get_version();
// Check to ensure we're running against the correct LLVM version.
unsafe {
let (llvm_major, llvm_minor, llvm_patch) = get_version();
let expected_version = llvm::LLVMRustVersionMajor();
if llvm_major != expected_version {
sess.dcx().emit_fatal(diagnostics::LlvmVersionMismatch {
Expand Down Expand Up @@ -127,10 +127,14 @@ unsafe fn configure_llvm(sess: &EarlySession) {
}
}

if get_version() < (24, 0, 0) && wants_wasm_eh(&sess.target) {
if llvm_major < 24 && wants_wasm_eh(&sess.target) {

@vadorovsky vadorovsky Nov 9, 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.

Happy to switch WASM over to llvm_args_by_version in a follow-up change.

View changes since the review

add("-wasm-enable-eh", false);
}

for arg in sess.target.llvm_args_for_llvm_version(llvm_major) {
add(arg.as_ref(), false);
}

// HACK(eddyb) LLVM inserts `llvm.assume` calls to preserve align attributes
// during inlining. Unfortunately these may block other optimizations.
add("-preserve-alignment-assumptions-during-inlining=false", false);
Expand Down
6 changes: 5 additions & 1 deletion compiler/rustc_target/src/spec/base/bpf.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use std::collections::BTreeMap;

use rustc_abi::Endian;

use crate::spec::{LinkerFlavor, MergeFunctions, PanicStrategy, TargetOptions};
use crate::spec::{LinkerFlavor, MergeFunctions, PanicStrategy, StaticCow, TargetOptions, cvs};

pub(crate) fn opts(endian: Endian) -> TargetOptions {
let allows_libcalls: StaticCow<[StaticCow<str>]> = cvs!["-bpf-allows-libcalls"];
TargetOptions {
allow_asm: true,
endian,
Expand All @@ -24,6 +27,7 @@ pub(crate) fn opts(endian: Endian) -> TargetOptions {
// and would require a bit of a refactor.
min_atomic_width: Some(64),
max_atomic_width: Some(64),
llvm_args_by_version: BTreeMap::from([(23, allows_libcalls)]),
..Default::default()
}
}
3 changes: 3 additions & 0 deletions compiler/rustc_target/src/spec/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ impl Target {
forward_opt!(rustc_abi);
forward!(relax_elf_relocations);
forward!(llvm_args);
forward!(llvm_args_by_version);
forward!(use_ctors_section);
forward!(eh_frame_header);
forward!(has_thumb_interworking);
Expand Down Expand Up @@ -396,6 +397,7 @@ impl ToJson for Target {
target_option_val!(rustc_abi);
target_option_val!(relax_elf_relocations);
target_option_val!(llvm_args);
target_option_val!(llvm_args_by_version);
target_option_val!(use_ctors_section);
target_option_val!(eh_frame_header);
target_option_val!(has_thumb_interworking);
Expand Down Expand Up @@ -619,6 +621,7 @@ struct TargetSpecJson {
rustc_abi: Option<RustcAbi>,
relax_elf_relocations: Option<bool>,
llvm_args: Option<StaticCow<[StaticCow<str>]>>,
llvm_args_by_version: Option<BTreeMap<u32, StaticCow<[StaticCow<str>]>>>,
use_ctors_section: Option<bool>,
eh_frame_header: Option<bool>,
has_thumb_interworking: Option<bool>,
Expand Down
9 changes: 9 additions & 0 deletions compiler/rustc_target/src/spec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2550,6 +2550,7 @@ pub struct TargetOptions {

/// Additional arguments to pass to LLVM, similar to the `-C llvm-args` codegen option.
pub llvm_args: StaticCow<[StaticCow<str>]>,
pub llvm_args_by_version: BTreeMap<u32, StaticCow<[StaticCow<str>]>>,

/// Whether to use legacy .ctors initialization hooks rather than .init_array. Defaults
/// to false (uses .init_array).
Expand Down Expand Up @@ -2835,6 +2836,7 @@ impl Default for TargetOptions {
rustc_abi: None,
relax_elf_relocations: false,
llvm_args: cvs![],
llvm_args_by_version: BTreeMap::new(),
use_ctors_section: false,
eh_frame_header: true,
has_thumb_interworking: false,
Expand Down Expand Up @@ -2893,6 +2895,13 @@ impl Target {
self.max_atomic_width.unwrap_or_else(|| self.pointer_width.into())
}

pub fn llvm_args_for_llvm_version<'a>(
&'a self,
major: u32,
) -> impl Iterator<Item = &'a StaticCow<str>> {
self.llvm_args_by_version.range(..=major).flat_map(|(_, args)| args.iter())
}

/// Test target self-consistency and JSON encoding/decoding roundtrip.
#[cfg(test)]
fn test_target(mut self) {
Expand Down
56 changes: 55 additions & 1 deletion compiler/rustc_target/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use std::borrow::Cow;
use std::collections::BTreeMap;

use crate::json::ToJson;
use crate::spec::Target;
use crate::spec::{Target, TargetTuple};

#[test]
fn report_unused_fields() {
Expand Down Expand Up @@ -40,3 +43,54 @@ fn custom_arch_propagates_from_json() {
assert_eq!(serialized["arch"], "customarch");
});
}

fn bpf_target_with_versioned_args(map: BTreeMap<u32, Cow<'static, [Cow<'static, str>]>>) -> Target {
let mut t = Target::expect_builtin(&TargetTuple::from_tuple("bpfeb-unknown-none"));
t.options.llvm_args_by_version = map;
t
}

#[test]
fn llvm_args_by_version_empty_map_returns_no_args() {
let t = bpf_target_with_versioned_args(BTreeMap::new());
assert!(t.llvm_args_for_llvm_version(1).collect::<Vec<_>>().is_empty());
assert!(t.llvm_args_for_llvm_version(9999).collect::<Vec<_>>().is_empty());
}

#[test]
fn llvm_args_by_version_excludes_entries_above_major() {
let t = bpf_target_with_versioned_args(BTreeMap::from([
(23u32, Cow::Borrowed(&[Cow::Borrowed("-a")][..])),
(24, Cow::Borrowed(&[Cow::Borrowed("-b")][..])),
]));
assert!(t.llvm_args_for_llvm_version(22).collect::<Vec<_>>().is_empty());
assert_eq!(t.llvm_args_for_llvm_version(23).collect::<Vec<_>>(), &[&Cow::Borrowed("-a")]);
assert_eq!(
t.llvm_args_for_llvm_version(24).collect::<Vec<_>>(),
&[&Cow::Borrowed("-a"), &Cow::Borrowed("-b")]
);
assert_eq!(
t.llvm_args_for_llvm_version(25).collect::<Vec<_>>(),
&[&Cow::Borrowed("-a"), &Cow::Borrowed("-b")]
);
}

#[test]
fn llvm_args_by_version_accumulates_multiple_entries() {
let t = bpf_target_with_versioned_args(BTreeMap::from([
(23u32, Cow::Borrowed(&[Cow::Borrowed("-a"), Cow::Borrowed("-b")][..])),
(24, Cow::Borrowed(&[Cow::Borrowed("-c")][..])),
]));
assert_eq!(
t.llvm_args_for_llvm_version(23).collect::<Vec<_>>(),
&[&Cow::Borrowed("-a"), &Cow::Borrowed("-b")]
);
assert_eq!(
t.llvm_args_for_llvm_version(24).collect::<Vec<_>>(),
&[&Cow::Borrowed("-a"), &Cow::Borrowed("-b"), &Cow::Borrowed("-c")]
);
assert_eq!(
t.llvm_args_for_llvm_version(100).collect::<Vec<_>>(),
&[&Cow::Borrowed("-a"), &Cow::Borrowed("-b"), &Cow::Borrowed("-c")]
);
}
19 changes: 19 additions & 0 deletions tests/assembly-llvm/bpf-libcall.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//@ add-minicore
//@ revisions: el eb
//@ assembly-output: emit-asm
//@ [el] compile-flags: --target bpfel-unknown-none -Copt-level=0
//@ [eb] compile-flags: --target bpfeb-unknown-none -Copt-level=0
//@ needs-llvm-components: bpf
//@ min-llvm-version: 23
#![feature(no_core, f16)]
#![no_core]
#![crate_type = "lib"]

extern crate minicore;

// CHECK-LABEL: f16_to_f32:
// CHECK: call __extendhfsf2
#[no_mangle]
pub fn f16_to_f32(x: f16) -> f32 {
x as f32
}
Loading