https://github.com/pvelesko updated 
https://github.com/llvm/llvm-project/pull/212452

>From e20b185a890f08dac29c271e4a4c25686edd01d3 Mon Sep 17 00:00:00 2001
From: Paulius Velesko <[email protected]>
Date: Tue, 28 Jul 2026 13:13:35 +0300
Subject: [PATCH] [clang][SPIR-V] Emit C++ vtable components in the generic
 address space

A vtable slot holds a function address. Functions live in the program
address space and the slots in the default globals address space, so on
spirv64 clang emits each slot as

  addrspacecast (ptr @fn to ptr addrspace(1))

SPIR-V has no cast between two non-generic storage classes:
OpPtrCastToGeneric converts a Workgroup, CrossWorkgroup or Function
pointer to Generic, and OpGenericCastToPtr converts back. The
SPIRV-LLVM-Translator rejects the module ("Casts from private/local/global
address space are allowed only to generic") and the SPIR-V backend emits a
vtable initializer that spirv-val rejects.

Emit the pointer components as generic pointers on non-logical SPIR and
SPIR-V targets whose program address space is not already the generic
one: spir, spir64, spirv32, spirv64 and spirv64-intel (functions in
CodeSectionINTEL, which SPV_INTEL_function_pointers makes part of
Generic). The RTTI component, a global, is cast to generic, and
getVirtualFunctionPointer loads the slot with the component type.

spirv64-amd-amdhsa is unchanged because its functions are already generic,
and so is logical SPIR-V, which has no generic pointers. Other targets are
unaffected because the component type stays GlobalsInt8PtrTy.

EmitTypeid loads the RTTI slot of the vtable, which is now a generic
pointer, so load it with the component type and cast it to the
std::type_info pointer type.
---
 clang/lib/CodeGen/CGVTables.cpp               | 17 ++++---
 clang/lib/CodeGen/CodeGenModule.cpp           |  7 +++
 clang/lib/CodeGen/CodeGenTypeCache.h          |  3 ++
 clang/lib/CodeGen/ItaniumCXXABI.cpp           | 15 ++++--
 clang/test/CIR/CodeGenCXX/device-rtti.cpp     |  3 +-
 .../CodeGenCXX/vtable-address-space-spirv.cpp | 49 +++++++++++++++++++
 6 files changed, 80 insertions(+), 14 deletions(-)
 create mode 100644 clang/test/CodeGenCXX/vtable-address-space-spirv.cpp

diff --git a/clang/lib/CodeGen/CGVTables.cpp b/clang/lib/CodeGen/CGVTables.cpp
index 14609eade00a28..e1178f534a8335 100644
--- a/clang/lib/CodeGen/CGVTables.cpp
+++ b/clang/lib/CodeGen/CGVTables.cpp
@@ -748,7 +748,7 @@ void 
CodeGenVTables::addRelativeComponent(ConstantArrayBuilder &builder,
 llvm::Type *CodeGenModule::getVTableComponentType() const {
   if (getLangOpts().RelativeCXXABIVTables)
     return Int32Ty;
-  return GlobalsInt8PtrTy;
+  return VTableComponentPtrTy;
 }
 
 llvm::Type *CodeGenVTables::getVTableComponentType() const {
@@ -760,7 +760,7 @@ static void AddPointerLayoutOffset(const CodeGenModule &CGM,
                                    CharUnits offset) {
   builder.add(llvm::ConstantExpr::getIntToPtr(
       llvm::ConstantInt::getSigned(CGM.PtrDiffTy, offset.getQuantity()),
-      CGM.GlobalsInt8PtrTy));
+      CGM.VTableComponentPtrTy));
 }
 
 static void AddRelativeLayoutOffset(const CodeGenModule &CGM,
@@ -798,7 +798,8 @@ void 
CodeGenVTables::addVTableComponent(ConstantArrayBuilder &builder,
                                   vtableHasLocalLinkage,
                                   /*isCompleteDtor=*/false);
     else
-      return builder.add(rtti);
+      return builder.add(llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast(
+          rtti, CGM.VTableComponentPtrTy));
 
   case VTableComponent::CK_FunctionPointer:
   case VTableComponent::CK_CompleteDtorPointer:
@@ -826,7 +827,7 @@ void 
CodeGenVTables::addVTableComponent(ConstantArrayBuilder &builder,
         if (IsThunk)
           nextVTableThunkIndex++;
         return builder.add(
-            llvm::ConstantExpr::getNullValue(CGM.GlobalsInt8PtrTy));
+            llvm::ConstantExpr::getNullValue(CGM.VTableComponentPtrTy));
       }
       // Method is acceptable, continue processing as usual.
     }
@@ -903,11 +904,11 @@ void 
CodeGenVTables::addVTableComponent(ConstantArrayBuilder &builder,
       //       globals;  fixing said issue might be intrusive, and will be done
       //       later.
       unsigned FnAS = fnPtr->getType()->getPointerAddressSpace();
-      unsigned GVAS = CGM.GlobalsInt8PtrTy->getPointerAddressSpace();
+      unsigned GVAS = CGM.VTableComponentPtrTy->getPointerAddressSpace();
 
       if (FnAS != GVAS)
-        fnPtr =
-            llvm::ConstantExpr::getAddrSpaceCast(fnPtr, CGM.GlobalsInt8PtrTy);
+        fnPtr = llvm::ConstantExpr::getAddrSpaceCast(fnPtr,
+                                                     CGM.VTableComponentPtrTy);
       if (const auto &Schema =
           CGM.getCodeGenOpts().PointerAuth.CXXVirtualFunctionPointers)
         return builder.addSignedPointer(fnPtr, Schema, GD, QualType());
@@ -919,7 +920,7 @@ void 
CodeGenVTables::addVTableComponent(ConstantArrayBuilder &builder,
     if (RelativeCXXABIVTables)
       return builder.add(llvm::ConstantExpr::getNullValue(CGM.Int32Ty));
     else
-      return builder.addNullPointer(CGM.GlobalsInt8PtrTy);
+      return builder.addNullPointer(CGM.VTableComponentPtrTy);
   }
 
   llvm_unreachable("Unexpected vtable component kind");
diff --git a/clang/lib/CodeGen/CodeGenModule.cpp 
b/clang/lib/CodeGen/CodeGenModule.cpp
index 94680cfee58202..0a78be0dd25c2a 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -609,11 +609,18 @@ CodeGenModule::CodeGenModule(ASTContext &C,
       llvm::PointerType::get(LLVMContext, DL.getAllocaAddrSpace());
   GlobalsInt8PtrTy =
       llvm::PointerType::get(LLVMContext, DL.getDefaultGlobalsAddressSpace());
+  VTableComponentPtrTy = GlobalsInt8PtrTy;
   ProgramPtrTy =
       llvm::PointerType::get(LLVMContext, DL.getProgramAddressSpace());
   ConstGlobalsPtrTy = llvm::PointerType::get(
       LLVMContext, C.getTargetAddressSpace(GetGlobalConstantAddressSpace()));
 
+  // SPIR-V can only cast a function pointer to the generic address space.
+  unsigned GenericAS = C.getTargetAddressSpace(LangAS::opencl_generic);
+  if (getTriple().isSPIROrSPIRV() && !getTriple().isSPIRVLogical() &&
+      DL.getProgramAddressSpace() != GenericAS)
+    VTableComponentPtrTy = llvm::PointerType::get(LLVMContext, GenericAS);
+
   // Build C++20 Module initializers.
   // TODO: Add Microsoft here once we know the mangling required for the
   // initializers.
diff --git a/clang/lib/CodeGen/CodeGenTypeCache.h 
b/clang/lib/CodeGen/CodeGenTypeCache.h
index 17eca207d7c807..b15ade84c5b928 100644
--- a/clang/lib/CodeGen/CodeGenTypeCache.h
+++ b/clang/lib/CodeGen/CodeGenTypeCache.h
@@ -72,6 +72,9 @@ struct CodeGenTypeCache {
     llvm::PointerType *GlobalsInt8PtrTy;
   };
 
+  /// Type of a pointer component of a C++ vtable.
+  llvm::PointerType *VTableComponentPtrTy;
+
   /// Pointer in program address space
   llvm::PointerType *ProgramPtrTy;
 
diff --git a/clang/lib/CodeGen/ItaniumCXXABI.cpp 
b/clang/lib/CodeGen/ItaniumCXXABI.cpp
index 3fbee4dc6bb031..de4aaae96cfce8 100644
--- a/clang/lib/CodeGen/ItaniumCXXABI.cpp
+++ b/clang/lib/CodeGen/ItaniumCXXABI.cpp
@@ -1587,9 +1587,14 @@ llvm::Value *ItaniumCXXABI::EmitTypeid(CodeGenFunction 
&CGF,
         CGM.getIntrinsic(llvm::Intrinsic::load_relative, {CGM.Int32Ty}),
         {Value, llvm::ConstantInt::getSigned(CGM.Int32Ty, -4)});
   } else {
-    // Load the type info.
-    Value =
-        CGF.Builder.CreateConstInBoundsGEP1_64(StdTypeInfoPtrTy, Value, -1ULL);
+    // Load the type info, using the vtable component type where it differs.
+    llvm::Type *ComponentTy = CGM.VTableComponentPtrTy == CGM.GlobalsInt8PtrTy
+                                  ? StdTypeInfoPtrTy
+                                  : CGM.VTableComponentPtrTy;
+    Value = CGF.Builder.CreateConstInBoundsGEP1_64(ComponentTy, Value, -1ULL);
+    Value = CGF.Builder.CreateAlignedLoad(ComponentTy, Value,
+                                          CGF.getPointerAlign());
+    return CGF.Builder.CreateAddrSpaceCast(Value, StdTypeInfoPtrTy);
   }
   return CGF.Builder.CreateAlignedLoad(StdTypeInfoPtrTy, Value,
                                        CGF.getPointerAlign());
@@ -2275,8 +2280,8 @@ CGCallee 
ItaniumCXXABI::getVirtualFunctionPointer(CodeGenFunction &CGF,
           {VTable, llvm::ConstantInt::get(CGM.Int32Ty, ByteOffset)});
     } else {
       VTableSlotPtr = CGF.Builder.CreateConstInBoundsGEP1_64(
-          PtrTy, VTable, VTableIndex, "vfn");
-      VFuncLoad = CGF.Builder.CreateAlignedLoad(PtrTy, VTableSlotPtr,
+          ComponentTy, VTable, VTableIndex, "vfn");
+      VFuncLoad = CGF.Builder.CreateAlignedLoad(ComponentTy, VTableSlotPtr,
                                                 CGF.getPointerAlign());
     }
 
diff --git a/clang/test/CIR/CodeGenCXX/device-rtti.cpp 
b/clang/test/CIR/CodeGenCXX/device-rtti.cpp
index deac890e1e2113..ed4fe5be9e17d5 100644
--- a/clang/test/CIR/CodeGenCXX/device-rtti.cpp
+++ b/clang/test/CIR/CodeGenCXX/device-rtti.cpp
@@ -8,7 +8,7 @@
 // RUN: %clang_cc1 -fsycl-is-device -triple spir64-unknown-unknown -aux-triple 
x86_64-unknown-linux-gnu -fclangir -emit-llvm %s -o %t-sycl-cir.ll
 // RUN: FileCheck --check-prefix=LLVM-DEV --implicit-check-not=_ZTI 
--input-file=%t-sycl-cir.ll %s
 // RUN: %clang_cc1 -fsycl-is-device -triple spir64-unknown-unknown -aux-triple 
x86_64-unknown-linux-gnu -emit-llvm %s -o %t-sycl.ll
-// RUN: FileCheck --check-prefix=LLVM-DEV --implicit-check-not=_ZTI 
--input-file=%t-sycl.ll %s
+// RUN: FileCheck --check-prefix=OGCG-DEV --implicit-check-not=_ZTI 
--input-file=%t-sycl.ll %s
 
 // OpenMP device on AMDGPU.
 // RUN: %clang_cc1 -fopenmp -fopenmp-is-target-device -triple 
amdgcn-amd-amdhsa -aux-triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s 
-o %t-omp.cir
@@ -57,6 +57,7 @@ DEVICE void Key::f() {}
 
 // CIR-DEV: cir.global {{.*}} @_ZTV3Key = 
#cir.vtable<{#cir.const_array<[#cir.ptr<null> : !cir.ptr<!u8i>, #cir.ptr<null> 
: !cir.ptr<!u8i>, #cir.global_view<@_ZN3Key1fEv> : !cir.ptr<!u8i>]>
 // LLVM-DEV: @_ZTV3Key = {{.*}}null, ptr{{.*}} null, ptr{{.*}} @_ZN3Key1fEv
+// OGCG-DEV: @_ZTV3Key = {{.*}}constant { [3 x ptr addrspace(4)] } {{.*}}ptr 
addrspace(4) null, ptr addrspace(4) addrspacecast (ptr addrspace(1) null to ptr 
addrspace(4)), ptr addrspace(4) addrspacecast (ptr @_ZN3Key1fEv to ptr 
addrspace(4))
 
 // CIR-HOST-DAG: cir.global {{.*}} @_ZTV3Key = 
#cir.vtable<{#cir.const_array<[#cir.ptr<null> : !cir.ptr<!u8i>, 
#cir.global_view<@_ZTI3Key> : !cir.ptr<!u8i>, #cir.global_view<@_ZN3Key1fEv> : 
!cir.ptr<!u8i>]>
 // CIR-HOST-DAG: cir.global constant external @_ZTI3Key = #cir.typeinfo<
