Debian Codegraph @

llvm-project/llvm/projects/llvm-spirv/lib/SPIRV/SPIRVMDWalker.h

raw
1 //===- SPIRVMDWalker.h - SPIR-V metadata walker header file ----*- C++ -*-===//
2 //
3 // The LLVM/SPIRV Translator
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 // Copyright (c) 2014 Advanced Micro Devices, Inc. All rights reserved.
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining a
11 // copy of this software and associated documentation files (the "Software"),
12 // to deal with the Software without restriction, including without limitation
13 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
14 // and/or sell copies of the Software, and to permit persons to whom the
15 // Software is furnished to do so, subject to the following conditions:
16 //
17 // Redistributions of source code must retain the above copyright notice,
18 // this list of conditions and the following disclaimers.
19 // Redistributions in binary form must reproduce the above copyright notice,
20 // this list of conditions and the following disclaimers in the documentation
21 // and/or other materials provided with the distribution.
22 // Neither the names of Advanced Micro Devices, Inc., nor the names of its
23 // contributors may be used to endorse or promote products derived from this
24 // Software without specific prior written permission.
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28 // CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH
31 // THE SOFTWARE.
32 //
33 //===----------------------------------------------------------------------===//
34 /// \file
35 ///
36 /// This file declares classes for walking SPIR-V metadata.
37 ///
38 //===----------------------------------------------------------------------===//
39
40 #ifndef SPIRV_SPIRVMDWALKER_H
41 #define SPIRV_SPIRVMDWALKER_H
42
43 #include "SPIRVInternal.h"
44 #include "llvm/IR/Metadata.h"
45
46 #include <functional>
47 using namespace llvm;
48
49 namespace SPIRV {
50
51 class SPIRVMDWalker {
52 public:
53 template <typename ParentT> struct MDWrapper;
54
55 struct NamedMDWrapper {
56 NamedMDWrapper(NamedMDNode *Named, SPIRVMDWalker &WW)
57 : NMD(Named), W(WW), I(0), Q(true) {
58 E = Named ? Named->getNumOperands() : 0;
59 }
60
61 operator bool() const { return NMD; }
62
63 bool atEnd() const { return !(NMD && I < E); }
64
65 MDWrapper<NamedMDWrapper> nextOp() {
66 if (!Q)
67 assert(I < E && "out of bound");
68 return MDWrapper<NamedMDWrapper>(
69 (NMD && I < E) ? NMD->getOperand(I++) : nullptr, *this, W);
70 }
71
72 NamedMDWrapper &setQuiet(bool Quiet) {
73 Q = Quiet;
74 return *this;
75 }
76
77 NamedMDNode *NMD;
78 SPIRVMDWalker &W;
79 unsigned I;
80 unsigned E;
81 bool Q; // Quiet
82 };
83
84 template <typename ParentT> struct MDWrapper {
85 MDWrapper(MDNode *Node, ParentT &Parent, SPIRVMDWalker &Walker)
86 : M(Node), P(Parent), W(Walker), I(0), Q(false) {
87 E = Node ? Node->getNumOperands() : 0;
88 }
89
90 operator bool() const { return M; }
91
92 bool atEnd() const { return !(M && I < E); }
93
94 template <typename T> MDWrapper &get(T &V) {
95 if (!Q)
96 assert(I < E && "out of bound");
97 if (atEnd())
98 return *this;
99 V = mdconst::dyn_extract<ConstantInt>(M->getOperand(I++))->getZExtValue();
100 return *this;
101 }
102
103 MDWrapper &get(std::string &S) {
104 if (!Q)
105 assert(I < E && "out of bound");
106 if (atEnd())
107 return *this;
108 Metadata *Op = M->getOperand(I++);
109 if (!Op)
110 S = "";
111 else if (auto Str = dyn_cast<MDString>(Op))
112 S = Str->getString().str();
113 else
114 S = "";
115 return *this;
116 }
117
118 MDWrapper &get(Function *&F) {
119 if (!Q)
120 assert(I < E && "out of bound");
121 if (atEnd())
122 return *this;
123 F = mdconst::dyn_extract<Function>(M->getOperand(I++));
124 return *this;
125 }
126
127 MDWrapper &get(SmallVectorImpl<std::string> &SV) {
128 if (atEnd())
129 return *this;
130 while (I < E) {
131 std::string S;
132 get(S);
133 SV.push_back(S);
134 }
135 return *this;
136 }
137
138 MDWrapper<MDWrapper> nextOp() {
139 if (!Q)
140 assert(I < E && "out of bound");
141 return MDWrapper<MDWrapper>(
142 (M && I < E) ? dyn_cast<MDNode>(M->getOperand(I++)) : nullptr, *this,
143 W);
144 }
145
146 ParentT &done() { return P; }
147
148 MDWrapper &setQuiet(bool Quiet) {
149 Q = Quiet;
150 return *this;
151 }
152
153 MDNode *M;
154 ParentT &P;
155 SPIRVMDWalker &W;
156 SmallVector<Metadata *, 10> V;
157 unsigned I;
158 unsigned E;
159 bool Q; // Quiet
160 };
161
162 explicit SPIRVMDWalker(Module &Mod) : M(Mod), C(Mod.getContext()) {}
163
164 NamedMDWrapper getNamedMD(StringRef Name) {
165 return NamedMDWrapper(M.getNamedMetadata(Name), *this);
166 }
167
168 friend struct NamedMDWrapper;
169
170 private:
171 Module &M;
172 LLVMContext &C;
173 };
174
175 } /* namespace SPIRV */
176
177 #endif // SPIRV_SPIRVMDWALKER_H