Skip to content

Commit 90e277b

Browse files
authored
[LLVM] add wasm32 wheels (#267)
This PR factors out the LLVM distro portion of the build of `mlir-python-bindings-wasm` - i.e., it creates a `wasm32-empscripten` wheel/distro in addition to our current set of platforms. This is in preparation for #268 which will build out various `mlir-wgpu` things on top of this distro. One tedious/weeds thing to call out: just building the distro via cross-compiling (using [Emscripten's toolchain](https://github.com/emscripten-core/emscripten/blob/main/cmake/Modules/Platform/Emscripten.cmake)) didn't work because `find_package(Python3 Development)` fails to successfully find Python dev libs. I believe it has something to do with [CMP0190](https://cmake.org/cmake/help/latest/policy/CMP0190.html) but I couldn't debug it all the way. But `pyodide build` succeeds (because it somehow isn't cross-compiling technically???) so that's good enough. In addition some drive-by cleanup (remove `macos-13` from the GHA workflows).
1 parent 1e84323 commit 90e277b

File tree

16 files changed

+684
-451
lines changed

16 files changed

+684
-451
lines changed

.github/actions/setup_base/action.yml

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ inputs:
1010
os:
1111
required: true
1212
description: ''
13-
arch:
13+
host-arch:
14+
required: true
15+
description: ''
16+
target-arch:
1417
required: true
1518
description: ''
1619
container:
@@ -119,18 +122,18 @@ runs:
119122
dnf install -y sudo ncurses-compat-libs tmate python3-pip git
120123
121124
# install ccache
122-
if [[ "${{ inputs.arch }}" == "x86_64" ]]; then
125+
if [[ "${{ inputs.host-arch }}" == "x86_64" ]]; then
123126
curl -sLO https://github.com/ccache/ccache/releases/download/v4.10.2/ccache-4.10.2-linux-x86_64.tar.xz
124127
tar -xf ccache-4.10.2-linux-x86_64.tar.xz
125128
pushd ccache-4.10.2-linux-x86_64 && make install && popd
126-
elif [[ "${{ inputs.arch }}" == "aarch64" ]]; then
129+
elif [[ "${{ inputs.host-arch }}" == "aarch64" ]]; then
127130
dnf install -y ccache
128131
fi
129132
130133
# install gh-cli
131-
if [[ "${{ inputs.arch }}" == "x86_64" ]]; then
134+
if [[ "${{ inputs.host-arch }}" == "x86_64" ]]; then
132135
rpm_arch=amd64
133-
elif [[ "${{ inputs.arch }}" == "aarch64" ]]; then
136+
elif [[ "${{ inputs.host-arch }}" == "aarch64" ]]; then
134137
rpm_arch=arm64
135138
fi
136139
curl -sLO https://github.com/cli/cli/releases/download/v2.61.0/gh_2.61.0_linux_${rpm_arch}.rpm
@@ -235,3 +238,38 @@ runs:
235238
echo MACOSX_DEPLOYMENT_TARGET=11.0 >> $GITHUB_ENV
236239
echo CMAKE_OSX_DEPLOYMENT_TARGET=11.0 >> $GITHUB_ENV
237240
fi
241+
242+
- name: Install pyodide
243+
if: inputs.target-arch == 'wasm32'
244+
shell: bash
245+
run: |
246+
247+
pip install pyodide-build>=0.28.0
248+
echo "EMSCRIPTEN_VERSION=$(pyodide config get emscripten_version)" >> $GITHUB_ENV
249+
250+
- name: Setup Emscripten
251+
if: inputs.target-arch == 'wasm32'
252+
shell: bash
253+
run: |
254+
255+
git clone https://github.com/emscripten-core/emsdk.git
256+
pushd emsdk
257+
./emsdk install ${{ env.EMSCRIPTEN_VERSION }}
258+
./emsdk activate ${{ env.EMSCRIPTEN_VERSION }}
259+
echo "EMSDK=$PWD" >> $GITHUB_ENV
260+
popd
261+
262+
- name: Setup emscripten ccache
263+
if: inputs.target-arch == 'wasm32'
264+
shell: bash
265+
run: |
266+
267+
source "$EMSDK/emsdk_env.sh"
268+
269+
$CMAKE_C_COMPILER_LAUNCHER -z
270+
# https://github.com/juj/ccache/blob/c4284c78e9ff286ee5208f449af4c2aaba062d37/cmake/StandardWarnings.cmake#L5
271+
# https://github.com/juj/ccache/blob/c4284c78e9ff286ee5208f449af4c2aaba062d37/CMakeLists.txt#L61
272+
# error: builtin __has_trivial_copy is deprecated; use __is_trivially_copyable instead [-Werror,-Wdeprecated-builtins]
273+
VERBOSE=1 CCACHE_COMPILERCHECK="string:$(g++ --version | head -n 1)" CC=gcc CXX=g++ emsdk install ccache-git-emscripten-64bit
274+
$CMAKE_C_COMPILER_LAUNCHER -s
275+
emsdk activate ccache-git-emscripten-64bit

.github/workflows/build_llvm.yml

Lines changed: 137 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,23 @@ on:
2020
default: false
2121
pull_request:
2222
paths:
23-
- ".github/actions/setup_base"
23+
- ".github/actions/setup_base/action.yml"
2424
- ".github/workflows/build_llvm.yml"
25-
- "scripts/cmake/llvm_cache.cmake"
26-
- "scripts/cmake/llvm_wasm_cache.cmake"
25+
- "scripts/cmake/*"
26+
- "scripts/llvm_wasm/*"
2727
- "scripts/build_llvm.sh"
28-
- "scripts/build_llvm_wasi.sh"
28+
- "scripts/build_mlir_native_tools.sh"
2929
- "third_party/llvm-project"
3030
push:
3131
branches:
3232
- main
3333
paths:
34-
- ".github/actions/setup_base"
34+
- ".github/actions/setup_base/action.yml"
3535
- ".github/workflows/build_llvm.yml"
36-
- "scripts/cmake/llvm_cache.cmake"
37-
- "scripts/cmake/llvm_wasm_cache.cmake"
36+
- "scripts/cmake/*"
37+
- "scripts/llvm_wasm/*"
3838
- "scripts/build_llvm.sh"
39-
- "scripts/build_llvm_wasi.sh"
39+
- "scripts/build_mlir_native_tools.sh"
4040
- "third_party/llvm-project"
4141

4242
concurrency:
@@ -48,6 +48,7 @@ concurrency:
4848
cancel-in-progress: true
4949

5050
jobs:
51+
5152
build:
5253

5354
strategy:
@@ -58,33 +59,27 @@ jobs:
5859
runs-on: "ubuntu-22.04"
5960
container: "quay.io/pypa/manylinux_2_28_x86_64"
6061
os: "almalinux"
61-
arch: x86_64
62+
host-arch: x86_64
63+
target-arch: x86_64
64+
6265
- name: "manylinux_aarch64"
6366
runs-on: "ubuntu-22.04-arm"
6467
container: "quay.io/pypa/manylinux_2_34_aarch64"
6568
os: "almalinux"
66-
arch: aarch64
67-
# - name: "ubuntu_aarch64"
68-
# runs-on: "ubuntu-22.04-arm"
69-
# os: "ubuntu"
70-
# arch: aarch64
71-
# - name: "wasm_wasi"
72-
# runs-on: "ubuntu-22.04"
73-
# container: "quay.io/pypa/manylinux_2_28_x86_64"
74-
# os: "almalinux"
75-
# arch: "wasm32-wasi"
69+
host-arch: "aarch64"
70+
target-arch: "aarch64"
71+
7672
- name: "windows_amd64"
7773
runs-on: "windows-2022"
7874
os: "windows"
79-
arch: amd64
75+
host-arch: amd64
76+
target-arch: amd64
77+
8078
- name: "macos_arm64"
8179
runs-on: "macos-14"
8280
os: "macos"
83-
arch: arm64
84-
# - name: "macos_x86_64"
85-
# runs-on: "macos-13"
86-
# os: "macos"
87-
# arch: x86_64
81+
host-arch: arm64
82+
target-arch: arm64
8883

8984
runs-on: ${{ matrix.runs-on }}
9085

@@ -127,7 +122,8 @@ jobs:
127122
cache-key: ${{ env.cache-key }}
128123
restore-key: "mlir_${{ matrix.name }}_clang"
129124
os: ${{ matrix.os }}
130-
arch: ${{ matrix.arch }}
125+
host-arch: ${{ matrix.host-arch }}
126+
target-arch: ${{ matrix.target-arch }}
131127
container: ${{ matrix.container }}
132128
runs-on: ${{ matrix.runs-on }}
133129

@@ -150,14 +146,11 @@ jobs:
150146
###############################
151147
# Build MLIR/LLVM distro
152148
###############################
153-
if [[ "${{ matrix.arch }}" == "wasm32-wasi" ]]; then
154-
scripts/build_llvm_wasi.sh
155-
else
156-
scripts/build_llvm.sh
157-
echo "*********************** SMOKE TEST *********************************"
158-
"$LLVM_INSTALL_DIR/bin/mlir-tblgen" --version
159-
echo "*********************** SMOKE TEST *********************************"
160-
fi
149+
150+
scripts/build_llvm.sh
151+
echo "*********************** SMOKE TEST *********************************"
152+
"$LLVM_INSTALL_DIR/bin/mlir-tblgen" --version
153+
echo "*********************** SMOKE TEST *********************************"
161154
ccache -s
162155
163156
###############################
@@ -178,7 +171,7 @@ jobs:
178171
elif [ x"${{ matrix.os }}" == x"windows" ]; then
179172
PLAT="win"
180173
fi
181-
PLAT="${PLAT}_${{ matrix.arch }}"
174+
PLAT="${PLAT}_${{ matrix.host-arch }}"
182175
183176
pushd projects/mlir-native-tools
184177
$python3_command setup.py bdist_wheel --dist-dir ../../wheelhouse --plat $PLAT
@@ -256,9 +249,117 @@ jobs:
256249
limit-access-to-actor: true
257250
install-dependencies: ${{ startsWith(matrix.runs-on, 'macos') || startsWith(matrix.runs-on, 'windows') }}
258251

252+
build-wasm:
253+
254+
runs-on: ubuntu-22.04
255+
256+
name: "Build llvm-mlir wasm"
257+
258+
defaults:
259+
run:
260+
shell: bash
261+
262+
permissions:
263+
id-token: write
264+
contents: write
265+
266+
env:
267+
cache-key: mlir_wasm_emscripten_${{ format('{0}-{1}', github.ref_name, github.run_number) }}
268+
269+
steps:
270+
- name: "Check out repository"
271+
uses: actions/[email protected]
272+
with:
273+
submodules: true
274+
275+
- name: "Setup base"
276+
uses: ./.github/actions/setup_base
277+
id: setup_base
278+
with:
279+
cache-key: ${{ env.cache-key }}
280+
restore-key: "mlir_wasm_emscripten"
281+
os: "ubuntu"
282+
host-arch: "x86_64"
283+
target-arch: "wasm32"
284+
285+
- name: Build MLIR and LLVM
286+
run: |
287+
288+
###############################
289+
# Build host mlir-native-tools wheel
290+
###############################
291+
292+
scripts/build_mlir_native_tools.sh
293+
export LLVM_NATIVE_TOOL_DIR="$PWD/mlir-native-tools-install/bin"
294+
export LLVM_NATIVE_TOOL_DIR="$LLVM_NATIVE_TOOL_DIR"
295+
export LLVM_TABLEGEN="$LLVM_NATIVE_TOOL_DIR/llvm-tblgen"
296+
export MLIR_TABLEGEN="$LLVM_NATIVE_TOOL_DIR/mlir-tblgen"
297+
export MLIR_LINALG_ODS_YAML_GEN="$LLVM_NATIVE_TOOL_DIR/mlir-linalg-ods-yaml-gen"
298+
299+
source "$EMSDK/emsdk_env.sh"
300+
301+
export PATH="$EMSDK/ccache/git-emscripten_64bit/bin:$PATH"
302+
export CCACHE_COMPILERCHECK="string:$($EMSDK/upstream/bin/clang --version | head -n 1)"
303+
export CCACHE="$EMSDK/ccache/git-emscripten_64bit/bin/ccache"
304+
export "CMAKE_C_COMPILER_LAUNCHER=$CCACHE"
305+
export "CMAKE_CXX_COMPILER_LAUNCHER=$CCACHE"
306+
307+
export LLVM_SOURCE_DIR="$PWD/third_party/llvm-project"
308+
pushd $LLVM_SOURCE_DIR && LLVM_SHA_SHORT="$(git rev-parse --short HEAD)" && popd
309+
export WHEEL_VERSION="$(date +'%Y%m%d')+$LLVM_SHA_SHORT"
310+
echo "WHEEL_VERSION=$WHEEL_VERSION" >> $GITHUB_OUTPUT
311+
312+
###############################
313+
# Build MLIR/LLVM distro
314+
###############################
315+
316+
$CCACHE -z
317+
pyodide build scripts/llvm_wasm -o wheelhouse --compression-level 10
318+
$CCACHE -s
319+
320+
- name: Upload ccache log
321+
uses: actions/upload-artifact@v4
322+
with:
323+
name: llvm_ccache_log_mlir_wasm32
324+
path: '/tmp/ccache.log'
325+
326+
- name: "Save cache"
327+
uses: actions/cache/save@v3
328+
if: (!cancelled() && github.event_name == 'push' && github.ref_name == 'main')
329+
with:
330+
path: ${{ steps.setup_base.outputs.cache-dir }}
331+
key: ${{ env.cache-key }}
332+
333+
- name: Upload LLVM distro artifacts
334+
uses: actions/upload-artifact@v4
335+
with:
336+
name: mlir_ubuntu_wasm32_artifact
337+
path: llvm-install
338+
if-no-files-found: warn
339+
340+
- name: Upload mlir-wheel artifact
341+
uses: actions/upload-artifact@v4
342+
with:
343+
name: mlir_wheel_ubuntu_wasm32_artifact
344+
path: wheelhouse/mlir_wheel*
345+
if-no-files-found: warn
346+
347+
- name: Release current commit
348+
if: (github.event_name == 'push' && github.ref_name == 'main') || github.event_name == 'workflow_dispatch'
349+
uses: ncipollo/[email protected]
350+
with:
351+
artifacts: "*.tar.gz,wheelhouse/*.whl"
352+
token: "${{ secrets.GITHUB_TOKEN }}"
353+
tag: "llvm"
354+
name: "llvm"
355+
removeArtifacts: false
356+
allowUpdates: true
357+
replacesArtifacts: true
358+
makeLatest: true
359+
259360
call-build-python-bindings:
260361

261-
needs: [build]
362+
needs: [build, build-wasm]
262363

263364
permissions:
264365
contents: write

0 commit comments

Comments
 (0)