clang 24.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 bool IsMultiSampledTexture = IsTexture && ResAttrs.isMultiSampled();
75 llvm::StringRef TypeName = "dx.TypedBuffer";
76 if (IsRawBuffer)
77 TypeName = "dx.RawBuffer";
78 else if (IsMultiSampledTexture)
79 TypeName = "dx.MSTexture";
80 else if (IsTexture)
81 TypeName = "dx.Texture";
82
83 // The second int operand is overloaded: dx.Texture holds IsROV there,
84 // dx.MSTexture holds the sample count. A sample count of 0 means the count
85 // comes from the bound resource at runtime, which is also what a
86 // Texture2DMS<T> written without an explicit N lowers to.
87 unsigned SampleCount = 0;
88 if (const Expr *SCE = ResAttrs.SampleCountExpr) {
89 std::optional<llvm::APSInt> Count =
90 SCE->getIntegerConstantExpr(CGM.getContext());
91 if (Count && Count->isNonNegative())
92 SampleCount = Count->getZExtValue();
93 }
94 SmallVector<unsigned, 4> Ints = {
95 /*IsWriteable*/ ResAttrs.ResourceClass ==
96 llvm::dxil::ResourceClass::UAV,
97 IsMultiSampledTexture ? SampleCount : /*IsROV*/ ResAttrs.IsROV};
98 if (!IsRawBuffer) {
99 const clang::Type *ElemType = ContainedTy->getUnqualifiedDesugaredType();
100 if (ElemType->isVectorType())
101 ElemType = cast<clang::VectorType>(ElemType)
102 ->getElementType()
103 ->getUnqualifiedDesugaredType();
104 Ints.push_back(/*IsSigned*/ ElemType->isSignedIntegerType());
105 }
106
107 if (IsTexture) {
108 // Map ResourceDimension to dxil::ResourceKind
109 llvm::dxil::ResourceKind RK = llvm::dxil::ResourceKind::Invalid;
110 switch (ResAttrs.ResourceDimension) {
111 case llvm::dxil::ResourceDimension::Dim1D:
112 RK = llvm::dxil::ResourceKind::Texture1D;
113 break;
114 case llvm::dxil::ResourceDimension::Dim2D:
115 if (ResAttrs.isMultiSampled())
116 RK = ResAttrs.IsArray ? llvm::dxil::ResourceKind::Texture2DMSArray
117 : llvm::dxil::ResourceKind::Texture2DMS;
118 else
119 RK = ResAttrs.IsArray ? llvm::dxil::ResourceKind::Texture2DArray
120 : llvm::dxil::ResourceKind::Texture2D;
121 break;
122 case llvm::dxil::ResourceDimension::Dim3D:
123 RK = llvm::dxil::ResourceKind::Texture3D;
124 break;
125 case llvm::dxil::ResourceDimension::Cube:
126 RK = llvm::dxil::ResourceKind::TextureCube;
127 break;
128 default:
129 llvm_unreachable("Unsupported resource dimension for texture.");
130 }
131 Ints.push_back(static_cast<unsigned>(RK));
132 }
133
134 return llvm::TargetExtType::get(Ctx, TypeName, {ElemType}, Ints);
135 }
136 case llvm::dxil::ResourceClass::CBuffer: {
137 QualType ContainedTy = ResType->getContainedType();
138 if (ContainedTy.isNull() || !ContainedTy->isStructureType())
139 return nullptr;
140
141 llvm::StructType *BufferLayoutTy =
142 HLSLBufferLayoutBuilder(CGM).layOutStruct(
143 ContainedTy->getAsCanonical<RecordType>(), OffsetInfo);
144 if (!BufferLayoutTy)
145 return nullptr;
146
147 return llvm::TargetExtType::get(Ctx, "dx.CBuffer", {BufferLayoutTy});
148 }
149 case llvm::dxil::ResourceClass::Sampler:
150 return llvm::TargetExtType::get(Ctx, "dx.Sampler", {}, {0});
151 }
152 llvm_unreachable("Unknown llvm::dxil::ResourceClass enum");
153}
154
155} // namespace
156
157std::unique_ptr<TargetCodeGenInfo>
159 return std::make_unique<DirectXTargetCodeGenInfo>(CGM.getTypes());
160}
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.
ASTContext & getContext() const
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:80
bool isNull() const
Return true if this QualType doesn't point to a type yet.
Definition TypeBase.h:1005
The base class of the type hierarchy.
Definition TypeBase.h:1879
bool isStructureType() const
Definition Type.cpp:715
bool isSignedIntegerType() const
Return true if this is an integer type that is signed, according to C99 6.2.5p4 [char,...
Definition Type.cpp:2296
bool isVectorType() const
Definition TypeBase.h:8878
const T * getAsCanonical() const
If this type is canonically the specified type, return its canonical type cast to that specified type...
Definition TypeBase.h:2998
const Type * getUnqualifiedDesugaredType() const
Return the specified type with any "sugar" removed from the type, removing any typedefs,...
Definition Type.cpp:690
std::unique_ptr< TargetCodeGenInfo > createDirectXTargetCodeGenInfo(CodeGenModule &CGM)
Definition DirectX.cpp:158
Top level wrappers for InstallAPI frontend operations.
const FunctionProtoType * T
@ Type
The name was classified as a type.
Definition Sema.h:559
U cast(CodeGen::Address addr)
Definition Address.h:327