1// clang-format off
2// RUN: %libomptarget-compile-generic -DREQ=1 && %libomptarget-run-generic 2>&1 | %fcheck-generic -check-prefix=GOOD
3// RUN: %libomptarget-compile-generic -DREQ=2 && not %libomptarget-run-generic 2>&1 | %fcheck-generic -check-prefix=BAD
4// clang-format on
5
6/*
7 Test for the 'requires' clause check.
8 When a target region is used, the requires flags are set in the
9 runtime for the entire compilation unit. If the flags are set again,
10 (for whatever reason) the set must be consistent with previously
11 set values.
12*/
13#include <omp.h>
14#include <stdio.h>
15
16// ---------------------------------------------------------------------------
17// Various definitions copied from OpenMP RTL
18
19typedef struct {
20 void *addr;
21 char *name;
22 size_t size;
23 int32_t flags;
24 int32_t data;
25} __tgt_offload_entry;
26
27enum Flags {
28 OMP_REGISTER_REQUIRES = 0x10,
29};
30
31typedef struct {
32 void *ImageStart;
33 void *ImageEnd;
34 __tgt_offload_entry *EntriesBegin;
35 __tgt_offload_entry *EntriesEnd;
36} __tgt_device_image;
37
38typedef struct {
39 int32_t NumDeviceImages;
40 __tgt_device_image *DeviceImages;
41 __tgt_offload_entry *HostEntriesBegin;
42 __tgt_offload_entry *HostEntriesEnd;
43} __tgt_bin_desc;
44
45void __tgt_register_lib(__tgt_bin_desc *Desc);
46void __tgt_unregister_lib(__tgt_bin_desc *Desc);
47
48// End of definitions copied from OpenMP RTL.
49// ---------------------------------------------------------------------------
50
51void run_reg_requires() {
52 // Before the target region is registered, the requires registers the status
53 // of the requires clauses. Since there are no requires clauses in this file
54 // the flags state can only be OMP_REQ_NONE i.e. 1.
55
56 // This is the 2nd time this function is called so it should print SUCCESS if
57 // REQ is compatible with `1` and otherwise cause an error.
58 __tgt_offload_entry entries[] = {{NULL, "", 0, OMP_REGISTER_REQUIRES, 1},
59 {NULL, "", 0, OMP_REGISTER_REQUIRES, REQ}};
60 __tgt_device_image image = {NULL, NULL, &entries[0], &entries[1] + 1};
61 __tgt_bin_desc bin = {1, &image, &entries[0], &entries[1] + 1};
62
63 __tgt_register_lib(Desc: &bin);
64
65 printf(format: "SUCCESS");
66
67 __tgt_unregister_lib(Desc: &bin);
68
69 // clang-format off
70 // GOOD: SUCCESS
71 // BAD: omptarget fatal error 2: '#pragma omp requires reverse_offload' not used consistently!
72 // clang-format on
73}
74
75// ---------------------------------------------------------------------------
76int main() {
77 run_reg_requires();
78
79// This also runs reg requires for the first time.
80#pragma omp target
81 {}
82
83 return 0;
84}
85

source code of offload/test/offloading/requires.c