[PATCH] D149130: [PartialInlining] Fix incorrect costing when IR has unreachable BBs

Vedant Paranjape via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 25 00:38:20 PDT 2023


vedant-amd created this revision.
Herald added subscribers: hoy, ormris, hiraditya.
Herald added a project: All.
vedant-amd requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Partial Inlining identifies basic blocks that can be outlined into a
function. It is possible that an unreachable basic block is marked for
outlining. During costing of the outlined region, such unreachable basic
blocks are included as well. However, the CodeExtractor eliminates such
unreachable basic blocks and emits outlined function without them.

Thus, during costing of the outlined function, it is possible that the
cost of the outlined function comes out to be lesser than the cost of
outlined region, which triggers an assert.

Assertion `OutlinedFunctionCost >= Cloner.OutlinedRegionCost && "Outlined
function cost should be no less than the outlined region"' failed.

This patch adds code to eliminate unreachable blocks from the function
body before passing it on to be inlined. It also adds a test that checks
for behaviour of costing in case of unreachable basic blocks.

Discussion: https://discourse.llvm.org/t/incorrect-costing-in-partialinliner-if-ir-has-unreachable-basic-blocks/70163


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D149130

Files:
  llvm/lib/Transforms/IPO/PartialInlining.cpp
  llvm/test/Transforms/PartialInlining/unreachable_basic_block.ll


Index: llvm/test/Transforms/PartialInlining/unreachable_basic_block.ll
===================================================================
--- /dev/null
+++ llvm/test/Transforms/PartialInlining/unreachable_basic_block.ll
@@ -0,0 +1,29 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt -passes=partial-inliner -S < %s | FileCheck %s
+declare i1 @llvm.public.type.test(ptr, metadata)
+
+define void @dummy() {
+; CHECK-LABEL: @dummy(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    br i1 false, label [[WHILE_END:%.*]], label [[WHILE_BODY:%.*]]
+; CHECK:       while.body:
+; CHECK-NEXT:    call void @dummy.1.while.body()
+; CHECK-NEXT:    br label [[WHILE_END]]
+; CHECK:       while.end:
+; CHECK-NEXT:    ret void
+;
+entry:
+  br i1 false, label %while.end, label %while.body
+
+while.body:                                       ; preds = %entry
+  call void @dummy()
+  br label %while.end
+
+unreachable.block:               ; No predecessors!
+  %0 = tail call i1 @llvm.public.type.test(ptr null, metadata !"test-function")
+  %result = getelementptr ptr, ptr null, i64 1
+  br label %while.end
+
+while.end:                                        ; preds = %unreachable.block, %while.body, %entry
+  ret void
+}
Index: llvm/lib/Transforms/IPO/PartialInlining.cpp
===================================================================
--- llvm/lib/Transforms/IPO/PartialInlining.cpp
+++ llvm/lib/Transforms/IPO/PartialInlining.cpp
@@ -50,6 +50,7 @@
 #include "llvm/Transforms/Utils/Cloning.h"
 #include "llvm/Transforms/Utils/CodeExtractor.h"
 #include "llvm/Transforms/Utils/ValueMapper.h"
+#include "llvm/Transforms/Utils/BasicBlockUtils.h"
 #include <algorithm>
 #include <cassert>
 #include <cstdint>
@@ -1440,6 +1441,13 @@
     if (CurrFunc->use_empty())
       continue;
 
+    // Try to eliminate unreachable blocks in the function, having unreachable
+    // blocks leads to incorrect costing because while evaluating the costs
+    // incase of outlining a function, such unreachable basic blocks might be
+    // taken into consideration, but the code extractor simply eliminates them
+    // while the outlined function is being constructed.
+    Changed |= EliminateUnreachableBlocks(*CurrFunc);
+
     std::pair<bool, Function *> Result = unswitchFunction(*CurrFunc);
     if (Result.second)
       Worklist.push_back(Result.second);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D149130.516687.patch
Type: text/x-patch
Size: 2383 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230425/c05d2445/attachment.bin>


More information about the llvm-commits mailing list