[flang][perf] Adding noalias for function arguments affects function specialization
CPU2017/548 slowed down after https://github.com/llvm/llvm-project/commit/a0d699a8e686cba99690cf28463d14526c5bfbc8, so this change was partially reverted by https://github.com/llvm/llvm-project/commit/d16ecad968244ca42ce09cb3199dea1ec994b211
I could reproduce it on neoverse-v1 with -Ofast -flto -mcpu=neoverse-v1 -fuse-ld=lld -mmlir -force-no-alias=false/true.
It looks like the digits_2 function does not pass the function specialization threshold on the Latency bonus.
Without noalias:
FnSpecialization: Specialization bonus {Latency = 2424 (71%)}
With noalias:
FnSpecialization: Specialization bonus {Latency = 1098 (34%)}
FnSpecialization: No possible specializations found for _QMbrute_forcePdigits_2
The current threshold is 40%.
It looks like the big portion of the latency bonus comes from the multiple mod(row, 3) expressions, which are not CSEd without noalias - so function specialization computes a lot of bonus for them when row is a constant. With noalias, there is a single srem instruction that computes mod(row, 3) at the beginning of the function.
I will investigate more to see if the function specialization misses to account for some bonuses.
It looks like the threshold has been lowered once (also due to the improved alias analysis): https://github.com/llvm/llvm-project/commit/2fb51fba8ca904a6d3ddf30ae94228ecf9e6a231#diff-066363256b7b4164e66b28a3028b2cb9e405c9136241baa33db76ebd2edb87cd
Here is performance data collected on different aarch64 CPUs (i.e. with different -mcpu options) with -mmlir -force-no-alias=false/true:
neoverse-v2:
true: digits_2 specialized, 134.09 seconds
false: digits_2 specialized, 123.11 seconds
neoverse-v1:
true: digits_2 not specialized, 580.35 seconds
false: digits_2 specialized, 174.63 seconds
neoverse-n1:
true: digts_2 specialized, 305.9 seconds
false: digits_2 not specialized, 699.45 seconds
It is obvious, the function specialization has the most impact, but even when it kicks in both cases, there is about 10% regression with noalias.
I also measured noticeable performance regression on x86 without LTO, and it looked like an LSR issue, which produced too many temps that then resulted in more stack accesses at the hot parts of the code.
I will investigate the following:
- Why specialization behaves so differently between v1 and n1.
- See if the function specialization misses to account for some latency bonuses (e.g. I believe we do not count the instructions that would be trivially dead, if their only uses are inside the blocks that are dead due to the constant propagation).
- Try to dig deeper into the LSR and see if it misses something (though, it is known that it does not care about the register pressure overhead).
With the adjusted latency threshold in https://github.com/llvm/llvm-project/pull/143954 I get the following times:
neoverse-v2:
true: digits_2 specialized, 133.78 seconds
false: digits_2 specialized, 121.24 seconds
neoverse-v1:
true: digits_2 specialized, 193.99 seconds
false: digits_2 specialized, 174.37 seconds
neoverse-n1:
true: digts_2 specialized, 305.96 seconds
false: digits_2 not specialized, 701.07 seconds
I created #144002 for the LSR inefficiency. I will appreciate any help with it.
Here is some other findings:
This is a loop nest of 6 iterations, but LLVM cannot figure it out, because of the way flang computes the loop count for the leading dimension:
block(row+1:row+2, 7:9, i8) = block(row+1:row+2, 7:9, i8) - 10
I will try to handle this in InstCombine, though, it seems future MLIR transformations may also benefit from knowing that the inner loop is just 2 iterations.
LLVM vectorizes these loops too wide on x86:
block(rnext:9, 8, i8) = block(rnext:9, 8, i8) - 10
It generates 32-elements vector loop with 4-elements vector epilog loop and a scalar remainder. These loops surely contribute to the number of IVs LSR creates for the address computations. Flang knows for sure that 1 <= rnext <= 9. I used llvm.assume to expose that, and vectorization was more reasonable with just one 8-elements vector loop and a scalar remainder. I am going to implement this under an option and see where it gets me.
With the FunctionSpecialization and the InstCombine patches I get the following numbers on v1:
LTO:
-force-no-alias=false: 175.52 seconds
-force-no-alias=true: 195.35 seconds
no-LTO:
-force-no-alias=false: 382.37 seconds
-force-no-alias=true: 362.55 seconds
So adding noalias helps a bit the non-LTO code, but it still slows down the LTO code by 11%. I will investigate further.
The noalias patch causes some block reordering due to the fact that some hoisting of loop invariants happens early in the pipeline (before the function specialization and inlining). I did my best to verify that the blocks are almost identical, except for integer operations related to the following pattern:
where(sudoku1(row, :) /= 0)
l = sudoku1(row, :)
u = l
elsewhere
l = 1
u = r
end where
%.not2302.1 = icmp eq i32 %23, 0, !dbg !957
%.sroa.3.1 = select i1 %.not2302.1, i32 9, i32 %23, !dbg !957
%.sroa.4.1 = call i32 @llvm.umax.i32(i32 %23, i32 1), !dbg !957
%reass.sub2744 = sub i32 %.sroa.3.1, %.sroa.4.1
%37 = icmp ult i32 %reass.sub2744, 2147483647
I see the comparisons like icmp ult i32 %reass.sub2744, 2147483647 showing up in different blocks compared to the original compiler.
The locations of spills/reloads are also a bit different with and w/o the patch.
Exchange2 seems to be quite sensitive to the placement of even light instructions in different blocks, so I am not sure I can do anything reasonable here to "fix" the effect of noalias patch. I still tried something...
The above select/umax/sub pattern can actually be optimized into the following:
%.not2302.1 = icmp eq i32 %22, 0, !dbg !958
%.sroa.4.1 = call i32 @llvm.umax.i32(i32 %22, i32 1), !dbg !958
%36 = select i1 %.not2302.1, i32 8, i32 0
So the icmp instructions start to evaluate just to true, and this results in removal of some non-taken branches. With this InstCombine patch I got the following times on v1:
LTO:
-force-no-alias=false: 186.83 seconds
-force-no-alias=true: 187.80 seconds
So I can turn the 11% regression from the noalias patch into just 7% regression (comparing to the current v1 times with LTO).
Comparing the times without the noalias patch and with and w/o the InstCombine patch, I see one thing that pops up in the profile:
4416B bb.1 (%ir-block.43):
; predecessors: %bb.0, %bb.267
successors: %bb.2(0x50000000), %bb.267(0x30000000); %bb.2(62.50%), %bb.267(37.50%)
liveins: $lr, $d8, $d9, $w24, $x6, $x7, $x19
4464B renamable $x8 = MOVNXi 323, 0, debug-location !2181; exchange2.f90:1014:6
4472B renamable $x12 = LDRXui %stack.89, 0 :: (load (s64) from %stack.89)
4476B STRXui renamable $x8, %stack.14, 0 :: (store (s64) into %stack.14)
4480B renamable $x14 = MADDXrrr renamable $x12, renamable $x7, killed renamable $x8, debug-location !2181; exchange2.f90:1014:6
4496B renamable $x13 = ADDXrr renamable $x19, renamable $x14, debug-location !2181; exchange2.f90:1014:6
4512B renamable $w8 = LDRWui renamable $x13, 3, debug-location !2181 :: (load (s32) from %ir.49, !tbaa !1412); exchange2.f90:1014:6
4528B dead $wzr = SUBSWri killed renamable $w8, 1, 0, implicit-def $nzcv, debug-location !2181; exchange2.f90:1014:6
4544B Bcc 11, %bb.267, implicit $nzcv, debug-location !2181; exchange2.f90:1014:6
4560B B %bb.2, debug-location !2181; exchange2.f90:1014:6
...
5808B bb.3 (%ir-block.95):
; predecessors: %bb.2, %bb.265
successors: %bb.4(0x50000000), %bb.265(0x30000000); %bb.4(62.50%), %bb.265(37.50%)
liveins: $lr, $d8, $d9, $w17, $w24, $x6, $x7, $x12, $x13, $x14, $x15, $x16, $x19
5816B renamable $x8 = LDRXui %stack.14, 0 :: (load (s64) from %stack.14)
5872B renamable $x8 = MADDXrrr renamable $x16, renamable $x7, killed renamable $x8, debug-location !2187; exchange2.f90:1025:10
5888B renamable $x18 = ADDXrr renamable $x19, killed renamable $x8, debug-location !2187; exchange2.f90:1025:10
5904B renamable $w8 = LDRWui renamable $x18, 12, debug-location !2187 :: (load (s32) from %ir.100, !tbaa !1412); exchange2.f90:1025:10
5920B dead $wzr = SUBSWri killed renamable $w8, 1, 0, implicit-def $nzcv, debug-location !2187; exchange2.f90:1025:10
5936B Bcc 11, %bb.265, implicit $nzcv, debug-location !2187; exchange2.f90:1025:10
5952B B %bb.4, debug-location !2187; exchange2.f90:1025:10
This reload from %stack.14 happens in many places with the InstCombine patch, and it does not happen w/o the patch.
It looks strange to me, because this is a reload of constant value -324, and the constant should probably be rematerialized.
@kiranchandramohan, @tblah, I do not think I can do much here, and I do not want to spend much more time on making sure a 100% valuable patch does not affect exchange2 in a negative way. I think a small regression on this benchmark should not block making reasonable changes in the compiler.
I suggest the following:
- I will upload my
InstCombinepatch for review and will try to enable thenoaliaspatch once again. - If we only get ~7% regression on exchange2 from both of these patches, I suggest not reverting it.
- We can try to measure the effect of constant
MOVNrematerialization, and maybe it will improve exchange2 to the good old 175 seconds.
Please let me know what you think.
Another inefficiency I noticed are the re-reads of the sudoku1 array at every call site where digits_2 is inlined. I think this may happen due to the fact that sudoku1 is passed by pointer to the covered call, so LLVM may think it is modified by the callee (otherwise, there are no explicit writes into sudoku1 in digits_2). Forcing covered inlining may help here.
I was thinking about using readonly function argument attribute, based on intent(in), but this won't help, because sudoku1 is passed to an assumed-shape dummy (i.e. via descriptor).
Thank you very much for all the time you have put into this investigation.
With this InstCombine patch I got the following times on v1: LTO: -force-no-alias=false: 186.83 seconds -force-no-alias=true: 187.80 seconds
Isn't that only a roughly 0.5% regression not 7%?
Thank you very much for all the time you have put into this investigation.
With this InstCombine patch I got the following times on v1: LTO: -force-no-alias=false: 186.83 seconds -force-no-alias=true: 187.80 seconds
Isn't that only a roughly 0.5% regression not 7%?
Slava previously reported 175.52 second on v1 with force-no-alias=false, So I am assuming he probably pasted the wrong number here.
-force-no-alias=false: 175.52 seconds
@davemgreen, Please see https://github.com/llvm/llvm-project/issues/143219#issuecomment-3006550285 and could you advise the best course of action here? CC: @bipmis @david-arm
I tried to avoid the confusion with the numbers, but apparenly failed to do so :)
My goal is to enable noalias patch, but it changes the time from 175.5 (the best time) to 195.35 - this is 11% regression.
If I make the InstCombine patch, then I get a slowdown even without the noalias patch: 175.5 (the best time) to 186.83 - this is 6.5% regressions.
If I enable noalias patch on top of the InstCombine patch, I get: 187.80 seconds - this is 7% regression comapring to the best time (that we have now). Note that this is better than 195.35 seconds with just the noalias patch alone, and exchange2 slows down from the InstCombine patch alone.
I have some (little) hope that rematerializing the constant MOVN instead of spilling/reloading it may bring performance back to the best one (together with #145830 and enabling noalias). If someone can draft a patch, I can try them all together.
Here is another idea. The MADD code above is generated for the mul/add in the following LLVM IR:
%1734 = mul i64 %1732, 324, !dbg !2378
%1735 = add i64 %1734, -324, !dbg !2378
%1736 = getelementptr i8, ptr getelementptr inbounds nuw (i8, ptr @_QMbrute_forceEblock, i64 64), i64 %1735, !dbg !2378
There are many such instances. It seems that the register allocator decides to spill -324 because there is not enough registers. What if we pull -324 into the gep like this:
%1734 = mul i64 %1732, 324, !dbg !2378
%1735 = add i64 %1734, -324, !dbg !2378
%1736 = getelementptr i8, ptr getelementptr inbounds nuw (i8, ptr @_QMbrute_forceEblock, i64 -260), i64 %1734, !dbg !2378
This can be done in InstCombine even if %1735 has multiple uses (which it has, though, they are all in the gep's like the one above, so it will eventually become dead).
Hopefully, with this change there will be enough registers to avoid reloading -324. Are there drawbacks to such pulling of the immediate offset?
@davemgreen @bipmis @david-arm ping
Please could we give Slava some help with this, otherwise adding noalias in flang is going to lead to a regression in spec intrate on V1.
I have some (little) hope that rematerializing the constant
MOVNinstead of spilling/reloading it may bring performance back to the best one (together with #145830 and enablingnoalias). If someone can draft a patch, I can try them all together.
@vzakhari Could you verify if this is the actual cause of regression so we know where to look at. A 7% regression would still be big enough for us to investigate.
Hi @bipmis, thank you for the reply! I would have verified it if I knew how to do that :)
There are multiple short-circuited loops, and reloads seem to be inserted in all of them. The price of a reload is paid on each short-circuited iteration, i.e. there is a simple check followed by cycle - so each reload takes a significant portion of the short-circuit path inside those loop. It would not be practical for me to go into the assembly and rematerialize the constant manually.
On the other hand, if rematerializing the constant MOVN seems reasonable from the ARM architecture's point of view, then it may make sense to try such a patch. I am not able to draft a patch like this myself, but I am willing to try it and report the results back.
I might have misunderstood your question, so please let me know if I can provide some particular data.
I was interested in why MOVN was not already materializable. Most immediates get lowered via MOVi32imm and are expanded later, allowing the immediate to be moved around as required. It was producing slightly different code in some cases, that #147510 attempts to fixup.
Thank you, @davemgreen! I will try this patch and report back.
@davemgreen, here is what I found. Your patch did not improve performance.
I see this change after your patch:
# *** IR Dump After AArch64 Instruction Selection (aarch64-isel) ***:
...
bb.4 (%ir-block.133):
; predecessors: %bb.2, %bb.193
successors: %bb.5(0x50000000), %bb.193(0x30000000); %bb.5(62.50%), %bb.193(37.50%)
%24:gpr64sp = PHI %3:gpr64all, %bb.2, %546:gpr64all, %bb.193
%25:gpr32sp = PHI %2:gpr32all, %bb.2, %545:gpr32all, %bb.193
%660:gpr64 = nsw SUBSXri %24:gpr64sp, 1, 0, implicit-def dead $nzcv, debug-location !2068; exchange2.f90:1025:10
%26:gpr64 = COPY %660:gpr64, debug-location !2068; exchange2.f90:1025:10
%661:gpr32 = MOVi32imm 324
%662:gpr64 = SUBREG_TO_REG 0, killed %661:gpr32, %subreg.sub_32
%663:gpr64 = MADDXrrr %660:gpr64, killed %662:gpr64, $xzr, debug-location !2068; exchange2.f90:1025:10
%27:gpr64 = COPY %663:gpr64, debug-location !2068; exchange2.f90:1025:10
%664:gpr64common = MOVaddr target-flags(aarch64-page) @_QMbrute_forceEblock, target-flags(aarch64-pageoff, aarch64-nc) @_QMbrute_forceEblock, debug-location !2068; \
exchange2.f90:1025:10
%665:gpr64common = ADDXrr killed %664:gpr64common, %663:gpr64, debug-location !2068; exchange2.f90:1025:10
%666:gpr32common = LDRWui killed %665:gpr64common, 12, debug-location !2068 :: (load (s32) from %ir.138, !tbaa !1285); exchange2.f90:1025:10
%667:gpr32 = SUBSWri killed %666:gpr32common, 1, 0, implicit-def $nzcv, debug-location !2068; exchange2.f90:1025:10
Bcc 11, %bb.193, implicit $nzcv, debug-location !2068; exchange2.f90:1025:10
B %bb.5, debug-location !2068; exchange2.f90:1025:10
Then:
# *** IR Dump After Early Machine Loop Invariant Code Motion (early-machinelicm) ***:
...
bb.0 (%ir-block.0):
successors: %bb.1(0x80000000); %bb.1(100.00%)
...
%616:gpr32 = MOVi32imm 324
...
bb.4 (%ir-block.133):
; predecessors: %bb.2, %bb.193
successors: %bb.5(0x50000000), %bb.193(0x30000000); %bb.5(62.50%), %bb.193(37.50%)
%24:gpr64sp = PHI %3:gpr64all, %bb.2, %546:gpr64all, %bb.193
%25:gpr32sp = PHI %2:gpr32all, %bb.2, %545:gpr32all, %bb.193
%660:gpr64 = nsw SUBSXri %24:gpr64sp, 1, 0, implicit-def dead $nzcv, debug-location !2068; exchange2.f90:1025:10
%26:gpr64 = COPY %660:gpr64, debug-location !2068; exchange2.f90:1025:10
%662:gpr64 = SUBREG_TO_REG 0, %616:gpr32, %subreg.sub_32
%663:gpr64 = MADDXrrr %660:gpr64, killed %662:gpr64, $xzr, debug-location !2068; exchange2.f90:1025:10
%27:gpr64 = COPY %663:gpr64, debug-location !2068; exchange2.f90:1025:10
%665:gpr64common = ADDXrr %619:gpr64common, %663:gpr64, debug-location !2068; exchange2.f90:1025:10
%666:gpr32common = LDRWui killed %665:gpr64common, 12, debug-location !2068 :: (load (s32) from %ir.138, !tbaa !1285); exchange2.f90:1025:10
%667:gpr32 = SUBSWri killed %666:gpr32common, 1, 0, implicit-def $nzcv, debug-location !2068; exchange2.f90:1025:10
Bcc 11, %bb.193, implicit $nzcv, debug-location !2068; exchange2.f90:1025:10
B %bb.5, debug-location !2068; exchange2.f90:1025:10
Then:
# *** IR Dump After Register Allocation Pass Scoring (regallocscoringpass) ***:
...
6608B bb.4 (%ir-block.133):
; predecessors: %bb.2, %bb.266
successors: %bb.5(0x50000000), %bb.266(0x30000000); %bb.5(62.50%), %bb.266(37.50%)
liveins: $d8, $d9, $w17, $w30, $x6, $x7, $x12, $x13, $x14, $x15, $x16, $x19, $x21
6656B renamable $x8 = nsw SUBXri renamable $x16, 1, 0, debug-location !2068; exchange2.f90:1025:10
6688B renamable $x9 = MADDXrrr renamable $x8, renamable $x7, $xzr, debug-location !2068; exchange2.f90:1025:10
6704B renamable $x18 = ADDXrr renamable $x19, killed renamable $x9, debug-location !2068; exchange2.f90:1025:10
6720B renamable $w9 = LDRWui renamable $x18, 12, debug-location !2068 :: (load (s32) from %ir.138, !tbaa !1285); exchange2.f90:1025:10
6736B dead $wzr = SUBSWri killed renamable $w9, 1, 0, implicit-def $nzcv, debug-location !2068; exchange2.f90:1025:10
6752B Bcc 11, %bb.266, implicit $nzcv, debug-location !2068; exchange2.f90:1025:10
6768B B %bb.5, debug-location !2068; exchange2.f90:1025:10
Even though I do not see register reloads here now, I think the long live range of $x16 may still be a problem in other parts of code.
Here is one of the contributing pieces of code. Both compilations used https://github.com/llvm/llvm-project/pull/145830 and https://github.com/llvm/llvm-project/pull/147510.
Without noalias patch:
16891 │ d54:┌─→subs w10, w10, #0x1 ▒
191 │ │ add x9, x9, #0x1 ▒
4201 │ │↓ b.le 35d8 ▒
5194 │ d60:│ madd x8, x9, x7, x19 ▒
52 │ │ add x12, x26, x8 ▒
3714 │ │ ldr w8, [x12, #232] ▒
21848 │ │ cmp w8, #0x1 ▒
16 │ └──b.lt d54
With noalias patch:
20063 │ d7c:┌─→subs w13, w13, #0x1 ▒
4479 │ │ add x12, x12, #0x1 ▒
576 │ │↓ b.le 3664 ▒
4783 │ d88:│ sub x9, x12, #0x1 ▒
711 │ │ mul x8, x9, x7 ▒
3732 │ │ str x8, [sp, #1360] ▒
3892 │ │ add x8, x19, x8 ▒
3654 │ │ ldr w10, [x8, #232] ▒
31104 │ │ cmp w10, #0x1 ▒
38 │ └──b.lt d7c ▒
I believe this corresponds to this MIR (with noalias patch):
# *** IR Dump After AArch64 Assembly Printer (aarch64-asm-printer) ***:
...
bb.0 (%ir-block.0):
...
renamable $w7 = MOVZWi 324, 0, implicit-def $x7
...
renamable $x21 = MOVNXi 323, 0
...
bb.34 (%ir-block.3054):
; predecessors: %bb.35, %bb.33
successors: %bb.35(0x7c000000), %bb.241(0x04000000); %bb.35(96.88%), %bb.241(3.12%)
liveins: $d8, $d9, $w11, $w13, $w30, $x6, $x7, $x12, $x14, $x15, $x19, $x20, $x21, $x22, $x23, $x24
renamable $w13 = nsw SUBSWri killed renamable $w13, 1, 0, implicit-def $nzcv, debug-location !2187; exchange2.f90:1155:28 @[ exchange2.f90:1129:37 ]
renamable $x12 = nsw ADDXri killed renamable $x12, 1, 0, debug-location !2187; exchange2.f90:1155:28 @[ exchange2.f90:1129:37 ]
Bcc 13, %bb.241, implicit $nzcv, debug-location !2179; exchange2.f90:1079:26 @[ exchange2.f90:1129:37 ]
bb.35 (%ir-block.989):
; predecessors: %bb.32, %bb.34
successors: %bb.36(0x50000000), %bb.34(0x30000000); %bb.36(62.50%), %bb.34(37.50%)
liveins: $d8, $d9, $w11, $w13, $w30, $x6, $x7, $x12, $x14, $x15, $x19, $x20, $x21, $x22, $x23, $x24
renamable $x9 = nsw SUBXri renamable $x12, 1, 0, debug-location !2182; exchange2.f90:1080:29 @[ exchange2.f90:1129:37 ]
renamable $x8 = MADDXrrr renamable $x9, renamable $x7, $xzr, debug-location !2182; exchange2.f90:1080:29 @[ exchange2.f90:1129:37 ]
STRXui renamable $x8, $sp, 170 :: (store (s64) into %stack.95)
$x8 = ADDXrs renamable $x19, killed renamable $x8, 0, debug-location !2182; exchange2.f90:1080:29 @[ exchange2.f90:1129:37 ]
renamable $w10 = LDRWui renamable $x8, 58, debug-location !2182 :: (load (s32) from %ir.994, !tbaa !1285); exchange2.f90:1080:29 @[ exchange2.f90:1129:37 ]
dead $wzr = SUBSWri killed renamable $w10, 1, 0, implicit-def $nzcv, debug-location !2182; exchange2.f90:1080:29 @[ exchange2.f90:1129:37 ]
Bcc 11, %bb.34, implicit $nzcv, debug-location !2182; exchange2.f90:1080:29 @[ exchange2.f90:1129:37 ]
x7 and x21 still have long live ranges, which affects how the other values are spilled/reloaded.
It looks like the register allocator does not rematerialize MOVi32imm and MOVi64imm and instead decides to spill/reload the other values. I guess it may depend on the compiler computed block weights, which may be quite incorrect without a dynamic profile.
@davemgreen, here is what I found. Your patch did not improve performance.
Yeah it felt unlikely to make a big difference, unfortunately. It should hopefully be useful in general though. We might need to address the issue at a higher level than register allocation, fixing the uses of the gep addresses and lowering them more nicely. I wasn't sure why there were so many differences between the two versions.