FLANG
CUDA.h
1//===-- Lower/CUDA.h -- CUDA Fortran utilities ------------------*- 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// Coding style: https://mlir.llvm.org/getting_started/DeveloperGuide/
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef FORTRAN_LOWER_CUDA_H
14#define FORTRAN_LOWER_CUDA_H
15
16#include "flang/Optimizer/Builder/FIRBuilder.h"
17#include "flang/Optimizer/Builder/MutableBox.h"
18#include "flang/Optimizer/Dialect/CUF/CUFOps.h"
19#include "flang/Runtime/allocator-registry-consts.h"
20#include "flang/Semantics/tools.h"
21
22namespace mlir {
23class Value;
24class Location;
25class MLIRContext;
26} // namespace mlir
27
28namespace hlfir {
29class ElementalOp;
30} // namespace hlfir
31
32namespace Fortran::lower {
33
35
36static inline unsigned getAllocatorIdx(const Fortran::semantics::Symbol &sym) {
37 std::optional<Fortran::common::CUDADataAttr> cudaAttr =
38 Fortran::semantics::GetCUDADataAttr(&sym.GetUltimate());
39 if (cudaAttr) {
40 if (*cudaAttr == Fortran::common::CUDADataAttr::Pinned)
41 return kPinnedAllocatorPos;
42 if (*cudaAttr == Fortran::common::CUDADataAttr::Device)
43 return kDeviceAllocatorPos;
44 if (*cudaAttr == Fortran::common::CUDADataAttr::Managed)
45 return kManagedAllocatorPos;
46 if (*cudaAttr == Fortran::common::CUDADataAttr::Unified)
47 return kUnifiedAllocatorPos;
48 }
49 return kDefaultAllocator;
50}
51
52// Under -gpu=unified, redirect a plain (unattributed)
53// allocatable/pointer from the default allocator to the unified allocator, so
54// its managed backing is carried by the descriptor and honored by every
55// allocation path (ALLOCATE, allocate-on-assignment, SOURCE=, ...). Objects
56// with an explicit CUDA data attribute keep their own allocator.
57static inline unsigned
58getAllocatorIdxForUnified(const Fortran::semantics::Symbol &sym,
59 bool unifiedEnabled) {
60 unsigned idx = getAllocatorIdx(sym);
61 if (unifiedEnabled && idx == kDefaultAllocator &&
62 (Fortran::semantics::IsAllocatable(sym) ||
63 Fortran::semantics::IsPointer(sym)))
64 return kUnifiedAllocatorPos;
65 return idx;
66}
67
68mlir::Type gatherDeviceComponentCoordinatesAndType(
69 fir::FirOpBuilder &builder, mlir::Location loc,
70 const Fortran::semantics::Symbol &sym, fir::RecordType recTy,
71 llvm::SmallVector<mlir::Value> &coordinates);
72
75cuf::DataAttributeAttr
76translateSymbolCUFDataAttribute(mlir::MLIRContext *mlirContext,
77 const Fortran::semantics::Symbol &sym);
78
81std::pair<hlfir::ElementalOp, hlfir::ElementalOp>
82isTransferWithConversion(mlir::Value rhs);
83
85bool hasDoubleDescriptor(mlir::Value);
86
87} // end namespace Fortran::lower
88
89#endif // FORTRAN_LOWER_CUDA_H
Definition AbstractConverter.h:87
Definition ParserActions.h:24
bool hasDoubleDescriptor(mlir::Value)
Check if the value is an allocatable with double descriptor.
Definition CUDA.cpp:117
std::pair< hlfir::ElementalOp, hlfir::ElementalOp > isTransferWithConversion(mlir::Value rhs)
Definition CUDA.cpp:72
cuf::DataAttributeAttr translateSymbolCUFDataAttribute(mlir::MLIRContext *mlirContext, const Fortran::semantics::Symbol &sym)
Definition CUDA.cpp:64
Definition AbstractConverter.h:32