From 1e6303e60ca5af4fbe7ca728572fd65666a98271 Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Fri, 25 Jun 2021 11:26:15 +0200 Subject: [PATCH] [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 --- llvm/lib/IR/Type.cpp | 12 ++++-------- llvm/test/Assembler/invalid-opaque-ptr.ll | 7 ------- llvm/test/Assembler/opaque-ptr.ll | 3 +++ 3 files changed, 7 insertions(+), 15 deletions(-) delete mode 100644 llvm/test/Assembler/invalid-opaque-ptr.ll diff --git a/llvm/lib/IR/Type.cpp b/llvm/lib/IR/Type.cpp index e7716cd445ea..a7be01415fbf 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(EltTy) && cast(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(this)) - if (PTy->isOpaque()) - return PointerType::get(getContext(), AddrSpace); - return PointerType::get(const_cast(this), AddrSpace); } bool PointerType::isValidElementType(Type *ElemTy) { return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() && !ElemTy->isMetadataTy() && !ElemTy->isTokenTy() && - !ElemTy->isX86_AMXTy() && - !(ElemTy->isPointerTy() && cast(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 6acb78e9bda7..000000000000 --- 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 62d2d50786db..9167ca9e4586 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 +@global = external global ptr + ; CHECK: define ptr @f(ptr %a) { ; CHECK: %b = bitcast ptr %a to ptr ; CHECK: ret ptr %b