summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGangzheng Tong <tonggangzheng@gmail.com>2025-09-15 18:46:59 -0700
committerGitHub <noreply@github.com>2025-09-16 01:46:59 +0000
commit8ad0ae17880480abe587617c997ab28b23abc146 (patch)
tree2e4449d239d26f30e95520a6ff7bf5aac814924f
parent8bcf6c443bbd24498e67ae54f06c2ad933421738 (diff)
Enable static mimalloc in Spriv-Tools (#8419)
* Added optional mimalloc integration for SPIRV-Tools to improve compilation performance * Cloning `mimalloc` repo to external/ on demand during cmake config * Enabled by default on Windows, configurable via `SLANG_ENABLE_SPIRV_TOOLS_MIMALLOC` CMake option, to enable static mimalloc build in spirv-tools. * There are some crashes in Linux and Mac with static mimalloc enabled, likely due to the system malloc and mimalloc mixed usage. This might be expected as mimalloc in spirv-tools is not extensively tested according to https://github.com/KhronosGroup/SPIRV-Tools?tab=readme-ov-file#dependency-on-mimalloc. So by default only Windows has this enabled. Close: https://github.com/shader-slang/slang/issues/8158 --------- Co-authored-by: slangbot <ellieh+slangbot@nvidia.com> Co-authored-by: slangbot <186143334+slangbot@users.noreply.github.com>
-rw-r--r--CMakeLists.txt5
-rw-r--r--external/CMakeLists.txt65
2 files changed, 70 insertions, 0 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 1ab208c2b..ab4556550 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -185,6 +185,11 @@ advanced_option(
"Build using system glslang library"
OFF
)
+advanced_option(
+ SLANG_ENABLE_SPIRV_TOOLS_MIMALLOC
+ "Enable mimalloc for SPIRV-Tools to improve compilation performance"
+ OFF
+)
option(
SLANG_SPIRV_HEADERS_INCLUDE_DIR
diff --git a/external/CMakeLists.txt b/external/CMakeLists.txt
index faa8a5730..fed9e5e4e 100644
--- a/external/CMakeLists.txt
+++ b/external/CMakeLists.txt
@@ -25,6 +25,15 @@ if(NOT SLANG_ENABLE_EXTERNAL_COMPILER_WARNINGS)
endif()
endif()
+# Option to enable mimalloc for SPIRV-Tools to improve compilation performance
+if(WIN32)
+ option(
+ SLANG_ENABLE_SPIRV_TOOLS_MIMALLOC
+ "Enable mimalloc for SPIRV-Tools to improve compilation performance"
+ ON
+ )
+endif()
+
# Unordered dense
if(NOT ${SLANG_USE_SYSTEM_UNORDERED_DENSE})
if(NOT SLANG_OVERRIDE_UNORDERED_DENSE_PATH)
@@ -147,6 +156,62 @@ if(SLANG_ENABLE_SLANG_GLSLANG)
set(SPIRV_TOOLS_BUILD_STATIC ON)
set(SPIRV_WERROR OFF)
set(SPIRV_SKIP_TESTS ON)
+
+ # Enable mimalloc for SPIRV-Tools to improve compilation performance
+ # Based on SPIRV-Tools PR #6267: https://github.com/KhronosGroup/SPIRV-Tools/pull/6267
+ if(SLANG_ENABLE_SPIRV_TOOLS_MIMALLOC)
+ # Place mimalloc in slang/external instead of spirv-tools/external for better organization
+ set(MIMALLOC_PATH "${CMAKE_CURRENT_SOURCE_DIR}/mimalloc")
+
+ # Download mimalloc if it's not available
+ if(NOT EXISTS "${MIMALLOC_PATH}/CMakeLists.txt")
+ message(
+ STATUS
+ "mimalloc not found at ${MIMALLOC_PATH} - downloading..."
+ )
+ execute_process(
+ COMMAND
+ ${GIT_EXECUTABLE} clone --depth 1 --branch v2.1.7
+ https://github.com/microsoft/mimalloc.git
+ "${MIMALLOC_PATH}"
+ RESULT_VARIABLE git_result
+ OUTPUT_QUIET
+ ERROR_QUIET
+ )
+ if(git_result EQUAL 0)
+ # Point SPIRV-Tools to our mimalloc location
+ set(mimalloc_SOURCE_DIR "${MIMALLOC_PATH}")
+ set(SPIRV_TOOLS_USE_MIMALLOC ON)
+ set(SPIRV_TOOLS_USE_MIMALLOC_IN_STATIC_BUILD ON)
+ # set(MI_INSTALL_TOPLEVEL OFF)
+ message(
+ STATUS
+ "Successfully downloaded and enabled mimalloc for SPIRV-Tools at ${MIMALLOC_PATH}"
+ )
+ else()
+ message(
+ WARNING
+ "Failed to download mimalloc. Building SPIRV-Tools without mimalloc support."
+ )
+ endif()
+ endif()
+
+ # Check if mimalloc is available now
+ if(EXISTS "${MIMALLOC_PATH}/CMakeLists.txt")
+ # Point SPIRV-Tools to our mimalloc location
+ set(mimalloc_SOURCE_DIR "${MIMALLOC_PATH}")
+ set(SPIRV_TOOLS_USE_MIMALLOC ON)
+ # Also enable mimalloc in static builds for better performance
+ set(SPIRV_TOOLS_USE_MIMALLOC_IN_STATIC_BUILD ON)
+ # Disable mimalloc installation to avoid install target conflicts
+ # set(MI_INSTALL_TOPLEVEL OFF)
+ message(
+ STATUS
+ "Enabling mimalloc for SPIRV-Tools (including static builds) from ${MIMALLOC_PATH}"
+ )
+ endif()
+ endif()
+
# Tools
if(NOT SLANG_OVERRIDE_SPIRV_TOOLS_PATH)
add_subdirectory(spirv-tools EXCLUDE_FROM_ALL ${system})