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"
11#include "HLSLBufferLayoutBuilder.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>(args&: 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(Context&: CGM.getLLVMContext(), Name: "dx.Padding", Types: {},
39 Ints: {Size});
40 }
41
42 bool isHLSLPadding(llvm::Type *Ty) const override {
43 if (auto *TET = dyn_cast<llvm::TargetExtType>(Val: 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>(Val: 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(T: 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>(Val: ElemType)
87 ->getElementType()
88 ->getUnqualifiedDesugaredType();
89 Ints.push_back(/*IsSigned*/ Elt: 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(Elt: static_cast<unsigned>(RK));
112 }
113
114 return llvm::TargetExtType::get(Context&: Ctx, Name: TypeName, Types: {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 StructType: ContainedTy->getAsCanonical<RecordType>(), OffsetInfo);
124 if (!BufferLayoutTy)
125 return nullptr;
126
127 return llvm::TargetExtType::get(Context&: Ctx, Name: "dx.CBuffer", Types: {BufferLayoutTy});
128 }
129 case llvm::dxil::ResourceClass::Sampler:
130 return llvm::TargetExtType::get(Context&: Ctx, Name: "dx.Sampler", Types: {}, Ints: {0});
131 }
132 llvm_unreachable("Unknown llvm::dxil::ResourceClass enum");
133}
134
135} // namespace
136
137std::unique_ptr<TargetCodeGenInfo>
138CodeGen::createDirectXTargetCodeGenInfo(CodeGenModule &CGM) {
139 return std::make_unique<DirectXTargetCodeGenInfo>(args&: CGM.getTypes());
140}
141