[llvm] 1e6303e - [OpaquePtr] Allow globals with opaque pointer value type
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Fri Jun 25 09:29:22 PDT 2021
Author: Nikita Popov
Date: 2021-06-25T18:22:26+02:00
New Revision: 1e6303e60ca5af4fbe7ca728572fd65666a98271
URL: https://github.com/llvm/llvm-project/commit/1e6303e60ca5af4fbe7ca728572fd65666a98271
DIFF: https://github.com/llvm/llvm-project/commit/1e6303e60ca5af4fbe7ca728572fd65666a98271.diff
LOG: [OpaquePtr] Allow globals with opaque pointer value type
Do this by making opaque pointers a valid pointer element type,
for which we implicitly create an opaque pointer (moving the logic
from getPointerTo into PointerType::get).
We'll never create something like a "pointer to opaque pointer",
but accept it in the API, because a lot of code reasonably assumes
that you can create a pointer to pointer type.
Differential Revision: https://reviews.llvm.org/D104902
Added:
Modified:
llvm/lib/IR/Type.cpp
llvm/test/Assembler/opaque-ptr.ll
Removed:
llvm/test/Assembler/invalid-opaque-ptr.ll
################################################################################
diff --git a/llvm/lib/IR/Type.cpp b/llvm/lib/IR/Type.cpp
index e7716cd445ea2..a7be01415fbf9 100644
--- a/llvm/lib/IR/Type.cpp
+++ b/llvm/lib/IR/Type.cpp
@@ -690,7 +690,9 @@ PointerType *PointerType::get(Type *EltTy, unsigned AddressSpace) {
LLVMContextImpl *CImpl = EltTy->getContext().pImpl;
- if (CImpl->ForceOpaquePointers)
+ // Create opaque pointer for pointer to opaque pointer.
+ if (CImpl->ForceOpaquePointers ||
+ (isa<PointerType>(EltTy) && cast<PointerType>(EltTy)->isOpaque()))
return get(EltTy->getContext(), AddressSpace);
// Since AddressSpace #0 is the common case, we special case it.
@@ -729,19 +731,13 @@ PointerType::PointerType(LLVMContext &C, unsigned AddrSpace)
}
PointerType *Type::getPointerTo(unsigned AddrSpace) const {
- // Pointer to opaque pointer is opaque pointer.
- if (auto *PTy = dyn_cast<PointerType>(this))
- if (PTy->isOpaque())
- return PointerType::get(getContext(), AddrSpace);
-
return PointerType::get(const_cast<Type*>(this), AddrSpace);
}
bool PointerType::isValidElementType(Type *ElemTy) {
return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() &&
!ElemTy->isMetadataTy() && !ElemTy->isTokenTy() &&
- !ElemTy->isX86_AMXTy() &&
- !(ElemTy->isPointerTy() && cast<PointerType>(ElemTy)->isOpaque());
+ !ElemTy->isX86_AMXTy();
}
bool PointerType::isLoadableOrStorableType(Type *ElemTy) {
diff --git a/llvm/test/Assembler/invalid-opaque-ptr.ll b/llvm/test/Assembler/invalid-opaque-ptr.ll
deleted file mode 100644
index 6acb78e9bda7d..0000000000000
--- a/llvm/test/Assembler/invalid-opaque-ptr.ll
+++ /dev/null
@@ -1,7 +0,0 @@
-; RUN: not llvm-as < %s -disable-output 2>&1 | FileCheck %s
-
-; CHECK: pointer to this type is invalid
-define void @f(ptr %a) {
- %b = bitcast ptr %a to ptr*
- ret void
-}
diff --git a/llvm/test/Assembler/opaque-ptr.ll b/llvm/test/Assembler/opaque-ptr.ll
index 62d2d50786db4..9167ca9e45866 100644
--- a/llvm/test/Assembler/opaque-ptr.ll
+++ b/llvm/test/Assembler/opaque-ptr.ll
@@ -1,6 +1,9 @@
; RUN: llvm-as < %s | llvm-dis | llvm-as | llvm-dis | FileCheck %s
; RUN: verify-uselistorder %s
+; CHECK: @global = external global ptr
+ at global = external global ptr
+
; CHECK: define ptr @f(ptr %a) {
; CHECK: %b = bitcast ptr %a to ptr
; CHECK: ret ptr %b
More information about the llvm-commits
mailing list