← Back to Pull requests

Fix clang module lambda mangling #235458

Open
mihaizegheru opened this pull request with 53 lines diff across 2 files
View on GitHub
mihaizegheru mihaizegheru commented on Oct 25, 2026 14:39 UTC
Author

Fix lambda mangling collisions in function templates in named modules. The D == RD branch of mangleLocalName() omitted closure prefixes, allowing lambdas with the same mangling number in different contexts to produce identical names.

Use mangleNestedNameWithClosurePrefix() to preserve the prefix without repeating the enclosing function’s encoding. Add regression coverage for ordinary and generic lambdas, including mixed static/non-static locals and two static locals.

Fixes #234701.

Diff: 53 lines (+52 / -1) across 2 files Reviewer: ChuanqiXu9 Review duration: 58223m
All Vouch review confidence checks passed
Residual risk 0.04 = Change Risk (0.35) × (1 − Review Confidence (0.90)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.35 · 53 lines diff across 2 files
0.35
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.92 · 5 substantive comment(s)
0.92
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 50%
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
12 items
llvmorg-github-actions[bot] llvmorg-github-actions[bot] commented on Oct 25, 2026 14:40 UTC
Bot

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Mihai Zegheru (mihaizegheru)

<details>
<summary>Changes</summary>

Fix lambda mangling collisions in function templates in named modules. The D == RD branch of mangleLocalName() omitted closure prefixes, allowing lambdas with the same mangling number in different contexts to produce identical names.

Use mangleNestedNameWithClosurePrefix() to preserve the prefix without repeating the enclosing function’s encoding. Add regression coverage for ordinary and generic lambdas, including mixed static/non-static locals and two static locals.

Fixes #<!-- -->234701.

---
Full diff: https://github.com/llvm/llvm-project/pull/235458.diff

2 Files Affected:

  • (modified) clang/lib/AST/ItaniumMangle.cpp (+5-1)

  • (added) clang/test/CodeGenCXX/mangle-lambda-static-local-module.cpp (+46)
  • ``````

    diff --git a/clang/lib/AST/ItaniumMangle.cpp b/clang/lib/AST/ItaniumMangle.cpp
    index 93ba1519518d2c..95ab08ddb6931a 100644
    --- a/clang/lib/AST/ItaniumMangle.cpp
    +++ b/clang/lib/AST/ItaniumMangle.cpp
    @@ -1949,7 +1949,11 @@ void CXXNameMangler::mangleLocalName(GlobalDecl GD,
         // Mangle the name relative to the closest enclosing function.
         // equality ok because RD derived from ND above
         if (D == RD) {
    -      mangleUnqualifiedName(RD, DC, AdditionalAbiTags);
    +      const NamedDecl *PrefixND = getClosurePrefix(RD);
    +      if (PrefixND && !isCompatibleWith(LangOptions::ClangABI::Ver18))
    +        mangleNestedNameWithClosurePrefix(GD, PrefixND, AdditionalAbiTags, /*NoFunction=*/true);
    +      else
    +        mangleUnqualifiedName(RD, DC, AdditionalAbiTags);
         } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
           if (const NamedDecl *PrefixND = getClosurePrefix(BD))
             mangleClosurePrefix(PrefixND, true /*NoFunction*/);
    diff --git a/clang/test/CodeGenCXX/mangle-lambda-static-local-module.cpp b/clang/test/CodeGenCXX/mangle-lambda-static-local-module.cpp
    new file mode 100644
    index 00000000000000..5885a62f0593e2
    --- /dev/null
    +++ b/clang/test/CodeGenCXX/mangle-lambda-static-local-module.cpp
    @@ -0,0 +1,46 @@
    +// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c++23 \
    +// RUN:   -emit-llvm -o - %s | FileCheck %s
    +
    +export module mod;
    +
    +template <class F> struct Iterator {
    +  F fn;
    +
    +  decltype(auto) operator*() {
    +    return fn(char{});
    +  }
    +};
    +
    +template <typename> void bad_function() {
    +  auto lam1 = [](char n) { return 0; };
    +  static auto lam2 = [](char n) { return n; };
    +
    +  *Iterator{lam1};
    +  *Iterator{lam2};
    +}
    +
    +template <typename> int both_static() {
    +  static auto lam1 = [](char) { return 1; };
    +  static auto lam2 = [](char) { return 2; };
    +
    +  return *Iterator{lam1} + *Iterator{lam2};
    +}
    +
    +template <typename> int both_generic() {
    +  static constexpr auto lam1 = [](auto n) { return 0; };
    +  static constexpr auto lam2 = [](auto n) { return n; };
    +
    +  return *Iterator{lam1} + *Iterator{lam2};
    +}
    +
    +int repro() {
    +  bad_function<int>();
    +  return both_static<int>() + both_generic<int>();
    +}
    +
    +// CHECK-DAG: define{{.*}} i32 @_ZNW3mod8IteratorIZS_12bad_functionIiEvvEUlcE_EdeEv(
    +// CHECK-DAG: define{{.*}} i8 @_ZNW3mod8IteratorIZS_12bad_functionIiEvvEN4lam2MUlcE_EEdeEv(
    +// CHECK-DAG: define{{.*}} i32 @_ZNW3mod8IteratorIZS_11both_staticIiEivEN4lam1MUlcE_EEdeEv(
    +// CHECK-DAG: define{{.*}} i32 @_ZNW3mod8IteratorIZS_11both_staticIiEivEN4lam2MUlcE_EEdeEv(
    +// CHECK-DAG: define{{.*}} i32 @_ZNW3mod8IteratorIZS_12both_genericIiEivEN4lam1MUlT_E_EEdeEv(
    +// CHECK-DAG: define{{.*}} i8 @_ZNW3mod8IteratorIZS_12both_genericIiEivEN4lam2MUlT_E_EEdeEv(
    \ No newline at end of file
    
    
    ``````

    </details>

    github-actions[bot] github-actions[bot] commented on Oct 25, 2026 14:41 UTC
    Bot

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

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

    ChuanqiXu9 ChuanqiXu9 commented on code on Oct 26, 2026 09:18 UTC
    Code Review
    clang/lib/AST/ItaniumMangle.cpp:1
    @@ -2004,7 +1949,12 @@ void CXXNameMangler::mangleLocalName(GlobalDecl GD,
         // Mangle the name relative to the closest enclosing function.
         // equality ok because RD derived from ND above
         if (D == RD) {
    -      mangleUnqualifiedName(RD, DC, AdditionalAbiTags);
    +      const NamedDecl *PrefixND = getClosurePrefix(RD);
    +      if (PrefixND && !isCompatibleWith(LangOptions::ClangABI::Ver18))

    How is this related with ClangABI::Ver18?

    mihaizegheru mihaizegheru commented on code on Oct 26, 2026 09:41 UTC
    Code Review
    clang/lib/AST/ItaniumMangle.cpp:1
    @@ -2004,7 +1949,12 @@ void CXXNameMangler::mangleLocalName(GlobalDecl GD,
         // Mangle the name relative to the closest enclosing function.
         // equality ok because RD derived from ND above
         if (D == RD) {
    -      mangleUnqualifiedName(RD, DC, AdditionalAbiTags);
    +      const NamedDecl *PrefixND = getClosurePrefix(RD);
    +      if (PrefixND && !isCompatibleWith(LangOptions::ClangABI::Ver18))

    Please corect me if I am wrong, but IIUC mangleNestedNameWithClosurePrefix() should always be guarded by that in order to avoid downstream breakages for older binary files.

    ChuanqiXu9 ChuanqiXu9 commented on Oct 27, 2026 03:00 UTC
    Contributor

    This basically says we should change the mangling for lambda. We should be pretty careful about mangling changes. Why is this correct?

    zyn0217 zyn0217 commented on code on Oct 27, 2026 06:47 UTC
    Code Review
    clang/lib/AST/ItaniumMangle.cpp:1
    @@ -2004,7 +1949,12 @@ void CXXNameMangler::mangleLocalName(GlobalDecl GD,
         // Mangle the name relative to the closest enclosing function.
         // equality ok because RD derived from ND above
         if (D == RD) {
    -      mangleUnqualifiedName(RD, DC, AdditionalAbiTags);
    +      const NamedDecl *PrefixND = getClosurePrefix(RD);
    +      if (PrefixND && !isCompatibleWith(LangOptions::ClangABI::Ver18))

    You changed ABI while trying to maintain the behavior for clang 18-, which is insufficient because 19~23 would be broken.
    This should be Ver23 IMO.

    mihaizegheru mihaizegheru commented on code on Oct 27, 2026 07:10 UTC
    Code Review
    clang/lib/AST/ItaniumMangle.cpp:1
    @@ -2004,7 +1949,12 @@ void CXXNameMangler::mangleLocalName(GlobalDecl GD,
         // Mangle the name relative to the closest enclosing function.
         // equality ok because RD derived from ND above
         if (D == RD) {
    -      mangleUnqualifiedName(RD, DC, AdditionalAbiTags);
    +      const NamedDecl *PrefixND = getClosurePrefix(RD);
    +      if (PrefixND && !isCompatibleWith(LangOptions::ClangABI::Ver18))

    Oh yeah, my bad, you're totally right. I will first try to prove that this change is right and come with the modifications.

    zahiraam zahiraam commented on code on Nov 13, 2026 15:38 UTC
    Code Review
    clang/lib/AST/ItaniumMangle.cpp:1
    @@ -2004,7 +1949,12 @@ void CXXNameMangler::mangleLocalName(GlobalDecl GD,
         // Mangle the name relative to the closest enclosing function.
         // equality ok because RD derived from ND above
         if (D == RD) {
    -      mangleUnqualifiedName(RD, DC, AdditionalAbiTags);
    +      const NamedDecl *PrefixND = getClosurePrefix(RD);
    +      if (PrefixND && !isCompatibleWith(LangOptions::ClangABI::Ver18))
    +        mangleNestedNameWithClosurePrefix(GD, PrefixND, AdditionalAbiTags,
    +                                          /*NoFunction=*/true);
    +      else
    +        mangleUnqualifiedName(RD, DC, AdditionalAbiTags);

    This changes names emitted by Clang 23 at the latest ABI, so I think -fclang-abi-compat=23 should keep the old ones. Shouldn't this be gated on !isCompatibleWith(LangOptions::ClangABI::Ver23)? If so, please also add a bullet under ABI_VER_MAJOR(23) in ABIVersions.def and a RN.

    zahiraam zahiraam commented on code on Nov 13, 2026 15:42 UTC
    Code Review
    clang/lib/AST/ItaniumMangle.cpp:1
    @@ -2004,7 +1949,12 @@ void CXXNameMangler::mangleLocalName(GlobalDecl GD,
         // Mangle the name relative to the closest enclosing function.
         // equality ok because RD derived from ND above
         if (D == RD) {
    -      mangleUnqualifiedName(RD, DC, AdditionalAbiTags);
    +      const NamedDecl *PrefixND = getClosurePrefix(RD);

    This duplicates lines 1968-1971 in the else branch below. Could PrefixND be computed once for both cases?

    zahiraam zahiraam commented on Nov 13, 2026 15:51 UTC
    Contributor

    Question: was it considered to number these lambdas within the enclosing function instead, so their names would be unique without the variable name? Or is there a reason they need the variable name?

    zahiraam zahiraam commented on code on Nov 13, 2026 15:54 UTC
    Code Review
    clang/test/CodeGenCXX/mangle-lambda-static-local-module.cpp:1
    @@ -0,0 +1,46 @@
    +// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c++23 \
    +// RUN:   -emit-llvm -o - %s | FileCheck %s
    +

    If this gets gated on Ver23 (see my comment in ItaniumMangle.cpp), please add a RUN line with -fclang-abi-compat=23 that checks the old names are kept.
    Nit: missing newline at end of file.

    Reviewers
    Assignees
    mihaizegheru mihaizegheru
    Labels
    risk: low confidence: 89.7% 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: 35.3% (MEDIUM) · Higher = larger blast radius
    35.3%
    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.7% · Lower = rushed or shallow review
    89.7%
    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.6% · Safe to merge
    3.6%