-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Redundant null checks in null-coalescing scenarios #111980
Comments
Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch |
Looks like both of your This problem is expected to be fixed by #107499 |
public static ReadOnlySpan<char> Test1(string s)
{
s = s ?? "";
return s;
} cc @AndyAyersMS |
Yep...We can't jump thread since the PHI def in BB03 is also used elsewhere, and we don't know how to revise those uses to be either V03.1 or V03.2
|
Local assertion prop in morph should get this, but it fumbles. We have
In We could add assertions about Also it is wrong to create subrange assertions for |
@AndyAyersMS I've filed a PR to fix it in global assertprop #111985 It shows nice diffs and presumably can find more cases because we also rely on VN with its powerful |
Seems like we should optimize away And this bit seems odd, we should have the copy and non-null assertions here...
|
Ah, no, I am being a bit dumb. BB03 also has BB02 as a pred. So the fix here is more complex. When we see V03 = cns in BB02, we should also generate V03 != null. When we generate the true condition V01 == null at the end of BB01, and we have V03 == V01, we should also generate V03 != null. Then these two will merge at BB03, and we can remove the branch. |
I wonder how costly this sort of assertion flooding ends up being. The root issue is that assertion merging is dumb, it literally requires the same exact assertion (each assertion is given a unique BV bit, and we just intersect BVs). Either we have to generalize the merging logic (which seems expensive) or we manifest all the implied assertions at gen time (which also may be expensive). VN based AP is perhaps a bit less prone to this, but not immune. Probably worth an experiment. |
JIT compiler generates redundant null checks in null-coalescing scenarios or after non-null assignment.
Example 1
In
Test1
, the JIT does not eliminate the dead branch for the null value, even thoughs
cannot be null after the null-coalescing operation. However, inTest2
, the generated code is optimized.Example 2
For
Test3
andTest4
, I expected the JIT to generate a simplereturn true
, since the result of(s ?? "")
can never be null. For example:However, the JIT generates explicit null checks instead:
Source code: godbolt
The text was updated successfully, but these errors were encountered: