MLIR  16.0.0git
DeserializeOps.cpp
Go to the documentation of this file.
1 //===- DeserializeOps.cpp - MLIR SPIR-V Deserialization (Ops) -------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines the Deserializer methods for SPIR-V binary instructions.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "Deserializer.h"
14 
17 #include "mlir/IR/Builders.h"
18 #include "mlir/IR/Location.h"
20 #include "llvm/ADT/STLExtras.h"
21 #include "llvm/ADT/SmallVector.h"
22 #include "llvm/Support/Debug.h"
23 
24 using namespace mlir;
25 
26 #define DEBUG_TYPE "spirv-deserialization"
27 
28 //===----------------------------------------------------------------------===//
29 // Utility Functions
30 //===----------------------------------------------------------------------===//
31 
32 /// Extracts the opcode from the given first word of a SPIR-V instruction.
33 static inline spirv::Opcode extractOpcode(uint32_t word) {
34  return static_cast<spirv::Opcode>(word & 0xffff);
35 }
36 
37 //===----------------------------------------------------------------------===//
38 // Instruction
39 //===----------------------------------------------------------------------===//
40 
41 Value spirv::Deserializer::getValue(uint32_t id) {
42  if (auto constInfo = getConstant(id)) {
43  // Materialize a `spv.Constant` op at every use site.
44  return opBuilder.create<spirv::ConstantOp>(unknownLoc, constInfo->second,
45  constInfo->first);
46  }
47  if (auto varOp = getGlobalVariable(id)) {
48  auto addressOfOp = opBuilder.create<spirv::AddressOfOp>(
49  unknownLoc, varOp.type(), SymbolRefAttr::get(varOp.getOperation()));
50  return addressOfOp.pointer();
51  }
52  if (auto constOp = getSpecConstant(id)) {
53  auto referenceOfOp = opBuilder.create<spirv::ReferenceOfOp>(
54  unknownLoc, constOp.default_value().getType(),
55  SymbolRefAttr::get(constOp.getOperation()));
56  return referenceOfOp.reference();
57  }
58  if (auto constCompositeOp = getSpecConstantComposite(id)) {
59  auto referenceOfOp = opBuilder.create<spirv::ReferenceOfOp>(
60  unknownLoc, constCompositeOp.type(),
61  SymbolRefAttr::get(constCompositeOp.getOperation()));
62  return referenceOfOp.reference();
63  }
64  if (auto specConstOperationInfo = getSpecConstantOperation(id)) {
65  return materializeSpecConstantOperation(
66  id, specConstOperationInfo->enclodesOpcode,
67  specConstOperationInfo->resultTypeID,
68  specConstOperationInfo->enclosedOpOperands);
69  }
70  if (auto undef = getUndefType(id)) {
71  return opBuilder.create<spirv::UndefOp>(unknownLoc, undef);
72  }
73  return valueMap.lookup(id);
74 }
75 
77 spirv::Deserializer::sliceInstruction(spirv::Opcode &opcode,
78  ArrayRef<uint32_t> &operands,
79  Optional<spirv::Opcode> expectedOpcode) {
80  auto binarySize = binary.size();
81  if (curOffset >= binarySize) {
82  return emitError(unknownLoc, "expected ")
83  << (expectedOpcode ? spirv::stringifyOpcode(*expectedOpcode)
84  : "more")
85  << " instruction";
86  }
87 
88  // For each instruction, get its word count from the first word to slice it
89  // from the stream properly, and then dispatch to the instruction handler.
90 
91  uint32_t wordCount = binary[curOffset] >> 16;
92 
93  if (wordCount == 0)
94  return emitError(unknownLoc, "word count cannot be zero");
95 
96  uint32_t nextOffset = curOffset + wordCount;
97  if (nextOffset > binarySize)
98  return emitError(unknownLoc, "insufficient words for the last instruction");
99 
100  opcode = extractOpcode(binary[curOffset]);
101  operands = binary.slice(curOffset + 1, wordCount - 1);
102  curOffset = nextOffset;
103  return success();
104 }
105 
106 LogicalResult spirv::Deserializer::processInstruction(
107  spirv::Opcode opcode, ArrayRef<uint32_t> operands, bool deferInstructions) {
108  LLVM_DEBUG(logger.startLine() << "[inst] processing instruction "
109  << spirv::stringifyOpcode(opcode) << "\n");
110 
111  // First dispatch all the instructions whose opcode does not correspond to
112  // those that have a direct mirror in the SPIR-V dialect
113  switch (opcode) {
114  case spirv::Opcode::OpCapability:
115  return processCapability(operands);
116  case spirv::Opcode::OpExtension:
117  return processExtension(operands);
118  case spirv::Opcode::OpExtInst:
119  return processExtInst(operands);
120  case spirv::Opcode::OpExtInstImport:
121  return processExtInstImport(operands);
122  case spirv::Opcode::OpMemberName:
123  return processMemberName(operands);
124  case spirv::Opcode::OpMemoryModel:
125  return processMemoryModel(operands);
126  case spirv::Opcode::OpEntryPoint:
127  case spirv::Opcode::OpExecutionMode:
128  if (deferInstructions) {
129  deferredInstructions.emplace_back(opcode, operands);
130  return success();
131  }
132  break;
133  case spirv::Opcode::OpVariable:
134  if (isa<spirv::ModuleOp>(opBuilder.getBlock()->getParentOp())) {
135  return processGlobalVariable(operands);
136  }
137  break;
138  case spirv::Opcode::OpLine:
139  return processDebugLine(operands);
140  case spirv::Opcode::OpNoLine:
141  clearDebugLine();
142  return success();
143  case spirv::Opcode::OpName:
144  return processName(operands);
145  case spirv::Opcode::OpString:
146  return processDebugString(operands);
147  case spirv::Opcode::OpModuleProcessed:
148  case spirv::Opcode::OpSource:
149  case spirv::Opcode::OpSourceContinued:
150  case spirv::Opcode::OpSourceExtension:
151  // TODO: This is debug information embedded in the binary which should be
152  // translated into the spv.module.
153  return success();
154  case spirv::Opcode::OpTypeVoid:
155  case spirv::Opcode::OpTypeBool:
156  case spirv::Opcode::OpTypeInt:
157  case spirv::Opcode::OpTypeFloat:
158  case spirv::Opcode::OpTypeVector:
159  case spirv::Opcode::OpTypeMatrix:
160  case spirv::Opcode::OpTypeArray:
161  case spirv::Opcode::OpTypeFunction:
162  case spirv::Opcode::OpTypeImage:
163  case spirv::Opcode::OpTypeSampledImage:
164  case spirv::Opcode::OpTypeRuntimeArray:
165  case spirv::Opcode::OpTypeStruct:
166  case spirv::Opcode::OpTypePointer:
167  case spirv::Opcode::OpTypeCooperativeMatrixNV:
168  return processType(opcode, operands);
169  case spirv::Opcode::OpTypeForwardPointer:
170  return processTypeForwardPointer(operands);
171  case spirv::Opcode::OpConstant:
172  return processConstant(operands, /*isSpec=*/false);
173  case spirv::Opcode::OpSpecConstant:
174  return processConstant(operands, /*isSpec=*/true);
175  case spirv::Opcode::OpConstantComposite:
176  return processConstantComposite(operands);
177  case spirv::Opcode::OpSpecConstantComposite:
178  return processSpecConstantComposite(operands);
179  case spirv::Opcode::OpSpecConstantOp:
180  return processSpecConstantOperation(operands);
181  case spirv::Opcode::OpConstantTrue:
182  return processConstantBool(/*isTrue=*/true, operands, /*isSpec=*/false);
183  case spirv::Opcode::OpSpecConstantTrue:
184  return processConstantBool(/*isTrue=*/true, operands, /*isSpec=*/true);
185  case spirv::Opcode::OpConstantFalse:
186  return processConstantBool(/*isTrue=*/false, operands, /*isSpec=*/false);
187  case spirv::Opcode::OpSpecConstantFalse:
188  return processConstantBool(/*isTrue=*/false, operands, /*isSpec=*/true);
189  case spirv::Opcode::OpConstantNull:
190  return processConstantNull(operands);
191  case spirv::Opcode::OpDecorate:
192  return processDecoration(operands);
193  case spirv::Opcode::OpMemberDecorate:
194  return processMemberDecoration(operands);
195  case spirv::Opcode::OpFunction:
196  return processFunction(operands);
197  case spirv::Opcode::OpLabel:
198  return processLabel(operands);
199  case spirv::Opcode::OpBranch:
200  return processBranch(operands);
201  case spirv::Opcode::OpBranchConditional:
202  return processBranchConditional(operands);
203  case spirv::Opcode::OpSelectionMerge:
204  return processSelectionMerge(operands);
205  case spirv::Opcode::OpLoopMerge:
206  return processLoopMerge(operands);
207  case spirv::Opcode::OpPhi:
208  return processPhi(operands);
209  case spirv::Opcode::OpUndef:
210  return processUndef(operands);
211  default:
212  break;
213  }
214  return dispatchToAutogenDeserialization(opcode, operands);
215 }
216 
217 LogicalResult spirv::Deserializer::processOpWithoutGrammarAttr(
218  ArrayRef<uint32_t> words, StringRef opName, bool hasResult,
219  unsigned numOperands) {
220  SmallVector<Type, 1> resultTypes;
221  uint32_t valueID = 0;
222 
223  size_t wordIndex = 0;
224  if (hasResult) {
225  if (wordIndex >= words.size())
226  return emitError(unknownLoc,
227  "expected result type <id> while deserializing for ")
228  << opName;
229 
230  // Decode the type <id>
231  auto type = getType(words[wordIndex]);
232  if (!type)
233  return emitError(unknownLoc, "unknown type result <id>: ")
234  << words[wordIndex];
235  resultTypes.push_back(type);
236  ++wordIndex;
237 
238  // Decode the result <id>
239  if (wordIndex >= words.size())
240  return emitError(unknownLoc,
241  "expected result <id> while deserializing for ")
242  << opName;
243  valueID = words[wordIndex];
244  ++wordIndex;
245  }
246 
247  SmallVector<Value, 4> operands;
249 
250  // Decode operands
251  size_t operandIndex = 0;
252  for (; operandIndex < numOperands && wordIndex < words.size();
253  ++operandIndex, ++wordIndex) {
254  auto arg = getValue(words[wordIndex]);
255  if (!arg)
256  return emitError(unknownLoc, "unknown result <id>: ") << words[wordIndex];
257  operands.push_back(arg);
258  }
259  if (operandIndex != numOperands) {
260  return emitError(
261  unknownLoc,
262  "found less operands than expected when deserializing for ")
263  << opName << "; only " << operandIndex << " of " << numOperands
264  << " processed";
265  }
266  if (wordIndex != words.size()) {
267  return emitError(
268  unknownLoc,
269  "found more operands than expected when deserializing for ")
270  << opName << "; only " << wordIndex << " of " << words.size()
271  << " processed";
272  }
273 
274  // Attach attributes from decorations
275  if (decorations.count(valueID)) {
276  auto attrs = decorations[valueID].getAttrs();
277  attributes.append(attrs.begin(), attrs.end());
278  }
279 
280  // Create the op and update bookkeeping maps
281  Location loc = createFileLineColLoc(opBuilder);
282  OperationState opState(loc, opName);
283  opState.addOperands(operands);
284  if (hasResult)
285  opState.addTypes(resultTypes);
286  opState.addAttributes(attributes);
287  Operation *op = opBuilder.create(opState);
288  if (hasResult)
289  valueMap[valueID] = op->getResult(0);
290 
291  if (op->hasTrait<OpTrait::IsTerminator>())
292  clearDebugLine();
293 
294  return success();
295 }
296 
297 LogicalResult spirv::Deserializer::processUndef(ArrayRef<uint32_t> operands) {
298  if (operands.size() != 2) {
299  return emitError(unknownLoc, "OpUndef instruction must have two operands");
300  }
301  auto type = getType(operands[0]);
302  if (!type) {
303  return emitError(unknownLoc, "unknown type <id> with OpUndef instruction");
304  }
305  undefMap[operands[1]] = type;
306  return success();
307 }
308 
309 LogicalResult spirv::Deserializer::processExtInst(ArrayRef<uint32_t> operands) {
310  if (operands.size() < 4) {
311  return emitError(unknownLoc,
312  "OpExtInst must have at least 4 operands, result type "
313  "<id>, result <id>, set <id> and instruction opcode");
314  }
315  if (!extendedInstSets.count(operands[2])) {
316  return emitError(unknownLoc, "undefined set <id> in OpExtInst");
317  }
318  SmallVector<uint32_t, 4> slicedOperands;
319  slicedOperands.append(operands.begin(), std::next(operands.begin(), 2));
320  slicedOperands.append(std::next(operands.begin(), 4), operands.end());
321  return dispatchToExtensionSetAutogenDeserialization(
322  extendedInstSets[operands[2]], operands[3], slicedOperands);
323 }
324 
325 namespace mlir {
326 namespace spirv {
327 
328 template <>
330 Deserializer::processOp<spirv::EntryPointOp>(ArrayRef<uint32_t> words) {
331  unsigned wordIndex = 0;
332  if (wordIndex >= words.size()) {
333  return emitError(unknownLoc,
334  "missing Execution Model specification in OpEntryPoint");
335  }
336  auto execModel = spirv::ExecutionModelAttr::get(
337  context, static_cast<spirv::ExecutionModel>(words[wordIndex++]));
338  if (wordIndex >= words.size()) {
339  return emitError(unknownLoc, "missing <id> in OpEntryPoint");
340  }
341  // Get the function <id>
342  auto fnID = words[wordIndex++];
343  // Get the function name
344  auto fnName = decodeStringLiteral(words, wordIndex);
345  // Verify that the function <id> matches the fnName
346  auto parsedFunc = getFunction(fnID);
347  if (!parsedFunc) {
348  return emitError(unknownLoc, "no function matching <id> ") << fnID;
349  }
350  if (parsedFunc.getName() != fnName) {
351  // The deserializer uses "spirv_fn_<id>" as the function name if the input
352  // SPIR-V blob does not contain a name for it. We should use a more clear
353  // indication for such case rather than relying on naming details.
354  if (!parsedFunc.getName().startswith("spirv_fn_"))
355  return emitError(unknownLoc,
356  "function name mismatch between OpEntryPoint "
357  "and OpFunction with <id> ")
358  << fnID << ": " << fnName << " vs. " << parsedFunc.getName();
359  parsedFunc.setName(fnName);
360  }
361  SmallVector<Attribute, 4> interface;
362  while (wordIndex < words.size()) {
363  auto arg = getGlobalVariable(words[wordIndex]);
364  if (!arg) {
365  return emitError(unknownLoc, "undefined result <id> ")
366  << words[wordIndex] << " while decoding OpEntryPoint";
367  }
368  interface.push_back(SymbolRefAttr::get(arg.getOperation()));
369  wordIndex++;
370  }
371  opBuilder.create<spirv::EntryPointOp>(
372  unknownLoc, execModel, SymbolRefAttr::get(opBuilder.getContext(), fnName),
373  opBuilder.getArrayAttr(interface));
374  return success();
375 }
376 
377 template <>
379 Deserializer::processOp<spirv::ExecutionModeOp>(ArrayRef<uint32_t> words) {
380  unsigned wordIndex = 0;
381  if (wordIndex >= words.size()) {
382  return emitError(unknownLoc,
383  "missing function result <id> in OpExecutionMode");
384  }
385  // Get the function <id> to get the name of the function
386  auto fnID = words[wordIndex++];
387  auto fn = getFunction(fnID);
388  if (!fn) {
389  return emitError(unknownLoc, "no function matching <id> ") << fnID;
390  }
391  // Get the Execution mode
392  if (wordIndex >= words.size()) {
393  return emitError(unknownLoc, "missing Execution Mode in OpExecutionMode");
394  }
395  auto execMode = spirv::ExecutionModeAttr::get(
396  context, static_cast<spirv::ExecutionMode>(words[wordIndex++]));
397 
398  // Get the values
399  SmallVector<Attribute, 4> attrListElems;
400  while (wordIndex < words.size()) {
401  attrListElems.push_back(opBuilder.getI32IntegerAttr(words[wordIndex++]));
402  }
403  auto values = opBuilder.getArrayAttr(attrListElems);
404  opBuilder.create<spirv::ExecutionModeOp>(
405  unknownLoc, SymbolRefAttr::get(opBuilder.getContext(), fn.getName()),
406  execMode, values);
407  return success();
408 }
409 
410 template <>
412 Deserializer::processOp<spirv::FunctionCallOp>(ArrayRef<uint32_t> operands) {
413  if (operands.size() < 3) {
414  return emitError(unknownLoc,
415  "OpFunctionCall must have at least 3 operands");
416  }
417 
418  Type resultType = getType(operands[0]);
419  if (!resultType) {
420  return emitError(unknownLoc, "undefined result type from <id> ")
421  << operands[0];
422  }
423 
424  // Use null type to mean no result type.
425  if (isVoidType(resultType))
426  resultType = nullptr;
427 
428  auto resultID = operands[1];
429  auto functionID = operands[2];
430 
431  auto functionName = getFunctionSymbol(functionID);
432 
433  SmallVector<Value, 4> arguments;
434  for (auto operand : llvm::drop_begin(operands, 3)) {
435  auto value = getValue(operand);
436  if (!value) {
437  return emitError(unknownLoc, "unknown <id> ")
438  << operand << " used by OpFunctionCall";
439  }
440  arguments.push_back(value);
441  }
442 
443  auto opFunctionCall = opBuilder.create<spirv::FunctionCallOp>(
444  unknownLoc, resultType,
445  SymbolRefAttr::get(opBuilder.getContext(), functionName), arguments);
446 
447  if (resultType)
448  valueMap[resultID] = opFunctionCall.getResult(0);
449  return success();
450 }
451 
452 template <>
454 Deserializer::processOp<spirv::CopyMemoryOp>(ArrayRef<uint32_t> words) {
455  SmallVector<Type, 1> resultTypes;
456  size_t wordIndex = 0;
457  SmallVector<Value, 4> operands;
459 
460  if (wordIndex < words.size()) {
461  auto arg = getValue(words[wordIndex]);
462 
463  if (!arg) {
464  return emitError(unknownLoc, "unknown result <id> : ")
465  << words[wordIndex];
466  }
467 
468  operands.push_back(arg);
469  wordIndex++;
470  }
471 
472  if (wordIndex < words.size()) {
473  auto arg = getValue(words[wordIndex]);
474 
475  if (!arg) {
476  return emitError(unknownLoc, "unknown result <id> : ")
477  << words[wordIndex];
478  }
479 
480  operands.push_back(arg);
481  wordIndex++;
482  }
483 
484  bool isAlignedAttr = false;
485 
486  if (wordIndex < words.size()) {
487  auto attrValue = words[wordIndex++];
488  auto attr = opBuilder.getAttr<spirv::MemoryAccessAttr>(
489  static_cast<spirv::MemoryAccess>(attrValue));
490  attributes.push_back(opBuilder.getNamedAttr("memory_access", attr));
491  isAlignedAttr = (attrValue == 2);
492  }
493 
494  if (isAlignedAttr && wordIndex < words.size()) {
495  attributes.push_back(opBuilder.getNamedAttr(
496  "alignment", opBuilder.getI32IntegerAttr(words[wordIndex++])));
497  }
498 
499  if (wordIndex < words.size()) {
500  auto attrValue = words[wordIndex++];
501  auto attr = opBuilder.getAttr<spirv::MemoryAccessAttr>(
502  static_cast<spirv::MemoryAccess>(attrValue));
503  attributes.push_back(opBuilder.getNamedAttr("source_memory_access", attr));
504  }
505 
506  if (wordIndex < words.size()) {
507  attributes.push_back(opBuilder.getNamedAttr(
508  "source_alignment", opBuilder.getI32IntegerAttr(words[wordIndex++])));
509  }
510 
511  if (wordIndex != words.size()) {
512  return emitError(unknownLoc,
513  "found more operands than expected when deserializing "
514  "spirv::CopyMemoryOp, only ")
515  << wordIndex << " of " << words.size() << " processed";
516  }
517 
518  Location loc = createFileLineColLoc(opBuilder);
519  opBuilder.create<spirv::CopyMemoryOp>(loc, resultTypes, operands, attributes);
520 
521  return success();
522 }
523 
524 // Pull in auto-generated Deserializer::dispatchToAutogenDeserialization() and
525 // various Deserializer::processOp<...>() specializations.
526 #define GET_DESERIALIZATION_FNS
527 #include "mlir/Dialect/SPIRV/IR/SPIRVSerialization.inc"
528 
529 } // namespace spirv
530 } // namespace mlir
Include the generated interface declarations.
Operation is a basic unit of execution within MLIR.
Definition: Operation.h:28
Operation * getParentOp()
Returns the closest surrounding operation that contains this block.
Definition: Block.cpp:30
This class provides the API for ops that are known to be terminators.
Definition: OpDefinition.h:707
static constexpr const bool value
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition: Location.h:48
Block * getBlock() const
Returns the current block of the builder.
Definition: Builders.h:394
LogicalResult success(bool isSuccess=true)
Utility function to generate a LogicalResult.
Definition: LogicalResult.h:56
Operation * create(const OperationState &state)
Creates an operation given the fields represented as an OperationState.
Definition: Builders.cpp:404
This class represents an efficient way to signal success or failure.
Definition: LogicalResult.h:26
void addOperands(ValueRange newOperands)
bool hasTrait()
Returns true if the operation was registered with a particular trait, e.g.
Definition: Operation.h:528
void addAttributes(ArrayRef< NamedAttribute > newAttributes)
Add an array of named attributes.
OpResult getResult(unsigned idx)
Get the &#39;idx&#39;th result of this operation.
Definition: Operation.h:324
void addTypes(ArrayRef< Type > newTypes)
This represents an operation in an abstracted form, suitable for use with the builder APIs...
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition: Types.h:72
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition: Value.h:85
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.
StringRef decodeStringLiteral(ArrayRef< uint32_t > words, unsigned &wordIndex)
Decodes a string literal in words starting at wordIndex.
static spirv::Opcode extractOpcode(uint32_t word)
Extracts the opcode from the given first word of a SPIR-V instruction.