17
17
#include " SPIRVBuiltins.h"
18
18
#include " SPIRVGlobalRegistry.h"
19
19
#include " SPIRVISelLowering.h"
20
+ #include " SPIRVMetadata.h"
20
21
#include " SPIRVRegisterInfo.h"
21
22
#include " SPIRVSubtarget.h"
22
23
#include " SPIRVUtils.h"
@@ -117,64 +118,12 @@ static FunctionType *getOriginalFunctionType(const Function &F) {
117
118
return FunctionType::get (RetTy, ArgTypes, F.isVarArg ());
118
119
}
119
120
120
- static MDString *getKernelArgAttribute (const Function &KernelFunction,
121
- unsigned ArgIdx,
122
- const StringRef AttributeName) {
123
- assert (KernelFunction.getCallingConv () == CallingConv::SPIR_KERNEL &&
124
- " Kernel attributes are attached/belong only to kernel functions" );
125
-
126
- // Lookup the argument attribute in metadata attached to the kernel function.
127
- MDNode *Node = KernelFunction.getMetadata (AttributeName);
128
- if (Node && ArgIdx < Node->getNumOperands ())
129
- return cast<MDString>(Node->getOperand (ArgIdx));
130
-
131
- // Sometimes metadata containing kernel attributes is not attached to the
132
- // function, but can be found in the named module-level metadata instead.
133
- // For example:
134
- // !opencl.kernels = !{!0}
135
- // !0 = !{void ()* @someKernelFunction, !1, ...}
136
- // !1 = !{!"kernel_arg_addr_space", ...}
137
- // In this case the actual index of searched argument attribute is ArgIdx + 1,
138
- // since the first metadata node operand is occupied by attribute name
139
- // ("kernel_arg_addr_space" in the example above).
140
- unsigned MDArgIdx = ArgIdx + 1 ;
141
- NamedMDNode *OpenCLKernelsMD =
142
- KernelFunction.getParent ()->getNamedMetadata (" opencl.kernels" );
143
- if (!OpenCLKernelsMD || OpenCLKernelsMD->getNumOperands () == 0 )
144
- return nullptr ;
145
-
146
- // KernelToMDNodeList contains kernel function declarations followed by
147
- // corresponding MDNodes for each attribute. Search only MDNodes "belonging"
148
- // to the currently lowered kernel function.
149
- MDNode *KernelToMDNodeList = OpenCLKernelsMD->getOperand (0 );
150
- bool FoundLoweredKernelFunction = false ;
151
- for (const MDOperand &Operand : KernelToMDNodeList->operands ()) {
152
- ValueAsMetadata *MaybeValue = dyn_cast<ValueAsMetadata>(Operand);
153
- if (MaybeValue && dyn_cast<Function>(MaybeValue->getValue ())->getName () ==
154
- KernelFunction.getName ()) {
155
- FoundLoweredKernelFunction = true ;
156
- continue ;
157
- }
158
- if (MaybeValue && FoundLoweredKernelFunction)
159
- return nullptr ;
160
-
161
- MDNode *MaybeNode = dyn_cast<MDNode>(Operand);
162
- if (FoundLoweredKernelFunction && MaybeNode &&
163
- cast<MDString>(MaybeNode->getOperand (0 ))->getString () ==
164
- AttributeName &&
165
- MDArgIdx < MaybeNode->getNumOperands ())
166
- return cast<MDString>(MaybeNode->getOperand (MDArgIdx));
167
- }
168
- return nullptr ;
169
- }
170
-
171
121
static SPIRV::AccessQualifier::AccessQualifier
172
122
getArgAccessQual (const Function &F, unsigned ArgIdx) {
173
123
if (F.getCallingConv () != CallingConv::SPIR_KERNEL)
174
124
return SPIRV::AccessQualifier::ReadWrite;
175
125
176
- MDString *ArgAttribute =
177
- getKernelArgAttribute (F, ArgIdx, " kernel_arg_access_qual" );
126
+ MDString *ArgAttribute = getOCLKernelArgAccessQual (F, ArgIdx);
178
127
if (!ArgAttribute)
179
128
return SPIRV::AccessQualifier::ReadWrite;
180
129
@@ -186,9 +135,8 @@ getArgAccessQual(const Function &F, unsigned ArgIdx) {
186
135
}
187
136
188
137
static std::vector<SPIRV::Decoration::Decoration>
189
- getKernelArgTypeQual (const Function &KernelFunction, unsigned ArgIdx) {
190
- MDString *ArgAttribute =
191
- getKernelArgAttribute (KernelFunction, ArgIdx, " kernel_arg_type_qual" );
138
+ getKernelArgTypeQual (const Function &F, unsigned ArgIdx) {
139
+ MDString *ArgAttribute = getOCLKernelArgTypeQual (F, ArgIdx);
192
140
if (ArgAttribute && ArgAttribute->getString ().compare (" volatile" ) == 0 )
193
141
return {SPIRV::Decoration::Volatile};
194
142
return {};
@@ -209,8 +157,7 @@ static SPIRVType *getArgSPIRVType(const Function &F, unsigned ArgIdx,
209
157
isSpecialOpaqueType (OriginalArgType))
210
158
return GR->getOrCreateSPIRVType (OriginalArgType, MIRBuilder, ArgAccessQual);
211
159
212
- MDString *MDKernelArgType =
213
- getKernelArgAttribute (F, ArgIdx, " kernel_arg_type" );
160
+ MDString *MDKernelArgType = getOCLKernelArgType (F, ArgIdx);
214
161
if (!MDKernelArgType || (!MDKernelArgType->getString ().ends_with (" *" ) &&
215
162
!MDKernelArgType->getString ().ends_with (" _t" )))
216
163
return GR->getOrCreateSPIRVType (OriginalArgType, MIRBuilder, ArgAccessQual);
0 commit comments