[PATCH] D135957: [AArch64][SeperateConstOffsetFromGEP] Prevent pass from splitting GEP if an index has more than one use

Zain Jaffal via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 14 04:11:16 PDT 2022


zjaffal created this revision.
zjaffal added reviewers: fhahn, t.p.northover, spatel, dmgreen.
Herald added subscribers: arphaman, hiraditya, kristof.beyls.
Herald added a project: All.
zjaffal requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

This fixes a 5% performance regression in CPython for some inputs by fixing codegen for any_find_slice in unicodeobject.c
https://github.com/python/cpython/blob/main/Objects/unicodeobject.c#L8748

When enabling -aarch64-enable-gep-opt the transform splits GEP instruction generating unecessary instructions.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D135957

Files:
  llvm/lib/Transforms/Scalar/SeparateConstOffsetFromGEP.cpp
  llvm/test/CodeGen/AArch64/aarch64-loop-gep-opt.ll


Index: llvm/test/CodeGen/AArch64/aarch64-loop-gep-opt.ll
===================================================================
--- llvm/test/CodeGen/AArch64/aarch64-loop-gep-opt.ll
+++ llvm/test/CodeGen/AArch64/aarch64-loop-gep-opt.ll
@@ -48,3 +48,30 @@
   br label %do.body.i.backedge
 }
 
+
+define i64 @test_loop2(i64* %arg, i64* %save) {
+bb:
+; CHECK-LABEL: bb:
+; CHECK %tmp1 = load i64, i64* null, align 8
+; CHECK %tmp22 = add nsw i64 %tmp1, -1
+; CHECK-NOT %uglygep = getelementptr i8, i8* %0, i64 %1
+; CHECK-NOT %uglygep2 = getelementptr i8, i8* %uglygep, i64 -8
+
+  %tmp1 = load i64, i64* null, align 8
+  %tmp22 = add nsw i64 %tmp1, -1
+  %tmp23 = getelementptr inbounds i64, i64* %arg, i64 %tmp22
+  %tmp24 = load i64, i64* %tmp23, align 2
+  br label %bb25
+
+bb25:                                             ; preds = %bb25, %bb18
+  %tmp26 = phi i64 [ 1, %bb ], [ 0, %bb25 ]
+  %tmp29 = icmp eq i64 0, %tmp24
+  %tmp30 = select i1 %tmp29, i64 0, i64 %tmp26
+  store i64 %tmp30, i64* %save
+  %tmp31 = icmp eq i64 0, %tmp22
+  br i1 %tmp31, label %bb32, label %bb25
+
+bb32:                                             ; preds = %bb25
+  ret i64 0
+
+}
\ No newline at end of file
Index: llvm/lib/Transforms/Scalar/SeparateConstOffsetFromGEP.cpp
===================================================================
--- llvm/lib/Transforms/Scalar/SeparateConstOffsetFromGEP.cpp
+++ llvm/lib/Transforms/Scalar/SeparateConstOffsetFromGEP.cpp
@@ -1003,6 +1003,15 @@
   // one. If LowerGEP is enabled, a structure index is accumulated in the
   // constant offset. LowerToSingleIndexGEPs or lowerToArithmetics will later
   // handle the constant offset and won't need a new structure index.
+
+  // If GEP only has one index and it is used by non-gep instructions,
+  // it is not benificial to do the split.
+  if (GEP->getNumOperands() == 2) {
+    Value *Idx = GEP->getOperand(1);
+    if (!llvm::all_of(Idx->users(),
+                      [](const User *U) { return isa<GetElementPtrInst>(U); }))
+      return false;
+  }
   gep_type_iterator GTI = gep_type_begin(*GEP);
   for (unsigned I = 1, E = GEP->getNumOperands(); I != E; ++I, ++GTI) {
     if (GTI.isSequential()) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D135957.467737.patch
Type: text/x-patch
Size: 2192 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221014/603e8c71/attachment.bin>


More information about the llvm-commits mailing list