1//===- HLSLBufferLayoutBuilder.h ------------------------------------------===//
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 "clang/AST/TypeBase.h"
10#include "llvm/ADT/StringRef.h"
11#include "llvm/IR/DerivedTypes.h"
12
13namespace clang {
14namespace CodeGen {
15class CGHLSLOffsetInfo;
16class CodeGenModule;
17class CGHLSLOffsetInfo;
18
19//===----------------------------------------------------------------------===//
20// Implementation of constant buffer layout common between DirectX and
21// SPIR/SPIR-V.
22//===----------------------------------------------------------------------===//
23
24class HLSLBufferLayoutBuilder {
25private:
26 CodeGenModule &CGM;
27
28 /// Pads an array of elements to 16-byte cbuffer row boundaries.
29 /// This implements the common pattern of padding all-but-the-last element.
30 llvm::Type *padArrayElements(llvm::Type *EltTy, uint64_t Count);
31
32public:
33 HLSLBufferLayoutBuilder(CodeGenModule &CGM) : CGM(CGM) {}
34
35 /// Lays out a struct type following HLSL buffer rules and considering any
36 /// explicit offset information. Previously created layout structs are cached
37 /// by CGHLSLRuntime.
38 ///
39 /// The function iterates over all fields of the record type (including base
40 /// classes) and works out a padded llvm type to represent the buffer layout.
41 ///
42 /// If a non-empty OffsetInfo is provided (ie, from `packoffset` annotations
43 /// in the source), any provided offsets offsets will be respected. If the
44 /// OffsetInfo is available but has empty entries, those will be layed out at
45 /// the end of the structure.
46 llvm::StructType *layOutStruct(const RecordType *StructType,
47 const CGHLSLOffsetInfo &OffsetInfo);
48
49 /// Lays out an array type following HLSL buffer rules.
50 llvm::Type *layOutArray(const ConstantArrayType *AT);
51
52 /// Lays out a matrix type following HLSL buffer rules.
53 llvm::Type *layOutMatrix(const ConstantMatrixType *MT);
54
55 /// Lays out a type following HLSL buffer rules. Arrays and structures will be
56 /// padded appropriately and nested objects will be converted as appropriate.
57 llvm::Type *layOutType(QualType Type);
58};
59
60} // namespace CodeGen
61} // namespace clang
62