Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
cdb56e2
Add Rust stubgen and integration tests
Blealtan Feb 7, 2026
9c53064
Refine stubgen tests and build setup
Blealtan Feb 7, 2026
6513145
refactor stubgen array usage
Blealtan Feb 7, 2026
b31a4ea
suppress non-snake-case warnings
Blealtan Feb 7, 2026
98028de
Improve stubgen typing and Map support
Blealtan Feb 8, 2026
891de62
Format Rust sources
Blealtan Feb 8, 2026
91af2fd
Use toml serializer for Cargo.toml
Blealtan Feb 8, 2026
72304d7
Add AnyValue for typed Any support
Blealtan Feb 8, 2026
6d17273
Avoid AnyValue TryFrom overlap
Blealtan Feb 8, 2026
c40f799
Extend typed macro arity
Blealtan Feb 8, 2026
be5076f
Improve stubgen wrappers and output
Blealtan Feb 8, 2026
addd67d
Add stubgen fields; first commit to build TileLang IR
Blealtan Feb 9, 2026
4a7b28e
Refine stubgen layout and warnings
Blealtan Feb 9, 2026
0724c46
fix(stubgen): update test to match _tvm_ffi_stubgen_detail layout
Blealtan Feb 13, 2026
98251f5
feat(rust): implement repr(C) zero-cost stubgen with safe Deref-based…
Blealtan Feb 14, 2026
c21e635
stubgen 第二轮修复:子类型转换、check_repr_c、fallback 与导出清理
Blealtan Feb 14, 2026
228ec25
fix(stubgen): use type_ancestors[type_depth-1] for direct parent
Blealtan Feb 14, 2026
6c49df7
docs(stubgen): split user guide and harden runnable examples
Blealtan Feb 14, 2026
7a7b2d1
feat(stubgen): resolve object methods via type metadata
Blealtan Feb 14, 2026
c445259
chore(stubgen): remove dead code and unused repr-c fields
Blealtan Feb 14, 2026
8764dcb
[stubgen] support multi-prefix generation for single-crate multi-name…
Blealtan Feb 26, 2026
4660f2c
feat(stubgen): format generated crates by default
Blealtan Mar 8, 2026
04283ce
feat(stubgen): gap-filling repr_c layout and relaxed parent resolution
Blealtan Mar 8, 2026
614a4cd
style(stubgen): cargo fmt repr_c.rs
Blealtan Mar 8, 2026
8e2068c
docs(stubgen): update README for gap-filling repr_c strategy
Blealtan Mar 8, 2026
d39c4fb
fix(stubgen): rename base-class field to __tvm_ffi_object_parent
Blealtan Mar 8, 2026
6eb9c9c
refactor(stubgen): generalize parent_range_fields to non_layout_fields
Blealtan Mar 8, 2026
0f7886d
docs(stubgen): document non_layout_fields and unified getter interface
Blealtan Mar 8, 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
1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ Table of Contents

packaging/python_packaging.rst
packaging/stubgen.rst
packaging/rust_stubgen.md
packaging/cpp_tooling.rst

.. toctree::
Expand Down
142 changes: 142 additions & 0 deletions docs/packaging/rust_stubgen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
<!--- 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. -->

# Rust Stubgen Guide

```{note}
The Rust stub generation flow is currently experimental and may evolve.
```

This guide covers practical usage of `tvm-ffi-stubgen`: generation command, output crate, and how to call generated APIs.

## Generate a Stub Crate

Run from `3rdparty/tvm/3rdparty/tvm-ffi/rust`:

```bash
cargo run -p tvm-ffi-stubgen -- <OUT_DIR> \
--init-prefix testing \
--init-crate tvm-ffi-testing \
--dlls /abs/path/to/libtvm_ffi_testing.so \
--overwrite
```

### Arguments

- `OUT_DIR`: positional output directory
- `--dlls`: one or more dynamic libraries for reflection metadata (`;`-separated)
- `--init-prefix`: registry prefix filter (repeatable; see multi-prefix below)
- `--init-crate`: generated crate name
- `--tvm-ffi-path`: optional local path override for `tvm-ffi`
- `--overwrite`: overwrite non-empty output directory
- `--no-format`: skip the post-generation `cargo fmt` pass

By default, `tvm-ffi-stubgen` runs `cargo fmt` on the generated crate after emitting
`Cargo.toml`, `build.rs`, and Rust sources. Use `--no-format` only when you need to inspect
the raw generated text before formatting or when debugging generator output itself.

### Multi-Prefix Mode

`--init-prefix` can be specified multiple times to generate a single crate covering
several namespaces:

```bash
cargo run -p tvm-ffi-stubgen -- <OUT_DIR> \
--dlls "libtilelang_module.so;libtvm.so" \
--init-prefix tl --init-prefix ir --init-prefix tir --init-prefix script \
--init-crate tilelang-ffi \
--overwrite
```

With a single prefix the prefix is stripped and items land at the crate root.
With multiple prefixes no stripping occurs; each prefix becomes a top-level module
(`crate::tl::*`, `crate::ir::*`, etc.).

## Generated Output Layout

The output is a standalone Rust crate:

- `Cargo.toml`
- `src/lib.rs`
- `src/_tvm_ffi_stubgen_detail/functions.rs`
- `src/_tvm_ffi_stubgen_detail/types.rs`

`src/lib.rs` re-exports generated wrappers and provides:

```rust
pub fn load_library(path: &str) -> tvm_ffi::Result<tvm_ffi::Module>
```

## Using Generated Crate

Using the generated stubs is straightforward—simply load the runtime library, call exported functions, and work with generated object wrappers and subtyping as needed. The full process is shown in the following example, covering typical usage:

```rust
use tvm_ffi_testing as stub;

fn main() -> tvm_ffi::Result<()> {
// Load FFI library (required before any calls)
stub::load_library("/abs/path/to/libtvm_ffi_testing.so")?;

// Call a generated function with typed arguments
let y = stub::add_one(1)?;
assert_eq!(y, 2);

// Call a function via packed interface for dynamic signature
let _out = stub::echo(&[tvm_ffi::Any::from(1_i64)])?;

// Object constructor/method wrappers are resolved from type metadata.
let pair_obj = stub::TestIntPair::new(3, 4)?;
let pair: stub::TestIntPair = pair_obj
.try_into()
.map_err(|_| tvm_ffi::Error::new(tvm_ffi::TYPE_ERROR, "downcast failed", ""))?;
let sum_any = pair.sum(&[])?;
let sum: i64 = sum_any.try_into()?;
assert_eq!(sum, 7);

// Cxx inheritance sample: construct derived, view as base, then convert back.
let derived_obj = stub::TestCxxClassDerived::new(11, 7, 3.5, 1.25)?;
let base: stub::TestCxxClassBase = derived_obj.clone().into();
let base_obj: tvm_ffi::object::ObjectRef = base.clone().into();
let derived_again: stub::TestCxxClassDerived = base_obj.into();
assert_eq!(base.v_i64()?, 11);
assert_eq!(base.v_i32()?, 7);
assert!((derived_again.v_f64()? - 3.5).abs() < 1e-9);
assert!((derived_again.v_f32()? - 1.25).abs() < 1e-6);

// Use object-returning wrappers and ObjectRef-based APIs
let obj = stub::make_unregistered_object()?;
let count = stub::object_use_count(obj.clone())?;
assert!(count >= 1);

// Fallback wrapper can be built from ObjectRef directly
let _wrapped: stub::TestUnregisteredObject = obj.into();

Ok(())
}
```

- Load the library once before using the APIs.
- Generated functions support typed signatures when possible and fall back to `Any` for dynamic calling.
- Generated object method wrappers (including constructor `new`) are resolved via type metadata rather than global function lookup.
- Generated object-returning wrappers integrate with `ObjectRef` APIs and wrapper conversions.


## Related Docs

- Rust language guide: `guides/rust_lang_guide.md`
- Rust stubgen design details (implementation-oriented): `rust/tvm-ffi-stubgen/README.md`
2 changes: 1 addition & 1 deletion rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
# under the License.

[workspace]
members = ["tvm-ffi", "tvm-ffi-sys", "tvm-ffi-macros"]
members = ["tvm-ffi", "tvm-ffi-sys", "tvm-ffi-macros", "tvm-ffi-stubgen"]

resolver = "2"
Loading
Loading