Debian Codegraph @

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

raw
1 //===- SPIRVFunction.h - 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 defines 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 #ifndef SPIRV_LIBSPIRV_SPIRVFUNCTION_H
40 #define SPIRV_LIBSPIRV_SPIRVFUNCTION_H
41 #include "SPIRVBasicBlock.h"
42 #include "SPIRVValue.h"
43 #include <functional>
44
45 namespace SPIRV {
46
47 class BIFunction;
48 class SPIRVDecoder;
49
50 class SPIRVFunctionParameter : public SPIRVValue {
51 public:
52 SPIRVFunctionParameter(SPIRVType *TheType, SPIRVId TheId,
53 SPIRVFunction *TheParent, unsigned TheArgNo);
54 SPIRVFunctionParameter()
55 : SPIRVValue(OpFunctionParameter), ParentFunc(nullptr), ArgNo(0) {}
56 unsigned getArgNo() const { return ArgNo; }
57 void foreachAttr(std::function<void(SPIRVFuncParamAttrKind)>);
58 void addAttr(SPIRVFuncParamAttrKind Kind) {
59 addDecorate(new SPIRVDecorate(DecorationFuncParamAttr, this, Kind));
60 }
61 void setParent(SPIRVFunction *Parent) { ParentFunc = Parent; }
62 bool hasAttr(SPIRVFuncParamAttrKind Kind) const {
63 return getDecorate(DecorationFuncParamAttr).count(Kind);
64 }
65 bool isByVal() const { return hasAttr(FunctionParameterAttributeByVal); }
66 bool isZext() const { return hasAttr(FunctionParameterAttributeZext); }
67 SPIRVCapVec getRequiredCapability() const override {
68 if (hasLinkageType() && getLinkageType() == LinkageTypeImport)
69 return getVec(CapabilityLinkage);
70 return SPIRVCapVec();
71 }
72
73 protected:
74 void validate() const override {
75 SPIRVValue::validate();
76 assert(ParentFunc && "Invalid parent function");
77 }
78 _SPIRV_DEF_ENCDEC2(Type, Id)
79 private:
80 SPIRVFunction *ParentFunc;
81 unsigned ArgNo;
82 };
83
84 class SPIRVFunction : public SPIRVValue, public SPIRVComponentExecutionModes {
85 public:
86 // Complete constructor. It does not construct basic blocks.
87 SPIRVFunction(SPIRVModule *M, SPIRVTypeFunction *FunctionType, SPIRVId TheId)
88 : SPIRVValue(M, 5, OpFunction, FunctionType->getReturnType(), TheId),
89 FuncType(FunctionType), FCtrlMask(FunctionControlMaskNone) {
90 addAllArguments(TheId + 1);
91 validate();
92 }
93
94 // Incomplete constructor
95 SPIRVFunction()
96 : SPIRVValue(OpFunction), FuncType(NULL),
97 FCtrlMask(FunctionControlMaskNone) {}
98
99 SPIRVDecoder getDecoder(std::istream &IS) override;
100 SPIRVTypeFunction *getFunctionType() const { return FuncType; }
101 SPIRVWord getFuncCtlMask() const { return FCtrlMask; }
102 size_t getNumBasicBlock() const { return BBVec.size(); }
103 SPIRVBasicBlock *getBasicBlock(size_t I) const { return BBVec[I]; }
104 size_t getNumArguments() const {
105 return getFunctionType()->getNumParameters();
106 }
107 SPIRVId getArgumentId(size_t I) const { return Parameters[I]->getId(); }
108 const std::vector<SPIRVId> getVariables() const {
109 std::vector<SPIRVId> Ids;
110 for (auto Variable : Variables)
111 Ids.push_back(Variable->getId());
112 return Ids;
113 }
114 void addVariable(const SPIRVValue *Variable) {
115 Variables.push_back(Variable);
116 }
117 SPIRVFunctionParameter *getArgument(size_t I) const { return Parameters[I]; }
118 void foreachArgument(std::function<void(SPIRVFunctionParameter *)> Func) {
119 for (size_t I = 0, E = getNumArguments(); I != E; ++I)
120 Func(getArgument(I));
121 }
122
123 void foreachReturnValueAttr(std::function<void(SPIRVFuncParamAttrKind)>);
124
125 void setFunctionControlMask(SPIRVWord Mask) { FCtrlMask = Mask; }
126
127 void takeExecutionModes(SPIRVForward *Forward) {
128 ExecModes = std::move(Forward->ExecModes);
129 }
130
131 // Assume BB contains valid Id.
132 SPIRVBasicBlock *addBasicBlock(SPIRVBasicBlock *BB) {
133 Module->add(BB);
134 BB->setParent(this);
135 BBVec.push_back(BB);
136 return BB;
137 }
138
139 void encodeChildren(spv_ostream &) const override;
140 void encodeExecutionModes(spv_ostream &) const;
141 _SPIRV_DCL_ENCDEC
142 void validate() const override {
143 SPIRVValue::validate();
144 validateFunctionControlMask(FCtrlMask);
145 assert(FuncType && "Invalid func type");
146 }
147
148 private:
149 SPIRVFunctionParameter *addArgument(unsigned TheArgNo, SPIRVId TheId) {
150 SPIRVFunctionParameter *Arg = new SPIRVFunctionParameter(
151 getFunctionType()->getParameterType(TheArgNo), TheId, this, TheArgNo);
152 Module->add(Arg);
153 Parameters.push_back(Arg);
154 return Arg;
155 }
156
157 void addAllArguments(SPIRVId FirstArgId) {
158 for (size_t I = 0, E = getFunctionType()->getNumParameters(); I != E; ++I)
159 addArgument(I, FirstArgId + I);
160 }
161 bool decodeBB(SPIRVDecoder &);
162
163 SPIRVTypeFunction *FuncType; // Function type
164 SPIRVWord FCtrlMask; // Function control mask
165
166 std::vector<SPIRVFunctionParameter *> Parameters;
167 std::vector<const SPIRVValue *> Variables;
168 typedef std::vector<SPIRVBasicBlock *> SPIRVLBasicBlockVector;
169 SPIRVLBasicBlockVector BBVec;
170 };
171
172 typedef SPIRVEntryOpCodeOnly<OpFunctionEnd> SPIRVFunctionEnd;
173
174 class SPIRVConstantFunctionPointerINTEL : public SPIRVValue {
175 const static Op OC = OpConstantFunctionPointerINTEL;
176 const static SPIRVWord FixedWordCount = 4;
177
178 public:
179 SPIRVConstantFunctionPointerINTEL(SPIRVId TheId, SPIRVType *TheType,
180 SPIRVFunction *TheFunction, SPIRVModule *M)
181 : SPIRVValue(M, FixedWordCount, OC, TheType, TheId),
182 TheFunction(TheFunction->getId()) {
183 validate();
184 }
185 SPIRVConstantFunctionPointerINTEL()
186 : SPIRVValue(OC), TheFunction(SPIRVID_INVALID) {}
187 SPIRVFunction *getFunction() const { return get<SPIRVFunction>(TheFunction); }
188 _SPIRV_DEF_ENCDEC3(Type, Id, TheFunction)
189 void validate() const override { SPIRVValue::validate(); }
190 std::optional<ExtensionID> getRequiredExtension() const override {
191 return ExtensionID::SPV_INTEL_function_pointers;
192 }
193 SPIRVCapVec getRequiredCapability() const override {
194 return getVec(CapabilityFunctionPointersINTEL);
195 }
196
197 protected:
198 SPIRVId TheFunction;
199 };
200
201 } // namespace SPIRV
202
203 #endif // SPIRV_LIBSPIRV_SPIRVFUNCTION_H