Skip to content

Commit

Permalink
Winch: implement rmw and, xor and or for x64 (bytecodealliance#…
Browse files Browse the repository at this point in the history
…10023)

* implement atomic rmw and, or, xor

implement rmw or

implement atomic rmw xor

fix operand sizes

implement and, or, xor

update rmw or tests

update rmw xor tests

fmt

use ad-hoc conversion for AtomicRmwSeqOp

fix test

fix rebae quirks

* cleanup tests
  • Loading branch information
MarinPostma authored Jan 19, 2025
1 parent d58f1b5 commit 2eb6513
Show file tree
Hide file tree
Showing 48 changed files with 1,010 additions and 5 deletions.
1 change: 1 addition & 0 deletions cranelift/codegen/src/isa/x64/inst/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use args::*;
// Instructions (top level): definition

// `Inst` is defined inside ISLE as `MInst`. We publicly re-export it here.
pub use super::lower::isle::generated_code::AtomicRmwSeqOp;
pub use super::lower::isle::generated_code::MInst as Inst;

/// Out-of-line data for return-calls, to keep the size of `Inst` down.
Expand Down
2 changes: 1 addition & 1 deletion cranelift/codegen/src/isa/x64/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! X86_64-bit Instruction Set Architecture.
pub use self::inst::{args, EmitInfo, EmitState, Inst};
pub use self::inst::{args, AtomicRmwSeqOp, EmitInfo, EmitState, Inst};

use super::{OwnedTargetIsa, TargetIsa};
use crate::dominator_tree::DominatorTree;
Expand Down
39 changes: 39 additions & 0 deletions tests/disas/winch/x64/atomic/rmw/and/i32_atomic_rmw16_andu.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
;;! target = "x86_64"
;;! test = "winch"

(module
(memory 1 1 shared)
(func (export "_start") (result i32)
(i32.atomic.rmw16.and_u (i32.const 0) (i32.const 42))))
;; wasm[0]::function[0]:
;; pushq %rbp
;; movq %rsp, %rbp
;; movq 8(%rdi), %r11
;; movq 0x10(%r11), %r11
;; addq $0x10, %r11
;; cmpq %rsp, %r11
;; ja 0x72
;; 1c: movq %rdi, %r14
;; subq $0x10, %rsp
;; movq %rdi, 8(%rsp)
;; movq %rsi, (%rsp)
;; movl $0x2a, %eax
;; movl $0, %ecx
;; andw $1, %cx
;; cmpw $0, %cx
;; jne 0x74
;; 44: movl $0, %ecx
;; movq 0x58(%r14), %r11
;; movq (%r11), %rdx
;; addq %rcx, %rdx
;; movzwq (%rdx), %rax
;; movq %rax, %r11
;; andq %rax, %r11
;; lock cmpxchgw %r11w, (%rdx)
;; jne 0x57
;; 69: movzwl %ax, %eax
;; addq $0x10, %rsp
;; popq %rbp
;; retq
;; 72: ud2
;; 74: ud2
34 changes: 34 additions & 0 deletions tests/disas/winch/x64/atomic/rmw/and/i32_atomic_rmw8_andu.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
;;! target = "x86_64"
;;! test = "winch"

(module
(memory 1 1 shared)
(func (export "_start") (result i32)
(i32.atomic.rmw8.and_u (i32.const 0) (i32.const 42))))
;; wasm[0]::function[0]:
;; pushq %rbp
;; movq %rsp, %rbp
;; movq 8(%rdi), %r11
;; movq 0x10(%r11), %r11
;; addq $0x10, %r11
;; cmpq %rsp, %r11
;; ja 0x5e
;; 1c: movq %rdi, %r14
;; subq $0x10, %rsp
;; movq %rdi, 8(%rsp)
;; movq %rsi, (%rsp)
;; movl $0x2a, %eax
;; movl $0, %ecx
;; movq 0x58(%r14), %r11
;; movq (%r11), %rdx
;; addq %rcx, %rdx
;; movzbq (%rdx), %rax
;; movq %rax, %r11
;; andq %rax, %r11
;; lock cmpxchgb %r11b, (%rdx)
;; jne 0x44
;; 55: movzbl %al, %eax
;; addq $0x10, %rsp
;; popq %rbp
;; retq
;; 5e: ud2
38 changes: 38 additions & 0 deletions tests/disas/winch/x64/atomic/rmw/and/i32_atomic_rmw_and.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
;;! target = "x86_64"
;;! test = "winch"

