[llvm] [WebAssembly] Constant fold wasm.dot (PR #149619)

Björn Pettersson via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 6 14:00:02 PDT 2025


================
@@ -3826,6 +3827,30 @@ static Constant *ConstantFoldFixedVectorCall(
     }
     return ConstantVector::get(Result);
   }
+  case Intrinsic::wasm_dot: {
+    unsigned NumElements =
+        cast<FixedVectorType>(Operands[0]->getType())->getNumElements();
+
+    assert(NumElements == 8 && Result.size() == 4 &&
+           "wasm dot takes i16x8 and produces i32x4");
+    assert(Ty->isIntegerTy());
+    int32_t MulVector[8];
+
+    for (unsigned I = 0; I < NumElements; ++I) {
+      ConstantInt *Elt0 =
+          cast<ConstantInt>(Operands[0]->getAggregateElement(I));
+      ConstantInt *Elt1 =
+          cast<ConstantInt>(Operands[1]->getAggregateElement(I));
+
+      MulVector[I] = Elt0->getSExtValue() * Elt1->getSExtValue();
+    }
+    for (unsigned I = 0; I < Result.size(); I++) {
+      int32_t IAdd = MulVector[I * 2] + MulVector[I * 2 + 1];
----------------
bjope wrote:

When building opt with ubsan this result in runtime errors when running the new dot.ll test:
```
../lib/Analysis/ConstantFolding.cpp:4011:39: runtime error: signed integer overflow: 1073741824 + 1073741824 cannot be represented in type 'int32_t' (aka 'int')
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior ../lib/Analysis/ConstantFolding.cpp:4011:39 
```

PS. The line numbers in the pull request does not match the final line numbers in the commit, but line 4011 in the output above seems to match with this line.

https://github.com/llvm/llvm-project/pull/149619


More information about the llvm-commits mailing list