Skip to content

Commit

Permalink
[OpenMP] Add Support for Mapping Names in Libomptarget RTL
Browse files Browse the repository at this point in the history
Summary:
This patch adds basic support for priting the source location and names for the mapped variables. This patch does not support names for custom mappers. This is based on D89802.

Reviewers: jdoerfert

Differential Revision: https://reviews.llvm.org/D90172
  • Loading branch information
jhuber6 authored and jhuber-ornl committed Nov 18, 2020
1 parent 18db29e commit 5378c6a
Show file tree
Hide file tree
Showing 8 changed files with 226 additions and 111 deletions.
80 changes: 80 additions & 0 deletions openmp/libomptarget/include/SourceInfo.h
@@ -0,0 +1,80 @@
//===------- SourceInfo.h - Target independent OpenMP target RTL -- C++ -*-===//
//
// 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
//
//===----------------------------------------------------------------------===//
//
// Methods used to describe source information in target regions
//
//===----------------------------------------------------------------------===//

#ifndef _SOURCE_INFO_H_
#define _SOURCE_INFO_H_

#include <string>

#ifdef _WIN32
static const bool OS_WINDOWS = true;
#else
static const bool OS_WINDOWS = false;
#endif

/// Type alias for source location information for variable mappings with
/// data layout ";name;filename;row;col;;\0" from clang.
using map_var_info_t = void *;

/// Struct to hold source individual location information.
class SourceInfo {
/// Underlying string copy of the original source information.
const std::string str;

std::string initStr(const map_var_info_t name) {
if (!name)
return ";unknown;unknown;0;0;;";
else
return std::string(reinterpret_cast<const char *>(name));
}

/// Get n-th substring in an expression separated by ;.
std::string getSubstring(const int n) {
std::size_t begin = str.find(';');
std::size_t end = str.find(';', begin + 1);
for (int i = 0; i < n; i++) {
begin = end;
end = str.find(';', begin + 1);
}
return str.substr(begin + 1, end - begin - 1);
};

/// Get the filename from a full path.
std::string removePath(const std::string &path) {
std::size_t pos = (OS_WINDOWS) ? path.rfind('\\') : path.rfind('/');
return path.substr(pos + 1);
};

public:
SourceInfo(const map_var_info_t name)
: str(initStr(name)), name(getSubstring(0)),
filename(removePath(getSubstring(1))), line(std::stoi(getSubstring(2))),
column(std::stoi(getSubstring(3))) {}

const std::string name;
const std::string filename;
const int32_t line;
const int32_t column;
};

/// Standalone function for getting the variable name of a mapping.
static inline std::string getNameFromMapping(const map_var_info_t name) {
if (!name)
return "unknown";

const std::string name_str(reinterpret_cast<const char *>(name));
std::size_t begin = name_str.find(';');
std::size_t end = name_str.find(';', begin + 1);
return name_str.substr(begin + 1, end - begin - 1);
}

#endif
44 changes: 24 additions & 20 deletions openmp/libomptarget/include/omptarget.h
Expand Up @@ -17,6 +17,8 @@
#include <stdint.h>
#include <stddef.h>

#include <SourceInfo.h>

#define OFFLOAD_SUCCESS (0)
#define OFFLOAD_FAIL (~0)

Expand Down Expand Up @@ -164,14 +166,13 @@ void __tgt_target_data_begin_nowait(int64_t device_id, int32_t arg_num,
void __tgt_target_data_begin_mapper(int64_t device_id, int32_t arg_num,
void **args_base, void **args,
int64_t *arg_sizes, int64_t *arg_types,
void **arg_names, void **arg_mappers);
void __tgt_target_data_begin_nowait_mapper(int64_t device_id, int32_t arg_num,
void **args_base, void **args,
int64_t *arg_sizes,
int64_t *arg_types, void **arg_names,
void **arg_mappers, int32_t depNum,
void *depList, int32_t noAliasDepNum,
void *noAliasDepList);
map_var_info_t *arg_names,
void **arg_mappers);
void __tgt_target_data_begin_nowait_mapper(
int64_t device_id, int32_t arg_num, void **args_base, void **args,
int64_t *arg_sizes, int64_t *arg_types, map_var_info_t *arg_names,
void **arg_mappers, int32_t depNum, void *depList, int32_t noAliasDepNum,
void *noAliasDepList);

