[PATCH] D147208: [WASM] Fix legalizer for LowerBUILD_VECTOR.

Peter Rong via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 30 01:44:33 PDT 2023


Peter created this revision.
Herald added subscribers: pmatos, asb, sunfish, hiraditya, jgravelle-google, sbc100, dschuff.
Herald added a project: All.
Peter requested review of this revision.
Herald added subscribers: llvm-commits, aheejin.
Herald added a project: LLVM.

Constants in BUILD_VECTOR may be downcasted 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 casted into -2^N, which still doesn't fit into LaneBits after casting.
This will cause an assertion in later legalization.

2^N should be casted 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/D147198


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D147208

Files:
  llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp
  llvm/test/CodeGen/WebAssembly/simd-pr61780.ll


Index: llvm/test/CodeGen/WebAssembly/simd-pr61780.ll
===================================================================
--- /dev/null
+++ llvm/test/CodeGen/WebAssembly/simd-pr61780.ll
@@ -0,0 +1,14 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
+; RUN: llc < %s -mtriple=wasm32 -mattr=+simd128 -opaque-pointers 
+
+define void @f(ptr %0, ptr %pr) {
+BB:
+  %v0 = load <4 x i32>, ptr %0
+  %v1 = icmp ugt <4 x i32> %v0, <i32 0, i32 1, i32 2, i32 3>
+  %v2 = zext <4 x i1> %v1 to <4 x i8>
+  %v3 = ashr <4 x i8> <i8 16, i8 16, i8 16, i8 16>, %v2
+  %v4 = mul <4 x i8> %v3, %v3
+  store <4 x i8> %v4, ptr %pr
+  ret void
+}
+
Index: llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp
===================================================================
--- llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp
+++ llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp
@@ -2151,7 +2151,8 @@
         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);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D147208.509574.patch
Type: text/x-patch
Size: 1485 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230330/6e2c694e/attachment.bin>


More information about the llvm-commits mailing list