Skip to content

Commit ca10db9

Browse files
authored
[Clang] Improve generation of GV for virtual funcs (#7449)
Reduce the code intended to generate valid device code global pointers with global address space in SYCL by introducing a new `DefaultInt8PtrTy` type.
1 parent 9ff6045 commit ca10db9

File tree

6 files changed

+23
-41
lines changed

6 files changed

+23
-41
lines changed

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ CodeGenModule::CodeGenModule(ASTContext &C,
141141
const llvm::DataLayout &DL = M.getDataLayout();
142142
AllocaInt8PtrTy = Int8Ty->getPointerTo(DL.getAllocaAddrSpace());
143143
GlobalsInt8PtrTy = Int8Ty->getPointerTo(DL.getDefaultGlobalsAddressSpace());
144+
DefaultInt8PtrTy =
145+
Int8Ty->getPointerTo(getContext().getTargetAddressSpace(LangAS::Default));
144146
ASTAllocaAddressSpace = getTargetCodeGenInfo().getASTAllocaAddressSpace();
145147

146148
// Build C++20 Module initializers.

clang/lib/CodeGen/CodeGenTypeCache.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ struct CodeGenTypeCache {
6969
llvm::PointerType *AllocaInt8PtrTy;
7070
};
7171

72+
/// void* in target address space
73+
llvm::PointerType *DefaultInt8PtrTy;
74+
7275
/// void* in default globals address space
7376
union {
7477
llvm::PointerType *GlobalsVoidPtrTy;

clang/lib/CodeGen/ItaniumCXXABI.cpp

Lines changed: 7 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "CGVTables.h"
2424
#include "CodeGenFunction.h"
2525
#include "CodeGenModule.h"
26+
#include "CodeGenTypeCache.h"
2627
#include "TargetInfo.h"
2728
#include "clang/AST/Attr.h"
2829
#include "clang/AST/Mangle.h"
@@ -3596,32 +3597,8 @@ void ItaniumRTTIBuilder::BuildVTablePointer(const Type *Ty) {
35963597
if (CGM.getItaniumVTableContext().isRelativeLayout())
35973598
VTable = CGM.getModule().getNamedAlias(VTableName);
35983599

3599-
// To generate valid device code global pointers should have global address
3600-
// space in SYCL.
3601-
bool GenTyInfoGVWithGlobalAS =
3602-
CGM.getLangOpts().SYCLIsDevice &&
3603-
CGM.getLangOpts().SYCLAllowVirtualFunctions &&
3604-
(VTableName == ClassTypeInfo || VTableName == SIClassTypeInfo);
3605-
auto VTableTy =
3606-
GenTyInfoGVWithGlobalAS
3607-
? CGM.Int8Ty->getPointerTo(
3608-
CGM.getContext().getTargetAddressSpace(LangAS::sycl_global))
3609-
: CGM.Int8PtrTy;
3610-
if (!VTable) {
3611-
if (GenTyInfoGVWithGlobalAS) {
3612-
VTable = CGM.getModule().getOrInsertGlobal(VTableName, VTableTy, [&] {
3613-
return new llvm::GlobalVariable(
3614-
CGM.getModule(), VTableTy, /*isConstant=*/false,
3615-
llvm::GlobalVariable::ExternalLinkage, /*Initializer=*/nullptr,
3616-
VTableName, /*InsertBefore=*/nullptr,
3617-
llvm::GlobalValue::ThreadLocalMode::NotThreadLocal,
3618-
llvm::Optional<unsigned>(
3619-
CGM.getContext().getTargetAddressSpace(LangAS::sycl_global)));
3620-
});
3621-
} else {
3622-
VTable = CGM.getModule().getOrInsertGlobal(VTableName, VTableTy);
3623-
}
3624-
}
3600+
if (!VTable)
3601+
VTable = CGM.CreateRuntimeVariable(CGM.DefaultInt8PtrTy, VTableName);
36253602

36263603
CGM.setDSOLocal(cast<llvm::GlobalValue>(VTable->stripPointerCasts()));
36273604

@@ -3633,15 +3610,15 @@ void ItaniumRTTIBuilder::BuildVTablePointer(const Type *Ty) {
36333610
// The vtable address point is 8 bytes after its start:
36343611
// 4 for the offset to top + 4 for the relative offset to rtti.
36353612
llvm::Constant *Eight = llvm::ConstantInt::get(CGM.Int32Ty, 8);
3636-
VTable = llvm::ConstantExpr::getBitCast(VTable, VTableTy);
3613+
VTable = llvm::ConstantExpr::getBitCast(VTable, CGM.DefaultInt8PtrTy);
36373614
VTable =
36383615
llvm::ConstantExpr::getInBoundsGetElementPtr(CGM.Int8Ty, VTable, Eight);
36393616
} else {
36403617
llvm::Constant *Two = llvm::ConstantInt::get(PtrDiffTy, 2);
3641-
VTable =
3642-
llvm::ConstantExpr::getInBoundsGetElementPtr(VTableTy, VTable, Two);
3618+
VTable = llvm::ConstantExpr::getInBoundsGetElementPtr(CGM.DefaultInt8PtrTy,
3619+
VTable, Two);
36433620
}
3644-
VTable = llvm::ConstantExpr::getBitCast(VTable, VTableTy);
3621+
VTable = llvm::ConstantExpr::getBitCast(VTable, CGM.DefaultInt8PtrTy);
36453622

36463623
Fields.push_back(VTable);
36473624
}

clang/test/CodeGenSYCL/no_opaque_virtual-types.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ int main() {
2020
// Struct layout big enough for vtable.
2121
// CHECK: %struct.Struct = type { i32 (...)** }
2222
// VTable:
23-
// CHECK: @_ZTV6Struct = linkonce_odr unnamed_addr constant { [3 x i8*] } { [3 x i8*] [i8* null, i8* bitcast ({ i8*, i8* }* @_ZTI6Struct to i8*), i8* bitcast (void (%struct.Struct addrspace(4)*)* @_ZN6Struct3fooEv to i8*)] }, comdat, align 8
24-
// CHECK: @[[TYPEINFO:.+]] = external global i8*
23+
// CHECK: @_ZTV6Struct = linkonce_odr unnamed_addr constant { [3 x i8*] } { [3 x i8*] [i8* null, i8* bitcast ({ i8 addrspace(4)*, i8* }* @_ZTI6Struct to i8*), i8* bitcast (void (%struct.Struct addrspace(4)*)* @_ZN6Struct3fooEv to i8*)] }, comdat, align 8
24+
// CHECK: @[[TYPEINFO:.+]] = external addrspace(1) global i8 addrspace(4)*
2525
// TypeInfo Name:
2626
// CHECK: @_ZTS6Struct = linkonce_odr constant [8 x i8] c"6Struct\00", comdat, align 1
2727
// TypeInfo:
28-
// CHECK: @_ZTI6Struct = linkonce_odr constant { i8*, i8* } { i8* bitcast (i8** getelementptr inbounds (i8*, i8** @[[TYPEINFO]], i64 2) to i8*), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @_ZTS6Struct, i32 0, i32 0) }, comdat, align 8
28+
// CHECK: @_ZTI6Struct = linkonce_odr constant { i8 addrspace(4)*, i8* } { i8 addrspace(4)* bitcast (i8 addrspace(4)* addrspace(4)* getelementptr inbounds (i8 addrspace(4)*, i8 addrspace(4)* addrspace(4)* addrspacecast (i8 addrspace(4)* addrspace(1)* @[[TYPEINFO]] to i8 addrspace(4)* addrspace(4)*), i64 2) to i8 addrspace(4)*), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @_ZTS6Struct, i32 0, i32 0) }, comdat, align 8

clang/test/CodeGenSYCL/simple-sycl-virtual-function.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
// RUN: %clang_cc1 -triple spir64 -fsycl-allow-virtual-functions -fsycl-is-device -emit-llvm %s -o - | FileCheck %s --check-prefixes CHECK,CHECK-PTR
66
// RUN: %clang_cc1 -triple spir64 -fsycl-allow-virtual-functions -fsycl-is-device -fexperimental-relative-c++-abi-vtables -emit-llvm %s -o - | FileCheck %s --check-prefixes CHECK,CHECK-REL
77

8-
// CHECK: @_ZTVN10__cxxabiv120__si_class_type_infoE = external addrspace(1) global ptr addrspace(1)
9-
// CHECK: @_ZTVN10__cxxabiv117__class_type_infoE = external addrspace(1) global ptr addrspace(1)
10-
// CHECK-PTR: @_ZTI4Base = linkonce_odr constant { ptr addrspace(1), ptr } { ptr addrspace(1) getelementptr inbounds (ptr addrspace(1), ptr addrspace(1) @_ZTVN10__cxxabiv117__class_type_infoE, i64 2)
11-
// CHECK-PTR: @_ZTI8Derived1 = linkonce_odr constant { ptr addrspace(1), ptr, ptr } { ptr addrspace(1) getelementptr inbounds (ptr addrspace(1), ptr addrspace(1) @_ZTVN10__cxxabiv120__si_class_type_infoE, i64 2)
12-
// CHECK-REL: @_ZTI4Base = linkonce_odr constant { ptr addrspace(1), ptr } { ptr addrspace(1) getelementptr inbounds (i8, ptr addrspace(1) @_ZTVN10__cxxabiv117__class_type_infoE, i32 8)
13-
// CHECK-REL: @_ZTI8Derived1 = linkonce_odr constant { ptr addrspace(1), ptr, ptr } { ptr addrspace(1) getelementptr inbounds (i8, ptr addrspace(1) @_ZTVN10__cxxabiv120__si_class_type_infoE, i32 8)
8+
// CHECK: @_ZTVN10__cxxabiv120__si_class_type_infoE = external addrspace(1) global ptr addrspace(4)
9+
// CHECK: @_ZTVN10__cxxabiv117__class_type_infoE = external addrspace(1) global ptr addrspace(4)
10+
// CHECK-PTR: @_ZTI4Base = linkonce_odr constant { ptr addrspace(4), ptr } { ptr addrspace(4) getelementptr inbounds (ptr addrspace(4), ptr addrspace(4) addrspacecast (ptr addrspace(1) @_ZTVN10__cxxabiv117__class_type_infoE to ptr addrspace(4)), i64 2)
11+
// CHECK-PTR: @_ZTI8Derived1 = linkonce_odr constant { ptr addrspace(4), ptr, ptr } { ptr addrspace(4) getelementptr inbounds (ptr addrspace(4), ptr addrspace(4) addrspacecast (ptr addrspace(1) @_ZTVN10__cxxabiv120__si_class_type_infoE to ptr addrspace(4)), i64 2)
12+
// CHECK-REL: @_ZTI4Base = linkonce_odr constant { ptr addrspace(4), ptr } { ptr addrspace(4) getelementptr inbounds (i8, ptr addrspace(4) addrspacecast (ptr addrspace(1) @_ZTVN10__cxxabiv117__class_type_infoE to ptr addrspace(4)), i32 8)
13+
// CHECK-REL: @_ZTI8Derived1 = linkonce_odr constant { ptr addrspace(4), ptr, ptr } { ptr addrspace(4) getelementptr inbounds (i8, ptr addrspace(4) addrspacecast (ptr addrspace(1) @_ZTVN10__cxxabiv120__si_class_type_infoE to ptr addrspace(4)), i32 8)
1414

1515
SYCL_EXTERNAL bool rand();
1616

clang/test/CodeGenSYCL/virtual-types.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ int main() {
2121
// CHECK: %struct.Struct = type { ptr }
2222
// VTable:
2323
// CHECK: @_ZTV6Struct = linkonce_odr unnamed_addr constant { [3 x ptr] } { [3 x ptr] [ptr null, ptr @_ZTI6Struct, ptr @_ZN6Struct3fooEv] }, comdat, align 8
24-
// CHECK: @[[TYPEINFO:.+]] = external global ptr
24+
// CHECK: @[[TYPEINFO:.+]] = external addrspace(1) global ptr addrspace(4)
2525
// TypeInfo Name:
2626
// CHECK: @_ZTS6Struct = linkonce_odr constant [8 x i8] c"6Struct\00", comdat, align 1
2727
// TypeInfo:
28-
// CHECK: @_ZTI6Struct = linkonce_odr constant { ptr, ptr } { ptr getelementptr inbounds (ptr, ptr @[[TYPEINFO]], i64 2), ptr @_ZTS6Struct }, comdat, align 8
28+
// CHECK: @_ZTI6Struct = linkonce_odr constant { ptr addrspace(4), ptr } { ptr addrspace(4) getelementptr inbounds (ptr addrspace(4), ptr addrspace(4) addrspacecast (ptr addrspace(1) @[[TYPEINFO]] to ptr addrspace(4)), i64 2), ptr @_ZTS6Struct }, comdat, align 8

0 commit comments

Comments
 (0)