Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Do not merge] Cherry-pick of https://github.com/intel/llvm/pull/12776 with customizations #12789

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions clang/lib/CodeGen/CGVTT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ CodeGenVTables::EmitVTTDefinition(llvm::GlobalVariable *VTT,
const CXXRecordDecl *RD) {
VTTBuilder Builder(CGM.getContext(), RD, /*GenerateDefinition=*/true);
llvm::ArrayType *ArrayType =
llvm::ArrayType::get(CGM.DefaultInt8PtrTy, Builder.getVTTComponents().size());
llvm::ArrayType::get(CGM.getTriple().isSPIR() ? CGM.DefaultInt8PtrTy : CGM.GlobalsInt8PtrTy, Builder.getVTTComponents().size());

SmallVector<llvm::GlobalVariable *, 8> VTables;
SmallVector<VTableAddressPointsMapTy, 8> VTableAddressPoints;
Expand Down Expand Up @@ -81,8 +81,9 @@ CodeGenVTables::EmitVTTDefinition(llvm::GlobalVariable *VTT,
VTable->getValueType(), VTable, Idxs, /*InBounds=*/true,
/*InRangeIndex=*/1);

Init = llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast(
Init, CGM.Int8PtrTy);
if (CGM.getTriple().isSPIR())
Init = llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast(
Init, CGM.DefaultInt8PtrTy);

VTTComponents.push_back(Init);
}
Expand Down Expand Up @@ -117,9 +118,9 @@ llvm::GlobalVariable *CodeGenVTables::GetAddrOfVTT(const CXXRecordDecl *RD) {

VTTBuilder Builder(CGM.getContext(), RD, /*GenerateDefinition=*/false);

llvm::ArrayType *ArrayType =
llvm::ArrayType::get(CGM.Int8PtrTy, Builder.getVTTComponents().size());
llvm::Align Align = CGM.getDataLayout().getABITypeAlign(CGM.Int8PtrTy);
llvm::ArrayType *ArrayType = llvm::ArrayType::get(
CGM.getTriple().isSPIR() ? CGM.DefaultInt8PtrTy : CGM.GlobalsInt8PtrTy, Builder.getVTTComponents().size());
llvm::Align Align = CGM.getDataLayout().getABITypeAlign(CGM.getTriple().isSPIR() ? CGM.DefaultInt8PtrTy : CGM.GlobalsInt8PtrTy);

llvm::GlobalVariable *GV = CGM.CreateOrReplaceCXXRuntimeVariable(
Name, ArrayType, llvm::GlobalValue::ExternalLinkage, Align);
Expand Down
39 changes: 29 additions & 10 deletions clang/lib/CodeGen/CGVTables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,11 @@ bool CodeGenVTables::useRelativeLayout() const {
llvm::Type *CodeGenModule::getVTableComponentType() const {
if (UseRelativeLayout(*this))
return Int32Ty;
return Int8PtrTy;

if (getTriple().isSPIR())
return DefaultInt8PtrTy;

return GlobalsInt8PtrTy;
}

llvm::Type *CodeGenVTables::getVTableComponentType() const {
Expand All @@ -704,7 +708,7 @@ static void AddPointerLayoutOffset(const CodeGenModule &CGM,
CharUnits offset) {
builder.add(llvm::ConstantExpr::getIntToPtr(
llvm::ConstantInt::get(CGM.PtrDiffTy, offset.getQuantity()),
CGM.Int8PtrTy));
CGM.getTriple().isSPIR() ? CGM.DefaultInt8PtrTy : CGM.GlobalsInt8PtrTy));
}

static void AddRelativeLayoutOffset(const CodeGenModule &CGM,
Expand Down Expand Up @@ -741,7 +745,7 @@ void CodeGenVTables::addVTableComponent(ConstantArrayBuilder &builder,
vtableHasLocalLinkage,
/*isCompleteDtor=*/false);
else
return builder.add(llvm::ConstantExpr::getBitCast(rtti, CGM.Int8PtrTy));
return builder.add(rtti);

case VTableComponent::CK_FunctionPointer:
case VTableComponent::CK_CompleteDtorPointer:
Expand All @@ -760,7 +764,8 @@ void CodeGenVTables::addVTableComponent(ConstantArrayBuilder &builder,
? MD->hasAttr<CUDADeviceAttr>()
: (MD->hasAttr<CUDAHostAttr>() || !MD->hasAttr<CUDADeviceAttr>());
if (!CanEmitMethod)
return builder.add(llvm::ConstantExpr::getNullValue(CGM.Int8PtrTy));
return builder.add(
llvm::ConstantExpr::getNullValue(CGM.getTriple().isSPIR() ? CGM.DefaultInt8PtrTy : CGM.GlobalsInt8PtrTy));
// Method is acceptable, continue processing as usual.
}

Expand All @@ -773,20 +778,20 @@ void CodeGenVTables::addVTableComponent(ConstantArrayBuilder &builder,
// with the local symbol. As a temporary solution, fill these components
// with zero. We shouldn't be calling these in the first place anyway.
if (useRelativeLayout())
return llvm::ConstantPointerNull::get(CGM.Int8PtrTy);
return llvm::ConstantPointerNull::get(CGM.GlobalsInt8PtrTy);

// For NVPTX devices in OpenMP emit special functon as null pointers,
// otherwise linking ends up with unresolved references.
if (CGM.getLangOpts().OpenMP && CGM.getLangOpts().OpenMPIsTargetDevice &&
CGM.getTriple().isNVPTX())
return llvm::ConstantPointerNull::get(CGM.Int8PtrTy);
return llvm::ConstantPointerNull::get(CGM.GlobalsInt8PtrTy);
llvm::FunctionType *fnTy =
llvm::FunctionType::get(CGM.VoidTy, /*isVarArg=*/false);
llvm::Constant *fn = cast<llvm::Constant>(
CGM.CreateRuntimeFunction(fnTy, name).getCallee());
if (auto f = dyn_cast<llvm::Function>(fn))
f->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
return llvm::ConstantExpr::getBitCast(fn, CGM.Int8PtrTy);
return fn;
};

llvm::Constant *fnPtr;
Expand Down Expand Up @@ -824,15 +829,29 @@ void CodeGenVTables::addVTableComponent(ConstantArrayBuilder &builder,
return addRelativeComponent(
builder, fnPtr, vtableAddressPoint, vtableHasLocalLinkage,
component.getKind() == VTableComponent::CK_CompleteDtorPointer);
} else
return builder.add(llvm::ConstantExpr::getBitCast(fnPtr, CGM.Int8PtrTy));
} else {
// TODO: this icky and only exists due to functions being in the generic
// address space, rather than the global one, even though they are
// globals; fixing said issue might be intrusive, and will be done
// later.
unsigned FnAS = fnPtr->getType()->getPointerAddressSpace();
unsigned GVAS = CGM.GlobalsInt8PtrTy->getPointerAddressSpace();

if (CGM.getTriple().isSPIR())
GVAS = CGM.DefaultInt8PtrTy->getPointerAddressSpace();

if (FnAS != GVAS)
fnPtr =
llvm::ConstantExpr::getAddrSpaceCast(fnPtr, CGM.getTriple().isSPIR() ? CGM.DefaultInt8PtrTy : CGM.GlobalsInt8PtrTy);
return builder.add(fnPtr);
}
}

