]> granicus.if.org Git - clang/commitdiff
[OPENMP] Mark global tors/dtors as used.
authorAlexey Bataev <a.bataev@hotmail.com>
Wed, 9 May 2018 14:15:18 +0000 (14:15 +0000)
committerAlexey Bataev <a.bataev@hotmail.com>
Wed, 9 May 2018 14:15:18 +0000 (14:15 +0000)
If the global variables are marked as declare target and they need
ctors/dtors, these ctors/dtors are emitted and then invoked by the
offloading runtime library. They are not explicitly used in the emitted
code and thus can be optimized out. Patch marks these functions as used,
so the optimizer cannot remove these function during the optimization
phase.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@331879 91177308-0d34-0410-b5e6-96231b3b80d8

lib/CodeGen/CGOpenMPRuntime.cpp
test/OpenMP/declare_target_codegen.cpp

index 59c0540e89c8a9024a0cdb9d1ee95beeaabcaeed..35f27b2320911124ff89e79544866aa60ebe24eb 100644 (file)
@@ -2686,6 +2686,7 @@ bool CGOpenMPRuntime::emitDeclareTargetVarDefinition(const VarDecl *VD,
       CtorCGF.FinishFunction();
       Ctor = Fn;
       ID = llvm::ConstantExpr::getBitCast(Fn, CGM.Int8PtrTy);
+      CGM.addUsedGlobal(cast<llvm::GlobalValue>(Ctor));
     } else {
       Ctor = new llvm::GlobalVariable(
           CGM.getModule(), CGM.Int8Ty, /*isConstant=*/true,
@@ -2724,6 +2725,7 @@ bool CGOpenMPRuntime::emitDeclareTargetVarDefinition(const VarDecl *VD,
       DtorCGF.FinishFunction();
       Dtor = Fn;
       ID = llvm::ConstantExpr::getBitCast(Fn, CGM.Int8PtrTy);
+      CGM.addUsedGlobal(cast<llvm::GlobalValue>(Dtor));
     } else {
       Dtor = new llvm::GlobalVariable(
           CGM.getModule(), CGM.Int8Ty, /*isConstant=*/true,
index f6ee670115d213da702519dae88ec73cc571794e..02ac148e491960561cddac1a8ecfe5e3456c1bc7 100644 (file)
 // CHECK-DAG: @b = global i32 15,
 // CHECK-DAG: @d = global i32 0,
 // CHECK-DAG: @c = external global i32,
+// CHECK-DAG: @globals = global %struct.S zeroinitializer,
+// CHECK-DAG: @llvm.used = appending global [1 x i8*] [i8* bitcast (void ()* @__omp_offloading__{{.+}}_globals_l[[@LINE+41]]_ctor to i8*)], section "llvm.metadata"
 
 // 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+36]]_ctor()
 
 #ifndef HEADER
 #define HEADER
@@ -56,6 +59,7 @@ struct S {
 int foo() { return 0; }
 int b = 15;
 int d;
+S globals(d);
 #pragma omp end declare target
 int c;