MLIR 24.0.0git
OpenACCCG.cpp
Go to the documentation of this file.
1//===- OpenACCCG.cpp - OpenACC codegen ops, attributes, and types ---------===//
2//
3// Part of the MLIR 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// Implementation for OpenACC codegen operations, attributes, and types.
10// These correspond to the definitions in OpenACCCG*.td tablegen files
11// and are kept in a separate file because they do not represent direct mappings
12// of OpenACC language constructs; they are intermediate representations used
13// when decomposing and lowering primary `acc` dialect operations.
14//
15//===----------------------------------------------------------------------===//
16
24#include "mlir/IR/Region.h"
28#include "llvm/ADT/DenseMap.h"
29#include "llvm/ADT/STLExtras.h"
30#include "llvm/ADT/SmallVector.h"
31
32using namespace mlir;
33using namespace acc;
34
35namespace {
36/// Generic helper for single-region OpenACC ops that execute their body once
37/// and then continue after the operation with their results (if any).
38static void
42 if (point.isParent()) {
43 regions.push_back(RegionSuccessor(&region));
44 return;
45 }
46 regions.push_back(RegionSuccessor(op));
47}
48
50 RegionSuccessor successor) {
51 return successor.isOperation() ? ValueRange(op->getResults()) : ValueRange();
52}
53
54/// Remove empty acc.kernel_environment operations. If the operation has wait
55/// operands, create a acc.wait operation to preserve synchronization.
56struct RemoveEmptyKernelEnvironment
57 : public OpRewritePattern<acc::KernelEnvironmentOp> {
58 using OpRewritePattern<acc::KernelEnvironmentOp>::OpRewritePattern;
59
60 LogicalResult matchAndRewrite(acc::KernelEnvironmentOp op,
61 PatternRewriter &rewriter) const override {
62 assert(op->getNumRegions() == 1 && "expected op to have one region");
63
64 Block &block = op.getRegion().front();
65 if (!block.empty())
66 return failure();
67
68 // Remove empty kernel environment.
69 // Preserve synchronization by creating acc.wait operation if needed.
70 if (!op.getWaitOperands().empty() || op.getWaitOnlyAttr())
71 rewriter.replaceOpWithNewOp<acc::WaitOp>(
72 op, op.getWaitOperands(), /*asyncOperand=*/Value(),
73 op.getWaitDevnum(), /*async=*/nullptr, /*ifCond=*/Value());
74 else
75 rewriter.eraseOp(op);
76
77 return success();
78 }
79};
80
81struct KernelEnvironmentDedupDataOperands
82 : public OpRewritePattern<acc::KernelEnvironmentOp> {
83 using OpRewritePattern<acc::KernelEnvironmentOp>::OpRewritePattern;
84
85 static bool isReductionDataOperand(Value operand) {
86 Operation *def = operand.getDefiningOp();
87 if (!def)
88 return false;
89 std::optional<DataClause> clause = getDataClause(def);
90 if (!clause || *clause != DataClause::acc_reduction)
91 return false;
92 assert(isa<CopyinOp>(def) && "reduction clause must be an acc.copyin");
93 return true;
94 }
95
96 LogicalResult matchAndRewrite(acc::KernelEnvironmentOp op,
97 PatternRewriter &rewriter) const override {
98 SmallVector<Value> operands(op.getDataClauseOperands());
99 const unsigned numOperands = operands.size();
100 if (numOperands < 2)
101 return failure();
102
103 SmallVector<Value> kept;
104 SmallVector<std::pair<Value, Value>> useRewrites;
105 SmallVector<SmallVector<Value>> boundsStorage;
106 boundsStorage.reserve(numOperands);
107 llvm::DenseMap<std::pair<Value, ArrayRef<Value>>, unsigned>
108 keptIndexByVarAndBounds;
109 for (Value operand : operands) {
110 Operation *def = operand.getDefiningOp();
111 // acc.map_info is a finalized mapping. Its order and semantics stand;
112 // matching only its variable and bounds would ignore flags and
113 // descriptors.
114 if (!def || isa<MapInfoOp>(def)) {
115 kept.push_back(operand);
116 continue;
117 }
118
119 Value var = getVar(def);
120 if (!var) {
121 kept.push_back(operand);
122 continue;
123 }
124
125 boundsStorage.push_back(getBounds(def));
126 auto [it, inserted] = keptIndexByVarAndBounds.try_emplace(
127 std::make_pair(var, ArrayRef<Value>(boundsStorage.back())),
128 kept.size());
129 if (inserted) {
130 kept.push_back(operand);
131 continue;
132 }
133
134 Value &survivor = kept[it->second];
135 // A reduction acc.copyin is that copyin plus the reduction. Keeping
136 // another clause would drop the reduction.
137 if (isReductionDataOperand(operand) && !isReductionDataOperand(survivor))
138 std::swap(survivor, operand);
139 useRewrites.emplace_back(operand, survivor);
140 }
141 if (kept.size() == numOperands)
142 return failure();
143
144 for (auto [from, to] : useRewrites) {
145 rewriter.replaceUsesWithIf(from, to, [&](OpOperand &use) {
146 return op->isProperAncestor(use.getOwner());
147 });
148 }
149 rewriter.modifyOpInPlace(
150 op, [&] { op.getDataClauseOperandsMutable().assign(kept); });
151 return success();
152 }
153};
154
155// Capture `hasStream` before erasing inputs because segment metadata is only
156// repaired at the end of the modification transaction.
157static void setComputeRegionInputOperandSegments(ComputeRegionOp op,
158 PatternRewriter &rewriter,
159 size_t numInput,
160 bool hasStream) {
161 const size_t numLaunch = op.getLaunchArgs().size();
162 op->setInherentAttr(
163 rewriter.getStringAttr(ComputeRegionOp::getOperandSegmentSizeAttr()),
164 rewriter.getDenseI32ArrayAttr({static_cast<int32_t>(numLaunch),
165 static_cast<int32_t>(numInput),
166 hasStream ? 1 : 0}));
167}
168
169struct ComputeRegionRemoveDuplicateArgs
170 : public OpRewritePattern<ComputeRegionOp> {
171 using Base::Base;
172
173 LogicalResult matchAndRewrite(ComputeRegionOp op,
174 PatternRewriter &rewriter) const override {
175 Block *body = op.getBody();
176 const size_t numLaunch = op.getLaunchArgs().size();
177 size_t numInput = op.getInputArgs().size();
178 assert(body->getNumArguments() == numLaunch + numInput &&
179 "region args mismatch");
180
181 bool hasDuplicate = false;
182 for (size_t j = 1; j < numInput && !hasDuplicate; ++j)
183 for (size_t i = 0; i < j; ++i)
184 if (op->getOperand(static_cast<unsigned>(numLaunch + i)) ==
185 op->getOperand(static_cast<unsigned>(numLaunch + j))) {
186 hasDuplicate = true;
187 break;
188 }
189 if (!hasDuplicate)
190 return failure();
191
192 const bool hasStream = static_cast<bool>(op.getStream());
193 rewriter.modifyOpInPlace(op, [&] {
194 while (true) {
195 bool merged = false;
196 for (size_t j = 1; j < numInput && !merged; ++j) {
197 for (size_t i = 0; i < j; ++i) {
198 if (op->getOperand(static_cast<unsigned>(numLaunch + i)) !=
199 op->getOperand(static_cast<unsigned>(numLaunch + j)))
200 continue;
201 unsigned keepIdx = static_cast<unsigned>(numLaunch + i);
202 unsigned dropIdx = static_cast<unsigned>(numLaunch + j);
203 rewriter.replaceAllUsesWith(body->getArgument(dropIdx),
204 body->getArgument(keepIdx));
205 body->eraseArgument(dropIdx);
206 op->eraseOperand(dropIdx);
207 --numInput;
208 merged = true;
209 break;
210 }
211 }
212 if (!merged)
213 break;
214 }
215 setComputeRegionInputOperandSegments(op, rewriter, numInput, hasStream);
216 });
217 return success();
218 }
219};
220
221struct ComputeRegionRemoveUnusedArgs
222 : public OpRewritePattern<ComputeRegionOp> {
223 using Base::Base;
224
225 LogicalResult matchAndRewrite(ComputeRegionOp op,
226 PatternRewriter &rewriter) const override {
227 Block *body = op.getBody();
228 const size_t numLaunch = op.getLaunchArgs().size();
229 size_t numInput = op.getInputArgs().size();
230 assert(body->getNumArguments() == numLaunch + numInput &&
231 "region args mismatch");
232
233 bool hasUnused = false;
234 for (size_t k = numLaunch; k < numLaunch + numInput; ++k)
235 if (body->getArgument(static_cast<unsigned>(k)).use_empty()) {
236 hasUnused = true;
237 break;
238 }
239 if (!hasUnused)
240 return failure();
241
242 const bool hasStream = static_cast<bool>(op.getStream());
243 rewriter.modifyOpInPlace(op, [&] {
244 for (size_t k = numLaunch; k < numLaunch + numInput;) {
245 if (!body->getArgument(static_cast<unsigned>(k)).use_empty()) {
246 ++k;
247 continue;
248 }
249 body->eraseArgument(static_cast<unsigned>(k));
250 op->eraseOperand(static_cast<unsigned>(k));
251 --numInput;
252 }
253 setComputeRegionInputOperandSegments(op, rewriter, numInput, hasStream);
254 });
255 return success();
256 }
257};
258
259template <typename EffectTy>
260static void addOperandEffect(
262 &effects,
263 const MutableOperandRange &operand) {
264 for (unsigned i = 0, e = operand.size(); i < e; ++i)
265 effects.emplace_back(EffectTy::get(), &operand[i]);
266}
267
268template <typename EffectTy>
269static void addResultEffect(
271 &effects,
272 Value result) {
273 effects.emplace_back(EffectTy::get(), mlir::cast<mlir::OpResult>(result));
274}
275
276static int64_t gpuProcessorIndex(gpu::Processor p) {
277 switch (p) {
278 case gpu::Processor::Sequential:
279 return 0;
280 case gpu::Processor::ThreadX:
281 return 1;
282 case gpu::Processor::ThreadY:
283 return 2;
284 case gpu::Processor::ThreadZ:
285 return 3;
286 case gpu::Processor::BlockX:
287 return 4;
288 case gpu::Processor::BlockY:
289 return 5;
290 case gpu::Processor::BlockZ:
291 return 6;
292 }
293 llvm_unreachable("unhandled gpu::Processor");
294}
295
296static gpu::Processor indexToGpuProcessor(int64_t idx) {
297 switch (idx) {
298 case 0:
299 return gpu::Processor::Sequential;
300 case 1:
301 return gpu::Processor::ThreadX;
302 case 2:
303 return gpu::Processor::ThreadY;
304 case 3:
305 return gpu::Processor::ThreadZ;
306 case 4:
307 return gpu::Processor::BlockX;
308 case 5:
309 return gpu::Processor::BlockY;
310 case 6:
311 return gpu::Processor::BlockZ;
312 default:
313 return gpu::Processor::Sequential;
314 }
315}
316
317static GPUParallelDimAttr intToParDim(MLIRContext *context, int64_t dimInt) {
318 return GPUParallelDimAttr::get(
319 context, IntegerAttr::get(IndexType::get(context), dimInt));
320}
321
322static GPUParallelDimAttr processorParDim(MLIRContext *context,
323 gpu::Processor proc) {
324 return GPUParallelDimAttr::get(
325 context,
326 IntegerAttr::get(IndexType::get(context), gpuProcessorIndex(proc)));
327}
328
329static ParseResult parseProcessorValue(AsmParser &parser,
330 GPUParallelDimAttr &dim) {
331 std::string keyword;
332 llvm::SMLoc loc = parser.getCurrentLocation();
333 if (failed(parser.parseKeywordOrString(&keyword)))
334 return failure();
335 auto maybeProcessor = gpu::symbolizeProcessor(keyword);
336 if (!maybeProcessor)
337 return parser.emitError(loc)
338 << "expected one of ::mlir::gpu::Processor enum names";
339 dim = intToParDim(parser.getContext(), gpuProcessorIndex(*maybeProcessor));
340 return success();
341}
342
343static void printProcessorValue(AsmPrinter &printer,
344 const GPUParallelDimAttr &attr) {
345 gpu::Processor processor = indexToGpuProcessor(attr.getValue().getInt());
346 printer << gpu::stringifyProcessor(processor);
347}
348
349static FailureOr<SmallVector<GPUParallelDimAttr>>
350parseGPUParallelDimList(AsmParser &parser) {
352 auto parseParDim = [&]() -> ParseResult {
353 GPUParallelDimAttr dim;
354 if (parseProcessorValue(parser, dim))
355 return failure();
356 parDims.push_back(dim);
357 return success();
358 };
360 "list of OpenACC GPU parallel dimensions"))
361 return failure();
362 return parDims;
363}
364
365static void printGPUParallelDimList(AsmPrinter &printer,
367 printer << "[";
368 llvm::interleaveComma(dims, printer, [&printer](const GPUParallelDimAttr &p) {
369 printProcessorValue(printer, p);
370 });
371 printer << "]";
372}
373} // namespace
374
375//===----------------------------------------------------------------------===//
376// KernelEnvironmentOp
377//===----------------------------------------------------------------------===//
378
379void KernelEnvironmentOp::getSuccessorRegions(
381 getSingleRegionOpSuccessorRegions(getOperation(), getRegion(), point,
382 regions);
383}
384
385ValueRange KernelEnvironmentOp::getSuccessorInputs(RegionSuccessor successor) {
386 return getSingleRegionSuccessorInputs(getOperation(), successor);
387}
388
389void KernelEnvironmentOp::getCanonicalizationPatterns(
390 RewritePatternSet &results, MLIRContext *context) {
391 results.add<RemoveEmptyKernelEnvironment, KernelEnvironmentDedupDataOperands>(
392 context);
393}
394
395/// Extract async for `clauseDeviceType`. Returns true if a clause was found.
396template <typename ComputeConstructT>
397static bool
398extractAsyncClause(ComputeConstructT computeConstruct,
399 DeviceType clauseDeviceType, MLIRContext *context,
400 std::optional<Value> &asyncOperand, UnitAttr &asyncOnly) {
401 if (computeConstruct.hasAsyncOnly(clauseDeviceType)) {
402 asyncOnly = UnitAttr::get(context);
403 return true;
404 }
405 if (Value asyncValue = computeConstruct.getAsyncValue(clauseDeviceType)) {
406 asyncOperand = asyncValue;
407 return true;
408 }
409 return false;
410}
411
412/// Extract wait for `clauseDeviceType`. Returns true if a clause was found.
413template <typename ComputeConstructT>
414static bool extractWaitClause(ComputeConstructT computeConstruct,
415 DeviceType clauseDeviceType, MLIRContext *context,
416 std::optional<Value> &waitDevnum,
417 SmallVectorImpl<Value> &waitOperands,
418 UnitAttr &waitOnly) {
419 if (computeConstruct.hasWaitOnly(clauseDeviceType)) {
420 waitOnly = UnitAttr::get(context);
421 return true;
422 }
423 Value devnum = computeConstruct.getWaitDevnum(clauseDeviceType);
424 auto waitValues = computeConstruct.getWaitValues(clauseDeviceType);
425 if (!devnum && waitValues.empty())
426 return false;
427 if (devnum)
428 waitDevnum = devnum;
429 waitOperands.append(waitValues.begin(), waitValues.end());
430 return true;
431}
432
433template <typename ComputeConstructT>
435 ComputeConstructT computeConstruct, DeviceType deviceType,
436 std::optional<Value> &asyncOperand, UnitAttr &asyncOnly,
437 std::optional<Value> &waitDevnum, SmallVectorImpl<Value> &waitOperands,
438 UnitAttr &waitOnly) {
439 MLIRContext *context = computeConstruct->getContext();
440
441 // Prefer device_type-specific clauses, then default ones.
442 if (!extractAsyncClause(computeConstruct, deviceType, context, asyncOperand,
443 asyncOnly)) {
444 if (deviceType != DeviceType::None)
445 extractAsyncClause(computeConstruct, DeviceType::None, context,
446 asyncOperand, asyncOnly);
447 }
448
449 if (!extractWaitClause(computeConstruct, deviceType, context, waitDevnum,
450 waitOperands, waitOnly)) {
451 if (deviceType != DeviceType::None)
452 extractWaitClause(computeConstruct, DeviceType::None, context, waitDevnum,
453 waitOperands, waitOnly);
454 }
455}
456
457template <typename ComputeConstructT>
458KernelEnvironmentOp
459KernelEnvironmentOp::createAndPopulate(ComputeConstructT computeConstruct,
460 DeviceType deviceType,
461 OpBuilder &builder) {
462 std::optional<Value> asyncOperand;
463 UnitAttr asyncOnly = nullptr;
464 std::optional<Value> waitDevnum;
465 SmallVector<Value> waitOperands;
466 UnitAttr waitOnly = nullptr;
467 populateKernelEnvironmentAsyncWait(computeConstruct, deviceType, asyncOperand,
468 asyncOnly, waitDevnum, waitOperands,
469 waitOnly);
470
471 auto kernelEnvironment = KernelEnvironmentOp::create(
472 builder, computeConstruct->getLoc(),
473 computeConstruct.getDataClauseOperands(), asyncOperand.value_or(Value()),
474 asyncOnly, waitDevnum.value_or(Value()), waitOperands, waitOnly);
475 Block &block = kernelEnvironment.getRegion().emplaceBlock();
476 builder.setInsertionPointToStart(&block);
477 return kernelEnvironment;
478}
479
480template KernelEnvironmentOp
481KernelEnvironmentOp::createAndPopulate<ParallelOp>(ParallelOp, DeviceType,
482 OpBuilder &);
483template KernelEnvironmentOp
484KernelEnvironmentOp::createAndPopulate<KernelsOp>(KernelsOp, DeviceType,
485 OpBuilder &);
486template KernelEnvironmentOp
487KernelEnvironmentOp::createAndPopulate<SerialOp>(SerialOp, DeviceType,
488 OpBuilder &);
489
490LogicalResult KernelEnvironmentOp::verify() {
491 if (getAsyncOnly() && getAsyncOperand())
492 return emitError("async-only cannot appear with async operand");
493 if (getWaitOnly() && (!getWaitOperands().empty() || getWaitDevnum()))
494 return emitError("wait-only cannot appear with wait operands or devnum");
495 if ((*this)->getParentOfType<KernelEnvironmentOp>())
496 return emitError(
497 "must not be nested within another acc.kernel_environment");
498 return success();
499}
500
501//===----------------------------------------------------------------------===//
502// FirstprivateMapInitialOp
503//===----------------------------------------------------------------------===//
504
505LogicalResult FirstprivateMapInitialOp::verify() {
506 if (getDataClause() != acc::DataClause::acc_firstprivate)
507 return emitError("data clause associated with firstprivate operation must "
508 "match its intent");
509 if (!getVar())
510 return emitError("must have var operand");
511 if (!mlir::isa<mlir::acc::PointerLikeType>(getVar().getType()) &&
512 !mlir::isa<mlir::acc::MappableType>(getVar().getType()))
513 return emitError("var must be mappable or pointer-like");
514 if (mlir::isa<mlir::acc::PointerLikeType>(getVar().getType()) &&
515 getVarType() == getVar().getType())
516 return emitError("varType must capture the element type of var");
517 if (getModifiers() != acc::DataClauseModifier::none)
518 return emitError("no data clause modifiers are allowed");
519 return success();
520}
521
522void FirstprivateMapInitialOp::getEffects(
524 &effects) {
525 effects.emplace_back(MemoryEffects::Read::get(),
527 addOperandEffect<MemoryEffects::Read>(effects, getVarMutable());
529}
530
531//===----------------------------------------------------------------------===//
532// ReductionInitOp
533//===----------------------------------------------------------------------===//
534
535void ReductionInitOp::getSuccessorRegions(
537 getSingleRegionOpSuccessorRegions(getOperation(), getRegion(), point,
538 regions);
539}
540
541void ReductionInitOp::getRegionInvocationBounds(
542 ArrayRef<Attribute> operands,
543 SmallVectorImpl<InvocationBounds> &invocationBounds) {
544 invocationBounds.emplace_back(1, 1);
545}
546
547ValueRange ReductionInitOp::getSuccessorInputs(RegionSuccessor successor) {
548 return getSingleRegionSuccessorInputs(getOperation(), successor);
549}
550
551LogicalResult ReductionInitOp::verify() {
552 Block &block = getRegion().front();
553 if (auto yieldOp = dyn_cast<acc::YieldOp>(block.getTerminator())) {
554 if (yieldOp.getNumOperands() != 1)
555 return emitOpError(
556 "region must yield exactly one value (private storage)");
557 if (yieldOp.getOperand(0).getType() != getVar().getType())
558 return emitOpError("yielded value type must match var type");
559 }
560 return success();
561}
562
563//===----------------------------------------------------------------------===//
564// ReductionCombineRegionOp
565//===----------------------------------------------------------------------===//
566
567void ReductionCombineRegionOp::getSuccessorRegions(
569 getSingleRegionOpSuccessorRegions(getOperation(), getRegion(), point,
570 regions);
571}
572
573void ReductionCombineRegionOp::getRegionInvocationBounds(
574 ArrayRef<Attribute> operands,
575 SmallVectorImpl<InvocationBounds> &invocationBounds) {
576 invocationBounds.emplace_back(1, 1);
577}
578
580ReductionCombineRegionOp::getSuccessorInputs(RegionSuccessor successor) {
581 return getSingleRegionSuccessorInputs(getOperation(), successor);
582}
583
584LogicalResult ReductionCombineRegionOp::verify() {
585 Block &block = getRegion().front();
586 if (auto yieldOp = dyn_cast<acc::YieldOp>(block.getTerminator())) {
587 if (yieldOp.getNumOperands() != 0)
588 return emitOpError("region must be terminated by acc.yield with no "
589 "operands");
590 }
591 return success();
592}
593
594//===----------------------------------------------------------------------===//
595// ReductionAccumulateOp
596//===----------------------------------------------------------------------===//
597
598LogicalResult ReductionAccumulateOp::verify() {
599 Type valueType = getValue().getType();
600 auto ptrLikeTy = cast<PointerLikeType>(getMemref().getType());
601 Type elementType = ptrLikeTy.getElementType();
602 if (!elementType)
603 return emitOpError("pointer-like destination must have an element type");
604 if (elementType != valueType)
605 return emitOpError("pointer-like element type must match value type");
606 if (getParDims().getArray().empty())
607 return emitOpError("par_dims must specify at least one parallel dimension");
608 return success();
609}
610
611//===----------------------------------------------------------------------===//
612// ReductionAccumulateArrayOp
613//===----------------------------------------------------------------------===//
614
615LogicalResult ReductionAccumulateArrayOp::verify() {
616 if (getParDims().getArray().empty())
617 return emitOpError("par_dims must specify at least one parallel dimension");
618 return success();
619}
620
621//===----------------------------------------------------------------------===//
622// ReductionCombineOp
623//===----------------------------------------------------------------------===//
624
625void ReductionCombineOp::getEffects(
627 &effects) {
628 effects.emplace_back(MemoryEffects::Read::get(), &getSrcMemrefMutable(),
630 effects.emplace_back(MemoryEffects::Read::get(), &getDestMemrefMutable(),
632 effects.emplace_back(MemoryEffects::Write::get(), &getDestMemrefMutable(),
634}
635
636//===----------------------------------------------------------------------===//
637// ComputeRegionOp
638//===----------------------------------------------------------------------===//
639
640static ParWidthOp getParWidthOpForLaunchArg(ComputeRegionOp op,
641 GPUParallelDimAttr parDim) {
642 for (auto launchArg : op.getLaunchArgs()) {
643 auto parOp = launchArg.getDefiningOp<ParWidthOp>();
644 if (!parOp)
645 continue;
646 auto launchArgDim = cast<GPUParallelDimAttr>(parOp.getParDim());
647 if (launchArgDim == parDim)
648 return parOp;
649 }
650 return nullptr;
651}
652
653std::optional<Value> ComputeRegionOp::getLaunchArg(GPUParallelDimAttr parDim) {
654 if (auto parWidthOp = getParWidthOpForLaunchArg(*this, parDim))
655 return parWidthOp.getResult();
656 return {};
657}
658
659std::optional<Value>
660ComputeRegionOp::getKnownLaunchArg(GPUParallelDimAttr parDim) {
661 if (auto parWidthOp = getParWidthOpForLaunchArg(*this, parDim))
662 if (parWidthOp.getLaunchArg())
663 return parWidthOp.getLaunchArg();
664 return {};
665}
666
667std::optional<uint64_t>
668ComputeRegionOp::getKnownConstantLaunchArg(GPUParallelDimAttr parDim) {
669 auto knownParWidth = getKnownLaunchArg(parDim);
670 if (!knownParWidth.has_value())
671 return {};
672 std::optional<int64_t> width = getConstantIntValue(knownParWidth.value());
673 if (!width || *width < 0)
674 return {};
675 return static_cast<uint64_t>(*width);
676}
677
678BlockArgument ComputeRegionOp::appendInputArg(Value value) {
679 getInputArgsMutable().append(value);
680 return getBody()->addArgument(value.getType(), getLoc());
681}
682
683std::optional<BlockArgument>
684ComputeRegionOp::wireHoistedValueThroughIns(Value value) {
685 Region &region = getRegion();
686
687 auto useIsInRegion = [&](OpOperand &use) -> bool {
688 return region.isAncestor(use.getOwner()->getParentRegion());
689 };
690
691 if (!areValuesDefinedAbove(ValueRange(value), region) ||
692 !llvm::any_of(value.getUses(), useIsInRegion))
693 return std::nullopt;
694
695 BlockArgument arg = appendInputArg(value);
696 replaceAllUsesInRegionWith(value, arg, region);
697 return arg;
698}
699
700bool ComputeRegionOp::isEffectivelySerial() {
701 auto *ctx = getContext();
702
703 if (getLaunchArg(GPUParallelDimAttr::seqDim(ctx)))
704 return true;
705
706 auto checkDim = [&](GPUParallelDimAttr dim) -> bool {
707 auto val = getKnownConstantLaunchArg(dim);
708 return val && *val == 1;
709 };
710
711 return checkDim(GPUParallelDimAttr::threadXDim(ctx)) &&
712 checkDim(GPUParallelDimAttr::threadYDim(ctx)) &&
713 checkDim(GPUParallelDimAttr::threadZDim(ctx)) &&
714 checkDim(GPUParallelDimAttr::blockXDim(ctx)) &&
715 checkDim(GPUParallelDimAttr::blockYDim(ctx)) &&
716 checkDim(GPUParallelDimAttr::blockZDim(ctx));
717}
718
719bool ComputeRegionOp::launchesMultipleBlocks() {
720 return llvm::any_of(getLaunchParDims(), [&](GPUParallelDimAttr dim) {
721 if (!dim.isAnyBlock())
722 return false;
723 std::optional<uint64_t> width = getKnownConstantLaunchArg(dim);
724 return !width || *width != 1;
725 });
726}
727
728BlockArgument ComputeRegionOp::parDimToWidth(GPUParallelDimAttr parDim) {
729 for (auto [pos, launchArg] : llvm::enumerate(getLaunchArgs())) {
730 auto parOp = launchArg.getDefiningOp<ParWidthOp>();
731 assert(parOp);
732 auto launchArgDim = cast<GPUParallelDimAttr>(parOp.getParDim());
733 if (launchArgDim == parDim) {
734 assert(pos < getRegion().front().getNumArguments() &&
735 "launch arg position out of range");
736 return getRegion().front().getArgument(pos);
737 }
738 }
739 llvm_unreachable("attempting to get unspecified parDim");
740}
741
742SmallVector<GPUParallelDimAttr> ComputeRegionOp::getLaunchParDims() {
744 for (auto launchArg : getLaunchArgs()) {
745 auto parOp = launchArg.getDefiningOp<ParWidthOp>();
746 auto launchArgDim = cast<GPUParallelDimAttr>(parOp.getParDim());
747 int64_t dimInt = launchArgDim.getValue().getInt();
748 parDims.push_back(intToParDim(getContext(), dimInt));
749 }
750 return parDims;
751}
752
753Value ComputeRegionOp::getOperand(BlockArgument blockArg) {
754 Block *body = getBody();
755 if (blockArg.getOwner() != body)
756 return Value();
757 unsigned argNumber = blockArg.getArgNumber();
758 unsigned numLaunchArgs = getLaunchArgs().size();
759 unsigned numInputArgs = getInputArgs().size();
760 if (argNumber >= numLaunchArgs + numInputArgs)
761 return Value();
762 if (argNumber < numLaunchArgs)
763 return getLaunchArgs()[argNumber];
764 return getInputArgs()[argNumber - numLaunchArgs];
765}
766
767std::optional<BlockArgument> ComputeRegionOp::getBlockArg(Value value) {
768 Block *body = getBody();
769 for (auto [idx, launchVal] : llvm::enumerate(getLaunchArgs())) {
770 if (launchVal == value)
771 return body->getArgument(idx);
772 }
773 unsigned numLaunch = getLaunchArgs().size();
774 for (auto [idx, inputVal] : llvm::enumerate(getInputArgs())) {
775 if (inputVal == value)
776 return body->getArgument(numLaunch + idx);
777 }
778 return std::nullopt;
779}
780
781void ComputeRegionOp::getCanonicalizationPatterns(RewritePatternSet &results,
782 MLIRContext *context) {
783 results.add<ComputeRegionRemoveDuplicateArgs, ComputeRegionRemoveUnusedArgs>(
784 context);
785}
786
787BlockArgument ComputeRegionOp::gpuParWidth(gpu::Processor processor) {
788 return parDimToWidth(GPUParallelDimAttr::get(getContext(), processor));
789}
790
791LogicalResult ComputeRegionOp::verify() {
792 if (getOperation()->getParentOfType<ComputeRegionOp>())
793 return emitOpError("cannot be nested inside another acc.compute_region");
794
795 for (auto op : getLaunchArgs())
796 if (!op.getDefiningOp<acc::ParWidthOp>())
797 return emitOpError(
798 "launch arguments must be results of acc.par_width operations");
799
800 unsigned expectedBlockArgs = getLaunchArgs().size() + getInputArgs().size();
801 unsigned actualBlockArgs = getRegion().front().getNumArguments();
802 if (expectedBlockArgs != actualBlockArgs)
803 return emitOpError("expected ")
804 << expectedBlockArgs << " block arguments (launch + input), got "
805 << actualBlockArgs;
806
807 return success();
808}
809
810void ComputeRegionOp::print(OpAsmPrinter &p) {
811 ValueRange regionArgs = getBody()->getArguments();
812 ValueRange launchArgs = getLaunchArgs();
813 ValueRange inputArgs = getInputArgs();
814
815 assert(regionArgs.size() == (launchArgs.size() + inputArgs.size()) &&
816 "region args mismatch");
817
818 if (getStream())
819 p << " stream(" << getStream() << " : " << getStream().getType() << ")";
820
821 size_t i = 0;
822 if (!launchArgs.empty()) {
823 p << " launch(";
824 for (size_t j = 0; j < launchArgs.size(); ++j, ++i) {
825 p << regionArgs[i] << " = " << launchArgs[j];
826 if (j < launchArgs.size() - 1)
827 p << ", ";
828 }
829 p << ")";
830 }
831 if (!inputArgs.empty()) {
832 p << " ins(";
833 for (size_t j = 0; j < inputArgs.size(); ++j, ++i) {
834 p << regionArgs[i] << " = " << inputArgs[j];
835 if (j < inputArgs.size() - 1)
836 p << ", ";
837 }
838 p << ") : (";
839 for (size_t j = 0; j < inputArgs.size(); ++j) {
840 p << inputArgs[j].getType();
841 if (j < inputArgs.size() - 1)
842 p << ", ";
843 }
844 p << ")";
845 }
846 p.printOptionalArrowTypeList(getResultTypes());
847 p << " ";
848 p.printRegion(getRegion(), /*printEntryBlockArgs=*/false);
849 ComputeRegionOp::printProperties(getContext(), p, getProperties(),
850 /*elidedProps=*/getOperandSegmentSizeAttr());
851 p.printOptionalAttrDict((*this)->getDiscardableAttrDictionary().getValue());
852}
853
854ParseResult ComputeRegionOp::parse(OpAsmParser &parser,
856 auto &builder = parser.getBuilder();
857
859 OpAsmParser::UnresolvedOperand streamOperand;
860 Type streamType;
863 SmallVector<Type> types;
864
865 bool hasStream = false;
866 if (succeeded(parser.parseOptionalKeyword("stream"))) {
867 hasStream = true;
868 if (parser.parseLParen() || parser.parseOperand(streamOperand) ||
869 parser.parseColon() || parser.parseType(streamType) ||
870 parser.parseRParen())
871 return failure();
872 }
873
874 if (succeeded(parser.parseOptionalKeyword("launch"))) {
875 if (parser.parseAssignmentList(regionArgs, launchOperands))
876 return failure();
877 Type indexType = builder.getIndexType();
878 for (size_t i = 0; i < regionArgs.size(); ++i)
879 types.push_back(indexType);
880 }
881
882 if (succeeded(parser.parseOptionalKeyword("ins"))) {
883 if (parser.parseAssignmentList(regionArgs, inputOperands) ||
884 parser.parseColon() || parser.parseLParen() ||
885 parser.parseTypeList(types) || parser.parseRParen())
886 return failure();
887 }
888
889 if (parser.parseOptionalArrowTypeList(result.types))
890 return failure();
891
892 for (auto [iterArg, type] : llvm::zip_equal(regionArgs, types))
893 iterArg.type = type;
894
895 Region *body = result.addRegion();
896 if (parser.parseRegion(*body, regionArgs))
897 return failure();
898 ComputeRegionOp::ensureTerminator(*body, parser.getBuilder(),
899 result.location);
900
901 const size_t numLaunchOperands = launchOperands.size();
902 const size_t numInputOperands = inputOperands.size();
903 assert(numLaunchOperands + numInputOperands == regionArgs.size() &&
904 "compute region args mismatch");
905
906 DenseI32ArrayAttr operandSegmentSizes = builder.getDenseI32ArrayAttr(
907 {static_cast<int32_t>(numLaunchOperands),
908 static_cast<int32_t>(numInputOperands), hasStream ? 1 : 0});
909
910 for (size_t i = 0; i < numLaunchOperands; ++i) {
911 if (parser.resolveOperand(launchOperands[i], types[i], result.operands))
912 return failure();
913 }
914
915 for (size_t i = numLaunchOperands; i < regionArgs.size(); ++i) {
916 if (parser.resolveOperand(inputOperands[i - numLaunchOperands], types[i],
917 result.operands))
918 return failure();
919 }
920
921 if (hasStream) {
922 if (parser.resolveOperand(streamOperand, streamType, result.operands))
923 return failure();
924 }
925
926 Attribute parsedProperties;
927 if (ComputeRegionOp::genericParseProperties(parser, parsedProperties))
928 return failure();
929 auto propertyDictionary = dyn_cast_or_null<DictionaryAttr>(parsedProperties);
930 if (parsedProperties && !propertyDictionary)
931 return parser.emitError(parser.getNameLoc(),
932 "expected properties dictionary");
933
934 NamedAttrList properties(propertyDictionary ? propertyDictionary
935 : builder.getDictionaryAttr({}));
936 properties.set(ComputeRegionOp::getOperandSegmentSizeAttr(),
937 operandSegmentSizes);
938 propertyDictionary = properties.getDictionary(builder.getContext());
939 auto emitError = [&]() {
940 return mlir::emitError(result.location, "invalid properties ")
941 << propertyDictionary << " for op " << result.name.getStringRef()
942 << ": ";
943 };
944 if (failed(ComputeRegionOp::setPropertiesFromParsedAttr(
945 result.getOrAddProperties<Properties>(), propertyDictionary,
946 emitError)))
947 return failure();
948
949 auto attrsLoc = parser.getCurrentLocation();
950 if (parser.parseOptionalAttrDict(result.attributes))
951 return failure();
952 for (StringRef attrName : ComputeRegionOp::getAttributeNames()) {
953 if (result.attributes.get(attrName))
954 return parser.emitError(attrsLoc)
955 << "inherent attribute '" << attrName
956 << "' cannot be parsed from attr-dict when strict properties in "
957 "assembly format is enabled";
958 }
959
960 return success();
961}
962
963//===----------------------------------------------------------------------===//
964// GPUSharedMemoryOp
965//===----------------------------------------------------------------------===//
966
967LogicalResult GPUSharedMemoryOp::verify() {
968 if (getNumCopies() <= 0)
969 return emitOpError("num_copies must be positive");
970 if (getStaticUpperBoundBytes() <= 0)
971 return emitOpError("static_upper_bound_bytes must be positive");
972
973 bool hasScaling = static_cast<bool>(getDynamicSharedMemoryScalingBytes());
974 bool hasFixed = static_cast<bool>(getDynamicSharedMemoryFixedBytes());
975 if (hasScaling != hasFixed)
976 return emitOpError(
977 "dynamic_shared_memory_scaling_bytes and "
978 "dynamic_shared_memory_fixed_bytes must both be present or both be "
979 "absent");
980 if (auto scalingAttr = getDynamicSharedMemoryScalingBytesAttr())
981 if (scalingAttr.getValue().isNegative())
982 return emitOpError("dynamic_shared_memory_scaling_bytes must be "
983 "non-negative");
984 if (auto fixedAttr = getDynamicSharedMemoryFixedBytesAttr())
985 if (fixedAttr.getValue().isNegative())
986 return emitOpError("dynamic_shared_memory_fixed_bytes must be "
987 "non-negative");
988
989 auto resultTy = cast<MemRefType>(getResult().getType());
990 auto addrSpace =
991 dyn_cast_if_present<gpu::AddressSpaceAttr>(resultTy.getMemorySpace());
992 if (!addrSpace || addrSpace.getValue() != gpu::AddressSpace::Workgroup)
993 return emitOpError("result memref must use #gpu.address_space<workgroup>");
994
995 return success();
996}
997
998//===----------------------------------------------------------------------===//
999// PredicateRegionOp
1000//===----------------------------------------------------------------------===//
1001
1002LogicalResult PredicateRegionOp::verify() {
1003 if (getRegion().empty())
1004 return emitOpError("region needs to have at least one block");
1005 if (getRegion().front().getNumArguments() > 0)
1006 return emitOpError("region cannot have any arguments");
1007 if (!getOperation()->getParentOfType<ComputeRegionOp>())
1008 return emitOpError("must be nested within an acc.compute_region operation");
1009 return success();
1010}
1011
1012//===----------------------------------------------------------------------===//
1013// MapInfoOp
1014//===----------------------------------------------------------------------===//
1015
1016LogicalResult MapInfoOp::verify() {
1017 // A pointer-like var addresses the mapped object, so varType has to name that
1018 // object rather than the address of it.
1019 if (mlir::isa<acc::PointerLikeType>(getVar().getType()) &&
1020 getVarType() == getVar().getType())
1021 return emitOpError("varType must capture the element type of var");
1022
1023 // A descriptor operand is only meaningful together with the layout it
1024 // follows. Without a kind, consumers have no way to interpret it.
1025 if (getDesc() && getDescKind() == DataDescKind::none)
1026 return emitOpError("desc requires a descKind other than none");
1027
1028 // Bounds count elements of the mapped object, which is the OpenACC
1029 // descriptor layout, so it must be among the kinds named here.
1030 if (!getBounds().empty() &&
1031 !acc::bitEnumContainsAny(getDescKind(), DataDescKind::openacc))
1032 return emitOpError("bounds require descKind openacc");
1033
1034 // Anything below -1 has no meaning: -1 states that the size is unknown at
1035 // compile time and 0 defers it to bounds or to a descriptor.
1036 if (getSize()) {
1037 std::optional<int64_t> constantSize = getConstantIntValue(getSize());
1038 if (constantSize && *constantSize < -1)
1039 return emitOpError("size must be -1, 0, or a positive byte count");
1040 }
1041
1042 return success();
1043}
1044
1045//===----------------------------------------------------------------------===//
1046// GPUParallelDimAttr
1047//===----------------------------------------------------------------------===//
1048
1049GPUParallelDimAttr GPUParallelDimAttr::get(MLIRContext *context,
1050 gpu::Processor proc) {
1051 return processorParDim(context, proc);
1052}
1053
1054GPUParallelDimAttr GPUParallelDimAttr::seqDim(MLIRContext *context) {
1055 return processorParDim(context, gpu::Processor::Sequential);
1056}
1057
1058GPUParallelDimAttr GPUParallelDimAttr::threadXDim(MLIRContext *context) {
1059 return processorParDim(context, gpu::Processor::ThreadX);
1060}
1061
1062GPUParallelDimAttr GPUParallelDimAttr::threadYDim(MLIRContext *context) {
1063 return processorParDim(context, gpu::Processor::ThreadY);
1064}
1065
1066GPUParallelDimAttr GPUParallelDimAttr::threadZDim(MLIRContext *context) {
1067 return processorParDim(context, gpu::Processor::ThreadZ);
1068}
1069
1070GPUParallelDimAttr GPUParallelDimAttr::blockXDim(MLIRContext *context) {
1071 return processorParDim(context, gpu::Processor::BlockX);
1072}
1073
1074GPUParallelDimAttr GPUParallelDimAttr::blockYDim(MLIRContext *context) {
1075 return processorParDim(context, gpu::Processor::BlockY);
1076}
1077
1078GPUParallelDimAttr GPUParallelDimAttr::blockZDim(MLIRContext *context) {
1079 return processorParDim(context, gpu::Processor::BlockZ);
1080}
1081
1082Attribute GPUParallelDimAttr::parse(AsmParser &parser, Type type) {
1083 GPUParallelDimAttr dim;
1084 if (parser.parseLess() || parseProcessorValue(parser, dim) ||
1085 parser.parseGreater()) {
1086 parser.emitError(parser.getCurrentLocation(),
1087 "expected format `<` processor_name `>`");
1088 return {};
1089 }
1090 return dim;
1091}
1092
1093void GPUParallelDimAttr::print(AsmPrinter &printer) const {
1094 printer << "<";
1095 printProcessorValue(printer, *this);
1096 printer << ">";
1097}
1098
1099GPUParallelDimAttr GPUParallelDimAttr::threadDim(MLIRContext *context,
1100 unsigned index) {
1101 assert(index <= 2 && "thread dimension index must be 0, 1, or 2");
1102 switch (index) {
1103 case 0:
1104 return threadXDim(context);
1105 case 1:
1106 return threadYDim(context);
1107 case 2:
1108 return threadZDim(context);
1109 }
1110 llvm_unreachable("validated thread dimension index");
1111}
1112
1113GPUParallelDimAttr GPUParallelDimAttr::blockDim(MLIRContext *context,
1114 unsigned index) {
1115 assert(index <= 2 && "block dimension index must be 0, 1, or 2");
1116 switch (index) {
1117 case 0:
1118 return blockXDim(context);
1119 case 1:
1120 return blockYDim(context);
1121 case 2:
1122 return blockZDim(context);
1123 }
1124 llvm_unreachable("validated block dimension index");
1125}
1126
1127gpu::Processor GPUParallelDimAttr::getProcessor() const {
1128 return indexToGpuProcessor(getValue().getInt());
1129}
1130
1131int GPUParallelDimAttr::getOrder() const {
1132 return gpuProcessorIndex(getProcessor());
1133}
1134
1135GPUParallelDimAttr GPUParallelDimAttr::getOneHigher() const {
1136 int order = getOrder();
1137 if (order >= 6) // BlockZ is the highest
1138 return *this;
1139 return get(getContext(), indexToGpuProcessor(order + 1));
1140}
1141
1142GPUParallelDimAttr GPUParallelDimAttr::getOneLower() const {
1143 int order = getOrder();
1144 if (order <= 0) // Sequential is the lowest
1145 return *this;
1146 return get(getContext(), indexToGpuProcessor(order - 1));
1147}
1148
1149bool GPUParallelDimAttr::isSeq() const {
1150 return getProcessor() == gpu::Processor::Sequential;
1151}
1152bool GPUParallelDimAttr::isThreadX() const {
1153 return getProcessor() == gpu::Processor::ThreadX;
1154}
1155bool GPUParallelDimAttr::isThreadY() const {
1156 return getProcessor() == gpu::Processor::ThreadY;
1157}
1158bool GPUParallelDimAttr::isThreadZ() const {
1159 return getProcessor() == gpu::Processor::ThreadZ;
1160}
1161bool GPUParallelDimAttr::isBlockX() const {
1162 return getProcessor() == gpu::Processor::BlockX;
1163}
1164bool GPUParallelDimAttr::isBlockY() const {
1165 return getProcessor() == gpu::Processor::BlockY;
1166}
1167bool GPUParallelDimAttr::isBlockZ() const {
1168 return getProcessor() == gpu::Processor::BlockZ;
1169}
1170bool GPUParallelDimAttr::isAnyThread() const {
1171 return isThreadX() || isThreadY() || isThreadZ();
1172}
1173bool GPUParallelDimAttr::isAnyBlock() const {
1174 return isBlockX() || isBlockY() || isBlockZ();
1175}
1176
1177//===----------------------------------------------------------------------===//
1178// GPUParallelDimsAttr
1179//===----------------------------------------------------------------------===//
1180
1181GPUParallelDimsAttr GPUParallelDimsAttr::seq(MLIRContext *ctx) {
1182 return GPUParallelDimsAttr::get(ctx, {GPUParallelDimAttr::seqDim(ctx)});
1183}
1184
1185bool GPUParallelDimsAttr::isSeq() const {
1186 assert(!getArray().empty() && "no par_dims found");
1187 if (getArray().size() == 1) {
1188 auto parDim = dyn_cast<GPUParallelDimAttr>(getArray()[0]);
1189 assert(parDim && "expected GPUParallelDimAttr");
1190 return parDim.isSeq();
1191 }
1192 return false;
1193}
1194
1195bool GPUParallelDimsAttr::isParallel() const { return !isSeq(); }
1196
1197bool GPUParallelDimsAttr::isMultiDim() const { return getArray().size() > 1; }
1198
1199bool GPUParallelDimsAttr::hasAnyBlockLevel() const {
1200 return llvm::any_of(
1201 getArray(), [](const GPUParallelDimAttr &p) { return p.isAnyBlock(); });
1202}
1203
1204bool GPUParallelDimsAttr::hasOnlyBlockLevel() const {
1205 return !getArray().empty() &&
1206 llvm::all_of(getArray(), [](const GPUParallelDimAttr &p) {
1207 return p.isAnyBlock();
1208 });
1209}
1210
1211bool GPUParallelDimsAttr::hasOnlyThreadYLevel() const {
1212 return !getArray().empty() &&
1213 llvm::all_of(getArray(), [](const GPUParallelDimAttr &p) {
1214 return p.isThreadY();
1215 });
1216}
1217
1218bool GPUParallelDimsAttr::hasOnlyThreadXLevel() const {
1219 return !getArray().empty() &&
1220 llvm::all_of(getArray(), [](const GPUParallelDimAttr &p) {
1221 return p.isThreadX();
1222 });
1223}
1224
1225Attribute GPUParallelDimsAttr::parse(AsmParser &parser, Type type) {
1226 FailureOr<SmallVector<GPUParallelDimAttr>> parDims =
1227 parseGPUParallelDimList(parser);
1228 if (failed(parDims))
1229 return {};
1230 return GPUParallelDimsAttr::get(parser.getContext(), *parDims);
1231}
1232
1233void GPUParallelDimsAttr::print(AsmPrinter &printer) const {
1234 printGPUParallelDimList(printer, getArray());
1235}
1236
1237//===----------------------------------------------------------------------===//
1238// ActiveParDimsAttr
1239//===----------------------------------------------------------------------===//
1240
1241Attribute ActiveParDimsAttr::parse(AsmParser &parser, Type type) {
1242 FailureOr<SmallVector<GPUParallelDimAttr>> parDims =
1243 parseGPUParallelDimList(parser);
1244 if (failed(parDims))
1245 return {};
1246 return ActiveParDimsAttr::get(parser.getContext(), *parDims);
1247}
1248
1249void ActiveParDimsAttr::print(AsmPrinter &printer) const {
1250 printGPUParallelDimList(printer, getArray());
1251}
return success()
static void addOperandEffect(SmallVectorImpl< SideEffects::EffectInstance< MemoryEffects::Effect > > &effects, MutableOperandRange operand)
Helper to add an effect on an operand, referenced by its mutable range.
Definition OpenACC.cpp:1066
static void addResultEffect(SmallVectorImpl< SideEffects::EffectInstance< MemoryEffects::Effect > > &effects, Value result)
Helper to add an effect on a result value.
Definition OpenACC.cpp:1076
static void getSingleRegionOpSuccessorRegions(Operation *op, Region &region, RegionBranchPoint point, SmallVectorImpl< RegionSuccessor > &regions)
Generic helper for single-region OpenACC ops that execute their body once and then continue after the...
Definition OpenACC.cpp:144
static ValueRange getSingleRegionSuccessorInputs(Operation *op, RegionSuccessor successor)
Definition OpenACC.cpp:155
b getContext())
*if copies could not be generated due to yet unimplemented cases *copyInPlacementStart and copyOutPlacementStart in copyPlacementBlock *specify the insertion points where the incoming copies and outgoing should be inserted(the insertion happens right before the *insertion point). Since `begin` can itself be invalidated due to the memref *rewriting done from this method
static ParWidthOp getParWidthOpForLaunchArg(ComputeRegionOp op, GPUParallelDimAttr parDim)
static bool extractWaitClause(ComputeConstructT computeConstruct, DeviceType clauseDeviceType, MLIRContext *context, std::optional< Value > &waitDevnum, SmallVectorImpl< Value > &waitOperands, UnitAttr &waitOnly)
Extract wait for clauseDeviceType. Returns true if a clause was found.
static bool extractAsyncClause(ComputeConstructT computeConstruct, DeviceType clauseDeviceType, MLIRContext *context, std::optional< Value > &asyncOperand, UnitAttr &asyncOnly)
Extract async for clauseDeviceType. Returns true if a clause was found.
static void populateKernelEnvironmentAsyncWait(ComputeConstructT computeConstruct, DeviceType deviceType, std::optional< Value > &asyncOperand, UnitAttr &asyncOnly, std::optional< Value > &waitDevnum, SmallVectorImpl< Value > &waitOperands, UnitAttr &waitOnly)
This base class exposes generic asm parser hooks, usable across the various derived parsers.
@ Square
Square brackets surrounding zero or more operands.
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 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.
ParseResult parseKeywordOrString(std::string *result)
Parse a keyword or a quoted string.
virtual ParseResult parseLess()=0
Parse a '<' token.
virtual SMLoc getCurrentLocation()=0
Get the location of the next token and store it into the argument.
virtual ParseResult parseColon()=0
Parse a : token.
virtual SMLoc getNameLoc() const =0
Return the location of the original name token.
virtual ParseResult parseGreater()=0
Parse a '>' token.
virtual ParseResult parseLParen()=0
Parse a ( token.
virtual ParseResult parseType(Type &result)=0
Parse a type.
virtual ParseResult parseOptionalArrowTypeList(SmallVectorImpl< Type > &result)=0
Parse an optional arrow followed by a type list.
ParseResult parseTypeList(SmallVectorImpl< Type > &result)
Parse a type list.
This base class exposes generic asm printer hooks, usable across the various derived printers.
void printOptionalArrowTypeList(TypeRange &&types)
Print an optional arrow followed by a type list.
Attributes are known-constant values of operations.
Definition Attributes.h:25
This class represents an argument of a Block.
Definition Value.h:308
unsigned getArgNumber() const
Returns the number of this argument.
Definition Value.h:320
Block * getOwner() const
Returns the block that owns this argument.
Definition Value.h:317
Block represents an ordered list of Operations.
Definition Block.h:34
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
Operation * getTerminator()
Get the terminator operation of this block.
Definition Block.cpp:249
void eraseArgument(unsigned index)
Erase the argument at 'index' and remove it from the argument list.
Definition Block.cpp:198
DenseI32ArrayAttr getDenseI32ArrayAttr(ArrayRef< int32_t > values)
Definition Builders.cpp:171
StringAttr getStringAttr(const Twine &bytes)
Definition Builders.cpp:271
MLIRContext * getContext() const
Definition Builders.h:56
IndexType getIndexType()
Definition Builders.cpp:59
DictionaryAttr getDictionaryAttr(ArrayRef< NamedAttribute > value)
Definition Builders.cpp:112
MLIRContext is the top-level object for a collection of MLIR operations.
Definition MLIRContext.h:71
This class provides a mutable adaptor for a range of operands.
Definition ValueRange.h:119
unsigned size() const
Returns the current size of the range.
Definition ValueRange.h:157
NamedAttrList is array of NamedAttributes that tracks whether it is sorted and does some basic work t...
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.
ParseResult parseAssignmentList(SmallVectorImpl< Argument > &lhs, SmallVectorImpl< UnresolvedOperand > &rhs)
Parse a list of assignments of the form (x1 = y1, x2 = y2, ...)
virtual ParseResult resolveOperand(const UnresolvedOperand &operand, Type type, SmallVectorImpl< Value > &result)=0
Resolve an operand to an SSA value, emitting an error on failure.
virtual ParseResult parseOperand(UnresolvedOperand &result, bool allowResultNumber=true)=0
Parse a single SSA value operand name along with a result number if allowResultNumber is true.
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.
This class helps build Operations.
Definition Builders.h:210
void setInsertionPointToStart(Block *block)
Sets the insertion point to the start of the specified block.
Definition Builders.h:434
This class represents an operand of an operation.
Definition Value.h:256
Operation is the basic unit of execution within MLIR.
Definition Operation.h:87
result_range getResults()
Definition Operation.h:442
Region * getParentRegion()
Returns the region to which the instruction belongs.
Definition Operation.h:247
A special type of RewriterBase that coordinates the application of a rewrite pattern on the current I...
This class represents a point being branched from in the methods of the RegionBranchOpInterface.
bool isParent() const
Returns true if branching from the parent op.
This class represents a successor of a region.
bool isOperation() const
Return true if the successor is an operation.
This class contains a list of basic blocks and a link to the parent operation it is attached to.
Definition Region.h:26
bool isAncestor(Region *other)
Return true if this region is ancestor of the other region.
Definition Region.h:234
RewritePatternSet & add(ConstructorArg &&arg, ConstructorArgs &&...args)
Add an instance of each of the pattern types 'Ts' to the pattern list with the given arguments.
virtual void eraseOp(Operation *op)
This method erases an operation that is known to have no uses.
void modifyOpInPlace(Operation *root, CallableT &&callable)
This method is a utility wrapper around an in-place modification of an operation.
virtual void replaceAllUsesWith(Value from, Value to)
Find uses of from and replace them with to.
OpTy replaceOpWithNewOp(Operation *op, Args &&...args)
Replace the results of the given (original) op with a new op that is created without verification (re...
virtual bool replaceUsesWithIf(Value from, Value to, function_ref< bool(OpOperand &)> functor, bool *allUsesReplaced=nullptr)
Find uses of from and replace them with to if the functor returns true.
This class represents a specific instance of an effect.
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
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition Value.h:96
bool use_empty() const
Returns true if this value has no uses.
Definition Value.h:208
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
Operation * getOwner() const
Return the owner of this operand.
Definition UseDefLists.h:38
mlir::Value getAccVar(mlir::Operation *accDataClauseOp)
Used to obtain the accVar from a data clause operation.
Definition OpenACC.cpp:5073
mlir::Value getVar(mlir::Operation *accDataClauseOp)
Used to obtain the var from a data clause operation.
Definition OpenACC.cpp:5041
std::optional< mlir::acc::DataClause > getDataClause(mlir::Operation *accDataEntryOp)
Used to obtain the dataClause from a data entry operation.
Definition OpenACC.cpp:5160
mlir::SmallVector< mlir::Value > getBounds(mlir::Operation *accDataClauseOp)
Used to obtain bounds from an acc data clause operation.
Definition OpenACC.cpp:5092
Value getDesc(Operation *mapEntryOp)
Returns descriptor value from acc.map_info.
mlir::ArrayAttr getAsyncOnly(mlir::Operation *accDataClauseOp)
Returns an array of acc:DeviceTypeAttr attributes attached to an acc data clause operation,...
Definition OpenACC.cpp:5131
mlir::Type getVarType(mlir::Operation *accDataClauseOp)
Used to obtains the varType from a data clause operation which records the type of variable.
Definition OpenACC.cpp:5049
detail::InFlightRemark failed(Location loc, RemarkOpts opts)
Report an optimization remark that failed.
Definition Remarks.h:886
Include the generated interface declarations.
void replaceAllUsesInRegionWith(Value orig, Value replacement, Region &region)
Replace all uses of orig within the given region with replacement.
std::optional< int64_t > getConstantIntValue(OpFoldResult ofr)
If ofr is a constant integer or an IntegerAttr, return the integer.
Type getType(OpFoldResult ofr)
Returns the int type of the integer in ofr.
Definition Utils.cpp:311
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.
detail::DenseArrayAttrImpl< int32_t > DenseI32ArrayAttr
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...
bool areValuesDefinedAbove(Range values, Region &limit)
Check if all values in the provided range are defined above the limit region.
Definition RegionUtils.h:26
This is the representation of an operand reference.
OpRewritePattern is a wrapper around RewritePattern that allows for matching and rewriting against an...
This represents an operation in an abstracted form, suitable for use with the builder APIs.
Eliminates variable at the specified position using Fourier-Motzkin variable elimination.