Skip to content

Commit 88d93c9

Browse files
committed
Added SPIRV-Tools to perform optimizations.
This provides much more advanced optimizations than the remap step. Optimization now has two levels: dead-code elimination only or full optimizations. Added update-submodules.sh script to handle submodules. This ensures that the SPIRV-Headers submodule for SPIRV-Tools is properly set up. Installs for static libraries should now include the proper find_dependency() calls for both boost and SPIRV-Tools. Updated CI build steps to build all relevant configurations, including debug, release, static, shared, and 32-bit and 64-bit for Windows.
1 parent e02b887 commit 88d93c9

File tree

12 files changed

+256
-68
lines changed

12 files changed

+256
-68
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,6 @@
44
[submodule "SPIRV-Cross"]
55
path = Compile/SPIRV-Cross
66
url = https://github.com/KhronosGroup/SPIRV-Cross
7+
[submodule "Compile/SPIRV-Tools"]
8+
path = Compile/SPIRV-Tools
9+
url = https://github.com/akb825/SPIRV-Tools.git

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ set(MSL_EXPORTS_DIR ${CMAKE_BINARY_DIR}/cmake CACHE PATH
1818
"Folder for placing the cmake exports while building. Useful when embedding in other projects.")
1919
set(MSL_ROOT_FOLDER MSL CACHE STRING
2020
"Root folder for the MSL projects. Usefull when embedding in other projects.")
21-
set(MSL_INSTALL TRUE CACHE BOOL "Allow installation for MSL components.")
21+
set(MSL_INSTALL ON CACHE BOOL "Allow installation for MSL components.")
2222

2323
if (MSL_BUILD_TESTS)
2424
find_package(GTest QUIET)

Compile/CMakeLists.txt

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ file(GLOB spirvCrossSources ${SPIRV_CROSS_DIR}/spirv_*.cpp ${SPIRV_CROSS_DIR}/sp
3737
set(externalSources
3838
${glslangSources} ${glslangOsSources} ${glslResourceSources} ${spirvCrossSources})
3939

40+
# SPIRV-Tools has too complex of a build configuration to just embed the source in this library,
41+
# but the CMakeLists.txt is set up to properly manage library exports. Only need to install the
42+
# libraries if a static build, though.
43+
set(SPIRV_SKIP_EXECUTABLES ON CACHE BOOL "")
44+
set(SPIRV_SKIP_TESTS ON CACHE BOOL "")
45+
set(SKIP_SPIRV_TOOLS_INSTALL ${MSL_SHARED} CACHE BOOL "")
46+
add_subdirectory(SPIRV-Tools)
47+
4048
# Don't care about warnings for external files.
4149
if (MSVC)
4250
set_source_files_properties(${externalSources} PROPERTIES COMPILE_FLAGS /w)
@@ -55,15 +63,22 @@ target_include_directories(msl_compile
5563
${SHARED_DIR}
5664
src)
5765
target_link_libraries(msl_compile
58-
PRIVATE ${Boost_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT})
66+
PRIVATE SPIRV-Tools-opt ${Boost_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT})
5967

6068
msl_set_folder(msl_compile libs)
6169
msl_setup_filters(SRC_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src
6270
INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/include/MSL/Compile
6371
FILES ${sources})
6472
msl_setup_filters(SRC_DIR ${CMAKE_CURRENT_SOURCE_DIR} FILES ${externalSources})
6573

66-
msl_install_library(TARGET msl_compile MODULE Compile)
74+
if (NOT MSL_SHARED)
75+
# Only need to include the external dependencies for static library build so it can be linked.
76+
set(externalDepends EXTERNAL_DEPENDS SPIRV-Tools-opt BOOST_DEPENDS system thread filesystem
77+
wave)
78+
else()
79+
set(externalDepends)
80+
endif()
81+
msl_install_library(TARGET msl_compile MODULE Compile ${externalDepends})
6782
set(MSL_DOC_PROJECTS ${MSL_DOC_PROJECTS} Compile PARENT_SCOPE)
6883

6984
add_subdirectory(test)

Compile/SPIRV-Tools

Submodule SPIRV-Tools added at 141b35e

Compile/include/MSL/Compile/Target.h

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,16 @@ class MSL_COMPILE_EXPORT Target
117117
static const unsigned int featureCount =
118118
static_cast<unsigned int>(Feature::EarlyFragmentTests) + 1;
119119

120+
/**
121+
* @brief Enum for the optimization passes to run.
122+
*/
123+
enum class Optimize
124+
{
125+
None, ///< Don't perform any optimizations.
126+
Minimal, ///< Minimal optimizations such as dead-code removal.
127+
Full ///< Full optimization passes.
128+
};
129+
120130
/**
121131
* @brief Information about a feature.
122132
*
@@ -308,24 +318,16 @@ class MSL_COMPILE_EXPORT Target
308318
void setRemapVariables(bool remap);
309319

310320
/**
311-
* @brief Retruns whether or not to optimize the output.
312-
*
313-
* This will do some simple optimizations on the SPIR-V code. This can also be used for targets
314-
* when passing through other tools.
315-
*
316-
* @return True to optimize.
321+
* @brief Retruns the optimization mode.
322+
* @return The optimization mode.
317323
*/
318-
bool getOptimize() const;
324+
Optimize getOptimize() const;
319325

320326
/**
321-
* @brief Sets whether or not to optimize the output.
322-
*
323-
* This will do some simple optimizations on the SPIR-V code. This can also be used for targets
324-
* when passing through other tools.
325-
*
326-
* @param optimize True to optimize.
327+
* @brief Sets the optimization mode.
328+
* @param optimize The optimization mode.
327329
*/
328-
void setOptimize(bool optimize);
330+
void setOptimize(Optimize optimize);
329331

