-
Notifications
You must be signed in to change notification settings - Fork 15.3k
[mlir] Add SelectPass
#130409
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
base: main
Are you sure you want to change the base?
[mlir] Add SelectPass
#130409
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| //===- SelectPass.cpp - Select pass code ----------------------------------===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // SelectPass dynamically selects pass pipeline to run based on root op | ||
| // attribute. | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #include "mlir/Transforms/Passes.h" | ||
|
|
||
| #include "mlir/Pass/Pass.h" | ||
| #include "mlir/Pass/PassManager.h" | ||
|
|
||
| namespace mlir { | ||
| #define GEN_PASS_DEF_SELECTPASS | ||
| #include "mlir/Transforms/Passes.h.inc" | ||
| } // namespace mlir | ||
|
|
||
| using namespace mlir; | ||
|
|
||
| namespace { | ||
| struct SelectPass final : public impl::SelectPassBase<SelectPass> { | ||
| using SelectPassBase::SelectPassBase; | ||
|
|
||
| SelectPass( | ||
| std::string name_, std::string selectCondName_, | ||
| ArrayRef<std::pair<StringRef, std::function<void(OpPassManager &)>>> | ||
| populateFuncs) { | ||
| name = std::move(name_); | ||
| selectCondName = std::move(selectCondName_); | ||
|
|
||
| SmallVector<std::string> selectVals; | ||
| SmallVector<std::string> selectPpls; | ||
| selectVals.reserve(populateFuncs.size()); | ||
| selectPpls.reserve(populateFuncs.size()); | ||
| selectPassManagers.reserve(populateFuncs.size()); | ||
| for (auto &&[name, populate] : populateFuncs) { | ||
| selectVals.emplace_back(name); | ||
|
|
||
| auto &pm = selectPassManagers.emplace_back(); | ||
| populate(pm); | ||
|
|
||
| llvm::raw_string_ostream os(selectPpls.emplace_back()); | ||
| pm.printAsTextualPipeline(os); | ||
| } | ||
|
|
||
| selectValues = selectVals; | ||
| selectPipelines = selectPpls; | ||
| } | ||
|
|
||
| LogicalResult initializeOptions( | ||
| StringRef options, | ||
| function_ref<LogicalResult(const Twine &)> errorHandler) override { | ||
| if (failed(SelectPassBase::initializeOptions(options, errorHandler))) | ||
| return failure(); | ||
|
|
||
| if (selectCondName.empty()) | ||
| return errorHandler("select-cond-name is empty"); | ||
|
|
||
| if (selectValues.size() != selectPipelines.size()) | ||
| return errorHandler("values and pipelines size mismatch"); | ||
|
|
||
| selectPassManagers.resize(selectPipelines.size()); | ||
|
|
||
| for (auto &&[i, pipeline] : llvm::enumerate(selectPipelines)) { | ||
| if (failed(parsePassPipeline(pipeline, selectPassManagers[i]))) | ||
| return errorHandler("failed to parse pipeline"); | ||
| } | ||
|
|
||
| return success(); | ||
| } | ||
|
|
||
| LogicalResult initialize(MLIRContext *context) override { | ||
| condAttrName = StringAttr::get(context, selectCondName); | ||
|
|
||
| selectAttrs.reserve(selectAttrs.size()); | ||
| for (StringRef value : selectValues) | ||
| selectAttrs.emplace_back(StringAttr::get(context, value)); | ||
|
|
||
| return success(); | ||
| } | ||
|
|
||
| void getDependentDialects(DialectRegistry ®istry) const override { | ||
| for (const OpPassManager &pipeline : selectPassManagers) | ||
| pipeline.getDependentDialects(registry); | ||
| } | ||
|
|
||
| void runOnOperation() override { | ||
| Operation *op = getOperation(); | ||
| Attribute condAttrValue = op->getAttr(condAttrName); | ||
| if (!condAttrValue) { | ||
| op->emitError("condition attribute not present: ") << condAttrName; | ||
| return signalPassFailure(); | ||
| } | ||
|
|
||
| for (auto &&[value, pm] : | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are probably few enough instances that a linear search would be appropriate but could be revised if needed.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, realistically this pass will only have a few options, so linear search should be fine. |
||
| llvm::zip_equal(selectAttrs, selectPassManagers)) { | ||
| if (value != condAttrValue) | ||
| continue; | ||
|
|
||
| if (failed(runPipeline(pm, op))) | ||
| return signalPassFailure(); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| // TODO: add a default pipeline option. | ||
| op->emitError("unhandled condition value: ") << condAttrValue; | ||
| return signalPassFailure(); | ||
| } | ||
|
|
||
| protected: | ||
| StringRef getName() const override { return name; } | ||
|
|
||
| private: | ||
| StringAttr condAttrName; | ||
| SmallVector<Attribute> selectAttrs; | ||
| SmallVector<OpPassManager> selectPassManagers; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not clear to me how is this handled with respect to serialization? Can you clarify please.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| }; | ||
| } // namespace | ||
|
|
||
| std::unique_ptr<Pass> mlir::createSelectPass( | ||
| std::string name, std::string selectCondName, | ||
| ArrayRef<std::pair<StringRef, std::function<void(OpPassManager &)>>> | ||
| populateFuncs) { | ||
| return std::make_unique<SelectPass>(std::move(name), | ||
| std::move(selectCondName), populateFuncs); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| // RUN: mlir-opt %s -pass-pipeline='builtin.module(gpu.module(select-pass{ \ | ||
| // RUN: name=TestSelectPass \ | ||
| // RUN: select-cond-name=test.attr \ | ||
| // RUN: select-values=rocdl,nvvm \ | ||
| // RUN: select-pipelines=convert-gpu-to-rocdl,convert-gpu-to-nvvm \ | ||
| // RUN: }))' | FileCheck %s | ||
|
|
||
| gpu.module @rocdl_module attributes {test.attr = "rocdl"} { | ||
| // CHECK-LABEL: func @foo() | ||
| // CHECK: rocdl.workitem.id.x | ||
| func.func @foo() -> index { | ||
| %0 = gpu.thread_id x | ||
| return %0 : index | ||
| } | ||
| } | ||
|
|
||
| gpu.module @nvvm_module attributes {test.attr = "nvvm"} { | ||
| // CHECK-LABEL: func @bar() | ||
| // CHECK: nvvm.read.ptx.sreg.tid.x | ||
| func.func @bar() -> index { | ||
| %0 = gpu.thread_id x | ||
| return %0 : index | ||
| } | ||
| } |
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.
OOC, what about introducing a default state, so if none is specified the default is run. So the pass always runs but the attribute is used for refinement. (Could also be a TODO here and follow.up)
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.
Added a TODO for now