1*700637cbSDimitry Andric //===- DirectX.cpp---------------------------------------------------------===//
2*700637cbSDimitry Andric //
3*700637cbSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*700637cbSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*700637cbSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*700637cbSDimitry Andric //
7*700637cbSDimitry Andric //===----------------------------------------------------------------------===//
8*700637cbSDimitry Andric
9*700637cbSDimitry Andric #include "ABIInfoImpl.h"
10*700637cbSDimitry Andric #include "CodeGenModule.h"
11*700637cbSDimitry Andric #include "HLSLBufferLayoutBuilder.h"
12*700637cbSDimitry Andric #include "TargetInfo.h"
13*700637cbSDimitry Andric #include "clang/AST/Type.h"
14*700637cbSDimitry Andric #include "llvm/ADT/SmallVector.h"
15*700637cbSDimitry Andric #include "llvm/IR/DerivedTypes.h"
16*700637cbSDimitry Andric #include "llvm/IR/Type.h"
17*700637cbSDimitry Andric
18*700637cbSDimitry Andric using namespace clang;
19*700637cbSDimitry Andric using namespace clang::CodeGen;
20*700637cbSDimitry Andric
21*700637cbSDimitry Andric //===----------------------------------------------------------------------===//
22*700637cbSDimitry Andric // Target codegen info implementation for DirectX.
23*700637cbSDimitry Andric //===----------------------------------------------------------------------===//
24*700637cbSDimitry Andric
25*700637cbSDimitry Andric namespace {
26*700637cbSDimitry Andric
27*700637cbSDimitry Andric class DirectXTargetCodeGenInfo : public TargetCodeGenInfo {
28*700637cbSDimitry Andric public:
DirectXTargetCodeGenInfo(CodeGen::CodeGenTypes & CGT)29*700637cbSDimitry Andric DirectXTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
30*700637cbSDimitry Andric : TargetCodeGenInfo(std::make_unique<DefaultABIInfo>(CGT)) {}
31*700637cbSDimitry Andric
32*700637cbSDimitry Andric llvm::Type *
33*700637cbSDimitry Andric getHLSLType(CodeGenModule &CGM, const Type *T,
34*700637cbSDimitry Andric const SmallVector<int32_t> *Packoffsets = nullptr) const override;
35*700637cbSDimitry Andric };
36*700637cbSDimitry Andric
getHLSLType(CodeGenModule & CGM,const Type * Ty,const SmallVector<int32_t> * Packoffsets) const37*700637cbSDimitry Andric llvm::Type *DirectXTargetCodeGenInfo::getHLSLType(
38*700637cbSDimitry Andric CodeGenModule &CGM, const Type *Ty,
39*700637cbSDimitry Andric const SmallVector<int32_t> *Packoffsets) const {
40*700637cbSDimitry Andric auto *ResType = dyn_cast<HLSLAttributedResourceType>(Ty);
41*700637cbSDimitry Andric if (!ResType)
42*700637cbSDimitry Andric return nullptr;
43*700637cbSDimitry Andric
44*700637cbSDimitry Andric llvm::LLVMContext &Ctx = CGM.getLLVMContext();
45*700637cbSDimitry Andric const HLSLAttributedResourceType::Attributes &ResAttrs = ResType->getAttrs();
46*700637cbSDimitry Andric switch (ResAttrs.ResourceClass) {
47*700637cbSDimitry Andric case llvm::dxil::ResourceClass::UAV:
48*700637cbSDimitry Andric case llvm::dxil::ResourceClass::SRV: {
49*700637cbSDimitry Andric // TypedBuffer and RawBuffer both need element type
50*700637cbSDimitry Andric QualType ContainedTy = ResType->getContainedType();
51*700637cbSDimitry Andric if (ContainedTy.isNull())
52*700637cbSDimitry Andric return nullptr;
53*700637cbSDimitry Andric
54*700637cbSDimitry Andric // convert element type
55*700637cbSDimitry Andric llvm::Type *ElemType = CGM.getTypes().ConvertTypeForMem(ContainedTy);
56*700637cbSDimitry Andric
57*700637cbSDimitry Andric llvm::StringRef TypeName =
58*700637cbSDimitry Andric ResAttrs.RawBuffer ? "dx.RawBuffer" : "dx.TypedBuffer";
59*700637cbSDimitry Andric SmallVector<unsigned, 3> Ints = {/*IsWriteable*/ ResAttrs.ResourceClass ==
60*700637cbSDimitry Andric llvm::dxil::ResourceClass::UAV,
61*700637cbSDimitry Andric /*IsROV*/ ResAttrs.IsROV};
62*700637cbSDimitry Andric if (!ResAttrs.RawBuffer) {
63*700637cbSDimitry Andric const clang::Type *ElemType = ContainedTy->getUnqualifiedDesugaredType();
64*700637cbSDimitry Andric if (ElemType->isVectorType())
65*700637cbSDimitry Andric ElemType = cast<clang::VectorType>(ElemType)
66*700637cbSDimitry Andric ->getElementType()
67*700637cbSDimitry Andric ->getUnqualifiedDesugaredType();
68*700637cbSDimitry Andric Ints.push_back(/*IsSigned*/ ElemType->isSignedIntegerType());
69*700637cbSDimitry Andric }
70*700637cbSDimitry Andric
71*700637cbSDimitry Andric return llvm::TargetExtType::get(Ctx, TypeName, {ElemType}, Ints);
72*700637cbSDimitry Andric }
73*700637cbSDimitry Andric case llvm::dxil::ResourceClass::CBuffer: {
74*700637cbSDimitry Andric QualType ContainedTy = ResType->getContainedType();
75*700637cbSDimitry Andric if (ContainedTy.isNull() || !ContainedTy->isStructureType())
76*700637cbSDimitry Andric return nullptr;
77*700637cbSDimitry Andric
78*700637cbSDimitry Andric llvm::Type *BufferLayoutTy =
79*700637cbSDimitry Andric HLSLBufferLayoutBuilder(CGM, "dx.Layout")
80*700637cbSDimitry Andric .createLayoutType(ContainedTy->getAsStructureType(), Packoffsets);
81*700637cbSDimitry Andric if (!BufferLayoutTy)
82*700637cbSDimitry Andric return nullptr;
83*700637cbSDimitry Andric
84*700637cbSDimitry Andric return llvm::TargetExtType::get(Ctx, "dx.CBuffer", {BufferLayoutTy});
85*700637cbSDimitry Andric }
86*700637cbSDimitry Andric case llvm::dxil::ResourceClass::Sampler:
87*700637cbSDimitry Andric llvm_unreachable("dx.Sampler handles are not implemented yet");
88*700637cbSDimitry Andric break;
89*700637cbSDimitry Andric }
90*700637cbSDimitry Andric llvm_unreachable("Unknown llvm::dxil::ResourceClass enum");
91*700637cbSDimitry Andric }
92*700637cbSDimitry Andric
93*700637cbSDimitry Andric } // namespace
94*700637cbSDimitry Andric
95*700637cbSDimitry Andric std::unique_ptr<TargetCodeGenInfo>
createDirectXTargetCodeGenInfo(CodeGenModule & CGM)96*700637cbSDimitry Andric CodeGen::createDirectXTargetCodeGenInfo(CodeGenModule &CGM) {
97*700637cbSDimitry Andric return std::make_unique<DirectXTargetCodeGenInfo>(CGM.getTypes());
98*700637cbSDimitry Andric }
99