[llvm-branch-commits] [llvm] [ThinLTO][AMDGPU] Implement `TTI::getCallTypeId()` (PR #245046)

via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Sun Nov 22 06:50:29 PST 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-backend-amdgpu

Author: Shilei Tian (shiltian)

<details>
<summary>Changes</summary>

We want to propagate attributes along the call graph at the thin link to enable
better code generation for each module. This requires a precise call graph.
ThinLTO supports call type IDs for indirect function calls, and this PR enables
that support for AMDGPU.

To maintain consistency with object linking, we need to use the same rules for
computing call type IDs, even though they might be a little less precise at the
IR level.

This PR was assisted by AI but I reviewed all the changes.


---
Full diff: https://github.com/llvm/llvm-project/pull/245046.diff


6 Files Affected:

- (modified) llvm/lib/Target/AMDGPU/AMDGPUAsmPrinter.cpp (+2-34) 
- (modified) llvm/lib/Target/AMDGPU/AMDGPUTargetTransformInfo.cpp (+11) 
- (modified) llvm/lib/Target/AMDGPU/AMDGPUTargetTransformInfo.h (+3) 
- (modified) llvm/lib/Target/AMDGPU/Utils/AMDGPUBaseInfo.cpp (+32) 
- (modified) llvm/lib/Target/AMDGPU/Utils/AMDGPUBaseInfo.h (+10) 
- (added) llvm/test/ThinLTO/AMDGPU/call-type-ids.ll (+139) 


``````````diff
diff --git a/llvm/lib/Target/AMDGPU/AMDGPUAsmPrinter.cpp b/llvm/lib/Target/AMDGPU/AMDGPUAsmPrinter.cpp
index 6958f80a2f3ac8..651aca9d74d69c 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPUAsmPrinter.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPUAsmPrinter.cpp
@@ -616,38 +616,6 @@ void AMDGPUAsmPrinter::validateMCResourceInfo(Function &F) {
   }
 }
 
-static void appendTypeEncoding(std::string &Enc, Type *Ty, const DataLayout &DL,
-                               bool IsReturnType) {
-  if (Ty->isVoidTy()) {
-    Enc += 'v';
-    return;
-  }
-  uint64_t Bits = DL.getTypeSizeInBits(Ty);
-  // Zero-sized non-void types (e.g. `{}` or `[0 x i8]`) consume no ABI
-  // registers. For returns, emit the same no-result marker as void so the
-  // parameter encoding still has an explicit return-type prefix.
-  if (Bits == 0) {
-    if (IsReturnType)
-      Enc += 'v';
-    return;
-  }
-  if (Bits <= 32)
-    Enc += 'i';
-  else if (Bits <= 64)
-    Enc += 'l';
-  else
-    Enc.append(divideCeil(Bits, 32), 'i');
-}
-
-static std::string computeTypeId(const FunctionType *FTy,
-                                 const DataLayout &DL) {
-  std::string Enc;
-  appendTypeEncoding(Enc, FTy->getReturnType(), DL, /*IsReturnType=*/true);
-  for (Type *ParamTy : FTy->params())
-    appendTypeEncoding(Enc, ParamTy, DL, /*IsReturnType=*/false);
-  return Enc;
-}
-
 void AMDGPUAsmPrinter::collectCallEdge(const MachineInstr &MI) {
   if (!AMDGPUTargetMachine::EnableObjectLinking)
     return;
@@ -683,7 +651,7 @@ void AMDGPUAsmPrinter::emitAMDGPUInfo(Module &M) {
                                        /*IgnoreAssumeLikeCalls=*/true,
                                        /*IgnoreLLVMUsed=*/true)) {
       AddrTakenTypeIds[&F] =
-          computeTypeId(F.getFunctionType(), M.getDataLayout());
+          AMDGPU::getFunctionTypeId(F.getFunctionType(), M.getDataLayout());
     }
 
     if (F.isDeclaration())
