https://github.com/mmha created https://github.com/llvm/llvm-project/pull/245032

ClangIR never destroyed a parameter whose type is destroyed by the callee, that 
is, a `[[clang::trivial_abi]]` class with a non-trivial destructor. Its 
destructor never ran, so whatever it releases leaked.

The caller already leaves the destruction to the callee, as classic codegen 
does. But the callee's prologue did not push the destructor cleanup that 
classic codegen pushes in `EmitParmDecl`.

Push the destructor cleanup for such parameters in the prologue, so it runs on 
every return path. As in classic codegen, thunks don't get it.

Found by comparing ClangIR and classic codegen with alive2 on 
`clang/test/CIR/CodeGen/trivial-abi.cpp`.

Assisted-By: Claude Opus 5.5 and GPT-6.2 Sol as reviewer

>From 3801f8dd7661992cd82699f016349419d4b714a1 Mon Sep 17 00:00:00 2001
From: Morris Hafner <[email protected]>
Date: Wed, 18 Nov 2026 02:07:09 +0000
Subject: [PATCH] [CIR] Destroy callee-destroyed parameters in the callee

ClangIR never destroyed a parameter whose type is destroyed by the
callee, that is, a [[clang::trivial_abi]] class with a non-trivial
destructor. Its destructor never ran, so whatever it releases leaked.

The caller already leaves the destruction to the callee, as classic
codegen does. But the callee's prologue did not push the destructor
cleanup that classic codegen pushes in EmitParmDecl.

The parameter must be destroyed. C++20 [expr.call]p7 (N4861 7.6.1.2)
only leaves open when: at the return of the function or at the end of
the caller's full-expression. For trivial_abi types Clang chooses the
first, in the callee.

Push the destructor cleanup for such parameters in the prologue, so it
runs on every return path. As in classic codegen, thunks don't get it.

Found by comparing ClangIR and classic codegen with alive2 on
clang/test/CIR/CodeGen/trivial-abi.cpp (testParamSmall and
testParamLarge).
---
 clang/lib/CIR/CodeGen/CIRGenFunction.cpp      | 12 +++
 .../CodeGen/trivial-abi-callee-destroy.cpp    | 75 +++++++++++++++++++
 2 files changed, 87 insertions(+)
 create mode 100644 clang/test/CIR/CodeGen/trivial-abi-callee-destroy.cpp

diff --git a/clang/lib/CIR/CodeGen/CIRGenFunction.cpp 
b/clang/lib/CIR/CodeGen/CIRGenFunction.cpp
index c88a7b9523422a..e60ac143c6ecdd 100644
--- a/clang/lib/CIR/CodeGen/CIRGenFunction.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenFunction.cpp
@@ -549,6 +549,18 @@ void CIRGenFunction::emitFunctionProlog(const 
FunctionArgList &args,
 
     if (paramVar->hasAttr<AnnotateAttr>())
       emitVarAnnotations(paramVar, addr.getPointer());
+
+    QualType paramTy = paramVar->getType();
+    if (paramTy->isRecordType() && !curFuncIsThunk &&
+        paramTy->castAsRecordDecl()->isParamDestroyedInCallee()) {
+      if (QualType::DestructionKind dtorKind =
+              paramVar->needsDestruction(getContext())) {
+        assert((dtorKind == QualType::DK_cxx_destructor ||
+                dtorKind == QualType::DK_nontrivial_c_struct) &&
+               "unexpected destructor type");
+        pushDestroy(dtorKind, addr, paramTy);
+      }
+    }
   }
   assert(builder.getInsertionBlock() && "Should be valid");
 }
diff --git a/clang/test/CIR/CodeGen/trivial-abi-callee-destroy.cpp 
b/clang/test/CIR/CodeGen/trivial-abi-callee-destroy.cpp
new file mode 100644
index 00000000000000..5e2078e92ca558
--- /dev/null
+++ b/clang/test/CIR/CodeGen/trivial-abi-callee-destroy.cpp
@@ -0,0 +1,75 @@
+// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -fclangir 
-emit-cir %s -o %t.cir
+// RUN: FileCheck --input-file=%t.cir %s -check-prefix=CIR
+// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -fclangir 
-emit-llvm %s -o %t-cir.ll
+// RUN: FileCheck --input-file=%t-cir.ll %s -check-prefix=LLVM
+// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -emit-llvm %s 
-o %t.ll
+// RUN: FileCheck --input-file=%t.ll %s -check-prefix=OGCG
+
+struct __attribute__((trivial_abi)) Handle {
+  int *p;
+  Handle();
+  Handle(const Handle &);
+  ~Handle();
+};
+
+void take(Handle h) {}
+
+// CIR-LABEL: cir.func {{.*}} @_Z4take6Handle(
+// CIR:         %[[H:.+]] = cir.alloca "h"
+// CIR:         cir.call @_ZN6HandleD1Ev(%[[H]])
+// CIR:         cir.return
+
+// LLVM-LABEL: define {{.*}} void @_Z4take6Handle(
+// LLVM:         call void @_ZN6HandleD1Ev(ptr {{.*}} %[[H:.+]])
+// LLVM:         ret void
+
+// OGCG-LABEL: define {{.*}} void @_Z4take6Handle(
+// OGCG:         call void @_ZN6HandleD1Ev(ptr {{.*}} %h)
+// OGCG:         ret void
+
+int deref(Handle h) {
+  if (*h.p)
+    return 1;
+  return 2;
+}
+
+// CIR-LABEL: cir.func {{.*}} @_Z5deref6Handle(
+// CIR:         %[[H:.+]] = cir.alloca "h"
+// CIR:         cir.cleanup.scope {
+// CIR:           cir.return
+// CIR:           cir.return
+// CIR:         } cleanup normal {
+// CIR-NEXT:      cir.call @_ZN6HandleD1Ev(%[[H]])
+
+// LLVM-LABEL: define {{.*}} i32 @_Z5deref6Handle(
+// LLVM:         call void @_ZN6HandleD1Ev(
+// LLVM-NOT:     call void @_ZN6HandleD1Ev(
+// LLVM:         ret i32
+
+// OGCG-LABEL: define {{.*}} i32 @_Z5deref6Handle(
+// OGCG:         call void @_ZN6HandleD1Ev(
+// OGCG-NOT:     call void @_ZN6HandleD1Ev(
+// OGCG:         ret i32
+
+void caller() {
+  Handle h;
+  take(h);
+}
+
+// CIR-LABEL: cir.func {{.*}} @_Z6callerv(
+// CIR:         cir.call @_Z4take6Handle(
+// CIR:         cir.call @_ZN6HandleD1Ev(
+// CIR-NOT:     cir.call @_ZN6HandleD1Ev(
+// CIR:         cir.return
+
+// LLVM-LABEL: define {{.*}} void @_Z6callerv(
+// LLVM:         call void @_Z4take6Handle(
+// LLVM:         call void @_ZN6HandleD1Ev(
+// LLVM-NOT:     call void @_ZN6HandleD1Ev(
+// LLVM:         ret void
+
+// OGCG-LABEL: define {{.*}} void @_Z6callerv(
+// OGCG:         call void @_Z4take6Handle(
+// OGCG:         call void @_ZN6HandleD1Ev(
+// OGCG-NOT:     call void @_ZN6HandleD1Ev(
+// OGCG:         ret void

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to