forked from PaddlePaddle/Paddle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request PaddlePaddle#20 from Superjomn/fea/add-ast-gen
add ast build
- Loading branch information
Showing
8 changed files
with
144 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,46 @@ | ||
#include "cinn/poly/ast_gen.h" | ||
|
||
namespace cinn { | ||
namespace poly { | ||
|
||
isl::ast_node AstGen::operator()(const std::vector<Element> &elements, const Scheduler &scheduler) { | ||
// Collect domains. | ||
auto sets = utils::Map<std::vector<Element>, isl::set>(elements, [](const Element &e) { return e.domain(); }); | ||
isl::union_set domain = SetsToUnionSet(sets); | ||
|
||
isl::ctx ctx = elements.front().domain().ctx(); | ||
|
||
// Collect schedule from scheduler. | ||
auto schedules = scheduler.BuildSchedule(); | ||
std::vector<isl::map> maps; | ||
for (auto &ele : elements) { | ||
auto it = schedules.find(ele.id()); | ||
CHECK(it != std::end(schedules)); | ||
maps.push_back(it->second); | ||
} | ||
auto schedule = MapsToUnionMap(maps); | ||
|
||
// Build it. | ||
auto build = isl::ast_build::from_context(context_); | ||
// Set iterators. | ||
if (!iterator_names_.empty()) { | ||
auto iterator_names = scheduler.WrapIteratorNames(iterator_names_); | ||
isl::id_list ids = isl::manage(isl_id_list_alloc(ctx.get(), iterator_names.size())); | ||
for (int i = 0; i < iterator_names.size(); i++) { | ||
ids = isl::manage(isl_id_list_add(ids.release(), isl_id_alloc(ctx.get(), iterator_names[i].c_str(), nullptr))); | ||
} | ||
build = isl::manage(isl_ast_build_set_iterators(build.release(), ids.release())); | ||
} | ||
|
||
auto ast = build.node_from_schedule_map(schedule.intersect_domain(domain)); | ||
VLOG(2) << "\n" << isl_ast_node_to_C_str(ast.get()); | ||
return ast; | ||
} | ||
|
||
AstGen &AstGen::SetIteratorNames(const std::vector<std::string> &names) { | ||
iterator_names_ = names; | ||
return *this; | ||
} | ||
|
||
} // namespace poly | ||
} // namespace cinn |
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 |
---|---|---|
@@ -1 +1,30 @@ | ||
#pragma once | ||
#include <isl/cpp.h> | ||
#include "cinn/poly/element.h" | ||
#include "cinn/poly/isl_utils.h" | ||
#include "cinn/poly/schedule.h" | ||
#include "cinn/utils/functional.h" | ||
|
||
namespace cinn { | ||
namespace poly { | ||
|
||
class AstGen { | ||
public: | ||
AstGen(const isl::set& context) : context_(context) {} | ||
|
||
/** | ||
* Set forloop iterator names. | ||
* @param names | ||
* @return AstGen itself. | ||
*/ | ||
AstGen& SetIteratorNames(const std::vector<std::string>& names); | ||
|
||
isl::ast_node operator()(const std::vector<Element>& elements, const Scheduler& scheduler); | ||
|
||
private: | ||
isl::set context_; | ||
std::vector<std::string> iterator_names_; | ||
}; | ||
|
||
} // namespace poly | ||
} // namespace cinn |
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,23 @@ | ||
#include "cinn/poly/ast_gen.h" | ||
#include <gtest/gtest.h> | ||
|
||
namespace cinn { | ||
namespace poly { | ||
|
||
TEST(ast_gen, basic) { | ||
isl::ctx ctx(isl_ctx_alloc()); | ||
Element A(isl::set(ctx, "{ A[i,j,k]: 0<i,j,k<100 }")); | ||
Element B(isl::set(ctx, "{ B[i,j,k]: 0<i,j,k<100 }")); | ||
|
||
Scheduler scheduler; | ||
scheduler.RegisterElement(A); | ||
scheduler.RegisterElement(B); | ||
scheduler.After(A, B, 2); | ||
|
||
AstGen gen(isl::set(ctx, "{:}")); | ||
gen.SetIteratorNames({"i", "j", "k"}); | ||
gen({A, B}, scheduler); | ||
} | ||
|
||
} // namespace poly | ||
} // namespace cinn |
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