-
Notifications
You must be signed in to change notification settings - Fork 14.2k
[OpenMP][NFC] Extract OffloadPolicy into a helper class #74029
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
Conversation
@llvm/pr-subscribers-openmp Author: Johannes Doerfert (jdoerfert) ChangesOpenMP allows 3 different offload policies, handling of which we want to encapsulate. Full diff: https://github.com/llvm/llvm-project/pull/74029.diff 5 Files Affected:
diff --git a/openmp/libomptarget/include/OffloadPolicy.h b/openmp/libomptarget/include/OffloadPolicy.h
new file mode 100644
index 000000000000000..858d9c323b15a20
--- /dev/null
+++ b/openmp/libomptarget/include/OffloadPolicy.h
@@ -0,0 +1,63 @@
+//===-- OffloadPolicy.h - Configuration of offload behavior -----*- 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
+//
+//===----------------------------------------------------------------------===//
+//
+// Configuration for offload behavior, e.g., if offload is disabled, can be
+// disabled, is mandatory, etc.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef OMPTARGET_OFFLOAD_POLICY_H
+#define OMPTARGET_OFFLOAD_POLICY_H
+
+#include "PluginManager.h"
+
+enum kmp_target_offload_kind_t {
+ tgt_disabled = 0,
+ tgt_default = 1,
+ tgt_mandatory = 2
+};
+
+extern "C" int __kmpc_get_target_offload(void) __attribute__((weak));
+
+class OffloadPolicy {
+
+ OffloadPolicy(PluginManager &PM) {
+ // TODO: Check for OpenMP.
+ switch ((kmp_target_offload_kind_t)__kmpc_get_target_offload()) {
+ case tgt_disabled:
+ Kind = DISABLED;
+ return;
+ case tgt_mandatory:
+ Kind = MANDATORY;
+ return;
+ default:
+ if (PM.getNumDevices()) {
+ DP("Default TARGET OFFLOAD policy is now mandatory "
+ "(devices were found)\n");
+ Kind = MANDATORY;
+ } else {
+ DP("Default TARGET OFFLOAD policy is now disabled "
+ "(no devices were found)\n");
+ Kind = DISABLED;
+ }
+ return;
+ };
+ }
+
+public:
+ static const OffloadPolicy &get(PluginManager &PM) {
+ static OffloadPolicy OP(PM);
+ return OP;
+ }
+
+ enum OffloadPolicyKind { DISABLED, MANDATORY };
+
+ OffloadPolicyKind Kind = MANDATORY;
+};
+
+#endif // OMPTARGET_OFFLOAD_POLICY_H
diff --git a/openmp/libomptarget/include/PluginManager.h b/openmp/libomptarget/include/PluginManager.h
index c92884d8e27df7a..55f74268dc99e1f 100644
--- a/openmp/libomptarget/include/PluginManager.h
+++ b/openmp/libomptarget/include/PluginManager.h
@@ -114,10 +114,6 @@ struct PluginManager {
HostPtrToTableMapTy HostPtrToTableMap;
std::mutex TblMapMtx; ///< For HostPtrToTableMap
- // Store target policy (disabled, mandatory, default)
- kmp_target_offload_kind_t TargetOffloadPolicy = tgt_default;
- std::mutex TargetOffloadMtx; ///< For TargetOffloadPolicy
-
// Work around for plugins that call dlopen on shared libraries that call
// tgt_register_lib during their initialisation. Stash the pointers in a
// vector until the plugins are all initialised and then register them.
diff --git a/openmp/libomptarget/include/device.h b/openmp/libomptarget/include/device.h
index 8d253df19215ef0..6602ee052ddd330 100644
--- a/openmp/libomptarget/include/device.h
+++ b/openmp/libomptarget/include/device.h
@@ -33,14 +33,6 @@ struct PluginAdaptorTy;
struct __tgt_bin_desc;
struct __tgt_target_table;
-// enum for OMP_TARGET_OFFLOAD; keep in sync with kmp.h definition
-enum kmp_target_offload_kind {
- tgt_disabled = 0,
- tgt_default = 1,
- tgt_mandatory = 2
-};
-typedef enum kmp_target_offload_kind kmp_target_offload_kind_t;
-
///
struct PendingCtorDtorListsTy {
std::list<void *> PendingCtors;
diff --git a/openmp/libomptarget/src/omptarget.cpp b/openmp/libomptarget/src/omptarget.cpp
index 9d75fd360108fa6..839c709f906cb62 100644
--- a/openmp/libomptarget/src/omptarget.cpp
+++ b/openmp/libomptarget/src/omptarget.cpp
@@ -12,6 +12,7 @@
//===----------------------------------------------------------------------===//
#include "omptarget.h"
+#include "OffloadPolicy.h"
#include "OpenMP/OMPT/Callback.h"
#include "OpenMP/OMPT/Interface.h"
#include "PluginManager.h"
@@ -281,17 +282,13 @@ static int initLibrary(DeviceTy &Device) {
}
void handleTargetOutcome(bool Success, ident_t *Loc) {
- switch (PM->TargetOffloadPolicy) {
- case tgt_disabled:
+ switch (OffloadPolicy::get(*PM).Kind) {
+ case OffloadPolicy::DISABLED:
if (Success) {
FATAL_MESSAGE0(1, "expected no offloading while offloading is disabled");
}
break;
- case tgt_default:
- FATAL_MESSAGE0(1, "default offloading policy must be switched to "
- "mandatory or disabled");
- break;
- case tgt_mandatory:
+ case OffloadPolicy::MANDATORY:
if (!Success) {
if (getInfoLevel() & OMP_INFOTYPE_DUMP_TABLE)
for (auto &Device : PM->Devices)
@@ -329,27 +326,6 @@ void handleTargetOutcome(bool Success, ident_t *Loc) {
}
}
-static void handleDefaultTargetOffload() {
- std::lock_guard<decltype(PM->TargetOffloadMtx)> LG(PM->TargetOffloadMtx);
- if (PM->TargetOffloadPolicy == tgt_default) {
- if (omp_get_num_devices() > 0) {
- DP("Default TARGET OFFLOAD policy is now mandatory "
- "(devices were found)\n");
- PM->TargetOffloadPolicy = tgt_mandatory;
- } else {
- DP("Default TARGET OFFLOAD policy is now disabled "
- "(no devices were found)\n");
- PM->TargetOffloadPolicy = tgt_disabled;
- }
- }
-}
-
-static bool isOffloadDisabled() {
- if (PM->TargetOffloadPolicy == tgt_default)
- handleDefaultTargetOffload();
- return PM->TargetOffloadPolicy == tgt_disabled;
-}
-
// If offload is enabled, ensure that device DeviceID has been initialized,
// global ctors have been executed, and global data has been mapped.
//
@@ -363,7 +339,7 @@ static bool isOffloadDisabled() {
// If DeviceID == OFFLOAD_DEVICE_DEFAULT, set DeviceID to the default device.
// This step might be skipped if offload is disabled.
bool checkDeviceAndCtors(int64_t &DeviceID, ident_t *Loc) {
- if (isOffloadDisabled()) {
+ if (OffloadPolicy::get(*PM).Kind == OffloadPolicy::DISABLED) {
DP("Offload is disabled\n");
return true;
}
diff --git a/openmp/libomptarget/src/rtl.cpp b/openmp/libomptarget/src/rtl.cpp
index 3cc7ac381640d2e..aaa37db59195c9f 100644
--- a/openmp/libomptarget/src/rtl.cpp
+++ b/openmp/libomptarget/src/rtl.cpp
@@ -66,13 +66,6 @@ __attribute__((destructor(101))) void deinit() {
}
void PluginAdaptorManagerTy::loadRTLs() {
- // Parse environment variable OMP_TARGET_OFFLOAD (if set)
- PM->TargetOffloadPolicy =
- (kmp_target_offload_kind_t)__kmpc_get_target_offload();
- if (PM->TargetOffloadPolicy == tgt_disabled) {
- return;
- }
-
DP("Loading RTLs...\n");
// Attempt to open all the plugins and, if they exist, check if the interface
|
tgt_mandatory = 2 | ||
}; | ||
|
||
extern "C" int __kmpc_get_target_offload(void) __attribute__((weak)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this weak?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was weak, so I kept it that way for now. I honestly don't know.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is just an OpenMP thing, maybe we should move it to the OpenMP/ subdirectory someday.
OpenMP allows 3 different offload policies, handling of which we want to encapsulate.
eef505a
to
b85f905
Compare
OpenMP allows 3 different offload policies, handling of which we want to encapsulate.