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
[mono] box+callvirt optimization incorrect when instructions are in different basic blocks #95347
The box+callvirt optimization from #73456 is firing incorrectly when the box and the callvirt are consecutive IL instructions but in different basic blocks and there is another basic block that jumps to the callvirt.
See #95347 (comment) and the next 3 comments for repro and diagnosis
Original issue description below:
Description
Runtime behaviour changes in net8.0.
In net8.0 when running the code under/linked repo in Release mode on Mono (Android, ios, wasm) vs Debug it behaves different.
It also behaves different to release mode of a windows console app.
In net7.0 - both of them worked the same, in both debug and release mode
Run console app in Release mode Should print 'False': False
Run Andrioid app in Release mode Should print same, but instead it prints 'False': true
Optional:
4. Uninstall app from device
5. Run app in Debug mode - now it shows 'False': False (as expected)
Expected behavior
Should work the same as in previous versions.
Debug & Release mode should behave the same with the given code.
Output should be the same in Android, iOS & Windows console app
Actual behavior
Returns different values in Debug vs Release mode & works different on different platforms and previous net versions
Output value on Android & iOS: true
Output value on Windows console app: FALSE
Regression?
This works with previous net7.0 version (latest)
Known Workarounds
nullableObject != null ? nullableObject.ToString() : false.ToString() (in this sample)
or
Downgrade back to net7.0
Configuration
Android - Unsure
iOS - ARM64
Other information
No response
Activity
ghost added
needs-area-labelAn area label is needed to ensure this gets routed to the appropriate area owners
changed the title [-][Android][iOS] Null check then tostring works differently[/-][+][mono] Release mode - Null check then tostring works differently[/+]on Nov 28, 2023
So there's a jump from IL_0008 to IL_0011 with a String value on the stack, but the fallthru from IL_000c to IL_0011 has a Nullable<Boolean> on the stack so if ToString is getting devirtualized based on the fallthru info, it makes sense we'd see True instead of abcd
here I would expect cfg->cbb to be the basic block of the box instruction and next_ip and callvirt_ip are IL_0011 and I would expect that ip_in_bb will be false.
changed the title [-][mono] Release mode - Null check then tostring works differently[/-][+][mono] box+callvirt optimization incorrect when instructions are in different basic blocks[/+]on Nov 30, 2023
ghost added
in-prThere is an active PR which will close this issue when it is merged
Activity
iXyles commentedon Nov 28, 2023
Noticed that this also impacts Blazor webassembly
[-][Android][iOS] Null check then tostring works differently[/-][+][mono] Release mode - Null check then tostring works differently[/+]lambdageek commentedon Nov 29, 2023
I think this is a dup of #94773lambdageek commentedon Nov 29, 2023
Hmm... actually in that case the interpreter was giving the wrong answers. but in this case Debug mode (with interp) is fine, but Release is wrong.
lambdageek commentedon Nov 29, 2023
Confirmed. run this console app:
CoreCLR:
Then set these in the .csproj:
Mono Interp:
Mono JIT:
@vargaz It's kind of similar IL like in #94923
So there's a jump from IL_0008 to IL_0011 with a String value on the stack, but the fallthru from IL_000c to IL_0011 has a
Nullable<Boolean>on the stack so ifToStringis getting devirtualized based on the fallthru info, it makes sense we'd seeTrueinstead ofabcdlambdageek commentedon Nov 29, 2023
It's probably related to #73456
Although I would expect the optimization not to fire:
runtime/src/mono/mono/mini/method-to-ir.c
Lines 9787 to 9800 in 8cb3c61
here I would expect
cfg->cbbto be the basic block of theboxinstruction andnext_ipandcallvirt_ipareIL_0011and I would expect thatip_in_bbwill be false.lambdageek commentedon Nov 30, 2023
/cc @SamMonoRT
lambdageek commentedon Nov 30, 2023
Yea running with
MONO_VERBOSE_METHOD='<Main>$'I seeWhich looks like we used the
cmethod_overridefrom hereruntime/src/mono/mono/mini/method-to-ir.c
Lines 7686 to 7701 in 8cb3c61
So the optimization fired
[-][mono] Release mode - Null check then tostring works differently[/-][+][mono] box+callvirt optimization incorrect when instructions are in different basic blocks[/+][mono][jit] Fix the bblock check in the box+callvirt optimization. (#…
[mono][jit] Fix the bblock check in the box+callvirt optimization.
[mono][jit] Fix the bblock check in the box+callvirt optimization. (#…