Skip to content

Commit

Permalink
[LLHD] Move maxPrimitives check before initializing the 'dontCare' ta…
Browse files Browse the repository at this point in the history
…ble (#8171)

Fixes a crash if the number of primitives is too big
  • Loading branch information
maerhart authored Feb 1, 2025
1 parent 15df83e commit af6761a
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions lib/Dialect/LLHD/Transforms/DesequentializationPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,15 +202,17 @@ namespace {
/// triggers.
class DnfAnalyzer {
public:
DnfAnalyzer(Value value, function_ref<bool(Value)> sampledInPast)
: root(value) {
DnfAnalyzer(Value value) : root(value) {
assert(value.getType().isSignlessInteger(1) &&
"only 1-bit signless integers supported");
}

LogicalResult prepareAnalyzer(function_ref<bool(Value)> sampledInPast,
unsigned maxPrimitives) {
DenseSet<Value> alreadyAdded;

SmallVector<Value> worklist;
worklist.push_back(value);
worklist.push_back(root);

while (!worklist.empty()) {
Value curr = worklist.pop_back_val();
Expand Down Expand Up @@ -256,9 +258,15 @@ class DnfAnalyzer {
llvm::dbgs() << " - Primitive variable: " << val << "\n";
});

if (primitives.size() > maxPrimitives) {
LLVM_DEBUG({ llvm::dbgs() << " Too many primitives, skipping...\n"; });
return failure();
}

this->isClock = SmallVector<bool>(primitives.size(), false);
this->dontCare = SmallVector<APInt>(primitives.size(),
APInt(1ULL << primitives.size(), 0));
return success();
}

/// Note that clocks can be dual edge triggered, but this is not directly
Expand All @@ -268,12 +276,7 @@ class DnfAnalyzer {
LogicalResult
computeTriggers(OpBuilder &builder, Location loc,
function_ref<bool(Value, Value)> sampledFromSameSignal,
SmallVectorImpl<Trigger> &triggers, unsigned maxPrimitives) {
if (primitives.size() > maxPrimitives) {
LLVM_DEBUG({ llvm::dbgs() << " Too many primitives, skipping...\n"; });
return failure();
}

SmallVectorImpl<Trigger> &triggers) {
// Populate the truth table and the result APInt.
computeTruthTable();

Expand Down Expand Up @@ -620,9 +623,10 @@ void DesequentializationPass::runOnProcess(llhd::ProcessOp procOp) const {
return false;
};

if (failed(DnfAnalyzer(op.getEnable(), sampledInPast)
.computeTriggers(builder, loc, sampledFromSameSignal,
triggers, maxPrimitives))) {
DnfAnalyzer analyzer(op.getEnable());
if (failed(analyzer.prepareAnalyzer(sampledInPast, maxPrimitives)) ||
failed(analyzer.computeTriggers(builder, loc, sampledFromSameSignal,
triggers))) {
LLVM_DEBUG({
llvm::dbgs() << " Unable to compute trigger list for drive condition, "
"skipping...\n";
Expand Down

0 comments on commit af6761a

Please sign in to comment.