Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/compile: constant indexing to private array should be folded #43403

Open
egonelbre opened this issue Dec 28, 2020 · 4 comments
Open

cmd/compile: constant indexing to private array should be folded #43403

egonelbre opened this issue Dec 28, 2020 · 4 comments

Comments

@egonelbre
Copy link
Contributor

@egonelbre egonelbre commented Dec 28, 2020

Lookup tables are quite common, as in https://golang.org/src/math/bits/bits_tables.go. However none of functions that rely on those tables can be constant folded.

Simplest example is https://go.godbolt.org/z/MhMTqb which compiles to:

movblzx math/bits.rev8tab+12(SB), AX
movb    AL, "".~r0+8(SP)

The rev8tab only contains indexing to that table, so it should be possible to prove that the array is not being modified. Of course that only holds, when assignment, slicing or addressing operator is not used.

If such table is replaced with a string, then the constant folding does work. However, that seems like an ugly hack to have.

Related Issues:

@ALTree ALTree added the Performance label Dec 28, 2020
@ALTree ALTree added this to the Unplanned milestone Dec 28, 2020
@josharian
Copy link
Contributor

@josharian josharian commented Dec 29, 2020

If anyone wants to work on this, one way to approach it would be to mark the array symbol as readonly in the pre-SSA phase of the compiler (if you can prove it is in fact readonly). Then SSA optimizations for loading from readonly memory should kick in.

@egonelbre
Copy link
Contributor Author

@egonelbre egonelbre commented Dec 29, 2020

@mdempsky mentioned that two other concerns are assembly code and //go:linkname.

@egonelbre
Copy link
Contributor Author

@egonelbre egonelbre commented Dec 29, 2020

I also realized there's a way to declare an array or slice, such that it's guaranteed to be read-only:

func lut() [256]byte { return [256]byte{1,2,3,4} }
@gopherbot
Copy link

@gopherbot gopherbot commented Dec 29, 2020

Change https://golang.org/cl/280645 mentions this issue: math/bits: folded reverse tables by using const string

@toothrot toothrot added the NeedsFix label Jan 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
5 participants
You can’t perform that action at this time.