[PATCH] D141613: [EarlyCSE] Fix crash when optimizing masked loads/stores

Fraser Cormack via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 12 09:34:12 PST 2023


This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rGd808ad822a5b: [EarlyCSE] Fix crash when optimizing masked loads/stores (authored by frasercrmck).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D141613/new/

https://reviews.llvm.org/D141613

Files:
  llvm/lib/Transforms/Scalar/EarlyCSE.cpp
  llvm/test/Transforms/EarlyCSE/opaque-ptr.ll


Index: llvm/test/Transforms/EarlyCSE/opaque-ptr.ll
===================================================================
--- llvm/test/Transforms/EarlyCSE/opaque-ptr.ll
+++ llvm/test/Transforms/EarlyCSE/opaque-ptr.ll
@@ -48,6 +48,38 @@
   ret i32 %sub
 }
 
+define i32 @different_elt_types_vector_load(ptr %p, <4 x i1> %c) {
+; CHECK-LABEL: @different_elt_types_vector_load(
+; CHECK-NEXT:    [[V1:%.*]] = call <4 x i32> @llvm.masked.load.v4i32.p0(ptr [[P:%.*]], i32 4, <4 x i1> [[C:%.*]], <4 x i32> poison)
+; CHECK-NEXT:    [[V2:%.*]] = call <4 x float> @llvm.masked.load.v4f32.p0(ptr [[P]], i32 4, <4 x i1> [[C]], <4 x float> poison)
+; CHECK-NEXT:    [[E1:%.*]] = extractelement <4 x i32> [[V1]], i32 0
+; CHECK-NEXT:    [[E2:%.*]] = extractelement <4 x float> [[V2]], i32 0
+; CHECK-NEXT:    [[E2I:%.*]] = fptosi float [[E2]] to i32
+; CHECK-NEXT:    [[SUM:%.*]] = add i32 [[E1]], [[E2I]]
+; CHECK-NEXT:    ret i32 [[SUM]]
+;
+  %v1 = call <4 x i32> @llvm.masked.load.v4i32.p(ptr %p, i32 4, <4 x i1> %c, <4 x i32> poison)
+  %v2 = call <4 x float> @llvm.masked.load.v4f32.p(ptr %p, i32 4, <4 x i1> %c, <4 x float> poison)
+  %e1 = extractelement <4 x i32> %v1, i32 0
+  %e2 = extractelement <4 x float> %v2, i32 0
+  %e2i = fptosi float %e2 to i32
+  %sum = add i32 %e1, %e2i
+  ret i32 %sum
+}
+
+define float @different_elt_types_vector_store_load(ptr %p, <4 x i32> %v1, <4 x i1> %c) {
+; CHECK-LABEL: @different_elt_types_vector_store_load(
+; CHECK-NEXT:    call void @llvm.masked.store.v4i32.p0(<4 x i32> [[V1:%.*]], ptr [[P:%.*]], i32 4, <4 x i1> [[C:%.*]])
+; CHECK-NEXT:    [[V2:%.*]] = call <4 x float> @llvm.masked.load.v4f32.p0(ptr [[P]], i32 4, <4 x i1> [[C]], <4 x float> poison)
+; CHECK-NEXT:    [[E2:%.*]] = extractelement <4 x float> [[V2]], i32 0
+; CHECK-NEXT:    ret float [[E2]]
+;
+  call void @llvm.masked.store.v4i32.p(<4 x i32> %v1, ptr %p, i32 4, <4 x i1> %c)
+  %v2 = call <4 x float> @llvm.masked.load.v4f32.p(ptr %p, i32 4, <4 x i1> %c, <4 x float> poison)
+  %e2 = extractelement <4 x float> %v2, i32 0
+  ret float %e2
+}
+
 define void @dse(ptr %p, i32 %i1, i8 %i2) {
 ; CHECK-LABEL: @dse(
 ; CHECK-NEXT:    store i32 [[I1:%.*]], ptr [[P:%.*]], align 4
@@ -61,3 +93,5 @@
 
 declare <4 x i32> @llvm.masked.load.v4i32.p(ptr, i32 immarg, <4 x i1>, <4 x i32>)
 declare <8 x i32> @llvm.masked.load.v8i32.p(ptr, i32 immarg, <8 x i1>, <8 x i32>)
+declare <4 x float> @llvm.masked.load.v4f32.p(ptr, i32 immarg, <4 x i1>, <4 x float>)
+declare void @llvm.masked.store.v4i32.p(<4 x i32>, ptr, i32 immarg, <4 x i1>)
Index: llvm/lib/Transforms/Scalar/EarlyCSE.cpp
===================================================================
--- llvm/lib/Transforms/Scalar/EarlyCSE.cpp
+++ llvm/lib/Transforms/Scalar/EarlyCSE.cpp
@@ -864,7 +864,7 @@
     // TODO: We could insert relevant casts on type mismatch here.
     if (auto *LI = dyn_cast<LoadInst>(Inst))
       return LI->getType() == ExpectedType ? LI : nullptr;
-    else if (auto *SI = dyn_cast<StoreInst>(Inst)) {
+    if (auto *SI = dyn_cast<StoreInst>(Inst)) {
       Value *V = SI->getValueOperand();
       return V->getType() == ExpectedType ? V : nullptr;
     }
@@ -877,11 +877,14 @@
 
   Value *getOrCreateResultNonTargetMemIntrinsic(IntrinsicInst *II,
                                                 Type *ExpectedType) const {
+    // TODO: We could insert relevant casts on type mismatch here.
     switch (II->getIntrinsicID()) {
     case Intrinsic::masked_load:
-      return II;
-    case Intrinsic::masked_store:
-      return II->getOperand(0);
+      return II->getType() == ExpectedType ? II : nullptr;
+    case Intrinsic::masked_store: {
+      Value *V = II->getOperand(0);
+      return V->getType() == ExpectedType ? V : nullptr;
+    }
     }
     return nullptr;
   }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D141613.488695.patch
Type: text/x-patch
Size: 3772 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230112/5d8ad4a6/attachment-0001.bin>


More information about the llvm-commits mailing list