Skip to content

Commit 673b024

Browse files
committed
proc_dff: refactor shrinking logic in optimizers
1 parent b690130 commit 673b024

File tree

1 file changed

+24
-28
lines changed

1 file changed

+24
-28
lines changed

passes/proc/proc_dff.cc

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <stdlib.h>
2626
#include <stdio.h>
2727
#include <algorithm>
28+
#include <type_traits>
2829

2930
USING_YOSYS_NAMESPACE
3031
PRIVATE_NAMESPACE_BEGIN
@@ -163,13 +164,7 @@ class Dff {
163164
return async_rules[i].value[bit] == async_rules[i + 1].value[bit];
164165
};
165166

166-
const bool lsb_optimizable = bit_optimizable(0);
167-
168-
size_t new_size;
169-
for (new_size = 1; new_size < size(); new_size++)
170-
if (bit_optimizable(new_size) != lsb_optimizable)
171-
break;
172-
resize(new_size);
167+
const bool lsb_optimizable = shrink_while_matching_values(bit_optimizable);
173168

174169
if (!lsb_optimizable) {
175170
i++;
@@ -212,16 +207,7 @@ class Dff {
212207
return foldable;
213208
};
214209

215-
// Work out how many bits from the lsb can be folded into the same
216-
// number of rules
217-
const size_t lsb_foldable_rules = foldable_rules(0);
218-
219-
size_t new_size;
220-
for (new_size = 1; new_size < size(); new_size++)
221-
if (foldable_rules(new_size) != lsb_foldable_rules)
222-
break;
223-
224-
resize(new_size);
210+
const size_t lsb_foldable_rules = shrink_while_matching_values(foldable_rules);
225211

226212
if (lsb_foldable_rules == 0)
227213
return;
@@ -250,17 +236,10 @@ class Dff {
250236
if (async_rules.size() != 1)
251237
return;
252238

253-
const auto& [val, trigger] = async_rules.front();
254-
log_assert(GetSize(val) > 0);
255-
256-
const bool lsb_wire = val[0].is_wire();
257-
258-
size_t new_size;
259-
for (new_size = 1; new_size < size(); new_size++)
260-
if (val[new_size].is_wire() != lsb_wire)
261-
break;
262-
263-
resize(new_size);
239+
const auto bit_is_wire = [&](const size_t i) {
240+
return async_rules.front().value[i].is_wire();
241+
};
242+
shrink_while_matching_values(bit_is_wire);
264243
}
265244

266245
void generate() {
@@ -432,6 +411,23 @@ class Dff {
432411
value = value.extract(0, new_size);
433412
}
434413

414+
// Given some function that maps from an index to a value, this resizes
415+
// the dff to a range starting at the LSB that all return the same value
416+
// from the function as the LSB. This function also returns the value
417+
// calculated for the LSB.
418+
template <typename F>
419+
typename std::result_of<F(size_t)>::type shrink_while_matching_values(F f) {
420+
const auto base_val = f(0);
421+
422+
size_t new_size;
423+
for (new_size = 1; new_size < size(); new_size++)
424+
if (f(new_size) != base_val)
425+
break;
426+
427+
resize(new_size);
428+
return base_val;
429+
}
430+
435431
RTLIL::Process& proc;
436432
RTLIL::Module& mod;
437433

0 commit comments

Comments
 (0)