Skip to content

Commit 0e498ef

Browse files
committed
[Attributor][AMDGPU] Improve the handling of indirect calls
1 parent ed46483 commit 0e498ef

File tree

5 files changed

+29
-19
lines changed

5 files changed

+29
-19
lines changed

llvm/include/llvm/Transforms/IPO/Attributor.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1448,7 +1448,7 @@ struct AttributorConfig {
14481448
/// Callback function to determine if an indirect call targets should be made
14491449
/// direct call targets (with an if-cascade).
14501450
std::function<bool(Attributor &A, const AbstractAttribute &AA, CallBase &CB,
1451-
Function &AssummedCallee)>
1451+
Function &AssummedCallee, bool IsSingleton)>
14521452
IndirectCalleeSpecializationCallback = nullptr;
14531453

14541454
/// Helper to update an underlying call graph and to delete functions.
@@ -1718,10 +1718,11 @@ struct Attributor {
17181718
/// Return true if we should specialize the call site \b CB for the potential
17191719
/// callee \p Fn.
17201720
bool shouldSpecializeCallSiteForCallee(const AbstractAttribute &AA,
1721-
CallBase &CB, Function &Callee) {
1721+
CallBase &CB, Function &Callee,
1722+
bool IsSingleton) {
17221723
return Configuration.IndirectCalleeSpecializationCallback
1723-
? Configuration.IndirectCalleeSpecializationCallback(*this, AA,
1724-
CB, Callee)
1724+
? Configuration.IndirectCalleeSpecializationCallback(
1725+
*this, AA, CB, Callee, IsSingleton)
17251726
: true;
17261727
}
17271728

llvm/lib/Target/AMDGPU/AMDGPUAttributor.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "GCNSubtarget.h"
1515
#include "Utils/AMDGPUBaseInfo.h"
1616
#include "llvm/Analysis/CycleAnalysis.h"
17+
#include "llvm/Analysis/TargetTransformInfo.h"
1718
#include "llvm/CodeGen/TargetPassConfig.h"
1819
#include "llvm/IR/IntrinsicsAMDGPU.h"
1920
#include "llvm/IR/IntrinsicsR600.h"
@@ -1041,11 +1042,28 @@ static bool runImpl(Module &M, AnalysisGetter &AG, TargetMachine &TM,
10411042
&AAPointerInfo::ID, &AAPotentialConstantValues::ID,
10421043
&AAUnderlyingObjects::ID, &AAIndirectCallInfo::ID});
10431044

1045+
/// Helper to decide if we should specialize the indirect \p CB for \p Callee.
1046+
/// \p IsSingleton indicates whether the \p Callee is the only assumed callee.
1047+
auto IndirectCalleeSpecializationCallback =
1048+
[&](Attributor &A, const AbstractAttribute &AA, CallBase &CB,
1049+
Function &Callee, bool IsSingleton) {
1050+
if (AMDGPU::isEntryFunctionCC(Callee.getCallingConv()))
1051+
return false;
1052+
// Singleton functions should be specialized.
1053+
if (IsSingleton)
1054+
return true;
1055+
// Otherwise specialize uniform values.
1056+
const auto &TTI = TM.getTargetTransformInfo(*CB.getCaller());
1057+
return TTI.isAlwaysUniform(CB.getCalledOperand());
1058+
};
1059+
10441060
AttributorConfig AC(CGUpdater);
10451061
AC.IsClosedWorldModule = HasWholeProgramVisibility;
10461062
AC.Allowed = &Allowed;
10471063
AC.IsModulePass = true;
10481064
AC.DefaultInitializeLiveInternals = false;
1065+
AC.IndirectCalleeSpecializationCallback =
1066+
IndirectCalleeSpecializationCallback;
10491067
AC.IPOAmendableCB = [](const Function &F) {
10501068
return F.getCallingConv() == CallingConv::AMDGPU_KERNEL;
10511069
};

llvm/lib/Transforms/IPO/Attributor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3836,7 +3836,7 @@ static bool runAttributorOnFunctions(InformationCache &InfoCache,
38363836
if (MaxSpecializationPerCB.getNumOccurrences()) {
38373837
AC.IndirectCalleeSpecializationCallback =
38383838
[&](Attributor &, const AbstractAttribute &AA, CallBase &CB,
3839-
Function &Callee) {
3839+
Function &Callee, bool IsSingleton) {
38403840
if (MaxSpecializationPerCB == 0)
38413841
return false;
38423842
auto &Set = IndirectCalleeTrackingMap[&CB];

llvm/lib/Transforms/IPO/AttributorAttributes.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12347,7 +12347,8 @@ struct AAIndirectCallInfoCallSite : public AAIndirectCallInfo {
1234712347
SmallVector<Function *, 8> SkippedAssumedCallees;
1234812348
SmallVector<std::pair<CallInst *, Instruction *>> NewCalls;
1234912349
for (Function *NewCallee : AssumedCallees) {
12350-
if (!A.shouldSpecializeCallSiteForCallee(*this, *CB, *NewCallee)) {
12350+
if (!A.shouldSpecializeCallSiteForCallee(*this, *CB, *NewCallee,
12351+
AssumedCallees.size() == 1)) {
1235112352
SkippedAssumedCallees.push_back(NewCallee);
1235212353
SpecializedForAllCallees = false;
1235312354
continue;

llvm/test/CodeGen/AMDGPU/amdgpu-attributor-no-agpr.ll

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -231,19 +231,7 @@ define amdgpu_kernel void @indirect_calls_none_agpr(i1 %cond) {
231231
; CHECK-LABEL: define amdgpu_kernel void @indirect_calls_none_agpr(
232232
; CHECK-SAME: i1 [[COND:%.*]]) #[[ATTR0]] {
233233
; CHECK-NEXT: [[FPTR:%.*]] = select i1 [[COND]], ptr @empty, ptr @also_empty
234-
; CHECK-NEXT: [[TMP1:%.*]] = icmp eq ptr [[FPTR]], @also_empty
235-
; CHECK-NEXT: br i1 [[TMP1]], label [[TMP2:%.*]], label [[TMP3:%.*]]
236-
; CHECK: 2:
237-
; CHECK-NEXT: call void @also_empty()
238-
; CHECK-NEXT: br label [[TMP6:%.*]]
239-
; CHECK: 3:
240-
; CHECK-NEXT: br i1 true, label [[TMP4:%.*]], label [[TMP5:%.*]]
241-
; CHECK: 4:
242-
; CHECK-NEXT: call void @empty()
243-
; CHECK-NEXT: br label [[TMP6]]
244-
; CHECK: 5:
245-
; CHECK-NEXT: unreachable
246-
; CHECK: 6:
234+
; CHECK-NEXT: call void [[FPTR]](), !callees [[META0:![0-9]+]]
247235
; CHECK-NEXT: ret void
248236
;
249237
%fptr = select i1 %cond, ptr @empty, ptr @also_empty
@@ -265,3 +253,5 @@ attributes #0 = { "amdgpu-no-agpr" }
265253
; CHECK: attributes #[[ATTR8:[0-9]+]] = { nocallback nofree nounwind willreturn memory(argmem: readwrite) "target-cpu"="gfx90a" }
266254
; CHECK: attributes #[[ATTR9]] = { "amdgpu-no-agpr" }
267255
;.
256+
; CHECK: [[META0]] = !{ptr @also_empty, ptr @empty}
257+
;.

0 commit comments

Comments
 (0)