-
Notifications
You must be signed in to change notification settings - Fork 3.8k
[TIR] Support Return in TIR #7084
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
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
033f56d
[TIR] Support build TIR function.
ZihengJiang 904fb01
[TIR] Support Return.
ZihengJiang 30a691b
[TIR] Update.
ZihengJiang 221a2f4
[TIR] Rename to ret.
ZihengJiang 424093b
[TIR] Update build.
ZihengJiang ecff372
[TIR] Update.
ZihengJiang 7194234
[TIR] ASF header.
ZihengJiang 86cb0f8
[TIR] Fix lint.
ZihengJiang fe49d69
[TIR] Fix lint.
ZihengJiang 2d4d178
[TIR] Fix lint.
ZihengJiang a8d42ac
[TIR] Update
ZihengJiang 7facb69
[TIR] Fix lint.
ZihengJiang f2578cd
[TIR] Handle control flow jump.
ZihengJiang b04c3a0
[TIR] Fix lint.
ZihengJiang 19be07d
[TIR] Fix lint.
ZihengJiang 5ab6d83
[TIR] Fix lint.
ZihengJiang 6525e9f
[TIR] Fix lint.
ZihengJiang 6c87c3d
[TIR] Error message.
ZihengJiang 9737b62
[TIR] Fix lint.
ZihengJiang 06ac4a2
[TIR] Fix lint.
ZihengJiang 4dd827b
[TIR] Fix lint.
ZihengJiang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -41,6 +41,67 @@ | |
| namespace tvm { | ||
| namespace tir { | ||
|
|
||
| class ReturnRewriter : public StmtMutator { | ||
| public: | ||
| explicit ReturnRewriter(Var ret_var, Var ret_tcode) : ret_var_(ret_var), ret_tcode_(ret_tcode) {} | ||
|
|
||
| Stmt VisitStmt_(const ForNode* node) override { | ||
| if (node->for_type == ForType::Parallel) in_parallel_ += 1; | ||
| Stmt ret = StmtMutator::VisitStmt_(node); | ||
| if (node->for_type == ForType::Parallel) in_parallel_ -= 1; | ||
| return ret; | ||
| } | ||
|
|
||
| Stmt VisitStmt_(const EvaluateNode* node) override { | ||
| Stmt ret = StmtMutator::VisitStmt_(node); | ||
| const EvaluateNode* eval = ret.as<EvaluateNode>(); | ||
| ICHECK(eval); | ||
| if (const CallNode* call = eval->value.as<CallNode>()) { | ||
| if (call->op.same_as(builtin::ret())) { | ||
| ICHECK_EQ(in_parallel_, 0) << "tir.ret cannot be used in parallel scope."; | ||
| ICHECK_EQ(call->args.size(), 1) << "tir.ret expect a single argument."; | ||
| ret = WriteToOut(call->args[0], ret_var_, ret_tcode_); | ||
| } | ||
| } | ||
| return ret; | ||
| } | ||
|
|
||
| private: | ||
| std::pair<int, PrimExpr> ConvertForFFI(PrimExpr val) { | ||
| // convert val's data type to FFI data type, return type code | ||
| DataType dtype = val.dtype(); | ||
| if (dtype.is_int() || dtype.is_uint()) { | ||
| return {kTVMArgInt, Cast(DataType::Int(64), val)}; | ||
| } else if (dtype.is_float()) { | ||
| return {kTVMArgFloat, Cast(DataType::Float(64), val)}; | ||
| } else if (dtype.is_void()) { | ||
| return {kTVMNullptr, val}; | ||
| } else { | ||
| LOG(FATAL) << "data type " << dtype << " not supported yet"; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we may need to return at least DLTensor for AOT
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add which data datatypes are supported to this error message |
||
| } | ||
| return {kTVMNullptr, val}; | ||
| } | ||
|
|
||
| Stmt WriteToOut(PrimExpr val, Var ret_var, Var ret_tcode) { | ||
| auto p = ConvertForFFI(val); | ||
| int tcode = p.first; | ||
| val = p.second; | ||
| Stmt store_val = Store(ret_var_, val, 0, const_true()); | ||
| Stmt store_tcode = Store(ret_tcode_, tcode, 0, const_true()); | ||
| Stmt ret_zero = Evaluate(tvm::ret(0)); | ||
| return SeqStmt({store_val, store_tcode, ret_zero}); | ||
| } | ||
|
|
||
| Var ret_var_; | ||
| Var ret_tcode_; | ||
| int in_parallel_{0}; | ||
| }; | ||
|
|
||
| Stmt RewriteReturn(Stmt body, Var ret_var, Var ret_tcode) { | ||
| ReturnRewriter rewriter(ret_var, ret_tcode); | ||
| return rewriter(body); | ||
| } | ||
|
|
||
| inline Stmt MakeAssertEQ(PrimExpr lhs, PrimExpr rhs, std::string msg) { | ||
| return AssertStmt(lhs == rhs, tvm::tir::StringImm(msg), Evaluate(0)); | ||
| } | ||
|
|
@@ -182,8 +243,9 @@ PrimFunc MakePackedAPI(PrimFunc&& func, int num_unpacked_args) { | |
| func = WithAttr(std::move(func), tvm::attr::kCallingConv, Integer(CallingConv::kCPackedFunc)); | ||
| } | ||
|
|
||
| Stmt body = AttrStmt(make_zero(DataType::Int(32)), attr::compute_scope, | ||
| StringImm(name_hint + "_compute_"), func_ptr->body); | ||
| Stmt body = RewriteReturn(func_ptr->body, v_out_ret_value, v_out_ret_tcode); | ||
| body = AttrStmt(make_zero(DataType::Int(32)), attr::compute_scope, | ||
| StringImm(name_hint + "_compute_"), body); | ||
| // Set device context | ||
| if (vmap.count(device_id.get())) { | ||
| PrimExpr node = StringImm("default"); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
| import tvm | ||
| from tvm import tir | ||
| from tvm.ir.transform import PassContext | ||
|
|
||
|
|
||
| def build_tir_func(func): | ||
| func = func.with_attr("global_symbol", "main") | ||
| pass_ctx = PassContext.current() | ||
| if pass_ctx.config.get("tir.noalias", True): | ||
| func = func.with_attr("tir.noalias", True) | ||
| mod = tvm.IRModule({"main": func}) | ||
| func = tvm.build(mod) | ||
| return func | ||
|
|
||
|
|
||
| def test_scalar_add(): | ||
| a = tir.Var("a", "float32") | ||
| b = tir.Var("b", "float32") | ||
| c = a + b | ||
| c = tir.ret(c) | ||
| c = tir.Evaluate(c) | ||
| func = tir.PrimFunc([a, b], c) | ||
| func = build_tir_func(func) | ||
| out = func(1.0, 2.0) | ||
| assert out == 3.0 | ||
|
|
||
|
|
||
| def test_control_flow_jump(): | ||
| ib = tvm.tir.ir_builder.create() | ||
| a = tir.Var("a", "float32") | ||
| b = tir.Var("b", "float32") | ||
| with ib.if_scope(True): | ||
| ib.emit(tir.Evaluate(tir.ret(a))) | ||
| ib.emit(tir.Evaluate(tir.ret(b))) | ||
| stmt = ib.get() | ||
| func = tir.PrimFunc([a, b], stmt) | ||
| func = build_tir_func(func) | ||
| out = func(1.0, 2.0) | ||
| assert out == 1.0 | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| test_scalar_add() | ||
| test_control_flow_jump() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.