Skip to content

Commit fa8c096

Browse files
committed
Addresses specs questions and added test to reflect
1 parent 3287cd6 commit fa8c096

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

llvm/lib/Analysis/ConstantFolding.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3845,13 +3845,12 @@ static Constant *ConstantFoldFixedVectorCall(
38453845
// sext 32 first, according to specs
38463846
APInt IMul = Elt0->getValue().sext(32) * Elt1->getValue().sext(32);
38473847

3848-
// TODO: imul in specs includes a modulo operation
3849-
// Is this performed automatically via trunc = true in APInt creation of *
3848+
// i16 -> i32 bypasses specs modulo on imul
38503849
MulVector.push_back(IMul);
38513850
}
3852-
for (unsigned I = 0; I < Result.size(); ++I) {
3853-
// Same case as with imul
3854-
APInt IAdd = MulVector[I] + MulVector[I + Result.size()];
3851+
for (unsigned I = 0; I < Result.size(); I++) {
3852+
// i16 -> i32 bypasses specs modulo on iadd
3853+
APInt IAdd = MulVector[I * 2] + MulVector[I * 2 + 1];
38553854
Result[I] = ConstantInt::get(Ty, IAdd);
38563855
}
38573856

llvm/test/Transforms/InstSimplify/ConstProp/WebAssembly/dot.ll

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ define <4 x i32> @dot_zero() {
1818
; a = 1 2 3 4 5 6 7 8
1919
; b = 1 2 3 4 5 6 7 8
2020
; k1|k2 = a * b = 1 4 9 16 25 36 49 64
21-
; k1 + k2 = (1+25) | (4+36) | (9+49) | (16+64)
22-
; result = 26 | 40 | 58 | 80
21+
; k1 + k2 = (1+4) | (9 + 16) | (25 + 36) | (49 + 64)
22+
; result = 5 | 25 | 61 | 113
2323
define <4 x i32> @dot_nonzero() {
2424
; CHECK-LABEL: define <4 x i32> @dot_nonzero() {
25-
; CHECK-NEXT: ret <4 x i32> <i32 26, i32 40, i32 58, i32 80>
25+
; CHECK-NEXT: ret <4 x i32> <i32 5, i32 25, i32 61, i32 113>
2626
;
2727
%res = tail call <4 x i32> @llvm.wasm.dot(<8 x i16> <i16 1, i16 2, i16 3, i16 4, i16 5, i16 6, i16 7, i16 8>, <8 x i16> <i16 1, i16 2, i16 3, i16 4, i16 5, i16 6, i16 7, i16 8>)
2828
ret <4 x i32> %res
@@ -36,4 +36,16 @@ define <4 x i32> @dot_doubly_negative() {
3636
ret <4 x i32> %res
3737
}
3838

39+
; This test checks for llvm's compliance on spec's wasm.dot's imul and iadd
40+
; Since the original number can only be i16::max == 2^15 - 1,
41+
; subsequent modulo of 2^32 of imul and iadd
42+
; should return the same result
43+
; 2*(2^15 - 1)^2 % 2^32 == 2*(2^15 - 1)^2
44+
define <4 x i32> @dot_follow_modulo_spec() {
45+
; CHECK-LABEL: define <4 x i32> @dot_follow_modulo_spec() {
46+
; CHECK-NEXT: ret <4 x i32> <i32 2147352578, i32 0, i32 0, i32 0>
47+
;
48+
%res = tail call <4 x i32> @llvm.wasm.dot(<8 x i16> <i16 32767, i16 32767, i16 0, i16 0, i16 0, i16 0, i16 0, i16 0>, <8 x i16> <i16 32767, i16 32767, i16 0, i16 0, i16 0, i16 0, i16 0, i16 0>)
49+
ret <4 x i32> %res
50+
}
3951

0 commit comments

Comments
 (0)