@@ -696,7 +664,7 @@ void AMDGPUAsmPrinter::emitAMDGPUInfo(Module &M) {
         if (!CB || !CB->isIndirectCall())
           continue;
         std::string TId =
-            computeTypeId(CB->getFunctionType(), M.getDataLayout());
+            AMDGPU::getFunctionTypeId(CB->getFunctionType(), M.getDataLayout());
         if (SeenTypeIds.insert(TId).second)
           IndirectCalls.push_back({&F, std::move(TId)});
       }
diff --git a/llvm/lib/Target/AMDGPU/AMDGPUTargetTransformInfo.cpp b/llvm/lib/Target/AMDGPU/AMDGPUTargetTransformInfo.cpp
index 3d2a767c51a050..928ea1c7a92ef1 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPUTargetTransformInfo.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPUTargetTransformInfo.cpp
@@ -19,7 +19,9 @@
 #include "AMDGPUTargetMachine.h"
 #include "MCTargetDesc/AMDGPUMCTargetDesc.h"
 #include "SIModeRegisterDefaults.h"
+#include "Utils/AMDGPUBaseInfo.h"
 #include "llvm/ADT/SmallBitVector.h"
+#include "llvm/ADT/StringExtras.h"
 #include "llvm/Analysis/InlineCost.h"
 #include "llvm/Analysis/LoopInfo.h"
 #include "llvm/Analysis/ValueTracking.h"
@@ -29,6 +31,7 @@
 #include "llvm/IR/IntrinsicsAMDGPU.h"
 #include "llvm/IR/PatternMatch.h"
 #include "llvm/Support/KnownBits.h"
+#include "llvm/Support/MD5.h"
 #include <optional>
 
 using namespace llvm;
@@ -1888,6 +1891,14 @@ bool GCNTTIImpl::isProfitableToSinkOperands(Instruction *I,
   return !Ops.empty();
 }
 
