Skip to content

Commit 2138c90

Browse files
[IR] Move support for dxil::TypedPointerType to LLVM core IR.
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
1 parent 448adfe commit 2138c90

19 files changed

+89
-113
lines changed

llvm/include/llvm/IR/LLVMContext.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424

2525
namespace llvm {
2626

27-
class Any;
2827
class DiagnosticInfo;
2928
enum DiagnosticSeverity : char;
3029
class Function;
@@ -323,10 +322,6 @@ class LLVMContext {
323322
/// Whether typed pointers are supported. If false, all pointers are opaque.
324323
bool supportsTypedPointers() const;
325324

326-
/// Optionally target-spcific data can be attached to the context for lifetime
327-
/// management and bypassing layering restrictions.
328-
llvm::Any &getTargetData() const;
329-
330325
private:
331326
// Module needs access to the add/removeModule methods.
332327
friend class Module;

llvm/include/llvm/IR/Type.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class Type {
7575
ArrayTyID, ///< Arrays
7676
FixedVectorTyID, ///< Fixed width SIMD vector type
7777
ScalableVectorTyID, ///< Scalable SIMD vector type
78-
DXILPointerTyID, ///< DXIL typed pointer used by DirectX target
78+
TypedPointerTyID, ///< Typed pointer used by some GPU targets
7979
};
8080

8181
private:

llvm/lib/Target/DirectX/DXILPointerType.h renamed to llvm/include/llvm/IR/TypedPointerType.h

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,27 @@
1-
//===- Target/DirectX/DXILPointerType.h - DXIL Typed Pointer Type ---------===//
1+
//===- llvm/IR/TypedPointerType.h - Typed Pointer Type --------------------===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
55
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66
//
77
//===----------------------------------------------------------------------===//
88
//
9+
// This file contains typed pointer type information. It is separated out into
10+
// a separate file to make it less likely to accidentally use this type.
911
//
1012
//===----------------------------------------------------------------------===//
1113

12-
#ifndef LLVM_TARGET_DIRECTX_DXILPOINTERTYPE_H
13-
#define LLVM_TARGET_DIRECTX_DXILPOINTERTYPE_H
14+
#ifndef LLVM_IR_TYPEDPOINTERTYPE_H
15+
#define LLVM_IR_TYPEDPOINTERTYPE_H
1416

1517
#include "llvm/IR/Type.h"
1618

1719
namespace llvm {
18-
namespace dxil {
1920

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

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

4346
/// Implement support type inquiry through isa, cast, and dyn_cast.
4447
static bool classof(const Type *T) {
45-
return T->getTypeID() == DXILPointerTyID;
48+
return T->getTypeID() == TypedPointerTyID;
4649
}
4750
};
4851

49-
} // namespace dxil
5052
} // namespace llvm
5153

52-
#endif // LLVM_TARGET_DIRECTX_DXILPOINTERTYPE_H
54+
#endif // LLVM_IR_TYPEDPOINTERTYPE_H

llvm/lib/Bitcode/Writer/BitcodeWriter.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1029,8 +1029,8 @@ void ModuleBitcodeWriter::writeTypeTable() {
10291029
TypeVals.push_back(true);
10301030
break;
10311031
}
1032-
case Type::DXILPointerTyID:
1033-
llvm_unreachable("DXIL pointers cannot be added to IR modules");
1032+
case Type::TypedPointerTyID:
1033+
llvm_unreachable("Typed pointers cannot be added to IR modules");
10341034
}
10351035

10361036
// Emit the finished record.

llvm/lib/IR/AsmWriter.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
#include "llvm/IR/Operator.h"
6363
#include "llvm/IR/Type.h"
6464
#include "llvm/IR/TypeFinder.h"
65+
#include "llvm/IR/TypedPointerType.h"
6566
#include "llvm/IR/Use.h"
6667
#include "llvm/IR/User.h"
6768
#include "llvm/IR/Value.h"
@@ -610,12 +611,13 @@ void TypePrinting::print(Type *Ty, raw_ostream &OS) {
610611
OS << '>';
611612
return;
612613
}
613-
case Type::DXILPointerTyID:
614-
// DXIL pointer types are only handled by the DirectX backend. To avoid
615-
// extra dependencies we just print the pointer's address here.
616-
OS << "dxil-ptr (" << Ty << ")";
614+
case Type::TypedPointerTyID: {
615+
TypedPointerType *TPTy = cast<TypedPointerType>(Ty);
616+
OS << "typedptr(" << *TPTy->getElementType() << ", "
617+
<< TPTy->getAddressSpace() << ")";
617618
return;
618619
}
620+
}
619621
llvm_unreachable("Invalid TypeID");
620622
}
621623