// passes data from the target, release target memory and destroys the
// host-target mapping (top entry from the stack of data maps) created by
Expand All @@ -186,13 +187,14 @@ void __tgt_target_data_end_nowait(int64_t device_id, int32_t arg_num,
void __tgt_target_data_end_mapper(int64_t device_id, int32_t arg_num,
void **args_base, void **args,
int64_t *arg_sizes, int64_t *arg_types,
void **arg_names, void **arg_mappers);
map_var_info_t *arg_names,
void **arg_mappers);
void __tgt_target_data_end_nowait_mapper(int64_t device_id, int32_t arg_num,
void **args_base, void **args,
int64_t *arg_sizes, int64_t *arg_types,
void **arg_names, void **arg_mappers,
int32_t depNum, void *depList,
int32_t noAliasDepNum,
map_var_info_t *arg_names,
void **arg_mappers, int32_t depNum,
void *depList, int32_t noAliasDepNum,
void *noAliasDepList);

/// passes data to/from the target
Expand All @@ -208,10 +210,11 @@ void __tgt_target_data_update_nowait(int64_t device_id, int32_t arg_num,
void __tgt_target_data_update_mapper(int64_t device_id, int32_t arg_num,
void **args_base, void **args,
int64_t *arg_sizes, int64_t *arg_types,
void **arg_names, void **arg_mappers);
map_var_info_t *arg_names,
void **arg_mappers);
void __tgt_target_data_update_nowait_mapper(
int64_t device_id, int32_t arg_num, void **args_base, void **args,
int64_t *arg_sizes, int64_t *arg_types, void **arg_names,
int64_t *arg_sizes, int64_t *arg_types, map_var_info_t *arg_names,
void **arg_mappers, int32_t depNum, void *depList, int32_t noAliasDepNum,
void *noAliasDepList);

Expand All @@ -230,12 +233,12 @@ int __tgt_target_nowait(int64_t device_id, void *host_ptr, int32_t arg_num,
int32_t noAliasDepNum, void *noAliasDepList);
int __tgt_target_mapper(int64_t device_id, void *host_ptr, int32_t arg_num,
void **args_base, void **args, int64_t *arg_sizes,
int64_t *arg_types, void **arg_names,
int64_t *arg_types, map_var_info_t *arg_names,
void **arg_mappers);
int __tgt_target_nowait_mapper(int64_t device_id, void *host_ptr,
int32_t arg_num, void **args_base, void **args,
int64_t *arg_sizes, int64_t *arg_types,
void **arg_names, void **arg_mappers,
map_var_info_t *arg_names, void **arg_mappers,
int32_t depNum, void *depList,
int32_t noAliasDepNum, void *noAliasDepList);

Expand All @@ -252,13 +255,14 @@ int __tgt_target_teams_nowait(int64_t device_id, void *host_ptr,
int __tgt_target_teams_mapper(int64_t device_id, void *host_ptr,
int32_t arg_num, void **args_base, void **args,
int64_t *arg_sizes, int64_t *arg_types,
void **arg_names, void **arg_mappers,
map_var_info_t *arg_names, void **arg_mappers,
int32_t num_teams, int32_t thread_limit);
int __tgt_target_teams_nowait_mapper(
int64_t device_id, void *host_ptr, int32_t arg_num, void **args_base,
void **args, int64_t *arg_sizes, int64_t *arg_types, void **arg_names,
void **arg_mappers, int32_t num_teams, int32_t thread_limit, int32_t depNum,
void *depList, int32_t noAliasDepNum, void *noAliasDepList);
void **args, int64_t *arg_sizes, int64_t *arg_types,
map_var_info_t *arg_names, void **arg_mappers, int32_t num_teams,
int32_t thread_limit, int32_t depNum, void *depList, int32_t noAliasDepNum,
void *noAliasDepList);

void __kmpc_push_target_tripcount(int64_t device_id, uint64_t loop_tripcount);

Expand Down
21 changes: 11 additions & 10 deletions openmp/libomptarget/src/device.cpp
Expand Up @@ -77,10 +77,10 @@ int DeviceTy::associatePtr(void *HstPtrBegin, void *TgtPtrBegin, int64_t Size) {
}

// Mapping does not exist, allocate it with refCount=INF
HostDataToTargetTy newEntry((uintptr_t) HstPtrBegin /*HstPtrBase*/,
(uintptr_t) HstPtrBegin /*HstPtrBegin*/,
(uintptr_t) HstPtrBegin + Size /*HstPtrEnd*/,
(uintptr_t) TgtPtrBegin /*TgtPtrBegin*/,
HostDataToTargetTy newEntry((uintptr_t)HstPtrBegin /*HstPtrBase*/,
(uintptr_t)HstPtrBegin /*HstPtrBegin*/,
(uintptr_t)HstPtrBegin + Size /*HstPtrEnd*/,
(uintptr_t)TgtPtrBegin /*TgtPtrBegin*/, nullptr,
true /*IsRefCountINF*/);

DP("Creating new map entry: HstBase=" DPxMOD ", HstBegin=" DPxMOD ", HstEnd="
Expand Down Expand Up @@ -194,9 +194,9 @@ LookupResult DeviceTy::lookupMapping(void *HstPtrBegin, int64_t Size) {
// If NULL is returned, then either data allocation failed or the user tried
// to do an illegal mapping.
void *DeviceTy::getOrAllocTgtPtr(void *HstPtrBegin, void *HstPtrBase,
int64_t Size, bool &IsNew, bool &IsHostPtr,
bool IsImplicit, bool UpdateRefCount,
bool HasCloseModifier,
int64_t Size, map_var_info_t HstPtrName,
bool &IsNew, bool &IsHostPtr, bool IsImplicit,
bool UpdateRefCount, bool HasCloseModifier,
bool HasPresentModifier) {
void *rc = NULL;
IsHostPtr = false;
Expand All @@ -220,10 +220,11 @@ void *DeviceTy::getOrAllocTgtPtr(void *HstPtrBegin, void *HstPtrBase,
INFO(DeviceID,
"Mapping exists%s with HstPtrBegin=" DPxMOD ", TgtPtrBegin=" DPxMOD
", "
"Size=%" PRId64 ",%s RefCount=%s\n",
"Size=%" PRId64 ",%s RefCount=%s, Name=%s\n",
(IsImplicit ? " (implicit)" : ""), DPxPTR(HstPtrBegin), DPxPTR(tp),
Size, (UpdateRefCount ? " updated" : ""),
HT.isRefCountInf() ? "INF" : std::to_string(HT.getRefCount()).c_str());
HT.isRefCountInf() ? "INF" : std::to_string(HT.getRefCount()).c_str(),
(HstPtrName) ? getNameFromMapping(HstPtrName).c_str() : "(null)");
rc = (void *)tp;
} else if ((lr.Flags.ExtendsBefore || lr.Flags.ExtendsAfter) && !IsImplicit) {
// Explicit extension of mapped data - not allowed.
Expand Down Expand Up @@ -268,7 +269,7 @@ void *DeviceTy::getOrAllocTgtPtr(void *HstPtrBegin, void *HstPtrBase,
DPxPTR((uintptr_t)HstPtrBegin + Size), DPxPTR(tp));
HostDataToTargetMap.emplace(
HostDataToTargetTy((uintptr_t)HstPtrBase, (uintptr_t)HstPtrBegin,
(uintptr_t)HstPtrBegin + Size, tp));
(uintptr_t)HstPtrBegin + Size, tp, HstPtrName));
rc = (void *)tp;
}

Expand Down
13 changes: 8 additions & 5 deletions openmp/libomptarget/src/device.h
Expand Up @@ -31,6 +31,8 @@ struct __tgt_target_table;
struct __tgt_async_info;
class MemoryManagerTy;

using map_var_info_t = void *;

// enum for OMP_TARGET_OFFLOAD; keep in sync with kmp.h definition
enum kmp_target_offload_kind {
tgt_disabled = 0,
Expand All @@ -44,6 +46,7 @@ struct HostDataToTargetTy {
uintptr_t HstPtrBase; // host info.
uintptr_t HstPtrBegin;
uintptr_t HstPtrEnd; // non-inclusive.
map_var_info_t HstPtrName; // Optional source name of mapped variable.

uintptr_t TgtPtrBegin; // target info.

Expand All @@ -54,8 +57,8 @@ struct HostDataToTargetTy {

public:
HostDataToTargetTy(uintptr_t BP, uintptr_t B, uintptr_t E, uintptr_t TB,
bool IsINF = false)
: HstPtrBase(BP), HstPtrBegin(B), HstPtrEnd(E),
map_var_info_t Name = nullptr, bool IsINF = false)
: HstPtrBase(BP), HstPtrBegin(B), HstPtrEnd(E), HstPtrName(Name),
TgtPtrBegin(TB), RefCount(IsINF ? INFRefCount : 1) {}

uint64_t getRefCount() const {
Expand Down Expand Up @@ -173,9 +176,9 @@ struct DeviceTy {
uint64_t getMapEntryRefCnt(void *HstPtrBegin);
LookupResult lookupMapping(void *HstPtrBegin, int64_t Size);
void *getOrAllocTgtPtr(void *HstPtrBegin, void *HstPtrBase, int64_t Size,
bool &IsNew, bool &IsHostPtr, bool IsImplicit,
bool UpdateRefCount, bool HasCloseModifier,
bool HasPresentModifier);
map_var_info_t HstPtrName, bool &IsNew,
bool &IsHostPtr, bool IsImplicit, bool UpdateRefCount,
bool HasCloseModifier, bool HasPresentModifier);
void *getTgtPtrBegin(void *HstPtrBegin, int64_t Size);
void *getTgtPtrBegin(void *HstPtrBegin, int64_t Size, bool &IsLast,
bool UpdateRefCount, bool &IsHostPtr,
Expand Down

0 comments on commit 5378c6a

Please sign in to comment.