clang 22.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 *
33 getHLSLType(CodeGenModule &CGM, const Type *T,
34 const SmallVector<int32_t> *Packoffsets = nullptr) const override;
35};
36
37llvm::Type *DirectXTargetCodeGenInfo::getHLSLType(
38 CodeGenModule &CGM, const Type *Ty,
39 const SmallVector<int32_t> *Packoffsets) const {
40 auto *ResType = dyn_cast<HLSLAttributedResourceType>(Ty);
41 if (!ResType)
42 return nullptr;
43
44 llvm::LLVMContext &Ctx = CGM.getLLVMContext();
45 const HLSLAttributedResourceType::Attributes &ResAttrs = ResType->getAttrs();
46 switch (ResAttrs.ResourceClass) {
47 case llvm::dxil::ResourceClass::UAV:
48 case llvm::dxil::ResourceClass::SRV: {
49 // TypedBuffer and RawBuffer both need element type
50 QualType ContainedTy = ResType->getContainedType();
51 if (ContainedTy.isNull())
52 return nullptr;
53
54 // convert element type
55 llvm::Type *ElemType = CGM.getTypes().ConvertTypeForMem(ContainedTy);
56
57 llvm::StringRef TypeName =
58 ResAttrs.RawBuffer ? "dx.RawBuffer" : "dx.TypedBuffer";
59 SmallVector<unsigned, 3> Ints = {/*IsWriteable*/ ResAttrs.ResourceClass ==
60 llvm::dxil::ResourceClass::UAV,
61 /*IsROV*/ ResAttrs.IsROV};
62 if (!ResAttrs.RawBuffer) {
63 const clang::Type *ElemType = ContainedTy->getUnqualifiedDesugaredType();
64 if (ElemType->isVectorType())
65 ElemType = cast<clang::VectorType>(ElemType)
66 ->getElementType()
67 ->getUnqualifiedDesugaredType();
68 Ints.push_back(/*IsSigned*/ ElemType->isSignedIntegerType());
69 }
70
71 return llvm::TargetExtType::get(Ctx, TypeName, {ElemType}, Ints);
72 }
73 case llvm::dxil::ResourceClass::CBuffer: {
74 QualType ContainedTy = ResType->getContainedType();
75 if (ContainedTy.isNull() || !ContainedTy->isStructureType())
76 return nullptr;
77
78 llvm::Type *BufferLayoutTy =
79 HLSLBufferLayoutBuilder(CGM, "dx.Layout")
80 .createLayoutType(ContainedTy->castAsCanonical<RecordType>(),
81 Packoffsets);
82 if (!BufferLayoutTy)
83 return nullptr;
84
85 return llvm::TargetExtType::get(Ctx, "dx.CBuffer", {BufferLayoutTy});
86 }
87 case llvm::dxil::ResourceClass::Sampler:
88 llvm_unreachable("dx.Sampler handles are not implemented yet");
89 break;
90 }
91 llvm_unreachable("Unknown llvm::dxil::ResourceClass enum");
92}
93
94} // namespace
95
96std::unique_ptr<TargetCodeGenInfo>
98 return std::make_unique<DirectXTargetCodeGenInfo>(CGM.getTypes());
99}
C Language Family Type Representation.
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:47
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:678
bool isSignedIntegerType() const
Return true if this is an integer type that is signed, according to C99 6.2.5p4 [char,...
Definition Type.cpp:2205
bool isVectorType() const
Definition TypeBase.h:8661
const T * castAsCanonical() const
Return this type's canonical type cast to the specified type.
Definition TypeBase.h:2928
const Type * getUnqualifiedDesugaredType() const
Return the specified type with any "sugar" removed from the type, removing any typedefs,...
Definition Type.cpp:653
std::unique_ptr< TargetCodeGenInfo > createDirectXTargetCodeGenInfo(CodeGenModule &CGM)
Definition DirectX.cpp:97
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:562
U cast(CodeGen::Address addr)
Definition Address.h:327