-
Notifications
You must be signed in to change notification settings - Fork 895
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
Adjust keep_hierarchy
behavior
#4706
Open
povik
wants to merge
5
commits into
YosysHQ:main
Choose a base branch
from
povik:keep_hierarchy-adjustalgo
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2425352
keep_hierarchy: Redo hierarchy traversal for `-min_cost`
povik cf79630
keep_hierarchy: Require size information on blackboxes
povik c8fffce
keep_hierarchy: Update messages
povik 426ef53
Document `gate_cost_equivalent`
povik 69a36ae
Add keep_hierarchy test
povik 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 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 |
---|---|---|
|
@@ -23,20 +23,77 @@ | |
USING_YOSYS_NAMESPACE | ||
PRIVATE_NAMESPACE_BEGIN | ||
|
||
struct ThresholdHiearchyKeeping { | ||
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. nit: |
||
Design *design; | ||
CellCosts costs; | ||
dict<Module *, int> done; | ||
pool<Module *> in_progress; | ||
uint64_t threshold; | ||
|
||
ThresholdHiearchyKeeping(Design *design, uint64_t threshold) | ||
: design(design), costs(design), threshold(threshold) {} | ||
|
||
uint64_t visit(RTLIL::Module *module) { | ||
if (module->has_attribute(ID(gate_cost_equivalent))) | ||
return module->attributes[ID(gate_cost_equivalent)].as_int(); | ||
|
||
if (module->get_blackbox_attribute()) | ||
log_error("Missing cost information on instanced blackbox %s\n", log_id(module)); | ||
|
||
if (done.count(module)) | ||
return done.at(module); | ||
|
||
if (in_progress.count(module)) | ||
log_error("Circular hierarchy\n"); | ||
in_progress.insert(module); | ||
|
||
uint64_t size = 0; | ||
module->has_processes_warn(); | ||
|
||
for (auto cell : module->cells()) { | ||
if (!cell->type.isPublic()) { | ||
size += costs.get(cell); | ||
} else { | ||
RTLIL::Module *submodule = design->module(cell->type); | ||
if (!submodule) | ||
log_error("Hierarchy contains unknown module '%s' (instanced as %s in %s)\n", | ||
log_id(cell->type), log_id(cell), log_id(module)); | ||
size += visit(submodule); | ||
} | ||
} | ||
|
||
if (size > threshold) { | ||
log("Keeping %s (estimated size above threshold: %llu > %llu).\n", log_id(module), size, threshold); | ||
module->set_bool_attribute(ID::keep_hierarchy); | ||
size = 0; | ||
} | ||
|
||
in_progress.erase(module); | ||
done[module] = size; | ||
return size; | ||
} | ||
}; | ||
|
||
struct KeepHierarchyPass : public Pass { | ||
KeepHierarchyPass() : Pass("keep_hierarchy", "add the keep_hierarchy attribute") {} | ||
KeepHierarchyPass() : Pass("keep_hierarchy", "selectively add the keep_hierarchy attribute") {} | ||
void help() override | ||
{ | ||
// |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---| | ||
log("\n"); | ||
log(" keep_hierarchy [options]\n"); | ||
log(" keep_hierarchy [options] [selection]\n"); | ||
log("\n"); | ||
log("Add the keep_hierarchy attribute.\n"); | ||
log("\n"); | ||
log(" -min_cost <min_cost>\n"); | ||
log(" only add the attribute to modules estimated to have more\n"); | ||
log(" than <min_cost> gates after simple techmapping. Intended\n"); | ||
log(" for tuning trade-offs between quality and yosys runtime.\n"); | ||
log(" only add the attribute to modules estimated to have more than <min_cost>\n"); | ||
log(" gates after simple techmapping. Intended for tuning trade-offs between\n"); | ||
log(" quality and yosys runtime.\n"); | ||
log("\n"); | ||
log(" When evaluating a module's cost, gates which are within a submodule\n"); | ||
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 seems like modules already labeled keep_hierarchy before this pass would still contribute to their supermodule's sum cost |
||
log(" which is marked with the keep_hierarchy attribute are not counted\n"); | ||
log(" towards the upper module's cost. This applies to both when the attribute\n"); | ||
log(" was added by this command or was pre-existing.\n"); | ||
log("\n"); | ||
} | ||
void execute(std::vector<std::string> args, RTLIL::Design *design) override | ||
{ | ||
|
@@ -54,16 +111,15 @@ struct KeepHierarchyPass : public Pass { | |
} | ||
extra_args(args, argidx, design); | ||
|
||
CellCosts costs(design); | ||
if (min_cost) { | ||
RTLIL::Module *top = design->top_module(); | ||
if (!top) | ||
log_cmd_error("'-min_cost' mode requires a single top module in the design\n"); | ||
|
||
for (auto module : design->selected_modules()) { | ||
if (min_cost) { | ||
unsigned int cost = costs.get(module); | ||
if (cost > min_cost) { | ||
log("Marking %s (module too big: %d > %d).\n", log_id(module), cost, min_cost); | ||
module->set_bool_attribute(ID::keep_hierarchy); | ||
} | ||
} else { | ||
ThresholdHiearchyKeeping worker(design, min_cost); | ||
worker.visit(top); | ||
} else { | ||
for (auto module : design->selected_modules()) { | ||
log("Marking %s.\n", log_id(module)); | ||
module->set_bool_attribute(ID::keep_hierarchy); | ||
} | ||
|
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,53 @@ | ||
read_verilog <<EOF | ||
(* blackbox *) | ||
(* gate_cost_equivalent=150 *) | ||
module macro; | ||
endmodule | ||
|
||
module branch1; | ||
macro inst1(); | ||
macro inst2(); | ||
macro inst3(); | ||
endmodule | ||
|
||
module branch2; | ||
macro inst1(); | ||
macro inst2(); | ||
macro inst3(); | ||
macro inst4(); | ||
endmodule | ||
|
||
// branch3_submod on its own doesn't meet the threshold | ||
module branch3_submod(); | ||
wire [2:0] y; | ||
wire [2:0] a; | ||
wire [2:0] b; | ||
assign y = a * b; | ||
endmodule | ||
|
||
// on the other hand four branch3_submods do | ||
module branch3; | ||
branch3_submod inst1(); | ||
branch3_submod inst2(); | ||
branch3_submod inst3(); | ||
branch3_submod inst4(); | ||
endmodule | ||
|
||
// wrapper should have zero cost when branch3 is marked | ||
// keep_hierarchy | ||
module branch3_wrapper; | ||
branch3 inst(); | ||
endmodule | ||
|
||
module top; | ||
branch1 inst1(); | ||
branch2 inst2(); | ||
branch3_wrapper wrapper(); | ||
endmodule | ||
EOF | ||
|
||
hierarchy -top top | ||
keep_hierarchy -min_cost 500 | ||
select -assert-mod-count 2 A:keep_hierarchy | ||
select -assert-any A:keep_hierarchy branch2 %i | ||
select -assert-any A:keep_hierarchy branch3 %i |
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.
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.
This chapter is "Non-standard or SystemVerilog features for formal verification", shouldn't it go into the preceding one?
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.
Maybe we could direct readers to check the keep_hierarchy help string?