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: completely optimize away loop over unused array #28313

Closed
marigonzes opened this issue Oct 22, 2018 · 3 comments

Comments

Projects
None yet
2 participants
@marigonzes
Copy link

commented Oct 22, 2018

What version of Go are you using (go version)?

go version go1.11.1 windows/amd64

What did you do?

I was refactoring some code and after compiling to Go's assembly, I noticed that the code generated was an half optimized version of my code:
https://play.golang.org/p/6E-AGvBCwvy

What did you expect to see?

I expected the compiler to compile away the entire function, producing only a RET instruction.

What did you see instead?

Instead, it generated the following instructions:

0	XORL	AX, AX
1	JMP	3
2	INCQ	AX
3	CMPQ	AX, $1000
4	JLT	2
5	RET

As you can see, the compiler got rid of the array-related operations, but kept the index-incrementing code.

@randall77

This comment has been minimized.

Copy link
Contributor

commented Oct 22, 2018

This we could fix. My only question is how often this case comes up. Do people really write loops that end up having no effect? We don't want to spend compile time on an optimization that will ~never trigger. If it comes for free as part of a larger optimization (some beefed up dead code elimination, say) then it might make sense.

@marigonzes

This comment has been minimized.

Copy link
Author

commented Oct 22, 2018

As I said above, I found this while refactoring some code, so I guess this doesn't appear all that often. In fact, I ended up deleting the code in question, because it was a noop.

But now that I'm looking at it from a different angle, the compiler could be smarter in the following case: https://play.golang.org/p/Ggmd6V8FDd2
The return value is a constant, but currently the compiler generates the code with a loop. But the same question rises: how often does this appear in codebases?

@randall77

This comment has been minimized.

Copy link
Contributor

commented Oct 22, 2018

I'm not inclined to do anything here unless there are some real-world situations where this happens (and matters).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.