r338399 - [OPENMP] Prevent problems with linking of the static variables.

Alexey Bataev via cfe-commits cfe-commits at lists.llvm.org
Tue Jul 31 09:40:15 PDT 2018


Author: abataev
Date: Tue Jul 31 09:40:15 2018
New Revision: 338399

URL: http://llvm.org/viewvc/llvm-project?rev=338399&view=rev
Log:
[OPENMP] Prevent problems with linking of the static variables.

No need to change the linkage, we can avoid the problem using special variable. That points to the original variable and, thus, prevent some of the optimizations that might break the compilation.

Modified:
    cfe/trunk/lib/AST/ASTContext.cpp
    cfe/trunk/lib/CodeGen/CGOpenMPRuntime.cpp
    cfe/trunk/test/OpenMP/declare_target_codegen.cpp
    cfe/trunk/test/OpenMP/nvptx_declare_target_var_ctor_dtor_codegen.cpp

Modified: cfe/trunk/lib/AST/ASTContext.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/ASTContext.cpp?rev=338399&r1=338398&r2=338399&view=diff
==============================================================================
--- cfe/trunk/lib/AST/ASTContext.cpp (original)
+++ cfe/trunk/lib/AST/ASTContext.cpp Tue Jul 31 09:40:15 2018
@@ -9535,21 +9535,6 @@ static GVALinkage basicGVALinkageForFunc
   return GVA_DiscardableODR;
 }
 
-static bool isDeclareTargetToDeclaration(const Decl *VD) {
-  for (const Decl *D : VD->redecls()) {
-    if (!D->hasAttrs())
-      continue;
-    if (const auto *Attr = D->getAttr<OMPDeclareTargetDeclAttr>())
-      return Attr->getMapType() == OMPDeclareTargetDeclAttr::MT_To;
-  }
-  if (const auto *V = dyn_cast<VarDecl>(VD)) {
-    if (const VarDecl *TD = V->getTemplateInstantiationPattern())
-      return isDeclareTargetToDeclaration(TD);
-  }
-
-  return false;
-}
-
 static GVALinkage adjustGVALinkageForAttributes(const ASTContext &Context,
                                                 const Decl *D, GVALinkage L) {
   // See http://msdn.microsoft.com/en-us/library/xa0d9ste.aspx
@@ -9566,12 +9551,6 @@ static GVALinkage adjustGVALinkageForAtt
     // visible externally so they can be launched from host.
     if (L == GVA_DiscardableODR || L == GVA_Internal)
       return GVA_StrongODR;
-  } else if (Context.getLangOpts().OpenMP && Context.getLangOpts().OpenMPIsDevice &&
-             isDeclareTargetToDeclaration(D)) {
-    // Static variables must be visible externally so they can be mapped from
-    // host.
-    if (L == GVA_Internal)
-      return GVA_StrongODR;
   }
   return L;
 }

Modified: cfe/trunk/lib/CodeGen/CGOpenMPRuntime.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGOpenMPRuntime.cpp?rev=338399&r1=338398&r2=338399&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGOpenMPRuntime.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGOpenMPRuntime.cpp Tue Jul 31 09:40:15 2018
@@ -8109,6 +8109,19 @@ void CGOpenMPRuntime::registerTargetGlob
       VarName = CGM.getMangledName(VD);
       VarSize = CGM.getContext().getTypeSizeInChars(VD->getType());
       Linkage = CGM.getLLVMLinkageVarDefinition(VD, /*IsConstant=*/false);
+      // Temp solution to prevent optimizations of the internal variables.
+      if (CGM.getLangOpts().OpenMPIsDevice && !VD->isExternallyVisible()) {
+        std::string RefName = getName({VarName, "ref"});
+        if (!CGM.GetGlobalValue(RefName)) {
+          llvm::Constant *AddrRef =
+              getOrCreateInternalVariable(Addr->getType(), RefName);
+          auto *GVAddrRef = cast<llvm::GlobalVariable>(AddrRef);
+          GVAddrRef->setConstant(/*Val=*/true);
+          GVAddrRef->setLinkage(llvm::GlobalValue::InternalLinkage);
+          GVAddrRef->setInitializer(Addr);
+          CGM.addCompilerUsedGlobal(GVAddrRef);
+        }
+      }
       break;
     case OMPDeclareTargetDeclAttr::MT_Link:
       Flags = OffloadEntriesInfoManagerTy::OMPTargetGlobalVarEntryLink;

