CMake: apply the warning treatment to msvc builds

* Respect WARNINGS_AS_ERRORS option for msvc builds.

* Disable a bunch of warnings for msvc builds. These were gathered from
  all the hand-crafted vcxproj files and merged into one set. Eventually
  we need to disable some of these and fix the actual warnings.

* Treat specific warnings as errors for msvc builds. These were gathered
  from the hand-crafted vcxproj files.

* Fix LLVM build showing warning 4141 despite being disabled in
  CMakeLists. Something wonky happens when using set with the first
  string argument. Going with list(APPEND,...) works, and is symmetrical
  with the non-msvc script.

* Define _SCL_SECURE_NO_WARNINGS for all modules, not just LLVM.

* Fix double to float conversion warning in SprivShader.cpp

* Fix mismatched type compare in IceTargetLoweringX664Traits.h

Bug: b/132445520
Change-Id: I1143e9fdd386e26d4f0b0f9e34c4d6b3ac2f2311
Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/31149
Presubmit-Ready: Antonio Maiorano <amaiorano@google.com>
Kokoro-Presubmit: kokoro <noreply+kokoro@google.com>
Tested-by: Antonio Maiorano <amaiorano@google.com>
Reviewed-by: Ben Clayton <bclayton@google.com>
Reviewed-by: Nicolas Capens <nicolascapens@google.com>
diff --git a/CMakeLists.txt b/CMakeLists.txt
index e5025b0..39b06dd 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -226,7 +226,39 @@
 if(MSVC)
     set_cpp_flag("/MP")
     add_definitions(-D_CRT_SECURE_NO_WARNINGS)
+    add_definitions(-D_SCL_SECURE_NO_WARNINGS)
     add_definitions(-D_SBCS)  # Single Byte Character Set (ASCII)
+
+    if(WARNINGS_AS_ERRORS)
+        set_cpp_flag("/WX") # Treat all warnings as errors
+    endif()
+
+    # Disable specific warnings
+    # TODO: Not all of these should be disabled, but for now, we want a warning-free msvc build. Remove these one by one
+    #       and fix the actual warnings in code.
+    list(APPEND SWIFTSHADER_COMPILE_OPTIONS
+        "/wd4005" # 'identifier' : macro redefinition
+        "/wd4018" # 'expression' : signed/unsigned mismatch
+        "/wd4141" # 'modifier' : used more than once
+        "/wd4146" # unary minus operator applied to unsigned type, result still unsigned
+        "/wd4244" # 'conversion' conversion from 'type1' to 'type2', possible loss of data
+        "/wd4267" # 'var' : conversion from 'size_t' to 'type', possible loss of data
+        "/wd4291" # 'void X new(size_t,unsigned int,unsigned int)': no matching operator delete found; memory will not be freed if initialization throws an exception
+        "/wd4309" # 'conversion' : truncation of constant value
+        "/wd4624" # 'derived class' : destructor was implicitly defined as deleted because a base class destructor is inaccessible or deleted
+        "/wd4800" # 'type' : forcing value to bool 'true' or 'false' (performance warning)
+        "/wd4838" # conversion from 'type_1' to 'type_2' requires a narrowing conversion
+        "/wd5030" # attribute 'attribute' is not recognized
+        "/wd5038" # data member 'member1' will be initialized after data member 'member2' data member 'member' will be initialized after base class 'base_class'
+    )
+
+    # Treat specific warnings as errors
+    list(APPEND SWIFTSHADER_COMPILE_OPTIONS
+        "/we4018" # 'expression' : signed/unsigned mismatch
+        "/we4838" # conversion from 'type_1' to 'type_2' requires a narrowing conversion
+        "/we5038" # data member 'member1' will be initialized after data member 'member2' data member 'member' will be initialized after base class 'base_class'
+    )
+
 else()
     set_cpp_flag("--std=c++11")
     set_cpp_flag("-fno-exceptions")
