jhuber6 created this revision. jhuber6 added reviewers: JonChesterfield, jdoerfert, tianshilei1992, ye-luo, RaviNarayanaswamy, ABataev. Herald added subscribers: sunshaoce, guansong, yaxunl. Herald added a project: All. jhuber6 requested review of this revision. Herald added subscribers: cfe-commits, jplehr, sstefan1. Herald added a project: clang.
Currently, the precense of the OpenMP target declare metadata requires that we always codegen a global declaration. This is undesirable in the case that we could defer or omit this declaration as is common with unused extern variables. This is important as it allows us, in the runtime, to rely on static linking semantics to omit unused symbols so they are not included when the user links it in. This patch changes the check for always emitting these variables. Because of this we also need to extend this logic to the generation of the offloading entries. This has the result of derring the offload entry generation to the canonical definitoin. So we are effectively assuming whoever owns the storage for this variable will perform that operation. This makes an exception for `link` attributes as those require their own special handling. Let me know if this is sound in the implementation, I do not have the largest view of the standards here. Fixes: https://github.com/llvm/llvm-project/issues/64133 Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D156368 Files: clang/lib/CodeGen/CGOpenMPRuntime.cpp clang/lib/CodeGen/CodeGenModule.cpp clang/test/OpenMP/declare_target_codegen.cpp Index: clang/test/OpenMP/declare_target_codegen.cpp =================================================================== --- clang/test/OpenMP/declare_target_codegen.cpp +++ clang/test/OpenMP/declare_target_codegen.cpp @@ -29,7 +29,6 @@ // CHECK-DAG: @flag = protected global i8 undef, // CHECK-DAG: @dx = {{protected | }}global i32 0, // CHECK-DAG: @dy = {{protected | }}global i32 0, -// CHECK-DAG: @aaa = external global i32, // CHECK-DAG: @bbb = {{protected | }}global i32 0, // CHECK-DAG: weak constant %struct.__tgt_offload_entry { ptr @bbb, // CHECK-DAG: @ccc = external global i32, @@ -80,7 +79,7 @@ extern int aaa; int bbb = 0; extern int ccc; -int ddd = 0; +int ddd = ccc; #pragma omp end declare target #pragma omp declare target @@ -260,8 +259,6 @@ // CHECK-NOT: define {{.*}}{{baz1|baz4|maini1|Base|virtual_}} -// CHECK-DAG: !{i32 1, !"aaa", i32 0, i32 {{[0-9]+}}} -// CHECK-DAG: !{i32 1, !"ccc", i32 0, i32 {{[0-9]+}}} // CHECK-DAG: !{{{.+}}virtual_foo #ifdef OMP5 Index: clang/lib/CodeGen/CodeGenModule.cpp =================================================================== --- clang/lib/CodeGen/CodeGenModule.cpp +++ clang/lib/CodeGen/CodeGenModule.cpp @@ -3605,6 +3605,13 @@ // Emit declaration of the must-be-emitted declare target variable. if (std::optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) { + + // If this variable has external storage and doesn't require special + // link handling we defer to its canonical definition. + if (VD->hasExternalStorage() && + Res != OMPDeclareTargetDeclAttr::MT_Link) + return; + bool UnifiedMemoryEnabled = getOpenMPRuntime().hasRequiresUnifiedSharedMemory(); if ((*Res == OMPDeclareTargetDeclAttr::MT_To || Index: clang/lib/CodeGen/CGOpenMPRuntime.cpp =================================================================== --- clang/lib/CodeGen/CGOpenMPRuntime.cpp +++ clang/lib/CodeGen/CGOpenMPRuntime.cpp @@ -10166,6 +10166,13 @@ std::optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD); + + // If this is an 'extern' declaration we defer to the canonical definition and + // do not emit an offloading entry. + if (Res && *Res != OMPDeclareTargetDeclAttr::MT_Link && + VD->hasExternalStorage()) + return; + if (!Res) { if (CGM.getLangOpts().OpenMPIsTargetDevice) { // Register non-target variables being emitted in device code (debug info
Index: clang/test/OpenMP/declare_target_codegen.cpp =================================================================== --- clang/test/OpenMP/declare_target_codegen.cpp +++ clang/test/OpenMP/declare_target_codegen.cpp @@ -29,7 +29,6 @@ // CHECK-DAG: @flag = protected global i8 undef, // CHECK-DAG: @dx = {{protected | }}global i32 0, // CHECK-DAG: @dy = {{protected | }}global i32 0, -// CHECK-DAG: @aaa = external global i32, // CHECK-DAG: @bbb = {{protected | }}global i32 0, // CHECK-DAG: weak constant %struct.__tgt_offload_entry { ptr @bbb, // CHECK-DAG: @ccc = external global i32, @@ -80,7 +79,7 @@ extern int aaa; int bbb = 0; extern int ccc; -int ddd = 0; +int ddd = ccc; #pragma omp end declare target #pragma omp declare target @@ -260,8 +259,6 @@ // CHECK-NOT: define {{.*}}{{baz1|baz4|maini1|Base|virtual_}} -// CHECK-DAG: !{i32 1, !"aaa", i32 0, i32 {{[0-9]+}}} -// CHECK-DAG: !{i32 1, !"ccc", i32 0, i32 {{[0-9]+}}} // CHECK-DAG: !{{{.+}}virtual_foo #ifdef OMP5 Index: clang/lib/CodeGen/CodeGenModule.cpp =================================================================== --- clang/lib/CodeGen/CodeGenModule.cpp +++ clang/lib/CodeGen/CodeGenModule.cpp @@ -3605,6 +3605,13 @@ // Emit declaration of the must-be-emitted declare target variable. if (std::optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) { + + // If this variable has external storage and doesn't require special + // link handling we defer to its canonical definition. + if (VD->hasExternalStorage() && + Res != OMPDeclareTargetDeclAttr::MT_Link) + return; + bool UnifiedMemoryEnabled = getOpenMPRuntime().hasRequiresUnifiedSharedMemory(); if ((*Res == OMPDeclareTargetDeclAttr::MT_To || Index: clang/lib/CodeGen/CGOpenMPRuntime.cpp =================================================================== --- clang/lib/CodeGen/CGOpenMPRuntime.cpp +++ clang/lib/CodeGen/CGOpenMPRuntime.cpp @@ -10166,6 +10166,13 @@ std::optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD); + + // If this is an 'extern' declaration we defer to the canonical definition and + // do not emit an offloading entry. + if (Res && *Res != OMPDeclareTargetDeclAttr::MT_Link && + VD->hasExternalStorage()) + return; + if (!Res) { if (CGM.getLangOpts().OpenMPIsTargetDevice) { // Register non-target variables being emitted in device code (debug info
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits