LLVM 24.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
11#include "llvm/IR/Metadata.h"
12#include "llvm/IR/Module.h"
13
14using namespace llvm;
15using namespace llvm::hlsl;
16
19 llvm::function_ref<bool(Type *)> IsPadding) {
20 SmallVector<size_t> Offsets;
21
22 auto *HandleTy = cast<TargetExtType>(Handle->getValueType());
23 assert((HandleTy->getName().ends_with(".CBuffer") ||
24 HandleTy->getName() == "spirv.VulkanBuffer") &&
25 "Not a cbuffer type");
26 assert(HandleTy->getNumTypeParameters() == 1 && "Expected layout type");
27 auto *LayoutTy = cast<StructType>(HandleTy->getTypeParameter(0));
28
29 const StructLayout *SL = DL.getStructLayout(LayoutTy);
30 for (int I = 0, E = LayoutTy->getNumElements(); I < E; ++I)
31 if (!IsPadding(LayoutTy->getElementType(I)))
32 Offsets.push_back(SL->getElementOffset(I));
33
34 return Offsets;
35}
36
37std::optional<CBufferMetadata>
39 NamedMDNode *CBufMD = M.getNamedMetadata("hlsl.cbs");
40 if (!CBufMD)
41 return std::nullopt;
42
43 std::optional<CBufferMetadata> Result({CBufMD});
44
45 for (const MDNode *MD : CBufMD->operands()) {
46 assert(MD->getNumOperands() && "Invalid cbuffer metadata");
47
48 // For an unused cbuffer, the handle may have been optimized out
49 Metadata *OpMD = MD->getOperand(0);
50 if (!OpMD)
51 continue;
52
53 auto *Handle =
55 CBufferMapping &Mapping = Result->Mappings.emplace_back(Handle);
56
57 SmallVector<size_t> MemberOffsets =
58 getMemberOffsets(M.getDataLayout(), Handle, IsPadding);
59
60 for (int I = 1, E = MD->getNumOperands(); I < E; ++I) {
61 Metadata *OpMD = MD->getOperand(I);
62 // Some members may be null if they've been optimized out.
63 if (!OpMD)
64 continue;
66 Mapping.Members.emplace_back(V, MemberOffsets[I - 1]);
67 }
68 }
69
70 return Result;
71}
72
74 // Remove the cbs named metadata
75 MD->eraseFromParent();
76}
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:18
Module.h This file contains the declarations for the Module class.
static constexpr Value * getValue(Ty &ValueOrUse)
#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:64
Type * getValueType() const
Metadata node.
Definition Metadata.h:1069
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:1755
iterator_range< op_iterator > operands()
Definition Metadata.h:1851
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:743
TypeSize getElementOffset(unsigned Idx) const
Definition DataLayout.h:774
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
An efficient, type-erasing, non-owning reference to a callable.
static LLVM_ABI std::optional< CBufferMetadata > get(Module &M, llvm::function_ref< bool(Type *)> IsPadding)
Definition CBuffer.cpp:38
LLVM_ABI void eraseFromModule()
Definition CBuffer.cpp:73
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