Skip to content

[Comb] Fix more recursive mux folders #8756

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

Merged
merged 3 commits into from
Jul 25, 2025

Conversation

TaoBi22
Copy link
Contributor

@TaoBi22 TaoBi22 commented Jul 21, 2025

This catches two more cases where muxes that have themselves as an input can cause a recursive folder in comb canonicalization, both triggered by the added integration test. Closes #8754

@TaoBi22 TaoBi22 requested a review from darthscsi as a code owner July 21, 2025 17:13
Copy link
Contributor

@fabianschuiki fabianschuiki left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks for fixing this 🙌

Copy link
Contributor

@fzi-hielscher fzi-hielscher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if it really makes sense to play whack-a-mole with this kind of issues, or if we shouldn't just bail out of canonicalize immediately when the op is trivially cyclic. Does anyone actually benefit from optimizations over cyclic comb expressions?

How about adding a more general helper function?

// Returns true if the op is in `comb` and if any of its operands
// is identical to any of its results.
static bool isTriviallyCyclicCombOp(Operation* op) {
  if (!op || !isa<comb::CombDialect>(op->getDialect()))
    return false;
  for (auto res: op->getResults()) {
    if (llvm::any_of(op->getOperands(), [res](auto operand){
      return operand == res;
    }))
      return true;
  }
  return false;
}

(Not tested!)
I'd expect this to come in handy at various places.

@TaoBi22
Copy link
Contributor Author

TaoBi22 commented Jul 25, 2025

I'm wondering if it really makes sense to play whack-a-mole with this kind of issues, or if we shouldn't just bail out of canonicalize immediately when the op is trivially cyclic. Does anyone actually benefit from optimizations over cyclic comb expressions?

How about adding a more general helper function?

// Returns true if the op is in `comb` and if any of its operands
// is identical to any of its results.
static bool isTriviallyCyclicCombOp(Operation* op) {
  if (!op || !isa<comb::CombDialect>(op->getDialect()))
    return false;
  for (auto res: op->getResults()) {
    if (llvm::any_of(op->getOperands(), [res](auto operand){
      return operand == res;
    }))
      return true;
  }
  return false;
}

(Not tested!) I'd expect this to come in handy at various places.

I agree that I think in general bailing out of canonicalization if the op is trivially cyclic probably makes sense and that having such a helper would be useful, but worth noting that these cases don't always occur when the root operation is trivially cyclic - in this case it's another operation that gets caught up in the fold. I guess maybe the answer is to call this on any op involved in a comb fold, but that's probably a topic for a later discussion (I can't make the next one, but maybe an ODM?). In the meantime I'm going to merge this just so we have the bug covered

Co-authored-by: Fabian Schuiki <fabian@schuiki.ch>
@TaoBi22 TaoBi22 merged commit 82a8af4 into main Jul 25, 2025
7 checks passed
@TaoBi22 TaoBi22 deleted the TaoBi22/fix-more-recursive-mux-folders branch July 25, 2025 14:57
TaoBi22 added a commit to TaoBi22/circt that referenced this pull request Jul 25, 2025
Co-authored-by: Fabian Schuiki <fabian@schuiki.ch>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Firtool] Conversion of reduced Rocket HW IR to Verilog doesn't terminate or segfaults
3 participants