Skip to content

Commit

Permalink
[CodeGen] Fix InstructionCount remarks for MI bundles (#107621)
Browse files Browse the repository at this point in the history
For MI bundles, the instruction count remark doesn't count the
instructions inside the bundle.
  • Loading branch information
francisvm authored Oct 2, 2024
1 parent f982443 commit 916e6ad
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 7 deletions.
40 changes: 33 additions & 7 deletions llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1703,6 +1703,20 @@ static bool needFuncLabels(const MachineFunction &MF, const AsmPrinter &Asm) {
classifyEHPersonality(MF.getFunction().getPersonalityFn()));
}

// Return the mnemonic of a MachineInstr if available, or the MachineInstr
// opcode name otherwise.
static StringRef getMIMnemonic(const MachineInstr &MI, MCStreamer &Streamer) {
const TargetInstrInfo *TII =
MI.getParent()->getParent()->getSubtarget().getInstrInfo();
MCInst MCI;
MCI.setOpcode(MI.getOpcode());
if (StringRef Name = Streamer.getMnemonic(MCI); !Name.empty())
return Name;
StringRef Name = TII->getName(MI.getOpcode());
assert(!Name.empty() && "Missing mnemonic and name for opcode");
return Name;
}

/// EmitFunctionBody - This method emits the body and trailer for a
/// function.
void AsmPrinter::emitFunctionBody() {
Expand Down Expand Up @@ -1746,7 +1760,6 @@ void AsmPrinter::emitFunctionBody() {
if (!MI.isPosition() && !MI.isImplicitDef() && !MI.isKill() &&
!MI.isDebugInstr()) {
HasAnyRealCode = true;
++NumInstsInFunction;
}

// If there is a pre-instruction symbol, emit a label for it here.
Expand Down Expand Up @@ -1845,12 +1858,25 @@ void AsmPrinter::emitFunctionBody() {
break;
default:
emitInstruction(&MI);
if (CanDoExtraAnalysis) {
MCInst MCI;
MCI.setOpcode(MI.getOpcode());
auto Name = OutStreamer->getMnemonic(MCI);
auto I = MnemonicCounts.insert({Name, 0u});
I.first->second++;

auto CountInstruction = [&](const MachineInstr &MI) {
// Skip Meta instructions inside bundles.
if (MI.isMetaInstruction())
return;
++NumInstsInFunction;
if (CanDoExtraAnalysis) {
StringRef Name = getMIMnemonic(MI, *OutStreamer);
++MnemonicCounts[Name];
}
};
if (!MI.isBundle()) {
CountInstruction(MI);
break;
}
// Separately count all the instructions in a bundle.
for (auto It = std::next(MI.getIterator());
It != MBB.end() && It->isInsideBundle(); ++It) {
CountInstruction(*It);
}
break;
}
Expand Down
75 changes: 75 additions & 0 deletions llvm/test/CodeGen/RISCV/instruction-count-remark.mir
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# RUN: llc -mtriple=riscv32 -verify-machineinstrs -start-before=riscv-expand-pseudo -simplify-mir -o /dev/null -pass-remarks-analysis=asm-printer %s 2>&1 | FileCheck %s
---
name: instrs
tracksRegLiveness: true
body: |
bb.0:
$x0 = ADDI $x0, 0
$x0 = ADDI $x0, 0
$x0 = ADDI $x0, 0
$x0 = LW $x0, 0
$x0 = LW $x0, 0
$x0 = XORI $x0, 0
; CHECK: addi : 3
; CHECK-NEXT: lw : 2
; CHECK-NEXT: xori : 1
; CHECK: 6 instructions in function
...
---
name: bundles
tracksRegLiveness: true
body: |
bb.0:
$x0 = ADDI $x0, 0
BUNDLE {
$x0 = ADDI $x0, 0
$x0 = ADDI $x0, 0
$x0 = LW $x0, 0
}
$x0 = LW $x0, 0
$x0 = XORI $x0, 0
; CHECK: addi : 3
; CHECK-NEXT: lw : 2
; CHECK-NEXT: xori : 1
; CHECK: 6 instructions in function
...
---
name: metainstrs
tracksRegLiveness: true
body: |
bb.0:
$x0 = ADDI $x0, 0
$x0 = ADDI $x0, 0
$x0 = ADDI $x0, 0
$x0 = IMPLICIT_DEF
$x0 = LW $x0, 0
$x0 = LW $x0, 0
CFI_INSTRUCTION adjust_cfa_offset 4
$x0 = XORI $x0, 0
DBG_VALUE $x0, 0
; CHECK: addi : 3
; CHECK-NEXT: lw : 2
; CHECK-NEXT: xori : 1
; CHECK: 6 instructions in function
...
---
name: metabundles
tracksRegLiveness: true
body: |
bb.0:
$x0 = ADDI $x0, 0
BUNDLE {
CFI_INSTRUCTION adjust_cfa_offset 4
$x0 = ADDI $x0, 0
$x0 = ADDI $x0, 0
DBG_VALUE $x0, 0
$x0 = LW $x0, 0
}
$x0 = LW $x0, 0
$x0 = IMPLICIT_DEF
$x0 = XORI $x0, 0
; CHECK: addi : 3
; CHECK-NEXT: lw : 2
; CHECK-NEXT: xori : 1
; CHECK: 6 instructions in function
...

0 comments on commit 916e6ad

Please sign in to comment.