Incorrect output when the order of Phi instructions changes #24

Closed
opened 2025-08-26 18:19:24 +00:00 by ed · 5 comments
Owner

This writes "012340" instead of "012345":

import io

main() {
	a := 1
	b := 2
	c := 3
	d := 4
	e := 5
	f := 6

	if true {
		a -= 1
		b -= 1
		c -= 1
		d -= 1
		e -= 1
		f -= 1
	}

	io.write(a)
	io.write(b)
	io.write(c)
	io.write(d)
	io.write(e)
	io.write(f)
}

The output is correct when the if statement does not exist and Phi instructions aren't generated.

This writes "012340" instead of "012345": ``` import io main() { a := 1 b := 2 c := 3 d := 4 e := 5 f := 6 if true { a -= 1 b -= 1 c -= 1 d -= 1 e -= 1 f -= 1 } io.write(a) io.write(b) io.write(c) io.write(d) io.write(e) io.write(f) } ``` The output is correct when the if statement does not exist and Phi instructions aren't generated.
ed self-assigned this 2025-08-26 18:19:24 +00:00
ed changed title from Incorrect register values with 6 simultaneous Phi instructions to Incorrect output when the order of Phi instructions changes 2025-08-27 12:27:43 +00:00
Author
Owner

Minimal example:

import io

main() {
	a := 1
	b := 2

	if true {
		a -= 1
		b -= 1
	}

	io.write(a)
	io.write(b)
}

Expected: 01
Encountered: 01 or 00 depending on the phi insertion order.

Minimal example: ``` import io main() { a := 1 b := 2 if true { a -= 1 b -= 1 } io.write(a) io.write(b) } ``` Expected: `01` Encountered: `01` or `00` depending on the phi insertion order.
Author
Owner

The root cause is destination and operand registers being equal for sub.
This is only supported on arm64 but impossible on x86-64.

The root cause is destination and operand registers being equal for `sub`. This is only supported on arm64 but impossible on x86-64.
Author
Owner

Partially fixed in cli/q@c53418bbdf.
The remaining work is to reorder the phi moves in such a way that registers don't overlap.

Partially fixed in https://git.urbach.dev/cli/q/commit/c53418bbdf328b5ba4f00bb9bd9f5936feaf5fdc. The remaining work is to reorder the phi moves in such a way that registers don't overlap.
Author
Owner

Another partial fix in cli/q@8b5e4d9c82.
Now the live values of all phis need to be equalized.

Another partial fix in https://git.urbach.dev/cli/q/commit/8b5e4d9c824d4ce80446e124b2656da2306e2959. Now the live values of all phis need to be equalized.
Author
Owner

Fixed in cli/q@47994041be.

Fixed in https://git.urbach.dev/cli/q/commit/47994041bead85a92d540f4063e95f96b83ccfb0.
ed closed this issue 2025-08-28 12:20:17 +00:00
ed added spent time 2025-08-29 07:45:42 +00:00
16 hours
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Total time spent: 16 hours
ed
16 hours
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
cli/q#24
No description provided.