[llvm] [VPlan] Lower safe uniform load to unconditional scalar load (Alt. approach) (PR #217564)

via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 20 02:57:28 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-vectorizers

Author: Shih-Po Hung (arcbbb)

<details>
<summary>Changes</summary>

When a load's address is uniform across VFs/UFs and provably safe to
access unconditionally, lower it to an unconditional scalar load recipe.
This avoids generating unnecessary masked loads or gathers for uniform memory
accesses.

Alternative approach to #<!-- -->216767.

---

Patch is 31.14 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/217564.diff


5 Files Affected:

- (modified) llvm/lib/Transforms/Vectorize/LoopVectorize.cpp (+1) 
- (modified) llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp (+33-1) 
- (modified) llvm/lib/Transforms/Vectorize/VPlanTransforms.h (+3) 
- (modified) llvm/test/Transforms/LoopVectorize/VPlan/vplan-print-before-after-all.ll (+1) 
- (added) llvm/test/Transforms/LoopVectorize/uniform-gather.ll (+413) 


``````````diff
diff --git a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
index c12a4f562f600..dc2388aa38668 100644
--- a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
+++ b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
@@ -6871,6 +6871,7 @@ VPlanPtr LoopVectorizationPlanner::tryToBuildVPlan(VPlanPtr Plan,
   // legal and profitable.
   RUN_VPLAN_PASS(VPlanTransforms::convertToStridedAccesses, *Plan, PSE,
                  *OrigLoop, CostCtx, Range);
+  RUN_VPLAN_PASS(VPlanTransforms::lowerSafeUniformLoads, *Plan, *TLI);
 
   // Ensure scalar VF plans only contain VF=1, as required by hasScalarVFOnly.
   if (Range.Start.isScalar())
diff --git a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
index e75e9cf4e9103..24d061ef2d287 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
@@ -5716,7 +5716,6 @@ void VPlanTransforms::convertToStridedAccesses(VPlan &Plan,
     for (VPRecipeBase &R : make_early_inc_range(*VPBB)) {
       auto *MemR = dyn_cast<VPWidenMemoryRecipe>(&R);
       // TODO: Transform reverse access into strided access with -1 stride.
-      // TODO: Transform gather/scatter with uniform address into strided access
       // with 0 stride.
       // TODO: Transform interleave access into multiple strided accesses.
       if (!MemR || MemR->isConsecutive())
@@ -5823,3 +5822,36 @@ void VPlanTransforms::convertToStridedAccesses(VPlan &Plan,
     }
   }
 }
+
+void VPlanTransforms::lowerSafeUniformLoads(VPlan &Plan,
+                                            const TargetLibraryInfo &TLI) {
+  for (VPBasicBlock *VPBB : VPBlockUtils::blocksOnly<VPBasicBlock>(
+           vp_depth_first_deep(Plan.getEntry()))) {
+    for (VPRecipeBase &R : make_early_inc_range(*VPBB)) {
+
+      auto *UniformGather = dyn_cast<VPWidenLoadRecipe>(&R);
+      if (!UniformGather ||
+          !vputils::isUniformAcrossVFsAndUFs(UniformGather->getAddr()))
+        continue;
+
+      auto &LI = cast<LoadInst>(UniformGather->getIngredient());
+      VPValue *Addr = UniformGather->getAddr();
+      Value *Underlying = Addr->getUnderlyingValue();
+      if (!Underlying ||
+          !isSafeToLoadUnconditionally(Underlying, LI.getType(), LI.getAlign(),
+                                       Plan.getDataLayout(), &LI,
+                                       /*AC=*/nullptr, /*DT=*/nullptr, &TLI))
+        continue;
+      DebugLoc DbgLoc = UniformGather->getDebugLoc();
+      auto *ScalarLoad =
+          new VPReplicateRecipe(&LI, {Addr}, /*IsSingleScalar=*/true,
+                                /*Mask=*/nullptr, {}, *UniformGather, DbgLoc);
+      ScalarLoad->insertBefore(UniformGather);
+      auto *Broadcast =
+          new VPInstruction(VPInstruction::Broadcast, {ScalarLoad});
+      Broadcast->insertBefore(UniformGather);
+      UniformGather->replaceAllUsesWith(Broadcast);
+      UniformGather->eraseFromParent();
+    }
+  }
+}
diff --git a/llvm/lib/Transforms/Vectorize/VPlanTransforms.h b/llvm/lib/Transforms/Vectorize/VPlanTransforms.h
index e1832cd2de27a..15f9e0988f69e 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanTransforms.h
+++ b/llvm/lib/Transforms/Vectorize/VPlanTransforms.h
@@ -352,6 +352,9 @@ struct VPlanTransforms {
   static void convertToStridedAccesses(VPlan &Plan,
                                        PredicatedScalarEvolution &PSE, Loop &L,
                                        VPCostContext &Ctx, VFRange &Range);
+  // Lower safe uniform (potentially masked) loads to unconditional scalar
+  // loads.
+  static void lowerSafeUniformLoads(VPlan &Plan, const TargetLibraryInfo &TLI);
 
   /// Remove dead recipes from \p Plan.
   static void removeDeadRecipes(VPlan &Plan);
diff --git a/llvm/test/Transforms/LoopVectorize/VPlan/vplan-print-before-after-all.ll b/llvm/test/Transforms/LoopVectorize/VPlan/vplan-print-before-after-all.ll
index a7271524f0191..ea6162d77f250 100644
--- a/llvm/test/Transforms/LoopVectorize/VPlan/vplan-print-before-after-all.ll
+++ b/llvm/test/Transforms/LoopVectorize/VPlan/vplan-print-before-after-all.ll
@@ -37,6 +37,7 @@
 ; CHECK: VPlan for loop in 'foo' [[BEFORE_OR_AFTER]] VPlanTransforms::convertToAbstractRecipes
 ; CHECK: VPlan for loop in 'foo' [[BEFORE_OR_AFTER]] VPlanTransforms::createInterleaveGroups
 ; CHECK: VPlan for loop in 'foo' [[BEFORE_OR_AFTER]] VPlanTransforms::convertToStridedAccesses
+; CHECK: VPlan for loop in 'foo' [[BEFORE_OR_AFTER]] VPlanTransforms::lowerSafeUniformLoads
 ; CHECK: VPlan for loop in 'foo' [[BEFORE_OR_AFTER]] VPlanTransforms::dropPoisonGeneratingRecipes
 ; CHECK: VPlan for loop in 'foo' [[BEFORE_OR_AFTER]] VPlanTransforms::hoistPredicatedLoads
 ; CHECK: VPlan for loop in 'foo' [[BEFORE_OR_AFTER]] VPlanTransforms::sinkPredicatedStores
diff --git a/llvm/test/Transforms/LoopVectorize/uniform-gather.ll b/llvm/test/Transforms/LoopVectorize/uniform-gather.ll
new file mode 100644
index 0000000000000..8ee6b2ea33c0f
--- /dev/null
+++ b/llvm/test/Transforms/LoopVectorize/uniform-gather.ll
@@ -0,0 +1,413 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --check-globals none --version 6
+; RUN: opt -S -passes=loop-vectorize -force-vector-width=2 -force-target-supports-gather-scatter-ops=true -tail-folding-policy=must-fold-tail %s 2>&1 | FileCheck %s --check-prefix=FIXED
+; RUN: opt -S -passes=loop-vectorize -force-vector-width=2 -force-target-supports-gather-scatter-ops=true -tail-folding-policy=must-fold-tail -force-target-supports-scalable-vectors -scalable-vectorization=on %s 2>&1 | FileCheck %s --check-prefix=SCALABLE
+
+ at log_table = internal constant [4 x i32] [i32 10, i32 20, i32 30, i32 40]
+ at exp_table = internal constant [4 x i32] [i32 1, i32 2, i32 3, i32 4]
+
+; Uniform, dereferenceable global variable.
+define void @global_table_load(ptr noalias %out, ptr noalias %cond, i64 %n) {
+; FIXED-LABEL: define void @global_table_load(
+; FIXED-SAME: ptr noalias [[OUT:%.*]], ptr noalias [[COND:%.*]], i64 [[N:%.*]]) {
+; FIXED-NEXT:  [[ENTRY:.*:]]
+; FIXED-NEXT:    br label %[[VECTOR_PH1:.*]]
+; FIXED:       [[VECTOR_PH1]]:
+; FIXED-NEXT:    [[N_RND_UP:%.*]] = add i64 [[N]], 1
+; FIXED-NEXT:    [[TMP0:%.*]] = and i64 [[N_RND_UP]], 1
+; FIXED-NEXT:    [[N_VEC:%.*]] = sub i64 [[N_RND_UP]], [[TMP0]]
+; FIXED-NEXT:    [[TRIP_COUNT_MINUS_1:%.*]] = sub i64 [[N]], 1
+; FIXED-NEXT:    [[BROADCAST_SPLATINSERT:%.*]] = insertelement <2 x i64> poison, i64 [[TRIP_COUNT_MINUS_1]], i64 0
+; FIXED-NEXT:    [[BROADCAST_SPLAT:%.*]] = shufflevector <2 x i64> [[BROADCAST_SPLATINSERT]], <2 x i64> poison, <2 x i32> zeroinitializer
+; FIXED-NEXT:    br label %[[VECTOR_BODY1:.*]]
+; FIXED:       [[VECTOR_BODY1]]:
+; FIXED-NEXT:    [[INDEX1:%.*]] = phi i64 [ 0, %[[VECTOR_PH1]] ], [ [[INDEX_NEXT:%.*]], %[[VECTOR_BODY1]] ]
+; FIXED-NEXT:    [[VEC_IND:%.*]] = phi <2 x i64> [ <i64 0, i64 1>, %[[VECTOR_PH1]] ], [ [[VEC_IND_NEXT:%.*]], %[[VECTOR_BODY1]] ]
+; FIXED-NEXT:    [[TMP1:%.*]] = icmp ule <2 x i64> [[VEC_IND]], [[BROADCAST_SPLAT]]
+; FIXED-NEXT:    [[TMP40:%.*]] = getelementptr inbounds i32, ptr [[COND]], i64 [[INDEX1]]
+; FIXED-NEXT:    [[WIDE_LOAD:%.*]] = call <2 x i32> @llvm.masked.load.v2i32.p0(ptr align 4 [[TMP40]], <2 x i1> [[TMP1]], <2 x i32> poison)
+; FIXED-NEXT:    [[TMP2:%.*]] = icmp ne <2 x i32> [[WIDE_LOAD]], zeroinitializer
+; FIXED-NEXT:    [[TMP6:%.*]] = load i32, ptr getelementptr inbounds nuw (i8, ptr @exp_table, i64 4), align 4
+; FIXED-NEXT:    [[BROADCAST_SPLATINSERT3:%.*]] = insertelement <2 x i32> poison, i32 [[TMP6]], i64 0
+; FIXED-NEXT:    [[WIDE_MASKED_GATHER:%.*]] = shufflevector <2 x i32> [[BROADCAST_SPLATINSERT3]], <2 x i32> poison, <2 x i32> zeroinitializer
+; FIXED-NEXT:    [[TMP7:%.*]] = load i32, ptr getelementptr inbounds nuw (i8, ptr @log_table, i64 8), align 4
+; FIXED-NEXT:    [[BROADCAST_SPLATINSERT1:%.*]] = insertelement <2 x i32> poison, i32 [[TMP7]], i64 0
+; FIXED-NEXT:    [[WIDE_MASKED_GATHER1:%.*]] = shufflevector <2 x i32> [[BROADCAST_SPLATINSERT1]], <2 x i32> poison, <2 x i32> zeroinitializer
+; FIXED-NEXT:    [[PREDPHI:%.*]] = select <2 x i1> [[TMP2]], <2 x i32> [[WIDE_MASKED_GATHER1]], <2 x i32> [[WIDE_MASKED_GATHER]]
+; FIXED-NEXT:    [[TMP4:%.*]] = getelementptr inbounds i32, ptr [[OUT]], i64 [[INDEX1]]
+; FIXED-NEXT:    call void @llvm.masked.store.v2i32.p0(<2 x i32> [[PREDPHI]], ptr align 4 [[TMP4]], <2 x i1> [[TMP1]])
+; FIXED-NEXT:    [[INDEX_NEXT]] = add i64 [[INDEX1]], 2
+; FIXED-NEXT:    [[VEC_IND_NEXT]] = add nuw <2 x i64> [[VEC_IND]], splat (i64 2)
+; FIXED-NEXT:    [[TMP5:%.*]] = icmp eq i64 [[INDEX_NEXT]], [[N_VEC]]
+; FIXED-NEXT:    br i1 [[TMP5]], label %[[PRED_STORE_IF27:.*]], label %[[VECTOR_BODY1]], !llvm.loop [[LOOP0:![0-9]+]]
+; FIXED:       [[PRED_STORE_IF27]]:
+; FIXED-NEXT:    br label %[[PRED_STORE_CONTINUE28:.*]]
+; FIXED:       [[PRED_STORE_CONTINUE28]]:
+; FIXED-NEXT:    ret void
+;
+; SCALABLE-LABEL: define void @global_table_load(
+; SCALABLE-SAME: ptr noalias [[OUT:%.*]], ptr noalias [[COND:%.*]], i64 [[N:%.*]]) {
+; SCALABLE-NEXT:  [[SCALAR_PH:.*:]]
+; SCALABLE-NEXT:    br label %[[VECTOR_PH:.*]]
+; SCALABLE:       [[VECTOR_PH]]:
+; SCALABLE-NEXT:    [[TMP0:%.*]] = call i64 @llvm.vscale.i64()
+; SCALABLE-NEXT:    [[TMP2:%.*]] = shl nuw i64 [[TMP0]], 1
+; SCALABLE-NEXT:    [[TMP10:%.*]] = sub i64 [[TMP2]], 1
+; SCALABLE-NEXT:    [[N_RND_UP:%.*]] = add i64 [[N]], [[TMP10]]
+; SCALABLE-NEXT:    [[N_MOD_VF:%.*]] = urem i64 [[N_RND_UP]], [[TMP2]]
+; SCALABLE-NEXT:    [[N_VEC:%.*]] = sub i64 [[N_RND_UP]], [[N_MOD_VF]]
+; SCALABLE-NEXT:    [[TRIP_COUNT_MINUS_1:%.*]] = sub i64 [[N]], 1
+; SCALABLE-NEXT:    [[BROADCAST_SPLATINSERT:%.*]] = insertelement <vscale x 2 x i64> poison, i64 [[TRIP_COUNT_MINUS_1]], i64 0
+; SCALABLE-NEXT:    [[BROADCAST_SPLAT:%.*]] = shufflevector <vscale x 2 x i64> [[BROADCAST_SPLATINSERT]], <vscale x 2 x i64> poison, <vscale x 2 x i32> zeroinitializer
+; SCALABLE-NEXT:    [[TMP3:%.*]] = call <vscale x 2 x i64> @llvm.stepvector.nxv2i64()
+; SCALABLE-NEXT:    [[BROADCAST_SPLATINSERT1:%.*]] = insertelement <vscale x 2 x i64> poison, i64 [[TMP2]], i64 0
+; SCALABLE-NEXT:    [[BROADCAST_SPLAT2:%.*]] = shufflevector <vscale x 2 x i64> [[BROADCAST_SPLATINSERT1]], <vscale x 2 x i64> poison, <vscale x 2 x i32> zeroinitializer
+; SCALABLE-NEXT:    br label %[[FOR_BODY:.*]]
+; SCALABLE:       [[FOR_BODY]]:
+; SCALABLE-NEXT:    [[IV:%.*]] = phi i64 [ 0, %[[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], %[[FOR_BODY]] ]
+; SCALABLE-NEXT:    [[VEC_IND:%.*]] = phi <vscale x 2 x i64> [ [[TMP3]], %[[VECTOR_PH]] ], [ [[VEC_IND_NEXT:%.*]], %[[FOR_BODY]] ]
+; SCALABLE-NEXT:    [[TMP11:%.*]] = icmp ule <vscale x 2 x i64> [[VEC_IND]], [[BROADCAST_SPLAT]]
+; SCALABLE-NEXT:    [[ARRAYIDX_COND:%.*]] = getelementptr inbounds i32, ptr [[COND]], i64 [[IV]]
+; SCALABLE-NEXT:    [[WIDE_LOAD:%.*]] = call <vscale x 2 x i32> @llvm.masked.load.nxv2i32.p0(ptr align 4 [[ARRAYIDX_COND]], <vscale x 2 x i1> [[TMP11]], <vscale x 2 x i32> poison)
+; SCALABLE-NEXT:    [[TMP4:%.*]] = icmp ne <vscale x 2 x i32> [[WIDE_LOAD]], zeroinitializer
+; SCALABLE-NEXT:    [[TMP9:%.*]] = load i32, ptr getelementptr inbounds nuw (i8, ptr @exp_table, i64 4), align 4
+; SCALABLE-NEXT:    [[BROADCAST_SPLATINSERT5:%.*]] = insertelement <vscale x 2 x i32> poison, i32 [[TMP9]], i64 0
+; SCALABLE-NEXT:    [[WIDE_MASKED_GATHER:%.*]] = shufflevector <vscale x 2 x i32> [[BROADCAST_SPLATINSERT5]], <vscale x 2 x i32> poison, <vscale x 2 x i32> zeroinitializer
+; SCALABLE-NEXT:    [[TMP8:%.*]] = load i32, ptr getelementptr inbounds nuw (i8, ptr @log_table, i64 8), align 4
+; SCALABLE-NEXT:    [[BROADCAST_SPLATINSERT3:%.*]] = insertelement <vscale x 2 x i32> poison, i32 [[TMP8]], i64 0
+; SCALABLE-NEXT:    [[WIDE_MASKED_GATHER1:%.*]] = shufflevector <vscale x 2 x i32> [[BROADCAST_SPLATINSERT3]], <vscale x 2 x i32> poison, <vscale x 2 x i32> zeroinitializer
+; SCALABLE-NEXT:    [[PREDPHI:%.*]] = select <vscale x 2 x i1> [[TMP4]], <vscale x 2 x i32> [[WIDE_MASKED_GATHER1]], <vscale x 2 x i32> [[WIDE_MASKED_GATHER]]
+; SCALABLE-NEXT:    [[TMP6:%.*]] = getelementptr inbounds i32, ptr [[OUT]], i64 [[IV]]
+; SCALABLE-NEXT:    call void @llvm.masked.store.nxv2i32.p0(<vscale x 2 x i32> [[PREDPHI]], ptr align 4 [[TMP6]], <vscale x 2 x i1> [[TMP11]])
+; SCALABLE-NEXT:    [[INDEX_NEXT]] = add i64 [[IV]], [[TMP2]]
+; SCALABLE-NEXT:    [[VEC_IND_NEXT]] = add nuw <vscale x 2 x i64> [[VEC_IND]], [[BROADCAST_SPLAT2]]
+; SCALABLE-NEXT:    [[TMP7:%.*]] = icmp eq i64 [[INDEX_NEXT]], [[N_VEC]]
+; SCALABLE-NEXT:    br i1 [[TMP7]], label %[[IF_END:.*]], label %[[FOR_BODY]], !llvm.loop [[LOOP0:![0-9]+]]
+; SCALABLE:       [[IF_END]]:
+; SCALABLE-NEXT:    br label %[[FOR_INC:.*]]
+; SCALABLE:       [[FOR_INC]]:
+; SCALABLE-NEXT:    ret void
+;
+entry:
+  br label %for.body
+
+for.body:
+  %iv = phi i64 [ 0, %entry ], [ %iv.next, %for.inc ]
+  %arrayidx.cond = getelementptr inbounds i32, ptr %cond, i64 %iv
+  %c = load i32, ptr %arrayidx.cond, align 4
+  %cmp = icmp ne i32 %c, 0
+  br i1 %cmp, label %if.then, label %if.else
+
+if.then:
+  %gep.log = getelementptr inbounds [4 x i32], ptr @log_table, i64 0, i64 2
+  %v.then = load i32, ptr %gep.log, align 4
+  br label %if.end
+
+if.else:
+  %gep.exp = getelementptr inbounds [4 x i32], ptr @exp_table, i64 0, i64 1
+  %v.else = load i32, ptr %gep.exp, align 4
+  br label %if.end
+
+if.end:
+  %v = phi i32 [ %v.then, %if.then ], [ %v.else, %if.else ]
+  %arrayidx.out = getelementptr inbounds i32, ptr %out, i64 %iv
+  store i32 %v, ptr %arrayidx.out, align 4
+  br label %for.inc
+
+for.inc:
+  %iv.next = add nuw nsw i64 %iv, 1
+  %exitcond = icmp eq i64 %iv.next, %n
+  br i1 %exitcond, label %exit, label %for.body
+
+exit:
+  ret void
+}
+
+; Uniform, dereferenceable(16) pointer parameter.
+; The access (bytes [12,16)) is inside the range.
+define void @deref_param_in_range(ptr noalias %out, ptr noalias %cond, ptr noalias nonnull align 4 dereferenceable(16) %table) nofree {
+; FIXED-LABEL: define void @deref_param_in_range(
+; FIXED-SAME: ptr noalias [[OUT:%.*]], ptr noalias [[COND:%.*]], ptr noalias nonnull align 4 dereferenceable(16) [[TABLE:%.*]]) #[[ATTR0:[0-9]+]] {
+; FIXED-NEXT:  [[ENTRY:.*:]]
+; FIXED-NEXT:    br label %[[VECTOR_PH:.*]]
+; FIXED:       [[VECTOR_PH]]:
+; FIXED-NEXT:    [[TMP0:%.*]] = getelementptr inbounds i32, ptr [[TABLE]], i64 3
+; FIXED-NEXT:    br label %[[VECTOR_BODY:.*]]
+; FIXED:       [[VECTOR_BODY]]:
+; FIXED-NEXT:    [[INDEX:%.*]] = phi i64 [ 0, %[[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], %[[VECTOR_BODY]] ]
+; FIXED-NEXT:    [[TMP1:%.*]] = getelementptr inbounds i32, ptr [[COND]], i64 [[INDEX]]
+; FIXED-NEXT:    [[WIDE_LOAD:%.*]] = load <2 x i32>, ptr [[TMP1]], align 4
+; FIXED-NEXT:    [[TMP2:%.*]] = icmp ne <2 x i32> [[WIDE_LOAD]], zeroinitializer
+; FIXED-NEXT:    [[TMP3:%.*]] = load i32, ptr [[TMP0]], align 4
+; FIXED-NEXT:    [[BROADCAST_SPLATINSERT:%.*]] = insertelement <2 x i32> poison, i32 [[TMP3]], i64 0
+; FIXED-NEXT:    [[TMP6:%.*]] = shufflevector <2 x i32> [[BROADCAST_SPLATINSERT]], <2 x i32> poison, <2 x i32> zeroinitializer
+; FIXED-NEXT:    [[PREDPHI:%.*]] = select <2 x i1> [[TMP2]], <2 x i32> [[TMP6]], <2 x i32> zeroinitializer
+; FIXED-NEXT:    [[TMP11:%.*]] = getelementptr inbounds i32, ptr [[OUT]], i64 [[INDEX]]
+; FIXED-NEXT:    store <2 x i32> [[PREDPHI]], ptr [[TMP11]], align 4
+; FIXED-NEXT:    [[INDEX_NEXT]] = add nuw i64 [[INDEX]], 2
+; FIXED-NEXT:    [[TMP12:%.*]] = icmp eq i64 [[INDEX_NEXT]], 16
+; FIXED-NEXT:    br i1 [[TMP12]], label %[[MIDDLE_BLOCK:.*]], label %[[VECTOR_BODY]], !llvm.loop [[LOOP3:![0-9]+]]
+; FIXED:       [[MIDDLE_BLOCK]]:
+; FIXED-NEXT:    br label %[[EXIT:.*]]
+; FIXED:       [[EXIT]]:
+; FIXED-NEXT:    ret void
+;
+; SCALABLE-LABEL: define void @deref_param_in_range(
+; SCALABLE-SAME: ptr noalias [[OUT:%.*]], ptr noalias [[COND:%.*]], ptr noalias nonnull align 4 dereferenceable(16) [[TABLE:%.*]]) #[[ATTR0:[0-9]+]] {
+; SCALABLE-NEXT:  [[VECTOR_PH:.*:]]
+; SCALABLE-NEXT:    br label %[[VECTOR_PH1:.*]]
+; SCALABLE:       [[VECTOR_PH1]]:
+; SCALABLE-NEXT:    [[TMP4:%.*]] = call i64 @llvm.vscale.i64()
+; SCALABLE-NEXT:    [[TMP2:%.*]] = shl nuw i64 [[TMP4]], 1
+; SCALABLE-NEXT:    [[TMP9:%.*]] = sub i64 [[TMP2]], 1
+; SCALABLE-NEXT:    [[N_RND_UP:%.*]] = add i64 16, [[TMP9]]
+; SCALABLE-NEXT:    [[N_MOD_VF:%.*]] = urem i64 [[N_RND_UP]], [[TMP2]]
+; SCALABLE-NEXT:    [[N_VEC:%.*]] = sub i64 [[N_RND_UP]], [[N_MOD_VF]]
+; SCALABLE-NEXT:    [[TMP3:%.*]] = getelementptr inbounds i32, ptr [[TABLE]], i64 3
+; SCALABLE-NEXT:    [[TMP10:%.*]] = call <vscale x 2 x i64> @llvm.stepvector.nxv2i64()
+; SCALABLE-NEXT:    [[BROADCAST_SPLATINSERT1:%.*]] = insertelement <vscale x 2 x i64> poison, i64 [[TMP2]], i64 0
+; SCALABLE-NEXT:    [[BROADCAST_SPLAT1:%.*]] = shufflevector <vscale x 2 x i64> [[BROADCAST_SPLATINSERT1]], <vscale x 2 x i64> poison, <vscale x 2 x i32> zeroinitializer
+; SCALABLE-NEXT:    br label %[[VECTOR_BODY:.*]]
+; SCALABLE:       [[VECTOR_BODY]]:
+; SCALABLE-NEXT:    [[INDEX:%.*]] = phi i64 [ 0, %[[VECTOR_PH1]] ], [ [[INDEX_NEXT:%.*]], %[[VECTOR_BODY]] ]
+; SCALABLE-NEXT:    [[VEC_IND:%.*]] = phi <vscale x 2 x i64> [ [[TMP10]], %[[VECTOR_PH1]] ], [ [[VEC_IND_NEXT:%.*]], %[[VECTOR_BODY]] ]
+; SCALABLE-NEXT:    [[TMP11:%.*]] = icmp ule <vscale x 2 x i64> [[VEC_IND]], splat (i64 15)
+; SCALABLE-NEXT:    [[TMP1:%.*]] = getelementptr inbounds i32, ptr [[COND]], i64 [[INDEX]]
+; SCALABLE-NEXT:    [[WIDE_LOAD:%.*]] = call <vscale x 2 x i32> @llvm.masked.load.nxv2i32.p0(ptr align 4 [[TMP1]], <vscale x 2 x i1> [[TMP11]], <vscale x 2 x i32> poison)
+; SCALABLE-NEXT:    [[TMP5:%.*]] = icmp ne <vscale x 2 x i32> [[WIDE_LOAD]], zeroinitializer
+; SCALABLE-NEXT:    [[TMP8:%.*]] = load i32, ptr [[TMP3]], align 4
+; SCALABLE-NEXT:    [[BROADCAST_SPLATINSERT2:%.*]] = insertelement <vscale x 2 x i32> poison, i32 [[TMP8]], i64 0
+; SCALABLE-NEXT:    [[WIDE_MASKED_GATHER:%.*]] = shufflevector <vscale x 2 x i32> [[BROADCAST_SPLATINSERT2]], <vscale x 2 x i32> poison, <vscale x 2 x i32> zeroinitializer
+; SCALABLE-NEXT:    [[PREDPHI:%.*]] = select <vscale x 2 x i1> [[TMP5]], <vscale x 2 x i32> [[WIDE_MASKED_GATHER]], <vscale x 2 x i32> zeroinitializer
+; SCALABLE-NEXT:    [[TMP6:%.*]] = getelementptr inbounds i32, ptr [[OUT]], i64 [[INDEX]]
+; SCALABLE-NEXT:    call void @llvm.masked.store.nxv2i32.p0(<vscale x 2 x i32> [[PREDPHI]], ptr align 4 [[TMP6]], <vscale x 2 x i1> [[TMP11]])
+; SCALABLE-NEXT:    [[INDEX_NEXT]] = add i64 [[INDEX]], [[TMP2]]
+; SCALABLE-NEXT:    [[VEC_IND_NEXT]] = add nuw <vscale x 2 x i64> [[VEC_IND]], [[BROADCAST_SPLAT1]]
+; SCALABLE-NEXT:    [[TMP7:%.*]] = icmp eq i64 [[INDEX_NEXT]], [[N_VEC]]
+; SCALABLE-NEXT:    br i1 [[TMP7]], label %[[MIDDLE_BLOCK:.*]], label %[[VECTOR_BODY]], !llvm.loop [[LOOP3:![0-9]+]]
+; SCALABLE:       [[MIDDLE_BLOCK]]:
+; SCALABLE-NEXT:    br label %[[EXIT:.*]]
+; SCALABLE:       [[EXIT]]:
+; SCALABLE-NEXT:    ret void
+;
+entry:
+  br label %for.body
+
+for.body:
+  %iv = phi i64 [ 0, %entry ], [ %iv.next, %for.inc ]
+  %arrayidx.cond = getelementptr inbounds i32, ptr %cond, i64 %iv
+  %c = load i32, ptr %arrayidx.cond, align 4
+  %cmp = icmp ne i32 %c, 0
+  br i1 %cmp, label %if.then, label %if.end
+
+if.then:
+  %gep.table = getelementptr inbounds i32, ptr %table, i64 3
+  %v.then = load i32, ptr %gep.table, align 4
+  br label %if.end
+
+if.end:
+  %v = phi i32 [ %v.then, %if.then ], [ 0, %for.body ]
+  %arrayidx.out = getelementptr inbounds i32, ptr %out, i64 %iv
+  store i32 %v, ptr %arrayidx.out, align 4
+  br label %for.inc
+
+for.inc:
+  %iv.next = add nuw nsw i64 %iv, 1
+  %exitcond = icmp eq i64 %iv.next, 16
+  br i1 %exitcond, label %exit, label %for.body
+
+exit:
+  ret void
+}
+
+; Uniform, dereferenceable(15) pointer parameter.
+; The last byte of the accessed i32 falls outside the range.
+define void @deref_param_out_of_range_last_byte(ptr noalias %out, ptr noalias %cond, ptr noalias nonnull align 4 dereferenceable(15) %table) nofree {
+; FIXED-LABEL: define void @deref_param_out_of_range_last_byte(
+; FIXED-SAME: ptr noalias [[OUT:%.*]], ptr noalias [[COND:%.*]], ptr noalias nonnull align 4 dereferenceable(15) [[TABLE:%.*]]) #[[ATTR0]] {
+; FIXED-NEXT:  [[ENTRY:.*:]]
+; FIXED-NEXT:    br label %[[VECTOR_PH:.*]]
+; FIXED:       [[VECTOR_PH]]:
+; FIXED-NEXT:    [[TMP0:%.*]] = getelementptr inbounds i32, ptr [[TABLE]], i64 3
+; FIXED-NEXT:    [[BROADCAST_SPLATINSERT:%.*]] = insertelement <2 x ptr> pois...
[truncated]

``````````

</details>


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


More information about the llvm-commits mailing list