[tint][cmd] Add `--format ir` as an option

We have --dump-ir / --emit-ir which prints the IR along with the requested output, but sometimes you just want to see the IR without something else.

Change-Id: I156fdae7a9c8396624cb195aa9e3c355f9ec69c6
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/186220
Commit-Queue: Ben Clayton <bclayton@google.com>
Reviewed-by: James Price <jrprice@google.com>
diff --git a/src/tint/cmd/tint/main.cc b/src/tint/cmd/tint/main.cc
index 7ea30107..c0bf14c 100644
--- a/src/tint/cmd/tint/main.cc
+++ b/src/tint/cmd/tint/main.cc
@@ -99,6 +99,12 @@
 #include "src/tint/lang/glsl/validate/validate.h"
 #endif  // TINT_BUILD_GLSL_VALIDATOR
 
+#if TINT_BUILD_WGSL_READER
+#define WGSL_READER_ONLY(x) x
+#else
+#define WGSL_READER_ONLY(x)
+#endif
+
 #if TINT_BUILD_SPV_WRITER
 #define SPV_WRITER_ONLY(x) x
 #else
@@ -145,6 +151,7 @@
     kMsl,
     kHlsl,
     kGlsl,
+    kIr,
 };
 
 #if TINT_BUILD_HLSL_WRITER
@@ -296,6 +303,7 @@
     MSL_WRITER_ONLY(format_enum_names.Emplace(Format::kMsl, "msl"));
     HLSL_WRITER_ONLY(format_enum_names.Emplace(Format::kHlsl, "hlsl"));
     GLSL_WRITER_ONLY(format_enum_names.Emplace(Format::kGlsl, "glsl"));
+    WGSL_READER_ONLY(format_enum_names.Emplace(Format::kIr, "ir"));
 
     OptionSet options;
     auto& fmt = options.Add<EnumOption<Format>>("format",
@@ -1199,6 +1207,24 @@
 #endif  // TINT_BUILD_GLSL_WRITER
 }
 
+/// Generate IR code for a program.
+/// @param program the program to generate
+/// @returns true on success
+bool GenerateIr([[maybe_unused]] const tint::Program& program) {
+#if !TINT_BUILD_WGSL_READER
+    std::cerr << "WGSL reader not enabled in tint build" << std::endl;
+    return false;
+#else
+    auto result = tint::wgsl::reader::ProgramToLoweredIR(program);
+    if (result != tint::Success) {
+        std::cerr << "Failed to build IR from program: " << result.Failure() << "\n";
+        return false;
+    }
+    std::cout << tint::core::ir::Disassemble(result.Get()) << "\n";
+    return true;
+#endif
+}
+
 }  // namespace
 
 int main(int argc, const char** argv) {
@@ -1336,15 +1362,7 @@
 
 #if TINT_BUILD_WGSL_READER
     if (options.dump_ir) {
-        auto result = tint::wgsl::reader::ProgramToLoweredIR(info.program);
-        if (result != tint::Success) {
-            std::cerr << "Failed to build IR from program: " << result.Failure() << "\n";
-        } else {
-            auto mod = result.Move();
-            if (options.dump_ir) {
-                std::cout << tint::core::ir::Disassemble(mod) << "\n";
-            }
-        }
+        GenerateIr(info.program);
     }
 #endif  // TINT_BUILD_WGSL_READER
 
@@ -1455,6 +1473,9 @@
         case Format::kGlsl:
             success = GenerateGlsl(program, options);
             break;
+        case Format::kIr:
+            success = GenerateIr(program);
+            break;
         case Format::kNone:
             break;
         default: