MLIR 23.0.0git
OpenMPOffloadUtils.h
Go to the documentation of this file.
1//===- OpenMPOffloadUtils.h - OpenMP offload 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/// \file
10/// Shared utilities for setting OpenMP offload module interface attributes.
11/// These are used by both Flang and Clang (CIR) frontends.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef MLIR_DIALECT_OPENMP_OPENMPOFFLOADUTILS_H_
16#define MLIR_DIALECT_OPENMP_OPENMPOFFLOADUTILS_H_
17
20#include "mlir/IR/BuiltinOps.h"
21#include "llvm/ADT/SmallVector.h"
22#include "llvm/TargetParser/Triple.h"
23#include <cstdint>
24#include <string>
25#include <vector>
26
27namespace mlir::omp {
28
30 OffloadModuleOpts() = default;
31 OffloadModuleOpts(uint32_t openMPTargetDebug, bool openMPTeamSubscription,
32 bool openMPThreadSubscription, bool openMPNoThreadState,
33 bool openMPNoNestedParallelism, bool openMPIsTargetDevice,
34 bool openMPIsGPU, bool openMPForceUSM,
35 uint32_t openMPVersion, std::string ompHostIRFile = {},
36 const std::vector<llvm::Triple> &ompTargetTriples = {},
37 bool noGPULib = false)
38 : OpenMPTargetDebug(openMPTargetDebug),
39 OpenMPTeamSubscription(openMPTeamSubscription),
40 OpenMPThreadSubscription(openMPThreadSubscription),
41 OpenMPNoThreadState(openMPNoThreadState),
42 OpenMPNoNestedParallelism(openMPNoNestedParallelism),
43 OpenMPIsTargetDevice(openMPIsTargetDevice), OpenMPIsGPU(openMPIsGPU),
44 OpenMPForceUSM(openMPForceUSM), OpenMPVersion(openMPVersion),
45 OMPHostIRFile(std::move(ompHostIRFile)),
46 OMPTargetTriples(ompTargetTriples.begin(), ompTargetTriples.end()),
47 NoGPULib(noGPULib) {}
48
49 uint32_t OpenMPTargetDebug = 0;
52 bool OpenMPNoThreadState = false;
55 bool OpenMPIsGPU = false;
56 bool OpenMPForceUSM = false;
57 uint32_t OpenMPVersion = 31;
58 std::string OMPHostIRFile = {};
59 std::vector<llvm::Triple> OMPTargetTriples = {};
60 bool NoGPULib = false;
61};
62
63/// Sets OpenMP offload module interface attributes on a ModuleOp, shared
64/// between Flang and Clang (CIR) frontends.
65[[maybe_unused]] static void
67 if (auto offloadMod =
68 llvm::dyn_cast<OffloadModuleInterface>(module.getOperation())) {
69 offloadMod.setIsTargetDevice(opts.OpenMPIsTargetDevice);
70 offloadMod.setIsGPU(opts.OpenMPIsGPU);
71 if (opts.OpenMPForceUSM)
72 offloadMod.setRequires(ClauseRequires::unified_shared_memory);
73 if (opts.OpenMPIsTargetDevice) {
74 offloadMod.setFlags(
78 if (!opts.OMPHostIRFile.empty())
79 offloadMod.setHostIRFilePath(opts.OMPHostIRFile);
80 }
81 auto strTriples = llvm::to_vector(
82 llvm::map_range(opts.OMPTargetTriples, [](llvm::Triple triple) {
83 return triple.normalize();
84 }));
85 offloadMod.setTargetTriples(strTriples);
86 }
87}
88
89[[maybe_unused]] static void setOpenMPVersionAttribute(ModuleOp module,
90 int64_t version) {
91 module.getOperation()->setAttr(
92 StringAttr::get(module.getContext(), llvm::Twine{"omp.version"}),
93 VersionAttr::get(module.getContext(), version));
94}
95
96[[maybe_unused]] static int64_t
97getOpenMPVersionAttribute(ModuleOp module, int64_t fallback = -1) {
98 if (Attribute verAttr = module->getAttr("omp.version"))
99 return llvm::cast<VersionAttr>(verAttr).getVersion();
100 return fallback;
101}
102
103} // namespace mlir::omp
104
105#endif // MLIR_DIALECT_OPENMP_OPENMPOFFLOADUTILS_H_
Attributes are known-constant values of operations.
Definition Attributes.h:25
static void setOffloadModuleInterfaceAttributes(ModuleOp module, OffloadModuleOpts opts)
Sets OpenMP offload module interface attributes on a ModuleOp, shared between Flang and Clang (CIR) f...
static int64_t getOpenMPVersionAttribute(ModuleOp module, int64_t fallback=-1)
static void setOpenMPVersionAttribute(ModuleOp module, int64_t version)
std::vector< llvm::Triple > OMPTargetTriples
OffloadModuleOpts(uint32_t openMPTargetDebug, bool openMPTeamSubscription, bool openMPThreadSubscription, bool openMPNoThreadState, bool openMPNoNestedParallelism, bool openMPIsTargetDevice, bool openMPIsGPU, bool openMPForceUSM, uint32_t openMPVersion, std::string ompHostIRFile={}, const std::vector< llvm::Triple > &ompTargetTriples={}, bool noGPULib=false)