Modified: cfe/trunk/test/OpenMP/declare_target_codegen.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/OpenMP/declare_target_codegen.cpp?rev=338399&r1=338398&r2=338399&view=diff
==============================================================================
--- cfe/trunk/test/OpenMP/declare_target_codegen.cpp (original)
+++ cfe/trunk/test/OpenMP/declare_target_codegen.cpp Tue Jul 31 09:40:15 2018
@@ -18,14 +18,15 @@
 // CHECK-DAG: @d = global i32 0,
 // CHECK-DAG: @c = external global i32,
 // CHECK-DAG: @globals = global %struct.S zeroinitializer,
-// CHECK-DAG: @{{.+}}stat = weak_odr global %struct.S zeroinitializer,
-// CHECK-DAG: @llvm.used = appending global [2 x i8*] [i8* bitcast (void ()* @__omp_offloading__{{.+}}_globals_l[[@LINE+42]]_ctor to i8*), i8* bitcast (void ()* @__omp_offloading__{{.+}}_stat_l[[@LINE+43]]_ctor to i8*)], section "llvm.metadata"
+// CHECK-DAG: [[STAT:@.+stat]] = internal global %struct.S zeroinitializer,
+// CHECK-DAG: [[STAT_REF:@.+]] = internal constant %struct.S* [[STAT]]
+// CHECK-DAG: @llvm.used = appending global [2 x i8*] [i8* bitcast (void ()* @__omp_offloading__{{.+}}_globals_l[[@LINE+42]]_ctor to i8*), i8* bitcast (void ()* @__omp_offloading__{{.+}}_stat_l[[@LINE+43]]_ctor to i8*)],
+// CHECK-DAG: @llvm.compiler.used = appending global [1 x i8*] [i8* bitcast (%struct.S** [[STAT_REF]] to i8*)],
 
 // CHECK-DAG: define {{.*}}i32 @{{.*}}{{foo|bar|baz2|baz3|FA|f_method}}{{.*}}()
 // CHECK-DAG: define {{.*}}void @{{.*}}TemplateClass{{.*}}(%class.TemplateClass* %{{.*}})
 // CHECK-DAG: define {{.*}}i32 @{{.*}}TemplateClass{{.*}}f_method{{.*}}(%class.TemplateClass* %{{.*}})
-// CHECK-DAG: define {{.*}}void @__omp_offloading__{{.*}}_globals_l[[@LINE+37]]_ctor()
-// CHECK-DAG: define {{.*}}void @__omp_offloading__{{.*}}_stat_l[[@LINE+37]]_ctor()
+// CHECK-DAG: define {{.*}}void @__omp_offloading__{{.*}}_globals_l[[@LINE+36]]_ctor()
 
 #ifndef HEADER
 #define HEADER

Modified: cfe/trunk/test/OpenMP/nvptx_declare_target_var_ctor_dtor_codegen.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/OpenMP/nvptx_declare_target_var_ctor_dtor_codegen.cpp?rev=338399&r1=338398&r2=338399&view=diff
==============================================================================
--- cfe/trunk/test/OpenMP/nvptx_declare_target_var_ctor_dtor_codegen.cpp (original)
+++ cfe/trunk/test/OpenMP/nvptx_declare_target_var_ctor_dtor_codegen.cpp Tue Jul 31 09:40:15 2018
@@ -15,7 +15,7 @@
 
 // SIMD-ONLY-NOT: {{__kmpc|__tgt}}
 
-// DEVICE-DAG: [[C_ADDR:.+]] = weak_odr global i32 0,
+// DEVICE-DAG: [[C_ADDR:.+]] = internal global i32 0,
 // DEVICE-DAG: [[CD_ADDR:@.+]] = global %struct.S zeroinitializer,
 // HOST-DAG: @[[C_ADDR:.+]] = internal global i32 0,
 // HOST-DAG: @[[CD_ADDR:.+]] = global %struct.S zeroinitializer,




More information about the cfe-commits mailing list