Skip to content

Commit

Permalink
llvm-reduce: Clone properties of blocks
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
arsenm committed Apr 20, 2022
1 parent 879ac41 commit 53d8858
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 10 deletions.
10 changes: 5 additions & 5 deletions llvm/include/llvm/CodeGen/MachineBasicBlock.h
Original file line number Diff line number Diff line change
Expand Up @@ -1086,6 +1086,11 @@ class MachineBasicBlock
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);
Expand All @@ -1095,11 +1100,6 @@ class MachineBasicBlock
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>;

Expand Down
72 changes: 72 additions & 0 deletions llvm/test/tools/llvm-reduce/mir/preserve-block-info.mir
Original file line number Diff line number Diff line change
@@ -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
...
54 changes: 49 additions & 5 deletions llvm/tools/llvm-reduce/ReducerWorkItem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 53d8858

Please sign in to comment.