Skip to content

Conversation

@Altoids1
Copy link
Contributor

@Altoids1 Altoids1 commented Oct 29, 2022

Summary

Right now, we aren't really consistent on how const-folding is handled. Things are only consistently folded whenever totally necessary, as in constant contexts like switch cases or whatever.

This fixes that by making ALL expressions go through a TryAsConstant pass during creation. Accordingly, this means that we don't really need DMASTSimplifier anymore, so I've removed that whole thing.

Doing this seems to shave off a kilobyte for the stripped TGstation copy I happen to have.

Works on #833 by adding == and != as const-foldable.

Changelog

  • Crunching down of lvalues and rvalues into constants is now just handled by TryAsConstant.
  • Calls to initial() and the == and != operators are now also const-folded.
  • The compiler now more consistently emits warnings when nulls are coerced to 0 in constant contexts.

@Altoids1 Altoids1 force-pushed the simplifier-slaughter branch from 226b2f0 to 2f3ce52 Compare November 1, 2022 22:28
@Altoids1 Altoids1 requested a review from wixoaGit November 1, 2022 22:29
@Altoids1 Altoids1 force-pushed the simplifier-slaughter branch 2 times, most recently from ff7dda0 to 4c7867c Compare November 2, 2022 00:25
@github-actions
Copy link

github-actions bot commented Nov 2, 2022

This pull request has conflicts, please resolve those before we can evaluate the pull request.

@SpaceManiac
Copy link
Contributor

The constant folding is incorrect:

/obj/var/derp = "/obj"
/datum/foldme/var/derp = "/datum/foldme"
/proc/Test()
    var/datum/foldme/D = new /obj
    src << "initial = [initial(D.derp)]"

Correct output is "/obj", this outputs "/datum/foldme".

@Altoids1
Copy link
Contributor Author

Altoids1 commented Nov 2, 2022

I pulled back a bit and reserved the initial() folding for static and const variable access, as those do rely on the type of the variable and not the value it necessarily contains.

@Altoids1 Altoids1 marked this pull request as draft November 16, 2022 02:25
@Altoids1
Copy link
Contributor Author

Marking this as having #896 as a dependency, since some things I want to do with initial() would break parity and should be deferred to user configuration.

@github-actions
Copy link

This pull request has conflicts, please resolve those before we can evaluate the pull request.

ZeWaka added a commit to ZeWaka/OpenDream that referenced this pull request Dec 31, 2022
ZeWaka added a commit to ZeWaka/OpenDream that referenced this pull request Dec 31, 2022
ZeWaka added a commit to ZeWaka/OpenDream that referenced this pull request Dec 31, 2022
wixoaGit pushed a commit that referenced this pull request Jan 1, 2023
* Const evaluation for comparison exprs

* Adds in a comparison unit test

* Equality fixes for nulls, for parity

* vestigial changes

* also leftshiftassign

* Remove duplicate behaviors

* review changes and better testing

* remove bit of code which will be obselete with #885

* this too
Doing this, with other changes, hopefully will significantly improve our ability to const up code.

Just doing this change by itself resulted in a 0.1% smaller JSON output.
Now all const-folding of lvalues and rvalues is orchestrated through TryAsConstant.

We don't yet have simplification for larger structures like conditional blocks and so on.
This is us acting slightly beyond what BYOND can do, but I expect this to be a VERY useful addition to our const analysis, especially for 514 builds.
This could've been a lot of repetitive writing, but luckily I have a galaxy brain. 😀

EDIT: Also includes null + String and String + null.
EDIT: Also includes fixes for member access and global access.
Vars that are typed can actually store ANYTHING. The type is a weak promise that BYOND uses as a guarantee in some contexts (as in statics and constants) but not in others (as in normal variable accesses).

It's still pretty sour to me that we're supporting this, but that is indeed the BYOND behaviour.
Missed a few things during rebase :^)
I *will* be coming back to this, in due time, but for now, this is parity-breaking and can't just be unmodifiable behaviour within OD.
null < "" is FALSE, and null == "" is FALSE, but null <= "" is TRUE.

Every weird dynamic language needs an example where it violates the holy trinity of equality, and apparently, this is DM's!
@Altoids1 Altoids1 force-pushed the simplifier-slaughter branch from 7be88df to af53ccc Compare January 18, 2023 00:39
@Altoids1 Altoids1 marked this pull request as ready for review January 18, 2023 00:40
@github-actions
Copy link

github-actions bot commented Apr 4, 2023

This pull request has conflicts, please resolve those before we can evaluate the pull request.

@wixoaGit
Copy link
Member

This PR is fairly out of date, and I don't see it being picked back up anytime soon. While the removal of DMASTSimplifier and better constant folding is desirable, I think it may be better served as a bytecode-level optimization. Anyone feel free to bring it up with me if they disagree.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants