[clang] [CodeGen] Replace of PointerType::get(Type) with opaque version (NFC) (PR #124771)
Mats Jun Larsen via cfe-commits
cfe-commits at lists.llvm.org
Fri Feb 7 07:47:31 PST 2025
https://github.com/junlarsen updated https://github.com/llvm/llvm-project/pull/124771
>From f3bbd6f0db4109016d98fe15a2b086b491df2d93 Mon Sep 17 00:00:00 2001
From: Mats Jun Larsen <mats at jun.codes>
Date: Wed, 29 Jan 2025 00:31:32 +0900
Subject: [PATCH 1/2] [CodeGen] Replace of PointerType::get(Type) with opaque
version (NFC)
Follow-up to https://github.com/llvm/llvm-project/issues/123569
---
clang/lib/CodeGen/Address.h | 8 ++++----
clang/lib/CodeGen/CGBlocks.cpp | 23 +----------------------
clang/lib/CodeGen/CGDecl.cpp | 5 +----
clang/lib/CodeGen/CGDeclCXX.cpp | 4 ++--
clang/lib/CodeGen/CGExpr.cpp | 4 ++--
clang/lib/CodeGen/CGObjCMac.cpp | 2 +-
clang/lib/CodeGen/CodeGenModule.cpp | 6 +++---
7 files changed, 14 insertions(+), 38 deletions(-)
diff --git a/clang/lib/CodeGen/Address.h b/clang/lib/CodeGen/Address.h
index a18c7169af1eb96..852aa0e686fe3c6 100644
--- a/clang/lib/CodeGen/Address.h
+++ b/clang/lib/CodeGen/Address.h
@@ -19,6 +19,7 @@
#include "clang/AST/Type.h"
#include "llvm/ADT/PointerIntPair.h"
#include "llvm/IR/Constants.h"
+#include "llvm/IR/DerivedTypes.h"
#include "llvm/Support/MathExtras.h"
namespace clang {
@@ -197,10 +198,9 @@ 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());
+ auto AS = llvm::cast<llvm::PointerType>(Pointer.getPointer()->getType())
+ ->getAddressSpace();
+ return llvm::PointerType::get(ElementType->getContext(), AS);
}
/// Return the type of the values stored in this address.
diff --git a/clang/lib/CodeGen/CGBlocks.cpp b/clang/lib/CodeGen/CGBlocks.cpp
index a7584a95c8ca7b9..033528dbceb3bd3 100644
--- a/clang/lib/CodeGen/CGBlocks.cpp
+++ b/clang/lib/CodeGen/CGBlocks.cpp
@@ -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
- // };
- 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;
}
diff --git a/clang/lib/CodeGen/CGDecl.cpp b/clang/lib/CodeGen/CGDecl.cpp
index ded905cdcc9f440..feb4ddbb52b2b7d 100644
--- a/clang/lib/CodeGen/CGDecl.cpp
+++ b/clang/lib/CodeGen/CGDecl.cpp
@@ -2868,15 +2868,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
diff --git a/clang/lib/CodeGen/CGDeclCXX.cpp b/clang/lib/CodeGen/CGDeclCXX.cpp
index 1c2fecea1a6ac25..5005f6b3cbd2d1a 100644
--- a/clang/lib/CodeGen/CGDeclCXX.cpp
+++ b/clang/lib/CodeGen/CGDeclCXX.cpp
@@ -347,7 +347,7 @@ 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),
+ CGM.getLLVMContext(),
dtorStub->getType()->getPointerAddressSpace()) &&
"Argument to atexit has a wrong type.");
@@ -374,7 +374,7 @@ CodeGenFunction::unregisterGlobalDtorWithUnAtExit(llvm::Constant *dtorStub) {
// extern "C" int unatexit(void (*f)(void));
assert(dtorStub->getType() ==
llvm::PointerType::get(
- llvm::FunctionType::get(CGM.VoidTy, false),
+ CGM.getLLVMContext(),
dtorStub->getType()->getPointerAddressSpace()) &&
"Argument to unatexit has a wrong type.");
diff --git a/clang/lib/CodeGen/CGExpr.cpp b/clang/lib/CodeGen/CGExpr.cpp
index 9676e61cf322d92..d3b1468f21b9531 100644
--- a/clang/lib/CodeGen/CGExpr.cpp
+++ b/clang/lib/CodeGen/CGExpr.cpp
@@ -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(),
@@ -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);
} else {
// Should we be using the alignment of the constant pointer we emitted?
diff --git a/clang/lib/CodeGen/CGObjCMac.cpp b/clang/lib/CodeGen/CGObjCMac.cpp
index dd900f9b32fb78e..6c929a6431c0f06 100644
--- a/clang/lib/CodeGen/CGObjCMac.cpp
+++ b/clang/lib/CodeGen/CGObjCMac.cpp
@@ -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,
diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp
index eb8d3ceeeba4c0d..2c51de5bf7da161 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -4424,7 +4424,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);
}
@@ -4596,8 +4596,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);
>From 1cab2da04deed320c8fe7ceccb09faf5b986290c Mon Sep 17 00:00:00 2001
From: Mats Jun Larsen <mats at jun.codes>
Date: Sat, 8 Feb 2025 00:19:50 +0900
Subject: [PATCH 2/2] [CodeGen] Replace now pointless bitcast
The pointer type is already in the correct address space. Thus the
potential addr space cast from getPointerBitCastOrAddrSpaceCast is not
needed. Bitcasting pointers does not change anything with opaque
pointers.
---
clang/lib/CodeGen/CGDecl.cpp | 18 ------------------
1 file changed, 18 deletions(-)
diff --git a/clang/lib/CodeGen/CGDecl.cpp b/clang/lib/CodeGen/CGDecl.cpp
index feb4ddbb52b2b7d..d1a449c67a5046c 100644
--- a/clang/lib/CodeGen/CGDecl.cpp
+++ b/clang/lib/CodeGen/CGDecl.cpp
@@ -2874,25 +2874,7 @@ void CodeGenModule::EmitOMPAllocateDecl(const OMPAllocateDecl *D) {
continue;
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
- // until we can create the cast.
- llvm::GlobalVariable *DummyGV = new llvm::GlobalVariable(
- getModule(), Entry->getValueType(), false,
- llvm::GlobalValue::CommonLinkage, nullptr, "dummy", nullptr,
- llvm::GlobalVariable::NotThreadLocal, Entry->getAddressSpace());
- Entry->replaceAllUsesWith(DummyGV);
-
Entry->mutateType(PTy);
- llvm::Constant *NewPtrForOldDecl =
- llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast(
- Entry, DummyGV->getType());
-
- // Now we have a casted version of the changed global, the dummy can be
- // replaced and deleted.
- DummyGV->replaceAllUsesWith(NewPtrForOldDecl);
- DummyGV->eraseFromParent();
}
}
More information about the cfe-commits
mailing list