[Clang][OpenMP] Skip offload metadata emission during function finalization #245735
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-flang-openmp
Author: Amadeu Moya Sardà (amad00r)
<details>
<summary>Changes</summary>
With -fopenmp-enable-irbuilder, Clang calls finalize(CurFn) after generating each function. Emitting module-wide offload entries there, and again at module completion, duplicates host offload metadata. Device compilation then fails with "All ordered entries must exist!" because duplicate records inflate the entry count.
Only emit offload entries from finalize() when no function is supplied.
Follow-up to commit 48c3ae5 @<!-- -->agozillon.
Fixes #<!-- -->242591.
Assisted by gpt-6.
---
Full diff: https://github.com/llvm/llvm-project/pull/245735.diff
3 Files Affected:
````````````diff --git a/clang/test/OpenMP/irbuilder_target_offload_metadata.c b/clang/test/OpenMP/irbuilder_target_offload_metadata.c
new file mode 100644
index 00000000000000..456b1d68591591
--- /dev/null
+++ b/clang/test/OpenMP/irbuilder_target_offload_metadata.c
@@ -0,0 +1,17 @@
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fopenmp -fopenmp-enable-irbuilder -fopenmp-targets=x86_64-unknown-linux-gnu -emit-llvm-bc %s -o %t-host.bc
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fopenmp -fopenmp-enable-irbuilder -fopenmp-is-target-device -fopenmp-host-ir-file-path %t-host.bc -emit-llvm %s -o - | FileCheck %s --check-prefix=DEVICE
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fopenmp -fopenmp-enable-irbuilder -fopenmp-targets=x86_64-unknown-linux-gnu -emit-llvm %s -o - | FileCheck %s --check-prefix=HOST
+
+// With OMPIRBuilder enabled, this target region must compile for both the host
+// and the device. Previously, the host recorded the same target region
+// multiple times in its offload metadata, causing device compilation to crash.
+void foo(void) {
+#pragma omp target
+ {}
+}
+
+// The host metadata must list this target region exactly once.
+// HOST: !omp_offload.info = !{![[ENTRY:[0-9]+]]}{{$}}
+// HOST: ![[ENTRY]] = !{i32 0, {{.*}}!"foo", {{.*}}}
+
+// DEVICE-LABEL: define {{.*}}@__omp_offloading_{{.*}}_foo_l{{[0-9]+}}(
diff --git a/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp b/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
index 40a90840603f80..3460c44c65698f 100644
--- a/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
+++ b/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
@@ -1029,7 +1029,8 @@ void OpenMPIRBuilder::finalize(Function *Fn) {
"OMPIRBuilder finalization \n";
};
- if (!OffloadInfoManager.empty())
+ // Function finalization must not emit module-wide offload entries.
+ if (!Fn && !OffloadInfoManager.empty())
createOffloadEntriesAndInfoMetadata(ErrorReportFn);
// Rewrite uses of globals to their replacement declare target globals if
diff --git a/llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp b/llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
index 45a244a1d1ab31..3c15319fb39bce 100644
--- a/llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
+++ b/llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
@@ -8533,6 +8533,36 @@ TEST_F(OpenMPIRBuilderTest, OffloadEntriesInfoManager) {
EXPECT_TRUE(InfoManager.hasDeviceGlobalVarEntryInfo("gvar"));
}
+TEST_F(OpenMPIRBuilderTest, FinalizeFunctionDoesNotEmitOffloadMetadata) {
+ OpenMPIRBuilder OMPBuilder(*M);
+ OMPBuilder.initialize();
+ OMPBuilder.setConfig(
+ OpenMPIRBuilderConfig(false, false, false, false, false, false, false));
+
+ F->setName("target_region");
+ IRBuilder<> Builder(BB);
+ Builder.CreateRetVoid();
+ TargetRegionEntryInfo EntryInfo("parent", 1, 2, 4, 0);
+ OMPBuilder.registerTargetRegionFunction(EntryInfo, F, F->getName(),
+ "target_region_id");
+
+ // Finishing one function must leave offload metadata emission until the
+ // module is finalized.
+ OMPBuilder.finalize(F);
+ EXPECT_EQ(M->getNamedMetadata("omp_offload.info"), nullptr);
+ EXPECT_EQ(M->getNamedGlobal(".offloading.entry.target_region"), nullptr);
+
+ OMPBuilder.finalize();
+ NamedMDNode *OffloadMetadata = M->getNamedMetadata("omp_offload.info");
+ ASSERT_NE(OffloadMetadata, nullptr);
+ ASSERT_EQ(OffloadMetadata->getNumOperands(), 1u);
+ EXPECT_EQ(cast<MDString>(OffloadMetadata->getOperand(0)->getOperand(3))
+ ->getString(),
+ "parent");
+ EXPECT_NE(M->getNamedGlobal(".offloading.entry.target_region"), nullptr);
+ EXPECT_FALSE(verifyModule(*M, &errs()));
+}
+
// Tests both registerTargetGlobalVariable and getAddrOfDeclareTargetVar as they
// call each other (recursively in some cases). The test case test these
// functions by utilising them for host code generation for declare target
</details>
<!--LLVM CODE FORMAT COMMENT: {clang-format}-->
:white_check_mark: With the latest revision this PR passed the C/C++ code formatter.
Update whats new and openmpsupport docs
Thanks for the review @alexey-bataev.
I updated clang's release notes.
I think it is not necessary to update the OpenMPSupport.md because this is a bugfix.
alexey-bataev
amad00r
With -fopenmp-enable-irbuilder, Clang calls finalize(CurFn) after generating each function. Emitting module-wide offload entries there, and again at module completion, duplicates host offload metadata. Device compilation then fails with "All ordered entries must exist!" because duplicate records inflate the entry count.
Only emit offload entries from finalize() when no function is supplied.
Follow-up to commit 48c3ae5 @agozillon.
Fixes #242591.
Assisted by gpt-6.