Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 2 additions & 2 deletions python/tvm/tir/op.py
Original file line number Diff line number Diff line change
Expand Up @@ -1789,7 +1789,7 @@ def infinity(dtype: str, span: Optional[Span] = None) -> Any:
return _ffi_api.infinity(dtype, span) # type: ignore


def reinterpret(dtype, value) -> Any:
def reinterpret(dtype, value, span: Optional[Span] = None) -> Any:
"""infinity value of dtype

Parameters
Expand All @@ -1808,7 +1808,7 @@ def reinterpret(dtype, value) -> Any:
value : tvm.Expr
The reinterpret cast value of dtype.
"""
return call_intrin(dtype, "tir.reinterpret", value)
return _ffi_api.reinterpret(dtype, value, span) # type: ignore


def exp(x):
Expand Down
8 changes: 6 additions & 2 deletions src/tir/op/op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -409,8 +409,10 @@ PrimExpr cast(const DataType& t, PrimExpr value, Span span) {
// reinterpret
PrimExpr reinterpret(const DataType& t, PrimExpr value, Span span) {
if (value.dtype() == t) return value;
ICHECK(value.dtype().bits() * value.dtype().lanes() == t.bits() * t.lanes())
<< "Bitcast requires size match " << t << " vs " << value.dtype();
if (!t.is_scalable_vector() && !value.dtype().is_scalable_vector()) {
ICHECK(value.dtype().bits() * value.dtype().lanes() == t.bits() * t.lanes())
<< "Bitcast requires size match " << t << " vs " << value.dtype();
}
return tir::Call(t, tir::builtin::reinterpret(), {value}, span);
}

Expand Down Expand Up @@ -1083,6 +1085,8 @@ TVM_REGISTER_GLOBAL("tir.trunc").set_body_typed(tvm::trunc);

TVM_REGISTER_GLOBAL("tir._cast").set_body_typed(tvm::cast);

TVM_REGISTER_GLOBAL("tir.reinterpret").set_body_typed(tvm::reinterpret);

// operator overloading, smarter than make
#define REGISTER_MAKE_BINARY_OP(Node, Func) \
TVM_REGISTER_GLOBAL("tir." #Node).set_body_typed([](PrimExpr a, PrimExpr b, Span span) { \
Expand Down
2 changes: 1 addition & 1 deletion tests/python/codegen/test_target_codegen_cuda.py
Original file line number Diff line number Diff line change
Expand Up @@ -1120,7 +1120,7 @@ def test_invalid_reinterpret():
@T.prim_func
def func(A: T.Buffer((4,), "uint32"), B: T.Buffer((4,), "uint8")) -> None:
for tx in T.thread_binding(4, "threadIdx.x"):
B[tx] = T.reinterpret("uint8", A[tx])
B[tx] = T.call_intrin("uint8", "tir.reinterpret", A[tx])

with pytest.raises(tvm.error.TVMError):
tvm.build(func, target="cuda")
Expand Down
22 changes: 22 additions & 0 deletions tests/python/tvmscript/test_tvmscript_parser_tir.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,5 +449,27 @@ def func(a_handle: T.handle, b_handle: T.handle):
tvm.ir.assert_structural_equal(func.struct_info, expected)


def test_reinterpret_nop():
"""Test builtin reinterpret op"""

@T.prim_func
def func(A: T.Buffer((32,), "float32"), B: T.Buffer((32,), "float32")) -> None:
T.func_attr({"global_symbol": "main"})
for i in T.serial(0, 32):
with T.block():
vi = T.axis.remap("S", [i])
B[vi] = T.reinterpret("float32", A[vi])

@T.prim_func
def expected(A: T.Buffer((32,), "float32"), B: T.Buffer((32,), "float32")) -> None:
T.func_attr({"global_symbol": "main"})
for i in T.serial(0, 32):
with T.block():
vi = T.axis.remap("S", [i])
B[vi] = A[vi]

tvm.ir.assert_structural_equal(func, expected)


if __name__ == "__main__":
tvm.testing.main()