cyx-6 commented on code in PR #254: URL: https://github.com/apache/tvm-ffi/pull/254#discussion_r2995329414
########## addons/tvm-ffi-orcjit/tests/build_test_objects.py: ########## @@ -0,0 +1,319 @@ +#!/usr/bin/env python3 +# 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. +"""Build test objects and quick-start example via direct compiler invocation. + +Detects platform and available compilers, then builds all applicable variants: + Linux: LLVM Clang (default) + GCC + macOS: LLVM Clang (default) + Apple Clang + Windows: LLVM Clang (default) + MSVC + clang-cl + +Objects are cached: files are only recompiled when the source is newer than the +output. Re-running this script is near-instant when nothing changed. + +Usage: + python build_test_objects.py [--llvm-prefix /opt/llvm] +""" + +from __future__ import annotations + +import argparse +import os +import platform +import shutil +import subprocess +import sys +from pathlib import Path + +TESTS_DIR = Path(__file__).resolve().parent +ADDON_DIR = TESTS_DIR.parent +QUICKSTART_DIR = ADDON_DIR / "examples" / "quick-start" +SOURCES_C = TESTS_DIR / "sources" / "c" +SOURCES_CC = TESTS_DIR / "sources" / "cc" + + +# --------------------------------------------------------------------------- +# Helpers +# --------------------------------------------------------------------------- + + +def _get_tvm_ffi_includedir() -> str: + """Return the tvm-ffi C/C++ include directory.""" + result = subprocess.run( + [sys.executable, "-m", "tvm_ffi.config", "--includedir"], + capture_output=True, + text=True, + check=True, + ) + return result.stdout.strip() + + +def _needs_build(source: Path, output: Path) -> bool: + """Check whether *output* is missing or older than *source*.""" + if not output.exists(): + return True + return source.stat().st_mtime > output.stat().st_mtime + + +def _compile( + compiler: str, source: Path, output: Path, flags: list[str], include_dirs: list[str] +) -> None: + """Compile *source* → *output*. Skips if cached.""" + if not _needs_build(source, output): + return + output.parent.mkdir(parents=True, exist_ok=True) + cmd: list[str] = [compiler, *flags] + for d in include_dirs: + if compiler in ("cl", "cl.exe", "clang-cl", "clang-cl.exe"): + cmd.append(f"/I{d}") + else: + cmd += ["-I", d] + if compiler in ("cl", "cl.exe", "clang-cl", "clang-cl.exe"): + cmd += ["/c", f"/Fo{output}", str(source)] + else: + cmd += ["-c", "-o", str(output), str(source)] + print(f" {' '.join(cmd)}", flush=True) + subprocess.check_call(cmd) + + +# --------------------------------------------------------------------------- +# Variant builder +# --------------------------------------------------------------------------- + + +def _build_variant( + name: str, + *, + c_compiler: str, + cxx_compiler: str | None, + c_flags: list[str], + cxx_flags: list[str], + include_dirs: list[str], + c_outdir: Path, + cc_outdir: Path | None, +) -> None: + """Build all test objects for one compiler variant.""" + print(f"\n--- {name} ---", flush=True) + for src in sorted(SOURCES_C.glob("*.c")): + _compile(c_compiler, src, c_outdir / f"{src.stem}.o", c_flags, include_dirs) + if cxx_compiler and cc_outdir: + for src in sorted(SOURCES_CC.glob("*.cc")): + _compile(cxx_compiler, src, cc_outdir / f"{src.stem}.o", cxx_flags, include_dirs) + + +# --------------------------------------------------------------------------- +# Platform dispatch +# --------------------------------------------------------------------------- + + +def _default_llvm_prefix() -> str: + if platform.system() == "Windows": + return "C:/opt/llvm" + return "/opt/llvm" + + +def _find_llvm_bin(prefix: str) -> Path: + """Return the LLVM bin directory under *prefix*.""" + p = Path(prefix) + # conda-forge on Windows: Library/bin; elsewhere: bin + win_lib = p / "Library" / "bin" + if win_lib.exists(): + return win_lib + return p / "bin" + + +def _base_flags(system: str, machine: str) -> tuple[list[str], list[str]]: + """Return (c_flags, cxx_flags) base lists for *system*.""" + c: list[str] = ["-O2"] + cxx: list[str] = ["-std=c++17", "-O2"] + if system != "Windows": + c.append("-fPIC") + cxx.append("-fPIC") + if machine in ("aarch64", "arm64"): + c.append("-mno-outline-atomics") + cxx.append("-mno-outline-atomics") + return c, cxx + + +def _build_all(llvm_prefix: str) -> None: Review Comment: updated ########## addons/tvm-ffi-orcjit/src/ffi/orcjit_utils.h: ########## @@ -0,0 +1,70 @@ +/* + * 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. + */ + +/*! + * \file orcjit_utils.h + * \brief LLVM ORC JIT utils + */ +#ifndef TVM_FFI_ORCJIT_ORCJIT_UTILS_H_ +#define TVM_FFI_ORCJIT_ORCJIT_UTILS_H_ + +#include <llvm/ExecutionEngine/Orc/LLJIT.h> +#include <llvm/Support/Error.h> +#include <tvm/ffi/string.h> + +#include <string> + +namespace tvm { +namespace ffi { +namespace orcjit { + +inline void call_llvm(llvm::Error err, const std::string& context_msg = "") { Review Comment: fixed -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
