xref: /llvm-project/offload/include/OffloadPolicy.h (revision 330d8983d25d08580fc1642fea48b2473f47a9da)
1 //===-- OffloadPolicy.h - Configuration of offload behavior -----*- 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 // Configuration for offload behavior, e.g., if offload is disabled, can be
10 // disabled, is mandatory, etc.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef OMPTARGET_OFFLOAD_POLICY_H
15 #define OMPTARGET_OFFLOAD_POLICY_H
16 
17 #include "PluginManager.h"
18 
19 enum kmp_target_offload_kind_t {
20   tgt_disabled = 0,
21   tgt_default = 1,
22   tgt_mandatory = 2
23 };
24 
25 extern "C" int __kmpc_get_target_offload(void) __attribute__((weak));
26 
27 class OffloadPolicy {
28 
OffloadPolicy(PluginManager & PM)29   OffloadPolicy(PluginManager &PM) {
30     // TODO: Check for OpenMP.
31     switch ((kmp_target_offload_kind_t)__kmpc_get_target_offload()) {
32     case tgt_disabled:
33       Kind = DISABLED;
34       return;
35     case tgt_mandatory:
36       Kind = MANDATORY;
37       return;
38     default:
39       if (PM.getNumDevices()) {
40         DP("Default TARGET OFFLOAD policy is now mandatory "
41            "(devices were found)\n");
42         Kind = MANDATORY;
43       } else {
44         DP("Default TARGET OFFLOAD policy is now disabled "
45            "(no devices were found)\n");
46         Kind = DISABLED;
47       }
48       return;
49     };
50   }
51 
52 public:
get(PluginManager & PM)53   static const OffloadPolicy &get(PluginManager &PM) {
54     static OffloadPolicy OP(PM);
55     return OP;
56   }
57 
58   enum OffloadPolicyKind { DISABLED, MANDATORY };
59 
60   OffloadPolicyKind Kind = MANDATORY;
61 };
62 
63 #endif // OMPTARGET_OFFLOAD_POLICY_H
64