-
Notifications
You must be signed in to change notification settings - Fork 16
Provide grant_predicate and calculate RecMII #39
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,146 @@ | ||
| #include <deque> | ||
|
|
||
| #include "NeuraDialect/NeuraDialect.h" | ||
| #include "NeuraDialect/NeuraOps.h" | ||
| #include "NeuraDialect/NeuraTypes.h" | ||
| #include "NeuraDialect/NeuraPasses.h" | ||
| #include "mlir/Dialect/Func/IR/FuncOps.h" | ||
| #include "mlir/IR/PatternMatch.h" | ||
| #include "mlir/Pass/Pass.h" | ||
| #include "mlir/Transforms/GreedyPatternRewriteDriver.h" | ||
|
|
||
| using namespace mlir; | ||
|
|
||
| #define GEN_PASS_DEF_MapToAccelerator | ||
| #include "NeuraDialect/NeuraPasses.h.inc" | ||
|
|
||
| namespace { | ||
|
|
||
| /// Represents a recurrence cycle rooted at a reserve operation and ending at a ctrl_mov. | ||
| /// The cycle consists of a sequence of operations and its corresponding length. | ||
| struct RecurrenceCycle { | ||
| SmallVector<Operation *> operations; // Ordered list of operations in the cycle. | ||
| int length = 0; // Number of operations excluding ctrl_mov and reserve_op. | ||
| }; | ||
|
|
||
| // Traverses (backward) the operation graph starting from the given operation | ||
| // towards reserve_value. | ||
| void traverseAlongPath(Operation *op, Value reserve_value, | ||
| std::deque<Operation *> ¤t_path, | ||
| DenseSet<Operation *> &visited_in_path, | ||
| SmallVector<RecurrenceCycle, 4> &collected_paths) { | ||
| if (!op || visited_in_path.contains(op)) | ||
| return; | ||
|
|
||
| visited_in_path.insert(op); | ||
| current_path.push_front(op); | ||
|
|
||
| for (Value operand : op->getOperands()) { | ||
| if (operand == reserve_value) { | ||
| Operation *res_op = reserve_value.getDefiningOp(); | ||
| if (res_op) current_path.push_front(res_op); | ||
|
|
||
| constexpr int kNumExcludedOps = 2; | ||
| collected_paths.push_back(RecurrenceCycle{ | ||
| operations: SmallVector<Operation *>(current_path.begin(), current_path.end()), | ||
| length: static_cast<int>(current_path.size()) - kNumExcludedOps | ||
| }); | ||
|
|
||
| if (res_op) current_path.pop_front(); // Remove reserve before backtracking | ||
| continue; | ||
| } | ||
|
|
||
| if (Operation *def_op = operand.getDefiningOp()) { | ||
| traverseAlongPath(def_op, reserve_value, current_path, visited_in_path, collected_paths); | ||
| } | ||
| } | ||
|
|
||
| current_path.pop_front(); // Backtrack | ||
| visited_in_path.erase(op); // Unmark from path | ||
| } | ||
|
|
||
| /// Collects all recurrence cycles rooted at reserve operations and closed by ctrl_mov. | ||
| /// Each cycle contains the operation sequence and its corresponding length. | ||
| SmallVector<RecurrenceCycle, 4> collectRecurrenceCycles(Operation *root_op) { | ||
| SmallVector<RecurrenceCycle, 4> recurrence_cycles; | ||
|
|
||
| root_op->walk([&](neura::CtrlMovOp ctrl_mov_op) { | ||
| Value target = ctrl_mov_op.getTarget(); | ||
| auto reserve_op = target.getDefiningOp<neura::ReserveOp>(); | ||
| if (!reserve_op) | ||
| return; | ||
|
|
||
| Value reserve_value = reserve_op.getResult(); | ||
| Value ctrl_mov_from = ctrl_mov_op.getValue(); | ||
|
|
||
| Operation *parent_op = ctrl_mov_from.getDefiningOp(); | ||
| if (!parent_op) | ||
| return; | ||
|
|
||
| std::deque<Operation *> current_path; | ||
| SmallVector<RecurrenceCycle, 4> collected_paths; | ||
| DenseSet<Operation *> visited_in_path; | ||
| traverseAlongPath(parent_op, reserve_value, current_path, visited_in_path, collected_paths); | ||
|
|
||
| for (auto &cycle : collected_paths) { | ||
| cycle.operations.push_back(ctrl_mov_op); | ||
| ++cycle.length; | ||
| recurrence_cycles.push_back(std::move(cycle)); | ||
| } | ||
| }); | ||
|
|
||
| return recurrence_cycles; | ||
| } | ||
|
|
||
| struct MapToAcceleratorPass | ||
| : public PassWrapper<MapToAcceleratorPass, OperationPass<ModuleOp>> { | ||
| MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(MapToAcceleratorPass) | ||
|
|
||
| StringRef getArgument() const override { return "map-to-accelerator"; } | ||
| StringRef getDescription() const override { | ||
| return "Maps IR to the target accelerator."; | ||
| } | ||
|
|
||
| void getDependentDialects(DialectRegistry ®istry) const override { | ||
| registry.insert<mlir::neura::NeuraDialect>(); | ||
| } | ||
|
|
||
| void runOnOperation() override { | ||
| ModuleOp module = getOperation(); | ||
|
|
||
| module.walk([&](func::FuncOp func) { | ||
| // Skips functions not targeting the neura accelerator. | ||
| auto accel_attr = func->getAttrOfType<StringAttr>("accelerator"); | ||
| if (!accel_attr || accel_attr.getValue() != "neura") | ||
| return; | ||
|
|
||
| // Collects and reports recurrence cycles found in the function. | ||
| auto recurrence_cycles = collectRecurrenceCycles(func); | ||
| RecurrenceCycle *longest = nullptr; | ||
| for (auto &cycle : recurrence_cycles) { | ||
| if (!longest || cycle.length > longest->length) | ||
| longest = &cycle; | ||
| } | ||
|
|
||
| if (longest) { | ||
| llvm::errs() << "[MapToAcceleratorPass] Longest recurrence cycle (length " | ||
| << longest->length << "):\n"; | ||
| for (Operation *op : longest->operations) | ||
| op->print(llvm::errs()), llvm::errs() << "\n"; | ||
| IntegerAttr mii_attr = IntegerAttr::get( | ||
| IntegerType::get(func.getContext(), 32), longest->length); | ||
| func->setAttr("RecMII", mii_attr); | ||
| } | ||
| }); | ||
| } | ||
| }; | ||
|
|
||
| } // namespace | ||
|
|
||
| namespace mlir::neura { | ||
|
|
||
| std::unique_ptr<Pass> createMapToAcceleratorPass() { | ||
| return std::make_unique<MapToAcceleratorPass>(); | ||
| } | ||
|
|
||
| } // namespace mlir::neura | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.