Skip to content

Commit 10d7dc9

Browse files
committed
Addresses nit, added negative test case
1 parent fa8c096 commit 10d7dc9

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

llvm/lib/Analysis/ConstantFolding.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3831,8 +3831,8 @@ static Constant *ConstantFoldFixedVectorCall(
38313831
unsigned NumElements =
38323832
cast<FixedVectorType>(Operands[0]->getType())->getNumElements();
38333833

3834-
assert(NumElements == 8 && NumElements / 2 == Result.size() &&
3835-
"wasm dot takes i16x8 and produce i32x4");
3834+
assert(NumElements == 8 && Result.size() == 4 &&
3835+
"wasm dot takes i16x8 and produces i32x4");
38363836
assert(Ty->isIntegerTy());
38373837
SmallVector<APInt, 8> MulVector;
38383838

@@ -3845,11 +3845,13 @@ static Constant *ConstantFoldFixedVectorCall(
38453845
// sext 32 first, according to specs
38463846
APInt IMul = Elt0->getValue().sext(32) * Elt1->getValue().sext(32);
38473847

3848-
// i16 -> i32 bypasses specs modulo on imul
3848+
// Multiplication can never be more than 32 bit.
3849+
// We can opt to not perform modulo of imul here.
38493850
MulVector.push_back(IMul);
38503851
}
38513852
for (unsigned I = 0; I < Result.size(); I++) {
3852-
// i16 -> i32 bypasses specs modulo on iadd
3853+
// Addition can never be more than 32 bit.
3854+
// We can opt to not perform modulo of iadd here.
38533855
APInt IAdd = MulVector[I * 2] + MulVector[I * 2 + 1];
38543856
Result[I] = ConstantInt::get(Ty, IAdd);
38553857
}

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,22 @@ define <4 x i32> @dot_doubly_negative() {
4141
; subsequent modulo of 2^32 of imul and iadd
4242
; should return the same result
4343
; 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() {
44+
define <4 x i32> @dot_follow_modulo_spec_1() {
45+
; CHECK-LABEL: define <4 x i32> @dot_follow_modulo_spec_1() {
4646
; CHECK-NEXT: ret <4 x i32> <i32 2147352578, i32 0, i32 0, i32 0>
4747
;
4848
%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>)
4949
ret <4 x i32> %res
5050
}
5151

52+
; This test checks for llvm's compliance on spec's wasm.dot's imul and iadd
53+
; 2*(- 2^15)^2 == 2^31, doesn't exceed 2^32 so we don't have to mod
54+
; wrapping around is -(2^31), still doesn't exceed 2^32
55+
define <4 x i32> @dot_follow_modulo_spec_2() {
56+
; CHECK-LABEL: define <4 x i32> @dot_follow_modulo_spec_2() {
57+
; CHECK-NEXT: ret <4 x i32> <i32 -2147483648, i32 0, i32 0, i32 0>
58+
;
59+
%res = tail call <4 x i32> @llvm.wasm.dot(<8 x i16> <i16 -32768, i16 -32768, i16 0, i16 0, i16 0, i16 0, i16 0, i16 0>, <8 x i16> <i16 -32768, i16 -32768, i16 0, i16 0, i16 0, i16 0, i16 0, i16 0>)
60+
ret <4 x i32> %res
61+
}
62+

0 commit comments

Comments
 (0)