MLIR 24.0.0git
SerializeOps.cpp
Go to the documentation of this file.
1//===- SerializeOps.cpp - MLIR SPIR-V Serialization (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 serialization methods for MLIR SPIR-V module ops.
10//
11//===----------------------------------------------------------------------===//
12
13#include "Serializer.h"
14
21#include "llvm/ADT/DepthFirstIterator.h"
22#include "llvm/ADT/StringExtras.h"
23#include "llvm/Support/Debug.h"
24#include "llvm/Support/FormatVariadic.h"
25
26#define DEBUG_TYPE "spirv-serialization"
27
28using namespace mlir;
29
30namespace {
31// Location::print() emits MLIR syntax such as `loc("name")` or
32// `loc(fused["op", "file":1:2])`. NonSemantic.Graph.DebugInfo stores the
33// source/debug name itself in an OpString, so keep this conversion to the
34// payload string explicit.
35std::string getDebugInfoStringFromLoc(Location loc) {
36 if (auto fileLineCol = dyn_cast<FileLineColLoc>(loc)) {
37 return llvm::formatv("{0}:{1}:{2}", fileLineCol.getFilename(),
38 fileLineCol.getLine(), fileLineCol.getColumn());
39 }
40 if (auto nameLoc = dyn_cast<NameLoc>(loc)) {
41 return nameLoc.getName().str();
42 }
43 if (auto fusedLoc = dyn_cast<FusedLoc>(loc)) {
44 std::string result;
45 llvm::raw_string_ostream os(result);
46 llvm::interleave(
47 map_range(fusedLoc.getLocations(), getDebugInfoStringFromLoc), os, ";");
48 return result;
49 }
50 return "";
51}
52} // namespace
54/// A pre-order depth-first visitor function for processing basic blocks.
55///
56/// Visits the basic blocks starting from the given `headerBlock` in pre-order
57/// depth-first manner and calls `blockHandler` on each block. Skips handling
58/// blocks in the `skipBlocks` list. If `skipHeader` is true, `blockHandler`
59/// will not be invoked in `headerBlock` but still handles all `headerBlock`'s
60/// successors.
61///
62/// SPIR-V spec "2.16.1. Universal Validation Rules" requires that "the order
63/// of blocks in a function must satisfy the rule that blocks appear before
64/// all blocks they dominate." This can be achieved by a pre-order CFG
65/// traversal algorithm. To make the serialization output more logical and
66/// readable to human, we perform depth-first CFG traversal and delay the
67/// serialization of the merge block and the continue block, if exists, until
68/// after all other blocks have been processed.
69static LogicalResult
71 function_ref<LogicalResult(Block *)> blockHandler,
72 bool skipHeader = false, BlockRange skipBlocks = {}) {
73 llvm::df_iterator_default_set<Block *, 4> doneBlocks;
74 doneBlocks.insert(skipBlocks.begin(), skipBlocks.end());
75
76 for (Block *block : llvm::depth_first_ext(headerBlock, doneBlocks)) {
77 if (skipHeader && block == headerBlock)
78 continue;
79 if (failed(blockHandler(block)))
80 return failure();
81 }
82 return success();
83}
84
85namespace mlir {
86namespace spirv {
87LogicalResult Serializer::processConstantOp(spirv::ConstantOp op) {
88 if (auto resultID =
89 prepareConstant(op.getLoc(), op.getType(), op.getValue())) {
90 valueIDMap[op.getResult()] = resultID;
91 if (isa<spirv::TensorArmType>(op.getType()) &&
92 failed(encodeDebugInfoTensorInst(op.getResult())))
93 return failure();
94 return success();
95 }
96 return failure();
97}
98
99LogicalResult Serializer::processConstantCompositeReplicateOp(
100 spirv::EXTConstantCompositeReplicateOp op) {
101 if (uint32_t resultID = prepareConstantCompositeReplicate(
102 op.getLoc(), op.getType(), op.getValue())) {
103 valueIDMap[op.getResult()] = resultID;
104 return success();
105 }
106 return failure();
107}
108
109LogicalResult Serializer::processSpecConstantOp(spirv::SpecConstantOp op) {
110 if (auto resultID = prepareConstantScalar(op.getLoc(), op.getDefaultValue(),
111 /*isSpec=*/true)) {
112 // Emit the OpDecorate instruction for SpecId.
113 if (auto specID = op->getDiscardableAttrOfType<IntegerAttr>("spec_id")) {
114 auto val = static_cast<uint32_t>(specID.getInt());
115 if (failed(emitDecoration(resultID, spirv::Decoration::SpecId, {val})))
116 return failure();
117 }
118
119 specConstIDMap[op.getSymName()] = resultID;
120 return processName(resultID, op.getSymName());
121 }
122 return failure();
123}
124
125LogicalResult
126Serializer::processSpecConstantCompositeOp(spirv::SpecConstantCompositeOp op) {
127 uint32_t typeID = 0;
128 if (failed(processType(op.getLoc(), op.getType(), typeID))) {
129 return failure();
130 }
131
132 auto resultID = getNextID();
133
134 SmallVector<uint32_t, 8> operands;
135 operands.push_back(typeID);
136 operands.push_back(resultID);
137
138 auto constituents = op.getConstituents();
139
140 for (auto index : llvm::seq<uint32_t>(0, constituents.size())) {
141 auto constituent = dyn_cast<FlatSymbolRefAttr>(constituents[index]);
143 auto constituentName = constituent.getValue();
144 auto constituentID = getSpecConstID(constituentName);
145
146 if (!constituentID) {
147 return op.emitError("unknown result <id> for specialization constant ")
148 << constituentName;
149 }
150
151 operands.push_back(constituentID);
152 }
153
154 encodeInstructionWithContinuationInto(
155 typesGlobalValues, spirv::Opcode::OpSpecConstantComposite, operands);
156 specConstIDMap[op.getSymName()] = resultID;
157
158 return processName(resultID, op.getSymName());
159}
160
161LogicalResult Serializer::processSpecConstantCompositeReplicateOp(
162 spirv::EXTSpecConstantCompositeReplicateOp op) {
163 uint32_t typeID = 0;
164 if (failed(processType(op.getLoc(), op.getType(), typeID))) {
165 return failure();
166 }
167
168 auto constituent = dyn_cast<FlatSymbolRefAttr>(op.getConstituent());
169 if (!constituent)
170 return op.emitError(
171 "expected flat symbol reference for constituent instead of ")
172 << op.getConstituent();
173
174 StringRef constituentName = constituent.getValue();
175 uint32_t constituentID = getSpecConstID(constituentName);
176 if (!constituentID) {
177 return op.emitError("unknown result <id> for replicated spec constant ")
178 << constituentName;
179 }
180
181 uint32_t resultID = getNextID();
182 uint32_t operands[] = {typeID, resultID, constituentID};
183
184 encodeInstructionInto(typesGlobalValues,
185 spirv::Opcode::OpSpecConstantCompositeReplicateEXT,
186 operands);
187
188 specConstIDMap[op.getSymName()] = resultID;
189
190 return processName(resultID, op.getSymName());
191}
192
193LogicalResult
194Serializer::processSpecConstantOperationOp(spirv::SpecConstantOperationOp op) {
195 uint32_t typeID = 0;
196 if (failed(processType(op.getLoc(), op.getType(), typeID))) {
197 return failure();
198 }
199
200 auto resultID = getNextID();
201
202 SmallVector<uint32_t, 8> operands;
203 operands.push_back(typeID);
204 operands.push_back(resultID);
205
206 Block &block = op.getRegion().getBlocks().front();
207 Operation &enclosedOp = block.getOperations().front();
208
209 std::string enclosedOpName;
210 llvm::raw_string_ostream rss(enclosedOpName);
211 rss << "Op" << enclosedOp.getName().stripDialect();
212 auto enclosedOpcode = spirv::symbolizeOpcode(enclosedOpName);
213
214 if (!enclosedOpcode) {
215 op.emitError("Couldn't find op code for op ")
216 << enclosedOp.getName().getStringRef();
217 return failure();
218 }
219
220 operands.push_back(static_cast<uint32_t>(*enclosedOpcode));
221
222 // Append operands to the enclosed op to the list of operands.
223 for (Value operand : enclosedOp.getOperands()) {
224 uint32_t id = getValueID(operand);
225 assert(id && "use before def!");
226 operands.push_back(id);
227 }
228
229 encodeInstructionInto(typesGlobalValues, spirv::Opcode::OpSpecConstantOp,
230 operands);
231 valueIDMap[op.getResult()] = resultID;
232
233 return success();
234}
235
236LogicalResult
237Serializer::processGraphConstantARMOp(spirv::GraphConstantARMOp op) {
238 if (uint32_t resultID = prepareGraphConstantId(op.getLoc(), op.getType(),
239 op.getGraphConstantIdAttr())) {
240 valueIDMap[op.getResult()] = resultID;
241 return success();
242 }
243 return failure();
244}
245
246LogicalResult Serializer::processUndefOp(spirv::UndefOp op) {
247 auto undefType = op.getType();
248 auto &id = undefValIDMap[undefType];
249 if (!id) {
250 id = getNextID();
251 uint32_t typeID = 0;
252 if (failed(processType(op.getLoc(), undefType, typeID)))
253 return failure();
254 encodeInstructionInto(typesGlobalValues, spirv::Opcode::OpUndef,
255 {typeID, id});
256 }
257 valueIDMap[op.getResult()] = id;
258 return success();
259}
260
261LogicalResult Serializer::processFuncParameter(spirv::FuncOp op) {
262 for (auto [idx, arg] : llvm::enumerate(op.getArguments())) {
263 uint32_t argTypeID = 0;
264 if (failed(processType(op.getLoc(), arg.getType(), argTypeID))) {
265 return failure();
266 }
267 auto argValueID = getNextID();
268
269 // Process decoration attributes of arguments.
270 auto funcOp = cast<FunctionOpInterface>(*op);
271 for (auto argAttr : funcOp.getArgAttrs(idx)) {
272 if (argAttr.getName() == spirv::getFuncParamAlignmentAttrName() ||
273 argAttr.getName() == spirv::getFuncParamAttrName()) {
274 auto decoration =
275 argAttr.getName() == spirv::getFuncParamAlignmentAttrName()
276 ? Decoration::Alignment
277 : Decoration::FuncParamAttr;
278 if (failed(processDecorationAttr(op.getLoc(), argValueID, decoration,
279 argAttr.getValue())))
280 return failure();
281 continue;
283 if (argAttr.getName() != DecorationAttr::name)
284 continue;
285
286 if (auto decAttr = dyn_cast<DecorationAttr>(argAttr.getValue())) {
287 if (failed(processDecorationAttr(op->getLoc(), argValueID,
288 decAttr.getValue(), decAttr)))
289 return failure();
290 }
291 }
292
293 valueIDMap[arg] = argValueID;
294 encodeInstructionInto(functionHeader, spirv::Opcode::OpFunctionParameter,
295 {argTypeID, argValueID});
296 }
297 return success();
298}
299
300LogicalResult Serializer::processFuncOp(spirv::FuncOp op) {
301 LLVM_DEBUG(llvm::dbgs() << "-- start function '" << op.getName() << "' --\n");
302 assert(functionHeader.empty() && functionBody.empty());
303
304 uint32_t fnTypeID = 0;
305 // Generate type of the function.
306 if (failed(processType(op.getLoc(), op.getFunctionType(), fnTypeID)))
307 return failure();
308
309 // Add the function definition.
310 SmallVector<uint32_t, 4> operands;
311 uint32_t resTypeID = 0;
312 auto resultTypes = op.getFunctionType().getResults();
313 if (resultTypes.size() > 1) {
314 return op.emitError("cannot serialize function with multiple return types");
315 }
316 if (failed(processType(op.getLoc(),
317 (resultTypes.empty() ? getVoidType() : resultTypes[0]),
318 resTypeID))) {
319 return failure();
320 }
321 operands.push_back(resTypeID);
322 auto funcID = getOrCreateFunctionID(op.getName());
323 operands.push_back(funcID);
324 operands.push_back(static_cast<uint32_t>(op.getFunctionControl()));
325 operands.push_back(fnTypeID);
326 encodeInstructionInto(functionHeader, spirv::Opcode::OpFunction, operands);
327
328 if (!resultTypes.empty()) {
329 if (Attribute attr = op.getResultAttr(0, spirv::getFuncParamAttrName()))
330 if (failed(processDecorationAttr(op.getLoc(), funcID,
331 Decoration::FuncParamAttr, attr)))
332 return failure();
333 }
334
335 // Add function name.
336 if (failed(processName(funcID, op.getName()))) {
337 return failure();
338 }
339 // Handle external functions with linkage_attributes(LinkageAttributes)
340 // differently.
341 auto linkageAttr = op.getLinkageAttributes();
342 auto hasImportLinkage =
343 linkageAttr && (linkageAttr.value().getLinkageType().getValue() ==
344 spirv::LinkageType::Import);
345 if (op.isExternal() && !hasImportLinkage) {
346 return op.emitError(
347 "'spirv.module' cannot contain external functions "
348 "without 'Import' linkage_attributes (LinkageAttributes)");
349 }
350 if (op.isExternal() && hasImportLinkage) {
351 // Add an entry block to set up the block arguments
352 // to match the signature of the function.
353 // This is to generate OpFunctionParameter for functions with
354 // LinkageAttributes.
355 // WARNING: This operation has side-effect, it essentially adds a body
356 // to the func. Hence, making it not external anymore (isExternal()
357 // is going to return false for this function from now on)
358 // Hence, we'll remove the body once we are done with the serialization.
359 op.addEntryBlock();
360 if (failed(processFuncParameter(op)))
361 return failure();
362
363 // Erasing the body of the function destroys arguments, so we need to remove
364 // them from the map to avoid problems when processing invalid values used
365 // as keys. We have already serialized function arguments so we probably can
366 // remove them from the map as external function will not have any uses.
367 for (Value arg : op.getArguments())
368 valueIDMap.erase(arg);
369
370 // Don't need to process the added block, there is nothing to process,
371 // the fake body was added just to get the arguments, remove the body,
372 // since it's use is done.
373 op.eraseBody();
374 } else {
375 if (failed(processFuncParameter(op)))
376 return failure();
377
378 // Some instructions (e.g., OpVariable) in a function must be in the first
379 // block in the function. These instructions will be put in
380 // functionHeader. Thus, we put the label in functionHeader first, and
381 // omit it from the first block. OpLabel only needs to be added for
382 // functions with body (including empty body). Since, we added a fake body
383 // for functions with 'Import' Linkage attributes, these functions are
384 // essentially function delcaration, so they should not have OpLabel and a
385 // terminating instruction. That's why we skipped it for those functions.
386 encodeInstructionInto(functionHeader, spirv::Opcode::OpLabel,
387 {getOrCreateBlockID(&op.front())});
388 if (failed(processBlock(&op.front(), /*omitLabel=*/true)))
389 return failure();
391 &op.front(), [&](Block *block) { return processBlock(block); },
392 /*skipHeader=*/true))) {
393 return failure();
394 }
395
396 // There might be OpPhi instructions who have value references needing to
397 // fix.
398 for (const auto &deferredValue : deferredPhiValues) {
399 Value value = deferredValue.first;
400 uint32_t id = getValueID(value);
401 LLVM_DEBUG(llvm::dbgs() << "[phi] fix reference of value " << value
402 << " to id = " << id << '\n');
403 assert(id && "OpPhi references undefined value!");
404 for (size_t offset : deferredValue.second)
405 functionBody[offset] = id;
406 }
407 deferredPhiValues.clear();
408 }
409 LLVM_DEBUG(llvm::dbgs() << "-- completed function '" << op.getName()
410 << "' --\n");
411 // Insert Decorations based on Function Attributes.
412 // Only attributes we should be considering for decoration are the
413 // ::mlir::spirv::Decoration attributes.
414
415 NamedAttrList attrs(op->getDiscardableAttrDictionary().getValue());
416 op->getName().populateInherentAttrs(op, attrs);
417 for (auto attr : attrs) {
418 // Only generate OpDecorate op for spirv::Decoration attributes.
419 auto isValidDecoration = mlir::spirv::symbolizeEnum<spirv::Decoration>(
420 llvm::convertToCamelFromSnakeCase(attr.getName().strref(),
421 /*capitalizeFirst=*/true));
422 if (isValidDecoration != std::nullopt) {
423 if (failed(processDecoration(op.getLoc(), funcID, attr))) {
424 return failure();
425 }
426 }
427 }
428 // Insert OpFunctionEnd.
429 encodeInstructionInto(functionBody, spirv::Opcode::OpFunctionEnd, {});
431 functions.append(functionHeader.begin(), functionHeader.end());
432 functions.append(functionBody.begin(), functionBody.end());
433 functionHeader.clear();
434 functionBody.clear();
435 valueIDMap.clear();
436 blockIDMap.clear();
437
438 return success();
439}
440
441uint32_t Serializer::encodeDebugStringInst(StringRef str) {
442 uint32_t stringID = debugStringIDMap.lookup(str);
443 if (stringID > 0) {
444 return stringID;
445 }
446
448 stringID = getNextID();
449 debugStringIDMap[str] = stringID;
450 operands.push_back(stringID);
451 spirv::encodeStringLiteralInto(operands, str);
452 encodeInstructionInto(debug, spirv::Opcode::OpString, operands);
453
454 return stringID;
455}
456
457LogicalResult Serializer::encodeDebugInfoGraphInst(spirv::GraphARMOp op,
458 uint32_t &debugGraphID) {
459 if (!options.emitDebugInfo)
460 return success();
461
462 uint32_t voidTypeID = 0;
463 if (failed(processType(op.getLoc(), getVoidType(), voidTypeID)))
464 return failure();
465
466 uint32_t stringID =
467 encodeDebugStringInst(getDebugInfoStringFromLoc(op.getLoc()));
470 operands.push_back(voidTypeID);
471 debugGraphID = getNextID();
472 operands.push_back(debugGraphID);
473 uint32_t graphID = getOrCreateFunctionID(op.getName());
474 operands.push_back(graphID);
475 operands.push_back(stringID);
476
477 if (failed(encodeExtensionInstruction(
478 nullptr, extGraphDebugInfo,
479 static_cast<uint32_t>(GraphDebugInfoExtInst::DebugGraph), operands,
480 graphsDebugInfo)))
481 return failure();
482
483 return success();
484}
485
486LogicalResult
487Serializer::encodeDebugInfoOperationInst(uint32_t debugGraphID,
488 const SetVector<Operation *> &ops) {
489 if (!options.emitDebugInfo)
490 return success();
491
492 if (ops.empty())
493 return success();
494
495 SmallVector<uint32_t, 4> instructionIDs;
496 for (Operation *op : ops)
497 for (OpResult result : op->getOpResults())
498 instructionIDs.push_back(getValueID(result));
499
500 if (instructionIDs.empty())
501 return success();
502
503 uint32_t voidTypeID = 0;
504 if (failed(processType(ops[0]->getLoc(), getVoidType(), voidTypeID)))
505 return failure();
506
507 uint32_t stringID =
508 encodeDebugStringInst(getDebugInfoStringFromLoc(ops[0]->getLoc()));
509
511 operands.push_back(voidTypeID);
512 operands.push_back(getNextID());
513 operands.push_back(debugGraphID);
514 operands.push_back(stringID);
515 operands.append(instructionIDs);
516
517 if (failed(encodeExtensionInstruction(
518 nullptr, extGraphDebugInfo,
519 static_cast<uint32_t>(GraphDebugInfoExtInst::DebugOperation),
520 operands, graphsDebugInfo)))
521 return failure();
522
523 return success();
524}
525
526LogicalResult Serializer::encodeDebugInfoTensorInst(Value tensor) {
527 if (!options.emitDebugInfo)
528 return success();
529
530 uint32_t voidTypeID = 0;
531 if (failed(processType(tensor.getLoc(), getVoidType(), voidTypeID)))
532 return failure();
533
534 uint32_t tensorID = valueIDMap.lookup(tensor);
535 if (tensorID == 0)
536 return success();
537
538 uint32_t stringID =
539 encodeDebugStringInst(getDebugInfoStringFromLoc(tensor.getLoc()));
540
542 operands.push_back(voidTypeID);
543 operands.push_back(getNextID());
544 operands.push_back(tensorID);
545 operands.push_back(stringID);
546
547 if (failed(encodeExtensionInstruction(
548 nullptr, extGraphDebugInfo,
549 static_cast<uint32_t>(GraphDebugInfoExtInst::DebugTensor), operands,
550 graphsDebugInfo)))
551 return failure();
552
553 return success();
554}
555
556LogicalResult Serializer::processGraphARMOp(spirv::GraphARMOp op) {
557 if (op.getNumResults() < 1) {
558 return op.emitError("cannot serialize graph with no return types");
559 }
560
561 LLVM_DEBUG(llvm::dbgs() << "-- start graph '" << op.getName() << "' --\n");
562 assert(functionHeader.empty() && functionBody.empty());
563
564 uint32_t funcID = getOrCreateFunctionID(op.getName());
565 uint32_t fnTypeID = 0;
566 // Generate type of the function.
567 if (failed(processType(op.getLoc(), op.getFunctionType(), fnTypeID)))
568 return failure();
569 encodeInstructionInto(functionHeader, spirv::Opcode::OpGraphARM,
570 {fnTypeID, funcID});
571
572 // Declare the parameters.
573 for (auto [idx, arg] : llvm::enumerate(op.getArguments())) {
574 uint32_t argTypeID = 0;
575 SmallVector<uint32_t, 3> inputOperands;
576
577 if (failed(processType(op.getLoc(), arg.getType(), argTypeID))) {
578 return failure();
579 }
580
581 uint32_t argValueID = getNextID();
582 valueIDMap[arg] = argValueID;
583
584 auto attr = IntegerAttr::get(IntegerType::get(op.getContext(), 32), idx);
585 uint32_t indexID = prepareConstantInt(op.getLoc(), attr, false);
586
587 inputOperands.push_back(argTypeID);
588 inputOperands.push_back(argValueID);
589 inputOperands.push_back(indexID);
590
591 encodeInstructionInto(functionHeader, spirv::Opcode::OpGraphInputARM,
592 inputOperands);
593
594 if (failed(encodeDebugInfoTensorInst(arg)))
595 return failure();
596 }
597
598 if (failed(processBlock(&op.front(), /*omitLabel=*/true)))
599 return failure();
601 &op.front(), [&](Block *block) { return processBlock(block); },
602 /*skipHeader=*/true))) {
603 return failure();
604 }
605
606 LLVM_DEBUG(llvm::dbgs() << "-- completed graph '" << op.getName()
607 << "' --\n");
608 // Insert OpGraphEndARM.
609 encodeInstructionInto(functionBody, spirv::Opcode::OpGraphEndARM, {});
610
611 llvm::append_range(graphs, functionHeader);
612 llvm::append_range(graphs, functionBody);
613 functionHeader.clear();
614 functionBody.clear();
615
616 uint32_t debugGraphID = 0;
617 if (failed(encodeDebugInfoGraphInst(op, debugGraphID)))
618 return failure();
619
620 for (const auto &debugEntry : tosaOpsMap[funcID]) {
621 if (failed(encodeDebugInfoOperationInst(debugGraphID, debugEntry.second)))
622 return failure();
623 }
624
625 return success();
626}
627
628LogicalResult
629Serializer::processGraphEntryPointARMOp(spirv::GraphEntryPointARMOp op) {
631 StringRef graph = op.getFn();
632 // Add the graph <id>.
633 uint32_t graphID = getOrCreateFunctionID(graph);
634 operands.push_back(graphID);
635 // Add the name of the graph.
636 spirv::encodeStringLiteralInto(operands, graph);
637
638 // Add the interface values.
639 if (ArrayAttr interface = op.getInterface()) {
640 for (Attribute var : interface.getValue()) {
641 StringRef value = cast<FlatSymbolRefAttr>(var).getValue();
642 if (uint32_t id = getVariableID(value)) {
643 operands.push_back(id);
644 } else {
645 return op.emitError(
646 "referencing undefined global variable."
647 "spirv.GraphEntryPointARM is at the end of spirv.module. All "
648 "referenced variables should already be defined");
649 }
650 }
651 }
652 encodeInstructionInto(graphs, spirv::Opcode::OpGraphEntryPointARM, operands);
653 return success();
654}
655
656LogicalResult
657Serializer::processGraphOutputsARMOp(spirv::GraphOutputsARMOp op) {
658 for (auto [idx, value] : llvm::enumerate(op->getOperands())) {
659 SmallVector<uint32_t, 2> outputOperands;
660
661 Type resType = value.getType();
662 uint32_t resTypeID = 0;
663 if (failed(processType(op.getLoc(), resType, resTypeID))) {
664 return failure();
665 }
666
667 uint32_t outputID = getValueID(value);
668 auto attr = IntegerAttr::get(IntegerType::get(op.getContext(), 32), idx);
669 uint32_t indexID = prepareConstantInt(op.getLoc(), attr, false);
670
671 outputOperands.push_back(outputID);
672 outputOperands.push_back(indexID);
673
674 if (failed(encodeDebugInfoTensorInst(value)))
675 return failure();
676
677 encodeInstructionInto(functionBody, spirv::Opcode::OpGraphSetOutputARM,
678 outputOperands);
679 }
680 return success();
681}
682
683LogicalResult Serializer::processVariableOp(spirv::VariableOp op) {
685 SmallVector<StringRef, 2> elidedAttrs;
686 uint32_t resultID = 0;
687 uint32_t resultTypeID = 0;
688 if (failed(processType(op.getLoc(), op.getType(), resultTypeID))) {
689 return failure();
690 }
691 operands.push_back(resultTypeID);
692 resultID = getNextID();
693 valueIDMap[op.getResult()] = resultID;
694 operands.push_back(resultID);
695 operands.push_back(static_cast<uint32_t>(op.getStorageClass()));
696 elidedAttrs.push_back(spirv::attributeName<spirv::StorageClass>());
697 for (auto arg : op.getODSOperands(0)) {
698 auto argID = getValueID(arg);
699 if (!argID) {
700 return emitError(op.getLoc(), "operand 0 has a use before def");
701 }
702 operands.push_back(argID);
703 }
704 if (failed(emitDebugLine(functionHeader, op.getLoc())))
705 return failure();
706 encodeInstructionInto(functionHeader, spirv::Opcode::OpVariable, operands);
707 for (auto attr : op->getDiscardableAttrDictionary().getValue()) {
708 if (llvm::any_of(elidedAttrs, [&](StringRef elided) {
709 return attr.getName() == elided;
710 })) {
711 continue;
712 }
713 if (failed(processDecoration(op.getLoc(), resultID, attr))) {
714 return failure();
715 }
716 }
717 return success();
718}
719
720LogicalResult
721Serializer::processGlobalVariableOp(spirv::GlobalVariableOp varOp) {
722 // Get TypeID.
723 uint32_t resultTypeID = 0;
724 SmallVector<StringRef, 4> elidedAttrs;
725 if (failed(processType(varOp.getLoc(), varOp.getType(), resultTypeID))) {
726 return failure();
727 }
728
729 elidedAttrs.push_back("type");
731 operands.push_back(resultTypeID);
732 auto resultID = getNextID();
733
734 // Encode the name.
735 auto varName = varOp.getSymName();
736 elidedAttrs.push_back(varOp.getSymNameAttrName());
737 if (failed(processName(resultID, varName))) {
738 return failure();
739 }
740 globalVarIDMap[varName] = resultID;
741 operands.push_back(resultID);
742
743 // Encode StorageClass.
744 operands.push_back(static_cast<uint32_t>(varOp.storageClass()));
745
746 // Encode the data type of an untyped variable.
747 if (std::optional<Type> dataType = varOp.getDataType()) {
748 uint32_t dataTypeID = 0;
749 if (failed(processType(varOp.getLoc(), *dataType, dataTypeID)))
750 return failure();
751 operands.push_back(dataTypeID);
752 }
753 elidedAttrs.push_back(varOp.getDataTypeAttrName());
754
755 // Encode initialization.
756 StringRef initAttrName = varOp.getInitializerAttrName().getValue();
757 if (std::optional<StringRef> initSymbolName = varOp.getInitializer()) {
758 uint32_t initializerID = 0;
759 FlatSymbolRefAttr initRef = varOp.getInitializerAttr();
760 Operation *initOp = SymbolTable::lookupNearestSymbolFrom(
761 varOp->getParentOp(), initRef.getAttr());
762
763 // Check if initializer is GlobalVariable or SpecConstant* cases.
764 if (isa<spirv::GlobalVariableOp>(initOp))
765 initializerID = getVariableID(*initSymbolName);
766 else
767 initializerID = getSpecConstID(*initSymbolName);
768
769 if (!initializerID)
770 return emitError(varOp.getLoc(),
771 "invalid usage of undefined variable as initializer");
772
773 operands.push_back(initializerID);
774 elidedAttrs.push_back(initAttrName);
775 }
776
777 if (failed(emitDebugLine(typesGlobalValues, varOp.getLoc())))
778 return failure();
779 spirv::Opcode opcode = isa<spirv::UntypedPointerType>(varOp.getType())
780 ? spirv::Opcode::OpUntypedVariableKHR
781 : spirv::Opcode::OpVariable;
782 encodeInstructionInto(typesGlobalValues, opcode, operands);
783 elidedAttrs.push_back(initAttrName);
784
785 // Encode decorations.
786 NamedAttrList attrs(varOp->getDiscardableAttrDictionary().getValue());
787 varOp->getName().populateInherentAttrs(varOp, attrs);
788 for (auto attr : attrs) {
789 if (llvm::any_of(elidedAttrs, [&](StringRef elided) {
790 return attr.getName() == elided;
791 })) {
792 continue;
793 }
794 if (failed(processDecoration(varOp.getLoc(), resultID, attr))) {
795 return failure();
796 }
797 }
798 return success();
799}
800
801LogicalResult Serializer::processSelectionOp(spirv::SelectionOp selectionOp) {
802 if (selectionOp.getBody().empty())
803 return success();
804
805 // Assign <id>s to all blocks so that branches inside the SelectionOp can
806 // resolve properly.
807 auto &body = selectionOp.getBody();
808 for (Block &block : body)
809 getOrCreateBlockID(&block);
810
811 auto *headerBlock = selectionOp.getHeaderBlock();
812 auto *mergeBlock = selectionOp.getMergeBlock();
813 auto headerID = getBlockID(headerBlock);
814 auto mergeID = getBlockID(mergeBlock);
815 auto loc = selectionOp.getLoc();
816
817 // Before we do anything replace results of the selection operation with
818 // values yielded (with `mlir.merge`) from inside the region. The selection op
819 // is being flattened so we do not have to worry about values being defined
820 // inside a region and used outside it anymore.
821 auto mergeOp = cast<spirv::MergeOp>(mergeBlock->back());
822 assert(selectionOp.getNumResults() == mergeOp.getNumOperands());
823 for (unsigned i = 0, e = selectionOp.getNumResults(); i != e; ++i)
824 selectionOp.getResult(i).replaceAllUsesWith(mergeOp.getOperand(i));
825
826 // This SelectionOp is in some MLIR block with preceding and following ops. In
827 // the binary format, it should reside in separate SPIR-V blocks from its
828 // preceding and following ops. So we need to emit unconditional branches to
829 // jump to this SelectionOp's SPIR-V blocks and jumping back to the normal
830 // flow afterwards.
831 encodeInstructionInto(functionBody, spirv::Opcode::OpBranch, {headerID});
832
833 // Emit the selection header block, which dominates all other blocks, first.
834 // We need to emit an OpSelectionMerge instruction before the selection header
835 // block's terminator.
836 auto emitSelectionMerge = [&]() {
837 if (failed(emitDebugLine(functionBody, loc)))
838 return failure();
839 lastProcessedWasMergeInst = true;
841 functionBody, spirv::Opcode::OpSelectionMerge,
842 {mergeID, static_cast<uint32_t>(selectionOp.getSelectionControl())});
843 return success();
844 };
845 if (failed(
846 processBlock(headerBlock, /*omitLabel=*/false, emitSelectionMerge)))
847 return failure();
848
849 // Process all blocks with a depth-first visitor starting from the header
850 // block. The selection header block and merge block are skipped by this
851 // visitor.
853 headerBlock, [&](Block *block) { return processBlock(block); },
854 /*skipHeader=*/true, /*skipBlocks=*/{mergeBlock})))
855 return failure();
856
857 // There is nothing to do for the merge block in the selection, which just
858 // contains a spirv.mlir.merge op, itself. But we need to have an OpLabel
859 // instruction to start a new SPIR-V block for ops following this SelectionOp.
860 // The block should use the <id> for the merge block.
861 encodeInstructionInto(functionBody, spirv::Opcode::OpLabel, {mergeID});
862
863 // We do not process the mergeBlock but we still need to generate phi
864 // functions from its block arguments.
865 if (failed(emitPhiForBlockArguments(mergeBlock)))
866 return failure();
867
868 LLVM_DEBUG(llvm::dbgs() << "done merge ");
869 LLVM_DEBUG(printBlock(mergeBlock, llvm::dbgs()));
870 LLVM_DEBUG(llvm::dbgs() << "\n");
871 return success();
872}
873
874LogicalResult Serializer::processLoopOp(spirv::LoopOp loopOp) {
875 if (loopOp.getBody().empty())
876 return success();
877
878 // Assign <id>s to all blocks so that branches inside the LoopOp can resolve
879 // properly. We don't need to assign for the entry block, which is just for
880 // satisfying MLIR region's structural requirement.
881 auto &body = loopOp.getBody();
882 for (Block &block : llvm::drop_begin(body))
883 getOrCreateBlockID(&block);
884
885 auto *headerBlock = loopOp.getHeaderBlock();
886 auto *continueBlock = loopOp.getContinueBlock();
887 auto *mergeBlock = loopOp.getMergeBlock();
888 auto headerID = getBlockID(headerBlock);
889 auto continueID = getBlockID(continueBlock);
890 auto mergeID = getBlockID(mergeBlock);
891 auto loc = loopOp.getLoc();
892
893 // Before we do anything replace results of the selection operation with
894 // values yielded (with `mlir.merge`) from inside the region.
895 auto mergeOp = cast<spirv::MergeOp>(mergeBlock->back());
896 assert(loopOp.getNumResults() == mergeOp.getNumOperands());
897 for (unsigned i = 0, e = loopOp.getNumResults(); i != e; ++i)
898 loopOp.getResult(i).replaceAllUsesWith(mergeOp.getOperand(i));
899
900 // This LoopOp is in some MLIR block with preceding and following ops. In the
901 // binary format, it should reside in separate SPIR-V blocks from its
902 // preceding and following ops. So we need to emit unconditional branches to
903 // jump to this LoopOp's SPIR-V blocks and jumping back to the normal flow
904 // afterwards.
905 encodeInstructionInto(functionBody, spirv::Opcode::OpBranch, {headerID});
906
907 // LoopOp's entry block is just there for satisfying MLIR's structural
908 // requirements so we omit it and start serialization from the loop header
909 // block.
910
911 // Emit the loop header block, which dominates all other blocks, first. We
912 // need to emit an OpLoopMerge instruction before the loop header block's
913 // terminator.
914 auto emitLoopMerge = [&]() {
915 if (failed(emitDebugLine(functionBody, loc)))
916 return failure();
917 lastProcessedWasMergeInst = true;
919 functionBody, spirv::Opcode::OpLoopMerge,
920 {mergeID, continueID, static_cast<uint32_t>(loopOp.getLoopControl())});
921 return success();
922 };
923 if (failed(processBlock(headerBlock, /*omitLabel=*/false, emitLoopMerge)))
924 return failure();
925
926 // Process all blocks with a depth-first visitor starting from the header
927 // block. The loop header block, loop continue block, and loop merge block are
928 // skipped by this visitor and handled later in this function.
930 headerBlock, [&](Block *block) { return processBlock(block); },
931 /*skipHeader=*/true, /*skipBlocks=*/{continueBlock, mergeBlock})))
932 return failure();
933
934 // We have handled all other blocks. Now get to the loop continue block.
935 if (failed(processBlock(continueBlock)))
936 return failure();
937
938 // There is nothing to do for the merge block in the loop, which just contains
939 // a spirv.mlir.merge op, itself. But we need to have an OpLabel instruction
940 // to start a new SPIR-V block for ops following this LoopOp. The block should
941 // use the <id> for the merge block.
942 encodeInstructionInto(functionBody, spirv::Opcode::OpLabel, {mergeID});
943 LLVM_DEBUG(llvm::dbgs() << "done merge ");
944 LLVM_DEBUG(printBlock(mergeBlock, llvm::dbgs()));
945 LLVM_DEBUG(llvm::dbgs() << "\n");
946 return success();
947}
948
949LogicalResult Serializer::processBranchConditionalOp(
950 spirv::BranchConditionalOp condBranchOp) {
951 auto conditionID = getValueID(condBranchOp.getCondition());
952 auto trueLabelID = getOrCreateBlockID(condBranchOp.getTrueBlock());
953 auto falseLabelID = getOrCreateBlockID(condBranchOp.getFalseBlock());
954 SmallVector<uint32_t, 5> arguments{conditionID, trueLabelID, falseLabelID};
956 if (auto weights = condBranchOp.getBranchWeights()) {
957 for (auto val : weights->getValue())
958 arguments.push_back(cast<IntegerAttr>(val).getInt());
959 }
961 if (failed(emitDebugLine(functionBody, condBranchOp.getLoc())))
962 return failure();
963 encodeInstructionInto(functionBody, spirv::Opcode::OpBranchConditional,
964 arguments);
965 return success();
966}
967
968LogicalResult Serializer::processBranchOp(spirv::BranchOp branchOp) {
969 if (failed(emitDebugLine(functionBody, branchOp.getLoc())))
970 return failure();
971 encodeInstructionInto(functionBody, spirv::Opcode::OpBranch,
972 {getOrCreateBlockID(branchOp.getTarget())});
973 return success();
974}
976LogicalResult Serializer::processSwitchOp(spirv::SwitchOp switchOp) {
977 Value selector = switchOp.getSelector();
978 uint32_t selectorID = getValueID(selector);
979 uint32_t defaultLabelID = getOrCreateBlockID(switchOp.getDefaultTarget());
980 SmallVector<uint32_t> arguments{selectorID, defaultLabelID};
981
982 std::optional<mlir::DenseIntElementsAttr> literals = switchOp.getLiterals();
983 BlockRange targets = switchOp.getTargets();
984 if (literals) {
985 Type selectorType = selector.getType();
986 unsigned bitWidth = selectorType.getIntOrFloatBitWidth();
987 bool isSigned = selectorType.isSignedInteger();
988 for (auto [literal, target] : llvm::zip_equal(*literals, targets)) {
989 if (bitWidth <= 32) {
990 // Sub-word literals occupy the low-order bits of the word and are
991 // sign-extended for signed selector types, zero-extended otherwise.
992 arguments.push_back(
993 isSigned ? static_cast<uint32_t>(literal.getSExtValue())
994 : static_cast<uint32_t>(literal.getZExtValue()));
995 } else {
996 // Wider literals span multiple words, low-order word first.
997 for (unsigned offset = 0; offset < bitWidth; offset += 32)
998 arguments.push_back(literal.extractBitsAsZExtValue(32, offset));
999 }
1000 uint32_t targetLabelID = getOrCreateBlockID(target);
1001 arguments.push_back(targetLabelID);
1002 }
1003 }
1004
1005 if (failed(emitDebugLine(functionBody, switchOp.getLoc())))
1006 return failure();
1007 encodeInstructionInto(functionBody, spirv::Opcode::OpSwitch, arguments);
1008 return success();
1009}
1011LogicalResult Serializer::processAddressOfOp(spirv::AddressOfOp addressOfOp) {
1012 auto varName = addressOfOp.getVariable();
1013 auto variableID = getVariableID(varName);
1014 if (!variableID) {
1015 return addressOfOp.emitError("unknown result <id> for variable ")
1016 << varName;
1017 }
1018 valueIDMap[addressOfOp.getPointer()] = variableID;
1019 return success();
1021
1022LogicalResult
1023Serializer::processReferenceOfOp(spirv::ReferenceOfOp referenceOfOp) {
1024 auto constName = referenceOfOp.getSpecConst();
1025 auto constID = getSpecConstID(constName);
1026 if (!constID) {
1027 return referenceOfOp.emitError(
1028 "unknown result <id> for specialization constant ")
1029 << constName;
1031 valueIDMap[referenceOfOp.getReference()] = constID;
1032 return success();
1033}
1034
1035template <>
1036LogicalResult
1037Serializer::processOp<spirv::EntryPointOp>(spirv::EntryPointOp op) {
1038 SmallVector<uint32_t, 4> operands;
1039 // Add the ExecutionModel.
1040 operands.push_back(static_cast<uint32_t>(op.getExecutionModel()));
1041 // Add the function <id>.
1042 auto funcID = getFunctionID(op.getFn());
1043 if (!funcID) {
1044 return op.emitError("missing <id> for function ")
1045 << op.getFn()
1046 << "; function needs to be defined before spirv.EntryPoint is "
1047 "serialized";
1048 }
1049 operands.push_back(funcID);
1050 // Add the name of the function.
1051 spirv::encodeStringLiteralInto(operands, op.getFn());
1052
1053 // Add the interface values.
1054 if (auto interface = op.getInterface()) {
1055 for (auto var : interface.getValue()) {
1056 auto id = getVariableID(cast<FlatSymbolRefAttr>(var).getValue());
1057 if (!id) {
1058 return op.emitError(
1059 "referencing undefined global variable."
1060 "spirv.EntryPoint is at the end of spirv.module. All "
1061 "referenced variables should already be defined");
1062 }
1063 operands.push_back(id);
1064 }
1066 encodeInstructionInto(entryPoints, spirv::Opcode::OpEntryPoint, operands);
1067 return success();
1068}
1069
1070template <>
1071LogicalResult
1072Serializer::processOp<spirv::ExecutionModeOp>(spirv::ExecutionModeOp op) {
1073 SmallVector<uint32_t, 4> operands;
1074 // Add the function <id>.
1075 auto funcID = getFunctionID(op.getFn());
1076 if (!funcID) {
1077 return op.emitError("missing <id> for function ")
1078 << op.getFn()
1079 << "; function needs to be serialized before ExecutionModeOp is "
1080 "serialized";
1081 }
1082 operands.push_back(funcID);
1083 // Add the ExecutionMode.
1084 operands.push_back(static_cast<uint32_t>(op.getExecutionMode()));
1086 // Serialize values if any.
1087 auto values = op.getValues();
1088 if (values) {
1089 for (auto &intVal : values.getValue()) {
1090 operands.push_back(static_cast<uint32_t>(
1091 cast<IntegerAttr>(intVal).getValue().getZExtValue()));
1092 }
1093 }
1094 encodeInstructionInto(executionModes, spirv::Opcode::OpExecutionMode,
1095 operands);
1096 return success();
1097}
1098
1099template <>
1100LogicalResult
1101Serializer::processOp<spirv::ExecutionModeIdOp>(spirv::ExecutionModeIdOp op) {
1102 SmallVector<uint32_t, 4> operands;
1103 // Add the function <id>.
1104 uint32_t funcID = getFunctionID(op.getFn());
1105 if (!funcID)
1106 return op.emitError("missing <id> for function ")
1107 << op.getFn()
1108 << "; function needs to be serialized before ExecutionModeIdOp is "
1109 "serialized";
1111 operands.push_back(funcID);
1112 operands.push_back(static_cast<uint32_t>(op.getExecutionMode()));
1113
1114 for (Attribute value : op.getValues().getValue()) {
1115 uint32_t id = 0;
1116 if (auto typeAttr = dyn_cast<TypeAttr>(value)) {
1117 if (failed(processType(op.getLoc(), typeAttr.getValue(), id)))
1118 return failure();
1119 } else if (auto refVal = dyn_cast<FlatSymbolRefAttr>(value)) {
1120 id = getSpecConstID(refVal.getValue());
1121 if (!id)
1122 return op.emitError("unknown <id> for specialization constant ")
1123 << refVal.getValue();
1124 } else {
1125 id = prepareConstantScalar(op.getLoc(), value);
1126 if (!id)
1127 return op.emitError("unsupported value operand: ") << value;
1128 }
1129 operands.push_back(id);
1131 encodeInstructionInto(executionModes, spirv::Opcode::OpExecutionModeId,
1132 operands);
1133 return success();
1134}
1136template <>
1137LogicalResult Serializer::processOp<spirv::INTELConstantFunctionPointerOp>(
1138 spirv::INTELConstantFunctionPointerOp op) {
1139 uint32_t &id = functionPointerIDMap[op.getFunction()];
1140 if (!id) {
1141 id = getNextID();
1142 uint32_t typeID = 0;
1143 if (failed(processType(op.getLoc(), op.getType(), typeID)))
1144 return failure();
1146 typesGlobalValues, spirv::Opcode::OpConstantFunctionPointerINTEL,
1147 {typeID, id, getOrCreateFunctionID(op.getFunction())});
1148 }
1149 valueIDMap[op.getResult()] = id;
1150 return success();
1151}
1152
1153template <>
1154LogicalResult
1155Serializer::processOp<spirv::FunctionCallOp>(spirv::FunctionCallOp op) {
1156 auto funcName = op.getCallee();
1157 uint32_t resTypeID = 0;
1158
1159 Type resultTy = op.getNumResults() ? *op.result_type_begin() : getVoidType();
1160 if (failed(processType(op.getLoc(), resultTy, resTypeID)))
1161 return failure();
1162
1163 auto funcID = getOrCreateFunctionID(funcName);
1164 auto funcCallID = getNextID();
1165 SmallVector<uint32_t, 8> operands{resTypeID, funcCallID, funcID};
1166
1167 for (auto value : op.getArguments()) {
1168 auto valueID = getValueID(value);
1169 assert(valueID && "cannot find a value for spirv.FunctionCall");
1170 operands.push_back(valueID);
1171 }
1172
1173 if (!isa<NoneType>(resultTy))
1174 valueIDMap[op.getResult(0)] = funcCallID;
1175
1176 encodeInstructionInto(functionBody, spirv::Opcode::OpFunctionCall, operands);
1177 for (auto attr : op->getDiscardableAttrs())
1178 if (failed(processDecoration(op.getLoc(), funcCallID, attr)))
1179 return failure();
1180 return success();
1181}
1182
1183template <>
1184LogicalResult Serializer::processOp<spirv::INTELFunctionPointerCallOp>(
1185 spirv::INTELFunctionPointerCallOp op) {
1186 uint32_t resTypeID = 0;
1187 Value returnValue = op.getReturnValue();
1188 Type resultTy = returnValue ? returnValue.getType() : getVoidType();
1189 if (failed(processType(op.getLoc(), resultTy, resTypeID)))
1190 return failure();
1191
1192 uint32_t callID = getNextID();
1193 SmallVector<uint32_t, 8> operands{resTypeID, callID};
1194 for (Value value : op->getOperands()) {
1195 uint32_t valueID = getValueID(value);
1196 assert(valueID &&
1197 "cannot find a value for spirv.INTEL.FunctionPointerCall");
1198 operands.push_back(valueID);
1200
1201 if (returnValue)
1202 valueIDMap[returnValue] = callID;
1203
1204 encodeInstructionInto(functionBody, spirv::Opcode::OpFunctionPointerCallINTEL,
1205 operands);
1206 for (NamedAttribute attr : op->getDiscardableAttrs())
1207 if (failed(processDecoration(op.getLoc(), callID, attr)))
1208 return failure();
1209 return success();
1210}
1211
1212template <>
1213LogicalResult
1214Serializer::processOp<spirv::CopyMemoryOp>(spirv::CopyMemoryOp op) {
1215 SmallVector<uint32_t, 4> operands;
1216 SmallVector<StringRef, 2> elidedAttrs;
1217
1218 for (Value operand : op->getOperands()) {
1219 auto id = getValueID(operand);
1220 assert(id && "use before def!");
1221 operands.push_back(id);
1222 }
1223
1224 StringAttr memoryAccess = op.getMemoryAccessAttrName();
1225 if (std::optional<spirv::MemoryAccess> value = op.getMemoryAccess())
1226 operands.push_back(static_cast<uint32_t>(*value));
1227
1228 elidedAttrs.push_back(memoryAccess.strref());
1230 StringAttr alignment = op.getAlignmentAttrName();
1231 if (std::optional<uint32_t> value = op.getAlignment())
1232 operands.push_back(*value);
1233
1234 elidedAttrs.push_back(alignment.strref());
1235
1236 StringAttr sourceMemoryAccess = op.getSourceMemoryAccessAttrName();
1237 if (std::optional<spirv::MemoryAccess> value = op.getSourceMemoryAccess())
1238 operands.push_back(static_cast<uint32_t>(*value));
1240 elidedAttrs.push_back(sourceMemoryAccess.strref());
1241
1242 StringAttr sourceAlignment = op.getSourceAlignmentAttrName();
1243 if (std::optional<uint32_t> value = op.getSourceAlignment())
1244 operands.push_back(*value);
1245
1246 elidedAttrs.push_back(sourceAlignment.strref());
1247 if (failed(emitDebugLine(functionBody, op.getLoc())))
1248 return failure();
1249 encodeInstructionInto(functionBody, spirv::Opcode::OpCopyMemory, operands);
1250
1251 return success();
1252}
1253template <>
1254LogicalResult Serializer::processOp<spirv::GenericCastToPtrExplicitOp>(
1255 spirv::GenericCastToPtrExplicitOp op) {
1256 SmallVector<uint32_t, 4> operands;
1257 Type resultTy;
1258 Location loc = op->getLoc();
1259 uint32_t resultTypeID = 0;
1260 uint32_t resultID = 0;
1261 resultTy = op->getResult(0).getType();
1262 if (failed(processType(loc, resultTy, resultTypeID)))
1263 return failure();
1264 operands.push_back(resultTypeID);
1265
1266 resultID = getNextID();
1267 operands.push_back(resultID);
1268 valueIDMap[op->getResult(0)] = resultID;
1270 for (Value operand : op->getOperands())
1271 operands.push_back(getValueID(operand));
1272 spirv::StorageClass resultStorage =
1273 cast<spirv::PointerType>(resultTy).getStorageClass();
1274 operands.push_back(static_cast<uint32_t>(resultStorage));
1275 encodeInstructionInto(functionBody, spirv::Opcode::OpGenericCastToPtrExplicit,
1276 operands);
1277 return success();
1278}
1280template <>
1281LogicalResult Serializer::processOp<spirv::KHRAbortOp>(spirv::KHRAbortOp op) {
1282 Value message = op.getMessage();
1283 uint32_t messageTypeID = 0;
1284 if (failed(processType(op.getLoc(), message.getType(), messageTypeID)))
1285 return failure();
1286 uint32_t messageID = getValueID(message);
1287 assert(messageID && "use before def!");
1288 if (failed(emitDebugLine(functionBody, op.getLoc())))
1289 return failure();
1290 encodeInstructionInto(functionBody, spirv::Opcode::OpAbortKHR,
1291 {messageTypeID, messageID});
1292 return success();
1293}
1295// Pull in auto-generated Serializer::dispatchToAutogenSerialization() and
1296// various Serializer::processOp<...>() specializations.
1297#define GET_SERIALIZATION_FNS
1298#include "mlir/Dialect/SPIRV/IR/SPIRVSerialization.inc"
1300} // namespace spirv
1301} // namespace mlir
return success()
ArrayAttr()
static llvm::ManagedStatic< PassManagerOptions > options
static LogicalResult visitInPrettyBlockOrder(Block *headerBlock, function_ref< LogicalResult(Block *)> blockHandler, bool skipHeader=false, BlockRange skipBlocks={})
A pre-order depth-first visitor function for processing basic blocks.
static void printBlock(llvm::raw_ostream &os, Block *block, OpPrintingFlags &flags)
Definition Unit.cpp:37
Attributes are known-constant values of operations.
Definition Attributes.h:25
This class provides an abstraction over the different types of ranges over Blocks.
Block represents an ordered list of Operations.
Definition Block.h:34
OpListType & getOperations()
Definition Block.h:162
StringAttr getAttr() const
Returns the name of the held symbol reference as a StringAttr.
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition Location.h:76
NamedAttrList is array of NamedAttributes that tracks whether it is sorted and does some basic work t...
NamedAttribute represents a combination of a name and an Attribute value.
Definition Attributes.h:166
This is a value defined by a result of an operation.
Definition Value.h:456
StringRef getStringRef() const
Return the name of this operation. This always succeeds.
StringRef stripDialect() const
Return the operation name with dialect name stripped, if it has one.
Operation is the basic unit of execution within MLIR.
Definition Operation.h:87
OperationName getName()
The name of an operation is the key identifier for it.
Definition Operation.h:115
operand_range getOperands()
Returns an iterator on the underlying Value's.
Definition Operation.h:405
static Operation * lookupNearestSymbolFrom(Operation *from, StringAttr symbol)
Returns the operation registered with the given symbol name within the closest parent operation of,...
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition Types.h:74
bool isSignedInteger() const
Return true if this is a signed integer type (with the specified width).
Definition Types.cpp:78
unsigned getIntOrFloatBitWidth() const
Return the bit width of an integer or a float type, assert failure on other types.
Definition Types.cpp:124
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition Value.h:96
Type getType() const
Return the type of this value.
Definition Value.h:105
detail::InFlightRemark failed(Location loc, RemarkOpts opts)
Report an optimization remark that failed.
Definition Remarks.h:886
void encodeStringLiteralInto(SmallVectorImpl< uint32_t > &binary, StringRef literal)
Encodes an SPIR-V literal string into the given binary vector.
constexpr llvm::StringLiteral extGraphDebugInfo
Extension set name for non-semantic graph debug info.
void encodeInstructionInto(SmallVectorImpl< uint32_t > &binary, spirv::Opcode op, ArrayRef< uint32_t > operands)
Encodes an SPIR-V instruction with the given opcode and operands into the given binary vector.
StringRef getFuncParamAttrName()
Returns the attribute name for specifying function parameter and result attributes.
constexpr StringRef attributeName()
StringRef getFuncParamAlignmentAttrName()
Returns the attribute name for specifying function parameter alignment.
Include the generated interface declarations.
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.
llvm::SetVector< T, Vector, Set, N > SetVector
Definition LLVM.h:128
llvm::function_ref< Fn > function_ref
Definition LLVM.h:155