-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Variable not promoted to register due to unrelated pointer assignment #35495
Comments
@damageboy When you say "bug", are you saying that the JIT is generating incorrect code (that generates an incorrect result, or crashes)? Or just that the JIT is generating sub-optimal code because it isn't using a register for something it could? |
Sorry for the long delay, was away from computer for a couple of days. The code is completely correct, it's sub-optimal due to missed opportunity to promote memory access to register. The "bug" is in the sense that it is completely erratic and dependent on moving the variable declaration later with respect to the pointer assignment I marked in the comment as:
|
Thanks for the update. Given that it's not a functional correctness issue, I'm setting the milestone to "Future". It still would be worth investigating and understanding why this behavior occurs. |
@damageboy has a really good article on the subject where this issue, as well as 2 other minor code quality issues, are described. I'm all for better code quality as everyone benefits from them. A tight loop with 1-2 unnecessary instructions can blow the roof off performance-critical code. I'll cheekily cc @mikedn as well since there has been no word from Andy or Carol yet. |
Sorry for the slow response. I'll try and take a look before too much longer, but my guess is this will end up being an allocator issue, and those can be tricky to address. |
It's not critical for me personally anymore, as I've worked around it, and I admit it's pretty obscure in many senses. Just to be clear, nothing is being blocked by this in any way on my end. |
I've chased the exact cause for this issue for a long long time, and I finally managed to narrow it down to a reproducible issue.
Environment
I'm using .NET 5.0 preview 3:
Buggy JIT behavior
The issue can be clearly seen when comparing the generated ASM between this buggy version:
Where in this order of variable declarations:
Where the
writeRight
variable is declared before thepBase
pointer is initialized, leads to the JIT incorrectly deciding to NOT promotewriteRight
into a register, later within the function:Workaround / Bug
By changing the order of declaration, as I did in my workaround version:
The generated assembly now correctly promotes the
writeRight
variable into a full fledged register without performing needless stack reads and write within my main loop:The full disassembly can be viewed here, in case someone is interested:
https://gist.github.com/damageboy/c09fc710d2b9010fe176645fa526a170
category:cq
theme:register-allocator
skill-level:expert
cost:medium
The text was updated successfully, but these errors were encountered: