Fix clang module lambda mangling #235458
<!--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:
````````````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>
<!--LLVM CODE FORMAT COMMENT: {clang-format}-->
:white_check_mark: With the latest revision this PR passed the C/C++ code formatter.
How is this related with 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.
This basically says we should change the mangling for lambda. We should be pretty careful about mangling changes. Why is this correct?
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.
Oh yeah, my bad, you're totally right. I will first try to prove that this change is right and come with the modifications.
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.
This duplicates lines 1968-1971 in the else branch below. Could PrefixND be computed once for both cases?
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?
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.
ChuanqiXu9
mihaizegheru
Fix lambda mangling collisions in function templates in named modules. The
D == RDbranch ofmangleLocalName()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.