Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to build Chapel with mimalloc as either the host or target allocator #26246

Draft
wants to merge 18 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,15 @@ set(CHPL_HOST_SYSTEM_COMPILE_ARGS CACHE INTERNAL "Description")
set(CHPL_HOST_BUNDLED_LINK_ARGS CACHE INTERNAL "Description")
set(CHPL_HOST_SYSTEM_LINK_ARGS CACHE INTERNAL "Description")
set(CHPL_HOST_JEMALLOC CACHE INTERNAL "Description")
set(CHPL_HOST_MIMALLOC CACHE INTERNAL "Description")
set(CHPL_TARGET_COMPILER_PRGENV CACHE INTERNAL "Description")
set(CHPL_TARGET_BUNDLED_COMPILE_ARGS CACHE INTERNAL "Description")
set(CHPL_TARGET_SYSTEM_COMPILE_ARGS CACHE INTERNAL "Description")
set(CHPL_TARGET_BUNDLED_LINK_ARGS CACHE INTERNAL "Description")
set(CHPL_TARGET_SYSTEM_LINK_ARGS CACHE INTERNAL "Description")
set(CHPL_TARGET_MEM CACHE INTERNAL "Description")
set(CHPL_TARGET_JEMALLOC CACHE INTERNAL "Description")
set(CHPL_TARGET_MIMALLOC CACHE INTERNAL "Description")
set(CHPL_TARGET_CPU_FLAG CACHE INTERNAL "Description")
set(CHPL_TARGET_BACKEND_CPU CACHE INTERNAL "Description")
set(CHPL_MAKE CACHE INTERNAL "Description")
Expand All @@ -153,6 +155,8 @@ set(CHPL_GMP_UNIQ_CFG_PATH CACHE INTERNAL "Description")
set(CHPL_HWLOC_UNIQ_CFG_PATH CACHE INTERNAL "Description")
set(CHPL_HOST_JEMALLOC_UNIQ_CFG_PATH CACHE INTERNAL "Description")
set(CHPL_TARGET_JEMALLOC_UNIQ_CFG_PATH CACHE INTERNAL "Description")
set(CHPL_TARGET_MIMALLOC_UNIQ_CFG_PATH CACHE INTERNAL "Description")
set(CHPL_LLVM_CLANG_C_UNIQ_CFG_PATH CACHE INTERNAL "Description")
set(CHPL_LIBFABRIC_UNIQ_CFG_PATH CACHE INTERNAL "Description")
set(CHPL_LIBUNWIND_UNIQ_CFG_PATH CACHE INTERNAL "Description")
set(CHPL_QTHREAD_UNIQ_CFG_PATH CACHE INTERNAL "Description")
Expand Down Expand Up @@ -355,12 +359,33 @@ add_subdirectory(compiler)
add_subdirectory(frontend)
add_subdirectory(tools/chpldoc)


if (CHPL_HOST_MEM STREQUAL "mimalloc")
if (CHPL_HOST_MIMALLOC STREQUAL "bundled")
find_package(mimalloc REQUIRED PATHS ${CMAKE_CURRENT_SOURCE_DIR}/third-party/mimalloc/install/${CHPL_HOST_MIMALLOC_UNIQ_CFG_PATH} NO_DEFAULT_PATH)
elseif (CHPL_HOST_MIMALLOC STREQUAL "system")
find_package(mimalloc REQUIRED)
else()
message(FATAL_ERROR "Invalid CHPL_HOST_MIMALLOC value: ${CHPL_HOST_MIMALLOC}")
endif()

# static linking results in much faster compile times, but casues crashes on Mac
if (APPLE)
target_link_libraries(ChplFrontend PRIVATE mimalloc)
target_link_libraries(ChplFrontendShared PRIVATE mimalloc)
else()
target_link_libraries(ChplFrontend PRIVATE mimalloc-static)
target_link_libraries(ChplFrontendShared PRIVATE mimalloc-static)
endif()
endif()

