[llvm] 243df83 - [LICM] Fix assert failure in no-allowspeculation mode

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 11 02:56:03 PDT 2023


Author: Nikita Popov
Date: 2023-04-11T11:55:54+02:00
New Revision: 243df834c6ae8b80441d8348368935b253c1be45

URL: https://github.com/llvm/llvm-project/commit/243df834c6ae8b80441d8348368935b253c1be45
DIFF: https://github.com/llvm/llvm-project/commit/243df834c6ae8b80441d8348368935b253c1be45.diff

LOG: [LICM] Fix assert failure in no-allowspeculation mode

In this case the source GEP might not be hoisted even though it
has invariant operands. For now just bail out, but we might need
additional checks for AllowSpeculation in these special-case
reassociation folds.

Added: 
    

Modified: 
    llvm/lib/Transforms/Scalar/LICM.cpp
    llvm/test/Transforms/LICM/gep-reassociate.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Scalar/LICM.cpp b/llvm/lib/Transforms/Scalar/LICM.cpp
index 8013bb828e80..40190f38cc75 100644
--- a/llvm/lib/Transforms/Scalar/LICM.cpp
+++ b/llvm/lib/Transforms/Scalar/LICM.cpp
@@ -2514,8 +2514,12 @@ static bool hoistGEP(Instruction &I, Loop &L, ICFLoopSafetyInfo &SafetyInfo,
   if (!L.isLoopInvariant(SrcPtr) || !all_of(GEP->indices(), LoopInvariant))
     return false;
 
-  assert(!all_of(Src->indices(), LoopInvariant) &&
-         "Would have been hoisted already");
+  // This can only happen if !AllowSpeculation, otherwise this would already be
+  // handled.
+  // FIXME: Should we respect AllowSpeculation in these reassociation folds?
+  // The flag exists to prevent metadata dropping, which is not relevant here.
+  if (all_of(Src->indices(), LoopInvariant))
+    return false;
 
   // The swapped GEPs are inbounds if both original GEPs are inbounds
   // and the sign of the offsets is the same. For simplicity, only

diff  --git a/llvm/test/Transforms/LICM/gep-reassociate.ll b/llvm/test/Transforms/LICM/gep-reassociate.ll
index 85329746d2a8..630a751999c4 100644
--- a/llvm/test/Transforms/LICM/gep-reassociate.ll
+++ b/llvm/test/Transforms/LICM/gep-reassociate.ll
@@ -1,5 +1,6 @@
 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 2
-; RUN: opt -S -passes=licm < %s | FileCheck %s
+; RUN: opt -S -passes=licm < %s | FileCheck %s --check-prefixes=CHECK,SPEC
+; RUN: opt -S -passes='licm<no-allowspeculation>' < %s | FileCheck %s --check-prefixes=CHECK,NOSPEC
 
 declare void @use(ptr)
 declare i32 @get.i32()
@@ -387,3 +388,55 @@ loop:
 exit:
   ret void
 }
+
+define void @src_already_invariant_speculation(ptr %ptr, i1 %c, i1 %c2, i64 %arg1, i64 %arg2) {
+; SPEC-LABEL: define void @src_already_invariant_speculation
+; SPEC-SAME: (ptr [[PTR:%.*]], i1 [[C:%.*]], i1 [[C2:%.*]], i64 [[ARG1:%.*]], i64 [[ARG2:%.*]]) {
+; SPEC-NEXT:  entry:
+; SPEC-NEXT:    [[PTR2:%.*]] = getelementptr i8, ptr [[PTR]], i64 [[ARG1]]
+; SPEC-NEXT:    [[PTR3:%.*]] = getelementptr i8, ptr [[PTR2]], i64 [[ARG2]]
+; SPEC-NEXT:    br label [[LOOP:%.*]]
+; SPEC:       loop:
+; SPEC-NEXT:    br i1 [[C2]], label [[IF:%.*]], label [[LATCH:%.*]]
+; SPEC:       if:
+; SPEC-NEXT:    call void @use(ptr [[PTR3]])
+; SPEC-NEXT:    br label [[LATCH]]
+; SPEC:       latch:
+; SPEC-NEXT:    br i1 [[C]], label [[LOOP]], label [[EXIT:%.*]]
+; SPEC:       exit:
+; SPEC-NEXT:    ret void
+;
+; NOSPEC-LABEL: define void @src_already_invariant_speculation
+; NOSPEC-SAME: (ptr [[PTR:%.*]], i1 [[C:%.*]], i1 [[C2:%.*]], i64 [[ARG1:%.*]], i64 [[ARG2:%.*]]) {
+; NOSPEC-NEXT:  entry:
+; NOSPEC-NEXT:    br label [[LOOP:%.*]]
+; NOSPEC:       loop:
+; NOSPEC-NEXT:    br i1 [[C2]], label [[IF:%.*]], label [[LATCH:%.*]]
+; NOSPEC:       if:
+; NOSPEC-NEXT:    [[PTR2:%.*]] = getelementptr i8, ptr [[PTR]], i64 [[ARG1]]
+; NOSPEC-NEXT:    [[PTR3:%.*]] = getelementptr i8, ptr [[PTR2]], i64 [[ARG2]]
+; NOSPEC-NEXT:    call void @use(ptr [[PTR3]])
+; NOSPEC-NEXT:    br label [[LATCH]]
+; NOSPEC:       latch:
+; NOSPEC-NEXT:    br i1 [[C]], label [[LOOP]], label [[EXIT:%.*]]
+; NOSPEC:       exit:
+; NOSPEC-NEXT:    ret void
+;
+entry:
+  br label %loop
+
+loop:
+  br i1 %c2, label %if, label %latch
+
+if:
+  %ptr2 = getelementptr i8, ptr %ptr, i64 %arg1
+  %ptr3 = getelementptr i8, ptr %ptr2, i64 %arg2
+  call void @use(ptr %ptr3)
+  br label %latch
+
+latch:
+  br i1 %c, label %loop, label %exit
+
+exit:
+  ret void
+}


        


More information about the llvm-commits mailing list