[llvm] 51a9382 - [WASM] Fix legalizer for LowerBUILD_VECTOR.
Peter Rong via llvm-commits
llvm-commits at lists.llvm.org
Thu Mar 30 19:20:10 PDT 2023
Author: Peter Rong
Date: 2023-03-30T19:20:04-07:00
New Revision: 51a93828d782dae09f2dc69aae53f4e6dd1ca98a
URL: https://github.com/llvm/llvm-project/commit/51a93828d782dae09f2dc69aae53f4e6dd1ca98a
DIFF: https://github.com/llvm/llvm-project/commit/51a93828d782dae09f2dc69aae53f4e6dd1ca98a.diff
LOG: [WASM] Fix legalizer for LowerBUILD_VECTOR.
Constants in BUILD_VECTOR may be down cast into a smaller value that fits LaneBits, i.e., the bit width of elements in the vector.
This cast didn't consider 2^N where it would be cast into -2^N, which still doesn't fit into LaneBits after casting.
This will cause an assertion in later legalization.
2^N should be cast into 0, and this patch reflects such behavior.
This patch also includes a test to reflect the fix.
This patch fixes [issue 61780](https://github.com/llvm/llvm-project/issues/61780)
Related patch: https://reviews.llvm.org/D108669
Reviewed By: tlively
Differential Revision: https://reviews.llvm.org/D147208
Added:
llvm/test/CodeGen/WebAssembly/simd-pr61780.ll
Modified:
llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp
Removed:
################################################################################
diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp b/llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp
index 2d352705e42bf..892478e3d706b 100644
--- a/llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp
+++ b/llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp
@@ -2151,7 +2151,8 @@ SDValue WebAssemblyTargetLowering::LowerBUILD_VECTOR(SDValue Op,
assert((LaneBits == 64 || Val >= -(1ll << (LaneBits - 1))) &&
"Unexpected out of bounds negative value");
if (Const && LaneBits != 64 && Val > (1ll << (LaneBits - 1)) - 1) {
- auto NewVal = ((uint64_t)Val % (1ll << LaneBits)) - (1ll << LaneBits);
+ uint64_t Mask = (1ll << LaneBits) - 1;
+ auto NewVal = (((uint64_t)Val & Mask) - (1ll << LaneBits)) & Mask;
ConstLanes.push_back(DAG.getConstant(NewVal, SDLoc(Lane), LaneT));
} else {
ConstLanes.push_back(Lane);
diff --git a/llvm/test/CodeGen/WebAssembly/simd-pr61780.ll b/llvm/test/CodeGen/WebAssembly/simd-pr61780.ll
new file mode 100644
index 0000000000000..d9a9d05df39f0
--- /dev/null
+++ b/llvm/test/CodeGen/WebAssembly/simd-pr61780.ll
@@ -0,0 +1,46 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
+; RUN: llc < %s -mtriple=wasm32 -mattr=+simd128 -opaque-pointers | FileCheck %s
+
+define void @f(ptr %0, ptr %pr) {
+; CHECK-LABEL: f:
+; CHECK: .functype f (i32, i32) -> ()
+; CHECK-NEXT: .local v128
+; CHECK-NEXT: # %bb.0: # %BB
+; CHECK-NEXT: local.get 1
+; CHECK-NEXT: local.get 2
+; CHECK-NEXT: i32.const 16
+; CHECK-NEXT: local.get 0
+; CHECK-NEXT: v128.load64_zero 0
+; CHECK-NEXT: v128.const 0, 1, 0, 0
+; CHECK-NEXT: i32x4.gt_u
+; CHECK-NEXT: local.tee 2
+; CHECK-NEXT: i32x4.extract_lane 0
+; CHECK-NEXT: i32.const 1
+; CHECK-NEXT: i32.and
+; CHECK-NEXT: i32.shr_u
+; CHECK-NEXT: local.tee 0
+; CHECK-NEXT: local.get 0
+; CHECK-NEXT: i32.mul
+; CHECK-NEXT: i8x16.replace_lane 0
+; CHECK-NEXT: i32.const 16
+; CHECK-NEXT: local.get 2
+; CHECK-NEXT: i32x4.extract_lane 1
+; CHECK-NEXT: i32.const 1
+; CHECK-NEXT: i32.and
+; CHECK-NEXT: i32.shr_u
+; CHECK-NEXT: local.tee 0
+; CHECK-NEXT: local.get 0
+; CHECK-NEXT: i32.mul
+; CHECK-NEXT: i8x16.replace_lane 1
+; CHECK-NEXT: v128.store16_lane 0, 0
+; CHECK-NEXT: # fallthrough-return
+BB:
+ %v0 = load <2 x i32>, ptr %0
+ %v1 = icmp ugt <2 x i32> %v0, <i32 0, i32 1>
+ %v2 = zext <2 x i1> %v1 to <2 x i8>
+ %v3 = ashr <2 x i8> <i8 16, i8 16>, %v2
+ %v4 = mul <2 x i8> %v3, %v3
+ store <2 x i8> %v4, ptr %pr
+ ret void
+}
+
More information about the llvm-commits
mailing list