From ca62ec2591de40e7c8226330670cc8c8d4840065 Mon Sep 17 00:00:00 2001 From: Sriram Murali <85252063+sriramm-nv@users.noreply.github.com> Date: Wed, 1 May 2024 00:06:59 -0700 Subject: Adds functionality to dump IR to stdout (#4065) Adds a member dump() to IRInst that can writes the immediate value or IR inst value to stdout to help with debugging --- source/slang/slang-ir.cpp | 20 ++++++++++++++++++++ source/slang/slang-ir.h | 4 ++++ 2 files changed, 24 insertions(+) diff --git a/source/slang/slang-ir.cpp b/source/slang/slang-ir.cpp index 5a917e88c..5f49d1966 100644 --- a/source/slang/slang-ir.cpp +++ b/source/slang/slang-ir.cpp @@ -4,6 +4,7 @@ #include "slang-ir-util.h" #include "../core/slang-basic.h" +#include "../core/slang-writer.h" #include "slang-ir-dominators.h" @@ -8605,6 +8606,25 @@ namespace Slang } } + void IRInst::dump() + { + if (auto intLit = as(this)) + { + std::cout << intLit->getValue() << std::endl; + } + else if (auto stringLit = as(this)) + { + std::cout << stringLit->getStringSlice().begin() << std::endl; + } + else + { + StringBuilder sb; + IRDumpOptions options; + StringWriter writer(&sb, Slang::WriterFlag::AutoFlush); + dumpIR(this, options, nullptr, &writer); + std::cout << sb.toString().begin() << std::endl; + } + } } // namespace Slang #if SLANG_VC diff --git a/source/slang/slang-ir.h b/source/slang/slang-ir.h index 50ed0096d..d3a4c8026 100644 --- a/source/slang/slang-ir.h +++ b/source/slang/slang-ir.h @@ -838,6 +838,10 @@ struct IRInst /// If both `inPrev` and `inNext` are null, then `inParent` must have no (raw) children. /// void _insertAt(IRInst* inPrev, IRInst* inNext, IRInst* inParent); + + /// Print the IR to stdout for debugging purposes + /// + void dump(); }; enum class IRDynamicCastBehavior -- cgit v1.2.3