FLANG
CUFCommon.h
1//===-- CUFCommon.h -------------------------------------------------------===//
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#ifndef FORTRAN_OPTIMIZER_TRANSFORMS_CUFCOMMON_H_
10#define FORTRAN_OPTIMIZER_TRANSFORMS_CUFCOMMON_H_
11
12#include "flang/Optimizer/Dialect/FIROps.h"
13#include "mlir/Dialect/Func/IR/FuncOps.h"
14#include "mlir/Dialect/GPU/IR/GPUDialect.h"
15#include "mlir/IR/BuiltinOps.h"
16#include "llvm/ADT/DenseMap.h"
17#include "llvm/ADT/SetVector.h"
18#include "llvm/TargetParser/Triple.h"
19
20static constexpr llvm::StringRef cudaDeviceModuleName = "cuda_device_mod";
21static constexpr llvm::StringRef cudaSharedMemSuffix = "__shared_mem__";
25static constexpr llvm::StringRef cudaDeviceCopySuffix = ".device";
29static constexpr llvm::StringRef cudaSharedTypeInfoSection = "__nv_type_info";
33static constexpr llvm::StringRef cudaSharedTypeInfoEndPrefix =
34 "__nv_type_info_end_";
37inline int64_t getCudaSharedTypeInfoAlignment(const llvm::Triple &triple) {
38 if (triple.isAArch64() || triple.isPPC64())
39 return 65536;
40 return 4096;
41}
44static constexpr llvm::StringRef cudaSharedTypeDescsAttrName =
45 "cuf.shared_type_descs";
48static constexpr llvm::StringRef cudaHostTypeDescAttrName =
49 "cuf.host_type_desc";
50
51namespace fir {
52class FirOpBuilder;
53class KindMapping;
54} // namespace fir
55
56namespace cuf {
57
59mlir::gpu::GPUModuleOp getOrCreateGPUModule(mlir::ModuleOp mod,
60 mlir::SymbolTable &symTab);
61
62bool isCUDADeviceContext(mlir::Operation *op);
63bool isCUDADeviceContext(mlir::Region &,
64 bool isDoConcurrentOffloadEnabled = false);
65
71bool isExecutingOnDevice(mlir::Operation *op);
72bool isRegisteredDeviceGlobal(fir::GlobalOp op);
73bool isRegisteredDeviceAttr(std::optional<cuf::DataAttribute> attr);
74
77bool isTypeInfoGlobal(fir::GlobalOp op);
78
81bool isTypeDescriptorGlobal(fir::GlobalOp op);
82
86bool isDeviceProcedure(mlir::func::FuncOp funcOp);
87
91 llvm::SetVector<mlir::func::FuncOp> deviceFuncs;
96 llvm::SetVector<mlir::func::FuncOp> calledFromDevice;
99 llvm::SetVector<mlir::func::FuncOp> keepInModule;
100};
101
106DeviceCodeSet collectDeviceCode(mlir::ModuleOp mod, mlir::SymbolTable &symTab,
107 bool rejectDynamicDispatch = false);
108
112 mlir::Operation *root,
113 const llvm::DenseMap<mlir::StringAttr, mlir::FlatSymbolRefAttr> &map);
114
118void setDeviceAllocationPolicy(mlir::Operation *func);
119
123void genPointerSync(mlir::Location loc, const mlir::Value box,
124 fir::FirOpBuilder &builder);
125
126int computeElementByteSize(mlir::Location loc, mlir::Type type,
127 fir::KindMapping &kindMap,
128 bool emitErrorOnFailure = true);
129
130mlir::Value computeElementCount(mlir::PatternRewriter &rewriter,
131 mlir::Location loc, mlir::Value shapeOperand,
132 mlir::Type seqType, mlir::Type targetType);
133
134} // namespace cuf
135
136#endif // FORTRAN_OPTIMIZER_TRANSFORMS_CUFCOMMON_H_
Definition FIRBuilder.h:66
Definition KindMapping.h:48
Definition ConvertVariable.h:28
bool isDeviceProcedure(mlir::func::FuncOp funcOp)
Definition CUFCommon.cpp:212
bool isExecutingOnDevice(mlir::Operation *op)
Definition CUFCommon.cpp:75
void remapProcedureSymbols(mlir::Operation *root, const llvm::DenseMap< mlir::StringAttr, mlir::FlatSymbolRefAttr > &map)
Definition CUFCommon.cpp:300
bool isTypeInfoGlobal(fir::GlobalOp op)
Definition CUFCommon.cpp:105
bool isTypeDescriptorGlobal(fir::GlobalOp op)
Definition CUFCommon.cpp:119
void setDeviceAllocationPolicy(mlir::Operation *func)
Definition CUFCommon.cpp:320
void genPointerSync(mlir::Location loc, const mlir::Value box, fir::FirOpBuilder &builder)
Definition CUFCommon.cpp:132
DeviceCodeSet collectDeviceCode(mlir::ModuleOp mod, mlir::SymbolTable &symTab, bool rejectDynamicDispatch=false)
Definition CUFCommon.cpp:232
mlir::gpu::GPUModuleOp getOrCreateGPUModule(mlir::ModuleOp mod, mlir::SymbolTable &symTab)
Retrieve or create the CUDA Fortran GPU module in the given mod.
Definition CUFCommon.cpp:25
Definition AbstractConverter.h:37
The device code of a module, as gathered by collectDeviceCode.
Definition CUFCommon.h:89
llvm::SetVector< mlir::func::FuncOp > deviceFuncs
Procedures with a device proc attribute (see isDeviceProcedure).
Definition CUFCommon.h:91
llvm::SetVector< mlir::func::FuncOp > calledFromDevice
Definition CUFCommon.h:96
llvm::SetVector< mlir::func::FuncOp > keepInModule
Definition CUFCommon.h:99