[PATCH] D87902: [GlobalISel] Fix enumeration of entry basic blocks when using GlobalISel

Mateja Marjanovic via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 18 06:45:44 PDT 2020


matejam created this revision.
matejam added a reviewer: djtodoro.
matejam added projects: LLVM, debug-info.
Herald added subscribers: llvm-commits, hiraditya, rovka.
matejam requested review of this revision.

IRTranslator's method runOnMachineFunction is used to translate from IR to MIR when using GlobalISel as an instruction selector.
When all IR instructions are translated, we move all the instructions from the current entry block to it's only successor, which leaves the current entry block empty and useless, we then delete it from the machine function.
The only issue is that the surviving entry block's number is greater by 1 than the minimal entry block number, which is fixed by assigning the number of the deleted entry block to the surviving entry block's number.
Entry blocks should be serialized (starting from 0) so that an entry block's number wouldn't be greater than the size of the machine function (number of entry blocks in the MF) in which it resides.
Specifically an error will occur in MIRParserImpl::initializeCallSiteInfo when asked if the call site's block number (1 without this patch, 0 with this patch) is greater or equal to the number of entry blocks (1, because the second entry block has been removed).


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D87902

Files:
  llvm/lib/CodeGen/GlobalISel/IRTranslator.cpp
  llvm/test/CodeGen/X86/rename-new-entry.ll


Index: llvm/test/CodeGen/X86/rename-new-entry.ll
===================================================================
--- /dev/null
+++ llvm/test/CodeGen/X86/rename-new-entry.ll
@@ -0,0 +1,49 @@
+;; Test if the entry basic block names start with bb.0.entry when using
+;; GlobalISel
+; RUN: llc -global-isel %s -stop-after=finalize-isel -o %t.mir
+; RUN: cat %t.mir | FileCheck %s
+; CHECK: name: fn1
+; CHECK: bb.0.entry:
+; CHECK: name: fn3
+; CHECK: bb.0.entry:
+; CHECK: name: fn2
+; CHECK: bb.0.entry:
+
+; ModuleID = 'test/CodeGen/X86/rename-new-entry.c'
+source_filename = "test/CodeGen/X86/rename-new-entry.c"
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
+
+; Function Attrs: noinline nounwind uwtable
+define dso_local i32 @fn1(i32 %a) local_unnamed_addr {
+entry:
+	%c = add i32 %a, 2
+	ret i32 %c
+}
+
+; Function Attrs: noinline nounwind uwtable
+define dso_local i32 @fn3(i32 %a, i32 %b, i32 %c) local_unnamed_addr {
+entry:
+	%d = add i32 %a, %b
+	%e = mul i32 %d, %c
+	%f = call i32 @fn1(i32 %e)
+	ret i32 %f
+}
+
+; Function Attrs: noinline nounwind uwtable
+define dso_local i64 @fn2(i32 %a, i32 %b, i32 %c) local_unnamed_addr {
+entry:
+  %call = call i32 @fn1(i32 5)
+  %add = mul i32 %a, 3
+  %sub = sub i32 %add, %b
+  %add2 = add i32 %sub, %c
+  %conv4 = sext i32 %add2 to i64
+  %call2 = call i32 @fn3(i32 5, i32 %add2, i32 %add2)
+  ret i64 %conv4
+}
+
+!llvm.module.flags = !{!0}
+!llvm.ident = !{!1}
+
+!0 = !{i32 1, !"wchar_size", i32 4}
+!1 = !{!"clang version 9.0.0"}
Index: llvm/lib/CodeGen/GlobalISel/IRTranslator.cpp
===================================================================
--- llvm/lib/CodeGen/GlobalISel/IRTranslator.cpp
+++ llvm/lib/CodeGen/GlobalISel/IRTranslator.cpp
@@ -3085,9 +3085,11 @@
   NewEntryBB.sortUniqueLiveIns();
 
   // Get rid of the now empty basic block.
+  int EntryBBNumber = EntryBB->getNumber();
   EntryBB->removeSuccessor(&NewEntryBB);
   MF->remove(EntryBB);
   MF->DeleteMachineBasicBlock(EntryBB);
+  NewEntryBB.setNumber(EntryBBNumber);
 
   assert(&MF->front() == &NewEntryBB &&
          "New entry wasn't next in the list of basic block!");


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D87902.292776.patch
Type: text/x-patch
Size: 2177 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200918/cd3938cc/attachment.bin>


More information about the llvm-commits mailing list