[PATCH] D140229: [SeparateConstOffsetFromGEP] Remove TypeSize error when collecting constant indices.

Paul Walker via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Dec 16 10:15:53 PST 2022


paulwalker-arm created this revision.
Herald added a subscriber: hiraditya.
Herald added a project: All.
paulwalker-arm requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D140229

Files:
  llvm/lib/Transforms/Scalar/SeparateConstOffsetFromGEP.cpp
  llvm/test/Transforms/SeparateConstOffsetFromGEP/AArch64/scalable-vector-geps.ll


Index: llvm/test/Transforms/SeparateConstOffsetFromGEP/AArch64/scalable-vector-geps.ll
===================================================================
--- /dev/null
+++ llvm/test/Transforms/SeparateConstOffsetFromGEP/AArch64/scalable-vector-geps.ll
@@ -0,0 +1,31 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt -S -passes=separate-const-offset-from-gep < %s | FileCheck %s
+
+target triple = "aarch64-linux-gnu"
+
+; Index is implicitly multiplied by vscale and so not really constant.
+define ptr @test1(ptr %base, i64 %idx) #0 {
+; CHECK-LABEL: @test1(
+; CHECK-NEXT:    [[IDX_NEXT:%.*]] = add nuw nsw i64 [[IDX:%.*]], 1
+; CHECK-NEXT:    [[GEP:%.*]] = getelementptr <vscale x 4 x float>, ptr [[BASE:%.*]], i64 [[IDX_NEXT]]
+; CHECK-NEXT:    ret ptr [[GEP]]
+;
+  %idx.next = add nuw nsw i64 %idx, 1
+  %gep = getelementptr <vscale x 4 x float>, ptr %base, i64 %idx.next
+  ret ptr %gep
+}
+
+; Whilst the first index is not constant, the calculation of the second index
+; does contain a constant that can be extracted.
+define ptr @test2(ptr %base, i64 %idx) {
+; CHECK-LABEL: @test2(
+; CHECK-NEXT:    [[TMP1:%.*]] = getelementptr <vscale x 4 x float>, ptr [[BASE:%.*]], i64 3, i64 [[IDX:%.*]]
+; CHECK-NEXT:    [[GEP2:%.*]] = getelementptr float, ptr [[TMP1]], i64 1
+; CHECK-NEXT:    ret ptr [[GEP2]]
+;
+  %idx.next = add nuw nsw i64 %idx, 1
+  %gep = getelementptr <vscale x 4 x float>, ptr %base, i64 3, i64 %idx.next
+  ret ptr %gep
+}
+
+attributes #0 = { "target-features"="+sve" }
Index: llvm/lib/Transforms/Scalar/SeparateConstOffsetFromGEP.cpp
===================================================================
--- llvm/lib/Transforms/Scalar/SeparateConstOffsetFromGEP.cpp
+++ llvm/lib/Transforms/Scalar/SeparateConstOffsetFromGEP.cpp
@@ -817,6 +817,12 @@
   gep_type_iterator GTI = gep_type_begin(*GEP);
   for (unsigned I = 1, E = GEP->getNumOperands(); I != E; ++I, ++GTI) {
     if (GTI.isSequential()) {
+      TypeSize AllocSize = DL->getTypeAllocSize(GTI.getIndexedType());
+
+      // Constant offsets of scalable types are not really constant.
+      if (AllocSize.isScalable())
+        continue;
+
       // Tries to extract a constant offset from this GEP index.
       int64_t ConstantOffset =
           ConstantOffsetExtractor::Find(GEP->getOperand(I), GEP, DT);
@@ -825,8 +831,7 @@
         // A GEP may have multiple indices.  We accumulate the extracted
         // constant offset to a byte offset, and later offset the remainder of
         // the original GEP with this byte offset.
-        AccumulativeByteOffset +=
-            ConstantOffset * DL->getTypeAllocSize(GTI.getIndexedType());
+        AccumulativeByteOffset += ConstantOffset * AllocSize;
       }
     } else if (LowerGEP) {
       StructType *StTy = GTI.getStructType();
@@ -1006,6 +1011,10 @@
   gep_type_iterator GTI = gep_type_begin(*GEP);
   for (unsigned I = 1, E = GEP->getNumOperands(); I != E; ++I, ++GTI) {
     if (GTI.isSequential()) {
+      // Constant offsets of scalable types are not really constant.
+      if (DL->getTypeAllocSize(GTI.getIndexedType()).isScalable())
+        continue;
+
       // Splits this GEP index into a variadic part and a constant offset, and
       // uses the variadic part as the new index.
       Value *OldIdx = GEP->getOperand(I);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D140229.483581.patch
Type: text/x-patch
Size: 3332 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221216/f41b4bae/attachment.bin>


More information about the llvm-commits mailing list