[NewPM][CodeGen] Add machine code verification callback

D83608 need this.

Reviewed By: aeubanks

Differential Revision: https://reviews.llvm.org/D85916
This commit is contained in:
Yuanfang Chen 2020-08-13 12:52:35 -07:00
parent 4cb016cd2d
commit a5ed20b549
4 changed files with 41 additions and 2 deletions

View File

@ -1141,6 +1141,11 @@ template <> struct GraphTraits<Inverse<const MachineFunction*>> :
} }
}; };
class MachineFunctionAnalysisManager;
void verifyMachineFunction(MachineFunctionAnalysisManager *,
const std::string &Banner,
const MachineFunction &MF);
} // end namespace llvm } // end namespace llvm
#endif // LLVM_CODEGEN_MACHINEFUNCTION_H #endif // LLVM_CODEGEN_MACHINEFUNCTION_H

View File

@ -134,8 +134,10 @@ class MachineFunctionPassManager
public: public:
MachineFunctionPassManager(bool DebugLogging = false, MachineFunctionPassManager(bool DebugLogging = false,
bool RequireCodeGenSCCOrder = false) bool RequireCodeGenSCCOrder = false,
: Base(DebugLogging), RequireCodeGenSCCOrder(RequireCodeGenSCCOrder) {} bool VerifyMachineFunction = false)
: Base(DebugLogging), RequireCodeGenSCCOrder(RequireCodeGenSCCOrder),
VerifyMachineFunction(VerifyMachineFunction) {}
MachineFunctionPassManager(MachineFunctionPassManager &&) = default; MachineFunctionPassManager(MachineFunctionPassManager &&) = default;
MachineFunctionPassManager & MachineFunctionPassManager &
operator=(MachineFunctionPassManager &&) = default; operator=(MachineFunctionPassManager &&) = default;
@ -245,6 +247,8 @@ private:
// Run codegen in the SCC order. // Run codegen in the SCC order.
bool RequireCodeGenSCCOrder; bool RequireCodeGenSCCOrder;
bool VerifyMachineFunction;
}; };
} // end namespace llvm } // end namespace llvm

View File

@ -11,6 +11,7 @@
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
#include "llvm/CodeGen/MachinePassManager.h" #include "llvm/CodeGen/MachinePassManager.h"
#include "llvm/CodeGen/MachineFunction.h"
#include "llvm/CodeGen/MachineModuleInfo.h" #include "llvm/CodeGen/MachineModuleInfo.h"
#include "llvm/IR/PassManagerImpl.h" #include "llvm/IR/PassManagerImpl.h"
@ -32,6 +33,22 @@ Error MachineFunctionPassManager::run(Module &M,
(void)RequireCodeGenSCCOrder; (void)RequireCodeGenSCCOrder;
assert(!RequireCodeGenSCCOrder && "not implemented"); assert(!RequireCodeGenSCCOrder && "not implemented");
// Add a PIC to verify machine functions.
if (VerifyMachineFunction) {
PassInstrumentation PI = MFAM.getResult<PassInstrumentationAnalysis>(M);
// No need to pop this callback later since MIR pipeline is flat which means
// current pipeline is the top-level pipeline. Callbacks are not used after
// current pipeline.
PI.pushBeforeNonSkippedPassCallback([&MFAM](StringRef PassID, Any IR) {
assert(any_isa<const MachineFunction *>(IR));
const MachineFunction *MF = any_cast<const MachineFunction *>(IR);
assert(MF && "Machine function should be valid for printing");
std::string Banner = std::string("After ") + std::string(PassID);
verifyMachineFunction(&MFAM, Banner, *MF);
});
}
if (DebugLogging) { if (DebugLogging) {
dbgs() << "Starting " << getTypeName<MachineFunction>() dbgs() << "Starting " << getTypeName<MachineFunction>()
<< " pass manager run.\n"; << " pass manager run.\n";

View File

@ -304,6 +304,19 @@ FunctionPass *llvm::createMachineVerifierPass(const std::string &Banner) {
return new MachineVerifierPass(Banner); return new MachineVerifierPass(Banner);
} }
void llvm::verifyMachineFunction(MachineFunctionAnalysisManager *,
const std::string &Banner,
const MachineFunction &MF) {
// TODO: Use MFAM after porting below analyses.
// LiveVariables *LiveVars;
// LiveIntervals *LiveInts;
// LiveStacks *LiveStks;
// SlotIndexes *Indexes;
unsigned FoundErrors = MachineVerifier(nullptr, Banner.c_str()).verify(MF);
if (FoundErrors)
report_fatal_error("Found " + Twine(FoundErrors) + " machine code errors.");
}
bool MachineFunction::verify(Pass *p, const char *Banner, bool AbortOnErrors) bool MachineFunction::verify(Pass *p, const char *Banner, bool AbortOnErrors)
const { const {
MachineFunction &MF = const_cast<MachineFunction&>(*this); MachineFunction &MF = const_cast<MachineFunction&>(*this);