(module
(memory 1 1 shared)
(func (export "_start") (result i32)
(i32.atomic.rmw.and (i32.const 0) (i32.const 42))))
;; wasm[0]::function[0]:
;; pushq %rbp
;; movq %rsp, %rbp
;; movq 8(%rdi), %r11
;; movq 0x10(%r11), %r11
;; addq $0x10, %r11
;; cmpq %rsp, %r11
;; ja 0x6a
;; 1c: movq %rdi, %r14
;; subq $0x10, %rsp
;; movq %rdi, 8(%rsp)
;; movq %rsi, (%rsp)
;; movl $0x2a, %eax
;; movl $0, %ecx
;; andl $3, %ecx
;; cmpl $0, %ecx
;; jne 0x6c
;; 42: movl $0, %ecx
;; movq 0x58(%r14), %r11
;; movq (%r11), %rdx
;; addq %rcx, %rdx
;; movl (%rdx), %eax
;; movq %rax, %r11
;; andq %rax, %r11
;; lock cmpxchgl %r11d, (%rdx)
;; jne 0x53
;; 64: addq $0x10, %rsp
;; popq %rbp
;; retq
;; 6a: ud2
;; 6c: ud2
39 changes: 39 additions & 0 deletions tests/disas/winch/x64/atomic/rmw/and/i64_atomic_rmw16_andu.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
;;! target = "x86_64"
;;! test = "winch"

(module
(memory 1 1 shared)
(func (export "_start") (result i64)
(i64.atomic.rmw16.and_u (i32.const 0) (i64.const 42))))
;; wasm[0]::function[0]:
;; pushq %rbp
;; movq %rsp, %rbp
;; movq 8(%rdi), %r11
;; movq 0x10(%r11), %r11
;; addq $0x10, %r11
;; cmpq %rsp, %r11
;; ja 0x75
;; 1c: movq %rdi, %r14
;; subq $0x10, %rsp
;; movq %rdi, 8(%rsp)
;; movq %rsi, (%rsp)
;; movq $0x2a, %rax
;; movl $0, %ecx
;; andw $1, %cx
;; cmpw $0, %cx
;; jne 0x77
;; 46: movl $0, %ecx
;; movq 0x58(%r14), %r11
;; movq (%r11), %rdx
;; addq %rcx, %rdx
;; movzwq (%rdx), %rax
;; movq %rax, %r11
;; andq %rax, %r11
;; lock cmpxchgw %r11w, (%rdx)
;; jne 0x59
;; 6b: movzwq %ax, %rax
;; addq $0x10, %rsp
;; popq %rbp
;; retq
;; 75: ud2
;; 77: ud2
38 changes: 38 additions & 0 deletions tests/disas/winch/x64/atomic/rmw/and/i64_atomic_rmw32_andu.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
;;! target = "x86_64"
;;! test = "winch"

(module
(memory 1 1 shared)
(func (export "_start") (result i64)
(i64.atomic.rmw32.and_u (i32.const 0) (i64.const 42))))
;; wasm[0]::function[0]:
;; pushq %rbp
;; movq %rsp, %rbp
;; movq 8(%rdi), %r11
;; movq 0x10(%r11), %r11
;; addq $0x10, %r11
;; cmpq %rsp, %r11
;; ja 0x6c
;; 1c: movq %rdi, %r14
;; subq $0x10, %rsp
;; movq %rdi, 8(%rsp)
;; movq %rsi, (%rsp)
;; movq $0x2a, %rax
;; movl $0, %ecx
;; andl $3, %ecx
;; cmpl $0, %ecx
;; jne 0x6e
;; 44: movl $0, %ecx
;; movq 0x58(%r14), %r11
;; movq (%r11), %rdx
;; addq %rcx, %rdx
;; movl (%rdx), %eax
;; movq %rax, %r11
;; andq %rax, %r11
;; lock cmpxchgl %r11d, (%rdx)
;; jne 0x55
;; 66: addq $0x10, %rsp
;; popq %rbp
;; retq
;; 6c: ud2
;; 6e: ud2
34 changes: 34 additions & 0 deletions tests/disas/winch/x64/atomic/rmw/and/i64_atomic_rmw8_andu.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
;;! target = "x86_64"
;;! test = "winch"

(module
(memory 1 1 shared)
(func (export "_start") (result i64)
(i64.atomic.rmw8.and_u (i32.const 0) (i64.const 42))))
;; wasm[0]::function[0]:
;; pushq %rbp
;; movq %rsp, %rbp
;; movq 8(%rdi), %r11
;; movq 0x10(%r11), %r11
;; addq $0x10, %r11
;; cmpq %rsp, %r11
;; ja 0x61
;; 1c: movq %rdi, %r14
;; subq $0x10, %rsp
;; movq %rdi, 8(%rsp)
;; movq %rsi, (%rsp)
;; movq $0x2a, %rax
;; movl $0, %ecx
;; movq 0x58(%r14), %r11
;; movq (%r11), %rdx
;; addq %rcx, %rdx
;; movzbq (%rdx), %rax
;; movq %rax, %r11
;; andq %rax, %r11
;; lock cmpxchgb %r11b, (%rdx)
;; jne 0x46
;; 57: movzbq %al, %rax
;; addq $0x10, %rsp
;; popq %rbp
;; retq
;; 61: ud2
38 changes: 38 additions & 0 deletions tests/disas/winch/x64/atomic/rmw/and/i64_atomic_rmw_and.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
;;! target = "x86_64"
;;! test = "winch"

