-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change-Id: I8aa43d3a1b837b03a5cf3c6b32fc760bd78d3436
- Loading branch information
Giuseppe Rossini
committed
Jun 22, 2021
1 parent
369745f
commit 6b010fd
Showing
6 changed files
with
181 additions
and
101 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,108 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
/*! | ||
* \file make_packed_call.cc | ||
* \brief Rewrite packed calls in AOT so that the arguments are packed | ||
*/ | ||
#include <tvm/tir/builtin.h> | ||
#include <tvm/tir/expr.h> | ||
#include <tvm/tir/function.h> | ||
#include <tvm/tir/op.h> | ||
#include <tvm/tir/stmt_functor.h> | ||
#include <tvm/tir/transform.h> | ||
|
||
#include <unordered_map> | ||
|
||
#include "ir_utils.h" | ||
|
||
namespace tvm { | ||
namespace tir { | ||
|
||
using InputMap = | ||
std::unordered_map<PrimExpr, bool, runtime::ObjectPtrHash, runtime::ObjectPtrEqual>; | ||
/** | ||
* This is a legalization pass only used in AOT. Traverse the TIR graph to legalize | ||
* packed calls by making its argument wrapped in TVMValues (by using tvm_set_struct built-in) | ||
*/ | ||
class PackedCallLegalizer : public StmtExprMutator { | ||
public: | ||
Stmt Legalize(const InputMap& params, tir::Stmt body) { | ||
inputs_ = params; | ||
return StmtExprMutator::VisitStmt(body); | ||
} | ||
|
||
Stmt VisitStmt_(const EvaluateNode* op) final { | ||
if (tir::is_const_int(op->value)) return StmtExprMutator::VisitStmt_(op); | ||
const CallNode* call = op->value.as<CallNode>(); | ||
// Given a packed call f(A,B,C), we need a set of new statements | ||
// let A_packed = set_struct(tvm_value1, A) | ||
// let B_packed = set_struct(tvm_value2, B) | ||
// let C_packed = set_struct(tvm_value3, C) | ||
// call_packed(f, A_packed, B_packed, C_packed) | ||
std::vector<Stmt> new_stmts; | ||
if (call) { | ||
if (call->op.same_as(builtin::tvm_call_cpacked())) { | ||
Array<PrimExpr> packed_args{call->args[0]}; | ||
for (unsigned i = 1; i < call->args.size(); i++) { | ||
// No need to pack inputs of the prim_func | ||
if (inputs_[call->args[i]] == true) { | ||
packed_args.push_back(call->args[i]); | ||
} else { | ||
// Pack the argument inside a TVMValue | ||
auto sid_array = tir::Var("tvm_value", DataType::Handle()); | ||
tir::Stmt set_struct_stmt = tir::Evaluate( | ||
tvm::tir::Call(DataType::Handle(), tvm::tir::builtin::tvm_struct_set(), | ||
{sid_array, 0, tir::builtin::kArrData, call->args[i]})); | ||
new_stmts.push_back(LetStmt(sid_array, StackAlloca("array", 1), set_struct_stmt)); | ||
packed_args.push_back(sid_array); | ||
} | ||
} | ||
// Finally, evaluate the packed call and return a sequential statement | ||
new_stmts.push_back(tir::Evaluate(tir::Call(call->dtype, call->op, packed_args))); | ||
return tir::SeqStmt(new_stmts); | ||
} | ||
} | ||
return StmtExprMutator::VisitStmt_(op); | ||
} | ||
|
||
private: | ||
InputMap inputs_; // Store the inputs to the primfunc that don't need to be packed. | ||
}; | ||
|
||
namespace transform { | ||
|
||
Pass LegalizePackedCalls() { | ||
auto pass_func = [=](PrimFunc f, IRModule m, PassContext ctx) { | ||
auto* n = f.CopyOnWrite(); | ||
|
||
// Create the | ||
InputMap inputs; | ||
for (auto i : f->params) { | ||
inputs[i] = true; | ||
} | ||
n->body = PackedCallLegalizer().Legalize(inputs, std::move(n->body)); | ||
return std::move(f); | ||
}; | ||
return CreatePrimFuncPass(pass_func, 0, "tir.LegalizePackedCalls", {}); | ||
} | ||
} // namespace transform | ||
|
||
} // namespace tir | ||
} // namespace tvm |
This file contains 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
Oops, something went wrong.