llvm/lib/IR/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ add_llvm_component_library(LLVMCore
5757
Statepoint.cpp
5858
StructuralHash.cpp
5959
Type.cpp
60+
TypedPointerType.cpp
6061
TypeFinder.cpp
6162
Use.cpp
6263
User.cpp

llvm/lib/IR/Core.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -541,8 +541,8 @@ LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) {
541541
return LLVMTokenTypeKind;
542542
case Type::ScalableVectorTyID:
543543
return LLVMScalableVectorTypeKind;
544-
case Type::DXILPointerTyID:
545-
llvm_unreachable("DXIL pointers are unsupported via the C API");
544+
case Type::TypedPointerTyID:
545+
llvm_unreachable("Typed pointers are unsupported via the C API");
546546
}
547547
llvm_unreachable("Unhandled TypeID.");
548548
}

llvm/lib/IR/LLVMContext.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,3 @@ void LLVMContext::setOpaquePointers(bool Enable) const {
374374
bool LLVMContext::supportsTypedPointers() const {
375375
return !pImpl->getOpaquePointers();
376376
}
377-
378-
Any &LLVMContext::getTargetData() const {
379-
return pImpl->TargetDataStorage;
380-
}

llvm/lib/IR/LLVMContextImpl.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ class RemarkStreamer;
7070
}
7171
template <typename T> class StringMapEntry;
7272
class StringRef;
73+
class TypedPointerType;
7374
class ValueHandleBase;
7475

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

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

1574-
llvm::Any TargetDataStorage;
1575-
15761576
private:
15771577
Optional<bool> OpaquePointers;
15781578
};

llvm/lib/IR/TypedPointerType.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//===- TypedPointerType.cpp - Typed Pointer Type --------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
//
10+
//===----------------------------------------------------------------------===//
11+
12+
#include "llvm/IR/TypedPointerType.h"
13+
#include "LLVMContextImpl.h"
14+
15+
using namespace llvm;
16+
17+
TypedPointerType *TypedPointerType::get(Type *EltTy, unsigned AddressSpace) {
18+
assert(EltTy && "Can't get a pointer to <null> type!");
19+
assert(isValidElementType(EltTy) && "Invalid type for pointer element!");
20+
21+
LLVMContextImpl *CImpl = EltTy->getContext().pImpl;
22+
23+
// Since AddressSpace #0 is the common case, we special case it.
24+
TypedPointerType *&Entry =
25+
CImpl->ASTypedPointerTypes[std::make_pair(EltTy, AddressSpace)];
26+
27+
if (!Entry)
28+
Entry = new (CImpl->Alloc) TypedPointerType(EltTy, AddressSpace);
29+
return Entry;
30+
}
31+
32+
TypedPointerType::TypedPointerType(Type *E, unsigned AddrSpace)
33+
: Type(E->getContext(), TypedPointerTyID), PointeeTy(E) {
34+
ContainedTys = &PointeeTy;
35+
NumContainedTys = 1;
36+
setSubclassData(AddrSpace);
37+
}
38+
39+
bool TypedPointerType::isValidElementType(Type *ElemTy) {
40+
return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() &&
41+
!ElemTy->isMetadataTy() && !ElemTy->isTokenTy() &&
42+
!ElemTy->isX86_AMXTy();
43+
}

