Non-functional: Rationalize some existing use of SPIRV-Tools.
diff --git a/SPIRV/GlslangToSpv.cpp b/SPIRV/GlslangToSpv.cpp
index 8e49e93..88569f5 100755
--- a/SPIRV/GlslangToSpv.cpp
+++ b/SPIRV/GlslangToSpv.cpp
@@ -3219,7 +3219,7 @@
// adjusting this late means inconsistencies with earlier code, which for reflection is an issue
// Until reflection is brought in sync with these adjustments, don't apply to $Global,
// which is the most likely to rely on reflection, and least likely to rely implicit layouts
- if (glslangIntermediate->usingHlslOFfsets() &&
+ if (glslangIntermediate->usingHlslOffsets() &&
! memberType.isArray() && memberType.isVector() && structType.getTypeName().compare("$Global") != 0) {
int dummySize;
int componentAlignment = glslangIntermediate->getBaseAlignmentScalar(memberType, dummySize);
@@ -7003,6 +7003,80 @@
GlslangToSpv(intermediate, spirv, &logger, options);
}
+#if ENABLE_OPT
+
+// Apply the SPIRV-Tools validator to generated SPIR-V.
+void SpirvToolsLegalize(const glslang::TIntermediate& intermediate, std::vector<unsigned int>& spirv,
+ spv::SpvBuildLogger* logger, const SpvOptions* options)
+{
+ spv_target_env target_env = SPV_ENV_UNIVERSAL_1_2;
+
+ spvtools::Optimizer optimizer(target_env);
+ optimizer.SetMessageConsumer(
+ [](spv_message_level_t level, const char *source, const spv_position_t &position, const char *message) {
+ auto &out = std::cerr;
+ switch (level)
+ {
+ case SPV_MSG_FATAL:
+ case SPV_MSG_INTERNAL_ERROR:
+ case SPV_MSG_ERROR:
+ out << "error: ";
+ break;
+ case SPV_MSG_WARNING:
+ out << "warning: ";
+ break;
+ case SPV_MSG_INFO:
+ case SPV_MSG_DEBUG:
+ out << "info: ";
+ break;
+ default:
+ break;
+ }
+ if (source)
+ {
+ out << source << ":";
+ }
+ out << position.line << ":" << position.column << ":" << position.index << ":";
+ if (message)
+ {
+ out << " " << message;
+ }
+ out << std::endl;
+ });
+
+ optimizer.RegisterPass(CreateMergeReturnPass());
+ optimizer.RegisterPass(CreateInlineExhaustivePass());
+ optimizer.RegisterPass(CreateEliminateDeadFunctionsPass());
+ optimizer.RegisterPass(CreateScalarReplacementPass());
+ optimizer.RegisterPass(CreateLocalAccessChainConvertPass());
+ optimizer.RegisterPass(CreateLocalSingleBlockLoadStoreElimPass());
+ optimizer.RegisterPass(CreateLocalSingleStoreElimPass());
+ optimizer.RegisterPass(CreateSimplificationPass());
+ optimizer.RegisterPass(CreateAggressiveDCEPass());
+ optimizer.RegisterPass(CreateVectorDCEPass());
+ optimizer.RegisterPass(CreateDeadInsertElimPass());
+ optimizer.RegisterPass(CreateAggressiveDCEPass());
+ optimizer.RegisterPass(CreateDeadBranchElimPass());
+ optimizer.RegisterPass(CreateBlockMergePass());
+ optimizer.RegisterPass(CreateLocalMultiStoreElimPass());
+ optimizer.RegisterPass(CreateIfConversionPass());
+ optimizer.RegisterPass(CreateSimplificationPass());
+ optimizer.RegisterPass(CreateAggressiveDCEPass());
+ optimizer.RegisterPass(CreateVectorDCEPass());
+ optimizer.RegisterPass(CreateDeadInsertElimPass());
+ if (options->optimizeSize) {
+ optimizer.RegisterPass(CreateRedundancyEliminationPass());
+ // TODO(greg-lunarg): Add this when AMD driver issues are resolved
+ // optimizer.RegisterPass(CreateCommonUniformElimPass());
+ }
+ optimizer.RegisterPass(CreateAggressiveDCEPass());
+ optimizer.RegisterPass(CreateCFGCleanupPass());
+
+ optimizer.Run(spirv.data(), spirv.size(), &spirv);
+}
+
+#endif
+
void GlslangToSpv(const glslang::TIntermediate& intermediate, std::vector<unsigned int>& spirv,
spv::SpvBuildLogger* logger, SpvOptions* options)
{
@@ -7025,74 +7099,9 @@
#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 ||
- options->optimizeSize) &&
+ if ((intermediate.getSource() == EShSourceHlsl || options->optimizeSize) &&
!options->disableOptimizer) {
- spv_target_env target_env = SPV_ENV_UNIVERSAL_1_2;
-
- spvtools::Optimizer optimizer(target_env);
- optimizer.SetMessageConsumer(
- [](spv_message_level_t level, const char *source, const spv_position_t &position, const char *message) {
- auto &out = std::cerr;
- switch (level)
- {
- case SPV_MSG_FATAL:
- case SPV_MSG_INTERNAL_ERROR:
- case SPV_MSG_ERROR:
- out << "error: ";
- break;
- case SPV_MSG_WARNING:
- out << "warning: ";
- break;
- case SPV_MSG_INFO:
- case SPV_MSG_DEBUG:
- out << "info: ";
- break;
- default:
- break;
- }
- if (source)
- {
- out << source << ":";
- }
- out << position.line << ":" << position.column << ":" << position.index << ":";
- if (message)
- {
- out << " " << message;
- }
- out << std::endl;
- });
-
- optimizer.RegisterPass(CreateMergeReturnPass());
- optimizer.RegisterPass(CreateInlineExhaustivePass());
- optimizer.RegisterPass(CreateEliminateDeadFunctionsPass());
- optimizer.RegisterPass(CreateScalarReplacementPass());
- optimizer.RegisterPass(CreateLocalAccessChainConvertPass());
- optimizer.RegisterPass(CreateLocalSingleBlockLoadStoreElimPass());
- optimizer.RegisterPass(CreateLocalSingleStoreElimPass());
- optimizer.RegisterPass(CreateSimplificationPass());
- optimizer.RegisterPass(CreateAggressiveDCEPass());
- optimizer.RegisterPass(CreateVectorDCEPass());
- optimizer.RegisterPass(CreateDeadInsertElimPass());
- optimizer.RegisterPass(CreateAggressiveDCEPass());
- optimizer.RegisterPass(CreateDeadBranchElimPass());
- optimizer.RegisterPass(CreateBlockMergePass());
- optimizer.RegisterPass(CreateLocalMultiStoreElimPass());
- optimizer.RegisterPass(CreateIfConversionPass());
- optimizer.RegisterPass(CreateSimplificationPass());
- optimizer.RegisterPass(CreateAggressiveDCEPass());
- optimizer.RegisterPass(CreateVectorDCEPass());
- optimizer.RegisterPass(CreateDeadInsertElimPass());
- if (options->optimizeSize) {
- optimizer.RegisterPass(CreateRedundancyEliminationPass());
- // TODO(greg-lunarg): Add this when AMD driver issues are resolved
- // optimizer.RegisterPass(CreateCommonUniformElimPass());
- }
- optimizer.RegisterPass(CreateAggressiveDCEPass());
- optimizer.RegisterPass(CreateCFGCleanupPass());
-
- if (!optimizer.Run(spirv.data(), spirv.size(), &spirv))
- return;
+ SpirvToolsLegalize(intermediate, spirv, logger, options);
}
#endif
diff --git a/SPIRV/disassemble.cpp b/SPIRV/disassemble.cpp
index a8efd69..b64584d 100755
--- a/SPIRV/disassemble.cpp
+++ b/SPIRV/disassemble.cpp
@@ -723,16 +723,23 @@
// Use the SPIRV-Tools disassembler to print SPIR-V.
void SpirvToolsDisassemble(std::ostream& out, const std::vector<unsigned int>& spirv)
{
+ // disassemble
spv_context context = spvContextCreate(SPV_ENV_UNIVERSAL_1_3);
spv_text text;
spv_diagnostic diagnostic = nullptr;
- spvBinaryToText(context, &spirv.front(), spirv.size(),
+ spvBinaryToText(context, spirv.data(), spirv.size(),
SPV_BINARY_TO_TEXT_OPTION_FRIENDLY_NAMES | SPV_BINARY_TO_TEXT_OPTION_INDENT,
&text, &diagnostic);
+
+ // dump
if (diagnostic == nullptr)
out << text->str;
else
spvDiagnosticPrint(diagnostic);
+
+ // teardown
+ spvDiagnosticDestroy(diagnostic);
+ spvContextDestroy(context);
}
#endif
diff --git a/glslang/MachineIndependent/localintermediate.h b/glslang/MachineIndependent/localintermediate.h
index 9cbc4c8..143d1af 100755
--- a/glslang/MachineIndependent/localintermediate.h
+++ b/glslang/MachineIndependent/localintermediate.h
@@ -349,7 +349,7 @@
if (hlslOffsets)
processes.addProcess("hlsl-offsets");
}
- bool usingHlslOFfsets() const { return hlslOffsets; }
+ bool usingHlslOffsets() const { return hlslOffsets; }
void setUseStorageBuffer()
{
useStorageBuffer = true;