-
Notifications
You must be signed in to change notification settings - Fork 15
Re-organize mapping util functions #50
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
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| #pragma once | ||
|
|
||
| #include "mlir/IR/Operation.h" | ||
|
|
||
| namespace mlir { | ||
| namespace neura { | ||
|
|
||
| // Represents a recurrence cycle rooted at a reserve operation and closed by ctrl_mov. | ||
| struct RecurrenceCycle { | ||
| SmallVector<Operation *> operations; // Ordered list of operations in the cycle. | ||
| int length = 0; // Number of operations excluding reserve/ctrl_mov. | ||
| }; | ||
|
|
||
| // Accelerator configuration struct. | ||
| struct AcceleratorConfig { | ||
| int num_tiles = 4; // Default to 4 tiles if unspecified. | ||
| }; | ||
|
|
||
| // Collects recurrence cycles rooted at reserve and closed by ctrl_mov. | ||
| SmallVector<RecurrenceCycle, 4> collectRecurrenceCycles(Operation *func_op); | ||
|
|
||
| // Calculates ResMII: ceil(#ops / #tiles). | ||
| int calculateResMii(Operation *func_op, const AcceleratorConfig &config); | ||
|
|
||
| } // namespace neura | ||
| } // namespace mlir |
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,104 @@ | ||
| #include <deque> | ||
|
|
||
| #include "NeuraDialect/mapping/mapping_util.h" | ||
| #include "NeuraDialect/NeuraOps.h" | ||
| #include "mlir/Dialect/Func/IR/FuncOps.h" | ||
| #include "mlir/IR/Operation.h" | ||
|
|
||
| using namespace mlir; | ||
| using namespace mlir::neura; | ||
|
|
||
| namespace { | ||
|
|
||
| // 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(); | ||
| continue; | ||
| } | ||
|
|
||
| if (Operation *def_op = operand.getDefiningOp()) { | ||
| traverseAlongPath(def_op, reserve_value, current_path, visited_in_path, collected_paths); | ||
| } | ||
| } | ||
|
|
||
| current_path.pop_front(); | ||
| visited_in_path.erase(op); | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| SmallVector<RecurrenceCycle, 4> mlir::neura::collectRecurrenceCycles(Operation *func_op) { | ||
| SmallVector<RecurrenceCycle, 4> recurrence_cycles; | ||
|
|
||
| func_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; | ||
| } | ||
|
|
||
| int mlir::neura::calculateResMii(Operation *func_op, const AcceleratorConfig &config) { | ||
| int num_ops = 0; | ||
|
|
||
| // Count all "compute" operations (non-terminators, non-block ops). | ||
| func_op->walk([&](Operation *op) { | ||
| // Skips non-materialized ops. | ||
| if (isa<func::FuncOp>(op) || | ||
| isa<neura::ConstantOp, | ||
| neura::CtrlMovOp, | ||
| neura::ReserveOp, | ||
| neura::ReturnOp>(op)) { | ||
| return; | ||
| } | ||
| ++num_ops; | ||
| }); | ||
|
|
||
| llvm::errs() << "[calculateResMii] Total operations: " << num_ops << "\n"; | ||
|
|
||
| // Avoid divide-by-zero | ||
| int tiles = std::max(1, config.num_tiles); | ||
|
|
||
| return llvm::divideCeil(num_ops, tiles); | ||
| } |
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
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.