MLIR 24.0.0git
ACCKernelLaunchPatterns.cpp
Go to the documentation of this file.
1//===- ACCKernelLaunchPatterns.cpp - Kernel launch to LLVM ------*- 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// Lower acc.kernel_environment and the gpu.launch_func operations in its body
10// together. The environment establishes one implicit data region. Its entry
11// call returns the device addresses that every contained kernel launch uses.
12//
13//===----------------------------------------------------------------------===//
14
18
33#include "mlir/IR/BuiltinOps.h"
34#include "llvm/ADT/DenseMap.h"
35#include "llvm/ADT/STLExtras.h"
36#include "llvm/Support/raw_ostream.h"
37
38#include <array>
39
40using namespace mlir;
41using namespace mlir::acc;
42
43static bool isDevicePtrMapping(Value operand) {
44 Operation *mapOp = operand.getDefiningOp();
45 if (!mapOp)
46 return false;
47 if (std::optional<MapFlags> flags = getMapFlags(mapOp))
48 return bitEnumContainsAny(*flags, MapFlags::devptr);
49 if (std::optional<DataClause> clause = getDataClause(mapOp))
50 return *clause == DataClause::acc_deviceptr;
51 return false;
52}
53
54static bool isDataEntryInsideEnvironment(KernelEnvironmentOp environment,
55 Value operand) {
56 Operation *mapOp = operand.getDefiningOp();
57 return isDataEntryOp(mapOp) && environment->isAncestor(mapOp);
58}
59
60/// The flags of a data clause operation that maps a kernel argument. A kernel
61/// argument only looks up the mapping its clause established, so the clauses
62/// that establish or tear down something else contribute no flag for it.
63static MapFlags getKernelDataClauseMapFlags(Operation *entryOp) {
64 bool ptrAndObj = hasAttachPoint(entryOp);
65 MapFlags ptrAndObjFlag = ptrAndObj ? MapFlags::ptr_and_obj : MapFlags::none;
66 std::optional<DataClause> clause = getDataClause(entryOp);
67 if (clause == DataClause::acc_declare_device_resident)
68 return MapFlags::present | ptrAndObjFlag;
69 if (clause == DataClause::acc_attach || clause == DataClause::acc_detach)
70 return ptrAndObjFlag;
71 return bitEnumClear(computeDataClauseMapFlags(entryOp, ptrAndObj),
72 MapFlags::device_resident);
73}
74
75/// The flags a kernel argument is passed with. An operand the environment
76/// already mapped is passed as the device address its entry call returned. An
77/// operand that no clause maps is passed by value.
78static MapFlags getKernelArgMapFlags(Value operand, bool alreadyMapped,
79 const ACCRuntimeCallConfig &config) {
80 Operation *mapOp = operand.getDefiningOp();
81 if (!isDataEntryOp(mapOp))
82 return mapOp ? config.postProcessMapFlags(mapOp, MapFlags::literal)
83 : MapFlags::literal;
84
85 MapFlags flags;
86 if (alreadyMapped)
87 flags = MapFlags::devptr | MapFlags::literal;
88 else if (auto mapInfo = dyn_cast<MapInfoOp>(mapOp))
89 flags = mapInfo.getMapFlags();
90 else
91 flags = getKernelDataClauseMapFlags(mapOp);
92 if (getImplicitFlag(mapOp))
93 flags = flags | MapFlags::implicit;
94 return config.postProcessMapFlags(mapOp, flags);
95}
96
97/// The descriptor of a kernel argument. The device address the environment
98/// returned is passed as is, since the runtime would map an address that
99/// carries a descriptor again.
100static FailureOr<Value>
102 bool alreadyMapped, OpenACCSupport &accSupport,
103 ConversionPatternRewriter &rewriter) {
104 Type ptrTy = LLVM::LLVMPointerType::get(rewriter.getContext());
105 Operation *mapOp = operand.getDefiningOp();
106 if (alreadyMapped)
107 return Value(LLVM::ZeroOp::create(rewriter, loc, ptrTy));
108 if (isDataEntryOp(mapOp))
109 return createACCArgumentDescriptor(mapOp, converted, accSupport, rewriter);
110 if (auto memrefType = dyn_cast<MemRefType>(operand.getType())) {
111 ModuleOp module = rewriter.getInsertionBlock()
112 ->getParentOp()
113 ->getParentOfType<ModuleOp>();
114 auto sizeAndAlignment = acc::getTypeSizeAndAlignment(
115 memrefType.getElementType(), module, &accSupport);
116 if (!sizeAndAlignment) {
117 std::string message;
118 llvm::raw_string_ostream os(message);
119 os << "unhandled type size computation for type: "
120 << memrefType.getElementType();
121 (void)accSupport.emitNYI(loc, message);
122 return failure();
123 }
125 loc, memrefType, converted, /*bounds=*/{},
126 constantI64(loc, sizeAndAlignment->first.getFixedValue(), rewriter),
127 rewriter);
128 }
129 return Value(LLVM::ZeroOp::create(rewriter, loc, ptrTy));
130}
131
132/// Emits the argument arrays of the kernel launched by \p launch, one entry per
133/// kernel operand. Each operand is one parameter of the kernel.
134static FailureOr<ACCDataRuntimeArgs> emitKernelRuntimeArgs(
135 gpu::LaunchFuncOp launch, gpu::LaunchFuncOpAdaptor adaptor,
136 ValueRange alreadyMappedOperands, ConversionPatternRewriter &rewriter,
137 OpenACCSupport &accSupport, Region &globalSymbolRegion,
138 SymbolTable &symbolTable, const ACCRuntimeCallConfig &config) {
139 Location loc = launch.getLoc();
141 for (auto [operand, converted] : llvm::zip_equal(
142 launch.getKernelOperands(), adaptor.getKernelOperands())) {
143 bool alreadyMapped = llvm::is_contained(alreadyMappedOperands, operand);
144 MapFlags mapFlags = getKernelArgMapFlags(operand, alreadyMapped, config);
145 Operation *mapOp = operand.getDefiningOp();
146
148 // A kernel argument states the base of the mapping it looks up, which for
149 // an object the declare directive made resident is that of its entry.
151 loc, isDataEntryOp(mapOp) ? mapOp : nullptr, mapFlags, rewriter);
152 args.pointer = createACCMappedObjectPointer(loc, operand, converted,
153 mapFlags, rewriter);
154 FailureOr<Value> size = createACCMappedObjectSize(
155 loc, operand, converted, mapFlags, accSupport, rewriter);
156 if (failed(size))
157 return failure();
158 args.size = *size;
159 args.type =
160 constantI64(loc, config.getMapFlagsRuntimeValue(mapFlags), rewriter);
162 loc, operand, accSupport, globalSymbolRegion, &symbolTable, rewriter);
163 FailureOr<Value> descriptor = getKernelArgDescriptor(
164 loc, operand, converted, alreadyMapped, accSupport, rewriter);
165 if (failed(descriptor))
166 return failure();
167 args.descriptor = *descriptor;
168 objects.push_back(args);
169 }
170
171 ACCDataRuntimeArgs runtimeArgs;
172 createACCMappedObjectArrays(loc, objects, runtimeArgs, rewriter);
173 return runtimeArgs;
174}
175
176static FailureOr<Value> createKernelArgumentStruct(
177 gpu::LaunchFuncOp launch, gpu::LaunchFuncOpAdaptor adaptor,
178 ValueRange alreadyMappedOperands, ConversionPatternRewriter &rewriter,
179 OpenACCSupport &accSupport, Region &globalSymbolRegion,
180 SymbolTable &symbolTable, const ACCRuntimeCallConfig &config) {
181 FailureOr<ACCDataRuntimeArgs> emitted = emitKernelRuntimeArgs(
182 launch, adaptor, alreadyMappedOperands, rewriter, accSupport,
183 globalSymbolRegion, symbolTable, config);
184 if (failed(emitted))
185 return failure();
186 ACCDataRuntimeArgs runtimeArgs = *emitted;
187
188 Location loc = launch.getLoc();
189 MLIRContext *context = rewriter.getContext();
190 Type i32Ty = rewriter.getI32Type();
191 Type ptrTy = LLVM::LLVMPointerType::get(context);
192 Type argsType = getKernelArgumentType(context);
193 Value args = LLVM::ZeroOp::create(rewriter, loc, argsType);
194 auto insert = [&](Value value, KernelArgumentField field) {
195 args = LLVM::InsertValueOp::create(
196 rewriter, loc, argsType, args, value,
198 };
199 insert(runtimeArgs.argNum, KernelArgumentField::ArgNum);
200 insert(runtimeArgs.argBasePtrs, KernelArgumentField::ArgBasePtrs);
201 insert(runtimeArgs.argPtrs, KernelArgumentField::ArgPtrs);
202 insert(runtimeArgs.argSizes, KernelArgumentField::ArgSizes);
203 insert(runtimeArgs.argTypes, KernelArgumentField::ArgTypes);
204 insert(runtimeArgs.argNames, KernelArgumentField::ArgNames);
205 insert(runtimeArgs.argMappers, KernelArgumentField::ArgMappers);
206 insert(runtimeArgs.argDescs, KernelArgumentField::ArgDescs);
207
208 // The vector length is the first block dimension. The worker count is the
209 // product of the remaining ones.
210 Value blockY = castToI64(loc, adaptor.getBlockSizeY(), rewriter);
211 Value blockZ = castToI64(loc, adaptor.getBlockSizeZ(), rewriter);
212 insert(LLVM::MulOp::create(rewriter, loc, blockY, blockZ),
213 KernelArgumentField::NumWorkers);
214 insert(castToI64(loc, adaptor.getBlockSizeX(), rewriter),
215 KernelArgumentField::VectorLength);
216 Value dynamicSharedMemorySize =
217 adaptor.getDynamicSharedMemorySize()
218 ? castToI64(loc, adaptor.getDynamicSharedMemorySize(), rewriter)
219 : constantI64(loc, 0, rewriter);
220 insert(dynamicSharedMemorySize, KernelArgumentField::SmemSize);
221
222 // The grid sizes are the entries of the array in the NumGangs field.
223 int64_t numGangsField =
224 getKernelArgumentFieldIndex(KernelArgumentField::NumGangs);
225 std::array<Value, 3> gridSizes = {
226 adaptor.getGridSizeX(), adaptor.getGridSizeY(), adaptor.getGridSizeZ()};
227 for (auto [dimension, gridSize] : llvm::enumerate(gridSizes))
228 args = LLVM::InsertValueOp::create(
229 rewriter, loc, argsType, args, castToI64(loc, gridSize, rewriter),
230 ArrayRef<int64_t>{numGangsField, static_cast<int64_t>(dimension)});
231
232 Value one = LLVM::ConstantOp::create(rewriter, loc, i32Ty, 1);
233 Value argsPtr = LLVM::AllocaOp::create(rewriter, loc, ptrTy, argsType, one);
234 LLVM::StoreOp::create(rewriter, loc, args, argsPtr);
235 return argsPtr;
236}
237
238static FailureOr<Value> getKernelPointer(gpu::LaunchFuncOp launch,
239 ConversionPatternRewriter &rewriter,
240 Region &globalSymbolRegion,
241 SymbolTable &symbolTable) {
242 Location loc = launch.getLoc();
243 Type ptrTy = LLVM::LLVMPointerType::get(rewriter.getContext());
244 StringRef kernelName = launch.getKernel().getLeafReference().getValue();
245 if (auto function = symbolTable.lookup<LLVM::LLVMFuncOp>(kernelName))
246 return LLVM::AddressOfOp::create(rewriter, loc, ptrTy, function.getName())
247 .getResult();
248 if (auto function = symbolTable.lookup<func::FuncOp>(kernelName))
249 return LLVM::AddressOfOp::create(rewriter, loc, ptrTy, function.getName())
250 .getResult();
251
253 launch, launch.getKernel());
254 if (!gpuFunction)
255 return failure();
256
257 {
258 OpBuilder::InsertionGuard guard(rewriter);
259 rewriter.setInsertionPointToStart(&globalSymbolRegion.front());
260 auto functionType = LLVM::LLVMFunctionType::get(
261 LLVM::LLVMVoidType::get(rewriter.getContext()), {});
262 auto declaration =
263 LLVM::LLVMFuncOp::create(rewriter, loc, kernelName, functionType);
264 symbolTable.insert(declaration);
265 }
266 return LLVM::AddressOfOp::create(rewriter, loc, ptrTy, kernelName)
267 .getResult();
268}
269
270static void declareLibc(SymbolTable &symbolTable, Region &globalSymbolRegion,
271 Location loc, StringRef name, Type result,
272 ArrayRef<Type> arguments) {
273 if (symbolTable.lookup<LLVM::LLVMFuncOp>(name))
274 return;
275 OpBuilder builder = OpBuilder::atBlockEnd(&globalSymbolRegion.front());
276 auto functionType = LLVM::LLVMFunctionType::get(result, arguments);
277 symbolTable.insert(
278 LLVM::LLVMFuncOp::create(builder, loc, name, functionType));
279}
280
281/// The kernel entry point reports a status other than success when the kernel
282/// could not run on the device. The program is then meant to run the region on
283/// the host, which is not implemented, so the failure is reported and the
284/// program stops.
286 ConversionPatternRewriter &rewriter,
287 Region &globalSymbolRegion,
288 SymbolTable &symbolTable) {
289 MLIRContext *context = rewriter.getContext();
290 Type i32Ty = rewriter.getI32Type();
291 Type ptrTy = LLVM::LLVMPointerType::get(context);
292 Type voidTy = LLVM::LLVMVoidType::get(context);
293
294 Block *preBlock = rewriter.getInsertionBlock();
295 Block *continueBlock =
296 rewriter.splitBlock(preBlock, rewriter.getInsertionPoint());
297 Block *errorBlock =
298 rewriter.createBlock(preBlock->getParent(), continueBlock->getIterator());
299 rewriter.setInsertionPointToEnd(preBlock);
300 Value success = LLVM::ConstantOp::create(
301 rewriter, loc, i32Ty, static_cast<int32_t>(RuntimeResult::Success));
302 Value failed = LLVM::ICmpOp::create(rewriter, loc, LLVM::ICmpPredicate::ne,
303 status, success);
304 LLVM::CondBrOp::create(rewriter, loc, failed, errorBlock, ValueRange{},
305 continueBlock, ValueRange{});
306
307 // TODO: Runtime API should provide a mechanism to report this error.
308 rewriter.setInsertionPointToStart(errorBlock);
309 declareLibc(symbolTable, globalSymbolRegion, loc, "puts", i32Ty, {ptrTy});
310 declareLibc(symbolTable, globalSymbolRegion, loc, "fflush", i32Ty, {ptrTy});
311 declareLibc(symbolTable, globalSymbolRegion, loc, "abort", voidTy, {});
313 loc, rewriter, getInternalGlobalName("message", "host_fallback_nyi"),
314 "host fallback execution is not implemented\n", globalSymbolRegion,
315 &symbolTable);
316 LLVM::CallOp::create(rewriter, loc, TypeRange{i32Ty},
317 SymbolRefAttr::get(context, "puts"),
318 ValueRange{message});
319 Value nullFile = LLVM::ZeroOp::create(rewriter, loc, ptrTy);
320 LLVM::CallOp::create(rewriter, loc, TypeRange{i32Ty},
321 SymbolRefAttr::get(context, "fflush"),
322 ValueRange{nullFile});
323 LLVM::CallOp::create(rewriter, loc, TypeRange{},
324 SymbolRefAttr::get(context, "abort"), ValueRange{});
325 LLVM::UnreachableOp::create(rewriter, loc);
326 rewriter.setInsertionPointToStart(continueBlock);
327}
328
329static LogicalResult
330emitKernelCall(gpu::LaunchFuncOp launch, gpu::LaunchFuncOpAdaptor adaptor,
331 Value asyncQueue, ValueRange alreadyMappedOperands,
332 ConversionPatternRewriter &rewriter, OpenACCSupport &accSupport,
333 Region &globalSymbolRegion, SymbolTable &symbolTable,
334 const ACCRuntimeCallConfig &config) {
335 Location loc = launch.getLoc();
336 FailureOr<Value> kernelPointer =
337 getKernelPointer(launch, rewriter, globalSymbolRegion, symbolTable);
338 if (failed(kernelPointer)) {
339 (void)accSupport.emitNYI(loc, "undefined kernel");
340 return failure();
341 }
342 FailureOr<Value> arguments = createKernelArgumentStruct(
343 launch, adaptor, alreadyMappedOperands, rewriter, accSupport,
344 globalSymbolRegion, symbolTable, config);
345 if (failed(arguments))
346 return failure();
347
348 StringRef kernelName = launch.getKernel().getLeafReference().getValue();
349 Value kernelNamePtr = getOrCreateGlobalString(
350 loc, rewriter, getInternalGlobalName("kernel_name", kernelName),
351 kernelName, globalSymbolRegion, &symbolTable);
352 Value ident = createIdent(loc, getParentFunctionName(launch), rewriter,
353 globalSymbolRegion, config, &symbolTable);
354 // The kernel call uses the explicit data-region flag.
355 Value flags = constantI64(loc, static_cast<int64_t>(ACCDataRegionFlags::None),
356 rewriter);
357 // The launch is made for the default device type, as the data calls of the
358 // environment are.
359 Value deviceType = constantI64(
360 loc, config.getDeviceTypeRuntimeValue(DeviceType::None), rewriter);
361 Value binaryDescriptor = config.createKernelBinaryDescriptor(loc, rewriter);
362 FailureOr<LLVM::CallOp> call =
363 createRuntimeCall(loc, rewriter, globalSymbolRegion, symbolTable,
364 RuntimeFunction::ACCRTL_tgt_acc_kernel, config,
365 {ident, *kernelPointer, flags, deviceType, *arguments,
366 asyncQueue, kernelNamePtr, binaryDescriptor});
367 if (failed(call))
368 return failure();
369 emitHostFallbackNotImplemented(loc, call->getResult(), rewriter,
370 globalSymbolRegion, symbolTable);
371 return success();
372}
373
374namespace {
375struct KernelEnvironmentLowering
376 : public ConvertOpToLLVMPattern<KernelEnvironmentOp> {
377 KernelEnvironmentLowering(const LLVMTypeConverter &converter,
378 OpenACCSupport &accSupport,
379 Region &globalSymbolRegion,
380 SymbolTable &symbolTable,
381 const ACCRuntimeCallConfig &config,
382 DeviceType clauseDeviceType,
383 PatternBenefit benefit = 1)
384 : ConvertOpToLLVMPattern<KernelEnvironmentOp>(converter, benefit),
385 accSupport(accSupport), globalSymbolRegion(globalSymbolRegion),
386 symbolTable(symbolTable), config(config),
387 clauseDeviceType(clauseDeviceType) {}
388
389 LogicalResult
390 matchAndRewrite(KernelEnvironmentOp environment, OpAdaptor adaptor,
391 ConversionPatternRewriter &rewriter) const override {
392 ValueRange mappingOperands = environment.getDataClauseOperands();
393 ValueRange convertedMappingOperands = adaptor.getDataClauseOperands();
394 Location beginLoc = mappingOperands.empty()
395 ? environment.getLoc()
396 : mappingOperands.front().getLoc();
397 Location endLoc = getMappingExitLoc(mappingOperands).value_or(beginLoc);
398 Value asyncQueue =
399 getAsyncQueue(environment, clauseDeviceType, rewriter, config);
400 if (failed(emitWaitClause(environment, clauseDeviceType, asyncQueue,
401 rewriter, accSupport, globalSymbolRegion,
402 symbolTable, config)))
403 return failure();
404
405 SmallVector<Value> alreadyMappedOperands;
406 for (Value operand : mappingOperands)
407 if (!isDevicePtrMapping(operand))
408 alreadyMappedOperands.push_back(operand);
409
410 ACCDataCallOptions beginOptions;
411 beginOptions.flags = static_cast<int64_t>(ACCDataRegionFlags::Implicit);
412 beginOptions.returnAddressOperands = alreadyMappedOperands;
413 FailureOr<SmallVector<Value>> devicePointers = emitACCDataRuntimeCall(
414 beginLoc, RuntimeFunction::ACCRTL_tgt_acc_data_begin, mappingOperands,
415 convertedMappingOperands, rewriter, accSupport, globalSymbolRegion,
416 symbolTable, config, {asyncQueue}, beginOptions);
417 if (failed(devicePointers))
418 return failure();
419
420 llvm::DenseMap<Value, Value> deviceAddress;
421 for (auto [operand, pointer] :
422 llvm::zip_equal(alreadyMappedOperands, *devicePointers))
423 deviceAddress[operand] = pointer;
424
425 SmallVector<gpu::LaunchFuncOp> launches;
426 environment.walk(
427 [&](gpu::LaunchFuncOp launch) { launches.push_back(launch); });
428 for (gpu::LaunchFuncOp launch : launches) {
429 SmallVector<Value> converted;
430 converted.reserve(launch->getNumOperands());
431 for (Value operand : launch->getOperands()) {
432 Value value;
433 // A data entry in the region is mapped by this kernel, so it is passed
434 // as the variable of its clause.
435 if (isDataEntryInsideEnvironment(environment, operand))
436 value = remapOrSelf(getVar(operand.getDefiningOp()), rewriter);
437 else
438 value = deviceAddress.lookup(operand);
439 if (!value)
440 value = rewriter.getRemappedValue(operand);
441 if (!value)
442 return rewriter.notifyMatchFailure(launch,
443 "unconverted kernel operand");
444 converted.push_back(value);
445 }
446
447 gpu::LaunchFuncOpAdaptor launchAdaptor(converted, launch);
448 Value queue = launchAdaptor.getAsyncObject();
449 queue = queue ? castToI64(launch.getLoc(), queue, rewriter) : asyncQueue;
450 rewriter.setInsertionPoint(launch);
451 if (failed(emitKernelCall(launch, launchAdaptor, queue,
452 alreadyMappedOperands, rewriter, accSupport,
453 globalSymbolRegion, symbolTable, config)))
454 return failure();
455 rewriter.eraseOp(launch);
456 }
457
458 rewriter.setInsertionPointAfter(environment);
459 ACCDataCallOptions endOptions;
460 endOptions.callKind = ACCDataCallKind::DataExit;
461 endOptions.flags = static_cast<int64_t>(ACCDataRegionFlags::Implicit);
463 endLoc, RuntimeFunction::ACCRTL_tgt_acc_data_end, mappingOperands,
464 convertedMappingOperands, rewriter, accSupport, globalSymbolRegion,
465 symbolTable, config, {asyncQueue}, endOptions)))
466 return failure();
467
468 spliceConstructRegion(environment, environment.getRegion(), rewriter);
469 rewriter.eraseOp(environment);
470 return success();
471 }
472
473 OpenACCSupport &accSupport;
474 Region &globalSymbolRegion;
475 SymbolTable &symbolTable;
476 ACCRuntimeCallConfig config;
477 DeviceType clauseDeviceType;
478};
479} // namespace
480
483 target.addIllegalOp<KernelEnvironmentOp>();
484}
485
487 RewritePatternSet &patterns,
488 OpenACCSupport &accSupport,
489 Region &globalSymbolRegion,
490 SymbolTable &symbolTable,
491 const ACCRuntimeCallConfig &config,
492 DeviceType clauseDeviceType) {
493 patterns.add<KernelEnvironmentLowering>(converter, accSupport,
494 globalSymbolRegion, symbolTable,
495 config, clauseDeviceType);
496}
static FailureOr< Value > getKernelArgDescriptor(Location loc, Value operand, Value converted, bool alreadyMapped, OpenACCSupport &accSupport, ConversionPatternRewriter &rewriter)
The descriptor of a kernel argument.
static FailureOr< Value > createKernelArgumentStruct(gpu::LaunchFuncOp launch, gpu::LaunchFuncOpAdaptor adaptor, ValueRange alreadyMappedOperands, ConversionPatternRewriter &rewriter, OpenACCSupport &accSupport, Region &globalSymbolRegion, SymbolTable &symbolTable, const ACCRuntimeCallConfig &config)
static FailureOr< ACCDataRuntimeArgs > emitKernelRuntimeArgs(gpu::LaunchFuncOp launch, gpu::LaunchFuncOpAdaptor adaptor, ValueRange alreadyMappedOperands, ConversionPatternRewriter &rewriter, OpenACCSupport &accSupport, Region &globalSymbolRegion, SymbolTable &symbolTable, const ACCRuntimeCallConfig &config)
Emits the argument arrays of the kernel launched by launch, one entry per kernel operand.
static void emitHostFallbackNotImplemented(Location loc, Value status, ConversionPatternRewriter &rewriter, Region &globalSymbolRegion, SymbolTable &symbolTable)
The kernel entry point reports a status other than success when the kernel could not run on the devic...
static bool isDevicePtrMapping(Value operand)
static FailureOr< Value > getKernelPointer(gpu::LaunchFuncOp launch, ConversionPatternRewriter &rewriter, Region &globalSymbolRegion, SymbolTable &symbolTable)
static MapFlags getKernelDataClauseMapFlags(Operation *entryOp)
The flags of a data clause operation that maps a kernel argument.
static void declareLibc(SymbolTable &symbolTable, Region &globalSymbolRegion, Location loc, StringRef name, Type result, ArrayRef< Type > arguments)
static bool isDataEntryInsideEnvironment(KernelEnvironmentOp environment, Value operand)
static MapFlags getKernelArgMapFlags(Value operand, bool alreadyMapped, const ACCRuntimeCallConfig &config)
The flags a kernel argument is passed with.
static LogicalResult emitKernelCall(gpu::LaunchFuncOp launch, gpu::LaunchFuncOpAdaptor adaptor, Value asyncQueue, ValueRange alreadyMappedOperands, ConversionPatternRewriter &rewriter, OpenACCSupport &accSupport, Region &globalSymbolRegion, SymbolTable &symbolTable, const ACCRuntimeCallConfig &config)
return success()
Block represents an ordered list of Operations.
Definition Block.h:34
Region * getParent() const
Provide a 'getParent' method for ilist_node_with_parent methods.
Definition Block.cpp:27
Utility class for operation conversions targeting the LLVM dialect that match exactly one source oper...
Definition Pattern.h:233
Conversion from types to the LLVM IR dialect.
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
RAII guard to reset the insertion point of the builder when destroyed.
Definition Builders.h:351
This class helps build Operations.
Definition Builders.h:210
static OpBuilder atBlockEnd(Block *block, Listener *listener=nullptr)
Create a builder and set the insertion point to after the last operation in the block but still insid...
Definition Builders.h:249
Operation is the basic unit of execution within MLIR.
Definition Operation.h:87
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
RewritePatternSet & add(ConstructorArg &&arg, ConstructorArgs &&...args)
Add an instance of each of the pattern types 'Ts' to the pattern list with the given arguments.
This class allows for representing and managing the symbol table used by operations with the 'SymbolT...
Definition SymbolTable.h:25
Operation * lookup(StringRef name) const
Look up a symbol with the specified name, returning null if no such name exists.
static Operation * lookupNearestSymbolFrom(Operation *from, StringAttr symbol)
Returns the operation registered with the given symbol name within the closest parent operation of,...
StringAttr insert(Operation *symbol, Block::iterator insertPt={})
Insert a new symbol into the table, and rename it as necessary to avoid collisions.
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
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
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.
int64_t getDeviceTypeRuntimeValue(DeviceType type) const
int64_t getMapFlagsRuntimeValue(MapFlags flags) const
MapFlags postProcessMapFlags(Operation *mapOp, MapFlags flags) const
Value createKernelBinaryDescriptor(Location loc, OpBuilder &builder) const
InFlightDiagnostic emitNYI(Location loc, const Twine &message)
Report a case that is not yet supported by the implementation.
mlir::Value getVar(mlir::Operation *accDataClauseOp)
Used to obtain the var from a data clause operation.
Definition OpenACC.cpp:5040
std::optional< Location > getMappingExitLoc(ValueRange dataClauseOperands)
Returns where the mappings of dataClauseOperands end, taken from the first of them that says.
std::optional< mlir::acc::DataClause > getDataClause(mlir::Operation *accDataEntryOp)
Used to obtain the dataClause from a data entry operation.
Definition OpenACC.cpp:5159
MapFlags computeDataClauseMapFlags(Operation *entryOp, bool ptrAndObj)
Fold enter (+ paired exit) data-clause semantics into offload map flags.
int64_t getKernelArgumentFieldIndex(KernelArgumentField field)
The index an insert of field addresses.
LLVM::LLVMStructType getKernelArgumentType(MLIRContext *ctx)
The LLVM type of the argument struct passed to __tgt_acc_kernel.
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
LogicalResult emitWaitClause(OpTy op, DeviceType deviceType, Value asyncQueue, ConversionPatternRewriter &rewriter, OpenACCSupport &accSupport, Region &globalSymbolRegion, SymbolTable &symbolTable, const ACCRuntimeCallConfig &config)
Emits the wait that a wait clause on op asks for before the runtime calls of the construct,...
Value getAsyncQueue(Location loc, Value asyncOperand, bool asyncOnly, OpBuilder &builder, const ACCRuntimeCallConfig &config)
Returns the queue an async clause selects: the value of the clause when it has one,...
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.
bool getImplicitFlag(mlir::Operation *accDataEntryOp)
Used to find out whether data operation is implicit.
Definition OpenACC.cpp:5173
Value remapOrSelf(Value value, ConversionPatternRewriter &rewriter)
Returns the value value was converted to, or value itself when the conversion left it as it was.
Value constantI64(Location loc, int64_t value, OpBuilder &builder)
Creates an i64 constant.
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.
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...
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.
std::optional< MapFlags > getMapFlags(Operation *mapEntryOp)
Returns offload map-type flags from acc.map_info, if present.
KernelArgumentField
Fields of the argument struct passed to __tgt_acc_kernel, in declaration order.
bool hasAttachPoint(Operation *mapEntryOp)
Returns true when mapEntryOp carries an attach point (varPtrPtr).
void spliceConstructRegion(Operation *op, Region &region, RewriterBase &rewriter)
Splices region, the body of a structured construct, into the block holding op, so that the construct ...
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.
detail::InFlightRemark failed(Location loc, RemarkOpts opts)
Report an optimization remark that failed.
Definition Remarks.h:886
Include the generated interface declarations.
void configureACCKernelLaunchConversionLegality(ConversionTarget &target)
Configure conversion legality for acc.kernel_environment.
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.
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...
void populateACCKernelLaunchPatterns(LLVMTypeConverter &converter, RewritePatternSet &patterns, acc::OpenACCSupport &accSupport, Region &globalSymbolRegion, SymbolTable &symbolTable, const acc::ACCRuntimeCallConfig &config={}, acc::DeviceType clauseDeviceType=acc::DeviceType::None)
Populate patterns that lower an acc.kernel_environment and all gpu.launch_func operations in its body...
Value createACCMemRefDescriptorWrapperArg(Location loc, MemRefType memrefType, Value convertedMemref, ValueRange bounds, Value elementSize, ConversionPatternRewriter &rewriter)
Build the runtime argument descriptor wrapping an already converted memref.
@ None
An explicit data construct.
int64_t flags
Region flags, in the encoding of ACCDataRegionFlags.
ValueRange returnAddressOperands
Mapping operands whose device address the entry point returns.
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 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 argTypes
Array of map-type flags, in the runtime encoding of acc::MapFlags.
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 argPtrs
Array of addresses of the mapped objects themselves.
The values that describe one object to the runtime, one for each argument array.