[PATCH] D118696: Fix incorrect TypeSize->uint64_t cast in InductionDescriptor::isInductionPHI

David Sherwood via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 1 05:24:29 PST 2022


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

The code was relying upon the implicit conversion of TypeSize to
uint64_t and assuming the type in question was always fixed. However,
I discovered an issue when running the canon-freeze pass with some
IR loops that contains scalable vector types. I've changed the code
to bail out if the size is unknown at compile time, since we cannot
compute whether the step is a multiple of the type size or not.

I added a test here:

  Transforms/CanonicalizeFreezeInLoops/phis.ll


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D118696

Files:
  llvm/lib/Analysis/IVDescriptors.cpp
  llvm/test/Transforms/CanonicalizeFreezeInLoops/phis.ll


Index: llvm/test/Transforms/CanonicalizeFreezeInLoops/phis.ll
===================================================================
--- llvm/test/Transforms/CanonicalizeFreezeInLoops/phis.ll
+++ llvm/test/Transforms/CanonicalizeFreezeInLoops/phis.ll
@@ -112,3 +112,37 @@
 exit:
   ret void
 }
+
+; Negative test - ensure we handle scalable vector types correctly
+define void @no_freeze_scalable(<vscale x 4 x float>* %ptr) {
+; CHECK-LABEL: @no_freeze_scalable(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    br label [[FOR_BODY:%.*]]
+; CHECK:       for.body:
+; CHECK-NEXT:    [[LSR_IV1:%.*]] = phi <vscale x 4 x float>* [ [[TMP0:%.*]], [[FOR_BODY]] ], [ [[PTR:%.*]], [[ENTRY:%.*]] ]
+; CHECK-NEXT:    [[J_0117:%.*]] = phi i64 [ [[INC:%.*]], [[FOR_BODY]] ], [ 0, [[ENTRY]] ]
+; CHECK-NEXT:    [[LSR_IV12:%.*]] = bitcast <vscale x 4 x float>* [[LSR_IV1]] to i8*
+; CHECK-NEXT:    [[INC]] = add nuw nsw i64 [[J_0117]], 1
+; CHECK-NEXT:    [[UGLYGEP:%.*]] = getelementptr i8, i8* [[LSR_IV12]], i64 4
+; CHECK-NEXT:    [[TMP0]] = bitcast i8* [[UGLYGEP]] to <vscale x 4 x float>*
+; CHECK-NEXT:    [[CMP:%.*]] = icmp ne i64 [[INC]], 1024
+; CHECK-NEXT:    br i1 [[CMP]], label [[FOR_BODY]], label [[END:%.*]]
+; CHECK:       end:
+; CHECK-NEXT:    ret void
+;
+entry:
+  br label %for.body
+
+for.body:                                         ; preds = %for.body, %entry
+  %lsr.iv1 = phi <vscale x 4 x float>* [ %0, %for.body ], [ %ptr, %entry ]
+  %j.0117 = phi i64 [ %inc, %for.body ], [ 0, %entry ]
+  %lsr.iv12 = bitcast <vscale x 4 x float>* %lsr.iv1 to i8*
+  %inc = add nuw nsw i64 %j.0117, 1
+  %uglygep = getelementptr i8, i8* %lsr.iv12, i64 4
+  %0 = bitcast i8* %uglygep to <vscale x 4 x float>*
+  %cmp = icmp ne i64 %inc, 1024
+  br i1 %cmp, label %for.body, label %end
+
+end:                                              ; preds = %for.body
+  ret void
+}
Index: llvm/lib/Analysis/IVDescriptors.cpp
===================================================================
--- llvm/lib/Analysis/IVDescriptors.cpp
+++ llvm/lib/Analysis/IVDescriptors.cpp
@@ -1428,10 +1428,11 @@
 
   ConstantInt *CV = ConstStep->getValue();
   const DataLayout &DL = Phi->getModule()->getDataLayout();
-  int64_t Size = static_cast<int64_t>(DL.getTypeAllocSize(ElementType));
-  if (!Size)
+  TypeSize TySize = DL.getTypeAllocSize(ElementType);
+  if (TySize.isZero() || TySize.isScalable())
     return false;
 
+  int64_t Size = static_cast<int64_t>(TySize.getFixedSize());
   int64_t CVSize = CV->getSExtValue();
   if (CVSize % Size)
     return false;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D118696.404891.patch
Type: text/x-patch
Size: 2539 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220201/2cc18f19/attachment.bin>


More information about the llvm-commits mailing list