Skip to content
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

[IR&PASS] part 3-1: Add PatternMatch base class. #54385

Merged
merged 2 commits into from
Jun 7, 2023
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/ir/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ endif()

add_subdirectory(core)
add_subdirectory(pass)
add_subdirectory(pattern_rewrite)
6 changes: 6 additions & 0 deletions paddle/ir/pattern_rewrite/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
file(GLOB PATTERN_SRCS "*.cc")

cc_library(
pattern_rewrite
SRCS ${PATTERN_SRCS}
DEPS new_ir)
144 changes: 144 additions & 0 deletions paddle/ir/pattern_rewrite/pattern_match.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
// Copyright (c) 2023 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/ir/pattern_rewrite/pattern_match.h"
#include <cassert>
#include <cstdint>
Copy link
Contributor

Choose a reason for hiding this comment

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

有用到吗?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

用到了assert, uint32_t

#include "paddle/ir/core/operation.h"

namespace ir {

//===----------------------------------------------------------------------===//
// Pattern
//===----------------------------------------------------------------------===//

// Pattern::Pattern(const void* root_val,
// RootKind root_kind,
// const std::vector<std::string>& generated_names,
// PatternBenefit benefit,
// ir::IrContext* context)
// : benefit_(benefit), context_(context), generated_names_(generated_names)
// {}

Pattern::Pattern(const std::string& root_name,
PatternBenefit benefit,
IrContext* context,
const std::vector<std::string>& generated_names)
: op_name_(root_name),
root_kind_(RootKind::OperationName),
benefit_(benefit),
context_(context),
generated_names_(generated_names) {}

Pattern::Pattern(MatchAnyOpTypeTag tag,
PatternBenefit benefit,
ir::IrContext* context,
const std::vector<std::string>& generated_names)
: root_kind_(RootKind::Any),
benefit_(benefit),
context_(context),
generated_names_(generated_names) {}

Pattern::Pattern(MatchInterfaceOpTypeTag tag,
ir::TypeId interface_id,
PatternBenefit benefit,
ir::IrContext* context,
const std::vector<std::string>& generated_names)
: interface_id_(interface_id),
root_kind_(RootKind::InterfaceId),
benefit_(benefit),
context_(context),
generated_names_(generated_names) {}

Pattern::Pattern(MatchTraitOpTypeTag tag,
ir::TypeId trait_id,
PatternBenefit benefit,
ir::IrContext* context,
const std::vector<std::string>& generated_names)
: trait_id_(trait_id),
root_kind_(RootKind::TraitId),
benefit_(benefit),
context_(context),
generated_names_(generated_names) {}

RewritePattern::~RewritePattern() = default;

//===----------------------------------------------------------------------===//
// RewriterBase
//===----------------------------------------------------------------------===//

RewriterBase::~RewriterBase() = default;

// TODO(wilber): value support replace method.
// void RewriterBase::ReplaceOpWithIf(Operation* op,
// ValueRange new_values,
// bool* all_uses_replaced,
// std::function<bool(OpOperand&)> functor) {
// // assert(op->num_results() == new_values.size() && "incorrect number of
// values to replace operation"); NotifyRootReplaced(op, new_values); bool
// replace_all_uses = true; for (uint32_t i = 0; i < op->num_results(); ++i) {
// // op->GetResultByIndex(0)
// }
// }
// void RewriterBase::ReplaceOpWithIf(Operation* op,
// ValueRange new_values,
// std::function<bool(OpOperand&)> functor) {
// ReplaceOpWithIf(op, new_values, nullptr, functor);
// }

// TODO(wilber): support erase.
// void ReplaceOp(Operation* op, ValueRange new_values) {
// NotifyRootReplaced(op, new_values);
// assert(op->num_results() == new_values.size() && "incorrect # of
// replacement values"); op->ReplaceAllUsesWith(new_values);
// NotifyOperationRemoved(op);
// op->erase();
// }
void RewriterBase::EraseOp(Operation* op) {
// assert(op->use_empty() && "expected 'op' to have no uses");
Copy link
Contributor

Choose a reason for hiding this comment

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

ir下有计划将assert和throw统一换成paddle下enforce和throw,后面需要统一改一下

Copy link
Contributor Author

Choose a reason for hiding this comment

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

等从phi里独立出来之后统一改下

// NotifyOperationRemoved(op);
// op->erase();
}

void RewriterBase::ReplaceAllUsesWith(Value from, Value to) {
// from.
// for (mlir::OpOperand& operand : llvm::make_early_inc_range(from.getUses()))
// {
// mlir::Operation* op = operand.getOwner();
// UpdateRootInPlace(op, [&]() { operand.set(to); });
// }
}

// TODO(wilber): iterator maybe should support modify inplace.
void RewriterBase::ReplaceUseIf(Value from,
Value to,
std::function<bool(OpOperand&)> functor) {
// for (auto it = from.begin(); it != from.end(); ++it) {
// // TODO: need a lvalue.
// if (functor(it.get())) {
// UpdateRootInplace(it.owner(), [&](){it.get().set(to)});
// }
// }
}

void RewriterBase::ReplaceOpWithResultsOfAnotherOp(Operation* op,
Operation* new_op) {
assert(op->num_results() == new_op->num_results() &&
"replacement op doesn't match results of original op");
// TODO(wilber): Op support results method.
// if (op->num_results() == 1) return ReplaceOp(op,
// new_op->GetResultByIndex(0)); return ReplaceOp(op, new_op->GetResults());
}

} // namespace ir
Loading