330332
/**
331333
* @brief Returns whether or not to strip the debug symbols from SPIR-V.
@@ -491,10 +493,10 @@ class MSL_COMPILE_EXPORT Target
491493
std::string m_spirVToolCommand;
492494

493495
bool m_remapVariables;
494-
bool m_optimize;
495496
bool m_stripDebug;
496497
bool m_dummyBindings;
497498
bool m_adjustableBindings;
499+
Optimize m_optimize;
498500
std::string m_resourcesFile;
499501
};
500502

Compile/src/Compiler.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#include <atomic>
2323
#include <cstring>
2424

25+
#include <spirv-tools/optimizer.hpp>
26+
2527
#if MSL_GCC || MSL_CLANG
2628
#pragma GCC diagnostic push
2729
#pragma GCC diagnostic ignored "-Wconversion"
@@ -338,13 +340,20 @@ void Compiler::process(SpirV& spirv, int processOptions)
338340
options |= spv::spirvbin_t::MAP_ALL;
339341
if (processOptions & DeadCodeElimination)
340342
options |= spv::spirvbin_t::DCE_ALL;
341-
if (processOptions & Optimize)
342-
options |= spv::spirvbin_t::OPT_ALL;
343343
if (processOptions & StripDebug)
344344
options |= spv::spirvbin_t::STRIP;
345345

346346
spv::spirvbin_t remapper;
347347
remapper.remap(spirv, options);
348+
349+
if (processOptions & Optimize)
350+
{
351+
spvtools::Optimizer optimizer(SPV_ENV_VULKAN_1_0);
352+
optimizer.RegisterPerformancePasses();
353+
SpirV optimizedSpirV;
354+
if (optimizer.Run(spirv.data(), spirv.size()*sizeof(std::uint32_t), &optimizedSpirV))
355+
spirv = std::move(optimizedSpirV);
356+
}
348357
}
349358

350359
} // namespace msl

Compile/src/Target.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -440,10 +440,10 @@ const Target::FeatureInfo& Target::getFeatureInfo(Target::Feature feature)
440440

441441
Target::Target()
442442
: m_remapVariables(false)
443-
, m_optimize(false)
444443
, m_stripDebug(false)
445444
, m_dummyBindings(false)
446445
, m_adjustableBindings(false)
446+
, m_optimize(Optimize::None)
447447
{
448448
Compiler::initialize();
449449
m_featureStates.fill(State::Default);
@@ -550,12 +550,12 @@ void Target::setRemapVariables(bool remap)
550550
m_remapVariables = remap;
551551
}
552552

553-
bool Target::getOptimize() const
553+
Target::Optimize Target::getOptimize() const
554554
{
555555
return m_optimize;
556556
}
557557

558-
void Target::setOptimize(bool optimize)
558+
void Target::setOptimize(Optimize optimize)
559559
{
560560
m_optimize = optimize;
561561
}
@@ -724,12 +724,16 @@ bool Target::compileImpl(CompiledResult& result, Output& output, Parser& parser,
724724
int processOptions = 0;
725725
if (m_remapVariables)
726726
processOptions |= Compiler::RemapVariables;
727-
if (m_optimize)
727+
switch (m_optimize)
728728
{
729-
// NOTE: Optimization may end up renaming interface variables.
730-
if (!needsReflectionNames())
731-
processOptions |= Compiler::Optimize;
732-
processOptions |= Compiler::DeadCodeElimination;
729+
case Optimize::None:
730+
break;
731+
case Optimize::Minimal:
732+
processOptions |= Compiler::DeadCodeElimination;
733+
break;
734+
case Optimize::Full:
735+
processOptions |= Compiler::DeadCodeElimination | Compiler::Optimize;
736+
break;
733737
}
734738

735739
SpirVProcessor::Strip strip;

README.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ The following software is required to build MSL:
4343
* [boost](http://www.boost.org/) (required unless only building client library without tests)
4444
* [glslang](https://github.com/KhronosGroup/glslang) (required for compiler, provided as submodule)
4545
* [SPIRV-Cross](https://github.com/KhronosGroup/SPIRV-Cross) (required for compiler, provided as submodule)
46+
* [SPIRV-Tools](https://github.com/KhronosGroup/SPIRV-Tools) (required for compiler, provided as submodule)
4647
* [FlatBuffers](https://google.github.io/flatbuffers/) (required if changing the schema)
4748
* [doxygen](http://www.stack.nl/~dimitri/doxygen/) (optional)
4849
* [gtest](https://github.com/google/googletest) (optional)
@@ -51,10 +52,7 @@ The following software is required to build MSL:
5152
5253
> **Note:** When Boost is manually installed, the appropriate variables should be set. In the case of Windows, the `BOOST_LIBRARYDIR` and `BOOST_ROOT` variables should be set. (examples: `BOOST_LIBRARYDIR=C:\local\boost_1_64_0\lib64-msvc-14.1` and `BOOST_ROOT=C:\local\boost_1_64_0`) In Windows, the value of `BOOST_LIBRARYDIR` should also be on `PATH` to ensure the DLLs can be loaded.
5354
54-
The glslang and SPIRV-Cross submodules can be grabbed by running the commands
55-
56-
ModularShaderLanguage$ git submodule init
57-
ModularShaderLanguage$ git submodule update
55+
The glslang, SPIRV-Cross, and SPIRV-Tools submodules can be grabbed by running script `update-submodules.sh`.
5856

5957
Additionally, additional tools such as Microsoft's HLSL compiler and Apple's Metal compiler will be required when compiling shaders for certain platforms.
6058

0 commit comments

Comments
 (0)