Skip to content

Commit

Permalink
[examples][cmake] Change mimalloc to snmalloc.
Browse files Browse the repository at this point in the history
  • Loading branch information
EllisLambda committed Nov 10, 2023
1 parent ee16eb3 commit fffaadb
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 27 deletions.
6 changes: 3 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
path = llvm
url = https://github.com/llvm/llvm-project.git
branch = main
[submodule "thirdparty/mimalloc"]
path = thirdparty/mimalloc
url = https://github.com/microsoft/mimalloc.git
[submodule "thirdparty/snmalloc"]
path = thirdparty/snmalloc
url = https://github.com/microsoft/snmalloc.git
11 changes: 6 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -168,14 +168,15 @@ if(BUDDY_DSL_EXAMPLES)
endif()

#-------------------------------------------------------------------------------
# The `mimalloc` Configuration
# The `snmalloc` Configuration
#-------------------------------------------------------------------------------

if(BUDDY_MLIR_USE_MIMALLOC)
if(MIMALLOC_BUILD_DIR)
list(APPEND CMAKE_PREFIX_PATH ${MIMALLOC_BUILD_DIR})
if(BUDDY_MLIR_USE_SNMALLOC)
if(SNMALLOC_BUILD_DIR)
list(APPEND CMAKE_PREFIX_PATH ${SNMALLOC_BUILD_DIR})
endif()
find_package(mimalloc REQUIRED)
add_library(snmalloc-static STATIC IMPORTED)
set_property(TARGET snmalloc-static PROPERTY IMPORTED_LOCATION "${SNMALLOC_BUILD_DIR}/libsnmallocshim-static.a")
endif()

#-------------------------------------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions examples/BuddyLlama/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ set(BUDDY_LLAMA_LIBS
mlir_c_runner_utils
omp
)
if(BUDDY_MLIR_USE_MIMALLOC)
list(APPEND BUDDY_LLAMA_LIBS mimalloc)
if(BUDDY_MLIR_USE_SNMALLOC)
list(APPEND BUDDY_LLAMA_LIBS snmalloc-static)
endif()

target_link_libraries(buddy-llama-run ${BUDDY_LLAMA_LIBS})
9 changes: 5 additions & 4 deletions examples/BuddyLlama/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ $ ./buddy-llama-run
```
This build will spend a few minutes. We recommend you to use better cpu such as server-level cpu to run buddy-llama-run.

If you wish to utilize `mimalloc` as a memory allocator, you need to set `BUDDY_MLIR_USE_MIMALLOC` and `MIMALLOC_BUILD_DIR`.
For more details, please see [here](../../thirdparty/README.md#the-mimalloc-allocator).
If you wish to utilize `smmalloc` as a memory allocator, you need to set `BUDDY_MLIR_USE_SNMALLOC` and `SNMALLOC_BUILD_DIR`.
For more details, please see [here](../../thirdparty/README.md#the-snmalloc-allocator).

If you wish to use LTO to optimize the performance, you need to set `BUDDY_MLIR_USE_LTO`. To ensure version compatibility with the project, you need to use the `lld` and `clang` built within the buddy-mlir. We need to make some modifications to the compilation process.

Expand All @@ -102,7 +102,8 @@ $ ninja check-clang check-mlir omp clang lld
6. Build and run LLaMA example

```
$ cmake -G Ninja .. -DBUDDY_LLAMA_EXAMPLES=ON \
-DBUDDY_LLAMA_USE_LTO=ON \
$ cmake -G Ninja .. \
-DBUDDY_LLAMA_EXAMPLES=ON \
-DBUDDY_LLAMA_USE_LTO=ON
$ ninja buddy-llama-run
```
29 changes: 16 additions & 13 deletions thirdparty/README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
# Third-Party Dependencies

## The `mimalloc` Allocator
## The `snmalloc` Allocator

### Build `mimalloc`
### Build `snmalloc`

```
$ cd buddy-mlir/thirdparty/mimalloc
$ mkdir -p out/release
$ cd out/release
$ cmake -G Ninja ../.. -DCMAKE_INSTALL_PREFIX=$PWD
$ cd buddy-mlir/thirdparty/snmalloc
$ mkdir -p build
$ cd build
$ cmake -G Ninja ..
$ ninja install
```
It may need new C++ feature to build, If there's problem, use the `clang` built within the buddy-mlir, just follow the instruction of LTO. and change the command to
```
$ cmake -G Ninja ..\
-DCMAKE_CXX_COMPILER=$PWD/../../../llvm/build/bin/clang++
```
### Use `snmalloc` in buddy-mlir

### Use `mimalloc` in buddy-mlir

Assign the `mimalloc` installation address to the `BUDDY_MLIR_USE_MIMALLOC` CMake variable.
Assign the `snmalloc` installation address to the `BUDDY_MLIR_USE_SNMALLOC` CMake variable.

For example:

Expand All @@ -25,13 +29,12 @@ $ cmake -G Ninja .. \
-DLLVM_DIR=$PWD/../llvm/build/lib/cmake/llvm \
-DLLVM_ENABLE_ASSERTIONS=ON \
-DCMAKE_BUILD_TYPE=RELEASE \
-DBUDDY_MLIR_USE_MIMALLOC=ON \
-DMIMALLOC_BUILD_DIR=$PWD/../thirdparty/mimalloc/out/release
-DBUDDY_MLIR_USE_SNMALLOC=ON \
-DMIMALLOC_BUILD_DIR=$PWD/../thirdparty/mimalloc/build
```

In the `CMakeLists.txt` file, link shared or static library by using:

```
target_link_libraries(myapp PUBLIC mimalloc)
target_link_libraries(myapp PUBLIC mimalloc-static)
target_link_libraries(myapp PUBLIC snmalloc-static)
```

0 comments on commit fffaadb

Please sign in to comment.