1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
|
//===-- InteropAPI.cpp - Implementation of OpenMP interoperability API ----===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "OpenMP/InteropAPI.h"
#include "OpenMP/InternalTypes.h"
#include "OpenMP/omp.h"
#include "PluginManager.h"
#include "device.h"
#include "omptarget.h"
#include "llvm/Support/Error.h"
#include <cstdlib>
#include <cstring>
extern "C" {
void __kmpc_omp_wait_deps(ident_t *loc_ref, int32_t gtid, int32_t ndeps,
kmp_depend_info_t *dep_list, int32_t ndeps_noalias,
kmp_depend_info_t *noalias_dep_list)
__attribute__((weak));
} // extern "C"
namespace {
omp_interop_rc_t getPropertyErrorType(omp_interop_property_t Property) {
switch (Property) {
case omp_ipr_fr_id:
return omp_irc_type_int;
case omp_ipr_fr_name:
return omp_irc_type_str;
case omp_ipr_vendor:
return omp_irc_type_int;
case omp_ipr_vendor_name:
return omp_irc_type_str;
case omp_ipr_device_num:
return omp_irc_type_int;
case omp_ipr_platform:
return omp_irc_type_int;
case omp_ipr_device:
return omp_irc_type_ptr;
case omp_ipr_device_context:
return omp_irc_type_ptr;
case omp_ipr_targetsync:
return omp_irc_type_ptr;
};
return omp_irc_no_value;
}
void getTypeMismatch(omp_interop_property_t Property, int *Err) {
if (Err)
*Err = getPropertyErrorType(Property);
}
const char *getVendorIdToStr(const omp_foreign_runtime_ids_t VendorId) {
switch (VendorId) {
case cuda:
return ("cuda");
case cuda_driver:
return ("cuda_driver");
case opencl:
return ("opencl");
case sycl:
return ("sycl");
case hip:
return ("hip");
case level_zero:
return ("level_zero");
}
return ("unknown");
}
template <typename PropertyTy>
PropertyTy getProperty(omp_interop_val_t &InteropVal,
omp_interop_property_t Property, int *Err);
template <>
intptr_t getProperty<intptr_t>(omp_interop_val_t &InteropVal,
omp_interop_property_t Property, int *Err) {
switch (Property) {
case omp_ipr_fr_id:
return InteropVal.backend_type_id;
case omp_ipr_vendor:
return InteropVal.vendor_id;
case omp_ipr_device_num:
return InteropVal.device_id;
default:;
}
getTypeMismatch(Property, Err);
return 0;
}
template <>
const char *getProperty<const char *>(omp_interop_val_t &InteropVal,
omp_interop_property_t Property,
int *Err) {
switch (Property) {
case omp_ipr_fr_id:
return InteropVal.interop_type == kmp_interop_type_tasksync
? "tasksync"
: "device+context";
case omp_ipr_vendor_name:
return getVendorIdToStr(InteropVal.vendor_id);
default:
getTypeMismatch(Property, Err);
return nullptr;
}
}
template <>
void *getProperty<void *>(omp_interop_val_t &InteropVal,
omp_interop_property_t Property, int *Err) {
switch (Property) {
case omp_ipr_device:
if (InteropVal.device_info.Device)
return InteropVal.device_info.Device;
*Err = omp_irc_no_value;
return const_cast<char *>(InteropVal.err_str);
case omp_ipr_device_context:
return InteropVal.device_info.Context;
case omp_ipr_targetsync:
return InteropVal.async_info->Queue;
default:;
}
getTypeMismatch(Property, Err);
return nullptr;
}
bool getPropertyCheck(omp_interop_val_t **InteropPtr,
omp_interop_property_t Property, int *Err) {
if (Err)
*Err = omp_irc_success;
if (!InteropPtr) {
if (Err)
*Err = omp_irc_empty;
return false;
}
if (Property >= 0 || Property < omp_ipr_first) {
if (Err)
*Err = omp_irc_out_of_range;
return false;
}
if (Property == omp_ipr_targetsync &&
(*InteropPtr)->interop_type != kmp_interop_type_tasksync) {
if (Err)
*Err = omp_irc_other;
return false;
}
if ((Property == omp_ipr_device || Property == omp_ipr_device_context) &&
(*InteropPtr)->interop_type == kmp_interop_type_tasksync) {
if (Err)
*Err = omp_irc_other;
return false;
}
return true;
}
} // namespace
#define __OMP_GET_INTEROP_TY(RETURN_TYPE, SUFFIX) \
RETURN_TYPE omp_get_interop_##SUFFIX(const omp_interop_t interop, \
omp_interop_property_t property_id, \
int *err) { \
omp_interop_val_t *interop_val = (omp_interop_val_t *)interop; \
assert((interop_val)->interop_type == kmp_interop_type_tasksync); \
if (!getPropertyCheck(&interop_val, property_id, err)) { \
return (RETURN_TYPE)(0); \
} \
return getProperty<RETURN_TYPE>(*interop_val, property_id, err); \
}
__OMP_GET_INTEROP_TY(intptr_t, int)
__OMP_GET_INTEROP_TY(void *, ptr)
__OMP_GET_INTEROP_TY(const char *, str)
#undef __OMP_GET_INTEROP_TY
#define __OMP_GET_INTEROP_TY3(RETURN_TYPE, SUFFIX) \
RETURN_TYPE omp_get_interop_##SUFFIX(const omp_interop_t interop, \
omp_interop_property_t property_id) { \
int err; \
omp_interop_val_t *interop_val = (omp_interop_val_t *)interop; \
if (!getPropertyCheck(&interop_val, property_id, &err)) { \
return (RETURN_TYPE)(0); \
} \
return nullptr; \
return getProperty<RETURN_TYPE>(*interop_val, property_id, &err); \
}
__OMP_GET_INTEROP_TY3(const char *, name)
__OMP_GET_INTEROP_TY3(const char *, type_desc)
__OMP_GET_INTEROP_TY3(const char *, rc_desc)
#undef __OMP_GET_INTEROP_TY3
static const char *copyErrorString(llvm::Error &&Err) {
// TODO: Use the error string while avoiding leaks.
std::string ErrMsg = llvm::toString(std::move(Err));
char *UsrMsg = reinterpret_cast<char *>(malloc(ErrMsg.size() + 1));
strcpy(UsrMsg, ErrMsg.c_str());
return UsrMsg;
}
extern "C" {
void __tgt_interop_init(ident_t *LocRef, int32_t Gtid,
omp_interop_val_t *&InteropPtr,
kmp_interop_type_t InteropType, int32_t DeviceId,
int32_t Ndeps, kmp_depend_info_t *DepList,
int32_t HaveNowait) {
int32_t NdepsNoalias = 0;
kmp_depend_info_t *NoaliasDepList = NULL;
assert(InteropType != kmp_interop_type_unknown &&
"Cannot initialize with unknown interop_type!");
if (DeviceId == -1) {
DeviceId = omp_get_default_device();
}
if (InteropType == kmp_interop_type_tasksync) {
__kmpc_omp_wait_deps(LocRef, Gtid, Ndeps, DepList, NdepsNoalias,
NoaliasDepList);
}
InteropPtr = new omp_interop_val_t(DeviceId, InteropType);
auto DeviceOrErr = PM->getDevice(DeviceId);
if (!DeviceOrErr) {
InteropPtr->err_str = copyErrorString(DeviceOrErr.takeError());
return;
}
DeviceTy &Device = *DeviceOrErr;
if (!Device.RTL ||
Device.RTL->init_device_info(DeviceId, &(InteropPtr)->device_info,
&(InteropPtr)->err_str)) {
delete InteropPtr;
InteropPtr = omp_interop_none;
}
if (InteropType == kmp_interop_type_tasksync) {
if (!Device.RTL ||
Device.RTL->init_async_info(DeviceId, &(InteropPtr)->async_info)) {
delete InteropPtr;
InteropPtr = omp_interop_none;
}
}
}
void __tgt_interop_use(ident_t *LocRef, int32_t Gtid,
omp_interop_val_t *&InteropPtr, int32_t DeviceId,
int32_t Ndeps, kmp_depend_info_t *DepList,
int32_t HaveNowait) {
int32_t NdepsNoalias = 0;
kmp_depend_info_t *NoaliasDepList = NULL;
assert(InteropPtr && "Cannot use nullptr!");
omp_interop_val_t *InteropVal = InteropPtr;
if (DeviceId == -1) {
DeviceId = omp_get_default_device();
}
assert(InteropVal != omp_interop_none &&
"Cannot use uninitialized interop_ptr!");
assert((DeviceId == -1 || InteropVal->device_id == DeviceId) &&
"Inconsistent device-id usage!");
auto DeviceOrErr = PM->getDevice(DeviceId);
if (!DeviceOrErr) {
InteropPtr->err_str = copyErrorString(DeviceOrErr.takeError());
return;
}
if (InteropVal->interop_type == kmp_interop_type_tasksync) {
__kmpc_omp_wait_deps(LocRef, Gtid, Ndeps, DepList, NdepsNoalias,
NoaliasDepList);
}
// TODO Flush the queue associated with the interop through the plugin
}
void __tgt_interop_destroy(ident_t *LocRef, int32_t Gtid,
omp_interop_val_t *&InteropPtr, int32_t DeviceId,
int32_t Ndeps, kmp_depend_info_t *DepList,
int32_t HaveNowait) {
int32_t NdepsNoalias = 0;
kmp_depend_info_t *NoaliasDepList = NULL;
assert(InteropPtr && "Cannot use nullptr!");
omp_interop_val_t *InteropVal = InteropPtr;
if (DeviceId == -1) {
DeviceId = omp_get_default_device();
}
if (InteropVal == omp_interop_none)
return;
assert((DeviceId == -1 || InteropVal->device_id == DeviceId) &&
"Inconsistent device-id usage!");
auto DeviceOrErr = PM->getDevice(DeviceId);
if (!DeviceOrErr) {
InteropPtr->err_str = copyErrorString(DeviceOrErr.takeError());
return;
}
if (InteropVal->interop_type == kmp_interop_type_tasksync) {
__kmpc_omp_wait_deps(LocRef, Gtid, Ndeps, DepList, NdepsNoalias,
NoaliasDepList);
}
// TODO Flush the queue associated with the interop through the plugin
// TODO Signal out dependences
delete InteropPtr;
InteropPtr = omp_interop_none;
}
} // extern "C"
|