Skip to content

Commit

Permalink
Merge pull request bytecodealliance#12 from dhil/wasmfx-merge
Browse files Browse the repository at this point in the history
Merge with upstream
  • Loading branch information
dhil committed Sep 26, 2023
2 parents 83bb453 + e6e0240 commit df79811
Show file tree
Hide file tree
Showing 65 changed files with 3,714 additions and 2,026 deletions.
7 changes: 7 additions & 0 deletions RELEASES.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,15 @@ Unreleased.

### Added

* Added the `wasmtime::FrameInfo::module` method, which returns the
`wasmtime::Module` associated with the stack frame.

### Changed

* The `wasmtime::FrameInfo::module_name` has been removed, however you can now
get identical results by chaining `wasmtime::FrameInfo::module` and
`wasmtime::Module::name`: `my_frame.module().name()`.

--------------------------------------------------------------------------------

## 13.0.0
Expand Down
163 changes: 100 additions & 63 deletions cranelift/codegen/src/isa/riscv64/inst.isle
Original file line number Diff line number Diff line change
Expand Up @@ -741,13 +741,45 @@
(CAddiw)
(CAddi16sp)
(CSlli)
(CLi)
(CLui)
(CLwsp)
(CLdsp)
(CFldsp)
))

;; Opcodes for the CIW compressed instruction format
(type CiwOp (enum
(CAddi4spn)
))

;; Opcodes for the CB compressed instruction format
(type CbOp (enum
(CSrli)
(CSrai)
(CAndi)
))

;; Opcodes for the CSS compressed instruction format
(type CssOp (enum
(CSwsp)
(CSdsp)
(CFsdsp)
))

;; Opcodes for the CS compressed instruction format
(type CsOp (enum
(CSw)
(CSd)
(CFsd)
))

;; Opcodes for the CL compressed instruction format
(type ClOp (enum
(CLw)
(CLd)
(CFld)
))

(type CsrRegOP (enum
;; Atomic Read/Write CSR
Expand Down Expand Up @@ -1664,10 +1696,10 @@

;; Otherwise we fall back to loading the immediate from the constant pool.
(rule 0 (imm (ty_int ty) c)
(emit_load
(gen_load
(gen_const_amode (emit_u64_le_const c))
(LoadOP.Ld)
(mem_flags_trusted)
(gen_const_amode (emit_u64_le_const c))))
(mem_flags_trusted)))

;; Imm12 Rules

Expand Down Expand Up @@ -2402,57 +2434,88 @@
(gen_select_reg (IntCC.UnsignedGreaterThanOrEqual) shamt_128 const64 low high)
)))

(decl gen_amode (Reg Offset32 Type) AMode)
(extern constructor gen_amode gen_amode)
;; Generates a AMode that points to a register plus an offset.
(decl gen_reg_offset_amode (Reg i64 Type) AMode)
(extern constructor gen_reg_offset_amode gen_reg_offset_amode)

;; Generates a AMode that an offset from the stack pointer.
(decl gen_sp_offset_amode (i64 Type) AMode)
(extern constructor gen_sp_offset_amode gen_sp_offset_amode)

;; Generates a AMode that an offset from the frame pointer.
(decl gen_fp_offset_amode (i64 Type) AMode)
(extern constructor gen_fp_offset_amode gen_fp_offset_amode)

;; Generates an AMode that points to a stack slot + offset.
(decl gen_stack_slot_amode (StackSlot i64 Type) AMode)
(extern constructor gen_stack_slot_amode gen_stack_slot_amode)

;; Generates a AMode that points to a constant in the constant pool.
(decl gen_const_amode (VCodeConstant) AMode)
(extern constructor gen_const_amode gen_const_amode)



;; Tries to match a Value + Offset into an AMode
(decl amode (Value i32 Type) AMode)
(rule 0 (amode addr offset ty) (amode_inner addr offset ty))

;; If we are adding a constant offset with an iadd we can instead make that
;; offset part of the amode offset.
;;
;; We can't recurse into `amode` again since that could cause stack overflows.
;; See: https://github.com/bytecodealliance/wasmtime/pull/6968
(rule 1 (amode (iadd addr (iconst (simm32 y))) offset ty)
(if-let new_offset (s32_add_fallible y offset))
(amode_inner addr new_offset ty))
(rule 2 (amode (iadd (iconst (simm32 x)) addr) offset ty)
(if-let new_offset (s32_add_fallible x offset))
(amode_inner addr new_offset ty))


;; These are the normal rules for generating an AMode.
(decl amode_inner (Value i32 Type) AMode)

;; In the simplest case we just lower into a Reg+Offset
(rule 0 (amode_inner r @ (value_type (ty_addr64 _)) offset ty)
(gen_reg_offset_amode r offset ty))

;; If the value is a `get_frame_pointer`, we can just use the offset from that.
(rule 1 (amode_inner (get_frame_pointer) offset ty)
(gen_fp_offset_amode offset ty))

