Skip to content

Commit

Permalink
Concat canonicalizer: Only do a single pass
Browse files Browse the repository at this point in the history
  • Loading branch information
fzi-hielscher committed Jun 21, 2024
1 parent 850c804 commit 36e76dc
Showing 1 changed file with 6 additions and 19 deletions.
25 changes: 6 additions & 19 deletions lib/Dialect/Sim/SimOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,29 +216,13 @@ LogicalResult FormatStringConcatOp::canonicalize(FormatStringConcatOp op,
if (op.getNumOperands() < 2)
return failure(); // Should be handled by the folder

// Check if there are adjacent literals we can merge or empty literals to
// remove
bool canBeSimplified = false;
bool prevIsLit = false;
for (auto oper : op.getOperands())
if (auto litOp = dyn_cast_or_null<FormatLitOp>(oper.getDefiningOp())) {
if (prevIsLit || litOp.getLiteral().empty()) {
canBeSimplified = true;
break;
}
prevIsLit = true;
} else {
prevIsLit = false;
}

if (!canBeSimplified)
return failure();

auto fmtStrType = FormatStringType::get(op.getContext());

// Simplify literal operands
// Check if there are adjacent literals we can merge or empty literals to
// remove
SmallVector<StringRef> litSequence;
SmallVector<Value> newOperands;
newOperands.reserve(op.getNumOperands());
FormatLitOp prevLitOp;

for (auto operand : op.getOperands()) {
Expand Down Expand Up @@ -279,6 +263,9 @@ LogicalResult FormatStringConcatOp::canonicalize(FormatStringConcatOp op,
}
}

if (newOperands.size() == op.getNumOperands())
return failure(); // Nothing changed

if (newOperands.empty())
rewriter.replaceOpWithNewOp<FormatLitOp>(op, fmtStrType,
rewriter.getStringAttr(""));
Expand Down

0 comments on commit 36e76dc

Please sign in to comment.