未验证 提交 d0224b83 编写于 作者: 傅剑寒 提交者: GitHub

【CINN】Remove if_simplify and rewrite block_simplify (#56307)

* remove if_simplify and rewrite block_simplify

* delete unused if_simplify
上级 f272d693
......@@ -24,7 +24,6 @@ gather_srcs(
buffer_assign.cc
replace_const_param_to_integer.cc
cast_simplify.cc
if_simplify.cc
lower_intrin.cc
cast_bool_to_int8.cc
collect_undefined_vars.cc
......@@ -60,7 +59,6 @@ cinn_cc_test(test_optimize SRCS optimize_test.cc DEPS cinncore)
cinn_cc_test(test_cache_read_write_replace SRCS
cache_read_write_replace_test.cc DEPS cinncore)
cinn_cc_test(test_cast_simplify SRCS cast_simplify_test.cc DEPS cinncore)
cinn_cc_test(test_if_simplify SRCS if_simplify_test.cc DEPS cinncore)
cinn_cc_test(test_remove_schedule_block SRCS remove_schedule_block_test.cc DEPS
cinncore)
cinn_cc_test(test_unroll_loops SRCS unroll_loops_test.cc DEPS cinncore)
// Copyright (c) 2021 CINN 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/optim/if_simplify.h"
#include "paddle/cinn/ir/utils/ir_mutator.h"
namespace cinn::optim {
namespace {
struct Mutator : public ir::IRMutator<> {
using ir::IRMutator<>::Visit;
void Visit(const ir::IfThenElse* op, Expr* expr) {
auto* condition_int = op->condition.As<ir::IntImm>();
auto* condition_uint = op->condition.As<ir::UIntImm>();
int64_t value;
if (condition_int || condition_uint) {
if (condition_int) {
value = condition_int->value;
} else {
value = condition_uint->value;
}
if (value) {
*expr = op->true_case;
} else {
if (op->false_case.defined()) {
*expr = op->false_case;
} else {
// null condition
*expr = ir::Block::Make({});
}
}
}
}
};
} // namespace
void IfSimplify(Expr* e) {
Mutator mutator;
mutator.Visit(e, e);
}
} // namespace cinn::optim
// Copyright (c) 2021 CINN 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 "paddle/cinn/ir/ir.h"
namespace cinn::optim {
void IfSimplify(Expr* e);
} // namespace cinn::optim
// Copyright (c) 2021 CINN 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/optim/if_simplify.h"
#include <gtest/gtest.h>
#include <string>
#include "paddle/cinn/ir/utils/ir_printer.h"
namespace cinn::optim {
TEST(IfSimplify, if_true) {
Var n("n");
auto e = ir::IfThenElse::Make(
Expr(1) /*true*/, ir::Let::Make(n, Expr(1)), ir::Let::Make(n, Expr(2)));
LOG(INFO) << "\n" << e;
IfSimplify(&e);
LOG(INFO) << e;
ASSERT_EQ(utils::GetStreamCnt(e), "int32 n = 1");
}
TEST(IfSimplify, if_false) {
Var n("n");
auto e = ir::IfThenElse::Make(
Expr(0) /*false*/, ir::Let::Make(n, Expr(1)), ir::Let::Make(n, Expr(2)));
LOG(INFO) << "\n" << e;
IfSimplify(&e);
LOG(INFO) << e;
ASSERT_EQ(utils::GetStreamCnt(e), "int32 n = 2");
}
TEST(IfSimplify, if_else_empty) {
Var n("n");
auto e = ir::IfThenElse::Make(Expr(0) /*false*/, ir::Let::Make(n, Expr(1)));
LOG(INFO) << "\n" << e;
IfSimplify(&e);
LOG(INFO) << e;
std::string target = utils::Trim(R"ROC(
{
}
)ROC");
ASSERT_EQ(utils::GetStreamCnt(e), target);
}
} // namespace cinn::optim
......@@ -299,9 +299,6 @@ struct SimplifyBlocksMutator : public ir::IRMutator<> {
*expr = node->stmts[0];
Visit(expr, expr);
} else {
for (auto& s : node->stmts) {
Visit(&s, &s);
}
std::vector<Expr> stmts;
for (auto& s : node->stmts) {
if (s.As<ir::Block>()) {
......@@ -311,35 +308,13 @@ struct SimplifyBlocksMutator : public ir::IRMutator<> {
stmts.push_back(inner_stmt);
}
} else {
IRMutator<>::Visit(&s, &s);
stmts.push_back(s);
}
}
expr->As<ir::Block>()->stmts = stmts;
}
}
void Visit(const IfThenElse* op, Expr* expr) override {
if (op->condition.As<ir::UIntImm>()) {
if (op->condition.as_bool() == false) {
VLOG(6) << "Simplify ir::IfThenElse false block";
if (expr->As<IfThenElse>()->false_case.defined()) {
*expr = expr->As<IfThenElse>()->false_case;
} else {
*expr = ir::Block::Make({});
}
} else {
if (expr->As<IfThenElse>()->true_case.defined()) {
VLOG(6) << "Simplify ir::IfThenElse true block";
*expr = expr->As<IfThenElse>()->true_case;
} else {
*expr = ir::Block::Make({});
}
}
ir::IRMutator<ir::Expr*>::Visit(expr, expr);
return;
}
ir::IRMutator<ir::Expr*>::Visit(op, expr);
}
};
struct SimplifyForLoopsMutator : public ir::IRMutator<> {
......
......@@ -23,7 +23,6 @@
#include "paddle/cinn/optim/eliminate_broadcast_in_forloop.h"
#include "paddle/cinn/optim/extern_call_process.h"
#include "paddle/cinn/optim/fold_cinn_call_arguments.h"
#include "paddle/cinn/optim/if_simplify.h"
#include "paddle/cinn/optim/insert_debug_log_callee.h"
#include "paddle/cinn/optim/ir_simplify.h"
#include "paddle/cinn/optim/lower_function_call_bind_vars.h"
......@@ -79,10 +78,6 @@ Expr Optimize(Expr e,
Simplify(&copied);
VLOG(10) << "After Optimize Simplify:" << copied;
// TODO(LiuYang): I attends to remove this part code, I integate it into
// ifthenelse part IfSimplify(&copied); VLOG(10) << "After Optimize
// IfSimplify:" << copied;
if (runtime_debug_info) {
LOG(WARNING) << "Turn on runtime debug information output";
InsertDebugLogCallee(&copied);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册