Skip to content

[CodeGen] Replace of PointerType::get(Type) with opaque version (NFC) #124771

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

Merged
Merged
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
5 changes: 1 addition & 4 deletions clang/lib/CodeGen/Address.h
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,7 @@ class Address {

/// Return the type of the pointer value.
llvm::PointerType *getType() const {
return llvm::PointerType::get(
ElementType,
llvm::cast<llvm::PointerType>(Pointer.getPointer()->getType())
->getAddressSpace());
return llvm::cast<llvm::PointerType>(Pointer.getPointer()->getType());
}

/// Return the type of the values stored in this address.
Expand Down
23 changes: 1 addition & 22 deletions clang/lib/CodeGen/CGBlocks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1097,31 +1097,10 @@ llvm::Type *CodeGenModule::getBlockDescriptorType() {
if (BlockDescriptorType)
return BlockDescriptorType;

llvm::Type *UnsignedLongTy =
getTypes().ConvertType(getContext().UnsignedLongTy);

// struct __block_descriptor {
// unsigned long reserved;
// unsigned long block_size;
//
// // later, the following will be added
//
// struct {
// void (*copyHelper)();
// void (*copyHelper)();
// } helpers; // !!! optional
//
// const char *signature; // the block signature
// const char *layout; // reserved
// };
Comment on lines -1100 to -1116
Copy link
Member Author

@junlarsen junlarsen Jan 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do feel a bit guilty about removing this comment. However, I'm not experienced enough to know if it's significant and something we'd like to keep?

Perhaps move it somewhere?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's okay to drop it, essentially the same comment also exists on buildBlockDescriptor.

BlockDescriptorType = llvm::StructType::create(
"struct.__block_descriptor", UnsignedLongTy, UnsignedLongTy);

// Now form a pointer to that.
unsigned AddrSpace = 0;
if (getLangOpts().OpenCL)
AddrSpace = getContext().getTargetAddressSpace(LangAS::opencl_constant);
BlockDescriptorType = llvm::PointerType::get(BlockDescriptorType, AddrSpace);
BlockDescriptorType = llvm::PointerType::get(getLLVMContext(), AddrSpace);
return BlockDescriptorType;
}

Expand Down
8 changes: 2 additions & 6 deletions clang/lib/CodeGen/CGDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2870,15 +2870,12 @@ void CodeGenModule::EmitOMPAllocateDecl(const OMPAllocateDecl *D) {

// We can also keep the existing global if the address space is what we
// expect it to be, if not, it is replaced.
QualType ASTTy = VD->getType();
clang::LangAS GVAS = GetGlobalVarAddressSpace(VD);
auto TargetAS = getContext().getTargetAddressSpace(GVAS);
if (Entry->getType()->getAddressSpace() == TargetAS)
continue;

// Make a new global with the correct type / address space.
llvm::Type *Ty = getTypes().ConvertTypeForMem(ASTTy);
llvm::PointerType *PTy = llvm::PointerType::get(Ty, TargetAS);
llvm::PointerType *PTy = llvm::PointerType::get(getLLVMContext(), TargetAS);

// Replace all uses of the old global with a cast. Since we mutate the type
// in place we neeed an intermediate that takes the spot of the old entry
Expand All @@ -2891,8 +2888,7 @@ void CodeGenModule::EmitOMPAllocateDecl(const OMPAllocateDecl *D) {

Entry->mutateType(PTy);
llvm::Constant *NewPtrForOldDecl =
llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast(
Entry, DummyGV->getType());
llvm::ConstantExpr::getAddrSpaceCast(Entry, DummyGV->getType());

// Now we have a casted version of the changed global, the dummy can be
// replaced and deleted.
Expand Down
10 changes: 2 additions & 8 deletions clang/lib/CodeGen/CGDeclCXX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -345,10 +345,7 @@ void CodeGenFunction::registerGlobalDtorWithLLVM(const VarDecl &VD,

void CodeGenFunction::registerGlobalDtorWithAtExit(llvm::Constant *dtorStub) {
// extern "C" int atexit(void (*f)(void));
assert(dtorStub->getType() ==
llvm::PointerType::get(
llvm::FunctionType::get(CGM.VoidTy, false),
dtorStub->getType()->getPointerAddressSpace()) &&
assert(dtorStub->getType()->isPointerTy() &&
"Argument to atexit has a wrong type.");

llvm::FunctionType *atexitTy =
Expand All @@ -372,10 +369,7 @@ CodeGenFunction::unregisterGlobalDtorWithUnAtExit(llvm::Constant *dtorStub) {
// value is returned.
//
// extern "C" int unatexit(void (*f)(void));
assert(dtorStub->getType() ==
llvm::PointerType::get(
llvm::FunctionType::get(CGM.VoidTy, false),
dtorStub->getType()->getPointerAddressSpace()) &&
assert(dtorStub->getType()->isPointerTy() &&
"Argument to unatexit has a wrong type.");

llvm::FunctionType *unatexitTy =
Expand Down
4 changes: 2 additions & 2 deletions clang/lib/CodeGen/CGExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -872,7 +872,7 @@ void CodeGenFunction::EmitTypeCheck(TypeCheckKind TCK, SourceLocation Loc,
llvm::Value *TypeHash =
llvm::ConstantInt::get(Int64Ty, xxh3_64bits(Out.str()));

llvm::Type *VPtrTy = llvm::PointerType::get(IntPtrTy, 0);
llvm::Type *VPtrTy = llvm::PointerType::get(getLLVMContext(), 0);
Address VPtrAddr(Ptr, IntPtrTy, getPointerAlign());
llvm::Value *VPtrVal = GetVTablePtr(VPtrAddr, VPtrTy,
Ty->getAsCXXRecordDecl(),
Expand Down Expand Up @@ -3054,7 +3054,7 @@ LValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E) {
getContext().getDeclAlign(VD));
llvm::Type *VarTy = getTypes().ConvertTypeForMem(VD->getType());
auto *PTy = llvm::PointerType::get(
VarTy, getTypes().getTargetAddressSpace(VD->getType()));
getLLVMContext(), getTypes().getTargetAddressSpace(VD->getType()));
Addr = Builder.CreatePointerBitCastOrAddrSpaceCast(Addr, PTy, VarTy);
Copy link
Member Author

@junlarsen junlarsen Feb 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can the below cast be replaced with an AddrSpaceCast only? i.e CGBuilder::CreateAddrSpaceCast? I'm not too sure how the Clang codegen Address code behaves.

} else {
// Should we be using the alignment of the constant pointer we emitted?
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/CodeGen/CGObjCMac.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5717,7 +5717,7 @@ ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
IntTy = CGM.IntTy;
LongTy = cast<llvm::IntegerType>(Types.ConvertType(Ctx.LongTy));
Int8PtrTy = CGM.Int8PtrTy;
Int8PtrProgramASTy = llvm::PointerType::get(CGM.Int8Ty, ProgramAS);
Int8PtrProgramASTy = llvm::PointerType::get(CGM.getLLVMContext(), ProgramAS);
Int8PtrPtrTy = CGM.Int8PtrPtrTy;

// arm64 targets use "int" ivar offset variables. All others,
Expand Down
6 changes: 3 additions & 3 deletions clang/lib/CodeGen/CodeGenModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4432,7 +4432,7 @@ void CodeGenModule::emitCPUDispatchDefinition(GlobalDecl GD) {
GlobalDecl ResolverGD;
if (getTarget().supportsIFunc()) {
ResolverType = llvm::FunctionType::get(
llvm::PointerType::get(DeclTy,
llvm::PointerType::get(getLLVMContext(),
getTypes().getTargetAddressSpace(FD->getType())),
false);
}
Expand Down Expand Up @@ -4604,8 +4604,8 @@ llvm::Constant *CodeGenModule::GetOrCreateMultiVersionResolver(GlobalDecl GD) {
// cpu_dispatch will be emitted in this translation unit.
if (ShouldReturnIFunc) {
unsigned AS = getTypes().getTargetAddressSpace(FD->getType());
llvm::Type *ResolverType =
llvm::FunctionType::get(llvm::PointerType::get(DeclTy, AS), false);
llvm::Type *ResolverType = llvm::FunctionType::get(
llvm::PointerType::get(getLLVMContext(), AS), false);
llvm::Constant *Resolver = GetOrCreateLLVMFunction(
MangledName + ".resolver", ResolverType, GlobalDecl{},
/*ForVTable=*/false);
Expand Down