Skip to content

[FIX][RUST] use $crate:: in tvm_ffi_dll_export_typed_func! and ensure!#590

Merged
tqchen merged 1 commit into
apache:mainfrom
lucifer1004:rust-macro-ergonomics
May 15, 2026
Merged

[FIX][RUST] use $crate:: in tvm_ffi_dll_export_typed_func! and ensure!#590
tqchen merged 1 commit into
apache:mainfrom
lucifer1004:rust-macro-ergonomics

Conversation

@lucifer1004
Copy link
Copy Markdown
Contributor

@lucifer1004 lucifer1004 commented May 15, 2026

Summary

Three small bugs in the Rust ergonomics that prevented the macros from
being usable from downstream cdylibs:

  1. ensure! macro expanded to crate::bail!, which resolves to
    the caller crate at expansion site rather than tvm_ffi. Switched
    to $crate::bail! so the path resolves correctly in any crate.

  2. tvm_ffi_dll_export_typed_func! referenced
    tvm_ffi_sys::TVMFFIAny without a $crate:: prefix, forcing every
    downstream crate to add tvm-ffi-sys to its own [dependencies].
    Switched to $crate::tvm_ffi_sys::TVMFFIAny; downstream crates now
    only need to depend on tvm-ffi.

  3. Missing #[no_mangle]: the generated
    pub unsafe extern "C" fn __tvm_ffi_<name> had no #[no_mangle],
    so the linker stripped the symbol from cdylibs and
    Module::GetFunction could not find it. Added
    #[unsafe(no_mangle)] (supported in 2021 + 2024 editions on
    rustc ≥ 1.82).

Reproduction (before)

Building a downstream cdylib that uses tvm_ffi_dll_export_typed_func!:

```text
error[E0433]: cannot find module or crate `tvm_ffi_sys` in this scope
= note: this error originates in the macro `tvm_ffi_dll_export_typed_func`
```

After adding `tvm-ffi-sys` as a direct dep:

```text
error[E0433]: cannot find `bail` in the crate root
= note: this error originates in the macro `tvm_ffi::ensure`
```

After working around `ensure!`, the cdylib builds — but
`nm -D libfoo.so | grep tvm_ffi` returns nothing because
`#[no_mangle]` is missing.

Test plan

  • Built a downstream cdylib that only depends on `tvm-ffi` (no
    `tvm-ffi-sys` direct dep).
  • Loaded via `tvm_ffi.load_module(...)` from Python.
  • Called three typed functions: scalar (`sum_i32`), 1-D tensor
    (`add_one_inplace`), 2-tensor (`dot_f32`). All round-trip.
  • `nm -D libfoo.so | grep __tvm_ffi` shows the exported symbols.

Part 1 of 2 — companion PR is #591 (Module::LoadFromBytes).
The second adds `Module::LoadFromBytes`.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request improves macro hygiene in rust/tvm-ffi by using $crate for internal references and adds the no_mangle attribute to exported FFI functions to ensure they are preserved in the output binary. A review comment points out that #[unsafe(no_mangle)] is a recent Rust feature (1.82.0+) and suggests using the standard #[no_mangle] to avoid an unnecessary increase in the Minimum Supported Rust Version (MSRV).

Comment thread rust/tvm-ffi/src/macros.rs Outdated
// than a bare `tvm_ffi_sys::…`) lets downstream crates use the
// macro without having to add `tvm-ffi-sys` to their own
// `[dependencies]`.
#[unsafe(no_mangle)]
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The #[unsafe(no_mangle)] attribute is a relatively new feature introduced in Rust 1.82.0. Using it will cause compilation errors for any users or downstream crates using an older version of the Rust compiler (e.g., 1.81 or earlier). Unless this project has a strict requirement to bump its Minimum Supported Rust Version (MSRV) to 1.82, it is safer to use the standard #[no_mangle] attribute, which is compatible with all Rust versions and remains perfectly valid.

Suggested change
#[unsafe(no_mangle)]
#[no_mangle]

Three small bugs in the Rust ergonomics that prevented the macros from
being usable from downstream cdylibs:

1. `ensure!` expanded to `crate::bail!`, which resolves to the *caller*
   crate at expansion site rather than `tvm_ffi`. Switched to
   `$crate::bail!` so the path resolves correctly in any crate.

2. `tvm_ffi_dll_export_typed_func!` referenced `tvm_ffi_sys::TVMFFIAny`
   without a `$crate::` prefix, forcing every downstream crate to add
   `tvm-ffi-sys` to its own `[dependencies]`. Switched to
   `$crate::tvm_ffi_sys::TVMFFIAny`; downstream now only needs `tvm-ffi`.

3. The generated `pub unsafe extern "C" fn __tvm_ffi_<name>` had no
   `#[no_mangle]`, so the linker stripped the symbol from cdylibs and
   `Module::GetFunction` could not find it. Added `#[unsafe(no_mangle)]`
   (supported in 2021 + 2024 editions on rustc >= 1.82).

Verified by building a downstream cdylib that only depends on `tvm-ffi`
(no `tvm-ffi-sys` direct dep), loading it via `tvm_ffi.load_module(...)`,
and calling exported scalar + Tensor functions from Python.
@lucifer1004 lucifer1004 force-pushed the rust-macro-ergonomics branch from 2395951 to 7232d83 Compare May 15, 2026 03:10
@lucifer1004
Copy link
Copy Markdown
Contributor Author

Thanks @gemini-code-assist — good catch. Switched to plain #[no_mangle] in 7232d83 so the macro stays compatible with rustc < 1.82. Edition-2024 callers will see a deprecation warning, which is harmless; if/when this project bumps MSRV to 1.82+, the attribute can be migrated to #[unsafe(no_mangle)] repo-wide in one go.

@gemini-code-assist
Copy link
Copy Markdown
Contributor

Thanks for the update, @lucifer1004. That approach makes sense for maintaining compatibility with older toolchains while preparing for future MSRV updates. The changes look correct and address the issues described in the PR summary.

@tqchen tqchen merged commit 35c99d0 into apache:main May 15, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants