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
20 llvm::function_ref<bool(Type *)> IsPadding) {
21 SmallVector<size_t> Offsets;
22
23 auto *HandleTy = cast<TargetExtType>(Handle->getValueType());
24 assert((HandleTy->getName().ends_with(".CBuffer") ||
25 HandleTy->getName() == "spirv.VulkanBuffer") &&
26 "Not a cbuffer type");
27 assert(HandleTy->getNumTypeParameters() == 1 && "Expected layout type");
28 auto *LayoutTy = cast<StructType>(HandleTy->getTypeParameter(0));
29
30 const StructLayout *SL = DL.getStructLayout(LayoutTy);
31 for (int I = 0, E = LayoutTy->getNumElements(); I < E; ++I)
32 if (!IsPadding(LayoutTy->getElementType(I)))
33 Offsets.push_back(SL->getElementOffset(I));
34
35 return Offsets;
36}
37
38std::optional<CBufferMetadata>
40 NamedMDNode *CBufMD = M.getNamedMetadata("hlsl.cbs");
41 if (!CBufMD)
42 return std::nullopt;
43
44 std::optional<CBufferMetadata> Result({CBufMD});
45
46 for (const MDNode *MD : CBufMD->operands()) {
47 assert(MD->getNumOperands() && "Invalid cbuffer metadata");
48
49 // For an unused cbuffer, the handle may have been optimized out
50 Metadata *OpMD = MD->getOperand(0);
51 if (!OpMD)
52 continue;
53
54 auto *Handle =
56 CBufferMapping &Mapping = Result->Mappings.emplace_back(Handle);
57
58 SmallVector<size_t> MemberOffsets =
59 getMemberOffsets(M.getDataLayout(), Handle, IsPadding);
60
61 for (int I = 1, E = MD->getNumOperands(); I < E; ++I) {
62 Metadata *OpMD = MD->getOperand(I);
63 // Some members may be null if they've been optimized out.
64 if (!OpMD)
65 continue;
66 auto *V = cast<GlobalVariable>(cast<ValueAsMetadata>(OpMD)->getValue());
67 Mapping.Members.emplace_back(V, MemberOffsets[I - 1]);
68 }
69 }
70
71 return Result;
72}
73
75 // Remove the cbs named metadata
76 MD->eraseFromParent();
77}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static SmallVector< size_t > getMemberOffsets(const DataLayout &DL, GlobalVariable *Handle, llvm::function_ref< bool(Type *)> IsPadding)
Definition CBuffer.cpp:19
Module.h This file contains the declarations for the Module class.
#define I(x, y, z)
Definition MD5.cpp:57
This file contains the declarations for metadata subclasses.
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:1078
Root of the metadata hierarchy.
Definition Metadata.h:64
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:1757
iterator_range< op_iterator > operands()
Definition Metadata.h:1853
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Used to lazily calculate structure layout information for a target machine, based on the DataLayout s...
Definition DataLayout.h:712
TypeSize getElementOffset(unsigned Idx) const
Definition DataLayout.h:743
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:45
An efficient, type-erasing, non-owning reference to a callable.
static std::optional< CBufferMetadata > get(Module &M, llvm::function_ref< bool(Type *)> IsPadding)
Definition CBuffer.cpp:39
This is an optimization pass for GlobalISel generic memory operations.
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
SmallVector< CBufferMember > Members
Definition CBuffer.h:37