@@ -802,7 +802,8 @@ void initCommandOptions(CommandOptions& options)
802
802
{OptionKind::VerifyDebugSerialIr,
803
803
" -verify-debug-serial-ir" ,
804
804
nullptr ,
805
- " Verify IR in the front-end." }};
805
+ " Verify IR in the front-end." },
806
+ {OptionKind::DumpModule, " -dump-module" , nullptr , " Disassemble and print the module IR." }};
806
807
_addOptions (makeConstArrayView (debuggingOpts), options);
807
808
808
809
/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Experimental !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */
@@ -2947,6 +2948,88 @@ SlangResult OptionsParser::_parse(int argc, char const* const* argv)
2947
2948
Int index = 0 ;
2948
2949
SLANG_RETURN_ON_FAIL (_expectInt (arg, index ));
2949
2950
linkage->m_optionSet .add (OptionKind::BindlessSpaceIndex, (int )index );
2951
+ break ;
2952
+ }
2953
+ case OptionKind::DumpModule:
2954
+ {
2955
+ CommandLineArg fileName;
2956
+ SLANG_RETURN_ON_FAIL (m_reader.expectArg (fileName));
2957
+ auto desc = slang::SessionDesc ();
2958
+ ComPtr<slang::ISession> session;
2959
+ m_session->createSession (desc, session.writeRef ());
2960
+ ComPtr<slang::IBlob> diagnostics;
2961
+
2962
+ // Coerce Slang to load from the given file, without letting it automatically
2963
+ // choose .slang-module files over .slang files.
2964
+ // First try to load as source string, and fall back to loading as an IR Blob.
2965
+ // Avoid guessing based on filename or inspecting the file contents.
2966
+ FileStream file;
2967
+ if (SLANG_FAILED (file.init (
2968
+ fileName.value ,
2969
+ FileMode::Open,
2970
+ FileAccess::Read,
2971
+ FileShare::None)))
2972
+ {
2973
+ m_sink->diagnose (arg.loc , Diagnostics::cannotOpenFile, fileName.value );
2974
+ return SLANG_FAIL;
2975
+ }
2976
+
2977
+ List<uint8_t > buffer;
2978
+ file.seek (SeekOrigin::End, 0 );
2979
+ const Int64 size = file.getPosition ();
2980
+ buffer.setCount (size + 1 );
2981
+ file.seek (SeekOrigin::Start, 0 );
2982
+ SLANG_RETURN_ON_FAIL (file.readExactly (buffer.getBuffer (), (size_t )size));
2983
+ buffer[size] = 0 ;
2984
+ file.close ();
2985
+
2986
+ ComPtr<slang::IModule> module;
2987
+ module = session->loadModuleFromSourceString (
2988
+ " module" ,
2989
+ " path" ,
2990
+ (const char *)buffer.getBuffer (),
2991
+ diagnostics.writeRef ());
2992
+ if (!module)
2993
+ {
2994
+ // Load buffer as an IR blob
2995
+ ComPtr<slang::IBlob> blob;
2996
+ blob = RawBlob::create (buffer.getBuffer (), size);
2997
+
2998
+ module = session->loadModuleFromIRBlob (
2999
+ " module" ,
3000
+ " path" ,
3001
+ blob,
3002
+ diagnostics.writeRef ());
3003
+ }
3004
+
3005
+ if (module)
3006
+ {
3007
+ ComPtr<slang::IBlob> disassemblyBlob;
3008
+ if (SLANG_FAILED (module->disassemble (disassemblyBlob.writeRef ())))
3009
+ {
3010
+ m_sink->diagnose (arg.loc , Diagnostics::cannotDisassemble, fileName.value );
3011
+ return SLANG_FAIL;
3012
+ }
3013
+ else
3014
+ {
3015
+ // success, print out the disassembly in a way that slang-test can read
3016
+ m_sink->diagnoseRaw (
3017
+ Severity::Note,
3018
+ (const char *)disassemblyBlob->getBufferPointer ());
3019
+ }
3020
+ }
3021
+ else
3022
+ {
3023
+ if (diagnostics)
3024
+ {
3025
+ m_sink->diagnoseRaw (
3026
+ Severity::Error,
3027
+ (const char *)diagnostics->getBufferPointer ());
3028
+ }
3029
+ return SLANG_FAIL;
3030
+ }
3031
+
3032
+
2950
3033
break ;
2951
3034
}
2952
3035
default :
0 commit comments