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 Type selectorType = getSelector().getType();
270 Type literalType = literals->getType().getElementType();
271 if (literalType != selectorType)
272 return emitOpError() << "'selector' type (" << selectorType
273 << ") should match literals type (" << literalType
274 << ")";
275
276 if (literals && literals->size() != static_cast<int64_t>(targets.size()))
277 return emitOpError() << "number of literals (" << literals->size()
278 << ") should match number of targets ("
279 << targets.size() << ")";
280 return success();
281}
282
283SuccessorOperands SwitchOp::getSuccessorOperands(unsigned index) {
284 assert(index < getNumSuccessors() && "invalid successor index");
285 return SuccessorOperands(index == 0 ? getDefaultOperandsMutable()
286 : getTargetOperandsMutable(index - 1));
287}
288
289Block *SwitchOp::getSuccessorForOperands(ArrayRef<Attribute> operands) {
290 std::optional<DenseIntElementsAttr> literals = getLiterals();
291
292 if (!literals)
293 return getDefaultTarget();
294
295 SuccessorRange targets = getTargets();
296 if (auto value = dyn_cast_or_null<IntegerAttr>(operands.front())) {
297 for (auto [index, literal] : llvm::enumerate(literals->getValues<APInt>()))
298 if (literal == value.getValue())
299 return targets[index];
300 return getDefaultTarget();
301 }
302 return nullptr;
303}
304
305//===----------------------------------------------------------------------===//
306// spirv.mlir.loop
307//===----------------------------------------------------------------------===//
308
309void LoopOp::build(OpBuilder &builder, OperationState &state) {
310 state.addAttribute("loop_control", builder.getAttr<spirv::LoopControlAttr>(
311 spirv::LoopControl::None));
312 state.addRegion();
313}
314
315ParseResult LoopOp::parse(OpAsmParser &parser, OperationState &result) {
317 result))
318 return failure();
319
320 if (succeeded(parser.parseOptionalArrow()))
321 if (parser.parseTypeList(result.types))
322 return failure();
323
324 return parser.parseRegion(*result.addRegion(), /*arguments=*/{});
325}
326
327void LoopOp::print(OpAsmPrinter &printer) {
328 auto control = getLoopControl();
329 if (control != spirv::LoopControl::None)
330 printer << " control(" << spirv::stringifyLoopControl(control) << ")";
331 if (getNumResults() > 0) {
332 printer << " -> ";
333 printer << getResultTypes();
334 }
335 printer << ' ';
336 printer.printRegion(getRegion(), /*printEntryBlockArgs=*/false,
337 /*printBlockTerminators=*/true);
338}
339
340/// Returns true if the given `srcBlock` contains only one `spirv.Branch` to the
341/// given `dstBlock`.
342static bool hasOneBranchOpTo(Block &srcBlock, Block &dstBlock) {
343 // Check that there is only one op in the `srcBlock`.
344 if (!llvm::hasSingleElement(srcBlock))
345 return false;
346
347 auto branchOp = dyn_cast<spirv::BranchOp>(srcBlock.back());
348 return branchOp && branchOp.getSuccessor() == &dstBlock;
349}
350
351/// Returns true if the given `block` only contains one `spirv.mlir.merge` op.
352static bool isMergeBlock(Block &block) {
353 return llvm::hasSingleElement(block) && isa<spirv::MergeOp>(block.front());
354}
355
356/// Returns true if a `spirv.mlir.merge` op outside the merge block.
357static bool hasOtherMerge(Region &region) {
358 return !region.empty() && llvm::any_of(region.getOps(), [&](Operation &op) {
359 return isa<spirv::MergeOp>(op) && op.getBlock() != &region.back();
360 });
361}
362
363/// Returns true if types yielded by `spirv.mlir.merge` in the region match
364/// those returned by the `op`.
365static bool returnTypesMatch(Region &region, Operation *op) {
366 auto mergeOps = region.getOps<spirv::MergeOp>();
367 Operation *mergeOp = llvm::getSingleElement(mergeOps);
368 return llvm::equal(mergeOp->getOperandTypes(), op->getResultTypes());
369}
370
371LogicalResult LoopOp::verifyRegions() {
372 auto *op = getOperation();
373
374 // We need to verify that the blocks follow the following layout:
375 //
376 // +-------------+
377 // | entry block |
378 // +-------------+
379 // |
380 // v
381 // +-------------+
382 // | loop header | <-----+
383 // +-------------+ |
384 // |
385 // ... |
386 // \ | / |
387 // v |
388 // +---------------+ |
389 // | loop continue | -----+
390 // +---------------+
391 //
392 // ...
393 // \ | /
394 // v
395 // +-------------+
396 // | merge block |
397 // +-------------+
398
399 auto &region = op->getRegion(0);
400 // Allow empty region as a degenerated case, which can come from
401 // optimizations.
402 if (region.empty())
403 return success();
404
405 // The last block is the merge block.
406 Block &merge = region.back();
407 if (!isMergeBlock(merge))
408 return emitOpError("last block must be the merge block with only one "
409 "'spirv.mlir.merge' op");
410 if (hasOtherMerge(region))
411 return emitOpError(
412 "should not have 'spirv.mlir.merge' op outside the merge block");
413
414 if (region.hasOneBlock())
415 return emitOpError(
416 "must have an entry block branching to the loop header block");
417 // The first block is the entry block.
418 Block &entry = region.front();
419
420 if (std::next(region.begin(), 2) == region.end())
421 return emitOpError(
422 "must have a loop header block branched from the entry block");
423 // The second block is the loop header block.
424 Block &header = *std::next(region.begin(), 1);
425
426 if (!hasOneBranchOpTo(entry, header))
427 return emitOpError(
428 "entry block must only have one 'spirv.Branch' op to the second block");
429
430 if (std::next(region.begin(), 3) == region.end())
431 return emitOpError(
432 "requires a loop continue block branching to the loop header block");
433 // The second to last block is the loop continue block.
434 Block &cont = *std::prev(region.end(), 2);
435
436 // Make sure that we have a branch from the loop continue block to the loop
437 // header block.
438 if (llvm::none_of(
439 llvm::seq<unsigned>(0, cont.getNumSuccessors()),
440 [&](unsigned index) { return cont.getSuccessor(index) == &header; }))
441 return emitOpError("second to last block must be the loop continue "
442 "block that branches to the loop header block");
443
444 // Make sure that no other blocks (except the entry and loop continue block)
445 // branches to the loop header block.
446 for (auto &block : llvm::make_range(std::next(region.begin(), 2),
447 std::prev(region.end(), 2))) {
448 for (auto i : llvm::seq<unsigned>(0, block.getNumSuccessors())) {
449 if (block.getSuccessor(i) == &header) {
450 return emitOpError("can only have the entry and loop continue "
451 "block branching to the loop header block");
452 }
453 }
454 }
455
456 if (!returnTypesMatch(region, op))
457 return emitOpError(
458 "result types do not match types yielded with `spirv.mlir.merge`");
459
460 return success();
461}
462
463Block *LoopOp::getEntryBlock() {
464 assert(!getBody().empty() && "op region should not be empty!");
465 return &getBody().front();
466}
467
468Block *LoopOp::getHeaderBlock() {
469 assert(!getBody().empty() && "op region should not be empty!");
470 // The second block is the loop header block.
471 return &*std::next(getBody().begin());
472}
473
474Block *LoopOp::getContinueBlock() {
475 assert(!getBody().empty() && "op region should not be empty!");
476 // The second to last block is the loop continue block.
477 return &*std::prev(getBody().end(), 2);
478}
479
480Block *LoopOp::getMergeBlock() {
481 assert(!getBody().empty() && "op region should not be empty!");
482 // The last block is the loop merge block.
483 return &getBody().back();
484}
485
486void LoopOp::addEntryAndMergeBlock(OpBuilder &builder) {
487 assert(getBody().empty() && "entry and merge block already exist");
488 OpBuilder::InsertionGuard g(builder);
489 builder.createBlock(&getBody());
490 builder.createBlock(&getBody());
491
492 // Add a spirv.mlir.merge op into the merge block.
493 spirv::MergeOp::create(builder, getLoc());
494}
495
496//===----------------------------------------------------------------------===//
497// spirv.Return
498//===----------------------------------------------------------------------===//
499
500LogicalResult ReturnOp::verify() {
501 // Verification is performed in spirv.func op.
502 return success();
503}
504
505//===----------------------------------------------------------------------===//
506// spirv.ReturnValue
507//===----------------------------------------------------------------------===//
508
509LogicalResult ReturnValueOp::verify() {
510 // Verification is performed in spirv.func op.
511 return success();
512}
513
514//===----------------------------------------------------------------------===//
515// spirv.Select
516//===----------------------------------------------------------------------===//
517
518LogicalResult SelectOp::verify() {
519 if (auto conditionTy = dyn_cast<VectorType>(getCondition().getType())) {
520 auto resultVectorTy = dyn_cast<VectorType>(getResult().getType());
521 if (!resultVectorTy) {
522 return emitOpError("result expected to be of vector type when "
523 "condition is of vector type");
524 }
525 if (resultVectorTy.getNumElements() != conditionTy.getNumElements()) {
526 return emitOpError("result should have the same number of elements as "
527 "the condition when condition is of vector type");
528 }
529 }
530 return success();
531}
532
533// Custom availability implementation is needed for spirv.Select given the
534// syntax changes starting v1.4.
535SmallVector<ArrayRef<spirv::Extension>, 1> SelectOp::getExtensions() {
536 return {};
537}
538SmallVector<ArrayRef<spirv::Capability>, 1> SelectOp::getCapabilities() {
539 return {};
540}
541std::optional<spirv::Version> SelectOp::getMinVersion() {
542 // Per the spec, "Before version 1.4, results are only computed per
543 // component."
544 if (isa<spirv::ScalarType>(getCondition().getType()) &&
545 isa<spirv::CompositeType>(getType()))
546 return Version::V_1_4;
547
548 return Version::V_1_0;
549}
550std::optional<spirv::Version> SelectOp::getMaxVersion() {
551 return Version::V_1_6;
552}
553
554//===----------------------------------------------------------------------===//
555// spirv.mlir.selection
556//===----------------------------------------------------------------------===//
557
558ParseResult SelectionOp::parse(OpAsmParser &parser, OperationState &result) {
559 if (parseControlAttribute<spirv::SelectionControlAttr,
560 spirv::SelectionControl>(parser, result))
561 return failure();
562
563 if (succeeded(parser.parseOptionalArrow()))
564 if (parser.parseTypeList(result.types))
565 return failure();
566
567 return parser.parseRegion(*result.addRegion(), /*arguments=*/{});
568}
569
570void SelectionOp::print(OpAsmPrinter &printer) {
571 auto control = getSelectionControl();
572 if (control != spirv::SelectionControl::None)
573 printer << " control(" << spirv::stringifySelectionControl(control) << ")";
574 if (getNumResults() > 0) {
575 printer << " -> ";
576 printer << getResultTypes();
577 }
578 printer << ' ';
579 printer.printRegion(getRegion(), /*printEntryBlockArgs=*/false,
580 /*printBlockTerminators=*/true);
581}
582
583LogicalResult SelectionOp::verifyRegions() {
584 auto *op = getOperation();
585
586 // We need to verify that the blocks follow the following layout:
587 //
588 // +--------------+
589 // | header block |
590 // +--------------+
591 // / | \
592 // ...
593 //
594 //
595 // +---------+ +---------+ +---------+
596 // | case #0 | | case #1 | | case #2 | ...
597 // +---------+ +---------+ +---------+
598 //
599 //
600 // ...
601 // \ | /
602 // v
603 // +-------------+
604 // | merge block |
605 // +-------------+
606
607 auto &region = op->getRegion(0);
608 // Allow empty region as a degenerated case, which can come from
609 // optimizations.
610 if (region.empty())
611 return success();
612
613 // The last block is the merge block.
614 if (!isMergeBlock(region.back()))
615 return emitOpError("last block must be the merge block with only one "
616 "'spirv.mlir.merge' op");
617 if (hasOtherMerge(region))
618 return emitOpError(
619 "should not have 'spirv.mlir.merge' op outside the merge block");
620
621 if (region.hasOneBlock())
622 return emitOpError("must have a selection header block");
623
624 if (!returnTypesMatch(region, op))
625 return emitOpError(
626 "result types do not match types yielded with `spirv.mlir.merge`");
627
628 return success();
629}
630
631Block *SelectionOp::getHeaderBlock() {
632 assert(!getBody().empty() && "op region should not be empty!");
633 // The first block is the loop header block.
634 return &getBody().front();
635}
636
637Block *SelectionOp::getMergeBlock() {
638 assert(!getBody().empty() && "op region should not be empty!");
639 // The last block is the loop merge block.
640 return &getBody().back();
641}
642
643void SelectionOp::addMergeBlock(OpBuilder &builder) {
644 assert(getBody().empty() && "entry and merge block already exist");
645 OpBuilder::InsertionGuard guard(builder);
646 builder.createBlock(&getBody());
647
648 // Add a spirv.mlir.merge op into the merge block.
649 spirv::MergeOp::create(builder, getLoc());
650}
651
652SelectionOp
653SelectionOp::createIfThen(Location loc, Value condition,
654 function_ref<void(OpBuilder &builder)> thenBody,
655 OpBuilder &builder) {
656 auto selectionOp =
657 spirv::SelectionOp::create(builder, loc, spirv::SelectionControl::None);
658
659 selectionOp.addMergeBlock(builder);
660 Block *mergeBlock = selectionOp.getMergeBlock();
661 Block *thenBlock = nullptr;
662
663 // Build the "then" block.
664 {
665 OpBuilder::InsertionGuard guard(builder);
666 thenBlock = builder.createBlock(mergeBlock);
667 thenBody(builder);
668 spirv::BranchOp::create(builder, loc, mergeBlock);
669 }
670
671 // Build the header block.
672 {
673 OpBuilder::InsertionGuard guard(builder);
674 builder.createBlock(thenBlock);
675 spirv::BranchConditionalOp::create(builder, loc, condition, thenBlock,
676 /*trueArguments=*/ArrayRef<Value>(),
677 mergeBlock,
678 /*falseArguments=*/ArrayRef<Value>());
679 }
680
681 return selectionOp;
682}
683
684//===----------------------------------------------------------------------===//
685// spirv.Unreachable
686//===----------------------------------------------------------------------===//
687
688LogicalResult spirv::UnreachableOp::verify() {
689 auto *block = (*this)->getBlock();
690 // Fast track: if this is in entry block, its invalid. Otherwise, if no
691 // predecessors, it's valid.
692 if (block->isEntryBlock())
693 return emitOpError("cannot be used in reachable block");
694 if (block->hasNoPredecessors())
695 return success();
696
697 // TODO: further verification needs to analyze reachability from
698 // the entry block.
699
700 return success();
701}
702
703} // 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:434
Operation is the basic unit of execution within MLIR.
Definition Operation.h:88
OperandRange operand_range
Definition Operation.h:371
result_type_range getResultTypes()
Definition Operation.h:428
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:305
llvm::function_ref< Fn > function_ref
Definition LLVM.h:144
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.