# Adjust the install rpath for chpl and chpldoc
if (INSTALLATION_MODE STREQUAL "prefix")
# let the build and install rpaths be absolute (which is the default)
set_target_properties(chpl chpldoc ChplFrontendShared
PROPERTIES
INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib/chapel/${CHPL_MAJOR_VERSION}.${CHPL_MINOR_VERSION}/compiler")
# TODO: handle rpath for mimalloc in prefix installs
install(TARGETS chpl
RUNTIME DESTINATION bin)
install(TARGETS chpldoc OPTIONAL
Expand All @@ -383,10 +408,23 @@ else()
set(CHPL_INSTALL_RPATH "@executable_path/../../lib/compiler/${CHPL_HOST_BIN_SUBDIR}")
endif()

if (CHPL_HOST_MEM STREQUAL "mimalloc")
if (CHPL_HOST_MIMALLOC STREQUAL "bundled")
if(APPLE)
set(CHPL_INSTALL_RPATH "${CHPL_INSTALL_RPATH};@executable_path/../../third-party/mimalloc/install/${CHPL_HOST_MIMALLOC_UNIQ_CFG_PATH}/lib")
else()
# TODO: this adds both lib and lib64 but should only add the correct one
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")
endif()
elseif (CHPL_HOST_MIMALLOC STREQUAL "system")
set(CHPL_INSTALL_RPATH "${CHPL_INSTALL_RPATH};${MIMALLOC_LIBRARY_DIR}")
endif()
endif()

set_target_properties(chpl chpldoc ChplFrontendShared
PROPERTIES
BUILD_WITH_INSTALL_RPATH TRUE
INSTALL_RPATH ${CHPL_INSTALL_RPATH})
INSTALL_RPATH "${CHPL_INSTALL_RPATH}")
endif()

install(TARGETS chpl
Expand Down
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -80,18 +80,21 @@ frontend: FORCE
@echo "Making the frontend compiler library..."
@cd third-party && $(MAKE) llvm
@cd third-party && $(MAKE) CHPL_MAKE_HOST_TARGET=--host jemalloc
@cd third-party && $(MAKE) CHPL_MAKE_HOST_TARGET=--host mimalloc
@cd compiler && $(MAKE) frontend

frontend-shared: FORCE
@echo "Making the frontend compiler library (always shared)..."
@cd third-party && $(MAKE) llvm
@cd third-party && $(MAKE) CHPL_MAKE_HOST_TARGET=--host jemalloc
@cd third-party && $(MAKE) CHPL_MAKE_HOST_TARGET=--host mimalloc
@cd compiler && $(MAKE) frontend-shared

compiler: FORCE
@echo "Making the compiler..."
@cd third-party && $(MAKE) llvm
@cd third-party && $(MAKE) CHPL_MAKE_HOST_TARGET=--host jemalloc
@cd third-party && $(MAKE) CHPL_MAKE_HOST_TARGET=--host mimalloc
@cd compiler && $(MAKE)

parser: FORCE
Expand Down
3 changes: 3 additions & 0 deletions make/Makefile.base
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,12 @@ include $(THIRD_PARTY_DIR)/qthread/Makefile.include
include $(THIRD_PARTY_DIR)/jemalloc/Makefile.common.include
ifeq ($(strip $(CHPL_MAKE_HOST_TARGET)),--host)
include $(THIRD_PARTY_DIR)/jemalloc/Makefile.host.include-$(CHPL_MAKE_HOST_JEMALLOC)
include $(THIRD_PARTY_DIR)/mimalloc/Makefile.host.include-$(CHPL_MAKE_HOST_MIMALLOC)
else
include $(THIRD_PARTY_DIR)/jemalloc/Makefile.target.include-$(CHPL_MAKE_TARGET_JEMALLOC)
include $(THIRD_PARTY_DIR)/mimalloc/Makefile.target.include-$(CHPL_MAKE_TARGET_MIMALLOC)
endif

include $(THIRD_PARTY_DIR)/gmp/Makefile.include
include $(THIRD_PARTY_DIR)/hwloc/Makefile.include
include $(THIRD_PARTY_DIR)/re2/Makefile.include
Expand Down
4 changes: 4 additions & 0 deletions runtime/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ endif
ifneq (, $(filter $(THIRD_PARTY_PKGS),jemalloc))
@echo "Making jemalloc..."
@$(MAKE) -C $(THIRD_PARTY_DIR) CHPL_MAKE_HOST_TARGET=--target jemalloc
endif
ifneq (, $(filter $(THIRD_PARTY_PKGS),mimalloc))
@echo "Making mimalloc..."
@$(MAKE) -C $(THIRD_PARTY_DIR) CHPL_MAKE_HOST_TARGET=--target mimalloc
endif
@echo "Making third-party-packages..."
@$(MAKE) -C $(THIRD_PARTY_DIR) $(THIRD_PARTY_PKGS)
Expand Down
69 changes: 69 additions & 0 deletions runtime/include/mem/mimalloc/chpl-mem-impl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright 2020-2024 Hewlett Packard Enterprise Development LP
* Copyright 2004-2019 Cray Inc.
* Other additional copyright holders may be indicated within.
*
* The entirety of this work is licensed under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
*
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/* mimalloc memory function implementation */
#ifndef _chpl_mem_impl_H_
#define _chpl_mem_impl_H_


#include <mimalloc.h>


#ifdef __cplusplus
extern "C" {
#endif

static inline void* chpl_calloc(size_t n, size_t size) {
return mi_calloc(n, size);
}

static inline void* chpl_malloc(size_t size) {
return mi_malloc(size);
}

static inline void* chpl_memalign(size_t boundary, size_t size) {
return mi_memalign(boundary, size);
}

static inline void* chpl_realloc(void* ptr, size_t size) {
return mi_realloc(ptr, size);
}

static inline void chpl_free(void* ptr) {
mi_free(ptr);
}

// malloc_good_size is OSX specific unfortunately. On other platforms just
// return minSize.
static inline size_t chpl_good_alloc_size(size_t minSize) {
return mi_malloc_good_size(minSize);
}

static inline size_t chpl_real_alloc_size(void* ptr) {
return 0;
}

#define CHPL_USING_MIMALLOC_MALLOC 1

#ifdef __cplusplus
}
#endif

#endif
4 changes: 4 additions & 0 deletions runtime/make/Makefile.runtime.head
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ ifneq ($(MAKE_LAUNCHER),1)
LAUNCHER_CFLAGS += -DCHPL_TARGET_MEM_JEMALLOC
LAUNCHER_INCLS += -I$(RUNTIME_ROOT)/include/mem/$(CHPL_MAKE_TARGET_MEM)
endif
ifeq ($(CHPL_MAKE_TARGET_MEM),mimalloc)
LAUNCHER_CFLAGS += -DCHPL_TARGET_MEM_MIMALLOC
LAUNCHER_INCLS += -I$(RUNTIME_ROOT)/include/mem/$(CHPL_MAKE_TARGET_MEM)
endif
endif

# Get runtime headers and required -D flags.
Expand Down
5 changes: 5 additions & 0 deletions runtime/make/Makefile.runtime.include
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ ifeq ($(MAKE_LAUNCHER),1)
$(RUNTIME_ROOT)/make/Makefile.runtime.mem-$(CHPL_MAKE_TARGET_MEM)
-include $(TARGET_MEM_INCLUDE)
endif
ifeq ($(CHPL_MAKE_TARGET_MEM),mimalloc)
TARGET_MEM_INCLUDE=\
$(RUNTIME_ROOT)/make/Makefile.runtime.mem-$(CHPL_MAKE_TARGET_MEM)
-include $(TARGET_MEM_INCLUDE)
endif
endif

# Add any further includes for COMM.
Expand Down
22 changes: 22 additions & 0 deletions runtime/make/Makefile.runtime.mem-mimalloc
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

# Copyright 2020-2024 Hewlett Packard Enterprise Development LP
# Copyright 2004-2019 Cray Inc.
# Other additional copyright holders may be indicated within.
#
# The entirety of this work is licensed under the Apache License,
# Version 2.0 (the "License"); you may not use this file except
# in compliance with the License.
#
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Add any further includes for MIMALLOC.
MIMALLOC_INCLUDE=$(RUNTIME_ROOT)/make/Makefile.runtime.mimalloc-$(CHPL_MAKE_TARGET_MIMALLOC)
-include $(MIMALLOC_INCLUDE)
19 changes: 19 additions & 0 deletions runtime/make/Makefile.runtime.mimalloc-bundled
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright 2020-2024 Hewlett Packard Enterprise Development LP
# Copyright 2004-2019 Cray Inc.
# Other additional copyright holders may be indicated within.
#
# The entirety of this work is licensed under the Apache License,
# Version 2.0 (the "License"); you may not use this file except
# in compliance with the License.
#
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

RUNTIME_INCLS += -I$(MIMALLOC_INCLUDE_DIR)
36 changes: 36 additions & 0 deletions runtime/src/mem/mimalloc/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Copyright 2020-2024 Hewlett Packard Enterprise Development LP
# Copyright 2004-2019 Cray Inc.
# Other additional copyright holders may be indicated within.
#
# The entirety of this work is licensed under the Apache License,
# Version 2.0 (the "License"); you may not use this file except
# in compliance with the License.
#
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

RUNTIME_ROOT = ../../..
RUNTIME_SUBDIR = src/mem/mimalloc

ifndef CHPL_MAKE_HOME
export CHPL_MAKE_HOME=$(shell pwd)/$(RUNTIME_ROOT)/..
endif

include $(RUNTIME_ROOT)/make/Makefile.runtime.head

MEM_OBJDIR = $(RUNTIME_OBJDIR)

include Makefile.share

TARGETS = $(MEM_COMMON_OBJS)

include $(RUNTIME_ROOT)/make/Makefile.runtime.subdirrules

include $(RUNTIME_ROOT)/make/Makefile.runtime.foot
25 changes: 25 additions & 0 deletions runtime/src/mem/mimalloc/Makefile.include
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Copyright 2020-2024 Hewlett Packard Enterprise Development LP
# Copyright 2004-2019 Cray Inc.
# Other additional copyright holders may be indicated within.
#
# The entirety of this work is licensed under the Apache License,
# Version 2.0 (the "License"); you may not use this file except
# in compliance with the License.
#
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

MEM_SUBDIR = src/mem/mimalloc

ALL_SRCS += $(CURDIR)/$(MEM_SUBDIR)/*.c

MEM_OBJDIR = $(RUNTIME_BUILD)/$(MEM_SUBDIR)

include $(RUNTIME_ROOT)/$(MEM_SUBDIR)/Makefile.share
23 changes: 23 additions & 0 deletions runtime/src/mem/mimalloc/Makefile.share
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Copyright 2020-2024 Hewlett Packard Enterprise Development LP
# Copyright 2004-2019 Cray Inc.
# Other additional copyright holders may be indicated within.
#
# The entirety of this work is licensed under the Apache License,
# Version 2.0 (the "License"); you may not use this file except
# in compliance with the License.
#
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

MEM_SRCS = mem-mimalloc.c

SRCS = $(MEM_SRCS)

MEM_COMMON_OBJS = $(MEM_SRCS:%.c=$(MEM_OBJDIR)/%.o)
Loading
Loading