Skip to content

Conversation

@Leo-Besancon
Copy link
Collaborator

@Leo-Besancon Leo-Besancon commented Aug 6, 2025

This PR closes #427

Tasks:

  • Implement folding of the following cases
    • BinOp(Const, Const) -> Const (BinOp being Add, Sub, Mul and Exp)
    • Add(x, 0) | Add(0, x) -> x
    • Mul(x, 0) | Mul(0, x) -> 0
    • Mul(x, 1) | Mul(1, x) -> x
    • Exp(0, x) -> 0
    • Exp(x, 0) -> 1 (note: this one is redundant with how we expand Exp node during lowering MIR > AIR, I've only included it here to be exhaustive)
    • Sub(x, 0) -> x
  • Adapt lowering to Air (e.g. relax the Sub(x,y) requirement for Enf nodes)
  • Clean code

@Leo-Besancon Leo-Besancon marked this pull request as ready for review August 7, 2025 13:15
@Leo-Besancon Leo-Besancon force-pushed the add_constant_propagation_mir_pass branch from 40a3c8e to 31ceb57 Compare August 12, 2025 18:45
@Leo-Besancon Leo-Besancon force-pushed the add_constant_propagation_mir_pass branch from 31ceb57 to b76d029 Compare August 18, 2025 05:33
Copy link
Contributor

@bobbinth bobbinth left a comment

Choose a reason for hiding this comment

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

Looks good! Thank you! I left a few questions/comments inline.

Copy link
Contributor

@bobbinth bobbinth left a comment

Choose a reason for hiding this comment

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

Looks good! Thank you! I left a few small comments inline. After these are addressed, we should be good to merge.

One general question: what's the main reason to do constant propagation in MIR rather than in the AlgebraicGraph? It seems like doing it in the AlgebraicGraph could be a bit more straight-forward?

@Leo-Besancon
Copy link
Collaborator Author

Looks good! Thank you! I left a few small comments inline. After these are addressed, we should be good to merge.

One general question: what's the main reason to do constant propagation in MIR rather than in the AlgebraicGraph? It seems like doing it in the AlgebraicGraph could be a bit more straight-forward?

Good point! Initially I just put it there as it was a regression introduced by MIR (expressions previously handled in AST's ConstantPropagation were no longer handled), but it would have been easier in the Air.
But now I think the main reason to keep it here is to ensure that computed indices (#444) are folded before the end of MIR.
This ensures that we do not have to handle vectors in the AIR (or during lowering of MIR to AIR).

@adr1anh adr1anh self-requested a review August 19, 2025 14:43
Copy link
Contributor

@bobbinth bobbinth left a comment

Choose a reason for hiding this comment

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

Looks good! Thank you!

Good point! Initially I just put it there as it was a regression introduced by MIR (expressions previously handled in AST's ConstantPropagation were no longer handled), but it would have been easier in the Air.
But now I think the main reason to keep it here is to ensure that computed indices (#444) are folded before the end of MIR.
This ensures that we do not have to handle vectors in the AIR (or during lowering of MIR to AIR).

Makes sense! Let's still create an issue to investigate moving constant folding into AIR, but maybe keeping a simpler pass for index folding here (not sure it would make sense - but we can discuss that in the issue).

@bobbinth bobbinth merged commit 4841d74 into 0xMiden:next Aug 20, 2025
8 checks passed
@Leo-Besancon
Copy link
Collaborator Author

Looks good! Thank you!

Good point! Initially I just put it there as it was a regression introduced by MIR (expressions previously handled in AST's ConstantPropagation were no longer handled), but it would have been easier in the Air.
But now I think the main reason to keep it here is to ensure that computed indices (#444) are folded before the end of MIR.
This ensures that we do not have to handle vectors in the AIR (or during lowering of MIR to AIR).

Makes sense! Let's still create an issue to investigate moving constant folding into AIR, but maybe keeping a simpler pass for index folding here (not sure it would make sense - but we can discuss that in the issue).

Perfect, here is the issue: #446

huitseeker pushed a commit to huitseeker/air-script that referenced this pull request Aug 22, 2025
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.

Add Constant Propagation pass after MIR passes

3 participants