Skip to content

Conversation

@ggreif
Copy link
Contributor

@ggreif ggreif commented Dec 5, 2025

Fixes #5555.

TODO

  • or (|) — DONE: a26f4b3
  • and could be supported too, is it worth it?
  • other obvious identities as listed by @christoph-dfinity, below

@github-actions
Copy link
Contributor

github-actions bot commented Dec 5, 2025

Comparing from 9c02140 to fb400c3:
In terms of gas, 1 tests improved and the mean change is -0.0%.
In terms of size, 1 tests improved and the mean change is -0.0%.

@ggreif ggreif self-assigned this Dec 5, 2025
Copy link
Contributor

@christoph-dfinity christoph-dfinity left a comment

Choose a reason for hiding this comment

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

Why do this during desugaring as opposed to the peephole optimizations we do in instrList.ml? That way we'd probably also catch things like x += 0, ....

What about other identities/constant exprs? Is there a line we want to draw somewhere?

x << 0 = x
x >> 0 = x
x | 0 = x
x | x = x
x & 0 = 0
x & x = x
x ^ x = 0
...

@ggreif
Copy link
Contributor Author

ggreif commented Dec 8, 2025

I am totally aware that we'll have to draw a line somewhere, short of including an algebraic solver/simplifier we can stop anywhere. This PR is not done yet by far. I am looking into a strange problem (bug?) where 0 + isn't being optimised the same way as + 0 even with my (not checked in yet) symmetric pattern. Let's be patient. I have to understand the disconnect first.

@christoph-dfinity
Copy link
Contributor

Got it 👍 Sorry for jumping the gun.

@ggreif
Copy link
Contributor Author

ggreif commented Dec 8, 2025

Got it 👍 Sorry for jumping the gun.

No worries, feedback is always welcome! The right spot probably is to have a more powerful optimiser as an IR-simplifier pass, but we can do that later. The peephole optimiser is less suited for spotting Int/Nat operations as those tend to go through bottleneck calls.

@ggreif ggreif marked this pull request as ready for review January 7, 2026 15:06
@ggreif ggreif requested a review from a team as a code owner January 7, 2026 15:06
shift

if grep -q "^//SKIP $ext$" $(basename $file); then return 1; fi
local FILTER_LINE=$(grep -E "^//FILTER $ext [A-Za-z0-9 -]*$" $(basename $file) | cut -d' ' -f3-)
Copy link
Contributor

Choose a reason for hiding this comment

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

So what are these changes doing, and do we need to keep them?

| S.LitE l -> I.LitE (lit !l)
| S.UnE (ot, o, e) ->
I.PrimE (I.UnPrim (!ot, o), [exp e])
| S.BinE (_ot, e1, op, e2) when neutral (Either.Left op) e1 ->
Copy link
Contributor

Choose a reason for hiding this comment

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

I seems a bit strange to do this in desugaring rather than a separe IR pass, but I guess one could easily move this logic to another pass in future.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, but out also an IR pass will burden everything with complete traversal over the full tree, while this way is very selective. But yes, we can move this out if we want some more comprehensive solution.

//SKIP run
//SKIP run-ir
//SKIP run-low
//FILTER comp tail -n 27
Copy link
Contributor

Choose a reason for hiding this comment

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

Ok, I guess its this.

Copy link
Contributor

@crusso crusso left a comment

Choose a reason for hiding this comment

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

Maybe we should remove the run.sh hacks. It's complicated enough as is.

@ggreif
Copy link
Contributor Author

ggreif commented Jan 13, 2026

Maybe we should remove the run.sh hacks. It's complicated enough as is.

The problem is that only the tail of the IR dump is relevant for testing, and the front part is huge and if we always compare that too the brittleness of changes (e.g. prelude additions) will surface. That's why I added this shell filtering. I considered FileCheck testing, but that is too late and quite some harder (and those expressions may vanish in the optimiser).

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.

moc not optimising i+0

4 participants