about summary refs log tree commit diff
diff options
context:
space:
mode:
authorStuart Cook <Zalathar@users.noreply.github.com>2025-08-15 16:16:38 +1000
committerGitHub <noreply@github.com>2025-08-15 16:16:38 +1000
commit201e6324a77fb4a91516dcd9a47a1fd31e18176f (patch)
tree4ac2d7c56f16a879f591df40941a5ee5e27fd367
parent8f1202cef20482312cefcfd0afe23aaf97bce853 (diff)
parent3a250b79390e9d0258ae463c875aac6c643956db (diff)
downloadrust-201e6324a77fb4a91516dcd9a47a1fd31e18176f.tar.gz
rust-201e6324a77fb4a91516dcd9a47a1fd31e18176f.zip
Rollup merge of #145275 - StackOverflowExcept1on:fix-wasm32v1-none, r=alexcrichton
fix(compiler/rustc_codegen_llvm): apply `target-cpu` attribute

Resolves rust-lang/rust#140174

r? ```@alexcrichton```

try-job: `test-various*`
-rw-r--r--Cargo.lock2
-rw-r--r--compiler/rustc_codegen_llvm/src/allocator.rs17
-rw-r--r--src/tools/run-make-support/Cargo.toml2
-rw-r--r--tests/run-make/wasm-unexpected-features/foo.rs43
-rw-r--r--tests/run-make/wasm-unexpected-features/rmake.rs26
5 files changed, 87 insertions, 3 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 2a5cc5e6f89..85df0dda818 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -3283,7 +3283,7 @@ dependencies = [
  "regex",
  "serde_json",
  "similar",
- "wasmparser 0.219.2",
+ "wasmparser 0.236.0",
 ]
 
 [[package]]
diff --git a/compiler/rustc_codegen_llvm/src/allocator.rs b/compiler/rustc_codegen_llvm/src/allocator.rs
index 2b5090ed6db..23610aa856c 100644
--- a/compiler/rustc_codegen_llvm/src/allocator.rs
+++ b/compiler/rustc_codegen_llvm/src/allocator.rs
@@ -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);
diff --git a/src/tools/run-make-support/Cargo.toml b/src/tools/run-make-support/Cargo.toml
index a4e7534137d..250e0f65a9f 100644
--- a/src/tools/run-make-support/Cargo.toml
+++ b/src/tools/run-make-support/Cargo.toml
@@ -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
diff --git a/tests/run-make/wasm-unexpected-features/foo.rs b/tests/run-make/wasm-unexpected-features/foo.rs
new file mode 100644
index 00000000000..5c7aa2d6190
--- /dev/null
+++ b/tests/run-make/wasm-unexpected-features/foo.rs
@@ -0,0 +1,43 @@
+#![no_core]
+#![crate_type = "cdylib"]
+#![feature(no_core, lang_items, allocator_internals, rustc_attrs)]
+#![needs_allocator]
+#![allow(internal_features)]
+
+#[rustc_std_internal_symbol]
+unsafe fn __rust_alloc(_size: usize, _align: usize) -> *mut u8 {
+    0 as *mut u8
+}
+
+unsafe extern "Rust" {
+    #[rustc_std_internal_symbol]
+    fn __rust_alloc_error_handler(size: usize, align: usize) -> !;
+}
+
+#[used]
+static mut BUF: [u8; 1024] = [0; 1024];
+
+#[unsafe(no_mangle)]
+extern "C" fn init() {
+    unsafe {
+        __rust_alloc_error_handler(0, 0);
+    }
+}
+
+mod minicore {
+    #[lang = "pointee_sized"]
+    pub trait PointeeSized {}
+
+    #[lang = "meta_sized"]
+    pub trait MetaSized: PointeeSized {}
+
+    #[lang = "sized"]
+    pub trait Sized: MetaSized {}
+
+    #[lang = "copy"]
+    pub trait Copy {}
+    impl Copy for u8 {}
+
+    #[lang = "drop_in_place"]
+    fn drop_in_place<T>(_: *mut T) {}
+}
diff --git a/tests/run-make/wasm-unexpected-features/rmake.rs b/tests/run-make/wasm-unexpected-features/rmake.rs
new file mode 100644
index 00000000000..416b5ef4caa
--- /dev/null
+++ b/tests/run-make/wasm-unexpected-features/rmake.rs
@@ -0,0 +1,26 @@
+//@ only-wasm32-wasip1
+
+use std::path::Path;
+
+use run_make_support::{rfs, rustc, wasmparser};
+
+fn main() {
+    rustc()
+        .input("foo.rs")
+        .target("wasm32-wasip1")
+        .target_cpu("mvp")
+        .opt_level("z")
+        .lto("fat")
+        .linker_plugin_lto("on")
+        .link_arg("--import-memory")
+        .run();
+    verify_features(Path::new("foo.wasm"));
+}
+
+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();
+}