From: Alexey Bataev Date: Mon, 16 Jul 2018 20:05:25 +0000 (+0000) Subject: [OPENMP] Fix checks for declare target link entries. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=7acd2b806207042702f4cece726c377ce7c236ec;p=clang [OPENMP] Fix checks for declare target link entries. 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. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@337207 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/CodeGen/CGOpenMPRuntime.cpp b/lib/CodeGen/CGOpenMPRuntime.cpp index d348816040..5594dd4438 100644 --- a/lib/CodeGen/CGOpenMPRuntime.cpp +++ b/lib/CodeGen/CGOpenMPRuntime.cpp @@ -3980,16 +3980,39 @@ void CGOpenMPRuntime::createOffloadEntriesAndInfoMetadata() { } else if (const auto *CE = dyn_cast(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( + 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(getAddrOfDeclareTargetLink(VD).getPointer()); } - VarName = Addr->getName(); VarSize = CGM.getPointerSize(); Linkage = llvm::GlobalValue::WeakAnyLinkage; break; diff --git a/lib/CodeGen/CGOpenMPRuntime.h b/lib/CodeGen/CGOpenMPRuntime.h index 599b3ba01f..01ff0c20fd 100644 --- a/lib/CodeGen/CGOpenMPRuntime.h +++ b/lib/CodeGen/CGOpenMPRuntime.h @@ -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. diff --git a/test/OpenMP/declare_target_link_codegen.cpp b/test/OpenMP/declare_target_link_codegen.cpp index b377408455..fdecd728ce 100644 --- a/test/OpenMP/declare_target_link_codegen.cpp +++ b/test/OpenMP/declare_target_link_codegen.cpp @@ -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