llvm-reduce: Clone properties of blocks
authorMatt Arsenault <Matthew.Arsenault@amd.com>
Thu, 14 Apr 2022 15:29:01 +0000 (11:29 -0400)
committerMatt Arsenault <Matthew.Arsenault@amd.com>
Wed, 20 Apr 2022 13:47:45 +0000 (09:47 -0400)
getSuccProbability was private for some reason, saying to go through
MachineBranchProbabilityInfo. There doesn't seem to be much point to
that, as that wrapper directly calls this.

Like other areas, some of these fields aren't handled by the MIR
printer/parser so aren't tested.

llvm/include/llvm/CodeGen/MachineBasicBlock.h
llvm/test/tools/llvm-reduce/mir/preserve-block-info.mir [new file with mode: 0644]
llvm/tools/llvm-reduce/ReducerWorkItem.cpp

index 7d9a9ac..23305b5 100644 (file)
@@ -1086,6 +1086,11 @@ public:
     IrrLoopHeaderWeight = Weight;
   }
 
+  /// Return probability of the edge from this block to MBB. This method should
+  /// NOT be called directly, but by using getEdgeProbability method from
+  /// MachineBranchProbabilityInfo class.
+  BranchProbability getSuccProbability(const_succ_iterator Succ) const;
+
 private:
   /// Return probability iterator corresponding to the I successor iterator.
   probability_iterator getProbabilityIterator(succ_iterator I);
@@ -1095,11 +1100,6 @@ private:
   friend class MachineBranchProbabilityInfo;
   friend class MIPrinter;
 
-  /// Return probability of the edge from this block to MBB. This method should
-  /// NOT be called directly, but by using getEdgeProbability method from
-  /// MachineBranchProbabilityInfo class.
-  BranchProbability getSuccProbability(const_succ_iterator Succ) const;
-
   // Methods used to maintain doubly linked list of blocks...
   friend struct ilist_callback_traits<MachineBasicBlock>;
 
