-
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.
- Loading branch information
Showing
8 changed files
with
146 additions
and
9 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
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,77 @@ | ||
/*! | ||
* Copyright (c) 2016 by Contributors | ||
* SSA related checks and pass. | ||
* \file ssa.cc | ||
*/ | ||
#include <tvm/ir.h> | ||
#include <tvm/ir_pass.h> | ||
#include <tvm/ir_mutator.h> | ||
#include <unordered_set> | ||
#include <unordered_map> | ||
#include <vector> | ||
#include "../schedule/compute_expr.h" | ||
|
||
namespace tvm { | ||
namespace ir { | ||
|
||
class LoopUnroller : public IRMutator { | ||
public: | ||
explicit LoopUnroller(int max_auto_step) | ||
: max_auto_step_(max_auto_step) { | ||
} | ||
|
||
Stmt Mutate_(const For* op, const Stmt& s) { | ||
Stmt stmt = s; | ||
// constant folding. | ||
Expr extent = ir::Simplify(op->extent); | ||
const IntImm* v1 = extent.as<IntImm>(); | ||
const UIntImm* v2 = extent.as<UIntImm>(); | ||
int value = -1; | ||
if (v1 != nullptr) { | ||
value = static_cast<int>(v1->value); | ||
} | ||
if (v2 != nullptr) { | ||
value = static_cast<int>(v2->value); | ||
} | ||
bool allow_unroll = value >= 0 && value <= max_auto_step_; | ||
if (op->for_type == ForType::Unrolled) { | ||
CHECK_GE(value, 0) | ||
<< "Cannot unroll non-constant loop"; | ||
allow_unroll = true; | ||
} | ||
|
||
if (allow_unroll) { | ||
if (value == 0) return Evaluate::make(0); | ||
Stmt body = op->body; | ||
Map<Var, Expr> vmap; | ||
Stmt unrolled; | ||
for (int i = 0; i < value; ++i) { | ||
Var lv(op->loop_var.node_); | ||
vmap.Set(lv, | ||
schedule::ComputeExpr<Add>( | ||
op->min, make_const(op->loop_var.type(), i))); | ||
Stmt step = Substitute(body, vmap); | ||
if (unrolled.defined()) { | ||
unrolled = Block::make(unrolled, step); | ||
} else { | ||
unrolled = step; | ||
} | ||
} | ||
return this->Mutate(unrolled); | ||
} else { | ||
return IRMutator::Mutate_(op, stmt); | ||
} | ||
} | ||
|
||
private: | ||
int max_auto_step_; | ||
}; | ||
|
||
|
||
Stmt UnrollLoop(Stmt stmt, int max_auto_step) { | ||
Stmt ret = LoopUnroller(max_auto_step).Mutate(stmt); | ||
return ConvertSSA(ret); | ||
} | ||
|
||
} // namespace ir | ||
} // 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
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,20 @@ | ||
import tvm | ||
|
||
def test_unroll_loop(): | ||
dtype = 'int64' | ||
n = tvm.Var('n') | ||
Ab = tvm.Buffer((n, ), dtype) | ||
i = tvm.Var('i') | ||
j = tvm.Var('j') | ||
# for i in 0 to n-1: | ||
stmt = tvm.make.For( | ||
i, n, 2, 0, 0, | ||
tvm.make.For(j, 0, n, 0, 0, | ||
tvm.make.Store(Ab.data, | ||
tvm.make.Load(dtype, Ab.data, i) + 1, | ||
j + 1))) | ||
stmt = tvm.ir_pass.UnrollLoop(stmt, 8) | ||
print(stmt) | ||
|
||
if __name__ == "__main__": | ||
test_unroll_loop() |