FreeTensor
Loading...
Searching...
No Matches
make_nested_loops.h
Go to the documentation of this file.
1#ifndef FREE_TENSOR_MAKE_NESTED_LOOPS_H
2#define FREE_TENSOR_MAKE_NESTED_LOOPS_H
3
4#include <type_traits>
5
6#include <container_utils.h>
7#include <stmt.h>
8
9namespace freetensor {
10
19template <class Titers, class Tbegins, class Tends, class Tsteps, class Tlens,
20 class Tproperties, class Tbody>
21Stmt makeNestedLoops(Titers &&iters, Tbegins &&begins, Tends &&ends,
22 Tsteps &&steps, Tlens &&lens, Tproperties &&properties,
23 Tbody &&body) {
24 Stmt ret = std::forward<Tbody>(body);
25 for (auto &&[_iter, begin, _end, step, _len, property] : views::reverse(
26 views::zip(iters, begins, ends, steps, lens, properties))) {
27 std::string *iter = nullptr;
28 if constexpr (std::is_same_v<std::decay_t<decltype(_iter)>,
29 std::string>) {
30 iter = &_iter;
31 } else if constexpr (std::is_same_v<std::decay_t<decltype(_iter)>,
32 Expr>) {
33 ASSERT(_iter->nodeType() == ASTNodeType::Var);
34 iter = &_iter.template as<VarNode>()->name_;
35 } else {
36 ASSERT(false);
37 }
38 auto &&end = ((Expr)_end).isValid()
39 ? (Expr)_end
40 : makeAdd(begin, makeMul(_len, step));
41 auto &&len = ((Expr)_len).isValid()
42 ? (Expr)_len
43 : makeCeilDiv(makeSub(_end, begin), step);
44 ret = makeFor(*iter, begin, end, step, len, property, ret);
45 }
46 return ret;
47}
48
49} // namespace freetensor
50
51#endif // FREE_TENSOR_MAKE_NESTED_LOOPS_H
bool isValid() const
Definition: ref.h:89
#define ASSERT(expr)
Definition: except.h:152
Definition: allocator.h:9
Expr makeMul(T &&lhs, U &&rhs, std::source_location loc=std::source_location::current())
Definition: expr.h:202
Expr makeAdd(T &&lhs, U &&rhs, std::source_location loc=std::source_location::current())
Definition: expr.h:174
Expr makeCeilDiv(T &&lhs, U &&rhs, std::source_location loc=std::source_location::current())
Definition: expr.h:259
Stmt makeNestedLoops(Titers &&iters, Tbegins &&begins, Tends &&ends, Tsteps &&steps, Tlens &&lens, Tproperties &&properties, Tbody &&body)
Definition: make_nested_loops.h:21
Expr makeSub(T &&lhs, U &&rhs, std::source_location loc=std::source_location::current())
Definition: expr.h:188
Stmt makeFor(const std::string &iter, Tbegin &&begin, Tend &&end, Tstep &&step, Tlen &&len, Tproperty &&property, Tbody &&body, const Metadata &metadata=nullptr, const ID &id={}, std::source_location loc=std::source_location::current())
Definition: stmt.h:311
Ref< ExprNode > Expr
Definition: ast.h:184