Skip to content

Commit 560293d

Browse files
committed
[interop] Upgrade CppInterOp to v1.8.0
1 parent 09d0883 commit 560293d

48 files changed

Lines changed: 3761 additions & 835 deletions

Some content is hidden

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

interpreter/CppInterOp/CMakeLists.txt

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ set(CMAKE_CXX_EXTENSIONS NO)
1212
option(CPPINTEROP_USE_CLING "Use Cling as backend" OFF)
1313
option(CPPINTEROP_USE_REPL "Use clang-repl as backend" ON)
1414
option(CPPINTEROP_ENABLE_TESTING "Enable the CppInterOp testing infrastructure." ON)
15+
if(EMSCRIPTEN)
16+
set(CPPINTEROP_EXTRA_WASM_FLAGS "-sSUPPORT_LONGJMP=wasm -fwasm-exceptions" CACHE STRING "Extra flags for wasm")
17+
endif()
1518

1619
if (CPPINTEROP_USE_CLING AND CPPINTEROP_USE_REPL)
1720
message(FATAL_ERROR "We can only use Cling (${CPPINTEROP_USE_CLING}=On) or Repl (CPPINTEROP_USE_REPL=On), but not both of them.")
@@ -68,14 +71,14 @@ include(GNUInstallDirs)
6871
## Define supported version of clang and llvm
6972

7073
set(CLANG_MIN_SUPPORTED 18.0)
71-
set(CLANG_MAX_SUPPORTED "20.1.x")
72-
set(CLANG_VERSION_UPPER_BOUND 21.0.0)
74+
set(CLANG_MAX_SUPPORTED "21.1.x")
75+
set(CLANG_VERSION_UPPER_BOUND 22.0.0)
7376
set(LLD_MIN_SUPPORTED 18.0)
74-
set(LLD_MAX_SUPPORTED "20.1.x")
75-
set(LLD_VERSION_UPPER_BOUND 21.0.0)
77+
set(LLD_MAX_SUPPORTED "21.1.x")
78+
set(LLD_VERSION_UPPER_BOUND 22.0.0)
7679
set(LLVM_MIN_SUPPORTED 18.0)
77-
set(LLVM_MAX_SUPPORTED "20.1.x")
78-
set(LLVM_VERSION_UPPER_BOUND 21.0.0)
80+
set(LLVM_MAX_SUPPORTED "21.1.x")
81+
set(LLVM_VERSION_UPPER_BOUND 22.0.0)
7982

8083
## Set Cmake packages search order
8184

@@ -298,6 +301,18 @@ include_directories(SYSTEM ${LLVM_INCLUDE_DIRS})
298301
separate_arguments(LLVM_DEFINITIONS_LIST NATIVE_COMMAND ${LLVM_DEFINITIONS})
299302
add_definitions(${LLVM_DEFINITIONS_LIST})
300303

