fix(compiler/rustc_codegen_llvm): apply target-cpu attribute

This commit is contained in:
StackOverflowExcept1on 2025-08-12 00:39:24 +03:00
parent 350d0ef0ec
commit f978932903
6 changed files with 103 additions and 3 deletions

View file

@ -3283,7 +3283,7 @@ dependencies = [
"regex",
"serde_json",
"similar",
"wasmparser 0.219.2",
"wasmparser 0.236.0",
]
[[package]]

View file

@ -8,11 +8,12 @@ use rustc_middle::bug;
use rustc_middle::ty::TyCtxt;
use rustc_session::config::{DebugInfo, OomStrategy};
use rustc_symbol_mangling::mangle_internal_symbol;
use smallvec::SmallVec;
use crate::builder::SBuilder;
use crate::declare::declare_simple_fn;
use crate::llvm::{self, False, True, Type, Value};
use crate::{SimpleCx, attributes, debuginfo};
use crate::{SimpleCx, attributes, debuginfo, llvm_util};
pub(crate) unsafe fn codegen(
tcx: TyCtxt<'_>,
@ -147,6 +148,20 @@ fn create_wrapper_function(
llvm::Visibility::from_generic(tcx.sess.default_visibility()),
ty,
);
let mut attrs = SmallVec::<[_; 2]>::new();
let target_cpu = llvm_util::target_cpu(tcx.sess);
let target_cpu_attr = llvm::CreateAttrStringValue(cx.llcx, "target-cpu", target_cpu);
let tune_cpu_attr = llvm_util::tune_cpu(tcx.sess)
.map(|tune_cpu| llvm::CreateAttrStringValue(cx.llcx, "tune-cpu", tune_cpu));
attrs.push(target_cpu_attr);
attrs.extend(tune_cpu_attr);
attributes::apply_to_llfn(llfn, llvm::AttributePlace::Function, &attrs);
let no_return = if no_return {
// -> ! DIFlagNoReturn
let no_return = llvm::AttributeKind::NoReturn.create_attr(cx.llcx);

View file

@ -17,7 +17,7 @@ object = "0.37"
regex = "1.11"
serde_json = "1.0"
similar = "2.7"
wasmparser = { version = "0.219", default-features = false, features = ["std"] }
wasmparser = { version = "0.236", default-features = false, features = ["std", "features", "validate"] }
# tidy-alphabetical-end
# Shared with bootstrap and compiletest

View file

@ -0,0 +1,38 @@
//@ only-wasm32-wasip1
use std::path::Path;
use run_make_support::{cargo, path, rfs, target, wasmparser};
fn main() {
let target_dir = path("target");
cargo()
.args([
"rustc",
"--manifest-path",
"wasm32_test/Cargo.toml",
"--profile",
"release",
"--target",
"wasm32-wasip1",
"-Zbuild-std=core,alloc,panic_abort",
"--",
"-Clink-arg=--import-memory",
"-Clinker-plugin-lto=on",
])
.env("RUSTFLAGS", "-Ctarget-cpu=mvp")
.env("CARGO_TARGET_DIR", &target_dir)
.run();
let wasm32_program_path = target_dir.join(target()).join("release").join("wasm32_program.wasm");
verify_features(&wasm32_program_path);
}
fn verify_features(path: &Path) {
eprintln!("verify {path:?}");
let file = rfs::read(&path);
let mut validator = wasmparser::Validator::new_with_features(wasmparser::WasmFeatures::WASM1);
validator.validate_all(&file).unwrap();
}

View file

@ -0,0 +1,13 @@
[package]
name = "wasm32_test"
version = "0.1.0"
edition = "2024"
[lib]
crate-type = ["cdylib"]
name = "wasm32_program"
[profile.release]
codegen-units = 1
lto = "fat"
opt-level = "z"

View file

@ -0,0 +1,34 @@
#![no_std]
extern crate alloc;
use core::alloc::{GlobalAlloc, Layout};
use core::mem::MaybeUninit;
#[global_allocator]
static ALLOC: GlobalDlmalloc = GlobalDlmalloc;
struct GlobalDlmalloc;
unsafe impl GlobalAlloc for GlobalDlmalloc {
#[inline]
unsafe fn alloc(&self, _layout: Layout) -> *mut u8 {
core::ptr::null_mut()
}
#[inline]
unsafe fn dealloc(&self, _ptr: *mut u8, _layout: Layout) {}
}
#[used]
static mut BUF: MaybeUninit<[u8; 1024]> = MaybeUninit::uninit();
#[unsafe(no_mangle)]
extern "C" fn init() {
alloc::alloc::handle_alloc_error(Layout::new::<[u8; 64 * 1024]>());
}
#[panic_handler]
fn my_panic(_: &core::panic::PanicInfo) -> ! {
loop {}
}