llvm-project/clang/lib/CodeGen/HLSLBufferLayoutBuilder.h
Justin Bogner c4898f3f22
[HLSL][DirectX] Use a padding type for HLSL buffers. (#167404)
This change drops the use of the "Layout" type and instead uses explicit
padding throughout the compiler to represent types in HLSL buffers.

There are a few parts to this, though it's difficult to split them up as
they're very interdependent:

1. Refactor HLSLBufferLayoutBuilder to allow us to calculate the padding
of arbitrary types.
2. Teach Clang CodeGen to use HLSL specific paths for cbuffers when
generating aggregate copies, array accesses, and structure accesses.
3. Simplify DXILCBufferAccesses such that it directly replaces accesses
with dx.resource.getpointer rather than recalculating the layout.
4. Basic infrastructure for SPIR-V handling, but the implementation
itself will need work in follow ups.

Fixes several issues, including #138996, #144573, and #156084.
Resolves #147352.
2025-11-18 13:38:43 -08:00

55 lines
2.0 KiB
C++

//===- HLSLBufferLayoutBuilder.h ------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "clang/AST/TypeBase.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/IR/DerivedTypes.h"
namespace clang {
namespace CodeGen {
class CGHLSLOffsetInfo;
class CodeGenModule;
class CGHLSLOffsetInfo;
//===----------------------------------------------------------------------===//
// Implementation of constant buffer layout common between DirectX and
// SPIR/SPIR-V.
//===----------------------------------------------------------------------===//
class HLSLBufferLayoutBuilder {
private:
CodeGenModule &CGM;
public:
HLSLBufferLayoutBuilder(CodeGenModule &CGM) : CGM(CGM) {}
/// Lays out a struct type following HLSL buffer rules and considering any
/// explicit offset information. Previously created layout structs are cached
/// by CGHLSLRuntime.
///
/// The function iterates over all fields of the record type (including base
/// classes) and works out a padded llvm type to represent the buffer layout.
///
/// If a non-empty OffsetInfo is provided (ie, from `packoffset` annotations
/// in the source), any provided offsets offsets will be respected. If the
/// OffsetInfo is available but has empty entries, those will be layed out at
/// the end of the structure.
llvm::StructType *layOutStruct(const RecordType *StructType,
const CGHLSLOffsetInfo &OffsetInfo);
/// Lays out an array type following HLSL buffer rules.
llvm::Type *layOutArray(const ConstantArrayType *AT);
/// Lays out a type following HLSL buffer rules. Arrays and structures will be
/// padded appropriately and nested objects will be converted as appropriate.
llvm::Type *layOutType(QualType Type);
};
} // namespace CodeGen
} // namespace clang