304+
string(REGEX REPLACE "/lib/cmake/llvm$" "" LLVM_BINARY_LIB_DIR "${LLVM_DIR}")
305+
add_definitions(-DLLVM_BINARY_LIB_DIR="${LLVM_BINARY_LIB_DIR}")
306+
307+
if(LLVM_BUILT_WITH_OOP_JIT)
308+
if((CMAKE_SYSTEM_NAME STREQUAL "Darwin" AND CMAKE_SYSTEM_PROCESSOR MATCHES "arm64") OR
309+
(CMAKE_SYSTEM_NAME STREQUAL "Linux" AND CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64"))
310+
add_definitions(-DLLVM_BUILT_WITH_OOP_JIT)
311+
else()
312+
message(FATAL_ERROR "LLVM_BUILT_WITH_OOP_JIT is only supported on MacOS arm64 or Linux x86_64. Build aborted.")
313+
endif()
314+
endif()
315+
301316
# If the llvm sources are present add them with higher priority.
302317
if (LLVM_BUILD_MAIN_SRC_DIR)
303318
# LLVM_INCLUDE_DIRS contains the include paths to both LLVM's source and
@@ -347,6 +362,9 @@ endif()
347362
if (LLVM_COMPILER_IS_GCC_COMPATIBLE)
348363
if (APPLE OR EMSCRIPTEN)
349364
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-common -Woverloaded-virtual -Wcast-qual -fno-strict-aliasing -Wno-long-long -Wall -W -Wno-unused-parameter -Wwrite-strings")
365+
if (EMSCRIPTEN)
366+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CPPINTEROP_EXTRA_WASM_FLAGS}")
367+
endif ()
350368
else()
351369
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-common -Woverloaded-virtual -Wcast-qual -fno-strict-aliasing -pedantic -Wno-long-long -Wall -W -Wno-unused-parameter -Wwrite-strings")
352370
endif ()

interpreter/CppInterOp/Emscripten-build-instructions.md

Lines changed: 61 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,34 +19,34 @@ cd ./CppInterOp-wasm
1919
```
2020

2121
To create a wasm build of CppInterOp we make use of the emsdk toolchain. This can be installed by executing (we only currently
22-
support version 3.1.73)
22+
support version 4.0.9)
2323
```bash
2424
git clone https://github.com/emscripten-core/emsdk.git
25-
./emsdk/emsdk install 3.1.73
25+
./emsdk/emsdk install 4.0.9
2626
```
2727

2828
and to activate the emsdk environment on Linux and osx execute (we are defining SYSROOT_PATH for use later)
2929

3030
```bash
31-
./emsdk/emsdk activate 3.1.73
31+
./emsdk/emsdk activate 4.0.9
3232
source ./emsdk/emsdk_env.sh
3333
export SYSROOT_PATH=$PWD/emsdk/upstream/emscripten/cache/sysroot
3434
```
3535

3636
and on Windows execute in Powershell
3737

3838
```powershell
39-
.\emsdk\emsdk activate 3.1.73
39+
.\emsdk\emsdk activate 4.0.9
4040
.\emsdk\emsdk_env.ps1
4141
$env:PWD_DIR= $PWD.Path
4242
$env:SYSROOT_PATH="$env:EMSDK/upstream/emscripten/cache/sysroot"
4343
```
4444

45-
Now clone the 20.x release of the LLVM project repository and CppInterOp (the building of the emscripten version of llvm can be
45+
Now clone the 21.x release of the LLVM project repository and CppInterOp (the building of the emscripten version of llvm can be
4646
avoided by executing micromamba install llvm -c <https://repo.mamba.pm/emscripten-forge> and setting the LLVM_BUILD_DIR/$env:LLVM_BUILD_DIR appropriately)
4747

4848
```bash
49-
git clone --depth=1 --branch release/20.x https://github.com/llvm/llvm-project.git
49+
git clone --depth=1 --branch release/21.x https://github.com/llvm/llvm-project.git
5050
git clone --depth=1 https://github.com/compiler-research/CppInterOp.git
5151
```
5252

@@ -55,16 +55,17 @@ executing
5555

5656
```bash
5757
cd ./llvm-project/
58-
git apply -v ../CppInterOp/patches/llvm/emscripten-clang20-*.patch
58+
git apply -v ../CppInterOp/patches/llvm/emscripten-clang21-*.patch
5959
```
6060

6161
On Windows execute the following
6262

6363
```powershell
6464
cd .\llvm-project\
65-
cp -r ..\patches\llvm\emscripten-clang20*
66-
git apply -v emscripten-clang20-2-shift-temporary-files-to-tmp-dir.patch
67-
git apply -v emscripten-clang20-3-enable_exception_handling.patch
65+
cp -r ..\patches\llvm\emscripten-clang21*
66+
git apply -v emscripten-clang21-1-shift-temporary-files-to-tmp-dir.patch
67+
git apply -v emscripten-clang21-2-enable_exception_handling.patch
68+
git apply -v emscripten-clang21-3-webassembly_target_machine_reordering.patch
6869
```
6970

7071
We are now in a position to build an emscripten build of llvm by executing the following on Linux
@@ -101,9 +102,7 @@ emcmake cmake -DCMAKE_BUILD_TYPE=Release \
101102
-DCMAKE_CXX_FLAGS_RELEASE="-Oz -g0 -DNDEBUG" \
102103
-DLLVM_ENABLE_LTO=Full \
103104
../llvm
104-
emmake make libclang -j $(nproc --all)
105-
emmake make clangInterpreter clangStaticAnalyzerCore -j $(nproc --all)
106-
emmake make lldWasm -j $(nproc --all)
105+
EMCC_CFLAGS="-sSUPPORT_LONGJMP=wasm -fwasm-exceptions" emmake make libclang clangInterpreter clangStaticAnalyzerCore -j $(nproc --all)
107106
```
108107

109108
or executing
@@ -142,7 +141,9 @@ emcmake cmake -DCMAKE_BUILD_TYPE=Release `
142141
-DCMAKE_CXX_FLAGS_RELEASE="-Oz -g0 -DNDEBUG" `
143142
-DLLVM_ENABLE_LTO=Full `
144143
..\llvm
145-
emmake ninja libclang clangInterpreter clangStaticAnalyzerCore lldWasm
144+
$env:EMCC_CFLAGS="-sSUPPORT_LONGJMP=wasm -fwasm-exceptions"
145+
emmake ninja libclang clangInterpreter clangStaticAnalyzerCore
146+
$env:EMCC_CFLAGS=""
146147
```
147148

148149
on Windows. Once this finishes building we need to take note of where we built our llvm build. This can be done by executing the following on Linux and osx
@@ -163,7 +164,7 @@ by executing (assumes you have micromamba installed and that your shell is initi
163164

164165
```bash
165166
cd ../../CppInterOp/
166-
micromamba create -f environment-wasm.yml --platform=emscripten-wasm32
167+
micromamba create -f environment-wasm.yml --platform=emscripten-wasm32 -c https://prefix.dev/emscripten-forge-4x -c https://prefix.dev/conda-forge
167168
micromamba activate CppInterOp-wasm
168169
```
169170

@@ -178,7 +179,7 @@ export CMAKE_SYSTEM_PREFIX_PATH=$PREFIX
178179
and
179180

180181
```powershell
181-
$env:PREFIX="%CONDA_PREFIX%/envs/CppInterOp-wasm"
182+
$env:PREFIX="$env:MAMBA_ROOT_PREFIX/envs/CppInterOp-wasm"
182183
$env:CMAKE_PREFIX_PATH=$env:PREFIX
183184
$env:CMAKE_SYSTEM_PREFIX_PATH=$env:PREFIX
184185
```
@@ -337,13 +338,13 @@ emmake make -j $(nproc --all) install
337338
## Xeus-cpp-lite Wasm Build Instructions
338339

339340
A project which makes use of the wasm build of CppInterOp is xeus-cpp. xeus-cpp is a C++ Jupyter kernel. Assuming you are in
340-
the CppInterOp build folder, you can build the wasm version of xeus-cpp by executing (replace LLVM_VERSION with the version
341+
the CppInterOp build folder, you can build the wasm version of xeus-cpp on Linux/MacOS by executing (replace LLVM_VERSION with the version
341342
of llvm you are building against)
342343

343344
```bash
344345
cd ../..
345346
git clone --depth=1 https://github.com/compiler-research/xeus-cpp.git
346-
export LLVM_VERSION=20
347+
export LLVM_VERSION=21
347348
cd ./xeus-cpp
348349
mkdir build
349350
cd build
@@ -356,20 +357,58 @@ emcmake cmake \
356357
-DXEUS_CPP_RESOURCE_DIR="$LLVM_BUILD_DIR/lib/clang/$LLVM_VERSION" \
357358
-DSYSROOT_PATH=$SYSROOT_PATH \
358359
..
359-
emmake make -j $(nproc --all) install
360+
EMCC_CFLAGS="-sSUPPORT_LONGJMP=wasm -fwasm-exceptions" emmake make -j $(nproc --all) install
360361
```
361362

362-
To build and test Jupyter Lite with this kernel locally you can execute the following
363+
and on Windows by executing
364+
365+
```powershell
366+
cd ..\..
367+
git clone --depth=1 https://github.com/compiler-research/xeus-cpp.git
368+
$env:LLVM_VERSION=21
369+
cd .\xeus-cpp
370+
mkdir build
371+
cd build
372+
emcmake cmake `
373+
-DCMAKE_BUILD_TYPE=Release `
374+
-DCMAKE_PREFIX_PATH="$env:PREFIX" `
375+
-DCMAKE_INSTALL_PREFIX="$env:PREFIX" `
376+
-DXEUS_CPP_EMSCRIPTEN_WASM_BUILD=ON `
377+
-DCMAKE_FIND_ROOT_PATH_MODE_PACKAGE=ON `
378+
-DXEUS_CPP_RESOURCE_DIR="$env:LLVM_BUILD_DIR/lib/clang/$env:LLVM_VERSION" `
379+
-DSYSROOT_PATH="$env:SYSROOT_PATH" `
380+
..
381+
$env:EMCC_CFLAGS="-sSUPPORT_LONGJMP=wasm -fwasm-exceptions"
382+
emmake make -j $(nproc --all) install
383+
$env:EMCC_CFLAGS=""
384+
```
385+
386+
To build and test Jupyter Lite with this kernel locally on Linux/MacOS you can execute the following
363387

364388
```bash
365389
cd ../..
366-
micromamba create -n xeus-lite-host jupyterlite-core=0.6 jupyterlite-xeus jupyter_server jupyterlab notebook python-libarchive-c -c conda-forge
390+
micromamba create -n xeus-lite-host jupyterlite-core jupyterlite-xeus jupyter_server jupyterlab notebook python-libarchive-c -c conda-forge
367391
micromamba activate xeus-lite-host
368392
jupyter lite serve --XeusAddon.prefix=$PREFIX \
369393
--contents xeus-cpp/notebooks/xeus-cpp-lite-demo.ipynb \
370-
--contents xeus-cpp/notebooks/smallpt.ipynb \
394+
--contents xeus-cpp/notebooks/tinyraytracer.ipynb \
371395
--contents xeus-cpp/notebooks/images/marie.png \
372396
--contents xeus-cpp/notebooks/audio/audio.wav \
373397
--XeusAddon.mounts="$PREFIX/share/xeus-cpp/tagfiles:/share/xeus-cpp/tagfiles" \
374398
--XeusAddon.mounts="$PREFIX/etc/xeus-cpp/tags.d:/etc/xeus-cpp/tags.d"
375399
```
400+
401+
and on Windows execute
402+
403+
```powershell
404+
cd ..\..
405+
micromamba create -n xeus-lite-host jupyterlite-core jupyterlite-xeus jupyter_server jupyterlab notebook python-libarchive-c -c conda-forge
406+
micromamba activate xeus-lite-host
407+
jupyter lite serve --XeusAddon.prefix="$env:PREFIX" `
408+
--contents xeus-cpp/notebooks/xeus-cpp-lite-demo.ipynb `
409+
--contents xeus-cpp/notebooks/tinyraytracer.ipynb `
410+
--contents xeus-cpp/notebooks/images/marie.png `
411+
--contents xeus-cpp/notebooks/audio/audio.wav `
412+
--XeusAddon.mounts="$env:PREFIX/share/xeus-cpp/tagfiles:/share/xeus-cpp/tagfiles" `
413+
--XeusAddon.mounts="$env:PREFIX/etc/xeus-cpp/tags.d:/etc/xeus-cpp/tags.d"
414+
```

interpreter/CppInterOp/README.md

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,20 @@ git clone --depth=1 https://github.com/compiler-research/cppyy-backend.git
104104

105105
#### Setup Clang-REPL
106106

107-
Clone the 20.x release of the LLVM project repository.
107+
Clone the 21.x release of the LLVM project repository.
108108

109109
```bash
110-
git clone --depth=1 --branch release/20.x https://github.com/llvm/llvm-project.git
110+
git clone --depth=1 --branch release/21.x https://github.com/llvm/llvm-project.git
111111
cd llvm-project
112112
```
113113

114+
If you want to have out-of-process JIT execution enabled in CppInterOp, then apply this patch on Linux-x86_64 and MacOS arm64 environment.
115+
> Note that this patch will not work for Windows because out-of-process JIT execution is currently implemented for Linux-x86_64 and MacOS arm64 only.
116+
117+
```bash
118+
git apply -v ../CppInterOp/patches/llvm/clang20-1-out-of-process.patch
119+
```
120+
114121
##### Build Clang-REPL
115122

116123
Clang-REPL is an interpreter that CppInterOp works alongside. Build Clang (and
@@ -140,6 +147,45 @@ export LLVM_DIR=$PWD
140147
cd ../
141148
```
142149

150+
##### Build Clang-REPL with Out-of-Process JIT Execution
151+
152+
To have ``Out-of-Process JIT Execution`` enabled, run following commands to build clang and clang-repl to support this feature:
153+
> Only for Linux x86_64 and MacOS arm64
154+
155+
```bash
156+
mkdir build
157+
cd build
158+
cmake -DLLVM_ENABLE_PROJECTS="clang;compiler-rt" \
159+
-DLLVM_TARGETS_TO_BUILD="host;NVPTX" \
160+
-DCMAKE_BUILD_TYPE=Release \
161+
-DLLVM_ENABLE_ASSERTIONS=ON \
162+
-DCLANG_ENABLE_STATIC_ANALYZER=OFF \
163+
-DCLANG_ENABLE_ARCMT=OFF \
164+
-DCLANG_ENABLE_FORMAT=OFF \
165+
-DCLANG_ENABLE_BOOTSTRAP=OFF \
166+
../llvm
167+
```
168+
169+
###### For Linux x86_64
170+
171+
```bash
172+
cmake --build . --target clang clang-repl llvm-jitlink-executor orc_rt-x86_64 --parallel $(nproc --all)
173+
```
174+
175+
###### For MacOS arm64
176+
177+
```bash
178+
cmake --build . --target clang clang-repl llvm-jitlink-executor orc_rt_osx --parallel $(sysctl -n hw.ncpu)
179+
```
180+
181+
Note the 'llvm-project' directory location by executing
182+
183+
```bash
184+
cd ../
185+
export LLVM_DIR=$PWD
186+
cd ../
187+
```
188+
143189
#### Environment variables
144190

145191
You will need to define the following environment variables for the build of CppInterOp and cppyy (as they clear for a new session, it is recommended that you also add these to your .bashrc in linux, .bash_profile if on MacOS). On Linux and MacOS you define as follows
@@ -163,10 +209,14 @@ Now CppInterOp can be built. This can be done by executing
163209
```bash
164210
mkdir CppInterOp/build/
165211
cd CppInterOp/build/
166-
cmake -DBUILD_SHARED_LIBS=ON -DCPPINTEROP_USE_CLING=ON -DCPPINTEROP_USE_REPL=Off -DCling_DIR=$LLVM_DIR/build/tools/cling -DLLVM_DIR=$LLVM_DIR/build/lib/cmake/llvm -DClang_DIR=$LLVM_DIR/build/lib/cmake/clang -DCMAKE_INSTALL_PREFIX=$CPPINTEROP_DIR ..
212+
cmake -DBUILD_SHARED_LIBS=ON -DLLVM_DIR=$LLVM_DIR/build/lib/cmake/llvm -DClang_DIR=$LLVM_DIR/build/lib/cmake/clang -DCMAKE_INSTALL_PREFIX=$CPPINTEROP_DIR ..
167213
cmake --build . --target install --parallel $(nproc --all)
168214
```
169215

216+
and
217+
218+
> Do make sure to pass ``DLLVM_BUILT_WITH_OOP_JIT=ON``, if you want to have out-of-process JIT execution feature enabled.
219+
170220
#### Testing CppInterOp
171221

172222
To test the built CppInterOp execute the following command in the CppInterOP build folder on Linux and MacOS
@@ -406,10 +456,10 @@ git clone --depth=1 https://github.com/compiler-research/cppyy-backend.git
406456

407457
#### Setup Clang-REPL
408458

409-
Clone the 20.x release of the LLVM project repository.
459+
Clone the 21.x release of the LLVM project repository.
410460

411461
```bash
412-
git clone --depth=1 --branch release/20.x https://github.com/llvm/llvm-project.git
462+
git clone --depth=1 --branch release/21.x https://github.com/llvm/llvm-project.git
413463
cd llvm-project
414464
```
415465

interpreter/CppInterOp/VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.8.0;dev
1+
1.9.0;dev

interpreter/CppInterOp/cmake/modules/GoogleTest.cmake

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ endif()
2020

2121
include(ExternalProject)
2222
if (EMSCRIPTEN)
23+
# FIXME: -sSUPPORT_LONGJMP=wasm in the default option causes a warning in the Emscripten build of Googletest
24+
# and as we treat warnings as errors in the ci, it causes the ci to fail.
25+
string(REPLACE "-sSUPPORT_LONGJMP=wasm" "" GOOGLETEST_CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
2326
set(config_cmd emcmake${EMCC_SUFFIX} cmake)
2427
if(CMAKE_GENERATOR STREQUAL "Ninja")
2528
set(build_cmd emmake${EMCC_SUFFIX} ninja)
@@ -34,8 +37,8 @@ endif()
3437
ExternalProject_Add(
3538
googletest
3639
GIT_REPOSITORY https://github.com/google/googletest.git
37-
GIT_SHALLOW 1
38-
GIT_TAG v1.17.0
40+
GIT_SHALLOW FALSE
41+
GIT_TAG fa8438ae6b70c57010177de47a9f13d7041a6328
3942
UPDATE_COMMAND ""
4043
# # Force separate output paths for debug and release builds to allow easy
4144
# # identification of correct lib in subsequent TARGET_LINK_LIBRARIES commands
@@ -49,7 +52,7 @@ ExternalProject_Add(
4952
-DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
5053
-DCMAKE_C_FLAGS=${CMAKE_C_FLAGS}
5154
-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
52-
-DCMAKE_CXX_FLAGS=${CMAKE_CXX_FLAGS}
55+
-DCMAKE_CXX_FLAGS=${GOOGLETEST_CMAKE_CXX_FLAGS}
5356
-DCMAKE_AR=${CMAKE_AR}
5457
-DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}
5558
${EXTRA_GTEST_OPTS}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
33a3e7e7629c0818bd92420568671325b016d6b2
1+
c211948a266d7d42466a3ea5a3ebbf81548486e5

0 commit comments

Comments
 (0)