Skip to content

Commit

Permalink
[IR] Move support for dxil::TypedPointerType to LLVM core IR.
Browse files Browse the repository at this point in the history
This allows the construct to be shared between different backends. However, it
still remains illegal to use TypedPointerType in LLVM IR--the type is intended
to remain an auxiliary type, not a real LLVM type. So no support is provided for
LLVM-C, nor bitcode, nor LLVM assembly (besides the bare minimum needed to make
Type->dump() work properly).

Reviewed By: beanz, nikic, aeubanks

Differential Revision: https://reviews.llvm.org/D130592
  • Loading branch information
jcranmer-intel authored and maleadt committed Jun 7, 2024
1 parent 2c071f6 commit 6415c86
Show file tree
Hide file tree
Showing 20 changed files with 90 additions and 114 deletions.
5 changes: 0 additions & 5 deletions llvm/include/llvm/IR/LLVMContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

namespace llvm {

class Any;
class DiagnosticInfo;
enum DiagnosticSeverity : char;
class Function;
Expand Down Expand Up @@ -323,10 +322,6 @@ class LLVMContext {
/// Whether typed pointers are supported. If false, all pointers are opaque.
bool supportsTypedPointers() const;

/// Optionally target-spcific data can be attached to the context for lifetime
/// management and bypassing layering restrictions.
llvm::Any &getTargetData() const;

private:
// Module needs access to the add/removeModule methods.
friend class Module;
Expand Down
2 changes: 1 addition & 1 deletion llvm/include/llvm/IR/Type.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class Type {
ArrayTyID, ///< Arrays
FixedVectorTyID, ///< Fixed width SIMD vector type
ScalableVectorTyID, ///< Scalable SIMD vector type
DXILPointerTyID, ///< DXIL typed pointer used by DirectX target
TypedPointerTyID, ///< Typed pointer used by some GPU targets
};

private:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
//===- Target/DirectX/DXILPointerType.h - DXIL Typed Pointer Type ---------===//
//===- llvm/IR/TypedPointerType.h - Typed Pointer Type --------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file contains typed pointer type information. It is separated out into
// a separate file to make it less likely to accidentally use this type.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_TARGET_DIRECTX_DXILPOINTERTYPE_H
#define LLVM_TARGET_DIRECTX_DXILPOINTERTYPE_H
#ifndef LLVM_IR_TYPEDPOINTERTYPE_H
#define LLVM_IR_TYPEDPOINTERTYPE_H

#include "llvm/IR/Type.h"

namespace llvm {
namespace dxil {

// DXIL has typed pointers, this pointer type abstraction is used for tracking
// in PointerTypeAnalysis and for the bitcode ValueEnumerator
/// A few GPU targets, such as DXIL and SPIR-V, have typed pointers. This
/// pointer type abstraction is used for tracking the types of these pointers.
/// It is not legal to use this type, or derived types containing this type, in
/// LLVM IR.
class TypedPointerType : public Type {
explicit TypedPointerType(Type *ElType, unsigned AddrSpace);

Expand All @@ -42,11 +45,10 @@ class TypedPointerType : public Type {

/// Implement support type inquiry through isa, cast, and dyn_cast.
static bool classof(const Type *T) {
return T->getTypeID() == DXILPointerTyID;
return T->getTypeID() == TypedPointerTyID;
}
};

} // namespace dxil
} // namespace llvm

#endif // LLVM_TARGET_DIRECTX_DXILPOINTERTYPE_H
#endif // LLVM_IR_TYPEDPOINTERTYPE_H
4 changes: 2 additions & 2 deletions llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1029,8 +1029,8 @@ void ModuleBitcodeWriter::writeTypeTable() {
TypeVals.push_back(true);
break;
}
case Type::DXILPointerTyID:
llvm_unreachable("DXIL pointers cannot be added to IR modules");
case Type::TypedPointerTyID:
llvm_unreachable("Typed pointers cannot be added to IR modules");
}

// Emit the finished record.
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Bitcode/Writer70/BitcodeWriter70.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -941,7 +941,7 @@ void ModuleBitcodeWriter70::writeTypeTable() {
llvm_unreachable("bfloat16 type is not supported with LLVM 7.0");
case Type::X86_AMXTyID:
llvm_unreachable("AMX types are not supported with LLVM 7.0");
case Type::DXILPointerTyID:
case Type::TypedPointerTyID:
llvm_unreachable("DXIL pointers cannot be added to IR modules");
}

Expand Down
10 changes: 6 additions & 4 deletions llvm/lib/IR/AsmWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
#include "llvm/IR/Operator.h"
#include "llvm/IR/Type.h"
#include "llvm/IR/TypeFinder.h"
#include "llvm/IR/TypedPointerType.h"
#include "llvm/IR/Use.h"
#include "llvm/IR/User.h"
#include "llvm/IR/Value.h"
Expand Down Expand Up @@ -610,12 +611,13 @@ void TypePrinting::print(Type *Ty, raw_ostream &OS) {
OS << '>';
return;
}
case Type::DXILPointerTyID:
// DXIL pointer types are only handled by the DirectX backend. To avoid
// extra dependencies we just print the pointer's address here.
OS << "dxil-ptr (" << Ty << ")";
case Type::TypedPointerTyID: {
TypedPointerType *TPTy = cast<TypedPointerType>(Ty);
OS << "typedptr(" << *TPTy->getElementType() << ", "
<< TPTy->getAddressSpace() << ")";
return;
}
}
llvm_unreachable("Invalid TypeID");
}

Expand Down
1 change: 1 addition & 0 deletions llvm/lib/IR/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ add_llvm_component_library(LLVMCore
Statepoint.cpp
StructuralHash.cpp
Type.cpp
TypedPointerType.cpp
TypeFinder.cpp
Use.cpp
User.cpp
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/IR/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -541,8 +541,8 @@ LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) {
return LLVMTokenTypeKind;
case Type::ScalableVectorTyID:
return LLVMScalableVectorTypeKind;
case Type::DXILPointerTyID:
llvm_unreachable("DXIL pointers are unsupported via the C API");
case Type::TypedPointerTyID:
llvm_unreachable("Typed pointers are unsupported via the C API");
}
llvm_unreachable("Unhandled TypeID.");
}
Expand Down
4 changes: 0 additions & 4 deletions llvm/lib/IR/LLVMContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,3 @@ void LLVMContext::setOpaquePointers(bool Enable) const {
bool LLVMContext::supportsTypedPointers() const {
return !pImpl->getOpaquePointers();
}

Any &LLVMContext::getTargetData() const {
return pImpl->TargetDataStorage;
}
4 changes: 2 additions & 2 deletions llvm/lib/IR/LLVMContextImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class RemarkStreamer;
}
template <typename T> class StringMapEntry;
class StringRef;
class TypedPointerType;
class ValueHandleBase;

