MLIR 24.0.0git
ACCDataRuntime.cpp
Go to the documentation of this file.
1//===- ACCDataRuntime.cpp - Emit OpenACC data runtime arguments -*- C++ -*-===//
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// Builds the mapping argument arrays used by OpenACC data runtime entry points.
10//
11//===----------------------------------------------------------------------===//
12
15
26#include "llvm/ADT/STLExtras.h"
27#include "llvm/ADT/SmallVectorExtras.h"
28#include "llvm/Support/Debug.h"
29#include "llvm/Support/raw_ostream.h"
30
31#include <algorithm>
32
33#define DEBUG_TYPE "acc-data-runtime"
34
35using namespace mlir;
36using namespace mlir::acc;
37
38static Value getPointer(Location loc, Value value,
39 ConversionPatternRewriter &rewriter) {
40 Type ptrTy = LLVM::LLVMPointerType::get(rewriter.getContext());
41 if (!value)
42 return LLVM::ZeroOp::create(rewriter, loc, ptrTy);
43 if (value.getType() == ptrTy)
44 return value;
45
46 if (isa<PointerLikeType>(value.getType()))
47 return castPointerLikeTypeIfNeeded(rewriter, loc, value, ptrTy);
48
49 Value one = LLVM::ConstantOp::create(rewriter, loc, rewriter.getI32Type(), 1);
50 Value storage =
51 LLVM::AllocaOp::create(rewriter, loc, ptrTy, value.getType(), one);
52 LLVM::StoreOp::create(rewriter, loc, value, storage);
53 return storage;
54}
55
56/// Returns the address of a slot holding \p value, so that the runtime reads
57/// the value from memory rather than receiving it directly.
59 ConversionPatternRewriter &rewriter) {
60 Type ptrTy = LLVM::LLVMPointerType::get(rewriter.getContext());
61 Value one = LLVM::ConstantOp::create(rewriter, loc, rewriter.getI32Type(), 1);
62 Value storage =
63 LLVM::AllocaOp::create(rewriter, loc, ptrTy, value.getType(), one);
64 LLVM::StoreOp::create(rewriter, loc, value, storage);
65 return storage;
66}
67
69 ArrayRef<Value> values, RewriterBase &rewriter) {
70 Type ptrTy = LLVM::LLVMPointerType::get(rewriter.getContext());
71 Type i32Ty = rewriter.getI32Type();
72 Value count = LLVM::ConstantOp::create(rewriter, loc, i32Ty, values.size());
73 Value array =
74 LLVM::AllocaOp::create(rewriter, loc, ptrTy, elementType, count);
75 for (auto [index, value] : llvm::enumerate(values)) {
76 Value indexValue = LLVM::ConstantOp::create(rewriter, loc, i32Ty, index);
77 Value element = LLVM::GEPOp::create(rewriter, loc, ptrTy, elementType,
78 array, ValueRange{indexValue});
79 LLVM::StoreOp::create(rewriter, loc, value, element);
80 }
81 return array;
82}
83
86 ACCDataRuntimeArgs &runtimeArgs,
87 RewriterBase &rewriter) {
88 Type i32Ty = rewriter.getI32Type();
89 Type i64Ty = rewriter.getI64Type();
90 Type ptrTy = LLVM::LLVMPointerType::get(rewriter.getContext());
91 auto createArray = [&](Type elementType, Value ACCMappedObjectArgs::*field) {
92 SmallVector<Value> values =
93 llvm::map_to_vector(objects, [&](const ACCMappedObjectArgs &object) {
94 return object.*field;
95 });
96 return createACCDataArray(loc, elementType, values, rewriter);
97 };
98 // The data-call emitter states the count before the object values.
99 if (!runtimeArgs.argNum)
100 runtimeArgs.argNum =
101 LLVM::ConstantOp::create(rewriter, loc, i32Ty, objects.size());
102 runtimeArgs.argBasePtrs = createArray(ptrTy, &ACCMappedObjectArgs::base);
103 runtimeArgs.argPtrs = createArray(ptrTy, &ACCMappedObjectArgs::pointer);
104 runtimeArgs.argSizes = createArray(i64Ty, &ACCMappedObjectArgs::size);
105 runtimeArgs.argTypes = createArray(i64Ty, &ACCMappedObjectArgs::type);
106 runtimeArgs.argNames = createArray(ptrTy, &ACCMappedObjectArgs::name);
107 if (!runtimeArgs.argMappers)
108 runtimeArgs.argMappers = LLVM::ZeroOp::create(rewriter, loc, ptrTy);
109 runtimeArgs.argDescs = createArray(ptrTy, &ACCMappedObjectArgs::descriptor);
110}
111
112/// Privatized storage only ever exists on the device, so there is no host
113/// address to give. A device address names storage the runtime must not look
114/// up, and an object mapped by value has no address at all, so both are handed
115/// over in a slot the runtime reads them from. A pointer that no clause maps
116/// but the device can already access is a device address as well.
118 Value converted, MapFlags mapFlags,
119 ConversionPatternRewriter &rewriter) {
120 Type ptrTy = LLVM::LLVMPointerType::get(rewriter.getContext());
121 if (isa<PrivateType>(operand.getType()))
122 return LLVM::ZeroOp::create(rewriter, loc, ptrTy);
123 if (converted && converted.getType() == ptrTy &&
124 (bitEnumContainsAny(mapFlags, MapFlags::devptr) ||
125 (!isDataEntryOp(operand.getDefiningOp()) &&
126 isDeviceAccessibleValue(operand))))
127 return getIndirectPointer(loc, converted, rewriter);
128 return getPointer(loc, converted, rewriter);
129}
130
132 MapFlags mapFlags,
133 ConversionPatternRewriter &rewriter) {
134 Value base;
135 if (mapOp) {
136 if (Value attach = acc::getVarPtrPtr(mapOp))
137 base = remapOrSelf(attach, rewriter);
138 else if (bitEnumContainsAny(mapFlags, MapFlags::ptr_and_obj))
139 base = remapOrSelf(acc::getDesc(mapOp), rewriter);
140 }
141 return getPointer(loc, base, rewriter);
142}
143
145 OpenACCSupport &accSupport,
146 Region &globalSymbolRegion,
147 SymbolTable *symbolTable,
148 ConversionPatternRewriter &rewriter) {
149 // The runtime resolves an object that lives on the device against the
150 // symbols of the binary by this name, so it is the name the object is
151 // emitted under rather than the one the source spells.
152 VariableNameConfig nameConfig;
153 nameConfig.preferDemangledName = false;
154 std::string name = accSupport.getVariableName(operand, nameConfig);
155 if (name.empty())
156 return LLVM::ZeroOp::create(
157 rewriter, loc, LLVM::LLVMPointerType::get(rewriter.getContext()));
158 return getOrCreateGlobalString(loc, rewriter,
159 getInternalGlobalName("var_name", name), name,
160 globalSymbolRegion, symbolTable);
161}
162
163static std::optional<MapFlags> computePackedMapFlags(Operation *mapOp) {
164 if (auto mapInfo = dyn_cast<MapInfoOp>(mapOp))
165 return mapInfo.getMapFlags();
166 if (isa<ACC_DATA_ENTRY_OPS>(mapOp))
167 return computeDataClauseMapFlags(mapOp, hasAttachPoint(mapOp));
168 return std::nullopt;
169}
170
171FailureOr<Value>
173 MapFlags mapFlags, OpenACCSupport &accSupport,
174 ConversionPatternRewriter &rewriter) {
175 Operation *scope = rewriter.getInsertionBlock()->getParentOp();
176 auto module = dyn_cast<ModuleOp>(scope);
177 if (!module)
178 module = scope->getParentOfType<ModuleOp>();
179 // An object mapped by value is passed as the argument itself, so its size is
180 // the size of what is passed and not of the object it was read from. The two
181 // differ when the argument holds one field of a decomposed aggregate.
182 if (bitEnumContainsAny(mapFlags, MapFlags::literal) && converted) {
183 auto sizeAndAlignment =
184 acc::getTypeSizeAndAlignment(converted.getType(), module, &accSupport);
185 if (!sizeAndAlignment) {
186 std::string message;
187 llvm::raw_string_ostream os(message);
188 os << "unhandled type size computation for type: " << converted.getType();
189 (void)accSupport.emitNYI(loc, message);
190 return failure();
191 }
192 return constantI64(loc, sizeAndAlignment->first.getFixedValue(), rewriter);
193 }
194 Operation *mapOp = operand.getDefiningOp();
195 if (!isDataEntryOp(mapOp))
196 return constantI64(loc, 0, rewriter);
197 if (Value size = acc::getMapSize(mapOp))
198 return castToI64(loc, remapOrSelf(size, rewriter), rewriter);
199
200 Value var = acc::getVar(mapOp);
201 if (!var)
202 return constantI64(loc, 0, rewriter);
203 Type varType = acc::getVarType(mapOp);
204 if (!varType)
205 varType = var.getType();
206 // A memref is handed over with a descriptor stating its extents and element
207 // size, which is what states the size of such a mapping.
208 if (isa<MemRefType>(var.getType()) || isa<MemRefType>(varType))
209 return constantI64(loc, 0, rewriter);
210
211 // A data clause operation that was not turned into an `acc.map_info` has no
212 // size of its own, so it is sized the way that operation would have been.
213 // A size that is not statically known is left to the runtime, as an unsized
214 // mapping is.
215 int64_t size =
217 acc::getBounds(mapOp), &accSupport);
218 return constantI64(loc, std::max<int64_t>(size, 0), rewriter);
219}
220
221static Value getElementSize(Operation *mapOp, OpenACCSupport &accSupport,
222 ConversionPatternRewriter &rewriter) {
223 Location loc = mapOp->getLoc();
224 ModuleOp module = mapOp->getParentOfType<ModuleOp>();
225 if (std::optional<int64_t> size = acc::getMapElementSize(mapOp))
226 return constantI64(loc, *size, rewriter);
227
228 Type type = acc::getVarType(mapOp);
229 if (auto shaped = dyn_cast<ShapedType>(type))
230 type = shaped.getElementType();
231 if (!type)
232 return constantI64(loc, 0, rewriter);
233 if (auto sizeAndAlignment =
234 acc::getTypeSizeAndAlignment(type, module, &accSupport))
235 return constantI64(loc, sizeAndAlignment->first.getFixedValue(), rewriter);
236 return constantI64(loc, 0, rewriter);
237}
238
240 ConversionPatternRewriter &rewriter) {
241 if (!value)
242 return constantI64(loc, 0, rewriter);
243 return castToI64(loc, remapOrSelf(value, rewriter), rewriter);
244}
245
247 Type baseDescriptorType, uint32_t version,
248 ValueRange bounds, Value elementSize,
249 ConversionPatternRewriter &rewriter) {
250 MLIRContext *context = rewriter.getContext();
251 Type i8Ty = rewriter.getI8Type();
252 Type i32Ty = rewriter.getI32Type();
253 Type i64Ty = rewriter.getI64Type();
254 Type ptrTy = LLVM::LLVMPointerType::get(context);
255
256 Value versionValue = LLVM::ConstantOp::create(
257 rewriter, loc, i32Ty,
258 version | static_cast<uint32_t>(
259 getDataDescriptorKind(DataDescriptor::AccDataDescOpenACC)));
260 baseDescriptor = LLVM::InsertValueOp::create(
261 rewriter, loc, baseDescriptorType, baseDescriptor, versionValue,
263
264 Type descriptorType = getDataDescriptorType(
265 context, DataDescriptor::AccDataDescOpenACC, baseDescriptorType);
266 Value descriptor = LLVM::ZeroOp::create(rewriter, loc, descriptorType);
267 Value rank = LLVM::ConstantOp::create(rewriter, loc, i8Ty, bounds.size());
268
269 SmallVector<Value> lowerBounds;
270 SmallVector<Value> upperBounds;
271 SmallVector<Value> extents;
272 SmallVector<Value> strides;
273 SmallVector<Value> starts;
274 for (Value boundValue : bounds) {
275 auto bound = cast<DataBoundsOp>(boundValue.getDefiningOp());
276 lowerBounds.push_back(
277 getBoundValue(bound.getLowerbound(), bound.getLoc(), rewriter));
278 upperBounds.push_back(
279 getBoundValue(bound.getUpperbound(), bound.getLoc(), rewriter));
280 Value sourceExtent =
281 bound.getSourceExtent() ? bound.getSourceExtent() : bound.getExtent();
282 extents.push_back(getBoundValue(sourceExtent, bound.getLoc(), rewriter));
283 Value stride = getBoundValue(bound.getStride(), bound.getLoc(), rewriter);
284 if (!bound.getStrideInBytes())
285 stride =
286 LLVM::MulOp::create(rewriter, bound.getLoc(), stride, elementSize);
287 strides.push_back(stride);
288 starts.push_back(
289 getBoundValue(bound.getStartIdx(), bound.getLoc(), rewriter));
290 }
291
292 Value lowerBoundsArray =
293 createACCDataArray(loc, i64Ty, lowerBounds, rewriter);
294 Value upperBoundsArray =
295 createACCDataArray(loc, i64Ty, upperBounds, rewriter);
296 Value extentsArray = createACCDataArray(loc, i64Ty, extents, rewriter);
297 Value stridesArray = createACCDataArray(loc, i64Ty, strides, rewriter);
298 Value startsArray = createACCDataArray(loc, i64Ty, starts, rewriter);
299
300 using Field = AccDataDescOpenACCField;
301 auto insert = [&](Value value, Field field) {
302 descriptor = LLVM::InsertValueOp::create(
303 rewriter, loc, descriptorType, descriptor, value,
305 };
306 insert(baseDescriptor, Field::Base);
307 insert(rank, Field::Rank);
308 insert(elementSize, Field::ElementSize);
309 insert(lowerBoundsArray, Field::LowerBounds);
310 insert(upperBoundsArray, Field::UpperBounds);
311 insert(extentsArray, Field::Extents);
312 insert(stridesArray, Field::StridesInBytes);
313 insert(startsArray, Field::StartIndices);
314
315 Value one = LLVM::ConstantOp::create(rewriter, loc, i32Ty, 1);
316 Value storage =
317 LLVM::AllocaOp::create(rewriter, loc, ptrTy, descriptorType, one);
318 LLVM::StoreOp::create(rewriter, loc, descriptor, storage);
319 return storage;
320}
321
323 Location loc, MemRefType memrefType, Value convertedMemref,
324 ValueRange bounds, Value elementSize, ConversionPatternRewriter &rewriter) {
325 MLIRContext *context = rewriter.getContext();
326 Type i8Ty = rewriter.getI8Type();
327 Type i32Ty = rewriter.getI32Type();
328 Type ptrTy = LLVM::LLVMPointerType::get(context);
329 using Field = AccDataDescMemRefField;
330 Type descriptorType =
331 getDataDescriptorType(context, DataDescriptor::AccDataDescMemRef);
332 auto version = static_cast<uint32_t>(
333 getDataDescriptorKind(DataDescriptor::AccDataDescMemRef));
334 Value descriptor = LLVM::ZeroOp::create(rewriter, loc, descriptorType);
335 Value rank =
336 LLVM::ConstantOp::create(rewriter, loc, i8Ty, memrefType.getRank());
337 auto insert = [&](Value value, Field field) {
338 descriptor = LLVM::InsertValueOp::create(
339 rewriter, loc, descriptorType, descriptor, value,
341 };
342 insert(rank, Field::Rank);
343 insert(elementSize, Field::ElementSize);
344 insert(getPointer(loc, convertedMemref, rewriter), Field::MemRefDescriptor);
345 if (!bounds.empty())
346 return createACCDataDescriptor(loc, descriptor, descriptorType, version,
347 bounds, elementSize, rewriter);
348
349 insert(LLVM::ConstantOp::create(rewriter, loc, i32Ty, version),
350 Field::Version);
351 Value one = LLVM::ConstantOp::create(rewriter, loc, i32Ty, 1);
352 Value storage =
353 LLVM::AllocaOp::create(rewriter, loc, ptrTy, descriptorType, one);
354 LLVM::StoreOp::create(rewriter, loc, descriptor, storage);
355 return storage;
356}
357
359 Value convertedOperand,
360 OpenACCSupport &accSupport,
361 ConversionPatternRewriter &rewriter) {
362 Location loc = mapOp->getLoc();
363 MLIRContext *context = rewriter.getContext();
364 Type i32Ty = rewriter.getI32Type();
365 Type ptrTy = LLVM::LLVMPointerType::get(context);
366 SmallVector<Value> bounds = acc::getBounds(mapOp);
367 DataDescKind descKind = acc::getDataDescKind(mapOp);
368 // Only a descriptor stating bounds reads the element size, so it is
369 // materialized where it is used, leaving no constant behind for a mapping
370 // that needs no descriptor at all.
371 auto elementSize = [&] {
372 return getElementSize(mapOp, accSupport, rewriter);
373 };
374
375 Value var = acc::getVar(mapOp);
376 if (var && isa<MemRefType>(var.getType())) {
377 auto memrefType = cast<MemRefType>(var.getType());
379 loc, memrefType, convertedOperand, bounds, elementSize(), rewriter);
380 }
381
382 if (bitEnumContainsAny(descKind, DataDescKind::cfi)) {
383 using Field = AccDataDescCFIField;
384 Type descriptorType =
385 getDataDescriptorType(context, DataDescriptor::AccDataDescCFI);
386 auto version = static_cast<uint32_t>(
387 getDataDescriptorKind(DataDescriptor::AccDataDescCFI));
388 Value descriptor = LLVM::ZeroOp::create(rewriter, loc, descriptorType);
389 Value descriptorStorage = remapOrSelf(acc::getDesc(mapOp), rewriter);
390 if (!descriptorStorage)
391 descriptorStorage = convertedOperand;
392 descriptor = LLVM::InsertValueOp::create(
393 rewriter, loc, descriptorType, descriptor,
394 getPointer(loc, descriptorStorage, rewriter),
395 ArrayRef<int64_t>{getDataDescriptorFieldIndex(Field::CFIDescriptor)});
396 if (!bounds.empty())
397 return createACCDataDescriptor(loc, descriptor, descriptorType, version,
398 bounds, elementSize(), rewriter);
399
400 Value versionValue =
401 LLVM::ConstantOp::create(rewriter, loc, i32Ty, version);
402 descriptor = LLVM::InsertValueOp::create(
403 rewriter, loc, descriptorType, descriptor, versionValue,
405 Value one = LLVM::ConstantOp::create(rewriter, loc, i32Ty, 1);
406 Value storage =
407 LLVM::AllocaOp::create(rewriter, loc, ptrTy, descriptorType, one);
408 LLVM::StoreOp::create(rewriter, loc, descriptor, storage);
409 return storage;
410 }
411
412 if (!bounds.empty()) {
413 Type descriptorType =
414 getDataDescriptorType(context, DataDescriptor::AccDataDescGeneric);
415 auto version = static_cast<uint32_t>(
416 getDataDescriptorKind(DataDescriptor::AccDataDescGeneric));
417 Value descriptor = LLVM::ZeroOp::create(rewriter, loc, descriptorType);
418 return createACCDataDescriptor(loc, descriptor, descriptorType, version,
419 bounds, elementSize(), rewriter);
420 }
421 return LLVM::ZeroOp::create(rewriter, loc, ptrTy);
422}
423
428
430 Location loc, ValueRange mappingOperands, ValueRange convertedOperands,
431 ConversionPatternRewriter &rewriter, Region &globalSymbolRegion,
432 acc::OpenACCSupport &accSupport, const acc::ACCRuntimeCallConfig &config,
433 ACCDataRuntimeArgs &runtimeArgs, const ACCDataCallOptions &options,
434 SymbolTable *symbolTable) {
435 if (mappingOperands.size() != convertedOperands.size())
436 return failure();
437
438 runtimeArgs.ident =
439 createIdent(loc, getParentFunctionName(mappingOperands), rewriter,
440 globalSymbolRegion, config, symbolTable);
441 runtimeArgs.flags = constantI64(loc, options.flags, rewriter);
442 runtimeArgs.deviceType = constantI64(
443 loc, config.getDeviceTypeRuntimeValue(DeviceType::None), rewriter);
444 runtimeArgs.argMappers = LLVM::ZeroOp::create(
445 rewriter, loc, LLVM::LLVMPointerType::get(rewriter.getContext()));
446
447 // An aggregate passed by value is decomposed into one argument per field by
448 // the target ABI, so the runtime is told about one object per field as well.
449 // Each of them keeps the map entry of the aggregate it came from. The device
450 // address of an aggregate is returned in the slot of its first field.
451 struct MappedObject {
452 Value operand;
453 Value converted;
454 bool firstField;
455 };
456 SmallVector<MappedObject> mappedObjects;
457 for (auto [operand, converted] :
458 llvm::zip_equal(mappingOperands, convertedOperands)) {
459 auto structType = dyn_cast<LLVM::LLVMStructType>(converted.getType());
460 if (structType && !structType.isIdentified() &&
461 structType.getBody().size() > 1) {
462 for (unsigned field = 0, fields = structType.getBody().size();
463 field != fields; ++field)
464 mappedObjects.push_back(
465 {operand,
466 LLVM::ExtractValueOp::create(rewriter, loc, converted, field),
467 field == 0});
468 continue;
469 }
470 mappedObjects.push_back({operand, converted, true});
471 }
472
473 // The count precedes the values of the objects, which is where a decomposed
474 // aggregate states how many fields it became.
475 runtimeArgs.argNum = LLVM::ConstantOp::create(
476 rewriter, loc, rewriter.getI32Type(), mappedObjects.size());
477
478 constexpr unsigned noSlot = ~0u;
479 runtimeArgs.returnAddressSlots.assign(options.returnAddressOperands.size(),
480 noSlot);
482 for (auto [slot, object] : llvm::enumerate(mappedObjects)) {
483 auto [operand, converted, firstField] = object;
484 Operation *mapOp = operand.getDefiningOp();
485 if (!isDataEntryOp(mapOp))
486 return failure();
487 std::optional<MapFlags> mapFlags = computePackedMapFlags(mapOp);
488 if (!mapFlags)
489 return failure();
490 *mapFlags = config.postProcessMapFlags(mapOp, *mapFlags);
491 LLVM_DEBUG(llvm::dbgs() << "mapping " << *mapOp << "\n as "
492 << config.formatMapFlags(*mapFlags) << "\n");
493
494 Location mapLoc = mapOp->getLoc();
496 args.pointer = createACCMappedObjectPointer(mapLoc, operand, converted,
497 *mapFlags, rewriter);
498 // An object that only lives on the device has no host address to state as
499 // its base.
500 args.base =
501 bitEnumContainsAny(*mapFlags, MapFlags::device_resident)
502 ? getPointer(mapLoc, Value(), rewriter)
503 : createACCMappedObjectBase(mapLoc, mapOp, *mapFlags, rewriter);
504
505 // A local object that lives on the device is torn down when the region
506 // that declared it ends, which the runtime only does for a mapping that
507 // states it is deleted.
508 if (options.callKind == ACCDataCallKind::DataExit &&
509 bitEnumContainsAny(*mapFlags, MapFlags::device_resident)) {
510 Value var = acc::getVar(mapOp);
511 if (var &&
512 !isa_and_nonnull<AddressOfGlobalOpInterface>(var.getDefiningOp()))
513 *mapFlags = *mapFlags | MapFlags::delete_;
514 }
515
516 if (firstField) {
517 for (auto [index, returned] :
518 llvm::enumerate(options.returnAddressOperands)) {
519 if (returned != operand)
520 continue;
521 *mapFlags = *mapFlags | MapFlags::return_param;
522 runtimeArgs.returnAddressSlots[index] = slot;
523 }
524 }
525 int64_t mapType = config.getMapFlagsRuntimeValue(*mapFlags);
526
527 FailureOr<Value> size = createACCMappedObjectSize(
528 mapLoc, operand, converted, *mapFlags, accSupport, rewriter);
529 if (failed(size))
530 return failure();
531 args.size = *size;
532 args.type = constantI64(mapLoc, mapType, rewriter);
534 mapLoc, operand, accSupport, globalSymbolRegion, symbolTable, rewriter);
535 args.descriptor =
536 createACCArgumentDescriptor(mapOp, converted, accSupport, rewriter);
537 objects.push_back(args);
538 }
539 if (llvm::is_contained(runtimeArgs.returnAddressSlots, noSlot))
540 return failure();
541
542 createACCMappedObjectArrays(loc, objects, runtimeArgs, rewriter);
543 return success();
544}
545
546FailureOr<SmallVector<Value>> mlir::emitACCDataRuntimeCall(
547 Location loc, RuntimeFunction fn, ValueRange mappingOperands,
548 ValueRange convertedOperands, ConversionPatternRewriter &rewriter,
549 OpenACCSupport &accSupport, Region &globalSymbolRegion,
550 SymbolTable &symbolTable, const ACCRuntimeCallConfig &config,
551 ArrayRef<Value> trailingArguments, const ACCDataCallOptions &options) {
552 SmallVector<Value> returnedAddresses;
553 if (mappingOperands.empty())
554 return returnedAddresses;
555
556 ACCDataRuntimeArgs runtimeArgs;
557 if (failed(emitACCDataRuntimeArgs(
558 loc, mappingOperands, convertedOperands, rewriter, globalSymbolRegion,
559 accSupport, config, runtimeArgs, options, &symbolTable)))
560 return rewriter.notifyMatchFailure(
561 mappingOperands.front().getDefiningOp(),
562 "unsupported OpenACC data-clause operand");
563
564 SmallVector<Value> arguments = runtimeArgs.getCallArgs();
565 arguments.append(trailingArguments.begin(), trailingArguments.end());
566 if (failed(createRuntimeCall(loc, rewriter, globalSymbolRegion, symbolTable,
567 fn, config, arguments)))
568 return failure();
569
570 Type i32Ty = rewriter.getI32Type();
571 Type ptrTy = LLVM::LLVMPointerType::get(rewriter.getContext());
572 for (unsigned slot : runtimeArgs.returnAddressSlots) {
573 Value index = LLVM::ConstantOp::create(rewriter, loc, i32Ty, slot);
574 Value element =
575 LLVM::GEPOp::create(rewriter, loc, ptrTy, ptrTy,
576 runtimeArgs.argBasePtrs, ValueRange{index});
577 returnedAddresses.push_back(
578 LLVM::LoadOp::create(rewriter, loc, ptrTy, element));
579 }
580 return returnedAddresses;
581}
static std::optional< MapFlags > computePackedMapFlags(Operation *mapOp)
static Value getBoundValue(Value value, Location loc, ConversionPatternRewriter &rewriter)
static Value getElementSize(Operation *mapOp, OpenACCSupport &accSupport, ConversionPatternRewriter &rewriter)
static Value getPointer(Location loc, Value value, ConversionPatternRewriter &rewriter)
static Value getIndirectPointer(Location loc, Value value, ConversionPatternRewriter &rewriter)
Returns the address of a slot holding value, so that the runtime reads the value from memory rather t...
return success()
static llvm::ManagedStatic< PassManagerOptions > options
IntegerType getI64Type()
Definition Builders.cpp:73
IntegerType getI32Type()
Definition Builders.cpp:71
MLIRContext * getContext() const
Definition Builders.h:56
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition Location.h:76
MLIRContext is the top-level object for a collection of MLIR operations.
Definition MLIRContext.h:71
Operation is the basic unit of execution within MLIR.
Definition Operation.h:87
Location getLoc()
The source location the operation was defined or derived from.
Definition Operation.h:240
OpTy getParentOfType()
Return the closest surrounding parent operation that is of type 'OpTy'.
Definition Operation.h:255
This class contains a list of basic blocks and a link to the parent operation it is attached to.
Definition Region.h:26
This class coordinates the application of a rewrite on a set of IR, providing a way for clients to tr...
This class allows for representing and managing the symbol table used by operations with the 'SymbolT...
Definition SymbolTable.h:25
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
Type getType() const
Return the type of this value.
Definition Value.h:105
Location getLoc() const
Return the location of this value.
Definition Value.cpp:24
Operation * getDefiningOp() const
If this value is the result of an operation, return the operation that defines it.
Definition Value.cpp:18
Configuration for OpenACC to LLVM runtime lowering.
std::string formatMapFlags(MapFlags flags) const
Renders flags for a diagnostic as the names of the set bits and the decimal and hexadecimal encoding ...
int64_t getDeviceTypeRuntimeValue(DeviceType type) const
int64_t getMapFlagsRuntimeValue(MapFlags flags) const
MapFlags postProcessMapFlags(Operation *mapOp, MapFlags flags) const
std::string getVariableName(Value v, VariableNameConfig config={})
Get the variable name for a given value.
InFlightDiagnostic emitNYI(Location loc, const Twine &message)
Report a case that is not yet supported by the implementation.
std::optional< int64_t > getMapElementSize(Operation *mapEntryOp)
Returns element size in bytes from acc.map_info, if present.
mlir::Value getVar(mlir::Operation *accDataClauseOp)
Used to obtain the var from a data clause operation.
Definition OpenACC.cpp:5040
MapFlags computeDataClauseMapFlags(Operation *entryOp, bool ptrAndObj)
Fold enter (+ paired exit) data-clause semantics into offload map flags.
mlir::SmallVector< mlir::Value > getBounds(mlir::Operation *accDataClauseOp)
Used to obtain bounds from an acc data clause operation.
Definition OpenACC.cpp:5091
LLVM::LLVMStructType getDataDescriptorType(MLIRContext *ctx, DataDescriptor desc, Type baseType={})
Builds the LLVM type of desc in ctx.
Value getMapSize(Operation *mapEntryOp)
Returns the optional size operand from acc.map_info, or null.
DataDescKind getDataDescriptorKind(DataDescriptor desc)
Returns the descriptor kind the runtime reads from the version field of desc.
bool isDataEntryOp(mlir::Operation *op)
Used to find out whether op is a data entry operation or an acc.map_info.
Definition OpenACC.cpp:5169
Value createIdent(Location loc, StringRef functionName, OpBuilder &builder, Region &globalSymbolRegion, const ACCRuntimeCallConfig &config, SymbolTable *symbolTable=nullptr)
Returns a pointer to a constant global holding an ident_t for OpenACC runtime calls.
Value castPointerLikeTypeIfNeeded(OpBuilder &builder, Location loc, Value value, Type resultType)
Cast value to resultType via PointerLikeType::genCast when needed.
DataDescKind getDataDescKind(Operation *mapEntryOp)
Returns descriptor kind from acc.map_info, or none for other ops.
Value remapOrSelf(Value value, ConversionPatternRewriter &rewriter)
Returns the value value was converted to, or value itself when the conversion left it as it was.
RuntimeFunction
IDs for OpenACC compiler-to-runtime entry points (__tgt_acc_*).
Value getDesc(Operation *mapEntryOp)
Returns descriptor value from acc.map_info.
Value constantI64(Location loc, int64_t value, OpBuilder &builder)
Creates an i64 constant.
mlir::Value getVarPtrPtr(mlir::Operation *accDataClauseOp)
Used to obtain the varPtrPtr from a data clause operation.
Definition OpenACC.cpp:5081
std::string getInternalGlobalName(StringRef kind, StringRef detail)
Returns the name to give a global that the conversion creates to hold detail of kind,...
StringRef getParentFunctionName(Operation *op)
Returns the symbol name of the function op belongs to, or of op itself when it is a function.
int64_t getDataDescriptorFieldIndex(FieldEnum field)
The index an insert or extract of field addresses.
Value castToI64(Location loc, Value value, OpBuilder &builder)
Sign-extends or truncates value to the i64 the runtime entry points take for values like queue number...
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:5048
Value getOrCreateGlobalString(Location loc, OpBuilder &builder, StringRef name, StringRef value, Region &globalSymbolRegion, SymbolTable *symbolTable=nullptr)
Creates or reuses a null-terminated string global in globalSymbolRegion.
bool isDeviceAccessibleValue(mlir::Value val, mlir::SymbolTableCollection *symbolTables=nullptr)
Check if a value represents device data.
int64_t computeMapInfoSizeBytes(Value var, Type varType, DataDescKind descKind, ValueRange bounds, const DataLayout &dataLayout, OpenACCSupport *support=nullptr)
Compute total mapped byte size for acc.map_info.
bool hasAttachPoint(Operation *mapEntryOp)
Returns true when mapEntryOp carries an attach point (varPtrPtr).
FailureOr< LLVM::CallOp > createRuntimeCall(Location loc, OpBuilder &builder, Region &globalSymbolRegion, SymbolTable &symbolTable, RuntimeFunction fn, const ACCRuntimeCallConfig &config, ArrayRef< Value > arguments)
Declares (if needed) and returns a call to the runtime function identified by fn using the name from ...
std::optional< TypeSizeAndAlignment > getTypeSizeAndAlignment(Type ty, ModuleOp module, const DataLayout &dl, OpenACCSupport *support=nullptr, Value var={})
Returns the size and ABI alignment in bytes.
Include the generated interface declarations.
Value createACCDataArray(Location loc, Type elementType, ArrayRef< Value > values, RewriterBase &rewriter)
Materialize values as a stack-allocated LLVM array.
Value createACCDataDescriptor(Location loc, Value baseDescriptor, Type baseDescriptorType, uint32_t version, ValueRange bounds, Value elementSize, ConversionPatternRewriter &rewriter)
Wrap baseDescriptor in the OpenACC overlay that carries bounds.
Value createACCMappedObjectName(Location loc, Value operand, acc::OpenACCSupport &accSupport, Region &globalSymbolRegion, SymbolTable *symbolTable, ConversionPatternRewriter &rewriter)
The name of operand for runtime diagnostics, or a null pointer when it has none.
void createACCMappedObjectArrays(Location loc, ArrayRef< ACCMappedObjectArgs > objects, ACCDataRuntimeArgs &runtimeArgs, RewriterBase &rewriter)
Materialize the argument arrays of objects into the argNum through argDescs fields of runtimeArgs.
LogicalResult emitACCDataRuntimeArgs(Location loc, ValueRange mappingOperands, ValueRange convertedOperands, ConversionPatternRewriter &rewriter, Region &globalSymbolRegion, acc::OpenACCSupport &accSupport, const acc::ACCRuntimeCallConfig &config, ACCDataRuntimeArgs &runtimeArgs, const ACCDataCallOptions &options={}, SymbolTable *symbolTable=nullptr)
Emit the OpenACC data runtime arguments for data-clause operands.
FailureOr< SmallVector< Value > > emitACCDataRuntimeCall(Location loc, acc::RuntimeFunction fn, ValueRange mappingOperands, ValueRange convertedOperands, ConversionPatternRewriter &rewriter, acc::OpenACCSupport &accSupport, Region &globalSymbolRegion, SymbolTable &symbolTable, const acc::ACCRuntimeCallConfig &config, ArrayRef< Value > trailingArguments, const ACCDataCallOptions &options={})
Emit the call to the mapping entry point fn for mappingOperands, followed by trailingArguments.
FailureOr< Value > createACCMappedObjectSize(Location loc, Value operand, Value converted, acc::MapFlags mapFlags, acc::OpenACCSupport &accSupport, ConversionPatternRewriter &rewriter)
The size in bytes of operand, or zero where its descriptor or bounds state the extent instead.
Value createACCMappedObjectBase(Location loc, Operation *mapOp, acc::MapFlags mapFlags, ConversionPatternRewriter &rewriter)
The host pointer slot the object mapOp maps is attached to, or a null pointer when it is not attached...
Value createACCArgumentDescriptor(Operation *mapOp, Value convertedOperand, acc::OpenACCSupport &accSupport, ConversionPatternRewriter &rewriter)
Build the runtime argument descriptor for mapOp, or a null pointer when the mapping needs no descript...
Value createACCMappedObjectPointer(Location loc, Value operand, Value converted, acc::MapFlags mapFlags, ConversionPatternRewriter &rewriter)
The address of operand as the runtime expects to receive it, given its type converted value converted...
Value createACCMemRefDescriptorWrapperArg(Location loc, MemRefType memrefType, Value convertedMemref, ValueRange bounds, Value elementSize, ConversionPatternRewriter &rewriter)
Build the runtime argument descriptor wrapping an already converted memref.
What a data runtime call is asked for besides mapping its operands.
The arguments every mapping entry point of the OpenACC runtime takes, in the order they are passed.
Value argNames
Array of variable names for runtime diagnostics, null where unknown.
Value deviceType
Device type the directive applies to, in the runtime encoding.
Value argSizes
Array of object sizes in bytes; zero where the descriptor or the bounds state the extent instead.
Value argMappers
Array of user-defined mappers. OpenACC has none, so this is always null.
Value ident
Source position and enclosing function name of the directive.
Value argTypes
Array of map-type flags, in the runtime encoding of acc::MapFlags.
SmallVector< Value > getCallArgs() const
Returns the fields above in the order the entry points take them, so that a caller only appends the a...
SmallVector< unsigned > returnAddressSlots
The argBasePtrs slot the entry point writes the device address of each requested operand into,...
Value argBasePtrs
Array of pointer slots to attach a mapped object to, null when the object is not attached to anything...
Value argNum
Number of mapped objects, that is, the length of each array below.
Value argDescs
Array of descriptors stating layout and bounds, null for whole objects of a known size.
Value flags
Region flags, in the encoding of ACCDataRegionFlags.
Value argPtrs
Array of addresses of the mapped objects themselves.
The values that describe one object to the runtime, one for each argument array.
How the name of a variable is to be rendered.
bool preferDemangledName
Render the name the source language spells, which is the name a message to the user states.