Skip to content

Commit 437476b

Browse files
authored
Add option to build Chapel with mimalloc as either the host or target allocator (#26246)
Adds the ability to set `CHPL_HOST_MEM` and/or `CHPL_TARGET_MEM` to [`mimalloc`](https://github.com/microsoft/mimalloc/). This PR only provides the option to use mimalloc, it does not change the default from jemalloc ### Performance comparisons for `CHPL_TARGET_MEM` on linux64 #### test/performance/memory/microMemoryAllocation.chpl Using `--trials=2_000_000` - jemalloc: 1.38 - mimalloc: 7.79 #### test/studies/shootout/binary-trees/binarytrees-inner.chpl Using `--n=21` - jemalloc: 2.274 - mimalloc: 0.984 ### Performance comparisons for `CHPL_HOST_MEM` on linux64 #### Compiling examples/hello.chpl - jemalloc: 6.0s - mimalloc: 5.1s #### Compiling Arkouda - jemalloc: 1879.510 - mimalloc: 1827.754 Major changes in this PR - Add support to use mimalloc from `$CHPL_HOME/third-party/mimalloc` or from the system - Add a bundled mimalloc 2.1.7 Correctness testing - [x] paratest with/without gasnet with CHPL_TARGET_MEM=mimalloc on linux64 - [x] paratest with/without gasnet with CHPL_HOST_MEM=mimalloc on linux64 - [x] `make check` with/without gasnet with CHPL_TARGET_MEM=mimalloc on MacOS - [x] `make check` with/without gasnet with CHPL_HOST_MEM=mimalloc on MacOS - [x] Test that fixed heap config with SS11 gives proper errors Future work: - enable fixed heap configs - enable asan builds [Reviewed by @jhh67]
2 parents 0d94e5d + b4c12b5 commit 437476b

File tree

88 files changed

+19527
-18
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

88 files changed

+19527
-18
lines changed

CMakeLists.txt

+39-1
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,15 @@ set(CHPL_HOST_SYSTEM_COMPILE_ARGS CACHE INTERNAL "Description")
126126
set(CHPL_HOST_BUNDLED_LINK_ARGS CACHE INTERNAL "Description")
127127
set(CHPL_HOST_SYSTEM_LINK_ARGS CACHE INTERNAL "Description")
128128
set(CHPL_HOST_JEMALLOC CACHE INTERNAL "Description")
129+
set(CHPL_HOST_MIMALLOC CACHE INTERNAL "Description")
129130
set(CHPL_TARGET_COMPILER_PRGENV CACHE INTERNAL "Description")
130131
set(CHPL_TARGET_BUNDLED_COMPILE_ARGS CACHE INTERNAL "Description")
131132
set(CHPL_TARGET_SYSTEM_COMPILE_ARGS CACHE INTERNAL "Description")
132133
set(CHPL_TARGET_BUNDLED_LINK_ARGS CACHE INTERNAL "Description")
133134
set(CHPL_TARGET_SYSTEM_LINK_ARGS CACHE INTERNAL "Description")
134135
set(CHPL_TARGET_MEM CACHE INTERNAL "Description")
135136
set(CHPL_TARGET_JEMALLOC CACHE INTERNAL "Description")
137+
set(CHPL_TARGET_MIMALLOC CACHE INTERNAL "Description")
136138
set(CHPL_TARGET_CPU_FLAG CACHE INTERNAL "Description")
137139
set(CHPL_TARGET_BACKEND_CPU CACHE INTERNAL "Description")
138140
set(CHPL_MAKE CACHE INTERNAL "Description")
@@ -157,6 +159,8 @@ set(CHPL_GMP_UNIQ_CFG_PATH CACHE INTERNAL "Description")
157159
set(CHPL_HWLOC_UNIQ_CFG_PATH CACHE INTERNAL "Description")
158160
set(CHPL_HOST_JEMALLOC_UNIQ_CFG_PATH CACHE INTERNAL "Description")
159161
set(CHPL_TARGET_JEMALLOC_UNIQ_CFG_PATH CACHE INTERNAL "Description")
162+
set(CHPL_TARGET_MIMALLOC_UNIQ_CFG_PATH CACHE INTERNAL "Description")
163+
set(CHPL_LLVM_CLANG_C_UNIQ_CFG_PATH CACHE INTERNAL "Description")
160164
set(CHPL_LIBFABRIC_UNIQ_CFG_PATH CACHE INTERNAL "Description")
161165
set(CHPL_LIBUNWIND_UNIQ_CFG_PATH CACHE INTERNAL "Description")
162166
set(CHPL_QTHREAD_UNIQ_CFG_PATH CACHE INTERNAL "Description")
@@ -359,12 +363,33 @@ add_subdirectory(compiler)
359363
add_subdirectory(frontend)
360364
add_subdirectory(tools/chpldoc)
361365

366+
367+
if (CHPL_HOST_MEM STREQUAL "mimalloc")
368+
if (CHPL_HOST_MIMALLOC STREQUAL "bundled")
369+
find_package(mimalloc REQUIRED PATHS ${CMAKE_CURRENT_SOURCE_DIR}/third-party/mimalloc/install/${CHPL_HOST_MIMALLOC_UNIQ_CFG_PATH} NO_DEFAULT_PATH)
370+
elseif (CHPL_HOST_MIMALLOC STREQUAL "system")
371+
find_package(mimalloc REQUIRED)
372+
else()
373+
message(FATAL_ERROR "Invalid CHPL_HOST_MIMALLOC value: ${CHPL_HOST_MIMALLOC}")
374+
endif()
375+
376+
# static linking results in much faster compile times, but causes crashes on Mac
377+
if (APPLE)
378+
target_link_libraries(ChplFrontend PRIVATE mimalloc)
379+
target_link_libraries(ChplFrontendShared PRIVATE mimalloc)
380+
else()
381+
target_link_libraries(ChplFrontend PRIVATE mimalloc-static)
382+
target_link_libraries(ChplFrontendShared PRIVATE mimalloc-static)
383+
endif()
384+
endif()
385+
362386
# Adjust the install rpath for chpl and chpldoc
363387
if (INSTALLATION_MODE STREQUAL "prefix")
364388
# let the build and install rpaths be absolute (which is the default)
365389
set_target_properties(chpl chpldoc ChplFrontendShared
366390
PROPERTIES
367391
INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib/chapel/${CHPL_MAJOR_VERSION}.${CHPL_MINOR_VERSION}/compiler")
392+
# TODO: handle rpath for mimalloc in prefix installs
368393
install(TARGETS chpl
369394
RUNTIME DESTINATION bin)
370395
install(TARGETS chpldoc OPTIONAL
@@ -387,10 +412,23 @@ else()
387412
set(CHPL_INSTALL_RPATH "@executable_path/../../lib/compiler/${CHPL_HOST_BIN_SUBDIR}")
388413
endif()
389414

415+
if (CHPL_HOST_MEM STREQUAL "mimalloc")
416+
if (CHPL_HOST_MIMALLOC STREQUAL "bundled")
417+
if(APPLE)
418+
set(CHPL_INSTALL_RPATH "${CHPL_INSTALL_RPATH};@executable_path/../../third-party/mimalloc/install/${CHPL_HOST_MIMALLOC_UNIQ_CFG_PATH}/lib")
419+
else()
420+
# TODO: this adds both lib and lib64 but should only add the correct one
421+
set(CHPL_INSTALL_RPATH "${CHPL_INSTALL_RPATH};$ORIGIN/../../third-party/mimalloc/install/${CHPL_HOST_MIMALLOC_UNIQ_CFG_PATH}/lib;$ORIGIN/../../third-party/mimalloc/install/${CHPL_HOST_MIMALLOC_UNIQ_CFG_PATH}/lib64")
422+
endif()
423+
elseif (CHPL_HOST_MIMALLOC STREQUAL "system")
424+
set(CHPL_INSTALL_RPATH "${CHPL_INSTALL_RPATH};${MIMALLOC_LIBRARY_DIR}")
425+
endif()
426+
endif()
427+
390428
set_target_properties(chpl chpldoc ChplFrontendShared
391429
PROPERTIES
392430
BUILD_WITH_INSTALL_RPATH TRUE
393-
INSTALL_RPATH ${CHPL_INSTALL_RPATH})
431+
INSTALL_RPATH "${CHPL_INSTALL_RPATH}")
394432
endif()
395433

396434
install(TARGETS chpl

Makefile

+3
Original file line numberDiff line numberDiff line change
@@ -80,18 +80,21 @@ frontend: FORCE
8080
@echo "Making the frontend compiler library..."
8181
@cd third-party && $(MAKE) llvm
8282
@cd third-party && $(MAKE) CHPL_MAKE_HOST_TARGET=--host jemalloc
83+
@cd third-party && $(MAKE) CHPL_MAKE_HOST_TARGET=--host mimalloc
8384
@cd compiler && $(MAKE) frontend
8485

8586
frontend-shared: FORCE
8687
@echo "Making the frontend compiler library (always shared)..."
8788
@cd third-party && $(MAKE) llvm
8889
@cd third-party && $(MAKE) CHPL_MAKE_HOST_TARGET=--host jemalloc
90+
@cd third-party && $(MAKE) CHPL_MAKE_HOST_TARGET=--host mimalloc
8991
@cd compiler && $(MAKE) frontend-shared
9092

9193
compiler: FORCE
9294
@echo "Making the compiler..."
9395
@cd third-party && $(MAKE) llvm
9496
@cd third-party && $(MAKE) CHPL_MAKE_HOST_TARGET=--host jemalloc
97+
@cd third-party && $(MAKE) CHPL_MAKE_HOST_TARGET=--host mimalloc
9598
@cd compiler && $(MAKE)
9699

97100
parser: FORCE

compiler/CMakeLists.txt

+11
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,17 @@ set(CMAKE_C_STANDARD_REQUIRED True)
2626

2727
set(SRC_DIR ${CMAKE_CURRENT_SOURCE_DIR})
2828

29+
30+
if (CHPL_HOST_MEM STREQUAL "mimalloc")
31+
if (APPLE)
32+
# filter mimalloc out of CHPL_HOST_BUNDLED_LINK_ARGS
33+
# this is a hack to avoid linking mimalloc twice
34+
string(REPLACE "-lmimalloc" "" CHPL_HOST_BUNDLED_LINK_ARGS "${CHPL_HOST_BUNDLED_LINK_ARGS}")
35+
string(STRIP "${CHPL_HOST_BUNDLED_LINK_ARGS}" CHPL_HOST_BUNDLED_LINK_ARGS)
36+
endif()
37+
endif()
38+
39+
2940
# TODO: See if we can use these directly, I am not sure we can because
3041
# the CMake way to concat strings requires an output variable
3142
string(CONCAT CHPL_LLVM_COMP_ARGS ${CHPL_HOST_BUNDLED_COMPILE_ARGS} " " ${CHPL_HOST_SYSTEM_COMPILE_ARGS})

frontend/CMakeLists.txt

+10
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,16 @@ endif()
6868
string(REPLACE "-ljemalloc" "" CHPL_LLVM_LINK_ARGS "${CHPL_LLVM_LINK_ARGS}")
6969
string(STRIP "${CHPL_LLVM_LINK_ARGS}" CHPL_LLVM_LINK_ARGS)
7070

71+
if (CHPL_HOST_MEM STREQUAL "mimalloc")
72+
if (APPLE)
73+
# filter mimalloc out of CHPL_LLVM_LINK_ARGS
74+
# this is a hack to avoid linking mimalloc twice
75+
string(REPLACE "-lmimalloc" "" CHPL_LLVM_LINK_ARGS "${CHPL_LLVM_LINK_ARGS}")
76+
string(STRIP "${CHPL_LLVM_LINK_ARGS}" CHPL_LLVM_LINK_ARGS)
77+
endif()
78+
endif()
79+
80+
7181
set(CHPL_MAIN_SRC_DIR ${CMAKE_CURRENT_SOURCE_DIR})
7282
set(CHPL_MAIN_INCLUDE_DIR ${CHPL_MAIN_SRC_DIR}/include)
7383
set(CHPL_INCLUDE_DIR ${CMAKE_CURRENT_BINARY_DIR}/include)

make/Makefile.base

+3
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,12 @@ include $(THIRD_PARTY_DIR)/qthread/Makefile.include
196196
include $(THIRD_PARTY_DIR)/jemalloc/Makefile.common.include
197197
ifeq ($(strip $(CHPL_MAKE_HOST_TARGET)),--host)
198198
include $(THIRD_PARTY_DIR)/jemalloc/Makefile.host.include-$(CHPL_MAKE_HOST_JEMALLOC)
199+
include $(THIRD_PARTY_DIR)/mimalloc/Makefile.host.include-$(CHPL_MAKE_HOST_MIMALLOC)
199200
else
200201
include $(THIRD_PARTY_DIR)/jemalloc/Makefile.target.include-$(CHPL_MAKE_TARGET_JEMALLOC)
202+
include $(THIRD_PARTY_DIR)/mimalloc/Makefile.target.include-$(CHPL_MAKE_TARGET_MIMALLOC)
201203
endif
204+
202205
include $(THIRD_PARTY_DIR)/gmp/Makefile.include
203206
include $(THIRD_PARTY_DIR)/hwloc/Makefile.include
204207
include $(THIRD_PARTY_DIR)/re2/Makefile.include

runtime/Makefile

+4
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ endif
7878
ifneq (, $(filter $(THIRD_PARTY_PKGS),jemalloc))
7979
@echo "Making jemalloc..."
8080
@$(MAKE) -C $(THIRD_PARTY_DIR) CHPL_MAKE_HOST_TARGET=--target jemalloc
81+
endif
82+
ifneq (, $(filter $(THIRD_PARTY_PKGS),mimalloc))
83+
@echo "Making mimalloc..."
84+
@$(MAKE) -C $(THIRD_PARTY_DIR) CHPL_MAKE_HOST_TARGET=--target mimalloc
8185
endif
8286
@echo "Making third-party-packages..."
8387
@$(MAKE) -C $(THIRD_PARTY_DIR) $(THIRD_PARTY_PKGS)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright 2020-2025 Hewlett Packard Enterprise Development LP
3+
* Copyright 2004-2019 Cray Inc.
4+
* Other additional copyright holders may be indicated within.
5+
*
6+
* The entirety of this work is licensed under the Apache License,
7+
* Version 2.0 (the "License"); you may not use this file except
8+
* in compliance with the License.
9+
*
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
*/
20+
21+
/* mimalloc memory function implementation */
22+
#ifndef _chpl_mem_impl_H_
23+
#define _chpl_mem_impl_H_
24+
25+
26+
#include <mimalloc.h>
27+
28+
29+
#ifdef __cplusplus
30+
extern "C" {
31+
#endif
32+
33+
static inline void* chpl_calloc(size_t n, size_t size) {
34+
return mi_calloc(n, size);
35+
}
36+
37+
static inline void* chpl_malloc(size_t size) {
38+
return mi_malloc(size);
39+
}
40+
41+
static inline void* chpl_memalign(size_t boundary, size_t size) {
42+
return mi_memalign(boundary, size);
43+
}
44+
45+
static inline void* chpl_realloc(void* ptr, size_t size) {
46+
return mi_realloc(ptr, size);
47+
}
48+
49+
static inline void chpl_free(void* ptr) {
50+
mi_free(ptr);
51+
}
52+
53+
// malloc_good_size is OSX specific unfortunately. On other platforms just
54+
// return minSize.
55+
static inline size_t chpl_good_alloc_size(size_t minSize) {
56+
return mi_malloc_good_size(minSize);
57+
}
58+
59+
static inline size_t chpl_real_alloc_size(void* ptr) {
60+
return 0;
61+
}
62+
63+
#define CHPL_USING_MIMALLOC_MALLOC 1
64+
65+
#ifdef __cplusplus
66+
}
67+
#endif
68+
69+
#endif

runtime/make/Makefile.runtime.head

+4
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ ifneq ($(MAKE_LAUNCHER),1)
6262
LAUNCHER_CFLAGS += -DCHPL_TARGET_MEM_JEMALLOC
6363
LAUNCHER_INCLS += -I$(RUNTIME_ROOT)/include/mem/$(CHPL_MAKE_TARGET_MEM)
6464
endif
65+
ifeq ($(CHPL_MAKE_TARGET_MEM),mimalloc)
66+
LAUNCHER_CFLAGS += -DCHPL_TARGET_MEM_MIMALLOC
67+
LAUNCHER_INCLS += -I$(RUNTIME_ROOT)/include/mem/$(CHPL_MAKE_TARGET_MEM)
68+
endif
6569
endif
6670

6771
# Get runtime headers and required -D flags.

runtime/make/Makefile.runtime.include

+5
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,11 @@ ifeq ($(MAKE_LAUNCHER),1)
101101
$(RUNTIME_ROOT)/make/Makefile.runtime.mem-$(CHPL_MAKE_TARGET_MEM)
102102
-include $(TARGET_MEM_INCLUDE)
103103
endif
104+
ifeq ($(CHPL_MAKE_TARGET_MEM),mimalloc)
105+
TARGET_MEM_INCLUDE=\
106+
$(RUNTIME_ROOT)/make/Makefile.runtime.mem-$(CHPL_MAKE_TARGET_MEM)
107+
-include $(TARGET_MEM_INCLUDE)
108+
endif
104109
endif
105110

106111
# Add any further includes for COMM.
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
# Copyright 2020-2025 Hewlett Packard Enterprise Development LP
3+
# Copyright 2004-2019 Cray Inc.
4+
# Other additional copyright holders may be indicated within.
5+
#
6+
# The entirety of this work is licensed under the Apache License,
7+
# Version 2.0 (the "License"); you may not use this file except
8+
# in compliance with the License.
9+
#
10+
# You may obtain a copy of the License at
11+
#
12+
# http://www.apache.org/licenses/LICENSE-2.0
13+
#
14+
# Unless required by applicable law or agreed to in writing, software
15+
# distributed under the License is distributed on an "AS IS" BASIS,
16+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
# See the License for the specific language governing permissions and
18+
# limitations under the License.
19+
20+
# Add any further includes for MIMALLOC.
21+
MIMALLOC_INCLUDE=$(RUNTIME_ROOT)/make/Makefile.runtime.mimalloc-$(CHPL_MAKE_TARGET_MIMALLOC)
22+
-include $(MIMALLOC_INCLUDE)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Copyright 2020-2025 Hewlett Packard Enterprise Development LP
2+
# Copyright 2004-2019 Cray Inc.
3+
# Other additional copyright holders may be indicated within.
4+
#
5+
# The entirety of this work is licensed under the Apache License,
6+
# Version 2.0 (the "License"); you may not use this file except
7+
# in compliance with the License.
8+
#
9+
# You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing, software
14+
# distributed under the License is distributed on an "AS IS" BASIS,
15+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
# See the License for the specific language governing permissions and
17+
# limitations under the License.
18+
19+
RUNTIME_INCLS += -I$(MIMALLOC_INCLUDE_DIR)

runtime/src/mem/mimalloc/Makefile

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Copyright 2020-2025 Hewlett Packard Enterprise Development LP
2+
# Copyright 2004-2019 Cray Inc.
3+
# Other additional copyright holders may be indicated within.
4+
#
5+
# The entirety of this work is licensed under the Apache License,
6+
# Version 2.0 (the "License"); you may not use this file except
7+
# in compliance with the License.
8+
#
9+
# You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing, software
14+
# distributed under the License is distributed on an "AS IS" BASIS,
15+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
# See the License for the specific language governing permissions and
17+
# limitations under the License.
18+
19+
RUNTIME_ROOT = ../../..
20+
RUNTIME_SUBDIR = src/mem/mimalloc
21+
22+
ifndef CHPL_MAKE_HOME
23+
export CHPL_MAKE_HOME=$(shell pwd)/$(RUNTIME_ROOT)/..
24+
endif
25+
26+
include $(RUNTIME_ROOT)/make/Makefile.runtime.head
27+
28+
MEM_OBJDIR = $(RUNTIME_OBJDIR)
29+
30+
include Makefile.share
31+
32+
TARGETS = $(MEM_COMMON_OBJS)
33+
34+
include $(RUNTIME_ROOT)/make/Makefile.runtime.subdirrules
35+
36+
include $(RUNTIME_ROOT)/make/Makefile.runtime.foot
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Copyright 2020-2025 Hewlett Packard Enterprise Development LP
2+
# Copyright 2004-2019 Cray Inc.
3+
# Other additional copyright holders may be indicated within.
4+
#
5+
# The entirety of this work is licensed under the Apache License,
6+
# Version 2.0 (the "License"); you may not use this file except
7+
# in compliance with the License.
8+
#
9+
# You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing, software
14+
# distributed under the License is distributed on an "AS IS" BASIS,
15+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
# See the License for the specific language governing permissions and
17+
# limitations under the License.
18+
19+
MEM_SUBDIR = src/mem/mimalloc
20+
21+
ALL_SRCS += $(CURDIR)/$(MEM_SUBDIR)/*.c
22+
23+
MEM_OBJDIR = $(RUNTIME_BUILD)/$(MEM_SUBDIR)
24+
25+
include $(RUNTIME_ROOT)/$(MEM_SUBDIR)/Makefile.share
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Copyright 2020-2025 Hewlett Packard Enterprise Development LP
2+
# Copyright 2004-2019 Cray Inc.
3+
# Other additional copyright holders may be indicated within.
4+
#
5+
# The entirety of this work is licensed under the Apache License,
6+
# Version 2.0 (the "License"); you may not use this file except
7+
# in compliance with the License.
8+
#
9+
# You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing, software
14+
# distributed under the License is distributed on an "AS IS" BASIS,
15+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
# See the License for the specific language governing permissions and
17+
# limitations under the License.
18+
19+
MEM_SRCS = mem-mimalloc.c
20+
21+
SRCS = $(MEM_SRCS)
22+
23+
MEM_COMMON_OBJS = $(MEM_SRCS:%.c=$(MEM_OBJDIR)/%.o)

0 commit comments

Comments
 (0)