diff --git a/clang/lib/CodeGen/CGOpenMPRuntime.cpp b/clang/lib/CodeGen/CGOpenMPRuntime.cpp --- a/clang/lib/CodeGen/CGOpenMPRuntime.cpp +++ b/clang/lib/CodeGen/CGOpenMPRuntime.cpp @@ -35,6 +35,7 @@ #include "llvm/IR/Constants.h" #include "llvm/IR/DerivedTypes.h" #include "llvm/IR/GlobalValue.h" +#include "llvm/IR/InstrTypes.h" #include "llvm/IR/Value.h" #include "llvm/Support/AtomicOrdering.h" #include "llvm/Support/Format.h" @@ -1940,8 +1941,12 @@ CtorCGF.StartFunction(GlobalDecl(), CGM.getContext().VoidTy, Fn, FI, FunctionArgList(), Loc, Loc); auto AL = ApplyDebugLocation::CreateArtificial(CtorCGF); + auto *AddrInAS0 = llvm::ConstantExpr::getAddrSpaceCast( + Addr, llvm::PointerType::getWithSamePointeeType( + cast(Addr->getType()), 0)); CtorCGF.EmitAnyExprToMem( - Init, Address::deprecated(Addr, CGM.getContext().getDeclAlign(VD)), + Init, + Address::deprecated(AddrInAS0, CGM.getContext().getDeclAlign(VD)), Init->getType().getQualifiers(), /*IsInitializer=*/true); CtorCGF.FinishFunction(); @@ -1980,9 +1985,12 @@ // Create a scope with an artificial location for the body of this // function. auto AL = ApplyDebugLocation::CreateArtificial(DtorCGF); + auto *AddrInAS0 = llvm::ConstantExpr::getAddrSpaceCast( + Addr, llvm::PointerType::getWithSamePointeeType( + cast(Addr->getType()), 0)); DtorCGF.emitDestroy( - Address::deprecated(Addr, CGM.getContext().getDeclAlign(VD)), ASTTy, - DtorCGF.getDestroyer(ASTTy.isDestructedType()), + Address::deprecated(AddrInAS0, CGM.getContext().getDeclAlign(VD)), + ASTTy, DtorCGF.getDestroyer(ASTTy.isDestructedType()), DtorCGF.needsEHCleanup(ASTTy.isDestructedType())); DtorCGF.FinishFunction(); Dtor = Fn; diff --git a/clang/test/OpenMP/amdgcn_target_global_constructor.cpp b/clang/test/OpenMP/amdgcn_target_global_constructor.cpp new file mode 100644 --- /dev/null +++ b/clang/test/OpenMP/amdgcn_target_global_constructor.cpp @@ -0,0 +1,21 @@ +// REQUIRES: amdgpu-registered-target + +// RUN: %clang_cc1 -fopenmp -x c++ -std=c++11 -triple x86_64-unknown-unknown -fopenmp-targets=amdgcn-amd-amdhsa -emit-llvm-bc %s -o %t-ppc-host.bc +// RUN: %clang_cc1 -fopenmp -x c++ -std=c++11 -triple amdgcn-amd-amdhsa -fopenmp-targets=amdgcn-amd-amdhsa -emit-llvm %s -fopenmp-is-device -fopenmp-host-ir-file-path %t-ppc-host.bc -o - | FileCheck %s +// expected-no-diagnostics +#ifndef HEADER +#define HEADER + +void foo(void); + +struct S { + int a; + S() : a(1) {} + ~S() { foo(); } +}; + +#pragma omp declare target +S A; +#pragma omp end declare target + +#endif