llvmorg-github-actions[bot] wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Morris Hafner (mmha)

<details>
<summary>Changes</summary>

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

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


2 Files Affected:

- (modified) clang/lib/CIR/CodeGen/CIRGenFunction.cpp (+12) 
- (added) clang/test/CIR/CodeGen/trivial-abi-callee-destroy.cpp (+75) 


``````````diff
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

``````````

</details>


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

Reply via email to