← Back to Pull requests

[Clang][OpenMP] Skip offload metadata emission during function finalization #245735

Open
amad00r opened this pull request with 53 lines diff across 4 files
View on GitHub
amad00r amad00r commented on Nov 24, 2026 19:13 UTC
Author

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.

Diff: 53 lines (+52 / -1) across 4 files Reviewer: alexey-bataev Review duration: 8786m
All Vouch review confidence checks passed
Residual risk 0.03 = Change Risk (0.31) × (1 − Review Confidence (0.90)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.31 · 53 lines diff across 4 files
0.31
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.90 · 3 substantive comment(s)
0.90
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
89%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.239
Real PR Conversation Timeline
5 items
llvmorg-github-actions[bot] llvmorg-github-actions[bot] commented on Nov 24, 2026 19:13 UTC
Bot

<!--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:

  • (added) clang/test/OpenMP/irbuilder_target_offload_metadata.c (+17)

  • (modified) llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp (+2-1)

  • (modified) llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp (+30)
  • ``````

    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>

    github-actions[bot] github-actions[bot] commented on Nov 24, 2026 19:15 UTC
    Bot

    <!--LLVM CODE FORMAT COMMENT: {clang-format}-->

    :white_check_mark: With the latest revision this PR passed the C/C++ code formatter.

    alexey-bataev alexey-bataev commented on Nov 26, 2026 21:46 UTC
    Reviewer

    Update whats new and openmpsupport docs

    amad00r amad00r commented on Nov 30, 2026 19:41 UTC
    Author

    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.

    Reviewers
    Assignees
    amad00r amad00r
    Labels
    risk: low confidence: 89.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 31.2% (LOW) · Higher = larger blast radius
    31.2%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 89.5% · Lower = rushed or shallow review
    89.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 3.3% · Safe to merge
    3.3%