From de6cc94e3b7fa5d1b8eeb53993dbf3ca2cec1cc1 Mon Sep 17 00:00:00 2001 From: cheneym2 Date: Thu, 20 Mar 2025 11:38:46 -0400 Subject: Add -dump-module command to slangc (#6638) * Add -dump-module command to slangc The new -dump-module command to slangc will load and disassemble a slang module, similar to what would be seen by the -dump-ir command, except that -dump-ir tells slangc to print IR as it performs some compilation command. That is, -dump-ir requires some larger compilation task. -dump-module on the otherhand requires no additional goal and will simply load a module and print its IR to stdout independently from other compilation steps. Its intended purpose is to inspect .slang-module files on disk. It can also be used on .slang files which will be parsed and lowered if slang does not find an associated ".slang-module" version of the module on disk. The compilation API is extended with a new IModule::disassemble() method which retrieves the string representation of the dumped IR. Closes #6599 * format code * Use FileStream not FILE * format code --------- Co-authored-by: slangbot <186143334+slangbot@users.noreply.github.com> Co-authored-by: Ellie Hermaszewska --- source/slang/slang-ir.cpp | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) (limited to 'source/slang/slang-ir.cpp') diff --git a/source/slang/slang-ir.cpp b/source/slang/slang-ir.cpp index 92e51fe3b..6d1d76afc 100644 --- a/source/slang/slang-ir.cpp +++ b/source/slang/slang-ir.cpp @@ -8950,24 +8950,33 @@ void IRInst::addBlock(IRBlock* block) block->insertAtEnd(this); } -void IRInst::dump() +void IRInst::dump(String& outStr) { + StringBuilder sb; + if (auto intLit = as(this)) { - std::cout << intLit->getValue() << std::endl; + sb << intLit->getValue(); } else if (auto stringLit = as(this)) { - std::cout << stringLit->getStringSlice().begin() << std::endl; + sb << stringLit->getStringSlice(); } else { - StringBuilder sb; IRDumpOptions options; StringWriter writer(&sb, Slang::WriterFlag::AutoFlush); dumpIR(this, options, nullptr, &writer); - std::cout << sb.toString().begin() << std::endl; } + + outStr = sb.toString(); +} + +void IRInst::dump() +{ + String s; + dump(s); + std::cout << s.begin() << std::endl; } } // namespace Slang -- cgit v1.2.3