MLIR 24.0.0git
OpenMPDialect.cpp
Go to the documentation of this file.
1//===- OpenMPDialect.cpp - MLIR Dialect for OpenMP implementation ---------===//
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 implements the OpenMP dialect and its operations.
10//
11//===----------------------------------------------------------------------===//
12
18#include "mlir/IR/Attributes.h"
20#include "mlir/IR/Matchers.h"
23#include "mlir/IR/SymbolTable.h"
26
27#include "llvm/ADT/ArrayRef.h"
28#include "llvm/ADT/PostOrderIterator.h"
29#include "llvm/ADT/STLExtras.h"
30#include "llvm/ADT/STLForwardCompat.h"
31#include "llvm/ADT/SmallString.h"
32#include "llvm/ADT/StringRef.h"
33#include "llvm/ADT/TypeSwitch.h"
34#include "llvm/ADT/bit.h"
35#include "llvm/Support/InterleavedRange.h"
36#include <cstddef>
37#include <iterator>
38#include <optional>
39#include <variant>
40
41#include "mlir/Dialect/OpenMP/OpenMPOpsDialect.cpp.inc"
42#include "mlir/Dialect/OpenMP/OpenMPOpsEnums.cpp.inc"
43#include "mlir/Dialect/OpenMP/OpenMPOpsInterfaces.cpp.inc"
44#include "mlir/Dialect/OpenMP/OpenMPTypeInterfaces.cpp.inc"
45
46using namespace mlir;
47using namespace mlir::omp;
48
51 return attrs.empty() ? nullptr : ArrayAttr::get(context, attrs);
52}
53
56 return boolArray.empty() ? nullptr : DenseBoolArrayAttr::get(ctx, boolArray);
57}
58
61 return intArray.empty() ? nullptr : DenseI64ArrayAttr::get(ctx, intArray);
62}
63
64namespace {
65struct MemRefPointerLikeModel
66 : public PointerLikeType::ExternalModel<MemRefPointerLikeModel,
67 MemRefType> {
68 Type getElementType(Type pointer) const {
69 return llvm::cast<MemRefType>(pointer).getElementType();
70 }
71};
72
73struct LLVMPointerPointerLikeModel
74 : public PointerLikeType::ExternalModel<LLVMPointerPointerLikeModel,
75 LLVM::LLVMPointerType> {
76 Type getElementType(Type pointer) const { return Type(); }
77};
78} // namespace
79
80/// Generate a name of a canonical loop nest of the format
81/// `<prefix>(_r<idx>_s<idx>)*`. Hereby, `_r<idx>` identifies the region
82/// argument index of an operation that has multiple regions, if the operation
83/// has multiple regions.
84/// `_s<idx>` identifies the position of an operation within a region, where
85/// only operations that may potentially contain loops ("container operations"
86/// i.e. have region arguments) are counted. Again, it is omitted if there is
87/// only one such operation in a region. If there are canonical loops nested
88/// inside each other, also may also use the format `_d<num>` where <num> is the
89/// nesting depth of the loop.
90///
91/// The generated name is a best-effort to make canonical loop unique within an
92/// SSA namespace. This also means that regions with IsolatedFromAbove property
93/// do not consider any parents or siblings.
94static std::string generateLoopNestingName(StringRef prefix,
95 CanonicalLoopOp op) {
96 struct Component {
97 /// If true, this component describes a region operand of an operation (the
98 /// operand's owner) If false, this component describes an operation located
99 /// in a parent region
100 bool isRegionArgOfOp;
101 bool skip = false;
102 bool isUnique = false;
103
104 size_t idx;
105 Operation *op;
106 Region *parentRegion;
107 size_t loopDepth;
108
109 Operation *&getOwnerOp() {
110 assert(isRegionArgOfOp && "Must describe a region operand");
111 return op;
112 }
113 size_t &getArgIdx() {
114 assert(isRegionArgOfOp && "Must describe a region operand");
115 return idx;
116 }
117
118 Operation *&getContainerOp() {
119 assert(!isRegionArgOfOp && "Must describe a operation of a region");
120 return op;
121 }
122 size_t &getOpPos() {
123 assert(!isRegionArgOfOp && "Must describe a operation of a region");
124 return idx;
125 }
126 bool isLoopOp() const {
127 assert(!isRegionArgOfOp && "Must describe a operation of a region");
128 return isa<CanonicalLoopOp>(op);
129 }
130 Region *&getParentRegion() {
131 assert(!isRegionArgOfOp && "Must describe a operation of a region");
132 return parentRegion;
133 }
134 size_t &getLoopDepth() {
135 assert(!isRegionArgOfOp && "Must describe a operation of a region");
136 return loopDepth;
137 }
138
139 void skipIf(bool v = true) { skip = skip || v; }
140 };
141
142 // List of ancestors, from inner to outer.
143 // Alternates between
144 // * region argument of an operation
145 // * operation within a region
146 SmallVector<Component> components;
147
148 // Gather a list of parent regions and operations, and the position within
149 // their parent
150 Operation *o = op.getOperation();
151 while (o) {
152 // Operation within a region
153 Region *r = o->getParentRegion();
154 if (!r)
155 break;
156
157 llvm::ReversePostOrderTraversal<Block *> traversal(&r->getBlocks().front());
158 size_t idx = 0;
159 bool found = false;
160 size_t sequentialIdx = -1;
161 bool isOnlyContainerOp = true;
162 for (Block *b : traversal) {
163 for (Operation &op : *b) {
164 if (&op == o && !found) {
165 sequentialIdx = idx;
166 found = true;
167 }
168 if (op.getNumRegions()) {
169 idx += 1;
170 if (idx > 1)
171 isOnlyContainerOp = false;
172 }
173 if (found && !isOnlyContainerOp)
174 break;
175 }
176 }
177
178 Component &containerOpInRegion = components.emplace_back();
179 containerOpInRegion.isRegionArgOfOp = false;
180 containerOpInRegion.isUnique = isOnlyContainerOp;
181 containerOpInRegion.getContainerOp() = o;
182 containerOpInRegion.getOpPos() = sequentialIdx;
183 containerOpInRegion.getParentRegion() = r;
184
185 Operation *parent = r->getParentOp();
186
187 // Region argument of an operation
188 Component &regionArgOfOperation = components.emplace_back();
189 regionArgOfOperation.isRegionArgOfOp = true;
190 regionArgOfOperation.isUnique = true;
191 regionArgOfOperation.getArgIdx() = 0;
192 regionArgOfOperation.getOwnerOp() = parent;
193
194 // The IsolatedFromAbove trait of the parent operation implies that each
195 // individual region argument has its own separate namespace, so no
196 // ambiguity.
197 if (!parent || parent->hasTrait<mlir::OpTrait::IsIsolatedFromAbove>())
198 break;
199
200 // Component only needed if operation has multiple region operands. Region
201 // arguments may be optional, but we currently do not consider this.
202 if (parent->getRegions().size() > 1) {
203 auto getRegionIndex = [](Operation *o, Region *r) {
204 for (auto [idx, region] : llvm::enumerate(o->getRegions())) {
205 if (&region == r)
206 return idx;
207 }
208 llvm_unreachable("Region not child of its parent operation");
209 };
210 regionArgOfOperation.isUnique = false;
211 regionArgOfOperation.getArgIdx() = getRegionIndex(parent, r);
212 }
213
214 // next parent
215 o = parent;
216 }
217
218 // Determine whether a region-argument component is not needed
219 for (Component &c : components)
220 c.skipIf(c.isRegionArgOfOp && c.isUnique);
221
222 // Find runs of nested loops and determine each loop's depth in the loop nest
223 size_t numSurroundingLoops = 0;
224 for (Component &c : llvm::reverse(components)) {
225 if (c.skip)
226 continue;
227
228 // non-skipped multi-argument operands interrupt the loop nest
229 if (c.isRegionArgOfOp) {
230 numSurroundingLoops = 0;
231 continue;
232 }
233
234 // Multiple loops in a region means each of them is the outermost loop of a
235 // new loop nest
236 if (!c.isUnique)
237 numSurroundingLoops = 0;
238
239 c.getLoopDepth() = numSurroundingLoops;
240
241 // Next loop is surrounded by one more loop
242 if (isa<CanonicalLoopOp>(c.getContainerOp()))
243 numSurroundingLoops += 1;
244 }
245
246 // In loop nests, skip all but the innermost loop that contains the depth
247 // number
248 bool isLoopNest = false;
249 for (Component &c : components) {
250 if (c.skip || c.isRegionArgOfOp)
251 continue;
252
253 if (!isLoopNest && c.getLoopDepth() >= 1) {
254 // Innermost loop of a loop nest of at least two loops
255 isLoopNest = true;
256 } else if (isLoopNest) {
257 // Non-innermost loop of a loop nest
258 c.skipIf(c.isUnique);
259
260 // If there is no surrounding loop left, this must have been the outermost
261 // loop; leave loop-nest mode for the next iteration
262 if (c.getLoopDepth() == 0)
263 isLoopNest = false;
264 }
265 }
266
267 // Skip non-loop unambiguous regions (but they should interrupt loop nests, so
268 // we mark them as skipped only after computing loop nests)
269 for (Component &c : components)
270 c.skipIf(!c.isRegionArgOfOp && c.isUnique &&
271 !isa<CanonicalLoopOp>(c.getContainerOp()));
272
273 // Components can be skipped if they are already disambiguated by their parent
274 // (or does not have a parent)
275 bool newRegion = true;
276 for (Component &c : llvm::reverse(components)) {
277 c.skipIf(newRegion && c.isUnique);
278
279 // non-skipped components disambiguate unique children
280 if (!c.skip)
281 newRegion = true;
282
283 // ...except canonical loops that need a suffix for each nest
284 if (!c.isRegionArgOfOp && c.getContainerOp())
285 newRegion = false;
286 }
287
288 // Compile the nesting name string
289 SmallString<64> Name{prefix};
290 llvm::raw_svector_ostream NameOS(Name);
291 for (auto &c : llvm::reverse(components)) {
292 if (c.skip)
293 continue;
294
295 if (c.isRegionArgOfOp)
296 NameOS << "_r" << c.getArgIdx();
297 else if (c.getLoopDepth() >= 1)
298 NameOS << "_d" << c.getLoopDepth();
299 else
300 NameOS << "_s" << c.getOpPos();
301 }
302
303 return NameOS.str().str();
304}
305
306void OpenMPDialect::initialize() {
307 addOperations<
308#define GET_OP_LIST
309#include "mlir/Dialect/OpenMP/OpenMPOps.cpp.inc"
310 >();
311 addAttributes<
312#define GET_ATTRDEF_LIST
313#include "mlir/Dialect/OpenMP/OpenMPOpsAttributes.cpp.inc"
314 >();
315 addTypes<
316#define GET_TYPEDEF_LIST
317#include "mlir/Dialect/OpenMP/OpenMPOpsTypes.cpp.inc"
318 >();
319
320 declarePromisedInterface<ConvertToLLVMPatternInterface, OpenMPDialect>();
321
322 MemRefType::attachInterface<MemRefPointerLikeModel>(*getContext());
323 LLVM::LLVMPointerType::attachInterface<LLVMPointerPointerLikeModel>(
324 *getContext());
325
326 // Attach default offload module interface to module op to access
327 // offload functionality through
328 mlir::ModuleOp::attachInterface<mlir::omp::OffloadModuleDefaultModel>(
329 *getContext());
330
331 // Attach default declare target interfaces to operations which can be marked
332 // as declare target (Global Operations and Functions/Subroutines in dialects
333 // that Fortran (or other languages that lower to MLIR) translates too
334 mlir::LLVM::GlobalOp::attachInterface<
336 *getContext());
337 mlir::LLVM::LLVMFuncOp::attachInterface<
339 *getContext());
340 mlir::func::FuncOp::attachInterface<
342}
343
344//===----------------------------------------------------------------------===//
345// Dialect operation attribute verification
346//===----------------------------------------------------------------------===//
347
348static LogicalResult verifyDeclareTargetAttr(Operation *op, Attribute attr) {
349 if (!isa<DeclareTargetInterface>(op))
350 return op->emitError() << "omp.declare_target can only be applied to "
351 "DeclareTargetInterface ops";
352
353 auto declareTargetAttr = dyn_cast<DeclareTargetAttr>(attr);
354 if (!declareTargetAttr)
355 return op->emitError()
356 << "omp.declare_target must be an #omp.declaretarget attribute";
357
358 if (isa<mlir::FunctionOpInterface>(op)) {
359 if (declareTargetAttr.getAutomap())
360 return op->emitOpError()
361 << "omp.declare_target 'automap' is not valid on functions";
362
363 // TODO: Disallow the `local` clause (OpenMP 6.0).
364 if (declareTargetAttr.getCaptureClause() ==
365 mlir::omp::DeclareTargetCaptureClause::link)
366 return op->emitOpError()
367 << "omp.declare_target 'link' is not valid on functions";
368 } else {
369 // TODO: Disallow the `indirect` clause (OpenMP 5.1).
370 if (declareTargetAttr.getImplicit())
371 return op->emitOpError()
372 << "omp.declare_target 'implicit' is only valid on functions";
373 }
374 return success();
375}
376
377LogicalResult
378OpenMPDialect::verifyOperationAttribute(Operation *op,
379 NamedAttribute attribute) {
380 if (attribute.getName() == "omp.declare_target")
381 return verifyDeclareTargetAttr(op, attribute.getValue());
382
383 return success();
384}
385
386//===----------------------------------------------------------------------===//
387// Parser and printer for Allocate Clause
388//===----------------------------------------------------------------------===//
389
390/// Parse an allocate clause with allocators and a list of operands with types.
391///
392/// allocate-operand-list :: = allocate-operand |
393/// allocator-operand `,` allocate-operand-list
394/// allocate-operand :: = ssa-id-and-type -> ssa-id-and-type
395/// ssa-id-and-type ::= ssa-id `:` type
396static ParseResult parseAllocateAndAllocator(
397 OpAsmParser &parser,
399 SmallVectorImpl<Type> &allocateTypes,
401 SmallVectorImpl<Type> &allocatorTypes) {
402
403 return parser.parseCommaSeparatedList([&]() {
405 Type type;
406 if (parser.parseOperand(operand) || parser.parseColonType(type))
407 return failure();
408 allocatorVars.push_back(operand);
409 allocatorTypes.push_back(type);
410 if (parser.parseArrow())
411 return failure();
412 if (parser.parseOperand(operand) || parser.parseColonType(type))
413 return failure();
414
415 allocateVars.push_back(operand);
416 allocateTypes.push_back(type);
417 return success();
418 });
419}
420
421/// Print allocate clause
423 OperandRange allocateVars,
424 TypeRange allocateTypes,
425 OperandRange allocatorVars,
426 TypeRange allocatorTypes) {
427 for (unsigned i = 0; i < allocateVars.size(); ++i) {
428 std::string separator = i == allocateVars.size() - 1 ? "" : ", ";
429 p << allocatorVars[i] << " : " << allocatorTypes[i] << " -> ";
430 p << allocateVars[i] << " : " << allocateTypes[i] << separator;
431 }
432}
433
434//===----------------------------------------------------------------------===//
435// Parser and printer for a clause attribute (StringEnumAttr)
436//===----------------------------------------------------------------------===//
437
438template <typename ClauseAttr>
439static ParseResult parseClauseAttr(AsmParser &parser, ClauseAttr &attr) {
440 using ClauseT = decltype(std::declval<ClauseAttr>().getValue());
441 StringRef enumStr;
442 SMLoc loc = parser.getCurrentLocation();
443 if (parser.parseKeyword(&enumStr))
444 return failure();
445 if (std::optional<ClauseT> enumValue = symbolizeEnum<ClauseT>(enumStr)) {
446 attr = ClauseAttr::get(parser.getContext(), *enumValue);
447 return success();
448 }
449 return parser.emitError(loc, "invalid clause value: '") << enumStr << "'";
450}
451
452template <typename ClauseAttr>
453static void printClauseAttr(OpAsmPrinter &p, Operation *op, ClauseAttr attr) {
454 p << stringifyEnum(attr.getValue());
455}
456
457//===----------------------------------------------------------------------===//
458// Parser and printer for Linear Clause
459//===----------------------------------------------------------------------===//
460
461/// linear ::= `linear` `(` linear-list `)`
462/// linear-list := linear-val | linear-val linear-list
463/// linear-val := ssa-id-and-type `=` ssa-id-and-type
464/// | `val` `(` ssa-id-and-type `=` ssa-id-and-type `)`
465/// | `ref` `(` ssa-id-and-type `=` ssa-id-and-type `)`
466/// | `uval` `(` ssa-id-and-type `=` ssa-id-and-type `)`
467static ParseResult parseLinearClause(
468 OpAsmParser &parser,
470 SmallVectorImpl<Type> &linearTypes,
472 SmallVectorImpl<Type> &linearStepTypes, ArrayAttr &linearModifiers,
473 ArrayAttr &linearSyms) {
474 SmallVector<Attribute> modifiers;
475 auto result = parser.parseCommaSeparatedList([&]() {
477 Type type, stepType;
479
480 std::optional<omp::LinearModifier> linearModifier;
481 if (succeeded(parser.parseOptionalKeyword("val"))) {
482 linearModifier = omp::LinearModifier::val;
483 } else if (succeeded(parser.parseOptionalKeyword("ref"))) {
484 linearModifier = omp::LinearModifier::ref;
485 } else if (succeeded(parser.parseOptionalKeyword("uval"))) {
486 linearModifier = omp::LinearModifier::uval;
487 }
488
489 bool hasLinearModifierParens = linearModifier.has_value();
490 if (hasLinearModifierParens && parser.parseLParen())
491 return failure();
492
493 if (parser.parseOperand(var) || parser.parseColonType(type) ||
494 parser.parseEqual() || parser.parseOperand(stepVar) ||
495 parser.parseColonType(stepType))
496 return failure();
497
498 if (hasLinearModifierParens && parser.parseRParen())
499 return failure();
500
501 linearVars.push_back(var);
502 linearTypes.push_back(type);
503 linearStepVars.push_back(stepVar);
504 linearStepTypes.push_back(stepType);
505 if (linearModifier) {
506 modifiers.push_back(
507 omp::LinearModifierAttr::get(parser.getContext(), *linearModifier));
508 } else {
509 modifiers.push_back(UnitAttr::get(parser.getContext()));
510 }
511 return success();
512 });
513 if (failed(result))
514 return failure();
515 linearModifiers = ArrayAttr::get(parser.getContext(), modifiers);
516 return success();
517}
518
519/// Print Linear Clause. `linearSyms` is only used by ops with a region, which
520/// print the linear clause with their entry block arguments.
522 ValueRange linearVars, TypeRange linearTypes,
523 ValueRange linearStepVars, TypeRange stepVarTypes,
524 ArrayAttr linearModifiers, ArrayAttr linearSyms) {
525 size_t linearVarsSize = linearVars.size();
526 for (unsigned i = 0; i < linearVarsSize; ++i) {
527 if (i != 0)
528 p << ", ";
529 // Print modifier keyword wrapper if present.
530 Attribute modAttr = linearModifiers ? linearModifiers[i] : nullptr;
531 auto mod = modAttr ? dyn_cast<omp::LinearModifierAttr>(modAttr) : nullptr;
532 if (mod) {
533 p << omp::stringifyLinearModifier(mod.getValue()) << "(";
534 }
535 p << linearVars[i] << " : " << linearTypes[i];
536 p << " = " << linearStepVars[i] << " : " << stepVarTypes[i];
537 if (mod)
538 p << ")";
539 }
540}
541
542//===----------------------------------------------------------------------===//
543// Verifier for Linear modifier
544//===----------------------------------------------------------------------===//
545
546/// OpenMP 5.2, Section 5.4.6: "A linear-modifier may be specified as ref or
547/// uval only on a declare simd directive."
548/// Also verifies that modifier count matches variable count.
549static LogicalResult
550verifyLinearModifiers(Operation *op, std::optional<ArrayAttr> linearModifiers,
551 OperandRange linearVars, bool isDeclareSimd = false) {
552 if (!linearModifiers)
553 return success();
554 if (linearModifiers->size() != linearVars.size())
555 return op->emitOpError()
556 << "expected as many linear modifiers as linear variables";
557 if (!isDeclareSimd) {
558 for (Attribute attr : *linearModifiers) {
559 if (!attr)
560 continue;
561 auto modAttr = dyn_cast<omp::LinearModifierAttr>(attr);
562 if (!modAttr)
563 continue;
564 omp::LinearModifier mod = modAttr.getValue();
565 if (mod == omp::LinearModifier::ref || mod == omp::LinearModifier::uval)
566 return op->emitOpError()
567 << "linear modifier '" << omp::stringifyLinearModifier(mod)
568 << "' may only be specified on a declare simd directive";
569 }
570 }
571 return success();
572}
573
574//===----------------------------------------------------------------------===//
575// Verifier for Nontemporal Clause
576//===----------------------------------------------------------------------===//
577
578static LogicalResult verifyNontemporalClause(Operation *op,
579 OperandRange nontemporalVars) {
580
581 // Check if each var is unique - OpenMP 5.0 -> 2.9.3.1 section
582 DenseSet<Value> nontemporalItems;
583 for (const auto &it : nontemporalVars)
584 if (!nontemporalItems.insert(it).second)
585 return op->emitOpError() << "nontemporal variable used more than once";
586
587 return success();
588}
589
590//===----------------------------------------------------------------------===//
591// Parser, verifier and printer for Aligned Clause
592//===----------------------------------------------------------------------===//
593static LogicalResult verifyAlignedClause(Operation *op,
594 std::optional<ArrayAttr> alignments,
595 OperandRange alignedVars) {
596 // Check if number of alignment values equals to number of aligned variables
597 if (!alignedVars.empty()) {
598 if (!alignments || alignments->size() != alignedVars.size())
599 return op->emitOpError()
600 << "expected as many alignment values as aligned variables";
601 } else {
602 if (alignments)
603 return op->emitOpError() << "unexpected alignment values attribute";
604 return success();
605 }
606
607 // Check if each var is aligned only once - OpenMP 4.5 -> 2.8.1 section
608 DenseSet<Value> alignedItems;
609 for (auto it : alignedVars)
610 if (!alignedItems.insert(it).second)
611 return op->emitOpError() << "aligned variable used more than once";
612
613 if (!alignments)
614 return success();
615
616 // Check if all alignment values are positive - OpenMP 4.5 -> 2.8.1 section
617 for (unsigned i = 0; i < (*alignments).size(); ++i) {
618 if (auto intAttr = llvm::dyn_cast<IntegerAttr>((*alignments)[i])) {
619 if (intAttr.getValue().sle(0))
620 return op->emitOpError() << "alignment should be greater than 0";
621 } else {
622 return op->emitOpError() << "expected integer alignment";
623 }
624 }
625
626 return success();
627}
628
629/// aligned ::= `aligned` `(` aligned-list `)`
630/// aligned-list := aligned-val | aligned-val aligned-list
631/// aligned-val := ssa-id-and-type `->` alignment
632static ParseResult
635 SmallVectorImpl<Type> &alignedTypes,
636 ArrayAttr &alignmentsAttr) {
637 SmallVector<Attribute> alignmentVec;
638 if (failed(parser.parseCommaSeparatedList([&]() {
639 if (parser.parseOperand(alignedVars.emplace_back()) ||
640 parser.parseColonType(alignedTypes.emplace_back()) ||
641 parser.parseArrow() ||
642 parser.parseAttribute(alignmentVec.emplace_back())) {
643 return failure();
644 }
645 return success();
646 })))
647 return failure();
648 SmallVector<Attribute> alignments(alignmentVec.begin(), alignmentVec.end());
649 alignmentsAttr = ArrayAttr::get(parser.getContext(), alignments);
650 return success();
651}
652
653/// Print Aligned Clause
655 ValueRange alignedVars, TypeRange alignedTypes,
656 std::optional<ArrayAttr> alignments) {
657 for (unsigned i = 0; i < alignedVars.size(); ++i) {
658 if (i != 0)
659 p << ", ";
660 p << alignedVars[i] << " : " << alignedVars[i].getType();
661 p << " -> " << (*alignments)[i];
662 }
663}
664
665static LogicalResult verifyAllocateClause(
666 Operation *op, ValueRange allocateVars, ValueRange allocatorVars,
667 DenseI64ArrayAttr allocateAlignments,
668 DenseI64ArrayAttr allocatePrivateIndices, ValueRange privateVars = {},
669 ArrayAttr privateSyms = nullptr, bool requirePrivateIndices = false) {
670 if (allocateVars.size() != allocatorVars.size())
671 return op->emitError(
672 "expected equal sizes for allocate and allocator variables");
673
674 if (allocateVars.empty()) {
675 if (allocateAlignments)
676 return op->emitError(
677 "unexpected allocate alignments without allocate variables");
678 if (allocatePrivateIndices)
679 return op->emitError(
680 "unexpected allocate private indices without allocate variables");
681 return success();
682 }
683
684 if (allocateAlignments) {
685 ArrayRef<int64_t> alignments = allocateAlignments.asArrayRef();
686 if (alignments.size() != allocateVars.size())
687 return op->emitError(
688 "expected as many allocate alignments as allocate variables");
689 for (int64_t alignment : alignments) {
690 if (alignment < 0)
691 return op->emitError("expected non-negative allocate alignments");
692 if (alignment != 0 && (alignment & (alignment - 1)) != 0)
693 return op->emitError(
694 "expected positive allocate alignments to be powers of two");
695 }
696 }
697
698 if (!allocatePrivateIndices) {
699 if (requirePrivateIndices)
700 return op->emitError(
701 "expected an allocate private index for each allocate variable");
702 return success();
703 }
704
705 ArrayRef<int64_t> indices = allocatePrivateIndices.asArrayRef();
706 if (indices.size() != allocateVars.size())
707 return op->emitError(
708 "expected as many allocate private indices as allocate variables");
709
710 DenseSet<int64_t> usedPrivateSlots;
711 for (auto [allocateVar, privateIndex] :
712 llvm::zip_equal(allocateVars, indices)) {
713 if (privateIndex < 0 ||
714 static_cast<uint64_t>(privateIndex) >= privateVars.size())
715 return op->emitError("allocate private index is out of range");
716 if (!usedPrivateSlots.insert(privateIndex).second)
717 return op->emitError(
718 "allocate private index refers to a private variable more than once");
719
720 Value privateVar = privateVars[privateIndex];
721 if (allocateVar.getType() != privateVar.getType())
722 return op->emitError()
723 << "type mismatch between allocate variable and private variable "
724 "at index "
725 << privateIndex;
726 if (allocateVar != privateVar)
727 return op->emitError()
728 << "allocate variable does not match private variable at index "
729 << privateIndex;
730
731 if (!privateSyms ||
732 static_cast<uint64_t>(privateIndex) >= privateSyms.size())
733 return op->emitError(
734 "allocate private index does not have a privatizer symbol");
735
736 auto privateSym = dyn_cast<SymbolRefAttr>(privateSyms[privateIndex]);
737 if (!privateSym)
738 return op->emitError(
739 "allocate private index does not reference a privatizer symbol");
740 PrivateClauseOp privatizer =
742 if (!privatizer)
743 return op->emitError() << "failed to lookup privatizer op with symbol: '"
744 << privateSym << "'";
745 if (privatizer.getDataSharingType() != DataSharingClauseType::Private &&
746 privatizer.getDataSharingType() != DataSharingClauseType::FirstPrivate)
747 return op->emitError(
748 "allocate private index must refer to private or firstprivate "
749 "storage");
750 }
751
752 return success();
753}
754
755//===----------------------------------------------------------------------===//
756// Parser, printer and verifier for Schedule Clause
757//===----------------------------------------------------------------------===//
758
759static ParseResult
761 SmallVectorImpl<SmallString<12>> &modifiers) {
762 if (modifiers.size() > 2)
763 return parser.emitError(parser.getNameLoc()) << " unexpected modifier(s)";
764 for (const auto &mod : modifiers) {
765 // Translate the string. If it has no value, then it was not a valid
766 // modifier!
767 auto symbol = symbolizeScheduleModifier(mod);
768 if (!symbol)
769 return parser.emitError(parser.getNameLoc())
770 << " unknown modifier type: " << mod;
771 }
772
773 // If we have one modifier that is "simd", then stick a "none" modiifer in
774 // index 0.
775 if (modifiers.size() == 1) {
776 if (symbolizeScheduleModifier(modifiers[0]) == ScheduleModifier::simd) {
777 modifiers.push_back(modifiers[0]);
778 modifiers[0] = stringifyScheduleModifier(ScheduleModifier::none);
779 }
780 } else if (modifiers.size() == 2) {
781 // If there are two modifier:
782 // First modifier should not be simd, second one should be simd
783 if (symbolizeScheduleModifier(modifiers[0]) == ScheduleModifier::simd ||
784 symbolizeScheduleModifier(modifiers[1]) != ScheduleModifier::simd)
785 return parser.emitError(parser.getNameLoc())
786 << " incorrect modifier order";
787 }
788 return success();
789}
790
791/// schedule ::= `schedule` `(` sched-list `)`
792/// sched-list ::= sched-val | sched-val sched-list |
793/// sched-val `,` sched-modifier
794/// sched-val ::= sched-with-chunk | sched-wo-chunk
795/// sched-with-chunk ::= sched-with-chunk-types (`=` ssa-id-and-type)?
796/// sched-with-chunk-types ::= `static` | `dynamic` | `guided`
797/// sched-wo-chunk ::= `auto` | `runtime`
798/// sched-modifier ::= sched-mod-val | sched-mod-val `,` sched-mod-val
799/// sched-mod-val ::= `monotonic` | `nonmonotonic` | `simd` | `none`
800static ParseResult
801parseScheduleClause(OpAsmParser &parser, ClauseScheduleKindAttr &scheduleAttr,
802 ScheduleModifierAttr &scheduleMod, UnitAttr &scheduleSimd,
803 std::optional<OpAsmParser::UnresolvedOperand> &chunkSize,
804 Type &chunkType) {
805 StringRef keyword;
806 if (parser.parseKeyword(&keyword))
807 return failure();
808 std::optional<mlir::omp::ClauseScheduleKind> schedule =
809 symbolizeClauseScheduleKind(keyword);
810 if (!schedule)
811 return parser.emitError(parser.getNameLoc()) << " expected schedule kind";
812
813 scheduleAttr = ClauseScheduleKindAttr::get(parser.getContext(), *schedule);
814 switch (*schedule) {
815 case ClauseScheduleKind::Static:
816 case ClauseScheduleKind::Dynamic:
817 case ClauseScheduleKind::Guided:
818 if (succeeded(parser.parseOptionalEqual())) {
819 chunkSize = OpAsmParser::UnresolvedOperand{};
820 if (parser.parseOperand(*chunkSize) || parser.parseColonType(chunkType))
821 return failure();
822 } else {
823 chunkSize = std::nullopt;
824 }
825 break;
826 case ClauseScheduleKind::Auto:
827 case ClauseScheduleKind::Runtime:
828 case ClauseScheduleKind::Distribute:
829 chunkSize = std::nullopt;
830 }
831
832 // If there is a comma, we have one or more modifiers..
834 while (succeeded(parser.parseOptionalComma())) {
835 StringRef mod;
836 if (parser.parseKeyword(&mod))
837 return failure();
838 modifiers.push_back(mod);
839 }
840
841 if (verifyScheduleModifiers(parser, modifiers))
842 return failure();
843
844 if (!modifiers.empty()) {
845 SMLoc loc = parser.getCurrentLocation();
846 if (std::optional<ScheduleModifier> mod =
847 symbolizeScheduleModifier(modifiers[0])) {
848 scheduleMod = ScheduleModifierAttr::get(parser.getContext(), *mod);
849 } else {
850 return parser.emitError(loc, "invalid schedule modifier");
851 }
852 // Only SIMD attribute is allowed here!
853 if (modifiers.size() > 1) {
854 assert(symbolizeScheduleModifier(modifiers[1]) == ScheduleModifier::simd);
855 scheduleSimd = UnitAttr::get(parser.getBuilder().getContext());
856 }
857 }
858
859 return success();
860}
861
862/// Print schedule clause
864 ClauseScheduleKindAttr scheduleKind,
865 ScheduleModifierAttr scheduleMod,
866 UnitAttr scheduleSimd, Value scheduleChunk,
867 Type scheduleChunkType) {
868 p << stringifyClauseScheduleKind(scheduleKind.getValue());
869 if (scheduleChunk)
870 p << " = " << scheduleChunk << " : " << scheduleChunk.getType();
871 if (scheduleMod)
872 p << ", " << stringifyScheduleModifier(scheduleMod.getValue());
873 if (scheduleSimd)
874 p << ", simd";
875}
876
877//===----------------------------------------------------------------------===//
878// Parser and printer for Order Clause
879//===----------------------------------------------------------------------===//
880
881// order ::= `order` `(` [order-modifier ':'] concurrent `)`
882// order-modifier ::= reproducible | unconstrained
883static ParseResult parseOrderClause(OpAsmParser &parser,
884 ClauseOrderKindAttr &order,
885 OrderModifierAttr &orderMod) {
886 StringRef enumStr;
887 SMLoc loc = parser.getCurrentLocation();
888 if (parser.parseKeyword(&enumStr))
889 return failure();
890 if (std::optional<OrderModifier> enumValue =
891 symbolizeOrderModifier(enumStr)) {
892 orderMod = OrderModifierAttr::get(parser.getContext(), *enumValue);
893 if (parser.parseOptionalColon())
894 return failure();
895 loc = parser.getCurrentLocation();
896 if (parser.parseKeyword(&enumStr))
897 return failure();
898 }
899 if (std::optional<ClauseOrderKind> enumValue =
900 symbolizeClauseOrderKind(enumStr)) {
901 order = ClauseOrderKindAttr::get(parser.getContext(), *enumValue);
902 return success();
903 }
904 return parser.emitError(loc, "invalid clause value: '") << enumStr << "'";
905}
906
908 ClauseOrderKindAttr order,
909 OrderModifierAttr orderMod) {
910 if (orderMod)
911 p << stringifyOrderModifier(orderMod.getValue()) << ":";
912 if (order)
913 p << stringifyClauseOrderKind(order.getValue());
914}
915
916template <typename ClauseTypeAttr, typename ClauseType>
917static ParseResult
918parseGranularityClause(OpAsmParser &parser, ClauseTypeAttr &prescriptiveness,
919 std::optional<OpAsmParser::UnresolvedOperand> &operand,
920 Type &operandType,
921 std::optional<ClauseType> (*symbolizeClause)(StringRef),
922 StringRef clauseName) {
923 StringRef enumStr;
924 if (succeeded(parser.parseOptionalKeyword(&enumStr))) {
925 if (std::optional<ClauseType> enumValue = symbolizeClause(enumStr)) {
926 prescriptiveness = ClauseTypeAttr::get(parser.getContext(), *enumValue);
927 if (parser.parseComma())
928 return failure();
929 } else {
930 return parser.emitError(parser.getCurrentLocation())
931 << "invalid " << clauseName << " modifier : '" << enumStr << "'";
932 ;
933 }
934 }
935
937 if (succeeded(parser.parseOperand(var))) {
938 operand = var;
939 } else {
940 return parser.emitError(parser.getCurrentLocation())
941 << "expected " << clauseName << " operand";
942 }
943
944 if (operand.has_value()) {
945 if (parser.parseColonType(operandType))
946 return failure();
947 }
948
949 return success();
950}
951
952template <typename ClauseTypeAttr, typename ClauseType>
953static void
955 ClauseTypeAttr prescriptiveness, Value operand,
956 mlir::Type operandType,
957 StringRef (*stringifyClauseType)(ClauseType)) {
958
959 if (prescriptiveness)
960 p << stringifyClauseType(prescriptiveness.getValue()) << ", ";
961
962 if (operand)
963 p << operand << ": " << operandType;
964}
965
966//===----------------------------------------------------------------------===//
967// Parser and printer for grainsize Clause
968//===----------------------------------------------------------------------===//
969
970// grainsize ::= `grainsize` `(` [strict ':'] grain-size `)`
971static ParseResult
972parseGrainsizeClause(OpAsmParser &parser, ClauseGrainsizeTypeAttr &grainsizeMod,
973 std::optional<OpAsmParser::UnresolvedOperand> &grainsize,
974 Type &grainsizeType) {
976 parser, grainsizeMod, grainsize, grainsizeType,
977 &symbolizeClauseGrainsizeType, "grainsize");
978}
979
981 ClauseGrainsizeTypeAttr grainsizeMod,
982 Value grainsize, mlir::Type grainsizeType) {
984 p, op, grainsizeMod, grainsize, grainsizeType,
985 &stringifyClauseGrainsizeType);
986}
987
988//===----------------------------------------------------------------------===//
989// Parser and printer for num_tasks Clause
990//===----------------------------------------------------------------------===//
991
992// numtask ::= `num_tasks` `(` [strict ':'] num-tasks `)`
993static ParseResult
994parseNumTasksClause(OpAsmParser &parser, ClauseNumTasksTypeAttr &numTasksMod,
995 std::optional<OpAsmParser::UnresolvedOperand> &numTasks,
996 Type &numTasksType) {
998 parser, numTasksMod, numTasks, numTasksType, &symbolizeClauseNumTasksType,
999 "num_tasks");
1000}
1001
1003 ClauseNumTasksTypeAttr numTasksMod,
1004 Value numTasks, mlir::Type numTasksType) {
1006 p, op, numTasksMod, numTasks, numTasksType, &stringifyClauseNumTasksType);
1007}
1008
1009//===----------------------------------------------------------------------===//
1010// Parser and printer for Heap Alloc Clause
1011//===----------------------------------------------------------------------===//
1012
1013/// operation ::= $in_type ( `(` $typeparams `)` )? ( `,` $shape )?
1014static ParseResult parseHeapAllocClause(
1015 OpAsmParser &parser, TypeAttr &inTypeAttr,
1017 SmallVectorImpl<Type> &typeparamsTypes,
1019 SmallVectorImpl<Type> &shapeTypes) {
1020 mlir::Type inType;
1021 if (parser.parseType(inType))
1022 return mlir::failure();
1023 inTypeAttr = TypeAttr::get(inType);
1024
1025 if (!parser.parseOptionalLParen()) {
1026 // parse the LEN params of the derived type. (<params> : <types>)
1027 if (parser.parseOperandList(typeparams, OpAsmParser::Delimiter::None) ||
1028 parser.parseColonTypeList(typeparamsTypes) || parser.parseRParen())
1029 return failure();
1030 }
1031
1032 if (!parser.parseOptionalComma()) {
1033 // parse size to scale by, vector of n dimensions of type index
1035 return failure();
1036
1037 // TODO: This overrides the actual types of the operands, which might cause
1038 // issues when they don't match. At the moment this is done in place of
1039 // making the corresponding operand type `Variadic<Index>` because index
1040 // types are lowered to I64 prior to LLVM IR translation.
1041 shapeTypes.append(shape.size(), IndexType::get(parser.getContext()));
1042 }
1043
1044 return success();
1045}
1046
1048 TypeAttr inType, ValueRange typeparams,
1049 TypeRange typeparamsTypes, ValueRange shape,
1050 TypeRange shapeTypes) {
1051 p << inType;
1052 if (!typeparams.empty()) {
1053 p << '(' << typeparams << " : " << typeparamsTypes << ')';
1054 }
1055 for (auto sh : shape) {
1056 p << ", ";
1057 p.printOperand(sh);
1058 }
1059}
1060
1061//===----------------------------------------------------------------------===//
1062// Parser, printer and verify for dyn_groupprivate Clause
1063//===----------------------------------------------------------------------===//
1064
1065static LogicalResult
1066verifyDynGroupprivateClause(Operation *op, AccessGroupModifierAttr accessGroup,
1067 FallbackModifierAttr fallback,
1068 Value dynGroupprivateSize) {
1069 if (!dynGroupprivateSize && (accessGroup || fallback))
1070 return op->emitOpError("dyn_groupprivate modifiers require a size operand");
1071
1072 return success();
1073}
1074
1076 OpAsmParser &parser, AccessGroupModifierAttr &accessGroupAttr,
1077 FallbackModifierAttr &fallbackAttr,
1078 std::optional<OpAsmParser::UnresolvedOperand> &dynGroupprivateSize,
1079 Type &sizeType) {
1080
1081 bool parsedAccessGroup = false;
1082 bool parsedFallback = false;
1083 bool parsedSize = false;
1084
1085 return parser.parseCommaSeparatedList([&]() -> ParseResult {
1086 // Parse AccessGroupModifier.
1087 if (succeeded(parser.parseOptionalKeyword("cgroup"))) {
1088 if (parsedAccessGroup)
1089 return parser.emitError(parser.getCurrentLocation(),
1090 "duplicate access group modifier");
1091 accessGroupAttr = AccessGroupModifierAttr::get(
1092 parser.getContext(), AccessGroupModifier::cgroup);
1093 parsedAccessGroup = true;
1094 return success();
1095 }
1096 // Parse FallbackModifier.
1097 if (succeeded(parser.parseOptionalKeyword("fallback"))) {
1098 if (parsedFallback)
1099 return parser.emitError(parser.getCurrentLocation(),
1100 "duplicate fallback modifier");
1101 if (parser.parseLParen())
1102 return parser.emitError(parser.getCurrentLocation(),
1103 "expected '(' after 'fallback'");
1104 llvm::StringRef fbKind;
1105 if (parser.parseKeyword(&fbKind))
1106 return parser.emitError(
1107 parser.getCurrentLocation(),
1108 "expected fallback modifier (abort/null/default_mem)");
1109 std::optional<FallbackModifier> fbEnum;
1110 if (fbKind == "abort")
1111 fbEnum = FallbackModifier::abort;
1112 else if (fbKind == "null")
1113 fbEnum = FallbackModifier::null;
1114 else if (fbKind == "default_mem")
1115 fbEnum = FallbackModifier::default_mem;
1116 else
1117 return parser.emitError(parser.getCurrentLocation(),
1118 "invalid fallback modifier '" + fbKind + "'");
1119 fallbackAttr = FallbackModifierAttr::get(parser.getContext(), *fbEnum);
1120 if (parser.parseRParen())
1121 return parser.emitError(parser.getCurrentLocation(),
1122 "expected ')' after fallback modifier");
1123 parsedFallback = true;
1124 return success();
1125 }
1126 // Parse size operand.
1128 if (succeeded(parser.parseOperand(operand))) {
1129 if (parsedSize)
1130 return parser.emitError(parser.getCurrentLocation(),
1131 "duplicate size operand");
1132 dynGroupprivateSize = operand;
1133 parsedSize = true;
1134 if (failed(parser.parseColon()) || failed(parser.parseType(sizeType)))
1135 return parser.emitError(parser.getCurrentLocation(),
1136 "expected ':' and type after size operand");
1137 return success();
1138 }
1139 return parser.emitError(parser.getCurrentLocation(),
1140 "expected dyn_groupprivate_size operand");
1141 });
1142}
1143
1145 AccessGroupModifierAttr modifierFirst,
1146 FallbackModifierAttr modifierSecond,
1147 Value dynGroupprivateSize,
1148 Type sizeType) {
1149
1150 bool needsComma = false;
1151
1152 if (modifierFirst) {
1153 printer << modifierFirst.getValue();
1154 needsComma = true;
1155 }
1156
1157 if (modifierSecond) {
1158 if (needsComma)
1159 printer << ", ";
1160 printer << "fallback(";
1161 printer << modifierSecond.getValue();
1162 printer << ")";
1163 needsComma = true;
1164 }
1165
1166 if (dynGroupprivateSize) {
1167 if (needsComma)
1168 printer << ", ";
1169 printer << dynGroupprivateSize << " : " << sizeType;
1170 }
1171}
1172
1173//===----------------------------------------------------------------------===//
1174// Parser and printer for in_reduction Clause
1175//===----------------------------------------------------------------------===//
1176
1177/// Parses an `in_reduction` clause for an operation that does not give its
1178/// list items entry block arguments (e.g. `omp.target`). The expected format is
1179/// a comma-separated list of `[byref] @sym %var` followed by `: types`.
1180static ParseResult parseInReductionClause(
1181 OpAsmParser &parser,
1183 SmallVectorImpl<Type> &inReductionTypes,
1184 DenseBoolArrayAttr &inReductionByref, ArrayAttr &inReductionSyms) {
1186 SmallVector<bool> isByRefVec;
1187
1188 if (parser.parseCommaSeparatedList([&]() {
1189 isByRefVec.push_back(parser.parseOptionalKeyword("byref").succeeded());
1190 if (parser.parseAttribute(symbolVec.emplace_back()) ||
1191 parser.parseOperand(inReductionVars.emplace_back()))
1192 return failure();
1193 return success();
1194 }))
1195 return failure();
1196
1197 if (parser.parseColon())
1198 return failure();
1199
1200 if (parser.parseCommaSeparatedList(
1201 [&]() { return parser.parseType(inReductionTypes.emplace_back()); }))
1202 return failure();
1203
1204 if (inReductionVars.size() != inReductionTypes.size())
1205 return failure();
1206
1207 inReductionByref = makeDenseBoolArrayAttr(parser.getContext(), isByRefVec);
1208 SmallVector<Attribute> symbolAttrs(symbolVec.begin(), symbolVec.end());
1209 inReductionSyms = ArrayAttr::get(parser.getContext(), symbolAttrs);
1210 return success();
1211}
1212
1213/// Prints an `in_reduction` clause for an operation that does not give its list
1214/// items entry block arguments (e.g. `omp.target`). Mirrors
1215/// `parseInReductionClause`.
1217 ValueRange inReductionVars,
1218 TypeRange inReductionTypes,
1219 DenseBoolArrayAttr inReductionByref,
1220 ArrayAttr inReductionSyms) {
1221 MLIRContext *ctx = op->getContext();
1222
1223 ArrayAttr syms = inReductionSyms;
1224 if (!syms) {
1225 SmallVector<Attribute> values(inReductionVars.size(), nullptr);
1226 syms = ArrayAttr::get(ctx, values);
1227 }
1228
1229 DenseBoolArrayAttr byref = inReductionByref;
1230 if (!byref) {
1231 SmallVector<bool> values(inReductionVars.size(), false);
1232 byref = DenseBoolArrayAttr::get(ctx, values);
1233 }
1234
1235 llvm::interleaveComma(
1236 llvm::zip_equal(inReductionVars, syms.getValue(), byref.asArrayRef()), p,
1237 [&p](auto t) {
1238 auto [var, sym, isByRef] = t;
1239 if (isByRef)
1240 p << "byref ";
1241 if (sym)
1242 p << sym << " ";
1243 p << var;
1244 });
1245 p << " : ";
1246 llvm::interleaveComma(inReductionTypes, p);
1247}
1248
1249//===----------------------------------------------------------------------===//
1250// Parsers for operations including clauses that define entry block arguments.
1251//===----------------------------------------------------------------------===//
1252
1253namespace {
1254struct MapParseArgs {
1255 SmallVectorImpl<OpAsmParser::UnresolvedOperand> &vars;
1256 SmallVectorImpl<Type> &types;
1257 MapParseArgs(SmallVectorImpl<OpAsmParser::UnresolvedOperand> &vars,
1258 SmallVectorImpl<Type> &types)
1259 : vars(vars), types(types) {}
1260};
1261struct PrivateParseArgs {
1262 llvm::SmallVectorImpl<OpAsmParser::UnresolvedOperand> &vars;
1263 llvm::SmallVectorImpl<Type> &types;
1264 ArrayAttr &syms;
1265 UnitAttr &needsBarrier;
1266 DenseI64ArrayAttr *mapIndices;
1267 PrivateParseArgs(SmallVectorImpl<OpAsmParser::UnresolvedOperand> &vars,
1268 SmallVectorImpl<Type> &types, ArrayAttr &syms,
1269 UnitAttr &needsBarrier,
1270 DenseI64ArrayAttr *mapIndices = nullptr)
1271 : vars(vars), types(types), syms(syms), needsBarrier(needsBarrier),
1272 mapIndices(mapIndices) {}
1273};
1274
1275struct LinearParseArgs {
1276 SmallVectorImpl<OpAsmParser::UnresolvedOperand> &vars;
1277 SmallVectorImpl<Type> &types;
1278 SmallVectorImpl<OpAsmParser::UnresolvedOperand> &stepVars;
1279 SmallVectorImpl<Type> &stepTypes;
1280 ArrayAttr &varTypes;
1281 ArrayAttr &modifiers;
1282 ArrayAttr &syms;
1283 LinearParseArgs(SmallVectorImpl<OpAsmParser::UnresolvedOperand> &vars,
1284 SmallVectorImpl<Type> &types,
1285 SmallVectorImpl<OpAsmParser::UnresolvedOperand> &stepVars,
1286 SmallVectorImpl<Type> &stepTypes, ArrayAttr &varTypes,
1287 ArrayAttr &modifiers, ArrayAttr &syms)
1288 : vars(vars), types(types), stepVars(stepVars), stepTypes(stepTypes),
1289 varTypes(varTypes), modifiers(modifiers), syms(syms) {}
1290};
1291
1292struct ReductionParseArgs {
1293 SmallVectorImpl<OpAsmParser::UnresolvedOperand> &vars;
1294 SmallVectorImpl<Type> &types;
1295 DenseBoolArrayAttr &byref;
1296 ArrayAttr &syms;
1297 ArrayAttr *modifier;
1298 ReductionParseArgs(SmallVectorImpl<OpAsmParser::UnresolvedOperand> &vars,
1299 SmallVectorImpl<Type> &types, DenseBoolArrayAttr &byref,
1300 ArrayAttr &syms, ArrayAttr *mod = nullptr)
1301 : vars(vars), types(types), byref(byref), syms(syms), modifier(mod) {}
1302};
1303
1304struct AllRegionParseArgs {
1305 std::optional<MapParseArgs> hasDeviceAddrArgs;
1306 std::optional<MapParseArgs> hostEvalArgs;
1307 std::optional<ReductionParseArgs> inReductionArgs;
1308 std::optional<LinearParseArgs> linearArgs;
1309 std::optional<MapParseArgs> mapArgs;
1310 std::optional<PrivateParseArgs> privateArgs;
1311 std::optional<ReductionParseArgs> reductionArgs;
1312 std::optional<ReductionParseArgs> taskReductionArgs;
1313 std::optional<MapParseArgs> useDeviceAddrArgs;
1314 std::optional<MapParseArgs> useDevicePtrArgs;
1315};
1316} // namespace
1317
1318static inline constexpr StringRef getPrivateNeedsBarrierSpelling() {
1319 return "private_barrier";
1320}
1321
1322static ParseResult parseClauseWithRegionArgs(
1323 OpAsmParser &parser,
1325 SmallVectorImpl<Type> &types,
1326 SmallVectorImpl<OpAsmParser::Argument> &regionPrivateArgs,
1327 ArrayAttr *symbols = nullptr, DenseI64ArrayAttr *mapIndices = nullptr,
1328 DenseBoolArrayAttr *byref = nullptr, ArrayAttr *modifier = nullptr,
1329 UnitAttr *needsBarrier = nullptr) {
1331 SmallVector<int64_t> mapIndicesVec;
1332 SmallVector<bool> isByRefVec;
1333 unsigned regionArgOffset = regionPrivateArgs.size();
1334
1335 if (parser.parseLParen())
1336 return failure();
1337
1338 SmallVector<Attribute> modifiers;
1339 if (parser.parseCommaSeparatedList([&]() -> ParseResult {
1340 if (modifier) {
1341 ReductionModifier mod = ReductionModifier::defaultmod;
1342 if (succeeded(parser.parseOptionalKeyword("task")))
1343 mod = ReductionModifier::task;
1344 else if (succeeded(parser.parseOptionalKeyword("inscan")))
1345 mod = ReductionModifier::inscan;
1346 else if (succeeded(parser.parseOptionalKeyword("defaultmod")))
1347 mod = ReductionModifier::defaultmod;
1348 modifiers.push_back(
1349 ReductionModifierAttr::get(parser.getContext(), mod));
1350 }
1351 if (byref)
1352 isByRefVec.push_back(
1353 parser.parseOptionalKeyword("byref").succeeded());
1354
1355 if (symbols && parser.parseAttribute(symbolVec.emplace_back()))
1356 return failure();
1357
1358 if (parser.parseOperand(operands.emplace_back()) ||
1359 parser.parseArrow() ||
1360 parser.parseArgument(regionPrivateArgs.emplace_back()))
1361 return failure();
1362
1363 if (mapIndices) {
1364 if (parser.parseOptionalLSquare().succeeded()) {
1365 if (parser.parseKeyword("map_idx") || parser.parseEqual() ||
1366 parser.parseInteger(mapIndicesVec.emplace_back()) ||
1367 parser.parseRSquare())
1368 return failure();
1369 } else {
1370 mapIndicesVec.push_back(-1);
1371 }
1372 }
1373
1374 return success();
1375 }))
1376 return failure();
1377
1378 if (parser.parseColon() || parser.parseCommaSeparatedList([&]() {
1379 return parser.parseType(types.emplace_back());
1380 }))
1381 return failure();
1382
1383 if (llvm::any_of(modifiers, [](Attribute attr) {
1384 return cast<ReductionModifierAttr>(attr).getValue() !=
1385 ReductionModifier::defaultmod;
1386 }))
1387 *modifier = ArrayAttr::get(parser.getContext(), modifiers);
1388
1389 if (operands.size() != types.size())
1390 return failure();
1391
1392 if (parser.parseRParen())
1393 return failure();
1394
1395 if (needsBarrier) {
1397 .succeeded())
1398 *needsBarrier = mlir::UnitAttr::get(parser.getContext());
1399 }
1400
1401 auto *argsBegin = regionPrivateArgs.begin();
1402 MutableArrayRef argsSubrange(argsBegin + regionArgOffset,
1403 argsBegin + regionArgOffset + types.size());
1404 for (auto [prv, type] : llvm::zip_equal(argsSubrange, types)) {
1405 prv.type = type;
1406 }
1407
1408 if (symbols) {
1409 SmallVector<Attribute> symbolAttrs(symbolVec.begin(), symbolVec.end());
1410 *symbols = ArrayAttr::get(parser.getContext(), symbolAttrs);
1411 }
1412
1413 if (!mapIndicesVec.empty())
1414 *mapIndices =
1415 mlir::DenseI64ArrayAttr::get(parser.getContext(), mapIndicesVec);
1416
1417 if (byref)
1418 *byref = makeDenseBoolArrayAttr(parser.getContext(), isByRefVec);
1419
1420 return success();
1421}
1422
1423static ParseResult parseBlockArgClause(
1424 OpAsmParser &parser,
1426 StringRef keyword, std::optional<MapParseArgs> mapArgs) {
1427 if (succeeded(parser.parseOptionalKeyword(keyword))) {
1428 if (!mapArgs)
1429 return failure();
1430
1431 if (failed(parseClauseWithRegionArgs(parser, mapArgs->vars, mapArgs->types,
1432 entryBlockArgs)))
1433 return failure();
1434 }
1435 return success();
1436}
1437
1438static ParseResult parseBlockArgClause(
1439 OpAsmParser &parser,
1441 StringRef keyword, std::optional<PrivateParseArgs> privateArgs) {
1442 if (succeeded(parser.parseOptionalKeyword(keyword))) {
1443 if (!privateArgs)
1444 return failure();
1445
1446 if (failed(parseClauseWithRegionArgs(
1447 parser, privateArgs->vars, privateArgs->types, entryBlockArgs,
1448 &privateArgs->syms, privateArgs->mapIndices, /*byref=*/nullptr,
1449 /*modifier=*/nullptr, &privateArgs->needsBarrier)))
1450 return failure();
1451 }
1452 return success();
1453}
1454
1455static ParseResult parseBlockArgClause(
1456 OpAsmParser &parser,
1458 StringRef keyword, std::optional<ReductionParseArgs> reductionArgs) {
1459 if (succeeded(parser.parseOptionalKeyword(keyword))) {
1460 if (!reductionArgs)
1461 return failure();
1462 if (failed(parseClauseWithRegionArgs(
1463 parser, reductionArgs->vars, reductionArgs->types, entryBlockArgs,
1464 &reductionArgs->syms, /*mapIndices=*/nullptr, &reductionArgs->byref,
1465 reductionArgs->modifier)))
1466 return failure();
1467 }
1468 return success();
1469}
1470
1471/// linear-entry := [modifier `(`] symbol ssa-id `->` ssa-id `:` type `=`
1472/// ssa-id `:` type [`)`]
1473static ParseResult parseBlockArgClause(
1474 OpAsmParser &parser,
1476 StringRef keyword, std::optional<LinearParseArgs> linearArgs) {
1477 if (!linearArgs || failed(parser.parseOptionalKeyword(keyword)))
1478 return success();
1479
1480 MLIRContext *ctx = parser.getContext();
1481 SmallVector<Attribute> syms, modifiers;
1482 if (parser.parseLParen() ||
1483 parser.parseCommaSeparatedList([&]() -> ParseResult {
1484 std::optional<LinearModifier> modifier;
1485 if (succeeded(parser.parseOptionalKeyword("val")))
1486 modifier = LinearModifier::val;
1487 else if (succeeded(parser.parseOptionalKeyword("ref")))
1488 modifier = LinearModifier::ref;
1489 else if (succeeded(parser.parseOptionalKeyword("uval")))
1490 modifier = LinearModifier::uval;
1491 if (modifier && parser.parseLParen())
1492 return failure();
1493
1494 SymbolRefAttr sym;
1495 OpAsmParser::Argument &arg = entryBlockArgs.emplace_back();
1496 if (parser.parseAttribute(sym) ||
1497 parser.parseOperand(linearArgs->vars.emplace_back()) ||
1498 parser.parseArrow() || parser.parseArgument(arg) ||
1499 parser.parseColonType(linearArgs->types.emplace_back()) ||
1500 parser.parseEqual() ||
1501 parser.parseOperand(linearArgs->stepVars.emplace_back()) ||
1502 parser.parseColonType(linearArgs->stepTypes.emplace_back()))
1503 return failure();
1504 arg.type = linearArgs->types.back();
1505
1506 if (modifier && parser.parseRParen())
1507 return failure();
1508
1509 syms.push_back(sym);
1510 modifiers.push_back(
1511 modifier ? Attribute(LinearModifierAttr::get(ctx, *modifier))
1512 : Attribute(UnitAttr::get(ctx)));
1513 return success();
1514 }) ||
1515 parser.parseRParen())
1516 return failure();
1517
1518 linearArgs->syms = ArrayAttr::get(ctx, syms);
1519 linearArgs->modifiers = ArrayAttr::get(ctx, modifiers);
1520
1521 if (succeeded(parser.parseOptionalKeyword("linear_var_types")) &&
1522 (parser.parseLParen() || parser.parseAttribute(linearArgs->varTypes) ||
1523 parser.parseRParen()))
1524 return failure();
1525 return success();
1526}
1527
1528static ParseResult parseBlockArgRegion(OpAsmParser &parser, Region &region,
1529 AllRegionParseArgs args) {
1531
1532 if (failed(parseBlockArgClause(parser, entryBlockArgs, "has_device_addr",
1533 args.hasDeviceAddrArgs)))
1534 return parser.emitError(parser.getCurrentLocation())
1535 << "invalid `has_device_addr` format";
1536
1537 if (failed(parseBlockArgClause(parser, entryBlockArgs, "host_eval",
1538 args.hostEvalArgs)))
1539 return parser.emitError(parser.getCurrentLocation())
1540 << "invalid `host_eval` format";
1541
1542 if (failed(parseBlockArgClause(parser, entryBlockArgs, "in_reduction",
1543 args.inReductionArgs)))
1544 return parser.emitError(parser.getCurrentLocation())
1545 << "invalid `in_reduction` format";
1546
1547 if (failed(parseBlockArgClause(parser, entryBlockArgs, "linear",
1548 args.linearArgs)))
1549 return parser.emitError(parser.getCurrentLocation())
1550 << "invalid `linear` format";
1551
1552 if (failed(parseBlockArgClause(parser, entryBlockArgs, "map_entries",
1553 args.mapArgs)))
1554 return parser.emitError(parser.getCurrentLocation())
1555 << "invalid `map_entries` format";
1556
1557 if (failed(parseBlockArgClause(parser, entryBlockArgs, "private",
1558 args.privateArgs)))
1559 return parser.emitError(parser.getCurrentLocation())
1560 << "invalid `private` format";
1561
1562 if (failed(parseBlockArgClause(parser, entryBlockArgs, "reduction",
1563 args.reductionArgs)))
1564 return parser.emitError(parser.getCurrentLocation())
1565 << "invalid `reduction` format";
1566
1567 if (failed(parseBlockArgClause(parser, entryBlockArgs, "task_reduction",
1568 args.taskReductionArgs)))
1569 return parser.emitError(parser.getCurrentLocation())
1570 << "invalid `task_reduction` format";
1571
1572 if (failed(parseBlockArgClause(parser, entryBlockArgs, "use_device_addr",
1573 args.useDeviceAddrArgs)))
1574 return parser.emitError(parser.getCurrentLocation())
1575 << "invalid `use_device_addr` format";
1576
1577 if (failed(parseBlockArgClause(parser, entryBlockArgs, "use_device_ptr",
1578 args.useDevicePtrArgs)))
1579 return parser.emitError(parser.getCurrentLocation())
1580 << "invalid `use_device_addr` format";
1581
1582 return parser.parseRegion(region, entryBlockArgs);
1583}
1584
1585// These parseXyz functions correspond to the custom<Xyz> definitions
1586// in the .td file(s).
1587static ParseResult parseTargetOpRegion(
1588 OpAsmParser &parser, Region &region,
1590 SmallVectorImpl<Type> &hasDeviceAddrTypes,
1592 SmallVectorImpl<Type> &hostEvalTypes,
1594 SmallVectorImpl<Type> &mapTypes,
1596 llvm::SmallVectorImpl<Type> &privateTypes, ArrayAttr &privateSyms,
1597 UnitAttr &privateNeedsBarrier, DenseI64ArrayAttr &privateMaps) {
1598 AllRegionParseArgs args;
1599 args.hasDeviceAddrArgs.emplace(hasDeviceAddrVars, hasDeviceAddrTypes);
1600 args.hostEvalArgs.emplace(hostEvalVars, hostEvalTypes);
1601 args.mapArgs.emplace(mapVars, mapTypes);
1602 args.privateArgs.emplace(privateVars, privateTypes, privateSyms,
1603 privateNeedsBarrier, &privateMaps);
1604 return parseBlockArgRegion(parser, region, args);
1605}
1606
1608 OpAsmParser &parser, Region &region,
1610 SmallVectorImpl<Type> &inReductionTypes,
1611 DenseBoolArrayAttr &inReductionByref, ArrayAttr &inReductionSyms,
1613 llvm::SmallVectorImpl<Type> &privateTypes, ArrayAttr &privateSyms,
1614 UnitAttr &privateNeedsBarrier) {
1615 AllRegionParseArgs args;
1616 args.inReductionArgs.emplace(inReductionVars, inReductionTypes,
1617 inReductionByref, inReductionSyms);
1618 args.privateArgs.emplace(privateVars, privateTypes, privateSyms,
1619 privateNeedsBarrier);
1620 return parseBlockArgRegion(parser, region, args);
1621}
1622
1624 OpAsmParser &parser, Region &region,
1626 SmallVectorImpl<Type> &inReductionTypes,
1627 DenseBoolArrayAttr &inReductionByref, ArrayAttr &inReductionSyms,
1629 llvm::SmallVectorImpl<Type> &privateTypes, ArrayAttr &privateSyms,
1630 UnitAttr &privateNeedsBarrier, ArrayAttr &reductionMods,
1632 SmallVectorImpl<Type> &reductionTypes, DenseBoolArrayAttr &reductionByref,
1633 ArrayAttr &reductionSyms) {
1634 AllRegionParseArgs args;
1635 args.inReductionArgs.emplace(inReductionVars, inReductionTypes,
1636 inReductionByref, inReductionSyms);
1637 args.privateArgs.emplace(privateVars, privateTypes, privateSyms,
1638 privateNeedsBarrier);
1639 args.reductionArgs.emplace(reductionVars, reductionTypes, reductionByref,
1640 reductionSyms, &reductionMods);
1641 return parseBlockArgRegion(parser, region, args);
1642}
1643
1644static ParseResult parsePrivateRegion(
1645 OpAsmParser &parser, Region &region,
1647 llvm::SmallVectorImpl<Type> &privateTypes, ArrayAttr &privateSyms,
1648 UnitAttr &privateNeedsBarrier) {
1649 AllRegionParseArgs args;
1650 args.privateArgs.emplace(privateVars, privateTypes, privateSyms,
1651 privateNeedsBarrier);
1652 return parseBlockArgRegion(parser, region, args);
1653}
1654
1656 OpAsmParser &parser, Region &region,
1658 llvm::SmallVectorImpl<Type> &privateTypes, ArrayAttr &privateSyms,
1659 UnitAttr &privateNeedsBarrier, ArrayAttr &reductionMods,
1661 SmallVectorImpl<Type> &reductionTypes, DenseBoolArrayAttr &reductionByref,
1662 ArrayAttr &reductionSyms) {
1663 AllRegionParseArgs args;
1664 args.privateArgs.emplace(privateVars, privateTypes, privateSyms,
1665 privateNeedsBarrier);
1666 args.reductionArgs.emplace(reductionVars, reductionTypes, reductionByref,
1667 reductionSyms, &reductionMods);
1668 return parseBlockArgRegion(parser, region, args);
1669}
1670
1672 OpAsmParser &parser, Region &region,
1674 SmallVectorImpl<Type> &linearTypes,
1676 SmallVectorImpl<Type> &linearStepTypes, ArrayAttr &linearVarTypes,
1677 ArrayAttr &linearModifiers, ArrayAttr &linearSyms,
1679 llvm::SmallVectorImpl<Type> &privateTypes, ArrayAttr &privateSyms,
1680 UnitAttr &privateNeedsBarrier, ArrayAttr &reductionMods,
1682 SmallVectorImpl<Type> &reductionTypes, DenseBoolArrayAttr &reductionByref,
1683 ArrayAttr &reductionSyms) {
1684 AllRegionParseArgs args;
1685 args.linearArgs.emplace(linearVars, linearTypes, linearStepVars,
1686 linearStepTypes, linearVarTypes, linearModifiers,
1687 linearSyms);
1688 args.privateArgs.emplace(privateVars, privateTypes, privateSyms,
1689 privateNeedsBarrier);
1690 args.reductionArgs.emplace(reductionVars, reductionTypes, reductionByref,
1691 reductionSyms, &reductionMods);
1692 return parseBlockArgRegion(parser, region, args);
1693}
1694
1695static ParseResult parseTaskReductionRegion(
1696 OpAsmParser &parser, Region &region,
1698 SmallVectorImpl<Type> &taskReductionTypes,
1699 DenseBoolArrayAttr &taskReductionByref, ArrayAttr &taskReductionSyms) {
1700 AllRegionParseArgs args;
1701 args.taskReductionArgs.emplace(taskReductionVars, taskReductionTypes,
1702 taskReductionByref, taskReductionSyms);
1703 return parseBlockArgRegion(parser, region, args);
1704}
1705
1707 OpAsmParser &parser, Region &region,
1709 SmallVectorImpl<Type> &useDeviceAddrTypes,
1711 SmallVectorImpl<Type> &useDevicePtrTypes) {
1712 AllRegionParseArgs args;
1713 args.useDeviceAddrArgs.emplace(useDeviceAddrVars, useDeviceAddrTypes);
1714 args.useDevicePtrArgs.emplace(useDevicePtrVars, useDevicePtrTypes);
1715 return parseBlockArgRegion(parser, region, args);
1716}
1717
1718//===----------------------------------------------------------------------===//
1719// Printers for operations including clauses that define entry block arguments.
1720//===----------------------------------------------------------------------===//
1721
1722namespace {
1723struct MapPrintArgs {
1724 ValueRange vars;
1725 TypeRange types;
1726 MapPrintArgs(ValueRange vars, TypeRange types) : vars(vars), types(types) {}
1727};
1728struct PrivatePrintArgs {
1729 ValueRange vars;
1730 TypeRange types;
1731 ArrayAttr syms;
1732 UnitAttr needsBarrier;
1733 DenseI64ArrayAttr mapIndices;
1734 PrivatePrintArgs(ValueRange vars, TypeRange types, ArrayAttr syms,
1735 UnitAttr needsBarrier, DenseI64ArrayAttr mapIndices)
1736 : vars(vars), types(types), syms(syms), needsBarrier(needsBarrier),
1737 mapIndices(mapIndices) {}
1738};
1739struct LinearPrintArgs {
1740 ValueRange vars;
1741 TypeRange types;
1742 ValueRange stepVars;
1743 TypeRange stepTypes;
1744 ArrayAttr varTypes;
1745 ArrayAttr modifiers;
1746 ArrayAttr syms;
1747 LinearPrintArgs(ValueRange vars, TypeRange types, ValueRange stepVars,
1748 TypeRange stepTypes, ArrayAttr varTypes, ArrayAttr modifiers,
1749 ArrayAttr syms)
1750 : vars(vars), types(types), stepVars(stepVars), stepTypes(stepTypes),
1751 varTypes(varTypes), modifiers(modifiers), syms(syms) {}
1752};
1753struct ReductionPrintArgs {
1754 ValueRange vars;
1755 TypeRange types;
1756 DenseBoolArrayAttr byref;
1757 ArrayAttr syms;
1758 ArrayAttr modifier;
1759 ReductionPrintArgs(ValueRange vars, TypeRange types, DenseBoolArrayAttr byref,
1760 ArrayAttr syms, ArrayAttr mod = nullptr)
1761 : vars(vars), types(types), byref(byref), syms(syms), modifier(mod) {}
1762};
1763struct AllRegionPrintArgs {
1764 std::optional<MapPrintArgs> hasDeviceAddrArgs;
1765 std::optional<MapPrintArgs> hostEvalArgs;
1766 std::optional<ReductionPrintArgs> inReductionArgs;
1767 std::optional<LinearPrintArgs> linearArgs;
1768 std::optional<MapPrintArgs> mapArgs;
1769 std::optional<PrivatePrintArgs> privateArgs;
1770 std::optional<ReductionPrintArgs> reductionArgs;
1771 std::optional<ReductionPrintArgs> taskReductionArgs;
1772 std::optional<MapPrintArgs> useDeviceAddrArgs;
1773 std::optional<MapPrintArgs> useDevicePtrArgs;
1774};
1775} // namespace
1776
1778 OpAsmPrinter &p, MLIRContext *ctx, StringRef clauseName,
1779 ValueRange argsSubrange, ValueRange operands, TypeRange types,
1780 ArrayAttr symbols = nullptr, DenseI64ArrayAttr mapIndices = nullptr,
1781 DenseBoolArrayAttr byref = nullptr, ArrayAttr modifier = nullptr,
1782 UnitAttr needsBarrier = nullptr) {
1783 if (argsSubrange.empty())
1784 return;
1785
1786 p << clauseName << "(";
1787
1788 if (!symbols) {
1789 llvm::SmallVector<Attribute> values(operands.size(), nullptr);
1790 symbols = ArrayAttr::get(ctx, values);
1791 }
1792
1793 if (!mapIndices) {
1794 llvm::SmallVector<int64_t> values(operands.size(), -1);
1795 mapIndices = DenseI64ArrayAttr::get(ctx, values);
1796 }
1797
1798 if (!byref) {
1799 mlir::SmallVector<bool> values(operands.size(), false);
1800 byref = DenseBoolArrayAttr::get(ctx, values);
1801 }
1802
1803 unsigned index = 0;
1804 llvm::interleaveComma(
1805 llvm::zip_equal(operands, argsSubrange, symbols, mapIndices.asArrayRef(),
1806 byref.asArrayRef()),
1807 p, [&](auto t) {
1808 auto [op, arg, sym, map, isByRef] = t;
1809 if (modifier) {
1810 ReductionModifier mod =
1811 cast<ReductionModifierAttr>(modifier[index]).getValue();
1812 if (mod != ReductionModifier::defaultmod)
1813 p << stringifyReductionModifier(mod) << " ";
1814 }
1815 ++index;
1816 if (isByRef)
1817 p << "byref ";
1818 if (sym)
1819 p << sym << " ";
1820 p << op << " -> " << arg;
1821 if (map != -1)
1822 p << " [map_idx=" << map << "]";
1823 });
1824 p << " : ";
1825 llvm::interleaveComma(types, p);
1826 p << ") ";
1827
1828 if (needsBarrier)
1829 p << getPrivateNeedsBarrierSpelling() << " ";
1830}
1831
1833 StringRef clauseName, ValueRange argsSubrange,
1834 std::optional<MapPrintArgs> mapArgs) {
1835 if (mapArgs)
1836 printClauseWithRegionArgs(p, ctx, clauseName, argsSubrange, mapArgs->vars,
1837 mapArgs->types);
1838}
1839
1841 StringRef clauseName, ValueRange argsSubrange,
1842 std::optional<PrivatePrintArgs> privateArgs) {
1843 if (privateArgs)
1845 p, ctx, clauseName, argsSubrange, privateArgs->vars, privateArgs->types,
1846 privateArgs->syms, privateArgs->mapIndices, /*byref=*/nullptr,
1847 /*modifier=*/nullptr, privateArgs->needsBarrier);
1848}
1849
1850static void
1851printBlockArgClause(OpAsmPrinter &p, MLIRContext *ctx, StringRef clauseName,
1852 ValueRange argsSubrange,
1853 std::optional<ReductionPrintArgs> reductionArgs) {
1854 if (reductionArgs)
1855 printClauseWithRegionArgs(p, ctx, clauseName, argsSubrange,
1856 reductionArgs->vars, reductionArgs->types,
1857 reductionArgs->syms, /*mapIndices=*/nullptr,
1858 reductionArgs->byref, reductionArgs->modifier);
1859}
1860
1862 StringRef clauseName, ValueRange argsSubrange,
1863 std::optional<LinearPrintArgs> linearArgs) {
1864 if (!linearArgs || argsSubrange.empty())
1865 return;
1866
1867 p << clauseName << "(";
1868 for (auto [i, var] : llvm::enumerate(linearArgs->vars)) {
1869 if (i != 0)
1870 p << ", ";
1871 auto modifier =
1872 linearArgs->modifiers
1873 ? dyn_cast_if_present<LinearModifierAttr>(linearArgs->modifiers[i])
1874 : nullptr;
1875 if (modifier)
1876 p << stringifyLinearModifier(modifier.getValue()) << "(";
1877 if (linearArgs->syms)
1878 p << linearArgs->syms[i] << " ";
1879 p << var << " -> " << argsSubrange[i] << " : " << linearArgs->types[i]
1880 << " = " << linearArgs->stepVars[i] << " : " << linearArgs->stepTypes[i];
1881 if (modifier)
1882 p << ")";
1883 }
1884 p << ") ";
1885
1886 if (linearArgs->varTypes)
1887 p << "linear_var_types(" << linearArgs->varTypes << ") ";
1888}
1889
1891 const AllRegionPrintArgs &args) {
1892 auto iface = llvm::cast<mlir::omp::BlockArgOpenMPOpInterface>(op);
1893 MLIRContext *ctx = op->getContext();
1894
1895 printBlockArgClause(p, ctx, "has_device_addr",
1896 iface.getHasDeviceAddrBlockArgs(),
1897 args.hasDeviceAddrArgs);
1898 printBlockArgClause(p, ctx, "host_eval", iface.getHostEvalBlockArgs(),
1899 args.hostEvalArgs);
1900 printBlockArgClause(p, ctx, "in_reduction", iface.getInReductionBlockArgs(),
1901 args.inReductionArgs);
1902 printBlockArgClause(p, ctx, "linear", iface.getLinearBlockArgs(),
1903 args.linearArgs);
1904 printBlockArgClause(p, ctx, "map_entries", iface.getMapBlockArgs(),
1905 args.mapArgs);
1906 printBlockArgClause(p, ctx, "private", iface.getPrivateBlockArgs(),
1907 args.privateArgs);
1908 printBlockArgClause(p, ctx, "reduction", iface.getReductionBlockArgs(),
1909 args.reductionArgs);
1910 printBlockArgClause(p, ctx, "task_reduction",
1911 iface.getTaskReductionBlockArgs(),
1912 args.taskReductionArgs);
1913 printBlockArgClause(p, ctx, "use_device_addr",
1914 iface.getUseDeviceAddrBlockArgs(),
1915 args.useDeviceAddrArgs);
1916 printBlockArgClause(p, ctx, "use_device_ptr",
1917 iface.getUseDevicePtrBlockArgs(), args.useDevicePtrArgs);
1918
1919 p.printRegion(region, /*printEntryBlockArgs=*/false);
1920}
1921
1922// These parseXyz functions correspond to the custom<Xyz> definitions
1923// in the .td file(s).
1925 ValueRange hasDeviceAddrVars,
1926 TypeRange hasDeviceAddrTypes,
1927 ValueRange hostEvalVars,
1928 TypeRange hostEvalTypes, ValueRange mapVars,
1929 TypeRange mapTypes, ValueRange privateVars,
1930 TypeRange privateTypes, ArrayAttr privateSyms,
1931 UnitAttr privateNeedsBarrier,
1932 DenseI64ArrayAttr privateMaps) {
1933 AllRegionPrintArgs args;
1934 args.hasDeviceAddrArgs.emplace(hasDeviceAddrVars, hasDeviceAddrTypes);
1935 args.hostEvalArgs.emplace(hostEvalVars, hostEvalTypes);
1936 args.mapArgs.emplace(mapVars, mapTypes);
1937 args.privateArgs.emplace(privateVars, privateTypes, privateSyms,
1938 privateNeedsBarrier, privateMaps);
1939 printBlockArgRegion(p, op, region, args);
1940}
1941
1943 OpAsmPrinter &p, Operation *op, Region &region, ValueRange inReductionVars,
1944 TypeRange inReductionTypes, DenseBoolArrayAttr inReductionByref,
1945 ArrayAttr inReductionSyms, ValueRange privateVars, TypeRange privateTypes,
1946 ArrayAttr privateSyms, UnitAttr privateNeedsBarrier) {
1947 AllRegionPrintArgs args;
1948 args.inReductionArgs.emplace(inReductionVars, inReductionTypes,
1949 inReductionByref, inReductionSyms);
1950 args.privateArgs.emplace(privateVars, privateTypes, privateSyms,
1951 privateNeedsBarrier,
1952 /*mapIndices=*/nullptr);
1953 printBlockArgRegion(p, op, region, args);
1954}
1955
1957 OpAsmPrinter &p, Operation *op, Region &region, ValueRange inReductionVars,
1958 TypeRange inReductionTypes, DenseBoolArrayAttr inReductionByref,
1959 ArrayAttr inReductionSyms, ValueRange privateVars, TypeRange privateTypes,
1960 ArrayAttr privateSyms, UnitAttr privateNeedsBarrier,
1961 ArrayAttr reductionMods, ValueRange reductionVars, TypeRange reductionTypes,
1962 DenseBoolArrayAttr reductionByref, ArrayAttr reductionSyms) {
1963 AllRegionPrintArgs args;
1964 args.inReductionArgs.emplace(inReductionVars, inReductionTypes,
1965 inReductionByref, inReductionSyms);
1966 args.privateArgs.emplace(privateVars, privateTypes, privateSyms,
1967 privateNeedsBarrier,
1968 /*mapIndices=*/nullptr);
1969 args.reductionArgs.emplace(reductionVars, reductionTypes, reductionByref,
1970 reductionSyms, reductionMods);
1971 printBlockArgRegion(p, op, region, args);
1972}
1973
1975 ValueRange privateVars, TypeRange privateTypes,
1976 ArrayAttr privateSyms,
1977 UnitAttr privateNeedsBarrier) {
1978 AllRegionPrintArgs args;
1979 args.privateArgs.emplace(privateVars, privateTypes, privateSyms,
1980 privateNeedsBarrier,
1981 /*mapIndices=*/nullptr);
1982 printBlockArgRegion(p, op, region, args);
1983}
1984
1986 OpAsmPrinter &p, Operation *op, Region &region, ValueRange privateVars,
1987 TypeRange privateTypes, ArrayAttr privateSyms, UnitAttr privateNeedsBarrier,
1988 ArrayAttr reductionMods, ValueRange reductionVars, TypeRange reductionTypes,
1989 DenseBoolArrayAttr reductionByref, ArrayAttr reductionSyms) {
1990 AllRegionPrintArgs args;
1991 args.privateArgs.emplace(privateVars, privateTypes, privateSyms,
1992 privateNeedsBarrier,
1993 /*mapIndices=*/nullptr);
1994 args.reductionArgs.emplace(reductionVars, reductionTypes, reductionByref,
1995 reductionSyms, reductionMods);
1996 printBlockArgRegion(p, op, region, args);
1997}
1998
2000 OpAsmPrinter &p, Operation *op, Region &region, ValueRange linearVars,
2001 TypeRange linearTypes, ValueRange linearStepVars, TypeRange linearStepTypes,
2002 ArrayAttr linearVarTypes, ArrayAttr linearModifiers, ArrayAttr linearSyms,
2003 ValueRange privateVars, TypeRange privateTypes, ArrayAttr privateSyms,
2004 UnitAttr privateNeedsBarrier, ArrayAttr reductionMods,
2005 ValueRange reductionVars, TypeRange reductionTypes,
2006 DenseBoolArrayAttr reductionByref, ArrayAttr reductionSyms) {
2007 AllRegionPrintArgs args;
2008 args.linearArgs.emplace(linearVars, linearTypes, linearStepVars,
2009 linearStepTypes, linearVarTypes, linearModifiers,
2010 linearSyms);
2011 args.privateArgs.emplace(privateVars, privateTypes, privateSyms,
2012 privateNeedsBarrier,
2013 /*mapIndices=*/nullptr);
2014 args.reductionArgs.emplace(reductionVars, reductionTypes, reductionByref,
2015 reductionSyms, reductionMods);
2016 printBlockArgRegion(p, op, region, args);
2017}
2018
2020 Region &region,
2021 ValueRange taskReductionVars,
2022 TypeRange taskReductionTypes,
2023 DenseBoolArrayAttr taskReductionByref,
2024 ArrayAttr taskReductionSyms) {
2025 AllRegionPrintArgs args;
2026 args.taskReductionArgs.emplace(taskReductionVars, taskReductionTypes,
2027 taskReductionByref, taskReductionSyms);
2028 printBlockArgRegion(p, op, region, args);
2029}
2030
2032 Region &region,
2033 ValueRange useDeviceAddrVars,
2034 TypeRange useDeviceAddrTypes,
2035 ValueRange useDevicePtrVars,
2036 TypeRange useDevicePtrTypes) {
2037 AllRegionPrintArgs args;
2038 args.useDeviceAddrArgs.emplace(useDeviceAddrVars, useDeviceAddrTypes);
2039 args.useDevicePtrArgs.emplace(useDevicePtrVars, useDevicePtrTypes);
2040 printBlockArgRegion(p, op, region, args);
2041}
2042
2043template <typename ParsePrefixFn>
2044static ParseResult parseSplitIteratedList(
2045 OpAsmParser &parser,
2047 SmallVectorImpl<Type> &iteratedTypes,
2049 SmallVectorImpl<Type> &plainTypes, ParsePrefixFn &&parsePrefix) {
2050
2051 return parser.parseCommaSeparatedList([&]() -> ParseResult {
2052 if (failed(parsePrefix()))
2053 return failure();
2054
2056 Type ty;
2057 if (parser.parseOperand(v) || parser.parseColonType(ty))
2058 return failure();
2059
2060 if (llvm::isa<mlir::omp::IteratedType>(ty)) {
2061 iteratedVars.push_back(v);
2062 iteratedTypes.push_back(ty);
2063 } else {
2064 plainVars.push_back(v);
2065 plainTypes.push_back(ty);
2066 }
2067 return success();
2068 });
2069}
2070
2071template <typename PrintPrefixFn>
2073 TypeRange iteratedTypes,
2074 ValueRange plainVars, TypeRange plainTypes,
2075 PrintPrefixFn &&printPrefixForPlain,
2076 PrintPrefixFn &&printPrefixForIterated) {
2077
2078 bool first = true;
2079 auto emit = [&](Value v, Type t, auto &&printPrefix) {
2080 if (!first)
2081 p << ", ";
2082 printPrefix(v, t);
2083 p << v << " : " << t;
2084 first = false;
2085 };
2086
2087 for (unsigned i = 0; i < iteratedVars.size(); ++i)
2088 emit(iteratedVars[i], iteratedTypes[i], printPrefixForIterated);
2089 for (unsigned i = 0; i < plainVars.size(); ++i)
2090 emit(plainVars[i], plainTypes[i], printPrefixForPlain);
2091}
2092
2093/// Verify the per-variable modifier list has one entry per reduction variable.
2094static LogicalResult verifyReductionModifiers(Operation *op,
2095 OperandRange reductionVars,
2096 ArrayAttr reductionMods) {
2097 if (reductionMods && reductionMods.size() != reductionVars.size())
2098 return op->emitOpError(
2099 "expected as many reduction modifiers as reduction variables");
2100 return success();
2101}
2102
2103/// Verifies Reduction Clause
2104static LogicalResult
2105verifyReductionVarList(Operation *op, std::optional<ArrayAttr> reductionSyms,
2106 OperandRange reductionVars,
2107 std::optional<ArrayRef<bool>> reductionByref,
2108 ArrayAttr reductionMods = nullptr) {
2109 if (failed(verifyReductionModifiers(op, reductionVars, reductionMods)))
2110 return failure();
2111 if (!reductionVars.empty()) {
2112 if (!reductionSyms || reductionSyms->size() != reductionVars.size())
2113 return op->emitOpError()
2114 << "expected as many reduction symbol references "
2115 "as reduction variables";
2116 if (reductionByref && reductionByref->size() != reductionVars.size())
2117 return op->emitError() << "expected as many reduction variable by "
2118 "reference attributes as reduction variables";
2119 } else {
2120 if (reductionSyms)
2121 return op->emitOpError() << "unexpected reduction symbol references";
2122 return success();
2123 }
2124
2125 // TODO: The followings should be done in
2126 // SymbolUserOpInterface::verifySymbolUses.
2127 DenseSet<Value> accumulators;
2128 for (auto args : llvm::zip(reductionVars, *reductionSyms)) {
2129 Value accum = std::get<0>(args);
2130
2131 if (!accumulators.insert(accum).second)
2132 return op->emitOpError() << "accumulator variable used more than once";
2133
2134 Type varType = accum.getType();
2135 auto symbolRef = llvm::cast<SymbolRefAttr>(std::get<1>(args));
2136 auto decl =
2138 if (!decl)
2139 return op->emitOpError() << "expected symbol reference " << symbolRef
2140 << " to point to a reduction declaration";
2141
2142 if (decl.getAccumulatorType() && decl.getAccumulatorType() != varType)
2143 return op->emitOpError()
2144 << "expected accumulator (" << varType
2145 << ") to be the same type as reduction declaration ("
2146 << decl.getAccumulatorType() << ")";
2147 }
2148
2149 return success();
2150}
2151
2152//===----------------------------------------------------------------------===//
2153// Parser, printer and verifier for Copyprivate
2154//===----------------------------------------------------------------------===//
2155
2156/// copyprivate-entry-list ::= copyprivate-entry
2157/// | copyprivate-entry-list `,` copyprivate-entry
2158/// copyprivate-entry ::= ssa-id `->` symbol-ref `:` type
2159static ParseResult parseCopyprivate(
2160 OpAsmParser &parser,
2162 SmallVectorImpl<Type> &copyprivateTypes, ArrayAttr &copyprivateSyms) {
2164 if (failed(parser.parseCommaSeparatedList([&]() {
2165 if (parser.parseOperand(copyprivateVars.emplace_back()) ||
2166 parser.parseArrow() ||
2167 parser.parseAttribute(symsVec.emplace_back()) ||
2168 parser.parseColonType(copyprivateTypes.emplace_back()))
2169 return failure();
2170 return success();
2171 })))
2172 return failure();
2173 SmallVector<Attribute> syms(symsVec.begin(), symsVec.end());
2174 copyprivateSyms = ArrayAttr::get(parser.getContext(), syms);
2175 return success();
2176}
2177
2178/// Print Copyprivate clause
2180 OperandRange copyprivateVars,
2181 TypeRange copyprivateTypes,
2182 std::optional<ArrayAttr> copyprivateSyms) {
2183 if (!copyprivateSyms.has_value())
2184 return;
2185 llvm::interleaveComma(
2186 llvm::zip(copyprivateVars, *copyprivateSyms, copyprivateTypes), p,
2187 [&](const auto &args) {
2188 p << std::get<0>(args) << " -> " << std::get<1>(args) << " : "
2189 << std::get<2>(args);
2190 });
2191}
2192
2193/// Verifies CopyPrivate Clause
2194static LogicalResult
2196 std::optional<ArrayAttr> copyprivateSyms) {
2197 size_t copyprivateSymsSize =
2198 copyprivateSyms.has_value() ? copyprivateSyms->size() : 0;
2199 if (copyprivateSymsSize != copyprivateVars.size())
2200 return op->emitOpError() << "inconsistent number of copyprivate vars (= "
2201 << copyprivateVars.size()
2202 << ") and functions (= " << copyprivateSymsSize
2203 << "), both must be equal";
2204 if (!copyprivateSyms.has_value())
2205 return success();
2206
2207 for (auto copyprivateVarAndSym :
2208 llvm::zip(copyprivateVars, *copyprivateSyms)) {
2209 auto symbolRef =
2210 llvm::cast<SymbolRefAttr>(std::get<1>(copyprivateVarAndSym));
2211 std::optional<std::variant<mlir::func::FuncOp, mlir::LLVM::LLVMFuncOp>>
2212 funcOp;
2213 if (mlir::func::FuncOp mlirFuncOp =
2215 symbolRef))
2216 funcOp = mlirFuncOp;
2217 else if (mlir::LLVM::LLVMFuncOp llvmFuncOp =
2219 op, symbolRef))
2220 funcOp = llvmFuncOp;
2221
2222 auto getNumArguments = [&] {
2223 return std::visit([](auto &f) { return f.getNumArguments(); }, *funcOp);
2224 };
2225
2226 auto getArgumentType = [&](unsigned i) {
2227 return std::visit([i](auto &f) { return f.getArgumentTypes()[i]; },
2228 *funcOp);
2229 };
2230
2231 if (!funcOp)
2232 return op->emitOpError() << "expected symbol reference " << symbolRef
2233 << " to point to a copy function";
2234
2235 if (getNumArguments() != 2)
2236 return op->emitOpError()
2237 << "expected copy function " << symbolRef << " to have 2 operands";
2238
2239 Type argTy = getArgumentType(0);
2240 if (argTy != getArgumentType(1))
2241 return op->emitOpError() << "expected copy function " << symbolRef
2242 << " arguments to have the same type";
2243
2244 Type varType = std::get<0>(copyprivateVarAndSym).getType();
2245 if (argTy != varType)
2246 return op->emitOpError()
2247 << "expected copy function arguments' type (" << argTy
2248 << ") to be the same as copyprivate variable's type (" << varType
2249 << ")";
2250 }
2251
2252 return success();
2253}
2254
2255//===----------------------------------------------------------------------===//
2256// Parser, printer and verifier for DependVarList
2257//===----------------------------------------------------------------------===//
2258
2259/// depend-entry-list ::= depend-entry
2260/// | depend-entry-list `,` depend-entry
2261/// depend-entry ::= depend-kind `->` ssa-id `:` type
2262/// | depend-kind `->` ssa-id `:` iterated-type
2263static ParseResult parseDependVarList(
2264 OpAsmParser &parser,
2266 SmallVectorImpl<Type> &dependTypes, ArrayAttr &dependKinds,
2268 SmallVectorImpl<Type> &iteratedTypes, ArrayAttr &iteratedKinds) {
2271 if (failed(parser.parseCommaSeparatedList([&]() {
2272 StringRef keyword;
2273 OpAsmParser::UnresolvedOperand operand;
2274 Type ty;
2275 if (parser.parseKeyword(&keyword) || parser.parseArrow() ||
2276 parser.parseOperand(operand) || parser.parseColonType(ty))
2277 return failure();
2278 std::optional<ClauseTaskDepend> keywordDepend =
2279 symbolizeClauseTaskDepend(keyword);
2280 if (!keywordDepend)
2281 return failure();
2282 auto kindAttr =
2283 ClauseTaskDependAttr::get(parser.getContext(), *keywordDepend);
2284 if (llvm::isa<mlir::omp::IteratedType>(ty)) {
2285 iteratedVars.push_back(operand);
2286 iteratedTypes.push_back(ty);
2287 iterKindsVec.push_back(kindAttr);
2288 } else {
2289 dependVars.push_back(operand);
2290 dependTypes.push_back(ty);
2291 kindsVec.push_back(kindAttr);
2292 }
2293 return success();
2294 })))
2295 return failure();
2296 SmallVector<Attribute> kinds(kindsVec.begin(), kindsVec.end());
2297 dependKinds = ArrayAttr::get(parser.getContext(), kinds);
2298 SmallVector<Attribute> iterKinds(iterKindsVec.begin(), iterKindsVec.end());
2299 iteratedKinds = ArrayAttr::get(parser.getContext(), iterKinds);
2300 return success();
2301}
2302
2303/// Print Depend clause
2305 OperandRange dependVars, TypeRange dependTypes,
2306 std::optional<ArrayAttr> dependKinds,
2307 OperandRange iteratedVars,
2308 TypeRange iteratedTypes,
2309 std::optional<ArrayAttr> iteratedKinds) {
2310 bool first = true;
2311 auto printEntries = [&](OperandRange vars, TypeRange types,
2312 std::optional<ArrayAttr> kinds) {
2313 for (unsigned i = 0, e = vars.size(); i < e; ++i) {
2314 if (!first)
2315 p << ", ";
2316 p << stringifyClauseTaskDepend(
2317 llvm::cast<mlir::omp::ClauseTaskDependAttr>((*kinds)[i])
2318 .getValue())
2319 << " -> " << vars[i] << " : " << types[i];
2320 first = false;
2321 }
2322 };
2323 printEntries(dependVars, dependTypes, dependKinds);
2324 printEntries(iteratedVars, iteratedTypes, iteratedKinds);
2325}
2326
2327/// Verifies Depend clause
2328static LogicalResult verifyDependVarList(Operation *op,
2329 std::optional<ArrayAttr> dependKinds,
2330 OperandRange dependVars,
2331 std::optional<ArrayAttr> iteratedKinds,
2332 OperandRange iteratedVars) {
2333 if (!dependVars.empty()) {
2334 if (!dependKinds || dependKinds->size() != dependVars.size())
2335 return op->emitOpError() << "expected as many depend values"
2336 " as depend variables";
2337 } else {
2338 if (dependKinds && !dependKinds->empty())
2339 return op->emitOpError() << "unexpected depend values";
2340 }
2341
2342 if (!iteratedVars.empty()) {
2343 if (!iteratedKinds || iteratedKinds->size() != iteratedVars.size())
2344 return op->emitOpError() << "expected as many depend iterated values"
2345 " as depend iterated variables";
2346 } else {
2347 if (iteratedKinds && !iteratedKinds->empty())
2348 return op->emitOpError() << "unexpected depend iterated values";
2349 }
2350
2351 return success();
2352}
2353
2354//===----------------------------------------------------------------------===//
2355// Parser, printer and verifier for Synchronization Hint (2.17.12)
2356//===----------------------------------------------------------------------===//
2357
2358/// Parses a Synchronization Hint clause. The value of hint is an integer
2359/// which is a combination of different hints from `omp_sync_hint_t`.
2360///
2361/// hint-clause = `hint` `(` hint-value `)`
2362static ParseResult parseSynchronizationHint(OpAsmParser &parser,
2363 IntegerAttr &hintAttr) {
2364 StringRef hintKeyword;
2365 int64_t hint = 0;
2366 if (succeeded(parser.parseOptionalKeyword("none"))) {
2367 hintAttr = IntegerAttr::get(parser.getBuilder().getI64Type(), 0);
2368 return success();
2369 }
2370 auto parseKeyword = [&]() -> ParseResult {
2371 if (failed(parser.parseKeyword(&hintKeyword)))
2372 return failure();
2373 if (hintKeyword == "uncontended")
2374 hint |= 1;
2375 else if (hintKeyword == "contended")
2376 hint |= 2;
2377 else if (hintKeyword == "nonspeculative")
2378 hint |= 4;
2379 else if (hintKeyword == "speculative")
2380 hint |= 8;
2381 else
2382 return parser.emitError(parser.getCurrentLocation())
2383 << hintKeyword << " is not a valid hint";
2384 return success();
2385 };
2386 if (parser.parseCommaSeparatedList(parseKeyword))
2387 return failure();
2388 hintAttr = IntegerAttr::get(parser.getBuilder().getI64Type(), hint);
2389 return success();
2390}
2391
2392/// Prints a Synchronization Hint clause
2394 IntegerAttr hintAttr) {
2395 int64_t hint = hintAttr.getInt();
2396
2397 if (hint == 0) {
2398 p << "none";
2399 return;
2400 }
2401
2402 // Helper function to get n-th bit from the right end of `value`
2403 auto bitn = [](int value, int n) -> bool { return value & (1 << n); };
2404
2405 bool uncontended = bitn(hint, 0);
2406 bool contended = bitn(hint, 1);
2407 bool nonspeculative = bitn(hint, 2);
2408 bool speculative = bitn(hint, 3);
2409
2411 if (uncontended)
2412 hints.push_back("uncontended");
2413 if (contended)
2414 hints.push_back("contended");
2415 if (nonspeculative)
2416 hints.push_back("nonspeculative");
2417 if (speculative)
2418 hints.push_back("speculative");
2419
2420 llvm::interleaveComma(hints, p);
2421}
2422
2423/// Verifies a synchronization hint clause
2424static LogicalResult verifySynchronizationHint(Operation *op, uint64_t hint) {
2425
2426 // Helper function to get n-th bit from the right end of `value`
2427 auto bitn = [](int value, int n) -> bool { return value & (1 << n); };
2428
2429 bool uncontended = bitn(hint, 0);
2430 bool contended = bitn(hint, 1);
2431 bool nonspeculative = bitn(hint, 2);
2432 bool speculative = bitn(hint, 3);
2433
2434 if (uncontended && contended)
2435 return op->emitOpError() << "the hints omp_sync_hint_uncontended and "
2436 "omp_sync_hint_contended cannot be combined";
2437 if (nonspeculative && speculative)
2438 return op->emitOpError() << "the hints omp_sync_hint_nonspeculative and "
2439 "omp_sync_hint_speculative cannot be combined.";
2440 return success();
2441}
2442
2443//===----------------------------------------------------------------------===//
2444// Parser, printer and verifier for Target
2445//===----------------------------------------------------------------------===//
2446
2447// Helper function to get bitwise AND of `value` and 'flag' then return it as a
2448// boolean
2449static bool mapTypeToBool(ClauseMapFlags value, ClauseMapFlags flag) {
2450 return (value & flag) == flag;
2451}
2452
2453/// Parses a map_entries map type from a string format back into its numeric
2454/// value.
2455///
2456/// map-clause = `map_clauses ( ( `(` `always, `? `implicit, `? `ompx_hold, `?
2457/// `close, `? `present, `? ( `to` | `from` | `delete` `)` )+ `)` )
2458static ParseResult parseMapClause(OpAsmParser &parser,
2459 ClauseMapFlagsAttr &mapType) {
2460 ClauseMapFlags mapTypeBits = ClauseMapFlags::none;
2461 // This simply verifies the correct keyword is read in, the
2462 // keyword itself is stored inside of the operation
2463 auto parseTypeAndMod = [&]() -> ParseResult {
2464 StringRef mapTypeMod;
2465 if (parser.parseKeyword(&mapTypeMod))
2466 return failure();
2467
2468 if (mapTypeMod == "always")
2469 mapTypeBits |= ClauseMapFlags::always;
2470
2471 if (mapTypeMod == "implicit")
2472 mapTypeBits |= ClauseMapFlags::implicit;
2473
2474 if (mapTypeMod == "ompx_hold")
2475 mapTypeBits |= ClauseMapFlags::ompx_hold;
2476
2477 if (mapTypeMod == "close")
2478 mapTypeBits |= ClauseMapFlags::close;
2479
2480 if (mapTypeMod == "present")
2481 mapTypeBits |= ClauseMapFlags::present;
2482
2483 if (mapTypeMod == "to")
2484 mapTypeBits |= ClauseMapFlags::to;
2485
2486 if (mapTypeMod == "from")
2487 mapTypeBits |= ClauseMapFlags::from;
2488
2489 if (mapTypeMod == "tofrom")
2490 mapTypeBits |= ClauseMapFlags::to | ClauseMapFlags::from;
2491
2492 if (mapTypeMod == "delete")
2493 mapTypeBits |= ClauseMapFlags::del;
2494
2495 if (mapTypeMod == "storage")
2496 mapTypeBits |= ClauseMapFlags::storage;
2497
2498 if (mapTypeMod == "return_param")
2499 mapTypeBits |= ClauseMapFlags::return_param;
2500
2501 if (mapTypeMod == "private")
2502 mapTypeBits |= ClauseMapFlags::priv;
2503
2504 if (mapTypeMod == "literal")
2505 mapTypeBits |= ClauseMapFlags::literal;
2506
2507 if (mapTypeMod == "attach")
2508 mapTypeBits |= ClauseMapFlags::attach;
2509
2510 if (mapTypeMod == "attach_always")
2511 mapTypeBits |= ClauseMapFlags::attach_always;
2512
2513 if (mapTypeMod == "attach_never")
2514 mapTypeBits |= ClauseMapFlags::attach_never;
2515
2516 if (mapTypeMod == "attach_auto")
2517 mapTypeBits |= ClauseMapFlags::attach_auto;
2518
2519 if (mapTypeMod == "ref_ptr")
2520 mapTypeBits |= ClauseMapFlags::ref_ptr;
2521
2522 if (mapTypeMod == "ref_ptee")
2523 mapTypeBits |= ClauseMapFlags::ref_ptee;
2524
2525 if (mapTypeMod == "is_device_ptr")
2526 mapTypeBits |= ClauseMapFlags::is_device_ptr;
2527
2528 if (mapTypeMod == "target_param")
2529 mapTypeBits |= ClauseMapFlags::target_param;
2530
2531 return success();
2532 };
2533
2534 if (parser.parseCommaSeparatedList(parseTypeAndMod))
2535 return failure();
2536
2537 mapType =
2538 parser.getBuilder().getAttr<mlir::omp::ClauseMapFlagsAttr>(mapTypeBits);
2539
2540 return success();
2541}
2542
2543/// Prints a map_entries map type from its numeric value out into its string
2544/// format.
2545static void printMapClause(OpAsmPrinter &p, Operation *op,
2546 ClauseMapFlagsAttr mapType) {
2548 ClauseMapFlags mapFlags = mapType.getValue();
2549
2550 // handling of always, close, present placed at the beginning of the string
2551 // to aid readability
2552 if (mapTypeToBool(mapFlags, ClauseMapFlags::always))
2553 mapTypeStrs.push_back("always");
2554 if (mapTypeToBool(mapFlags, ClauseMapFlags::implicit))
2555 mapTypeStrs.push_back("implicit");
2556 if (mapTypeToBool(mapFlags, ClauseMapFlags::ompx_hold))
2557 mapTypeStrs.push_back("ompx_hold");
2558 if (mapTypeToBool(mapFlags, ClauseMapFlags::close))
2559 mapTypeStrs.push_back("close");
2560 if (mapTypeToBool(mapFlags, ClauseMapFlags::present))
2561 mapTypeStrs.push_back("present");
2562 if (mapTypeToBool(mapFlags, ClauseMapFlags::target_param))
2563 mapTypeStrs.push_back("target_param");
2564
2565 // special handling of to/from/tofrom/delete and release/alloc, release +
2566 // alloc are the abscense of one of the other flags, whereas tofrom requires
2567 // both the to and from flag to be set.
2568 bool to = mapTypeToBool(mapFlags, ClauseMapFlags::to);
2569 bool from = mapTypeToBool(mapFlags, ClauseMapFlags::from);
2570
2571 if (to && from)
2572 mapTypeStrs.push_back("tofrom");
2573 else if (from)
2574 mapTypeStrs.push_back("from");
2575 else if (to)
2576 mapTypeStrs.push_back("to");
2577
2578 if (mapTypeToBool(mapFlags, ClauseMapFlags::del))
2579 mapTypeStrs.push_back("delete");
2580 if (mapTypeToBool(mapFlags, ClauseMapFlags::return_param))
2581 mapTypeStrs.push_back("return_param");
2582 if (mapTypeToBool(mapFlags, ClauseMapFlags::storage))
2583 mapTypeStrs.push_back("storage");
2584 if (mapTypeToBool(mapFlags, ClauseMapFlags::priv))
2585 mapTypeStrs.push_back("private");
2586 if (mapTypeToBool(mapFlags, ClauseMapFlags::literal))
2587 mapTypeStrs.push_back("literal");
2588 if (mapTypeToBool(mapFlags, ClauseMapFlags::attach))
2589 mapTypeStrs.push_back("attach");
2590 if (mapTypeToBool(mapFlags, ClauseMapFlags::attach_always))
2591 mapTypeStrs.push_back("attach_always");
2592 if (mapTypeToBool(mapFlags, ClauseMapFlags::attach_never))
2593 mapTypeStrs.push_back("attach_never");
2594 if (mapTypeToBool(mapFlags, ClauseMapFlags::attach_auto))
2595 mapTypeStrs.push_back("attach_auto");
2596 if (mapTypeToBool(mapFlags, ClauseMapFlags::ref_ptr))
2597 mapTypeStrs.push_back("ref_ptr");
2598 if (mapTypeToBool(mapFlags, ClauseMapFlags::ref_ptee))
2599 mapTypeStrs.push_back("ref_ptee");
2600 if (mapTypeToBool(mapFlags, ClauseMapFlags::is_device_ptr))
2601 mapTypeStrs.push_back("is_device_ptr");
2602 if (mapFlags == ClauseMapFlags::none)
2603 mapTypeStrs.push_back("none");
2604
2605 for (unsigned int i = 0; i < mapTypeStrs.size(); ++i) {
2606 p << mapTypeStrs[i];
2607 if (i + 1 < mapTypeStrs.size()) {
2608 p << ", ";
2609 }
2610 }
2611}
2612
2613static ParseResult parseMembersIndex(OpAsmParser &parser,
2614 ArrayAttr &membersIdx) {
2615 SmallVector<Attribute> values, memberIdxs;
2616
2617 auto parseIndices = [&]() -> ParseResult {
2618 int64_t value;
2619 if (parser.parseInteger(value))
2620 return failure();
2621 values.push_back(IntegerAttr::get(parser.getBuilder().getIntegerType(64),
2622 APInt(64, value, /*isSigned=*/false)));
2623 return success();
2624 };
2625
2626 do {
2627 if (failed(parser.parseLSquare()))
2628 return failure();
2629
2630 if (parser.parseCommaSeparatedList(parseIndices))
2631 return failure();
2632
2633 if (failed(parser.parseRSquare()))
2634 return failure();
2635
2636 memberIdxs.push_back(ArrayAttr::get(parser.getContext(), values));
2637 values.clear();
2638 } while (succeeded(parser.parseOptionalComma()));
2639
2640 if (!memberIdxs.empty())
2641 membersIdx = ArrayAttr::get(parser.getContext(), memberIdxs);
2642
2643 return success();
2644}
2645
2646static void printMembersIndex(OpAsmPrinter &p, MapInfoOp op,
2647 ArrayAttr membersIdx) {
2648 if (!membersIdx)
2649 return;
2650
2651 llvm::interleaveComma(membersIdx, p, [&p](Attribute v) {
2652 p << "[";
2653 auto memberIdx = cast<ArrayAttr>(v);
2654 llvm::interleaveComma(memberIdx.getValue(), p, [&p](Attribute v2) {
2655 p << cast<IntegerAttr>(v2).getInt();
2656 });
2657 p << "]";
2658 });
2659}
2660
2662 VariableCaptureKindAttr mapCaptureType) {
2663 std::string typeCapStr;
2664 llvm::raw_string_ostream typeCap(typeCapStr);
2665 if (mapCaptureType.getValue() == mlir::omp::VariableCaptureKind::ByRef)
2666 typeCap << "ByRef";
2667 if (mapCaptureType.getValue() == mlir::omp::VariableCaptureKind::ByCopy)
2668 typeCap << "ByCopy";
2669 if (mapCaptureType.getValue() == mlir::omp::VariableCaptureKind::VLAType)
2670 typeCap << "VLAType";
2671 if (mapCaptureType.getValue() == mlir::omp::VariableCaptureKind::This)
2672 typeCap << "This";
2673 p << typeCapStr;
2674}
2675
2676static ParseResult parseCaptureType(OpAsmParser &parser,
2677 VariableCaptureKindAttr &mapCaptureType) {
2678 StringRef mapCaptureKey;
2679 if (parser.parseKeyword(&mapCaptureKey))
2680 return failure();
2681
2682 if (mapCaptureKey == "This")
2683 mapCaptureType = mlir::omp::VariableCaptureKindAttr::get(
2684 parser.getContext(), mlir::omp::VariableCaptureKind::This);
2685 if (mapCaptureKey == "ByRef")
2686 mapCaptureType = mlir::omp::VariableCaptureKindAttr::get(
2687 parser.getContext(), mlir::omp::VariableCaptureKind::ByRef);
2688 if (mapCaptureKey == "ByCopy")
2689 mapCaptureType = mlir::omp::VariableCaptureKindAttr::get(
2690 parser.getContext(), mlir::omp::VariableCaptureKind::ByCopy);
2691 if (mapCaptureKey == "VLAType")
2692 mapCaptureType = mlir::omp::VariableCaptureKindAttr::get(
2693 parser.getContext(), mlir::omp::VariableCaptureKind::VLAType);
2694
2695 return success();
2696}
2697
2698static LogicalResult verifyMapInfoForMapClause(
2699 Operation *op, mlir::omp::MapInfoOp mapInfoOp,
2702 &updateFromVars) {
2703 mlir::omp::ClauseMapFlags mapTypeBits = mapInfoOp.getMapType();
2704
2705 bool to = mapTypeToBool(mapTypeBits, ClauseMapFlags::to);
2706 bool from = mapTypeToBool(mapTypeBits, ClauseMapFlags::from);
2707 bool del = mapTypeToBool(mapTypeBits, ClauseMapFlags::del);
2708
2709 bool always = mapTypeToBool(mapTypeBits, ClauseMapFlags::always);
2710 bool close = mapTypeToBool(mapTypeBits, ClauseMapFlags::close);
2711 bool implicit = mapTypeToBool(mapTypeBits, ClauseMapFlags::implicit);
2712 bool attach = mapTypeToBool(mapTypeBits, ClauseMapFlags::attach);
2713
2714 if ((isa<TargetDataOp>(op) || isa<TargetOp>(op)) && del)
2715 return emitError(op->getLoc(),
2716 "to, from, tofrom and alloc map types are permitted");
2717
2718 if (isa<TargetEnterDataOp>(op) && (from || del))
2719 return emitError(op->getLoc(), "to and alloc map types are permitted");
2720
2721 if (isa<TargetExitDataOp>(op) && to)
2722 return emitError(op->getLoc(),
2723 "from, release and delete map types are permitted");
2724
2725 if (isa<TargetUpdateOp>(op)) {
2726 if (del) {
2727 return emitError(op->getLoc(),
2728 "at least one of to or from map types must be "
2729 "specified, other map types are not permitted");
2730 }
2731
2732 if (!to && !from && !attach) {
2733 return emitError(op->getLoc(),
2734 "at least one of to or from or attach map types must be "
2735 "specified, other map types are not permitted");
2736 }
2737
2738 auto updateVar = mapInfoOp.getVarPtr();
2739
2740 if ((to && from) || (to && updateFromVars.contains(updateVar)) ||
2741 (from && updateToVars.contains(updateVar))) {
2742 return emitError(
2743 op->getLoc(),
2744 "either to or from map types can be specified, not both");
2745 }
2746
2747 if (always || close || implicit) {
2748 return emitError(
2749 op->getLoc(),
2750 "present, mapper and iterator map type modifiers are permitted");
2751 }
2752
2753 // It's possible we have an attach map, in which case if there is no to
2754 // or from tied to it, we skip insertion.
2755 if (to || from) {
2756 to ? updateToVars.insert(updateVar) : updateFromVars.insert(updateVar);
2757 }
2758 }
2759
2760 if ((mapInfoOp.getVarPtrPtr() && !mapInfoOp.getVarPtrPtrType()) ||
2761 (!mapInfoOp.getVarPtrPtr() && mapInfoOp.getVarPtrPtrType())) {
2762 return emitError(op->getLoc(),
2763 "if varPtrPtr or varPtrPtrType is specified, then both "
2764 "must be present");
2765 }
2766
2767 return success();
2768}
2769
2770static LogicalResult verifyMapClause(Operation *op, OperandRange mapVars,
2771 OperandRange mapIterated) {
2774
2775 for (auto mapOp : mapVars) {
2776 if (!mapOp.getDefiningOp())
2777 return emitError(op->getLoc(), "missing map operation");
2778
2779 if (auto mapInfoOp = mapOp.getDefiningOp<mlir::omp::MapInfoOp>()) {
2780 if (failed(verifyMapInfoForMapClause(op, mapInfoOp, updateToVars,
2781 updateFromVars)))
2782 return failure();
2783 } else if (!isa<DeclareMapperInfoOp>(op)) {
2784 return emitError(op->getLoc(),
2785 "map argument is not a map entry operation");
2786 }
2787 }
2788
2789 // Verify iterated map entries.
2790 for (auto iterVal : mapIterated) {
2791 auto iterOp = iterVal.getDefiningOp<mlir::omp::IteratorOp>();
2792 if (!iterOp)
2793 return op->emitOpError() << "'map_iterated' arguments must be defined by "
2794 "'omp.iterator' ops";
2795
2796 // Check that the iterator body yields a value defined by omp.map.info.
2797 mlir::omp::YieldOp yieldOp = iterOp.getYieldOp();
2798 auto yieldedMapInfo =
2799 yieldOp && yieldOp.getNumOperands() == 1
2800 ? yieldOp.getResults()[0].getDefiningOp<mlir::omp::MapInfoOp>()
2801 : nullptr;
2802 if (!yieldedMapInfo)
2803 return op->emitOpError() << "'map_iterated' iterator body must yield "
2804 "a value defined by 'omp.map.info'";
2805
2806 if (failed(verifyMapInfoForMapClause(op, yieldedMapInfo, updateToVars,
2807 updateFromVars)))
2808 return failure();
2809 }
2810
2811 return success();
2812}
2813
2814template <typename OpType>
2815static LogicalResult verifyPrivateVarList(OpType &op);
2816
2817static LogicalResult verifyPrivateVarsMapping(TargetOp targetOp) {
2818 std::optional<DenseI64ArrayAttr> privateMapIndices =
2819 targetOp.getPrivateMapsAttr();
2820
2821 // None of the private operands are mapped.
2822 if (!privateMapIndices.has_value() || !privateMapIndices.value())
2823 return success();
2824
2825 OperandRange privateVars = targetOp.getPrivateVars();
2826
2827 if (privateMapIndices.value().size() !=
2828 static_cast<int64_t>(privateVars.size()))
2829 return emitError(targetOp.getLoc(), "sizes of `private` operand range and "
2830 "`private_maps` attribute mismatch");
2831
2832 return success();
2833}
2834
2835//===----------------------------------------------------------------------===//
2836// MapInfoOp
2837//===----------------------------------------------------------------------===//
2838
2839static LogicalResult verifyMapInfoDefinedArgs(Operation *op,
2840 StringRef clauseName,
2841 OperandRange vars) {
2842 for (Value var : vars)
2843 if (!llvm::isa_and_present<MapInfoOp>(var.getDefiningOp()))
2844 return op->emitOpError()
2845 << "'" << clauseName
2846 << "' arguments must be defined by 'omp.map.info' ops";
2847 return success();
2848}
2849
2850LogicalResult MapInfoOp::verify() {
2851 if (getMapperId() &&
2853 *this, getMapperIdAttr())) {
2854 return emitError("invalid mapper id");
2855 }
2856
2857 if (failed(verifyMapInfoDefinedArgs(*this, "members", getMembers())))
2858 return failure();
2859
2860 return success();
2861}
2862
2863//===----------------------------------------------------------------------===//
2864// TargetDataOp
2865//===----------------------------------------------------------------------===//
2866
2867void TargetDataOp::build(OpBuilder &builder, OperationState &state,
2868 const TargetDataOperands &clauses) {
2869 TargetDataOp::build(builder, state, clauses.device, clauses.ifExpr,
2870 clauses.mapVars, clauses.mapIterated,
2871 clauses.useDeviceAddrVars, clauses.useDevicePtrVars);
2872}
2873
2874LogicalResult TargetDataOp::verify() {
2875 if (getMapVars().empty() && getMapIterated().empty() &&
2876 getUseDevicePtrVars().empty() && getUseDeviceAddrVars().empty()) {
2877 return ::emitError(this->getLoc(),
2878 "At least one of map, use_device_ptr_vars, or "
2879 "use_device_addr_vars operand must be present");
2880 }
2881
2882 if (failed(verifyMapInfoDefinedArgs(*this, "use_device_ptr",
2883 getUseDevicePtrVars())))
2884 return failure();
2885
2886 if (failed(verifyMapInfoDefinedArgs(*this, "use_device_addr",
2887 getUseDeviceAddrVars())))
2888 return failure();
2889
2890 return verifyMapClause(*this, getMapVars(), getMapIterated());
2891}
2892
2893//===----------------------------------------------------------------------===//
2894// TargetEnterDataOp
2895//===----------------------------------------------------------------------===//
2896
2897void TargetEnterDataOp::build(
2898 OpBuilder &builder, OperationState &state,
2899 const TargetEnterExitUpdateDataOperands &clauses) {
2900 MLIRContext *ctx = builder.getContext();
2901 TargetEnterDataOp::build(
2902 builder, state, makeArrayAttr(ctx, clauses.dependKinds),
2903 clauses.dependVars, makeArrayAttr(ctx, clauses.dependIteratedKinds),
2904 clauses.dependIterated, clauses.device, clauses.ifExpr, clauses.mapVars,
2905 clauses.mapIterated, clauses.nowait);
2906}
2907
2908LogicalResult TargetEnterDataOp::verify() {
2909 LogicalResult verifyDependVars =
2910 verifyDependVarList(*this, getDependKinds(), getDependVars(),
2911 getDependIteratedKinds(), getDependIterated());
2912 return failed(verifyDependVars)
2913 ? verifyDependVars
2914 : verifyMapClause(*this, getMapVars(), getMapIterated());
2915}
2916
2917//===----------------------------------------------------------------------===//
2918// TargetExitDataOp
2919//===----------------------------------------------------------------------===//
2920
2921void TargetExitDataOp::build(OpBuilder &builder, OperationState &state,
2922 const TargetEnterExitUpdateDataOperands &clauses) {
2923 MLIRContext *ctx = builder.getContext();
2924 TargetExitDataOp::build(
2925 builder, state, makeArrayAttr(ctx, clauses.dependKinds),
2926 clauses.dependVars, makeArrayAttr(ctx, clauses.dependIteratedKinds),
2927 clauses.dependIterated, clauses.device, clauses.ifExpr, clauses.mapVars,
2928 clauses.mapIterated, clauses.nowait);
2929}
2930
2931LogicalResult TargetExitDataOp::verify() {
2932 LogicalResult verifyDependVars =
2933 verifyDependVarList(*this, getDependKinds(), getDependVars(),
2934 getDependIteratedKinds(), getDependIterated());
2935 return failed(verifyDependVars)
2936 ? verifyDependVars
2937 : verifyMapClause(*this, getMapVars(), getMapIterated());
2938}
2939
2940//===----------------------------------------------------------------------===//
2941// TargetUpdateOp
2942//===----------------------------------------------------------------------===//
2943
2944void TargetUpdateOp::build(OpBuilder &builder, OperationState &state,
2945 const TargetEnterExitUpdateDataOperands &clauses) {
2946 MLIRContext *ctx = builder.getContext();
2947 TargetUpdateOp::build(builder, state, makeArrayAttr(ctx, clauses.dependKinds),
2948 clauses.dependVars,
2949 makeArrayAttr(ctx, clauses.dependIteratedKinds),
2950 clauses.dependIterated, clauses.device, clauses.ifExpr,
2951 clauses.mapVars, clauses.mapIterated, clauses.nowait);
2952}
2953
2954LogicalResult TargetUpdateOp::verify() {
2955 LogicalResult verifyDependVars =
2956 verifyDependVarList(*this, getDependKinds(), getDependVars(),
2957 getDependIteratedKinds(), getDependIterated());
2958 return failed(verifyDependVars)
2959 ? verifyDependVars
2960 : verifyMapClause(*this, getMapVars(), getMapIterated());
2961}
2962
2963//===----------------------------------------------------------------------===//
2964// TargetOp
2965//===----------------------------------------------------------------------===//
2966
2967void TargetOp::build(OpBuilder &builder, OperationState &state,
2968 const TargetExtOperands &clauses) {
2969 MLIRContext *ctx = builder.getContext();
2970 TargetOp::build(
2971 builder, state, clauses.allocateVars, clauses.allocatorVars,
2972 makeDenseI64ArrayAttr(ctx, clauses.allocateAlignments),
2973 makeDenseI64ArrayAttr(ctx, clauses.allocatePrivateIndices),
2974 makeArrayAttr(ctx, clauses.dependKinds), clauses.dependVars,
2975 makeArrayAttr(ctx, clauses.dependIteratedKinds), clauses.dependIterated,
2976 clauses.device, clauses.dynGroupprivateAccessGroup,
2977 clauses.dynGroupprivateFallback, clauses.dynGroupprivateSize,
2978 clauses.hasDeviceAddrVars, clauses.hostEvalVars, clauses.ifExpr,
2979 clauses.inReductionVars,
2980 makeDenseBoolArrayAttr(ctx, clauses.inReductionByref),
2981 makeArrayAttr(ctx, clauses.inReductionSyms), clauses.isDevicePtrVars,
2982 clauses.mapVars, clauses.mapIterated, clauses.nowait, clauses.privateVars,
2983 makeArrayAttr(ctx, clauses.privateSyms), clauses.privateNeedsBarrier,
2984 clauses.threadLimitVars, /*private_maps=*/nullptr, clauses.kernelType);
2985}
2986
2987bool TargetOp::hasHostEvalTripCount() {
2988 TargetExecMode mode = getKernelType();
2989 if (mode == TargetExecMode::spmd || mode == TargetExecMode::spmd_no_loop)
2990 return true;
2991
2992 if (mode == TargetExecMode::bare)
2993 return false;
2994
2995 // If it represents a `target teams distribute` construct, also evaluate the
2996 // `distribute` trip count on the host.
2997 Operation *capturedOp =
2998 cast<ComposableOpInterface>(getOperation()).findCapturedOp();
2999 if (auto loopNestOp = dyn_cast_if_present<LoopNestOp>(capturedOp)) {
3001 loopNestOp.gatherWrappers(loopWrappers);
3002
3003 LoopWrapperInterface *innermostWrapper = loopWrappers.begin();
3004 if (isa<SimdOp>(innermostWrapper))
3005 innermostWrapper = std::next(innermostWrapper);
3006
3007 auto numWrappers = std::distance(innermostWrapper, loopWrappers.end());
3008 if (numWrappers != 1)
3009 return false;
3010
3011 if (!isa<DistributeOp>(innermostWrapper))
3012 return false;
3013
3014 Operation *parentOp = innermostWrapper->getOperation()->getParentOp();
3015 if (isa_and_present<TeamsOp>(parentOp) &&
3016 parentOp->getParentOp() == getOperation())
3017 return true;
3018 }
3019
3020 return false;
3021}
3022
3023/// An `omp.target` `in_reduction` operand is captured by a `map_entries` entry
3024/// when the entry's `MapInfoOp` var_ptr is the same SSA value, or another
3025/// result of the same defining op. At this stage, exact identity can only be
3026/// required for block arguments, which have no defining op. Flang emits
3027/// `hlfir.declare` #0 for the `in_reduction` operand and #1 for the map
3028/// `var_ptr`; these collapse to the same value after lowering, but that cannot
3029/// be enforced here.
3030static bool targetInReductionCapturedBy(Value inReductionVar, Value mapVarPtr) {
3031 if (mapVarPtr == inReductionVar)
3032 return true;
3033 Operation *def = inReductionVar.getDefiningOp();
3034 return def && mapVarPtr.getDefiningOp() == def;
3035}
3036
3037LogicalResult TargetOp::verify() {
3039 getOperation(), getAllocateVars(), getAllocatorVars(),
3040 getAllocateAlignmentsAttr(), getAllocatePrivateIndicesAttr(),
3041 getPrivateVars(), getPrivateSymsAttr())))
3042 return failure();
3043
3044 if (getKernelType() == TargetExecMode::bare && !isCombined())
3045 return emitOpError() << "bare kernel requires 'omp.combined'";
3046
3047 if (failed(verifyDependVarList(*this, getDependKinds(), getDependVars(),
3048 getDependIteratedKinds(),
3049 getDependIterated())))
3050 return failure();
3051
3052 if (failed(verifyMapInfoDefinedArgs(*this, "has_device_addr",
3053 getHasDeviceAddrVars())))
3054 return failure();
3055
3056 if (failed(verifyMapClause(*this, getMapVars(), getMapIterated())))
3057 return failure();
3058
3060 *this, getDynGroupprivateAccessGroupAttr(),
3061 getDynGroupprivateFallbackAttr(), getDynGroupprivateSize())))
3062 return failure();
3063
3064 if (failed(verifyPrivateVarList(*this)))
3065 return failure();
3066
3067 if (failed(verifyReductionVarList(*this, getInReductionSyms(),
3068 getInReductionVars(),
3069 getInReductionByref())))
3070 return failure();
3071
3072 // An `in_reduction` operand on `omp.target` has no dedicated entry block
3073 // argument; inside the region it is accessed through the block argument of a
3074 // matching `map_entries` entry, and the host rewrites that map argument to
3075 // the reduction-private storage. Require every `in_reduction` operand to be
3076 // captured by at least one `map_entries` entry.
3077 for (Value inReductionVar : getInReductionVars()) {
3078 bool captured = false;
3079 for (Value mapVar : getMapVars()) {
3080 auto mapInfo = mapVar.getDefiningOp<MapInfoOp>();
3081 if (targetInReductionCapturedBy(inReductionVar, mapInfo.getVarPtr())) {
3082 captured = true;
3083 break;
3084 }
3085 }
3086 if (!captured)
3087 return emitOpError() << "in_reduction variable must be captured by a "
3088 "matching map_entries entry";
3089 }
3090
3091 return verifyPrivateVarsMapping(*this);
3092}
3093
3094LogicalResult TargetOp::verifyRegions() {
3095 auto teamsOps = getOps<TeamsOp>();
3096 auto numNestedTeams = std::distance(teamsOps.begin(), teamsOps.end());
3097 if (numNestedTeams > 1)
3098 return emitError("target containing multiple 'omp.teams' nested ops");
3099
3100 if (numNestedTeams == 0) {
3101 switch (getKernelType()) {
3102 case TargetExecMode::bare:
3103 return emitOpError()
3104 << "bare kernel must contain a nested 'omp.teams' operation";
3105 case TargetExecMode::spmd_no_loop:
3106 return emitOpError() << "spmd_no_loop kernel must contain a nested "
3107 "'omp.teams' operation";
3108 default:
3109 break;
3110 }
3111 }
3112
3113 Operation *capturedOp =
3114 cast<ComposableOpInterface>(getOperation()).findCapturedOp();
3115 if ((getKernelType() == TargetExecMode::spmd ||
3116 getKernelType() == TargetExecMode::spmd_no_loop) &&
3117 !isa_and_present<LoopNestOp>(capturedOp))
3118 return emitOpError()
3119 << "SPMD kernel must capture an 'omp.loop_nest' operation";
3120
3121 bool isTargetDevice = false;
3122 if (auto offloadMod = (*this)->getParentOfType<OffloadModuleInterface>())
3123 if (offloadMod.getIsTargetDevice())
3124 isTargetDevice = true;
3125
3126 // Check that host_eval values are only used in legal ways.
3127 llvm::ArrayRef<BlockArgument> hostEvalBlockArgs =
3128 cast<BlockArgOpenMPOpInterface>(getOperation()).getHostEvalBlockArgs();
3129
3130 bool hostEvalTripCount = hasHostEvalTripCount();
3131 for (Value hostEvalArg : hostEvalBlockArgs) {
3132 for (Operation *user : hostEvalArg.getUsers()) {
3133 if (auto teamsOp = dyn_cast<TeamsOp>(user)) {
3134 // Check if used in num_teams_lower or any of num_teams_upper_vars
3135 if (hostEvalArg == teamsOp.getNumTeamsLower() ||
3136 llvm::is_contained(teamsOp.getNumTeamsUpperVars(), hostEvalArg) ||
3137 llvm::is_contained(teamsOp.getThreadLimitVars(), hostEvalArg))
3138 continue;
3139
3140 return emitOpError() << "host_eval argument only legal as 'num_teams' "
3141 "and 'thread_limit' in 'omp.teams'";
3142 }
3143 if (auto parallelOp = dyn_cast<ParallelOp>(user)) {
3144 if (llvm::is_contained(parallelOp.getNumThreadsVars(), hostEvalArg))
3145 continue;
3146
3147 return emitOpError()
3148 << "host_eval argument only legal as 'num_threads' in "
3149 "'omp.parallel'";
3150 }
3151 if (auto loopNestOp = dyn_cast<LoopNestOp>(user)) {
3152 if (hostEvalTripCount &&
3153 (llvm::is_contained(loopNestOp.getLoopLowerBounds(), hostEvalArg) ||
3154 llvm::is_contained(loopNestOp.getLoopUpperBounds(), hostEvalArg) ||
3155 llvm::is_contained(loopNestOp.getLoopSteps(), hostEvalArg)))
3156 continue;
3157
3158 return emitOpError() << "host_eval argument only legal as loop bounds "
3159 "and steps in 'omp.loop_nest' when trip count "
3160 "must be evaluated in the host";
3161 }
3162
3163 return emitOpError() << "host_eval argument illegal use in '"
3164 << user->getName() << "' operation";
3165 }
3166 }
3167
3168 if (hostEvalTripCount && !isTargetDevice) {
3169 auto loopOp = cast<LoopNestOp>(capturedOp);
3170 for (auto arg : llvm::concat<Value>(loopOp.getLoopLowerBounds(),
3171 loopOp.getLoopUpperBounds(),
3172 loopOp.getLoopSteps())) {
3173 if (!llvm::is_contained(hostEvalBlockArgs, arg))
3174 return emitOpError() << "nested 'omp.loop_nest' bounds expected to "
3175 "be host-evaluated";
3176 }
3177 }
3178
3179 return success();
3180}
3181
3182//===----------------------------------------------------------------------===//
3183// ParallelOp
3184//===----------------------------------------------------------------------===//
3185
3186void ParallelOp::build(OpBuilder &builder, OperationState &state,
3187 ArrayRef<NamedAttribute> attributes) {
3188 ParallelOp::build(builder, state, /*allocate_vars=*/ValueRange(),
3189 /*allocator_vars=*/ValueRange(),
3190 /*allocate_alignments=*/nullptr,
3191 /*allocate_private_indices=*/nullptr, /*if_expr=*/nullptr,
3192 /*num_threads_vars=*/ValueRange(),
3193 /*private_vars=*/ValueRange(),
3194 /*private_syms=*/nullptr, /*private_needs_barrier=*/false,
3195 /*proc_bind_kind=*/nullptr,
3196 /*reduction_mods =*/nullptr,
3197 /*reduction_vars=*/ValueRange(),
3198 /*reduction_byref=*/nullptr, /*reduction_syms=*/nullptr);
3199 state.addAttributes(attributes);
3200}
3201
3202void ParallelOp::build(OpBuilder &builder, OperationState &state,
3203 const ParallelOperands &clauses) {
3204 MLIRContext *ctx = builder.getContext();
3205 ParallelOp::build(builder, state, clauses.allocateVars, clauses.allocatorVars,
3206 makeDenseI64ArrayAttr(ctx, clauses.allocateAlignments),
3207 makeDenseI64ArrayAttr(ctx, clauses.allocatePrivateIndices),
3208 clauses.ifExpr, clauses.numThreadsVars, clauses.privateVars,
3209 makeArrayAttr(ctx, clauses.privateSyms),
3210 clauses.privateNeedsBarrier, clauses.procBindKind,
3211 makeArrayAttr(ctx, clauses.reductionMods),
3212 clauses.reductionVars,
3213 makeDenseBoolArrayAttr(ctx, clauses.reductionByref),
3214 makeArrayAttr(ctx, clauses.reductionSyms));
3215}
3216
3217template <typename OpType>
3218static LogicalResult verifyPrivateVarList(OpType &op) {
3219 auto privateVars = op.getPrivateVars();
3220 auto privateSyms = op.getPrivateSymsAttr();
3221
3222 if (privateVars.empty() && (privateSyms == nullptr || privateSyms.empty()))
3223 return success();
3224
3225 auto numPrivateVars = privateVars.size();
3226 auto numPrivateSyms = (privateSyms == nullptr) ? 0 : privateSyms.size();
3227
3228 if (numPrivateVars != numPrivateSyms)
3229 return op.emitError() << "inconsistent number of private variables and "
3230 "privatizer op symbols, private vars: "
3231 << numPrivateVars
3232 << " vs. privatizer op symbols: " << numPrivateSyms;
3233
3234 for (auto privateVarInfo : llvm::zip_equal(privateVars, privateSyms)) {
3235 Type varType = std::get<0>(privateVarInfo).getType();
3236 SymbolRefAttr privateSym = cast<SymbolRefAttr>(std::get<1>(privateVarInfo));
3237 PrivateClauseOp privatizerOp =
3239
3240 if (privatizerOp == nullptr)
3241 return op.emitError() << "failed to lookup privatizer op with symbol: '"
3242 << privateSym << "'";
3243
3244 Type privatizerType = privatizerOp.getArgType();
3245
3246 if (privatizerType && (varType != privatizerType))
3247 return op.emitError()
3248 << "type mismatch between a "
3249 << stringifyDataSharingClauseType(
3250 privatizerOp.getDataSharingType())
3251 << " variable and its privatizer op, var type: " << varType
3252 << " vs. privatizer op type: " << privatizerType;
3253
3254 if (privatizerOp.getDataSharingType() == DataSharingClauseType::Linear)
3255 return op.emitError() << "`linear` privatizer '" << privateSym
3256 << "' can only be used in a `linear` clause";
3257 }
3258
3259 return success();
3260}
3261
3262/// Verifies that each linear variable has a `linear` privatizer.
3263template <typename OpType>
3264static LogicalResult verifyLinearSyms(OpType op) {
3265 OperandRange linearVars = op.getLinearVars();
3266 std::optional<ArrayAttr> linearSyms = op.getLinearSyms();
3267 if (linearVars.empty() && (!linearSyms || linearSyms->empty()))
3268 return success();
3269
3270 if (!linearSyms || linearSyms->size() != linearVars.size())
3271 return op.emitOpError()
3272 << "expected as many linear privatizers as linear variables";
3273
3274 for (auto [var, sym] : llvm::zip_equal(linearVars, *linearSyms)) {
3275 auto symRef = cast<SymbolRefAttr>(sym);
3276 auto privatizer =
3278 if (!privatizer)
3279 return op.emitOpError() << "failed to lookup privatizer op with symbol: '"
3280 << symRef << "'";
3281 if (privatizer.getDataSharingType() != DataSharingClauseType::Linear)
3282 return op.emitOpError() << "privatizer '" << symRef
3283 << "' of a linear variable must be `linear`";
3284 Type argType = privatizer.getArgType();
3285 if (argType && argType != var.getType())
3286 return op.emitOpError()
3287 << "type mismatch between a linear variable and its privatizer "
3288 "op, var type: "
3289 << var.getType() << " vs. privatizer op type: " << argType;
3290 }
3291 return success();
3292}
3293
3294LogicalResult ParallelOp::verify() {
3295 if (failed(verifyPrivateVarList(*this)))
3296 return failure();
3298 getOperation(), getAllocateVars(), getAllocatorVars(),
3299 getAllocateAlignmentsAttr(), getAllocatePrivateIndicesAttr(),
3300 getPrivateVars(), getPrivateSymsAttr(),
3301 /*requirePrivateIndices=*/true)))
3302 return failure();
3303
3304 return verifyReductionVarList(*this, getReductionSyms(), getReductionVars(),
3305 getReductionByref(), getReductionModsAttr());
3306}
3307
3308LogicalResult ParallelOp::verifyRegions() {
3309 auto distChildOps = getOps<DistributeOp>();
3310 int numDistChildOps = std::distance(distChildOps.begin(), distChildOps.end());
3311 if (numDistChildOps > 1)
3312 return emitError()
3313 << "multiple 'omp.distribute' nested inside of 'omp.parallel'";
3314
3315 if (numDistChildOps == 1) {
3316 if (!isComposite())
3317 return emitError()
3318 << "'omp.composite' attribute missing from composite operation";
3319
3320 auto *ompDialect = getContext()->getLoadedDialect<OpenMPDialect>();
3321 Operation &distributeOp = **distChildOps.begin();
3322 for (Operation &childOp : getOps()) {
3323 if (&childOp == &distributeOp || ompDialect != childOp.getDialect())
3324 continue;
3325
3326 if (!childOp.hasTrait<OpTrait::IsTerminator>())
3327 return emitError() << "unexpected OpenMP operation inside of composite "
3328 "'omp.parallel': "
3329 << childOp.getName();
3330 }
3331 } else if (isComposite()) {
3332 return emitError()
3333 << "'omp.composite' attribute present in non-composite operation";
3334 }
3335 return success();
3336}
3337
3338//===----------------------------------------------------------------------===//
3339// TeamsOp
3340//===----------------------------------------------------------------------===//
3341
3343 while ((op = op->getParentOp()))
3344 if (isa<OpenMPDialect>(op->getDialect()))
3345 return false;
3346 return true;
3347}
3348
3349void TeamsOp::build(OpBuilder &builder, OperationState &state,
3350 const TeamsOperands &clauses) {
3351 MLIRContext *ctx = builder.getContext();
3352 // TODO Store clauses in op: privateVars, privateSyms, privateNeedsBarrier
3353 TeamsOp::build(
3354 builder, state, clauses.allocateVars, clauses.allocatorVars,
3355 makeDenseI64ArrayAttr(ctx, clauses.allocateAlignments),
3356 makeDenseI64ArrayAttr(ctx, clauses.allocatePrivateIndices),
3357 clauses.dynGroupprivateAccessGroup, clauses.dynGroupprivateFallback,
3358 clauses.dynGroupprivateSize, clauses.ifExpr, clauses.numTeamsLower,
3359 clauses.numTeamsUpperVars, /*private_vars=*/{}, /*private_syms=*/nullptr,
3360 /*private_needs_barrier=*/false,
3361 makeArrayAttr(ctx, clauses.reductionMods), clauses.reductionVars,
3362 makeDenseBoolArrayAttr(ctx, clauses.reductionByref),
3363 makeArrayAttr(ctx, clauses.reductionSyms), clauses.threadLimitVars);
3364}
3365
3366// Verify num_teams clause
3367static LogicalResult verifyNumTeamsClause(Operation *op, Value numTeamsLower,
3368 OperandRange numTeamsUpperVars) {
3369 // If lower is specified, upper must have exactly one value
3370 if (numTeamsLower) {
3371 if (numTeamsUpperVars.size() != 1)
3372 return op->emitError(
3373 "expected exactly one num_teams upper bound when lower bound is "
3374 "specified");
3375 if (numTeamsLower.getType() != numTeamsUpperVars[0].getType())
3376 return op->emitError(
3377 "expected num_teams upper bound and lower bound to be "
3378 "the same type");
3379 }
3380
3381 return success();
3382}
3383
3384LogicalResult TeamsOp::verify() {
3385 // Check parent region
3386 // TODO If nested inside of a target region, also check that it does not
3387 // contain any statements, declarations or directives other than this
3388 // omp.teams construct. The issue is how to support the initialization of
3389 // this operation's own arguments (allow SSA values across omp.target?).
3390 Operation *op = getOperation();
3391 auto parentTarget = llvm::dyn_cast_if_present<TargetOp>(op->getParentOp());
3392 if (!parentTarget && !opInGlobalImplicitParallelRegion(op))
3393 return emitError("expected to be nested inside of omp.target or not nested "
3394 "in any OpenMP dialect operations");
3395
3396 // Check for num_teams clause restrictions
3397 if (failed(verifyNumTeamsClause(op, this->getNumTeamsLower(),
3398 this->getNumTeamsUpperVars())))
3399 return failure();
3400
3401 if (parentTarget &&
3402 parentTarget.getKernelType() == TargetExecMode::spmd_no_loop &&
3403 (getNumTeamsLower() || !getNumTeamsUpperVars().empty()))
3404 return emitOpError() << "'num_teams' not allowed in SPMD-no-loop kernels";
3405
3407 getOperation(), getAllocateVars(), getAllocatorVars(),
3408 getAllocateAlignmentsAttr(), getAllocatePrivateIndicesAttr(),
3409 getPrivateVars(), getPrivateSymsAttr())))
3410 return failure();
3411
3413 op, getDynGroupprivateAccessGroupAttr(),
3414 getDynGroupprivateFallbackAttr(), getDynGroupprivateSize())))
3415 return failure();
3416
3417 if (failed(verifyPrivateVarList(*this)))
3418 return failure();
3419
3420 return verifyReductionVarList(*this, getReductionSyms(), getReductionVars(),
3421 getReductionByref(), getReductionModsAttr());
3422}
3423
3424//===----------------------------------------------------------------------===//
3425// SectionOp
3426//===----------------------------------------------------------------------===//
3427
3428OperandRange SectionOp::getPrivateVars() {
3429 return getParentOp().getPrivateVars();
3430}
3431
3432OperandRange SectionOp::getReductionVars() {
3433 return getParentOp().getReductionVars();
3434}
3435
3436//===----------------------------------------------------------------------===//
3437// SectionsOp
3438//===----------------------------------------------------------------------===//
3439
3440void SectionsOp::build(OpBuilder &builder, OperationState &state,
3441 const SectionsOperands &clauses) {
3442 MLIRContext *ctx = builder.getContext();
3443 // TODO Store clauses in op: privateVars, privateSyms, privateNeedsBarrier
3444 SectionsOp::build(builder, state, clauses.allocateVars, clauses.allocatorVars,
3445 makeDenseI64ArrayAttr(ctx, clauses.allocateAlignments),
3446 makeDenseI64ArrayAttr(ctx, clauses.allocatePrivateIndices),
3447 clauses.nowait, /*private_vars=*/{},
3448 /*private_syms=*/nullptr, /*private_needs_barrier=*/nullptr,
3449 /*private_assume_bounded_recipe_effects=*/nullptr,
3450 makeArrayAttr(ctx, clauses.reductionMods),
3451 clauses.reductionVars,
3452 makeDenseBoolArrayAttr(ctx, clauses.reductionByref),
3453 makeArrayAttr(ctx, clauses.reductionSyms),
3454 /*reduction_assume_bounded_recipe_effects=*/nullptr);
3455}
3456
3457LogicalResult SectionsOp::verify() {
3458 if (isCombined())
3459 return emitOpError() << "cannot be a non-innermost combined construct leaf";
3460
3462 getOperation(), getAllocateVars(), getAllocatorVars(),
3463 getAllocateAlignmentsAttr(), getAllocatePrivateIndicesAttr(),
3464 getPrivateVars(), getPrivateSymsAttr())))
3465 return failure();
3466
3467 return verifyReductionVarList(*this, getReductionSyms(), getReductionVars(),
3468 getReductionByref(), getReductionModsAttr());
3469}
3470
3471LogicalResult SectionsOp::verifyRegions() {
3472 for (auto &inst : *getRegion().begin()) {
3473 if (!(isa<SectionOp>(inst) || isa<TerminatorOp>(inst))) {
3474 return emitOpError()
3475 << "expected omp.section op or terminator op inside region";
3476 }
3477 }
3478
3479 return success();
3480}
3481
3482/// Add arbitrary read, write, allocate, and free effects.
3485 effects.emplace_back(MemoryEffects::Read::get());
3486 effects.emplace_back(MemoryEffects::Write::get());
3487 effects.emplace_back(MemoryEffects::Allocate::get());
3488 effects.emplace_back(MemoryEffects::Free::get());
3489}
3490
3491/// Privatisation may read the original as a mold or firstprivate copy source.
3492/// Any lastprivate copy-back is explicit in the nested operations.
3495 auto iface = dyn_cast<BlockArgOpenMPOpInterface>(op);
3496
3497 OperandRange sourceVars = iface.getPrivateVars();
3498 for (OpOperand &source :
3499 MutableArrayRef(sourceVars.getBase(), sourceVars.size()))
3500 effects.emplace_back(MemoryEffects::Read::get(), &source);
3501 for (BlockArgument priv : iface.getPrivateBlockArgs()) {
3502 effects.emplace_back(MemoryEffects::Write::get(), priv);
3503 effects.emplace_back(MemoryEffects::Allocate::get(), priv);
3504 effects.emplace_back(MemoryEffects::Free::get(), priv);
3505 }
3506}
3507
3508/// Reductions initialise private accumulators and combine them into the
3509/// original variables. Account for all three reduction clauses.
3510static void
3513 auto iface = dyn_cast<BlockArgOpenMPOpInterface>(op);
3514
3515 auto addEffects = [&](OperandRange sourceVars,
3516 ArrayRef<BlockArgument> privateArgs) {
3517 for (OpOperand &source :
3518 MutableArrayRef(sourceVars.getBase(), sourceVars.size())) {
3519 effects.emplace_back(MemoryEffects::Read::get(), &source);
3520 effects.emplace_back(MemoryEffects::Write::get(), &source);
3521 }
3522
3523 for (BlockArgument priv : privateArgs) {
3524 effects.emplace_back(MemoryEffects::Allocate::get(), priv);
3525 effects.emplace_back(MemoryEffects::Free::get(), priv);
3526 effects.emplace_back(MemoryEffects::Read::get(), priv);
3527 effects.emplace_back(MemoryEffects::Write::get(), priv);
3528 }
3529 };
3530
3531 addEffects(iface.getReductionVars(), iface.getReductionBlockArgs());
3532 addEffects(iface.getInReductionVars(), iface.getInReductionBlockArgs());
3533 addEffects(iface.getTaskReductionVars(), iface.getTaskReductionBlockArgs());
3534}
3535
3538 &effects) {
3539 effects.emplace_back(MemoryEffects::Read::get());
3540 effects.emplace_back(MemoryEffects::Write::get());
3541}
3542
3543void SectionsOp::getEffects(
3545 &effects) {
3546 // The presence of the nowait clause eliminates the implicit barrier at the
3547 // end of a sections construct
3548 if (!getNowait()) {
3549 getBarrierEffects(effects);
3550 }
3551
3552 bool hasUnknownRecipeEffects =
3553 (!getPrivateVars().empty() && !getPrivateAssumeBoundedRecipeEffects()) ||
3554 (!getReductionVars().empty() &&
3555 !getReductionAssumeBoundedRecipeEffects());
3556
3557 if (hasUnknownRecipeEffects) {
3558 addUnknownMemoryEffects(effects);
3559 } else {
3560 getPrivatisationEffects(*this, effects);
3561 getReductionEffects(*this, effects);
3562 }
3563}
3564
3565//===----------------------------------------------------------------------===//
3566// ScopeOp
3567//===----------------------------------------------------------------------===//
3568
3569void ScopeOp::build(OpBuilder &builder, OperationState &state,
3570 const ScopeOperands &clauses) {
3571 MLIRContext *ctx = builder.getContext();
3572 ScopeOp::build(
3573 builder, state, clauses.allocateVars, clauses.allocatorVars,
3574 makeDenseI64ArrayAttr(ctx, clauses.allocateAlignments),
3575 makeDenseI64ArrayAttr(ctx, clauses.allocatePrivateIndices),
3576 clauses.nowait, clauses.privateVars,
3577 makeArrayAttr(ctx, clauses.privateSyms), clauses.privateNeedsBarrier,
3578 makeArrayAttr(ctx, clauses.reductionMods), clauses.reductionVars,
3579 makeDenseBoolArrayAttr(ctx, clauses.reductionByref),
3580 makeArrayAttr(ctx, clauses.reductionSyms));
3581}
3582
3583LogicalResult ScopeOp::verify() {
3585 getOperation(), getAllocateVars(), getAllocatorVars(),
3586 getAllocateAlignmentsAttr(), getAllocatePrivateIndicesAttr(),
3587 getPrivateVars(), getPrivateSymsAttr(),
3588 /*requirePrivateIndices=*/true)))
3589 return failure();
3590
3591 if (failed(verifyPrivateVarList(*this)))
3592 return failure();
3593
3594 return verifyReductionVarList(*this, getReductionSyms(), getReductionVars(),
3595 getReductionByref(), getReductionModsAttr());
3596}
3597
3598//===----------------------------------------------------------------------===//
3599// SingleOp
3600//===----------------------------------------------------------------------===//
3601
3602void SingleOp::build(OpBuilder &builder, OperationState &state,
3603 const SingleOperands &clauses) {
3604 MLIRContext *ctx = builder.getContext();
3605 // TODO Store clauses in op: privateVars, privateSyms, privateNeedsBarrier
3606 SingleOp::build(
3607 builder, state, clauses.allocateVars, clauses.allocatorVars,
3608 makeDenseI64ArrayAttr(ctx, clauses.allocateAlignments),
3609 makeDenseI64ArrayAttr(ctx, clauses.allocatePrivateIndices),
3610 clauses.copyprivateVars, makeArrayAttr(ctx, clauses.copyprivateSyms),
3611 /*copyprivate_assume_bounded_recipe_effects=*/nullptr, clauses.nowait,
3612 /*private_vars=*/{}, /*private_syms=*/nullptr,
3613 /*private_needs_barrier=*/nullptr,
3614 /*private_assume_bounded_recipe_effects=*/nullptr);
3615}
3616
3617LogicalResult SingleOp::verify() {
3619 getOperation(), getAllocateVars(), getAllocatorVars(),
3620 getAllocateAlignmentsAttr(), getAllocatePrivateIndicesAttr(),
3621 getPrivateVars(), getPrivateSymsAttr())))
3622 return failure();
3623
3624 return verifyCopyprivateVarList(*this, getCopyprivateVars(),
3625 getCopyprivateSyms());
3626}
3627
3628void SingleOp::getEffects(
3630 &effects) {
3631 // The presence of the nowait clause eliminates the implicit barrier at the
3632 // end of a single construct.
3633 if (!getNowait()) {
3634 getBarrierEffects(effects);
3635 }
3636
3637 bool hasUnknownRecipeEffects =
3638 (!getPrivateVars().empty() && !getPrivateAssumeBoundedRecipeEffects()) ||
3639 (!getCopyprivateVars().empty() &&
3640 !getCopyprivateAssumeBoundedRecipeEffects());
3641 if (hasUnknownRecipeEffects) {
3642 addUnknownMemoryEffects(effects);
3643 } else {
3644 getPrivatisationEffects(*this, effects);
3645 // The unknown executing thread supplies the value and the other threads
3646 // receive it, so each copyprivate operand may be both read and written.
3647 OperandRange copyprivateVars = getCopyprivateVars();
3648 for (OpOperand &variable :
3649 MutableArrayRef(copyprivateVars.getBase(), copyprivateVars.size())) {
3650 effects.emplace_back(MemoryEffects::Read::get(), &variable);
3651 effects.emplace_back(MemoryEffects::Write::get(), &variable);
3652 }
3653 }
3654}
3655
3656//===----------------------------------------------------------------------===//
3657// WorkshareOp
3658//===----------------------------------------------------------------------===//
3659
3660void WorkshareOp::build(OpBuilder &builder, OperationState &state,
3661 const WorkshareOperands &clauses) {
3662 WorkshareOp::build(builder, state, clauses.nowait);
3663}
3664
3665LogicalResult WorkshareOp::verify() {
3666 if (isCombined())
3667 return emitOpError() << "cannot be a non-innermost combined construct leaf";
3668
3669 return success();
3670}
3671
3672//===----------------------------------------------------------------------===//
3673// WorkshareLoopWrapperOp
3674//===----------------------------------------------------------------------===//
3675
3676LogicalResult WorkshareLoopWrapperOp::verifyRegions() {
3677 if (isa_and_nonnull<LoopWrapperInterface>((*this)->getParentOp()) ||
3678 getNestedWrapper())
3679 return emitOpError() << "expected to be a standalone loop wrapper";
3680
3681 return success();
3682}
3683
3684//===----------------------------------------------------------------------===//
3685// LoopWrapperInterface
3686//===----------------------------------------------------------------------===//
3687
3688LogicalResult LoopWrapperInterface::verifyImpl() {
3689 Operation *op = this->getOperation();
3690 if (!op->hasTrait<OpTrait::NoTerminator>() ||
3692 return emitOpError() << "loop wrapper must also have the `NoTerminator` "
3693 "and `SingleBlock` traits";
3694
3695 if (op->getNumRegions() != 1)
3696 return emitOpError() << "loop wrapper does not contain exactly one region";
3697
3698 Region &region = op->getRegion(0);
3699 if (range_size(region.getOps()) != 1)
3700 return emitOpError()
3701 << "loop wrapper does not contain exactly one nested op";
3702
3703 Operation &firstOp = *region.op_begin();
3704 if (!isa<LoopNestOp, LoopWrapperInterface>(firstOp))
3705 return emitOpError() << "nested in loop wrapper is not another loop "
3706 "wrapper or `omp.loop_nest`";
3707
3708 return success();
3709}
3710
3711//===----------------------------------------------------------------------===//
3712// ComposableOpInterface
3713//===----------------------------------------------------------------------===//
3714
3715Operation *ComposableOpInterface::findCapturedOp() {
3716 Operation *op = this->getOperation();
3717
3718 // Handle the composite case by returning the wrapped omp.loop_nest.
3719 if (auto wrapperOp = dyn_cast<LoopWrapperInterface>(op))
3720 return wrapperOp.getWrappedLoop();
3721
3722 // Do not look further if this op is not combined with any of its children.
3723 // Need to check for composite for the omp.parallel case, which is not a loop
3724 // wrapper itself.
3725 if (!isCombined() && !isComposite())
3726 return op;
3727
3728 Region &region = op->getRegion(0);
3729 for (Operation &nestedOp : region.getOps()) {
3730 if (auto wrapperOp = dyn_cast<LoopWrapperInterface>(&nestedOp))
3731 return wrapperOp.getWrappedLoop();
3732
3733 if (auto composableOp = dyn_cast<ComposableOpInterface>(&nestedOp))
3734 return composableOp.findCapturedOp();
3735 }
3736
3737 // This can only be reached if the op has an omp.combined attribute but the
3738 // corresponding nested composable op has been deleted. In that case, it's
3739 // correct to return this operation.
3740 return op;
3741}
3742
3743LogicalResult ComposableOpInterface::verifyImpl() {
3744 Operation *op = this->getOperation();
3745
3746 if (op->getNumRegions() != 1)
3747 return emitOpError() << "composable ops must have a single region";
3748
3749 if (isComposite() && !isa<LoopWrapperInterface, ParallelOp>(op))
3750 return emitOpError() << "non-loop wrapper cannot be composite";
3751
3752 // If combined, must have exactly one eligible nested op (composable or loop
3753 // wrapper).
3754 if (isCombined()) {
3755 Operation *nestedOp = nullptr;
3756 auto count = llvm::count_if(
3757 op->getRegion(0).getOps(), [&nestedOp](mlir::Operation &op) {
3758 if (isa<ComposableOpInterface, LoopWrapperInterface>(op)) {
3759 nestedOp = &op;
3760 return true;
3761 }
3762 return false;
3763 });
3764
3765 // Make an exception for ops marked as omp.combined with no eligible nested
3766 // ops: this situation should be disallowed, but it can be reached if an
3767 // MLIR optimization pass find that the child operation has no side effects
3768 // (many ComposableOpInterface ops have RecursiveMemoryEffects), so it gets
3769 // deleted without updating the parent's attribute.
3770 //
3771 // Since there's a well defined way of handling that situation (treat it as
3772 // non-combined), we relax the requirement here. Ensuring the parent is
3773 // updated every time a pass that can potentially remove a child composable
3774 // op runs is less preferable as a solution.
3775 if (count == 0)
3776 return success();
3777
3778 if (count > 1)
3779 return emitOpError()
3780 << "multiple eligible child ops found in combined op";
3781
3782 // This operation cannot be combined if its captured nested op can be
3783 // executed more than once (i.e. its block's successors can reach it) or if
3784 // it's not guaranteed to be executed before all exits of the region (i.e.
3785 // it doesn't dominate all blocks with no successors reachable from the
3786 // entry block).
3787 DominanceInfo domInfo;
3788 Block *parentBlock = nestedOp->getBlock();
3789
3790 for (Block *successor : parentBlock->getSuccessors())
3791 if (successor->isReachable(parentBlock))
3792 return emitOpError() << "nested combined child op is part of a loop";
3793
3794 for (Block &block : op->getRegion(0))
3795 if (domInfo.isReachableFromEntry(&block) && block.hasNoSuccessors() &&
3796 !domInfo.dominates(parentBlock, &block))
3797 return emitOpError()
3798 << "nested combined child op doesn't unconditionally execute";
3799 }
3800 return success();
3801}
3802
3803//===----------------------------------------------------------------------===//
3804// LoopOp
3805//===----------------------------------------------------------------------===//
3806
3807void LoopOp::build(OpBuilder &builder, OperationState &state,
3808 const LoopOperands &clauses) {
3809 MLIRContext *ctx = builder.getContext();
3810
3811 LoopOp::build(builder, state, clauses.bindKind, clauses.privateVars,
3812 makeArrayAttr(ctx, clauses.privateSyms),
3813 clauses.privateNeedsBarrier, clauses.order, clauses.orderMod,
3814 makeArrayAttr(ctx, clauses.reductionMods),
3815 clauses.reductionVars,
3816 makeDenseBoolArrayAttr(ctx, clauses.reductionByref),
3817 makeArrayAttr(ctx, clauses.reductionSyms));
3818}
3819
3820LogicalResult LoopOp::verify() {
3821 if (failed(verifyPrivateVarList(*this)))
3822 return failure();
3823
3824 return verifyReductionVarList(*this, getReductionSyms(), getReductionVars(),
3825 getReductionByref(), getReductionModsAttr());
3826}
3827
3828LogicalResult LoopOp::verifyRegions() {
3829 if (llvm::isa_and_nonnull<LoopWrapperInterface>((*this)->getParentOp()) ||
3830 getNestedWrapper())
3831 return emitOpError() << "expected to be a standalone loop wrapper";
3832
3833 return success();
3834}
3835
3836//===----------------------------------------------------------------------===//
3837// WsloopOp
3838//===----------------------------------------------------------------------===//
3839
3840void WsloopOp::build(OpBuilder &builder, OperationState &state,
3841 ArrayRef<NamedAttribute> attributes) {
3842 build(builder, state, /*allocate_vars=*/{}, /*allocator_vars=*/{},
3843 /*allocate_alignments=*/nullptr,
3844 /*allocate_private_indices=*/nullptr,
3845 /*linear_vars=*/ValueRange(), /*linear_step_vars=*/ValueRange(),
3846 /*linear_var_types*/ nullptr, /*linear_modifiers=*/nullptr,
3847 /*linear_syms=*/nullptr, /*nowait=*/false, /*order=*/nullptr,
3848 /*order_mod=*/nullptr,
3849 /*ordered=*/nullptr, /*private_vars=*/{}, /*private_syms=*/nullptr,
3850 /*private_needs_barrier=*/false,
3851 /*reduction_mods=*/nullptr, /*reduction_vars=*/ValueRange(),
3852 /*reduction_byref=*/nullptr,
3853 /*reduction_syms=*/nullptr, /*schedule_kind=*/nullptr,
3854 /*schedule_chunk=*/nullptr, /*schedule_mod=*/nullptr,
3855 /*schedule_simd=*/false);
3856 state.addAttributes(attributes);
3857}
3858
3859void WsloopOp::build(OpBuilder &builder, OperationState &state,
3860 const WsloopOperands &clauses) {
3861 MLIRContext *ctx = builder.getContext();
3862 WsloopOp::build(
3863 builder, state, clauses.allocateVars, clauses.allocatorVars,
3864 makeDenseI64ArrayAttr(ctx, clauses.allocateAlignments),
3865 makeDenseI64ArrayAttr(ctx, clauses.allocatePrivateIndices),
3866 clauses.linearVars, clauses.linearStepVars, clauses.linearVarTypes,
3867 clauses.linearModifiers, makeArrayAttr(ctx, clauses.linearSyms),
3868 clauses.nowait, clauses.order, clauses.orderMod, clauses.ordered,
3869 clauses.privateVars, makeArrayAttr(ctx, clauses.privateSyms),
3870 clauses.privateNeedsBarrier, makeArrayAttr(ctx, clauses.reductionMods),
3871 clauses.reductionVars,
3872 makeDenseBoolArrayAttr(ctx, clauses.reductionByref),
3873 makeArrayAttr(ctx, clauses.reductionSyms), clauses.scheduleKind,
3874 clauses.scheduleChunk, clauses.scheduleMod, clauses.scheduleSimd);
3875}
3876
3877LogicalResult WsloopOp::verify() {
3879 getOperation(), getAllocateVars(), getAllocatorVars(),
3880 getAllocateAlignmentsAttr(), getAllocatePrivateIndicesAttr(),
3881 getPrivateVars(), getPrivateSymsAttr())))
3882 return failure();
3883
3884 if (failed(
3885 verifyLinearModifiers(*this, getLinearModifiers(), getLinearVars())))
3886 return failure();
3887 if (getLinearVars().size() &&
3888 getLinearVarTypes().value().size() != getLinearVars().size())
3889 return emitError() << "Ill-formed type attributes for linear variables";
3890
3891 if (failed(verifyLinearSyms(*this)) || failed(verifyPrivateVarList(*this)))
3892 return failure();
3893
3894 return verifyReductionVarList(*this, getReductionSyms(), getReductionVars(),
3895 getReductionByref(), getReductionModsAttr());
3896}
3897
3898LogicalResult WsloopOp::verifyRegions() {
3899 bool isCompositeChildLeaf =
3900 llvm::dyn_cast_if_present<LoopWrapperInterface>((*this)->getParentOp());
3901
3902 if (LoopWrapperInterface nested = getNestedWrapper()) {
3903 if (!isComposite())
3904 return emitError()
3905 << "'omp.composite' attribute missing from composite wrapper";
3906
3907 // Check for the allowed leaf constructs that may appear in a composite
3908 // construct directly after DO/FOR.
3909 if (!isa<SimdOp>(nested))
3910 return emitError() << "only supported nested wrapper is 'omp.simd'";
3911
3912 } else if (isComposite() && !isCompositeChildLeaf) {
3913 return emitError()
3914 << "'omp.composite' attribute present in non-composite wrapper";
3915 } else if (!isComposite() && isCompositeChildLeaf) {
3916 return emitError()
3917 << "'omp.composite' attribute missing from composite wrapper";
3918 }
3919
3920 return success();
3921}
3922
3923//===----------------------------------------------------------------------===//
3924// Simd construct [2.9.3.1]
3925//===----------------------------------------------------------------------===//
3926
3927void SimdOp::build(OpBuilder &builder, OperationState &state,
3928 const SimdOperands &clauses) {
3929 MLIRContext *ctx = builder.getContext();
3930 SimdOp::build(
3931 builder, state, clauses.alignedVars,
3932 makeArrayAttr(ctx, clauses.alignments), clauses.ifExpr,
3933 clauses.linearVars, clauses.linearStepVars, clauses.linearVarTypes,
3934 clauses.linearModifiers, makeArrayAttr(ctx, clauses.linearSyms),
3935 clauses.nontemporalVars, clauses.order, clauses.orderMod,
3936 clauses.privateVars, makeArrayAttr(ctx, clauses.privateSyms),
3937 clauses.privateNeedsBarrier, makeArrayAttr(ctx, clauses.reductionMods),
3938 clauses.reductionVars,
3939 makeDenseBoolArrayAttr(ctx, clauses.reductionByref),
3940 makeArrayAttr(ctx, clauses.reductionSyms), clauses.safelen,
3941 clauses.simdlen);
3942}
3943
3944LogicalResult SimdOp::verify() {
3945 if (failed(verifyReductionModifiers(*this, getReductionVars(),
3946 getReductionModsAttr())))
3947 return failure();
3948
3949 if (getSimdlen().has_value() && getSafelen().has_value() &&
3950 getSimdlen().value() > getSafelen().value())
3951 return emitOpError()
3952 << "simdlen clause and safelen clause are both present, but the "
3953 "simdlen value is not less than or equal to safelen value";
3954
3955 if (verifyAlignedClause(*this, getAlignments(), getAlignedVars()).failed())
3956 return failure();
3957
3958 if (verifyNontemporalClause(*this, getNontemporalVars()).failed())
3959 return failure();
3960
3961 if (failed(
3962 verifyLinearModifiers(*this, getLinearModifiers(), getLinearVars())))
3963 return failure();
3964
3965 bool isCompositeChildLeaf =
3966 llvm::dyn_cast_if_present<LoopWrapperInterface>((*this)->getParentOp());
3967
3968 if (!isComposite() && isCompositeChildLeaf)
3969 return emitError()
3970 << "'omp.composite' attribute missing from composite wrapper";
3971
3972 if (isComposite() && !isCompositeChildLeaf)
3973 return emitError()
3974 << "'omp.composite' attribute present in non-composite wrapper";
3975
3976 // Firstprivate is not allowed for SIMD in the standard. Check that none of
3977 // the private decls are for firstprivate.
3978 std::optional<ArrayAttr> privateSyms = getPrivateSyms();
3979 if (privateSyms) {
3980 for (const Attribute &sym : *privateSyms) {
3981 auto symRef = cast<SymbolRefAttr>(sym);
3982 omp::PrivateClauseOp privatizer =
3984 getOperation(), symRef);
3985 if (!privatizer)
3986 return emitError() << "Cannot find privatizer '" << symRef << "'";
3987 if (privatizer.getDataSharingType() ==
3988 DataSharingClauseType::FirstPrivate)
3989 return emitError() << "FIRSTPRIVATE cannot be used with SIMD";
3990 }
3991 }
3992
3993 if (failed(verifyPrivateVarList(*this)))
3994 return failure();
3995
3996 if (getLinearVars().size() &&
3997 getLinearVarTypes().value().size() != getLinearVars().size())
3998 return emitError() << "Ill-formed type attributes for linear variables";
3999
4000 if (failed(verifyLinearSyms(*this)))
4001 return failure();
4002
4003 llvm::DenseSet<Value> privateVars(llvm::from_range, getPrivateVars());
4004 llvm::DenseSet<Value> reductionVars(llvm::from_range, getReductionVars());
4005 // TODO Check lastprivate vars when their support is added to SimdOp.
4006 for (Value var : getLinearVars()) {
4007 if (privateVars.contains(var) || reductionVars.contains(var))
4008 return emitOpError()
4009 << "linear variables cannot appear in other data-sharing clauses";
4010 }
4011
4012 return success();
4013}
4014
4015LogicalResult SimdOp::verifyRegions() {
4016 if (getNestedWrapper())
4017 return emitOpError() << "must wrap an 'omp.loop_nest' directly";
4018
4019 return success();
4020}
4021
4022//===----------------------------------------------------------------------===//
4023// Distribute construct [2.9.4.1]
4024//===----------------------------------------------------------------------===//
4025
4026void DistributeOp::build(OpBuilder &builder, OperationState &state,
4027 const DistributeOperands &clauses) {
4028 DistributeOp::build(
4029 builder, state, clauses.allocateVars, clauses.allocatorVars,
4030 makeDenseI64ArrayAttr(builder.getContext(), clauses.allocateAlignments),
4032 clauses.allocatePrivateIndices),
4033 clauses.distScheduleStatic, clauses.distScheduleChunkSize, clauses.order,
4034 clauses.orderMod, clauses.privateVars,
4035 makeArrayAttr(builder.getContext(), clauses.privateSyms),
4036 clauses.privateNeedsBarrier);
4037}
4038
4039LogicalResult DistributeOp::verify() {
4040 if (this->getDistScheduleChunkSize() && !this->getDistScheduleStatic())
4041 return emitOpError() << "chunk size set without "
4042 "dist_schedule_static being present";
4043
4045 getOperation(), getAllocateVars(), getAllocatorVars(),
4046 getAllocateAlignmentsAttr(), getAllocatePrivateIndicesAttr(),
4047 getPrivateVars(), getPrivateSymsAttr())))
4048 return failure();
4049
4050 if (failed(verifyPrivateVarList(*this)))
4051 return failure();
4052
4053 return success();
4054}
4055
4056LogicalResult DistributeOp::verifyRegions() {
4057 if (LoopWrapperInterface nested = getNestedWrapper()) {
4058 if (!isComposite())
4059 return emitError()
4060 << "'omp.composite' attribute missing from composite wrapper";
4061 // Check for the allowed leaf constructs that may appear in a composite
4062 // construct directly after DISTRIBUTE.
4063 if (isa<WsloopOp>(nested)) {
4064 Operation *parentOp = (*this)->getParentOp();
4065 if (!llvm::dyn_cast_if_present<ParallelOp>(parentOp) ||
4066 !cast<ComposableOpInterface>(parentOp).isComposite()) {
4067 return emitError() << "an 'omp.wsloop' nested wrapper is only allowed "
4068 "when a composite 'omp.parallel' is the direct "
4069 "parent";
4070 }
4071 } else if (!isa<SimdOp>(nested))
4072 return emitError() << "only supported nested wrappers are 'omp.simd' and "
4073 "'omp.wsloop'";
4074 } else if (isComposite()) {
4075 return emitError()
4076 << "'omp.composite' attribute present in non-composite wrapper";
4077 }
4078
4079 return success();
4080}
4081
4082//===----------------------------------------------------------------------===//
4083// DeclareMapperOp / DeclareMapperInfoOp
4084//===----------------------------------------------------------------------===//
4085
4086void DeclareMapperInfoOp::build(OpBuilder &builder, OperationState &state,
4087 const DeclareMapperInfoOperands &clauses) {
4088 DeclareMapperInfoOp::build(builder, state, clauses.mapVars,
4089 clauses.mapIterated);
4090}
4091
4092LogicalResult DeclareMapperInfoOp::verify() {
4093 return verifyMapClause(*this, getMapVars(), getMapIterated());
4094}
4095
4096LogicalResult DeclareMapperOp::verifyRegions() {
4097 if (!llvm::isa_and_present<DeclareMapperInfoOp>(
4098 getRegion().getBlocks().front().getTerminator()))
4099 return emitOpError() << "expected terminator to be a DeclareMapperInfoOp";
4100
4101 return success();
4102}
4103
4104//===----------------------------------------------------------------------===//
4105// DeclareReductionOp
4106//===----------------------------------------------------------------------===//
4107
4108LogicalResult DeclareReductionOp::verifyRegions() {
4109 if (!getAllocRegion().empty()) {
4110 for (YieldOp yieldOp : getAllocRegion().getOps<YieldOp>()) {
4111 if (yieldOp.getResults().size() != 1 ||
4112 yieldOp.getResults().getTypes()[0] != getType())
4113 return emitOpError() << "expects alloc region to yield a value "
4114 "of the reduction type";
4115 }
4116 }
4117
4118 if (getInitializerRegion().empty())
4119 return emitOpError() << "expects non-empty initializer region";
4120 Block &initializerEntryBlock = getInitializerRegion().front();
4121
4122 if (initializerEntryBlock.getNumArguments() == 1) {
4123 if (!getAllocRegion().empty())
4124 return emitOpError() << "expects two arguments to the initializer region "
4125 "when an allocation region is used";
4126 } else if (initializerEntryBlock.getNumArguments() == 2) {
4127 if (getAllocRegion().empty())
4128 return emitOpError() << "expects one argument to the initializer region "
4129 "when no allocation region is used";
4130 } else {
4131 return emitOpError()
4132 << "expects one or two arguments to the initializer region";
4133 }
4134
4135 for (mlir::Value arg : initializerEntryBlock.getArguments())
4136 if (arg.getType() != getType())
4137 return emitOpError() << "expects initializer region argument to match "
4138 "the reduction type";
4139
4140 for (YieldOp yieldOp : getInitializerRegion().getOps<YieldOp>()) {
4141 if (yieldOp.getResults().size() != 1 ||
4142 yieldOp.getResults().getTypes()[0] != getType())
4143 return emitOpError() << "expects initializer region to yield a value "
4144 "of the reduction type";
4145 }
4146
4147 if (getReductionRegion().empty())
4148 return emitOpError() << "expects non-empty reduction region";
4149 Block &reductionEntryBlock = getReductionRegion().front();
4150 if (reductionEntryBlock.getNumArguments() != 2 ||
4151 reductionEntryBlock.getArgumentTypes()[0] !=
4152 reductionEntryBlock.getArgumentTypes()[1] ||
4153 reductionEntryBlock.getArgumentTypes()[0] != getType())
4154 return emitOpError() << "expects reduction region with two arguments of "
4155 "the reduction type";
4156 for (YieldOp yieldOp : getReductionRegion().getOps<YieldOp>()) {
4157 if (yieldOp.getResults().size() != 1 ||
4158 yieldOp.getResults().getTypes()[0] != getType())
4159 return emitOpError() << "expects reduction region to yield a value "
4160 "of the reduction type";
4161 }
4162
4163 if (!getAtomicReductionRegion().empty()) {
4164 Block &atomicReductionEntryBlock = getAtomicReductionRegion().front();
4165 if (atomicReductionEntryBlock.getNumArguments() != 2 ||
4166 atomicReductionEntryBlock.getArgumentTypes()[0] !=
4167 atomicReductionEntryBlock.getArgumentTypes()[1])
4168 return emitOpError() << "expects atomic reduction region with two "
4169 "arguments of the same type";
4170 auto ptrType = llvm::dyn_cast<PointerLikeType>(
4171 atomicReductionEntryBlock.getArgumentTypes()[0]);
4172 if (!ptrType ||
4173 (ptrType.getElementType() && ptrType.getElementType() != getType()))
4174 return emitOpError() << "expects atomic reduction region arguments to "
4175 "be accumulators containing the reduction type";
4176 }
4177
4178 if (getCleanupRegion().empty())
4179 return success();
4180 Block &cleanupEntryBlock = getCleanupRegion().front();
4181 if (cleanupEntryBlock.getNumArguments() != 1 ||
4182 cleanupEntryBlock.getArgument(0).getType() != getType())
4183 return emitOpError() << "expects cleanup region with one argument "
4184 "of the reduction type";
4185
4186 return success();
4187}
4188
4189//===----------------------------------------------------------------------===//
4190// TaskOp
4191//===----------------------------------------------------------------------===//
4192
4193void TaskOp::build(OpBuilder &builder, OperationState &state,
4194 const TaskOperands &clauses) {
4195 MLIRContext *ctx = builder.getContext();
4196 TaskOp::build(builder, state, clauses.iterated, clauses.affinityVars,
4197 clauses.allocateVars, clauses.allocatorVars,
4198 makeDenseI64ArrayAttr(ctx, clauses.allocateAlignments),
4199 makeDenseI64ArrayAttr(ctx, clauses.allocatePrivateIndices),
4200 makeArrayAttr(ctx, clauses.dependKinds), clauses.dependVars,
4201 makeArrayAttr(ctx, clauses.dependIteratedKinds),
4202 clauses.dependIterated, clauses.final, clauses.ifExpr,
4203 clauses.inReductionVars,
4204 makeDenseBoolArrayAttr(ctx, clauses.inReductionByref),
4205 makeArrayAttr(ctx, clauses.inReductionSyms), clauses.mergeable,
4206 clauses.priority, /*private_vars=*/clauses.privateVars,
4207 /*private_syms=*/makeArrayAttr(ctx, clauses.privateSyms),
4208 clauses.privateNeedsBarrier, clauses.threadset, clauses.untied,
4209 clauses.eventHandle);
4210}
4211
4212LogicalResult TaskOp::verify() {
4214 getOperation(), getAllocateVars(), getAllocatorVars(),
4215 getAllocateAlignmentsAttr(), getAllocatePrivateIndicesAttr(),
4216 getPrivateVars(), getPrivateSymsAttr())))
4217 return failure();
4218
4219 LogicalResult verifyDependVars =
4220 verifyDependVarList(*this, getDependKinds(), getDependVars(),
4221 getDependIteratedKinds(), getDependIterated());
4222 if (failed(verifyDependVars))
4223 return verifyDependVars;
4224
4225 if (failed(verifyPrivateVarList(*this)))
4226 return failure();
4227
4228 return verifyReductionVarList(*this, getInReductionSyms(),
4229 getInReductionVars(), getInReductionByref());
4230}
4231
4232//===----------------------------------------------------------------------===//
4233// TaskgroupOp
4234//===----------------------------------------------------------------------===//
4235
4236void TaskgroupOp::build(OpBuilder &builder, OperationState &state,
4237 const TaskgroupOperands &clauses) {
4238 MLIRContext *ctx = builder.getContext();
4239 TaskgroupOp::build(builder, state, clauses.allocateVars,
4240 clauses.allocatorVars,
4241 makeDenseI64ArrayAttr(ctx, clauses.allocateAlignments),
4242 makeDenseI64ArrayAttr(ctx, clauses.allocatePrivateIndices),
4243 clauses.taskReductionVars,
4244 makeDenseBoolArrayAttr(ctx, clauses.taskReductionByref),
4245 makeArrayAttr(ctx, clauses.taskReductionSyms));
4246}
4247
4248LogicalResult TaskgroupOp::verify() {
4250 getOperation(), getAllocateVars(), getAllocatorVars(),
4251 getAllocateAlignmentsAttr(), getAllocatePrivateIndicesAttr())))
4252 return failure();
4253
4254 return verifyReductionVarList(*this, getTaskReductionSyms(),
4255 getTaskReductionVars(),
4256 getTaskReductionByref());
4257}
4258
4259//===----------------------------------------------------------------------===//
4260// TaskloopContextOp
4261//===----------------------------------------------------------------------===//
4262
4263void TaskloopContextOp::build(OpBuilder &builder, OperationState &state,
4264 const TaskloopContextOperands &clauses) {
4265 MLIRContext *ctx = builder.getContext();
4266 TaskloopContextOp::build(
4267 builder, state, clauses.allocateVars, clauses.allocatorVars,
4268 makeDenseI64ArrayAttr(ctx, clauses.allocateAlignments),
4269 makeDenseI64ArrayAttr(ctx, clauses.allocatePrivateIndices), clauses.final,
4270 clauses.grainsizeMod, clauses.grainsize, clauses.ifExpr,
4271 clauses.inReductionVars,
4272 makeDenseBoolArrayAttr(ctx, clauses.inReductionByref),
4273 makeArrayAttr(ctx, clauses.inReductionSyms), clauses.mergeable,
4274 clauses.nogroup, clauses.numTasksMod, clauses.numTasks, clauses.priority,
4275 /*private_vars=*/clauses.privateVars,
4276 /*private_syms=*/makeArrayAttr(ctx, clauses.privateSyms),
4277 clauses.privateNeedsBarrier, makeArrayAttr(ctx, clauses.reductionMods),
4278 clauses.reductionVars,
4279 makeDenseBoolArrayAttr(ctx, clauses.reductionByref),
4280 makeArrayAttr(ctx, clauses.reductionSyms), clauses.threadset,
4281 clauses.untied);
4282 state.addAttribute("omp.combined", UnitAttr::get(ctx));
4283}
4284
4285TaskloopWrapperOp TaskloopContextOp::getLoopOp() {
4286 return cast<TaskloopWrapperOp>(
4287 *llvm::find_if(getRegion().front(), [](mlir::Operation &op) {
4288 return isa<TaskloopWrapperOp>(op);
4289 }));
4290}
4291
4292LogicalResult TaskloopContextOp::verify() {
4293 if (failed(verifyPrivateVarList(*this)))
4294 return failure();
4296 getOperation(), getAllocateVars(), getAllocatorVars(),
4297 getAllocateAlignmentsAttr(), getAllocatePrivateIndicesAttr(),
4298 getPrivateVars(), getPrivateSymsAttr())))
4299 return failure();
4300
4301 if (failed(verifyReductionVarList(*this, getReductionSyms(),
4302 getReductionVars(), getReductionByref(),
4303 getReductionModsAttr())) ||
4304 failed(verifyReductionVarList(*this, getInReductionSyms(),
4305 getInReductionVars(),
4306 getInReductionByref())))
4307 return failure();
4308
4309 if (!getReductionVars().empty() && getNogroup())
4310 return emitError("if a reduction clause is present on the taskloop "
4311 "directive, the nogroup clause must not be specified");
4312 for (auto var : getReductionVars()) {
4313 if (llvm::is_contained(getInReductionVars(), var))
4314 return emitError("the same list item cannot appear in both a reduction "
4315 "and an in_reduction clause");
4316 }
4317
4318 if (getGrainsize() && getNumTasks()) {
4319 return emitError(
4320 "the grainsize clause and num_tasks clause are mutually exclusive and "
4321 "may not appear on the same taskloop directive");
4322 }
4323
4324 // Without this restriction, any compound construct including `taskloop` would
4325 // fail to correctly identify the whole chain of operations (see
4326 // ComposableOpInterface::findCapturedOp()), as well as failing to do so even
4327 // for standalone `taskloop` constructs.
4328 if (!isCombined())
4329 return emitOpError("must always contain the 'omp.combined' attribute");
4330
4331 return success();
4332}
4333
4334LogicalResult TaskloopContextOp::verifyRegions() {
4335 Region &region = getRegion();
4336 auto loopWrapperIt = llvm::find_if(region.front(), [](mlir::Operation &op) {
4337 return isa<TaskloopWrapperOp>(op);
4338 });
4339 if (loopWrapperIt == region.front().end())
4340 return emitOpError()
4341 << "expected a TaskloopWrapperOp directly nested in the region";
4342
4343 auto loopWrapperOp = cast<TaskloopWrapperOp>(*loopWrapperIt);
4344 auto loopNestOp = dyn_cast<LoopNestOp>(loopWrapperOp.getWrappedLoop());
4345 // This will fail the verifier for TaskloopWrapperOp and print an error
4346 // message there.
4347 if (!loopNestOp)
4348 return failure();
4349
4350 std::function<bool(Value)> isValidBoundValue = [&](Value value) -> bool {
4351 Region *valueRegion = value.getParentRegion();
4352 // A loop bound value defined outside of the taskloop context region is
4353 // valid. A region is considered an ancestor of itself.
4354 if (!region.isAncestor(valueRegion))
4355 return true;
4356
4357 Operation *defOp = value.getDefiningOp();
4358 if (!defOp || defOp->getNumRegions() != 0 || !isPure(defOp))
4359 return false;
4360
4361 return llvm::all_of(defOp->getOperands(), isValidBoundValue);
4362 };
4363 auto hasUnsupportedTaskloopLocalBound = [&](OperandRange range) -> bool {
4364 return llvm::any_of(range,
4365 [&](Value value) { return !isValidBoundValue(value); });
4366 };
4367
4368 if (hasUnsupportedTaskloopLocalBound(loopNestOp.getLoopLowerBounds()) ||
4369 hasUnsupportedTaskloopLocalBound(loopNestOp.getLoopUpperBounds()) ||
4370 hasUnsupportedTaskloopLocalBound(loopNestOp.getLoopSteps())) {
4371 return emitOpError()
4372 << "expects loop bounds and steps to be defined outside of the "
4373 "taskloop.context region or by pure, regionless operations "
4374 "that do not depend on block arguments";
4375 }
4376
4377 return success();
4378}
4379
4380//===----------------------------------------------------------------------===//
4381// TaskloopWrapperOp
4382//===----------------------------------------------------------------------===//
4383
4384void TaskloopWrapperOp::build(OpBuilder &builder, OperationState &state,
4385 const TaskloopWrapperOperands &clauses) {
4386 TaskloopWrapperOp::build(builder, state);
4387}
4388
4389TaskloopContextOp TaskloopWrapperOp::getTaskloopContext() {
4390 return dyn_cast<TaskloopContextOp>(getOperation()->getParentOp());
4391}
4392
4393LogicalResult TaskloopWrapperOp::verify() {
4394 TaskloopContextOp context = getTaskloopContext();
4395 if (!context)
4396 return emitOpError() << "expected to be nested in a taskloop context op";
4397 return success();
4398}
4399
4400LogicalResult TaskloopWrapperOp::verifyRegions() {
4401 if (LoopWrapperInterface nested = getNestedWrapper()) {
4402 if (!isComposite())
4403 return emitError()
4404 << "'omp.composite' attribute missing from composite wrapper";
4405
4406 // Check for the allowed leaf constructs that may appear in a composite
4407 // construct directly after TASKLOOP.
4408 if (!isa<SimdOp>(nested))
4409 return emitError() << "only supported nested wrapper is 'omp.simd'";
4410 } else if (isComposite()) {
4411 return emitError()
4412 << "'omp.composite' attribute present in non-composite wrapper";
4413 }
4414
4415 return success();
4416}
4417
4418//===----------------------------------------------------------------------===//
4419// LoopNestOp
4420//===----------------------------------------------------------------------===//
4421
4422ParseResult LoopNestOp::parse(OpAsmParser &parser, OperationState &result) {
4423 // Parse an opening `(` followed by induction variables followed by `)`
4426 Type loopVarType;
4428 parser.parseColonType(loopVarType) ||
4429 // Parse loop bounds.
4430 parser.parseEqual() ||
4431 parser.parseOperandList(lbs, ivs.size(), OpAsmParser::Delimiter::Paren) ||
4432 parser.parseKeyword("to") ||
4433 parser.parseOperandList(ubs, ivs.size(), OpAsmParser::Delimiter::Paren))
4434 return failure();
4435
4436 for (auto &iv : ivs)
4437 iv.type = loopVarType;
4438
4439 auto *ctx = parser.getBuilder().getContext();
4440 // Parse "inclusive" flag.
4441 if (succeeded(parser.parseOptionalKeyword("inclusive")))
4442 result.addAttribute("loop_inclusive", UnitAttr::get(ctx));
4443
4444 // Parse step values.
4446 if (parser.parseKeyword("step") ||
4447 parser.parseOperandList(steps, ivs.size(), OpAsmParser::Delimiter::Paren))
4448 return failure();
4449
4450 // Parse collapse
4451 int64_t value = 0;
4452 if (!parser.parseOptionalKeyword("collapse") &&
4453 (parser.parseLParen() || parser.parseInteger(value) ||
4454 parser.parseRParen()))
4455 return failure();
4456 if (value > 1)
4457 result.addAttribute(
4458 "collapse_num_loops",
4459 IntegerAttr::get(parser.getBuilder().getI64Type(), value));
4460
4461 // Parse tiles
4463 auto parseTiles = [&]() -> ParseResult {
4464 int64_t tile;
4465 if (parser.parseInteger(tile))
4466 return failure();
4467 tiles.push_back(tile);
4468 return success();
4469 };
4470
4471 if (!parser.parseOptionalKeyword("tiles") &&
4472 (parser.parseLParen() || parser.parseCommaSeparatedList(parseTiles) ||
4473 parser.parseRParen()))
4474 return failure();
4475
4476 if (tiles.size() > 0)
4477 result.addAttribute("tile_sizes", DenseI64ArrayAttr::get(ctx, tiles));
4478
4479 // Parse the body.
4480 Region *region = result.addRegion();
4481 if (parser.parseRegion(*region, ivs))
4482 return failure();
4483
4484 // Resolve operands.
4485 if (parser.resolveOperands(lbs, loopVarType, result.operands) ||
4486 parser.resolveOperands(ubs, loopVarType, result.operands) ||
4487 parser.resolveOperands(steps, loopVarType, result.operands))
4488 return failure();
4489
4490 // Parse the optional attribute list.
4491 return parser.parseOptionalAttrDict(result.attributes);
4492}
4493
4494void LoopNestOp::print(OpAsmPrinter &p) {
4495 Region &region = getRegion();
4496 auto args = region.getArguments();
4497 p << " (" << args << ") : " << args[0].getType() << " = ("
4498 << getLoopLowerBounds() << ") to (" << getLoopUpperBounds() << ") ";
4499 if (getLoopInclusive())
4500 p << "inclusive ";
4501 p << "step (" << getLoopSteps() << ") ";
4502 if (int64_t numCollapse = getCollapseNumLoops())
4503 if (numCollapse > 1)
4504 p << "collapse(" << numCollapse << ") ";
4505
4506 if (const auto tiles = getTileSizes())
4507 p << "tiles(" << tiles.value() << ") ";
4508
4509 p.printRegion(region, /*printEntryBlockArgs=*/false);
4510}
4511
4512void LoopNestOp::build(OpBuilder &builder, OperationState &state,
4513 const LoopNestOperands &clauses) {
4514 MLIRContext *ctx = builder.getContext();
4515 LoopNestOp::build(builder, state, clauses.collapseNumLoops,
4516 clauses.loopLowerBounds, clauses.loopUpperBounds,
4517 clauses.loopSteps, clauses.loopInclusive,
4518 makeDenseI64ArrayAttr(ctx, clauses.tileSizes));
4519}
4520
4521LogicalResult LoopNestOp::verify() {
4522 if (getLoopLowerBounds().empty())
4523 return emitOpError() << "must represent at least one loop";
4524
4525 if (getLoopLowerBounds().size() != getIVs().size())
4526 return emitOpError() << "number of range arguments and IVs do not match";
4527
4528 for (auto [lb, iv] : llvm::zip_equal(getLoopLowerBounds(), getIVs())) {
4529 if (lb.getType() != iv.getType())
4530 return emitOpError()
4531 << "range argument type does not match corresponding IV type";
4532 }
4533
4534 uint64_t numIVs = getIVs().size();
4535
4536 if (const auto &numCollapse = getCollapseNumLoops())
4537 if (numCollapse > numIVs)
4538 return emitOpError()
4539 << "collapse value is larger than the number of loops";
4540
4541 if (const auto &tiles = getTileSizes())
4542 if (tiles.value().size() > numIVs)
4543 return emitOpError() << "too few canonical loops for tile dimensions";
4544
4545 if (!llvm::dyn_cast_if_present<LoopWrapperInterface>((*this)->getParentOp()))
4546 return emitOpError() << "expects parent op to be a loop wrapper";
4547
4548 return success();
4549}
4550
4551void LoopNestOp::gatherWrappers(
4553 Operation *parent = (*this)->getParentOp();
4554 while (auto wrapper =
4555 llvm::dyn_cast_if_present<LoopWrapperInterface>(parent)) {
4556 wrappers.push_back(wrapper);
4557 parent = parent->getParentOp();
4558 }
4559}
4560
4561//===----------------------------------------------------------------------===//
4562// OpenMP canonical loop handling
4563//===----------------------------------------------------------------------===//
4564
4565std::tuple<NewCliOp, OpOperand *, OpOperand *>
4566mlir::omp ::decodeCli(Value cli) {
4567
4568 // Defining a CLI for a generated loop is optional; if there is none then
4569 // there is no followup-tranformation
4570 if (!cli)
4571 return {{}, nullptr, nullptr};
4572
4573 assert(cli.getType() == CanonicalLoopInfoType::get(cli.getContext()) &&
4574 "Unexpected type of cli");
4575
4576 NewCliOp create = cast<NewCliOp>(cli.getDefiningOp());
4577 OpOperand *gen = nullptr;
4578 OpOperand *cons = nullptr;
4579 for (OpOperand &use : cli.getUses()) {
4580 auto op = cast<LoopTransformationInterface>(use.getOwner());
4581
4582 unsigned opnum = use.getOperandNumber();
4583 if (op.isGeneratee(opnum)) {
4584 assert(!gen && "Each CLI may have at most one def");
4585 gen = &use;
4586 } else if (op.isApplyee(opnum)) {
4587 assert(!cons && "Each CLI may have at most one consumer");
4588 cons = &use;
4589 } else {
4590 llvm_unreachable("Unexpected operand for a CLI");
4591 }
4592 }
4593
4594 return {create, gen, cons};
4595}
4596
4597ClauseProcBindKind
4598mlir::omp::convertProcBindKind(llvm::omp::ProcBindKind kind) {
4599 switch (kind) {
4600 case llvm::omp::ProcBindKind::OMP_PROC_BIND_close:
4601 return ClauseProcBindKind::Close;
4602 case llvm::omp::ProcBindKind::OMP_PROC_BIND_master:
4603 return ClauseProcBindKind::Master;
4604 case llvm::omp::ProcBindKind::OMP_PROC_BIND_primary:
4605 return ClauseProcBindKind::Primary;
4606 case llvm::omp::ProcBindKind::OMP_PROC_BIND_spread:
4607 return ClauseProcBindKind::Spread;
4608 case llvm::omp::ProcBindKind::OMP_PROC_BIND_default:
4609 case llvm::omp::ProcBindKind::OMP_PROC_BIND_unknown:
4610 break;
4611 }
4612 llvm_unreachable("unexpected proc-bind kind");
4613}
4614
4615void NewCliOp::build(::mlir::OpBuilder &odsBuilder,
4616 ::mlir::OperationState &odsState) {
4617 odsState.addTypes(CanonicalLoopInfoType::get(odsBuilder.getContext()));
4618}
4619
4620void NewCliOp::getAsmResultNames(OpAsmSetValueNameFn setNameFn) {
4621 Value result = getResult();
4622 auto [newCli, gen, cons] = decodeCli(result);
4623
4624 // Structured binding `gen` cannot be captured in lambdas before C++20
4625 OpOperand *generator = gen;
4626
4627 // Derive the CLI variable name from its generator:
4628 // * "canonloop" for omp.canonical_loop
4629 // * custom name for loop transformation generatees
4630 // * "cli" as fallback if no generator
4631 // * "_r<idx>" suffix for nested loops, where <idx> is the sequential order
4632 // at that level
4633 // * "_s<idx>" suffix for operations with multiple regions, where <idx> is
4634 // the index of that region
4635 std::string cliName{"cli"};
4636 if (gen) {
4637 cliName =
4639 .Case([&](CanonicalLoopOp op) {
4640 return generateLoopNestingName("canonloop", op);
4641 })
4642 .Case([&](UnrollHeuristicOp op) -> std::string {
4643 llvm_unreachable("heuristic unrolling does not generate a loop");
4644 })
4645 .Case([&](FuseOp op) -> std::string {
4646 unsigned opnum = generator->getOperandNumber();
4647 // The position of the first loop to be fused is the same position
4648 // as the resulting fused loop
4649 if (op.getFirst().has_value() && opnum != op.getFirst().value())
4650 return "canonloop_fuse";
4651 else
4652 return "fused";
4653 })
4654 .Case([&](TileOp op) -> std::string {
4655 auto [generateesFirst, generateesCount] =
4656 op.getGenerateesODSOperandIndexAndLength();
4657 unsigned firstGrid = generateesFirst;
4658 unsigned firstIntratile = generateesFirst + generateesCount / 2;
4659 unsigned end = generateesFirst + generateesCount;
4660 unsigned opnum = generator->getOperandNumber();
4661 // In the OpenMP apply and looprange clauses, indices are 1-based
4662 if (firstGrid <= opnum && opnum < firstIntratile) {
4663 unsigned gridnum = opnum - firstGrid + 1;
4664 return ("grid" + Twine(gridnum)).str();
4665 }
4666 if (firstIntratile <= opnum && opnum < end) {
4667 unsigned intratilenum = opnum - firstIntratile + 1;
4668 return ("intratile" + Twine(intratilenum)).str();
4669 }
4670 llvm_unreachable("Unexpected generatee argument");
4671 })
4672 .DefaultUnreachable("TODO: Custom name for this operation");
4673 }
4674
4675 setNameFn(result, cliName);
4676}
4677
4678LogicalResult NewCliOp::verify() {
4679 Value cli = getResult();
4680
4681 assert(cli.getType() == CanonicalLoopInfoType::get(cli.getContext()) &&
4682 "Unexpected type of cli");
4683
4684 // Check that the CLI is used in at most generator and one consumer
4685 OpOperand *gen = nullptr;
4686 OpOperand *cons = nullptr;
4687 for (mlir::OpOperand &use : cli.getUses()) {
4688 auto op = dyn_cast<mlir::omp::LoopTransformationInterface>(use.getOwner());
4689 if (!op) {
4690 InFlightDiagnostic error =
4691 emitOpError("CLI must only be used by loop transformations");
4692 error.attachNote(use.getOwner()->getLoc()).append("invalid use here:");
4693 return error;
4694 }
4695
4696 unsigned opnum = use.getOperandNumber();
4697 if (op.isGeneratee(opnum)) {
4698 if (gen) {
4699 InFlightDiagnostic error =
4700 emitOpError("CLI must have at most one generator");
4701 error.attachNote(gen->getOwner()->getLoc())
4702 .append("first generator here:");
4703 error.attachNote(use.getOwner()->getLoc())
4704 .append("second generator here:");
4705 return error;
4706 }
4707
4708 gen = &use;
4709 } else if (op.isApplyee(opnum)) {
4710 if (cons) {
4711 InFlightDiagnostic error =
4712 emitOpError("CLI must have at most one consumer");
4713 error.attachNote(cons->getOwner()->getLoc())
4714 .append("first consumer here:")
4715 .appendOp(*cons->getOwner(),
4716 OpPrintingFlags().printGenericOpForm());
4717 error.attachNote(use.getOwner()->getLoc())
4718 .append("second consumer here:")
4719 .appendOp(*use.getOwner(), OpPrintingFlags().printGenericOpForm());
4720 return error;
4721 }
4722
4723 cons = &use;
4724 } else {
4725 llvm_unreachable("Unexpected operand for a CLI");
4726 }
4727 }
4728
4729 // If the CLI is source of a transformation, it must have a generator
4730 if (cons && !gen) {
4731 InFlightDiagnostic error = emitOpError("CLI has no generator");
4732 error.attachNote(cons->getOwner()->getLoc())
4733 .append("see consumer here: ")
4734 .appendOp(*cons->getOwner(), OpPrintingFlags().printGenericOpForm());
4735 return error;
4736 }
4737
4738 return success();
4739}
4740
4741void CanonicalLoopOp::build(OpBuilder &odsBuilder, OperationState &odsState,
4742 Value tripCount) {
4743 odsState.addOperands(tripCount);
4744 odsState.addOperands(Value());
4745 (void)odsState.addRegion();
4746}
4747
4748void CanonicalLoopOp::build(OpBuilder &odsBuilder, OperationState &odsState,
4749 Value tripCount, ::mlir::Value cli) {
4750 odsState.addOperands(tripCount);
4751 odsState.addOperands(cli);
4752 (void)odsState.addRegion();
4753}
4754
4755void CanonicalLoopOp::getAsmBlockNames(OpAsmSetBlockNameFn setNameFn) {
4756 setNameFn(&getRegion().front(), "body_entry");
4757}
4758
4759void CanonicalLoopOp::getAsmBlockArgumentNames(Region &region,
4760 OpAsmSetValueNameFn setNameFn) {
4761 std::string ivName = generateLoopNestingName("iv", *this);
4762 setNameFn(region.getArgument(0), ivName);
4763}
4764
4765void CanonicalLoopOp::print(OpAsmPrinter &p) {
4766 if (getCli())
4767 p << '(' << getCli() << ')';
4768 p << ' ' << getInductionVar() << " : " << getInductionVar().getType()
4769 << " in range(" << getTripCount() << ") ";
4770
4771 p.printRegion(getRegion(), /*printEntryBlockArgs=*/false,
4772 /*printBlockTerminators=*/true);
4773
4774 p.printOptionalAttrDict((*this)->getDiscardableAttrDictionary().getValue());
4775}
4776
4777mlir::ParseResult CanonicalLoopOp::parse(::mlir::OpAsmParser &parser,
4779 CanonicalLoopInfoType cliType =
4780 CanonicalLoopInfoType::get(parser.getContext());
4781
4782 // Parse (optional) omp.cli identifier
4784 SmallVector<mlir::Value, 1> cliOperand;
4785 if (!parser.parseOptionalLParen()) {
4786 if (parser.parseOperand(cli) ||
4787 parser.resolveOperand(cli, cliType, cliOperand) || parser.parseRParen())
4788 return failure();
4789 }
4790
4791 // We derive the type of tripCount from inductionVariable. MLIR requires the
4792 // type of tripCount to be known when calling resolveOperand so we have parse
4793 // the type before processing the inductionVariable.
4794 OpAsmParser::Argument inductionVariable;
4796 if (parser.parseArgument(inductionVariable, /*allowType*/ true) ||
4797 parser.parseKeyword("in") || parser.parseKeyword("range") ||
4798 parser.parseLParen() || parser.parseOperand(tripcount) ||
4799 parser.parseRParen() ||
4800 parser.resolveOperand(tripcount, inductionVariable.type, result.operands))
4801 return failure();
4802
4803 // Parse the loop body.
4804 Region *region = result.addRegion();
4805 if (parser.parseRegion(*region, {inductionVariable}))
4806 return failure();
4807
4808 // We parsed the cli operand forst, but because it is optional, it must be
4809 // last in the operand list.
4810 result.operands.append(cliOperand);
4811
4812 // Parse the optional attribute list.
4813 if (parser.parseOptionalAttrDict(result.attributes))
4814 return failure();
4815
4816 return mlir::success();
4817}
4818
4819LogicalResult CanonicalLoopOp::verify() {
4820 // The region's entry must accept the induction variable
4821 // It can also be empty if just created
4822 if (!getRegion().empty()) {
4823 Region &region = getRegion();
4824 if (region.getNumArguments() != 1)
4825 return emitOpError(
4826 "Canonical loop region must have exactly one argument");
4827
4828 if (getInductionVar().getType() != getTripCount().getType())
4829 return emitOpError(
4830 "Region argument must be the same type as the trip count");
4831 }
4832
4833 return success();
4834}
4835
4836Value CanonicalLoopOp::getInductionVar() { return getRegion().getArgument(0); }
4837
4838std::pair<unsigned, unsigned>
4839CanonicalLoopOp::getApplyeesODSOperandIndexAndLength() {
4840 // No applyees
4841 return {0, 0};
4842}
4843
4844std::pair<unsigned, unsigned>
4845CanonicalLoopOp::getGenerateesODSOperandIndexAndLength() {
4846 return getODSOperandIndexAndLength(odsIndex_cli);
4847}
4848
4849//===----------------------------------------------------------------------===//
4850// UnrollHeuristicOp
4851//===----------------------------------------------------------------------===//
4852
4853void UnrollHeuristicOp::build(::mlir::OpBuilder &odsBuilder,
4854 ::mlir::OperationState &odsState,
4855 ::mlir::Value cli) {
4856 odsState.addOperands(cli);
4857}
4858
4859void UnrollHeuristicOp::print(OpAsmPrinter &p) {
4860 p << '(' << getApplyee() << ')';
4861
4862 p.printOptionalAttrDict((*this)->getDiscardableAttrDictionary().getValue());
4863}
4864
4865mlir::ParseResult UnrollHeuristicOp::parse(::mlir::OpAsmParser &parser,
4867 auto cliType = CanonicalLoopInfoType::get(parser.getContext());
4868
4869 if (parser.parseLParen())
4870 return failure();
4871
4873 if (parser.parseOperand(applyee) ||
4874 parser.resolveOperand(applyee, cliType, result.operands))
4875 return failure();
4876
4877 if (parser.parseRParen())
4878 return failure();
4879
4880 // Optional output loop (full unrolling has none)
4881 if (!parser.parseOptionalArrow()) {
4882 if (parser.parseLParen() || parser.parseRParen())
4883 return failure();
4884 }
4885
4886 // Parse the optional attribute list.
4887 if (parser.parseOptionalAttrDict(result.attributes))
4888 return failure();
4889
4890 return mlir::success();
4891}
4892
4893std::pair<unsigned, unsigned>
4894UnrollHeuristicOp ::getApplyeesODSOperandIndexAndLength() {
4895 return getODSOperandIndexAndLength(odsIndex_applyee);
4896}
4897
4898std::pair<unsigned, unsigned>
4899UnrollHeuristicOp::getGenerateesODSOperandIndexAndLength() {
4900 return {0, 0};
4901}
4902
4903//===----------------------------------------------------------------------===//
4904// UnrollFullOp
4905//===----------------------------------------------------------------------===//
4906
4907void UnrollFullOp::build(::mlir::OpBuilder &odsBuilder,
4908 ::mlir::OperationState &odsState, ::mlir::Value cli) {
4909 odsState.addOperands(cli);
4910}
4911
4912void UnrollFullOp::print(OpAsmPrinter &p) {
4913 p << '(' << getApplyee() << ')';
4914
4915 p.printOptionalAttrDict((*this)->getDiscardableAttrDictionary().getValue());
4916}
4917
4918mlir::ParseResult UnrollFullOp::parse(::mlir::OpAsmParser &parser,
4920 auto cliType = CanonicalLoopInfoType::get(parser.getContext());
4921
4922 if (parser.parseLParen())
4923 return failure();
4924
4926 if (parser.parseOperand(applyee) ||
4927 parser.resolveOperand(applyee, cliType, result.operands))
4928 return failure();
4929
4930 if (parser.parseRParen())
4931 return failure();
4932
4933 // Optional output loop; full unrolling has none.
4934 if (!parser.parseOptionalArrow()) {
4935 if (parser.parseLParen() || parser.parseRParen())
4936 return failure();
4937 }
4938
4939 // Parse the optional attribute list.
4940 if (parser.parseOptionalAttrDict(result.attributes))
4941 return failure();
4942
4943 return mlir::success();
4944}
4945
4946std::pair<unsigned, unsigned>
4947UnrollFullOp::getApplyeesODSOperandIndexAndLength() {
4948 return getODSOperandIndexAndLength(odsIndex_applyee);
4949}
4950
4951std::pair<unsigned, unsigned>
4952UnrollFullOp::getGenerateesODSOperandIndexAndLength() {
4953 return {0, 0};
4954}
4955
4956LogicalResult UnrollFullOp::verify() {
4957 auto [create, gen, cons] = decodeCli(getApplyee());
4958 if (!gen)
4959 return emitOpError() << "applyee CLI has no generator";
4960
4961 // Full unrolling leaves no loop, so the trip count must be constant. Only
4962 // omp.canonical_loop states one.
4963 if (auto loop = dyn_cast<CanonicalLoopOp>(gen->getOwner())) {
4964 if (!matchPattern(loop.getTripCount(), m_Constant()))
4965 return emitOpError() << "applyee loop must have a constant trip count";
4966 }
4967
4968 return success();
4969}
4970
4971//===----------------------------------------------------------------------===//
4972// UnrollPartialOp
4973//===----------------------------------------------------------------------===//
4974
4975void UnrollPartialOp::build(::mlir::OpBuilder &odsBuilder,
4977 uint64_t unrollFactor) {
4978 odsState.addOperands(cli);
4979 Properties &props = odsState.getOrAddProperties<Properties>();
4980 props.unroll_factor = odsBuilder.getI64IntegerAttr(unrollFactor);
4981}
4982
4983void UnrollPartialOp::print(OpAsmPrinter &p) {
4984 p << '(' << getApplyee() << ')';
4985
4986 SmallVector<NamedAttribute> attrs((*this)->getDiscardableAttrs());
4987 attrs.emplace_back(getUnrollFactorAttrName(), getUnrollFactorAttr());
4988 llvm::sort(attrs);
4989 p.printOptionalAttrDict(attrs);
4990}
4991
4992mlir::ParseResult UnrollPartialOp::parse(::mlir::OpAsmParser &parser,
4994 auto cliType = CanonicalLoopInfoType::get(parser.getContext());
4995
4996 if (parser.parseLParen())
4997 return failure();
4998
5000 if (parser.parseOperand(applyee) ||
5001 parser.resolveOperand(applyee, cliType, result.operands))
5002 return failure();
5003
5004 if (parser.parseRParen())
5005 return failure();
5006
5007 // The unroll factor is carried by the `unroll_factor` attribute.
5008 if (parser.parseOptionalAttrDict(result.attributes))
5009 return failure();
5010
5011 return mlir::success();
5012}
5013
5014std::pair<unsigned, unsigned>
5015UnrollPartialOp::getApplyeesODSOperandIndexAndLength() {
5016 return getODSOperandIndexAndLength(odsIndex_applyee);
5017}
5018
5019std::pair<unsigned, unsigned>
5020UnrollPartialOp::getGenerateesODSOperandIndexAndLength() {
5021 return {0, 0};
5022}
5023
5024//===----------------------------------------------------------------------===//
5025// TileOp
5026//===----------------------------------------------------------------------===//
5027
5028static void printLoopTransformClis(OpAsmPrinter &p, TileOp op,
5029 OperandRange generatees,
5030 OperandRange applyees) {
5031 if (!generatees.empty())
5032 p << '(' << llvm::interleaved(generatees) << ')';
5033
5034 if (!applyees.empty())
5035 p << " <- (" << llvm::interleaved(applyees) << ')';
5036}
5037
5038static ParseResult parseLoopTransformClis(
5039 OpAsmParser &parser,
5042 if (parser.parseOptionalLess()) {
5043 // Syntax 1: generatees present
5044
5045 if (parser.parseOperandList(generateesOperands,
5047 return failure();
5048
5049 if (parser.parseLess())
5050 return failure();
5051 } else {
5052 // Syntax 2: generatees omitted
5053 }
5054
5055 // Parse `<-` (`<` has already been parsed)
5056 if (parser.parseMinus())
5057 return failure();
5058
5059 if (parser.parseOperandList(applyeesOperands,
5061 return failure();
5062
5063 return success();
5064}
5065
5066/// Check properties of the loop nest consisting of the transformation's
5067/// applyees:
5068/// 1. They are nested inside each other
5069/// 2. They are perfectly nested
5070/// (no code with side-effects in-between the loops)
5071/// 3. They are rectangular
5072/// (loop bounds are invariant in respect to the outer loops)
5073///
5074/// TODO: Generalize for LoopTransformationInterface.
5075static LogicalResult checkApplyeesNesting(TileOp op) {
5076 // Collect the loops from the nest
5077 bool isOnlyCanonLoops = true;
5079 for (Value applyee : op.getApplyees()) {
5080 auto [create, gen, cons] = decodeCli(applyee);
5081
5082 if (!gen)
5083 return op.emitOpError() << "applyee CLI has no generator";
5084
5085 auto loop = dyn_cast_or_null<CanonicalLoopOp>(gen->getOwner());
5086 canonLoops.push_back(loop);
5087 if (!loop)
5088 isOnlyCanonLoops = false;
5089 }
5090
5091 // FIXME: We currently can only verify non-rectangularity and perfect nest of
5092 // omp.canonical_loop.
5093 if (!isOnlyCanonLoops)
5094 return success();
5095
5096 DenseSet<Value> parentIVs;
5097 for (auto i : llvm::seq<int>(1, canonLoops.size())) {
5098 auto parentLoop = canonLoops[i - 1];
5099 auto loop = canonLoops[i];
5100
5101 if (parentLoop.getOperation() != loop.getOperation()->getParentOp())
5102 return op.emitOpError()
5103 << "tiled loop nest must be nested within each other";
5104
5105 parentIVs.insert(parentLoop.getInductionVar());
5106
5107 // Canonical loop must be perfectly nested, i.e. the body of the parent must
5108 // only contain the omp.canonical_loop of the nested loops, and
5109 // omp.terminator
5110 bool isPerfectlyNested = [&]() {
5111 auto &parentBody = parentLoop.getRegion();
5112 if (!parentBody.hasOneBlock())
5113 return false;
5114 auto &parentBlock = parentBody.getBlocks().front();
5115
5116 auto nestedLoopIt = parentBlock.begin();
5117 if (nestedLoopIt == parentBlock.end() ||
5118 (&*nestedLoopIt != loop.getOperation()))
5119 return false;
5120
5121 auto termIt = std::next(nestedLoopIt);
5122 if (termIt == parentBlock.end() || !isa<TerminatorOp>(termIt))
5123 return false;
5124
5125 if (std::next(termIt) != parentBlock.end())
5126 return false;
5127
5128 return true;
5129 }();
5130 if (!isPerfectlyNested)
5131 return op.emitOpError() << "tiled loop nest must be perfectly nested";
5132
5133 if (parentIVs.contains(loop.getTripCount()))
5134 return op.emitOpError() << "tiled loop nest must be rectangular";
5135 }
5136
5137 // TODO: The tile sizes must be computed before the loop, but checking this
5138 // requires dominance analysis. For instance:
5139 //
5140 // %canonloop = omp.new_cli
5141 // omp.canonical_loop(%canonloop) %iv : i32 in range(%tc) {
5142 // // write to %x
5143 // omp.terminator
5144 // }
5145 // %ts = llvm.load %x
5146 // omp.tile <- (%canonloop) sizes(%ts : i32)
5147
5148 return success();
5149}
5150
5151LogicalResult TileOp::verify() {
5152 if (getApplyees().empty())
5153 return emitOpError() << "must apply to at least one loop";
5154
5155 if (getSizes().size() != getApplyees().size())
5156 return emitOpError() << "there must be one tile size for each applyee";
5157
5158 if (!getGeneratees().empty() &&
5159 2 * getSizes().size() != getGeneratees().size())
5160 return emitOpError()
5161 << "expecting two times the number of generatees than applyees";
5162
5163 return checkApplyeesNesting(*this);
5164}
5165
5166std::pair<unsigned, unsigned> TileOp ::getApplyeesODSOperandIndexAndLength() {
5167 return getODSOperandIndexAndLength(odsIndex_applyees);
5168}
5169
5170std::pair<unsigned, unsigned> TileOp::getGenerateesODSOperandIndexAndLength() {
5171 return getODSOperandIndexAndLength(odsIndex_generatees);
5172}
5173
5174//===----------------------------------------------------------------------===//
5175// FuseOp
5176//===----------------------------------------------------------------------===//
5177
5178static void printLoopTransformClis(OpAsmPrinter &p, FuseOp op,
5179 OperandRange generatees,
5180 OperandRange applyees) {
5181 if (!generatees.empty())
5182 p << '(' << llvm::interleaved(generatees) << ')';
5183
5184 if (!applyees.empty())
5185 p << " <- (" << llvm::interleaved(applyees) << ')';
5186}
5187
5188LogicalResult FuseOp::verify() {
5189 if (getApplyees().size() < 2)
5190 return emitOpError() << "must apply to at least two loops";
5191
5192 if (getFirst().has_value() && getCount().has_value()) {
5193 int64_t first = getFirst().value();
5194 int64_t count = getCount().value();
5195 if ((unsigned)(first + count - 1) > getApplyees().size())
5196 return emitOpError() << "the numbers of applyees must be at least first "
5197 "minus one plus count attributes";
5198 if (!getGeneratees().empty() &&
5199 getGeneratees().size() != getApplyees().size() + 1 - count)
5200 return emitOpError() << "the number of generatees must be the number of "
5201 "aplyees plus one minus count";
5202
5203 } else {
5204 if (!getGeneratees().empty() && getGeneratees().size() != 1)
5205 return emitOpError()
5206 << "in a complete fuse the number of generatees must be exactly 1";
5207 }
5208 for (auto &&applyee : getApplyees()) {
5209 auto [create, gen, cons] = decodeCli(applyee);
5210
5211 if (!gen)
5212 return emitOpError() << "applyee CLI has no generator";
5213 auto loop = dyn_cast_or_null<CanonicalLoopOp>(gen->getOwner());
5214 if (!loop)
5215 return emitOpError()
5216 << "currently only supports omp.canonical_loop as applyee";
5217 }
5218 return success();
5219}
5220std::pair<unsigned, unsigned> FuseOp::getApplyeesODSOperandIndexAndLength() {
5221 return getODSOperandIndexAndLength(odsIndex_applyees);
5222}
5223
5224std::pair<unsigned, unsigned> FuseOp::getGenerateesODSOperandIndexAndLength() {
5225 return getODSOperandIndexAndLength(odsIndex_generatees);
5226}
5227
5228//===----------------------------------------------------------------------===//
5229// Critical construct (2.17.1)
5230//===----------------------------------------------------------------------===//
5231
5232void CriticalDeclareOp::build(OpBuilder &builder, OperationState &state,
5233 const CriticalDeclareOperands &clauses) {
5234 CriticalDeclareOp::build(builder, state, clauses.symName,
5235 clauses.symVisibility, clauses.hint);
5236}
5237
5238LogicalResult CriticalDeclareOp::verify() {
5239 return verifySynchronizationHint(*this, getHint());
5240}
5241
5242LogicalResult CriticalOp::verify() {
5243 SymbolRefAttr currentName = getNameAttr();
5244
5245 CriticalOp parentCritical = (*this)->getParentOfType<CriticalOp>();
5246
5247 while (parentCritical) {
5248 SymbolRefAttr parentName = parentCritical.getNameAttr();
5249
5250 if (currentName == parentName) {
5251 if (currentName) {
5252 return emitOpError() << "cannot be nested inside another omp.critical "
5253 "region with the same name ("
5254 << currentName << ")";
5255 } else {
5256 return emitOpError() << "cannot be nested inside another unnamed "
5257 "omp.critical region";
5258 }
5259 }
5260
5261 parentCritical = parentCritical->getParentOfType<CriticalOp>();
5262 }
5263
5264 return success();
5265}
5266
5267LogicalResult CriticalOp::verifySymbolUses(SymbolTableCollection &symbolTable) {
5268 if (getNameAttr()) {
5269 SymbolRefAttr symbolRef = getNameAttr();
5270 auto decl = symbolTable.lookupNearestSymbolFrom<CriticalDeclareOp>(
5271 *this, symbolRef);
5272 if (!decl) {
5273 return emitOpError() << "expected symbol reference " << symbolRef
5274 << " to point to a critical declaration";
5275 }
5276 }
5277
5278 return success();
5279}
5280
5281//===----------------------------------------------------------------------===//
5282// Spec 5.1: Error directive (2.5.4)
5283//===----------------------------------------------------------------------===//
5284
5285LogicalResult ErrorOp::verify() {
5286 if (getMessage() && getMessageExpr())
5287 return emitOpError() << "the message must be provided either as a constant "
5288 "`message` attribute or as a `message_expr` "
5289 "operand, but not both";
5290 return success();
5291}
5292
5293//===----------------------------------------------------------------------===//
5294// Ordered construct
5295//===----------------------------------------------------------------------===//
5296
5297static LogicalResult verifyOrderedParent(Operation &op) {
5298 bool hasRegion = op.getNumRegions() > 0;
5299 auto loopOp = op.getParentOfType<LoopNestOp>();
5300 if (!loopOp) {
5301 if (hasRegion)
5302 return success();
5303
5304 // TODO: Consider if this needs to be the case only for the standalone
5305 // variant of the ordered construct.
5306 return op.emitOpError() << "must be nested inside of a loop";
5307 }
5308
5309 Operation *wrapper = loopOp->getParentOp();
5310 if (auto wsloopOp = dyn_cast<WsloopOp>(wrapper)) {
5311 IntegerAttr orderedAttr = wsloopOp.getOrderedAttr();
5312 if (!orderedAttr)
5313 return op.emitOpError() << "the enclosing worksharing-loop region must "
5314 "have an ordered clause";
5315
5316 if (hasRegion && orderedAttr.getInt() != 0)
5317 return op.emitOpError() << "the enclosing loop's ordered clause must not "
5318 "have a parameter present";
5319
5320 if (!hasRegion && orderedAttr.getInt() == 0)
5321 return op.emitOpError() << "the enclosing loop's ordered clause must "
5322 "have a parameter present";
5323 } else if (!isa<SimdOp>(wrapper)) {
5324 return op.emitOpError() << "must be nested inside of a worksharing, simd "
5325 "or worksharing simd loop";
5326 }
5327 return success();
5328}
5329
5330void OrderedOp::build(OpBuilder &builder, OperationState &state,
5331 const OrderedOperands &clauses) {
5332 OrderedOp::build(builder, state, clauses.doacrossDependType,
5333 clauses.doacrossNumLoops, clauses.doacrossDependVars);
5334}
5335
5336LogicalResult OrderedOp::verify() {
5337 if (failed(verifyOrderedParent(**this)))
5338 return failure();
5339
5340 auto wrapper = (*this)->getParentOfType<WsloopOp>();
5341 if (!wrapper || *wrapper.getOrdered() != *getDoacrossNumLoops())
5342 return emitOpError() << "number of variables in depend clause does not "
5343 << "match number of iteration variables in the "
5344 << "doacross loop";
5345
5346 return success();
5347}
5348
5349void OrderedRegionOp::build(OpBuilder &builder, OperationState &state,
5350 const OrderedRegionOperands &clauses) {
5351 OrderedRegionOp::build(builder, state, clauses.parLevelSimd);
5352}
5353
5354LogicalResult OrderedRegionOp::verify() { return verifyOrderedParent(**this); }
5355
5356//===----------------------------------------------------------------------===//
5357// TaskwaitOp
5358//===----------------------------------------------------------------------===//
5359
5360void TaskwaitOp::build(OpBuilder &builder, OperationState &state,
5361 const TaskwaitOperands &clauses) {
5362 // TODO Store clauses in op: depend_iterated_kinds, depend_iterated, nowait.
5363 MLIRContext *ctx = builder.getContext();
5364 TaskwaitOp::build(
5365 builder, state,
5366 /*depend_kinds=*/makeArrayAttr(ctx, clauses.dependKinds),
5367 /*depend_vars=*/clauses.dependVars,
5368 /*depend_iterated_kinds=*/makeArrayAttr(ctx, clauses.dependIteratedKinds),
5369 /*depend_iterated=*/ValueRange(clauses.dependIterated),
5370 /*nowait=*/clauses.nowait);
5371}
5372
5373//===----------------------------------------------------------------------===//
5374// Verifier for AtomicReadOp
5375//===----------------------------------------------------------------------===//
5376
5377LogicalResult AtomicReadOp::verify() {
5378 if (verifyCommon().failed())
5379 return mlir::failure();
5380
5381 int64_t version = 50;
5382 if (auto moduleOp = getOperation()->getParentOfType<ModuleOp>())
5383 if (Attribute verAttr = moduleOp->getDiscardableAttr("omp.version"))
5384 version = llvm::cast<VersionAttr>(verAttr).getVersion();
5385
5386 if (auto mo = getMemoryOrder()) {
5387 if (*mo == ClauseMemoryOrderKind::Release) {
5388 return emitError("memory-order must not be release for atomic reads");
5389 }
5390 if (*mo == ClauseMemoryOrderKind::Acq_rel) {
5391 // acq_rel is prohibited on read only in OpenMP 5.0; allowed in 5.1+.
5392 if (version < 51)
5393 return emitError("memory-order must not be acq_rel for atomic reads");
5394 }
5395 }
5396 return verifySynchronizationHint(*this, getHint());
5397}
5398
5399//===----------------------------------------------------------------------===//
5400// Verifier for AtomicWriteOp
5401//===----------------------------------------------------------------------===//
5402
5403LogicalResult AtomicWriteOp::verify() {
5404 if (verifyCommon().failed())
5405 return mlir::failure();
5406
5407 int64_t version = 50;
5408 if (auto moduleOp = getOperation()->getParentOfType<ModuleOp>())
5409 if (Attribute verAttr = moduleOp->getDiscardableAttr("omp.version"))
5410 version = llvm::cast<VersionAttr>(verAttr).getVersion();
5411
5412 if (auto mo = getMemoryOrder()) {
5413 if (*mo == ClauseMemoryOrderKind::Acquire) {
5414 return emitError("memory-order must not be acquire for atomic writes");
5415 }
5416 if (*mo == ClauseMemoryOrderKind::Acq_rel) {
5417 // acq_rel is prohibited on write only in OpenMP 5.0; allowed in 5.1+.
5418 if (version < 51)
5419 return emitError("memory-order must not be acq_rel for atomic writes");
5420 }
5421 }
5422 return verifySynchronizationHint(*this, getHint());
5423}
5424
5425//===----------------------------------------------------------------------===//
5426// Verifier for AtomicUpdateOp
5427//===----------------------------------------------------------------------===//
5428
5429LogicalResult AtomicUpdateOp::canonicalize(AtomicUpdateOp op,
5430 PatternRewriter &rewriter) {
5431 if (op.isNoOp()) {
5432 rewriter.eraseOp(op);
5433 return success();
5434 }
5435 if (Value writeVal = op.getWriteOpVal()) {
5436 rewriter.replaceOpWithNewOp<AtomicWriteOp>(
5437 op, op.getX(), writeVal, op.getHintAttr(), op.getMemoryOrderAttr());
5438 return success();
5439 }
5440 return failure();
5441}
5442
5443LogicalResult AtomicUpdateOp::verify() {
5444 if (verifyCommon().failed())
5445 return mlir::failure();
5446
5447 int64_t version = 50;
5448 if (auto moduleOp = getOperation()->getParentOfType<ModuleOp>())
5449 if (Attribute verAttr = moduleOp->getDiscardableAttr("omp.version"))
5450 version = llvm::cast<VersionAttr>(verAttr).getVersion();
5451
5452 if (auto mo = getMemoryOrder()) {
5453 if (*mo == ClauseMemoryOrderKind::Acq_rel ||
5454 *mo == ClauseMemoryOrderKind::Acquire) {
5455 // This restriction applies only to OpenMP 5.0; removed in 5.1.
5456 if (version < 51)
5457 return emitError(
5458 "memory-order must not be acq_rel or acquire for atomic updates");
5459 }
5460 }
5461
5462 return verifySynchronizationHint(*this, getHint());
5463}
5464
5465LogicalResult AtomicUpdateOp::verifyRegions() { return verifyRegionsCommon(); }
5466
5467//===----------------------------------------------------------------------===//
5468// Verifier for AtomicCaptureOp
5469//===----------------------------------------------------------------------===//
5470
5471AtomicReadOp AtomicCaptureOp::getAtomicReadOp() {
5472 if (auto op = dyn_cast<AtomicReadOp>(getFirstOp()))
5473 return op;
5474 return dyn_cast<AtomicReadOp>(getSecondOp());
5475}
5476
5477AtomicWriteOp AtomicCaptureOp::getAtomicWriteOp() {
5478 if (auto op = dyn_cast<AtomicWriteOp>(getFirstOp()))
5479 return op;
5480 return dyn_cast<AtomicWriteOp>(getSecondOp());
5481}
5482
5483AtomicUpdateOp AtomicCaptureOp::getAtomicUpdateOp() {
5484 if (auto op = dyn_cast<AtomicUpdateOp>(getFirstOp()))
5485 return op;
5486 return dyn_cast<AtomicUpdateOp>(getSecondOp());
5487}
5488
5489AtomicCompareOp AtomicCaptureOp::getAtomicCompareOp() {
5490 if (auto op = dyn_cast<AtomicCompareOp>(getFirstOp()))
5491 return op;
5492 return dyn_cast<AtomicCompareOp>(getSecondOp());
5493}
5494
5495LogicalResult AtomicCaptureOp::verify() {
5496 return verifySynchronizationHint(*this, getHint());
5497}
5498
5499LogicalResult AtomicCaptureOp::verifyRegions() {
5500 if (verifyRegionsCommon().failed())
5501 return mlir::failure();
5502
5503 if (getFirstOp()->getInherentAttr("hint").value_or(Attribute{}) ||
5504 getSecondOp()->getInherentAttr("hint").value_or(Attribute{}))
5505 return emitOpError(
5506 "operations inside capture region must not have hint clause");
5507
5508 if (getFirstOp()->getInherentAttr("memory_order").value_or(Attribute{}) ||
5509 getSecondOp()->getInherentAttr("memory_order").value_or(Attribute{}))
5510 return emitOpError(
5511 "operations inside capture region must not have memory_order clause");
5512 return success();
5513}
5514
5515//===----------------------------------------------------------------------===//
5516// AtomicCompareOp
5517//===----------------------------------------------------------------------===//
5518
5519LogicalResult AtomicCompareOp::verify() {
5520 if (verifyCommon().failed())
5521 return mlir::failure();
5522 // OpenMP 5.2 [15.8.3]: the fail clause argument must be one of seq_cst,
5523 // acquire or relaxed ('release' and 'acq_rel' are not valid failure
5524 // orderings and map to invalid cmpxchg failure orderings).
5525 if (auto failOrder = getFailMemoryOrder()) {
5526 if (*failOrder != ClauseMemoryOrderKind::Seq_cst &&
5527 *failOrder != ClauseMemoryOrderKind::Acquire &&
5528 *failOrder != ClauseMemoryOrderKind::Relaxed)
5529 return emitOpError(
5530 "fail_memory_order must be 'seq_cst', 'acquire' or 'relaxed'");
5531 }
5532 return verifySynchronizationHint(*this, getHint());
5533}
5534
5535LogicalResult AtomicCompareOp::verifyRegions() {
5536 if (verifyRegionsCommon().failed())
5537 return mlir::failure();
5538
5539 if (verifyOperator().failed())
5540 return mlir::failure();
5541
5542 Block &block = getRegion().front();
5543
5544 Operation *terminator = block.getTerminator();
5545 if (!terminator || !isa<YieldOp>(terminator))
5546 return emitOpError("region must be terminated with omp.yield");
5547
5548 return success();
5549}
5550
5551//===----------------------------------------------------------------------===//
5552// CancelOp
5553//===----------------------------------------------------------------------===//
5554
5555void CancelOp::build(OpBuilder &builder, OperationState &state,
5556 const CancelOperands &clauses) {
5557 CancelOp::build(builder, state, clauses.cancelDirective, clauses.ifExpr);
5558}
5559
5561 Operation *parent = thisOp->getParentOp();
5562 while (parent) {
5563 if (parent->getDialect() == thisOp->getDialect())
5564 return parent;
5565 parent = parent->getParentOp();
5566 }
5567 return nullptr;
5568}
5569
5570LogicalResult CancelOp::verify() {
5571 ClauseCancellationConstructType cct = getCancelDirective();
5572 // The next OpenMP operation in the chain of parents
5573 Operation *structuralParent = getParentInSameDialect((*this).getOperation());
5574 if (!structuralParent)
5575 return emitOpError() << "Orphaned cancel construct";
5576
5577 if ((cct == ClauseCancellationConstructType::Parallel) &&
5578 !mlir::isa<ParallelOp>(structuralParent)) {
5579 return emitOpError() << "cancel parallel must appear "
5580 << "inside a parallel region";
5581 }
5582 if (cct == ClauseCancellationConstructType::Loop) {
5583 // structural parent will be omp.loop_nest, directly nested inside
5584 // omp.wsloop
5585 auto wsloopOp = mlir::dyn_cast<WsloopOp>(structuralParent->getParentOp());
5586
5587 if (!wsloopOp) {
5588 return emitOpError()
5589 << "cancel loop must appear inside a worksharing-loop region";
5590 }
5591 if (wsloopOp.getNowaitAttr()) {
5592 return emitError() << "A worksharing construct that is canceled "
5593 << "must not have a nowait clause";
5594 }
5595 if (wsloopOp.getOrderedAttr()) {
5596 return emitError() << "A worksharing construct that is canceled "
5597 << "must not have an ordered clause";
5598 }
5599
5600 } else if (cct == ClauseCancellationConstructType::Sections) {
5601 // structural parent will be an omp.section, directly nested inside
5602 // omp.sections
5603 auto sectionsOp =
5604 mlir::dyn_cast<SectionsOp>(structuralParent->getParentOp());
5605 if (!sectionsOp) {
5606 return emitOpError() << "cancel sections must appear "
5607 << "inside a sections region";
5608 }
5609 if (sectionsOp.getNowait()) {
5610 return emitError() << "A sections construct that is canceled "
5611 << "must not have a nowait clause";
5612 }
5613 }
5614 if ((cct == ClauseCancellationConstructType::Taskgroup) &&
5615 (!mlir::isa<omp::TaskOp>(structuralParent) &&
5616 !mlir::isa<omp::TaskloopWrapperOp>(structuralParent->getParentOp()))) {
5617 return emitOpError() << "cancel taskgroup must appear "
5618 << "inside a task region";
5619 }
5620 return success();
5621}
5622
5623//===----------------------------------------------------------------------===//
5624// CancellationPointOp
5625//===----------------------------------------------------------------------===//
5626
5627void CancellationPointOp::build(OpBuilder &builder, OperationState &state,
5628 const CancellationPointOperands &clauses) {
5629 CancellationPointOp::build(builder, state, clauses.cancelDirective);
5630}
5631
5632LogicalResult CancellationPointOp::verify() {
5633 ClauseCancellationConstructType cct = getCancelDirective();
5634 // The next OpenMP operation in the chain of parents
5635 Operation *structuralParent = getParentInSameDialect((*this).getOperation());
5636 if (!structuralParent)
5637 return emitOpError() << "Orphaned cancellation point";
5638
5639 if ((cct == ClauseCancellationConstructType::Parallel) &&
5640 !mlir::isa<ParallelOp>(structuralParent)) {
5641 return emitOpError() << "cancellation point parallel must appear "
5642 << "inside a parallel region";
5643 }
5644 // Strucutal parent here will be an omp.loop_nest. Get the parent of that to
5645 // find the wsloop
5646 if ((cct == ClauseCancellationConstructType::Loop) &&
5647 !mlir::isa<WsloopOp>(structuralParent->getParentOp())) {
5648 return emitOpError() << "cancellation point loop must appear "
5649 << "inside a worksharing-loop region";
5650 }
5651 if ((cct == ClauseCancellationConstructType::Sections) &&
5652 !mlir::isa<omp::SectionOp>(structuralParent)) {
5653 return emitOpError() << "cancellation point sections must appear "
5654 << "inside a sections region";
5655 }
5656 if ((cct == ClauseCancellationConstructType::Taskgroup) &&
5657 (!mlir::isa<omp::TaskOp>(structuralParent) &&
5658 !mlir::isa<omp::TaskloopWrapperOp>(structuralParent->getParentOp()))) {
5659 return emitOpError() << "cancellation point taskgroup must appear "
5660 << "inside a task region";
5661 }
5662 return success();
5663}
5664
5665//===----------------------------------------------------------------------===//
5666// MapBoundsOp
5667//===----------------------------------------------------------------------===//
5668
5669LogicalResult MapBoundsOp::verify() {
5670 auto extent = getExtent();
5671 auto upperbound = getUpperBound();
5672 if (!extent && !upperbound)
5673 return emitError("expected extent or upperbound.");
5674 return success();
5675}
5676
5677void PrivateClauseOp::build(OpBuilder &odsBuilder, OperationState &odsState,
5678 TypeRange /*result_types*/, StringAttr symName,
5679 TypeAttr type) {
5680 PrivateClauseOp::build(
5681 odsBuilder, odsState, symName, /*sym_visibility=*/nullptr, type,
5682 DataSharingClauseTypeAttr::get(odsBuilder.getContext(),
5683 DataSharingClauseType::Private));
5684}
5685
5686LogicalResult PrivateClauseOp::verifyRegions() {
5687 Type argType = getArgType();
5688 auto verifyTerminator = [&](Operation *terminator, bool yieldsValue,
5689 bool yieldsAnyType) -> LogicalResult {
5690 if (!terminator->getBlock()->getSuccessors().empty())
5691 return success();
5692
5693 if (!llvm::isa<YieldOp>(terminator))
5694 return mlir::emitError(terminator->getLoc())
5695 << "expected exit block terminator to be an `omp.yield` op.";
5696
5697 YieldOp yieldOp = llvm::cast<YieldOp>(terminator);
5698 TypeRange yieldedTypes = yieldOp.getResults().getTypes();
5699
5700 if (!yieldsValue) {
5701 if (yieldedTypes.empty())
5702 return success();
5703
5704 return mlir::emitError(terminator->getLoc())
5705 << "Did not expect any values to be yielded.";
5706 }
5707
5708 if (yieldedTypes.size() == 1 &&
5709 (yieldsAnyType || yieldedTypes.front() == argType))
5710 return success();
5711
5712 auto error = mlir::emitError(yieldOp.getLoc())
5713 << "Invalid yielded value. Expected ";
5714 if (yieldsAnyType)
5715 error << "a single value";
5716 else
5717 error << "type: " << argType;
5718 error << ", got: ";
5719
5720 if (yieldedTypes.empty())
5721 error << "None";
5722 else
5723 error << yieldedTypes;
5724
5725 return error;
5726 };
5727
5728 auto verifyRegion = [&](Region &region, unsigned expectedNumArgs,
5729 StringRef regionName, bool yieldsValue,
5730 bool yieldsAnyType = false) -> LogicalResult {
5731 assert(!region.empty());
5732
5733 if (region.getNumArguments() != expectedNumArgs)
5734 return mlir::emitError(region.getLoc())
5735 << "`" << regionName << "`: " << "expected " << expectedNumArgs
5736 << " region arguments, got: " << region.getNumArguments();
5737
5738 for (Block &block : region) {
5739 // MLIR will verify the absence of the terminator for us.
5740 if (!block.mightHaveTerminator())
5741 continue;
5742
5743 if (failed(verifyTerminator(block.getTerminator(), yieldsValue,
5744 yieldsAnyType)))
5745 return failure();
5746 }
5747
5748 return success();
5749 };
5750
5751 // Ensure all of the region arguments have the same type
5752 for (Region *region : getRegions())
5753 for (Type ty : region->getArgumentTypes())
5754 if (ty != argType)
5755 return emitError() << "Region argument type mismatch: got " << ty
5756 << " expected " << argType << ".";
5757
5758 mlir::Region &initRegion = getInitRegion();
5759 if (!initRegion.empty() &&
5760 failed(verifyRegion(getInitRegion(), /*expectedNumArgs=*/2, "init",
5761 /*yieldsValue=*/true)))
5762 return failure();
5763
5764 DataSharingClauseType dsType = getDataSharingType();
5765
5766 if (dsType == DataSharingClauseType::Private && !getCopyRegion().empty())
5767 return emitError("`private` clauses do not require a `copy` region.");
5768
5769 if (dsType == DataSharingClauseType::FirstPrivate && getCopyRegion().empty())
5770 return emitError(
5771 "`firstprivate` clauses require at least a `copy` region.");
5772
5773 if (dsType == DataSharingClauseType::FirstPrivate &&
5774 failed(verifyRegion(getCopyRegion(), /*expectedNumArgs=*/2, "copy",
5775 /*yieldsValue=*/true)))
5776 return failure();
5777
5778 if (dsType == DataSharingClauseType::Linear && !getCopyRegion().empty())
5779 return emitError("`linear` clauses do not require a `copy` region.");
5780
5781 if (!getDataAddressRegion().empty()) {
5782 if (dsType != DataSharingClauseType::Linear)
5783 return emitError(
5784 "only `linear` clauses can have a `data_address` region.");
5785 if (failed(verifyRegion(getDataAddressRegion(), /*expectedNumArgs=*/1,
5786 "data_address", /*yieldsValue=*/true,
5787 /*yieldsAnyType=*/true)))
5788 return failure();
5789 }
5790
5791 if (!getDeallocRegion().empty() &&
5792 failed(verifyRegion(getDeallocRegion(), /*expectedNumArgs=*/1, "dealloc",
5793 /*yieldsValue=*/false)))
5794 return failure();
5795
5796 return success();
5797}
5798
5799//===----------------------------------------------------------------------===//
5800// Spec 5.2: Masked construct (10.5)
5801//===----------------------------------------------------------------------===//
5802
5803void MaskedOp::build(OpBuilder &builder, OperationState &state,
5804 const MaskedOperands &clauses) {
5805 MaskedOp::build(builder, state, clauses.filteredThreadId);
5806}
5807
5808//===----------------------------------------------------------------------===//
5809// Spec 5.2: Dispatch construct (7.6)
5810//===----------------------------------------------------------------------===//
5811
5812void DispatchOp::build(OpBuilder &builder, OperationState &state,
5813 const DispatchOperands &clauses) {
5814 DispatchOp::build(builder, state, clauses.nocontext, clauses.novariants,
5815 clauses.nowait);
5816}
5817
5818//===----------------------------------------------------------------------===//
5819// Spec 5.2: Scan construct (5.6)
5820//===----------------------------------------------------------------------===//
5821
5822void ScanOp::build(OpBuilder &builder, OperationState &state,
5823 const ScanOperands &clauses) {
5824 ScanOp::build(builder, state, clauses.inclusiveVars, clauses.exclusiveVars);
5825}
5826
5827LogicalResult ScanOp::verify() {
5828 if (hasExclusiveVars() == hasInclusiveVars())
5829 return emitError(
5830 "Exactly one of EXCLUSIVE or INCLUSIVE clause is expected");
5831 if (WsloopOp parentWsLoopOp = (*this)->getParentOfType<WsloopOp>()) {
5832 if (parentWsLoopOp.hasReductionModifier(ReductionModifier::inscan))
5833 return success();
5834 }
5835 if (SimdOp parentSimdOp = (*this)->getParentOfType<SimdOp>()) {
5836 if (parentSimdOp.hasReductionModifier(ReductionModifier::inscan))
5837 return success();
5838 }
5839 return emitError("SCAN directive needs to be enclosed within a parent "
5840 "worksharing loop construct or SIMD construct with INSCAN "
5841 "reduction modifier");
5842}
5843
5844/// Verifies align clause in allocate directive
5845LogicalResult verifyAlignment(Operation &op,
5846 std::optional<uint64_t> alignment) {
5847 if (alignment.has_value()) {
5848 if ((alignment.value() != 0) && !llvm::has_single_bit(alignment.value()))
5849 return op.emitError()
5850 << "ALIGN value : " << alignment.value() << " must be power of 2";
5851 }
5852 return success();
5853}
5854
5855LogicalResult AllocateDirOp::verify() {
5856 return verifyAlignment(*getOperation(), getAlign());
5857}
5858
5859//===----------------------------------------------------------------------===//
5860// AllocSharedMemOp
5861//===----------------------------------------------------------------------===//
5862
5863LogicalResult AllocSharedMemOp::verify() {
5864 return verifyAlignment(*getOperation(), getMemAlignment());
5865}
5866
5867//===----------------------------------------------------------------------===//
5868// FreeSharedMemOp
5869//===----------------------------------------------------------------------===//
5870
5871LogicalResult FreeSharedMemOp::verify() {
5872 return verifyAlignment(*getOperation(), getMemAlignment());
5873}
5874
5875//===----------------------------------------------------------------------===//
5876// WorkdistributeOp
5877//===----------------------------------------------------------------------===//
5878
5879LogicalResult WorkdistributeOp::verify() {
5880 if (isCombined())
5881 return emitOpError() << "cannot be a non-innermost combined construct leaf";
5882
5883 // Check that region exists and is not empty
5884 Region &region = getRegion();
5885 if (region.empty())
5886 return emitOpError("region cannot be empty");
5887 // Verify single entry point.
5888 Block &entryBlock = region.front();
5889 if (entryBlock.empty())
5890 return emitOpError("region must contain a structured block");
5891 // Verify single exit point.
5892 bool hasTerminator = false;
5893 for (Block &block : region) {
5894 if (isa<TerminatorOp>(block.back())) {
5895 if (hasTerminator) {
5896 return emitOpError("region must have exactly one terminator");
5897 }
5898 hasTerminator = true;
5899 }
5900 }
5901 if (!hasTerminator) {
5902 return emitOpError("region must be terminated with omp.terminator");
5903 }
5904 auto walkResult = region.walk([&](Operation *op) -> WalkResult {
5905 // No implicit barrier at end
5906 if (isa<BarrierOp>(op)) {
5907 return emitOpError(
5908 "explicit barriers are not allowed in workdistribute region");
5909 }
5910 // Check for invalid nested constructs
5911 if (isa<ParallelOp>(op)) {
5912 return emitOpError(
5913 "nested parallel constructs not allowed in workdistribute");
5914 }
5915 if (isa<TeamsOp>(op)) {
5916 return emitOpError(
5917 "nested teams constructs not allowed in workdistribute");
5918 }
5919 return WalkResult::advance();
5920 });
5921 if (walkResult.wasInterrupted())
5922 return failure();
5923
5924 Operation *parentOp = (*this)->getParentOp();
5925 if (!llvm::dyn_cast<TeamsOp>(parentOp))
5926 return emitOpError("workdistribute must be nested under teams");
5927 return success();
5928}
5929
5930//===----------------------------------------------------------------------===//
5931// Declare simd [7.7]
5932//===----------------------------------------------------------------------===//
5933
5934LogicalResult DeclareSimdOp::verify() {
5935 // Must be nested inside a function-like op
5936 auto func =
5937 dyn_cast_if_present<mlir::FunctionOpInterface>((*this)->getParentOp());
5938 if (!func)
5939 return emitOpError() << "must be nested inside a function";
5940
5941 if (getInbranch() && getNotinbranch())
5942 return emitOpError("cannot have both 'inbranch' and 'notinbranch'");
5943
5944 if (failed(verifyLinearModifiers(*this, getLinearModifiers(), getLinearVars(),
5945 /*isDeclareSimd=*/true)))
5946 return failure();
5947
5948 if (getLinearSyms())
5949 return emitOpError("linear privatizers are not allowed");
5950
5951 return verifyAlignedClause(*this, getAlignments(), getAlignedVars());
5952}
5953
5954void DeclareSimdOp::build(OpBuilder &odsBuilder, OperationState &odsState,
5955 const DeclareSimdOperands &clauses) {
5956 MLIRContext *ctx = odsBuilder.getContext();
5957 DeclareSimdOp::build(
5958 odsBuilder, odsState, clauses.alignedVars,
5959 makeArrayAttr(ctx, clauses.alignments), clauses.inbranch,
5960 clauses.linearVars, clauses.linearStepVars, clauses.linearVarTypes,
5961 clauses.linearModifiers, makeArrayAttr(ctx, clauses.linearSyms),
5962 clauses.notinbranch, clauses.simdlen, clauses.uniformVars);
5963}
5964
5965//===----------------------------------------------------------------------===//
5966// Parser and printer for Uniform Clause
5967//===----------------------------------------------------------------------===//
5968
5969/// uniform ::= `uniform` `(` uniform-list `)`
5970/// uniform-list := uniform-val (`,` uniform-val)*
5971/// uniform-val := ssa-id `:` type
5972static ParseResult
5975 SmallVectorImpl<Type> &uniformTypes) {
5976 return parser.parseCommaSeparatedList([&]() -> mlir::ParseResult {
5977 if (parser.parseOperand(uniformVars.emplace_back()) ||
5978 parser.parseColonType(uniformTypes.emplace_back()))
5979 return mlir::failure();
5980 return mlir::success();
5981 });
5982}
5983
5984/// Print Uniform Clauses
5986 ValueRange uniformVars, TypeRange uniformTypes) {
5987 for (unsigned i = 0; i < uniformVars.size(); ++i) {
5988 if (i != 0)
5989 p << ", ";
5990 p << uniformVars[i] << " : " << uniformTypes[i];
5991 }
5992}
5993
5994//===----------------------------------------------------------------------===//
5995// Parser and printer for Affinity Clause
5996//===----------------------------------------------------------------------===//
5997
5998static ParseResult parseAffinityClause(
5999 OpAsmParser &parser,
6002 SmallVectorImpl<Type> &iteratedTypes,
6003 SmallVectorImpl<Type> &affinityVarTypes) {
6004 if (failed(parseSplitIteratedList(
6005 parser, iterated, iteratedTypes, affinityVars, affinityVarTypes,
6006 /*parsePrefix=*/[&]() -> ParseResult { return success(); })))
6007 return failure();
6008 return success();
6009}
6010
6012 ValueRange iterated, ValueRange affinityVars,
6013 TypeRange iteratedTypes,
6014 TypeRange affinityVarTypes) {
6015 auto nop = [&](Value, Type) {};
6016 printSplitIteratedList(p, iterated, iteratedTypes, affinityVars,
6017 affinityVarTypes,
6018 /*plain prefix*/ nop,
6019 /*iterated prefix*/ nop);
6020}
6021
6022//===----------------------------------------------------------------------===//
6023// Parser, printer, and verifier for Iterator modifier
6024//===----------------------------------------------------------------------===//
6025
6026static ParseResult
6031 SmallVectorImpl<Type> &lbTypes,
6032 SmallVectorImpl<Type> &ubTypes,
6033 SmallVectorImpl<Type> &stepTypes) {
6034
6035 llvm::SMLoc ivLoc = parser.getCurrentLocation();
6037
6038 // Parse induction variables: %i : i32, %j : i32
6039 if (parser.parseCommaSeparatedList([&]() -> ParseResult {
6040 OpAsmParser::Argument &arg = ivArgs.emplace_back();
6041 if (parser.parseArgument(arg))
6042 return failure();
6043
6044 // Optional type, default to Index if not provided
6045 if (succeeded(parser.parseOptionalColon())) {
6046 if (parser.parseType(arg.type))
6047 return failure();
6048 } else {
6049 arg.type = parser.getBuilder().getIndexType();
6050 }
6051 return success();
6052 }))
6053 return failure();
6054
6055 // ) = (
6056 if (parser.parseRParen() || parser.parseEqual() || parser.parseLParen())
6057 return failure();
6058
6059 // Parse Ranges: (%lb to %ub step %st, ...)
6060 if (parser.parseCommaSeparatedList([&]() -> ParseResult {
6061 OpAsmParser::UnresolvedOperand lb, ub, st;
6062 if (parser.parseOperand(lb) || parser.parseKeyword("to") ||
6063 parser.parseOperand(ub) || parser.parseKeyword("step") ||
6064 parser.parseOperand(st))
6065 return failure();
6066
6067 lbs.push_back(lb);
6068 ubs.push_back(ub);
6069 steps.push_back(st);
6070 return success();
6071 }))
6072 return failure();
6073
6074 if (parser.parseRParen())
6075 return failure();
6076
6077 if (ivArgs.size() != lbs.size())
6078 return parser.emitError(ivLoc)
6079 << "mismatch: " << ivArgs.size() << " variables but " << lbs.size()
6080 << " ranges";
6081
6082 for (auto &arg : ivArgs) {
6083 lbTypes.push_back(arg.type);
6084 ubTypes.push_back(arg.type);
6085 stepTypes.push_back(arg.type);
6086 }
6087
6088 return parser.parseRegion(region, ivArgs);
6089}
6090
6092 ValueRange lbs, ValueRange ubs,
6094 TypeRange) {
6095 Block &entry = region.front();
6096
6097 for (unsigned i = 0, e = entry.getNumArguments(); i < e; ++i) {
6098 if (i != 0)
6099 p << ", ";
6100 p.printRegionArgument(entry.getArgument(i));
6101 }
6102 p << ") = (";
6103
6104 // (%lb0 to %ub0 step %step0, %lb1 to %ub1 step %step1, ...)
6105 for (unsigned i = 0, e = lbs.size(); i < e; ++i) {
6106 if (i)
6107 p << ", ";
6108 p << lbs[i] << " to " << ubs[i] << " step " << steps[i];
6109 }
6110 p << ") ";
6111
6112 p.printRegion(region, /*printEntryBlockArgs=*/false,
6113 /*printBlockTerminators=*/true);
6114}
6115
6116YieldOp IteratorOp::getYieldOp() {
6117 YieldOp yield;
6118 for (Block &block : getRegion()) {
6119 auto blockYield =
6120 dyn_cast_if_present<YieldOp>(block.empty() ? nullptr : &block.back());
6121 if (!blockYield)
6122 continue;
6123 if (yield)
6124 return {};
6125 yield = blockYield;
6126 }
6127 return yield;
6128}
6129
6130LogicalResult IteratorOp::verify() {
6131 auto iteratedTy = llvm::dyn_cast<omp::IteratedType>(getIterated().getType());
6132 if (!iteratedTy)
6133 return emitOpError() << "result must be omp.iterated<entry_ty>";
6134
6135 Block::BlockArgListType ivs = getRegion().front().getArguments();
6136 if (getLoopLowerBounds().size() != ivs.size())
6137 return emitOpError() << "number of range arguments and IVs do not match";
6138
6139 for (auto [lb, iv] : llvm::zip_equal(getLoopLowerBounds(), ivs)) {
6140 if (lb.getType() != iv.getType())
6141 return emitOpError()
6142 << "range argument type does not match corresponding IV type";
6143 }
6144
6145 for (Value step : getLoopSteps()) {
6146 if (matchPattern(step, m_Zero()))
6147 return emitOpError() << "loop step must not be zero";
6148 }
6149
6150 YieldOp yield = getYieldOp();
6151 if (!yield) {
6152 bool hasYield = llvm::any_of(getRegion(), [](Block &block) {
6153 return !block.empty() && isa<YieldOp>(block.back());
6154 });
6155 if (hasYield)
6156 return emitOpError() << "region must contain exactly one omp.yield";
6157 return emitOpError() << "region must be terminated by omp.yield";
6158 }
6159
6160 if (yield.getNumOperands() != 1)
6161 return emitOpError()
6162 << "omp.yield in omp.iterator region must yield exactly one value";
6163
6164 mlir::Type yieldedTy = yield.getOperand(0).getType();
6165 mlir::Type elemTy = iteratedTy.getElementType();
6166
6167 if (yieldedTy != elemTy)
6168 return emitOpError() << "omp.iterated element type (" << elemTy
6169 << ") does not match omp.yield operand type ("
6170 << yieldedTy << ")";
6171
6172 return success();
6173}
6174
6175LogicalResult IteratorOp::verifyRegions() {
6176 // Translation routes only the omp.yield block back to the iterator loop.
6177 for (Block &block : getRegion()) {
6178 Operation *terminator = block.getTerminator();
6179 if (block.getNumSuccessors() == 0 && !isa<YieldOp>(terminator))
6180 return mlir::emitError(terminator->getLoc())
6181 << "expected exit block terminator to be an `omp.yield` op.";
6182 }
6183 return success();
6184}
6185
6186//===----------------------------------------------------------------------===//
6187// GroupprivateOp
6188//===----------------------------------------------------------------------===//
6189
6190LogicalResult
6191GroupprivateOp::verifySymbolUses(SymbolTableCollection &symbolTable) {
6192 auto *symbol = symbolTable.lookupNearestSymbolFrom(*this, getSymNameAttr());
6193 if (!symbol)
6194 return emitOpError() << "expected symbol reference '" << getSymName()
6195 << "' to point to a global variable";
6196
6197 if (isa<FunctionOpInterface>(symbol))
6198 return emitOpError() << "expected symbol reference '" << getSymName()
6199 << "' to point to a global variable, not a function";
6200
6201 return success();
6202}
6203
6204#define GET_ATTRDEF_CLASSES
6205#include "mlir/Dialect/OpenMP/OpenMPOpsAttributes.cpp.inc"
6206
6207#define GET_OP_CLASSES
6208#include "mlir/Dialect/OpenMP/OpenMPOps.cpp.inc"
6209
6210#define GET_TYPEDEF_CLASSES
6211#include "mlir/Dialect/OpenMP/OpenMPOpsTypes.cpp.inc"
return success()
if(failed(verifyVectorMemoryOp(getOperation(), memrefType, getVectorType()))) return failure()
static std::optional< int64_t > getUpperBound(Value iv)
Gets the constant upper bound on an affine.for iv.
static LogicalResult verifyRegion(emitc::SwitchOp op, Region &region, const Twine &name)
Definition EmitC.cpp:1521
b
Return true if permutation is a valid permutation of the outer_dims_perm (case OuterOrInnerPerm::Oute...
ArrayAttr()
b getContext())
static const mlir::GenInfo * generator
static LogicalResult verifyNontemporalClause(Operation *op, OperandRange nontemporalVars)
static DenseI64ArrayAttr makeDenseI64ArrayAttr(MLIRContext *ctx, const ArrayRef< int64_t > intArray)
static void printDependVarList(OpAsmPrinter &p, Operation *op, OperandRange dependVars, TypeRange dependTypes, std::optional< ArrayAttr > dependKinds, OperandRange iteratedVars, TypeRange iteratedTypes, std::optional< ArrayAttr > iteratedKinds)
Print Depend clause.
static ParseResult parseTargetOpRegion(OpAsmParser &parser, Region &region, SmallVectorImpl< OpAsmParser::UnresolvedOperand > &hasDeviceAddrVars, SmallVectorImpl< Type > &hasDeviceAddrTypes, SmallVectorImpl< OpAsmParser::UnresolvedOperand > &hostEvalVars, SmallVectorImpl< Type > &hostEvalTypes, SmallVectorImpl< OpAsmParser::UnresolvedOperand > &mapVars, SmallVectorImpl< Type > &mapTypes, llvm::SmallVectorImpl< OpAsmParser::UnresolvedOperand > &privateVars, llvm::SmallVectorImpl< Type > &privateTypes, ArrayAttr &privateSyms, UnitAttr &privateNeedsBarrier, DenseI64ArrayAttr &privateMaps)
static constexpr StringRef getPrivateNeedsBarrierSpelling()
static void printHeapAllocClause(OpAsmPrinter &p, Operation *op, TypeAttr inType, ValueRange typeparams, TypeRange typeparamsTypes, ValueRange shape, TypeRange shapeTypes)
static ParseResult parseInReductionPrivateReductionRegion(OpAsmParser &parser, Region &region, SmallVectorImpl< OpAsmParser::UnresolvedOperand > &inReductionVars, SmallVectorImpl< Type > &inReductionTypes, DenseBoolArrayAttr &inReductionByref, ArrayAttr &inReductionSyms, llvm::SmallVectorImpl< OpAsmParser::UnresolvedOperand > &privateVars, llvm::SmallVectorImpl< Type > &privateTypes, ArrayAttr &privateSyms, UnitAttr &privateNeedsBarrier, ArrayAttr &reductionMods, SmallVectorImpl< OpAsmParser::UnresolvedOperand > &reductionVars, SmallVectorImpl< Type > &reductionTypes, DenseBoolArrayAttr &reductionByref, ArrayAttr &reductionSyms)
static ParseResult parseInReductionPrivateRegion(OpAsmParser &parser, Region &region, SmallVectorImpl< OpAsmParser::UnresolvedOperand > &inReductionVars, SmallVectorImpl< Type > &inReductionTypes, DenseBoolArrayAttr &inReductionByref, ArrayAttr &inReductionSyms, llvm::SmallVectorImpl< OpAsmParser::UnresolvedOperand > &privateVars, llvm::SmallVectorImpl< Type > &privateTypes, ArrayAttr &privateSyms, UnitAttr &privateNeedsBarrier)
static ArrayAttr makeArrayAttr(MLIRContext *context, llvm::ArrayRef< Attribute > attrs)
static ParseResult parseClauseAttr(AsmParser &parser, ClauseAttr &attr)
static void printLinearPrivateReductionRegion(OpAsmPrinter &p, Operation *op, Region &region, ValueRange linearVars, TypeRange linearTypes, ValueRange linearStepVars, TypeRange linearStepTypes, ArrayAttr linearVarTypes, ArrayAttr linearModifiers, ArrayAttr linearSyms, ValueRange privateVars, TypeRange privateTypes, ArrayAttr privateSyms, UnitAttr privateNeedsBarrier, ArrayAttr reductionMods, ValueRange reductionVars, TypeRange reductionTypes, DenseBoolArrayAttr reductionByref, ArrayAttr reductionSyms)
static ParseResult parseLinearPrivateReductionRegion(OpAsmParser &parser, Region &region, SmallVectorImpl< OpAsmParser::UnresolvedOperand > &linearVars, SmallVectorImpl< Type > &linearTypes, SmallVectorImpl< OpAsmParser::UnresolvedOperand > &linearStepVars, SmallVectorImpl< Type > &linearStepTypes, ArrayAttr &linearVarTypes, ArrayAttr &linearModifiers, ArrayAttr &linearSyms, llvm::SmallVectorImpl< OpAsmParser::UnresolvedOperand > &privateVars, llvm::SmallVectorImpl< Type > &privateTypes, ArrayAttr &privateSyms, UnitAttr &privateNeedsBarrier, ArrayAttr &reductionMods, SmallVectorImpl< OpAsmParser::UnresolvedOperand > &reductionVars, SmallVectorImpl< Type > &reductionTypes, DenseBoolArrayAttr &reductionByref, ArrayAttr &reductionSyms)
static void printDynGroupprivateClause(OpAsmPrinter &printer, Operation *op, AccessGroupModifierAttr modifierFirst, FallbackModifierAttr modifierSecond, Value dynGroupprivateSize, Type sizeType)
static void printAllocateAndAllocator(OpAsmPrinter &p, Operation *op, OperandRange allocateVars, TypeRange allocateTypes, OperandRange allocatorVars, TypeRange allocatorTypes)
Print allocate clause.
static DenseBoolArrayAttr makeDenseBoolArrayAttr(MLIRContext *ctx, const ArrayRef< bool > boolArray)
static std::string generateLoopNestingName(StringRef prefix, CanonicalLoopOp op)
Generate a name of a canonical loop nest of the format <prefix>(_r<idx>_s<idx>)*.
static ParseResult parseAffinityClause(OpAsmParser &parser, SmallVectorImpl< OpAsmParser::UnresolvedOperand > &iterated, SmallVectorImpl< OpAsmParser::UnresolvedOperand > &affinityVars, SmallVectorImpl< Type > &iteratedTypes, SmallVectorImpl< Type > &affinityVarTypes)
static void printSplitIteratedList(OpAsmPrinter &p, ValueRange iteratedVars, TypeRange iteratedTypes, ValueRange plainVars, TypeRange plainTypes, PrintPrefixFn &&printPrefixForPlain, PrintPrefixFn &&printPrefixForIterated)
static LogicalResult verifyDependVarList(Operation *op, std::optional< ArrayAttr > dependKinds, OperandRange dependVars, std::optional< ArrayAttr > iteratedKinds, OperandRange iteratedVars)
Verifies Depend clause.
static void addUnknownMemoryEffects(SmallVectorImpl< MemoryEffects::EffectInstance > &effects)
Add arbitrary read, write, allocate, and free effects.
static void printBlockArgClause(OpAsmPrinter &p, MLIRContext *ctx, StringRef clauseName, ValueRange argsSubrange, std::optional< MapPrintArgs > mapArgs)
static void printAffinityClause(OpAsmPrinter &p, Operation *op, ValueRange iterated, ValueRange affinityVars, TypeRange iteratedTypes, TypeRange affinityVarTypes)
static void printBlockArgRegion(OpAsmPrinter &p, Operation *op, Region &region, const AllRegionPrintArgs &args)
static ParseResult parseGranularityClause(OpAsmParser &parser, ClauseTypeAttr &prescriptiveness, std::optional< OpAsmParser::UnresolvedOperand > &operand, Type &operandType, std::optional< ClauseType >(*symbolizeClause)(StringRef), StringRef clauseName)
static void printIteratorHeader(OpAsmPrinter &p, Operation *op, Region &region, ValueRange lbs, ValueRange ubs, ValueRange steps, TypeRange, TypeRange, TypeRange)
static LogicalResult verifyDeclareTargetAttr(Operation *op, Attribute attr)
static ParseResult parseHeapAllocClause(OpAsmParser &parser, TypeAttr &inTypeAttr, SmallVectorImpl< OpAsmParser::UnresolvedOperand > &typeparams, SmallVectorImpl< Type > &typeparamsTypes, SmallVectorImpl< OpAsmParser::UnresolvedOperand > &shape, SmallVectorImpl< Type > &shapeTypes)
operation ::= $in_type ( ( $typeparams ) )? ( , $shape )?
static void printInReductionClause(OpAsmPrinter &p, Operation *op, ValueRange inReductionVars, TypeRange inReductionTypes, DenseBoolArrayAttr inReductionByref, ArrayAttr inReductionSyms)
Prints an in_reduction clause for an operation that does not give its list items entry block argument...
static void printClauseWithRegionArgs(OpAsmPrinter &p, MLIRContext *ctx, StringRef clauseName, ValueRange argsSubrange, ValueRange operands, TypeRange types, ArrayAttr symbols=nullptr, DenseI64ArrayAttr mapIndices=nullptr, DenseBoolArrayAttr byref=nullptr, ArrayAttr modifier=nullptr, UnitAttr needsBarrier=nullptr)
static ParseResult parseIteratorHeader(OpAsmParser &parser, Region &region, SmallVectorImpl< OpAsmParser::UnresolvedOperand > &lbs, SmallVectorImpl< OpAsmParser::UnresolvedOperand > &ubs, SmallVectorImpl< OpAsmParser::UnresolvedOperand > &steps, SmallVectorImpl< Type > &lbTypes, SmallVectorImpl< Type > &ubTypes, SmallVectorImpl< Type > &stepTypes)
static LogicalResult verifyReductionVarList(Operation *op, std::optional< ArrayAttr > reductionSyms, OperandRange reductionVars, std::optional< ArrayRef< bool > > reductionByref, ArrayAttr reductionMods=nullptr)
Verifies Reduction Clause.
static ParseResult parseBlockArgRegion(OpAsmParser &parser, Region &region, AllRegionParseArgs args)
static ParseResult parseLoopTransformClis(OpAsmParser &parser, SmallVectorImpl< OpAsmParser::UnresolvedOperand > &generateesOperands, SmallVectorImpl< OpAsmParser::UnresolvedOperand > &applyeesOperands)
static ParseResult parseSynchronizationHint(OpAsmParser &parser, IntegerAttr &hintAttr)
Parses a Synchronization Hint clause.
static void printLinearClause(OpAsmPrinter &p, Operation *op, ValueRange linearVars, TypeRange linearTypes, ValueRange linearStepVars, TypeRange stepVarTypes, ArrayAttr linearModifiers, ArrayAttr linearSyms)
Print Linear Clause.
static void printScheduleClause(OpAsmPrinter &p, Operation *op, ClauseScheduleKindAttr scheduleKind, ScheduleModifierAttr scheduleMod, UnitAttr scheduleSimd, Value scheduleChunk, Type scheduleChunkType)
Print schedule clause.
static ParseResult parsePrivateReductionRegion(OpAsmParser &parser, Region &region, llvm::SmallVectorImpl< OpAsmParser::UnresolvedOperand > &privateVars, llvm::SmallVectorImpl< Type > &privateTypes, ArrayAttr &privateSyms, UnitAttr &privateNeedsBarrier, ArrayAttr &reductionMods, SmallVectorImpl< OpAsmParser::UnresolvedOperand > &reductionVars, SmallVectorImpl< Type > &reductionTypes, DenseBoolArrayAttr &reductionByref, ArrayAttr &reductionSyms)
static void printCopyprivate(OpAsmPrinter &p, Operation *op, OperandRange copyprivateVars, TypeRange copyprivateTypes, std::optional< ArrayAttr > copyprivateSyms)
Print Copyprivate clause.
static ParseResult parseOrderClause(OpAsmParser &parser, ClauseOrderKindAttr &order, OrderModifierAttr &orderMod)
static bool mapTypeToBool(ClauseMapFlags value, ClauseMapFlags flag)
static void printAlignedClause(OpAsmPrinter &p, Operation *op, ValueRange alignedVars, TypeRange alignedTypes, std::optional< ArrayAttr > alignments)
Print Aligned Clause.
static bool targetInReductionCapturedBy(Value inReductionVar, Value mapVarPtr)
An omp.target in_reduction operand is captured by a map_entries entry when the entry's MapInfoOp var_...
static LogicalResult verifySynchronizationHint(Operation *op, uint64_t hint)
Verifies a synchronization hint clause.
static ParseResult parseUseDeviceAddrUseDevicePtrRegion(OpAsmParser &parser, Region &region, SmallVectorImpl< OpAsmParser::UnresolvedOperand > &useDeviceAddrVars, SmallVectorImpl< Type > &useDeviceAddrTypes, SmallVectorImpl< OpAsmParser::UnresolvedOperand > &useDevicePtrVars, SmallVectorImpl< Type > &useDevicePtrTypes)
static void printPrivateReductionRegion(OpAsmPrinter &p, Operation *op, Region &region, ValueRange privateVars, TypeRange privateTypes, ArrayAttr privateSyms, UnitAttr privateNeedsBarrier, ArrayAttr reductionMods, ValueRange reductionVars, TypeRange reductionTypes, DenseBoolArrayAttr reductionByref, ArrayAttr reductionSyms)
static ParseResult parseUniformClause(OpAsmParser &parser, SmallVectorImpl< OpAsmParser::UnresolvedOperand > &uniformVars, SmallVectorImpl< Type > &uniformTypes)
uniform ::= uniform ( uniform-list ) uniform-list := uniform-val (, uniform-val)* uniform-val := ssa-...
static void printInReductionPrivateRegion(OpAsmPrinter &p, Operation *op, Region &region, ValueRange inReductionVars, TypeRange inReductionTypes, DenseBoolArrayAttr inReductionByref, ArrayAttr inReductionSyms, ValueRange privateVars, TypeRange privateTypes, ArrayAttr privateSyms, UnitAttr privateNeedsBarrier)
static LogicalResult verifyAllocateClause(Operation *op, ValueRange allocateVars, ValueRange allocatorVars, DenseI64ArrayAttr allocateAlignments, DenseI64ArrayAttr allocatePrivateIndices, ValueRange privateVars={}, ArrayAttr privateSyms=nullptr, bool requirePrivateIndices=false)
static void printSynchronizationHint(OpAsmPrinter &p, Operation *op, IntegerAttr hintAttr)
Prints a Synchronization Hint clause.
static void printGranularityClause(OpAsmPrinter &p, Operation *op, ClauseTypeAttr prescriptiveness, Value operand, mlir::Type operandType, StringRef(*stringifyClauseType)(ClauseType))
static ParseResult parseDependVarList(OpAsmParser &parser, SmallVectorImpl< OpAsmParser::UnresolvedOperand > &dependVars, SmallVectorImpl< Type > &dependTypes, ArrayAttr &dependKinds, SmallVectorImpl< OpAsmParser::UnresolvedOperand > &iteratedVars, SmallVectorImpl< Type > &iteratedTypes, ArrayAttr &iteratedKinds)
depend-entry-list ::= depend-entry | depend-entry-list , depend-entry depend-entry ::= depend-kind ->...
static Operation * getParentInSameDialect(Operation *thisOp)
static void printUniformClause(OpAsmPrinter &p, Operation *op, ValueRange uniformVars, TypeRange uniformTypes)
Print Uniform Clauses.
static LogicalResult verifyCopyprivateVarList(Operation *op, OperandRange copyprivateVars, std::optional< ArrayAttr > copyprivateSyms)
Verifies CopyPrivate Clause.
static LogicalResult verifyAlignedClause(Operation *op, std::optional< ArrayAttr > alignments, OperandRange alignedVars)
static ParseResult parsePrivateRegion(OpAsmParser &parser, Region &region, llvm::SmallVectorImpl< OpAsmParser::UnresolvedOperand > &privateVars, llvm::SmallVectorImpl< Type > &privateTypes, ArrayAttr &privateSyms, UnitAttr &privateNeedsBarrier)
static void printNumTasksClause(OpAsmPrinter &p, Operation *op, ClauseNumTasksTypeAttr numTasksMod, Value numTasks, mlir::Type numTasksType)
static void printLoopTransformClis(OpAsmPrinter &p, TileOp op, OperandRange generatees, OperandRange applyees)
static ParseResult parseDynGroupprivateClause(OpAsmParser &parser, AccessGroupModifierAttr &accessGroupAttr, FallbackModifierAttr &fallbackAttr, std::optional< OpAsmParser::UnresolvedOperand > &dynGroupprivateSize, Type &sizeType)
static void printPrivateRegion(OpAsmPrinter &p, Operation *op, Region &region, ValueRange privateVars, TypeRange privateTypes, ArrayAttr privateSyms, UnitAttr privateNeedsBarrier)
static ParseResult parseSplitIteratedList(OpAsmParser &parser, SmallVectorImpl< OpAsmParser::UnresolvedOperand > &iteratedVars, SmallVectorImpl< Type > &iteratedTypes, SmallVectorImpl< OpAsmParser::UnresolvedOperand > &plainVars, SmallVectorImpl< Type > &plainTypes, ParsePrefixFn &&parsePrefix)
static void printTaskReductionRegion(OpAsmPrinter &p, Operation *op, Region &region, ValueRange taskReductionVars, TypeRange taskReductionTypes, DenseBoolArrayAttr taskReductionByref, ArrayAttr taskReductionSyms)
static LogicalResult verifyMapInfoForMapClause(Operation *op, mlir::omp::MapInfoOp mapInfoOp, llvm::DenseSet< mlir::TypedValue< mlir::omp::PointerLikeType > > &updateToVars, llvm::DenseSet< mlir::TypedValue< mlir::omp::PointerLikeType > > &updateFromVars)
static void getPrivatisationEffects(Operation *op, SmallVectorImpl< MemoryEffects::EffectInstance > &effects)
Privatisation may read the original as a mold or firstprivate copy source. Any lastprivate copy-back ...
static void printInReductionPrivateReductionRegion(OpAsmPrinter &p, Operation *op, Region &region, ValueRange inReductionVars, TypeRange inReductionTypes, DenseBoolArrayAttr inReductionByref, ArrayAttr inReductionSyms, ValueRange privateVars, TypeRange privateTypes, ArrayAttr privateSyms, UnitAttr privateNeedsBarrier, ArrayAttr reductionMods, ValueRange reductionVars, TypeRange reductionTypes, DenseBoolArrayAttr reductionByref, ArrayAttr reductionSyms)
return success()
static void getBarrierEffects(llvm::SmallVectorImpl< SideEffects::EffectInstance< MemoryEffects::Effect > > &effects)
static LogicalResult verifyOrderedParent(Operation &op)
static void printOrderClause(OpAsmPrinter &p, Operation *op, ClauseOrderKindAttr order, OrderModifierAttr orderMod)
static ParseResult parseBlockArgClause(OpAsmParser &parser, llvm::SmallVectorImpl< OpAsmParser::Argument > &entryBlockArgs, StringRef keyword, std::optional< MapParseArgs > mapArgs)
static LogicalResult verifyPrivateVarsMapping(TargetOp targetOp)
static ParseResult parseScheduleClause(OpAsmParser &parser, ClauseScheduleKindAttr &scheduleAttr, ScheduleModifierAttr &scheduleMod, UnitAttr &scheduleSimd, std::optional< OpAsmParser::UnresolvedOperand > &chunkSize, Type &chunkType)
schedule ::= schedule ( sched-list ) sched-list ::= sched-val | sched-val sched-list | sched-val ,...
static LogicalResult verifyDynGroupprivateClause(Operation *op, AccessGroupModifierAttr accessGroup, FallbackModifierAttr fallback, Value dynGroupprivateSize)
static LogicalResult verifyLinearModifiers(Operation *op, std::optional< ArrayAttr > linearModifiers, OperandRange linearVars, bool isDeclareSimd=false)
OpenMP 5.2, Section 5.4.6: "A linear-modifier may be specified as ref or uval only on a declare simd ...
static void printClauseAttr(OpAsmPrinter &p, Operation *op, ClauseAttr attr)
static LogicalResult verifyLinearSyms(OpType op)
Verifies that each linear variable has a linear privatizer.
static ParseResult parseAllocateAndAllocator(OpAsmParser &parser, SmallVectorImpl< OpAsmParser::UnresolvedOperand > &allocateVars, SmallVectorImpl< Type > &allocateTypes, SmallVectorImpl< OpAsmParser::UnresolvedOperand > &allocatorVars, SmallVectorImpl< Type > &allocatorTypes)
Parse an allocate clause with allocators and a list of operands with types.
static void printMembersIndex(OpAsmPrinter &p, MapInfoOp op, ArrayAttr membersIdx)
static void getReductionEffects(Operation *op, SmallVectorImpl< MemoryEffects::EffectInstance > &effects)
Reductions initialise private accumulators and combine them into the original variables....
static ParseResult parseClauseWithRegionArgs(OpAsmParser &parser, SmallVectorImpl< OpAsmParser::UnresolvedOperand > &operands, SmallVectorImpl< Type > &types, SmallVectorImpl< OpAsmParser::Argument > &regionPrivateArgs, ArrayAttr *symbols=nullptr, DenseI64ArrayAttr *mapIndices=nullptr, DenseBoolArrayAttr *byref=nullptr, ArrayAttr *modifier=nullptr, UnitAttr *needsBarrier=nullptr)
static void printCaptureType(OpAsmPrinter &p, Operation *op, VariableCaptureKindAttr mapCaptureType)
static LogicalResult verifyReductionModifiers(Operation *op, OperandRange reductionVars, ArrayAttr reductionMods)
Verify the per-variable modifier list has one entry per reduction variable.
static LogicalResult verifyNumTeamsClause(Operation *op, Value numTeamsLower, OperandRange numTeamsUpperVars)
static bool opInGlobalImplicitParallelRegion(Operation *op)
static void printTargetOpRegion(OpAsmPrinter &p, Operation *op, Region &region, ValueRange hasDeviceAddrVars, TypeRange hasDeviceAddrTypes, ValueRange hostEvalVars, TypeRange hostEvalTypes, ValueRange mapVars, TypeRange mapTypes, ValueRange privateVars, TypeRange privateTypes, ArrayAttr privateSyms, UnitAttr privateNeedsBarrier, DenseI64ArrayAttr privateMaps)
static void printUseDeviceAddrUseDevicePtrRegion(OpAsmPrinter &p, Operation *op, Region &region, ValueRange useDeviceAddrVars, TypeRange useDeviceAddrTypes, ValueRange useDevicePtrVars, TypeRange useDevicePtrTypes)
static LogicalResult verifyMapClause(Operation *op, OperandRange mapVars, OperandRange mapIterated)
static LogicalResult verifyPrivateVarList(OpType &op)
static ParseResult parseNumTasksClause(OpAsmParser &parser, ClauseNumTasksTypeAttr &numTasksMod, std::optional< OpAsmParser::UnresolvedOperand > &numTasks, Type &numTasksType)
LogicalResult verifyAlignment(Operation &op, std::optional< uint64_t > alignment)
Verifies align clause in allocate directive.
static ParseResult parseAlignedClause(OpAsmParser &parser, SmallVectorImpl< OpAsmParser::UnresolvedOperand > &alignedVars, SmallVectorImpl< Type > &alignedTypes, ArrayAttr &alignmentsAttr)
aligned ::= aligned ( aligned-list ) aligned-list := aligned-val | aligned-val aligned-list aligned-v...
static ParseResult parseLinearClause(OpAsmParser &parser, SmallVectorImpl< OpAsmParser::UnresolvedOperand > &linearVars, SmallVectorImpl< Type > &linearTypes, SmallVectorImpl< OpAsmParser::UnresolvedOperand > &linearStepVars, SmallVectorImpl< Type > &linearStepTypes, ArrayAttr &linearModifiers, ArrayAttr &linearSyms)
linear ::= linear ( linear-list ) linear-list := linear-val | linear-val linear-list linear-val := ss...
static LogicalResult checkApplyeesNesting(TileOp op)
Check properties of the loop nest consisting of the transformation's applyees:
static ParseResult parseCaptureType(OpAsmParser &parser, VariableCaptureKindAttr &mapCaptureType)
static ParseResult parseTaskReductionRegion(OpAsmParser &parser, Region &region, SmallVectorImpl< OpAsmParser::UnresolvedOperand > &taskReductionVars, SmallVectorImpl< Type > &taskReductionTypes, DenseBoolArrayAttr &taskReductionByref, ArrayAttr &taskReductionSyms)
static ParseResult parseGrainsizeClause(OpAsmParser &parser, ClauseGrainsizeTypeAttr &grainsizeMod, std::optional< OpAsmParser::UnresolvedOperand > &grainsize, Type &grainsizeType)
static ParseResult parseCopyprivate(OpAsmParser &parser, SmallVectorImpl< OpAsmParser::UnresolvedOperand > &copyprivateVars, SmallVectorImpl< Type > &copyprivateTypes, ArrayAttr &copyprivateSyms)
copyprivate-entry-list ::= copyprivate-entry | copyprivate-entry-list , copyprivate-entry copyprivate...
static ParseResult parseInReductionClause(OpAsmParser &parser, SmallVectorImpl< OpAsmParser::UnresolvedOperand > &inReductionVars, SmallVectorImpl< Type > &inReductionTypes, DenseBoolArrayAttr &inReductionByref, ArrayAttr &inReductionSyms)
Parses an in_reduction clause for an operation that does not give its list items entry block argument...
static LogicalResult verifyMapInfoDefinedArgs(Operation *op, StringRef clauseName, OperandRange vars)
static void printGrainsizeClause(OpAsmPrinter &p, Operation *op, ClauseGrainsizeTypeAttr grainsizeMod, Value grainsize, mlir::Type grainsizeType)
static ParseResult verifyScheduleModifiers(OpAsmParser &parser, SmallVectorImpl< SmallString< 12 > > &modifiers)
static Type getElementType(Type type, ArrayRef< int32_t > indices, function_ref< InFlightDiagnostic(StringRef)> emitErrorFn)
Walks the given type hierarchy with the given indices, potentially down to component granularity,...
Definition SPIRVOps.cpp:229
static bool isUnique(It begin, It end)
Definition ShardOps.cpp:159
static LogicalResult emit(SolverOp solver, const SMTEmissionOptions &options, mlir::raw_indented_ostream &stream)
Emit the SMT operations in the given 'solver' to the 'stream'.
static SmallVector< Value > getTileSizes(Location loc, x86::amx::TileType tType, RewriterBase &rewriter)
Maps the 2-dim vector shape to the two 16-bit tile sizes.
This base class exposes generic asm parser hooks, usable across the various derived parsers.
virtual ParseResult parseMinus()=0
Parse a '-' token.
@ Paren
Parens surrounding zero or more operands.
@ None
Zero or more operands with no delimiters.
virtual ParseResult parseColonTypeList(SmallVectorImpl< Type > &result)=0
Parse a colon followed by a type list, which must have at least one type.
virtual Builder & getBuilder() const =0
Return a builder which provides useful access to MLIRContext, global objects like types and attribute...
virtual ParseResult parseCommaSeparatedList(Delimiter delimiter, function_ref< ParseResult()> parseElementFn, StringRef contextMessage=StringRef())=0
Parse a list of comma-separated items with an optional delimiter.
virtual ParseResult parseOptionalAttrDict(NamedAttrList &result)=0
Parse a named dictionary into 'result' if it is present.
virtual ParseResult parseOptionalEqual()=0
Parse a = token if present.
virtual ParseResult parseOptionalKeyword(StringRef keyword)=0
Parse the given keyword if present.
MLIRContext * getContext() const
virtual ParseResult parseRParen()=0
Parse a ) token.
virtual InFlightDiagnostic emitError(SMLoc loc, const Twine &message={})=0
Emit a diagnostic at the specified location and return failure.
virtual ParseResult parseOptionalColon()=0
Parse a : token if present.
virtual ParseResult parseLSquare()=0
Parse a [ token.
virtual ParseResult parseRSquare()=0
Parse a ] token.
ParseResult parseInteger(IntT &result)
Parse an integer value from the stream.
virtual ParseResult parseOptionalArrow()=0
Parse a '->' token if present.
virtual ParseResult parseLess()=0
Parse a '<' token.
virtual ParseResult parseEqual()=0
Parse a = token.
virtual ParseResult parseColonType(Type &result)=0
Parse a colon followed by a type.
virtual SMLoc getCurrentLocation()=0
Get the location of the next token and store it into the argument.
virtual ParseResult parseOptionalComma()=0
Parse a , token if present.
virtual ParseResult parseColon()=0
Parse a : token.
virtual SMLoc getNameLoc() const =0
Return the location of the original name token.
virtual ParseResult parseOptionalLess()=0
Parse a '<' token if present.
virtual ParseResult parseArrow()=0
Parse a '->' token.
virtual ParseResult parseLParen()=0
Parse a ( token.
virtual ParseResult parseType(Type &result)=0
Parse a type.
virtual ParseResult parseComma()=0
Parse a , token.
virtual ParseResult parseOptionalLParen()=0
Parse a ( token if present.
ParseResult parseKeyword(StringRef keyword)
Parse a given keyword.
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.
Attributes are known-constant values of operations.
Definition Attributes.h:25
This class represents an argument of a Block.
Definition Value.h:308
Block represents an ordered list of Operations.
Definition Block.h:34
MutableArrayRef< BlockArgument > BlockArgListType
Definition Block.h:110
ValueTypeRange< BlockArgListType > getArgumentTypes()
Return a range containing the types of the arguments for this block.
Definition Block.cpp:154
unsigned getNumSuccessors()
Definition Block.cpp:270
bool empty()
Definition Block.h:173
BlockArgument getArgument(unsigned i)
Definition Block.h:154
unsigned getNumArguments()
Definition Block.h:153
Operation & front()
Definition Block.h:178
SuccessorRange getSuccessors()
Definition Block.h:280
Operation & back()
Definition Block.h:177
Operation * getTerminator()
Get the terminator operation of this block.
Definition Block.cpp:249
bool mightHaveTerminator()
Return "true" if this block might have a terminator.
Definition Block.cpp:255
BlockArgListType getArguments()
Definition Block.h:112
iterator end()
Definition Block.h:169
iterator begin()
Definition Block.h:168
IntegerType getI64Type()
Definition Builders.cpp:73
IntegerAttr getI64IntegerAttr(int64_t value)
Definition Builders.cpp:120
IntegerType getIntegerType(unsigned width)
Definition Builders.cpp:75
MLIRContext * getContext() const
Definition Builders.h:56
Attr getAttr(Args &&...args)
Get or construct an instance of the attribute Attr with provided arguments.
Definition Builders.h:101
Diagnostic & append(Arg1 &&arg1, Arg2 &&arg2, Args &&...args)
Append arguments to the diagnostic.
Diagnostic & appendOp(Operation &op, const OpPrintingFlags &flags)
Append an operation with the given printing flags.
A class for computing basic dominance information.
Definition Dominance.h:143
bool dominates(Operation *a, Operation *b) const
Return true if operation A dominates operation B, i.e.
Definition Dominance.h:161
This class represents a diagnostic that is inflight and set to be reported.
Diagnostic & attachNote(std::optional< Location > noteLoc=std::nullopt)
Attaches a note to this diagnostic.
MLIRContext is the top-level object for a collection of MLIR operations.
Definition MLIRContext.h:71
NamedAttribute represents a combination of a name and an Attribute value.
Definition Attributes.h:166
StringAttr getName() const
Return the name of the attribute.
Attribute getValue() const
Return the value of the attribute.
Definition Attributes.h:181
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 parseArgument(Argument &result, bool allowType=false, bool allowAttrs=false)=0
Parse a single argument with the following syntax:
virtual ParseResult parseArgumentList(SmallVectorImpl< Argument > &result, Delimiter delimiter=Delimiter::None, bool allowType=false, bool allowAttrs=false)=0
Parse zero or more arguments with a specified surrounding delimiter.
virtual ParseResult resolveOperand(const UnresolvedOperand &operand, Type type, SmallVectorImpl< Value > &result)=0
Resolve an operand to an SSA value, emitting an error on failure.
ParseResult resolveOperands(Operands &&operands, Type type, SmallVectorImpl< Value > &result)
Resolve a list of operands to SSA values, emitting an error on failure, or appending the results to t...
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 ParseResult parseOperandList(SmallVectorImpl< UnresolvedOperand > &result, Delimiter delimiter=Delimiter::None, bool allowResultNumber=true, int requiredOperandCount=-1)=0
Parse zero or more SSA comma-separated operand references with a specified surrounding delimiter,...
This is a pure-virtual base class that exposes the asmprinter hooks necessary to implement a custom p...
virtual void printOptionalAttrDict(ArrayRef< NamedAttribute > attrs, ArrayRef< StringRef > elidedAttrs={})=0
If the specified operation has attributes, print out an attribute dictionary with their values.
virtual void printRegion(Region &blocks, bool printEntryBlockArgs=true, bool printBlockTerminators=true, bool printEmptyBlock=false)=0
Prints a region.
virtual void printRegionArgument(BlockArgument arg, ArrayRef< NamedAttribute > argAttrs={}, bool omitType=false)=0
Print a block argument in the usual format of: ssaName : type {attr1=42} loc("here") where location p...
virtual void printOperand(Value value)=0
Print implementations for various things an operation contains.
This class helps build Operations.
Definition Builders.h:210
This class represents an operand of an operation.
Definition Value.h:256
Set of flags used to control the behavior of the various IR print methods (e.g.
This class provides the API for ops that are known to be isolated from above.
This class provides the API for ops that are known to be terminators.
This class indicates that the regions associated with this op don't have terminators.
This class implements the operand iterators for the Operation class.
Definition ValueRange.h:44
type_range getType() const
Operation is the basic unit of execution within MLIR.
Definition Operation.h:87
Dialect * getDialect()
Return the dialect this operation is associated with, or nullptr if the associated dialect is not loa...
Definition Operation.h:237
Region & getRegion(unsigned index)
Returns the region held by this operation at position 'index'.
Definition Operation.h:740
bool hasTrait()
Returns true if the operation was registered with a particular trait, e.g.
Definition Operation.h:803
Block * getBlock()
Returns the operation block that contains this operation.
Definition Operation.h:230
unsigned getNumRegions()
Returns the number of regions held by this operation.
Definition Operation.h:728
Location getLoc()
The source location the operation was defined or derived from.
Definition Operation.h:240
Operation * getParentOp()
Returns the closest surrounding operation that contains this operation or nullptr if this is a top-le...
Definition Operation.h:251
InFlightDiagnostic emitError(const Twine &message={})
Emit an error about fatal conditions with this operation, reporting up to any diagnostic handlers tha...
OpTy getParentOfType()
Return the closest surrounding parent operation that is of type 'OpTy'.
Definition Operation.h:255
MutableArrayRef< Region > getRegions()
Returns the regions held by this operation.
Definition Operation.h:731
operand_range getOperands()
Returns an iterator on the underlying Value's.
Definition Operation.h:405
user_range getUsers()
Returns a range of all users.
Definition Operation.h:927
Region * getParentRegion()
Returns the region to which the instruction belongs.
Definition Operation.h:247
MLIRContext * getContext()
Return the context this operation is associated with.
Definition Operation.h:233
InFlightDiagnostic emitOpError(const Twine &message={})
Emit an error with the op name prefixed, like "'dim' op " which is convenient for verifiers.
A special type of RewriterBase that coordinates the application of a rewrite pattern on the current I...
This class contains a list of basic blocks and a link to the parent operation it is attached to.
Definition Region.h:26
Block & front()
Definition Region.h:65
BlockArgListType getArguments()
Definition Region.h:94
OpIterator op_begin()
Return iterators that walk the operations nested directly within this region.
Definition Region.h:178
bool isAncestor(Region *other)
Return true if this region is ancestor of the other region.
Definition Region.h:234
iterator_range< OpIterator > getOps()
Definition Region.h:180
bool empty()
Definition Region.h:60
unsigned getNumArguments()
Definition Region.h:136
Location getLoc()
Return a location for this region.
Definition Region.cpp:31
BlockArgument getArgument(unsigned i)
Definition Region.h:137
Operation * getParentOp()
Return the parent operation this region is attached to.
Definition Region.h:198
BlockListType & getBlocks()
Definition Region.h:45
virtual void eraseOp(Operation *op)
This method erases an operation that is known to have no uses.
OpTy replaceOpWithNewOp(Operation *op, Args &&...args)
Replace the results of the given (original) op with a new op that is created without verification (re...
This class represents a specific instance of an effect.
This class represents a collection of SymbolTables.
virtual Operation * lookupNearestSymbolFrom(Operation *from, StringAttr symbol)
Returns the operation registered with the given symbol name within the closest parent operation of,...
static Operation * lookupNearestSymbolFrom(Operation *from, StringAttr symbol)
Returns the operation registered with the given symbol name within the closest parent operation of,...
This class provides an abstraction over the various different ranges of value types.
Definition TypeRange.h:40
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition Types.h:74
This class provides an abstraction over the different types of ranges over Values.
Definition ValueRange.h:392
type_range getType() const
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition Value.h:96
MLIRContext * getContext() const
Utility to get the associated MLIRContext that this value is defined in.
Definition Value.h:108
Type getType() const
Return the type of this value.
Definition Value.h:105
use_range getUses() const
Returns a range of all uses, which is useful for iterating over all uses.
Definition Value.h:188
Operation * getDefiningOp() const
If this value is the result of an operation, return the operation that defines it.
Definition Value.cpp:18
A utility result that is used to signal how to proceed with an ongoing walk:
Definition WalkResult.h:29
static WalkResult advance()
Definition WalkResult.h:47
static DenseArrayAttrImpl get(MLIRContext *context, ArrayRef< bool > content)
bool isReachableFromEntry(Block *a) const
Return true if the specified block is reachable from the entry block of its region.
Operation * getOwner() const
Return the owner of this operand.
Definition UseDefLists.h:38
TargetEnterDataOperands TargetEnterExitUpdateDataOperands
omp.target_enter_data, omp.target_exit_data and omp.target_update take the same clauses,...
std::tuple< NewCliOp, OpOperand *, OpOperand * > decodeCli(mlir::Value cli)
Find the omp.new_cli, generator, and consumer of a canonical loop info.
ClauseProcBindKind convertProcBindKind(llvm::omp::ProcBindKind kind)
Convert a proc_bind kind from the LLVM frontend enum to the corresponding OpenMP dialect enum.
detail::InFlightRemark failed(Location loc, RemarkOpts opts)
Report an optimization remark that failed.
Definition Remarks.h:744
Include the generated interface declarations.
bool matchPattern(Value value, const Pattern &pattern)
Entry point for matching a pattern over a Value.
Definition Matchers.h:490
detail::DenseArrayAttrImpl< int64_t > DenseI64ArrayAttr
function_ref< void(Value, StringRef)> OpAsmSetValueNameFn
A functor used to set the name of the start of a result group of an operation.
Type getType(OpFoldResult ofr)
Returns the int type of the integer in ofr.
Definition Utils.cpp:311
llvm::DenseSet< ValueT, ValueInfoT > DenseSet
Definition LLVM.h:125
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.
bool isPure(Operation *op)
Returns true if the given operation is pure, i.e., is speculatable that does not touch memory.
detail::constant_int_predicate_matcher m_Zero()
Matches a constant scalar / vector splat / tensor splat integer zero.
Definition Matchers.h:442
std::conditional_t< std::is_same_v< Ty, mlir::Type >, mlir::Value, detail::TypedValue< Ty > > TypedValue
If Ty is mlir::Type this will select Value instead of having a wrapper around it.
Definition Value.h:496
llvm::TypeSwitch< T, ResultT > TypeSwitch
Definition LLVM.h:147
SmallVector< Loops, 8 > tile(ArrayRef< scf::ForOp > forOps, ArrayRef< Value > sizes, ArrayRef< scf::ForOp > targets)
Performs tiling fo imperfectly nested loops (with interchange) by strip-mining the forOps by sizes an...
Definition Utils.cpp:1389
detail::DenseArrayAttrImpl< bool > DenseBoolArrayAttr
detail::constant_op_matcher m_Constant()
Matches a constant foldable operation.
Definition Matchers.h:369
function_ref< void(Block *, StringRef)> OpAsmSetBlockNameFn
A functor used to set the name of blocks in regions directly nested under an operation.
This is the representation of an operand reference.
This class provides APIs and verifiers for ops with regions having a single block.
This represents an operation in an abstracted form, suitable for use with the builder APIs.
T & getOrAddProperties()
Get (or create) the properties of the provided type to be set on the operation on creation.
void addOperands(ValueRange newOperands)
void addAttributes(ArrayRef< NamedAttribute > newAttributes)
Add an array of named attributes.
void addAttribute(StringRef name, Attribute attr)
Add an attribute with the specified name.
void addTypes(ArrayRef< Type > newTypes)
Region * addRegion()
Create a region that should be attached to the operation.
Extended TargetOperands with kernel_type attribute.
TargetExecModeAttr kernelType
Kernel execution mode for the target region.