Skip to content
Merged
Show file tree
Hide file tree
Changes from 27 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
d09caac
[FEAT] Add tvm-ffi-orcjit addon with ORC JIT support
cyx-6 Mar 23, 2026
3d802f2
Merge commit 'd153a2c9407ee04ec3dc1ccbdf6a0664854337b7' into orcjit
cyx-6 Mar 23, 2026
ba5c121
add a primer document for the reader to understand the techniques use…
yaoyaoding Mar 23, 2026
d9fa347
Correct an error resulting from AI hallucination
yaoyaoding Mar 23, 2026
87ce090
add more intro to Platform
yaoyaoding Mar 23, 2026
da45d5a
[FIX] Fix ref cycle in InitFiniPlugin and dangling pointer in GetFunc…
cyx-6 Mar 24, 2026
edef2a9
[EXTRA] Address review feedback: rename tools to scripts, simplify ru…
cyx-6 Mar 24, 2026
925aeec
[FIX] Update CI workflow to reference scripts/ instead of tools/
cyx-6 Mar 24, 2026
8715111
[FIX] Move LLVM environment vars from pyproject.toml to workflow YAML
cyx-6 Mar 24, 2026
ce36430
[EXTRA] Rename call_llvm to TVM_FFI_ORCJIT_LLVM_CALL macro; rename pr…
cyx-6 Mar 24, 2026
45252b9
[EXTRA] Unify tvm_ffi.cpp.build API with single sources parameter
cyx-6 Mar 24, 2026
8afa314
fix CIBW_BEFORE_TEST path: {project} is already the repo root
cyx-6 Mar 24, 2026
2637553
fix CI: install tvm-ffi from source after wheel to prevent PyPI override
cyx-6 Mar 24, 2026
06a41bf
fix CI: add ninja to test requires, drop --no-deps to get full depend…
cyx-6 Mar 24, 2026
4225c06
fix test: use pytest_configure hook to build objects before collection
cyx-6 Mar 24, 2026
2a3535a
fix: add -mno-outline-atomics on aarch64, use clang-cl on Windows
cyx-6 Mar 24, 2026
6a01796
fix: return correct intermediate object name for C files on Windows
cyx-6 Mar 24, 2026
5170092
fix cmake-format: rewrap comment line
cyx-6 Mar 24, 2026
e3475c2
address review feedback: null checks, atomic counter, docstrings, ver…
cyx-6 Mar 25, 2026
f3aa823
fix: use correct symbol name in VisitContextSymbols callback
cyx-6 Mar 25, 2026
44a6c49
doc: add three-platform init/fini strategy documentation
cyx-6 Mar 25, 2026
a2226bb
fix: use plain comments to avoid stray backtick/hash compiler errors
cyx-6 Mar 25, 2026
aec22bd
doc: clarify initialize/deinitialize refcount balance in GetSymbol
cyx-6 Mar 26, 2026
d73f00a
fix: use conda zstd-static instead of building from source
cyx-6 Mar 26, 2026
c48c353
fix: remove zstd build-from-source, hide symbols by default
cyx-6 Mar 26, 2026
7deaae9
doc: add build-from-source instructions, remove stale script references
cyx-6 Mar 26, 2026
9cd4e4b
fix: cmake-format set_target_properties
cyx-6 Mar 26, 2026
c0a896f
fix: rename tvm-ffi-orcjit folder to tvm_ffi_orcjit
cyx-6 Mar 27, 2026
b36e343
refactor: simplify orcjit test structure
cyx-6 Mar 27, 2026
929618c
fix: remove unused Path import, fix ruff import ordering
cyx-6 Mar 27, 2026
3aad6d9
fix: cuda tests
cyx-6 Mar 27, 2026
1a9f575
ci: add reusable orcjit wheel action, integrate into CI, add publish …
cyx-6 Mar 27, 2026
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
118 changes: 118 additions & 0 deletions .github/workflows/tvm-ffi-orcjit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you 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.

name: OrcJIT Build

on:
push:
paths:
- "addons/tvm-ffi-orcjit/**"
- ".github/workflows/tvm-ffi-orcjit.yml"
pull_request:
paths:
- "addons/tvm-ffi-orcjit/**"
- ".github/workflows/tvm-ffi-orcjit.yml"
workflow_dispatch:

jobs:
build_wheels:
name: ${{ matrix.os }} (${{ matrix.arch }})
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
include:
- {os: ubuntu-latest, arch: x86_64, build: "cp312-manylinux_x86_64"}
- {os: ubuntu-24.04-arm, arch: aarch64, build: "cp312-manylinux_aarch64"}
- {os: macos-14, arch: arm64, build: "cp312-macosx_arm64"}
- {os: windows-latest, arch: AMD64, build: "cp312-win_amd64"}

steps:
- uses: actions/checkout@v5
with:
submodules: recursive

- uses: ./.github/actions/detect-env-vars
id: env_vars

# ---- Cache LLVM prefix ----
- name: Cache LLVM
uses: actions/cache@v4
id: llvm-cache
with:
path: ${{ runner.os == 'Windows' && 'C:/opt/llvm' || '/opt/llvm' }}
key: llvm-22.1.0-${{ runner.os }}-${{ matrix.arch }}-v3

# ---- Install LLVM via conda (cache miss only) ----
- name: Setup conda
if: steps.llvm-cache.outputs.cache-hit != 'true'
uses: conda-incubator/setup-miniconda@fc2d68f6413eb2d87b895e92f8584b5b94a10167 # v3.3.0
continue-on-error: true
id: conda1
with:
miniforge-version: latest

- name: Setup conda (retry with tar.bz2)
if: steps.llvm-cache.outputs.cache-hit != 'true' && steps.conda1.outcome == 'failure'
uses: conda-incubator/setup-miniconda@fc2d68f6413eb2d87b895e92f8584b5b94a10167 # v3.3.0
with:
miniforge-version: latest
use-only-tar-bz2: true

- name: Create /opt/llvm (macOS)
if: steps.llvm-cache.outputs.cache-hit != 'true' && runner.os == 'macOS'
run: sudo mkdir -p /opt/llvm && sudo chown -R $(whoami) /opt/llvm

- name: Install LLVM (Unix)
if: steps.llvm-cache.outputs.cache-hit != 'true' && runner.os != 'Windows'
shell: bash -l {0}
run: |
conda create -q -p /opt/llvm -c conda-forge \
llvmdev=22.1.0 clangdev=22.1.0 compiler-rt=22.1.0 zlib zstd-static \
-y

- name: Install LLVM (Windows)
if: steps.llvm-cache.outputs.cache-hit != 'true' && runner.os == 'Windows'
shell: cmd /C call {0}
run: |
conda create -q -p C:\opt\llvm -c conda-forge llvmdev=22.1.0 zlib zstd-static -y

# ---- Build and test wheels ----
- name: Build and test wheels
uses: pypa/cibuildwheel@298ed2fb2c105540f5ed055e8a6ad78d82dd3a7e # v3.3.1
with:
package-dir: addons/tvm-ffi-orcjit
output-dir: wheelhouse
env:
CIBW_BUILD: ${{ matrix.build }}
CIBW_ARCHS_LINUX: ${{ matrix.arch }}
CIBW_ARCHS_MACOS: ${{ matrix.arch }}
CIBW_ARCHS_WINDOWS: ${{ matrix.arch }}
CIBW_BUILD_VERBOSITY: 1
CMAKE_BUILD_PARALLEL_LEVEL: ${{ steps.env_vars.outputs.cpu_count }}
CIBW_ENVIRONMENT: LLVM_PREFIX=/opt/llvm
CIBW_ENVIRONMENT_WINDOWS: LLVM_PREFIX="C:/opt/llvm"
CIBW_CONTAINER_ENGINE: "docker; create_args: --volume /opt/llvm:/opt/llvm"
CIBW_TEST_REQUIRES: pytest ninja
CIBW_TEST_COMMAND: >-
pip install --force-reinstall {project} &&
python {project}/addons/tvm-ffi-orcjit/tests/run_all_tests.py

