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
Loop elimination in presence of invariant expression #35279
area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMIoptimizationuntriagedNew issue has not been triaged by the area owner
The InnerIterationCount200000 field is a read+write static field. There's a comment in the microbenchmarks source that the field should not be marked read-only, but I don't know why it's important for those tests. Whatever. :)
Anyway, I imagine that the JIT could compute the ref once outside the loop, but I think it would still need to dereference it every iteration as part of the loop condition. The reason for this is that the loop itself makes a method call to the unbox helper, and I think most compilers pessimistically assume that such methods could have side effects. One potential side effect could be mutating the stored static value.
There's a comment in the microbenchmarks source that the field should not be marked read-only, but I don't know why it's important for those tests.
I added the comment when I was porting all benchmarks from xunit-performance to BenchmarkDotNet and I was told to not change any of the logic of the ported benchmarks. I think that as of today we should most probably just rewrite these benchmarks without using any loops but validating that they are not optimized by a JIT to a const.
I think most compilers pessimistically assume that such methods could have side effects. One potential side effect could be mutating the stored static value.
I think that is a valid assumption if there are calls to user code. But in this case, it is calling the JIT helper and perhaps it will be known beforehand that we are not updating the stored static value.
The castclass instruction can end up calling user code. Take exception filters as an example:
publicstaticclassProgram{privatestaticint_loopCount=10000;publicstaticvoidMain(){try{DoIt(42);// not a string!}catchwhen(ResetStatic()){}}privatestaticboolResetStatic(){_loopCount=20000;returnfalse;}privatestaticvoidDoIt(objecto){Console.WriteLine($"Start of DoIt: _loopCount = {_loopCount}");// prints 10000try{for(inti=0;i<_loopCount;i++){strings=(string)o;}}finally{Console.WriteLine($"End of DoIt: _loopCount = {_loopCount}");// prints 20000}}}
@kunalspathak which part do you think is invariant ?
We generally assume statics can be changed by other threads so the static read of o and anything derived from it are not loop invariant., and likewise for InnerIterationCount200000. So the helper call properties aren't the primary blockers for loop optimization.
Stores to statics can be observed by other threads so can't be sunk out of the loop. So the only hoistable computations in this loop are the constants and the fetching of orvt's address.
This all changes for readonly statics since they can't be safely modified after their corresponding class constructors have completed.
If we marked o as readonly static then the jit would likely eliminate the type test and call to unbox (at least once the class init had run); if we marked InnerIterationCount200000 as readonly the jit would propagate the constant value into the loop exit test.
To begin with, my impression was that none of the loop index variable's value participate in the expression inside for-loop, the entire expression orvt = (FooORVT)o; could have been hoisted outside and hence no loop is needed. Of course, this is after we ensure that accessing InnerIterationCount200000 doesn't change the static value. In this case, it is returning the primitive value (and not executing any code such as getter), so it should be ok to assume that change its value won't have any impact on value of o that we are trying to assign. But I get that other threads can change o and hence the end effect with and without hoisting it out of the loop will differ.
If we marked o as readonly static then the jit would likely eliminate the type test and call to unbox (at least once the class init had run);
I don't see this happening currently. public readonly static Object o = (Object)new FooORVT();
if we marked InnerIterationCount200000 as readonly the jit would propagate the constant value into the loop exit test.
I don't see this happening as well. public readonly static int InnerIterationCount200000 = 200000;
Edit: It is in the loop exit test, but basically i didn't see any difference after adding readonly for both those variables.
To see the impact of readonly statics you typically need to either look at tier1 code, restructure the code so it forces cctors to run before you jit the method you're looking at, or if you're using PMI for diffs, try passing --cctors (works sometimes) to get class constructors run early.
To see the impact of readonly statics you typically need to either look at tier1 code, restructure the code so it forces cctors to run before you jit the method you're looking at, or if you're using PMI for diffs, try passing --cctors (works sometimes) to get class constructors run early.
@kunalspathak I think everything here is working as expected (modulo hoisting of large constants). If you agree, please close. Otherwise let's pin down where you think we are missing out.
area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMIoptimizationuntriagedNew issue has not been triaged by the area owner
Activity
GrabYourPitchforks commentedon Apr 22, 2020
The InnerIterationCount200000 field is a read+write static field. There's a comment in the microbenchmarks source that the field should not be marked read-only, but I don't know why it's important for those tests. Whatever. :)
Anyway, I imagine that the JIT could compute the ref once outside the loop, but I think it would still need to dereference it every iteration as part of the loop condition. The reason for this is that the loop itself makes a method call to the unbox helper, and I think most compilers pessimistically assume that such methods could have side effects. One potential side effect could be mutating the stored static value.
adamsitnik commentedon Apr 22, 2020
I added the comment when I was porting all benchmarks from
xunit-performanceto BenchmarkDotNet and I was told to not change any of the logic of the ported benchmarks. I think that as of today we should most probably just rewrite these benchmarks without using any loops but validating that they are not optimized by a JIT to a const.In this case, the benchmark could be just:
kunalspathak commentedon Apr 22, 2020
I think that is a valid assumption if there are calls to user code. But in this case, it is calling the JIT helper and perhaps it will be known beforehand that we are not updating the stored static value.
GrabYourPitchforks commentedon Apr 22, 2020
The
castclassinstruction can end up calling user code. Take exception filters as an example:kunalspathak commentedon Apr 22, 2020
Interesting. Thanks for sharing the example.
AndyAyersMS commentedon Apr 27, 2020
@kunalspathak which part do you think is invariant ?
We generally assume statics can be changed by other threads so the static read of
oand anything derived from it are not loop invariant., and likewise forInnerIterationCount200000. So the helper call properties aren't the primary blockers for loop optimization.Stores to statics can be observed by other threads so can't be sunk out of the loop. So the only hoistable computations in this loop are the constants and the fetching of
orvt's address.This all changes for readonly statics since they can't be safely modified after their corresponding class constructors have completed.
If we marked
oas readonly static then the jit would likely eliminate the type test and call to unbox (at least once the class init had run); if we markedInnerIterationCount200000as readonly the jit would propagate the constant value into the loop exit test.kunalspathak commentedon Apr 27, 2020
To begin with, my impression was that none of the loop index variable's value participate in the expression inside for-loop, the entire expression
orvt = (FooORVT)o;could have been hoisted outside and hence no loop is needed. Of course, this is after we ensure that accessingInnerIterationCount200000doesn't change the static value. In this case, it is returning the primitive value (and not executing any code such as getter), so it should be ok to assume that change its value won't have any impact on value ofothat we are trying to assign. But I get that other threads can changeoand hence the end effect with and without hoisting it out of the loop will differ.I don't see this happening currently.
public readonly static Object o = (Object)new FooORVT();I don't see this happening as well.
public readonly static int InnerIterationCount200000 = 200000;Edit: It is in the loop exit test, but basically i didn't see any difference after adding
readonlyfor both those variables.kunalspathak commentedon Apr 27, 2020
Another minor thing I noticed for ARM64 is when we are about to call
CORINFO_HELP_UNBOXwe re-populatex0with same value.AndyAyersMS commentedon Apr 27, 2020
To see the impact of readonly statics you typically need to either look at tier1 code, restructure the code so it forces cctors to run before you jit the method you're looking at, or if you're using PMI for diffs, try passing --cctors (works sometimes) to get class constructors run early.
kunalspathak commentedon Apr 27, 2020
Now I see it. Thanks!
AndyAyersMS commentedon May 6, 2020
@kunalspathak I think everything here is working as expected (modulo hoisting of large constants). If you agree, please close. Otherwise let's pin down where you think we are missing out.
kunalspathak commentedon May 6, 2020
Yes, makes sense.