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") && "Not a cbuffer type");
21 assert(HandleTy->getNumTypeParameters() == 1 && "Expected layout type");
22
23 auto *LayoutTy = cast<TargetExtType>(HandleTy->getTypeParameter(0));
24 assert(LayoutTy->getName().ends_with(".Layout") && "Not a layout type");
25
26 // Skip the "size" parameter.
27 size_t ParamIndex = Index + 1;
28 assert(LayoutTy->getNumIntParameters() > ParamIndex &&
29 "Not enough parameters");
30
31 return LayoutTy->getIntParameter(ParamIndex);
32}
33
34std::optional<CBufferMetadata> CBufferMetadata::get(Module &M) {
35 NamedMDNode *CBufMD = M.getNamedMetadata("hlsl.cbs");
36 if (!CBufMD)
37 return std::nullopt;
38
39 std::optional<CBufferMetadata> Result({CBufMD});
40
41 for (const MDNode *MD : CBufMD->operands()) {
42 assert(MD->getNumOperands() && "Invalid cbuffer metadata");
43
44 auto *Handle = cast<GlobalVariable>(
45 cast<ValueAsMetadata>(MD->getOperand(0))->getValue());
46 CBufferMapping &Mapping = Result->Mappings.emplace_back(Handle);
47
48 for (int I = 1, E = MD->getNumOperands(); I < E; ++I) {
49 Metadata *OpMD = MD->getOperand(I);
50 // Some members may be null if they've been optimized out.
51 if (!OpMD)
52 continue;
53 auto *V = cast<GlobalVariable>(cast<ValueAsMetadata>(OpMD)->getValue());
54 Mapping.Members.emplace_back(V, getMemberOffset(Handle, I - 1));
55 }
56 }
57
58 return Result;
59}
60
62 // Remove the cbs named metadata
63 MD->eraseFromParent();
64}
65
67 ArrayType *Ty) {
68 int64_t TypeSize = DL.getTypeSizeInBits(Ty->getElementType()) / 8;
69 int64_t RoundUp = alignTo(TypeSize, Align(CBufferRowSizeInBytes));
70 return Offset.udiv(TypeSize) * RoundUp;
71}
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:34
const unsigned CBufferRowSizeInBytes
APInt translateCBufArrayOffset(const DataLayout &DL, APInt Offset, ArrayType *Ty)
Definition CBuffer.cpp:66
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