Thanks to visit codestin.com
Credit goes to llvm.org

LLVM 22.0.0git
CBuffer.cpp
Go to the documentation of this file.
1//===- CBuffer.cpp - HLSL constant buffer handling ------------------------===//
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
12#include "llvm/IR/Metadata.h"
13#include "llvm/IR/Module.h"
14
15using namespace llvm;
16using namespace llvm::hlsl;
17
18static size_t getMemberOffset(GlobalVariable *Handle, size_t Index) {
19 auto *HandleTy = cast<TargetExtType>(Handle->getValueType());
20 assert((HandleTy->getName().ends_with(".CBuffer") ||
21 HandleTy->getName() == "spirv.VulkanBuffer") &&
22 "Not a cbuffer type");
23 assert(HandleTy->getNumTypeParameters() == 1 && "Expected layout type");
24
25 auto *LayoutTy = cast<TargetExtType>(HandleTy->getTypeParameter(0));
26 assert(LayoutTy->getName().ends_with(".Layout") && "Not a layout type");
27
28 // Skip the "size" parameter.
29 size_t ParamIndex = Index + 1;
30 assert(LayoutTy->getNumIntParameters() > ParamIndex &&
31 "Not enough parameters");
32
33 return LayoutTy->getIntParameter(ParamIndex);
34}
35
36std::optional<CBufferMetadata> CBufferMetadata::get(Module &M) {
37 NamedMDNode *CBufMD = M.getNamedMetadata("hlsl.cbs");
38 if (!CBufMD)
39 return std::nullopt;
40
41 std::optional<CBufferMetadata> Result({CBufMD});
42
43 for (const MDNode *MD : CBufMD->operands()) {
44 assert(MD->getNumOperands() && "Invalid cbuffer metadata");
45
46 auto *Handle = cast<GlobalVariable>(
47 cast<ValueAsMetadata>(MD->getOperand(0))->getValue());
48 CBufferMapping &Mapping = Result->Mappings.emplace_back(Handle);
49
50 for (int I = 1, E = MD->getNumOperands(); I < E; ++I) {
51 Metadata *OpMD = MD->getOperand(I);
52 // Some members may be null if they've been optimized out.
53 if (!OpMD)
54 continue;
55 auto *V = cast<GlobalVariable>(cast<ValueAsMetadata>(OpMD)->getValue());
56 Mapping.Members.emplace_back(V, getMemberOffset(Handle, I - 1));
57 }
58 }
59
60 return Result;
61}
62
64 // Remove the cbs named metadata
65 MD->eraseFromParent();
66}
67
69 ArrayType *Ty) {
70 int64_t TypeSize = DL.getTypeSizeInBits(Ty->getElementType()) / 8;
71 int64_t RoundUp = alignTo(TypeSize, Align(CBufferRowSizeInBytes));
72 return Offset.udiv(TypeSize) * RoundUp;
73}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static size_t getMemberOffset(GlobalVariable *Handle, size_t Index)
Definition CBuffer.cpp:18
Module.h This file contains the declarations for the Module class.
#define I(x, y, z)
Definition MD5.cpp:58
This file contains the declarations for metadata subclasses.
Class for arbitrary precision integers.
Definition APInt.h:78
Class to represent array types.
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:63
Type * getValueType() const
Metadata node.
Definition Metadata.h:1077
Root of the metadata hierarchy.
Definition Metadata.h:63
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
A tuple of MDNodes.
Definition Metadata.h:1749
iterator_range< op_iterator > operands()
Definition Metadata.h:1845
static std::optional< CBufferMetadata > get(Module &M)
Definition CBuffer.cpp:36
const unsigned CBufferRowSizeInBytes
APInt translateCBufArrayOffset(const DataLayout &DL, APInt Offset, ArrayType *Ty)
Definition CBuffer.cpp:68
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:477
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition Alignment.h:155
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:565
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
SmallVector< CBufferMember > Members
Definition CBuffer.h:37