Add IR dumping functions to use during debugging.

When using lldb and/or gdb I frequently get odd std::string failures
when using the IR printing instructions we have now.  This adds the
methods  Instruction::Dump(), BasicBlock::Dump() and Function::Dump() to
emit the output of the pretty print to stderr.

With this I can now reliably print IR from gdb and lldb sessions.
This commit is contained in:
Diego Novillo 2018-09-14 13:57:12 -04:00
parent 63265097e5
commit 4a4632264e
6 changed files with 24 additions and 0 deletions

View File

@ -193,6 +193,10 @@ std::ostream& operator<<(std::ostream& str, const BasicBlock& block) {
return str;
}
void BasicBlock::Dump() const {
std::cerr << "Basic block #" << id() << "\n" << *this << "\n ";
}
std::string BasicBlock::PrettyPrint(uint32_t options) const {
std::ostringstream str;
ForEachInst([&str, options](const Instruction* inst) {

View File

@ -209,6 +209,10 @@ class BasicBlock {
// is always added to |options|.
std::string PrettyPrint(uint32_t options = 0u) const;
// Dump this basic block on stderr. Useful when running interactive
// debuggers.
void Dump() const;
private:
// The enclosing function.
Function* function_;

View File

@ -94,6 +94,10 @@ std::ostream& operator<<(std::ostream& str, const Function& func) {
return str;
}
void Function::Dump() const {
std::cerr << "Function #" << result_id() << "\n" << *this << "\n";
}
std::string Function::PrettyPrint(uint32_t options) const {
std::ostringstream str;
ForEachInst([&str, options](const Instruction* inst) {

View File

@ -125,6 +125,10 @@ class Function {
// is always added to |options|.
std::string PrettyPrint(uint32_t options = 0u) const;
// Dump this function on stderr. Useful when running interactive
// debuggers.
void Dump() const;
private:
// The OpFunction instruction that begins the definition of this function.
std::unique_ptr<Instruction> def_inst_;

View File

@ -540,6 +540,10 @@ std::ostream& operator<<(std::ostream& str, const Instruction& inst) {
return str;
}
void Instruction::Dump() const {
std::cerr << "Instruction #" << unique_id() << "\n" << *this << "\n";
}
bool Instruction::IsOpcodeCodeMotionSafe() const {
switch (opcode_) {
case SpvOpNop:

View File

@ -427,6 +427,10 @@ class Instruction : public utils::IntrusiveNodeBase<Instruction> {
// rules for physical addressing.
bool IsValidBasePointer() const;
// Dump this instruction on stderr. Useful when running interactive
// debuggers.
void Dump() const;
private:
// Returns the total count of result type id and result id.
uint32_t TypeResultIdCount() const {