diff --git a/clang/test/CodeGenCXX/vtable-address-space-spirv.cpp 
b/clang/test/CodeGenCXX/vtable-address-space-spirv.cpp
new file mode 100644
index 00000000000000..5ffd8fb7668c46
--- /dev/null
+++ b/clang/test/CodeGenCXX/vtable-address-space-spirv.cpp
@@ -0,0 +1,49 @@
+// RUN: %clang_cc1 %s -triple=spirv64 -std=c++11 -emit-llvm -o - | FileCheck %s
+// RUN: %clang_cc1 %s -triple=spirv32 -std=c++11 -emit-llvm -o - | FileCheck %s
+// RUN: %clang_cc1 %s -triple=spir64-unknown-unknown -std=c++11 -emit-llvm -o 
- | FileCheck %s
+// RUN: %clang_cc1 %s -triple=spir-unknown-unknown -std=c++11 -emit-llvm -o - 
| FileCheck %s
+
+// Functions are already generic here, so the components stay global.
+// RUN: %clang_cc1 %s -triple=spirv64-amd-amdhsa -std=c++11 -emit-llvm -o - | 
FileCheck %s --check-prefix=AMDGCNSPIRV
+
+namespace std {
+struct type_info;
+} // namespace std
+
+struct A {
+  virtual void f();
+  virtual void g();
+};
+
+void A::f() {}
+
+// CHECK: @_ZTV1A ={{.*}}addrspace(1) constant { [4 x ptr addrspace(4)] } { [4 
x ptr addrspace(4)] [ptr addrspace(4) null, ptr addrspace(4) addrspacecast (ptr 
addrspace(1) @_ZTI1A to ptr addrspace(4)), ptr addrspace(4) addrspacecast (ptr 
@_ZN1A1fEv to ptr addrspace(4)), ptr addrspace(4) addrspacecast (ptr @_ZN1A1gEv 
to ptr addrspace(4))]
+// CHECK: @_ZTI1A ={{.*}}addrspace(1) constant { ptr addrspace(1), ptr 
addrspace(1) }
+
+// AMDGCNSPIRV: @_ZTV1A ={{.*}}addrspace(1) constant { [4 x ptr addrspace(1)] 
} { [4 x ptr addrspace(1)] [ptr addrspace(1) null, ptr addrspace(1) @_ZTI1A, 
ptr addrspace(1) addrspacecast (ptr addrspace(4) @_ZN1A1fEv to ptr 
addrspace(1)), ptr addrspace(1) addrspacecast (ptr addrspace(4) @_ZN1A1gEv to 
ptr addrspace(1))]
+
+void call(A *a) { a->g(); }
+
+// CHECK-LABEL: define {{.*}}@_Z4callP1A
+// CHECK: %[[VT:.*]] = load ptr addrspace(1),
+// CHECK: %[[SLOT:.*]] = getelementptr inbounds ptr addrspace(4), ptr 
addrspace(1) %[[VT]], i64 1
+// CHECK: %[[FN:.*]] = load ptr addrspace(4), ptr addrspace(1) %[[SLOT]]
+// CHECK: call {{.*}}addrspace(4) void %[[FN]]
+
+// AMDGCNSPIRV-LABEL: define {{.*}}@_Z4callP1A
+// AMDGCNSPIRV: %[[VT:.*]] = load ptr addrspace(1),
+// AMDGCNSPIRV: %[[SLOT:.*]] = getelementptr inbounds ptr addrspace(1), ptr 
addrspace(1) %[[VT]], i64 1
+// AMDGCNSPIRV: %[[FN:.*]] = load ptr addrspace(1), ptr addrspace(1) %[[SLOT]]
+
+const std::type_info &ti(A *a) { return typeid(*a); }
+
+// CHECK-LABEL: define {{.*}}@_Z2tiP1A
+// CHECK: %[[TVT:.*]] = load ptr addrspace(1),
+// CHECK: %[[TSLOT:.*]] = getelementptr inbounds ptr addrspace(4), ptr 
addrspace(1) %[[TVT]], i64 -1
+// CHECK: %[[TI:.*]] = load ptr addrspace(4), ptr addrspace(1) %[[TSLOT]]
+// CHECK: addrspacecast ptr addrspace(4) %[[TI]] to ptr
+
+// AMDGCNSPIRV-LABEL: define {{.*}}@_Z2tiP1A
+// AMDGCNSPIRV: %[[TVT:.*]] = load ptr addrspace(1),
+// AMDGCNSPIRV: %[[TSLOT:.*]] = getelementptr inbounds ptr addrspace(4), ptr 
addrspace(1) %[[TVT]], i64 -1
+// AMDGCNSPIRV: %[[TI:.*]] = load ptr addrspace(4), ptr addrspace(1) %[[TSLOT]]

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to