- uses: actions/upload-artifact@v4
with:
name: cibw-wheels-${{ matrix.os }}-${{ matrix.arch }}-${{ strategy.job-index }}
path: wheelhouse/*.whl
178 changes: 178 additions & 0 deletions addons/tvm-ffi-orcjit/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
cmake_minimum_required(VERSION 3.20)
project(
tvm_ffi_orcjit
VERSION 0.1.0
LANGUAGES C CXX
)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)

# ---- LLVM_PREFIX handling ----
# scikit-build-core overrides CMAKE_PREFIX_PATH via its init cache, so we read LLVM_PREFIX from the
# environment instead.
if (DEFINED ENV{LLVM_PREFIX})
list(APPEND CMAKE_PREFIX_PATH "$ENV{LLVM_PREFIX}")
if (EXISTS "$ENV{LLVM_PREFIX}/Library")
list(APPEND CMAKE_PREFIX_PATH "$ENV{LLVM_PREFIX}/Library")
endif ()
endif ()

# ---- Find packages ----
find_package(LLVM REQUIRED CONFIG)
message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION} in ${LLVM_DIR}")

find_package(
Python
COMPONENTS Interpreter
REQUIRED
)
execute_process(
COMMAND "${Python_EXECUTABLE}" -m tvm_ffi.config --cmakedir
OUTPUT_STRIP_TRAILING_WHITESPACE
OUTPUT_VARIABLE tvm_ffi_ROOT
)
find_package(tvm_ffi CONFIG REQUIRED)

# ---- Build shared library ----
add_library(tvm_ffi_orcjit SHARED src/ffi/orcjit_session.cc src/ffi/orcjit_dylib.cc)
set_target_properties(
tvm_ffi_orcjit PROPERTIES CXX_VISIBILITY_PRESET hidden VISIBILITY_INLINES_HIDDEN ON
)
target_include_directories(
tvm_ffi_orcjit PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src ${LLVM_INCLUDE_DIRS}
)
separate_arguments(LLVM_DEFINITIONS_LIST NATIVE_COMMAND ${LLVM_DEFINITIONS})
target_compile_definitions(tvm_ffi_orcjit PRIVATE ${LLVM_DEFINITIONS_LIST})

# ---- Static LLVM linking via llvm-config ----
set(_llvm_config_hints ${LLVM_TOOLS_BINARY_DIR} "${LLVM_DIR}/../../../bin")
if (DEFINED ENV{LLVM_PREFIX})
list(APPEND _llvm_config_hints "$ENV{LLVM_PREFIX}/bin" "$ENV{LLVM_PREFIX}/Library/bin")
endif ()
find_program(
LLVM_CONFIG_EXE
NAMES llvm-config-${LLVM_VERSION_MAJOR} llvm-config
HINTS ${_llvm_config_hints}
)
if (NOT LLVM_CONFIG_EXE)
message(FATAL_ERROR "llvm-config not found")
endif ()

execute_process(
COMMAND ${LLVM_CONFIG_EXE} --link-static --libs Core OrcJIT Support native
OUTPUT_STRIP_TRAILING_WHITESPACE
OUTPUT_VARIABLE _llvm_libs
)
execute_process(
COMMAND ${LLVM_CONFIG_EXE} --link-static --ldflags
OUTPUT_STRIP_TRAILING_WHITESPACE
OUTPUT_VARIABLE _llvm_ldflags
)
separate_arguments(_llvm_libs_list NATIVE_COMMAND "${_llvm_libs}")
separate_arguments(_llvm_ldflags_list NATIVE_COMMAND "${_llvm_ldflags}")

# ---- zlib (static, from LLVM prefix) ----
cmake_path(GET LLVM_DIR PARENT_PATH _llvm_prefix)
cmake_path(GET _llvm_prefix PARENT_PATH _llvm_prefix)
cmake_path(GET _llvm_prefix PARENT_PATH _llvm_prefix)
set(_lib_hints "${_llvm_prefix}/lib" "${_llvm_prefix}/lib64" "${_llvm_prefix}/Library/lib")

if (WIN32)
find_library(
ZLIB_STATIC
NAMES zlibstatic z
HINTS ${_lib_hints} REQUIRED
)
else ()
find_library(
ZLIB_STATIC
NAMES libz.a
HINTS ${_lib_hints} REQUIRED
)
endif ()

# ---- zstd (static, from LLVM prefix) ----
if (WIN32)
find_library(
ZSTD_STATIC
NAMES zstd_static zstd
HINTS ${_lib_hints} REQUIRED
)
else ()
find_library(
ZSTD_STATIC
NAMES libzstd.a
HINTS ${_lib_hints} REQUIRED
)
endif ()

# ---- Link everything ----
target_link_libraries(
tvm_ffi_orcjit PRIVATE tvm_ffi::header tvm_ffi::shared ${_llvm_ldflags_list} ${_llvm_libs_list}
${ZLIB_STATIC} ${ZSTD_STATIC}
)

# LLVM system libs (ntdll/psapi on Windows, rt/dl on Linux), minus zlib/zstd (linked above).
execute_process(
COMMAND ${LLVM_CONFIG_EXE} --system-libs
OUTPUT_STRIP_TRAILING_WHITESPACE
OUTPUT_VARIABLE _sys_libs
RESULT_VARIABLE _sys_libs_rc
)
if (_sys_libs_rc EQUAL 0 AND _sys_libs)
separate_arguments(_sys_libs_list NATIVE_COMMAND "${_sys_libs}")
list(FILTER _sys_libs_list EXCLUDE REGEX "(zstd|^-lz$|^z\\.lib$|xml2)")
target_link_libraries(tvm_ffi_orcjit PRIVATE ${_sys_libs_list})
endif ()

# ---- Platform-specific fixups ----
if (APPLE)
add_custom_command(
TARGET tvm_ffi_orcjit
POST_BUILD
COMMAND install_name_tool -change @rpath/libc++.1.dylib /usr/lib/libc++.1.dylib
$<TARGET_FILE:tvm_ffi_orcjit>
COMMENT "Fixing libc++ rpath to use system library"
)
elseif (UNIX AND NOT WIN32)
target_link_options(tvm_ffi_orcjit PRIVATE -static-libstdc++ -static-libgcc)
endif ()

# ---- Find and bundle liborc_rt ----
set(ORC_RT_PATH "")
if (DEFINED ENV{ORC_RT_PATH})
set(ORC_RT_PATH "$ENV{ORC_RT_PATH}")
else ()
if (WIN32)
file(GLOB_RECURSE _orc_rt_candidates "${_llvm_prefix}/lib/clang/*/lib/orc_rt*.lib")
if (NOT _orc_rt_candidates)
file(GLOB_RECURSE _orc_rt_candidates "${_llvm_prefix}/Library/lib/clang/*/lib/orc_rt*.lib")
endif ()
else ()
file(GLOB_RECURSE _orc_rt_candidates "${_llvm_prefix}/lib/clang/*/lib/liborc_rt*.a")
endif ()
if (_orc_rt_candidates)
list(GET _orc_rt_candidates 0 ORC_RT_PATH)
endif ()
endif ()

if (NOT ORC_RT_PATH)
message(WARNING "Could not find liborc_rt. ORC runtime features will be disabled.")
endif ()

if (ORC_RT_PATH)
message(STATUS "Found liborc_rt: ${ORC_RT_PATH}")
file(COPY "${ORC_RT_PATH}" DESTINATION "${CMAKE_BINARY_DIR}")
install(FILES "${ORC_RT_PATH}" DESTINATION lib)
endif ()

# ---- Install ----
install(
TARGETS tvm_ffi_orcjit
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib
RUNTIME DESTINATION lib
)
Loading
Loading