From d89165239711daa47669da319b68ab85c95fde6a Mon Sep 17 00:00:00 2001 From: Benjamin Bouvier Date: Thu, 18 Mar 2021 15:21:40 +0100 Subject: [PATCH] cranelift: add support for the Mac aarch64 calling convention This bumps target-lexicon and adds support for the AppleAarch64 calling convention. Specifically for WebAssembly support, we only have to worry about the new stack slots convention. Stack slots don't need to be at least 8-bytes, they can be as small as the data type's size. For instance, if we need stack slots for (i32, i32), they can be located at offsets (+0, +4). Note that they still need to be properly aligned on the data type they're containing, though, so if we need stack slots for (i32, i64), we can't start the i64 slot at the +4 offset (it must start at the +8 offset). Added one test that was failing on the Mac M1, as well as other tests stressing different yet similar situations. --- Cargo.lock | 80 +++++------ cranelift/codegen/meta/src/shared/settings.rs | 1 + cranelift/codegen/src/isa/aarch64/abi.rs | 22 ++- cranelift/codegen/src/isa/call_conv.rs | 20 ++- cranelift/codegen/src/isa/x86/abi.rs | 1 + cranelift/codegen/src/machinst/abi_impl.rs | 3 +- tests/all/func.rs | 3 - tests/all/host_funcs.rs | 134 +++++++++++++++++- 8 files changed, 206 insertions(+), 58 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 574c97a2f20c..de64f99252d6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -127,9 +127,9 @@ checksum = "23b62fc65de8e4e7f52534fb52b0f3ed04746ae267519eef2a83941e8085068b" [[package]] name = "async-trait" -version = "0.1.42" +version = "0.1.48" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8d3a45e77e34375a7923b1e8febb049bb011f064714a8e17a1a616fef01da13d" +checksum = "36ea56748e10732c49404c153638a15ec3d6211ec5ff35d9bb20e13b93576adf" dependencies = [ "proc-macro2", "quote", @@ -281,9 +281,9 @@ checksum = "63396b8a4b9de3f4fdfb320ab6080762242f66a8ef174c49d8e19b674db4cdbe" [[package]] name = "byteorder" -version = "1.4.2" +version = "1.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ae44d1a3d5a19df61dd0c8beb138458ac2a53a7ac09eba97d55592540004306b" +checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" [[package]] name = "cap-fs-ext" @@ -483,9 +483,9 @@ dependencies = [ [[package]] name = "console" -version = "0.14.0" +version = "0.14.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7cc80946b3480f421c2f17ed1cb841753a371c7c5104f51d507e13f532c856aa" +checksum = "3993e6445baa160675931ec041a5e03ca84b9c6e32a056150d3aa2bdda0a1f45" dependencies = [ "encode_unicode", "lazy_static", @@ -1330,9 +1330,9 @@ checksum = "25a2bc672d1148e28034f176e01fffebb08b35768468cc954630da77a1449005" [[package]] name = "indexmap" -version = "1.6.1" +version = "1.6.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4fb1fa934250de4de8aef298d81c729a7d33d8c239daa3a7575e6b92bfc7313b" +checksum = "824845a0bf897a9042383849b02c1bc219c2383772efcd5c6f9766fa4b81aef3" dependencies = [ "autocfg 1.0.1", "hashbrown", @@ -1444,9 +1444,9 @@ checksum = "3576a87f2ba00f6f106fdfcd16db1d698d648a26ad8e0573cad8537c3c362d2a" [[package]] name = "libc" -version = "0.2.87" +version = "0.2.90" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "265d751d31d6780a3f956bb5b8022feba2d94eeee5a84ba64f4212eedca42213" +checksum = "ba4aede83fc3617411dc6993bc8c70919750c1c257c6ca6a502aed6e0e2394ae" [[package]] name = "libfuzzer-sys" @@ -1622,9 +1622,9 @@ dependencies = [ [[package]] name = "num-bigint" -version = "0.3.1" +version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5e9a41747ae4633fce5adffb4d2e81ffc5e89593cb19917f8fb2cc5ff76507bf" +checksum = "7d0a3d5e207573f948a9e5376662aa743a2ea13f7c50a554d7af443a73fbfeba" dependencies = [ "autocfg 1.0.1", "num-integer", @@ -1804,9 +1804,9 @@ dependencies = [ [[package]] name = "paste" -version = "1.0.4" +version = "1.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c5d65c4d95931acda4498f675e332fcbdc9a06705cd07086c510e9b6009cd1c1" +checksum = "acbf547ad0c65e31259204bd90935776d1c693cec2f4ff7abb7a1bbbd40dfe58" [[package]] name = "peeking_take_while" @@ -2288,14 +2288,13 @@ dependencies = [ [[package]] name = "regex" -version = "1.4.3" +version = "1.4.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d9251239e129e16308e70d853559389de218ac275b515068abc96829d05b948a" +checksum = "957056ecddbeba1b26965114e191d2e8589ce74db242b6ea25fc4062427a5c19" dependencies = [ "aho-corasick", "memchr", "regex-syntax", - "thread_local", ] [[package]] @@ -2310,9 +2309,9 @@ dependencies = [ [[package]] name = "regex-syntax" -version = "0.6.22" +version = "0.6.23" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b5eb417147ba9860a96cfe72a0b93bf88fee1744b5636ec99ab20c1aa9376581" +checksum = "24d5f089152e60f62d28b835fbff2cd2e8dc0baf1ac13343bef92ab7eed84548" [[package]] name = "region" @@ -2471,18 +2470,18 @@ dependencies = [ [[package]] name = "serde" -version = "1.0.123" +version = "1.0.124" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "92d5161132722baa40d802cc70b15262b98258453e85e5d1d365c757c73869ae" +checksum = "bd761ff957cb2a45fbb9ab3da6512de9de55872866160b23c25f1a841e99d29f" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.123" +version = "1.0.124" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9391c295d64fc0abb2c556bad848f33cb8296276b1ad2677d1ae1ace4f258f31" +checksum = "1800f7693e94e186f5e25a28291ae1570da908aff7d97a095dec1e56ff99069b" dependencies = [ "proc-macro2", "quote", @@ -2502,9 +2501,9 @@ dependencies = [ [[package]] name = "serde_test" -version = "1.0.123" +version = "1.0.124" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "38145a8510bdf71d9a8cceeb57664049538446e77f24648328bdbcf22dc7e169" +checksum = "61c59238fc0762e8aee0c6ec5f1a2e61d580d94f6274b9fcc91dc51d03fb40ba" dependencies = [ "serde", ] @@ -2586,7 +2585,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "db8d597fce66eb0f19dd129b9956e4054cba21aeaf97d4116595027b670fac50" dependencies = [ "chrono", - "num-bigint 0.3.1", + "num-bigint 0.3.2", "num-traits", "thiserror", ] @@ -2665,9 +2664,9 @@ dependencies = [ [[package]] name = "syn" -version = "1.0.60" +version = "1.0.64" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c700597eca8a5a762beb35753ef6b94df201c81cca676604f547495a0d7f0081" +checksum = "3fd9d1e9976102a03c542daa2eff1b43f9d72306342f3f8b3ed5fb8908195d6f" dependencies = [ "proc-macro2", "quote", @@ -2705,9 +2704,9 @@ dependencies = [ [[package]] name = "target-lexicon" -version = "0.11.2" +version = "0.11.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "422045212ea98508ae3d28025bc5aaa2bd4a9cdaecd442a08da2ee620ee9ea95" +checksum = "62b29e388d11a2c0605bdc806ce6ed1d623a5bdbbdd5b423053444999331184e" [[package]] name = "tempfile" @@ -2832,9 +2831,9 @@ dependencies = [ [[package]] name = "tracing-attributes" -version = "0.1.13" +version = "0.1.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a8a9bd1db7706f2373a190b0d067146caa39350c486f3d455b0e33b431f94c07" +checksum = "c42e6fa53307c8a17e4ccd4dc81cf5ec38db9209f59b222210375b54ee40d1e2" dependencies = [ "proc-macro2", "quote", @@ -2873,9 +2872,9 @@ dependencies = [ [[package]] name = "tracing-subscriber" -version = "0.2.16" +version = "0.2.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8ab8966ac3ca27126141f7999361cc97dd6fb4b71da04c02044fa9045d98bb96" +checksum = "705096c6f83bf68ea5d357a6aa01829ddbdac531b357b45abeca842938085baa" dependencies = [ "ansi_term 0.12.1", "chrono", @@ -2910,9 +2909,9 @@ dependencies = [ [[package]] name = "typenum" -version = "1.12.0" +version = "1.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "373c8a200f9e67a0c95e62a4f52fbf80c23b4381c05a17845531982fa99e6b33" +checksum = "879f6906492a7cd215bfa4cf595b600146ccfac0c79bcbd1f3000162af5e8b06" [[package]] name = "ucd-trie" @@ -2969,11 +2968,12 @@ dependencies = [ [[package]] name = "userfaultfd" -version = "0.3.0" +version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "18d8164d4a8198fa546e7553b529f53e82907214a25fafda4a6f90d978b30a5c" +checksum = "84740ac2a9f4b6e71686910763fba6badc645c0c677efd749b9ca8f5308bed26" dependencies = [ "bitflags", + "cfg-if 1.0.0", "libc", "nix", "thiserror", @@ -2999,9 +2999,9 @@ checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191" [[package]] name = "version_check" -version = "0.9.2" +version = "0.9.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b5a972e5669d67ba988ce3dc826706fb0a8b01471c088cb0b6110b805cc36aed" +checksum = "5fecdca9a5291cc2b8dcf7dc02453fee791a280f3743cb0905f8822ae463b3fe" [[package]] name = "void" diff --git a/cranelift/codegen/meta/src/shared/settings.rs b/cranelift/codegen/meta/src/shared/settings.rs index 2d3cfc6f389d..2acc34118dd7 100644 --- a/cranelift/codegen/meta/src/shared/settings.rs +++ b/cranelift/codegen/meta/src/shared/settings.rs @@ -192,6 +192,7 @@ pub(crate) fn define() -> SettingGroup { "cold", "system_v", "windows_fastcall", + "apple_aarch64", "baldrdash_system_v", "baldrdash_windows", "baldrdash_2020", diff --git a/cranelift/codegen/src/isa/aarch64/abi.rs b/cranelift/codegen/src/isa/aarch64/abi.rs index 2c3776fbf7bb..f6cb9cc5694e 100644 --- a/cranelift/codegen/src/isa/aarch64/abi.rs +++ b/cranelift/codegen/src/isa/aarch64/abi.rs @@ -171,6 +171,9 @@ impl ABIMachineSpec for AArch64MachineDeps { let has_baldrdash_tls = call_conv == isa::CallConv::Baldrdash2020; // See AArch64 ABI (https://c9x.me/compile/bib/abi-arm64.pdf), sections 5.4. + // MacOS aarch64 is slightly different, see also + // https://developer.apple.com/documentation/xcode/writing_arm64_code_for_apple_platforms. + let mut next_xreg = 0; let mut next_vreg = 0; let mut next_stack: u64 = 0; @@ -264,13 +267,24 @@ impl ABIMachineSpec for AArch64MachineDeps { *next_reg += 1; remaining_reg_vals -= 1; } else { - // Compute size. Every arg takes a minimum slot of 8 bytes. (16-byte - // stack alignment happens separately after all args.) + // Compute the stack slot's size. let size = (ty_bits(param.value_type) / 8) as u64; - let size = std::cmp::max(size, 8); - // Align. + + let size = if call_conv != isa::CallConv::AppleAarch64 { + // Every arg takes a minimum slot of 8 bytes. (16-byte stack + // alignment happens separately after all args.) + std::cmp::max(size, 8) + } else { + // MacOS aarch64 allows stack slots with sizes less than 8 + // bytes. They still need to be properly aligned on their + // natural data alignment, though. + size + }; + + // Align the stack slot. debug_assert!(size.is_power_of_two()); next_stack = align_to(next_stack, size); + ret.push(ABIArg::stack( next_stack as i64, param.value_type, diff --git a/cranelift/codegen/src/isa/call_conv.rs b/cranelift/codegen/src/isa/call_conv.rs index 61a94e5a43b0..a81c606ff295 100644 --- a/cranelift/codegen/src/isa/call_conv.rs +++ b/cranelift/codegen/src/isa/call_conv.rs @@ -10,22 +10,24 @@ use serde::{Deserialize, Serialize}; #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] #[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))] pub enum CallConv { - /// Best performance, not ABI-stable + /// Best performance, not ABI-stable. Fast, - /// Smallest caller code size, not ABI-stable + /// Smallest caller code size, not ABI-stable. Cold, - /// System V-style convention used on many platforms + /// System V-style convention used on many platforms. SystemV, - /// Windows "fastcall" convention, also used for x64 and ARM + /// Windows "fastcall" convention, also used for x64 and ARM. WindowsFastcall, - /// SpiderMonkey WebAssembly convention on systems using natively SystemV + /// Mac aarch64 calling convention, which is a tweak aarch64 ABI. + AppleAarch64, + /// SpiderMonkey WebAssembly convention on systems using natively SystemV. BaldrdashSystemV, - /// SpiderMonkey WebAssembly convention on Windows + /// SpiderMonkey WebAssembly convention on Windows. BaldrdashWindows, /// SpiderMonkey WebAssembly convention for "ABI-2020", with extra TLS /// register slots in the frame. Baldrdash2020, - /// Specialized convention for the probestack function + /// Specialized convention for the probestack function. Probestack, } @@ -36,6 +38,7 @@ impl CallConv { // Default to System V for unknown targets because most everything // uses System V. Ok(CallingConvention::SystemV) | Err(()) => Self::SystemV, + Ok(CallingConvention::AppleAarch64) => Self::AppleAarch64, Ok(CallingConvention::WindowsFastcall) => Self::WindowsFastcall, Ok(unimp) => unimplemented!("calling convention: {:?}", unimp), } @@ -49,6 +52,7 @@ impl CallConv { LibcallCallConv::Cold => Self::Cold, LibcallCallConv::SystemV => Self::SystemV, LibcallCallConv::WindowsFastcall => Self::WindowsFastcall, + LibcallCallConv::AppleAarch64 => Self::AppleAarch64, LibcallCallConv::BaldrdashSystemV => Self::BaldrdashSystemV, LibcallCallConv::BaldrdashWindows => Self::BaldrdashWindows, LibcallCallConv::Baldrdash2020 => Self::Baldrdash2020, @@ -80,6 +84,7 @@ impl fmt::Display for CallConv { Self::Cold => "cold", Self::SystemV => "system_v", Self::WindowsFastcall => "windows_fastcall", + Self::AppleAarch64 => "mac_aarch64", Self::BaldrdashSystemV => "baldrdash_system_v", Self::BaldrdashWindows => "baldrdash_windows", Self::Baldrdash2020 => "baldrdash_2020", @@ -96,6 +101,7 @@ impl str::FromStr for CallConv { "cold" => Ok(Self::Cold), "system_v" => Ok(Self::SystemV), "windows_fastcall" => Ok(Self::WindowsFastcall), + "mac_aarch64" => Ok(Self::AppleAarch64), "baldrdash_system_v" => Ok(Self::BaldrdashSystemV), "baldrdash_windows" => Ok(Self::BaldrdashWindows), "baldrdash_2020" => Ok(Self::Baldrdash2020), diff --git a/cranelift/codegen/src/isa/x86/abi.rs b/cranelift/codegen/src/isa/x86/abi.rs index 0117b8918b4e..c6df87ae2203 100644 --- a/cranelift/codegen/src/isa/x86/abi.rs +++ b/cranelift/codegen/src/isa/x86/abi.rs @@ -512,6 +512,7 @@ pub fn prologue_epilogue(func: &mut ir::Function, isa: &dyn TargetIsa) -> Codege } CallConv::Probestack => unimplemented!("probestack calling convention"), CallConv::Baldrdash2020 => unimplemented!("Baldrdash ABI 2020"), + CallConv::AppleAarch64 => unreachable!(), } } diff --git a/cranelift/codegen/src/machinst/abi_impl.rs b/cranelift/codegen/src/machinst/abi_impl.rs index f240c9a238e1..c17406b334d4 100644 --- a/cranelift/codegen/src/machinst/abi_impl.rs +++ b/cranelift/codegen/src/machinst/abi_impl.rs @@ -646,7 +646,8 @@ impl ABICalleeImpl { || call_conv == isa::CallConv::Fast || call_conv == isa::CallConv::Cold || call_conv.extends_baldrdash() - || call_conv.extends_windows_fastcall(), + || call_conv.extends_windows_fastcall() + || call_conv == isa::CallConv::AppleAarch64, "Unsupported calling convention: {:?}", call_conv ); diff --git a/tests/all/func.rs b/tests/all/func.rs index 73166cc8918a..b334056893ae 100644 --- a/tests/all/func.rs +++ b/tests/all/func.rs @@ -121,9 +121,6 @@ fn signatures_match() { } #[test] -// Note: Cranelift only supports refrerence types (used in the wasm in this -// test) on x64. -#[cfg(target_arch = "x86_64")] fn import_works() -> Result<()> { static HITS: AtomicUsize = AtomicUsize::new(0); diff --git a/tests/all/host_funcs.rs b/tests/all/host_funcs.rs index de9470114418..e193b761fcb4 100644 --- a/tests/all/host_funcs.rs +++ b/tests/all/host_funcs.rs @@ -219,9 +219,6 @@ fn signatures_match() -> Result<()> { } #[test] -// Note: Cranelift only supports refrerence types (used in the wasm in this -// test) on x64. -#[cfg(target_arch = "x86_64")] fn import_works() -> Result<()> { static HITS: AtomicUsize = AtomicUsize::new(0); @@ -328,6 +325,137 @@ fn import_works() -> Result<()> { Ok(()) } +#[test] +fn call_import_many_args() -> Result<()> { + let wasm = wat::parse_str( + r#" + (import "" "host" (func (param i32 i32 i32 i32 i32 i32 i32 i32 i32 i32))) + (func (export "run") + i32.const 1 + i32.const 2 + i32.const 3 + i32.const 4 + i32.const 5 + i32.const 6 + i32.const 7 + i32.const 8 + i32.const 9 + i32.const 10 + call 0 + ) + "#, + )?; + + let mut config = Config::new(); + + config.wrap_host_func( + "", + "host", + |x1: i32, + x2: i32, + x3: i32, + x4: i32, + x5: i32, + x6: i32, + x7: i32, + x8: i32, + x9: i32, + x10: i32| { + assert_eq!(x1, 1); + assert_eq!(x2, 2); + assert_eq!(x3, 3); + assert_eq!(x4, 4); + assert_eq!(x5, 5); + assert_eq!(x6, 6); + assert_eq!(x7, 7); + assert_eq!(x8, 8); + assert_eq!(x9, 9); + assert_eq!(x10, 10); + }, + ); + + let engine = Engine::new(&config)?; + let module = Module::new(&engine, &wasm)?; + + let store = Store::new(&engine); + let instance = Instance::new( + &store, + &module, + &[store + .get_host_func("", "host") + .expect("should be defined") + .into()], + )?; + + let run = instance.get_func("run").unwrap(); + run.call(&[])?; + + Ok(()) +} + +#[test] +fn call_wasm_many_args() -> Result<()> { + let wasm = wat::parse_str( + r#" + (func (export "run") (param i32 i32 i32 i32 i32 i32 i32 i32 i32 i32) + i32.const 1 + get_local 0 + i32.ne + if + unreachable + end + + i32.const 10 + get_local 9 + i32.ne + if + unreachable + end + ) + + (func (export "test") + i32.const 1 + i32.const 2 + i32.const 3 + i32.const 4 + i32.const 5 + i32.const 6 + i32.const 7 + i32.const 8 + i32.const 9 + i32.const 10 + call 0 + ) + "#, + )?; + + let config = Config::new(); + let engine = Engine::new(&config)?; + let module = Module::new(&engine, &wasm)?; + + let store = Store::new(&engine); + let instance = Instance::new(&store, &module, &[])?; + + let run = instance.get_func("run").unwrap(); + run.call(&[ + 1.into(), + 2.into(), + 3.into(), + 4.into(), + 5.into(), + 6.into(), + 7.into(), + 8.into(), + 9.into(), + 10.into(), + ])?; + + let test = instance.get_func("test").unwrap(); + test.call(&[])?; + + Ok(()) +} + #[test] fn trap_smoke() -> Result<()> { let mut config = Config::default();