Skip to content

Commit

Permalink
Rewrite load-store-vectorizer.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jlebar committed May 26, 2023
1 parent 9249129 commit 2be0abb
Show file tree
Hide file tree
Showing 29 changed files with 2,935 additions and 1,656 deletions.
2,035 changes: 1,095 additions & 940 deletions llvm/lib/Transforms/Vectorize/LoadStoreVectorizer.cpp

Large diffs are not rendered by default.

562 changes: 280 additions & 282 deletions llvm/test/CodeGen/AMDGPU/GlobalISel/sdivrem.ll

Large diffs are not rendered by default.

76 changes: 38 additions & 38 deletions llvm/test/CodeGen/AMDGPU/GlobalISel/udivrem.ll
Original file line number Diff line number Diff line change
Expand Up @@ -985,8 +985,8 @@ define amdgpu_kernel void @udivrem_v4i32(ptr addrspace(1) %out0, ptr addrspace(1
define amdgpu_kernel void @udivrem_v2i64(ptr addrspace(1) %out0, ptr addrspace(1) %out1, <2 x i64> %x, <2 x i64> %y) {
; GFX8-LABEL: udivrem_v2i64:
; GFX8: ; %bb.0:
; GFX8-NEXT: s_load_dwordx8 s[8:15], s[4:5], 0x10
; GFX8-NEXT: s_load_dwordx4 s[4:7], s[4:5], 0x0
; GFX8-NEXT: s_load_dwordx4 s[12:15], s[4:5], 0x20
; GFX8-NEXT: s_load_dwordx8 s[4:11], s[4:5], 0x0
; GFX8-NEXT: s_waitcnt lgkmcnt(0)
; GFX8-NEXT: v_cvt_f32_u32_e32 v0, s13
; GFX8-NEXT: v_cvt_f32_u32_e32 v1, s12
Expand Down Expand Up @@ -1255,7 +1255,7 @@ define amdgpu_kernel void @udivrem_v2i64(ptr addrspace(1) %out0, ptr addrspace(1
;
; GFX9-LABEL: udivrem_v2i64:
; GFX9: ; %bb.0:
; GFX9-NEXT: s_load_dwordx8 s[8:15], s[4:5], 0x10
; GFX9-NEXT: s_load_dwordx4 s[12:15], s[4:5], 0x20
; GFX9-NEXT: s_waitcnt lgkmcnt(0)
; GFX9-NEXT: v_cvt_f32_u32_e32 v0, s13
; GFX9-NEXT: v_cvt_f32_u32_e32 v1, s12
Expand All @@ -1264,7 +1264,7 @@ define amdgpu_kernel void @udivrem_v2i64(ptr addrspace(1) %out0, ptr addrspace(1
; GFX9-NEXT: v_mul_f32_e32 v0, 0x4f800000, v0
; GFX9-NEXT: v_add_f32_e32 v0, v0, v1
; GFX9-NEXT: v_rcp_iflag_f32_e32 v0, v0
; GFX9-NEXT: s_load_dwordx4 s[4:7], s[4:5], 0x0
; GFX9-NEXT: s_load_dwordx8 s[4:11], s[4:5], 0x0
; GFX9-NEXT: v_mul_f32_e32 v0, 0x5f7ffffc, v0
; GFX9-NEXT: v_mul_f32_e32 v1, 0x2f800000, v0
; GFX9-NEXT: v_trunc_f32_e32 v2, v1
Expand Down Expand Up @@ -1325,6 +1325,7 @@ define amdgpu_kernel void @udivrem_v2i64(ptr addrspace(1) %out0, ptr addrspace(1
; GFX9-NEXT: v_add3_u32 v1, v5, v2, v1
; GFX9-NEXT: v_add_co_u32_e32 v0, vcc, v3, v0
; GFX9-NEXT: v_addc_co_u32_e32 v1, vcc, v4, v1, vcc
; GFX9-NEXT: s_waitcnt lgkmcnt(0)
; GFX9-NEXT: v_mul_lo_u32 v2, s9, v0
; GFX9-NEXT: v_mul_lo_u32 v3, s8, v1
; GFX9-NEXT: v_mul_hi_u32 v4, s8, v0
Expand Down Expand Up @@ -1510,14 +1511,13 @@ define amdgpu_kernel void @udivrem_v2i64(ptr addrspace(1) %out0, ptr addrspace(1
; GFX9-NEXT: v_cndmask_b32_e32 v9, v13, v20, vcc
; GFX9-NEXT: v_cndmask_b32_e64 v7, v8, v7, s[0:1]
; GFX9-NEXT: v_cndmask_b32_e64 v8, v11, v9, s[0:1]
; GFX9-NEXT: s_waitcnt lgkmcnt(0)
; GFX9-NEXT: global_store_dwordx4 v0, v[1:4], s[4:5]
; GFX9-NEXT: global_store_dwordx4 v0, v[5:8], s[6:7]
; GFX9-NEXT: s_endpgm
;
; GFX10-LABEL: udivrem_v2i64:
; GFX10: ; %bb.0:
; GFX10-NEXT: s_load_dwordx8 s[8:15], s[4:5], 0x10
; GFX10-NEXT: s_load_dwordx4 s[12:15], s[4:5], 0x20
; GFX10-NEXT: s_waitcnt lgkmcnt(0)
; GFX10-NEXT: v_cvt_f32_u32_e32 v0, s13
; GFX10-NEXT: v_cvt_f32_u32_e32 v1, s15
Expand Down Expand Up @@ -1616,11 +1616,11 @@ define amdgpu_kernel void @udivrem_v2i64(ptr addrspace(1) %out0, ptr addrspace(1
; GFX10-NEXT: v_mul_lo_u32 v10, v5, v1
; GFX10-NEXT: v_mul_lo_u32 v11, v4, v1
; GFX10-NEXT: v_mul_hi_u32 v14, v5, v1
; GFX10-NEXT: v_mul_hi_u32 v1, v4, v1
; GFX10-NEXT: s_load_dwordx8 s[4:11], s[4:5], 0x0
; GFX10-NEXT: v_mul_lo_u32 v15, v8, v3
; GFX10-NEXT: v_mul_lo_u32 v16, v6, v3
; GFX10-NEXT: v_mul_hi_u32 v17, v8, v3
; GFX10-NEXT: v_mul_hi_u32 v3, v6, v3
; GFX10-NEXT: v_mul_hi_u32 v1, v4, v1
; GFX10-NEXT: v_add_co_u32 v10, s0, v12, v10
; GFX10-NEXT: v_cndmask_b32_e64 v12, 0, 1, s0
; GFX10-NEXT: v_add_co_u32 v0, s0, v11, v0
Expand All @@ -1642,65 +1642,66 @@ define amdgpu_kernel void @udivrem_v2i64(ptr addrspace(1) %out0, ptr addrspace(1
; GFX10-NEXT: v_add_nc_u32_e32 v11, v11, v13
; GFX10-NEXT: v_cndmask_b32_e64 v9, 0, 1, s0
; GFX10-NEXT: v_add_nc_u32_e32 v7, v15, v7
; GFX10-NEXT: v_mul_hi_u32 v3, v6, v3
; GFX10-NEXT: v_add_co_u32 v0, vcc_lo, v5, v0
; GFX10-NEXT: v_add_nc_u32_e32 v10, v16, v10
; GFX10-NEXT: v_add3_u32 v1, v11, v9, v1
; GFX10-NEXT: v_add_co_u32 v2, s0, v2, v7
; GFX10-NEXT: v_add_nc_u32_e32 v10, v16, v10
; GFX10-NEXT: v_cndmask_b32_e64 v7, 0, 1, s0
; GFX10-NEXT: v_mul_hi_u32 v5, s8, v0
; GFX10-NEXT: v_add_co_ci_u32_e32 v1, vcc_lo, v4, v1, vcc_lo
; GFX10-NEXT: s_waitcnt lgkmcnt(0)
; GFX10-NEXT: v_mul_lo_u32 v4, s9, v0
; GFX10-NEXT: v_mul_hi_u32 v5, s8, v0
; GFX10-NEXT: v_add3_u32 v3, v10, v7, v3
; GFX10-NEXT: v_mul_hi_u32 v0, s9, v0
; GFX10-NEXT: v_mul_lo_u32 v7, s8, v1
; GFX10-NEXT: v_mul_lo_u32 v10, s9, v1
; GFX10-NEXT: v_mul_hi_u32 v0, s9, v0
; GFX10-NEXT: v_mul_lo_u32 v9, s9, v1
; GFX10-NEXT: v_add_co_u32 v2, vcc_lo, v8, v2
; GFX10-NEXT: v_add_co_ci_u32_e32 v3, vcc_lo, v6, v3, vcc_lo
; GFX10-NEXT: v_mul_hi_u32 v6, s8, v1
; GFX10-NEXT: v_mul_hi_u32 v1, s9, v1
; GFX10-NEXT: v_add_co_u32 v4, s0, v4, v7
; GFX10-NEXT: v_cndmask_b32_e64 v7, 0, 1, s0
; GFX10-NEXT: v_add_co_u32 v0, s0, v10, v0
; GFX10-NEXT: v_add_co_u32 v0, s0, v9, v0
; GFX10-NEXT: v_cndmask_b32_e64 v8, 0, 1, s0
; GFX10-NEXT: v_add_co_u32 v4, s0, v4, v5
; GFX10-NEXT: v_cndmask_b32_e64 v4, 0, 1, s0
; GFX10-NEXT: v_add_co_u32 v0, s0, v0, v6
; GFX10-NEXT: v_cndmask_b32_e64 v5, 0, 1, s0
; GFX10-NEXT: v_mul_lo_u32 v6, s11, v2
; GFX10-NEXT: v_mul_hi_u32 v1, s9, v1
; GFX10-NEXT: v_add_nc_u32_e32 v4, v7, v4
; GFX10-NEXT: v_mul_lo_u32 v6, s11, v2
; GFX10-NEXT: v_mul_lo_u32 v7, s10, v3
; GFX10-NEXT: v_mul_lo_u32 v10, s11, v3
; GFX10-NEXT: v_add_nc_u32_e32 v5, v8, v5
; GFX10-NEXT: v_mul_hi_u32 v8, s10, v2
; GFX10-NEXT: v_add_co_u32 v4, s0, v0, v4
; GFX10-NEXT: v_cndmask_b32_e64 v0, 0, 1, s0
; GFX10-NEXT: v_mul_hi_u32 v2, s11, v2
; GFX10-NEXT: v_mul_hi_u32 v11, s10, v3
; GFX10-NEXT: v_mul_lo_u32 v9, s11, v3
; GFX10-NEXT: v_mul_hi_u32 v10, s10, v3
; GFX10-NEXT: v_add_co_u32 v6, s0, v6, v7
; GFX10-NEXT: v_add3_u32 v5, v5, v0, v1
; GFX10-NEXT: v_cndmask_b32_e64 v7, 0, 1, s0
; GFX10-NEXT: v_mad_u64_u32 v[0:1], s0, s12, v4, 0
; GFX10-NEXT: v_mul_lo_u32 v12, s13, v4
; GFX10-NEXT: v_mul_lo_u32 v13, s12, v5
; GFX10-NEXT: v_add_co_u32 v2, s0, v10, v2
; GFX10-NEXT: v_cndmask_b32_e64 v10, 0, 1, s0
; GFX10-NEXT: v_mul_lo_u32 v11, s13, v4
; GFX10-NEXT: v_mul_lo_u32 v12, s12, v5
; GFX10-NEXT: v_add_co_u32 v2, s0, v9, v2
; GFX10-NEXT: v_cndmask_b32_e64 v9, 0, 1, s0
; GFX10-NEXT: v_add_co_u32 v6, s0, v6, v8
; GFX10-NEXT: v_cndmask_b32_e64 v6, 0, 1, s0
; GFX10-NEXT: v_add_co_u32 v2, s0, v2, v11
; GFX10-NEXT: v_add_co_u32 v2, s0, v2, v10
; GFX10-NEXT: v_cndmask_b32_e64 v8, 0, 1, s0
; GFX10-NEXT: v_add3_u32 v1, v1, v13, v12
; GFX10-NEXT: v_add3_u32 v1, v1, v12, v11
; GFX10-NEXT: v_add_nc_u32_e32 v6, v7, v6
; GFX10-NEXT: v_mul_hi_u32 v3, s11, v3
; GFX10-NEXT: s_load_dwordx4 s[4:7], s[4:5], 0x0
; GFX10-NEXT: v_add_nc_u32_e32 v7, v10, v8
; GFX10-NEXT: v_mov_b32_e32 v10, 0
; GFX10-NEXT: v_add_nc_u32_e32 v7, v9, v8
; GFX10-NEXT: v_sub_nc_u32_e32 v8, s9, v1
; GFX10-NEXT: v_sub_co_u32 v10, vcc_lo, s8, v0
; GFX10-NEXT: v_sub_co_u32 v9, vcc_lo, s8, v0
; GFX10-NEXT: v_sub_co_ci_u32_e64 v11, s0, s9, v1, vcc_lo
; GFX10-NEXT: v_subrev_co_ci_u32_e32 v0, vcc_lo, s13, v8, vcc_lo
; GFX10-NEXT: v_cmp_le_u32_e32 vcc_lo, s12, v10
; GFX10-NEXT: v_mov_b32_e32 v9, 0
; GFX10-NEXT: v_cmp_le_u32_e32 vcc_lo, s12, v9
; GFX10-NEXT: v_cndmask_b32_e64 v1, 0, -1, vcc_lo
; GFX10-NEXT: v_sub_co_u32 v8, vcc_lo, v10, s12
; GFX10-NEXT: v_sub_co_u32 v8, vcc_lo, v9, s12
; GFX10-NEXT: v_subrev_co_ci_u32_e64 v12, s0, 0, v0, vcc_lo
; GFX10-NEXT: v_cmp_le_u32_e64 s0, s13, v11
; GFX10-NEXT: v_subrev_co_ci_u32_e32 v0, vcc_lo, s13, v0, vcc_lo
Expand Down Expand Up @@ -1747,34 +1748,33 @@ define amdgpu_kernel void @udivrem_v2i64(ptr addrspace(1) %out0, ptr addrspace(1
; GFX10-NEXT: v_sub_co_u32 v14, s0, v7, s14
; GFX10-NEXT: v_subrev_co_ci_u32_e64 v15, s2, 0, v2, s0
; GFX10-NEXT: v_cndmask_b32_e64 v5, v5, v8, s1
; GFX10-NEXT: v_cndmask_b32_e32 v4, v10, v4, vcc_lo
; GFX10-NEXT: v_cndmask_b32_e32 v4, v9, v4, vcc_lo
; GFX10-NEXT: v_subrev_co_ci_u32_e64 v2, s0, s15, v2, s0
; GFX10-NEXT: v_cmp_le_u32_e64 s1, s15, v15
; GFX10-NEXT: v_cndmask_b32_e64 v8, 0, -1, s1
; GFX10-NEXT: v_cmp_le_u32_e64 s1, s14, v14
; GFX10-NEXT: v_cndmask_b32_e64 v10, 0, -1, s1
; GFX10-NEXT: v_cndmask_b32_e64 v9, 0, -1, s1
; GFX10-NEXT: v_add_co_u32 v16, s1, v6, 1
; GFX10-NEXT: v_add_co_ci_u32_e64 v17, s1, 0, v3, s1
; GFX10-NEXT: v_cmp_eq_u32_e64 s1, s15, v15
; GFX10-NEXT: v_cndmask_b32_e64 v8, v8, v10, s1
; GFX10-NEXT: v_add_co_u32 v10, s1, v16, 1
; GFX10-NEXT: v_cndmask_b32_e64 v8, v8, v9, s1
; GFX10-NEXT: v_add_co_u32 v9, s1, v16, 1
; GFX10-NEXT: v_add_co_ci_u32_e64 v18, s1, 0, v17, s1
; GFX10-NEXT: v_cmp_ne_u32_e64 s0, 0, v8
; GFX10-NEXT: v_sub_co_u32 v8, s1, v14, s14
; GFX10-NEXT: v_subrev_co_ci_u32_e64 v2, s1, 0, v2, s1
; GFX10-NEXT: v_cndmask_b32_e64 v10, v16, v10, s0
; GFX10-NEXT: v_cndmask_b32_e64 v9, v16, v9, s0
; GFX10-NEXT: v_cndmask_b32_e64 v16, v17, v18, s0
; GFX10-NEXT: v_cmp_ne_u32_e64 s1, 0, v5
; GFX10-NEXT: v_cndmask_b32_e64 v8, v14, v8, s0
; GFX10-NEXT: v_cndmask_b32_e64 v14, v15, v2, s0
; GFX10-NEXT: v_cndmask_b32_e32 v5, v11, v12, vcc_lo
; GFX10-NEXT: v_cndmask_b32_e64 v2, v6, v10, s1
; GFX10-NEXT: v_cndmask_b32_e64 v2, v6, v9, s1
; GFX10-NEXT: v_cndmask_b32_e64 v3, v3, v16, s1
; GFX10-NEXT: v_cndmask_b32_e64 v6, v7, v8, s1
; GFX10-NEXT: v_cndmask_b32_e64 v7, v13, v14, s1
; GFX10-NEXT: s_waitcnt lgkmcnt(0)
; GFX10-NEXT: global_store_dwordx4 v9, v[0:3], s[4:5]
; GFX10-NEXT: global_store_dwordx4 v9, v[4:7], s[6:7]
; GFX10-NEXT: global_store_dwordx4 v10, v[0:3], s[4:5]
; GFX10-NEXT: global_store_dwordx4 v10, v[4:7], s[6:7]
; GFX10-NEXT: s_endpgm
%div = udiv <2 x i64> %x, %y
store <2 x i64> %div, ptr addrspace(1) %out0
Expand Down
Loading

0 comments on commit 2be0abb

Please sign in to comment.