Skip to content

[Comb] Fix assertion failure in extractConcatToConcatExtract pattern (Fixes #9573)#9598

Draft
m2kar wants to merge 1 commit intollvm:mainfrom
m2kar:fix-9573
Draft

[Comb] Fix assertion failure in extractConcatToConcatExtract pattern (Fixes #9573)#9598
m2kar wants to merge 1 commit intollvm:mainfrom
m2kar:fix-9573

Conversation

@m2kar
Copy link
Contributor

@m2kar m2kar commented Feb 3, 2026

Summary

Fixes #9573

The extractConcatToConcatExtract canonicalization pattern crashed with an assertion failure when attempting to replace an ExtractOp with a single value using replaceOpAndCopyNamehint().

Root Cause: When reverseConcatArgs.size() == 1, the code created a new ExtractOp and immediately tried to replace the original op with it. This led to an inconsistent IR state because replaceOpAndCopyNamehint() calls rewriter.replaceOp() which expects the operation to be in a valid state for erasure.

Fix: Remove the special case for single-element results and always use replaceOpWithNewOpAndCopyNamehint<ConcatOp>, which performs atomic operation creation and replacement. Single-operand ConcatOps are automatically optimized away by the ConcatOp folder (line 1726-1727 returns getOperand(0) for single operand).

Test plan

  • Existing test extractCatOnSinglePartialElement in test/Dialect/Comb/canonicalization.mlir:460-472 covers the crash scenario
  • All Comb dialect tests pass
  • All HW dialect tests pass

Fixes llvm#9573

The extractConcatToConcatExtract canonicalization pattern crashed when
attempting to replace an ExtractOp with a single value using
replaceOpAndCopyNamehint(). The issue occurred because the pattern
created a new ExtractOp and immediately tried to replace the original
op with it, leading to an inconsistent IR state during erasure.

This fix removes the special case for single-element results and always
uses replaceOpWithNewOpAndCopyNamehint<ConcatOp>, which performs atomic
operation creation and replacement. Single-operand ConcatOps are
automatically optimized away by the ConcatOp folder.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@m2kar m2kar requested a review from darthscsi as a code owner February 3, 2026 06:21
@m2kar m2kar marked this pull request as draft February 3, 2026 06:23
@m2kar m2kar changed the title [Comb] Fix assertion failure in extractConcatToConcatExtract pattern [Comb] Fix assertion failure in extractConcatToConcatExtract pattern (Fixes #9573) Feb 3, 2026
Copy link
Member

@uenoku uenoku left a comment

Choose a reason for hiding this comment

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

Could you add a test? I'm not certain why first one causes an issue.

@m2kar
Copy link
Contributor Author

m2kar commented Feb 4, 2026

Could you add a test? I'm not certain why first one causes an issue.

Here is a test case. Here are details you should look at: #9573.

module test_module(
  input  logic [1:0] in,
  output logic [3:0] out
);

  // Continuous assignments to output bits
  assign out[0] = in[0] ^ in[1];
  assign out[3] = 1'h0;

  // Combinational always block with implicit sensitivity list
  always @* begin
    out[1] = in[0] & in[1];
    out[2] = in[0] | in[1];
  end

endmodule

@m2kar
Copy link
Contributor Author

m2kar commented Feb 4, 2026

I must test it completely and make sure that all the issues have been resolved. This PR is therefore in draft status until the check is approved.

@uenoku
Copy link
Member

uenoku commented Feb 4, 2026

Sorry I meant to add a test to test/Dialect/Comb/canonicalization.mlir. You can extract IR before the crashing canonicalizer with --mlir-print-ir-after-all.

@m2kar
Copy link
Contributor Author

m2kar commented Feb 5, 2026

Sorry I meant to add a test to test/Dialect/Comb/canonicalization.mlir. You can extract IR before the crashing canonicalizer with --mlir-print-ir-after-all.

OK, I will add it.

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.

[Comb] Assertion failure in Canonicalizer with extractConcatToConcatExtract pattern

2 participants