llvm/lib/IR/Value.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "llvm/IR/IntrinsicInst.h"
2626
#include "llvm/IR/Module.h"
2727
#include "llvm/IR/Operator.h"
28+
#include "llvm/IR/TypedPointerType.h"
2829
#include "llvm/IR/ValueHandle.h"
2930
#include "llvm/IR/ValueSymbolTable.h"
3031
#include "llvm/Support/CommandLine.h"
@@ -43,6 +44,8 @@ static cl::opt<unsigned> UseDerefAtPointSemantics(
4344
//===----------------------------------------------------------------------===//
4445
static inline Type *checkType(Type *Ty) {
4546
assert(Ty && "Value defined with a null type: Error!");
47+
assert(!isa<TypedPointerType>(Ty) &&
48+
"Cannot have values with typed pointer types");
4649
return Ty;
4750
}
4851

llvm/lib/Target/DirectX/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ add_llvm_target(DirectXCodeGen
1919
DirectXTargetMachine.cpp
2020
DXILOpBuilder.cpp
2121
DXILOpLowering.cpp
22-
DXILPointerType.cpp
2322
DXILPrepare.cpp
2423
DXILTranslateMetadata.cpp
2524
PointerTypeAnalysis.cpp

llvm/lib/Target/DirectX/DXILPointerType.cpp

Lines changed: 0 additions & 66 deletions
This file was deleted.

llvm/lib/Target/DirectX/DXILWriter/DXILBitcodeWriter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1068,7 +1068,7 @@ void DXILBitcodeWriter::writeTypeTable() {
10681068
Code = bitc::TYPE_CODE_INTEGER;
10691069
TypeVals.push_back(cast<IntegerType>(T)->getBitWidth());
10701070
break;
1071-
case Type::DXILPointerTyID: {
1071+
case Type::TypedPointerTyID: {
10721072
TypedPointerType *PTy = cast<TypedPointerType>(T);
10731073
// POINTER: [pointee type, address space]
10741074
Code = bitc::TYPE_CODE_POINTER;

llvm/lib/Target/DirectX/DXILWriter/DXILValueEnumerator.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
//===----------------------------------------------------------------------===//
1313

1414
#include "DXILValueEnumerator.h"
15-
#include "DXILPointerType.h"
1615
#include "llvm/ADT/SmallVector.h"
1716
#include "llvm/Config/llvm-config.h"
1817
#include "llvm/IR/Argument.h"
@@ -32,6 +31,7 @@
3231
#include "llvm/IR/Module.h"
3332
#include "llvm/IR/Operator.h"
3433
#include "llvm/IR/Type.h"
34+
#include "llvm/IR/TypedPointerType.h"
3535
#include "llvm/IR/Use.h"
3636
#include "llvm/IR/User.h"
3737
#include "llvm/IR/Value.h"
@@ -373,7 +373,7 @@ ValueEnumerator::ValueEnumerator(const Module &M, Type *PrefixType) {
373373
EnumerateValue(&F);
374374
EnumerateType(F.getValueType());
375375
EnumerateType(
376-
dxil::TypedPointerType::get(F.getFunctionType(), F.getAddressSpace()));
376+
TypedPointerType::get(F.getFunctionType(), F.getAddressSpace()));
377377
EnumerateAttributes(F.getAttributes());
378378
}
379379

@@ -394,7 +394,7 @@ ValueEnumerator::ValueEnumerator(const Module &M, Type *PrefixType) {
394394
if (GV.hasInitializer())
395395
EnumerateValue(GV.getInitializer());
396396
EnumerateType(
397-
dxil::TypedPointerType::get(GV.getValueType(), GV.getAddressSpace()));
397+
TypedPointerType::get(GV.getValueType(), GV.getAddressSpace()));
398398
if (GV.hasAttributes())
399399
EnumerateAttributes(GV.getAttributesAsList(AttributeList::FunctionIndex));
400400
}

llvm/lib/Target/DirectX/PointerTypeAnalysis.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
#ifndef LLVM_TARGET_DIRECTX_POINTERTYPEANALYSIS_H
1414
#define LLVM_TARGET_DIRECTX_POINTERTYPEANALYSIS_H
1515

16-
#include "DXILPointerType.h"
1716
#include "llvm/ADT/DenseMap.h"
1817
#include "llvm/IR/PassManager.h"
18+
#include "llvm/IR/TypedPointerType.h"
1919

2020
namespace llvm {
2121

llvm/lib/Target/Hexagon/HexagonTargetObjectFile.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ unsigned HexagonTargetObjectFile::getSmallestAddressableSize(const Type *Ty,
332332
case Type::X86_MMXTyID:
333333
case Type::X86_AMXTyID:
334334
case Type::TokenTyID:
335-
case Type::DXILPointerTyID:
335+
case Type::TypedPointerTyID:
336336
return 0;
337337
}
338338

llvm/unittests/IR/TypesTest.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include "llvm/IR/DerivedTypes.h"
1010
#include "llvm/IR/LLVMContext.h"
11+
#include "llvm/IR/TypedPointerType.h"
1112
#include "gtest/gtest.h"
1213
using namespace llvm;
1314

@@ -60,4 +61,14 @@ TEST(TypesTest, CopyPointerType) {
6061
EXPECT_FALSE(P2C0->isOpaque());
6162
}
6263

64+
TEST(TypedPointerType, PrintTest) {
65+
std::string Buffer;
66+
LLVMContext Context;
67+
raw_string_ostream OS(Buffer);
68+
69+
Type *I8Ptr = TypedPointerType::get(Type::getInt8Ty(Context), 0);
70+
I8Ptr->print(OS);
71+
EXPECT_EQ(StringRef(Buffer), ("typedptr(i8, 0)"));
72+
}
73+
6374
} // end anonymous namespace

0 commit comments

Comments
 (0)