using DenseMapAPIntKeyInfo = DenseMapInfo<APInt>;
Expand Down Expand Up @@ -1484,6 +1485,7 @@ class LLVMContextImpl {
DenseMap<std::pair<Type *, ElementCount>, VectorType *> VectorTypes;
DenseMap<Type *, PointerType *> PointerTypes; // Pointers in AddrSpace = 0
DenseMap<std::pair<Type *, unsigned>, PointerType *> ASPointerTypes;
DenseMap<std::pair<Type *, unsigned>, TypedPointerType *> ASTypedPointerTypes;

/// ValueHandles - This map keeps track of all of the value handles that are
/// watching a Value*. The Value::HasValueHandle bit is used to know
Expand Down Expand Up @@ -1571,8 +1573,6 @@ class LLVMContextImpl {
bool hasOpaquePointersValue();
void setOpaquePointers(bool OP);

llvm::Any TargetDataStorage;

private:
Optional<bool> OpaquePointers;
};
Expand Down
43 changes: 43 additions & 0 deletions llvm/lib/IR/TypedPointerType.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//===- TypedPointerType.cpp - Typed Pointer Type --------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
//
//===----------------------------------------------------------------------===//

#include "llvm/IR/TypedPointerType.h"
#include "LLVMContextImpl.h"

using namespace llvm;

TypedPointerType *TypedPointerType::get(Type *EltTy, unsigned AddressSpace) {
assert(EltTy && "Can't get a pointer to <null> type!");
assert(isValidElementType(EltTy) && "Invalid type for pointer element!");

LLVMContextImpl *CImpl = EltTy->getContext().pImpl;

// Since AddressSpace #0 is the common case, we special case it.
TypedPointerType *&Entry =
CImpl->ASTypedPointerTypes[std::make_pair(EltTy, AddressSpace)];

if (!Entry)
Entry = new (CImpl->Alloc) TypedPointerType(EltTy, AddressSpace);
return Entry;
}

TypedPointerType::TypedPointerType(Type *E, unsigned AddrSpace)
: Type(E->getContext(), TypedPointerTyID), PointeeTy(E) {
ContainedTys = &PointeeTy;
NumContainedTys = 1;
setSubclassData(AddrSpace);
}

bool TypedPointerType::isValidElementType(Type *ElemTy) {
return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() &&
!ElemTy->isMetadataTy() && !ElemTy->isTokenTy() &&
!ElemTy->isX86_AMXTy();
}
3 changes: 3 additions & 0 deletions llvm/lib/IR/Value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Operator.h"
#include "llvm/IR/TypedPointerType.h"
#include "llvm/IR/ValueHandle.h"
#include "llvm/IR/ValueSymbolTable.h"
#include "llvm/Support/CommandLine.h"
Expand All @@ -43,6 +44,8 @@ static cl::opt<unsigned> UseDerefAtPointSemantics(
//===----------------------------------------------------------------------===//
static inline Type *checkType(Type *Ty) {
assert(Ty && "Value defined with a null type: Error!");
assert(!isa<TypedPointerType>(Ty) &&
"Cannot have values with typed pointer types");
return Ty;
}

Expand Down
1 change: 0 additions & 1 deletion llvm/lib/Target/DirectX/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ add_llvm_target(DirectXCodeGen
DirectXTargetMachine.cpp
DXILOpBuilder.cpp
DXILOpLowering.cpp
DXILPointerType.cpp
DXILPrepare.cpp
DXILTranslateMetadata.cpp
PointerTypeAnalysis.cpp
Expand Down
66 changes: 0 additions & 66 deletions llvm/lib/Target/DirectX/DXILPointerType.cpp

This file was deleted.

2 changes: 1 addition & 1 deletion llvm/lib/Target/DirectX/DXILWriter/DXILBitcodeWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1068,7 +1068,7 @@ void DXILBitcodeWriter::writeTypeTable() {
Code = bitc::TYPE_CODE_INTEGER;
TypeVals.push_back(cast<IntegerType>(T)->getBitWidth());
break;
case Type::DXILPointerTyID: {
case Type::TypedPointerTyID: {
TypedPointerType *PTy = cast<TypedPointerType>(T);
// POINTER: [pointee type, address space]
Code = bitc::TYPE_CODE_POINTER;
Expand Down
6 changes: 3 additions & 3 deletions llvm/lib/Target/DirectX/DXILWriter/DXILValueEnumerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
//===----------------------------------------------------------------------===//

#include "DXILValueEnumerator.h"
#include "DXILPointerType.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/Config/llvm-config.h"
#include "llvm/IR/Argument.h"
Expand All @@ -32,6 +31,7 @@
#include "llvm/IR/Module.h"
#include "llvm/IR/Operator.h"
#include "llvm/IR/Type.h"
#include "llvm/IR/TypedPointerType.h"
#include "llvm/IR/Use.h"
#include "llvm/IR/User.h"
#include "llvm/IR/Value.h"
Expand Down Expand Up @@ -373,7 +373,7 @@ ValueEnumerator::ValueEnumerator(const Module &M, Type *PrefixType) {
EnumerateValue(&F);
EnumerateType(F.getValueType());
EnumerateType(
dxil::TypedPointerType::get(F.getFunctionType(), F.getAddressSpace()));
TypedPointerType::get(F.getFunctionType(), F.getAddressSpace()));
EnumerateAttributes(F.getAttributes());
}

Expand All @@ -394,7 +394,7 @@ ValueEnumerator::ValueEnumerator(const Module &M, Type *PrefixType) {
if (GV.hasInitializer())
EnumerateValue(GV.getInitializer());
EnumerateType(
dxil::TypedPointerType::get(GV.getValueType(), GV.getAddressSpace()));
TypedPointerType::get(GV.getValueType(), GV.getAddressSpace()));
if (GV.hasAttributes())
EnumerateAttributes(GV.getAttributesAsList(AttributeList::FunctionIndex));
}
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Target/DirectX/PointerTypeAnalysis.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
#ifndef LLVM_TARGET_DIRECTX_POINTERTYPEANALYSIS_H
#define LLVM_TARGET_DIRECTX_POINTERTYPEANALYSIS_H

#include "DXILPointerType.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/IR/PassManager.h"
#include "llvm/IR/TypedPointerType.h"

namespace llvm {

Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Target/Hexagon/HexagonTargetObjectFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ unsigned HexagonTargetObjectFile::getSmallestAddressableSize(const Type *Ty,
case Type::X86_MMXTyID:
case Type::X86_AMXTyID:
case Type::TokenTyID:
case Type::DXILPointerTyID:
case Type::TypedPointerTyID:
return 0;
}

Expand Down
11 changes: 11 additions & 0 deletions llvm/unittests/IR/TypesTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/TypedPointerType.h"
#include "gtest/gtest.h"
using namespace llvm;

Expand Down Expand Up @@ -60,4 +61,14 @@ TEST(TypesTest, CopyPointerType) {
EXPECT_FALSE(P2C0->isOpaque());
}

TEST(TypedPointerType, PrintTest) {
std::string Buffer;
LLVMContext Context;
raw_string_ostream OS(Buffer);

Type *I8Ptr = TypedPointerType::get(Type::getInt8Ty(Context), 0);
I8Ptr->print(OS);
EXPECT_EQ(StringRef(Buffer), ("typedptr(i8, 0)"));
}

} // end anonymous namespace
Loading

0 comments on commit 6415c86

Please sign in to comment.