Restore -Wunused to CMake build and add dev mode to disable (#7004)

Avoiding unused variables is good for committed code and really annoying
as a build error when doing interactive builds. We let users distinguish
between these two situations with the `IREE_DEV_MODE` option. The CI
will enforce the strictest options.

Includes fixing `iree_select_compiler_opts` to actually append to the
variable it's given (as the documentation says) rather than overwriting
it.
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 3e7469d..de032a8 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -193,6 +193,7 @@
   endif()
 endif()
 
+option(IREE_DEV_MODE "Configure settings to optimize for IREE development (as opposed to CI or release)" OFF)
 
 #-------------------------------------------------------------------------------
 # IREE assertions
diff --git a/build_tools/cmake/iree_copts.cmake b/build_tools/cmake/iree_copts.cmake
index 2a6e541..5a92b6f 100644
--- a/build_tools/cmake/iree_copts.cmake
+++ b/build_tools/cmake/iree_copts.cmake
@@ -68,96 +68,15 @@
 set(IREE_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
 set(IREE_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
 
+# Key compilation options
 iree_select_compiler_opts(IREE_DEFAULT_COPTS
-  CLANG
-    # Set clang diagnostics. These largely match the set of warnings used within
-    # Google. They have not been audited super carefully by the IREE team but
-    # are generally thought to be a good set and consistency with those used
-    # internally is very useful when importing. If you feel that some of these
-    # should be different, please raise an issue!
-
-    # Please keep these in sync with build_tools/bazel/iree.bazelrc
-
-    "-Wall"
-
-    # Disable warnings we don't care about or that generally have a low
-    # signal/noise ratio.
-    "-Wno-ambiguous-member-template"
-    "-Wno-char-subscripts"
-    "-Wno-deprecated-declarations"
-    "-Wno-extern-c-compat" # Matches upstream. Cannot impact due to extern C inclusion method.
-    "-Wno-gnu-alignof-expression"
-    "-Wno-gnu-variable-sized-type-not-at-end"
-    "-Wno-ignored-optimization-argument"
-    "-Wno-invalid-offsetof" # Technically UB but needed for intrusive ptrs
-    "-Wno-invalid-source-encoding"
-    "-Wno-mismatched-tags"
-    "-Wno-pointer-sign"
-    "-Wno-reserved-user-defined-literal"
-    "-Wno-return-type-c-linkage"
-    "-Wno-self-assign-overloaded"
-    "-Wno-sign-compare"
-    "-Wno-signed-unsigned-wchar"
-    "-Wno-strict-overflow"
-    "-Wno-trigraphs"
-    "-Wno-unknown-pragmas"
-    "-Wno-unknown-warning-option"
-    "-Wno-unused-command-line-argument"
-    "-Wno-unused-const-variable"
-    "-Wno-unused-function"
-    "-Wno-unused-local-typedef"
-    "-Wno-unused-private-field"
-    "-Wno-user-defined-warnings"
-    # Explicitly enable some additional warnings.
-    # Some of these aren't on by default, or under -Wall, or are subsets of
-    # warnings turned off above.
-    "-Wno-ambiguous-member-template"
-    "-Wctad-maybe-unsupported"
-    "-Wfloat-overflow-conversion"
-    "-Wfloat-zero-conversion"
-    "-Wfor-loop-analysis"
-    "-Wformat-security"
-    "-Wgnu-redeclared-enum"
-    "-Wimplicit-fallthrough"
-    "-Winfinite-recursion"
-    "-Wliteral-conversion"
-    "-Wnon-virtual-dtor"
-    "-Woverloaded-virtual"
-    "-Wself-assign"
-    "-Wstring-conversion"
-    "-Wtautological-overlap-compare"
-    "-Wthread-safety"
-    "-Wthread-safety-beta"
-    "-Wunused-comparison"
-    "-Wvla"
-
-    # Turn off some additional warnings (CMake only)
-    "-Wno-strict-prototypes"
-    "-Wno-shadow-uncaptured-local"
-    "-Wno-gnu-zero-variadic-macro-arguments"
-    "-Wno-shadow-field-in-constructor"
-    "-Wno-unreachable-code-return"
-    "-Wno-missing-variable-declarations"
-    "-Wno-gnu-label-as-value"
   CLANG_OR_GCC
-    "-Wno-unused-parameter"
-    "-Wno-unused-variable"
-    "-Wno-undef"
     "-fvisibility=hidden"
     # NOTE: The RTTI setting must match what LLVM was compiled with (defaults
     # to RTTI disabled).
     "$<$<COMPILE_LANGUAGE:CXX>:-fno-rtti>"
     "$<$<COMPILE_LANGUAGE:CXX>:-fno-exceptions>"
-
   MSVC_OR_CLANG_CL
-    # Default warning level (severe + significant + production quality).
-    # This does not include level 4, "informational", warnings or those that
-    # are off by default.
-    # https://docs.microsoft.com/en-us/cpp/build/reference/compiler-option-warning-level
-    # Note that we set CMake policy CMP0092 (if found), making this explicit:
-    # https://cmake.org/cmake/help/v3.15/policy/CMP0092.html
-    "/W3"
-
     # Exclude a bunch of rarely-used APIs, such as crypto/DDE/shell.
     # https://docs.microsoft.com/en-us/windows/win32/winprog/using-the-windows-headers
     # NOTE: this is not really required anymore for build performance but does
@@ -206,6 +125,94 @@
     # but it's better to not get spurious failures during LTCG.
     # https://docs.microsoft.com/en-us/cpp/build/reference/bigobj-increase-number-of-sections-in-dot-obj-file
     "/bigobj"
+)
+
+# Compiler diagnostics.
+# Please keep these in sync with build_tools/bazel/iree.bazelrc
+iree_select_compiler_opts(IREE_DEFAULT_COPTS
+  # Clang diagnostics. These largely match the set of warnings used within
+  # Google. They have not been audited super carefully by the IREE team but are
+  # generally thought to be a good set and consistency with those used
+  # internally is very useful when importing. If you feel that some of these
+  # should be different (especially more strict), please raise an issue!
+  CLANG
+    "-Werror"
+    "-Wall"
+
+    # Disable warnings we don't care about or that generally have a low
+    # signal/noise ratio.
+    "-Wno-ambiguous-member-template"
+    "-Wno-char-subscripts"
+    "-Wno-deprecated-declarations"
+    "-Wno-extern-c-compat" # Matches upstream. Cannot impact due to extern C inclusion method.
+    "-Wno-gnu-alignof-expression"
+    "-Wno-gnu-variable-sized-type-not-at-end"
+    "-Wno-ignored-optimization-argument"
+    "-Wno-invalid-offsetof" # Technically UB but needed for intrusive ptrs
+    "-Wno-invalid-source-encoding"
+    "-Wno-mismatched-tags"
+    "-Wno-pointer-sign"
+    "-Wno-reserved-user-defined-literal"
+    "-Wno-return-type-c-linkage"
+    "-Wno-self-assign-overloaded"
+    "-Wno-sign-compare"
+    "-Wno-signed-unsigned-wchar"
+    "-Wno-strict-overflow"
+    "-Wno-trigraphs"
+    "-Wno-unknown-pragmas"
+    "-Wno-unknown-warning-option"
+    "-Wno-unused-command-line-argument"
+    "-Wno-unused-const-variable"
+    "-Wno-unused-function"
+    "-Wno-unused-local-typedef"
+    "-Wno-unused-private-field"
+    "-Wno-user-defined-warnings"
+
+    # Explicitly enable some additional warnings.
+    # Some of these aren't on by default, or under -Wall, or are subsets of
+    # warnings turned off above.
+    "-Wctad-maybe-unsupported"
+    "-Wfloat-overflow-conversion"
+    "-Wfloat-zero-conversion"
+    "-Wfor-loop-analysis"
+    "-Wformat-security"
+    "-Wgnu-redeclared-enum"
+    "-Wimplicit-fallthrough"
+    "-Winfinite-recursion"
+    "-Wliteral-conversion"
+    "-Wnon-virtual-dtor"
+    "-Woverloaded-virtual"
+    "-Wself-assign"
+    "-Wstring-conversion"
+    "-Wtautological-overlap-compare"
+    "-Wthread-safety"
+    "-Wthread-safety-beta"
+    "-Wunused-comparison"
+    "-Wvla"
+
+  # Disable some warnings to get GCC to build. Until we have a CI for this, we
+  # just need it for releases and extra diagnostics (or Werror) only bring pain.
+  # TODO(#6959): Trim these down and add -Werror -Wall once we have a CI.
+  GCC
+    "-Wno-unused-but-set-parameter"
+    "-Wno-comment"
+    "-Wno-attributes"
+    "-Wno-strict-prototypes"
+    "-Wno-shadow-uncaptured-local"
+    "-Wno-gnu-zero-variadic-macro-arguments"
+    "-Wno-shadow-field-in-constructor"
+    "-Wno-unreachable-code-return"
+    "-Wno-missing-variable-declarations"
+    "-Wno-gnu-label-as-value"
+
+  MSVC_OR_CLANG_CL
+    # Default warning level (severe + significant + production quality).
+    # This does not include level 4, "informational", warnings or those that
+    # are off by default.
+    # https://docs.microsoft.com/en-us/cpp/build/reference/compiler-option-warning-level
+    # Note that we set CMake policy CMP0092 (if found), making this explicit:
+    # https://cmake.org/cmake/help/v3.15/policy/CMP0092.html
+    "/W3"
 
     # "nonstandard extension used : zero-sized array in struct/union"
     # This happens with unsized or zero-length arrays at the end of structs,
@@ -259,6 +266,16 @@
     "/wd5105"  # allow: macro expansion producing 'defined' has undefined behavior
 )
 
+# Set some things back to warnings that are really annoying as build errors
+# during active development (but we still want as errors on CI).
+if (IREE_DEV_MODE)
+  iree_select_compiler_opts(IREE_DEFAULT_COPTS
+    CLANG_OR_GCC
+      "-Wno-error=unused-parameter"
+      "-Wno-error=unused-variable"
+  )
+endif()
+
 # On MSVC, CMake sets /GR by default (enabling RTTI), but we set /GR-
 # (disabling it) above. To avoid Command line warning D9025 which warns about
 # overriding the flag value, we remove /GR from global CMake flags.
diff --git a/build_tools/cmake/iree_macros.cmake b/build_tools/cmake/iree_macros.cmake
index cacbaf1..f5049eb 100644
--- a/build_tools/cmake/iree_macros.cmake
+++ b/build_tools/cmake/iree_macros.cmake
@@ -156,7 +156,9 @@
     ""
     "ALL;CLANG;CLANG_CL;MSVC;GCC;CLANG_OR_GCC;MSVC_OR_CLANG_CL"
   )
-  set(_OPTS)
+  # OPTS is a variable containing the *name* of the variable being populated, so
+  # we need to dereference it twice.
+  set(_OPTS "${${OPTS}}")
   list(APPEND _OPTS "${_IREE_SELECTS_ALL}")
   if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
     list(APPEND _OPTS "${_IREE_SELECTS_GCC}")
diff --git a/docs/developers/get_started/cmake_options_and_variables.md b/docs/developers/get_started/cmake_options_and_variables.md
index 998b292..5fbf9ba 100644
--- a/docs/developers/get_started/cmake_options_and_variables.md
+++ b/docs/developers/get_started/cmake_options_and_variables.md
@@ -54,7 +54,7 @@
 
 Builds the IREE TFLite C API compatibility shim. Defaults to `ON`.
 
-### `IREE_BUILD_BINDINGS_TFLITE_JAVA`:BOOL
+#### `IREE_BUILD_BINDINGS_TFLITE_JAVA`:BOOL
 
 Builds the IREE TFLite Java bindings with the C API compatibility shim. Defaults to `ON`.
 
@@ -79,6 +79,12 @@
 build no target backends. Defaults to `all`. Example:
 `-DIREE_TARGET_BACKENDS_TO_BUILD=Vulkan-SPIRV;VMLA`.
 
+#### `IREE_DEV_MODE`:BOOL
+
+Configure settings to optimize for IREE development (as opposed to CI or
+release). Defaults to `OFF`. For example, this will downgrade some compiler
+diagnostics from errors to warnings.
+
 #### `IREE_ENABLE_LLD`:BOOL
 
 Use lld when linking. Defaults to `OFF`. This option is equivalent to