[ir] Add framework to dump IR to text

This CL adds the framework and `tint` option to write the IR to stdout
as text for debug purposes.

Bug: tint:1718
Change-Id: I05bd83635800fbfe3b65d968a84b30931ec1bdb6
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/110171
Reviewed-by: Ben Clayton <bclayton@google.com>
Commit-Queue: Dan Sinclair <dsinclair@chromium.org>
Kokoro: Kokoro <noreply+kokoro@google.com>
This commit is contained in:
dan sinclair 2022-11-16 14:36:30 +00:00 committed by Dawn LUCI CQ
parent fad31fe74a
commit c6d46994af
3 changed files with 24 additions and 3 deletions

View File

@ -110,6 +110,7 @@ struct Options {
std::optional<tint::sem::BindingPoint> hlsl_root_constant_binding_point; std::optional<tint::sem::BindingPoint> hlsl_root_constant_binding_point;
#if TINT_BUILD_IR #if TINT_BUILD_IR
bool dump_ir = false;
bool dump_ir_graph = false; bool dump_ir_graph = false;
#endif // TINT_BUILD_IR #endif // TINT_BUILD_IR
}; };
@ -475,6 +476,8 @@ bool ParseArgs(const std::vector<std::string>& args, Options* opts) {
} }
opts->dxc_path = args[i]; opts->dxc_path = args[i];
#if TINT_BUILD_IR #if TINT_BUILD_IR
} else if (arg == "--dump-ir") {
opts->dump_ir = true;
} else if (arg == "--dump-ir-graph") { } else if (arg == "--dump-ir-graph") {
opts->dump_ir_graph = true; opts->dump_ir_graph = true;
#endif // TINT_BUILD_IR #endif // TINT_BUILD_IR
@ -1205,6 +1208,7 @@ int main(int argc, const char** argv) {
std::string usage = tint::utils::ReplaceAll(kUsage, "${transforms}", transform_names()); std::string usage = tint::utils::ReplaceAll(kUsage, "${transforms}", transform_names());
#if TINT_BUILD_IR #if TINT_BUILD_IR
usage += usage +=
" --dump-ir -- Writes the IR to stdout\n"
" --dump-ir-graph -- Writes the IR graph to 'tint.dot' as a dot graph\n"; " --dump-ir-graph -- Writes the IR graph to 'tint.dot' as a dot graph\n";
#endif // TINT_BUILD_IR #endif // TINT_BUILD_IR
@ -1327,16 +1331,21 @@ int main(int argc, const char** argv) {
} }
#if TINT_BUILD_IR #if TINT_BUILD_IR
if (options.dump_ir_graph) { if (options.dump_ir || options.dump_ir_graph) {
auto result = tint::ir::Module::FromProgram(program.get()); auto result = tint::ir::Module::FromProgram(program.get());
if (!result) { if (!result) {
std::cerr << "Failed to build IR from program: " << result.Failure() << std::endl; std::cerr << "Failed to build IR from program: " << result.Failure() << std::endl;
} else { } else {
auto mod = result.Move(); auto mod = result.Move();
if (options.dump_ir) {
std::cout << tint::ir::Debug::AsString(&mod) << std::endl;
}
if (options.dump_ir_graph) {
auto graph = tint::ir::Debug::AsDotGraph(&mod); auto graph = tint::ir::Debug::AsDotGraph(&mod);
WriteFile("tint.dot", "w", graph); WriteFile("tint.dot", "w", graph);
} }
} }
}
#endif // TINT_BUILD_IR #endif // TINT_BUILD_IR
tint::inspector::Inspector inspector(program.get()); tint::inspector::Inspector inspector(program.get());

View File

@ -156,4 +156,11 @@ std::string Debug::AsDotGraph(const Module* mod) {
return out.str(); return out.str();
} }
// static
std::string Debug::AsString(const Module*) {
std::stringstream out;
out << "IR" << std::endl;
return out.str();
}
} // namespace tint::ir } // namespace tint::ir

View File

@ -28,6 +28,11 @@ class Debug {
/// @param mod the module to emit /// @param mod the module to emit
/// @returns the dot graph for the given module /// @returns the dot graph for the given module
static std::string AsDotGraph(const Module* mod); static std::string AsDotGraph(const Module* mod);
/// Returns the module as a string
/// @param mod the module to emit
/// @returns the string representation of the module
static std::string AsString(const Module* mod);
}; };
} // namespace tint::ir } // namespace tint::ir