Skip to content

Commit a8511db

Browse files
danylo1999V8-internal LUCI CQ
authored andcommitted
[wasm] Add float binary operations for relaxed SIMD
This adds relaxed_min and relaxed_max from the relaxed SIMD proposal. Bug: 427134596 Change-Id: I2b00d22e96feb03f740e3762b3dddb620eddd5ac Reviewed-on: https://chrome-internal-review.googlesource.com/c/v8/fuzzilli/+/8443159 Commit-Queue: Danylo Mocherniuk <[email protected]> Reviewed-by: Matthias Liedtke <[email protected]>
1 parent 0343370 commit a8511db

File tree

3 files changed

+48
-11
lines changed

3 files changed

+48
-11
lines changed

Sources/Fuzzilli/FuzzIL/WasmOperations.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1595,9 +1595,18 @@ public enum WasmSimd128FloatBinOpKind: Int, CaseIterable {
15951595
case pmin = 6
15961596
case pmax = 7
15971597

1598+
// f32x4: 0x100 + offset
1599+
// f64x2: 0x102 + offset
1600+
case relaxed_min = 13
1601+
case relaxed_max = 14
1602+
15981603
func isValidForShape(shape: WasmSimd128Shape) -> Bool {
15991604
return shape.isFloat()
16001605
}
1606+
1607+
func isRelaxed() -> Bool {
1608+
return self == .relaxed_min || self == .relaxed_max
1609+
}
16011610
}
16021611

16031612
final class WasmSimd128FloatBinOp: WasmOperation {
@@ -1611,6 +1620,15 @@ final class WasmSimd128FloatBinOp: WasmOperation {
16111620
self.binOpKind = binOpKind
16121621
super.init(numInputs: 2, numOutputs: 1, attributes: [.isMutable], requiredContext: [.wasmFunction])
16131622
}
1623+
1624+
func getOpcode() -> Int {
1625+
let base = if(binOpKind.isRelaxed()) {
1626+
shape == .f32x4 ? 0x100 : 0x102;
1627+
} else {
1628+
shape == .f32x4 ? 0xE4 : 0xF0;
1629+
}
1630+
return base + binOpKind.rawValue
1631+
}
16141632
}
16151633

