Skip to content

Commit 2be0abb

Browse files
committed
Rewrite load-store-vectorizer.
The motivation for this change is a workload generated by the XLA compiler targeting nvidia GPUs. This kernel has a few hundred i8 loads and stores. Merging is critical for performance. The current LSV doesn't merge these well because it only considers instructions within a block of 64 loads+stores. This limit is necessary to contain the O(n^2) behavior of the pass. I'm hesitant to increase the limit, because this pass is already one of the slowest parts of compiling an XLA program. So we rewrite basically the whole thing to use a new algorithm. Before, we compared every load/store to every other to see if they're consecutive. The insight (from tra@) is that this is redundant. If we know the offset from PtrA to PtrB, then we don't need to compare PtrC to both of them in order to tell whether C may be adjacent to A or B. So that's what we do. When scanning a basic block, we maintain a list of chains, where we know the offset from every element in the chain to the first element in the chain. Each instruction gets compared only to the leaders of all the chains. In the worst case, this is still O(n^2), because all chains might be of length 1. To prevent compile time blowup, we only consider the 64 most recently used chains. Thus we do no more comparisons than before, but we have the potential to make much longer chains. This rewrite affects many tests. The changes to tests fall into two categories. 1. The old code had what appears to be a bug when deciding whether a misaligned vectorized load is fast. Suppose TTI reports that load <i32 x 4> align 4 has relative speed 1, and suppose that load i32 align 4 has relative speed 32. The intent of the code seems to be that we prefer the scalar load, because it's faster. But the old code would choose the vectorized load. accessIsMisaligned would set RelativeSpeed to 0 for the scalar load (and not even call into TTI to get the relative speed), because the scalar load is aligned. After this patch, we will prefer the scalar load if it's faster. 2. This patch changes the logic for how we vectorize. Usually this results in vectorizing more. Explanation of changes to tests: - AMDGPU/adjust-alloca-alignment.ll: #1 - AMDGPU/flat_atomic.ll: #2, we vectorize more. - AMDGPU/int_sideeffect.ll: #2, there are two possible locations for the call to @foo, and the pass is brittle to this. Before, we'd vectorize in case 1 and not case 2. Now we vectorize in case 2 and not case 1. So we just move the call. - AMDGPU/adjust-alloca-alignment.ll: #2, we vectorize more - AMDGPU/insertion-point.ll: #2 we vectorize more - AMDGPU/merge-stores-private.ll: #1 (undoes changes from git rev 86f9117, which appear to have hit the bug from #1) - AMDGPU/multiple_tails.ll: #1 - AMDGPU/vect-ptr-ptr-size-mismatch.ll: Fix alignment (I think related to #1 above). - AMDGPU CodeGen: I have difficulty commenting on these changes, but many of them look like #2, we vectorize more. - NVPTX/4x2xhalf.ll: Fix alignment (I think related to #1 above). - NVPTX/vectorize_i8.ll: We don't generate <3 x i8> vectors on NVPTX because they're not legal (and eventually get split) - X86/correct-order.ll: #2, we vectorize more, probably because of changes to the chain-splitting logic. - X86/subchain-interleaved.ll: #2, we vectorize more - X86/vector-scalar.ll: #2, we can now vectorize scalar float + <1 x float> - X86/vectorize-i8-nested-add-inseltpoison.ll: Deleted the nuw test because it was nonsensical. It was doing `add nuw %v0, -1`, but this is equivalent to `add nuw %v0, 0xffff'ffff`, which is equivalent to asserting that %v0 == 0. - X86/vectorize-i8-nested-add.ll: Same as nested-add-inseltpoison.ll Differential Revision: https://reviews.llvm.org/D149893
1 parent 9249129 commit 2be0abb

29 files changed

+2935
-1656
lines changed

llvm/lib/Transforms/Vectorize/LoadStoreVectorizer.cpp

Lines changed: 1095 additions & 940 deletions
Large diffs are not rendered by default.

