[tint] Add build guard for IR validation assertions

This is enabled by default when building Dawn standalone or building
the fuzzers, but off by default when building Chromium without the
fuzzers.

Bug: 498327991
Change-Id: I1aaf2b40eb525c0af36207a8baace303503ae11c
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/300796
Commit-Queue: James Price <jrprice@google.com>
Reviewed-by: dan sinclair <dsinclair@chromium.org>
diff --git a/CMakeLists.txt b/CMakeLists.txt
index fd0ef46..7df6803 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -260,6 +260,7 @@
 option(TINT_BUILD_TINTD "Build the WGSL language server" OFF)
 
 option(TINT_ENABLE_IR_DUMPING "Enable runtime control for dumping IR" ON)
+option(TINT_ENABLE_IR_VALIDATION_ASSERTS "Enable runtime control for IR validation assertions" ON)
 option(TINT_ENABLE_BREAK_IN_DEBUGGER "Enable tint::debugger::Break()" OFF)
 option(TINT_CHECK_CHROMIUM_STYLE "Check for [chromium-style] issues during build" OFF)
 option(TINT_RANDOMIZE_HASHES "Randomize the hash seed value to detect non-deterministic output" OFF)
@@ -335,6 +336,7 @@
 message(STATUS "Tint build tests: ${TINT_BUILD_TESTS}")
 message(STATUS "Tint build tintd: ${TINT_BUILD_TINTD}")
 message(STATUS "Tint enable IR dumping: ${TINT_ENABLE_IR_DUMPING}")
+message(STATUS "Tint enable IR validation assertions: ${TINT_ENABLE_IR_VALIDATION_ASSERTS}")
 message(STATUS "Tint enable break in debugger: ${TINT_ENABLE_BREAK_IN_DEBUGGER}")
 message(STATUS "Tint build checking [chromium-style]: ${TINT_CHECK_CHROMIUM_STYLE}")
 message(STATUS "Tint randomize hashes: ${TINT_RANDOMIZE_HASHES}")
diff --git a/scripts/tint_overrides_with_defaults.gni b/scripts/tint_overrides_with_defaults.gni
index 48cdc07..22f56c1 100644
--- a/scripts/tint_overrides_with_defaults.gni
+++ b/scripts/tint_overrides_with_defaults.gni
@@ -88,6 +88,10 @@
     tint_enable_ir_dumping = tint_standalone
   }
 
+  if (!defined(tint_enable_ir_validation_asserts)) {
+    tint_enable_ir_validation_asserts = tint_standalone
+  }
+
   # Build the SPIR-V input reader
   if (!defined(tint_build_spv_reader)) {
     tint_build_spv_reader = true
diff --git a/src/tint/BUILD.gn b/src/tint/BUILD.gn
index 95b214c..2ee43ad 100644
--- a/src/tint/BUILD.gn
+++ b/src/tint/BUILD.gn
@@ -152,6 +152,12 @@
     defines += [ "TINT_ENABLE_IR_DUMPING=0" ]
   }
 
+  if (tint_enable_ir_validation_asserts || use_libfuzzer) {
+    defines += [ "TINT_ENABLE_IR_VALIDATION_ASSERTS=1" ]
+  } else {
+    defines += [ "TINT_ENABLE_IR_VALIDATION_ASSERTS=0" ]
+  }
+
   include_dirs = [
     "${tint_root_dir}/",
     "${tint_root_dir}/include/",
diff --git a/src/tint/CMakeLists.txt b/src/tint/CMakeLists.txt
index 22aa5d4..e30d31c 100644
--- a/src/tint/CMakeLists.txt
+++ b/src/tint/CMakeLists.txt
@@ -76,6 +76,7 @@
   target_compile_definitions(${TARGET} PUBLIC -DTINT_BUILD_NULL_WRITER=$<BOOL:${TINT_BUILD_NULL_WRITER}>)
   target_compile_definitions(${TARGET} PUBLIC -DTINT_BUILD_TINTD=$<BOOL:${TINT_BUILD_TINTD}>)
   target_compile_definitions(${TARGET} PUBLIC -DTINT_ENABLE_IR_DUMPING=$<BOOL:${TINT_ENABLE_IR_DUMPING}>)
+  target_compile_definitions(${TARGET} PUBLIC -DTINT_ENABLE_IR_VALIDATION_ASSERTS=$<BOOL:${TINT_ENABLE_IR_VALIDATION_ASSERTS}>)
 
   if(TINT_BUILD_FUZZERS)
     target_compile_options(${TARGET} PRIVATE "-fsanitize=fuzzer")
diff --git a/src/tint/lang/core/ir/validator.cc b/src/tint/lang/core/ir/validator.cc
index 1330079..a0ee08f 100644
--- a/src/tint/lang/core/ir/validator.cc
+++ b/src/tint/lang/core/ir/validator.cc
@@ -5255,9 +5255,12 @@
     return v.Run();
 }
 
-void AssertValid(const Module& mod, Capabilities capabilities, std::string_view msg) {
+void AssertValid(const Module& mod,
+                 [[maybe_unused]] Capabilities capabilities,
+                 std::string_view msg) {
     DumpIRIfEnabled(mod, msg);
 
+#if TINT_ENABLE_IR_VALIDATION_ASSERTS
     if (mod.enable_validation_asserts) {
         Validator v(mod, capabilities);
         auto result = v.Run();
@@ -5268,6 +5271,7 @@
                        << result.Failure().reason;
         }
     }
+#endif
 }
 
 }  // namespace tint::core::ir