(module
(memory 1 1 shared)
(func (export "_start") (result i64)
(i64.atomic.rmw.and (i32.const 0) (i64.const 42))))
;; wasm[0]::function[0]:
;; pushq %rbp
;; movq %rsp, %rbp
;; movq 8(%rdi), %r11
;; movq 0x10(%r11), %r11
;; addq $0x10, %r11
;; cmpq %rsp, %r11
;; ja 0x6f
;; 1c: movq %rdi, %r14
;; subq $0x10, %rsp
;; movq %rdi, 8(%rsp)
;; movq %rsi, (%rsp)
;; movq $0x2a, %rax
;; movl $0, %ecx
;; andq $7, %rcx
;; cmpq $0, %rcx
;; jne 0x71
;; 46: movl $0, %ecx
;; movq 0x58(%r14), %r11
;; movq (%r11), %rdx
;; addq %rcx, %rdx
;; movq (%rdx), %rax
;; movq %rax, %r11
;; andq %rax, %r11
;; lock cmpxchgq %r11, (%rdx)
;; jne 0x58
;; 69: addq $0x10, %rsp
;; popq %rbp
;; retq
;; 6f: ud2
;; 71: ud2
39 changes: 39 additions & 0 deletions tests/disas/winch/x64/atomic/rmw/or/i32_atomic_rmw16_oru.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
;;! target = "x86_64"
;;! test = "winch"

(module
(memory 1 1 shared)
(func (export "_start") (result i32)
(i32.atomic.rmw16.or_u (i32.const 0) (i32.const 42))))
;; wasm[0]::function[0]:
;; pushq %rbp
;; movq %rsp, %rbp
;; movq 8(%rdi), %r11
;; movq 0x10(%r11), %r11
;; addq $0x10, %r11
;; cmpq %rsp, %r11
;; ja 0x72
;; 1c: movq %rdi, %r14
;; subq $0x10, %rsp
;; movq %rdi, 8(%rsp)
;; movq %rsi, (%rsp)
;; movl $0x2a, %eax
;; movl $0, %ecx
;; andw $1, %cx
;; cmpw $0, %cx
;; jne 0x74
;; 44: movl $0, %ecx
;; movq 0x58(%r14), %r11
;; movq (%r11), %rdx
;; addq %rcx, %rdx
;; movzwq (%rdx), %rax
;; movq %rax, %r11
;; orq %rax, %r11
;; lock cmpxchgw %r11w, (%rdx)
;; jne 0x57
;; 69: movzwl %ax, %eax
;; addq $0x10, %rsp
;; popq %rbp
;; retq
;; 72: ud2
;; 74: ud2
34 changes: 34 additions & 0 deletions tests/disas/winch/x64/atomic/rmw/or/i32_atomic_rmw8_oru.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
;;! target = "x86_64"
;;! test = "winch"

(module
(memory 1 1 shared)
(func (export "_start") (result i32)
(i32.atomic.rmw8.or_u (i32.const 0) (i32.const 42))))
;; wasm[0]::function[0]:
;; pushq %rbp
;; movq %rsp, %rbp
;; movq 8(%rdi), %r11
;; movq 0x10(%r11), %r11
;; addq $0x10, %r11
;; cmpq %rsp, %r11
;; ja 0x5e
;; 1c: movq %rdi, %r14
;; subq $0x10, %rsp
;; movq %rdi, 8(%rsp)
;; movq %rsi, (%rsp)
;; movl $0x2a, %eax
;; movl $0, %ecx
;; movq 0x58(%r14), %r11
;; movq (%r11), %rdx
;; addq %rcx, %rdx
;; movzbq (%rdx), %rax
;; movq %rax, %r11
;; orq %rax, %r11
;; lock cmpxchgb %r11b, (%rdx)
;; jne 0x44
;; 55: movzbl %al, %eax
;; addq $0x10, %rsp
;; popq %rbp
;; retq
;; 5e: ud2
38 changes: 38 additions & 0 deletions tests/disas/winch/x64/atomic/rmw/or/i32_atomic_rmw_or.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
;;! target = "x86_64"
;;! test = "winch"

(module
(memory 1 1 shared)
(func (export "_start") (result i32)
(i32.atomic.rmw.or (i32.const 0) (i32.const 42))))
;; wasm[0]::function[0]:
;; pushq %rbp
;; movq %rsp, %rbp
;; movq 8(%rdi), %r11
;; movq 0x10(%r11), %r11
;; addq $0x10, %r11
;; cmpq %rsp, %r11
;; ja 0x6a
;; 1c: movq %rdi, %r14
;; subq $0x10, %rsp
;; movq %rdi, 8(%rsp)
;; movq %rsi, (%rsp)
;; movl $0x2a, %eax
;; movl $0, %ecx
;; andl $3, %ecx
;; cmpl $0, %ecx
;; jne 0x6c
;; 42: movl $0, %ecx
;; movq 0x58(%r14), %r11
;; movq (%r11), %rdx
;; addq %rcx, %rdx
;; movl (%rdx), %eax
;; movq %rax, %r11
;; orq %rax, %r11
;; lock cmpxchgl %r11d, (%rdx)
;; jne 0x53
;; 64: addq $0x10, %rsp
;; popq %rbp
;; retq
;; 6a: ud2
;; 6c: ud2
Loading

0 comments on commit 2eb6513

Please sign in to comment.