case VTableComponent::CK_UnusedFunctionPointer:
if (useRelativeLayout())
return builder.add(llvm::ConstantExpr::getNullValue(CGM.Int32Ty));
else
return builder.addNullPointer(CGM.Int8PtrTy);
return builder.addNullPointer(CGM.getTriple().isSPIR() ? CGM.DefaultInt8PtrTy : CGM.GlobalsInt8PtrTy);
}

llvm_unreachable("Unexpected vtable component kind");
Expand Down
7 changes: 5 additions & 2 deletions clang/lib/CodeGen/CodeGenModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7955,8 +7955,11 @@ llvm::Constant *CodeGenModule::GetAddrOfRTTIDescriptor(QualType Ty,
// Return a bogus pointer if RTTI is disabled, unless it's for EH.
// FIXME: should we even be calling this method if RTTI is disabled
// and it's not for EH?
if (!shouldEmitRTTI(ForEH))
return llvm::Constant::getNullValue(Int8PtrTy);
if (!shouldEmitRTTI(ForEH)) {
if (getTriple().isSPIR())
return llvm::Constant::getNullValue(DefaultInt8PtrTy);
return llvm::Constant::getNullValue(GlobalsInt8PtrTy);
}

if (ForEH && Ty->isObjCObjectPointerType() &&
LangOpts.ObjCRuntime.isGNUFamily())
Expand Down
52 changes: 34 additions & 18 deletions clang/lib/CodeGen/ItaniumCXXABI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,7 @@ CGCallee ItaniumCXXABI::EmitLoadOfMemberFunctionPointer(
CGF.EmitBlock(FnVirtual);

// Cast the adjusted this to a pointer to vtable pointer and load.
llvm::Type *VTableTy = Builder.getInt8PtrTy();
llvm::Type *VTableTy = CGF.CGM.GlobalsInt8PtrTy;
CharUnits VTablePtrAlign =
CGF.CGM.getDynamicOffsetAlignment(ThisAddr.getAlignment(), RD,
CGF.getPointerAlign());
Expand Down Expand Up @@ -1943,10 +1943,10 @@ llvm::Value *ItaniumCXXABI::getVTableAddressPointInStructorWithVTT(
llvm::Value *VTT = CGF.LoadCXXVTT();
if (VirtualPointerIndex)
VTT = CGF.Builder.CreateConstInBoundsGEP1_64(
CGF.VoidPtrTy, VTT, VirtualPointerIndex);
CGF.CGM.getTriple().isSPIR() ? CGF.DefaultInt8PtrTy : CGF.GlobalsVoidPtrTy, VTT, VirtualPointerIndex);

// And load the address point from the VTT.
return CGF.Builder.CreateAlignedLoad(CGF.VoidPtrTy, VTT,
return CGF.Builder.CreateAlignedLoad(CGF.CGM.getTriple().isSPIR() ? CGF.DefaultInt8PtrTy : CGF.GlobalsVoidPtrTy, VTT,
CGF.getPointerAlign());
}

Expand Down Expand Up @@ -1974,12 +1974,13 @@ llvm::GlobalVariable *ItaniumCXXABI::getAddrOfVTable(const CXXRecordDecl *RD,
CGM.getItaniumVTableContext().getVTableLayout(RD);
llvm::Type *VTableType = CGM.getVTables().getVTableType(VTLayout);

// Use pointer alignment for the vtable. Otherwise we would align them based
// on the size of the initializer which doesn't make sense as only single
// values are read.
// Use pointer to global alignment for the vtable. Otherwise we would align
// them based on the size of the initializer which doesn't make sense as only
// single values are read.
LangAS AS = CGM.GetGlobalVarAddressSpace(nullptr);
unsigned PAlign = CGM.getItaniumVTableContext().isRelativeLayout()
? 32
: CGM.getTarget().getPointerAlign(LangAS::Default);
: CGM.getTarget().getPointerAlign(AS);

VTable = CGM.CreateOrReplaceCXXRuntimeVariable(
Name, VTableType, llvm::GlobalValue::ExternalLinkage,
Expand Down Expand Up @@ -3281,10 +3282,9 @@ ItaniumRTTIBuilder::GetAddrOfExternalRTTIDescriptor(QualType Ty) {
// Note for the future: If we would ever like to do deferred emission of
// RTTI, check if emitting vtables opportunistically need any adjustment.

GV = new llvm::GlobalVariable(CGM.getModule(), CGM.Int8PtrTy,
/*isConstant=*/true,
llvm::GlobalValue::ExternalLinkage, nullptr,
Name);
GV = new llvm::GlobalVariable(
CGM.getModule(), CGM.getTriple().isSPIR() ? CGM.DefaultInt8PtrTy : CGM.GlobalsInt8PtrTy,
/*isConstant=*/true, llvm::GlobalValue::ExternalLinkage, nullptr, Name);
const CXXRecordDecl *RD = Ty->getAsCXXRecordDecl();
CGM.setGVProperties(GV, RD);
// Import the typeinfo symbol when all non-inline virtual methods are
Expand All @@ -3297,6 +3297,9 @@ ItaniumRTTIBuilder::GetAddrOfExternalRTTIDescriptor(QualType Ty) {
}
}

if (CGM.getTriple().isSPIR())
return llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast(GV, CGM.DefaultInt8PtrTy);

return GV;
}

Expand Down Expand Up @@ -3680,8 +3683,11 @@ void ItaniumRTTIBuilder::BuildVTablePointer(const Type *Ty) {
if (CGM.getItaniumVTableContext().isRelativeLayout())
VTable = CGM.getModule().getNamedAlias(VTableName);
if (!VTable) {
llvm::Type *Ty = llvm::ArrayType::get(CGM.DefaultInt8PtrTy, 0);
VTable = CGM.CreateRuntimeVariable(Ty, VTableName);
llvm::Type *Ty = llvm::ArrayType::get(CGM.GlobalsInt8PtrTy, 0);
if (CGM.getTriple().isSPIR())
VTable = CGM.CreateRuntimeVariable(CGM.DefaultInt8PtrTy, VTableName);
else
VTable = CGM.getModule().getOrInsertGlobal(VTableName, Ty);
}

CGM.setDSOLocal(cast<llvm::GlobalValue>(VTable->stripPointerCasts()));
Expand All @@ -3698,8 +3704,7 @@ void ItaniumRTTIBuilder::BuildVTablePointer(const Type *Ty) {
llvm::ConstantExpr::getInBoundsGetElementPtr(CGM.Int8Ty, VTable, Eight);
} else {
llvm::Constant *Two = llvm::ConstantInt::get(PtrDiffTy, 2);
VTable = llvm::ConstantExpr::getInBoundsGetElementPtr(CGM.DefaultInt8PtrTy,
VTable, Two);
VTable = llvm::ConstantExpr::getInBoundsGetElementPtr(CGM.getTriple().isSPIR() ? CGM.DefaultInt8PtrTy :CGM.GlobalsInt8PtrTy, VTable, Two);
}

Fields.push_back(VTable);
Expand Down Expand Up @@ -3775,6 +3780,9 @@ llvm::Constant *ItaniumRTTIBuilder::BuildTypeInfo(QualType Ty) {
assert(!OldGV->hasAvailableExternallyLinkage() &&
"available_externally typeinfos not yet implemented");

if (CGM.getTriple().isSPIR())
return llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast(OldGV, CGM.DefaultInt8PtrTy);

return OldGV;
}

Expand Down Expand Up @@ -3835,9 +3843,13 @@ llvm::Constant *ItaniumRTTIBuilder::BuildTypeInfo(
llvm::ConstantInt::get(CGM.Int64Ty, ((uint64_t)1) << 63);
TypeNameField = llvm::ConstantExpr::getAdd(TypeNameField, flag);
TypeNameField =
llvm::ConstantExpr::getIntToPtr(TypeNameField, CGM.Int8PtrTy);
llvm::ConstantExpr::getIntToPtr(TypeNameField, CGM.GlobalsInt8PtrTy);
} else {
TypeNameField = TypeName;
if (CGM.getTriple().isSPIR())
TypeNameField = llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast(
TypeName, CGM.DefaultInt8PtrTy);
else
TypeNameField = TypeName;
}
Fields.push_back(TypeNameField);

Expand Down Expand Up @@ -3965,7 +3977,7 @@ llvm::Constant *ItaniumRTTIBuilder::BuildTypeInfo(
GV->setComdat(M.getOrInsertComdat(GV->getName()));

CharUnits Align = CGM.getContext().toCharUnitsFromBits(
CGM.getTarget().getPointerAlign(LangAS::Default));
CGM.getTarget().getPointerAlign(CGM.GetGlobalVarAddressSpace(nullptr)));
GV->setAlignment(Align.getAsAlign());

// The Itanium ABI specifies that type_info objects must be globally
Expand Down Expand Up @@ -3997,6 +4009,10 @@ llvm::Constant *ItaniumRTTIBuilder::BuildTypeInfo(
TypeName->setPartition(CGM.getCodeGenOpts().SymbolPartition);
GV->setPartition(CGM.getCodeGenOpts().SymbolPartition);

if (CGM.getTriple().isSPIR())
return llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast(
GV, CGM.DefaultInt8PtrTy);

return GV;
}

Expand Down
13 changes: 13 additions & 0 deletions clang/test/CodeGenCXX/vtable-align-address-space.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// RUN: %clang_cc1 %s -triple=amdgcn-amd-amdhsa -std=c++11 -emit-llvm -o - | FileCheck %s

struct A {
virtual void f();
virtual void g();
virtual void h();
};

void A::f() {}

// CHECK: @_ZTV1A ={{.*}} unnamed_addr addrspace(1) constant { [5 x ptr addrspace(1)] } { [5 x ptr addrspace(1)] [ptr addrspace(1) null, ptr addrspace(1) @_ZTI1A, ptr addrspace(1) addrspacecast (ptr @_ZN1A1fEv to ptr addrspace(1)), ptr addrspace(1) addrspacecast (ptr @_ZN1A1gEv to ptr addrspace(1)), ptr addrspace(1) addrspacecast (ptr @_ZN1A1hEv to ptr addrspace(1))]
// CHECK: @_ZTS1A ={{.*}} constant [3 x i8] c"1A\00", align 1
// CHECK: @_ZTI1A ={{.*}} addrspace(1) constant { ptr addrspace(1), ptr addrspace(1) } { ptr addrspace(1) getelementptr inbounds (ptr addrspace(1), ptr addrspace(1) @_ZTVN10__cxxabiv117__class_type_infoE, i64 2), ptr addrspace(1) @_ZTS1A }, align 8
Loading
Loading