;; If the value is a `get_stack_pointer`, we can just use the offset from that.
(rule 1 (amode_inner (get_stack_pointer) offset ty)
(gen_sp_offset_amode offset ty))

;; Similarly if the value is a `stack_addr` we can also turn that into an sp offset.
(rule 1 (amode_inner (stack_addr ss ss_offset) amode_offset ty)
(if-let combined_offset (s32_add_fallible ss_offset amode_offset))
(gen_stack_slot_amode ss combined_offset ty))




;; Returns a canonical type for a LoadOP. We only return I64 or F64.
(decl load_op_reg_type (LoadOP) Type)
(rule 1 (load_op_reg_type (LoadOP.Fld)) $F64)
(rule 1 (load_op_reg_type (LoadOP.Flw)) $F64)
(rule 0 (load_op_reg_type _) $I64)

(decl emit_load (LoadOP MemFlags AMode) Reg)
(rule (emit_load op flags from)
(let ((dst WritableReg (temp_writable_reg (load_op_reg_type op)))
(_ Unit (emit (MInst.Load dst op flags from))))
dst))

;; helper function to load from memory.
(decl gen_load (Reg Offset32 LoadOP MemFlags Type) Reg)
(rule (gen_load p offset op flags ty)
(emit_load op flags (gen_amode p offset $I64)))

(decl gen_load_128 (Reg Offset32 MemFlags) ValueRegs)
(rule (gen_load_128 p offset flags)
(let ((low Reg (gen_load p offset (LoadOP.Ld) flags $I64))
(high Reg (gen_load p (offset32_add offset 8) (LoadOP.Ld) flags $I64)))
(value_regs low high)))
(decl gen_load (AMode LoadOP MemFlags) Reg)
(rule (gen_load amode op flags)
(let ((dst WritableReg (temp_writable_reg (load_op_reg_type op)))
(_ Unit (emit (MInst.Load dst op flags amode))))
dst))

(decl default_memflags () MemFlags)
(extern constructor default_memflags default_memflags)
;; helper function to store to memory.
(decl gen_store (AMode StoreOP MemFlags Reg) InstOutput)
(rule (gen_store amode op flags src)
(side_effect (SideEffectNoResult.Inst (MInst.Store amode op flags src))))

(decl offset32_add (Offset32 i64) Offset32)
(extern constructor offset32_add offset32_add)

;; helper function to store to memory.
(decl gen_store (Reg Offset32 StoreOP MemFlags Reg) InstOutput)
(rule
(gen_store base offset op flags src)
(side_effect (SideEffectNoResult.Inst (MInst.Store (gen_amode base offset $I64) op flags src)))
)

(decl gen_store_128 (Reg Offset32 MemFlags ValueRegs) InstOutput)
(rule
(gen_store_128 p offset flags src)
(side_effect
(SideEffectNoResult.Inst2
(MInst.Store (gen_amode p offset $I64) (StoreOP.Sd) flags (value_regs_get src 0))
(MInst.Store (gen_amode p (offset32_add offset 8) $I64) (StoreOP.Sd) flags (value_regs_get src 1)))))

(decl valid_atomic_transaction (Type) Type)
(extern extractor valid_atomic_transaction valid_atomic_transaction)
Expand Down Expand Up @@ -2611,32 +2674,6 @@
(decl store_op (Type) StoreOP)
(extern constructor store_op store_op)

;; bool is "is_signed"
(decl int_load_op (bool u8) LoadOP)
(rule
(int_load_op $false 8)
(LoadOP.Lbu))

(rule
(int_load_op $true 8)
(LoadOP.Lb))

(rule
(int_load_op $false 16)
(LoadOP.Lhu))
(rule
(int_load_op $true 16)
(LoadOP.Lh))
(rule
(int_load_op $false 32)
(LoadOP.Lwu))
(rule
(int_load_op $true 32)
(LoadOP.Lw))

(rule
(int_load_op _ 64)
(LoadOP.Ld))

;;;; load extern name
(decl load_ext_name (ExternalName i64) Reg)
Expand Down
111 changes: 106 additions & 5 deletions cranelift/codegen/src/isa/riscv64/inst/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::ir::condcodes::CondCode;
use crate::isa::riscv64::inst::{reg_name, reg_to_gpr_num};

use crate::isa::riscv64::lower::isle::generated_code::{
COpcodeSpace, CaOp, CiOp, CiwOp, CjOp, CrOp,
COpcodeSpace, CaOp, CbOp, CiOp, CiwOp, CjOp, ClOp, CrOp, CsOp, CssOp,
};
use crate::machinst::isle::WritableReg;

Expand Down Expand Up @@ -1317,6 +1317,15 @@ impl LoadOP {
}
}

