Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CodeGen] Fix InstructionCount remarks for MI bundles #107621

Merged
merged 1 commit into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
arsenm marked this conversation as resolved.
Show resolved Hide resolved
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
...
francisvm marked this conversation as resolved.
Show resolved Hide resolved
---
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
...
Loading