This is an archive of the discontinued LLVM Phabricator instance.

[SCEVExpander] Expand umin_seq using freeze
ClosedPublic

Authored by nikic on May 11 2022, 3:57 AM.

Details

Summary

%x umin_seq %y is currently expanded to %x == 0 ? 0 : umin(%x, %y). This patch changes the expansion to umin(%x, freeze %y) instead (https://alive2.llvm.org/ce/z/wujUhp).

The motivating for this change are the test cases affected by D124910, where the freeze expansion ultimately produces better optimization results. This is largely because (%x umin_seq %y) == %x is a common expansion pattern, which reliably optimizes in freeze representation, but only sometimes with the zero comparison (in particular, if %x == 0 can fold to something else, we generally won't be able to cover reasonable code from this.)

Diff Detail

Event Timeline

nikic created this revision.May 11 2022, 3:57 AM
Herald added a project: Restricted Project. · View Herald TranscriptMay 11 2022, 3:57 AM
Herald added a subscriber: hiraditya. · View Herald Transcript
nikic requested review of this revision.May 11 2022, 3:57 AM
Herald added a project: Restricted Project. · View Herald TranscriptMay 11 2022, 3:57 AM
reames accepted this revision.May 17 2022, 8:05 AM

LGTM.

I don't have any strong opinion as to which form is a better canonical one, and am thus open to trying the alternate and seeing how it works out.

This revision is now accepted and ready to land.May 17 2022, 8:05 AM
This revision was landed with ongoing or failed builds.May 18 2022, 12:54 AM
This revision was automatically updated to reflect the committed changes.
lebedev.ri added a comment.EditedNov 18 2022, 7:58 AM

Hello.

(This is part of my ~6 months backlog
in which i was not really around,
so i'm commenting on these things
as i stumble into them.)

While it is good that this helps optimizations,
there is one thing that is not mentioned at all here: round-tripping.
SCEV does not recognize this new canonical expansion back as umin_seq,
which means subsequent SCEV usages will have less insight into the IR.

I think, this is salvageable, but given that we model freeze as a SCEVUnkown,
the solution will be controversial.