Skip to content

[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
merged 1 commit into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions paddle/cinn/ir/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ gather_srcs(
schedule_block_graph.cc
stmt.cc
stmt_visitors.cc
expr_visitors.cc
dim.cc)

add_subdirectory(ir_analyzer)
Expand Down
218 changes: 218 additions & 0 deletions paddle/cinn/ir/expr_visitors.cc
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
70 changes: 70 additions & 0 deletions paddle/cinn/ir/expr_visitors.h
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
6 changes: 3 additions & 3 deletions paddle/cinn/optim/longlong2int_pass.cc
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ class LongLong2IntStmtPass : public StmtPass {
class LongLong2IntExprPass : public ExprPass {
public:
LongLong2IntExprPass() : ExprPass("longlong2int_expr") {}
LogicalResult Run(ir::Expr expr) override;
LogicalResult Run(ir::Expr* expr) override;
};
} // namespace

Expand Down Expand Up @@ -233,9 +233,9 @@ LogicalResult LongLong2IntStmtPass::Run(ir::stmt::StmtRef stmt) {
return LogicalResult::success();
}

LogicalResult LongLong2IntExprPass::Run(ir::Expr expr) {
LogicalResult LongLong2IntExprPass::Run(ir::Expr* expr) {
CastLonglong2IntMutator narrow;
narrow(&expr);
narrow(expr);
return LogicalResult::success();
}
std::unique_ptr<StmtPass> CreateLongLong2IntStmtPass() {
Expand Down
11 changes: 9 additions & 2 deletions paddle/cinn/pass/pass.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,14 @@ class FuncPass : public Pass<ir::LoweredFunc> {
public:
explicit FuncPass(const std::string& name) : Pass(PassKind::PK_FUNC, name) {}

// Run on the whole function.
virtual LogicalResult Run(ir::LoweredFunc f) = 0;
// Only run on function body.
virtual LogicalResult Run(ir::stmt::BlockRef block) {
LOG(WARNING) << name()
<< "should run on the whole function, not on the body block.";
return LogicalResult::failure();
}
};

class BlockPass : public Pass<ir::stmt::BlockRef> {
Expand All @@ -74,10 +81,10 @@ class StmtPass : public Pass<ir::stmt::StmtRef> {
virtual LogicalResult Run(ir::stmt::StmtRef stmt) = 0;
};

class ExprPass : public Pass<ir::Expr> {
class ExprPass : public Pass<ir::Expr*> {
public:
explicit ExprPass(const std::string& name) : Pass(PassKind::PK_STMT, name) {}
virtual LogicalResult Run(ir::Expr expr) = 0;
virtual LogicalResult Run(ir::Expr* expr) = 0;
Comment on lines +84 to +87
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expr本身已经是一个指针类型了,为什么要换成Expr*类型?

Copy link
Contributor Author

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也需要全部升级改造重写允许返回值

Copy link
Contributor

@zyfncg zyfncg Jan 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

允许返回值的Visitor的问题是什么?为什么不选用这个方式

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

成本问题,需要把对所有Expr的访问再升级改造

};

} // namespace optim
Expand Down
Loading
Loading