[PATCH] D105824: [LV] Avoid scalable vectorization for loops containing alloca

Kerry McLaughlin via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 12 09:09:30 PDT 2021


kmclaughlin created this revision.
kmclaughlin added reviewers: sdesmalen, dmgreen, CarolineConcatto, david-arm.
Herald added a subscriber: hiraditya.
kmclaughlin requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

This patch returns an Invalid cost from getInstructionCost() for alloca
instructions if the VF is scalable, as otherwise loops which contain
these instructions will crash when attempting to scalarize the alloca.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D105824

Files:
  llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
  llvm/test/Transforms/LoopVectorize/AArch64/scalable-alloca.ll


Index: llvm/test/Transforms/LoopVectorize/AArch64/scalable-alloca.ll
===================================================================
--- /dev/null
+++ llvm/test/Transforms/LoopVectorize/AArch64/scalable-alloca.ll
@@ -0,0 +1,50 @@
+; RUN: opt -S -loop-vectorize -mattr=+sve -mtriple aarch64-unknown-linux-gnu -scalable-vectorization=on < %s | FileCheck %s
+
+define void @alloca(i64 %N) {
+; CHECK-LABEL: @alloca(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    [[VLA:%.*]] = alloca i32*, i64 [[N:%.*]], align 8
+; CHECK:       vector.body:
+; CHECK-NEXT:    [[INDEX:%.*]] = phi i64 [ 0, %vector.ph ], [ [[INDEX_NEXT:%.*]], %vector.body ]
+; CHECK-NEXT:    [[INDUCTION:%.*]] = add i64 [[INDEX]], 0
+; CHECK-NEXT:    [[INDUCTION1:%.*]] = add i64 [[INDEX]], 1
+; CHECK-NEXT:    [[TMP0:%.*]] = alloca i32, align 16
+; CHECK-NEXT:    [[TMP1:%.*]] = alloca i32, align 16
+; CHECK-NEXT:    [[TMP2:%.*]] = getelementptr inbounds i32*, i32** [[VLA]], i64 [[INDUCTION]]
+; CHECK-NEXT:    [[TMP3:%.*]] = getelementptr inbounds i32*, i32** [[VLA]], i64 [[INDUCTION1]]
+; CHECK-NEXT:    store i32* [[TMP0]], i32** [[TMP2]], align 8
+; CHECK-NEXT:    store i32* [[TMP1]], i32** [[TMP3]], align 8
+; CHECK:       scalar.ph:
+; CHECK-NEXT:    [[BC_RESUME_VAL:%.*]] = phi i64 [ {{.*}}, %middle.block ], [ 0, %for.body.preheader ]
+; CHECK-NEXT:    br label %for.body
+; CHECK:       for.body:
+; CHECK-NEXT:    [[IV:%.*]] = phi i64 [ [[IV_NEXT:%.*]], %for.body ], [ [[BC_RESUME_VAL]], %scalar.ph ]
+; CHECK-NEXT:    [[ALLOCA:%.*]] = alloca i32, align 16
+; CHECK-NEXT:    [[ARRAYIDX:%.*]] = getelementptr inbounds i32*, i32** [[VLA]], i64 [[IV]]
+; CHECK-NEXT:    store i32* [[ALLOCA]], i32** [[ARRAYIDX]], align 8
+; CHECK:       for.end:
+; CHECK-NEXT:    call void @foo(i32** nonnull [[VLA]])
+; CHECK-NEXT:    ret void
+
+; CHECK-NOT: <vscale x
+
+entry:
+  %vla = alloca i32*, i64 %N, align 8
+  %cmp5 = icmp sgt i64 %N, 0
+  br i1 %cmp5, label %for.body, label %for.end
+
+for.body:
+  %iv = phi i64 [ %iv.next, %for.body ], [ 0, %entry ]
+  %alloca = alloca i32, align 16
+  %arrayidx = getelementptr inbounds i32*, i32** %vla, i64 %iv
+  store i32* %alloca, i32** %arrayidx, align 8
+  %iv.next = add nuw nsw i64 %iv, 1
+  %exitcond.not = icmp eq i64 %iv.next, %N
+  br i1 %exitcond.not, label %for.end, label %for.body
+
+for.end:
+  call void @foo(i32** nonnull %vla)
+  ret void
+}
+
+declare void @foo(i32**)
Index: llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
===================================================================
--- llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
+++ llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
@@ -7832,6 +7832,10 @@
   }
   case Instruction::ExtractValue:
     return TTI.getInstructionCost(I, TTI::TCK_RecipThroughput);
+  case Instruction::Alloca:
+    if (VF.isScalable())
+      return InstructionCost::getInvalid();
+    LLVM_FALLTHROUGH;
   default:
     // This opcode is unknown. Assume that it is the same as 'mul'.
     return TTI.getArithmeticInstrCost(Instruction::Mul, VectorTy, CostKind);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D105824.357961.patch
Type: text/x-patch
Size: 3038 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210712/fbcd81f8/attachment.bin>


More information about the llvm-commits mailing list