pub(crate) fn size(&self) -> i64 {
match self {
Self::Lb | Self::Lbu => 1,
Self::Lh | Self::Lhu => 2,
Self::Lw | Self::Lwu | Self::Flw => 4,
Self::Ld | Self::Fld => 8,
}
}

pub(crate) fn op_code(self) -> u32 {
match self {
Self::Lb | Self::Lh | Self::Lw | Self::Lbu | Self::Lhu | Self::Lwu | Self::Ld => {
Expand Down Expand Up @@ -1363,6 +1372,16 @@ impl StoreOP {
_ => unreachable!(),
}
}

pub(crate) fn size(&self) -> i64 {
match self {
Self::Sb => 1,
Self::Sh => 2,
Self::Sw | Self::Fsw => 4,
Self::Sd | Self::Fsd => 8,
}
}

pub(crate) fn op_code(self) -> u32 {
match self {
Self::Sb | Self::Sh | Self::Sw | Self::Sd => 0b0100011,
Expand Down Expand Up @@ -1983,16 +2002,19 @@ impl CiOp {
// https://github.com/michaeljclark/riscv-meta/blob/master/opcodes
match self {
CiOp::CAddi | CiOp::CSlli => 0b000,
CiOp::CAddiw => 0b001,
CiOp::CAddi16sp => 0b011,
CiOp::CAddiw | CiOp::CFldsp => 0b001,
CiOp::CLi | CiOp::CLwsp => 0b010,
CiOp::CAddi16sp | CiOp::CLui | CiOp::CLdsp => 0b011,
}
}

pub fn op(&self) -> COpcodeSpace {
// https://five-embeddev.com/riscv-isa-manual/latest/rvc-opcode-map.html#rvcopcodemap
match self {
CiOp::CAddi | CiOp::CAddiw | CiOp::CAddi16sp => COpcodeSpace::C1,
CiOp::CSlli => COpcodeSpace::C2,
CiOp::CAddi | CiOp::CAddiw | CiOp::CAddi16sp | CiOp::CLi | CiOp::CLui => {
COpcodeSpace::C1
}
CiOp::CSlli | CiOp::CLwsp | CiOp::CLdsp | CiOp::CFldsp => COpcodeSpace::C2,
}
}
}
Expand All @@ -2012,3 +2034,82 @@ impl CiwOp {
}
}
}

impl CbOp {
pub fn funct3(&self) -> u32 {
// https://github.com/michaeljclark/riscv-meta/blob/master/opcodes
match self {
CbOp::CSrli | CbOp::CSrai | CbOp::CAndi => 0b100,
}
}

pub fn funct2(&self) -> u32 {
// https://github.com/michaeljclark/riscv-meta/blob/master/opcodes
match self {
CbOp::CSrli => 0b00,
CbOp::CSrai => 0b01,
CbOp::CAndi => 0b10,
}
}

pub fn op(&self) -> COpcodeSpace {
// https://five-embeddev.com/riscv-isa-manual/latest/rvc-opcode-map.html#rvcopcodemap
match self {
CbOp::CSrli | CbOp::CSrai | CbOp::CAndi => COpcodeSpace::C1,
}
}
}

impl CssOp {
pub fn funct3(&self) -> u32 {
// https://github.com/michaeljclark/riscv-meta/blob/master/opcodes
match self {
CssOp::CFsdsp => 0b101,
CssOp::CSwsp => 0b110,
CssOp::CSdsp => 0b111,
}
}

pub fn op(&self) -> COpcodeSpace {
// https://five-embeddev.com/riscv-isa-manual/latest/rvc-opcode-map.html#rvcopcodemap
match self {
CssOp::CSwsp | CssOp::CSdsp | CssOp::CFsdsp => COpcodeSpace::C2,
}
}
}

impl CsOp {
pub fn funct3(&self) -> u32 {
// https://github.com/michaeljclark/riscv-meta/blob/master/opcodes
match self {
CsOp::CFsd => 0b101,
CsOp::CSw => 0b110,
CsOp::CSd => 0b111,
}
}

pub fn op(&self) -> COpcodeSpace {
// https://five-embeddev.com/riscv-isa-manual/latest/rvc-opcode-map.html#rvcopcodemap
match self {
CsOp::CSw | CsOp::CSd | CsOp::CFsd => COpcodeSpace::C0,
}
}
}

impl ClOp {
pub fn funct3(&self) -> u32 {
// https://github.com/michaeljclark/riscv-meta/blob/master/opcodes
match self {
ClOp::CFld => 0b001,
ClOp::CLw => 0b010,
ClOp::CLd => 0b011,
}
}

pub fn op(&self) -> COpcodeSpace {
// https://five-embeddev.com/riscv-isa-manual/latest/rvc-opcode-map.html#rvcopcodemap
match self {
ClOp::CLw | ClOp::CLd | ClOp::CFld => COpcodeSpace::C0,
}
}
}
Loading

0 comments on commit df79811

Please sign in to comment.