Skip to content

Commit 8ad0ae1

Browse files
gtong-nvslangbot
andauthored
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: #8158 --------- Co-authored-by: slangbot <[email protected]> Co-authored-by: slangbot <[email protected]>
1 parent 8bcf6c4 commit 8ad0ae1

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,11 @@ advanced_option(
185185
"Build using system glslang library"
186186
OFF
187187
)
188+
advanced_option(
189+
SLANG_ENABLE_SPIRV_TOOLS_MIMALLOC
190+
"Enable mimalloc for SPIRV-Tools to improve compilation performance"
191+
OFF
192+
)
188193

189194
option(
190195
SLANG_SPIRV_HEADERS_INCLUDE_DIR

external/CMakeLists.txt

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@ if(NOT SLANG_ENABLE_EXTERNAL_COMPILER_WARNINGS)
2525
endif()
2626
endif()
2727

28+
# Option to enable mimalloc for SPIRV-Tools to improve compilation performance
29+
if(WIN32)
30+
option(
31+
SLANG_ENABLE_SPIRV_TOOLS_MIMALLOC
32+
"Enable mimalloc for SPIRV-Tools to improve compilation performance"
33+
ON
34+
)
35+
endif()
36+
2837
# Unordered dense
2938
if(NOT ${SLANG_USE_SYSTEM_UNORDERED_DENSE})
3039
if(NOT SLANG_OVERRIDE_UNORDERED_DENSE_PATH)
@@ -147,6 +156,62 @@ if(SLANG_ENABLE_SLANG_GLSLANG)
147156
set(SPIRV_TOOLS_BUILD_STATIC ON)
148157
set(SPIRV_WERROR OFF)
149158
set(SPIRV_SKIP_TESTS ON)
159+
160+
# Enable mimalloc for SPIRV-Tools to improve compilation performance
161+
# Based on SPIRV-Tools PR #6267: https://github.com/KhronosGroup/SPIRV-Tools/pull/6267
162+
if(SLANG_ENABLE_SPIRV_TOOLS_MIMALLOC)
163+
# Place mimalloc in slang/external instead of spirv-tools/external for better organization
164+
set(MIMALLOC_PATH "${CMAKE_CURRENT_SOURCE_DIR}/mimalloc")
165+
166+
# Download mimalloc if it's not available
167+
if(NOT EXISTS "${MIMALLOC_PATH}/CMakeLists.txt")
168+
message(
169+
STATUS
170+
"mimalloc not found at ${MIMALLOC_PATH} - downloading..."
171+
)
172+
execute_process(
173+
COMMAND
174+
${GIT_EXECUTABLE} clone --depth 1 --branch v2.1.7
175+
https://github.com/microsoft/mimalloc.git
176+
"${MIMALLOC_PATH}"
177+
RESULT_VARIABLE git_result
178+
OUTPUT_QUIET
179+
ERROR_QUIET
180+
)
181+
if(git_result EQUAL 0)
182+
# Point SPIRV-Tools to our mimalloc location
183+
set(mimalloc_SOURCE_DIR "${MIMALLOC_PATH}")
184+
set(SPIRV_TOOLS_USE_MIMALLOC ON)
185+
set(SPIRV_TOOLS_USE_MIMALLOC_IN_STATIC_BUILD ON)
186+
# set(MI_INSTALL_TOPLEVEL OFF)
187+
message(
188+
STATUS
189+
"Successfully downloaded and enabled mimalloc for SPIRV-Tools at ${MIMALLOC_PATH}"
190+
)
191+
else()
192+
message(
193+
WARNING
194+
"Failed to download mimalloc. Building SPIRV-Tools without mimalloc support."
195+
)
196+
endif()
197+
endif()
198+
199+
# Check if mimalloc is available now
200+
if(EXISTS "${MIMALLOC_PATH}/CMakeLists.txt")
201+
# Point SPIRV-Tools to our mimalloc location
202+
set(mimalloc_SOURCE_DIR "${MIMALLOC_PATH}")
203+
set(SPIRV_TOOLS_USE_MIMALLOC ON)
204+
# Also enable mimalloc in static builds for better performance
205+
set(SPIRV_TOOLS_USE_MIMALLOC_IN_STATIC_BUILD ON)
206+
# Disable mimalloc installation to avoid install target conflicts
207+
# set(MI_INSTALL_TOPLEVEL OFF)
208+
message(
209+
STATUS
210+
"Enabling mimalloc for SPIRV-Tools (including static builds) from ${MIMALLOC_PATH}"
211+
)
212+
endif()
213+
endif()
214+
150215
# Tools
151216
if(NOT SLANG_OVERRIDE_SPIRV_TOOLS_PATH)
152217
add_subdirectory(spirv-tools EXCLUDE_FROM_ALL ${system})

0 commit comments

Comments
 (0)