-
Notifications
You must be signed in to change notification settings - Fork 5.7k
[CINN]backend support stmt to expr visitor and run on func body #70739
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
Hongqing-work
merged 1 commit into
PaddlePaddle:develop
from
Hongqing-work:add-expr-visitor
Jan 10, 2025
Merged
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,218 @@ | ||
// Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved. | ||
// | ||
// Licensed 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. | ||
#include "paddle/cinn/ir/expr_visitors.h" | ||
|
||
namespace cinn { | ||
namespace ir { | ||
|
||
void VisitExpr(const stmt::Let &stmt, | ||
const std::function<void(const Expr &)> &callback) { | ||
const auto &symbol = stmt->symbol(); | ||
const auto &body = stmt->body(); | ||
callback(symbol); | ||
if (body.defined()) { | ||
callback(body); | ||
} | ||
} | ||
|
||
void VisitExpr(const stmt::Store &stmt, | ||
const std::function<void(const Expr &)> &callback) { | ||
const auto &value = stmt->value(); | ||
const auto &tensor = stmt->tensor(); | ||
const auto &indices = stmt->indices(); | ||
callback(value); | ||
callback(tensor); | ||
for (const auto &indice : indices) { | ||
callback(indice); | ||
} | ||
} | ||
|
||
void VisitExpr(const stmt::Alloc &stmt, | ||
const std::function<void(const Expr &)> &callback) { | ||
const auto &destination = stmt->destination(); | ||
const auto &extents = stmt->extents(); | ||
const auto &condition = stmt->condition(); | ||
const auto &body = stmt->body(); | ||
callback(destination); | ||
for (const auto &extent : extents) { | ||
callback(extent); | ||
} | ||
if (condition.defined()) { | ||
callback(condition); | ||
} | ||
if (body.defined()) { | ||
callback(body); | ||
} | ||
} | ||
|
||
void VisitExpr(const stmt::Free &stmt, | ||
const std::function<void(const Expr &)> &callback) { | ||
const auto &destination = stmt->destination(); | ||
callback(destination); | ||
} | ||
|
||
void VisitExpr(const stmt::IfThenElse &stmt, | ||
const std::function<void(const Expr &)> &callback) { | ||
const auto &condition = stmt->condition(); | ||
callback(condition); | ||
} | ||
|
||
void VisitExpr(const stmt::For &stmt, | ||
const std::function<void(const Expr &)> &callback) { | ||
const auto &min = stmt->min(); | ||
const auto &extent = stmt->extent(); | ||
callback(min); | ||
callback(extent); | ||
} | ||
|
||
void VisitExpr(const stmt::Schedule &stmt, | ||
const std::function<void(const Expr &)> &callback) { | ||
const auto &iter_vars = stmt->iter_vars(); | ||
const auto &iter_values = stmt->iter_values(); | ||
const auto &read_buffers = stmt->read_buffers(); | ||
const auto &write_buffers = stmt->write_buffers(); | ||
|
||
for (const auto &iter_var : iter_vars) { | ||
if (iter_var->lower_bound.defined()) { | ||
callback(iter_var->lower_bound); | ||
} | ||
if (iter_var->upper_bound.defined()) { | ||
callback(iter_var->upper_bound); | ||
} | ||
} | ||
for (const auto &iter_value : iter_values) { | ||
callback(iter_value); | ||
} | ||
for (const auto &read_buffer : read_buffers) { | ||
callback(read_buffer); | ||
} | ||
for (const auto &write_buffer : write_buffers) { | ||
callback(write_buffer); | ||
} | ||
} | ||
|
||
void VisitExpr(const stmt::Evaluate &stmt, | ||
const std::function<void(const Expr &)> &callback) { | ||
const auto &value = stmt->value(); | ||
callback(value); | ||
} | ||
|
||
void MutateExpr(stmt::Let stmt, const std::function<void(Expr *)> &callback) { | ||
ir::Expr symbol = stmt->symbol(); | ||
ir::Expr body = stmt->body(); | ||
callback(&symbol); | ||
if (body.defined()) { | ||
callback(&body); | ||
} | ||
stmt->set_symbol(symbol); | ||
stmt->set_body(body); | ||
} | ||
|
||
void MutateExpr(stmt::Store stmt, const std::function<void(Expr *)> &callback) { | ||
ir::Expr value = stmt->value(); | ||
ir::Expr tensor = stmt->tensor(); | ||
std::vector<ir::Expr> indices = stmt->indices(); | ||
callback(&value); | ||
callback(&tensor); | ||
for (ir::Expr &indice : indices) { | ||
callback(&indice); | ||
} | ||
stmt->set_value(value); | ||
stmt->set_tensor(tensor); | ||
stmt->set_indices(indices); | ||
} | ||
|
||
void MutateExpr(stmt::Alloc stmt, const std::function<void(Expr *)> &callback) { | ||
ir::Expr destination = stmt->destination(); | ||
std::vector<ir::Expr> extents = stmt->extents(); | ||
ir::Expr condition = stmt->condition(); | ||
ir::Expr body = stmt->body(); | ||
callback(&destination); | ||
for (ir::Expr &extent : extents) { | ||
callback(&extent); | ||
} | ||
if (condition.defined()) { | ||
callback(&condition); | ||
} | ||
if (body.defined()) { | ||
callback(&body); | ||
} | ||
stmt->set_destination(destination); | ||
stmt->set_extents(extents); | ||
stmt->set_condition(condition); | ||
stmt->set_body(body); | ||
} | ||
|
||
void MutateExpr(stmt::Free stmt, const std::function<void(Expr *)> &callback) { | ||
ir::Expr destination = stmt->destination(); | ||
callback(&destination); | ||
stmt->set_destination(destination); | ||
} | ||
|
||
void MutateExpr(stmt::IfThenElse stmt, | ||
const std::function<void(Expr *)> &callback) { | ||
ir::Expr condition = stmt->condition(); | ||
callback(&condition); | ||
stmt->set_condition(condition); | ||
} | ||
|
||
void MutateExpr(stmt::For stmt, const std::function<void(Expr *)> &callback) { | ||
ir::Expr min = stmt->min(); | ||
ir::Expr extent = stmt->extent(); | ||
callback(&min); | ||
callback(&extent); | ||
stmt->set_min(min); | ||
stmt->set_extent(extent); | ||
} | ||
|
||
void MutateExpr(stmt::Schedule stmt, | ||
const std::function<void(Expr *)> &callback) { | ||
std::vector<ir::Var> iter_vars = stmt->iter_vars(); | ||
std::vector<ir::Expr> iter_values = stmt->iter_values(); | ||
std::vector<ir::Expr> read_buffers = stmt->read_buffers(); | ||
std::vector<ir::Expr> write_buffers = stmt->write_buffers(); | ||
|
||
for (ir::Var iter_var : iter_vars) { | ||
if (iter_var->lower_bound.defined()) { | ||
callback(&(iter_var->lower_bound)); | ||
} | ||
if (iter_var->upper_bound.defined()) { | ||
callback(&(iter_var->upper_bound)); | ||
} | ||
} | ||
for (ir::Expr &iter_value : iter_values) { | ||
callback(&iter_value); | ||
} | ||
for (ir::Expr &read_buffer : read_buffers) { | ||
callback(&read_buffer); | ||
} | ||
for (ir::Expr &write_buffer : write_buffers) { | ||
callback(&write_buffer); | ||
} | ||
|
||
stmt->set_iter_vars(iter_vars); | ||
stmt->set_iter_values(iter_values); | ||
stmt->set_read_buffers(read_buffers); | ||
stmt->set_write_buffers(write_buffers); | ||
} | ||
|
||
void MutateExpr(stmt::Evaluate stmt, | ||
const std::function<void(Expr *)> &callback) { | ||
ir::Expr value = stmt->value(); | ||
callback(&value); | ||
stmt->set_value(value); | ||
} | ||
|
||
} // namespace ir | ||
} // namespace cinn |
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,70 @@ | ||
// Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved. | ||
// | ||
// Licensed 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. | ||
|
||
#pragma once | ||
|
||
#include <functional> | ||
|
||
#include "paddle/cinn/ir/ir.h" | ||
#include "paddle/cinn/ir/stmt.h" | ||
|
||
namespace cinn { | ||
namespace ir { | ||
|
||
// Defines utilities for walking exprs (no walking into nest block) | ||
void VisitExpr(const stmt::Let &stmt, | ||
const std::function<void(const Expr &)> &callback); | ||
|
||
void VisitExpr(const stmt::Store &stmt, | ||
const std::function<void(const Expr &)> &callback); | ||
|
||
void VisitExpr(const stmt::Alloc &stmt, | ||
const std::function<void(const Expr &)> &callback); | ||
|
||
void VisitExpr(const stmt::Free &stmt, | ||
const std::function<void(const Expr &)> &callback); | ||
|
||
void VisitExpr(const stmt::IfThenElse &stmt, | ||
const std::function<void(const Expr &)> &callback); | ||
|
||
void VisitExpr(const stmt::For &stmt, | ||
const std::function<void(const Expr &)> &callback); | ||
|
||
void VisitExpr(const stmt::Schedule &stmt, | ||
const std::function<void(const Expr &)> &callback); | ||
|
||
void VisitExpr(const stmt::Evaluate &stmt, | ||
const std::function<void(const Expr &)> &callback); | ||
|
||
void MutateExpr(stmt::Let stmt, const std::function<void(Expr *)> &callback); | ||
|
||
void MutateExpr(stmt::Store stmt, const std::function<void(Expr *)> &callback); | ||
|
||
void MutateExpr(stmt::Alloc stmt, const std::function<void(Expr *)> &callback); | ||
|
||
void MutateExpr(stmt::Free stmt, const std::function<void(Expr *)> &callback); | ||
|
||
void MutateExpr(stmt::IfThenElse stmt, | ||
const std::function<void(Expr *)> &callback); | ||
|
||
void MutateExpr(stmt::For stmt, const std::function<void(Expr *)> &callback); | ||
|
||
void MutateExpr(stmt::Schedule stmt, | ||
const std::function<void(Expr *)> &callback); | ||
|
||
void MutateExpr(stmt::Evaluate stmt, | ||
const std::function<void(Expr *)> &callback); | ||
|
||
} // namespace ir | ||
} // namespace cinn |
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Expr本身已经是一个指针类型了,为什么要换成
Expr*
类型?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
因为现有的对Expr的操作是允许inplace替换的,直接把这个指针改成新的指针了,要兼容这种替换必须用Expr*,否则底层对Expr的Visitor也需要全部升级改造重写允许返回值
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
允许返回值的Visitor的问题是什么?为什么不选用这个方式
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
成本问题,需要把对所有Expr的访问再升级改造