llvm/test/CodeGen/AMDGPU/GlobalISel/sdivrem.ll

Lines changed: 280 additions & 282 deletions
Large diffs are not rendered by default.

llvm/test/CodeGen/AMDGPU/GlobalISel/udivrem.ll

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -985,8 +985,8 @@ define amdgpu_kernel void @udivrem_v4i32(ptr addrspace(1) %out0, ptr addrspace(1
985985
define amdgpu_kernel void @udivrem_v2i64(ptr addrspace(1) %out0, ptr addrspace(1) %out1, <2 x i64> %x, <2 x i64> %y) {
986986
; GFX8-LABEL: udivrem_v2i64:
987987
; GFX8: ; %bb.0:
988-
; GFX8-NEXT: s_load_dwordx8 s[8:15], s[4:5], 0x10
989-
; GFX8-NEXT: s_load_dwordx4 s[4:7], s[4:5], 0x0
988+
; GFX8-NEXT: s_load_dwordx4 s[12:15], s[4:5], 0x20
989+
; GFX8-NEXT: s_load_dwordx8 s[4:11], s[4:5], 0x0
990990
; GFX8-NEXT: s_waitcnt lgkmcnt(0)
991991
; GFX8-NEXT: v_cvt_f32_u32_e32 v0, s13
992992
; GFX8-NEXT: v_cvt_f32_u32_e32 v1, s12
@@ -1255,7 +1255,7 @@ define amdgpu_kernel void @udivrem_v2i64(ptr addrspace(1) %out0, ptr addrspace(1
12551255
;
12561256
; GFX9-LABEL: udivrem_v2i64:
12571257
; GFX9: ; %bb.0:
1258-
; GFX9-NEXT: s_load_dwordx8 s[8:15], s[4:5], 0x10
1258+
; GFX9-NEXT: s_load_dwordx4 s[12:15], s[4:5], 0x20
12591259
; GFX9-NEXT: s_waitcnt lgkmcnt(0)
12601260
; GFX9-NEXT: v_cvt_f32_u32_e32 v0, s13
12611261
; GFX9-NEXT: v_cvt_f32_u32_e32 v1, s12
@@ -1264,7 +1264,7 @@ define amdgpu_kernel void @udivrem_v2i64(ptr addrspace(1) %out0, ptr addrspace(1
12641264
; GFX9-NEXT: v_mul_f32_e32 v0, 0x4f800000, v0
12651265
; GFX9-NEXT: v_add_f32_e32 v0, v0, v1
12661266
; GFX9-NEXT: v_rcp_iflag_f32_e32 v0, v0
1267-
; GFX9-NEXT: s_load_dwordx4 s[4:7], s[4:5], 0x0
1267+
; GFX9-NEXT: s_load_dwordx8 s[4:11], s[4:5], 0x0
12681268
; GFX9-NEXT: v_mul_f32_e32 v0, 0x5f7ffffc, v0
12691269
; GFX9-NEXT: v_mul_f32_e32 v1, 0x2f800000, v0
12701270
; GFX9-NEXT: v_trunc_f32_e32 v2, v1
@@ -1325,6 +1325,7 @@ define amdgpu_kernel void @udivrem_v2i64(ptr addrspace(1) %out0, ptr addrspace(1
13251325
; GFX9-NEXT: v_add3_u32 v1, v5, v2, v1
13261326
; GFX9-NEXT: v_add_co_u32_e32 v0, vcc, v3, v0
13271327
; GFX9-NEXT: v_addc_co_u32_e32 v1, vcc, v4, v1, vcc
1328+
; GFX9-NEXT: s_waitcnt lgkmcnt(0)
13281329
; GFX9-NEXT: v_mul_lo_u32 v2, s9, v0
13291330
; GFX9-NEXT: v_mul_lo_u32 v3, s8, v1
13301331
; GFX9-NEXT: v_mul_hi_u32 v4, s8, v0
@@ -1510,14 +1511,13 @@ define amdgpu_kernel void @udivrem_v2i64(ptr addrspace(1) %out0, ptr addrspace(1
15101511
; GFX9-NEXT: v_cndmask_b32_e32 v9, v13, v20, vcc
15111512
; GFX9-NEXT: v_cndmask_b32_e64 v7, v8, v7, s[0:1]
15121513
; GFX9-NEXT: v_cndmask_b32_e64 v8, v11, v9, s[0:1]
1513-
; GFX9-NEXT: s_waitcnt lgkmcnt(0)
15141514
; GFX9-NEXT: global_store_dwordx4 v0, v[1:4], s[4:5]
15151515
; GFX9-NEXT: global_store_dwordx4 v0, v[5:8], s[6:7]
15161516
; GFX9-NEXT: s_endpgm
15171517
;
15181518
; GFX10-LABEL: udivrem_v2i64:
15191519
; GFX10: ; %bb.0:
1520-
; GFX10-NEXT: s_load_dwordx8 s[8:15], s[4:5], 0x10
1520+
; GFX10-NEXT: s_load_dwordx4 s[12:15], s[4:5], 0x20
15211521
; GFX10-NEXT: s_waitcnt lgkmcnt(0)
15221522
; GFX10-NEXT: v_cvt_f32_u32_e32 v0, s13
15231523
; GFX10-NEXT: v_cvt_f32_u32_e32 v1, s15
@@ -1616,11 +1616,11 @@ define amdgpu_kernel void @udivrem_v2i64(ptr addrspace(1) %out0, ptr addrspace(1
16161616
; GFX10-NEXT: v_mul_lo_u32 v10, v5, v1
16171617
; GFX10-NEXT: v_mul_lo_u32 v11, v4, v1
16181618
; GFX10-NEXT: v_mul_hi_u32 v14, v5, v1
1619-
; GFX10-NEXT: v_mul_hi_u32 v1, v4, v1
1619+
; GFX10-NEXT: s_load_dwordx8 s[4:11], s[4:5], 0x0
16201620
; GFX10-NEXT: v_mul_lo_u32 v15, v8, v3
16211621
; GFX10-NEXT: v_mul_lo_u32 v16, v6, v3
16221622
; GFX10-NEXT: v_mul_hi_u32 v17, v8, v3
1623-
; GFX10-NEXT: v_mul_hi_u32 v3, v6, v3
1623+
; GFX10-NEXT: v_mul_hi_u32 v1, v4, v1
16241624
; GFX10-NEXT: v_add_co_u32 v10, s0, v12, v10
16251625
; GFX10-NEXT: v_cndmask_b32_e64 v12, 0, 1, s0
16261626
; GFX10-NEXT: v_add_co_u32 v0, s0, v11, v0
@@ -1642,65 +1642,66 @@ define amdgpu_kernel void @udivrem_v2i64(ptr addrspace(1) %out0, ptr addrspace(1
16421642
; GFX10-NEXT: v_add_nc_u32_e32 v11, v11, v13
16431643
; GFX10-NEXT: v_cndmask_b32_e64 v9, 0, 1, s0
16441644
; GFX10-NEXT: v_add_nc_u32_e32 v7, v15, v7
1645+
; GFX10-NEXT: v_mul_hi_u32 v3, v6, v3
16451646
; GFX10-NEXT: v_add_co_u32 v0, vcc_lo, v5, v0
1646-
; GFX10-NEXT: v_add_nc_u32_e32 v10, v16, v10
16471647
; GFX10-NEXT: v_add3_u32 v1, v11, v9, v1
16481648
; GFX10-NEXT: v_add_co_u32 v2, s0, v2, v7
1649+
; GFX10-NEXT: v_add_nc_u32_e32 v10, v16, v10
16491650
; GFX10-NEXT: v_cndmask_b32_e64 v7, 0, 1, s0
1650-
; GFX10-NEXT: v_mul_hi_u32 v5, s8, v0
16511651
; GFX10-NEXT: v_add_co_ci_u32_e32 v1, vcc_lo, v4, v1, vcc_lo
1652+
; GFX10-NEXT: s_waitcnt lgkmcnt(0)
16521653
; GFX10-NEXT: v_mul_lo_u32 v4, s9, v0
1654+
; GFX10-NEXT: v_mul_hi_u32 v5, s8, v0
16531655
; GFX10-NEXT: v_add3_u32 v3, v10, v7, v3
1654-
; GFX10-NEXT: v_mul_hi_u32 v0, s9, v0
16551656
; GFX10-NEXT: v_mul_lo_u32 v7, s8, v1
1656-
; GFX10-NEXT: v_mul_lo_u32 v10, s9, v1
1657+
; GFX10-NEXT: v_mul_hi_u32 v0, s9, v0
1658+
; GFX10-NEXT: v_mul_lo_u32 v9, s9, v1
16571659
; GFX10-NEXT: v_add_co_u32 v2, vcc_lo, v8, v2
16581660
; GFX10-NEXT: v_add_co_ci_u32_e32 v3, vcc_lo, v6, v3, vcc_lo
16591661
; GFX10-NEXT: v_mul_hi_u32 v6, s8, v1
1660-
; GFX10-NEXT: v_mul_hi_u32 v1, s9, v1
16611662
; GFX10-NEXT: v_add_co_u32 v4, s0, v4, v7
16621663
; GFX10-NEXT: v_cndmask_b32_e64 v7, 0, 1, s0
1663-
; GFX10-NEXT: v_add_co_u32 v0, s0, v10, v0
1664+
; GFX10-NEXT: v_add_co_u32 v0, s0, v9, v0
16641665
; GFX10-NEXT: v_cndmask_b32_e64 v8, 0, 1, s0
16651666
; GFX10-NEXT: v_add_co_u32 v4, s0, v4, v5
16661667
; GFX10-NEXT: v_cndmask_b32_e64 v4, 0, 1, s0
16671668
; GFX10-NEXT: v_add_co_u32 v0, s0, v0, v6
16681669
; GFX10-NEXT: v_cndmask_b32_e64 v5, 0, 1, s0
1669-
; GFX10-NEXT: v_mul_lo_u32 v6, s11, v2
1670+
; GFX10-NEXT: v_mul_hi_u32 v1, s9, v1
16701671
; GFX10-NEXT: v_add_nc_u32_e32 v4, v7, v4
1672+
; GFX10-NEXT: v_mul_lo_u32 v6, s11, v2
16711673
; GFX10-NEXT: v_mul_lo_u32 v7, s10, v3
1672-
; GFX10-NEXT: v_mul_lo_u32 v10, s11, v3
16731674
; GFX10-NEXT: v_add_nc_u32_e32 v5, v8, v5
16741675
; GFX10-NEXT: v_mul_hi_u32 v8, s10, v2
16751676
; GFX10-NEXT: v_add_co_u32 v4, s0, v0, v4
16761677
; GFX10-NEXT: v_cndmask_b32_e64 v0, 0, 1, s0
16771678
; GFX10-NEXT: v_mul_hi_u32 v2, s11, v2
1678-
; GFX10-NEXT: v_mul_hi_u32 v11, s10, v3
1679+
; GFX10-NEXT: v_mul_lo_u32 v9, s11, v3
1680+
; GFX10-NEXT: v_mul_hi_u32 v10, s10, v3
16791681
; GFX10-NEXT: v_add_co_u32 v6, s0, v6, v7
16801682
; GFX10-NEXT: v_add3_u32 v5, v5, v0, v1
16811683
; GFX10-NEXT: v_cndmask_b32_e64 v7, 0, 1, s0
16821684
; GFX10-NEXT: v_mad_u64_u32 v[0:1], s0, s12, v4, 0
1683-
; GFX10-NEXT: v_mul_lo_u32 v12, s13, v4
1684-
; GFX10-NEXT: v_mul_lo_u32 v13, s12, v5
1685-
; GFX10-NEXT: v_add_co_u32 v2, s0, v10, v2
1686-
; GFX10-NEXT: v_cndmask_b32_e64 v10, 0, 1, s0
1685+
; GFX10-NEXT: v_mul_lo_u32 v11, s13, v4
1686+
; GFX10-NEXT: v_mul_lo_u32 v12, s12, v5
1687+
; GFX10-NEXT: v_add_co_u32 v2, s0, v9, v2
1688+
; GFX10-NEXT: v_cndmask_b32_e64 v9, 0, 1, s0
16871689
; GFX10-NEXT: v_add_co_u32 v6, s0, v6, v8
16881690
; GFX10-NEXT: v_cndmask_b32_e64 v6, 0, 1, s0
1689-
; GFX10-NEXT: v_add_co_u32 v2, s0, v2, v11
1691+
; GFX10-NEXT: v_add_co_u32 v2, s0, v2, v10
16901692
; GFX10-NEXT: v_cndmask_b32_e64 v8, 0, 1, s0
1691-
; GFX10-NEXT: v_add3_u32 v1, v1, v13, v12
1693+
; GFX10-NEXT: v_add3_u32 v1, v1, v12, v11
16921694
; GFX10-NEXT: v_add_nc_u32_e32 v6, v7, v6
16931695
; GFX10-NEXT: v_mul_hi_u32 v3, s11, v3
1694-
; GFX10-NEXT: s_load_dwordx4 s[4:7], s[4:5], 0x0
1695-
; GFX10-NEXT: v_add_nc_u32_e32 v7, v10, v8
1696+
; GFX10-NEXT: v_mov_b32_e32 v10, 0
1697+
; GFX10-NEXT: v_add_nc_u32_e32 v7, v9, v8
16961698
; GFX10-NEXT: v_sub_nc_u32_e32 v8, s9, v1
1697-
; GFX10-NEXT: v_sub_co_u32 v10, vcc_lo, s8, v0
1699+
; GFX10-NEXT: v_sub_co_u32 v9, vcc_lo, s8, v0
16981700
; GFX10-NEXT: v_sub_co_ci_u32_e64 v11, s0, s9, v1, vcc_lo
16991701
; GFX10-NEXT: v_subrev_co_ci_u32_e32 v0, vcc_lo, s13, v8, vcc_lo
1700-
; GFX10-NEXT: v_cmp_le_u32_e32 vcc_lo, s12, v10
1701-
; GFX10-NEXT: v_mov_b32_e32 v9, 0
1702+
; GFX10-NEXT: v_cmp_le_u32_e32 vcc_lo, s12, v9
17021703
; GFX10-NEXT: v_cndmask_b32_e64 v1, 0, -1, vcc_lo
1703-
; GFX10-NEXT: v_sub_co_u32 v8, vcc_lo, v10, s12
1704+
; GFX10-NEXT: v_sub_co_u32 v8, vcc_lo, v9, s12
17041705
; GFX10-NEXT: v_subrev_co_ci_u32_e64 v12, s0, 0, v0, vcc_lo
17051706
; GFX10-NEXT: v_cmp_le_u32_e64 s0, s13, v11
17061707
; GFX10-NEXT: v_subrev_co_ci_u32_e32 v0, vcc_lo, s13, v0, vcc_lo
@@ -1747,34 +1748,33 @@ define amdgpu_kernel void @udivrem_v2i64(ptr addrspace(1) %out0, ptr addrspace(1
17471748
; GFX10-NEXT: v_sub_co_u32 v14, s0, v7, s14
17481749
; GFX10-NEXT: v_subrev_co_ci_u32_e64 v15, s2, 0, v2, s0
17491750
; GFX10-NEXT: v_cndmask_b32_e64 v5, v5, v8, s1
1750-
; GFX10-NEXT: v_cndmask_b32_e32 v4, v10, v4, vcc_lo
1751+
; GFX10-NEXT: v_cndmask_b32_e32 v4, v9, v4, vcc_lo
17511752
; GFX10-NEXT: v_subrev_co_ci_u32_e64 v2, s0, s15, v2, s0
17521753
; GFX10-NEXT: v_cmp_le_u32_e64 s1, s15, v15
17531754
; GFX10-NEXT: v_cndmask_b32_e64 v8, 0, -1, s1
17541755
; GFX10-NEXT: v_cmp_le_u32_e64 s1, s14, v14
1755-
; GFX10-NEXT: v_cndmask_b32_e64 v10, 0, -1, s1
1756+
; GFX10-NEXT: v_cndmask_b32_e64 v9, 0, -1, s1
17561757
; GFX10-NEXT: v_add_co_u32 v16, s1, v6, 1
17571758
; GFX10-NEXT: v_add_co_ci_u32_e64 v17, s1, 0, v3, s1
17581759
; GFX10-NEXT: v_cmp_eq_u32_e64 s1, s15, v15
1759-
; GFX10-NEXT: v_cndmask_b32_e64 v8, v8, v10, s1
1760-
; GFX10-NEXT: v_add_co_u32 v10, s1, v16, 1
1760+
; GFX10-NEXT: v_cndmask_b32_e64 v8, v8, v9, s1
1761+
; GFX10-NEXT: v_add_co_u32 v9, s1, v16, 1
17611762
; GFX10-NEXT: v_add_co_ci_u32_e64 v18, s1, 0, v17, s1
17621763
; GFX10-NEXT: v_cmp_ne_u32_e64 s0, 0, v8
17631764
; GFX10-NEXT: v_sub_co_u32 v8, s1, v14, s14
17641765
; GFX10-NEXT: v_subrev_co_ci_u32_e64 v2, s1, 0, v2, s1
1765-
; GFX10-NEXT: v_cndmask_b32_e64 v10, v16, v10, s0
1766+
; GFX10-NEXT: v_cndmask_b32_e64 v9, v16, v9, s0
17661767
; GFX10-NEXT: v_cndmask_b32_e64 v16, v17, v18, s0
17671768
; GFX10-NEXT: v_cmp_ne_u32_e64 s1, 0, v5
17681769
; GFX10-NEXT: v_cndmask_b32_e64 v8, v14, v8, s0
17691770
; GFX10-NEXT: v_cndmask_b32_e64 v14, v15, v2, s0
17701771
; GFX10-NEXT: v_cndmask_b32_e32 v5, v11, v12, vcc_lo
1771-
; GFX10-NEXT: v_cndmask_b32_e64 v2, v6, v10, s1
1772+
; GFX10-NEXT: v_cndmask_b32_e64 v2, v6, v9, s1
17721773
; GFX10-NEXT: v_cndmask_b32_e64 v3, v3, v16, s1
17731774
; GFX10-NEXT: v_cndmask_b32_e64 v6, v7, v8, s1
17741775
; GFX10-NEXT: v_cndmask_b32_e64 v7, v13, v14, s1
1775-
; GFX10-NEXT: s_waitcnt lgkmcnt(0)
1776-
; GFX10-NEXT: global_store_dwordx4 v9, v[0:3], s[4:5]
1777-
; GFX10-NEXT: global_store_dwordx4 v9, v[4:7], s[6:7]
1776+
; GFX10-NEXT: global_store_dwordx4 v10, v[0:3], s[4:5]
1777+
; GFX10-NEXT: global_store_dwordx4 v10, v[4:7], s[6:7]
17781778
; GFX10-NEXT: s_endpgm
17791779
%div = udiv <2 x i64> %x, %y
17801780
store <2 x i64> %div, ptr addrspace(1) %out0

0 commit comments

Comments
 (0)