1 //===- CBuffer.h - HLSL constant buffer handling ----------------*- C++ -*-===// 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 /// 9 /// \file This file contains utilities to work with constant buffers in HLSL. 10 /// 11 //===----------------------------------------------------------------------===// 12 13 #ifndef LLVM_FRONTEND_HLSL_CBUFFER_H 14 #define LLVM_FRONTEND_HLSL_CBUFFER_H 15 16 #include "llvm/ADT/SmallVector.h" 17 #include "llvm/IR/DataLayout.h" 18 #include "llvm/IR/DerivedTypes.h" 19 #include <optional> 20 21 namespace llvm { 22 class Module; 23 class GlobalVariable; 24 class NamedMDNode; 25 26 namespace hlsl { 27 28 struct CBufferMember { 29 GlobalVariable *GV; 30 size_t Offset; 31 CBufferMemberCBufferMember32 CBufferMember(GlobalVariable *GV, size_t Offset) : GV(GV), Offset(Offset) {} 33 }; 34 35 struct CBufferMapping { 36 GlobalVariable *Handle; 37 SmallVector<CBufferMember> Members; 38 CBufferMappingCBufferMapping39 CBufferMapping(GlobalVariable *Handle) : Handle(Handle) {} 40 }; 41 42 class CBufferMetadata { 43 NamedMDNode *MD; 44 SmallVector<CBufferMapping> Mappings; 45 CBufferMetadata(NamedMDNode * MD)46 CBufferMetadata(NamedMDNode *MD) : MD(MD) {} 47 48 public: 49 static std::optional<CBufferMetadata> get(Module &M); 50 51 using iterator = SmallVector<CBufferMapping>::iterator; begin()52 iterator begin() { return Mappings.begin(); } end()53 iterator end() { return Mappings.end(); } 54 55 void eraseFromModule(); 56 }; 57 58 APInt translateCBufArrayOffset(const DataLayout &DL, APInt Offset, 59 ArrayType *Ty); 60 61 } // namespace hlsl 62 } // namespace llvm 63 64 #endif // LLVM_FRONTEND_HLSL_CBUFFER_H 65