+std::optional<uint64_t> GCNTTIImpl::getCallTypeId(FunctionType *FTy,
+                                                  CallingConv::ID CC) const {
+  // Signatures match by the sizes of their types, not by their IR types. This
+  // is the same rule as for the type IDs in the .amdgpu.info section.
+  return MD5Hash("amdgpu.v1:cc" + utostr(CC) + ":" +
+                 AMDGPU::getFunctionTypeId(FTy, DL));
+}
+
 bool GCNTTIImpl::areInlineCompatible(const Function *Caller,
                                      const Function *Callee) const {
   const TargetMachine &TM = getTLI()->getTargetMachine();
diff --git a/llvm/lib/Target/AMDGPU/AMDGPUTargetTransformInfo.h b/llvm/lib/Target/AMDGPU/AMDGPUTargetTransformInfo.h
index a414f214dd6645..c6eea683a06118 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPUTargetTransformInfo.h
+++ b/llvm/lib/Target/AMDGPU/AMDGPUTargetTransformInfo.h
@@ -272,6 +272,9 @@ class GCNTTIImpl final : public BasicTTIImplBase<GCNTTIImpl> {
   bool areInlineCompatible(const Function *Caller,
                            const Function *Callee) const override;
 
+  std::optional<uint64_t> getCallTypeId(FunctionType *FTy,
+                                        CallingConv::ID CC) const override;
+
   int getInliningLastCallToStaticBonus() const override;
   unsigned getInliningThresholdMultiplier() const override { return 11; }
   unsigned adjustInliningThreshold(const CallBase *CB) const override;
diff --git a/llvm/lib/Target/AMDGPU/Utils/AMDGPUBaseInfo.cpp b/llvm/lib/Target/AMDGPU/Utils/AMDGPUBaseInfo.cpp
index 19dd0e6214ec5b..64b8e8e1ca3e89 100644
--- a/llvm/lib/Target/AMDGPU/Utils/AMDGPUBaseInfo.cpp
+++ b/llvm/lib/Target/AMDGPU/Utils/AMDGPUBaseInfo.cpp
@@ -16,6 +16,7 @@
 #include "llvm/BinaryFormat/ELF.h"
 #include "llvm/IR/Attributes.h"
 #include "llvm/IR/Constants.h"
+#include "llvm/IR/DataLayout.h"
 #include "llvm/IR/Function.h"
 #include "llvm/IR/GlobalValue.h"
 #include "llvm/IR/IntrinsicInst.h"
@@ -2391,6 +2392,37 @@ unsigned getDynamicVGPRBlockSize(const Function &F) {
   return 0;
 }
 
+static void appendTypeEncoding(std::string &Enc, Type *Ty, const DataLayout &DL,
+                               bool IsReturnType) {
+  if (Ty->isVoidTy()) {
+    Enc += 'v';
+    return;
+  }
+  uint64_t Bits = DL.getTypeSizeInBits(Ty);
+  // Zero-sized non-void types (e.g. `{}` or `[0 x i8]`) consume no ABI
+  // registers. For returns, emit the same no-result marker as void so the
+  // parameter encoding still has an explicit return-type prefix.
+  if (Bits == 0) {
+    if (IsReturnType)
+      Enc += 'v';
+    return;
+  }
+  if (Bits <= 32)
+    Enc += 'i';
+  else if (Bits <= 64)
+    Enc += 'l';
+  else
+    Enc.append(divideCeil(Bits, 32), 'i');
+}
+
+std::string getFunctionTypeId(const FunctionType *FTy, const DataLayout &DL) {
+  std::string Enc;
+  appendTypeEncoding(Enc, FTy->getReturnType(), DL, /*IsReturnType=*/true);
+  for (Type *ParamTy : FTy->params())
+    appendTypeEncoding(Enc, ParamTy, DL, /*IsReturnType=*/false);
+  return Enc;
+}
+
 bool hasMIMG_R128(const MCSubtargetInfo &STI) {
   return STI.hasFeature(AMDGPU::FeatureMIMG_R128) &&
          !STI.hasFeature(AMDGPU::FeatureR128A16);
diff --git a/llvm/lib/Target/AMDGPU/Utils/AMDGPUBaseInfo.h b/llvm/lib/Target/AMDGPU/Utils/AMDGPUBaseInfo.h
index 933111145e5799..475610d4358120 100644
--- a/llvm/lib/Target/AMDGPU/Utils/AMDGPUBaseInfo.h
+++ b/llvm/lib/Target/AMDGPU/Utils/AMDGPUBaseInfo.h
@@ -34,7 +34,9 @@ namespace llvm {
 
 struct Align;
 class Argument;
+class DataLayout;
 class Function;
+class FunctionType;
 class GlobalValue;
 class MachineInstr;
 class MCInstrInfo;
@@ -1400,6 +1402,14 @@ bool getHasDepthExport(const Function &F);
 // the attribute is missing or its value is invalid.
 unsigned getDynamicVGPRBlockSize(const Function &F);
 
+/// \returns The type ID of the function type \p FTy, which the .amdgpu.info
+/// section and the call type IDs in the module summary use. It encodes the
+/// return type, then each parameter type, by size: 'v' for a void or zero-size
+/// return type, 'i' for at most 32 bits, 'l' for at most 64 bits, and one 'i'
+/// for each 32 bits of a larger type. Zero-size parameter types are not
+/// encoded.
+std::string getFunctionTypeId(const FunctionType *FTy, const DataLayout &DL);
+
 LLVM_READNONE
 constexpr bool isShader(CallingConv::ID CC) {
   switch (CC) {
diff --git a/llvm/test/ThinLTO/AMDGPU/call-type-ids.ll b/llvm/test/ThinLTO/AMDGPU/call-type-ids.ll
new file mode 100644
index 00000000000000..8370899c96ba7e
--- /dev/null
+++ b/llvm/test/ThinLTO/AMDGPU/call-type-ids.ll
@@ -0,0 +1,139 @@
+;; Test the call type IDs in the module summary of an AMDGPU module, in the
+;; per-module summary and in the combined index. AMDGPU call type IDs compare
+;; the sizes of the types, not the IR types. The summary stores the MD5 hash of
+;; each ID:
+;;   798791889831736034   = "amdgpu.v1:cc0:l"
+;;   3430040695729964317  = "amdgpu.v1:cc0:ii"
+;;   5485679292981660066  = "amdgpu.v1:cc0:v"
+;;   5970146889962867861  = "amdgpu.v1:cc0:vl"
+;;   11963068178503280946 = "amdgpu.v1:cc100:vl"
+
+; RUN: opt -mtriple=amdgcn-amd-amdhsa -module-summary %s -o %t.bc
+; RUN: llvm-dis %t.bc -o - | FileCheck %s
+; RUN: llvm-bcanalyzer -dump %t.bc | FileCheck %s --check-prefix=BCAN
+; RUN: llvm-lto2 run %t.bc -o %t.o -thinlto-distributed-indexes \
+; RUN:   -r=%t.bc,f_float,px -r=%t.bc,f_i32,px -r=%t.bc,device,px \
+; RUN:   -r=%t.bc,gfx,px -r=%t.bc,kernel,px -r=%t.bc,fp,px -r=%t.bc,ifn,px \
+; RUN:   -r=%t.bc,ifn_alias,px -r=%t.bc,resolver,px -r=%t.bc,ifunc_caller,px \
+; RUN:   -r=%t.bc,asm_goto,px -r=%t.bc,asm_invoke,px -r=%t.bc,personality, \
+; RUN:   -r=%t.bc,const_callers,px
+; RUN: llvm-dis %t.bc.thinlto.bc -o - | FileCheck %s --check-prefix=COMBINED
+
+;; A target that does not use call type IDs gets none.
+; RUN: opt -module-summary %s -o %t.none.bc
+; RUN: llvm-dis %t.none.bc -o - | FileCheck %s --check-prefix=NONE --implicit-check-not=callTypeIdInfo
+
+;; float(float) and i32(i32) have the same call type ID, and the calling
+;; convention is part of the ID.
+; CHECK-DAG: (name: "f_float", {{.*}}, callTypeIdInfo: (callTypeId: 3430040695729964317))))
+; CHECK-DAG: (name: "f_i32", {{.*}}, callTypeIdInfo: (callTypeId: 3430040695729964317))))
+; CHECK-DAG: (name: "device", {{.*}}, callTypeIdInfo: (callTypeId: 5970146889962867861))))
+; CHECK-DAG: (name: "gfx", {{.*}}, callTypeIdInfo: (callTypeId: 11963068178503280946))))
+; CHECK-DAG: (name: "resolver", {{.*}}, callTypeIdInfo: (callTypeId: 798791889831736034), refs: (^{{[0-9]+}}))))
+;; A local function has a call type ID only if its address is taken. Without
+;; one, the summary still has call type IDs, all empty.
+; CHECK-DAG: (name: "local_not_taken", {{.*}}, callTypeIdInfo: (callTypeId: 0))))
+; CHECK-DAG: (name: "local_taken", {{.*}}, callTypeIdInfo: (callTypeId: 5485679292981660066))))
+;; A kernel cannot be called, so it has no call type ID. Its indirect calls
+;; have distinct IDs, and its call to inline assembly has none.
+; CHECK-DAG: (name: "kernel", {{.*}}, calls: ((callee: ^{{[0-9]+}})), callTypeIdInfo: (callTypeId: 0, indirectCallTypeIds: (3430040695729964317, 11963068178503280946), hasCallWithoutTypeId: 1))))
+;; A call to an ifunc, also through an alias, has the call type ID of its type.
+; CHECK-DAG: (name: "ifunc_caller", {{.*}}, callTypeIdInfo: (callTypeId: 5485679292981660066, indirectCallTypeIds: (3430040695729964317)))))
+;; Inline assembly in callbr and invoke has no call type ID either.
+; CHECK-DAG: (name: "asm_goto", {{.*}}, callTypeIdInfo: (callTypeId: 5485679292981660066, hasCallWithoutTypeId: 1))))
+; CHECK-DAG: (name: "asm_invoke", {{.*}}, callTypeIdInfo: (callTypeId: 5485679292981660066, hasCallWithoutTypeId: 1), refs: (^{{[0-9]+}}))))
+;; A call to an absolute address has no call type ID. Calls through
+;; dso_local_equivalent and no_cfi values have one.
+; CHECK-DAG: (name: "const_callers", {{.*}}, callTypeIdInfo: (callTypeId: 5485679292981660066, indirectCallTypeIds: (5485679292981660066, 5970146889962867861), hasCallWithoutTypeId: 1), refs: (^{{[0-9]+}}, ^{{[0-9]+}}))))
+
+;; The bitcode analyzer prints the operands as signed values. The second operand
+;; holds the flags.
+; BCAN: <CALL_TYPE_IDS op0=0 op1=1 op2=3430040695729964317 op3=-6483675895206270670/>
+; BCAN: <CALL_TYPE_IDS op0=5485679292981660066 op1=1 op2=5485679292981660066 op3=5970146889962867861/>
+
+;; The combined index has no names. The GUIDs below are those of f_float,
+;; kernel, and const_callers. Only local_not_taken has empty call type IDs.
+; COMBINED-DAG: (guid: 6286836387820926777, {{.*}}, callTypeIdInfo: (callTypeId: 3430040695729964317))))
+; COMBINED-DAG: insts: 1, callTypeIdInfo: (callTypeId: 0))))
+; COMBINED-DAG: (guid: 17571550346185820240, {{.*}}, callTypeIdInfo: (callTypeId: 0, indirectCallTypeIds: (3430040695729964317, 11963068178503280946), hasCallWithoutTypeId: 1))))
+; COMBINED-DAG: (guid: 14653077686872413374, {{.*}}, callTypeIdInfo: (callTypeId: 5485679292981660066, indirectCallTypeIds: (5485679292981660066, 5970146889962867861), hasCallWithoutTypeId: 1), refs: (^{{[0-9]+}}, ^{{[0-9]+}}))))
+
+; NONE: (name: "kernel", {{.*}}, calls: ((callee: ^{{[0-9]+}})))))
+
+target datalayout = "e-p:64:64-p1:64:64-p2:32:32-p3:32:32-p4:64:64-p5:32:32-p6:32:32-p7:160:256:256:32-p8:128:128-p9:192:256:256:32-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024-v2048:2048-n32:64-S32-A5-G1-ni:7:8:9"
+
+ at fp = global ptr @local_taken
+
+ at ifn = ifunc i32 (i32), ptr @resolver
+ at ifn_alias = alias i32 (i32), ptr @ifn
+
+define float @f_float(float %x) {
+  ret float %x
+}
+
+define i32 @f_i32(i32 %x) {
+  ret i32 %x
+}
+
+define void @device(ptr %p) {
+  ret void
+}
+
+define amdgpu_gfx void @gfx(ptr %p) {
+  ret void
+}
+
+define internal void @local_not_taken() {
+  ret void
+}
+
+define internal void @local_taken() {
+  ret void
+}
+
+define amdgpu_kernel void @kernel(ptr %fp) {
+  call void @local_not_taken()
+  %a = call float %fp(float 1.0)
+  %b = call i32 %fp(i32 1)
+  call amdgpu_gfx void %fp(ptr null)
+  call void asm sideeffect "", ""()
+  ret void
+}
+
+define ptr @resolver() {
+  ret ptr @f_i32
+}
+
+define void @ifunc_caller() {
+  %a = call i32 @ifn(i32 1)
+  %b = call i32 @ifn_alias(i32 2)
+  ret void
+}
+
+define void @asm_goto() {
+  callbr void asm "", "!i"()
+          to label %normal [label %indirect]
+normal:
+  ret void
+indirect:
+  ret void
+}
+
+define void @asm_invoke() personality ptr @personality {
+  invoke void asm sideeffect unwind "", ""()
+          to label %cont unwind label %lpad
+cont:
+  ret void
+lpad:
+  %lp = landingpad { ptr, i32 } cleanup
+  ret void
+}
+
+declare i32 @personality(...)
+
+define void @const_callers() {
+  call void inttoptr (i64 4096 to ptr)()
+  call void dso_local_equivalent @local_taken()
+  call void no_cfi @device(ptr null)
+  ret void
+}

``````````

</details>


https://github.com/llvm/llvm-project/pull/245046


More information about the llvm-branch-commits mailing list