Skip to content

Commit d065ba5

Browse files
cyx-6claude
andcommitted
Fix lint: ASF header, ruff, clang-format, file type checks
- Add ASF license header to install_llvm.sh - Add .ps1 to allowed file types in check_file_type.py - Fix ruff check: move import to top-level (session.py), fix docstring mood (build_test_objects.py), use unpacking instead of list concat - Fix ruff format: one-arg-per-line in function calls, list items - Run clang-format on all C/C++ source files - Add per-file-ignores for addons/**/tests/* (D102, D103, ANN) - Fix unused variable (RUF059) and remove unused import (F401) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 46e8204 commit d065ba5

22 files changed

Lines changed: 174 additions & 118 deletions

addons/tvm-ffi-orcjit/examples/quick-start/add_c.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
/* add: add two integers */
3030
TVM_FFI_DLL_EXPORT int __tvm_ffi_add(void* self, const TVMFFIAny* args, int32_t num_args,
31-
TVMFFIAny* result) {
31+
TVMFFIAny* result) {
3232
result->type_index = kTVMFFIInt;
3333
result->zero_padding = 0;
3434
result->v_int64 = args[0].v_int64 + args[1].v_int64;
@@ -37,7 +37,7 @@ TVM_FFI_DLL_EXPORT int __tvm_ffi_add(void* self, const TVMFFIAny* args, int32_t
3737

3838
/* multiply: multiply two integers */
3939
TVM_FFI_DLL_EXPORT int __tvm_ffi_multiply(void* self, const TVMFFIAny* args, int32_t num_args,
40-
TVMFFIAny* result) {
40+
TVMFFIAny* result) {
4141
result->type_index = kTVMFFIInt;
4242
result->zero_padding = 0;
4343
result->v_int64 = args[0].v_int64 * args[1].v_int64;
@@ -51,7 +51,7 @@ static int64_t fib(int64_t n) {
5151
}
5252

5353
TVM_FFI_DLL_EXPORT int __tvm_ffi_fibonacci(void* self, const TVMFFIAny* args, int32_t num_args,
54-
TVMFFIAny* result) {
54+
TVMFFIAny* result) {
5555
result->type_index = kTVMFFIInt;
5656
result->zero_padding = 0;
5757
result->v_int64 = fib(args[0].v_int64);

addons/tvm-ffi-orcjit/python/tvm_ffi_orcjit/session.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
from __future__ import annotations
2020

21+
import sys
22+
2123
from tvm_ffi import Object, register_object
2224

2325
from . import _ffi_api, _lib_dir
@@ -26,8 +28,6 @@
2628

2729
def _find_orc_rt_library() -> str | None:
2830
"""Find the bundled liborc_rt library in the same directory as the .so/.dll."""
29-
import sys
30-
3131
# Windows: skip ORC runtime entirely. LLVM's COFFPlatform (loaded via
3232
# ExecutorNativePlatform with liborc_rt) depends on MSVC C++ runtime symbols
3333
# that are not available in the JIT environment. On Windows, ORC JIT uses a

addons/tvm-ffi-orcjit/src/ffi/orcjit_dylib.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ class ORCJITDynamicLibraryObj : public ModuleObj {
117117
*/
118118
class ORCJITDynamicLibrary : public Module {
119119
public:
120-
explicit ORCJITDynamicLibrary(const ObjectPtr<ORCJITDynamicLibraryObj>& ptr) : Module(ptr){};
120+
explicit ORCJITDynamicLibrary(const ObjectPtr<ORCJITDynamicLibraryObj>& ptr) : Module(ptr) {};
121121
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(ORCJITDynamicLibrary, Module,
122122
ORCJITDynamicLibraryObj);
123123
};

addons/tvm-ffi-orcjit/src/ffi/orcjit_session.cc

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@
5151
#ifndef WIN32_LEAN_AND_MEAN
5252
#define WIN32_LEAN_AND_MEAN
5353
#endif
54-
#include <windows.h>
5554
#include <psapi.h>
55+
#include <windows.h>
5656
#endif
5757

5858
#include "orcjit_dylib.h"
@@ -92,8 +92,7 @@ class InitFiniPlugin : public llvm::orc::ObjectLinkingLayer::Plugin {
9292
// COFF: .CRT$XC* (ctors), .CRT$XT* (dtors)
9393
if (section_name.starts_with(".init_array") || section_name.starts_with(".fini_array") ||
9494
section_name.starts_with(".ctors") || section_name.starts_with(".dtors") ||
95-
section_name == "__DATA,__mod_init_func" ||
96-
section_name == "__DATA,__mod_term_func" ||
95+
section_name == "__DATA,__mod_init_func" || section_name == "__DATA,__mod_term_func" ||
9796
section_name.starts_with(".CRT$XC") || section_name.starts_with(".CRT$XT")) {
9897
for (auto* Block : Section.blocks()) {
9998
bool has_live_sym = false;
@@ -323,7 +322,10 @@ class DLLImportDefinitionGenerator : public llvm::orc::DefinitionGenerator {
323322
// Try specific runtime DLLs first, then tvm_ffi.dll (loaded by Python),
324323
// then all process modules, then LLVM's search.
325324
static const char* kRuntimeDLLs[] = {
326-
"vcruntime140.dll", "vcruntime140_1.dll", "ucrtbase.dll", "msvcp140.dll",
325+
"vcruntime140.dll",
326+
"vcruntime140_1.dll",
327+
"ucrtbase.dll",
328+
"msvcp140.dll",
327329
};
328330
for (const char* dll : kRuntimeDLLs) {
329331
if (HMODULE hMod = LoadLibraryA(dll)) {
@@ -360,8 +362,7 @@ class DLLImportDefinitionGenerator : public llvm::orc::DefinitionGenerator {
360362
: ES_(ES), L_(L) {}
361363

362364
llvm::Error tryToGenerate(llvm::orc::LookupState& LS, llvm::orc::LookupKind K,
363-
llvm::orc::JITDylib& JD,
364-
llvm::orc::JITDylibLookupFlags JDLookupFlags,
365+
llvm::orc::JITDylib& JD, llvm::orc::JITDylibLookupFlags JDLookupFlags,
365366
const llvm::orc::SymbolLookupSet& LookupSet) override {
366367
// Step 1: Collect unique base names (strip __imp_ prefix) and resolve addresses.
367368
llvm::DenseMap<llvm::orc::SymbolStringPtr, llvm::orc::ExecutorAddr> Resolved;
@@ -383,16 +384,15 @@ class DLLImportDefinitionGenerator : public llvm::orc::DefinitionGenerator {
383384
auto G = std::make_unique<llvm::jitlink::LinkGraph>(
384385
"<DLL_IMPORT_STUBS>", ES_.getSymbolStringPool(), ES_.getTargetTriple(),
385386
llvm::SubtargetFeatures(), llvm::jitlink::getGenericEdgeKindName);
386-
auto Prot = static_cast<llvm::orc::MemProt>(
387-
static_cast<unsigned>(llvm::orc::MemProt::Read) |
388-
static_cast<unsigned>(llvm::orc::MemProt::Exec));
387+
auto Prot = static_cast<llvm::orc::MemProt>(static_cast<unsigned>(llvm::orc::MemProt::Read) |
388+
static_cast<unsigned>(llvm::orc::MemProt::Exec));
389389
auto& Sec = G->createSection("__dll_stubs", Prot);
390390

391391
for (auto& [InternedName, Addr] : Resolved) {
392392
// Absolute symbol at the real address (local to this graph)
393-
auto& Target = G->addAbsoluteSymbol(
394-
G->intern(("__real_" + *InternedName).str()), Addr, G->getPointerSize(),
395-
llvm::jitlink::Linkage::Strong, llvm::jitlink::Scope::Local, false);
393+
auto& Target = G->addAbsoluteSymbol(G->intern(("__real_" + *InternedName).str()), Addr,
394+
G->getPointerSize(), llvm::jitlink::Linkage::Strong,
395+
llvm::jitlink::Scope::Local, false);
396396
// __imp_XXX pointer (GOT-like entry)
397397
auto& Ptr = llvm::jitlink::x86_64::createAnonymousPointer(*G, Sec, &Target);
398398
Ptr.setName(G->intern(("__imp_" + *InternedName).str()));
@@ -454,8 +454,7 @@ ORCJITExecutionSessionObj::ORCJITExecutionSessionObj(const std::string& orc_rt_p
454454
auto builder = llvm::orc::LLJITBuilder();
455455
builder.setPlatformSetUp(llvm::orc::ExecutorNativePlatform(orc_rt_path));
456456
setup_builder(builder);
457-
jit_ = std::move(
458-
call_llvm(builder.create(), "Failed to create LLJIT with ORC runtime"));
457+
jit_ = std::move(call_llvm(builder.create(), "Failed to create LLJIT with ORC runtime"));
459458
} else {
460459
auto builder = llvm::orc::LLJITBuilder();
461460
setup_builder(builder);
@@ -510,8 +509,7 @@ ORCJITExecutionSessionObj::ORCJITExecutionSessionObj(const std::string& orc_rt_p
510509
if (machine != 0x8664) return std::move(Buf);
511510

512511
// String table follows the symbol table
513-
size_t strtab_start =
514-
ptr_to_symtab + static_cast<size_t>(num_symbols) * sym_entry_size;
512+
size_t strtab_start = ptr_to_symtab + static_cast<size_t>(num_symbols) * sym_entry_size;
515513

516514
// Resolve a section name (inline 8-byte or "/offset" string table ref)
517515
constexpr size_t kSecHdrSize = 40;
@@ -554,8 +552,7 @@ ORCJITExecutionSessionObj::ORCJITExecutionSessionObj(const std::string& orc_rt_p
554552
std::memset(&MutableBuf[off + 32], 0, 2); // NumberOfRelocations
555553
}
556554
return llvm::MemoryBuffer::getMemBufferCopy(
557-
llvm::StringRef(MutableBuf.data(), MutableBuf.size()),
558-
Buf->getBufferIdentifier());
555+
llvm::StringRef(MutableBuf.data(), MutableBuf.size()), Buf->getBufferIdentifier());
559556
});
560557
#endif
561558
#if defined(__linux__) || defined(_WIN32)
@@ -572,10 +569,9 @@ ORCJITExecutionSessionObj::ORCJITExecutionSessionObj(const std::string& orc_rt_p
572569
// comprehensive generator that searches all loaded DLLs (vcruntime140,
573570
// ucrtbase, tvm_ffi, etc.) and creates __imp_* pointer stubs.
574571
if (auto PSG = jit_->getProcessSymbolsJITDylib()) {
575-
auto& ObjLayer =
576-
static_cast<llvm::orc::ObjectLinkingLayer&>(jit_->getObjLinkingLayer());
577-
PSG->addGenerator(std::make_unique<DLLImportDefinitionGenerator>(
578-
jit_->getExecutionSession(), ObjLayer));
572+
auto& ObjLayer = static_cast<llvm::orc::ObjectLinkingLayer&>(jit_->getObjLinkingLayer());
573+
PSG->addGenerator(
574+
std::make_unique<DLLImportDefinitionGenerator>(jit_->getExecutionSession(), ObjLayer));
579575
}
580576
#endif
581577
}

addons/tvm-ffi-orcjit/src/ffi/orcjit_session.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ class ORCJITExecutionSessionObj : public Object {
102102

103103
std::unordered_map<llvm::orc::JITDylib*, std::vector<InitFiniEntry>> pending_initializers_;
104104
std::unordered_map<llvm::orc::JITDylib*, std::vector<InitFiniEntry>> pending_deinitializers_;
105-
106105
};
107106

108107
/*!

addons/tvm-ffi-orcjit/tests/build_test_objects.py

Lines changed: 61 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -63,19 +63,20 @@ def _get_tvm_ffi_includedir() -> str:
6363

6464

6565
def _needs_build(source: Path, output: Path) -> bool:
66-
"""True if *output* is missing or older than *source*."""
66+
"""Check whether *output* is missing or older than *source*."""
6767
if not output.exists():
6868
return True
6969
return source.stat().st_mtime > output.stat().st_mtime
7070

7171

72-
def _compile(compiler: str, source: Path, output: Path, flags: list[str],
73-
include_dirs: list[str]) -> None:
72+
def _compile(
73+
compiler: str, source: Path, output: Path, flags: list[str], include_dirs: list[str]
74+
) -> None:
7475
"""Compile *source* → *output*. Skips if cached."""
7576
if not _needs_build(source, output):
7677
return
7778
output.parent.mkdir(parents=True, exist_ok=True)
78-
cmd: list[str] = [compiler] + flags
79+
cmd: list[str] = [compiler, *flags]
7980
for d in include_dirs:
8081
if compiler in ("cl", "cl.exe", "clang-cl", "clang-cl.exe"):
8182
cmd.append(f"/I{d}")
@@ -165,24 +166,32 @@ def _build_all(llvm_prefix: str) -> None:
165166
clangxx = str(llvm_bin / "clang++")
166167
_build_variant(
167168
"LLVM Clang",
168-
c_compiler=clang, cxx_compiler=clangxx,
169-
c_flags=c_flags, cxx_flags=cxx_flags,
169+
c_compiler=clang,
170+
cxx_compiler=clangxx,
171+
c_flags=c_flags,
172+
cxx_flags=cxx_flags,
170173
include_dirs=include_dirs,
171-
c_outdir=TESTS_DIR / "c", cc_outdir=TESTS_DIR / "cc",
174+
c_outdir=TESTS_DIR / "c",
175+
cc_outdir=TESTS_DIR / "cc",
172176
)
173177
if system == "Linux" and shutil.which("gcc"):
174178
_build_variant(
175179
"GCC",
176-
c_compiler="gcc", cxx_compiler="g++",
177-
c_flags=c_flags, cxx_flags=cxx_flags,
180+
c_compiler="gcc",
181+
cxx_compiler="g++",
182+
c_flags=c_flags,
183+
cxx_flags=cxx_flags,
178184
include_dirs=include_dirs,
179-
c_outdir=TESTS_DIR / "c-gcc", cc_outdir=TESTS_DIR / "cc-gcc",
185+
c_outdir=TESTS_DIR / "c-gcc",
186+
cc_outdir=TESTS_DIR / "cc-gcc",
180187
)
181188
if system == "Darwin" and Path("/usr/bin/clang").exists():
182189
_build_variant(
183190
"Apple Clang",
184-
c_compiler="/usr/bin/clang", cxx_compiler="/usr/bin/clang++",
185-
c_flags=c_flags, cxx_flags=cxx_flags,
191+
c_compiler="/usr/bin/clang",
192+
cxx_compiler="/usr/bin/clang++",
193+
c_flags=c_flags,
194+
cxx_flags=cxx_flags,
186195
include_dirs=include_dirs,
187196
c_outdir=TESTS_DIR / "c-appleclang",
188197
cc_outdir=TESTS_DIR / "cc-appleclang",
@@ -199,28 +208,37 @@ def _build_all(llvm_prefix: str) -> None:
199208
if clang:
200209
_build_variant(
201210
"LLVM Clang",
202-
c_compiler=clang, cxx_compiler=None,
203-
c_flags=c_flags, cxx_flags=[],
211+
c_compiler=clang,
212+
cxx_compiler=None,
213+
c_flags=c_flags,
214+
cxx_flags=[],
204215
include_dirs=include_dirs,
205-
c_outdir=TESTS_DIR / "c", cc_outdir=None,
216+
c_outdir=TESTS_DIR / "c",
217+
cc_outdir=None,
206218
)
207219
# MSVC
208220
if shutil.which("cl"):
209221
_build_variant(
210222
"MSVC",
211-
c_compiler="cl", cxx_compiler=None,
212-
c_flags=["/O2", "/GS-"], cxx_flags=[],
223+
c_compiler="cl",
224+
cxx_compiler=None,
225+
c_flags=["/O2", "/GS-"],
226+
cxx_flags=[],
213227
include_dirs=include_dirs,
214-
c_outdir=TESTS_DIR / "c-msvc", cc_outdir=None,
228+
c_outdir=TESTS_DIR / "c-msvc",
229+
cc_outdir=None,
215230
)
216231
# clang-cl
217232
if shutil.which("clang-cl"):
218233
_build_variant(
219234
"clang-cl",
220-
c_compiler="clang-cl", cxx_compiler=None,
221-
c_flags=["/O2", "/GS-"], cxx_flags=[],
235+
c_compiler="clang-cl",
236+
cxx_compiler=None,
237+
c_flags=["/O2", "/GS-"],
238+
cxx_flags=[],
222239
include_dirs=include_dirs,
223-
c_outdir=TESTS_DIR / "c-clang-cl", cc_outdir=None,
240+
c_outdir=TESTS_DIR / "c-clang-cl",
241+
cc_outdir=None,
224242
)
225243

226244

@@ -237,7 +255,7 @@ def _build_quickstart(llvm_prefix: str) -> None:
237255
c_flags, cxx_flags = _base_flags(system, machine)
238256
llvm_bin = _find_llvm_bin(llvm_prefix)
239257

240-
print(f"\n{'='*60}\nBuilding quick-start example\n{'='*60}", flush=True)
258+
print(f"\n{'=' * 60}\nBuilding quick-start example\n{'=' * 60}", flush=True)
241259

242260
# C variant (all platforms)
243261
c_compiler: str | None = None
@@ -250,16 +268,22 @@ def _build_quickstart(llvm_prefix: str) -> None:
250268

251269
if c_compiler:
252270
_compile(
253-
c_compiler, QUICKSTART_DIR / "add_c.c",
254-
QUICKSTART_DIR / "add_c.o", c_flags, include_dirs,
271+
c_compiler,
272+
QUICKSTART_DIR / "add_c.c",
273+
QUICKSTART_DIR / "add_c.o",
274+
c_flags,
275+
include_dirs,
255276
)
256277

257278
# C++ variant (Linux/macOS only)
258279
if system != "Windows":
259280
clangxx = str(llvm_bin / "clang++")
260281
_compile(
261-
clangxx, QUICKSTART_DIR / "add.cc",
262-
QUICKSTART_DIR / "add.o", cxx_flags, include_dirs,
282+
clangxx,
283+
QUICKSTART_DIR / "add.cc",
284+
QUICKSTART_DIR / "add.o",
285+
cxx_flags,
286+
include_dirs,
263287
)
264288

265289

@@ -271,17 +295,22 @@ def _build_quickstart(llvm_prefix: str) -> None:
271295
def ensure_built(llvm_prefix: str | None = None) -> None:
272296
"""Build all test objects and quick-start example if needed."""
273297
prefix = llvm_prefix or os.environ.get("LLVM_PREFIX", _default_llvm_prefix())
274-
print(f"{'='*60}\nBuilding test objects\n{'='*60}", flush=True)
298+
print(f"{'=' * 60}\nBuilding test objects\n{'=' * 60}", flush=True)
275299
_build_all(prefix)
276300
_build_quickstart(prefix)
277-
print(f"\n{'='*60}\nAll builds completed\n{'='*60}", flush=True)
301+
print(f"\n{'=' * 60}\nAll builds completed\n{'=' * 60}", flush=True)
278302

279303

280304
def main() -> None:
281-
parser = argparse.ArgumentParser(description=__doc__,
282-
formatter_class=argparse.RawDescriptionHelpFormatter)
283-
parser.add_argument("--llvm-prefix", default=None,
284-
help=f"LLVM install prefix (default: {_default_llvm_prefix()})")
305+
"""Parse arguments and build all test objects."""
306+
parser = argparse.ArgumentParser(
307+
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
308+
)
309+
parser.add_argument(
310+
"--llvm-prefix",
311+
default=None,
312+
help=f"LLVM install prefix (default: {_default_llvm_prefix()})",
313+
)
285314
args = parser.parse_args()
286315
ensure_built(args.llvm_prefix)
287316

addons/tvm-ffi-orcjit/tests/run_all_tests.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
from __future__ import annotations
2828

2929
import argparse
30-
import os
3130
import platform
3231
import subprocess
3332
import sys
@@ -43,22 +42,27 @@ def _run(cmd: list[str], **kwargs: object) -> None:
4342

4443

4544
def main() -> int:
46-
parser = argparse.ArgumentParser(description=__doc__,
47-
formatter_class=argparse.RawDescriptionHelpFormatter)
48-
parser.add_argument("--llvm-prefix", default=None,
49-
help="LLVM install prefix (forwarded to build_test_objects)")
45+
parser = argparse.ArgumentParser(
46+
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
47+
)
48+
parser.add_argument(
49+
"--llvm-prefix",
50+
default=None,
51+
help="LLVM install prefix (forwarded to build_test_objects)",
52+
)
5053
args = parser.parse_args()
5154

5255
# 1. Build test objects + quick-start example
53-
from build_test_objects import ensure_built
56+
from build_test_objects import ensure_built # noqa: PLC0415
57+
5458
ensure_built(args.llvm_prefix)
5559

5660
# 2. Run pytest
57-
print(f"\n{'='*60}\nRunning pytest\n{'='*60}\n", flush=True)
61+
print(f"\n{'=' * 60}\nRunning pytest\n{'=' * 60}\n", flush=True)
5862
_run([sys.executable, "-m", "pytest", str(TESTS_DIR), "-v"])
5963

6064
# 3. Run quick-start examples
61-
print(f"\n{'='*60}\nRunning quick-start examples\n{'='*60}\n", flush=True)
65+
print(f"\n{'=' * 60}\nRunning quick-start examples\n{'=' * 60}\n", flush=True)
6266
langs = ["c"]
6367
if platform.system() != "Windows":
6468
langs.insert(0, "cpp")
@@ -68,7 +72,7 @@ def main() -> int:
6872
cwd=str(QUICKSTART_DIR),
6973
)
7074

71-
print(f"\n{'='*60}\nAll tests passed\n{'='*60}")
75+
print(f"\n{'=' * 60}\nAll tests passed\n{'=' * 60}")
7276
return 0
7377

7478

0 commit comments

Comments
 (0)