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
JIT: Optimize simple arithmetic with GT_NEG #13837
I was surprised it's not handled in morph, e.g. -a + b => b - a.
intM1(inta,intb)=>-a+b;// optimize to "b - a"intM2(inta,intb)=>a+-b;// optimize to "a - b"intM3(inta,intb)=>a--b;// optimize to "a + b"intM4(inta,intb)=>-a--b;// optimize to "b - a"
intM5(inta)=>-a/10;// optimize to "a / -10"intM6(inta)=>-(a/10);// optimize to "a / -10"intM7(inta)=>-a*10;// optimize to "a * -10"intM8(inta)=>-(a*10);// optimize to "a * -10"
With the help with Eugene, I was able to get optimized code for M1 to M4 as shown in https://godbolt.org/z/nmZxv8.
Found some issues during jit-diff that does not handle cases where exception is thrown from both operands, and investigation WIP.
That seems the best solution. We will skip transforming NEG when non negated operand is constant. Will check if bitwise shift pattern needs to be revisited where NEG was replaced with SUB pattern.
Simple ADD and SUB arithmetic optimization has been completed with no regression.
We skipped optimizing patterns when non-NEG operand is constant as discussed above.
Many patterns in the jit-diff --frameworks test have constant operands, so code size reduction is not big.
NEG operator is more expensive (e.g., 5-6 CPU cycles) than SUB operator (e.g., 1 cycle) in terms of latency, so it shows better perf scores when optimized.
PMI CodeSize Diffs for System.Private.CoreLib.dll, framework assemblies for default jit
Summary of Code Size diffs:
(Lower is better)
Total bytes of diff: -50 (-0.00% of base)
diff is an improvement.
Top file improvements (bytes):
-13 : System.Private.CoreLib.dasm (-0.00% of base)
-12 : Microsoft.VisualBasic.Core.dasm (-0.00% of base)
-12 : System.Runtime.Numerics.dasm (-0.02% of base)
-8 : System.Private.Xml.dasm (-0.00% of base)
-4 : Microsoft.CodeAnalysis.CSharp.dasm (-0.00% of base)
-1 : Microsoft.CodeAnalysis.dasm (-0.00% of base)
6 total files with Code Size differences (6 improved, 0 regressed), 259 unchanged.
Top method improvements (bytes):
-12 (-2.11% of base) : Microsoft.VisualBasic.Core.dasm - Financial:NPer(double,double,double,double,int):double
-12 (-7.50% of base) : System.Runtime.Numerics.dasm - Complex:op_Division(Complex,Complex):Complex
-6 (-1.46% of base) : System.Private.Xml.dasm - XmlSqlBinaryReader:FillAllowEOF():bool:this
-5 (-0.70% of base) : System.Private.CoreLib.dasm - Number:NumberToFloatingPointBitsSlow(byref,byref,int,int,int):long
-4 (-1.02% of base) : Microsoft.CodeAnalysis.CSharp.dasm - SourceConstructorSymbol:CalculateLocalSyntaxOffset(int,SyntaxTree):int:this
-4 (-1.94% of base) : System.Private.CoreLib.dasm - ScopeTree:AddScopeInfo(byte,int):this
-2 (-1.55% of base) : System.Private.CoreLib.dasm - Grisu3:TryRunCounted(byref,int,Span`1,byref,byref):bool
-2 (-1.05% of base) : System.Private.CoreLib.dasm - Grisu3:TryRunShortest(byref,byref,byref,Span`1,byref,byref):bool
-2 (-0.19% of base) : System.Private.Xml.dasm - FloatingDecimal:AdjustDbl(double):double:this
-1 (-0.20% of base) : Microsoft.CodeAnalysis.dasm - CustomDebugInfoWriter:SerializeReferenceToIteratorClass(String,ArrayBuilder`1)
Top method improvements (percentages):
-12 (-7.50% of base) : System.Runtime.Numerics.dasm - Complex:op_Division(Complex,Complex):Complex
-12 (-2.11% of base) : Microsoft.VisualBasic.Core.dasm - Financial:NPer(double,double,double,double,int):double
-4 (-1.94% of base) : System.Private.CoreLib.dasm - ScopeTree:AddScopeInfo(byte,int):this
-2 (-1.55% of base) : System.Private.CoreLib.dasm - Grisu3:TryRunCounted(byref,int,Span`1,byref,byref):bool
-6 (-1.46% of base) : System.Private.Xml.dasm - XmlSqlBinaryReader:FillAllowEOF():bool:this
-2 (-1.05% of base) : System.Private.CoreLib.dasm - Grisu3:TryRunShortest(byref,byref,byref,Span`1,byref,byref):bool
-4 (-1.02% of base) : Microsoft.CodeAnalysis.CSharp.dasm - SourceConstructorSymbol:CalculateLocalSyntaxOffset(int,SyntaxTree):int:this
-5 (-0.70% of base) : System.Private.CoreLib.dasm - Number:NumberToFloatingPointBitsSlow(byref,byref,int,int,int):long
-1 (-0.20% of base) : Microsoft.CodeAnalysis.dasm - CustomDebugInfoWriter:SerializeReferenceToIteratorClass(String,ArrayBuilder`1)
-2 (-0.19% of base) : System.Private.Xml.dasm - FloatingDecimal:AdjustDbl(double):double:this
10 total methods with Code Size differences (10 improved, 0 regressed), 244443 unchanged.
Next, will look into MUL and DIV operator optimization.
Activity
JulieLeeMSFT commentedon Aug 3, 2020
With the help with Eugene, I was able to get optimized code for M1 to M4 as shown in https://godbolt.org/z/nmZxv8.
Found some issues during jit-diff that does not handle cases where exception is thrown from both operands, and investigation WIP.
erozenfeld commentedon Aug 17, 2020
Note that currently morph does the opposite transformation when one of the operands is a const:
runtime/src/coreclr/src/jit/morph.cpp
Lines 13217 to 13263 in 7742b57
The first transformation is fine, since we are negating a const.
The second transformation is then continued
to canonicalize the location of the const.
Since constants are canonicalized as second operands, transformations like
and
are simplified.
@JulieLeeMSFT I think you should just disable your optimization when the operand that's not negated is a const.
JulieLeeMSFT commentedon Aug 17, 2020
That seems the best solution. We will skip transforming NEG when non negated operand is constant. Will check if bitwise shift pattern needs to be revisited where NEG was replaced with SUB pattern.
JulieLeeMSFT commentedon Aug 22, 2020
Simple ADD and SUB arithmetic optimization has been completed with no regression.
Next, will look into MUL and DIV operator optimization.
Simple GT_NEG optimization for #13837 (#43921)
JulieLeeMSFT commentedon Nov 6, 2020
Completed optimizing + and - operators. Will open a new issue to track * and /.
parent 77fba3b