diff --git a/CMakeLists.txt b/CMakeLists.txt
index 0d453cc6efd64fe998969c1bf74486c68ff46b82..d54ff9bd01934fa1d8283bd5df50ac39847260a1 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -94,9 +94,10 @@ endif()
 
 if(ENABLE_OPT)
     message(STATUS "optimizer enabled")
-    add_definitions(-DENABLE_OPT)
+    add_definitions(-DENABLE_OPT=1)
 elseif(ENABLE_HLSL)
     message(STATUS "spirv-tools not linked - illegal SPIRV may be generated for HLSL")
+    add_definitions(-DENABLE_OPT=0)
 endif()
 
 add_subdirectory(glslang)
diff --git a/SPIRV/GlslangToSpv.cpp b/SPIRV/GlslangToSpv.cpp
index 56bc4cf7ff5a413358e7e4f8f35e0f49ce06ce94..d39c260907d068ca6a097d4ade03e092250ee244 100644
--- a/SPIRV/GlslangToSpv.cpp
+++ b/SPIRV/GlslangToSpv.cpp
@@ -54,12 +54,12 @@ namespace spv {
 #endif
 }
 
-#ifdef ENABLE_OPT
+#if ENABLE_OPT
     #include "spirv-tools/optimizer.hpp"
     #include "message.h"
 #endif
 
-#ifdef ENABLE_OPT
+#if ENABLE_OPT
 using namespace spvtools;
 #endif
 
