Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

Cranelift: ISLE: veri: add vector canary types to catch missing guards (#14789)

Alexa VanHattum24831ba419ae
diff --git a/cranelift/codegen/src/isa/aarch64/inst.isle b/cranelift/codegen/src/isa/aarch64/inst.isle
index 33b3923066..22d8765ce1 100644
--- a/cranelift/codegen/src/isa/aarch64/inst.isle
+++ b/cranelift/codegen/src/isa/aarch64/inst.isle
@@ -2427,11 +2427,12 @@
(spec (integral_ty ty)
(provide (= result ty))
- (match (or
- (= (:bits result) 8)
- (= (:bits result) 16)
- (= (:bits result) 32)
- (= (:bits result) 64))))
+ (match (= (:lanes result) 1)
+ (or
+ (= (:bits result) 8)
+ (= (:bits result) 16)
+ (= (:bits result) 32)
+ (= (:bits result) 64))))
(decl integral_ty (Type) Type)
(extern extractor integral_ty integral_ty)
diff --git a/cranelift/codegen/src/prelude.isle b/cranelift/codegen/src/prelude.isle
index 9cd9509312..5c7b744b7b 100644
--- a/cranelift/codegen/src/prelude.isle
+++ b/cranelift/codegen/src/prelude.isle
@@ -13,7 +13,7 @@
(decl pure unit () Unit)
(extern constructor unit unit)
;; `cranelift-entity`-based identifiers.
-(model Type (type (struct (bits Int))))
+(model Type (type (struct (bits Int) (lanes Int))))
(type Type (primitive Type))
(model Value (type (bv (8 16 32 64))))
@@ -33,7 +33,7 @@
(type ValueSlice (primitive ValueSlice))
;; Extract the type of a `Value`.
-(spec (value_type ty) (provide (= (:bits ty) (widthof result))))
+(spec (value_type ty) (provide (= (:bits ty) (widthof result)) (= (:lanes ty) (lanesof result))))
(decl value_type (Type) Value)
(extern extractor infallible value_type value_type)
@@ -327,7 +327,9 @@
(decl pure lane_type (Type) Type)
(extern constructor lane_type lane_type)
;; For now: single-lane, non-vector types assumed here
-(spec (lane_type ty) (provide (= result ty)))
+(spec (lane_type ty)
+ (provide (= (:lanes result) 1)
+ (= (:bits ty) (* (:bits result) (:lanes ty)))))
;; Get a type with the same element type, but half the number of lanes.
(decl pure partial ty_half_lanes (Type) Type)
@@ -472,7 +474,8 @@
(= (:bits result) 8)
(= (:bits result) 16)
(= (:bits result) 32)
- (= (:bits result) 64)))
+ (= (:bits result) 64))
+ (= (:lanes result) 1))
(provide (= result ty)))
(decl pure partial ty_int_ref_scalar_64_extract (Type) Type)
(extern extractor ty_int_ref_scalar_64_extract ty_int_ref_scalar_64_extract)
@@ -505,7 +508,9 @@
(extern extractor ty_int_ref_16_to_64 ty_int_ref_16_to_64)
;; An extractor that only matches integers.
-(spec (ty_int arg) (provide (= arg result)))
+(spec (ty_int arg)
+ (match (= (:lanes result) 1))
+ (provide (= arg result)))
(decl ty_int (Type) Type)
(extern extractor ty_int ty_int)
@@ -515,7 +520,8 @@
;; An extractor that only matches scalar floating-point types.
(spec (ty_scalar_float ty)
- (match (or (= (:bits result) 32) (= (:bits result) 64)))
+ (match (or (= (:bits result) 32) (= (:bits result) 64))
+ (= (:lanes result) 1))
(provide (= result ty)))
(decl ty_scalar_float (Type) Type)
(extern extractor ty_scalar_float ty_scalar_float)
@@ -759,15 +765,15 @@
(model i64 (type (bv 64)))
(model isize (type Int))
-(model I8 (const (struct (bits 8))))
-(model I16 (const (struct (bits 16))))
-(model I32 (const (struct (bits 32))))
-(model I64 (const (struct (bits 64))))
-(model I128 (const (struct (bits 128))))
-(model F16 (const (struct (bits 16))))
-(model F32 (const (struct (bits 32))))
-(model F64 (const (struct (bits 64))))
-(model I8X16 (const (struct (bits 128))))
+(model I8 (const (struct (bits 8) (lanes 1))))
+(model I16 (const (struct (bits 16) (lanes 1))))
+(model I32 (const (struct (bits 32) (lanes 1))))
+(model I64 (const (struct (bits 64) (lanes 1))))
+(model I128 (const (struct (bits 128) (lanes 1))))
+(model F16 (const (struct (bits 16) (lanes 1))))
+(model F32 (const (struct (bits 32) (lanes 1))))
+(model F64 (const (struct (bits 64) (lanes 1))))
+(model I8X16 (const (struct (bits 128) (lanes 16))))
(spec (i64_neg x) (provide (= result (bvneg x))))
(spec (u64_sub x y) (provide (= result (bvsub x y))))
diff --git a/cranelift/codegen/src/prelude_lower.isle b/cranelift/codegen/src/prelude_lower.isle
index 2b5c868b3a..7ebedc6ff9 100644
--- a/cranelift/codegen/src/prelude_lower.isle
+++ b/cranelift/codegen/src/prelude_lower.isle
@@ -113,7 +113,7 @@
(extern constructor output_vec output_vec)
;; Get a temporary register for writing.
-(spec (temp_writable_reg ty) (provide (= (:bits ty) (widthof result))))
+(spec (temp_writable_reg ty) (provide (= (:bits ty) (widthof result)) (= (:lanes ty) (lanesof result))))
(decl temp_writable_reg (Type) WritableReg)
(extern constructor temp_writable_reg temp_writable_reg)
@@ -340,7 +340,7 @@
(spec (has_type ty arg)
(provide
(= result arg)
- (= (:bits ty) (widthof arg))))
+ (= (:bits ty) (widthof arg)) (= (:lanes ty) (lanesof arg))))
(decl has_type (Type Inst) Inst)
(extractor (has_type ty inst)
(and (result_type ty)
diff --git a/cranelift/codegen/src/spec/inst_specs.isle b/cranelift/codegen/src/spec/inst_specs.isle
index 66d1ffac2c..4867cbf3c9 100644
--- a/cranelift/codegen/src/spec/inst_specs.isle
+++ b/cranelift/codegen/src/spec/inst_specs.isle
@@ -76,11 +76,25 @@
;;;; Common Term Forms ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;; Instructions whose spec maps over lanes with `lanemap` are instantiated at
+;; the canary vector shapes `i32x2` `(bv 32 x 2)` and `i8x4` `(bv 8 x 4)`, which
+;; fit `fits_in_64`/`fits_in_32` guards and so catch rules missing a scalar
+;; guard.
+
(form
bv_unary_8_to_64
((args (named Type) (bv 8)) (ret (bv 8)))
((args (named Type) (bv 16)) (ret (bv 16)))
((args (named Type) (bv 32)) (ret (bv 32)))
+ ((args (named Type) (bv 64)) (ret (bv 64)))
+ ((args (named Type) (bv 32 x 2)) (ret (bv 32 x 2)))
+ ((args (named Type) (bv 8 x 4)) (ret (bv 8 x 4))))
+
+(form
+ bv_unary_scalar_8_to_64
+ ((args (named Type) (bv 8)) (ret (bv 8)))
+ ((args (named Type) (bv 16)) (ret (bv 16)))
+ ((args (named Type) (bv 32)) (ret (bv 32)))
((args (named Type) (bv 64)) (ret (bv 64))))
(form
@@ -88,6 +102,15 @@
((args (named Type) (bv 8) (bv 8)) (ret (bv 8)))
((args (named Type) (bv 16) (bv 16)) (ret (bv 16)))
((args (named Type) (bv 32) (bv 32)) (ret (bv 32)))
+ ((args (named Type) (bv 64) (bv 64)) (ret (bv 64)))
+ ((args (named Type) (bv 32 x 2) (bv 32 x 2)) (ret (bv 32 x 2)))
+ ((args (named Type) (bv 8 x 4) (bv 8 x 4)) (ret (bv 8 x 4))))
+
+(form
+ bv_binary_scalar_8_to_64
+ ((args (named Type) (bv 8) (bv 8)) (ret (bv 8)))
+ ((args (named Type) (bv 16) (bv 16)) (ret (bv 16)))
+ ((args (named Type) (bv 32) (bv 32)) (ret (bv 32)))
((args (named Type) (bv 64) (bv 64)) (ret (bv 64))))
;; Binary operations whose first result has the operands' width and whose
@@ -123,7 +146,9 @@
((args (named Type) (bv 32) (bv 32)) (ret (bv 32)))
((args (named Type) (bv 32) (bv 64)) (ret (bv 32)))
((args (named Type) (bv 64) (bv 8)) (ret (bv 64)))
- ((args (named Type) (bv 64) (bv 64)) (ret (bv 64))))
+ ((args (named Type) (bv 64) (bv 64)) (ret (bv 64)))
+ ((args (named Type) (bv 32 x 2) (bv 64)) (ret (bv 32 x 2)))
+ ((args (named Type) (bv 8 x 4) (bv 64)) (ret (bv 8 x 4))))
(form
bv_shift_8_to_64_extra
@@ -139,45 +164,56 @@
((args (named Type) (bv 8) (bv 8) (bv 8)) (ret (bv 8)))
((args (named Type) (bv 16) (bv 16) (bv 16)) (ret (bv 16)))
((args (named Type) (bv 32) (bv 32) (bv 32)) (ret (bv 32)))
- ((args (named Type) (bv 64) (bv 64) (bv 64)) (ret (bv 64))))
+ ((args (named Type) (bv 64) (bv 64) (bv 64)) (ret (bv 64)))
+ ((args (named Type) (bv 32 x 2) (bv 32 x 2) (bv 32 x 2)) (ret (bv 32 x 2)))
+ ((args (named Type) (bv 8 x 4) (bv 8 x 4) (bv 8 x 4)) (ret (bv 8 x 4))))
;;;; CLIF Instruction Specifications ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Integer Instructions
(spec (iadd ty x y)
- (provide (= result (bvadd x y))
- (= (:bits ty) (widthof result))))
+ (provide (= result (lanemap ((x x) (y y)) (bvadd x y)))
+ (= (:bits ty) (widthof result))
+ (= (:lanes ty) (lanesof result))))
(instantiate iadd
((args (named Type) (bv 8) (bv 8)) (ret (bv 8)))
((args (named Type) (bv 16) (bv 16)) (ret (bv 16)))
((args (named Type) (bv 32) (bv 32)) (ret (bv 32)))
((args (named Type) (bv 64) (bv 64)) (ret (bv 64)))
- ((args (named Type) (bv 128) (bv 128)) (ret (bv 128))))
+ ((args (named Type) (bv 128) (bv 128)) (ret (bv 128)))
+ ((args (named Type) (bv 32 x 2) (bv 32 x 2)) (ret (bv 32 x 2)))
+ ((args (named Type) (bv 8 x 4) (bv 8 x 4)) (ret (bv 8 x 4))))
(spec (isub ty x y)
- (provide (= result (bvsub x y))
- (= (:bits ty) (widthof result))))
+ (provide (= result (lanemap ((x x) (y y)) (bvsub x y)))
+ (= (:bits ty) (widthof result))
+ (= (:lanes ty) (lanesof result))))
(instantiate isub bv_binary_8_to_64)
(spec (ineg ty x)
- (provide (= result (bvneg x))
- (= (:bits ty) (widthof result))))
+ (provide (= result (lanemap ((x x)) (bvneg x)))
+ (= (:bits ty) (widthof result))
+ (= (:lanes ty) (lanesof result))))
(instantiate ineg bv_unary_8_to_64)
(spec (iabs ty x)
- (provide (= result (if (bvsge x (zero_ext (widthof x) #b0)) x (bvneg x)))
- (= (:bits ty) (widthof result))))
+ (provide (= result (lanemap ((x x)) (if (bvsge x (zero_ext (widthof x) #b0)) x (bvneg x))))
+ (= (:bits ty) (widthof result))
+ (= (:lanes ty) (lanesof result))))
(instantiate iabs bv_unary_8_to_64)
(spec (imul ty x y)
- (provide (= result (bvmul x y))
- (= (:bits ty) (widthof result))))
+ (provide (= result (lanemap ((x x) (y y)) (bvmul x y)))
+ (= (:bits ty) (widthof result))
+ (= (:lanes ty) (lanesof result))))
(instantiate imul bv_binary_8_to_64)
(spec (umul_overflow ty x y)
+ (require (= (:lanes ty) 1))
(provide
(= (:bits ty) (widthof x))
+ (= (:lanes ty) (lanesof x))
(= (widthof x) (widthof y))
(let
((double (concat x x))
@@ -191,8 +227,10 @@
(instantiate umul_overflow bv_binary_8_to_64_with_flag)
(spec (smul_overflow ty x y)
+ (require (= (:lanes ty) 1))
(provide
(= (:bits ty) (widthof x))
+ (= (:lanes ty) (lanesof x))
(= (widthof x) (widthof y))
(let
((double (concat x x))
@@ -208,6 +246,8 @@
(spec (smulhi ty x y)
(provide
(= (:bits ty) (widthof result))
+ (= (:lanes ty) (lanesof result))
+ (all_lanes ((result result) (x x) (y y))
(let
(
(double (concat x x))
@@ -215,12 +255,14 @@
(xwide (sign_ext double_width x))
(ywide (sign_ext double_width y)))
(with (low)
- (= (concat result low) (bvmul xwide ywide))))))
+ (= (concat result low) (bvmul xwide ywide)))))))
(instantiate smulhi bv_binary_8_to_64)
(spec (umulhi ty x y)
(provide
(= (:bits ty) (widthof result))
+ (= (:lanes ty) (lanesof result))
+ (all_lanes ((result result) (x x) (y y))
(let
(
(double (concat x x))
@@ -228,18 +270,21 @@
(xwide (zero_ext double_width x))
(ywide (zero_ext double_width y)))
(with (low)
- (= (concat result low) (bvmul xwide ywide))))))
+ (= (concat result low) (bvmul xwide ywide)))))))
(instantiate umulhi bv_binary_8_to_64)
(spec (udiv ty x y)
+ (require (= (:lanes ty) 1))
(modifies clif_trap)
(provide
(= result (bvudiv x y))
(= clif_trap (bv_is_zero! y))
- (= (:bits ty) (widthof result))))
-(instantiate udiv bv_binary_8_to_64)
+ (= (:bits ty) (widthof result))
+ (= (:lanes ty) (lanesof result))))
+(instantiate udiv bv_binary_scalar_8_to_64)
(spec (sdiv ty x y)
+ (require (= (:lanes ty) 1))
(modifies clif_trap)
(provide
; Constrain the result width in all cases
@@ -261,11 +306,13 @@
(not clif_trap)
(= result (bvsdiv x y)))))))
-(instantiate sdiv bv_binary_8_to_64)
+(instantiate sdiv bv_binary_scalar_8_to_64)
(spec (urem ty x y)
+ (require (= (:lanes ty) 1))
(modifies clif_trap)
(provide
(= (:bits ty) (widthof result))
+ (= (:lanes ty) (lanesof result))
; If i2 is 0, then the result is undefined.
(if (bv_is_zero! y)
clif_trap
@@ -273,12 +320,14 @@
(and
(not clif_trap)
(= result (bvurem x y))))))
-(instantiate urem bv_binary_8_to_64)
+(instantiate urem bv_binary_scalar_8_to_64)
(spec (srem ty x y)
+ (require (= (:lanes ty) 1))
(modifies clif_trap)
(provide
(= (:bits ty) (widthof result))
+ (= (:lanes ty) (lanesof result))
; Let j1 be the signed interpretation of i1.
; Let j2 be the signed interpretation of i2.
; If i2 is 0, then the result is undefined.
@@ -288,14 +337,16 @@
(and
(not clif_trap)
(= result (bvsrem x y))))))
-(instantiate srem bv_binary_8_to_64)
+(instantiate srem bv_binary_scalar_8_to_64)
;; "Unsigned addition of x and y, trapping if the result overflows."
;; "Accepts 32 or 64-bit integers, and does not support vector types."
(spec (uadd_overflow_trap ty x y trap_code)
+ (require (= (:lanes ty) 1))
(modifies clif_trap)
(provide
(= (:bits ty) (widthof result))
+ (= (:lanes ty) (lanesof result))
(let
(
(N (widthof x))
@@ -325,8 +376,10 @@
(provide (= clif_trap (bv_is_zero! val))))
(spec (iconst ty arg)
+ (require (= (:lanes ty) 1))
(provide (= arg (zero_ext 64 result))
- (= (:bits ty) (widthof result))))
+ (= (:bits ty) (widthof result))
+ (= (:lanes ty) (lanesof result))))
(instantiate iconst
((args (named Type) (bv 64)) (ret (bv 8)))
((args (named Type) (bv 64)) (ret (bv 16)))
@@ -348,82 +401,99 @@
(spec (ishl ty x y)
(provide
(= (:bits ty) (widthof result))
- (= result (bvshl x (shift_amount! x y)))))
+ (= (:lanes ty) (lanesof result))
+ (= result (lanemap ((x x)) (bvshl x (shift_amount! x y))))))
(instantiate ishl bv_shift_8_to_64)
(instantiate ishl bv_shift_8_to_64_extra (tag slow))
(spec (ushr ty x y)
(provide
(= (:bits ty) (widthof result))
- (= result (bvlshr x (shift_amount! x y)))))
+ (= (:lanes ty) (lanesof result))
+ (= result (lanemap ((x x)) (bvlshr x (shift_amount! x y))))))
(instantiate ushr bv_shift_8_to_64)
(instantiate ushr bv_shift_8_to_64_extra (tag slow))
(spec (sshr ty x y)
(provide
(= (:bits ty) (widthof result))
- (= result (bvashr x (shift_amount! x y)))))
+ (= (:lanes ty) (lanesof result))
+ (= result (lanemap ((x x)) (bvashr x (shift_amount! x y))))))
(instantiate sshr bv_shift_8_to_64)
(instantiate sshr bv_shift_8_to_64_extra (tag slow))
(spec (band ty x y)
- (provide (= result (bvand x y))
- (= (:bits ty) (widthof result))))
+ (provide (= result (lanemap ((x x) (y y)) (bvand x y)))
+ (= (:bits ty) (widthof result))
+ (= (:lanes ty) (lanesof result))))
(instantiate band bv_binary_8_to_64)
(spec (bxor ty x y)
- (provide (= result (bvxor x y))
- (= (:bits ty) (widthof result))))
+ (provide (= result (lanemap ((x x) (y y)) (bvxor x y)))
+ (= (:bits ty) (widthof result))
+ (= (:lanes ty) (lanesof result))))
(instantiate bxor bv_binary_8_to_64)
(spec (bor ty x y)
- (provide (= result (bvor x y))
- (= (:bits ty) (widthof result))))
+ (provide (= result (lanemap ((x x) (y y)) (bvor x y)))
+ (= (:bits ty) (widthof result))
+ (= (:lanes ty) (lanesof result))))
(instantiate bor bv_binary_8_to_64)
(spec (bnot ty x)
- (provide (= result (bvnot x))
- (= (:bits ty) (widthof result))))
+ (provide (= result (lanemap ((x x)) (bvnot x)))
+ (= (:bits ty) (widthof result))
+ (= (:lanes ty) (lanesof result))))
(instantiate bnot bv_unary_8_to_64)
;; Rotate amounts are taken modulo the bit width, so `shift_amount!` applies
;; here too: masking with `bits - 1` is the same as reducing modulo `bits` for
;; the power-of-two widths modelled here.
(spec (rotl ty x y)
- (provide (= result (rotl x (shift_amount! x y)))
- (= (:bits ty) (widthof result))))
+ (provide (= result (lanemap ((x x)) (rotl x (shift_amount! x y))))
+ (= (:bits ty) (widthof result))
+ (= (:lanes ty) (lanesof result))))
(instantiate rotl bv_shift_8_to_64)
(instantiate rotl bv_shift_8_to_64_extra (tag slow))
(spec (rotr ty x y)
- (provide (= result (rotr x (shift_amount! x y)))
- (= (:bits ty) (widthof result))))
+ (provide (= result (lanemap ((x x)) (rotr x (shift_amount! x y))))
+ (= (:bits ty) (widthof result))
+ (= (:lanes ty) (lanesof result))))
(instantiate rotr bv_shift_8_to_64)
(instantiate rotr bv_shift_8_to_64_extra (tag slow))
(spec (bitselect ty c x y)
- (provide (= result (bvor (bvand c x) (bvand (bvnot c) y)))
- (= (:bits ty) (widthof result))))
+ (provide (= result (lanemap ((c c) (x x) (y y)) (bvor (bvand c x) (bvand (bvnot c) y))))
+ (= (:bits ty) (widthof result))
+ (= (:lanes ty) (lanesof result))))
(instantiate bitselect bv_ternary_8_to_64)
(spec (cls ty x)
+ (require (= (:lanes ty) 1))
(provide (= result (cls x))
- (= (:bits ty) (widthof result))))
-(instantiate cls bv_unary_8_to_64)
+ (= (:bits ty) (widthof result))
+ (= (:lanes ty) (lanesof result))))
+(instantiate cls bv_unary_scalar_8_to_64)
(spec (clz ty x)
+ (require (= (:lanes ty) 1))
(provide (= result (clz x))
- (= (:bits ty) (widthof result))))
-(instantiate clz bv_unary_8_to_64)
+ (= (:bits ty) (widthof result))
+ (= (:lanes ty) (lanesof result))))
+(instantiate clz bv_unary_scalar_8_to_64)
(spec (ctz ty x)
+ (require (= (:lanes ty) 1))
(provide (= result (clz (rev x)))
- (= (:bits ty) (widthof result))))
-(instantiate ctz bv_unary_8_to_64)
+ (= (:bits ty) (widthof result))
+ (= (:lanes ty) (lanesof result))))
+(instantiate ctz bv_unary_scalar_8_to_64)
(spec (popcnt ty x)
- (provide (= result (popcnt x))
- (= (:bits ty) (widthof result))))
+ (provide (= result (lanemap ((x x)) (popcnt x)))
+ (= (:bits ty) (widthof result))
+ (= (:lanes ty) (lanesof result))))
(instantiate popcnt bv_unary_8_to_64)
;; Reverse the byte order of `x`. There is no byte-swap primitive, so we build
@@ -446,8 +516,10 @@
(concat (extract 55 48 x)
(extract 63 56 x))))))
(spec (bswap ty x)
+ (require (= (:lanes ty) 1))
(provide
(= (:bits ty) (widthof result))
+ (= (:lanes ty) (lanesof result))
(let ((w (conv_to 64 x)))
(= result
(conv_to (widthof result)
@@ -461,9 +533,11 @@
((args (named Type) (bv 64)) (ret (bv 64))))
(spec (ireduce ty x)
- (require (< (:bits ty) (widthof x)))
+ (require (< (:bits ty) (widthof x))
+ (= (:lanes ty) 1))
(provide (= result (conv_to (widthof result) x))
- (= (:bits ty) (widthof result))))
+ (= (:bits ty) (widthof result))
+ (= (:lanes ty) (lanesof result))))
(instantiate ireduce
((args (named Type) (bv 16)) (ret (bv 8)))
((args (named Type) (bv 32)) (ret (bv 8)))
@@ -483,35 +557,43 @@
;; (8->8, 16->16, 32->32, 64->64) are not accepted in clif.
(spec (uextend ty x)
- (require (< (widthof x) (:bits ty)))
+ (require (< (widthof x) (:bits ty))
+ (= (:lanes ty) 1))
(provide (= result (zero_ext (widthof result) x))
- (= (:bits ty) (widthof result))))
+ (= (:bits ty) (widthof result))
+ (= (:lanes ty) (lanesof result))))
(instantiate uextend extend)
(spec (sextend ty x)
- (require (< (widthof x) (:bits ty)))
+ (require (< (widthof x) (:bits ty))
+ (= (:lanes ty) 1))
(provide (= result (sign_ext (widthof result) x))
- (= (:bits ty) (widthof result))))
+ (= (:bits ty) (widthof result))
+ (= (:lanes ty) (lanesof result))))
(instantiate sextend extend)
(spec (smin ty x y)
- (provide (= result (if (bvsle x y) x y))
- (= (:bits ty) (widthof result))))
+ (provide (= result (lanemap ((x x) (y y)) (if (bvsle x y) x y)))
+ (= (:bits ty) (widthof result))
+ (= (:lanes ty) (lanesof result))))
(instantiate smin bv_binary_8_to_64)
(spec (umin ty x y)
- (provide (= result (if (bvule x y) x y))
- (= (:bits ty) (widthof result))))
+ (provide (= result (lanemap ((x x) (y y)) (if (bvule x y) x y)))
+ (= (:bits ty) (widthof result))
+ (= (:lanes ty) (lanesof result))))
(instantiate umin bv_binary_8_to_64)
(spec (smax ty x y)
- (provide (= result (if (bvsge x y) x y))
- (= (:bits ty) (widthof result))))
+ (provide (= result (lanemap ((x x) (y y)) (if (bvsge x y) x y)))
+ (= (:bits ty) (widthof result))
+ (= (:lanes ty) (lanesof result))))
(instantiate smax bv_binary_8_to_64)
(spec (umax ty x y)
- (provide (= result (if (bvuge x y) x y))
- (= (:bits ty) (widthof result))))
+ (provide (= result (lanemap ((x x) (y y)) (if (bvuge x y) x y)))
+ (= (:bits ty) (widthof result))
+ (= (:lanes ty) (lanesof result))))
(instantiate umax bv_binary_8_to_64)
(spec (icmp ty cc x y)
@@ -556,6 +638,7 @@
(modifies loaded_value)
(provide
(= (:bits ty) (widthof result))
+ (= (:lanes ty) (lanesof result))
; Activate the CLIF load effect.
(clif_load_activate! clif_load (widthof result) p offset)
@@ -579,10 +662,12 @@
; Unsigned 8-bit load
(spec (uload8 ty flags p offset)
+ (require (= (:lanes ty) 1))
(modifies clif_load)
(modifies loaded_value)
(provide (uloadN! clif_load 8 p offset loaded_value result)
- (= (:bits ty) (widthof result))))
+ (= (:bits ty) (widthof result))
+ (= (:lanes ty) (lanesof result))))
(instantiate uload8
((args (named Type) (named MemFlagsData) (named Value) (named Offset32)) (ret (bv 16)))
((args (named Type) (named MemFlagsData) (named Value) (named Offset32)) (ret (bv 32)))
@@ -590,20 +675,24 @@
; Unsigned 16-bit load
(spec (uload16 ty flags p offset)
+ (require (= (:lanes ty) 1))
(modifies clif_load)
(modifies loaded_value)
(provide (uloadN! clif_load 16 p offset loaded_value result)
- (= (:bits ty) (widthof result))))
+ (= (:bits ty) (widthof result))
+ (= (:lanes ty) (lanesof result))))
(instantiate uload16
((args (named Type) (named MemFlagsData) (named Value) (named Offset32)) (ret (bv 32)))
((args (named Type) (named MemFlagsData) (named Value) (named Offset32)) (ret (bv 64))))
; Unsigned 32-bit load
(spec (uload32 ty flags p offset)
+ (require (= (:lanes ty) 1))
(modifies clif_load)
(modifies loaded_value)
(provide (uloadN! clif_load 32 p offset loaded_value result)
- (= (:bits ty) (widthof result))))
+ (= (:bits ty) (widthof result))
+ (= (:lanes ty) (lanesof result))))
(instantiate uload32
((args (named Type) (named MemFlagsData) (named Value) (named Offset32)) (ret (bv 64))))
@@ -618,10 +707,12 @@
; Signed 8-bit load
(spec (sload8 ty flags p offset)
+ (require (= (:lanes ty) 1))
(modifies clif_load)
(modifies loaded_value)
(provide (sloadN! clif_load 8 p offset loaded_value result)
- (= (:bits ty) (widthof result))))
+ (= (:bits ty) (widthof result))
+ (= (:lanes ty) (lanesof result))))
(instantiate sload8
((args (named Type) (named MemFlagsData) (named Value) (named Offset32)) (ret (bv 16)))
((args (named Type) (named MemFlagsData) (named Value) (named Offset32)) (ret (bv 32)))
@@ -629,20 +720,24 @@
; Signed 16-bit load
(spec (sload16 ty flags p offset)
+ (require (= (:lanes ty) 1))
(modifies clif_load)
(modifies loaded_value)
(provide (sloadN! clif_load 16 p offset loaded_value result)
- (= (:bits ty) (widthof result))))
+ (= (:bits ty) (widthof result))
+ (= (:lanes ty) (lanesof result))))
(instantiate sload16
((args (named Type) (named MemFlagsData) (named Value) (named Offset32)) (ret (bv 32)))
((args (named Type) (named MemFlagsData) (named Value) (named Offset32)) (ret (bv 64))))
; Signed 32-bit load
(spec (sload32 ty flags p offset)
+ (require (= (:lanes ty) 1))
(modifies clif_load)
(modifies loaded_value)
(provide (sloadN! clif_load 32 p offset loaded_value result)
- (= (:bits ty) (widthof result))))
+ (= (:bits ty) (widthof result))
+ (= (:lanes ty) (lanesof result))))
(instantiate sload32
((args (named Type) (named MemFlagsData) (named Value) (named Offset32)) (ret (bv 64))))
@@ -767,14 +862,18 @@
; f32const: single-precision floating-point constant.
(spec (f32const ty x)
+ (require (= (:lanes ty) 1))
(provide (= result x)
- (= (:bits ty) (widthof result))))
+ (= (:bits ty) (widthof result))
+ (= (:lanes ty) (lanesof result))))
(instantiate f32const ((args (named Type) (bv 32)) (ret (bv 32))))
; f64const: double-precision floating-point constant.
(spec (f64const ty x)
+ (require (= (:lanes ty) 1))
(provide (= result x)
- (= (:bits ty) (widthof result))))
+ (= (:bits ty) (widthof result))
+ (= (:lanes ty) (lanesof result))))
(instantiate f64const ((args (named Type) (bv 64)) (ret (bv 64))))
; fcmp: floating-point compare.
@@ -808,10 +907,11 @@
(spec (fadd ty x y)
(modifies relax_nan produces_nan)
(provide
- (= produces_nan (fp.isNaN result))
+ (= produces_nan (any_lanes ((result result)) (fp.isNaN result)))
(=> produces_nan relax_nan)
(= (:bits ty) (widthof result))
- (= result
+ (= (:lanes ty) (lanesof result))
+ (= result (lanemap ((x x) (y y))
; If either z1 or z2 is a NaN, then return an element of nans{z1,z2}.
(if (or (fp.isNaN x) (fp.isNaN y))
(nans2! x y)
@@ -841,10 +941,11 @@
(if (and (= (fp_magnitude! x) (fp_magnitude! y)) (fp_opposite_sign! x y))
(fp.+zero (widthof x))
; Else return the result of adding z1 and z2, rounded to the nearest representable value.
- (fp.add x y))))))))))))))
+ (fp.add x y)))))))))))))))
(instantiate fadd
((args (named Type) (bv 32) (bv 32)) (ret (bv 32)))
- ((args (named Type) (bv 64) (bv 64)) (ret (bv 64))))
+ ((args (named Type) (bv 64) (bv 64)) (ret (bv 64)))
+ ((args (named Type) (bv 32 x 2) (bv 32 x 2)) (ret (bv 32 x 2))))
; fsub: floating-point subtraction.
;
@@ -852,10 +953,11 @@
(spec (fsub ty x y)
(modifies relax_nan produces_nan)
(provide
- (= produces_nan (fp.isNaN result))
+ (= produces_nan (any_lanes ((result result)) (fp.isNaN result)))
(=> produces_nan relax_nan)
(= (:bits ty) (widthof result))
- (= result
+ (= (:lanes ty) (lanesof result))
+ (= result (lanemap ((x x) (y y))
; If either z1 or z2 is a NaN, then return an element of nans{z1,z2}.
(if (or (fp.isNaN x) (fp.isNaN y))
(nans2! x y)
@@ -887,10 +989,11 @@
(if (and (= (fp_magnitude! x) (fp_magnitude! y)) (fp_equal_sign! x y))
(fp.+zero (widthof x))
; Else return the result of subtracting z2 from z1, rounded to the nearest representable value.
- (fp.sub x y))))))))))))))
+ (fp.sub x y)))))))))))))))
(instantiate fsub
((args (named Type) (bv 32) (bv 32)) (ret (bv 32)))
- ((args (named Type) (bv 64) (bv 64)) (ret (bv 64))))
+ ((args (named Type) (bv 64) (bv 64)) (ret (bv 64)))
+ ((args (named Type) (bv 32 x 2) (bv 32 x 2)) (ret (bv 32 x 2))))
; fmul: floating-point multiplication.
;
@@ -898,10 +1001,11 @@
(spec (fmul ty x y)
(modifies relax_nan produces_nan)
(provide
- (= produces_nan (fp.isNaN result))
+ (= produces_nan (any_lanes ((result result)) (fp.isNaN result)))
(=> produces_nan relax_nan)
(= (:bits ty) (widthof result))
- (= result
+ (= (:lanes ty) (lanesof result))
+ (= result (lanemap ((x x) (y y))
; If either z1 or z2 is a NaN, then return an element of nans{z1,z2}.
(if (or (fp.isNaN x) (fp.isNaN y))
(nans2! x y)
@@ -933,10 +1037,11 @@
(if (and (fp.isZero x) (fp.isZero y) (fp_opposite_sign! x y))
(fp.-zero (widthof x))
; Else return the result of multiplying z1 and z2, rounded to the nearest representable value.
- (fp.mul x y)))))))))))))))
+ (fp.mul x y))))))))))))))))
(instantiate fmul
((args (named Type) (bv 32) (bv 32)) (ret (bv 32)))
- ((args (named Type) (bv 64) (bv 64)) (ret (bv 64))))
+ ((args (named Type) (bv 64) (bv 64)) (ret (bv 64)))
+ ((args (named Type) (bv 32 x 2) (bv 32 x 2)) (ret (bv 32 x 2))))
; fdiv: floating-point division.
;
@@ -944,10 +1049,11 @@
(spec (fdiv ty x y)
(modifies relax_nan produces_nan)
(provide
- (= produces_nan (fp.isNaN result))
+ (= produces_nan (any_lanes ((result result)) (fp.isNaN result)))
(=> produces_nan relax_nan)
(= (:bits ty) (widthof result))
- (= result
+ (= (:lanes ty) (lanesof result))
+ (= result (lanemap ((x x) (y y))
; If either z1 or z2 is a NaN, then return an element of nans{z1,z2}.
(if (or (fp.isNaN x) (fp.isNaN y))
(nans2! x y)
@@ -982,10 +1088,11 @@
(if (and (fp.isZero y) (fp_opposite_sign! x y))
(fp.-oo (widthof x))
; Else return the result of dividing z1 by z2, rounded to the nearest representable value.
- (fp.div x y)))))))))))))))
+ (fp.div x y))))))))))))))))
(instantiate fdiv
((args (named Type) (bv 32) (bv 32)) (ret (bv 32)))
- ((args (named Type) (bv 64) (bv 64)) (ret (bv 64))))
+ ((args (named Type) (bv 64) (bv 64)) (ret (bv 64)))
+ ((args (named Type) (bv 32 x 2) (bv 32 x 2)) (ret (bv 32 x 2))))
; fmin: floating-point minimum.
@@ -994,10 +1101,11 @@
(spec (fmin ty x y)
(modifies relax_nan produces_nan)
(provide
- (= produces_nan (fp.isNaN result))
+ (= produces_nan (any_lanes ((result result)) (fp.isNaN result)))
(=> produces_nan relax_nan)
(= (:bits ty) (widthof result))
- (= result
+ (= (:lanes ty) (lanesof result))
+ (= result (lanemap ((x x) (y y))
; If either z1 or z2 is a NaN, then return an element of nans{z1,z2}.
(if (or (fp.isNaN x) (fp.isNaN y))
(nans2! x y)
@@ -1013,10 +1121,11 @@
(if (and (fp.isZero x) (fp.isZero y) (fp_opposite_sign! x y))
(fp.-zero (widthof x))
; Else return the smaller value of z1 and z2.
- (fp.min x y)))))))))
+ (fp.min x y))))))))))
(instantiate fmin
((args (named Type) (bv 32) (bv 32)) (ret (bv 32)))
- ((args (named Type) (bv 64) (bv 64)) (ret (bv 64))))
+ ((args (named Type) (bv 64) (bv 64)) (ret (bv 64)))
+ ((args (named Type) (bv 32 x 2) (bv 32 x 2)) (ret (bv 32 x 2))))
; fmax: floating-point minimum.
;
@@ -1024,10 +1133,11 @@
(spec (fmax ty x y)
(modifies relax_nan produces_nan)
(provide
- (= produces_nan (fp.isNaN result))
+ (= produces_nan (any_lanes ((result result)) (fp.isNaN result)))
(=> produces_nan relax_nan)
(= (:bits ty) (widthof result))
- (= result
+ (= (:lanes ty) (lanesof result))
+ (= result (lanemap ((x x) (y y))
; If either z1 or z2 is a NaN, then return an element of nans{z1,z2}.
(if (or (fp.isNaN x) (fp.isNaN y))
(nans2! x y)
@@ -1043,10 +1153,11 @@
(if (and (fp.isZero x) (fp.isZero y) (fp_opposite_sign! x y))
(fp.+zero (widthof x))
; Else return the smaller value of z1 and z2.
- (fp.max x y)))))))))
+ (fp.max x y))))))))))
(instantiate fmax
((args (named Type) (bv 32) (bv 32)) (ret (bv 32)))
- ((args (named Type) (bv 64) (bv 64)) (ret (bv 64))))
+ ((args (named Type) (bv 64) (bv 64)) (ret (bv 64)))
+ ((args (named Type) (bv 32 x 2) (bv 32 x 2)) (ret (bv 32 x 2))))
; fabs: floating-point absolute value.
@@ -1055,7 +1166,8 @@
(spec (fabs ty x)
(provide
(= (:bits ty) (widthof result))
- (= result
+ (= (:lanes ty) (lanesof result))
+ (= result (lanemap ((x x))
; If z is a NaN, then return z with positive sign.
(if (fp.isNaN x)
(bvand x (bvnot (fp_sign_bit_set! (widthof x))))
@@ -1069,10 +1181,11 @@
(if (fp.isPositive x)
x
; Else return z negated.
- (fp.neg x))))))))
+ (fp.neg x)))))))))
(instantiate fabs
((args (named Type) (bv 32)) (ret (bv 32)))
- ((args (named Type) (bv 64)) (ret (bv 64))))
+ ((args (named Type) (bv 64)) (ret (bv 64)))
+ ((args (named Type) (bv 32 x 2)) (ret (bv 32 x 2))))
; fneg: floating-point negation.
@@ -1081,14 +1194,16 @@
(spec (fneg ty x)
(provide
(= (:bits ty) (widthof result))
+ (= (:lanes ty) (lanesof result))
; IEEE-754 negation just toggles the sign bit for every input (normal,
; +/-0, +/-inf, NaN), so model it directly in the bit-vector theory: this
; is bit-exact and avoids an fp<->bv bridge that makes queries like
; `fmul_fneg_fneg` intractable.
- (= result (bvxor x (fp_sign_bit_set! (widthof x))))))
+ (= result (lanemap ((x x)) (bvxor x (fp_sign_bit_set! (widthof x)))))))
(instantiate fneg
((args (named Type) (bv 32)) (ret (bv 32)))
- ((args (named Type) (bv 64)) (ret (bv 64))))
+ ((args (named Type) (bv 64)) (ret (bv 64)))
+ ((args (named Type) (bv 32 x 2)) (ret (bv 32 x 2))))
; sqrt: floating-point square root.
@@ -1097,10 +1212,11 @@
(spec (sqrt ty x)
(modifies relax_nan produces_nan)
(provide
- (= produces_nan (fp.isNaN result))
+ (= produces_nan (any_lanes ((result result)) (fp.isNaN result)))
(=> produces_nan relax_nan)
(= (:bits ty) (widthof result))
- (= result
+ (= (:lanes ty) (lanesof result))
+ (= result (lanemap ((x x))
; If z is a NaN, then return an element of nans{z}.
(if (fp.isNaN x)
(nans1! x)
@@ -1117,10 +1233,11 @@
(if (fp.isNegative x)
(nans0! (widthof x))
; Else return the square root of z.
- (fp.sqrt x)))))))))
+ (fp.sqrt x))))))))))
(instantiate sqrt
((args (named Type) (bv 32)) (ret (bv 32)))
- ((args (named Type) (bv 64)) (ret (bv 64))))
+ ((args (named Type) (bv 64)) (ret (bv 64)))
+ ((args (named Type) (bv 32 x 2)) (ret (bv 32 x 2))))
; ceil: floating-point ceiling.
;
@@ -1128,10 +1245,11 @@
(spec (ceil ty x)
(modifies relax_nan produces_nan)
(provide
- (= produces_nan (fp.isNaN result))
+ (= produces_nan (any_lanes ((result result)) (fp.isNaN result)))
(=> produces_nan relax_nan)
(= (:bits ty) (widthof result))
- (= result
+ (= (:lanes ty) (lanesof result))
+ (= result (lanemap ((x x))
; If z is a NaN, then return an element of nans{z}.
(if (fp.isNaN x)
(nans1! x)
@@ -1145,10 +1263,11 @@
(if (and (fp.lt x (fp.-zero (widthof x))) (fp.gt x (fp_minus_one! (widthof x))))
(fp.-zero (widthof x))
; Else return the smallest integral value that is not smaller than z.
- (fp.ceil x))))))))
+ (fp.ceil x)))))))))
(instantiate ceil
((args (named Type) (bv 32)) (ret (bv 32)))
- ((args (named Type) (bv 64)) (ret (bv 64))))
+ ((args (named Type) (bv 64)) (ret (bv 64)))
+ ((args (named Type) (bv 32 x 2)) (ret (bv 32 x 2))))
; floor: floating-point floor.
;
@@ -1156,10 +1275,11 @@
(spec (floor ty x)
(modifies relax_nan produces_nan)
(provide
- (= produces_nan (fp.isNaN result))
+ (= produces_nan (any_lanes ((result result)) (fp.isNaN result)))
(=> produces_nan relax_nan)
(= (:bits ty) (widthof result))
- (= result
+ (= (:lanes ty) (lanesof result))
+ (= result (lanemap ((x x))
; If z is a NaN, then return an element of nans{z}.
(if (fp.isNaN x)
(nans1! x)
@@ -1173,10 +1293,11 @@
(if (and (fp.gt x (fp.-zero (widthof x))) (fp.lt x (fp_one! (widthof x))))
(fp.+zero (widthof x))
; Else return the smallest integral value that is not smaller than z.
- (fp.floor x))))))))
+ (fp.floor x)))))))))
(instantiate floor
((args (named Type) (bv 32)) (ret (bv 32)))
- ((args (named Type) (bv 64)) (ret (bv 64))))
+ ((args (named Type) (bv 64)) (ret (bv 64)))
+ ((args (named Type) (bv 32 x 2)) (ret (bv 32 x 2))))
; trunc: floating-point truncate.
;
@@ -1184,10 +1305,11 @@
(spec (trunc ty x)
(modifies relax_nan produces_nan)
(provide
- (= produces_nan (fp.isNaN result))
+ (= produces_nan (any_lanes ((result result)) (fp.isNaN result)))
(=> produces_nan relax_nan)
(= (:bits ty) (widthof result))
- (= result
+ (= (:lanes ty) (lanesof result))
+ (= result (lanemap ((x x))
; If z is a NaN, then return an element of nans{z}.
(if (fp.isNaN x)
(nans1! x)
@@ -1204,20 +1326,22 @@
(if (and (fp.lt x (fp.-zero (widthof x))) (fp.gt x (fp_minus_one! (widthof x))))
(fp.-zero (widthof x))
; Else return the integral value with the same sign as z and the largest magnitude that is not larger than the magnitude of z.
- (fp.trunc x)))))))))
+ (fp.trunc x))))))))))
(instantiate trunc
((args (named Type) (bv 32)) (ret (bv 32)))
- ((args (named Type) (bv 64)) (ret (bv 64))))
+ ((args (named Type) (bv 64)) (ret (bv 64)))
+ ((args (named Type) (bv 32 x 2)) (ret (bv 32 x 2))))
; nearest: floating-point nearest.
;
; Specification derived from WebAssembly Specification prose (Floating Point Numerics, section 4.3.3).
(spec (nearest ty x)
(modifies relax_nan produces_nan)
(provide
- (= produces_nan (fp.isNaN result))
+ (= produces_nan (any_lanes ((result result)) (fp.isNaN result)))
(=> produces_nan relax_nan)
(= (:bits ty) (widthof result))
- (= result
+ (= (:lanes ty) (lanesof result))
+ (= result (lanemap ((x x))
; If z is a NaN, then return an element of nans{z}.
(if (fp.isNaN x)
(nans1! x)
@@ -1234,10 +1358,11 @@
(if (and (fp.lt x (fp.-zero (widthof x))) (fp.ge x (fp_minus_half! (widthof x))))
(fp.-zero (widthof x))
; Else return the integral value that is nearest to z; if two values are equally near, return the even one.
- (fp.nearest x)))))))))
+ (fp.nearest x))))))))))
(instantiate nearest
((args (named Type) (bv 32)) (ret (bv 32)))
- ((args (named Type) (bv 64)) (ret (bv 64))))
+ ((args (named Type) (bv 64)) (ret (bv 64)))
+ ((args (named Type) (bv 32 x 2)) (ret (bv 32 x 2))))
; fcopysign: floating-point copysign.
;
@@ -1245,21 +1370,24 @@
(spec (fcopysign ty x y)
(provide
(= (:bits ty) (widthof result))
- (= result
+ (= (:lanes ty) (lanesof result))
+ (= result (lanemap ((x x) (y y))
; If z1 and z2 have the same sign, then return z1.
(if (fp_equal_sign_inc_nan! x y)
x
; Else return z1 with negated sign.
(if (fp.isNaN x)
(nan_neg! x)
- (fp.neg x))))))
+ (fp.neg x)))))))
(instantiate fcopysign
((args (named Type) (bv 32) (bv 32)) (ret (bv 32)))
- ((args (named Type) (bv 64) (bv 64)) (ret (bv 64))))
+ ((args (named Type) (bv 64) (bv 64)) (ret (bv 64)))
+ ((args (named Type) (bv 32 x 2) (bv 32 x 2)) (ret (bv 32 x 2))))
(spec (bitcast ty flags x)
(provide (= result x)
- (= (:bits ty) (widthof result))))
+ (= (:bits ty) (widthof result))
+ (= (:lanes ty) (lanesof result))))
; I32ReinterpretF32
; I64ReinterpretF64
; F32ReinterpretI32
@@ -1282,12 +1410,14 @@
(spec (fcvt_from_uint ty x)
(provide
(= (:bits ty) (widthof result))
+ (= (:lanes ty) (lanesof result))
(= result (to_fp_unsigned (widthof result) (zero_ext 64 x)))))
(instantiate fcvt_from_uint fcvt)
(spec (fcvt_from_sint ty x)
(provide
(= (:bits ty) (widthof result))
+ (= (:lanes ty) (lanesof result))
(= result (to_fp (widthof result) (sign_ext 64 x)))))
(instantiate fcvt_from_sint fcvt)
@@ -1299,9 +1429,11 @@
(32 #x000000004f800000)
(64 #x43f0000000000000)))
(spec (fcvt_to_uint ty x)
+ (require (= (:lanes ty) 1))
(modifies clif_trap)
(provide
(= (:bits ty) (widthof result))
+ (= (:lanes ty) (lanesof result))
(let (
(s (widthof x))
(d (widthof result)))
@@ -1325,9 +1457,11 @@
(32 (fp_minus_i32_min! w))
(64 (fp_minus_i64_min! w))))
(spec (fcvt_to_sint ty x)
+ (require (= (:lanes ty) 1))
(modifies clif_trap)
(provide
(= (:bits ty) (widthof result))
+ (= (:lanes ty) (lanesof result))
(let (
(s (widthof x))
(d (widthof result)))
@@ -1357,11 +1491,13 @@
; Specification derived from WebAssembly Specification prose (Conversions, section 4.3.4).
(spec (fdemote ty z)
+ (require (= (:lanes ty) 1))
(match
;; Demote only can return bv-32 as written
(= (widthof result) 32))
(provide
(= (:bits ty) (widthof result))
+ (= (:lanes ty) (lanesof result))
(= result
; If z is a canonical NaN, then return an element of nans{} (i.e., a canonical NaN of size N).
; Else if z is a NaN, then return an element of nans{±nan(1)} (i.e., any NaN of size N).
@@ -1386,11 +1522,13 @@
; Specification derived from WebAssembly Specification prose (Conversions, section 4.3.4).
(spec (fpromote ty z)
+ (require (= (:lanes ty) 1))
(match
;; Promote only can return bv-32 as written
(= (widthof result) 64))
(provide
(= (:bits ty) (widthof result))
+ (= (:lanes ty) (lanesof result))
(= result
; If z is a canonical NaN, then return an element of nans{} (i.e., a canonical NaN of size N).
; Else if z is a NaN, then return an element of nans{±nan(1)} (i.e., any arithmetic NaN of size N).
diff --git a/cranelift/codegen/src/spec/opt.isle b/cranelift/codegen/src/spec/opt.isle
index 2c544b16b6..78f75b1318 100644
--- a/cranelift/codegen/src/spec/opt.isle
+++ b/cranelift/codegen/src/spec/opt.isle
@@ -19,13 +19,16 @@
(spec (simplify arg)
(provide
(if relax_nan
- (fp_equiv! result arg)
+ (all_lanes ((result result) (arg arg)) (fp_equiv! result arg))
(= result arg))))
(instantiate simplify
((args (bv 8)) (ret (bv 8)))
((args (bv 16)) (ret (bv 16)))
((args (bv 32)) (ret (bv 32)))
- ((args (bv 64)) (ret (bv 64))))
+ ((args (bv 64)) (ret (bv 64)))
+ ;; Canary vector shapes to catch missing scalar guards.
+ ((args (bv 32 x 2)) (ret (bv 32 x 2)))
+ ((args (bv 8 x 4)) (ret (bv 8 x 4))))
;; `remat` and `subsume` annotate a rewrite without changing the value.
(spec (remat x) (provide (= result x)))
@@ -66,16 +69,20 @@
;; extractors they yield the constant `c` as the zero/sign-extension of the
;; matched value to 64 bits, mirroring the `iconst` spec.
(spec (iconst_u ty c)
+ (require (or (= (:lanes ty) 1) (= (:bits ty) 128)))
(provide (= c (zero_ext 64 result))
- (= (:bits ty) (widthof result))))
+ (= (:bits ty) (widthof result))
+ (= (:lanes ty) (lanesof result))))
(instantiate iconst_u
((args (named Type) (bv 64)) (ret (bv 8)))
((args (named Type) (bv 64)) (ret (bv 16)))
((args (named Type) (bv 64)) (ret (bv 32)))
((args (named Type) (bv 64)) (ret (bv 64))))
(spec (iconst_s ty c)
+ (require (or (= (:lanes ty) 1) (= (:bits ty) 128)))
(provide (= c (sign_ext 64 result))
- (= (:bits ty) (widthof result))))
+ (= (:bits ty) (widthof result))
+ (= (:lanes ty) (lanesof result))))
(instantiate iconst_s
((args (named Type) (bv 64)) (ret (bv 8)))
((args (named Type) (bv 64)) (ret (bv 16)))
@@ -137,7 +144,8 @@
;; rather than chaining.
(spec (all_zero ty)
(provide (= result (bvzero! (widthof result)))
- (= (:bits ty) (widthof result))))
+ (= (:bits ty) (widthof result))
+ (= (:lanes ty) (lanesof result))))
(instantiate all_zero
((args (named Type)) (ret (bv 8)))
((args (named Type)) (ret (bv 16)))
@@ -155,8 +163,10 @@
;; it yields the inner `val`, with the outer being the zero-extension of the
;; inner to `ty` bits. Mirrors `maybe_uextend`.
(spec (uextend_maybe ty val)
+ (require (= (:lanes ty) 1))
(provide (= result (zero_ext (widthof result) val))
- (= (:bits ty) (widthof result))))
+ (= (:bits ty) (widthof result))
+ (= (:lanes ty) (lanesof result))))
;; The rule `(uextend_maybe ty val) => (uextend ty val)` needs concrete widths to
;; type-check standalone (its spec leaves `val`'s width free). Unlike `uextend`,
@@ -176,8 +186,10 @@
;; Same as `uextend_maybe` but with sign extension.
(spec (sextend_maybe ty val)
+ (require (= (:lanes ty) 1))
(provide (= result (sign_ext (widthof result) val))
- (= (:bits ty) (widthof result))))
+ (= (:bits ty) (widthof result))
+ (= (:lanes ty) (lanesof result))))
(instantiate sextend_maybe
((args (named Type) (bv 8)) (ret (bv 8)))
((args (named Type) (bv 8)) (ret (bv 16)))
@@ -485,14 +497,16 @@
;; Partial: half-width integer type (i16->i8, ...); undefined for i8.
(spec (ty_half_width ty)
(match (bvuge (int2bv 64 (:bits ty)) (int2bv 64 16)))
- (provide (= (int2bv 64 (:bits result)) (bvlshr (int2bv 64 (:bits ty)) (int2bv 64 1)))))
+ (provide (= (int2bv 64 (:bits result)) (bvlshr (int2bv 64 (:bits ty)) (int2bv 64 1)))
+ (= (:lanes result) (:lanes ty))))
(spec (ty_equal lhs rhs) (provide (= result (= lhs rhs))))
;; Partial: bit width (8/16/32) -> that integer type.
(spec (shift_amt_to_type v)
(match (or (= v (int2bv 64 8)) (= v (int2bv 64 16)) (= v (int2bv 64 32))))
(provide (= (:bits result)
- (if (= v (int2bv 64 8)) 8 (if (= v (int2bv 64 16)) 16 32)))))
+ (if (= v (int2bv 64 8)) 8 (if (= v (int2bv 64 16)) 16 32)))
+ (= (:lanes result) 1)))
;;;; `imm64_*` min/max/shift folding ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 64-bit `Imm64`, masked to the low `ty` bits. Signed ops sign-extend those bits
@@ -659,11 +673,16 @@
;; `bitrev` = `rev`. `bmask` broadcasts truthiness to an all-ones/all-zeros mask of
;; the (possibly different-width) result type.
(spec (bitrev ty x)
- (provide (= result (rev x)) (= (:bits ty) (widthof result))))
-(instantiate bitrev bv_unary_8_to_64)
+ (require (= (:lanes ty) 1))
+ (provide (= result (rev x))
+ (= (:bits ty) (widthof result))
+ (= (:lanes ty) (lanesof result))))
+(instantiate bitrev bv_unary_scalar_8_to_64)
(spec (bmask ty v)
+ (require (= (:lanes ty) 1))
(provide (= result (if (bv_is_zero! v) (bvzero! (widthof result)) (bvones! (widthof result))))
- (= (:bits ty) (widthof result))))
+ (= (:bits ty) (widthof result))
+ (= (:lanes ty) (lanesof result))))
(instantiate bmask
((args (named Type) (bv 8)) (ret (bv 8)))
((args (named Type) (bv 8)) (ret (bv 16)))
diff --git a/cranelift/isle/isle/src/ast.rs b/cranelift/isle/isle/src/ast.rs
index 1642c7ef86..c5e9ee278c 100644
--- a/cranelift/isle/isle/src/ast.rs
+++ b/cranelift/isle/isle/src/ast.rs
@@ -181,6 +181,17 @@ pub enum AttrKind {
Tag(Ident),
}
+/// How a lane map combines the body's results over the lanes.
+#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
+pub enum Lanes {
+ /// `lanemap`: concatenate, lane 0 in the low bits.
+ Map,
+ /// `all_lanes`: conjoin booleans.
+ All,
+ /// `any_lanes`: disjoin booleans.
+ Any,
+}
+
/// An expression used to specify term semantics, similar to SMT-LIB syntax.
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum SpecExpr {
@@ -248,6 +259,14 @@ pub enum SpecExpr {
body: Box<SpecExpr>,
pos: Pos,
},
+ /// Map the body over the lanes of bit-vectors: each binder names one lane
+ /// of its bit-vector, shadowing any outer variable of that name.
+ LaneMap {
+ binders: Vec<(Ident, SpecExpr)>,
+ body: Box<SpecExpr>,
+ reduce: Lanes,
+ pos: Pos,
+ },
/// Inline macro definition, or lambda.
Macro {
/// Parameter names.
@@ -296,6 +315,7 @@ impl SpecExpr {
| &Self::Match { pos, .. }
| &Self::Let { pos, .. }
| &Self::With { pos, .. }
+ | &Self::LaneMap { pos, .. }
| &Self::Macro { pos, .. }
| &Self::Expand { pos, .. }
| &Self::Pair { pos, .. }
@@ -413,6 +433,7 @@ pub enum SpecOp {
FPToUBV,
FPToSBV,
WidthOf,
+ LanesOf,
// Control operations
If,
@@ -494,6 +515,9 @@ pub enum ModelType {
BitVec(Option<usize>),
/// SMT-LIB bitvector whose width is one of the given widths.
BitVecOneOf(Vec<usize>),
+ /// Bitvector of `lanes` lanes of `lane_bits` bits each, lowered to an SMT-LIB
+ /// bivector of size `lanes` x `lane_bits`.
+ BitVecLanes { lane_bits: usize, lanes: usize },
/// Structured type.
Struct(Vec<ModelField>),
/// Same model as the named type.
diff --git a/cranelift/isle/isle/src/parser.rs b/cranelift/isle/isle/src/parser.rs
index 89e85c1496..eeef9ce1de 100644
--- a/cranelift/isle/isle/src/parser.rs
+++ b/cranelift/isle/isle/src/parser.rs
@@ -525,6 +525,29 @@ impl<'a> Parser<'a> {
})
}
+ /// Parse the rest of `(lanemap ((<lane> <vector>) ...) <body>)`, or the
+ /// `all_lanes`/`any_lanes` forms.
+ fn parse_lane_map(&mut self, pos: Pos, reduce: Lanes) -> Result<SpecExpr> {
+ let mut binders = Vec::new();
+ self.expect_lparen()?;
+ while !(self.is_rparen()) {
+ self.expect_lparen()?;
+ let ident = self.parse_ident()?;
+ let x = self.parse_spec_expr()?;
+ self.expect_rparen()?;
+ binders.push((ident, x));
+ }
+ self.expect_rparen()?;
+ let body = Box::new(self.parse_spec_expr()?);
+ self.expect_rparen()?;
+ Ok(SpecExpr::LaneMap {
+ binders,
+ body,
+ reduce,
+ pos,
+ })
+ }
+
fn parse_spec_expr(&mut self) -> Result<SpecExpr> {
let pos = self.pos();
if self.is_spec_bit_vector() {
@@ -574,6 +597,12 @@ impl<'a> Parser<'a> {
let body = Box::new(self.parse_spec_expr()?);
self.expect_rparen()?;
Ok(SpecExpr::Let { defs, body, pos })
+ } else if self.eat_sym_str("lanemap")? {
+ self.parse_lane_map(pos, Lanes::Map)
+ } else if self.eat_sym_str("all_lanes")? {
+ self.parse_lane_map(pos, Lanes::All)
+ } else if self.eat_sym_str("any_lanes")? {
+ self.parse_lane_map(pos, Lanes::Any)
} else if self.eat_sym_str("with")? {
let mut decls = Vec::new();
self.expect_lparen()?;
@@ -720,6 +749,7 @@ impl<'a> Parser<'a> {
"int2bv" => Ok(SpecOp::Int2BV),
"bv2nat" => Ok(SpecOp::BV2Nat),
"widthof" => Ok(SpecOp::WidthOf),
+ "lanesof" => Ok(SpecOp::LanesOf),
"if" => Ok(SpecOp::If),
"switch" => Ok(SpecOp::Switch),
"popcnt" => Ok(SpecOp::Popcnt),
@@ -882,6 +912,19 @@ impl<'a> Parser<'a> {
} else {
return Err(self.error(pos, "Badly formed BitVector (bv ...)".to_string()));
};
+ // (bv w x n): n lanes of w bits.
+ if let Some(lane_bits) = width
+ && self.eat_sym_str("x")?
+ {
+ let lanes = usize::try_from(self.expect_int()?).map_err(|err| {
+ self.error(pos, format!("Invalid BitVector lane count: {err}"))
+ })?;
+ if lanes == 0 {
+ return Err(self.error(pos, "Zero BitVector lane count".to_string()));
+ }
+ self.expect_rparen()?;
+ return Ok(ModelType::BitVecLanes { lane_bits, lanes });
+ }
self.expect_rparen()?;
Ok(ModelType::BitVec(width))
} else if self.eat_sym_str("struct")? {
diff --git a/cranelift/isle/isle/src/printer.rs b/cranelift/isle/isle/src/printer.rs
index d4b1c1ee35..9fe483a94f 100644
--- a/cranelift/isle/isle/src/printer.rs
+++ b/cranelift/isle/isle/src/printer.rs
@@ -513,6 +513,12 @@ impl ToSExpr for ModelType {
SExpr::atom("bv"),
SExpr::List(widths.iter().map(SExpr::atom).collect()),
]),
+ ModelType::BitVecLanes { lane_bits, lanes } => SExpr::List(vec![
+ SExpr::atom("bv"),
+ SExpr::atom(lane_bits),
+ SExpr::atom("x"),
+ SExpr::atom(lanes),
+ ]),
ModelType::Struct(fields) => {
let mut parts = vec![SExpr::atom("struct")];
parts.extend(fields.iter().map(ToSExpr::to_sexpr));
@@ -573,6 +579,23 @@ impl ToSExpr for SpecExpr {
SExpr::List(vec![SExpr::atom("let"), SExpr::List(defs), body.to_sexpr()])
}
+ SpecExpr::LaneMap {
+ binders,
+ body,
+ reduce,
+ pos: _,
+ } => {
+ let binders = binders
+ .iter()
+ .map(|(name, expr)| SExpr::List(vec![name.to_sexpr(), expr.to_sexpr()]))
+ .collect::<Vec<_>>();
+ let op = match reduce {
+ Lanes::Map => "lanemap",
+ Lanes::All => "all_lanes",
+ Lanes::Any => "any_lanes",
+ };
+ SExpr::List(vec![SExpr::atom(op), SExpr::List(binders), body.to_sexpr()])
+ }
SpecExpr::With {
decls,
body,
@@ -677,6 +700,7 @@ impl ToSExpr for SpecOp {
SpecOp::ToFPUnsigned => "to_fp_unsigned",
SpecOp::ToFPFromFP => "to_fp_from_fp",
SpecOp::WidthOf => "widthof",
+ SpecOp::LanesOf => "lanesof",
SpecOp::If => "if",
SpecOp::Switch => "switch",
SpecOp::Popcnt => "popcnt",
diff --git a/cranelift/isle/veri/docs/language.md b/cranelift/isle/veri/docs/language.md
index 0b7d15aca3..6cc995253e 100644
--- a/cranelift/isle/veri/docs/language.md
+++ b/cranelift/isle/veri/docs/language.md
@@ -16,7 +16,9 @@ _Primitives_:
* Integer: `Int`
* Boolean: `Bool`
-* Bit-vector: unknown width `(bv)`, fixed width `(bv <n>)`
+* Bit-vector: unknown width `(bv)`, fixed width `(bv <n>)`, one of a set of
+ widths `(bv (<n1> <n2> ...))`, or a vector of `<k>` lanes of `<n>` bits
+ each `(bv <n> x <k>)`
* Unit: `Unit`
* Unspecified: `!`
* Auto: `_`, a primitive type to be deduced by type inference
@@ -268,6 +270,7 @@ Spec expression operators:
Int2BV,
BV2Nat,
WidthOf,
+ LanesOf,
// Control operations
If,
@@ -316,6 +319,44 @@ In verification, the cartesian product of all type instantantiations for all
present terms is considered. Many of the combinations will be ruled out by type
inference before proceeding to verification.
+### Vectors and lane maps
+
+In the verifier, `(bv 32 x 2)` is a 64-bit bit-vector with two 32-bit lanes used to model the
+CLIF type `i32x2`. The lower lane occupies the lower bits. Lane counts are static,
+like widths. A fixed-width bit-vector in a signature has one lane. Operands required to
+have the same type also must have the same lane count. `(widthof x)` gets the total
+(product) width and `(lanesof x)` gets the number of lanes.
+
+Specs can apply to lanes explicitly:
+
+```
+(lanemap ((<lane> <vector>) ...) <body>)
+(all_lanes ((<lane> <vector>) ...) <body>)
+(any_lanes ((<lane> <vector>) ...) <body>)
+```
+
+Each binder names one lane of its bit-vector, shadowing any outer variable of
+that name, so `(lanemap ((x x) (y y)) (bvadd x y))` maps a scalar spec over
+the individual lanes of `x` and `y`. The bound bit-vectors must have the same
+lane count. `lanemap` evaluates the body once per lane and concatenates the
+results. `all_lanes` and `any_lanes` conjoin or disjoin a
+boolean body over the lanes. Lane maps do not nest.
+
+For example, with
+
+```
+(spec (iadd ty x y)
+ (provide (= result (lanemap ((x x) (y y)) (bvadd x y)))
+ (= (:bits ty) (widthof result))
+ (= (:lanes ty) (lanesof result))))
+(instantiate iadd
+ ((args (named Type) (bv 32) (bv 32)) (ret (bv 32)))
+ ((args (named Type) (bv 32 x 2) (bv 32 x 2)) (ret (bv 32 x 2))))
+```
+
+`iadd` applied to two `(bv 32 x 2)` arguments adds each 32-bit lane separately, with no carry between
+lanes. Scalars are treated with a lane count of 1 and apply just the body expression `(bvadd x y)`.
+
## State
State variables are declared with a type and default specification:
diff --git a/cranelift/isle/veri/isaspec/src/spec.rs b/cranelift/isle/veri/isaspec/src/spec.rs
index 7a8c966949..69818492e8 100644
--- a/cranelift/isle/veri/isaspec/src/spec.rs
+++ b/cranelift/isle/veri/isaspec/src/spec.rs
@@ -319,6 +319,25 @@ pub fn substitute(expr: SpecExpr, substitutions: &HashMap<String, SpecExpr>) ->
pos,
}
}
+ SpecExpr::LaneMap {
+ binders,
+ body,
+ reduce,
+ pos,
+ } => SpecExpr::LaneMap {
+ binders: binders
+ .into_iter()
+ .map(|(var, expr)| {
+ if substitutions.contains_key(&var.0) {
+ bail!("substituted variable collides with lane binding");
+ }
+ Ok((var, substitute(expr, substitutions)?))
+ })
+ .collect::<Result<_>>()?,
+ body: Box::new(substitute(*body, substitutions)?),
+ reduce,
+ pos,
+ },
// Recurse into child expressions.
SpecExpr::As { x, ty, pos } => SpecExpr::As {
diff --git a/cranelift/isle/veri/veri/filetests/broken/lanes_carry.isle b/cranelift/isle/veri/veri/filetests/broken/lanes_carry.isle
new file mode 100644
index 0000000000..a42b053555
--- /dev/null
+++ b/cranelift/isle/veri/veri/filetests/broken/lanes_carry.isle
@@ -0,0 +1,22 @@
+; Lane-wise addition does not carry between lanes, so it differs from a flat
+; 16-bit addition at the `(bv 8 x 2)` shape.
+
+(type Value (primitive Value))
+(model Value (type (bv 16)))
+
+(decl test (Value) Value)
+(spec (test arg) (provide (= result arg)))
+(instantiate test ((args (bv 8 x 2)) (ret (bv 8 x 2))))
+
+; Lane-wise addition.
+(decl add (Value Value) Value)
+(extern extractor add add)
+(spec (add x y) (provide (= result (lanemap ((x x) (y y)) (bvadd x y)))))
+(instantiate add ((args (bv 8 x 2) (bv 8 x 2)) (ret (bv 8 x 2))))
+
+; Flat addition over the whole bit-vector.
+(decl flat_add (Value Value) Value)
+(extern constructor flat_add flat_add)
+(spec (flat_add x y) (provide (= result (bvadd x y))))
+
+(rule test (test (add x y)) (flat_add x y))
diff --git a/cranelift/isle/veri/veri/filetests/broken/lanes_missing_scalar_guard.isle b/cranelift/isle/veri/veri/filetests/broken/lanes_missing_scalar_guard.isle
new file mode 100644
index 0000000000..92a6e73ae5
--- /dev/null
+++ b/cranelift/isle/veri/veri/filetests/broken/lanes_missing_scalar_guard.isle
@@ -0,0 +1,36 @@
+; Building a scalar-only term from a type that may be a vector, as in
+; Cranelift issue #14566: the vector instantiation reaches `zero` with two
+; lanes, violating its requirement.
+
+(type Type (primitive Type))
+(model Type (type (struct (bits Int) (lanes Int))))
+
+(type Value (primitive Value))
+(model Value (type (bv (8 16))))
+
+(decl test (Value) Value)
+(spec (test arg) (provide (= result arg)))
+(instantiate test
+ ((args (bv 16)) (ret (bv 16)))
+ ((args (bv 8 x 2)) (ret (bv 8 x 2))))
+
+(decl sub (Type Value Value) Value)
+(extern extractor sub sub)
+(spec (sub ty x y)
+ (provide (= result (lanemap ((x x) (y y)) (bvsub x y)))
+ (= (:bits ty) (widthof result))
+ (= (:lanes ty) (lanesof result))))
+(instantiate sub
+ ((args (named Type) (bv 16) (bv 16)) (ret (bv 16)))
+ ((args (named Type) (bv 8 x 2) (bv 8 x 2)) (ret (bv 8 x 2))))
+
+; Scalar zero.
+(decl zero (Type) Value)
+(extern constructor zero zero)
+(spec (zero ty)
+ (require (= (:lanes ty) 1))
+ (provide (= result (conv_to (widthof result) #x0000))
+ (= (:bits ty) (widthof result))
+ (= (:lanes ty) (lanesof result))))
+
+(rule test (test (sub ty x x)) (zero ty))
diff --git a/cranelift/isle/veri/veri/filetests/pass/lanes_double_negation.isle b/cranelift/isle/veri/veri/filetests/pass/lanes_double_negation.isle
new file mode 100644
index 0000000000..a6c683771f
--- /dev/null
+++ b/cranelift/isle/veri/veri/filetests/pass/lanes_double_negation.isle
@@ -0,0 +1,26 @@
+; `lanemap` applies its body to each lane of a vector instantiation. Double
+; negation is the identity at every shape.
+
+(type Type (primitive Type))
+(model Type (type (struct (bits Int) (lanes Int))))
+
+(type Value (primitive Value))
+(model Value (type (bv (8 16))))
+
+(decl test (Value) Value)
+(spec (test arg) (provide (= result arg)))
+(instantiate test
+ ((args (bv 16)) (ret (bv 16)))
+ ((args (bv 8 x 2)) (ret (bv 8 x 2))))
+
+(decl neg (Type Value) Value)
+(extern extractor neg neg)
+(spec (neg ty x)
+ (provide (= result (lanemap ((x x)) (bvneg x)))
+ (= (:bits ty) (widthof result))
+ (= (:lanes ty) (lanesof result))))
+(instantiate neg
+ ((args (named Type) (bv 16)) (ret (bv 16)))
+ ((args (named Type) (bv 8 x 2)) (ret (bv 8 x 2))))
+
+(rule test (test (neg ty (neg ty x))) x)
diff --git a/cranelift/isle/veri/veri/filetests/pass/lanes_scalar_guard.isle b/cranelift/isle/veri/veri/filetests/pass/lanes_scalar_guard.isle
new file mode 100644
index 0000000000..4c9570153e
--- /dev/null
+++ b/cranelift/isle/veri/veri/filetests/pass/lanes_scalar_guard.isle
@@ -0,0 +1,41 @@
+; A scalar guard keeps the vector instantiation of `sub` from reaching the
+; scalar-only `zero`: it is unreachable there, while scalars verify.
+
+(type Type (primitive Type))
+(model Type (type (struct (bits Int) (lanes Int))))
+
+(type Value (primitive Value))
+(model Value (type (bv (8 16))))
+
+(decl test (Value) Value)
+(spec (test arg) (provide (= result arg)))
+(instantiate test
+ ((args (bv 16)) (ret (bv 16)))
+ ((args (bv 8 x 2)) (ret (bv 8 x 2))))
+
+(decl sub (Type Value Value) Value)
+(extern extractor sub sub)
+(spec (sub ty x y)
+ (provide (= result (lanemap ((x x) (y y)) (bvsub x y)))
+ (= (:bits ty) (widthof result))
+ (= (:lanes ty) (lanesof result))))
+(instantiate sub
+ ((args (named Type) (bv 16) (bv 16)) (ret (bv 16)))
+ ((args (named Type) (bv 8 x 2) (bv 8 x 2)) (ret (bv 8 x 2))))
+
+; Scalar zero.
+(decl zero (Type) Value)
+(extern constructor zero zero)
+(spec (zero ty)
+ (require (= (:lanes ty) 1))
+ (provide (= result (conv_to (widthof result) #x0000))
+ (= (:bits ty) (widthof result))
+ (= (:lanes ty) (lanesof result))))
+
+(decl scalar (Type) Type)
+(extern extractor scalar scalar)
+(spec (scalar ty)
+ (match (= (:lanes result) 1))
+ (provide (= result ty)))
+
+(rule test (test (sub (scalar ty) x x)) (zero ty))
diff --git a/cranelift/isle/veri/veri/src/runner.rs b/cranelift/isle/veri/veri/src/runner.rs
index 0b0caf93d6..6542b87a85 100644
--- a/cranelift/isle/veri/veri/src/runner.rs
+++ b/cranelift/isle/veri/veri/src/runner.rs
@@ -1451,7 +1451,7 @@ impl Runner {
Ok(match verification {
Verification::Failure(model) => {
let mut rendered = Vec::new();
- conditions.write_model(&mut rendered, &model, &self.prog)?;
+ conditions.write_model(&mut rendered, &model, assignment, &self.prog)?;
let rendered = String::from_utf8(rendered)?;
let mut notes = String::new();
diff --git a/cranelift/isle/veri/veri/src/solver.rs b/cranelift/isle/veri/veri/src/solver.rs
index 759b5b292d..ced9525558 100644
--- a/cranelift/isle/veri/veri/src/solver.rs
+++ b/cranelift/isle/veri/veri/src/solver.rs
@@ -1,6 +1,11 @@
-use std::{cmp::Ordering, collections::HashSet, iter::zip};
+use std::{
+ cmp::Ordering,
+ collections::{HashMap, HashSet},
+ iter::zip,
+};
use anyhow::{Context as _, Error, Result, bail};
+use cranelift_isle::ast::Lanes;
use cranelift_isle_veri_caching::{Context, Response, SExpr, SExprData};
use num_bigint::BigUint;
use num_traits::Num as _;
@@ -101,6 +106,13 @@ pub struct Solver<'a> {
/// Ill-typed expressions, left unconstrained.
ill_typed: Vec<(ExprId, String)>,
+
+ /// Expressions that stand for another: the lane variables and results of
+ /// single-lane lane maps, which are their vectors and bodies.
+ alias: HashMap<ExprId, ExprId>,
+
+ /// Bindings for per-lane mappings.
+ lane_env: HashMap<ExprId, SExpr>,
}
impl Drop for Solver<'_> {
@@ -126,6 +138,8 @@ impl<'a> Solver<'a> {
tmp_idx: 0,
sqrt_uf_widths: HashSet::new(),
ill_typed: Vec::new(),
+ alias: HashMap::new(),
+ lane_env: HashMap::new(),
};
solver.prelude()?;
Ok(solver)
@@ -147,9 +161,15 @@ impl<'a> Solver<'a> {
}
pub fn encode(&mut self) -> Result<()> {
+ // Simplifies the scalar case of specs to just the body
+ self.resolve_single_lane_maps();
+
// Expressions
for (i, expr) in self.conditions.exprs.iter().enumerate() {
let x = ExprId(i);
+ if self.alias.contains_key(&x) {
+ continue;
+ }
self.declare_expr(x)?;
if !expr.is_variable() {
self.assign_expr(x, expr)?;
@@ -462,6 +482,16 @@ impl<'a> Solver<'a> {
Expr::FPToUBV(w, x) => self.fp_to_bv(w, x, false),
Expr::FPToSBV(w, x) => self.fp_to_bv(w, x, true),
Expr::WidthOf(x) => self.width_of(x),
+ Expr::LanesOf(x) => Ok(self.smt.numeral(self.assignment.lanes_or_scalar(x))),
+ Expr::LaneMap {
+ ref binders,
+ ref scope,
+ body,
+ lanes,
+ } => {
+ let (binders, scope) = (binders.clone(), scope.clone());
+ self.lane_map(&binders, scope, body, lanes)
+ }
Expr::FPPositiveInfinity(x) => Ok(self.fp_value("+oo", x)?),
Expr::FPNegativeInfinity(x) => Ok(self.fp_value("-oo", x)?),
Expr::FPPositiveZero(x) => Ok(self.fp_value("+zero", x)?),
@@ -615,6 +645,97 @@ impl<'a> Solver<'a> {
Ok(self.smt.numeral(width))
}
+ /// A lane map over a single lane is just its body: alias each lane
+ /// variable to its vector and the lane map to its body, so none of them is
+ /// declared and the encoding is the body written over the vectors.
+ fn resolve_single_lane_maps(&mut self) {
+ for (i, expr) in self.conditions.exprs.iter().enumerate() {
+ let Expr::LaneMap { binders, body, .. } = expr else {
+ continue;
+ };
+ if Self::lane_count(&self.assignment, binders) != 1 {
+ continue;
+ }
+ for (lane, vector) in binders {
+ self.alias.insert(*lane, *vector);
+ }
+ self.alias.insert(ExprId(i), *body);
+ }
+ }
+
+ /// Lane count of a lane map. Type inference gives every bound vector the
+ /// same count, so the first decides.
+ fn lane_count(assignment: &Assignment, binders: &[(ExprId, ExprId)]) -> usize {
+ binders
+ .first()
+ .map_or(1, |(_, vector)| assignment.lanes_or_scalar(*vector))
+ }
+
+ /// Encode a lane map over several lanes. Per lane, each lane variable is
+ /// bound to that lane of its vector and every expression in the body's
+ /// scope is freshly declared.
+ fn lane_map(
+ &mut self,
+ binders: &[(ExprId, ExprId)],
+ scope: std::ops::Range<usize>,
+ body: ExprId,
+ lanes: Lanes,
+ ) -> Result<SExpr> {
+ let conditions = self.conditions;
+ let n = Self::lane_count(&self.assignment, binders);
+ debug_assert!(n > 1, "single-lane lane maps are resolved before encoding");
+
+ let mut vectors = Vec::new();
+ for (lane, vector) in binders {
+ let width = self
+ .assignment
+ .try_bit_vector_width(*vector)
+ .context("lane map vector should be a bit-vector of known width")?;
+ vectors.push((*lane, self.expr_atom(*vector), width / n));
+ }
+
+ let mut results = Vec::new();
+ for i in 0..n {
+ self.lane_env.clear();
+ for &(lane, vector, lane_bits) in &vectors {
+ let slice = self.extract((i + 1) * lane_bits - 1, i * lane_bits, vector);
+ self.lane_env.insert(lane, slice);
+ }
+ for x in scope.clone().map(ExprId) {
+ let expr = &conditions.exprs[x.index()];
+ if matches!(expr, Expr::LaneMap { .. }) {
+ bail!("lane maps do not nest");
+ }
+ let sort = self.type_to_sort(&self.assignment.try_assignment(x)?.ty())?;
+ let name = format!("$lane{i}_{}_{}", self.expr_name(x), self.tmp_idx);
+ self.tmp_idx += 1;
+ self.smt.declare_const(&name, sort)?;
+ let atom = self.smt.atom(name);
+ if !expr.is_variable() {
+ let rhs = self.expr_to_smt(expr)?;
+ self.smt.assert(self.smt.eq(atom, rhs))?;
+ }
+ self.lane_env.insert(x, atom);
+ }
+ results.push(self.expr_atom(body));
+ }
+ self.lane_env.clear();
+
+ match lanes {
+ Lanes::All => Ok(self.smt.and_many(results)),
+ Lanes::Any => Ok(self.smt.or_many(results)),
+ Lanes::Map => {
+ // `concat` puts its first argument in the high bits; lane 0 is lowest.
+ let mut results = results.into_iter().rev();
+ let mut acc = results.next().expect("at least one lane");
+ for lane in results {
+ acc = self.smt.concat(acc, lane);
+ }
+ Ok(acc)
+ }
+ }
+ }
+
fn verification_condition(&mut self) -> Result<()> {
// (not (<assumptions> => <assertions>))
let assumptions = self.all(&self.conditions.assumptions);
@@ -967,6 +1088,13 @@ impl<'a> Solver<'a> {
fn const_from_sexpr(&self, sexpr: SExpr) -> Result<Const> {
match self.smt.get(sexpr) {
SExprData::Atom(a) => Self::const_from_literal(a),
+ // Negative integer: (- n).
+ SExprData::List([op, n]) if matches!(self.smt.get(*op), SExprData::Atom("-")) => {
+ match self.const_from_sexpr(*n)? {
+ Const::Int(v) => Ok(Const::Int(-v)),
+ c => bail!("unsupported negated smt const: {c}"),
+ }
+ }
SExprData::List(exprs) => self.const_from_qualified_abstract_value(exprs),
SExprData::String(s) => bail!("unsupported smt const: {s}"),
}
@@ -1032,6 +1160,13 @@ impl<'a> Solver<'a> {
}
fn expr_atom(&self, x: ExprId) -> SExpr {
+ let mut x = x;
+ while let Some(target) = self.alias.get(&x) {
+ x = *target;
+ }
+ if let Some(atom) = self.lane_env.get(&x) {
+ return *atom;
+ }
self.smt.atom(self.expr_name(x))
}
diff --git a/cranelift/isle/veri/veri/src/spec.rs b/cranelift/isle/veri/veri/src/spec.rs
index 6b29146118..238d3986a3 100644
--- a/cranelift/isle/veri/veri/src/spec.rs
+++ b/cranelift/isle/veri/veri/src/spec.rs
@@ -1,6 +1,6 @@
use anyhow::{Ok, Result, bail, format_err};
use cranelift_isle::{
- ast::{self, AttrKind, AttrTarget, Def, Ident, Model, ModelType, Modifies, SpecOp},
+ ast::{self, AttrKind, AttrTarget, Def, Ident, Lanes, Model, ModelType, Modifies, SpecOp},
lexer::Pos,
sema::{ReturnKind, RuleId, Sym, Term, TermEnv, TermId, TypeEnv, TypeId},
};
@@ -50,6 +50,8 @@ pub enum ExprKind {
// Get the width of a bitvector
WidthOf(Expr),
+ // Get the number of lanes of a bitvector
+ LanesOf(Expr),
// Boolean operations
Not(Expr),
@@ -165,6 +167,9 @@ pub enum ExprKind {
Match(Expr, Vec<Arm>),
// Let bindings
Let(Vec<(Ident, Expr)>, Expr),
+
+ // Lane mapping: (binders, body, reduction)
+ LaneMap(Vec<(Ident, Expr)>, Expr, Lanes),
// With scope.
With(Vec<Ident>, Expr),
// Macro definition.
@@ -342,6 +347,7 @@ impl ExprKind {
SpecOp::Int2BV => binary_expr!(ExprKind::Int2BV, args, pos),
SpecOp::BV2Nat => unary_expr!(ExprKind::BV2Nat, args, pos),
SpecOp::WidthOf => unary_expr!(ExprKind::WidthOf, args, pos),
+ SpecOp::LanesOf => unary_expr!(ExprKind::LanesOf, args, pos),
SpecOp::ToFP => binary_expr!(ExprKind::ToFP, args, pos),
SpecOp::ToFPUnsigned => binary_expr!(ExprKind::ToFPUnsigned, args, pos),
SpecOp::ToFPFromFP => binary_expr!(ExprKind::ToFPFromFP, args, pos),
@@ -419,6 +425,19 @@ impl ExprKind {
let body = expr_from_ast(body);
ExprKind::Let(defs, body)
}
+ ast::SpecExpr::LaneMap {
+ binders,
+ body,
+ reduce,
+ pos: _,
+ } => {
+ let binders = binders
+ .iter()
+ .map(|(ident, x)| (ident.clone(), expr_from_ast(x)))
+ .collect();
+ let body = expr_from_ast(body);
+ ExprKind::LaneMap(binders, body, *reduce)
+ }
ast::SpecExpr::With {
decls,
body,
diff --git a/cranelift/isle/veri/veri/src/type_inference.rs b/cranelift/isle/veri/veri/src/type_inference.rs
index 0b38467029..1c43b5390e 100644
--- a/cranelift/isle/veri/veri/src/type_inference.rs
+++ b/cranelift/isle/veri/veri/src/type_inference.rs
@@ -6,7 +6,7 @@ use std::{
};
use anyhow::{Result, bail, format_err};
-use cranelift_isle::{files::Files, sema::TermId};
+use cranelift_isle::{ast::Lanes, files::Files, sema::TermId};
use crate::{
spec::Signature,
@@ -95,6 +95,15 @@ pub enum Constraint {
Identical { x: ExprId, y: ExprId },
/// Expression x is a bitvector with width given by the integer expression w.
WidthOf { x: ExprId, w: ExprId },
+ /// Bitvector x has n lanes.
+ Lanes { x: ExprId, n: usize },
+ /// Bitvectors have the same number of lanes.
+ SameLanes { x: ExprId, y: ExprId },
+ /// Integer expression l is the lane count of bitvector x.
+ LanesOf { x: ExprId, l: ExprId },
+ /// Bitvector x is one lane of bitvector v: v's width is its lane count
+ /// times x's width.
+ LaneOf { x: ExprId, v: ExprId },
/// Bitvector x is the concatenation bitvectors l and r.
Concat { x: ExprId, l: ExprId, r: ExprId },
/// Expression x has known constant value v.
@@ -114,6 +123,12 @@ impl std::fmt::Display for Constraint {
}
Constraint::Identical { x, y } => write!(f, "{} == {}", x.index(), y.index()),
Constraint::WidthOf { x, w } => write!(f, "{} = width_of({})", w.index(), x.index()),
+ Constraint::Lanes { x, n } => write!(f, "lanes({}) = {n}", x.index()),
+ Constraint::SameLanes { x, y } => {
+ write!(f, "lanes({}) == lanes({})", x.index(), y.index())
+ }
+ Constraint::LanesOf { x, l } => write!(f, "{} = lanes_of({})", l.index(), x.index()),
+ Constraint::LaneOf { x, v } => write!(f, "{} = lane_of({})", x.index(), v.index()),
Constraint::Concat { x, l, r } => {
write!(f, "{} = {}:{}", x.index(), l.index(), r.index())
}
@@ -279,6 +294,11 @@ impl<'a> SystemBuilder<'a> {
self.qualifier(qualifier);
}
+ // Lanes.
+ for (x, n) in &self.conditions.lane_counts {
+ self.constraint(Constraint::Lanes { x: *x, n: *n });
+ }
+
self.system.width_domains = self.conditions.width_domains.clone();
self.system
@@ -499,6 +519,49 @@ impl<'a> SystemBuilder<'a> {
self.bit_vector(*y);
self.width_of(*y, x);
}
+ Expr::LanesOf(y) => {
+ self.integer(x);
+ self.bit_vector(*y);
+ self.constraint(Constraint::LanesOf { x: *y, l: x });
+ }
+ Expr::LaneMap {
+ binders,
+ body,
+ lanes,
+ ..
+ } => {
+ match lanes {
+ Lanes::Map => {
+ self.bit_vector(x);
+ self.bit_vector(*body);
+ self.constraint(Constraint::Lanes { x: *body, n: 1 });
+ self.constraint(Constraint::LaneOf { x: *body, v: x });
+ // The result has the lane count of the vectors.
+ for (_, vector) in binders {
+ self.constraint(Constraint::SameLanes { x, y: *vector });
+ }
+ }
+ Lanes::All | Lanes::Any => {
+ self.boolean(x);
+ self.boolean(*body);
+ }
+ }
+ for (lane, vector) in binders {
+ self.bit_vector(*lane);
+ self.bit_vector(*vector);
+ self.constraint(Constraint::Lanes { x: *lane, n: 1 });
+ self.constraint(Constraint::LaneOf {
+ x: *lane,
+ v: *vector,
+ });
+ }
+ for pair in binders.windows(2) {
+ self.constraint(Constraint::SameLanes {
+ x: pair[0].1,
+ y: pair[1].1,
+ });
+ }
+ }
Expr::FPPositiveInfinity(w)
| Expr::FPNegativeInfinity(w)
| Expr::FPPositiveZero(w)
@@ -529,17 +592,28 @@ impl<'a> SystemBuilder<'a> {
// Arguments.
assert_eq!(call.args.len(), sig.args.len());
for (a, ty) in zip(&call.args, &sig.args) {
- self.symbolic(a, ty.clone());
+ self.signature_type(a, ty.clone());
}
// Return.
- self.symbolic(&call.ret, sig.ret.clone());
+ self.signature_type(&call.ret, sig.ret.clone());
// Pop branch arm.
self.pop();
}
}
+ /// Type from a term signature
+ fn signature_type(&mut self, v: &Symbolic, ty: Compound) {
+ match (v, ty) {
+ (Symbolic::Scalar(x), Compound::Primitive(Type::BitVector(Width::Bits(bits)))) => {
+ self.bit_vector_of_width(*x, bits);
+ self.constraint(Constraint::Lanes { x: *x, n: 1 });
+ }
+ (v, ty) => self.symbolic(v, ty),
+ }
+ }
+
fn qualifier(&mut self, qualifier: &Qualifier) {
self.symbolic(&qualifier.value, qualifier.ty.clone());
}
@@ -547,6 +621,11 @@ impl<'a> SystemBuilder<'a> {
fn symbolic(&mut self, v: &Symbolic, ty: Compound) {
match (v, ty) {
(Symbolic::Scalar(x), Compound::Primitive(ty)) => self.ty(*x, ty),
+ (Symbolic::Scalar(x), ty @ Compound::Vector { .. }) => {
+ let (width, lanes) = ty.as_vector().expect("vector");
+ self.bit_vector_of_width(*x, width);
+ self.constraint(Constraint::Lanes { x: *x, n: lanes });
+ }
(Symbolic::Struct(fields), Compound::Struct(field_tys)) => {
assert_eq!(fields.len(), field_tys.len());
for (field, field_ty) in zip(fields, field_tys) {
@@ -649,15 +728,27 @@ impl<'a> SystemBuilder<'a> {
#[derive(Default, Clone)]
pub struct Assignment {
pub expr_type_value: HashMap<ExprId, TypeValue>,
+ pub expr_lanes: HashMap<ExprId, usize>,
}
impl Assignment {
pub fn new() -> Self {
Self {
expr_type_value: HashMap::new(),
+ expr_lanes: HashMap::new(),
}
}
+ /// Lane count, if determined.
+ pub fn lanes(&self, x: ExprId) -> Option<usize> {
+ self.expr_lanes.get(&x).copied()
+ }
+
+ /// Lane count, where an undetermined count is a scalar.
+ pub fn lanes_or_scalar(&self, x: ExprId) -> usize {
+ self.lanes(x).unwrap_or(1)
+ }
+
pub fn is_concrete(&self) -> bool {
self.expr_type_value
.values()
@@ -690,6 +781,12 @@ impl Assignment {
Constraint::SameType { x, y } => self.expect_same_type(x, y),
Constraint::Identical { x, y } => self.expect_identical(x, y),
Constraint::WidthOf { x, w } => self.expect_width_of(x, w),
+ Constraint::Lanes { x, n } => self.expect_lanes(x, n),
+ Constraint::SameLanes { x, y } => self.expect_lanes(y, self.lanes_or_scalar(x)),
+ Constraint::LanesOf { x, l } => {
+ self.expect_value(l, &Const::Int(self.lanes_or_scalar(x).try_into().unwrap()))
+ }
+ Constraint::LaneOf { x, v } => self.expect_lane_of(x, v),
Constraint::Concat { x, l, r } => self.expect_concat(x, l, r),
Constraint::Value { x, ref c } => self.expect_value(x, c),
Constraint::Implies { c, ref then } => self.expect_implies(c, then),
@@ -796,6 +893,31 @@ impl Assignment {
Ok(())
}
+ fn expect_lanes(&self, x: ExprId, n: usize) -> Result<()> {
+ let lanes = self.lanes_or_scalar(x);
+ if lanes != n {
+ bail!(
+ "expression {x} should have {n} lanes: got {lanes}",
+ x = x.index()
+ );
+ }
+ Ok(())
+ }
+
+ fn expect_lane_of(&self, x: ExprId, v: ExprId) -> Result<()> {
+ let x_width = self.try_bit_vector_width(x)?;
+ let v_width = self.try_bit_vector_width(v)?;
+ let n = self.lanes_or_scalar(v);
+ if v_width != n * x_width {
+ bail!(
+ "expression {x} should be one of {n} lanes of {v}",
+ x = x.index(),
+ v = v.index()
+ );
+ }
+ Ok(())
+ }
+
fn expect_concat(&self, x: ExprId, l: ExprId, r: ExprId) -> Result<()> {
// All inputs should be bitvectors of known width.
let x_width = self.try_bit_vector_width(x)?;
@@ -965,6 +1087,31 @@ impl Solver {
return solutions;
}
+ // Bit-vectors whose lane count is still undetermined are scalars.
+ let undetermined: Vec<_> = system
+ .constraints
+ .iter()
+ .filter_map(|c| match c {
+ Constraint::LanesOf { x, .. } | Constraint::LaneOf { v: x, .. }
+ if self.assignment.lanes(*x).is_none() =>
+ {
+ Some(Constraint::Lanes { x: *x, n: 1 })
+ }
+ _ => None,
+ })
+ .collect();
+ if !undetermined.is_empty() {
+ let mut constraints = system.constraints.clone();
+ constraints.extend(undetermined);
+ let child = System {
+ choices: system.choices.clone(),
+ constraints,
+ branches: Vec::new(),
+ width_domains: system.width_domains.clone(),
+ };
+ return self.solve(&child);
+ }
+
let status = if self.assignment.is_concrete() {
Status::Solved
} else {
@@ -1009,6 +1156,10 @@ impl Solver {
Constraint::SameType { x, y } => self.same_type(*x, *y),
Constraint::Identical { x, y } => self.identical(*x, *y),
Constraint::WidthOf { x, w } => self.width_of(*x, *w),
+ Constraint::Lanes { x, n } => self.set_lanes(*x, *n),
+ Constraint::SameLanes { x, y } => self.same_lanes(*x, *y),
+ Constraint::LanesOf { x, l } => self.lanes_of(*x, *l),
+ Constraint::LaneOf { x, v } => self.lane_of(*x, *v),
Constraint::Concat { x, l, r } => self.concat(*x, *l, *r),
Constraint::Value { x, c } => self.set_value(*x, c.clone()),
Constraint::Implies { c, then } => self.implies(*c, then),
@@ -1069,6 +1220,62 @@ impl Solver {
(None, Some(tvy)) => self.set_type(x, tvy.ty()),
(Some(tvx), Some(tvy)) => Ok(self.set_type(x, tvy.ty())? | self.set_type(y, tvx.ty())?),
}
+ .and_then(|change| Ok(change | self.same_lanes(x, y)?))
+ }
+
+ fn set_lanes(&mut self, x: ExprId, n: usize) -> Result<bool, Status> {
+ match self.assignment.expr_lanes.entry(x) {
+ Entry::Vacant(e) => {
+ e.insert(n);
+ Ok(true)
+ }
+ Entry::Occupied(e) if *e.get() == n => Ok(false),
+ Entry::Occupied(e) => Err(Status::Inapplicable(Conflict::new(
+ x,
+ format!("inapplicable lanes: {existing} != {n}", existing = e.get()),
+ ))),
+ }
+ }
+
+ fn same_lanes(&mut self, x: ExprId, y: ExprId) -> Result<bool, Status> {
+ match (self.assignment.lanes(x), self.assignment.lanes(y)) {
+ (None, None) => Ok(false),
+ (Some(n), _) => self.set_lanes(y, n),
+ (None, Some(n)) => self.set_lanes(x, n),
+ }
+ }
+
+ fn lanes_of(&mut self, x: ExprId, l: ExprId) -> Result<bool, Status> {
+ match (self.assignment.lanes(x), self.assignment.int_value(l)) {
+ (Some(n), _) => self.set_int_value(l, n.try_into().unwrap()),
+ (None, Some(v)) => {
+ let n = usize::try_from(v).map_err(|_| {
+ Status::Inapplicable(Conflict::new(l, format!("invalid lane count {v}")))
+ })?;
+ self.set_lanes(x, n)
+ }
+ (None, None) => Ok(false),
+ }
+ }
+
+ fn lane_of(&mut self, x: ExprId, v: ExprId) -> Result<bool, Status> {
+ match (
+ self.assignment.lanes(v),
+ self.assignment.bit_vector_width(x),
+ self.assignment.bit_vector_width(v),
+ ) {
+ (Some(n), _, Some(vw)) => {
+ if vw % n != 0 {
+ return Err(Status::Inapplicable(Conflict::new(
+ v,
+ format!("width {vw} is not {n} equal lanes"),
+ )));
+ }
+ self.set_bit_vector_width(x, vw / n)
+ }
+ (Some(n), Some(xw), None) => self.set_bit_vector_width(v, n * xw),
+ (None, _, _) | (Some(_), None, None) => Ok(false),
+ }
}
fn identical(&mut self, x: ExprId, y: ExprId) -> Result<bool, Status> {
@@ -1083,6 +1290,7 @@ impl Solver {
Ok(self.set_type_value(x, tvy)? | self.set_type_value(y, tvx)?)
}
}
+ .and_then(|change| Ok(change | self.same_lanes(x, y)?))
}
fn width_of(&mut self, x: ExprId, w: ExprId) -> Result<bool, Status> {
diff --git a/cranelift/isle/veri/veri/src/types.rs b/cranelift/isle/veri/veri/src/types.rs
index 059e790785..7f3d4e87a7 100644
--- a/cranelift/isle/veri/veri/src/types.rs
+++ b/cranelift/isle/veri/veri/src/types.rs
@@ -112,6 +112,7 @@ impl PartialOrd for Type {
#[derive(Debug, Clone)]
pub enum Compound {
Primitive(Type),
+ Vector { lane_bits: usize, lanes: usize },
Struct(Vec<Field>),
Enum(Enum),
// TODO(mbm): intern name identifier
@@ -296,6 +297,10 @@ impl Compound {
Self::Primitive(Type::BitVector(Width::Unknown))
}
ModelType::BitVec(Some(bits)) => Self::Primitive(Type::BitVector(Width::Bits(*bits))),
+ ModelType::BitVecLanes { lane_bits, lanes } => Self::Vector {
+ lane_bits: *lane_bits,
+ lanes: *lanes,
+ },
ModelType::Struct(fields) => Self::Struct(
fields
.iter()
@@ -349,6 +354,14 @@ impl Compound {
}
}
+ /// Total width and lane count, if this is a vector.
+ pub fn as_vector(&self) -> Option<(usize, usize)> {
+ match self {
+ Compound::Vector { lane_bits, lanes } => Some((lane_bits * lanes, *lanes)),
+ _ => None,
+ }
+ }
+
pub fn as_enum(&self) -> Option<&Enum> {
match self {
Compound::Enum(e) => Some(e),
@@ -362,7 +375,7 @@ impl Compound {
F: FnMut(&Ident) -> Result<Compound>,
{
match self {
- Compound::Primitive(_) => Ok(self.clone()),
+ Compound::Primitive(_) | Compound::Vector { .. } => Ok(self.clone()),
Compound::Struct(fields) => Ok(Compound::Struct(
fields
.iter()
@@ -383,6 +396,7 @@ impl std::fmt::Display for Compound {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Compound::Primitive(ty) => ty.fmt(f),
+ Compound::Vector { lane_bits, lanes } => write!(f, "bv {lane_bits}x{lanes}"),
Compound::Struct(fields) => write!(
f,
"{{{fields}}}",
diff --git a/cranelift/isle/veri/veri/src/veri.rs b/cranelift/isle/veri/veri/src/veri.rs
index 548289ab9d..93d01982fd 100644
--- a/cranelift/isle/veri/veri/src/veri.rs
+++ b/cranelift/isle/veri/veri/src/veri.rs
@@ -1,17 +1,19 @@
use crate::{
expand::{Constrain, Expansion},
program::Program,
- spec::{self, Arm, Constructor, Signature, State},
+ spec::{self, Arm, Constructor, Signature, Spec, State},
trie::{BindingType, binding_type},
+ type_inference::Assignment,
types::{Compound, Const, Type, Variant, Width, field_name_by_index},
};
use anyhow::{Context, Error, Result, bail, format_err};
use cranelift_isle::{
- ast::Ident,
+ ast::{Ident, Lanes},
lexer::Pos,
sema::{self, Sym, TermId, TypeId, VariantId},
trie_again::{Binding, BindingId, Constraint, TupleIndex},
};
+use num_bigint::BigUint;
use std::{
collections::{HashMap, HashSet, hash_map::Entry},
iter::zip,
@@ -104,6 +106,19 @@ pub enum Expr {
// Bitwidth.
WidthOf(ExprId),
+ // Lane count.
+ LanesOf(ExprId),
+
+ // Map `body` over lanes: each binder is a (lane variable, vector) pair.
+ // `scope` is the range of expressions allocated while evaluating the body,
+ // which is every expression that can depend on a lane variable.
+ LaneMap {
+ binders: Vec<(ExprId, ExprId)>,
+ scope: std::ops::Range<usize>,
+ body: ExprId,
+ lanes: Lanes,
+ },
+
// Floating point conversion.
ToFP(ExprId, ExprId),
ToFPUnsigned(ExprId, ExprId),
@@ -165,6 +180,7 @@ impl Expr {
| Expr::Rev(x)
| Expr::Popcnt(x)
| Expr::WidthOf(x)
+ | Expr::LanesOf(x)
| Expr::FPPositiveInfinity(x)
| Expr::FPNegativeInfinity(x)
| Expr::FPPositiveZero(x)
@@ -241,6 +257,11 @@ impl Expr {
// Ternary
Expr::Conditional(c, t, e) => vec![*c, *t, *e],
+ Expr::LaneMap { binders, body, .. } => binders
+ .iter()
+ .flat_map(|(lane, vector)| [*lane, *vector])
+ .chain([*body])
+ .collect(),
}
}
}
@@ -306,6 +327,24 @@ impl std::fmt::Display for Expr {
Expr::FPToSBV(w, x) => write!(f, "fp.to_sbv({}, {})", w.index(), x.index()),
Expr::BV2Nat(x) => write!(f, "bv2nat({})", x.index()),
Expr::WidthOf(x) => write!(f, "width_of({})", x.index()),
+ Expr::LanesOf(x) => write!(f, "lanes_of({})", x.index()),
+ Expr::LaneMap {
+ binders,
+ body,
+ lanes,
+ ..
+ } => {
+ let op = match lanes {
+ Lanes::Map => "lanemap",
+ Lanes::All => "all_lanes",
+ Lanes::Any => "any_lanes",
+ };
+ write!(f, "{op}(")?;
+ for (lane, vector) in binders {
+ write!(f, "{} of {}, ", lane.index(), vector.index())?;
+ }
+ write!(f, "{})", body.index())
+ }
Expr::FPPositiveInfinity(x) => write!(f, "fp.+oo({})", x.index()),
Expr::FPNegativeInfinity(x) => write!(f, "fp.-oo({})", x.index()),
Expr::FPPositiveZero(x) => write!(f, "fp.+zero({})", x.index()),
@@ -775,6 +814,8 @@ pub struct Conditions {
pub qualifiers: Vec<Qualifier>,
pub pos: HashMap<ExprId, Pos>,
pub width_domains: Vec<(ExprId, Vec<usize>)>,
+ /// Bit-vectors with a known lane count.
+ pub lane_counts: Vec<(ExprId, usize)>,
}
impl Conditions {
@@ -901,16 +942,43 @@ impl Conditions {
reach
}
- pub fn print_model(&self, model: &Model, prog: &Program) -> Result<()> {
- self.write_model(&mut std::io::stdout(), model, prog)
+ pub fn print_model(
+ &self,
+ model: &Model,
+ assignment: &Assignment,
+ prog: &Program,
+ ) -> Result<()> {
+ self.write_model(&mut std::io::stdout(), model, assignment, prog)
}
pub fn write_model(
&self,
out: &mut dyn std::io::Write,
model: &Model,
+ assignment: &Assignment,
prog: &Program,
) -> Result<()> {
+ // Vectors are shown lane by lane, lane 0 first, as in CLIF.
+ let show = |v: &Symbolic| -> Result<String> {
+ let value = v.eval(model)?;
+ if let (Symbolic::Scalar(x), Value::Const(Const::BitVector(width, bits))) = (v, &value)
+ {
+ let lanes = assignment.lanes_or_scalar(*x);
+ if lanes > 1 {
+ let lane_bits = width / lanes;
+ let mask = (BigUint::from(1u8) << lane_bits) - 1u8;
+ let lanes = (0..lanes)
+ .map(|i| {
+ Const::BitVector(lane_bits, (bits >> (i * lane_bits)) & &mask)
+ .to_string()
+ })
+ .collect::<Vec<_>>();
+ return Ok(format!("[{}]", lanes.join(" ")));
+ }
+ }
+ Ok(value.to_string())
+ };
+
// State
for (name, value) in &self.state.0 {
writeln!(out, "state: {name} = {}", value.eval(model)?)?;
@@ -932,13 +1000,25 @@ impl Conditions {
args = call
.args
.iter()
- .map(|a| Ok(a.eval(model)?.to_string()))
+ .map(show)
.collect::<Result<Vec<_>>>()?
.join(", "),
- ret = call.ret.eval(model)?
+ ret = show(&call.ret)?
)?;
}
+ // Assertions that the model violates
+ for a in &self.assertions {
+ if model.get(a) == Some(&Const::Bool(false)) {
+ let position = self
+ .pos
+ .get(a)
+ .map(|pos| pos.pretty_print_line(&prog.files))
+ .unwrap_or_else(|| "?".to_string());
+ writeln!(out, "violated: {position}: e{x}", x = a.index())?;
+ }
+ }
+
Ok(())
}
@@ -1004,6 +1084,10 @@ impl Variables {
.ok_or(format_err!("undefined variable {name}"))
}
+ fn shadow(&mut self, name: String, value: Symbolic) {
+ self.0.insert(name, value);
+ }
+
fn set(&mut self, name: String, value: Symbolic) -> Result<()> {
match self.0.entry(name) {
Entry::Occupied(e) => {
@@ -1031,6 +1115,7 @@ struct ConditionsBuilder<'a> {
expr_map: HashMap<Expr, ExprId>,
conditions: Conditions,
position_stack: Vec<Pos>,
+ in_lane_map: bool,
}
impl<'a> ConditionsBuilder<'a> {
@@ -1049,6 +1134,7 @@ impl<'a> ConditionsBuilder<'a> {
expr_map: HashMap::new(),
conditions: Conditions::default(),
position_stack: Vec::new(),
+ in_lane_map: false,
}
}
@@ -1396,19 +1482,9 @@ impl<'a> ConditionsBuilder<'a> {
.get(&term)
.ok_or(self.error(format!("no spec for term {term_name}",)))?;
- // We are provided the arguments and return value as they appear
- // syntactically in the term declaration and specification. However,
- // whether these are the actual inputs and outputs of the corresponding
- // function depends on the term kind.
if term_spec.args.len() != args.len() {
bail!("incorrect number of arguments for term {term_name}");
}
- let arguments: Vec<_> = zip(&term_spec.args, args).collect();
- let result = (&term_spec.ret, &ret);
- let (inputs, outputs) = match kind {
- TermKind::Constructor => (arguments.as_slice(), std::slice::from_ref(&result)),
- TermKind::Extractor => (std::slice::from_ref(&result), arguments.as_slice()),
- };
// Scope for spec expression evaluation. State variables are always available.
let mut vars = self.conditions.state.clone();
@@ -1428,7 +1504,6 @@ impl<'a> ConditionsBuilder<'a> {
// TODO(mbm): warn when state is both conditionally and unconditionaly modified.
self.boolean(true)
};
-
// Record condition to determine when the default spec applies.
self.state_modification_conds
.entry(modifies.state.0.clone())
@@ -1436,6 +1511,65 @@ impl<'a> ConditionsBuilder<'a> {
.push(cond);
}
+ let (requires, matches, mut provides) =
+ self.spec_clauses(term_spec, kind, args, &ret, vars)?;
+
+ // Partial function.
+ // REVIEW(mbm): pin down semantics for partial function specifications.
+ if let Domain::Partial(p) = domain {
+ // Matches describe when the function applies.
+ let all_matches = self.all(matches);
+ let eq = self.exprs_equal(p, all_matches);
+ self.conditions.assumptions.push(eq);
+
+ // Provides are conditioned on the match.
+ let all_provides = self.all(provides);
+ let provide = self.dedup_expr(Expr::Imp(all_matches, all_provides));
+ provides = vec![provide];
+ } else if !matches.is_empty() {
+ bail!("spec matches on non-partial function");
+ }
+
+ // Assert/assume depending on caller or callee.
+ match invocation {
+ Invocation::Caller => {
+ self.conditions.assertions.extend(requires);
+ self.conditions.assumptions.extend(provides);
+ }
+ Invocation::Callee => {
+ self.conditions.assumptions.extend(requires);
+ self.conditions.assertions.extend(provides);
+ }
+ }
+
+ // Record callsite. Instantiations do not constrain constructor/RHS terms.
+ let rhs = kind == TermKind::Constructor && matches!(invocation, Invocation::Caller);
+ self.record_term_instantiation(term, args.to_vec(), ret, !rhs)?;
+
+ Ok(())
+ }
+
+ /// Requires, matches and provides of a term's spec applied to the given
+ /// arguments and return value.
+ fn spec_clauses(
+ &mut self,
+ term_spec: &Spec,
+ kind: TermKind,
+ args: &[Symbolic],
+ ret: &Symbolic,
+ mut vars: Variables,
+ ) -> Result<(Vec<ExprId>, Vec<ExprId>, Vec<ExprId>)> {
+ // We are provided the arguments and return value as they appear
+ // syntactically in the term declaration and specification. However,
+ // whether these are the actual inputs and outputs of the corresponding
+ // function depends on the term kind.
+ let arguments: Vec<_> = zip(&term_spec.args, args).collect();
+ let result = (&term_spec.ret, ret);
+ let (inputs, outputs) = match kind {
+ TermKind::Constructor => (arguments.as_slice(), std::slice::from_ref(&result)),
+ TermKind::Extractor => (std::slice::from_ref(&result), arguments.as_slice()),
+ };
+
// Inputs are available to the requires and matches clauses.
for (name, input) in inputs {
vars.set(name.0.clone(), (*input).clone())?;
@@ -1477,39 +1611,7 @@ impl<'a> ConditionsBuilder<'a> {
}
}
- // Partial function.
- // REVIEW(mbm): pin down semantics for partial function specifications.
- if let Domain::Partial(p) = domain {
- // Matches describe when the function applies.
- let all_matches = self.all(matches);
- let eq = self.exprs_equal(p, all_matches);
- self.conditions.assumptions.push(eq);
-
- // Provides are conditioned on the match.
- let all_provides = self.all(provides);
- let provide = self.dedup_expr(Expr::Imp(all_matches, all_provides));
- provides = vec![provide];
- } else if !matches.is_empty() {
- bail!("spec matches on non-partial function");
- }
-
- // Assert/assume depending on caller or callee.
- match invocation {
- Invocation::Caller => {
- self.conditions.assertions.extend(requires);
- self.conditions.assumptions.extend(provides);
- }
- Invocation::Callee => {
- self.conditions.assumptions.extend(requires);
- self.conditions.assertions.extend(provides);
- }
- }
-
- // Record callsite. Instantiations do not constrain constructor/RHS terms.
- let rhs = kind == TermKind::Constructor && matches!(invocation, Invocation::Caller);
- self.record_term_instantiation(term, args.to_vec(), ret, !rhs)?;
-
- Ok(())
+ Ok((requires, matches, provides))
}
fn record_term_instantiation(
@@ -1956,6 +2058,10 @@ impl<'a> ConditionsBuilder<'a> {
spec::ExprKind::Let(defs, body) => self.spec_let(defs, body, vars),
+ spec::ExprKind::LaneMap(binders, body, lanes) => {
+ self.spec_lane_map(binders, body, *lanes, vars)
+ }
+
spec::ExprKind::With(decls, body) => self.spec_with(decls, body, vars),
spec::ExprKind::Expand(ident, args) => self.spec_expand(ident, args, vars),
@@ -1983,6 +2089,7 @@ impl<'a> ConditionsBuilder<'a> {
spec::ExprKind::FPToUBV(w, x) => binary_expr!(Expr::FPToUBV, w, x),
spec::ExprKind::FPToSBV(w, x) => binary_expr!(Expr::FPToSBV, w, x),
spec::ExprKind::WidthOf(x) => unary_expr!(Expr::WidthOf, x),
+ spec::ExprKind::LanesOf(x) => unary_expr!(Expr::LanesOf, x),
spec::ExprKind::As(x, ty) => {
let x = self.spec_expr(x, vars)?;
@@ -2270,6 +2377,42 @@ impl<'a> ConditionsBuilder<'a> {
self.spec_expr(body, &let_vars)
}
+ /// Lane mapping: each binder names a fresh lane variable, one lane of its
+ /// bit-vector, shadowing any outer variable of that name. The body is
+ /// evaluated once over each lane.
+ fn spec_lane_map(
+ &mut self,
+ binders: &[(Ident, spec::Expr)],
+ body: &spec::Expr,
+ lanes: Lanes,
+ vars: &Variables,
+ ) -> Result<Symbolic> {
+ if self.in_lane_map {
+ bail!("lane maps cannot nest");
+ }
+ let mut lane_vars = vars.clone();
+ let mut bound = Vec::new();
+ for (name, vector) in binders {
+ let vector = self.spec_expr(vector, vars)?;
+ let vector = self.as_scalar(vector)?;
+ let lane = self.alloc_variable(Type::BitVector(Width::Unknown), name.0.clone());
+ lane_vars.shadow(name.0.clone(), Symbolic::Scalar(lane));
+ bound.push((lane, vector));
+ }
+ let start = self.conditions.exprs.len();
+ self.in_lane_map = true;
+ let body = self.spec_expr(body, &lane_vars);
+ self.in_lane_map = false;
+ let body = self.as_scalar(body?)?;
+ let scope = start..self.conditions.exprs.len();
+ Ok(self.scalar(Expr::LaneMap {
+ binders: bound,
+ scope,
+ body,
+ lanes,
+ }))
+ }
+
fn spec_with(
&mut self,
decls: &[Ident],
@@ -2493,6 +2636,12 @@ impl<'a> ConditionsBuilder<'a> {
fn alloc_value(&mut self, ty: &Compound, name: String) -> Result<Symbolic> {
match ty {
Compound::Primitive(ty) => Ok(Symbolic::Scalar(self.alloc_variable(ty.clone(), name))),
+ Compound::Vector { .. } => {
+ let (width, lanes) = ty.as_vector().expect("vector");
+ let x = self.alloc_variable(Type::BitVector(Width::Bits(width)), name);
+ self.conditions.lane_counts.push((x, lanes));
+ Ok(Symbolic::Scalar(x))
+ }
Compound::Struct(fields) => Ok(Symbolic::Struct(
fields
.iter()
diff --git a/cranelift/isle/veri/veri/tests/filetests.rs b/cranelift/isle/veri/veri/tests/filetests.rs
index 98cf79071f..94f8c45b4c 100644
--- a/cranelift/isle/veri/veri/tests/filetests.rs
+++ b/cranelift/isle/veri/veri/tests/filetests.rs
@@ -43,8 +43,17 @@ fn pass(test_file: &str) {
#[file_tests(path = "filetests/broken", ext = "isle")]
fn broken(test_file: &str) {
- let (_, result) = run(test_file).expect("run should complete without errors");
+ let (summary, result) = run(test_file).expect("run should complete without errors");
result.expect_err("verification should fail");
+ // Fail by counterexample, not by an error in building the conditions.
+ assert!(
+ summary.failure > 0,
+ "expected at least one verification failure, got summary {summary:?}"
+ );
+ assert_eq!(
+ summary.expansion_errors, 0,
+ "expected no expansion errors, got summary {summary:?}"
+ );
}
/// Test files containing a term whose spec does not type check on its own.