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

[QoL][Relax] Return well-formed IR from relax::Function::CreateEmpty #16861

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
2 changes: 2 additions & 0 deletions include/tvm/relax/expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -1045,6 +1045,8 @@ class ExternFuncNode : public BaseFuncNode {
class ExternFunc : public BaseFunc {
public:
TVM_DLL ExternFunc(String global_symbol, Span span = Span());
TVM_DLL ExternFunc(String global_symbol, StructInfo struct_info, Span span = Span());

TVM_DEFINE_OBJECT_REF_METHODS(ExternFunc, BaseFunc, ExternFuncNode);
TVM_DEFINE_OBJECT_REF_COW_METHOD(ExternFuncNode);
};
Expand Down
24 changes: 19 additions & 5 deletions src/relax/ir/expr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -559,10 +559,18 @@ Function Function::CreateEmpty(Array<Var> params, StructInfo ret_struct_info, bo

FuncStructInfo finfo(param_sinfo, ret_struct_info, is_pure);

// A dummy body, to ensure that the empty function is still well-formed.
Expr body = [&]() -> Expr {
Var output("output", ret_struct_info);
Call expr(ExternFunc("_dummy_function", FuncStructInfo({}, ret_struct_info)), {});

return SeqExpr({BindingBlock({VarBinding(output, expr)})}, output);
}();

// set the fields
ObjectPtr<FunctionNode> n = make_object<FunctionNode>();
n->params = std::move(params);
n->body = Expr();
n->body = std::move(body);
n->is_pure = is_pure;
n->checked_type_ = GetStaticType(finfo);
n->struct_info_ = std::move(finfo);
Expand Down Expand Up @@ -602,13 +610,19 @@ FuncStructInfo GetExternFuncStructInfo() {

TVM_REGISTER_NODE_TYPE(ExternFuncNode);

ExternFunc::ExternFunc(String global_symbol, Span span) {
ExternFunc::ExternFunc(String global_symbol, Span span)
: ExternFunc(global_symbol, GetExternFuncStructInfo(), span) {}

ExternFunc::ExternFunc(String global_symbol, StructInfo struct_info, Span span) {
CHECK(struct_info.as<FuncStructInfoNode>())
<< "ExternFunc must have FuncStructInfo, "
<< "but declaration of '" << global_symbol << "' received " << struct_info;

ObjectPtr<ExternFuncNode> n = make_object<ExternFuncNode>();
n->global_symbol = std::move(global_symbol);
n->span = span;
static auto sinfo = GetExternFuncStructInfo();
n->struct_info_ = sinfo;
n->checked_type_ = GetStaticType(sinfo);
n->struct_info_ = struct_info;
n->checked_type_ = GetStaticType(struct_info);
data_ = std::move(n);
}

Expand Down
Loading