@@ -6757,7 +6757,7 @@ void GlslangToSpv(const glslang::TIntermediate& intermediate, std::vector<unsign
     it.finishSpv();
     it.dumpSpv(spirv);
 
-#ifdef ENABLE_OPT
+#if ENABLE_OPT
     // If from HLSL, run spirv-opt to "legalize" the SPIR-V for Vulkan
     // eg. forward and remove memory writes of opaque types.
     if ((intermediate.getSource() == EShSourceHlsl ||
diff --git a/StandAlone/StandAlone.cpp b/StandAlone/StandAlone.cpp
index 356e016e8dc2dd32c83c8238bd9f10fe0aae17a2..bb38e1cf5e885edec8001473e021ccb3f0694f95 100644
--- a/StandAlone/StandAlone.cpp
+++ b/StandAlone/StandAlone.cpp
@@ -574,7 +574,7 @@ void ProcessArguments(std::vector<std::unique_ptr<glslang::TWorkItem>>& workItem
                 if (argv[0][2] == 'd')
                     Options |= EOptionOptimizeDisable;
                 else if (argv[0][2] == 's')
-#ifdef ENABLE_OPT
+#if ENABLE_OPT
                     Options |= EOptionOptimizeSize;
 #else
                     Error("-Os not available; optimizer not linked");
@@ -720,6 +720,8 @@ void SetMessageOptions(EShMessages& messages)
         messages = (EShMessages)(messages | EShMsgDebugInfo);
     if (HlslEnable16BitTypes)
         messages = (EShMessages)(messages | EShMsgHlslEnable16BitTypes);
+    if ((Options & EOptionOptimizeDisable) || !ENABLE_OPT)
+        messages = (EShMessages)(messages | EShMsgHlslLegalization);
 }
 
 //
diff --git a/Test/baseLegalResults/hlsl.aliasOpaque.frag.out b/Test/baseLegalResults/hlsl.aliasOpaque.frag.out
index 895b3373d31e1b1aac9d403bf31bb23719b1d9a0..f877db6787ad1eef7ba2145e3cf73e2643674f93 100644
--- a/Test/baseLegalResults/hlsl.aliasOpaque.frag.out
+++ b/Test/baseLegalResults/hlsl.aliasOpaque.frag.out
@@ -1,5 +1,4 @@
 hlsl.aliasOpaque.frag
-WARNING: AST will form illegal SPIR-V; need to transform to legalize
 // Module Version 10000
 // Generated by (magic number): 80006
 // Id's are bound by 87
diff --git a/Test/baseLegalResults/hlsl.flattenOpaque.frag.out b/Test/baseLegalResults/hlsl.flattenOpaque.frag.out
index 3ad7f1dd755641fc3216f2650d564d809511e480..ab9237b1e0b55c578101650301547a27b04dec96 100644
--- a/Test/baseLegalResults/hlsl.flattenOpaque.frag.out
+++ b/Test/baseLegalResults/hlsl.flattenOpaque.frag.out
@@ -1,5 +1,4 @@
 hlsl.flattenOpaque.frag
-WARNING: AST will form illegal SPIR-V; need to transform to legalize
 // Module Version 10000
 // Generated by (magic number): 80006
 // Id's are bound by 185
diff --git a/Test/baseLegalResults/hlsl.flattenOpaqueInit.vert.out b/Test/baseLegalResults/hlsl.flattenOpaqueInit.vert.out
index d4a985de4d2e4fa0b7993ec7f06352c436a1753e..3d0a0bd0857da23359c5eb73d9f9d0ea4315f259 100644
--- a/Test/baseLegalResults/hlsl.flattenOpaqueInit.vert.out
+++ b/Test/baseLegalResults/hlsl.flattenOpaqueInit.vert.out
@@ -1,5 +1,4 @@
 hlsl.flattenOpaqueInit.vert
-WARNING: AST will form illegal SPIR-V; need to transform to legalize
 // Module Version 10000
 // Generated by (magic number): 80006
 // Id's are bound by 134
diff --git a/Test/baseLegalResults/hlsl.flattenOpaqueInitMix.vert.out b/Test/baseLegalResults/hlsl.flattenOpaqueInitMix.vert.out
index e3568e5a934e5340d7a7021414a0fd4786a65e29..9793d575521ace6e7d8428ebfc4f9e146e6c3fc4 100644
--- a/Test/baseLegalResults/hlsl.flattenOpaqueInitMix.vert.out
+++ b/Test/baseLegalResults/hlsl.flattenOpaqueInitMix.vert.out
@@ -1,5 +1,4 @@
 hlsl.flattenOpaqueInitMix.vert
-WARNING: AST will form illegal SPIR-V; need to transform to legalize
 // Module Version 10000
 // Generated by (magic number): 80006
 // Id's are bound by 97
diff --git a/Test/baseLegalResults/hlsl.flattenSubset.frag.out b/Test/baseLegalResults/hlsl.flattenSubset.frag.out
index 3d2bee3495eda14140729a6c112a116201587129..617c719e424fe71e31a70bec4e06b79f8e96b9d1 100755
--- a/Test/baseLegalResults/hlsl.flattenSubset.frag.out
+++ b/Test/baseLegalResults/hlsl.flattenSubset.frag.out
@@ -1,5 +1,4 @@
 hlsl.flattenSubset.frag
-WARNING: AST will form illegal SPIR-V; need to transform to legalize
 // Module Version 10000
 // Generated by (magic number): 80006
 // Id's are bound by 66
diff --git a/Test/baseLegalResults/hlsl.flattenSubset2.frag.out b/Test/baseLegalResults/hlsl.flattenSubset2.frag.out
index 5823bc5c538fa3d1c92e4958c8aeec9047b80958..ef661ba6af5c6c227a5c054a62a4b8814c436274 100755
--- a/Test/baseLegalResults/hlsl.flattenSubset2.frag.out
+++ b/Test/baseLegalResults/hlsl.flattenSubset2.frag.out
@@ -1,5 +1,4 @@
 hlsl.flattenSubset2.frag
-WARNING: AST will form illegal SPIR-V; need to transform to legalize
 // Module Version 10000
 // Generated by (magic number): 80006
 // Id's are bound by 53
diff --git a/Test/baseLegalResults/hlsl.partialFlattenLocal.vert.out b/Test/baseLegalResults/hlsl.partialFlattenLocal.vert.out
index cae5d073ca295d36f991c10e55ec3ed95106d951..8bf9614dcb8def3fb59acdb5ac3831cefbf06e00 100755
--- a/Test/baseLegalResults/hlsl.partialFlattenLocal.vert.out
+++ b/Test/baseLegalResults/hlsl.partialFlattenLocal.vert.out
@@ -1,5 +1,4 @@
 hlsl.partialFlattenLocal.vert
-WARNING: AST will form illegal SPIR-V; need to transform to legalize
 // Module Version 10000
 // Generated by (magic number): 80006
 // Id's are bound by 169
diff --git a/Test/baseLegalResults/hlsl.partialFlattenMixed.vert.out b/Test/baseLegalResults/hlsl.partialFlattenMixed.vert.out
index 1662eca39823ecfb19885cb4608d9161e0184b71..7e36eb9c50056045f26f2ff92a2e71b67247a7c0 100755
--- a/Test/baseLegalResults/hlsl.partialFlattenMixed.vert.out
+++ b/Test/baseLegalResults/hlsl.partialFlattenMixed.vert.out
@@ -1,5 +1,4 @@
 hlsl.partialFlattenMixed.vert
-WARNING: AST will form illegal SPIR-V; need to transform to legalize
 // Module Version 10000
 // Generated by (magic number): 80006
 // Id's are bound by 36
diff --git a/glslang/Public/ShaderLang.h b/glslang/Public/ShaderLang.h
index ef6c07458d8f70c3de0efd251de017d097cf37b3..732c17ad8f3261c9477f8474b58133dac4c8d337 100644
--- a/glslang/Public/ShaderLang.h
+++ b/glslang/Public/ShaderLang.h
@@ -215,6 +215,7 @@ enum EShMessages {
     EShMsgHlslOffsets      = (1 << 9),  // allow block offsets to follow HLSL rules instead of GLSL rules
     EShMsgDebugInfo        = (1 << 10), // save debug information
     EShMsgHlslEnable16BitTypes  = (1 << 11), // enable use of 16-bit types in SPIR-V for HLSL
+    EShMsgHlslLegalization  = (1 << 12), // enable HLSL Legalization messages
 };
 
 //
diff --git a/gtests/Hlsl.FromFile.cpp b/gtests/Hlsl.FromFile.cpp
index 068338ae89a37a636ea713eeb5f8b005c9a0e49d..79e9592ced6cf5637f4d25cb4c9902bf7bb4d06c 100644
--- a/gtests/Hlsl.FromFile.cpp
+++ b/gtests/Hlsl.FromFile.cpp
@@ -92,7 +92,7 @@ TEST_P(HlslLegalizeTest, FromFile)
     loadFileCompileAndCheck(GlobalTestSettings.testRoot, GetParam().fileName,
                             Source::HLSL, Semantics::Vulkan, glslang::EShTargetVulkan_1_0,
                             Target::Spv, true, GetParam().entryPoint,
-                            "/baseLegalResults/", false);
+                            "/baseLegalResults/", true);
 }
 
 // clang-format off
@@ -410,7 +410,7 @@ INSTANTIATE_TEST_CASE_P(
 );
 // clang-format on
 
-#ifdef ENABLE_OPT
+#if ENABLE_OPT
 // clang-format off
 INSTANTIATE_TEST_CASE_P(
     ToSpirv, HlslLegalizeTest,
diff --git a/gtests/TestFixture.cpp b/gtests/TestFixture.cpp
index db2b81d1a0565c1be4e11f9f0e4158a8fcefe35c..77bada5f7dea443311ec3e932dce85427759635e 100644
--- a/gtests/TestFixture.cpp
+++ b/gtests/TestFixture.cpp
@@ -100,6 +100,8 @@ EShMessages DeriveOptions(Source source, Semantics semantics, Target target)
             break;
     }
 
+    result = static_cast<EShMessages>(result | EShMsgHlslLegalization);
+
     return result;
 }
 
