Skip to content

Commit 2626ddd

Browse files
authored
Update Emscripten build to llvm 22 (#907)
1 parent 9c8b2b5 commit 2626ddd

10 files changed

Lines changed: 159 additions & 28 deletions

.github/actions/Build_LLVM_WASM/action.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ runs:
3535
# Apply repl-only emscripten patches; cling has its own patch flow above.
3636
if [[ "${cling_on}" != "ON" ]]; then
3737
rt='${{ matrix.clang-runtime }}'
38-
if [[ "$rt" =~ ^(19|20|21)$ ]]; then
38+
if [[ "$rt" =~ ^(19|20|21|22)$ ]]; then
3939
git apply -v ../patches/llvm/emscripten-clang${rt}-*.patch
4040
echo "Applied emscripten-clang${rt}-*.patch"
4141
fi
@@ -124,7 +124,7 @@ runs:
124124
# Apply repl-only emscripten patches; cling has its own patch flow above.
125125
if ( -not $cling_on ) {
126126
$rt = "${{ matrix.clang-runtime }}"
127-
if ( $rt -match "^(19|20|21)$" ) {
127+
if ( $rt -match "^(19|20|21|22)$" ) {
128128
Get-ChildItem -Path "..\patches\llvm" -Filter "emscripten-clang${rt}-*.patch" | ForEach-Object {
129129
git apply -v $_.FullName
130130
}

.github/workflows/deploy-pages.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ jobs:
2626
matrix:
2727
# micromamba_shell_init is derived below from runner.os.
2828
include:
29-
- { name: osx26-arm-emscr-llvm21, os: macos-26, clang-runtime: '21', wasm: true }
29+
- { name: osx26-arm-emscr-llvm22, os: macos-26, clang-runtime: '22', wasm: true }
3030

3131
steps:
3232
- uses: actions/checkout@v6

.github/workflows/emscripten.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ jobs:
3232
# (cache key reads matrix.clang-runtime, so the field must live on
3333
# each row). micromamba_shell_init is derived below from runner.os.
3434
include:
35-
- { name: ubu24-arm-emscr-llvm21, os: ubuntu-24.04-arm, clang-runtime: &clang_rt '21', wasm: true }
36-
- { name: osx26-arm-emscr-llvm21, os: macos-26, clang-runtime: *clang_rt, wasm: true }
37-
- { name: ubu24-x86-emscr-llvm21, os: ubuntu-24.04, clang-runtime: *clang_rt, wasm: true }
38-
- { name: win2025-x86-emscr-llvm21, os: windows-2025, clang-runtime: *clang_rt, wasm: true, skip-in-prs: true }
35+
- { name: ubu24-arm-emscr-llvm22, os: ubuntu-24.04-arm, clang-runtime: &clang_rt '22', wasm: true }
36+
- { name: osx26-arm-emscr-llvm22, os: macos-26, clang-runtime: *clang_rt, wasm: true }
37+
- { name: ubu24-x86-emscr-llvm22, os: ubuntu-24.04, clang-runtime: *clang_rt, wasm: true }
38+
- { name: win2025-x86-emscr-llvm22, os: windows-2025, clang-runtime: *clang_rt, wasm: true, skip-in-prs: true }
3939

4040
steps:
4141
- uses: actions/checkout@v6

Emscripten-build-instructions.md

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ It should be noted that the wasm build of CppInterOp is still experimental and s
55
## CppInterOp Wasm Build Instructions
66

77
This document first starts with the instructions on how to build a wasm build of CppInterOp. Before we start it should be noted that
8-
unlike the non wasm version of CppInterOp we currently only support the Clang-REPL backend using llvm>19.
8+
unlike the non wasm version of CppInterOp we currently only support the Clang-REPL backend using llvm>20.
99
We will first make folder to build our wasm build of CppInterOp. This can be done by executing the following command
1010

1111
```bash
@@ -42,11 +42,11 @@ $env:PWD_DIR= $PWD.Path
4242
$env:SYSROOT_PATH="$env:EMSDK/upstream/emscripten/cache/sysroot"
4343
```
4444

45-
Now clone the 21.x release of the LLVM project repository and CppInterOp (the building of the emscripten version of llvm can be
45+
Now clone the 22.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/21.x https://github.com/llvm/llvm-project.git
49+
git clone --depth=1 --branch release/22.x https://github.com/llvm/llvm-project.git
5050
git clone --depth=1 https://github.com/compiler-research/CppInterOp.git
5151
```
5252

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

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

6161
On Windows execute the following
6262

6363
```powershell
6464
cd .\llvm-project\
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
65+
cp -r ..\patches\llvm\emscripten-clang22*
66+
git apply -v emscripten-clang22-1-enable_exception_handling.patch
67+
git apply -v emscripten-clang22-2-webassembly_target_machine_reordering.patch
6968
```
7069

7170
We are now in a position to build an emscripten build of llvm by executing the following on Linux
@@ -381,7 +380,7 @@ of llvm you are building against)
381380
```bash
382381
cd ../..
383382
git clone --depth=1 https://github.com/compiler-research/xeus-cpp.git
384-
export LLVM_VERSION=21
383+
export LLVM_VERSION=22
385384
cd ./xeus-cpp
386385
mkdir build
387386
cd build
@@ -402,7 +401,7 @@ and on Windows by executing
402401
```powershell
403402
cd ..\..
404403
git clone --depth=1 https://github.com/compiler-research/xeus-cpp.git
405-
$env:LLVM_VERSION=21
404+
$env:LLVM_VERSION=22
406405
cd .\xeus-cpp
407406
mkdir build
408407
cd build

docs/Emscripten-build-instructions.rst

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ experimental and subject to change. Try a Jupyter Lite demo of xeus-cpp by click
1616
This document first starts with the instructions on how to build a wasm
1717
build of CppInterOp. Before we start it should be noted that unlike the
1818
non wasm version of CppInterOp we currently only support the Clang-REPL
19-
backend using llvm>19. We will first make folder to
19+
backend using llvm>20. We will first make folder to
2020
build our wasm build of CppInterOp. This can be done by executing the
2121
following command
2222

@@ -57,15 +57,15 @@ and on Windows execute in Powershell
5757
$env:PWD_DIR= $PWD.Path
5858
$env:SYSROOT_PATH="$env:EMSDK/upstream/emscripten/cache/sysroot"
5959
60-
Now clone the 21.x release of the LLVM project repository and CppInterOp
60+
Now clone the 22.x release of the LLVM project repository and CppInterOp
6161
(the building of the emscripten version of llvm can be avoided by
6262
executing micromamba install llvm -c
6363
<https://repo.mamba.pm/emscripten-forge> and setting the LLVM_BUILD_DIR/$env:LLVM_BUILD_DIR
6464
appropriately)
6565

6666
.. code:: bash
6767
68-
git clone --depth=1 --branch release/21.x https://github.com/llvm/llvm-project.git
68+
git clone --depth=1 --branch release/22.x https://github.com/llvm/llvm-project.git
6969
git clone --depth=1 https://github.com/compiler-research/CppInterOp.git
7070
7171
Now move into the cloned llvm-project folder and apply the required patches. On Linux and osx this
@@ -74,17 +74,16 @@ executing
7474
.. code:: bash
7575
7676
cd ./llvm-project/
77-
git apply -v ../CppInterOp/patches/llvm/emscripten-clang21-*.patch
77+
git apply -v ../CppInterOp/patches/llvm/emscripten-clang22-*.patch
7878
7979
On Windows execute the following
8080

8181
.. code:: powershell
8282
8383
cd .\llvm-project\
84-
cp -r ..\patches\llvm\emscripten-clang21*
85-
git apply -v emscripten-clang21-1-shift-temporary-files-to-tmp-dir.patch
86-
git apply -v emscripten-clang21-2-enable_exception_handling.patch
87-
git apply -v emscripten-clang21-3-webassembly_target_machine_reordering.patch
84+
cp -r ..\patches\llvm\emscripten-clang22*
85+
git apply -v emscripten-clang22-1-enable_exception_handling.patch
86+
git apply -v emscripten-clang22-2-webassembly_target_machine_reordering.patch
8887
8988
We are now in a position to build an emscripten build of llvm by executing the following on Linux
9089
and osx
@@ -405,7 +404,7 @@ by executing (replace LLVM_VERSION with the version of llvm you are building aga
405404
406405
cd ../..
407406
git clone --depth=1 https://github.com/compiler-research/xeus-cpp.git
408-
export LLVM_VERSION=21
407+
export LLVM_VERSION=22
409408
cd ./xeus-cpp
410409
mkdir build
411410
cd build
@@ -426,7 +425,7 @@ and on Windows by executing
426425
427426
cd ..\..
428427
git clone --depth=1 https://github.com/compiler-research/xeus-cpp.git
429-
$env:LLVM_VERSION=21
428+
$env:LLVM_VERSION=22
430429
cd .\xeus-cpp
431430
mkdir build
432431
cd build

lib/CppInterOp/exports.ld

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,5 @@
5555
-Wl,--export=_ZN5clang11Interpreter6createENSt3__210unique_ptrINS_16CompilerInstanceENS1_14default_deleteIS3_EEEENS2_IN4llvm3orc12LLJITBuilderENS4_IS9_EEEE
5656
-Wl,--export=_ZNK5clang13CXXRecordDecl19isInjectedClassNameEv
5757
-Wl,--export=_ZNK5clang8QualType11getAsStringERKNS_14PrintingPolicyE
58+
-Wl,--export=_ZN5clang11Interpreter6createENSt3__210unique_ptrINS_16CompilerInstanceENS1_14default_deleteIS3_EEEENS2_INS_26IncrementalExecutorBuilderENS4_IS7_EEEE
59+
-Wl,--export=_ZNK5clang7TagDecl13getDefinitionEv
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
diff --git a/clang/include/clang/Frontend/CompilerInstance.h b/clang/include/clang/Frontend/CompilerInstance.h
2+
index f56da69a05caf..f5dfc36953308 100644
3+
--- a/clang/include/clang/Frontend/CompilerInstance.h
4+
+++ b/clang/include/clang/Frontend/CompilerInstance.h
5+
@@ -246,6 +246,11 @@ class CompilerInstance : public ModuleLoader {
6+
/// Load the list of plugins requested in the \c FrontendOptions.
7+
void LoadRequestedPlugins();
8+
9+
+ /// Parse and apply LLVM command line arguments from FrontendOptions.
10+
+ /// This processes the LLVMArgs option that comes from -mllvm flags.
11+
+ /// This should be called after plugins are loaded and before ExecuteAction.
12+
+ void parseLLVMArgs();
13+
+
14+
/// @}
15+
/// @name Compiler Invocation and Options
16+
/// @{
17+
diff --git a/clang/lib/Frontend/CompilerInstance.cpp b/clang/lib/Frontend/CompilerInstance.cpp
18+
index 2cd7888b0d192..a1ea9486ac406 100644
19+
--- a/clang/lib/Frontend/CompilerInstance.cpp
20+
+++ b/clang/lib/Frontend/CompilerInstance.cpp
21+
@@ -1102,6 +1102,20 @@ void CompilerInstance::LoadRequestedPlugins() {
22+
}
23+
}
24+
25+
+void CompilerInstance::parseLLVMArgs() {
26+
+ if (!getFrontendOpts().LLVMArgs.empty()) {
27+
+ unsigned NumArgs = getFrontendOpts().LLVMArgs.size();
28+
+ auto Args = std::make_unique<const char *[]>(NumArgs + 2);
29+
+ Args[0] = "clang (LLVM option parsing)";
30+
+ for (unsigned i = 0; i != NumArgs; ++i)
31+
+ Args[i + 1] = getFrontendOpts().LLVMArgs[i].c_str();
32+
+ Args[NumArgs + 1] = nullptr;
33+
+ llvm::cl::ParseCommandLineOptions(NumArgs + 1, Args.get(), /*Overview=*/"",
34+
+ /*Errs=*/nullptr,
35+
+ /*VFS=*/&getVirtualFileSystem());
36+
+ }
37+
+}
38+
+
39+
/// Determine the appropriate source input kind based on language
40+
/// options.
41+
static Language getLanguageFromOptions(const LangOptions &LangOpts) {
42+
diff --git a/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp b/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp
43+
index 05f646b43e3c4..9fa6fbb0017e1 100644
44+
--- a/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp
45+
+++ b/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp
46+
@@ -237,17 +237,7 @@ bool ExecuteCompilerInvocation(CompilerInstance *Clang) {
47+
//
48+
// FIXME: Remove this, one day.
49+
// This should happen AFTER plugins have been loaded!
50+
- if (!Clang->getFrontendOpts().LLVMArgs.empty()) {
51+
- unsigned NumArgs = Clang->getFrontendOpts().LLVMArgs.size();
52+
- auto Args = std::make_unique<const char*[]>(NumArgs + 2);
53+
- Args[0] = "clang (LLVM option parsing)";
54+
- for (unsigned i = 0; i != NumArgs; ++i)
55+
- Args[i + 1] = Clang->getFrontendOpts().LLVMArgs[i].c_str();
56+
- Args[NumArgs + 1] = nullptr;
57+
- llvm::cl::ParseCommandLineOptions(NumArgs + 1, Args.get(), /*Overview=*/"",
58+
- /*Errs=*/nullptr,
59+
- /*VFS=*/&Clang->getVirtualFileSystem());
60+
- }
61+
+ Clang->parseLLVMArgs();
62+
63+
#if CLANG_ENABLE_STATIC_ANALYZER
64+
// These should happen AFTER plugins have been loaded!
65+
diff --git a/clang/lib/Interpreter/Interpreter.cpp b/clang/lib/Interpreter/Interpreter.cpp
66+
index 9c94cfa5ee381..bfb6c2a0505d1 100644
67+
--- a/clang/lib/Interpreter/Interpreter.cpp
68+
+++ b/clang/lib/Interpreter/Interpreter.cpp
69+
@@ -257,6 +257,9 @@ Interpreter::Interpreter(std::unique_ptr<CompilerInstance> Instance,
70+
auto LLVMCtx = std::make_unique<llvm::LLVMContext>();
71+
TSCtx = std::make_unique<llvm::orc::ThreadSafeContext>(std::move(LLVMCtx));
72+
73+
+ // Honor -mllvm options
74+
+ CI->parseLLVMArgs();
75+
+
76+
Act = TSCtx->withContextDo([&](llvm::LLVMContext *Ctx) {
77+
return std::make_unique<IncrementalAction>(*CI, *Ctx, ErrOut, *this,
78+
std::move(Consumer));
79+
@@ -465,6 +468,12 @@ Interpreter::Parse(llvm::StringRef Code) {
80+
return std::move(Err);
81+
}
82+
83+
+ // Re-apply stored -mllvm options; wasm builds reset LLVM opts in wasm-ld.
84+
+ llvm::Triple Triple(getCompilerInstance()->getTargetOpts().Triple);
85+
+ if (Triple.isWasm()) {
86+
+ getCompilerInstance()->parseLLVMArgs();
87+
+ }
88+
+
89+
// Tell the interpreter sliently ignore unused expressions since value
90+
// printing could cause it.
91+
getCompilerInstance()->getDiagnostics().setSeverity(
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.cpp b/llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.cpp
2+
index 6827ee652794..3ad7ec5d32b6 100644
3+
--- a/llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.cpp
4+
+++ b/llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.cpp
5+
@@ -227,8 +227,8 @@ WebAssemblyTargetMachine::WebAssemblyTargetMachine(
6+
this->Options.DataSections = true;
7+
this->Options.UniqueSectionNames = true;
8+
9+
- initAsmInfo();
10+
basicCheckForEHAndSjLj(this);
11+
+ initAsmInfo();
12+
// Note that we don't use setRequiresStructuredCFG(true). It disables
13+
// optimizations than we're ok with, and want, such as critical edge
14+
// splitting and tail merging.

unittests/CppInterOp/FunctionReflectionTest.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1607,7 +1607,11 @@ TYPED_TEST(CPPINTEROP_TEST_MODE, FunctionReflection_JitCallAdvanced) {
16071607
#if CLANG_VERSION_MAJOR == 20 && defined(CPPINTEROP_USE_CLING) && defined(_WIN32)
16081608
GTEST_SKIP() << "Test fails with Cling on Windows";
16091609
#endif
1610-
1610+
#ifdef EMSCRIPTEN
1611+
#if CLANG_VERSION_MAJOR > 21
1612+
GTEST_SKIP() << "Test fails for Emscipten builds using LLVM 22";
1613+
#endif
1614+
#endif
16111615
if (TypeParam::isOutOfProcess)
16121616
GTEST_SKIP() << "Test fails for OOP JIT builds";
16131617

@@ -2413,6 +2417,11 @@ TYPED_TEST(CPPINTEROP_TEST_MODE, FunctionReflection_GetFunctionArgDefault) {
24132417
TYPED_TEST(CPPINTEROP_TEST_MODE, FunctionReflection_Construct) {
24142418
#ifdef _WIN32
24152419
GTEST_SKIP() << "Disabled on Windows. Needs fixing.";
2420+
#endif
2421+
#ifdef EMSCRIPTEN
2422+
#if CLANG_VERSION_MAJOR > 21
2423+
GTEST_SKIP() << "Test fails for Emscipten builds using LLVM 22";
2424+
#endif
24162425
#endif
24172426
if (TypeParam::isOutOfProcess)
24182427
GTEST_SKIP() << "Test fails for OOP JIT builds";
@@ -2499,6 +2508,11 @@ TYPED_TEST(CPPINTEROP_TEST_MODE, FunctionReflection_Construct) {
24992508
TYPED_TEST(CPPINTEROP_TEST_MODE, FunctionReflection_ConstructPOD) {
25002509
#ifdef _WIN32
25012510
GTEST_SKIP() << "Disabled on Windows. Needs fixing.";
2511+
#endif
2512+
#ifdef EMSCRIPTEN
2513+
#if CLANG_VERSION_MAJOR > 21
2514+
GTEST_SKIP() << "Test fails for Emscipten builds using LLVM 22";
2515+
#endif
25022516
#endif
25032517
if (TypeParam::isOutOfProcess)
25042518
GTEST_SKIP() << "Test fails for OOP JIT builds";
@@ -2539,6 +2553,11 @@ TYPED_TEST(CPPINTEROP_TEST_MODE, FunctionReflection_ConstructPOD) {
25392553
TYPED_TEST(CPPINTEROP_TEST_MODE, FunctionReflection_ConstructNested) {
25402554
#ifdef _WIN32
25412555
GTEST_SKIP() << "Disabled on Windows. Needs fixing.";
2556+
#endif
2557+
#ifdef EMSCRIPTEN
2558+
#if CLANG_VERSION_MAJOR > 21
2559+
GTEST_SKIP() << "Test fails for Emscipten builds using LLVM 22";
2560+
#endif
25422561
#endif
25432562
if (TypeParam::isOutOfProcess)
25442563
GTEST_SKIP() << "Test fails for OOP JIT builds";

unittests/CppInterOp/TracingTests.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "../../lib/CppInterOp/Tracing.h"
22

3+
#include "clang/Basic/Version.h"
4+
35
#include "CppInterOp/CppInterOp.h"
46

57
#include "llvm/Support/FileSystem.h"
@@ -636,6 +638,11 @@ TEST_F(TracingTest, ReproducerCompilesViaInterpreter) {
636638
// ---------------------------------------------------------------------------
637639

638640
TEST_F(TracingTest, JitCallWrapperSourceLogged) {
641+
#ifdef EMSCRIPTEN
642+
#if CLANG_VERSION_MAJOR > 21
643+
GTEST_SKIP() << "Test fails for Emscipten builds using LLVM 22";
644+
#endif
645+
#endif
639646
Cpp::CreateInterpreter({});
640647
ASSERT_NE(TraceInfo::TheTraceInfo, nullptr);
641648

0 commit comments

Comments
 (0)