Debian Codegraph @

llvm-project/llvm/projects/llvm-spirv/lib/SPIRV/libSPIRV/SPIRVDecorate.cpp

raw
1 //===- SPIRVDecorate.cpp -SPIR-V Decorations --------------------*- 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 implements SPIR-V decorations.
37 ///
38 //===----------------------------------------------------------------------===//
39
40 #include "SPIRVDecorate.h"
41 #include "SPIRVModule.h"
42 #include "SPIRVStream.h"
43 #include "SPIRVValue.h"
44
45 namespace SPIRV {
46 template <class T>
47 spv_ostream &operator<<(spv_ostream &O, const std::vector<T *> &V) {
48 for (auto &I : V)
49 O << *I;
50 return O;
51 }
52
53 SPIRVDecorateGeneric::SPIRVDecorateGeneric(Op OC, SPIRVWord WC,
54 Decoration TheDec,
55 SPIRVEntry *TheTarget)
56 : SPIRVAnnotationGeneric(TheTarget->getModule(), WC, OC,
57 TheTarget->getId()),
58 Dec(TheDec), Owner(nullptr) {
59 validate();
60 updateModuleVersion();
61 }
62
63 SPIRVDecorateGeneric::SPIRVDecorateGeneric(Op OC, SPIRVWord WC,
64 Decoration TheDec,
65 SPIRVEntry *TheTarget, SPIRVWord V)
66 : SPIRVAnnotationGeneric(TheTarget->getModule(), WC, OC,
67 TheTarget->getId()),
68 Dec(TheDec), Owner(nullptr) {
69 Literals.push_back(V);
70 validate();
71 updateModuleVersion();
72 }
73
74 SPIRVDecorateGeneric::SPIRVDecorateGeneric(Op OC, SPIRVWord WC,
75 Decoration TheDec,
76 SPIRVEntry *TheTarget, SPIRVWord V1,
77 SPIRVWord V2)
78 : SPIRVDecorateGeneric(OC, WC, TheDec, TheTarget, V1) {
79 Literals.push_back(V2);
80 validate();
81 updateModuleVersion();
82 }
83
84 SPIRVDecorateGeneric::SPIRVDecorateGeneric(Op OC)
85 : SPIRVAnnotationGeneric(OC), Dec(DecorationRelaxedPrecision),
86 Owner(nullptr) {}
87
88 Decoration SPIRVDecorateGeneric::getDecorateKind() const { return Dec; }
89
90 SPIRVWord SPIRVDecorateGeneric::getLiteral(size_t I) const {
91 assert(I <= Literals.size() && "Out of bounds");
92 return Literals[I];
93 }
94
95 std::vector<SPIRVWord> SPIRVDecorateGeneric::getVecLiteral() const {
96 return Literals;
97 }
98
99 size_t SPIRVDecorateGeneric::getLiteralCount() const { return Literals.size(); }
100
101 void SPIRVDecorate::encode(spv_ostream &O) const {
102 SPIRVEncoder Encoder = getEncoder(O);
103 Encoder << Target << Dec;
104 switch (static_cast<size_t>(Dec)) {
105 case DecorationLinkageAttributes:
106 SPIRVDecorateLinkageAttr::encodeLiterals(Encoder, Literals);
107 break;
108 case DecorationMemoryINTEL:
109 SPIRVDecorateMemoryINTELAttr::encodeLiterals(Encoder, Literals);
110 break;
111 case DecorationMergeINTEL:
112 SPIRVDecorateMergeINTELAttr::encodeLiterals(Encoder, Literals);
113 break;
114 case DecorationUserSemantic:
115 SPIRVDecorateUserSemanticAttr::encodeLiterals(Encoder, Literals);
116 break;
117 case internal::DecorationHostAccessINTEL:
118 SPIRVDecorateHostAccessINTELLegacy::encodeLiterals(Encoder, Literals);
119 break;
120 case DecorationHostAccessINTEL:
121 SPIRVDecorateHostAccessINTEL::encodeLiterals(Encoder, Literals);
122 break;
123 case DecorationInitModeINTEL:
124 SPIRVDecorateInitModeINTEL::encodeLiterals(Encoder, Literals);
125 break;
126 default:
127 Encoder << Literals;
128 }
129 }
130
131 void SPIRVDecorate::setWordCount(SPIRVWord Count) {
132 WordCount = Count;
133 Literals.resize(WordCount - FixedWC);
134 }
135
136 void SPIRVDecorate::decode(std::istream &I) {
137 SPIRVDecoder Decoder = getDecoder(I);
138 Decoder >> Target >> Dec;
139 switch (static_cast<size_t>(Dec)) {
140 case DecorationLinkageAttributes:
141 SPIRVDecorateLinkageAttr::decodeLiterals(Decoder, Literals);
142 break;
143 case DecorationMemoryINTEL:
144 SPIRVDecorateMemoryINTELAttr::decodeLiterals(Decoder, Literals);
145 break;
146 case DecorationMergeINTEL:
147 SPIRVDecorateMergeINTELAttr::decodeLiterals(Decoder, Literals);
148 break;
149 case DecorationUserSemantic:
150 SPIRVDecorateUserSemanticAttr::decodeLiterals(Decoder, Literals);
151 break;
152 case internal::DecorationHostAccessINTEL:
153 SPIRVDecorateHostAccessINTELLegacy::decodeLiterals(Decoder, Literals);
154 break;
155 case DecorationHostAccessINTEL:
156 SPIRVDecorateHostAccessINTEL::decodeLiterals(Decoder, Literals);
157 break;
158 default:
159 Decoder >> Literals;
160 }
161 getOrCreateTarget()->addDecorate(this);
162 }
163
164 void SPIRVDecorateId::encode(spv_ostream &O) const {
165 SPIRVEncoder Encoder = getEncoder(O);
166 Encoder << Target << Dec << Literals;
167 }
168
169 void SPIRVDecorateId::setWordCount(SPIRVWord Count) {
170 WordCount = Count;
171 Literals.resize(WordCount - FixedWC);
172 }
173
174 void SPIRVDecorateId::decode(std::istream &I) {
175 SPIRVDecoder Decoder = getDecoder(I);
176 Decoder >> Target >> Dec >> Literals;
177 getOrCreateTarget()->addDecorate(this);
178 }
179
180 void SPIRVMemberDecorate::encode(spv_ostream &O) const {
181 SPIRVEncoder Encoder = getEncoder(O);
182 Encoder << Target << MemberNumber << Dec;
183 switch (Dec) {
184 case DecorationMemoryINTEL:
185 SPIRVDecorateMemoryINTELAttr::encodeLiterals(Encoder, Literals);
186 break;
187 case DecorationMergeINTEL:
188 SPIRVDecorateMergeINTELAttr::encodeLiterals(Encoder, Literals);
189 break;
190 case DecorationUserSemantic:
191 SPIRVDecorateUserSemanticAttr::encodeLiterals(Encoder, Literals);
192 break;
193 default:
194 Encoder << Literals;
195 }
196 }
197
198 void SPIRVMemberDecorate::setWordCount(SPIRVWord Count) {
199 WordCount = Count;
200 Literals.resize(WordCount - FixedWC);
201 }
202
203 void SPIRVMemberDecorate::decode(std::istream &I) {
204 SPIRVDecoder Decoder = getDecoder(I);
205 Decoder >> Target >> MemberNumber >> Dec;
206 switch (Dec) {
207 case DecorationMemoryINTEL:
208 SPIRVDecorateMemoryINTELAttr::decodeLiterals(Decoder, Literals);
209 break;
210 case DecorationMergeINTEL:
211 SPIRVDecorateMergeINTELAttr::decodeLiterals(Decoder, Literals);
212 break;
213 case DecorationUserSemantic:
214 SPIRVDecorateUserSemanticAttr::decodeLiterals(Decoder, Literals);
215 break;
216 default:
217 Decoder >> Literals;
218 }
219 getOrCreateTarget()->addMemberDecorate(this);
220 }
221
222 void SPIRVDecorationGroup::encode(spv_ostream &O) const { getEncoder(O) << Id; }
223
224 void SPIRVDecorationGroup::decode(std::istream &I) {
225 getDecoder(I) >> Id;
226 Module->addDecorationGroup(this);
227 }
228
229 void SPIRVDecorationGroup::encodeAll(spv_ostream &O) const {
230 O << Decorations;
231 SPIRVEntry::encodeAll(O);
232 }
233
234 void SPIRVGroupDecorateGeneric::encode(spv_ostream &O) const {
235 getEncoder(O) << DecorationGroup << Targets;
236 }
237
238 void SPIRVGroupDecorateGeneric::decode(std::istream &I) {
239 getDecoder(I) >> DecorationGroup >> Targets;
240 Module->addGroupDecorateGeneric(this);
241 }
242
243 void SPIRVGroupDecorate::decorateTargets() {
244 for (auto &I : Targets) {
245 auto Target = getOrCreate(I);
246 for (auto &Dec : DecorationGroup->getDecorations()) {
247 assert(Dec->isDecorate());
248 Target->addDecorate(static_cast<SPIRVDecorate *>(Dec));
249 }
250 }
251 }
252
253 void SPIRVGroupMemberDecorate::decorateTargets() {
254 for (auto &I : Targets) {
255 auto Target = getOrCreate(I);
256 for (auto &Dec : DecorationGroup->getDecorations()) {
257 assert(Dec->isMemberDecorate());
258 Target->addMemberDecorate(static_cast<SPIRVMemberDecorate *>(Dec));
259 }
260 }
261 }
262 } // namespace SPIRV