Skip to content

Commit

Permalink
opt_lut: Narrow LUTs with constant inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
povik committed Oct 12, 2023
1 parent 6b8203f commit c8ea2fa
Showing 1 changed file with 39 additions and 2 deletions.
41 changes: 39 additions & 2 deletions passes/opt/opt_lut.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ struct OptLutWorker

int eliminated_count = 0, combined_count = 0;

bool evaluate_lut(RTLIL::Cell *lut, dict<SigBit, bool> inputs)
State evaluate_lut_x(RTLIL::Cell *lut, dict<SigBit, bool> inputs)
{
SigSpec lut_input = sigmap(lut->getPort(ID::A));
int lut_width = lut->getParam(ID::WIDTH).as_int();
Expand All @@ -64,7 +64,12 @@ struct OptLutWorker
}
}

return lut_table.extract(lut_index).as_bool();
return lut_table.bits[lut_index];
}

bool evaluate_lut(RTLIL::Cell *lut, dict<SigBit, bool> inputs)
{
return evaluate_lut_x(lut, inputs) != State::S0;
}

void show_stats_by_arity()
Expand Down Expand Up @@ -512,6 +517,38 @@ struct OptLutWorker
}
}
show_stats_by_arity();

log("\n");
log("Narrowing LUTs.\n");
worklist = luts;
while (worklist.size())
{
auto lut = worklist.pop();
SigSpec lut_input = sigmap(lut->getPort(ID::A));

SigSpec lut_new_input = lut_input;
lut_new_input.remove_const();

if (lut_new_input.size() == lut_input.size())
continue;

log_debug("Found to-be-narrowed cell %s.%s.\n", log_id(module), log_id(lut));

int lut_width = lut_new_input.size();

RTLIL::Const lut_new_table(State::Sx, 1 << lut_width);
for (int eval = 0; eval < 1 << lut_width; eval++)
{
dict<SigBit, bool> eval_inputs;
for (size_t i = 0; i < (size_t) lut_new_input.size(); i++)
eval_inputs[lut_new_input[i]] = (eval >> i) & 1;
lut_new_table.bits[eval] = evaluate_lut_x(lut, eval_inputs);
}

lut->setPort(ID::A, lut_new_input);
lut->setParam(ID::WIDTH, lut_width);
lut->setParam(ID::LUT, lut_new_table);
}
}
};

Expand Down

0 comments on commit c8ea2fa

Please sign in to comment.