forked from PaddlePaddle/Paddle
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
152 additions
and
7 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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// 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 "Pass/PassRegistry.h" | ||
#include <memory> | ||
#include "llvm/ADT/StringMap.h" | ||
#include "llvm/Support/ManagedStatic.h" | ||
|
||
namespace infra { | ||
|
||
static llvm::ManagedStatic<llvm::StringMap<PassAllocatorFunction>> PassRegistry; | ||
|
||
void RegisterPass(const PassAllocatorFunction& func) { | ||
std::unique_ptr<Pass> pass = func(); | ||
|
||
PassRegistry->try_emplace(pass->GetPassInfo().name, func); | ||
} | ||
|
||
} // namespace infra |
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,36 @@ | ||
// 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. | ||
|
||
#pragma once | ||
|
||
#include <functional> | ||
#include <memory> | ||
#include "Pass.h" | ||
|
||
namespace infra { | ||
|
||
using PassAllocatorFunction = std::function<std::unique_ptr<Pass>()>; | ||
|
||
void RegisterPass(const PassAllocatorFunction& func); | ||
|
||
template <typename ConcretePass> | ||
struct PassRegistration { | ||
explicit PassRegistration(const PassAllocatorFunction& func) { | ||
RegisterPass(func); | ||
} | ||
PassRegistration() | ||
: PassRegistration([] { return std::make_unique<ConcretePass>(); }) {} | ||
}; | ||
|
||
} // namespace infra |
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 |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// 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 <pybind11/detail/common.h> | ||
#include "mlir/IR/Operation.h" | ||
#include "pybind11/pybind11.h" | ||
#include "pybind11/stl.h" | ||
|
||
#include "Pass/Pass.h" | ||
#include "Pass/PassManager.h" | ||
|
||
namespace py = pybind11; | ||
|
||
namespace infra { | ||
namespace { | ||
|
||
class PassTrampoline : public Pass { | ||
public: | ||
using Pass::Pass; | ||
|
||
inline bool CanScheduleOn(mlir::Operation* op) const override { | ||
PYBIND11_OVERRIDE(bool, Pass, CanScheduleOn, op); | ||
} | ||
void Run(mlir::Operation* op) override { | ||
PYBIND11_OVERRIDE_PURE(void, Pass, Run, op); | ||
} | ||
}; | ||
|
||
class PassPublicist : public Pass { | ||
public: | ||
using Pass::CanScheduleOn; | ||
using Pass::Pass; | ||
using Pass::Run; | ||
}; | ||
} // namespace | ||
PYBIND11_MODULE(pass_python, m) { | ||
py::class_<PassInfo>(m, "PassInfo") | ||
.def(py::init<const std::string&, int, const std::vector<std::string>&>()) | ||
.def_readwrite("name", &PassInfo::name, "...") | ||
.def_readwrite("opt_level", &PassInfo::opt_level, "...") | ||
.def_readwrite("dependents", &PassInfo::dependents, "..."); | ||
|
||
py::class_<Pass, PassTrampoline>(m, "Pass") | ||
.def(py::init<const std::string&, int, const std::vector<std::string>&>()) | ||
.def("can_schedule_on", &PassPublicist::CanScheduleOn, "...") | ||
.def("run", &PassPublicist::Run, "...") | ||
.def("get_pass_info", &Pass::GetPassInfo, "..."); | ||
|
||
py::class_<PassManager>(m, "PassManager") | ||
.def(py::init<mlir::MLIRContext*, int>()) | ||
.def("run", &PassManager::Run) | ||
|
||
// pybind not support `RetType func(unique_ptr)` signature. | ||
// https://pybind11.readthedocs.io/en/stable/advanced/smart_ptrs.html#std-unique-ptr | ||
.def("add_pass", [](PassManager& pm, Pass* pass) { | ||
auto cp = pass->Clone(); | ||
pm.addPass(std::move(cp)); | ||
}); | ||
} | ||
} // namespace infra |