diff --git a/llvm/test/tools/llvm-reduce/mir/preserve-block-info.mir b/llvm/test/tools/llvm-reduce/mir/preserve-block-info.mir
new file mode 100644 (file)
index 0000000..f20ebd5
--- /dev/null
@@ -0,0 +1,72 @@
+# REQUIRES: amdgpu-registered-target
+# RUN: llvm-reduce -simplify-mir -mtriple=amdgcn-amd-amdhsa --test FileCheck --test-arg --check-prefix=CHECK-INTERESTINGNESS --test-arg %s --test-arg --input-file %s -o %t 2> %t.log
+# RUN: FileCheck --match-full-lines --check-prefix=RESULT %s < %t
+
+# CHECK-INTERESTINGNESS: V_MOV_B32
+
+
+# RESULT: bb.0.entry:
+# RESULT: %{{[0-9]+}}:vgpr_32 = V_MOV_B32_e32 0, implicit $exec
+
+# RESULT: bb.1 (address-taken, align 8):
+# RESULT: bb.2 (landing-pad, align 16):
+# RESULT: bb.3 (inlineasm-br-indirect-target):
+# RESULT: bb.4 (ehfunclet-entry):
+# RESULT: bb.5 (bbsections 1):
+# RESULT: bb.6 (bbsections 2):
+# RESULT: bb.7 (bbsections 3):
+# RESULT: bb.8:
+# RESULT-NEXT: successors: %bb.9(0x66666666), %bb.10(0x1999999a)
+# RESULT: bb.9:
+# RESULT: bb.10.exitblock:
+
+--- |
+  define void @func(i32 %size)  {
+  entry:
+    br label %exitblock
+
+  exitblock:
+    ret void
+  }
+
+...
+
+---
+name: func
+alignment:       32
+exposesReturnsTwice: true
+legalized:       true
+regBankSelected: true
+selected:        true
+failedISel:      true
+tracksRegLiveness: true
+hasWinCFI:       true
+failsVerification: true
+tracksDebugUserValues: true
+body:             |
+  bb.0.entry:
+    S_NOP 0
+    %0:vgpr_32 = V_MOV_B32_e32 0, implicit $exec
+
+  bb.1 (address-taken, align 8):
+
+  bb.2 (landing-pad, align 16):
+
+  bb.3 (inlineasm-br-indirect-target):
+
+  bb.4 (ehfunclet-entry):
+
+  bb.5 (bbsections 1):
+  bb.6 (bbsections 2):
+  bb.7 (bbsections 3):
+
+  bb.8:
+    successors: %bb.9(4), %bb.10(1)
+    S_CBRANCH_SCC1 %bb.10, implicit undef $scc
+    S_BRANCH %bb.9
+
+  bb.9:
+
+  bb.10.exitblock:
+    S_ENDPGM 0, implicit %0
+...
index 75fef69..e12e811 100644 (file)
@@ -125,8 +125,41 @@ static std::unique_ptr<MachineFunction> cloneMF(MachineFunction *SrcMF) {
   auto *DstMRI = &DstMF->getRegInfo();
 
   // Clone blocks.
-  for (MachineBasicBlock &SrcMBB : *SrcMF)
-    Src2DstMBB[&SrcMBB] = DstMF->CreateMachineBasicBlock();
+  for (MachineBasicBlock &SrcMBB : *SrcMF) {
+    MachineBasicBlock *DstMBB =
+        DstMF->CreateMachineBasicBlock(SrcMBB.getBasicBlock());
+    Src2DstMBB[&SrcMBB] = DstMBB;
+
+    if (SrcMBB.hasAddressTaken())
+      DstMBB->setHasAddressTaken();
+
+    // FIXME: This is not serialized
+    if (SrcMBB.hasLabelMustBeEmitted())
+      DstMBB->setLabelMustBeEmitted();
+
+    DstMBB->setAlignment(SrcMBB.getAlignment());
+
+    // FIXME: This is not serialized
+    DstMBB->setMaxBytesForAlignment(SrcMBB.getMaxBytesForAlignment());
+
+    DstMBB->setIsEHPad(SrcMBB.isEHPad());
+    DstMBB->setIsEHScopeEntry(SrcMBB.isEHScopeEntry());
+    DstMBB->setIsEHCatchretTarget(SrcMBB.isEHCatchretTarget());
+    DstMBB->setIsEHFuncletEntry(SrcMBB.isEHFuncletEntry());
+
+    // FIXME: These are not serialized
+    DstMBB->setIsCleanupFuncletEntry(SrcMBB.isCleanupFuncletEntry());
+    DstMBB->setIsBeginSection(SrcMBB.isBeginSection());
+    DstMBB->setIsEndSection(SrcMBB.isEndSection());
+
+    DstMBB->setSectionID(SrcMBB.getSectionID());
+    DstMBB->setIsInlineAsmBrIndirectTarget(
+        SrcMBB.isInlineAsmBrIndirectTarget());
+
+    // FIXME: This is not serialized
+    if (Optional<uint64_t> Weight = SrcMBB.getIrrLoopHeaderWeight())
+      DstMBB->setIrrLoopHeaderWeight(*Weight);
+  }
 
   const MachineFrameInfo &SrcMFI = SrcMF->getFrameInfo();
   MachineFrameInfo &DstMFI = DstMF->getFrameInfo();
@@ -187,25 +220,36 @@ static std::unique_ptr<MachineFunction> cloneMF(MachineFunction *SrcMF) {
     }
   }
 
+  const TargetSubtargetInfo &STI = DstMF->getSubtarget();
+  const TargetInstrInfo *TII = STI.getInstrInfo();
+  const TargetRegisterInfo *TRI = STI.getRegisterInfo();
+
   // Link blocks.
   for (auto &SrcMBB : *SrcMF) {
     auto *DstMBB = Src2DstMBB[&SrcMBB];
     DstMF->push_back(DstMBB);
+
     for (auto It = SrcMBB.succ_begin(), IterEnd = SrcMBB.succ_end();
          It != IterEnd; ++It) {
       auto *SrcSuccMBB = *It;
       auto *DstSuccMBB = Src2DstMBB[SrcSuccMBB];
-      DstMBB->addSuccessor(DstSuccMBB);
+      DstMBB->addSuccessor(DstSuccMBB, SrcMBB.getSuccProbability(It));
     }
     for (auto &LI : SrcMBB.liveins())
       DstMBB->addLiveIn(LI);
+
+    // Make sure MRI knows about registers clobbered by unwinder.
+    if (DstMBB->isEHPad()) {
+      if (auto *RegMask = TRI->getCustomEHPadPreservedMask(*DstMF))
+        DstMRI->addPhysRegsUsedFromRegMask(RegMask);
+    }
   }
+
   // Clone instructions.
   for (auto &SrcMBB : *SrcMF) {
     auto *DstMBB = Src2DstMBB[&SrcMBB];
     for (auto &SrcMI : SrcMBB) {
-      const auto &MCID =
-          DstMF->getSubtarget().getInstrInfo()->get(SrcMI.getOpcode());
+      const auto &MCID = TII->get(SrcMI.getOpcode());
       auto *DstMI = DstMF->CreateMachineInstr(MCID, SrcMI.getDebugLoc(),
                                               /*NoImplicit=*/true);
       DstMBB->push_back(DstMI);