diff --git a/gtests/TestFixture.h b/gtests/TestFixture.h
index 0e984d4e56f1100c6e64e10214a5b47c3a1f7935..a58978d3390b8b7162ff62f881e98fd9479e5855 100644
--- a/gtests/TestFixture.h
+++ b/gtests/TestFixture.h
@@ -200,7 +200,7 @@ public:
             glslang::EShTargetClientVersion clientTargetVersion,
             bool flattenUniformArrays = false,
             EShTextureSamplerTransformMode texSampTransMode = EShTexSampTransKeep,
-            bool disableOptimizer = true,
+            bool enableOptimizer = false,
             bool automap = true)
     {
         const EShLanguage stage = GetShaderStage(GetSuffix(shaderName));
@@ -242,7 +242,7 @@ public:
         if (success && (controls & EShMsgSpvRules)) {
             std::vector<uint32_t> spirv_binary;
             glslang::SpvOptions options;
-            options.disableOptimizer = disableOptimizer;
+            options.disableOptimizer = !enableOptimizer;
             glslang::GlslangToSpv(*program.getIntermediate(stage),
                                   spirv_binary, &logger, &options);
 
@@ -412,7 +412,7 @@ public:
                                  bool automap = true,
                                  const std::string& entryPointName="",
                                  const std::string& baseDir="/baseResults/",
-                                 const bool disableOptimizer = true)
+                                 const bool enableOptimizer = false)
     {
         const std::string inputFname = testDir + "/" + testName;
         const std::string expectedOutputFname =
@@ -422,9 +422,11 @@ public:
         tryLoadFile(inputFname, "input", &input);
         tryLoadFile(expectedOutputFname, "expected output", &expectedOutput);
 
-        const EShMessages controls = DeriveOptions(source, semantics, target);
+        EShMessages controls = DeriveOptions(source, semantics, target);
+        if (enableOptimizer)
+            controls = static_cast<EShMessages>(controls & ~EShMsgHlslLegalization);
         GlslangResult result = compileAndLink(testName, input, entryPointName, controls, clientTargetVersion, false,
-                                              EShTexSampTransKeep, disableOptimizer, automap);
+                                              EShTexSampTransKeep, enableOptimizer, automap);
 
         // Generate the hybrid output in the way of glslangValidator.
         std::ostringstream stream;
diff --git a/hlsl/hlslParseHelper.cpp b/hlsl/hlslParseHelper.cpp
index 1b9cc538bf6589b554786365fd3de4f8aa0d92fd..5738321a78fd64eb73c2856f3aea310561fce745 100755
--- a/hlsl/hlslParseHelper.cpp
+++ b/hlsl/hlslParseHelper.cpp
@@ -9924,7 +9924,7 @@ void HlslParseContext::finish()
 
     // Communicate out (esp. for command line) that we formed AST that will make
     // illegal AST SPIR-V and it needs transforms to legalize it.
-    if (intermediate.needsLegalization())
+    if (intermediate.needsLegalization() && (messages & EShMsgHlslLegalization))
         infoSink.info << "WARNING: AST will form illegal SPIR-V; need to transform to legalize";
 
     TParseContextBase::finish();