Debian Codegraph @

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

raw
1 //===- SPIRVFunction.cpp - Class to represent a SPIR-V Function --- 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 //===----------------------------------------------------------------------===//
9 //
10 // This file implements Function class for SPIRV.
11 //
12 // Copyright (c) 2014 Advanced Micro Devices, Inc. All rights reserved.
13 //
14 // Permission is hereby granted, free of charge, to any person obtaining a
15 // copy of this software and associated documentation files (the "Software"),
16 // to deal with the Software without restriction, including without limitation
17 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
18 // and/or sell copies of the Software, and to permit persons to whom the
19 // Software is furnished to do so, subject to the following conditions:
20 //
21 // Redistributions of source code must retain the above copyright notice,
22 // this list of conditions and the following disclaimers.
23 // Redistributions in binary form must reproduce the above copyright notice,
24 // this list of conditions and the following disclaimers in the documentation
25 // and/or other materials provided with the distribution.
26 // Neither the names of Advanced Micro Devices, Inc., nor the names of its
27 // contributors may be used to endorse or promote products derived from this
28 // Software without specific prior written permission.
29 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
31 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
32 // CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
33 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
34 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH
35 // THE SOFTWARE.
36 //
37 //===----------------------------------------------------------------------===//
38
39 #include "SPIRVFunction.h"
40 #include "SPIRVBasicBlock.h"
41 #include "SPIRVEntry.h"
42 #include "SPIRVInstruction.h"
43 #include "SPIRVStream.h"
44
45 #include <algorithm>
46 #include <functional>
47 using namespace SPIRV;
48
49 SPIRVFunctionParameter::SPIRVFunctionParameter(SPIRVType *TheType,
50 SPIRVId TheId,
51 SPIRVFunction *TheParent,
52 unsigned TheArgNo)
53 : SPIRVValue(TheParent->getModule(), 3, OpFunctionParameter, TheType,
54 TheId),
55 ParentFunc(TheParent), ArgNo(TheArgNo) {
56 validate();
57 }
58
59 void SPIRVFunctionParameter::foreachAttr(
60 std::function<void(SPIRVFuncParamAttrKind)> Func) {
61 auto Locs = Decorates.equal_range(DecorationFuncParamAttr);
62 for (auto I = Locs.first, E = Locs.second; I != E; ++I) {
63 auto Attr = static_cast<SPIRVFuncParamAttrKind>(I->second->getLiteral(0));
64 assert(isValid(Attr));
65 Func(Attr);
66 }
67 }
68
69 SPIRVDecoder SPIRVFunction::getDecoder(std::istream &IS) {
70 return SPIRVDecoder(IS, *this);
71 }
72
73 void SPIRVFunction::encode(spv_ostream &O) const {
74 getEncoder(O) << Type << Id << FCtrlMask << FuncType;
75 }
76
77 void SPIRVFunction::encodeChildren(spv_ostream &O) const {
78 O << SPIRVNL();
79 for (auto &I : Parameters)
80 O << *I;
81 O << SPIRVNL();
82 for (auto &I : BBVec)
83 O << *I;
84 O << SPIRVFunctionEnd();
85 }
86
87 void SPIRVFunction::encodeExecutionModes(spv_ostream &O) const {
88 for (auto &I : ExecModes)
89 O << *I.second;
90 }
91
92 void SPIRVFunction::decode(std::istream &I) {
93 SPIRVDecoder Decoder = getDecoder(I);
94 Decoder >> Type >> Id >> FCtrlMask >> FuncType;
95 Module->addFunction(this);
96 SPIRVDBG(spvdbgs() << "Decode function: " << Id << '\n');
97
98 Decoder.getWordCountAndOpCode();
99 while (!I.eof()) {
100 if (Decoder.OpCode == OpFunctionEnd)
101 break;
102
103 switch (Decoder.OpCode) {
104 case OpFunctionParameter: {
105 auto Param = static_cast<SPIRVFunctionParameter *>(Decoder.getEntry());
106 assert(Param);
107 Module->add(Param);
108 Param->setParent(this);
109 Parameters.push_back(Param);
110 Decoder.getWordCountAndOpCode();
111 break;
112 }
113 case OpLabel: {
114 if (!decodeBB(Decoder))
115 return;
116 break;
117 }
118 default:
119 assert(0 && "Invalid SPIRV format");
120 }
121 }
122 }
123
124 /// Decode basic block and contained instructions.
125 /// Do it here instead of in BB:decode to avoid back track in input stream.
126 bool SPIRVFunction::decodeBB(SPIRVDecoder &Decoder) {
127 SPIRVBasicBlock *BB = static_cast<SPIRVBasicBlock *>(Decoder.getEntry());
128 assert(BB);
129 addBasicBlock(BB);
130 SPIRVDBG(spvdbgs() << "Decode BB: " << BB->getId() << '\n');
131
132 Decoder.setScope(BB);
133 SPIRVEntry *DebugScope = nullptr;
134 while (Decoder.getWordCountAndOpCode()) {
135 if (Decoder.OpCode == OpFunctionEnd || Decoder.OpCode == OpLabel) {
136 break;
137 }
138
139 if (Decoder.OpCode == OpNoLine || Decoder.OpCode == OpNop) {
140 continue;
141 }
142
143 SPIRVEntry *Entry = Decoder.getEntry();
144
145 if (Decoder.OpCode == OpLine) {
146 std::shared_ptr<const SPIRVLine> L(static_cast<SPIRVLine *>(Entry));
147 Module->setCurrentLine(L);
148 continue;
149 }
150
151 if (!Module->getErrorLog().checkError(Entry->isImplemented(),
152 SPIRVEC_UnimplementedOpCode,
153 std::to_string(Entry->getOpCode()))) {
154 // Bail out if the opcode is not implemented.
155 Module->setInvalid();
156 return false;
157 }
158
159 auto *Inst = static_cast<SPIRVInstruction *>(Entry);
160 assert(Inst);
161 if (Inst->getOpCode() == OpUndef) {
162 Module->add(Inst);
163 } else if (Inst->isExtInst(SPIRVEIS_NonSemantic_Shader_DebugInfo_100,
164 SPIRVDebug::DebugNoLine) ||
165 Inst->isExtInst(SPIRVEIS_NonSemantic_Shader_DebugInfo_200,
166 SPIRVDebug::DebugNoLine)) {
167 continue;
168 } else if (Inst->isExtInst(SPIRVEIS_NonSemantic_Shader_DebugInfo_100,
169 SPIRVDebug::DebugLine) ||
170 Inst->isExtInst(SPIRVEIS_NonSemantic_Shader_DebugInfo_200,
171 SPIRVDebug::DebugLine)) {
172 std::shared_ptr<const SPIRVExtInst> DL(static_cast<SPIRVExtInst *>(Inst));
173 Module->setCurrentDebugLine(DL);
174 } else {
175 if (Inst->isExtInst(SPIRVEIS_Debug, SPIRVDebug::Scope) ||
176 Inst->isExtInst(SPIRVEIS_OpenCL_DebugInfo_100, SPIRVDebug::Scope) ||
177 Inst->isExtInst(SPIRVEIS_NonSemantic_Shader_DebugInfo_100,
178 SPIRVDebug::Scope) ||
179 Inst->isExtInst(SPIRVEIS_NonSemantic_Shader_DebugInfo_200,
180 SPIRVDebug::Scope)) {
181 DebugScope = Inst;
182 } else if (Inst->isExtInst(SPIRVEIS_Debug, SPIRVDebug::NoScope) ||
183 Inst->isExtInst(SPIRVEIS_OpenCL_DebugInfo_100,
184 SPIRVDebug::NoScope) ||
185 Inst->isExtInst(SPIRVEIS_NonSemantic_Shader_DebugInfo_100,
186 SPIRVDebug::NoScope) ||
187 Inst->isExtInst(SPIRVEIS_NonSemantic_Shader_DebugInfo_200,
188 SPIRVDebug::NoScope)) {
189 DebugScope = nullptr;
190 } else {
191 Inst->setDebugScope(DebugScope);
192 }
193 BB->addInstruction(Inst);
194 }
195 }
196 Decoder.setScope(this);
197 return true;
198 }
199
200 void SPIRVFunction::foreachReturnValueAttr(
201 std::function<void(SPIRVFuncParamAttrKind)> Func) {
202 auto Locs = Decorates.equal_range(DecorationFuncParamAttr);
203 for (auto I = Locs.first, E = Locs.second; I != E; ++I) {
204 auto Attr = static_cast<SPIRVFuncParamAttrKind>(I->second->getLiteral(0));
205 assert(isValid(Attr));
206 Func(Attr);
207 }
208 }