You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
cmd/compile: possible confusion over whether phis are always the first values in a block #20178
At some point in the distant past, it was (almost) always the case that OpPhi values, if present, were the first values in the block. And some passes appear to assume that. Here are two comments from the deadstore pass:
for_, v:=rangeb.Values {
ifv.Op==OpPhi {
// Ignore phis - they will always be first and can't be eliminatedcontinue
}
/* ... */// walk to previous storeifv.Op==OpPhi {
continue// At start of block. Move on to next block.
}
But it is definitely no longer the case that all phis are the at the beginning of the block. Among other reasons, the nilcheck and writebarrier passes call storeOrder, which re-orders values in a way that would move some phis away from the beginning.
I tried to test this by adding code that randomizes value order before all passes prior to scheduling and running toolstash, but register selection changes made that unhelpful before I could detect anything substantive. Sample:
decide if/when we want to assume that OpPhis are always first, and if we ever do, add an ssa internal check for it
fix up any code that depends on faulty assumptions
make the schedule pass more fully deterministic
do some randomization-based testing to make sure that any assumptions about value order are safe/correct (perhaps always when ssa/check is on, somehow?)
@josharian you are right that storeOrder doesn't always keep Phis at the beginning. And there are other passes that doesn't keep this. For example, decompose may decompose a Phi into multiple Phis and add them to the end. Maybe we should make code don't depend on this. Many passes indeed don't need this.
And in passes where this proves complicated or risky or expensive, we can just fix it up at the beginning. That's a fairly cheap O(1) single pass through the slice.
A visual inspection of the ssa passes suggests that the only two passes that depend on phis being first are deadstore and the various rule-based rewrites (see e.g. comments in canMergeLoad).
Before values are ordered in a block, phis will not necessarily be first. After they are ordered (in the schedule pass), phis must be first. I mailed CL 45018 to check the latter in check.go.
CL 33909 (randomize the order of b.Values before the schedule pass) will not get in for 1.9. I intend to get that in for 1.10. It will help make sure that before the schedule pass, we don't depend on phis being first.
I looked closely at both deadstore and canMergeLoad - they don't depend on phis being first in b.Values, just being logically the first store in the store chain for a block. That should still be the case, even if they are out of order in b.Values.
Activity
cherrymui commentedon Apr 29, 2017
I thought storeOrder doesn't move Phi from the beginning. I'll check. (EDIT: this is not true)
Do you have a testcase that demonstrates it?
cherrymui commentedon Apr 29, 2017
Keith has a CL https://go-review.googlesource.com/c/33909/ that does the value order randomization.
cherrymui commentedon Apr 30, 2017
@josharian you are right that storeOrder doesn't always keep Phis at the beginning. And there are other passes that doesn't keep this. For example, decompose may decompose a Phi into multiple Phis and add them to the end. Maybe we should make code don't depend on this. Many passes indeed don't need this.
josharian commentedon Apr 30, 2017
SGTM
josharian commentedon Apr 30, 2017
And in passes where this proves complicated or risky or expensive, we can just fix it up at the beginning. That's a fairly cheap O(1) single pass through the slice.
dr2chase commentedon May 2, 2017
The loop-unroller also appends new phis to the per-block value slices.
cmd/compile: sort phis first for some SSA passes
josharian commentedon May 10, 2017
A visual inspection of the ssa passes suggests that the only two passes that depend on phis being first are deadstore and the various rule-based rewrites (see e.g. comments in canMergeLoad).
gopherbot commentedon May 17, 2017
CL https://golang.org/cl/43570 mentions this issue.
cmd/compile: fuse from end to beginning
randall77 commentedon Jun 6, 2017
Before values are ordered in a block, phis will not necessarily be first. After they are ordered (in the schedule pass), phis must be first. I mailed CL 45018 to check the latter in check.go.
CL 33909 (randomize the order of b.Values before the schedule pass) will not get in for 1.9. I intend to get that in for 1.10. It will help make sure that before the schedule pass, we don't depend on phis being first.
I looked closely at both deadstore and canMergeLoad - they don't depend on phis being first in b.Values, just being logically the first store in the store chain for a block. That should still be the case, even if they are out of order in b.Values.
gopherbot commentedon Jun 6, 2017
CL https://golang.org/cl/45018 mentions this issue.
cmd/compile: check that phis are always first after scheduling
2 remaining items
ianlancetaylor commentedon Dec 6, 2017
@randall77 This issue is marked 1.10. Should it be moved to 1.11, or Unplanned?
randall77 commentedon Dec 6, 2017
I will punt to 1.11. I have a CL that mostly, but not quite, works.
dr2chase commentedon Jun 26, 2018
1.12 now?
gopherbot commentedon Nov 27, 2018
Change https://golang.org/cl/151517 mentions this issue:
cmd/compile: order nil checks by source positiongopherbot commentedon Nov 27, 2018
Change https://golang.org/cl/33909 mentions this issue:
cmd/compile: randomize value order in block for testingcmd/compile: order nil checks by source position