This is an automated email from the ASF dual-hosted git repository.
tlopex pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tvm-tirx.git
The following commit(s) were added to refs/heads/main by this push:
new c4fbd89b6 [DOCS] Clean up IR guide example code (#243)
c4fbd89b6 is described below
commit c4fbd89b6a986a52883d47358967305df3772a24
Author: Tianqi Chen <[email protected]>
AuthorDate: Thu Nov 12 17:03:46 2026 -0500
[DOCS] Clean up IR guide example code (#243)
Two cleanups in the IR guides:
- The `describe` example in the Rust IR guide wrapped its `&PrimExpr` in
`AnyView::from` before `match_any!`. The macro takes a reference
directly, and the `ref` arms borrow the matched node without a refcount
change, so the example now passes `expr` as is.
- Eight included example snippets ended with the blank lines the
formatter keeps after a function, which showed up as an empty last line
in the rendered code blocks. Those snippets are now wrapped in `# fmt:
off` / `# fmt: on`, outside the include markers, and end right before
their end marker.
---
examples/extension_guides/run_example.py | 4 ++--
examples/ir_guides/add_one_printed.py | 4 ++--
examples/ir_guides/functions_and_modules.py | 7 ++++---
examples/ir_guides/printing_and_parsing.py | 4 ++--
examples/ir_guides/rust/src/lib.rs | 2 +-
examples/ir_guides/symbolic_integer_analysis.py | 12 ++++++------
6 files changed, 17 insertions(+), 16 deletions(-)
diff --git a/examples/extension_guides/run_example.py
b/examples/extension_guides/run_example.py
index 4aa4a4648..e38248de3 100644
--- a/examples/extension_guides/run_example.py
+++ b/examples/extension_guides/run_example.py
@@ -23,6 +23,7 @@ import tvm_tirx
from tvm_tirx import runtime
from tvm_tirx.script import tirx as T
+# fmt: off
# begin kernel
N = 16
@@ -42,9 +43,8 @@ def complex_product(
product = my_ext.cmul(z, w)
R[i] = my_ext.real(product)
Im[i] = my_ext.imag(product)
-
-
# end kernel
+# fmt: on
# begin compile
compiled = tvm_tirx.compile(complex_product, target="llvm", pipeline="my_ext")
diff --git a/examples/ir_guides/add_one_printed.py
b/examples/ir_guides/add_one_printed.py
index 6fa8c1848..c6981c137 100644
--- a/examples/ir_guides/add_one_printed.py
+++ b/examples/ir_guides/add_one_printed.py
@@ -16,6 +16,7 @@
# under the License.
"""The printed form of add_one, as produced by add_one.script()."""
+# fmt: off
# [printed.begin]
from tvm_tirx.script import tirx as T
@@ -24,6 +25,5 @@ from tvm_tirx.script import tirx as T
def add_one[n](A: T.Tensor((n,), T.float32), B: T.Tensor((n,), T.float32)):
for i in range(n):
B[i] = A[i] + T.float32(1.0)
-
-
# [printed.end]
+# fmt: on
diff --git a/examples/ir_guides/functions_and_modules.py
b/examples/ir_guides/functions_and_modules.py
index 3f35573e3..fbee9ccdf 100644
--- a/examples/ir_guides/functions_and_modules.py
+++ b/examples/ir_guides/functions_and_modules.py
@@ -16,6 +16,7 @@
# under the License.
"""Functions, modules, and global variables in a two-function module."""
+# fmt: off
# [module.begin]
from tvm_tirx.script import tirx as T
@@ -33,9 +34,8 @@ class Module:
def add_two(A: T.Tensor((n,), T.float32), B: T.Tensor((n,), T.float32)):
for i in range(n):
B[i] = A[i] + 2
-
-
# [module.end]
+# fmt: on
# [ty.begin]
add_one = Module["add_one"]
@@ -58,14 +58,15 @@ assert "global_symbol" not in private_example().attrs
def attrs_example():
+ # fmt: off
# [func_attr.begin]
@T.function
def add_one(A: T.Tensor((n,), T.float32), B: T.Tensor((n,), T.float32)):
T.func_attr({"note": "example"})
for i in range(n):
B[i] = A[i] + 1
-
# [func_attr.end]
+ # fmt: on
return add_one
diff --git a/examples/ir_guides/printing_and_parsing.py
b/examples/ir_guides/printing_and_parsing.py
index 6f187098b..8125b972d 100644
--- a/examples/ir_guides/printing_and_parsing.py
+++ b/examples/ir_guides/printing_and_parsing.py
@@ -19,6 +19,7 @@
# Imports stay next to the snippets that use them in the docs.
# ruff: noqa: E402
+# fmt: off
# [function.begin]
from tvm_tirx import ir
from tvm_tirx.script import tirx as T
@@ -30,9 +31,8 @@ n = T.dynamic("n")
def add_one(A: T.Tensor((n,), T.float32), B: T.Tensor((n,), T.float32)):
for i in range(n):
B[i] = A[i] + 1
-
-
# [function.end]
+# fmt: on
# [print.begin]
print(add_one.script())
diff --git a/examples/ir_guides/rust/src/lib.rs
b/examples/ir_guides/rust/src/lib.rs
index 76a40c739..2f72595f7 100644
--- a/examples/ir_guides/rust/src/lib.rs
+++ b/examples/ir_guides/rust/src/lib.rs
@@ -92,7 +92,7 @@ fn casts() -> Result<Strings> {
// [describe.begin]
fn describe(expr: &PrimExpr) -> Result<String> {
let text = match_any! {
- AnyView::from(expr) {
+ expr {
Add(ref add) => format!("({} + {})", describe(&add.a)?,
describe(&add.b)?),
Mul(ref mul) => format!("({} * {})", describe(&mul.a)?,
describe(&mul.b)?),
Var(ref var) => var.name.as_str().to_owned(),
diff --git a/examples/ir_guides/symbolic_integer_analysis.py
b/examples/ir_guides/symbolic_integer_analysis.py
index cd970d1f1..dfe28f811 100644
--- a/examples/ir_guides/symbolic_integer_analysis.py
+++ b/examples/ir_guides/symbolic_integer_analysis.py
@@ -19,6 +19,7 @@
# Imports stay next to the snippets that use them in the docs.
# ruff: noqa: E402
+# fmt: off
# [add_one_2d.begin]
from tvm_tirx.script import tirx as T
@@ -29,11 +30,11 @@ n = T.dynamic("n")
def add_one_2d(A: T.Tensor((n, 4), T.float32), B: T.Tensor((n, 4), T.float32)):
for i in range(n * 4):
B[i // 4, i % 4] = A[i // 4, i % 4] + 1
-
-
# [add_one_2d.end]
+# fmt: on
+# fmt: off
# [add_one_1d.begin]
@T.function
def add_one_1d(A: T.Tensor((n, 4), T.float32), B: T.Tensor((n, 4), T.float32)):
@@ -41,9 +42,8 @@ def add_one_1d(A: T.Tensor((n, 4), T.float32), B:
T.Tensor((n, 4), T.float32)):
B_flat = T.decl_tensor(B.data, ty_args=[T.Tensor((n * 4,), T.float32)])
for i in range(n * 4):
B_flat[i // 4 * 4 + i % 4] = A_flat[i // 4 * 4 + i % 4] + 1
-
-
# [add_one_1d.end]
+# fmt: on
# [simplify.begin]
from tvm_tirx import ir, sym
@@ -97,15 +97,15 @@ assert not analyzer.can_prove(i % 4 < 3)
assert analyzer.can_prove(i // 4 < n)
+# fmt: off
# [add_one_guarded.begin]
@T.function
def add_one_guarded(A: T.Tensor((n, 4), T.float32), B: T.Tensor((n, 4),
T.float32)):
for i in range(n * 4):
if i // 4 < n:
B[i // 4, i % 4] = A[i // 4, i % 4] + 1
-
-
# [add_one_guarded.end]
+# fmt: on
# [walk.begin]
from tvm_tirx import tirx