Skip to content

Refactor FoldConstantPass with generic framework#176

Merged
n0thingNoob merged 12 commits intocoredac:mainfrom
YanzhouTang:render
Oct 25, 2025
Merged

Refactor FoldConstantPass with generic framework#176
n0thingNoob merged 12 commits intocoredac:mainfrom
YanzhouTang:render

Conversation

@YanzhouTang
Copy link
Copy Markdown
Collaborator

Problem

The current --fold-constant pass has a limitation: it can only fold right operand constants for non-commutative operations (such as fsub, fdiv, sub, div), and cannot fold left operand constants.

Since our hardware supports folding any operand (left, right, or multiple operands), we should enhance the pass to support arbitrary operand constant folding.

Solution

This PR refactors the FoldConstantPass with a generic framework that supports folding constants from any operand position.

The pass scans operands from right to left to determine which constants to fold.

When an operand is a constant, it is removed from the operand list and stored as an attribute instead.

Special case: If all operands are constants, the leftmost operand is preserved (since MLIR requires operations to have at least one operand).

@YanzhouTang
Copy link
Copy Markdown
Collaborator Author

PR Summary

1. FoldConstantPass.cpp

Generic Framework: Introduced GenericFuseConstantPattern template with macro DEFINE_BINARY_OP_PATTERN to auto-generate constant folding patterns for binary operations.

Four Special Cases:

  • FuseGEPConstantPattern: Custom handling for operandSegmentSizes with base + multiple indices structure
  • FuseLoadIndexedConstantPattern: Only folds base, never indices (assemblyFormat requirement)
  • FuseStoreIndexedConstantPattern: Handles three-segment operands (value, base, indices) where indices cannot be folded
  • FuseConstantAndGrantPattern: Fuses constants into GrantOnce/GrantAlways operations for predicated dataflow mode

2. TransformCtrlToDataFlowPass.cpp

RPO Optimization: Implemented reverse post-order traversal when flattening blocks to maintain SSA dominance (definitions before uses).

@tancheng
Copy link
Copy Markdown
Contributor

Excellent work @YanzhouTang~!

@n0thingNoob n0thingNoob merged commit 8bbdefd into coredac:main Oct 25, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

3 participants