-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[clang][CodeGen] Make UnqualPtrTy
truly unqualified
#94388
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
base: main
Are you sure you want to change the base?
Conversation
@llvm/pr-subscribers-clang-codegen @llvm/pr-subscribers-clang Author: Alex Voicu (AlexVlx) ChangesAt the moment we alias Full diff: https://github.com/llvm/llvm-project/pull/94388.diff 3 Files Affected:
diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp
index be7bf0b72dc0c..b58ca03ce69da 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -368,6 +368,7 @@ CodeGenModule::CodeGenModule(ASTContext &C,
IntTy = llvm::IntegerType::get(LLVMContext, C.getTargetInfo().getIntWidth());
IntPtrTy = llvm::IntegerType::get(LLVMContext,
C.getTargetInfo().getMaxPointerWidth());
+ UnqualPtrTy = llvm::PointerType::getUnqual(LLVMContext);
Int8PtrTy = llvm::PointerType::get(LLVMContext,
C.getTargetAddressSpace(LangAS::Default));
const llvm::DataLayout &DL = M.getDataLayout();
diff --git a/clang/lib/CodeGen/CodeGenTypeCache.h b/clang/lib/CodeGen/CodeGenTypeCache.h
index e273ebe3b060f..3b659bc182aa4 100644
--- a/clang/lib/CodeGen/CodeGenTypeCache.h
+++ b/clang/lib/CodeGen/CodeGenTypeCache.h
@@ -51,9 +51,11 @@ struct CodeGenTypeCache {
llvm::IntegerType *PtrDiffTy;
};
+ /// unqualified void*
+ llvm::PointerType *UnqualPtrTy;
+
/// void*, void** in the target's default address space (often 0)
union {
- llvm::PointerType *UnqualPtrTy;
llvm::PointerType *VoidPtrTy;
llvm::PointerType *Int8PtrTy;
llvm::PointerType *VoidPtrPtrTy;
diff --git a/clang/lib/CodeGen/ItaniumCXXABI.cpp b/clang/lib/CodeGen/ItaniumCXXABI.cpp
index 8427286dee887..8952e7ca072da 100644
--- a/clang/lib/CodeGen/ItaniumCXXABI.cpp
+++ b/clang/lib/CodeGen/ItaniumCXXABI.cpp
@@ -4656,14 +4656,14 @@ static void InitCatchParam(CodeGenFunction &CGF,
auto catchRD = CatchType->getAsCXXRecordDecl();
CharUnits caughtExnAlignment = CGF.CGM.getClassPointerAlignment(catchRD);
- llvm::Type *PtrTy = CGF.UnqualPtrTy; // addrspace 0 ok
+ llvm::Type *PtrTy = CGF.UnqualPtrTy; // unqualified is ok
// Check for a copy expression. If we don't have a copy expression,
// that means a trivial copy is okay.
const Expr *copyExpr = CatchParam.getInit();
if (!copyExpr) {
llvm::Value *rawAdjustedExn = CallBeginCatch(CGF, Exn, true);
- Address adjustedExn(CGF.Builder.CreateBitCast(rawAdjustedExn, PtrTy),
+ Address adjustedExn(CGF.Builder.CreatePointerCast(rawAdjustedExn, PtrTy),
LLVMCatchTy, caughtExnAlignment);
LValue Dest = CGF.MakeAddrLValue(ParamAddr, CatchType);
LValue Src = CGF.MakeAddrLValue(adjustedExn, CatchType);
@@ -4677,7 +4677,7 @@ static void InitCatchParam(CodeGenFunction &CGF,
CGF.EmitNounwindRuntimeCall(getGetExceptionPtrFn(CGF.CGM), Exn);
// Cast that to the appropriate type.
- Address adjustedExn(CGF.Builder.CreateBitCast(rawAdjustedExn, PtrTy),
+ Address adjustedExn(CGF.Builder.CreatePointerCast(rawAdjustedExn, PtrTy),
LLVMCatchTy, caughtExnAlignment);
// The copy expression is defined in terms of an OpaqueValueExpr.
|
I don't think that assumption is currently true. I think it's also worth clarifying what this thing is, and possibly renaming it, because "unqualified" has C language level meaning that would contradict what it is here. |
llvm::PointerType::getUnqual assumes pointers to addr-space 0 are "unqualified"... but the discussion on #88182 concluded that doesn't make sense: the "unqualified" address space is a target-specific choice, and address-space zero isn't required to have any meaning at all. Given that, it seems like this is going in the opposite direction of what you want. |
I think the point is there are two things going on here that people think of as unqualified:
The vast majority of the time we're talking about 1, which is what most people would think of unqualified as meaning. #93601 / #93914 are about the three special cases that are case 2 above. But those are a special case, and normally when people say unqualified they mean "default" address space / |
Building on the above, let me add a bit more colour I should have added from the get go:
Having said that, this change is somewhat inert at the moment beyond carving out the name / making it shorthand for calling
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think that assumption is currently true. I think it's also worth clarifying what this thing is, and possibly renaming it, because "unqualified" has C language level meaning that would contradict what it is here.
It's probably optimistic, based on us also having Int8PtrTy
/ VoidPtrTy
which seem less "scary" (for lack of a better word) and more familiar for someone coming from e.g. C. In what regards renaming, I don't have an immediate idea of what to rename it to, and it's tied to PointerType::getUnqual
, so if we rename this we'd probably have to rename that as well, no?
@@ -4656,14 +4656,14 @@ static void InitCatchParam(CodeGenFunction &CGF, | |||
auto catchRD = CatchType->getAsCXXRecordDecl(); | |||
CharUnits caughtExnAlignment = CGF.CGM.getClassPointerAlignment(catchRD); | |||
|
|||
llvm::Type *PtrTy = CGF.UnqualPtrTy; // addrspace 0 ok |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not convinced this is actually correct, it's been a long time since I looked at C++ exception handling, but I would have assumed this to either be a stack variable or a global?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be a pointer to a heap-allocated object.
In any case, we shouldn't need to cast the return value of __cxa_begin_catch to a different address-space; we can just drop the bitcasts.
@@ -51,9 +51,11 @@ struct CodeGenTypeCache { | |||
llvm::IntegerType *PtrDiffTy; | |||
}; | |||
|
|||
/// unqualified void* | |||
llvm::PointerType *UnqualPtrTy; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd argue we should just remove this variable, and the few cases that actually want AS0 can call PointerType::get(0) directly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not opposed to this in principle; might have some additional benefits in that it forces people to knowingly opt for something via PointerType::getUnqual
/ PointerType::get(some_as)
, rather than accidentally stumbling into it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks fine to me now, but I'd suggest a TODO comment that UnqualPtrTy should be removed in the future.
There are still quite a few references to UnqualPtrTy that you're not changing yet are now wrong, no?
|
And I still strongly urge renaming what this is, given it is not the type for an unqualified C |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah I did not realize there were so many uses remaining. I that case those probably need auditing/changing before this change can land.
DummyPtrTy seems mnemonic / on point, but it might trigger antibodies from the LLVM side:) Perhaps |
Right, going through these at the moment, I got sidetracked. |
At the moment we alias
UnqualPtrTy
with the other default AS pointer types, which means that for certain targets using it doesn't actually yield an unqualified pointer. This patch changes that so that it does exactly what it says on the tin, under the assumption that if someone asks for an unqualified pointer they do so knowingly and expect to get exactly that.