Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 6 additions & 0 deletions llvm/include/llvm/CodeGen/TargetInstrInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,12 @@ class LLVM_ABI TargetInstrInfo : public MCInstrInfo {
return ~0U;
}

/// Whether the correctness of the instruction size returned by
/// getInstSizeInBytes() should be verified.
virtual bool shouldVerifyInstSize(const MachineInstr &MI) const {
return false;
}

/// Return true if the instruction is as cheap as a move instruction.
///
/// Targets for different archs need to override this, and different
Expand Down
28 changes: 28 additions & 0 deletions llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2147,6 +2147,11 @@ void AsmPrinter::emitFunctionBody() {
if (isVerbose())
emitComments(MI, STI, OutStreamer->getCommentOS());

#ifndef NDEBUG
MCFragment *OldFragment = OutStreamer->getCurrentFragment();
size_t OldFragSize = OldFragment->getFixedSize();
#endif

switch (MI.getOpcode()) {
case TargetOpcode::CFI_INSTRUCTION:
emitCFIInstruction(MI);
Expand Down Expand Up @@ -2265,6 +2270,29 @@ void AsmPrinter::emitFunctionBody() {
break;
}

#ifndef NDEBUG
// Verify that the instruction size reported by InstrInfo matches the
// actually emitted size. Many backends performing branch relaxation
// on the MIR level rely on this for correctness.
if (OutStreamer->isObj()) {
const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
MCFragment *NewFragment = OutStreamer->getCurrentFragment();
// Don't try to handle fragment splitting cases.
if (NewFragment == OldFragment && TII->shouldVerifyInstSize(MI)) {
unsigned ExpectedSize = TII->getInstSizeInBytes(MI);
unsigned ActualSize = NewFragment->getFixedSize() - OldFragSize;
// FIXME: InstrInfo currently over-estimates the size of STACKMAP.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getInstSizeInBytes should always return a conservative overestimate. It cannot be expected to give an exact match in all cases

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's at least true with pseudos. It's fuzzier at this point in the emission. For AMDGPU have some cases where we should be able to reduce the instruction size after relaxation determines the exact constants, we just happen to not perform that optimization today

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, but for the purposes of this verification, I do think being able to check the exact size is quite useful. After all, for most instructions (even most pseudos) you can determine the exact size, and it's useful to check that.

I've updated the hook to return a InstSizeVerifyMode instead that allows picking between NoVerify, ExactSize and AllowOverEstimate. In the future, I'd like to enable AllowOverEstimate by default, but a backend can use ExactSize to get stricter validation (e.g. for PPC it holds for everything apart from stackmap).

if (ActualSize != ExpectedSize &&
MI.getOpcode() != TargetOpcode::STACKMAP) {
dbgs() << "Size mismatch for: " << MI << "\n";

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider also print the section name , sth like NewFragment->getParent()->getName().

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
dbgs() << "Size mismatch for: " << MI << "\n";
dbgs() << "Size mismatch for: " << MI;

This already includes the newline

dbgs() << "Expected size: " << ExpectedSize << "\n";
dbgs() << "Actual size: " << ActualSize << "\n";
abort();
}
}
}
#endif

if (MI.isCall()) {
if (MF->getTarget().Options.BBAddrMap)
OutStreamer->emitLabel(createCallsiteEndSymbol(MBB));
Expand Down
26 changes: 0 additions & 26 deletions llvm/lib/Target/PowerPC/PPCAsmPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
#include "PPCTargetMachine.h"
#include "TargetInfo/PowerPCTargetInfo.h"
#include "llvm/ADT/MapVector.h"
#include "llvm/ADT/ScopeExit.h"
#include "llvm/ADT/SetVector.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/ADT/StringExtras.h"
Expand Down Expand Up @@ -946,31 +945,6 @@ void PPCAsmPrinter::emitInstruction(const MachineInstr *MI) {
return PPC::S_None;
};

#ifndef NDEBUG
// Instruction sizes must be correct for PPCBranchSelector to pick the
// right branch kind. Verify that the reported sizes and the actually
// emitted sizes match.
unsigned ExpectedSize = Subtarget->getInstrInfo()->getInstSizeInBytes(*MI);
MCFragment *OldFragment = OutStreamer->getCurrentFragment();
size_t OldFragSize = OldFragment->getFixedSize();
scope_exit VerifyInstSize([&]() {
if (!OutStreamer->isObj())
return; // Can only verify size when streaming to object.
MCFragment *NewFragment = OutStreamer->getCurrentFragment();
if (NewFragment != OldFragment)
return; // Don't try to handle fragment splitting cases.
unsigned ActualSize = NewFragment->getFixedSize() - OldFragSize;
// FIXME: InstrInfo currently over-estimates the size of STACKMAP.
if (ActualSize != ExpectedSize &&
MI->getOpcode() != TargetOpcode::STACKMAP) {
dbgs() << "Size mismatch for: " << *MI << "\n";
dbgs() << "Expected size: " << ExpectedSize << "\n";
dbgs() << "Actual size: " << ActualSize << "\n";
abort();
}
});
#endif

// Lower multi-instruction pseudo operations.
switch (MI->getOpcode()) {
default: break;
Expand Down
5 changes: 5 additions & 0 deletions llvm/lib/Target/PowerPC/PPCInstrInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3034,6 +3034,11 @@ unsigned PPCInstrInfo::getInstSizeInBytes(const MachineInstr &MI) const {
}
}

bool PPCInstrInfo::shouldVerifyInstSize(const MachineInstr &MI) const {
// FIXME: The size of STACKMAP is currently over-estimated.
return MI.getOpcode() != TargetOpcode::STACKMAP;
}

std::pair<unsigned, unsigned>
PPCInstrInfo::decomposeMachineOperandsTargetFlags(unsigned TF) const {
// PPC always uses a direct mask.
Expand Down
2 changes: 2 additions & 0 deletions llvm/lib/Target/PowerPC/PPCInstrInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,8 @@ class PPCInstrInfo : public PPCGenInstrInfo {
///
unsigned getInstSizeInBytes(const MachineInstr &MI) const override;

bool shouldVerifyInstSize(const MachineInstr &MI) const override;

MCInst getNop() const override;

std::pair<unsigned, unsigned>
Expand Down
Loading