[PATCH] D127178: Fix FunctionPropertiesAnalysis updating callsite in 1-BB loop

Mircea Trofin via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 6 20:53:55 PDT 2022


mtrofin created this revision.
mtrofin added reviewers: kazu, Northbadge.
Herald added a subscriber: hiraditya.
Herald added a project: All.
mtrofin requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

If the callsite is in a single BB loop, we need to exclude the BB from
the successor set (in which it'd be a member), because that set forms a
boundary at which we stop traversing the CFG, when re-ingesting BBs
after inlining; but after inlining, the callsite BB's new successors
should be visited.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D127178

Files:
  llvm/lib/Analysis/FunctionPropertiesAnalysis.cpp
  llvm/unittests/Analysis/FunctionPropertiesAnalysisTest.cpp


Index: llvm/unittests/Analysis/FunctionPropertiesAnalysisTest.cpp
===================================================================
--- llvm/unittests/Analysis/FunctionPropertiesAnalysisTest.cpp
+++ llvm/unittests/Analysis/FunctionPropertiesAnalysisTest.cpp
@@ -543,4 +543,71 @@
   EXPECT_EQ(FPI, FunctionPropertiesInfo::getFunctionPropertiesInfo(*F1, LINew));
 }
 
+TEST_F(FunctionPropertiesAnalysisTest, InlineSameLoopBB) {
+  LLVMContext C;
+  std::unique_ptr<Module> M = makeLLVMModule(C,
+                                             R"IR(
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-pc-linux-gnu"
+
+declare i32 @a()
+declare i32 @b()
+
+define i32 @f1(i32 %a) {
+entry:
+  br label %loop
+loop:
+  %i = call i32 @f2(i32 %a)
+  %c = icmp slt i32 %i, %a
+  br i1 %c, label %loop, label %end
+end:
+  %r = phi i32 [%i, %loop], [%a, %entry]
+  ret i32 %r
+}
+
+define i32 @f2(i32 %a) {
+  %cnd = icmp slt i32 %a, 0
+  br i1 %cnd, label %then, label %else
+then:
+  %r1 = call i32 @a()
+  br label %end
+else:
+  %r2 = call i32 @b()
+  br label %end
+end:
+  %r = phi i32 [%r1, %then], [%r2, %else]
+  ret i32 %r
+}
+)IR");
+
+  Function *F1 = M->getFunction("f1");
+  CallBase *CB = findCall(*F1);
+  EXPECT_NE(CB, nullptr);
+
+  FunctionPropertiesInfo ExpectedInitial;
+  ExpectedInitial.BasicBlockCount = 3;
+  ExpectedInitial.TotalInstructionCount = 6;
+  ExpectedInitial.BlocksReachedFromConditionalInstruction = 2;
+  ExpectedInitial.Uses = 1;
+  ExpectedInitial.DirectCallsToDefinedFunctions = 1;
+  ExpectedInitial.MaxLoopDepth = 1;
+  ExpectedInitial.TopLevelLoopCount = 1;
+
+  FunctionPropertiesInfo ExpectedFinal = ExpectedInitial;
+  ExpectedFinal.BasicBlockCount = 6;
+  ExpectedFinal.DirectCallsToDefinedFunctions = 0;
+  ExpectedFinal.BlocksReachedFromConditionalInstruction = 4;
+  ExpectedFinal.TotalInstructionCount = 12;
+
+  auto FPI = buildFPI(*F1);
+  EXPECT_EQ(FPI, ExpectedInitial);
+
+  FunctionPropertiesUpdater FPU(FPI, *CB);
+  InlineFunctionInfo IFI;
+  auto IR = llvm::InlineFunction(*CB, IFI);
+  EXPECT_TRUE(IR.isSuccess());
+  FPU.finish(*LI);
+  EXPECT_EQ(FPI, ExpectedFinal);
+}
+
 } // end anonymous namespace
Index: llvm/lib/Analysis/FunctionPropertiesAnalysis.cpp
===================================================================
--- llvm/lib/Analysis/FunctionPropertiesAnalysis.cpp
+++ llvm/lib/Analysis/FunctionPropertiesAnalysis.cpp
@@ -134,6 +134,14 @@
   // We track successors separately, too, because they form a boundary, together
   // with the CB BB ('Entry') between which the inlined callee will be pasted.
   Successors.insert(succ_begin(&CallSiteBB), succ_end(&CallSiteBB));
+
+  // Exclude the CallSiteBB, if it happens to be its own successor (1-BB loop).
+  // We are only interested in BBs the graph moves past the callsite BB to
+  // define the frontier past which we don't want to re-process BBs. Including
+  // the callsite BB in this case would stop prematurely the traversal in
+  // finish().
+  Successors.erase(&CallSiteBB);
+
   for (const auto *BB : Successors)
     LikelyToChangeBBs.insert(BB);
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D127178.434686.patch
Type: text/x-patch
Size: 3116 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220607/094bcd8e/attachment.bin>


More information about the llvm-commits mailing list