clang 23.0.0git
DirectX.cpp
Go to the documentation of this file.
1//===- DirectX.cpp---------------------------------------------------------===//
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#include "ABIInfoImpl.h"
10#include "CodeGenModule.h"
12#include "TargetInfo.h"
13#include "clang/AST/Type.h"
14#include "llvm/ADT/SmallVector.h"
15#include "llvm/IR/DerivedTypes.h"
16#include "llvm/IR/Type.h"
17
18using namespace clang;
19using namespace clang::CodeGen;
20
21//===----------------------------------------------------------------------===//
22// Target codegen info implementation for DirectX.
23//===----------------------------------------------------------------------===//
24
25namespace {
26
27class DirectXTargetCodeGenInfo : public TargetCodeGenInfo {
28public:
29 DirectXTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
30 : TargetCodeGenInfo(std::make_unique<DefaultABIInfo>(CGT)) {}
31
32 llvm::Type *getHLSLType(CodeGenModule &CGM, const Type *T,
33 const CGHLSLOffsetInfo &OffsetInfo) const override;
34
35 llvm::Type *getHLSLPadding(CodeGenModule &CGM,
36 CharUnits NumBytes) const override {
37 unsigned Size = NumBytes.getQuantity();
38 return llvm::TargetExtType::get(CGM.getLLVMContext(), "dx.Padding", {},
39 {Size});
40 }
41
42 bool isHLSLPadding(llvm::Type *Ty) const override {
43 if (auto *TET = dyn_cast<llvm::TargetExtType>(Ty))
44 return TET->getName() == "dx.Padding";
45 return false;
46 }
47};
48
49llvm::Type *DirectXTargetCodeGenInfo::getHLSLType(
50 CodeGenModule &CGM, const Type *Ty,
51 const CGHLSLOffsetInfo &OffsetInfo) const {
52 auto *ResType = dyn_cast<HLSLAttributedResourceType>(Ty);
53 if (!ResType)
54 return nullptr;
55
56 llvm::LLVMContext &Ctx = CGM.getLLVMContext();
57 const HLSLAttributedResourceType::Attributes &ResAttrs = ResType->getAttrs();
58 switch (ResAttrs.ResourceClass) {
59 case llvm::dxil::ResourceClass::UAV:
60 case llvm::dxil::ResourceClass::SRV: {
61 // TypedBuffer, RawBuffer and Texture all need element type
62 QualType ContainedTy = ResType->getContainedType();
63 if (ContainedTy.isNull())
64 return nullptr;
65
66 // convert element type
67 llvm::Type *ElemType = CGM.getTypes().ConvertTypeForMem(ContainedTy);
68
69 bool IsRawBuffer = ResAttrs.RawBuffer;
70 bool IsTexture =
71 ResAttrs.ResourceDimension != llvm::dxil::ResourceDimension::Unknown;
72 assert((!IsRawBuffer || !IsTexture) && "A resource cannot be both a raw "
73 "buffer and a texture.");
74 llvm::StringRef TypeName = "dx.TypedBuffer";
75 if (IsRawBuffer)
76 TypeName = "dx.RawBuffer";
77 else if (IsTexture)
78 TypeName = "dx.Texture";
79
80 SmallVector<unsigned, 4> Ints = {/*IsWriteable*/ ResAttrs.ResourceClass ==
81 llvm::dxil::ResourceClass::UAV,
82 /*IsROV*/ ResAttrs.IsROV};
83 if (!IsRawBuffer) {
84 const clang::Type *ElemType = ContainedTy->getUnqualifiedDesugaredType();
85 if (ElemType->isVectorType())
86 ElemType = cast<clang::VectorType>(ElemType)
87 ->getElementType()
88 ->getUnqualifiedDesugaredType();
89 Ints.push_back(/*IsSigned*/ ElemType->isSignedIntegerType());
90 }
91
92 if (IsTexture) {
93 // Map ResourceDimension to dxil::ResourceKind
94 llvm::dxil::ResourceKind RK = llvm::dxil::ResourceKind::Invalid;
95 switch (ResAttrs.ResourceDimension) {
96 case llvm::dxil::ResourceDimension::Dim1D:
97 RK = llvm::dxil::ResourceKind::Texture1D;
98 break;
99 case llvm::dxil::ResourceDimension::Dim2D:
100 RK = llvm::dxil::ResourceKind::Texture2D;
101 break;
102 case llvm::dxil::ResourceDimension::Dim3D:
103 RK = llvm::dxil::ResourceKind::Texture3D;
104 break;
105 case llvm::dxil::ResourceDimension::Cube:
106 RK = llvm::dxil::ResourceKind::TextureCube;
107 break;
108 default:
109 llvm_unreachable("Unsupported resource dimension for textur.");
110 }
111 Ints.push_back(static_cast<unsigned>(RK));
112 }
113
114 return llvm::TargetExtType::get(Ctx, TypeName, {ElemType}, Ints);
115 }
116 case llvm::dxil::ResourceClass::CBuffer: {
117 QualType ContainedTy = ResType->getContainedType();
118 if (ContainedTy.isNull() || !ContainedTy->isStructureType())
119 return nullptr;
120
121 llvm::StructType *BufferLayoutTy =
122 HLSLBufferLayoutBuilder(CGM).layOutStruct(
123 ContainedTy->getAsCanonical<RecordType>(), OffsetInfo);
124 if (!BufferLayoutTy)
125 return nullptr;
126
127 return llvm::TargetExtType::get(Ctx, "dx.CBuffer", {BufferLayoutTy});
128 }
129 case llvm::dxil::ResourceClass::Sampler:
130 return llvm::TargetExtType::get(Ctx, "dx.Sampler", {}, {0});
131 }
132 llvm_unreachable("Unknown llvm::dxil::ResourceClass enum");
133}
134
135} // namespace
136
137std::unique_ptr<TargetCodeGenInfo>
139 return std::make_unique<DirectXTargetCodeGenInfo>(CGM.getTypes());
140}
C Language Family Type Representation.
QuantityType getQuantity() const
getQuantity - Get the raw integer representation of this quantity.
Definition CharUnits.h:185
This class organizes the cross-function state that is used while generating LLVM code.
llvm::LLVMContext & getLLVMContext()
llvm::Type * ConvertTypeForMem(QualType T)
ConvertTypeForMem - Convert type T into a llvm::Type.
TargetCodeGenInfo - This class organizes various target-specific codegeneration issues,...
Definition TargetInfo.h:49
bool isNull() const
Return true if this QualType doesn't point to a type yet.
Definition TypeBase.h:1004
The base class of the type hierarchy.
Definition TypeBase.h:1833
bool isStructureType() const
Definition Type.cpp:679
bool isSignedIntegerType() const
Return true if this is an integer type that is signed, according to C99 6.2.5p4 [char,...
Definition Type.cpp:2206
bool isVectorType() const
Definition TypeBase.h:8678
const T * getAsCanonical() const
If this type is canonically the specified type, return its canonical type cast to that specified type...
Definition TypeBase.h:2922
const Type * getUnqualifiedDesugaredType() const
Return the specified type with any "sugar" removed from the type, removing any typedefs,...
Definition Type.cpp:654
std::unique_ptr< TargetCodeGenInfo > createDirectXTargetCodeGenInfo(CodeGenModule &CGM)
Definition DirectX.cpp:138
The JSON file list parser is used to communicate input to InstallAPI.
const FunctionProtoType * T
@ Type
The name was classified as a type.
Definition Sema.h:564
U cast(CodeGen::Address addr)
Definition Address.h:327