Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TVMScript] Support SizeVar Roundtripping #14227

Merged
merged 1 commit into from
Mar 8, 2023
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
25 changes: 19 additions & 6 deletions include/tvm/script/ir_builder/tir/ir.h
Original file line number Diff line number Diff line change
Expand Up @@ -430,14 +430,27 @@ void Evaluate(PrimExpr value);
* \brief Create a TIR var that represents a pointer
* \param dtype The data type of the pointer.
* \param storage_scope The storage scope of the pointer.
* \param is_size_var Whether the pointer is a size var.
* \return The pointer.
*/
Var Handle(runtime::DataType dtype = runtime::DataType::Void(), String storage_scope = "global");

#define TVM_TIR_IR_BUILDER_DEF_DTYPE_CAST(FuncName, DType) \
inline PrimExpr FuncName(Optional<PrimExpr> expr = NullOpt) { \
DataType dtype = DType; \
return expr.defined() ? tvm::cast(dtype, expr.value()) : tvm::tir::Var("", dtype); \
inline Var Handle(runtime::DataType dtype = runtime::DataType::Void(), //
String storage_scope = "global", //
bool is_size_var = false) {
Type type_annotation{nullptr};
if (dtype.is_void() && storage_scope == "global") {
type_annotation = PrimType(runtime::DataType::Handle());
} else {
type_annotation = PointerType(PrimType(dtype), storage_scope);
}
return is_size_var ? tvm::tir::SizeVar("", type_annotation) : tvm::tir::Var("", type_annotation);
}

#define TVM_TIR_IR_BUILDER_DEF_DTYPE_CAST(FuncName, DType) \
inline PrimExpr FuncName(Optional<PrimExpr> expr = NullOpt, bool is_size_var = false) { \
DataType dtype = DType; \
return expr.defined() \
? tvm::cast(dtype, expr.value()) \
: (is_size_var ? tvm::tir::SizeVar("", dtype) : tvm::tir::Var("", dtype)); \
}

#define TVM_TIR_IR_BUILDER_DEF_DTYPE_CAST_SIZES(DType, FDType) \
Expand Down
7 changes: 7 additions & 0 deletions include/tvm/tir/var.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,13 @@ class SizeVar : public Var {
*/
TVM_DLL explicit SizeVar(String name_hint = "s", DataType t = DataType::Int(32),
Span span = Span());
/*!
* \brief Constructor which provides a more detailed type annotation.
* \param name_hint variable name.
* \param type_annotation The type annotation.
* \param span The location of this object in the source code.
*/
TVM_DLL explicit SizeVar(String name_hint, Type type_annotation, Span span = Span());
/*!
* \brief Get pointer to the internal value.
* \return the corresponding Variable.
Expand Down
31 changes: 21 additions & 10 deletions python/tvm/script/ir_builder/tir/ir.py
Original file line number Diff line number Diff line change
Expand Up @@ -1333,11 +1333,13 @@ def func(
Literal["inf", "-inf", "nan"],
int,
float,
] = None
] = None,
*,
is_size_var: bool = False,
) -> PrimExpr:
if isinstance(expr, str):
expr = float(expr)
return getattr(_ffi_api, name)(expr)
return getattr(_ffi_api, name)(expr, is_size_var)

return func

Expand Down Expand Up @@ -1420,23 +1422,26 @@ def func(
# pylint: enable=invalid-name


def boolean(expr: Optional[PrimExpr] = None) -> PrimExpr:
def boolean(expr: Optional[PrimExpr] = None, is_size_var: bool = False) -> PrimExpr:
"""Construct a new tir.Var with type boolean or cast expression to type boolean.

Parameters
----------
expr: PrimExpr
The expression to be cast.

is_size_var: bool
Whether or not to return a SizeVar instead of Var.

Returns
-------
res : PrimExpr
The new tir.Var with type boolean or casted expression with type boolean.
"""
return _ffi_api.Boolean(expr) # type: ignore[attr-defined] # pylint: disable=no-member
return _ffi_api.Boolean(expr, is_size_var) # type: ignore[attr-defined] # pylint: disable=no-member


def handle(dtype: str = "void", storage_scope: str = "global") -> Var:
def handle(dtype: str = "void", storage_scope: str = "global", *, is_size_var: bool = False) -> Var:
"""Create a TIR var that represents a pointer.

Parameters
Expand All @@ -1447,15 +1452,18 @@ def handle(dtype: str = "void", storage_scope: str = "global") -> Var:
storage_scope: str
The storage scope of the pointer.

is_size_var: bool
Whether or not to return a SizeVar instead of Var.

Returns
-------
res : PrimExpr
The new tir.Var with type handle or casted expression with type handle.
"""
return _ffi_api.Handle(dtype, storage_scope) # type: ignore[attr-defined] # pylint: disable=no-member
return _ffi_api.Handle(dtype, storage_scope, is_size_var) # type: ignore[attr-defined] # pylint: disable=no-member


def void(expr: Optional[PrimExpr] = None) -> PrimExpr:
def void(expr: Optional[PrimExpr] = None, *, is_size_var: bool = False) -> PrimExpr:
"""Construct a new tir.Var with type void or cast expression to type void.

Parameters
Expand All @@ -1468,7 +1476,7 @@ def void(expr: Optional[PrimExpr] = None) -> PrimExpr:
res : PrimExpr
The new tir.Var with type void or casted expression with type void.
"""
return _ffi_api.Void(expr) # type: ignore[attr-defined] # pylint: disable=no-member
return _ffi_api.Void(expr, is_size_var) # type: ignore[attr-defined] # pylint: disable=no-member


@deprecated("T.var", "T.{dtype}")
Expand All @@ -1491,7 +1499,7 @@ def var(dtype: str, name: str = "") -> Var:
return Var(name, dtype) # pylint: disable=no-member


def ptr(dtype: str, storage_scope: str = "global") -> Var:
def ptr(dtype: str, storage_scope: str = "global", is_size_var: bool = False) -> Var:
"""The pointer declaration function.

Parameters
Expand All @@ -1502,12 +1510,15 @@ def ptr(dtype: str, storage_scope: str = "global") -> Var:
storage_scope : str
The storage scope of the pointer.

is_size_var: bool
Whether or not to return a SizeVar instead of Var.

Returns
-------
res : Var
The pointer.
"""
return _ffi_api.Ptr(dtype, storage_scope) # type: ignore[attr-defined] # pylint: disable=no-member
return _ffi_api.Ptr(dtype, storage_scope, is_size_var) # type: ignore[attr-defined] # pylint: disable=no-member


@deprecated("T.buffer_var", "T.handle")
Expand Down
15 changes: 3 additions & 12 deletions src/script/ir_builder/tir/ir.cc
Original file line number Diff line number Diff line change
Expand Up @@ -560,18 +560,9 @@ DeclBufferFrame DeclBuffer(Array<PrimExpr> shape, DataType dtype, String buffer_

void Evaluate(PrimExpr value) { AddToParent(tvm::tir::Evaluate(value)); }

PrimExpr Ptr(runtime::DataType dtype, String storage_scope) {
return tvm::tir::Var("", tvm::PointerType(PrimType(dtype), storage_scope));
}

Var Handle(runtime::DataType dtype, String storage_scope) {
Type type_annotation{nullptr};
if (dtype.is_void() && storage_scope == "global") {
type_annotation = PrimType(runtime::DataType::Handle());
} else {
type_annotation = PointerType(PrimType(dtype), storage_scope);
}
return tvm::tir::Var("", type_annotation);
PrimExpr Ptr(runtime::DataType dtype, String storage_scope = "global", bool is_size_var = false) {
PointerType type_annotation(PrimType(dtype), storage_scope);
return is_size_var ? tvm::tir::SizeVar("", type_annotation) : tvm::tir::Var("", type_annotation);
}

using tvm::script::ir_builder::details::Namer;
Expand Down
15 changes: 12 additions & 3 deletions src/script/printer/tir/expr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ ExprDoc PrintVarCreation(const tir::Var& var, const ObjectPath& var_p, const IRD
Type type = var->type_annotation;
ObjectPath type_p = var_p->Attr("type_annotation");
ExprDoc rhs{nullptr};
Array<String> kwargs_keys;
Array<ExprDoc> kwargs_values;

if (var->IsInstance<tir::SizeVarNode>()) {
kwargs_keys.push_back("is_size_var");
kwargs_values.push_back(LiteralDoc::Boolean(true, NullOpt));
}

if (const auto* ptr_type = type.as<PointerTypeNode>()) {
const auto* prim_type = ptr_type->element_type.as<PrimTypeNode>();
ICHECK(prim_type);
Expand All @@ -36,16 +44,17 @@ ExprDoc PrintVarCreation(const tir::Var& var, const ObjectPath& var_p, const IRD
rhs = TIR(d, "handle");
rhs->source_paths.push_back(var_p->Attr("dtype"));
if (ptr_type->storage_scope == "") {
rhs = rhs->Call({element_type});
rhs = rhs->Call({element_type}, kwargs_keys, kwargs_values);
} else {
rhs = rhs->Call({element_type,
LiteralDoc::Str(ptr_type->storage_scope, //
type_p->Attr("storage_scope"))});
type_p->Attr("storage_scope"))},
kwargs_keys, kwargs_values);
}
} else {
rhs = TIR(d, DType2Str(var->dtype));
rhs->source_paths.push_back(var_p->Attr("dtype"));
rhs = rhs->Call({});
rhs = rhs->Call({}, kwargs_keys, kwargs_values);
}
rhs->source_paths.push_back(type_p);
return rhs;
Expand Down
9 changes: 9 additions & 0 deletions src/tir/ir/expr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,15 @@ SizeVar::SizeVar(String name_hint, DataType dtype, Span span) {
data_ = std::move(n);
}

SizeVar::SizeVar(String name_hint, Type type_annotation, Span span) {
auto n = make_object<SizeVarNode>();
n->name_hint = std::move(name_hint);
n->dtype = GetRuntimeDataType(type_annotation);
n->type_annotation = std::move(type_annotation);
n->span = std::move(span);
data_ = std::move(n);
}

TVM_REGISTER_GLOBAL("tir.SizeVar").set_body_typed([](String s, DataType t, Span span) {
return SizeVar(s, t, span);
});
Expand Down
2 changes: 1 addition & 1 deletion tests/python/unittest/test_tvmscript_printer_tir.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ def test_size_var():
_assert_print(
a,
"""
a = T.float32()
a = T.float32(is_size_var=True)
a""",
)

Expand Down