Skip to content

Commit f978932

Browse files
fix(compiler/rustc_codegen_llvm): apply target-cpu attribute
1 parent 350d0ef commit f978932

File tree

6 files changed

+103
-3
lines changed

6 files changed

+103
-3
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3283,7 +3283,7 @@ dependencies = [
32833283
"regex",
32843284
"serde_json",
32853285
"similar",
3286-
"wasmparser 0.219.2",
3286+
"wasmparser 0.236.0",
32873287
]
32883288

32893289
[[package]]

compiler/rustc_codegen_llvm/src/allocator.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ use rustc_middle::bug;
88
use rustc_middle::ty::TyCtxt;
99
use rustc_session::config::{DebugInfo, OomStrategy};
1010
use rustc_symbol_mangling::mangle_internal_symbol;
11+
use smallvec::SmallVec;
1112

1213
use crate::builder::SBuilder;
1314
use crate::declare::declare_simple_fn;
1415
use crate::llvm::{self, False, True, Type, Value};
15-
use crate::{SimpleCx, attributes, debuginfo};
16+
use crate::{SimpleCx, attributes, debuginfo, llvm_util};
1617

1718
pub(crate) unsafe fn codegen(
1819
tcx: TyCtxt<'_>,
@@ -147,6 +148,20 @@ fn create_wrapper_function(
147148
llvm::Visibility::from_generic(tcx.sess.default_visibility()),
148149
ty,
149150
);
151+
152+
let mut attrs = SmallVec::<[_; 2]>::new();
153+
154+
let target_cpu = llvm_util::target_cpu(tcx.sess);
155+
let target_cpu_attr = llvm::CreateAttrStringValue(cx.llcx, "target-cpu", target_cpu);
156+
157+
let tune_cpu_attr = llvm_util::tune_cpu(tcx.sess)
158+
.map(|tune_cpu| llvm::CreateAttrStringValue(cx.llcx, "tune-cpu", tune_cpu));
159+
160+
attrs.push(target_cpu_attr);
161+
attrs.extend(tune_cpu_attr);
162+
163+
attributes::apply_to_llfn(llfn, llvm::AttributePlace::Function, &attrs);
164+
150165
let no_return = if no_return {
151166
// -> ! DIFlagNoReturn
152167
let no_return = llvm::AttributeKind::NoReturn.create_attr(cx.llcx);

src/tools/run-make-support/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ object = "0.37"
1717
regex = "1.11"
1818
serde_json = "1.0"
1919
similar = "2.7"
20-
wasmparser = { version = "0.219", default-features = false, features = ["std"] }
20+
wasmparser = { version = "0.236", default-features = false, features = ["std", "features", "validate"] }
2121
# tidy-alphabetical-end
2222

2323
# Shared with bootstrap and compiletest
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//@ only-wasm32-wasip1
2+
3+
use std::path::Path;
4+
5+
use run_make_support::{cargo, path, rfs, target, wasmparser};
6+
7+
fn main() {
8+
let target_dir = path("target");
9+
10+
cargo()
11+
.args([
12+
"rustc",
13+
"--manifest-path",
14+
"wasm32_test/Cargo.toml",
15+
"--profile",
16+
"release",
17+
"--target",
18+
"wasm32-wasip1",
19+
"-Zbuild-std=core,alloc,panic_abort",
20+
"--",
21+
"-Clink-arg=--import-memory",
22+
"-Clinker-plugin-lto=on",
23+
])
24+
.env("RUSTFLAGS", "-Ctarget-cpu=mvp")
25+
.env("CARGO_TARGET_DIR", &target_dir)
26+
.run();
27+
28+
let wasm32_program_path = target_dir.join(target()).join("release").join("wasm32_program.wasm");
29+
verify_features(&wasm32_program_path);
30+
}
31+
32+
fn verify_features(path: &Path) {
33+
eprintln!("verify {path:?}");
34+
let file = rfs::read(&path);
35+
36+
let mut validator = wasmparser::Validator::new_with_features(wasmparser::WasmFeatures::WASM1);
37+
validator.validate_all(&file).unwrap();
38+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[package]
2+
name = "wasm32_test"
3+
version = "0.1.0"
4+
edition = "2024"
5+
6+
[lib]
7+
crate-type = ["cdylib"]
8+
name = "wasm32_program"
9+
10+
[profile.release]
11+
codegen-units = 1
12+
lto = "fat"
13+
opt-level = "z"
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#![no_std]
2+
3+
extern crate alloc;
4+
5+
use core::alloc::{GlobalAlloc, Layout};
6+
use core::mem::MaybeUninit;
7+
8+
#[global_allocator]
9+
static ALLOC: GlobalDlmalloc = GlobalDlmalloc;
10+
11+
struct GlobalDlmalloc;
12+
13+
unsafe impl GlobalAlloc for GlobalDlmalloc {
14+
#[inline]
15+
unsafe fn alloc(&self, _layout: Layout) -> *mut u8 {
16+
core::ptr::null_mut()
17+
}
18+
19+
#[inline]
20+
unsafe fn dealloc(&self, _ptr: *mut u8, _layout: Layout) {}
21+
}
22+
23+
#[used]
24+
static mut BUF: MaybeUninit<[u8; 1024]> = MaybeUninit::uninit();
25+
26+
#[unsafe(no_mangle)]
27+
extern "C" fn init() {
28+
alloc::alloc::handle_alloc_error(Layout::new::<[u8; 64 * 1024]>());
29+
}
30+
31+
#[panic_handler]
32+
fn my_panic(_: &core::panic::PanicInfo) -> ! {
33+
loop {}
34+
}

0 commit comments

Comments
 (0)