[OPENMP] Fix checks for declare target link entries.
authorAlexey Bataev <a.bataev@hotmail.com>
Mon, 16 Jul 2018 20:05:25 +0000 (20:05 +0000)
committerAlexey Bataev <a.bataev@hotmail.com>
Mon, 16 Jul 2018 20:05:25 +0000 (20:05 +0000)
If the declare target link entries are created but not used, the
compiler will produce an error message. Patch improves handling of such
situations + improves checks for possibly lost declare target variables.

llvm-svn: 337207

clang/lib/CodeGen/CGOpenMPRuntime.cpp
clang/lib/CodeGen/CGOpenMPRuntime.h
clang/test/OpenMP/declare_target_link_codegen.cpp

index d348816..5594dd4 100644 (file)
@@ -3980,16 +3980,39 @@ void CGOpenMPRuntime::createOffloadEntriesAndInfoMetadata() {
     } else if (const auto *CE =
                    dyn_cast<OffloadEntriesInfoManagerTy::
                                 OffloadEntryInfoDeviceGlobalVar>(E)) {
-      if (!CE->getAddress()) {
-        unsigned DiagID = CGM.getDiags().getCustomDiagID(
-            DiagnosticsEngine::Error,
-            "Offloading entry for declare target variable is incorrect: the "
-            "address is invalid.");
-        CGM.getDiags().Report(DiagID);
-        continue;
+      OffloadEntriesInfoManagerTy::OMPTargetGlobalVarEntryKind Flags =
+          static_cast<OffloadEntriesInfoManagerTy::OMPTargetGlobalVarEntryKind>(
+              CE->getFlags());
+      switch (Flags) {
+      case OffloadEntriesInfoManagerTy::OMPTargetGlobalVarEntryTo: {
+        if (!CE->getAddress()) {
+          unsigned DiagID = CGM.getDiags().getCustomDiagID(
+              DiagnosticsEngine::Error,
+              "Offloading entry for declare target variable is incorrect: the "
+              "address is invalid.");
+          CGM.getDiags().Report(DiagID);
+          continue;
+        }
+        break;
+      }
+      case OffloadEntriesInfoManagerTy::OMPTargetGlobalVarEntryLink:
+        assert(((CGM.getLangOpts().OpenMPIsDevice && !CE->getAddress()) ||
+                (!CGM.getLangOpts().OpenMPIsDevice && CE->getAddress())) &&
+               "Declaret target link address is set.");
+        if (CGM.getLangOpts().OpenMPIsDevice)
+          continue;
+        if (!CE->getAddress()) {
+          unsigned DiagID = CGM.getDiags().getCustomDiagID(
+              DiagnosticsEngine::Error,
+              "Offloading entry for declare target variable is incorrect: the "
+              "address is invalid.");
+          CGM.getDiags().Report(DiagID);
+          continue;
+        }
+        break;
       }
       createOffloadEntry(CE->getAddress(), CE->getAddress(),
-                         CE->getVarSize().getQuantity(), CE->getFlags(),
+                         CE->getVarSize().getQuantity(), Flags,
                          CE->getLinkage());
     } else {
       llvm_unreachable("Unsupported entry kind.");
@@ -7889,14 +7912,15 @@ void CGOpenMPRuntime::registerTargetGlobalVariable(const VarDecl *VD,
       Linkage = CGM.getLLVMLinkageVarDefinition(VD, /*IsConstant=*/false);
       break;
     case OMPDeclareTargetDeclAttr::MT_Link:
-      // Map type 'to' because we do not map the original variable but the
-      // reference.
-      Flags = OffloadEntriesInfoManagerTy::OMPTargetGlobalVarEntryTo;
-      if (!CGM.getLangOpts().OpenMPIsDevice) {
+      Flags = OffloadEntriesInfoManagerTy::OMPTargetGlobalVarEntryLink;
+      if (CGM.getLangOpts().OpenMPIsDevice) {
+        VarName = Addr->getName();
+        Addr = nullptr;
+      } else {
+        VarName = getAddrOfDeclareTargetLink(VD).getName();
         Addr =
             cast<llvm::Constant>(getAddrOfDeclareTargetLink(VD).getPointer());
       }
-      VarName = Addr->getName();
       VarSize = CGM.getPointerSize();
       Linkage = llvm::GlobalValue::WeakAnyLinkage;
       break;
index 599b3ba..01ff0c2 100644 (file)
@@ -522,6 +522,8 @@ private:
     enum OMPTargetGlobalVarEntryKind : uint32_t {
       /// Mark the entry as a to declare target.
       OMPTargetGlobalVarEntryTo = 0x0,
+      /// Mark the entry as a to declare target link.
+      OMPTargetGlobalVarEntryLink = 0x1,
     };
 
     /// Device global variable entries info.
index b377408..fdecd72 100644 (file)
@@ -23,8 +23,9 @@
 // HOST: @c_decl_tgt_link_ptr = global i32* @c
 // HOST: [[SIZES:@.+]] = private unnamed_addr constant [2 x i64] [i64 4, i64 4]
 // HOST: [[MAPTYPES:@.+]] = private unnamed_addr constant [2 x i64] [i64 35, i64 531]
-// CHECK: @.omp_offloading.entry_name{{.*}} = internal unnamed_addr constant [{{[0-9]+}} x i8] c"c_decl_tgt_link_ptr\00"
-// CHECK: @.omp_offloading.entry.c_decl_tgt_link_ptr = weak constant %struct.__tgt_offload_entry { i8* bitcast (i32** @c_decl_tgt_link_ptr to i8*), i8* getelementptr inbounds ([{{[0-9]+}} x i8], [{{[0-9]+}} x i8]* @.omp_offloading.entry_name, i32 0, i32 0), i64 8, i32 0, i32 0 }, section ".omp_offloading.entries", align 1
+// HOST: @.omp_offloading.entry_name{{.*}} = internal unnamed_addr constant [{{[0-9]+}} x i8] c"c_decl_tgt_link_ptr\00"
+// HOST: @.omp_offloading.entry.c_decl_tgt_link_ptr = weak constant %struct.__tgt_offload_entry { i8* bitcast (i32** @c_decl_tgt_link_ptr to i8*), i8* getelementptr inbounds ([{{[0-9]+}} x i8], [{{[0-9]+}} x i8]* @.omp_offloading.entry_name, i32 0, i32 0), i64 8, i32 1, i32 0 }, section ".omp_offloading.entries", align 1
+// DEVICE-NOT: internal unnamed_addr constant [{{[0-9]+}} x i8] c"c_decl_tgt_link_ptr\00"
 // CHECK: @llvm.used = appending global [1 x i8*] [i8* bitcast (i32** @c_decl_tgt_link_ptr to i8*)]
 
 extern int c;
@@ -64,5 +65,5 @@ int maini1() {
 // HOST: [[C:%.*]] = load i32, i32* @c,
 // HOST: store i32 [[C]], i32* %
 
-// CHECK: !{i32 1, !"c_decl_tgt_link_ptr", i32 0, i32 {{[0-9]+}}}
+// CHECK: !{i32 1, !"c_decl_tgt_link_ptr", i32 1, i32 {{[0-9]+}}}
 #endif // HEADER