16161634
final class WasmSimdSplat: WasmOperation {

Sources/Fuzzilli/Lifting/WasmLifter.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1918,9 +1918,8 @@ public class WasmLifter {
19181918
}
19191919
return Data([Prefix.Simd.rawValue]) + encoding
19201920
case .wasmSimd128FloatBinOp(let op):
1921-
assert(WasmSimd128FloatBinOpKind.allCases.count == 8, "New WasmSimd128FloatBinOpKind added: check if the encoding is still correct!")
1922-
let base = (op.shape == .f32x4) ? 0xE4 : 0xF0;
1923-
return Data([Prefix.Simd.rawValue]) + Leb128.unsignedEncode(base + op.binOpKind.rawValue) + Leb128.unsignedEncode(0x01)
1921+
assert(WasmSimd128FloatBinOpKind.allCases.count == 10, "New WasmSimd128FloatBinOpKind added: check if the encoding is still correct!")
1922+
return Data([Prefix.Simd.rawValue]) + Leb128.unsignedEncode(op.getOpcode()) + Leb128.unsignedEncode(0x01)
19241923
case .wasmSimd128Compare(let op):
19251924
assert(WasmIntegerCompareOpKind.allCases.count == 10, "New WasmIntegerCompareOpKind added: check if the encoding is still correct!")
19261925
assert(WasmFloatCompareOpKind.allCases.count == 6, "New WasmFloatCompareOpKind added: check if the encoding is still correct!")

Tests/FuzzilliTests/WasmTests.swift

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2271,7 +2271,7 @@ class WasmFoundationTests: XCTestCase {
22712271
let result = function.wasmSimd128FloatBinOp(varA, varB, WasmSimd128Shape.f32x4, WasmSimd128FloatBinOpKind.add)
22722272
return (0..<4).map {function.wasmSimdExtractLane(kind: WasmSimdExtractLane.Kind.F32x4, result, $0)}
22732273
}
2274-
},"4.8[0-9]*,7.4[0-9]*,9.6[0-9]*,11.8[0-9]*"), // for floating point math need to allow trailing digits
2274+
},"4.8[0-9]*,7.4[0-9]*,9.6[0-9]*,11.8[0-9]*"), // for floating point math need to allow trailing digits and take into account rounding issues
22752275
// Test float sub
22762276
({wasmModule in
22772277
let returnType = (0..<4).map {_ in ILType.wasmf32}
@@ -2281,7 +2281,7 @@ class WasmFoundationTests: XCTestCase {
22812281
let result = function.wasmSimd128FloatBinOp(varA, varB, WasmSimd128Shape.f32x4, WasmSimd128FloatBinOpKind.sub)
22822282
return (0..<4).map {function.wasmSimdExtractLane(kind: WasmSimdExtractLane.Kind.F32x4, result, $0)}
22832283
}
2284-
},"4.8[0-9]*,-4.4[0-9]*,4.4[0-9]*,4.4[0-9]*"), // for floating point math need to allow trailing digits
2284+
},"4.8[0-9]*,-4.4[0-9]*,4.4[0-9]*,4.4[0-9]*"),
22852285
// Test float mul
22862286
({wasmModule in
22872287
let returnType = (0..<4).map {_ in ILType.wasmf32}
@@ -2291,7 +2291,7 @@ class WasmFoundationTests: XCTestCase {
22912291
let result = function.wasmSimd128FloatBinOp(varA, varB, WasmSimd128Shape.f32x4, WasmSimd128FloatBinOpKind.mul)
22922292
return (0..<4).map {function.wasmSimdExtractLane(kind: WasmSimdExtractLane.Kind.F32x4, result, $0)}
22932293
}
2294-
},"0,8.85[0-9]*,(18.1[0-9]*|18.2[0-9]*),29.97[0-9]*"), // for floating point math need to allow trailing digits and take into account rounding issues
2294+
},"0,8.85[0-9]*,(18.1[0-9]*|18.2[0-9]*),29.97[0-9]*"),
22952295
// Test float div
22962296
({wasmModule in
22972297
let returnType = (0..<4).map {_ in ILType.wasmf32}
@@ -2301,7 +2301,7 @@ class WasmFoundationTests: XCTestCase {
23012301
let result = function.wasmSimd128FloatBinOp(varA, varB, WasmSimd128Shape.f32x4, WasmSimd128FloatBinOpKind.div)
23022302
return (0..<4).map {function.wasmSimdExtractLane(kind: WasmSimdExtractLane.Kind.F32x4, result, $0)}
23032303
}
2304-
},"0,0.2542[0-9]*,2.6923[0-9]*,2.1891[0-9]*"), // for floating point math need to allow trailing digits and take into account rounding issues
2304+
},"0,0.2542[0-9]*,2.6923[0-9]*,2.1891[0-9]*"),
23052305
// Test float min
23062306
({wasmModule in
23072307
let returnType = (0..<4).map {_ in ILType.wasmf32}
@@ -2311,7 +2311,7 @@ class WasmFoundationTests: XCTestCase {
23112311
let result = function.wasmSimd128FloatBinOp(varA, varB, WasmSimd128Shape.f32x4, WasmSimd128FloatBinOpKind.min)
23122312
return (0..<4).map {function.wasmSimdExtractLane(kind: WasmSimdExtractLane.Kind.F32x4, result, $0)}
23132313
}
2314-
},"0,1.5[0-9]*,NaN,NaN"), // for floating point math need to allow trailing digits and take into account rounding issues
2314+
},"0,1.5[0-9]*,NaN,NaN"),
23152315
// Test float max
23162316
({wasmModule in
23172317
let returnType = (0..<4).map {_ in ILType.wasmf32}
@@ -2321,7 +2321,7 @@ class WasmFoundationTests: XCTestCase {
23212321
let result = function.wasmSimd128FloatBinOp(varA, varB, WasmSimd128Shape.f32x4, WasmSimd128FloatBinOpKind.max)
23222322
return (0..<4).map {function.wasmSimdExtractLane(kind: WasmSimdExtractLane.Kind.F32x4, result, $0)}
23232323
}
2324-
},"4.8[0-9]*,5.9[0-9]*,NaN,NaN"), // for floating point math need to allow trailing digits and take into account rounding issues
2324+
},"4.8[0-9]*,5.9[0-9]*,NaN,NaN"),
23252325
// Test float pmin
23262326
({wasmModule in
23272327
let returnType = (0..<4).map {_ in ILType.wasmf32}
@@ -2331,7 +2331,7 @@ class WasmFoundationTests: XCTestCase {
23312331
let result = function.wasmSimd128FloatBinOp(varA, varB, WasmSimd128Shape.f32x4, WasmSimd128FloatBinOpKind.pmin)
23322332
return (0..<4).map {function.wasmSimdExtractLane(kind: WasmSimdExtractLane.Kind.F32x4, result, $0)}
23332333
}
2334-
},"0,1.5[0-9]*,7,NaN"), // for floating point math need to allow trailing digits and take into account rounding issues
2334+
},"0,1.5[0-9]*,7,NaN"),
23352335
// Test float pmax
23362336
({wasmModule in
23372337
let returnType = (0..<4).map {_ in ILType.wasmf32}
@@ -2341,7 +2341,27 @@ class WasmFoundationTests: XCTestCase {
23412341
let result = function.wasmSimd128FloatBinOp(varA, varB, WasmSimd128Shape.f32x4, WasmSimd128FloatBinOpKind.pmax)
23422342
return (0..<4).map {function.wasmSimdExtractLane(kind: WasmSimdExtractLane.Kind.F32x4, result, $0)}
23432343
}
2344-
},"4.8[0-9]*,5.9[0-9]*,7,NaN"), // for floating point math need to allow trailing digits and take into account rounding issues
2344+
},"4.8[0-9]*,5.9[0-9]*,7,NaN"),
2345+
// Test float relaxed_min
2346+
({wasmModule in
2347+
let returnType = (0..<4).map {_ in ILType.wasmf32}
2348+
wasmModule.addWasmFunction(with: [] => returnType) { function, label, args in
2349+
let varA = function.constSimd128(value: floatToByteArray([0.0, 5.9, 7.0, 0.0 / 0.0]))
2350+
let varB = function.constSimd128(value: floatToByteArray([4.8, 1.5, 0.0 / 0.0, 3.7]))
2351+
let result = function.wasmSimd128FloatBinOp(varA, varB, WasmSimd128Shape.f32x4, WasmSimd128FloatBinOpKind.relaxed_min)
2352+
return (0..<4).map {function.wasmSimdExtractLane(kind: WasmSimdExtractLane.Kind.F32x4, result, $0)}
2353+
}
2354+
},"0,1.5[0-9]*,(NaN|7),(NaN|3.7[0-9]*)"),
2355+
// Test float relaxed_max
2356+
({wasmModule in
2357+
let returnType = (0..<4).map {_ in ILType.wasmf32}
2358+
wasmModule.addWasmFunction(with: [] => returnType) { function, label, args in
2359+
let varA = function.constSimd128(value: floatToByteArray([0.0, 5.9, 7.0, 0.0 / 0.0]))
2360+
let varB = function.constSimd128(value: floatToByteArray([4.8, 1.5, 0.0 / 0.0, 3.7]))
2361+
let result = function.wasmSimd128FloatBinOp(varA, varB, WasmSimd128Shape.f32x4, WasmSimd128FloatBinOpKind.relaxed_max)
2362+
return (0..<4).map {function.wasmSimdExtractLane(kind: WasmSimdExtractLane.Kind.F32x4, result, $0)}
2363+
}
2364+
},"4.8[0-9]*,5.9[0-9]*,(NaN|7),(NaN|3.7[0-9]*)"),
23452365

23462366

23472367
]

0 commit comments

Comments
 (0)