Skip to content

Commit 0a4637a

Browse files
Allow mimalloc in static builds (KhronosGroup#6267)
Allow users to explicitly request that mimalloc be used in static builds.
1 parent eb500be commit 0a4637a

File tree

6 files changed

+26
-14
lines changed

6 files changed

+26
-14
lines changed

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -317,9 +317,7 @@ Effcee itself depends on [RE2][re2], and RE2 depends on [Abseil][abseil-cpp].
317317
#### Dependency on mimalloc
318318

319319
SPIRV-Tools may be configured to use the [mimalloc][mimalloc] library to improve memory
320-
allocation performance. In order to avoid unexpectedly changing allocation behavior of
321-
applications that link SPIRV-Tools libraries statically, this option has no effect on
322-
the static libraries.
320+
allocation performance.
323321

324322
In the CMake build, usage of mimalloc is controlled by the `SPIRV_TOOLS_USE_MIMALLOC`
325323
option. This variable defaults on `ON` when building for Windows and `OFF` when building
@@ -328,6 +326,11 @@ expected to work normally, but this has not been tested as thoroughly and extens
328326
the Windows version. In the future, the `SPIRV_TOOLS_USE_MIMALLOC` option may default to
329327
`ON` for non-Windows platforms as well.
330328

329+
In order to avoid unexpectedly changing allocation behavior of applications that link
330+
SPIRV-Tools libraries statically, mimalloc is disabled by default on static libraries.
331+
The option 'SPIRV_TOOLS_USE_MIMALLOC_IN_STATIC_BUILD' can be used to force the usage of
332+
mimalloc on static libraries.
333+
331334
*Note*: mimalloc is currently only supported when building with CMake. When using Bazel,
332335
mimalloc is not used.
333336

external/CMakeLists.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,23 @@ endif()
3838
# Used on Windows by default, but allow opt-in on other platforms
3939
if (WIN32)
4040
set(SPIRV_TOOLS_USE_MIMALLOC_DEFAULT_VALUE ON)
41+
set(SPIRV_TOOLS_USE_MIMALLOC_IN_STATIC_BUILD_DEFAULT_VALUE OFF)
4142
else()
4243
set(SPIRV_TOOLS_USE_MIMALLOC_DEFAULT_VALUE OFF)
44+
set(SPIRV_TOOLS_USE_MIMALLOC_IN_STATIC_BUILD_DEFAULT_VALUE OFF)
4345
endif()
4446

4547
# To avoid unexpected side effects on users of the static library, mimalloc
46-
# may only be used when building executables and shared libraries.
48+
# must be explicitly enabled when building static libraries.
4749
include(CMakeDependentOption)
4850
cmake_dependent_option(SPIRV_TOOLS_USE_MIMALLOC
4951
"Executables and shared libraries use mimalloc instead of the default allocator"
5052
${SPIRV_TOOLS_USE_MIMALLOC_DEFAULT_VALUE} "MIMALLOC_DIR" OFF)
5153

54+
cmake_dependent_option(SPIRV_TOOLS_USE_MIMALLOC_IN_STATIC_BUILD
55+
"Static libraries use mimalloc instead of the default allocator"
56+
${SPIRV_TOOLS_USE_MIMALLOC_IN_STATIC_BUILD_DEFAULT_VALUE} SPIRV_TOOLS_USE_MIMALLOC OFF)
57+
5258
if (SPIRV_TOOLS_USE_MIMALLOC)
5359
if (NOT WIN32)
5460
push_variable(MI_OVERRIDE 0)

source/CMakeLists.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,9 @@ target_compile_definitions(${SPIRV_TOOLS}-shared
351351

352352
if(SPIRV_TOOLS_BUILD_STATIC)
353353
add_library(${SPIRV_TOOLS}-static STATIC ${SPIRV_SOURCES})
354+
if (SPIRV_TOOLS_USE_MIMALLOC AND SPIRV_TOOLS_USE_MIMALLOC_IN_STATIC_BUILD)
355+
target_link_libraries(${SPIRV_TOOLS}-shared PRIVATE mimalloc-static)
356+
endif()
354357
spirv_tools_default_target_options(${SPIRV_TOOLS}-static)
355358
# The static target does not have the '-static' suffix.
356359
set_target_properties(${SPIRV_TOOLS}-static PROPERTIES OUTPUT_NAME "${SPIRV_TOOLS}")
@@ -383,7 +386,7 @@ if("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux")
383386
endif()
384387

385388
if(ENABLE_SPIRV_TOOLS_INSTALL)
386-
if (SPIRV_TOOLS_USE_MIMALLOC AND NOT SPIRV_TOOLS_BUILD_STATIC)
389+
if (SPIRV_TOOLS_USE_MIMALLOC AND (NOT SPIRV_TOOLS_BUILD_STATIC OR SPIRV_TOOLS_USE_MIMALLOC_IN_STATIC_BUILD))
387390
list(APPEND SPIRV_TOOLS_TARGETS mimalloc-static)
388391
endif()
389392
install(TARGETS ${SPIRV_TOOLS_TARGETS} EXPORT ${SPIRV_TOOLS}Targets)

source/diff/CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ set(SPIRV_TOOLS_DIFF_SOURCES
1818
diff.cpp
1919
)
2020

21-
if (SPIRV_TOOLS_USE_MIMALLOC AND NOT SPIRV_TOOLS_BUILD_STATIC)
21+
if (SPIRV_TOOLS_USE_MIMALLOC AND (NOT SPIRV_TOOLS_BUILD_STATIC OR SPIRV_TOOLS_USE_MIMALLOC_IN_STATIC_BUILD))
2222
list(APPEND SPIRV_TOOLS_DIFF_SOURCES ${spirv-tools_SOURCE_DIR}/source/mimalloc.cpp)
2323
endif()
2424

2525
add_library(SPIRV-Tools-diff ${SPIRV_TOOLS_LIBRARY_TYPE} ${SPIRV_TOOLS_DIFF_SOURCES})
2626

27-
if (SPIRV_TOOLS_USE_MIMALLOC AND NOT SPIRV_TOOLS_BUILD_STATIC)
27+
if (SPIRV_TOOLS_USE_MIMALLOC AND (NOT SPIRV_TOOLS_BUILD_STATIC OR SPIRV_TOOLS_USE_MIMALLOC_IN_STATIC_BUILD))
2828
target_link_libraries(SPIRV-Tools-diff PRIVATE mimalloc-static)
2929
endif()
3030

@@ -49,7 +49,7 @@ spvtools_check_symbol_exports(SPIRV-Tools-diff)
4949
if(ENABLE_SPIRV_TOOLS_INSTALL)
5050
set(SPIRV-Tools-diff-InstallTargets SPIRV-Tools-diff)
5151

52-
if (SPIRV_TOOLS_USE_MIMALLOC AND NOT SPIRV_TOOLS_BUILD_STATIC)
52+
if (SPIRV_TOOLS_USE_MIMALLOC AND (NOT SPIRV_TOOLS_BUILD_STATIC OR SPIRV_TOOLS_USE_MIMALLOC_IN_STATIC_BUILD))
5353
list(APPEND SPIRV-Tools-diff-InstallTargets mimalloc-static)
5454
endif()
5555

source/fuzz/CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -438,13 +438,13 @@ if(SPIRV_BUILD_FUZZER)
438438

439439
spvtools_pch(SPIRV_TOOLS_FUZZ_SOURCES pch_source_fuzz)
440440

441-
if (SPIRV_TOOLS_USE_MIMALLOC AND NOT SPIRV_TOOLS_BUILD_STATIC)
441+
if (SPIRV_TOOLS_USE_MIMALLOC AND (NOT SPIRV_TOOLS_BUILD_STATIC OR SPIRV_TOOLS_USE_MIMALLOC_IN_STATIC_BUILD))
442442
list(APPEND SPIRV_TOOLS_DIFF_SOURCES ${spirv-tools_SOURCE_DIR}/source/mimalloc.cpp)
443443
endif()
444444

445445
add_library(SPIRV-Tools-fuzz ${SPIRV_TOOLS_FUZZ_SOURCES})
446446

447-
if (SPIRV_TOOLS_USE_MIMALLOC AND NOT SPIRV_TOOLS_BUILD_STATIC)
447+
if (SPIRV_TOOLS_USE_MIMALLOC AND (NOT SPIRV_TOOLS_BUILD_STATIC OR SPIRV_TOOLS_USE_MIMALLOC_IN_STATIC_BUILD))
448448
target_link_libraries(SPIRV-Tools-fuzz PRIVATE mimalloc-static)
449449
endif()
450450

@@ -480,7 +480,7 @@ if(SPIRV_BUILD_FUZZER)
480480
if(ENABLE_SPIRV_TOOLS_INSTALL)
481481
set(SPIRV-Tools-fuzz-InstallTargets SPIRV-Tools-fuzz)
482482

483-
if (SPIRV_TOOLS_USE_MIMALLOC AND NOT SPIRV_TOOLS_BUILD_STATIC)
483+
if (SPIRV_TOOLS_USE_MIMALLOC AND (NOT SPIRV_TOOLS_BUILD_STATIC OR SPIRV_TOOLS_USE_MIMALLOC_IN_STATIC_BUILD))
484484
list(APPEND SPIRV-Tools-fuzz-InstallTargets mimalloc-static)
485485
endif()
486486

source/opt/CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -262,13 +262,13 @@ endif()
262262

263263
spvtools_pch(SPIRV_TOOLS_OPT_SOURCES pch_source_opt)
264264

265-
if (SPIRV_TOOLS_USE_MIMALLOC AND NOT SPIRV_TOOLS_BUILD_STATIC)
265+
if (SPIRV_TOOLS_USE_MIMALLOC AND (NOT SPIRV_TOOLS_BUILD_STATIC OR SPIRV_TOOLS_USE_MIMALLOC_IN_STATIC_BUILD))
266266
list(APPEND SPIRV_TOOLS_OPT_SOURCES ${spirv-tools_SOURCE_DIR}/source/mimalloc.cpp)
267267
endif()
268268

269269
add_library(SPIRV-Tools-opt ${SPIRV_TOOLS_LIBRARY_TYPE} ${SPIRV_TOOLS_OPT_SOURCES})
270270

271-
if (SPIRV_TOOLS_USE_MIMALLOC AND NOT SPIRV_TOOLS_BUILD_STATIC)
271+
if (SPIRV_TOOLS_USE_MIMALLOC AND (NOT SPIRV_TOOLS_BUILD_STATIC OR SPIRV_TOOLS_USE_MIMALLOC_IN_STATIC_BUILD))
272272
target_link_libraries(SPIRV-Tools-opt PRIVATE mimalloc-static)
273273
endif()
274274

@@ -290,7 +290,7 @@ spvtools_check_symbol_exports(SPIRV-Tools-opt)
290290
if(ENABLE_SPIRV_TOOLS_INSTALL)
291291
set(SPIRV-Tools-opt-InstallTargets SPIRV-Tools-opt)
292292

293-
if (SPIRV_TOOLS_USE_MIMALLOC AND NOT SPIRV_TOOLS_BUILD_STATIC)
293+
if (SPIRV_TOOLS_USE_MIMALLOC AND (NOT SPIRV_TOOLS_BUILD_STATIC OR SPIRV_TOOLS_USE_MIMALLOC_IN_STATIC_BUILD))
294294
list(APPEND SPIRV-Tools-opt-InstallTargets mimalloc-static)
295295
endif()
296296

0 commit comments

Comments
 (0)