-
Notifications
You must be signed in to change notification settings - Fork 14.5k
[OFFLOAD][OPENMP] 6.0 compatible interop interface #143491
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
adurang
wants to merge
17
commits into
llvm:main
Choose a base branch
from
adurang:new_interop
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 9 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
8694e6e
[OFFLOAD][OPENMP] 6.0 compatible interop interface
adurang 81c7402
Add missed ext API and minor fix
adurang 0a28255
Fix format
adurang b1a9e10
Fix corner cases related to atexit ordering in PerThreadTable
adurang b72e46a
fix format
adurang fd69d49
remove unnecessary conditions
adurang fbca384
another corner case when unloading
adurang f5715cd
make version 32bits to simplify codegen
adurang 82fa72d
Fix sporadic race condition with helper threads on deinit
adurang 0c29ac6
change const to constexpr
adurang 0440af0
address review comments
adurang 85eb7a7
Add asserts; Bury virtual interfaces
adurang 5774a89
Add error handling to create/releaseInterop
adurang a1f81c3
Remove interfaces with implicity DeviceTy from interop object
adurang 6395c45
Adjust format
adurang e4827f6
reorder dep_pack fields
adurang b27b3af
Rename some symbols according to reviews
adurang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,17 +13,70 @@ | |
|
||
#include "omp.h" | ||
|
||
#include "PerThreadTable.h" | ||
#include "omptarget.h" | ||
|
||
extern "C" { | ||
|
||
typedef enum kmp_interop_type_t { | ||
kmp_interop_type_unknown = -1, | ||
kmp_interop_type_platform, | ||
kmp_interop_type_device, | ||
kmp_interop_type_tasksync, | ||
kmp_interop_type_target, | ||
kmp_interop_type_targetsync, | ||
} kmp_interop_type_t; | ||
|
||
struct interop_attrs_t { | ||
bool inorder : 1; | ||
int reserved : 31; | ||
|
||
/* Check if the supported attributes are compatible with the current | ||
attributes. Only if an attribute is supported can the value be true, | ||
otherwise it needs to be false | ||
*/ | ||
bool checkSupportedOnly(interop_attrs_t supported) const { | ||
return supported.inorder || (!supported.inorder && !inorder); | ||
} | ||
}; | ||
|
||
struct interop_spec_t { | ||
int32_t fr_id; | ||
interop_attrs_t attrs; // Common attributes | ||
int64_t impl_attrs; // Implementation specific attributes (recognized by each | ||
// plugin) | ||
}; | ||
|
||
struct interop_flags_t { | ||
bool implicit : 1; // dispatch (true) or interop (false) | ||
bool nowait : 1; // has nowait flag | ||
int reserved : 30; | ||
}; | ||
|
||
struct interop_ctx_t { | ||
uint32_t version; // version of the interface (current is 0) | ||
interop_flags_t flags; | ||
int gtid; | ||
}; | ||
|
||
struct dep_pack_t { | ||
int32_t ndeps; | ||
kmp_depend_info_t *deplist; | ||
int32_t ndeps_noalias; | ||
kmp_depend_info_t *noalias_deplist; | ||
}; | ||
|
||
struct omp_interop_val_t; | ||
|
||
typedef void ompx_interop_cb_t(omp_interop_val_t *interop, void *data); | ||
|
||
struct omp_interop_cb_instance_t { | ||
ompx_interop_cb_t *cb; | ||
void *data; | ||
|
||
omp_interop_cb_instance_t(ompx_interop_cb_t *cb, void *data) | ||
: cb(cb), data(data) {} | ||
|
||
void operator()(omp_interop_val_t *interop) { cb(interop, data); } | ||
}; | ||
|
||
/// The interop value type, aka. the interop object. | ||
typedef struct omp_interop_val_t { | ||
/// Device and interop-type are determined at construction time and fix. | ||
|
@@ -34,10 +87,96 @@ typedef struct omp_interop_val_t { | |
__tgt_device_info device_info; | ||
const kmp_interop_type_t interop_type; | ||
const intptr_t device_id; | ||
const omp_foreign_runtime_ids_t vendor_id = cuda; | ||
const intptr_t backend_type_id = omp_interop_backend_type_cuda_1; | ||
omp_vendor_id_t vendor_id = omp_vendor_llvm; | ||
omp_foreign_runtime_id_t fr_id = omp_fr_none; | ||
interop_attrs_t attrs{false, 0}; // Common prefer specification attributes | ||
int64_t impl_attrs = 0; // Implementation prefer specification attributes | ||
adurang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
void *RTLProperty = nullptr; // Plugin dependent information | ||
// For implicitly created Interop objects (e.g., from a dispatch construct) | ||
// who owns the object | ||
int OwnerGtid = -1; | ||
// Marks whether the object was requested since the last time it was synced | ||
bool Clean = true; | ||
|
||
typedef llvm::SmallVector<omp_interop_cb_instance_t> callback_list_t; | ||
kevinsala marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
callback_list_t CompletionCbs; | ||
|
||
void reset() { | ||
OwnerGtid = -1; | ||
markClean(); | ||
clearCompletionCbs(); | ||
} | ||
|
||
bool hasOwner() const { return OwnerGtid != -1; } | ||
|
||
void setOwner(int gtid) { OwnerGtid = gtid; } | ||
bool isOwnedBy(int gtid) { return OwnerGtid == gtid; } | ||
bool isCompatibleWith(int32_t InteropType, const interop_spec_t &Spec); | ||
bool isCompatibleWith(int32_t InteropType, const interop_spec_t &Spec, | ||
int64_t DeviceNum, int gtid); | ||
void markClean() { Clean = true; } | ||
void markDirty() { Clean = false; } | ||
bool isClean() const { return Clean; } | ||
|
||
int32_t flush(DeviceTy &Device); | ||
int32_t sync_barrier(DeviceTy &Device); | ||
int32_t async_barrier(DeviceTy &Device); | ||
int32_t release(DeviceTy &Device); | ||
|
||
int32_t flush(); | ||
int32_t syncBarrier(); | ||
int32_t asyncBarrier(); | ||
int32_t release(); | ||
adurang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
void addCompletionCb(ompx_interop_cb_t *cb, void *data) { | ||
CompletionCbs.push_back(omp_interop_cb_instance_t(cb, data)); | ||
} | ||
|
||
int numCompletionCbs() const { return CompletionCbs.size(); } | ||
void clearCompletionCbs() { CompletionCbs.clear(); } | ||
|
||
void runCompletionCbs() { | ||
for (auto &cbInstance : CompletionCbs) | ||
cbInstance(this); | ||
clearCompletionCbs(); | ||
} | ||
} omp_interop_val_t; | ||
|
||
} // extern "C" | ||
|
||
struct InteropTableEntry { | ||
using ContainerTy = typename std::vector<omp_interop_val_t *>; | ||
using iterator = typename ContainerTy::iterator; | ||
|
||
ContainerTy Interops; | ||
|
||
const int reservedEntriesPerThread = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can be |
||
20; // reserve some entries to avoid reallocation | ||
|
||
void add(omp_interop_val_t *obj) { | ||
if (Interops.capacity() == 0) | ||
Interops.reserve(reservedEntriesPerThread); | ||
Interops.push_back(obj); | ||
} | ||
|
||
template <class ClearFuncTy> void clear(ClearFuncTy f) { | ||
for (auto &Obj : Interops) { | ||
f(Obj); | ||
} | ||
} | ||
|
||
/* vector interface */ | ||
adurang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
int size() const { return Interops.size(); } | ||
iterator begin() { return Interops.begin(); } | ||
iterator end() { return Interops.end(); } | ||
iterator erase(iterator it) { return Interops.erase(it); } | ||
}; | ||
|
||
struct InteropTblTy | ||
: public PerThreadTable<InteropTableEntry, omp_interop_val_t *> { | ||
void clear(); | ||
}; | ||
|
||
#endif // OMPTARGET_OPENMP_INTEROP_API_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
//===-- PerThreadTable.h -- PerThread Storage Structure ----*- 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Table indexed with one entry per thread. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef OFFLOAD_PERTHREADTABLE_H | ||
#define OFFLOAD_PERTHREADTABLE_H | ||
|
||
#include <list> | ||
#include <memory> | ||
#include <mutex> | ||
|
||
// Using an STL container (such as std::vector) indexed by thread ID has | ||
// too many race conditions issues so we store each thread entry into a | ||
// thread_local variable. | ||
// T is the container type used to store the objects, e.g., std::vector, | ||
// std::set, etc. by each thread. O is the type of the stored objects e.g., | ||
// omp_interop_val_t *, ... | ||
|
||
template <typename ContainerType, typename ObjectType> struct PerThreadTable { | ||
using iterator = typename ContainerType::iterator; | ||
|
||
struct PerThreadData { | ||
size_t NElements = 0; | ||
std::unique_ptr<ContainerType> ThEntry; | ||
}; | ||
|
||
std::mutex Mtx; | ||
std::list<std::shared_ptr<PerThreadData>> ThreadDataList; | ||
|
||
// define default constructors, disable copy and move constructors | ||
PerThreadTable() = default; | ||
PerThreadTable(const PerThreadTable &) = delete; | ||
PerThreadTable(PerThreadTable &&) = delete; | ||
PerThreadTable &operator=(const PerThreadTable &) = delete; | ||
PerThreadTable &operator=(PerThreadTable &&) = delete; | ||
~PerThreadTable() { | ||
std::lock_guard<std::mutex> Lock(Mtx); | ||
ThreadDataList.clear(); | ||
} | ||
|
||
private: | ||
PerThreadData &getThreadData() { | ||
static thread_local std::shared_ptr<PerThreadData> ThData = nullptr; | ||
if (!ThData) { | ||
ThData = std::make_shared<PerThreadData>(); | ||
std::lock_guard<std::mutex> Lock(Mtx); | ||
ThreadDataList.push_back(ThData); | ||
} | ||
return *ThData; | ||
} | ||
|
||
protected: | ||
ContainerType &getThreadEntry() { | ||
auto &ThData = getThreadData(); | ||
if (ThData.ThEntry) | ||
return *ThData.ThEntry; | ||
ThData.ThEntry = std::make_unique<ContainerType>(); | ||
return *ThData.ThEntry; | ||
} | ||
|
||
size_t &getThreadNElements() { | ||
auto &ThData = getThreadData(); | ||
return ThData.NElements; | ||
} | ||
|
||
public: | ||
void add(ObjectType obj) { | ||
auto &Entry = getThreadEntry(); | ||
auto &NElements = getThreadNElements(); | ||
NElements++; | ||
Entry.add(obj); | ||
} | ||
|
||
iterator erase(iterator it) { | ||
auto &Entry = getThreadEntry(); | ||
auto &NElements = getThreadNElements(); | ||
NElements--; | ||
return Entry.erase(it); | ||
} | ||
|
||
size_t size() { return getThreadNElements(); } | ||
|
||
// Iterators to traverse objects owned by | ||
// the current thread | ||
iterator begin() { | ||
auto &Entry = getThreadEntry(); | ||
return Entry.begin(); | ||
} | ||
iterator end() { | ||
auto &Entry = getThreadEntry(); | ||
return Entry.end(); | ||
} | ||
|
||
template <class F> void clear(F f) { | ||
std::lock_guard<std::mutex> Lock(Mtx); | ||
for (auto ThData : ThreadDataList) { | ||
if (!ThData->ThEntry || ThData->NElements == 0) | ||
continue; | ||
ThData->ThEntry->clear(f); | ||
ThData->NElements = 0; | ||
} | ||
ThreadDataList.clear(); | ||
} | ||
}; | ||
|
||
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.