Files
clang-p2996/llvm/test/Transforms/Inline/AMDGPU/amdgpu-inline-alloca-argument-cost.ll
Juan Manuel MARTINEZ CAAMAÑO dd1df099ae [InlineCost][TargetTransformInfo][AMDGPU] Consider cost of alloca instructions in the caller (2/2)
Before this patch, the compiler gave a bump to the inline-threshold
when the total size of the allocas passed as arguments to the
callee was below 256 bytes.
This heuristic ignores that some of these allocas could have be removed
by SROA if inlining was applied.

Ideally, this bonus would be attributed to the threshold once the
size of all the allocas that could not be handled by SROA is known:
at the end of the InlineCost analysis.
However, we may never reach this point if the inline-cost analysis exits
early when the inline cost goes over the threshold mid-analysis.

This patch proposes:
* Attribute the bonus in the inline-threshold when allocas are passed
  as arguments (regardless of their total size).
* Assigns a cost to each alloca proportional to its size,
  such that the cost of all the allocas cancels the bonus.

Potential problems:
* This patch assumes that removing alloca instructions with SROA is
  always profitable. This may not be the case if the total size of the
  allocas is still too big to be promoted to registers/LDS.
* Redundant calls to getTotalAllocaSize
* Awkwardly, the threshold attributed contributes to the single-bb and
  vector bonus.

Reviewed By: scchan

Differential Revision: https://reviews.llvm.org/D149741
2023-06-29 09:49:16 +02:00

97 lines
3.5 KiB
LLVM

; RUN: opt -mtriple=amdgcn--amdhsa -S -passes=inline -inline-threshold=0 -debug-only=inline-cost %s 2>&1 | FileCheck %s
; REQUIRES: asserts
target datalayout = "A5"
; Verify we are properly adding cost of the -amdgpu-inline-arg-alloca-cost to the threshold.
define void @local_access_only(ptr addrspace(5) %p, i32 %idx) {
%arrayidx = getelementptr inbounds [64 x float], ptr addrspace(5) %p, i32 0, i32 %idx
%value = load float, ptr addrspace(5) %arrayidx
store float %value , ptr addrspace(5) %arrayidx, align 4
ret void
}
; Below the cutoff, the alloca cost is 0, and only the cost of the instructions saved by sroa is counted
; CHECK: Analyzing call of local_access_only... (caller:test_inliner_sroa_single_below_cutoff)
; CHECK: NumAllocaArgs: 1
; CHECK: SROACostSavings: 10
; CHECK: SROACostSavingsLost: 0
; CHECK: Threshold: 66000
define amdgpu_kernel void @test_inliner_sroa_single_below_cutoff(ptr addrspace(1) %a, i32 %n) {
entry:
%pvt_arr = alloca [64 x float], align 4, addrspace(5)
call void @local_access_only(ptr addrspace(5) %pvt_arr, i32 4)
ret void
}
; Above the cutoff, attribute a cost to the alloca
; CHECK: Analyzing call of local_access_only... (caller:test_inliner_sroa_single_above_cutoff)
; CHECK: NumAllocaArgs: 1
; CHECK: SROACostSavings: 66010
; CHECK: SROACostSavingsLost: 0
; CHECK: Threshold: 66000
define amdgpu_kernel void @test_inliner_sroa_single_above_cutoff(ptr addrspace(1) %a, i32 %n) {
entry:
%pvt_arr = alloca [65 x float], align 4, addrspace(5)
call void @local_access_only(ptr addrspace(5) %pvt_arr, i32 4)
ret void
}
define void @use_first_externally(ptr addrspace(5) %p1, ptr addrspace(5) %p2) {
call void @external(ptr addrspace(5) %p1)
%arrayidx = getelementptr inbounds [64 x float], ptr addrspace(5) %p2, i32 0, i32 7
%value = load float, ptr addrspace(5) %arrayidx
store float %value , ptr addrspace(5) %arrayidx, align 4
ret void
}
define void @use_both_externally(ptr addrspace(5) %p1, ptr addrspace(5) %p2) {
call void @external(ptr addrspace(5) %p1)
call void @external(ptr addrspace(5) %p2)
ret void
}
; One array cannot get handled by SROA
; CHECK: Analyzing call of use_first_externally... (caller:test_inliner_sroa_double)
; CHECK: NumAllocaArgs: 2
; CHECK: SROACostSavings: 32502
; CHECK: SROACostSavingsLost: 33507
; CHECK: Threshold: 66000
define amdgpu_kernel void @test_inliner_sroa_double() {
entry:
%pvt_arr1 = alloca [33 x float], align 4, addrspace(5)
%pvt_arr2 = alloca [32 x float], align 4, addrspace(5)
call void @use_first_externally(ptr addrspace(5) %pvt_arr1, ptr addrspace(5) %pvt_arr2)
ret void
}
; The two arrays cannot get handled by SROA
; CHECK: Analyzing call of use_both_externally... (caller:test_inliner_no_sroa)
; CHECK: NumAllocaArgs: 2
; CHECK: SROACostSavings: 0
; CHECK: SROACostSavingsLost: 65999
; CHECK: Threshold: 66000
define amdgpu_kernel void @test_inliner_no_sroa() {
entry:
%pvt_arr1 = alloca [33 x float], align 4, addrspace(5)
%pvt_arr2 = alloca [32 x float], align 4, addrspace(5)
call void @use_both_externally(ptr addrspace(5) %pvt_arr1, ptr addrspace(5) %pvt_arr2)
ret void
}
; No private arrays
; CHECK: Analyzing call of use_both_externally... (caller:test_inliner_no_alloc)
; CHECK: NumAllocaArgs: 0
; CHECK: SROACostSavings: 0
; CHECK: SROACostSavingsLost: 0
; CHECK: Threshold: 0
define amdgpu_kernel void @test_inliner_no_alloc(ptr addrspace(5) %a, ptr addrspace(5) %b) {
entry:
call void @use_both_externally(ptr addrspace(5) %a, ptr addrspace(5) %b)
ret void
}
declare void @external(ptr addrspace(5) %p)