Skip to content

Commit a53cc8b

Browse files
jhuber6Chenyang-L
authored andcommitted
[OpenMP] Always apply target declarations to canonical definitions
This patch changes the handling of OpenMP to add the device attributes to the canonical definitions when we encounter a non-canonical definition. Previously, the following code would not work because it would find the non-canonical definition first which would then not be used anywhere else. ``` int x; extern int x; ``` This patch now adds the attribute to both of them. This allows us to perform the following operation if, for example, there were an implementation of `stderr` on the device. ``` #include <stdio.h> // List of libc symbols supported on the device. extern FILE *stderr; ``` Unfortunately I cannot think of an equivalent solution to HIP / CUDA device declarations as those are done with simple attributes. Attributes themselves cannot be used to affect a definition once its canonical definition has already been seen. Some help on that front would be appreciated. Fixes llvm/llvm-project#63355 Reviewed By: ABataev Differential Revision: https://reviews.llvm.org/D153369
1 parent c9ce139 commit a53cc8b

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

clang/lib/AST/AttrImpl.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -151,14 +151,16 @@ void OMPDeclareTargetDeclAttr::printPrettyPragma(
151151

152152
std::optional<OMPDeclareTargetDeclAttr *>
153153
OMPDeclareTargetDeclAttr::getActiveAttr(const ValueDecl *VD) {
154-
if (!VD->hasAttrs())
154+
if (llvm::all_of(VD->redecls(), [](const Decl *D) { return !D->hasAttrs(); }))
155155
return std::nullopt;
156156
unsigned Level = 0;
157157
OMPDeclareTargetDeclAttr *FoundAttr = nullptr;
158-
for (auto *Attr : VD->specific_attrs<OMPDeclareTargetDeclAttr>()) {
159-
if (Level <= Attr->getLevel()) {
160-
Level = Attr->getLevel();
161-
FoundAttr = Attr;
158+
for (const Decl *D : VD->redecls()) {
159+
for (auto *Attr : D->specific_attrs<OMPDeclareTargetDeclAttr>()) {
160+
if (Level <= Attr->getLevel()) {
161+
Level = Attr->getLevel();
162+
FoundAttr = Attr;
163+
}
162164
}
163165
}
164166
if (FoundAttr)

clang/test/OpenMP/declare_target_codegen.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,13 @@
2727
// CHECK-DAG: Bake
2828
// CHECK-NOT: @{{hhh|ggg|fff|eee}} =
2929
// CHECK-DAG: @flag = protected global i8 undef,
30+
// CHECK-DAG: @dx = {{protected | }}global i32 0,
31+
// CHECK-DAG: @dy = {{protected | }}global i32 0,
3032
// CHECK-DAG: @aaa = external global i32,
31-
// CHECK-DAG: @bbb ={{ protected | }}global i32 0,
33+
// CHECK-DAG: @bbb = {{protected | }}global i32 0,
3234
// CHECK-DAG: weak constant %struct.__tgt_offload_entry { ptr @bbb,
3335
// CHECK-DAG: @ccc = external global i32,
34-
// CHECK-DAG: @ddd ={{ protected | }}global i32 0,
36+
// CHECK-DAG: @ddd = {{protected | }}global i32 0,
3537
// CHECK-DAG: @hhh_decl_tgt_ref_ptr = weak global ptr null
3638
// CHECK-DAG: @ggg_decl_tgt_ref_ptr = weak global ptr null
3739
// CHECK-DAG: @fff_decl_tgt_ref_ptr = weak global ptr null
@@ -51,10 +53,21 @@
5153
// CHECK-DAG: define {{.*}}i32 @{{.*}}{{foo|bar|baz2|baz3|FA|f_method}}{{.*}}()
5254
// CHECK-DAG: define {{.*}}void @{{.*}}TemplateClass{{.*}}(ptr {{[^,]*}} %{{.*}})
5355
// CHECK-DAG: define {{.*}}i32 @{{.*}}TemplateClass{{.*}}f_method{{.*}}(ptr {{[^,]*}} %{{.*}})
54-
// CHECK-DAG: define {{.*}}void @__omp_offloading_{{.*}}_globals_l[[@LINE+78]]_ctor()
56+
// CHECK-DAG: define {{.*}}void @__omp_offloading_{{.*}}_globals_l[[@LINE+89]]_ctor()
5557

5658
#ifndef HEADER
5759
#define HEADER
60+
61+
int dx = 0;
62+
extern int dx;
63+
#pragma omp declare target to(dx)
64+
65+
int dy = 0;
66+
#pragma omp begin declare target
67+
68+
extern int dy;
69+
#pragma omp end declare target
70+
5871
#pragma omp declare target
5972
bool flag [[clang::loader_uninitialized]];
6073
extern int bbb;

0 commit comments

Comments
 (0)