MLIR 23.0.0git
ControlFlowOps.cpp
Go to the documentation of this file.
1//===- ControlFlowOps.cpp - MLIR SPIR-V Control Flow 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// Defines the control flow operations in the SPIR-V dialect.
10//
11//===----------------------------------------------------------------------===//
12
17
18#include "llvm/Support/InterleavedRange.h"
19
20#include "SPIRVOpUtils.h"
21#include "SPIRVParsingUtils.h"
22
23using namespace mlir::spirv::AttrNames;
24
25namespace mlir::spirv {
26
27/// Parses Function, Selection and Loop control attributes. If no control is
28/// specified, "None" is used as a default.
29template <typename EnumAttrClass, typename EnumClass>
30static ParseResult
32 StringRef attrName = spirv::attributeName<EnumClass>()) {
33 if (succeeded(parser.parseOptionalKeyword(kControl))) {
34 EnumClass control;
35 if (parser.parseLParen() ||
36 spirv::parseEnumKeywordAttr<EnumAttrClass>(control, parser, state) ||
37 parser.parseRParen())
38 return failure();
39 return success();
40 }
41 // Set control to "None" otherwise.
42 Builder builder = parser.getBuilder();
43 state.addAttribute(attrName,
44 builder.getAttr<EnumAttrClass>(static_cast<EnumClass>(0)));
45 return success();
46}
47
48//===----------------------------------------------------------------------===//
49// spirv.BranchOp
50//===----------------------------------------------------------------------===//
51
52SuccessorOperands BranchOp::getSuccessorOperands(unsigned index) {
53 assert(index == 0 && "invalid successor index");
54 return SuccessorOperands(0, getTargetOperandsMutable());
55}
56
57//===----------------------------------------------------------------------===//
58// spirv.BranchConditionalOp
59//===----------------------------------------------------------------------===//
60
61SuccessorOperands BranchConditionalOp::getSuccessorOperands(unsigned index) {
62 assert(index < 2 && "invalid successor index");
63 return SuccessorOperands(index == kTrueIndex
64 ? getTrueTargetOperandsMutable()
65 : getFalseTargetOperandsMutable());
66}
67
68ParseResult BranchConditionalOp::parse(OpAsmParser &parser,
69 OperationState &result) {
70 auto &builder = parser.getBuilder();
71 OpAsmParser::UnresolvedOperand condInfo;
72 Block *dest;
73
74 // Parse the condition.
75 Type boolTy = builder.getI1Type();
76 if (parser.parseOperand(condInfo) ||
77 parser.resolveOperand(condInfo, boolTy, result.operands))
78 return failure();
79
80 // Parse the optional branch weights.
81 if (succeeded(parser.parseOptionalLSquare())) {
82 IntegerAttr trueWeight, falseWeight;
83 NamedAttrList weights;
84
85 auto i32Type = builder.getIntegerType(32);
86 if (parser.parseAttribute(trueWeight, i32Type, "weight", weights) ||
87 parser.parseComma() ||
88 parser.parseAttribute(falseWeight, i32Type, "weight", weights) ||
89 parser.parseRSquare())
90 return failure();
91
92 StringAttr branchWeightsAttrName =
93 BranchConditionalOp::getBranchWeightsAttrName(result.name);
94 result.addAttribute(branchWeightsAttrName,
95 builder.getArrayAttr({trueWeight, falseWeight}));
96 }
97
98 // Parse the true branch.
99 SmallVector<Value, 4> trueOperands;
100 if (parser.parseComma() ||
101 parser.parseSuccessorAndUseList(dest, trueOperands))
102 return failure();
103 result.addSuccessors(dest);
104 result.addOperands(trueOperands);
105
106 // Parse the false branch.
107 SmallVector<Value, 4> falseOperands;
108 if (parser.parseComma() ||
109 parser.parseSuccessorAndUseList(dest, falseOperands))
110 return failure();
111 result.addSuccessors(dest);
112 result.addOperands(falseOperands);
113 result.addAttribute(spirv::BranchConditionalOp::getOperandSegmentSizeAttr(),
114 builder.getDenseI32ArrayAttr(
115 {1, static_cast<int32_t>(trueOperands.size()),
116 static_cast<int32_t>(falseOperands.size())}));
117
118 return success();
119}
120
121void BranchConditionalOp::print(OpAsmPrinter &printer) {
122 printer << ' ' << getCondition();
123
124 if (std::optional<ArrayAttr> weights = getBranchWeights()) {
125 printer << ' '
126 << llvm::interleaved_array(weights->getAsValueRange<IntegerAttr>());
127 }
128
129 printer << ", ";
130 printer.printSuccessorAndUseList(getTrueBlock(), getTrueBlockArguments());
131 printer << ", ";
132 printer.printSuccessorAndUseList(getFalseBlock(), getFalseBlockArguments());
133}
134
135LogicalResult BranchConditionalOp::verify() {
136 if (auto weights = getBranchWeights()) {
137 if (weights->getValue().size() != 2) {
138 return emitOpError("must have exactly two branch weights");
139 }
140 if (llvm::all_of(*weights, [](Attribute attr) {
141 return cast<IntegerAttr>(attr).getValue().isZero();
142 }))
143 return emitOpError("branch weights cannot both be zero");
144 }
145
146 return success();
147}
148
149//===----------------------------------------------------------------------===//
150// spirv.FunctionCall
151//===----------------------------------------------------------------------===//
152
153LogicalResult FunctionCallOp::verify() {
154 if (getNumResults() > 1) {
155 return emitOpError(
156 "expected callee function to have 0 or 1 result, but provided ")
157 << getNumResults();
158 }
159 return success();
160}
161
162LogicalResult
163FunctionCallOp::verifySymbolUses(SymbolTableCollection &symbolTable) {
164 auto fnName = getCalleeAttr();
165
166 auto funcOp =
167 symbolTable.lookupNearestSymbolFrom<spirv::FuncOp>(*this, fnName);
168 if (!funcOp) {
169 return emitOpError("callee function '")
170 << fnName.getValue() << "' not found in nearest symbol table";
171 }
172
173 auto functionType = funcOp.getFunctionType();
174
175 if (functionType.getNumInputs() != getNumOperands()) {
176 return emitOpError("has incorrect number of operands for callee: expected ")
177 << functionType.getNumInputs() << ", but provided "
178 << getNumOperands();
179 }
180
181 for (uint32_t i = 0, e = functionType.getNumInputs(); i != e; ++i) {
182 if (getOperand(i).getType() != functionType.getInput(i)) {
183 return emitOpError("operand type mismatch: expected operand type ")
184 << functionType.getInput(i) << ", but provided "
185 << getOperand(i).getType() << " for operand number " << i;
186 }
187 }
188
189 if (functionType.getNumResults() != getNumResults()) {
190 return emitOpError(
191 "has incorrect number of results has for callee: expected ")
192 << functionType.getNumResults() << ", but provided "
193 << getNumResults();
194 }
195
196 if (getNumResults() &&
197 (getResult(0).getType() != functionType.getResult(0))) {
198 return emitOpError("result type mismatch: expected ")
199 << functionType.getResult(0) << ", but provided "
200 << getResult(0).getType();
201 }
202
203 return success();
204}
205
206CallInterfaceCallable FunctionCallOp::getCallableForCallee() {
207 return (*this)->getAttrOfType<SymbolRefAttr>(getCalleeAttrName());
208}
209
210void FunctionCallOp::setCalleeFromCallable(CallInterfaceCallable callee) {
211 (*this)->setAttr(getCalleeAttrName(), cast<SymbolRefAttr>(callee));
212}
213
214Operation::operand_range FunctionCallOp::getArgOperands() {
215 return getArguments();
216}
217
218MutableOperandRange FunctionCallOp::getArgOperandsMutable() {
219 return getArgumentsMutable();
220}
221
222//===----------------------------------------------------------------------===//
223// spirv.Switch
224//===----------------------------------------------------------------------===//
225
226void SwitchOp::build(OpBuilder &builder, OperationState &result, Value selector,
227 Block *defaultTarget, ValueRange defaultOperands,
228 DenseIntElementsAttr literals, BlockRange targets,
229 ArrayRef<ValueRange> targetOperands) {
230 build(builder, result, selector, defaultOperands, targetOperands, literals,
231 defaultTarget, targets);
232}
233
234void SwitchOp::build(OpBuilder &builder, OperationState &result, Value selector,
235 Block *defaultTarget, ValueRange defaultOperands,
236 ArrayRef<APInt> literals, BlockRange targets,
237 ArrayRef<ValueRange> targetOperands) {
238 DenseIntElementsAttr literalsAttr;
239 if (!literals.empty()) {
240 ShapedType literalType = VectorType::get(
241 static_cast<int64_t>(literals.size()), selector.getType());
242 literalsAttr = DenseIntElementsAttr::get(literalType, literals);
243 }
244 build(builder, result, selector, defaultTarget, defaultOperands, literalsAttr,
245 targets, targetOperands);
246}
247
248void SwitchOp::build(OpBuilder &builder, OperationState &result, Value selector,
249 Block *defaultTarget, ValueRange defaultOperands,
250 ArrayRef<int32_t> literals, BlockRange targets,
251 ArrayRef<ValueRange> targetOperands) {
252 DenseIntElementsAttr literalsAttr;
253 if (!literals.empty()) {
254 ShapedType literalType = VectorType::get(
255 static_cast<int64_t>(literals.size()), selector.getType());
256 literalsAttr = DenseIntElementsAttr::get(literalType, literals);
257 }
258 build(builder, result, selector, defaultTarget, defaultOperands, literalsAttr,
259 targets, targetOperands);
260}
261
262LogicalResult SwitchOp::verify() {
263 std::optional<DenseIntElementsAttr> literals = getLiterals();
264 BlockRange targets = getTargets();
265
266 if (!literals && targets.empty())
267 return success();
268
269 if (literals) {
270 Type selectorType = getSelector().getType();
271 Type literalType = literals->getType().getElementType();
272 if (literalType != selectorType)
273 return emitOpError() << "'selector' type (" << selectorType
274 << ") should match literals type (" << literalType
275 << ")";
276 }
277
278 if (literals && literals->size() != static_cast<int64_t>(targets.size()))
279 return emitOpError() << "number of literals (" << literals->size()
280 << ") should match number of targets ("
281 << targets.size() << ")";
282 return success();
283}
284
285SuccessorOperands SwitchOp::getSuccessorOperands(unsigned index) {
286 assert(index < getNumSuccessors() && "invalid successor index");
287 return SuccessorOperands(index == 0 ? getDefaultOperandsMutable()
288 : getTargetOperandsMutable(index - 1));
289}
290
291Block *SwitchOp::getSuccessorForOperands(ArrayRef<Attribute> operands) {
292 std::optional<DenseIntElementsAttr> literals = getLiterals();
293
294 if (!literals)
295 return getDefaultTarget();
296
297 SuccessorRange targets = getTargets();
298 if (auto value = dyn_cast_or_null<IntegerAttr>(operands.front())) {
299 for (auto [index, literal] : llvm::enumerate(literals->getValues<APInt>()))
300 if (literal == value.getValue())
301 return targets[index];
302 return getDefaultTarget();
303 }
304 return nullptr;
305}
306
307//===----------------------------------------------------------------------===//
308// spirv.mlir.loop
309//===----------------------------------------------------------------------===//
310
311void LoopOp::build(OpBuilder &builder, OperationState &state) {
312 state.addAttribute("loop_control", builder.getAttr<spirv::LoopControlAttr>(
313 spirv::LoopControl::None));
314 state.addRegion();
315}
316
317ParseResult LoopOp::parse(OpAsmParser &parser, OperationState &result) {
319 result))
320 return failure();
321
322 if (succeeded(parser.parseOptionalArrow()))
323 if (parser.parseTypeList(result.types))
324 return failure();
325
326 return parser.parseRegion(*result.addRegion(), /*arguments=*/{});
327}
328
329void LoopOp::print(OpAsmPrinter &printer) {
330 auto control = getLoopControl();
331 if (control != spirv::LoopControl::None)
332 printer << " control(" << spirv::stringifyLoopControl(control) << ")";
333 if (getNumResults() > 0) {
334 printer << " -> ";
335 printer << getResultTypes();
336 }
337 printer << ' ';
338 printer.printRegion(getRegion(), /*printEntryBlockArgs=*/false,
339 /*printBlockTerminators=*/true);
340}
341
342/// Returns true if the given `srcBlock` contains only one `spirv.Branch` to the
343/// given `dstBlock`.
344static bool hasOneBranchOpTo(Block &srcBlock, Block &dstBlock) {
345 // Check that there is only one op in the `srcBlock`.
346 if (!llvm::hasSingleElement(srcBlock))
347 return false;
348
349 auto branchOp = dyn_cast<spirv::BranchOp>(srcBlock.back());
350 return branchOp && branchOp.getSuccessor() == &dstBlock;
351}
352
353/// Returns true if the given `block` only contains one `spirv.mlir.merge` op.
354static bool isMergeBlock(Block &block) {
355 return llvm::hasSingleElement(block) && isa<spirv::MergeOp>(block.front());
356}
357
358/// Returns true if a `spirv.mlir.merge` op outside the merge block.
359static bool hasOtherMerge(Region &region) {
360 return !region.empty() && llvm::any_of(region.getOps(), [&](Operation &op) {
361 return isa<spirv::MergeOp>(op) && op.getBlock() != &region.back();
362 });
363}
364
365/// Returns true if types yielded by `spirv.mlir.merge` in the region match
366/// those returned by the `op`.
367static bool returnTypesMatch(Region &region, Operation *op) {
368 auto mergeOps = region.getOps<spirv::MergeOp>();
369 Operation *mergeOp = llvm::getSingleElement(mergeOps);
370 return llvm::equal(mergeOp->getOperandTypes(), op->getResultTypes());
371}
372
373LogicalResult LoopOp::verifyRegions() {
374 auto *op = getOperation();
375
376 // We need to verify that the blocks follow the following layout:
377 //
378 // +-------------+
379 // | entry block |
380 // +-------------+
381 // |
382 // v
383 // +-------------+
384 // | loop header | <-----+
385 // +-------------+ |
386 // |
387 // ... |
388 // \ | / |
389 // v |
390 // +---------------+ |
391 // | loop continue | -----+
392 // +---------------+
393 //
394 // ...
395 // \ | /
396 // v
397 // +-------------+
398 // | merge block |
399 // +-------------+
400
401 auto &region = op->getRegion(0);
402 // Allow empty region as a degenerated case, which can come from
403 // optimizations.
404 if (region.empty())
405 return success();
406
407 // The last block is the merge block.
408 Block &merge = region.back();
409 if (!isMergeBlock(merge))
410 return emitOpError("last block must be the merge block with only one "
411 "'spirv.mlir.merge' op");
412 if (hasOtherMerge(region))
413 return emitOpError(
414 "should not have 'spirv.mlir.merge' op outside the merge block");
415
416 if (region.hasOneBlock())
417 return emitOpError(
418 "must have an entry block branching to the loop header block");
419 // The first block is the entry block.
420 Block &entry = region.front();
421
422 if (std::next(region.begin(), 2) == region.end())
423 return emitOpError(
424 "must have a loop header block branched from the entry block");
425 // The second block is the loop header block.
426 Block &header = *std::next(region.begin(), 1);
427
428 if (!hasOneBranchOpTo(entry, header))
429 return emitOpError(
430 "entry block must only have one 'spirv.Branch' op to the second block");
431
432 if (std::next(region.begin(), 3) == region.end())
433 return emitOpError(
434 "requires a loop continue block branching to the loop header block");
435 // The second to last block is the loop continue block.
436 Block &cont = *std::prev(region.end(), 2);
437
438 // Make sure that we have a branch from the loop continue block to the loop
439 // header block.
440 if (llvm::none_of(
441 llvm::seq<unsigned>(0, cont.getNumSuccessors()),
442 [&](unsigned index) { return cont.getSuccessor(index) == &header; }))
443 return emitOpError("second to last block must be the loop continue "
444 "block that branches to the loop header block");
445
446 // Make sure that no other blocks (except the entry and loop continue block)
447 // branches to the loop header block.
448 for (auto &block : llvm::make_range(std::next(region.begin(), 2),
449 std::prev(region.end(), 2))) {
450 for (auto i : llvm::seq<unsigned>(0, block.getNumSuccessors())) {
451 if (block.getSuccessor(i) == &header) {
452 return emitOpError("can only have the entry and loop continue "
453 "block branching to the loop header block");
454 }
455 }
456 }
457
458 if (!returnTypesMatch(region, op))
459 return emitOpError(
460 "result types do not match types yielded with `spirv.mlir.merge`");
461
462 return success();
463}
464
465Block *LoopOp::getEntryBlock() {
466 assert(!getBody().empty() && "op region should not be empty!");
467 return &getBody().front();
468}
469
470Block *LoopOp::getHeaderBlock() {
471 assert(!getBody().empty() && "op region should not be empty!");
472 // The second block is the loop header block.
473 return &*std::next(getBody().begin());
474}
475
476Block *LoopOp::getContinueBlock() {
477 assert(!getBody().empty() && "op region should not be empty!");
478 // The second to last block is the loop continue block.
479 return &*std::prev(getBody().end(), 2);
480}
481
482Block *LoopOp::getMergeBlock() {
483 assert(!getBody().empty() && "op region should not be empty!");
484 // The last block is the loop merge block.
485 return &getBody().back();
486}
487
488void LoopOp::addEntryAndMergeBlock(OpBuilder &builder) {
489 assert(getBody().empty() && "entry and merge block already exist");
490 OpBuilder::InsertionGuard g(builder);
491 builder.createBlock(&getBody());
492 builder.createBlock(&getBody());
493
494 // Add a spirv.mlir.merge op into the merge block.
495 spirv::MergeOp::create(builder, getLoc());
496}
497
498//===----------------------------------------------------------------------===//
499// spirv.Return
500//===----------------------------------------------------------------------===//
501
502LogicalResult ReturnOp::verify() {
503 // Verification is performed in spirv.func op.
504 return success();
505}
506
507//===----------------------------------------------------------------------===//
508// spirv.ReturnValue
509//===----------------------------------------------------------------------===//
510
511LogicalResult ReturnValueOp::verify() {
512 // Verification is performed in spirv.func op.
513 return success();
514}
515
516//===----------------------------------------------------------------------===//
517// spirv.Select
518//===----------------------------------------------------------------------===//
519
520LogicalResult SelectOp::verify() {
521 if (auto conditionTy = dyn_cast<VectorType>(getCondition().getType())) {
522 auto resultVectorTy = dyn_cast<VectorType>(getResult().getType());
523 if (!resultVectorTy) {
524 return emitOpError("result expected to be of vector type when "
525 "condition is of vector type");
526 }
527 if (resultVectorTy.getNumElements() != conditionTy.getNumElements()) {
528 return emitOpError("result should have the same number of elements as "
529 "the condition when condition is of vector type");
530 }
531 }
532 return success();
533}
534
535// Custom availability implementation is needed for spirv.Select given the
536// syntax changes starting v1.4.
537SmallVector<ArrayRef<spirv::Extension>, 1> SelectOp::getExtensions() {
538 return {};
539}
540SmallVector<ArrayRef<spirv::Capability>, 1> SelectOp::getCapabilities() {
541 return {};
542}
543std::optional<spirv::Version> SelectOp::getMinVersion() {
544 // Per the spec, "Before version 1.4, results are only computed per
545 // component."
546 if (isa<spirv::ScalarType>(getCondition().getType()) &&
547 isa<spirv::CompositeType>(getType()))
548 return Version::V_1_4;
549
550 return Version::V_1_0;
551}
552std::optional<spirv::Version> SelectOp::getMaxVersion() {
553 return Version::V_1_6;
554}
555
556//===----------------------------------------------------------------------===//
557// spirv.mlir.selection
558//===----------------------------------------------------------------------===//
559
560ParseResult SelectionOp::parse(OpAsmParser &parser, OperationState &result) {
561 if (parseControlAttribute<spirv::SelectionControlAttr,
562 spirv::SelectionControl>(parser, result))
563 return failure();
564
565 if (succeeded(parser.parseOptionalArrow()))
566 if (parser.parseTypeList(result.types))
567 return failure();
568
569 return parser.parseRegion(*result.addRegion(), /*arguments=*/{});
570}
571
572void SelectionOp::print(OpAsmPrinter &printer) {
573 auto control = getSelectionControl();
574 if (control != spirv::SelectionControl::None)
575 printer << " control(" << spirv::stringifySelectionControl(control) << ")";
576 if (getNumResults() > 0) {
577 printer << " -> ";
578 printer << getResultTypes();
579 }
580 printer << ' ';
581 printer.printRegion(getRegion(), /*printEntryBlockArgs=*/false,
582 /*printBlockTerminators=*/true);
583}
584
585LogicalResult SelectionOp::verifyRegions() {
586 auto *op = getOperation();
587
588 // We need to verify that the blocks follow the following layout:
589 //
590 // +--------------+
591 // | header block |
592 // +--------------+
593 // / | \
594 // ...
595 //
596 //
597 // +---------+ +---------+ +---------+
598 // | case #0 | | case #1 | | case #2 | ...
599 // +---------+ +---------+ +---------+
600 //
601 //
602 // ...
603 // \ | /
604 // v
605 // +-------------+
606 // | merge block |
607 // +-------------+
608
609 auto &region = op->getRegion(0);
610 // Allow empty region as a degenerated case, which can come from
611 // optimizations.
612 if (region.empty())
613 return success();
614
615 // The last block is the merge block.
616 if (!isMergeBlock(region.back()))
617 return emitOpError("last block must be the merge block with only one "
618 "'spirv.mlir.merge' op");
619 if (hasOtherMerge(region))
620 return emitOpError(
621 "should not have 'spirv.mlir.merge' op outside the merge block");
622
623 if (region.hasOneBlock())
624 return emitOpError("must have a selection header block");
625
626 if (!returnTypesMatch(region, op))
627 return emitOpError(
628 "result types do not match types yielded with `spirv.mlir.merge`");
629
630 return success();
631}
632
633Block *SelectionOp::getHeaderBlock() {
634 assert(!getBody().empty() && "op region should not be empty!");
635 // The first block is the loop header block.
636 return &getBody().front();
637}
638
639Block *SelectionOp::getMergeBlock() {
640 assert(!getBody().empty() && "op region should not be empty!");
641 // The last block is the loop merge block.
642 return &getBody().back();
643}
644
645void SelectionOp::addMergeBlock(OpBuilder &builder) {
646 assert(getBody().empty() && "entry and merge block already exist");
647 OpBuilder::InsertionGuard guard(builder);
648 builder.createBlock(&getBody());
649
650 // Add a spirv.mlir.merge op into the merge block.
651 spirv::MergeOp::create(builder, getLoc());
652}
653
654SelectionOp
655SelectionOp::createIfThen(Location loc, Value condition,
656 function_ref<void(OpBuilder &builder)> thenBody,
657 OpBuilder &builder) {
658 auto selectionOp =
659 spirv::SelectionOp::create(builder, loc, spirv::SelectionControl::None);
660
661 selectionOp.addMergeBlock(builder);
662 Block *mergeBlock = selectionOp.getMergeBlock();
663 Block *thenBlock = nullptr;
664
665 // Build the "then" block.
666 {
667 OpBuilder::InsertionGuard guard(builder);
668 thenBlock = builder.createBlock(mergeBlock);
669 thenBody(builder);
670 spirv::BranchOp::create(builder, loc, mergeBlock);
671 }
672
673 // Build the header block.
674 {
675 OpBuilder::InsertionGuard guard(builder);
676 builder.createBlock(thenBlock);
677 spirv::BranchConditionalOp::create(builder, loc, condition, thenBlock,
678 /*trueArguments=*/ArrayRef<Value>(),
679 mergeBlock,
680 /*falseArguments=*/ArrayRef<Value>());
681 }
682
683 return selectionOp;
684}
685
686//===----------------------------------------------------------------------===//
687// spirv.Unreachable
688//===----------------------------------------------------------------------===//
689
690LogicalResult spirv::UnreachableOp::verify() {
691 auto *block = (*this)->getBlock();
692 // Fast track: if this is in entry block, its invalid. Otherwise, if no
693 // predecessors, it's valid.
694 if (block->isEntryBlock())
695 return emitOpError("cannot be used in reachable block");
696 if (block->hasNoPredecessors())
697 return success();
698
699 // TODO: further verification needs to analyze reachability from
700 // the entry block.
701
702 return success();
703}
704
705} // namespace mlir::spirv
return success()
p<< " : "<< getMemRefType()<< ", "<< getType();}static LogicalResult verifyVectorMemoryOp(Operation *op, MemRefType memrefType, VectorType vectorType) { if(memrefType.getElementType() !=vectorType.getElementType()) return op-> emitOpError("requires memref and vector types of the same elemental type")
Given a list of lists of parsed operands, populates uniqueOperands with unique operands.
virtual Builder & getBuilder() const =0
Return a builder which provides useful access to MLIRContext, global objects like types and attribute...
virtual ParseResult parseOptionalKeyword(StringRef keyword)=0
Parse the given keyword if present.
virtual ParseResult parseRParen()=0
Parse a ) token.
virtual ParseResult parseRSquare()=0
Parse a ] token.
virtual ParseResult parseOptionalArrow()=0
Parse a '->' token if present.
virtual ParseResult parseLParen()=0
Parse a ( token.
virtual ParseResult parseComma()=0
Parse a , token.
ParseResult parseTypeList(SmallVectorImpl< Type > &result)
Parse a type list.
virtual ParseResult parseOptionalLSquare()=0
Parse a [ token if present.
virtual ParseResult parseAttribute(Attribute &result, Type type={})=0
Parse an arbitrary attribute of a given type and return it in result.
Block represents an ordered list of Operations.
Definition Block.h:33
unsigned getNumSuccessors()
Definition Block.cpp:270
Operation & front()
Definition Block.h:163
Operation & back()
Definition Block.h:162
This class is a general helper class for creating context-global objects like types,...
Definition Builders.h:51
IntegerType getI1Type()
Definition Builders.cpp:57
Attr getAttr(Args &&...args)
Get or construct an instance of the attribute Attr with provided arguments.
Definition Builders.h:100
static DenseIntElementsAttr get(const ShapedType &type, Arg &&arg)
Get an instance of a DenseIntElementsAttr with the given arguments.
The OpAsmParser has methods for interacting with the asm parser: parsing things from it,...
virtual ParseResult parseRegion(Region &region, ArrayRef< Argument > arguments={}, bool enableNameShadowing=false)=0
Parses a region.
virtual ParseResult resolveOperand(const UnresolvedOperand &operand, Type type, SmallVectorImpl< Value > &result)=0
Resolve an operand to an SSA value, emitting an error on failure.
virtual ParseResult parseSuccessorAndUseList(Block *&dest, SmallVectorImpl< Value > &operands)=0
Parse a single operation successor and its operand list.
virtual ParseResult parseOperand(UnresolvedOperand &result, bool allowResultNumber=true)=0
Parse a single SSA value operand name along with a result number if allowResultNumber is true.
virtual void printSuccessorAndUseList(Block *successor, ValueRange succOperands)=0
Print the successor and its operands.
virtual void printRegion(Region &blocks, bool printEntryBlockArgs=true, bool printBlockTerminators=true, bool printEmptyBlock=false)=0
Prints a region.
Block * createBlock(Region *parent, Region::iterator insertPt={}, TypeRange argTypes={}, ArrayRef< Location > locs={})
Add new block with 'argTypes' arguments and set the insertion point to the end of it.
Definition Builders.cpp:435
Operation is the basic unit of execution within MLIR.
Definition Operation.h:87
OperandRange operand_range
Definition Operation.h:396
result_type_range getResultTypes()
Definition Operation.h:453
This class contains a list of basic blocks and a link to the parent operation it is attached to.
Definition Region.h:26
iterator_range< OpIterator > getOps()
Definition Region.h:172
bool empty()
Definition Region.h:60
This class models how operands are forwarded to block arguments in control flow.
virtual Operation * lookupNearestSymbolFrom(Operation *from, StringAttr symbol)
Returns the operation registered with the given symbol name within the closest parent operation of,...
Type getType() const
Return the type of this value.
Definition Value.h:105
constexpr char kControl[]
ParseResult parseEnumKeywordAttr(EnumClass &value, ParserType &parser, StringRef attrName=spirv::attributeName< EnumClass >())
Parses the next keyword in parser as an enumerant of the given EnumClass.
static bool hasOtherMerge(Region &region)
Returns true if a spirv.mlir.merge op outside the merge block.
static bool returnTypesMatch(Region &region, Operation *op)
Returns true if types yielded by spirv.mlir.merge in the region match those returned by the op.
static bool hasOneBranchOpTo(Block &srcBlock, Block &dstBlock)
Returns true if the given srcBlock contains only one spirv.Branch to the given dstBlock.
constexpr StringRef attributeName()
static ParseResult parseControlAttribute(OpAsmParser &parser, OperationState &state, StringRef attrName=spirv::attributeName< EnumClass >())
Parses Function, Selection and Loop control attributes.
static bool isMergeBlock(Block &block)
Returns true if the given block only contains one spirv.mlir.merge op.
Type getType(OpFoldResult ofr)
Returns the int type of the integer in ofr.
Definition Utils.cpp:307
llvm::function_ref< Fn > function_ref
Definition LLVM.h:147
This represents an operation in an abstracted form, suitable for use with the builder APIs.
void addAttribute(StringRef name, Attribute attr)
Add an attribute with the specified name.
Region * addRegion()
Create a region that should be attached to the operation.