@@ -1236,7 +1268,7 @@
         "-Wno-unused-but-set-variable" # variable ‘X’ set but not used
     )
 elseif(CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
-    set(LLVM_DISABLE_WARNINGS
+    list(APPEND LLVM_COMPILE_OPTIONS
         "/wd4141" # 'inline': used more than once
         "/wd4146" # unary minus operator applied to unsigned type, result still unsigned
         "/wd4244" # 'X': conversion from 'uint64_t' to 'const unsigned int', possible loss of data
@@ -1247,7 +1279,6 @@
         "/wd4805" # 'X': unsafe mix of type 'unsigned int' and type 'bool' in operation
         "/wd4996" # 'X': Call to 'Y' with parameters that may be unsafe - this call relies on the caller to check that the passed values are correct.
     )
-    set(LLVM_COMPILE_OPTIONS "/D \"_SCL_SECURE_NO_WARNINGS\" ${LLVM_DISABLE_WARNINGS}")
 endif()
 
 add_library(llvm STATIC ${LLVM_LIST})
@@ -1369,6 +1400,13 @@
         list(APPEND SUBZERO_INCLUDE_DIR ${SUBZERO_LLVM_DIR}/build/MacOS/include/)
     endif()
 
+    if(WIN32)
+        list(APPEND SUBZERO_COMPILE_OPTIONS
+            "/wd4334" # ''operator' : result of 32-bit shift implicitly converted to 64 bits (was 64-bit shift intended?)
+            "/wd4996" # The POSIX name for this item is deprecated. Instead, use the ISO C and C++ conformant name: new_name.
+        )
+    endif()
+
     add_library(ReactorSubzero STATIC
         ${SUBZERO_LIST}
         ${SUBZERO_DEPENDENCIES_LIST}
@@ -1377,7 +1415,7 @@
     set_target_properties(ReactorSubzero PROPERTIES
         POSITION_INDEPENDENT_CODE 1
         INCLUDE_DIRECTORIES "${SUBZERO_INCLUDE_DIR}"
-        COMPILE_OPTIONS "${SWIFTSHADER_COMPILE_OPTIONS}"
+        COMPILE_OPTIONS "${SUBZERO_COMPILE_OPTIONS};${SWIFTSHADER_COMPILE_OPTIONS}"
         COMPILE_DEFINITIONS "SZTARGET=${SUBZERO_TARGET}; ALLOW_DUMP=0; ALLOW_TIMERS=0; ALLOW_LLVM_CL=0; ALLOW_LLVM_IR=0; ALLOW_LLVM_IR_AS_INPUT=0; ALLOW_MINIMAL_BUILD=0; ALLOW_WASM=0; ICE_THREAD_LOCAL_HACK=0;"
         FOLDER "Subzero"
     )
diff --git a/src/Pipeline/SpirvShader.cpp b/src/Pipeline/SpirvShader.cpp
index d16ea68..3ff890e 100644
--- a/src/Pipeline/SpirvShader.cpp
+++ b/src/Pipeline/SpirvShader.cpp
@@ -3210,7 +3210,7 @@
 			{
 				auto abs = Abs(src.Float(i));
 				auto sign = src.Int(i) & SIMD::Int(0x80000000);
-				auto isZero = CmpLT(abs, SIMD::Float(0.000061035));
+				auto isZero = CmpLT(abs, SIMD::Float(0.000061035f));
 				auto isInf  = CmpGT(abs, SIMD::Float(65504.0f));
 				auto isNaN  = IsNan(abs);
 				auto isInfOrNan = isInf | isNaN;
diff --git a/third_party/subzero/src/IceTargetLoweringX8664Traits.h b/third_party/subzero/src/IceTargetLoweringX8664Traits.h
index 0f152e5..ba67c69 100644
--- a/third_party/subzero/src/IceTargetLoweringX8664Traits.h
+++ b/third_party/subzero/src/IceTargetLoweringX8664Traits.h
@@ -545,7 +545,7 @@
       // still be used by the Target Lowering (e.g., base pointer), so the
       // register alias table still needs to be defined.
       (*RegisterAliases)[Entry.Val].resize(RegisterSet::Reg_NUM);
-      for (int J = 0; J < Entry.NumAliases; ++J) {
+      for (Ice::SizeT J = 0; J < Entry.NumAliases; ++J) {
         SizeT Alias = Entry.Aliases[J];
         assert(!(*RegisterAliases)[Entry.Val][Alias] && "Duplicate alias");
         (*RegisterAliases)[Entry.Val].set(Alias);