A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
Cranelift: generalize `stack_addr` offsets. (#14710)
Chris Fallin5948451372cc
diff --git a/cranelift/codegen/src/isa/aarch64/lower.isle b/cranelift/codegen/src/isa/aarch64/lower.isleindex a5a6d13e93..253e0f063b 100644--- a/cranelift/codegen/src/isa/aarch64/lower.isle+++ b/cranelift/codegen/src/isa/aarch64/lower.isle@@ -2978,6 +2978,14 @@(rule (lower (stack_addr _ stack_slot offset))(compute_stack_addr stack_slot offset))+;; An offset too large to fold into the stack slot's address computation is+;; added separately.+(rule 1 (lower (stack_addr _ stack_slot offset))+ (if-let large_offset (abi_stackslot_large_offset stack_slot offset))+ (add $I64+ (compute_stack_addr stack_slot (i32_to_offset32 0))+ (imm $I64 (ImmExtend.Sign) (i64_cast_unsigned large_offset))))+;;; Rules for `vhigh_bits` ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; All three sequences use one integer temporary and two vectordiff --git a/cranelift/codegen/src/isa/pulley_shared/lower.isle b/cranelift/codegen/src/isa/pulley_shared/lower.isleindex da423813ac..28798e01aa 100644--- a/cranelift/codegen/src/isa/pulley_shared/lower.isle+++ b/cranelift/codegen/src/isa/pulley_shared/lower.isle@@ -1363,6 +1363,17 @@(rule (lower (stack_addr _ stack_slot offset))(lower_stack_addr stack_slot offset))+;; An offset too large to fold into the stack slot's address computation is+;; added separately.+(rule 1 (lower (stack_addr $I32 stack_slot offset))+ (if-let large_offset (abi_stackslot_large_offset stack_slot offset))+ (pulley_xadd32 (lower_stack_addr stack_slot (i32_to_offset32 0))+ (imm large_offset)))+(rule 1 (lower (stack_addr $I64 stack_slot offset))+ (if-let large_offset (abi_stackslot_large_offset stack_slot offset))+ (pulley_xadd64 (lower_stack_addr stack_slot (i32_to_offset32 0))+ (imm large_offset)))+(decl lower_stack_addr (StackSlot Offset32) XReg)(rule (lower_stack_addr stack_slot offset)(let ((dst WritableXReg (temp_writable_xreg))diff --git a/cranelift/codegen/src/isa/riscv64/lower.isle b/cranelift/codegen/src/isa/riscv64/lower.isleindex aaf29497db..598a8d6acd 100644--- a/cranelift/codegen/src/isa/riscv64/lower.isle+++ b/cranelift/codegen/src/isa/riscv64/lower.isle@@ -1976,6 +1976,14 @@(lower (stack_addr _ ss offset))(gen_stack_addr ss offset))+;; An offset too large to fold into the stack slot's address computation is+;; added separately.+(rule 1+ (lower (stack_addr _ ss offset))+ (if-let large_offset (abi_stackslot_large_offset ss offset))+ (rv_add (gen_stack_addr ss (i32_to_offset32 0))+ (imm $I64 (i64_cast_unsigned large_offset))))+;;;;; Rules for `select`;;;;;;;;;;; Manually matching (iconst 0) here is a bit of a hack. We can't do that as partdiff --git a/cranelift/codegen/src/isa/riscv64/lower/isle.rs b/cranelift/codegen/src/isa/riscv64/lower/isle.rsindex 12b7dd9eb4..52e610a9b2 100644--- a/cranelift/codegen/src/isa/riscv64/lower/isle.rs+++ b/cranelift/codegen/src/isa/riscv64/lower/isle.rs@@ -574,7 +574,7 @@ impl generated_code::Context for RV64IsleContext<'_, '_, MInst, Riscv64Backend>let i = self.lower_ctx.abi()- .sized_stackslot_addr(slot, i64::from(offset) as u32, result);+ .sized_stackslot_addr(slot, i32::from(offset), result);self.emit(&i);result.to_reg()}diff --git a/cranelift/codegen/src/isa/s390x/lower.isle b/cranelift/codegen/src/isa/s390x/lower.isleindex a3d378cf80..83f272f108 100644--- a/cranelift/codegen/src/isa/s390x/lower.isle+++ b/cranelift/codegen/src/isa/s390x/lower.isle@@ -2798,6 +2798,14 @@(rule (lower (stack_addr ty stack_slot offset))(stack_addr_impl ty stack_slot offset))+;; An offset too large to fold into the stack slot's address computation is+;; added separately.+(rule 1 (lower (stack_addr ty stack_slot offset))+ (if-let large_offset (abi_stackslot_large_offset stack_slot offset))+ (add_reg ty+ (stack_addr_impl ty stack_slot (i32_to_offset32 0))+ (imm ty (i64_cast_unsigned large_offset))))+;;;; Rules for `func_addr` ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;diff --git a/cranelift/codegen/src/isa/x64/lower.isle b/cranelift/codegen/src/isa/x64/lower.isleindex dc5c3169cc..328751d16d 100644--- a/cranelift/codegen/src/isa/x64/lower.isle+++ b/cranelift/codegen/src/isa/x64/lower.isle@@ -4460,6 +4460,14 @@(rule (lower (stack_addr _ stack_slot offset))(stack_addr_impl stack_slot offset))+;; An offset too large to fold into the stack slot's address computation is+;; added separately.+(rule 1 (lower (stack_addr _ stack_slot offset))+ (if-let large_offset (abi_stackslot_large_offset stack_slot offset))+ (x64_add $I64+ (stack_addr_impl stack_slot (i32_to_offset32 0))+ (imm $I64 (i64_cast_unsigned large_offset))))+;; Rules for `udiv` ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; NB: a `RegMem` divisor, while allowed in the instruction encoding, isn'tdiff --git a/cranelift/codegen/src/machinst/abi.rs b/cranelift/codegen/src/machinst/abi.rsindex e424ec8011..da2cef54d5 100644--- a/cranelift/codegen/src/machinst/abi.rs+++ b/cranelift/codegen/src/machinst/abi.rs@@ -1331,27 +1331,10 @@ impl<M: ABIMachineSpec> Callee<M> {// The whole frame must fit in `maximum_frame_size` (checked with the// final layout in `compute_frame_layout`), so reject oversized stack- // slots now. Also check the offsets that `stack_addr` adds to them, so- // that every stack slot address computed during lowering is in range.+ // slots now.if stackslots_size > M::maximum_frame_size() {return Err(CodegenError::ImplLimitExceeded);}- for block in f.layout.blocks() {- for inst in f.layout.block_insts(block) {- if let ir::InstructionData::StackAddr {- stack_slot, offset, ..- } = f.dfg.insts[inst]- {- let offset = i64::from(offset);- if offset < 0- || stackslot_area_offset::<M>(sized_stackslots[stack_slot], offset)- .is_none()- {- return Err(CodegenError::ImplLimitExceeded);- }- }- }- }let mut dynamic_type_sizes = HashMap::with_capacity(f.dfg.dynamic_types.len());for (dyn_ty, _data) in f.dfg.dynamic_types.iter() {@@ -2255,16 +2238,21 @@ impl<M: ABIMachineSpec> Callee<M> {}/// Produce an instruction that computes a sized stackslot address.+ ///+ /// The offset must be small enough to address directly (see+ /// `sized_stackslot_area_offset`); larger offsets must be added to the+ /// stackslot's address separately.pub fn sized_stackslot_addr(&self,slot: StackSlot,- offset: u32,+ offset: i32,into_reg: Writable<Reg>,) -> M::I {// Offset from beginning of stackslot area.- let stack_off = self.sized_stackslots[slot] as i64;- let sp_off: i64 = stack_off + (offset as i64);- M::gen_get_stack_addr(StackAMode::Slot(sp_off), into_reg)+ let sp_off = self+ .sized_stackslot_area_offset(slot, i64::from(offset))+ .expect("stackslot offset too large to address directly");+ M::gen_get_stack_addr(StackAMode::Slot(i64::from(sp_off)), into_reg)}/// Produce an instruction that computes a dynamic stackslot address.diff --git a/cranelift/codegen/src/machinst/isle.rs b/cranelift/codegen/src/machinst/isle.rsindex 3e895caa6b..b0aaa1f879 100644--- a/cranelift/codegen/src/machinst/isle.rs+++ b/cranelift/codegen/src/machinst/isle.rs@@ -544,13 +544,28 @@ macro_rules! isle_lower_prelude_methods {stack_slot: StackSlot,offset: Offset32,) -> MInst {- let offset = u32::try_from(i32::from(offset)).unwrap();self.lower_ctx.abi()- .sized_stackslot_addr(stack_slot, offset, dst)+ .sized_stackslot_addr(stack_slot, i32::from(offset), dst).into()}+ fn abi_stackslot_large_offset(+ &mut self,+ stack_slot: StackSlot,+ offset: Offset32,+ ) -> Option<i64> {+ let offset = i64::from(i32::from(offset));+ match self+ .lower_ctx+ .abi()+ .sized_stackslot_area_offset(stack_slot, offset)+ {+ Some(_) => None,+ None => Some(offset),+ }+ }+fn abi_stackslot_offset_into_slot_region(&mut self,stack_slot: StackSlot,diff --git a/cranelift/codegen/src/prelude_lower.isle b/cranelift/codegen/src/prelude_lower.isleindex d12101c847..2b5c868b3a 100644--- a/cranelift/codegen/src/prelude_lower.isle+++ b/cranelift/codegen/src/prelude_lower.isle@@ -1454,10 +1454,19 @@(decl abi_unwrap_ret_area_ptr () Reg)(extern constructor abi_unwrap_ret_area_ptr abi_unwrap_ret_area_ptr)-;; StackSlot addr+;; StackSlot addr. The offset must be small enough to address directly; see+;; `abi_stackslot_large_offset`.(decl abi_stackslot_addr (WritableReg StackSlot Offset32) MInst)(extern constructor abi_stackslot_addr abi_stackslot_addr)+;; Matches an offset into a StackSlot that is too large to address directly+;; with `abi_stackslot_addr`, returning it as an `i64`. Such an offset must+;; instead be added to the StackSlot's address separately. (`stack_addr` is+;; ordinary pointer arithmetic, so its offset may point anywhere, including+;; outside of the StackSlot or the frame.)+(decl pure partial abi_stackslot_large_offset (StackSlot Offset32) i64)+(extern constructor abi_stackslot_large_offset abi_stackslot_large_offset)+;; StackSlot raw offset into slot region, if it is small enough to address;; directly; otherwise, the address must be computed in a register.(decl pure partial abi_stackslot_offset_into_slot_region (StackSlot Offset32 Offset32) i32)diff --git a/cranelift/filetests/filetests/isa/aarch64/stackslot-addr-offsets.clif b/cranelift/filetests/filetests/isa/aarch64/stackslot-addr-offsets.clifnew file mode 100644index 0000000000..be179241a6--- /dev/null+++ b/cranelift/filetests/filetests/isa/aarch64/stackslot-addr-offsets.clif@@ -0,0 +1,162 @@+; `stack_addr` is ordinary pointer arithmetic, so its offset may point+; anywhere, including before or after the stack slot. Offsets that are too+; large to fold into the stack slot's address are added separately.+test compile precise-output+target aarch64++function %negative_offset() -> i64 {+ ss0 = explicit_slot 8+ ss1 = explicit_slot 8+block0:+ v0 = stack_addr.i64 ss1-8+ return v0+}++; VCode:+; stp fp, lr, [sp, #-16]!+; mov fp, sp+; sub sp, sp, #16+; block0:+; mov x0, sp+; add sp, sp, #16+; ldp fp, lr, [sp], #16+; ret+;+; Disassembled:+; block0: ; offset 0x0+; stp x29, x30, [sp, #-0x10]!+; mov x29, sp+; sub sp, sp, #0x10+; block1: ; offset 0xc+; mov x0, sp+; add sp, sp, #0x10+; ldp x29, x30, [sp], #0x10+; ret++function %offset_past_slot() -> i64 {+ ss0 = explicit_slot 8+ ss1 = explicit_slot 8+block0:+ v0 = stack_addr.i64 ss0+16+ return v0+}++; VCode:+; stp fp, lr, [sp, #-16]!+; mov fp, sp+; sub sp, sp, #16+; block0:+; add x0, sp, #16+; add sp, sp, #16+; ldp fp, lr, [sp], #16+; ret+;+; Disassembled:+; block0: ; offset 0x0+; stp x29, x30, [sp, #-0x10]!+; mov x29, sp+; sub sp, sp, #0x10+; block1: ; offset 0xc+; add x0, sp, #0x10+; add sp, sp, #0x10+; ldp x29, x30, [sp], #0x10+; ret++function %large_offset() -> i64 {+ ss0 = explicit_slot 8+ ss1 = explicit_slot 8+block0:+ v0 = stack_addr.i64 ss1+2147483647+ return v0+}++; VCode:+; stp fp, lr, [sp, #-16]!+; mov fp, sp+; sub sp, sp, #16+; block0:+; add x0, sp, #8+; movn w2, #32768, LSL #16+; add x0, x0, x2+; add sp, sp, #16+; ldp fp, lr, [sp], #16+; ret+;+; Disassembled:+; block0: ; offset 0x0+; stp x29, x30, [sp, #-0x10]!+; mov x29, sp+; sub sp, sp, #0x10+; block1: ; offset 0xc+; add x0, sp, #8+; mov w2, #0x7fffffff+; add x0, x0, x2+; add sp, sp, #0x10+; ldp x29, x30, [sp], #0x10+; ret++function %large_negative_offset() -> i64 {+ ss0 = explicit_slot 8+ ss1 = explicit_slot 8+block0:+ v0 = stack_addr.i64 ss1-2147483648+ return v0+}++; VCode:+; stp fp, lr, [sp, #-16]!+; mov fp, sp+; sub sp, sp, #16+; block0:+; add x0, sp, #8+; movn x3, #65535+; movk x3, x3, #32768, LSL #16+; add x0, x0, x3+; add sp, sp, #16+; ldp fp, lr, [sp], #16+; ret+;+; Disassembled:+; block0: ; offset 0x0+; stp x29, x30, [sp, #-0x10]!+; mov x29, sp+; sub sp, sp, #0x10+; block1: ; offset 0xc+; add x0, sp, #8+; mov x3, #-0x10000+; movk x3, #0x8000, lsl #16+; add x0, x0, x3+; add sp, sp, #0x10+; ldp x29, x30, [sp], #0x10+; ret++function %large_offset_load() -> i64 {+ ss0 = explicit_slot 8+ ss1 = explicit_slot 8+block0:+ v0 = stack_addr.i64 ss1+2147483647+ v1 = load.i64 notrap v0-2147483648+ return v1+}++; VCode:+; stp fp, lr, [sp, #-16]!+; mov fp, sp+; sub sp, sp, #16+; block0:+; ldr x0, [sp, #7]+; add sp, sp, #16+; ldp fp, lr, [sp], #16+; ret+;+; Disassembled:+; block0: ; offset 0x0+; stp x29, x30, [sp, #-0x10]!+; mov x29, sp+; sub sp, sp, #0x10+; block1: ; offset 0xc+; ldur x0, [sp, #7]+; add sp, sp, #0x10+; ldp x29, x30, [sp], #0x10+; ret+diff --git a/cranelift/filetests/filetests/isa/aarch64/stackslot-impl-limits.clif b/cranelift/filetests/filetests/isa/aarch64/stackslot-impl-limits.clifindex f7b86f88e5..aba0cf7143 100644--- a/cranelift/filetests/filetests/isa/aarch64/stackslot-impl-limits.clif+++ b/cranelift/filetests/filetests/isa/aarch64/stackslot-impl-limits.clif@@ -1,6 +1,6 @@-; Stack slots and `stack_addr` offsets that can't fit in a frame (limited to-; 1 GiB) fail with an implementation-limit error rather than overflowing the-; frame-size arithmetic or panicking during lowering.+; Stack slots that can't fit in a frame (limited to 1 GiB) fail with an+; implementation-limit error rather than overflowing the frame-size arithmetic+; or panicking during lowering.test compile expect-failtarget aarch64@@ -20,20 +20,4 @@ block0:return v0}-; A `stack_addr` offset beyond the frame size limit.-function %large_offset() -> i64 {- ss0 = explicit_slot 8- ss1 = explicit_slot 8-block0:- v0 = stack_addr.i64 ss1+2147483647- return v0-}-; A negative `stack_addr` offset.-function %negative_offset() -> i64 {- ss0 = explicit_slot 8- ss1 = explicit_slot 8-block0:- v0 = stack_addr.i64 ss1-8- return v0-}diff --git a/cranelift/filetests/filetests/isa/pulley32/stackslot-addr-offsets.clif b/cranelift/filetests/filetests/isa/pulley32/stackslot-addr-offsets.clifnew file mode 100644index 0000000000..c0678ac8ad--- /dev/null+++ b/cranelift/filetests/filetests/isa/pulley32/stackslot-addr-offsets.clif@@ -0,0 +1,130 @@+; `stack_addr` is ordinary pointer arithmetic, so its offset may point+; anywhere, including before or after the stack slot. Offsets that are too+; large to fold into the stack slot's address are added separately.+test compile precise-output+target pulley32++function %negative_offset() -> i32 {+ ss0 = explicit_slot 8+ ss1 = explicit_slot 8+block0:+ v0 = stack_addr.i32 ss1-8+ return v0+}++; VCode:+; push_frame_save 16, {}+; block0:+; x0 = load_addr Slot(0)+; pop_frame_restore 16, {}+; ret+;+; Disassembled:+; push_frame_save 16,+; xmov x0, sp+; pop_frame_restore 16,+; ret++function %offset_past_slot() -> i32 {+ ss0 = explicit_slot 8+ ss1 = explicit_slot 8+block0:+ v0 = stack_addr.i32 ss0+16+ return v0+}++; VCode:+; push_frame_save 16, {}+; block0:+; x0 = load_addr Slot(16)+; pop_frame_restore 16, {}+; ret+;+; Disassembled:+; push_frame_save 16,+; xconst8 x0, 16+; xadd32 x0, sp, x0+; pop_frame_restore 16,+; ret++function %large_offset() -> i32 {+ ss0 = explicit_slot 8+ ss1 = explicit_slot 8+block0:+ v0 = stack_addr.i32 ss1+2147483647+ return v0+}++; VCode:+; push_frame_save 16, {}+; block0:+; x0 = load_addr Slot(8)+; xconst32 x2, 2147483647+; xadd32 x0, x0, x2+; pop_frame_restore 16, {}+; ret+;+; Disassembled:+; push_frame_save 16,+; xconst8 x0, 8+; xadd32 x0, sp, x0+; xconst32 x2, 2147483647+; xadd32 x0, x0, x2+; pop_frame_restore 16,+; ret++function %large_negative_offset() -> i32 {+ ss0 = explicit_slot 8+ ss1 = explicit_slot 8+block0:+ v0 = stack_addr.i32 ss1-2147483648+ return v0+}++; VCode:+; push_frame_save 16, {}+; block0:+; x0 = load_addr Slot(8)+; xconst32 x2, -2147483648+; xadd32 x0, x0, x2+; pop_frame_restore 16, {}+; ret+;+; Disassembled:+; push_frame_save 16,+; xconst8 x0, 8+; xadd32 x0, sp, x0+; xconst32 x2, -2147483648+; xadd32 x0, x0, x2+; pop_frame_restore 16,+; ret++function %large_offset_load() -> i32 {+ ss0 = explicit_slot 8+ ss1 = explicit_slot 8+block0:+ v0 = stack_addr.i32 ss1+2147483647+ v1 = load.i32 notrap v0-2147483648+ return v1+}++; VCode:+; push_frame_save 16, {}+; block0:+; x1 = load_addr Slot(8)+; xconst32 x3, 2147483647+; xadd32 x5, x1, x3+; xload32le_o32 x0, x5, -2147483648+; pop_frame_restore 16, {}+; ret+;+; Disassembled:+; push_frame_save 16,+; xconst8 x1, 8+; xadd32 x1, sp, x1+; xconst32 x3, 2147483647+; xadd32 x5, x1, x3+; xload32le_o32 x0, x5, -2147483648+; pop_frame_restore 16,+; ret+diff --git a/cranelift/filetests/filetests/isa/pulley64/stackslot-addr-offsets.clif b/cranelift/filetests/filetests/isa/pulley64/stackslot-addr-offsets.clifnew file mode 100644index 0000000000..19ae3567a1--- /dev/null+++ b/cranelift/filetests/filetests/isa/pulley64/stackslot-addr-offsets.clif@@ -0,0 +1,130 @@+; `stack_addr` is ordinary pointer arithmetic, so its offset may point+; anywhere, including before or after the stack slot. Offsets that are too+; large to fold into the stack slot's address are added separately.+test compile precise-output+target pulley64++function %negative_offset() -> i64 {+ ss0 = explicit_slot 8+ ss1 = explicit_slot 8+block0:+ v0 = stack_addr.i64 ss1-8+ return v0+}++; VCode:+; push_frame_save 16, {}+; block0:+; x0 = load_addr Slot(0)+; pop_frame_restore 16, {}+; ret+;+; Disassembled:+; push_frame_save 16,+; xmov x0, sp+; pop_frame_restore 16,+; ret++function %offset_past_slot() -> i64 {+ ss0 = explicit_slot 8+ ss1 = explicit_slot 8+block0:+ v0 = stack_addr.i64 ss0+16+ return v0+}++; VCode:+; push_frame_save 16, {}+; block0:+; x0 = load_addr Slot(16)+; pop_frame_restore 16, {}+; ret+;+; Disassembled:+; push_frame_save 16,+; xconst8 x0, 16+; xadd64 x0, sp, x0+; pop_frame_restore 16,+; ret++function %large_offset() -> i64 {+ ss0 = explicit_slot 8+ ss1 = explicit_slot 8+block0:+ v0 = stack_addr.i64 ss1+2147483647+ return v0+}++; VCode:+; push_frame_save 16, {}+; block0:+; x0 = load_addr Slot(8)+; xconst32 x2, 2147483647+; xadd64 x0, x0, x2+; pop_frame_restore 16, {}+; ret+;+; Disassembled:+; push_frame_save 16,+; xconst8 x0, 8+; xadd64 x0, sp, x0+; xconst32 x2, 2147483647+; xadd64 x0, x0, x2+; pop_frame_restore 16,+; ret++function %large_negative_offset() -> i64 {+ ss0 = explicit_slot 8+ ss1 = explicit_slot 8+block0:+ v0 = stack_addr.i64 ss1-2147483648+ return v0+}++; VCode:+; push_frame_save 16, {}+; block0:+; x0 = load_addr Slot(8)+; xconst32 x2, -2147483648+; xadd64 x0, x0, x2+; pop_frame_restore 16, {}+; ret+;+; Disassembled:+; push_frame_save 16,+; xconst8 x0, 8+; xadd64 x0, sp, x0+; xconst32 x2, -2147483648+; xadd64 x0, x0, x2+; pop_frame_restore 16,+; ret++function %large_offset_load() -> i64 {+ ss0 = explicit_slot 8+ ss1 = explicit_slot 8+block0:+ v0 = stack_addr.i64 ss1+2147483647+ v1 = load.i64 notrap v0-2147483648+ return v1+}++; VCode:+; push_frame_save 16, {}+; block0:+; x1 = load_addr Slot(8)+; xconst32 x3, 2147483647+; xadd64 x5, x1, x3+; xload64le_o32 x0, x5, -2147483648+; pop_frame_restore 16, {}+; ret+;+; Disassembled:+; push_frame_save 16,+; xconst8 x1, 8+; xadd64 x1, sp, x1+; xconst32 x3, 2147483647+; xadd64 x5, x1, x3+; xload64le_o32 x0, x5, -2147483648+; pop_frame_restore 16,+; ret+diff --git a/cranelift/filetests/filetests/isa/riscv64/stackslot-addr-offsets.clif b/cranelift/filetests/filetests/isa/riscv64/stackslot-addr-offsets.clifnew file mode 100644index 0000000000..6edbdd242a--- /dev/null+++ b/cranelift/filetests/filetests/isa/riscv64/stackslot-addr-offsets.clif@@ -0,0 +1,203 @@+; `stack_addr` is ordinary pointer arithmetic, so its offset may point+; anywhere, including before or after the stack slot. Offsets that are too+; large to fold into the stack slot's address are added separately.+test compile precise-output+target riscv64++function %negative_offset() -> i64 {+ ss0 = explicit_slot 8+ ss1 = explicit_slot 8+block0:+ v0 = stack_addr.i64 ss1-8+ return v0+}++; VCode:+; addi sp,sp,-16+; sd ra,8(sp)+; sd fp,0(sp)+; mv fp,sp+; addi sp,sp,-16+; block0:+; load_addr a0,0(slot)+; addi sp,sp,16+; ld ra,8(sp)+; ld fp,0(sp)+; addi sp,sp,16+; ret+;+; Disassembled:+; block0: ; offset 0x0+; addi sp, sp, -0x10+; sd ra, 8(sp)+; sd s0, 0(sp)+; mv s0, sp+; addi sp, sp, -0x10+; block1: ; offset 0x14+; mv a0, sp+; addi sp, sp, 0x10+; ld ra, 8(sp)+; ld s0, 0(sp)+; addi sp, sp, 0x10+; ret++function %offset_past_slot() -> i64 {+ ss0 = explicit_slot 8+ ss1 = explicit_slot 8+block0:+ v0 = stack_addr.i64 ss0+16+ return v0+}++; VCode:+; addi sp,sp,-16+; sd ra,8(sp)+; sd fp,0(sp)+; mv fp,sp+; addi sp,sp,-16+; block0:+; load_addr a0,16(slot)+; addi sp,sp,16+; ld ra,8(sp)+; ld fp,0(sp)+; addi sp,sp,16+; ret+;+; Disassembled:+; block0: ; offset 0x0+; addi sp, sp, -0x10+; sd ra, 8(sp)+; sd s0, 0(sp)+; mv s0, sp+; addi sp, sp, -0x10+; block1: ; offset 0x14+; addi a0, sp, 0x10+; addi sp, sp, 0x10+; ld ra, 8(sp)+; ld s0, 0(sp)+; addi sp, sp, 0x10+; ret++function %large_offset() -> i64 {+ ss0 = explicit_slot 8+ ss1 = explicit_slot 8+block0:+ v0 = stack_addr.i64 ss1+2147483647+ return v0+}++; VCode:+; addi sp,sp,-16+; sd ra,8(sp)+; sd fp,0(sp)+; mv fp,sp+; addi sp,sp,-16+; block0:+; load_addr a0,8(slot)+; ld a1,[const(0)]+; add a0,a0,a1+; addi sp,sp,16+; ld ra,8(sp)+; ld fp,0(sp)+; addi sp,sp,16+; ret+;+; Disassembled:+; block0: ; offset 0x0+; addi sp, sp, -0x10+; sd ra, 8(sp)+; sd s0, 0(sp)+; mv s0, sp+; addi sp, sp, -0x10+; block1: ; offset 0x14+; addi a0, sp, 8+; auipc a1, 0+; ld a1, 0x20(a1)+; add a0, a0, a1+; addi sp, sp, 0x10+; ld ra, 8(sp)+; ld s0, 0(sp)+; addi sp, sp, 0x10+; ret+; .byte 0xff, 0xff, 0xff, 0x7f+; .byte 0x00, 0x00, 0x00, 0x00++function %large_negative_offset() -> i64 {+ ss0 = explicit_slot 8+ ss1 = explicit_slot 8+block0:+ v0 = stack_addr.i64 ss1-2147483648+ return v0+}++; VCode:+; addi sp,sp,-16+; sd ra,8(sp)+; sd fp,0(sp)+; mv fp,sp+; addi sp,sp,-16+; block0:+; load_addr a0,8(slot)+; lui a1,-524288+; add a0,a0,a1+; addi sp,sp,16+; ld ra,8(sp)+; ld fp,0(sp)+; addi sp,sp,16+; ret+;+; Disassembled:+; block0: ; offset 0x0+; addi sp, sp, -0x10+; sd ra, 8(sp)+; sd s0, 0(sp)+; mv s0, sp+; addi sp, sp, -0x10+; block1: ; offset 0x14+; addi a0, sp, 8+; lui a1, 0x80000+; add a0, a0, a1+; addi sp, sp, 0x10+; ld ra, 8(sp)+; ld s0, 0(sp)+; addi sp, sp, 0x10+; ret++function %large_offset_load() -> i64 {+ ss0 = explicit_slot 8+ ss1 = explicit_slot 8+block0:+ v0 = stack_addr.i64 ss1+2147483647+ v1 = load.i64 notrap v0-2147483648+ return v1+}++; VCode:+; addi sp,sp,-16+; sd ra,8(sp)+; sd fp,0(sp)+; mv fp,sp+; addi sp,sp,-16+; block0:+; ld a0,7(slot)+; addi sp,sp,16+; ld ra,8(sp)+; ld fp,0(sp)+; addi sp,sp,16+; ret+;+; Disassembled:+; block0: ; offset 0x0+; addi sp, sp, -0x10+; sd ra, 8(sp)+; sd s0, 0(sp)+; mv s0, sp+; addi sp, sp, -0x10+; block1: ; offset 0x14+; ld a0, 7(sp)+; addi sp, sp, 0x10+; ld ra, 8(sp)+; ld s0, 0(sp)+; addi sp, sp, 0x10+; ret+diff --git a/cranelift/filetests/filetests/isa/s390x/stackslot-addr-offsets.clif b/cranelift/filetests/filetests/isa/s390x/stackslot-addr-offsets.clifnew file mode 100644index 0000000000..508305199b--- /dev/null+++ b/cranelift/filetests/filetests/isa/s390x/stackslot-addr-offsets.clif@@ -0,0 +1,137 @@+; `stack_addr` is ordinary pointer arithmetic, so its offset may point+; anywhere, including before or after the stack slot. Offsets that are too+; large to fold into the stack slot's address are added separately.+test compile precise-output+target s390x++function %negative_offset() -> i64 {+ ss0 = explicit_slot 8+ ss1 = explicit_slot 8+block0:+ v0 = stack_addr.i64 ss1-8+ return v0+}++; VCode:+; aghi %r15, -16+; block0:+; la %r2, 0(%r15)+; aghi %r15, 16+; br %r14+;+; Disassembled:+; block0: ; offset 0x0+; aghi %r15, -0x10+; block1: ; offset 0x4+; la %r2, 0(%r15)+; aghi %r15, 0x10+; br %r14++function %offset_past_slot() -> i64 {+ ss0 = explicit_slot 8+ ss1 = explicit_slot 8+block0:+ v0 = stack_addr.i64 ss0+16+ return v0+}++; VCode:+; aghi %r15, -16+; block0:+; la %r2, 16(%r15)+; aghi %r15, 16+; br %r14+;+; Disassembled:+; block0: ; offset 0x0+; aghi %r15, -0x10+; block1: ; offset 0x4+; la %r2, 0x10(%r15)+; aghi %r15, 0x10+; br %r14++function %large_offset() -> i64 {+ ss0 = explicit_slot 8+ ss1 = explicit_slot 8+block0:+ v0 = stack_addr.i64 ss1+2147483647+ return v0+}++; VCode:+; aghi %r15, -16+; block0:+; la %r2, 8(%r15)+; lgfi %r3, 2147483647+; agr %r2, %r3+; aghi %r15, 16+; br %r14+;+; Disassembled:+; block0: ; offset 0x0+; aghi %r15, -0x10+; block1: ; offset 0x4+; la %r2, 8(%r15)+; lgfi %r3, 0x7fffffff+; agr %r2, %r3+; aghi %r15, 0x10+; br %r14++function %large_negative_offset() -> i64 {+ ss0 = explicit_slot 8+ ss1 = explicit_slot 8+block0:+ v0 = stack_addr.i64 ss1-2147483648+ return v0+}++; VCode:+; aghi %r15, -16+; block0:+; la %r2, 8(%r15)+; lgfi %r3, -2147483648+; agr %r2, %r3+; aghi %r15, 16+; br %r14+;+; Disassembled:+; block0: ; offset 0x0+; aghi %r15, -0x10+; block1: ; offset 0x4+; la %r2, 8(%r15)+; lgfi %r3, -0x80000000+; agr %r2, %r3+; aghi %r15, 0x10+; br %r14++function %large_offset_load() -> i64 {+ ss0 = explicit_slot 8+ ss1 = explicit_slot 8+block0:+ v0 = stack_addr.i64 ss1+2147483647+ v1 = load.i64 notrap v0-2147483648+ return v1+}++; VCode:+; aghi %r15, -16+; block0:+; la %r2, 8(%r15)+; lgfi %r3, 2147483647+; agrk %r5, %r2, %r3+; lgfi %r1, -2147483648 ; lg %r2, 0(%r1,%r5)+; aghi %r15, 16+; br %r14+;+; Disassembled:+; block0: ; offset 0x0+; aghi %r15, -0x10+; block1: ; offset 0x4+; la %r2, 8(%r15)+; lgfi %r3, 0x7fffffff+; agrk %r5, %r2, %r3+; lgfi %r1, -0x80000000+; lg %r2, 0(%r1, %r5)+; aghi %r15, 0x10+; br %r14+diff --git a/cranelift/filetests/filetests/isa/x64/stackslot-addr-offsets.clif b/cranelift/filetests/filetests/isa/x64/stackslot-addr-offsets.clifnew file mode 100644index 0000000000..4b6230a0fd--- /dev/null+++ b/cranelift/filetests/filetests/isa/x64/stackslot-addr-offsets.clif@@ -0,0 +1,170 @@+; `stack_addr` is ordinary pointer arithmetic, so its offset may point+; anywhere, including before or after the stack slot. Offsets that are too+; large to fold into the stack slot's address are added separately.+test compile precise-output+target x86_64++function %negative_offset() -> i64 {+ ss0 = explicit_slot 8+ ss1 = explicit_slot 8+block0:+ v0 = stack_addr.i64 ss1-8+ return v0+}++; VCode:+; pushq %rbp+; movq %rsp, %rbp+; subq $0x10, %rsp+; block0:+; leaq <offset:1>+(%rsp), %rax+; addq $0x10, %rsp+; movq %rbp, %rsp+; popq %rbp+; retq+;+; Disassembled:+; block0: ; offset 0x0+; pushq %rbp+; movq %rsp, %rbp+; subq $0x10, %rsp+; block1: ; offset 0x8+; leaq (%rsp), %rax+; addq $0x10, %rsp+; movq %rbp, %rsp+; popq %rbp+; retq++function %offset_past_slot() -> i64 {+ ss0 = explicit_slot 8+ ss1 = explicit_slot 8+block0:+ v0 = stack_addr.i64 ss0+16+ return v0+}++; VCode:+; pushq %rbp+; movq %rsp, %rbp+; subq $0x10, %rsp+; block0:+; leaq <offset:1>+0x10(%rsp), %rax+; addq $0x10, %rsp+; movq %rbp, %rsp+; popq %rbp+; retq+;+; Disassembled:+; block0: ; offset 0x0+; pushq %rbp+; movq %rsp, %rbp+; subq $0x10, %rsp+; block1: ; offset 0x8+; leaq 0x10(%rsp), %rax+; addq $0x10, %rsp+; movq %rbp, %rsp+; popq %rbp+; retq++function %large_offset() -> i64 {+ ss0 = explicit_slot 8+ ss1 = explicit_slot 8+block0:+ v0 = stack_addr.i64 ss1+2147483647+ return v0+}++; VCode:+; pushq %rbp+; movq %rsp, %rbp+; subq $0x10, %rsp+; block0:+; leaq <offset:1>+8(%rsp), %rax+; movl $0x7fffffff, %edx+; addq %rdx, %rax+; addq $0x10, %rsp+; movq %rbp, %rsp+; popq %rbp+; retq+;+; Disassembled:+; block0: ; offset 0x0+; pushq %rbp+; movq %rsp, %rbp+; subq $0x10, %rsp+; block1: ; offset 0x8+; leaq 8(%rsp), %rax+; movl $0x7fffffff, %edx+; addq %rdx, %rax+; addq $0x10, %rsp+; movq %rbp, %rsp+; popq %rbp+; retq++function %large_negative_offset() -> i64 {+ ss0 = explicit_slot 8+ ss1 = explicit_slot 8+block0:+ v0 = stack_addr.i64 ss1-2147483648+ return v0+}++; VCode:+; pushq %rbp+; movq %rsp, %rbp+; subq $0x10, %rsp+; block0:+; leaq <offset:1>+8(%rsp), %rax+; movq $0xffffffff80000000, %rdx+; addq %rdx, %rax+; addq $0x10, %rsp+; movq %rbp, %rsp+; popq %rbp+; retq+;+; Disassembled:+; block0: ; offset 0x0+; pushq %rbp+; movq %rsp, %rbp+; subq $0x10, %rsp+; block1: ; offset 0x8+; leaq 8(%rsp), %rax+; movq $18446744071562067968, %rdx+; addq %rdx, %rax+; addq $0x10, %rsp+; movq %rbp, %rsp+; popq %rbp+; retq++function %large_offset_load() -> i64 {+ ss0 = explicit_slot 8+ ss1 = explicit_slot 8+block0:+ v0 = stack_addr.i64 ss1+2147483647+ v1 = load.i64 notrap v0-2147483648+ return v1+}++; VCode:+; pushq %rbp+; movq %rsp, %rbp+; subq $0x10, %rsp+; block0:+; movq <offset:1>+7(%rsp), %rax+; addq $0x10, %rsp+; movq %rbp, %rsp+; popq %rbp+; retq+;+; Disassembled:+; block0: ; offset 0x0+; pushq %rbp+; movq %rsp, %rbp+; subq $0x10, %rsp+; block1: ; offset 0x8+; movq 7(%rsp), %rax+; addq $0x10, %rsp+; movq %rbp, %rsp+; popq %rbp+; retq+diff --git a/cranelift/filetests/filetests/isa/x64/stackslot-impl-limits.clif b/cranelift/filetests/filetests/isa/x64/stackslot-impl-limits.clifindex 9c0057d866..01f67955e4 100644--- a/cranelift/filetests/filetests/isa/x64/stackslot-impl-limits.clif+++ b/cranelift/filetests/filetests/isa/x64/stackslot-impl-limits.clif@@ -1,6 +1,6 @@-; Stack slots and `stack_addr` offsets that can't fit in a frame (limited to-; 1 GiB) fail with an implementation-limit error rather than overflowing the-; frame-size arithmetic or panicking during lowering.+; Stack slots that can't fit in a frame (limited to 1 GiB) fail with an+; implementation-limit error rather than overflowing the frame-size arithmetic+; or panicking during lowering.test compile expect-failtarget x86_64@@ -20,20 +20,4 @@ block0:return v0}-; A `stack_addr` offset beyond the frame size limit.-function %large_offset() -> i64 {- ss0 = explicit_slot 8- ss1 = explicit_slot 8-block0:- v0 = stack_addr.i64 ss1+2147483647- return v0-}-; A negative `stack_addr` offset.-function %negative_offset() -> i64 {- ss0 = explicit_slot 8- ss1 = explicit_slot 8-block0:- v0 = stack_addr.i64 ss1-8- return v0-}diff --git a/cranelift/filetests/filetests/runtests/stack-addr-offsets.clif b/cranelift/filetests/filetests/runtests/stack-addr-offsets.clifnew file mode 100644index 0000000000..384d3ec6fa--- /dev/null+++ b/cranelift/filetests/filetests/runtests/stack-addr-offsets.clif@@ -0,0 +1,62 @@+test run+; `stack_addr` is ordinary pointer arithmetic: its offset may point outside of+; the stack slot, and the result may be offset back into the slot. (Not run+; under the interpreter, which tracks addresses per-slot.)+set enable_probestack=false+target x86_64+target s390x+target aarch64+target riscv64+target riscv64 has_c has_zcb+target pulley64+target pulley64be++function %negative_offset_into_prev_slot(i64) -> i64 {+ ss0 = explicit_slot 8+ ss1 = explicit_slot 8+block0(v0: i64):+ v1 = stack_addr.i64 ss1-8+ store notrap v0, v1+ v2 = stack_addr.i64 ss0+ v3 = load.i64 notrap v2+ return v3+}+; run: %negative_offset_into_prev_slot(0x0123456789abcdef) == 0x0123456789abcdef++function %offset_past_slot(i64) -> i64 {+ ss0 = explicit_slot 8+ ss1 = explicit_slot 8+block0(v0: i64):+ v1 = stack_addr.i64 ss1+ store notrap v0, v1+ v2 = stack_addr.i64 ss0+8+ v3 = load.i64 notrap v2+ return v3+}+; run: %offset_past_slot(0x0123456789abcdef) == 0x0123456789abcdef++function %large_offset_round_trip(i64) -> i64 {+ ss0 = explicit_slot 8+block0(v0: i64):+ v1 = stack_addr.i64 ss0+ store notrap v0, v1+ v2 = stack_addr.i64 ss0+2147483647+ v3 = iconst.i64 -2147483647+ v4 = iadd v2, v3+ v5 = load.i64 notrap v4+ return v5+}+; run: %large_offset_round_trip(0x0123456789abcdef) == 0x0123456789abcdef++function %large_negative_offset_round_trip(i64) -> i64 {+ ss0 = explicit_slot 8+block0(v0: i64):+ v1 = stack_addr.i64 ss0+ store notrap v0, v1+ v2 = stack_addr.i64 ss0-2147483648+ v3 = iconst.i64 2147483648+ v4 = iadd v2, v3+ v5 = load.i64 notrap v4+ return v5+}+; run: %large_negative_offset_round_trip(0x0123456789abcdef) == 0x0123456789abcdef