[PATCH] D153369: [OpenMP] Always apply target declarations to canonical definitions

Joseph Huber via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Jun 20 10:10:18 PDT 2023


jhuber6 created this revision.
jhuber6 added reviewers: jdoerfert, tianshilei1992, JonChesterfield, tra, ABataev, carlo.bertolli.
Herald added subscribers: sunshaoce, guansong.
Herald added a project: All.
jhuber6 requested review of this revision.
Herald added subscribers: cfe-commits, jplehr, sstefan1.
Herald added a project: clang.

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 https://github.com/llvm/llvm-project/issues/63355


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D153369

Files:
  clang/lib/Sema/SemaOpenMP.cpp
  clang/test/AST/dump.cpp


Index: clang/test/AST/dump.cpp
===================================================================
--- clang/test/AST/dump.cpp
+++ clang/test/AST/dump.cpp
@@ -79,11 +79,32 @@
 }
 #pragma omp end declare target
 
-// CHECK:       `-FunctionDecl {{.+}} <line:[[@LINE-6]]:1, line:[[@LINE-3]]:1> line:[[@LINE-6]]:5 bar 'int ()'
-// CHECK-NEXT:  |-CompoundStmt {{.+}} <col:11, line:[[@LINE-4]]:1>
-// CHECK-NEXT:  | |-DeclStmt {{.+}} <line:[[@LINE-7]]:3, col:8>
-// CHECK-NEXT:  | | `-VarDecl {{.+}} <col:3, col:7> col:7 used f 'int'
-// CHECK-NEXT:  | `-ReturnStmt {{.+}} <line:[[@LINE-8]]:3, col:10>
-// CHECK-NEXT:  |   `-ImplicitCastExpr {{.+}} <col:10> 'int' <LValueToRValue>
-// CHECK-NEXT:  |     `-DeclRefExpr {{.+}} <col:10> 'int' lvalue Var {{.+}} 'f' 'int'
-// CHECK-NEXT:  `-OMPDeclareTargetDeclAttr {{.+}} <line:75:21> Implicit MT_To DT_Any 1
+// CHECK:      |-FunctionDecl {{.+}} <line:[[@LINE-6]]:1, line:[[@LINE-3]]:1> line:[[@LINE-6]]:5 bar 'int ()'
+// CHECK-NEXT: | |-CompoundStmt {{.+}} <col:11, line:[[@LINE-4]]:1>
+// CHECK-NEXT: | | |-DeclStmt {{.+}} <line:[[@LINE-7]]:3, col:8>
+// CHECK-NEXT: | | | `-VarDecl {{.+}} <col:3, col:7> col:7 used f 'int'
+// CHECK-NEXT: | | `-ReturnStmt {{.+}} <line:[[@LINE-8]]:3, col:10>
+// CHECK-NEXT: | |   `-ImplicitCastExpr {{.+}} <col:10> 'int' <LValueToRValue>
+// CHECK-NEXT: | |     `-DeclRefExpr {{.+}} <col:10> 'int' lvalue Var {{.+}} 'f' 'int'
+// CHECK-NEXT: | `-OMPDeclareTargetDeclAttr {{.+}} <line:[[@LINE-14]]:21> Implicit MT_To DT_Any 1
+
+int dx;
+
+extern int dx;
+#pragma omp declare target to(dx)
+
+// CHECK:      |-VarDecl {{.+}} <line:[[@LINE-5]]:1, col:5> col:5 dx 'int'
+// CHECK-NEXT: | `-OMPDeclareTargetDeclAttr {{.+}} <line:[[@LINE-3]]:31> Implicit MT_To DT_Any 4294967295
+// CHECK:      |-VarDecl {{.+}} prev {{.+}} <line:[[@LINE-5]]:1, col:12> col:12 dx 'int' extern
+// CHECK-NEXT: | `-OMPDeclareTargetDeclAttr {{.+}} <line:[[@LINE-5]]:31> Implicit MT_To DT_Any 4294967295
+
+int dy;
+
+#pragma omp begin declare target
+extern int dy;
+#pragma omp end declare target
+
+// CHECK:      |-VarDecl {{.+}} <line:[[@LINE-6]]:1, col:5> col:5 dy 'int'
+// CHECK-NEXT: | `-OMPDeclareTargetDeclAttr {{.+}} <line:[[@LINE-5]]:27> Implicit MT_To DT_Any 1
+// CHECK:      `-VarDecl {{.+}} prev {{.+}} <line:[[@LINE-5]]:1, col:12> col:12 dy 'int' extern
+// CHECK-NEXT:   `-OMPDeclareTargetDeclAttr {{.+}} <line:[[@LINE-7]]:27> Implicit MT_To DT_Any 1
Index: clang/lib/Sema/SemaOpenMP.cpp
===================================================================
--- clang/lib/Sema/SemaOpenMP.cpp
+++ clang/lib/Sema/SemaOpenMP.cpp
@@ -22986,6 +22986,9 @@
       Context, MT, DTCI.DT, IndirectE, IsIndirect, Level,
       SourceRange(Loc, Loc));
   ND->addAttr(A);
+  if (auto *CD = dyn_cast<NamedDecl>(ND->getCanonicalDecl()))
+    if (!CD->hasAttr<OMPDeclareTargetDeclAttr>())
+      CD->addAttr(A);
   if (ASTMutationListener *ML = Context.getASTMutationListener())
     ML->DeclarationMarkedOpenMPDeclareTarget(ND, A);
   checkDeclIsAllowedInOpenMPTarget(nullptr, ND, Loc);
@@ -23090,6 +23093,9 @@
             DTCI.DT, IndirectE, IsIndirect, Level,
             SourceRange(DTCI.Loc, DTCI.Loc));
         D->addAttr(A);
+        if (auto *CD = dyn_cast<NamedDecl>(D->getCanonicalDecl()))
+          if (!CD->hasAttr<OMPDeclareTargetDeclAttr>())
+            CD->addAttr(A);
         if (ASTMutationListener *ML = Context.getASTMutationListener())
           ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
       }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D153369.532979.patch
Type: text/x-patch
Size: 3486 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230620/9c1bc4d7/attachment.bin>


More information about the cfe-commits mailing list