https://github.com/mmha updated https://github.com/llvm/llvm-project/pull/245032
>From 11151b0d0049b269354b530dd3219c587008477a Mon Sep 17 00:00:00 2001 From: Morris Hafner <[email protected]> Date: Wed, 18 Nov 2026 02:07:09 +0000 Subject: [PATCH 1/2] [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 >From 1f51f538d5ba2eb7141818cf9aec3a056e1a3e43 Mon Sep 17 00:00:00 2001 From: Morris Hafner <[email protected]> Date: Sun, 22 Nov 2026 20:57:40 +0000 Subject: [PATCH 2/2] Address test feedback --- .../CodeGen/trivial-abi-callee-destroy.cpp | 75 --------------- clang/test/CIR/CodeGen/trivial-abi.cpp | 96 +++++++++++++------ 2 files changed, 66 insertions(+), 105 deletions(-) delete mode 100644 clang/test/CIR/CodeGen/trivial-abi-callee-destroy.cpp diff --git a/clang/test/CIR/CodeGen/trivial-abi-callee-destroy.cpp b/clang/test/CIR/CodeGen/trivial-abi-callee-destroy.cpp deleted file mode 100644 index 5e2078e92ca558..00000000000000 --- a/clang/test/CIR/CodeGen/trivial-abi-callee-destroy.cpp +++ /dev/null @@ -1,75 +0,0 @@ -// 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 diff --git a/clang/test/CIR/CodeGen/trivial-abi.cpp b/clang/test/CIR/CodeGen/trivial-abi.cpp index 478fde16411700..40741bef7469be 100644 --- a/clang/test/CIR/CodeGen/trivial-abi.cpp +++ b/clang/test/CIR/CodeGen/trivial-abi.cpp @@ -1,9 +1,9 @@ // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c++11 -fclangir -emit-cir %s -o %t.cir // RUN: FileCheck --check-prefix=CIR --input-file=%t.cir %s // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c++11 -fclangir -emit-llvm %s -o %t-cir.ll -// RUN: FileCheck --check-prefix=LLVM --input-file=%t-cir.ll %s +// RUN: FileCheck --check-prefixes=CHECK,LLVM --input-file=%t-cir.ll %s // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c++11 -emit-llvm %s -o %t.ll -// RUN: FileCheck --check-prefix=OGCG --input-file=%t.ll %s +// RUN: FileCheck --check-prefixes=CHECK,OGCG --input-file=%t.ll %s // All 17 cases from clang/test/CodeGenCXX/trivial_abi.cpp, adapted for // x86_64-unknown-linux-gnu with CIR/LLVM/OGCG checks. @@ -60,12 +60,15 @@ Small D0::m0() { return {}; } void testParamSmall(Small a) noexcept {} // CIR-LABEL: cir.func {{.*}} @_Z14testParamSmall5Small +// CIR: %[[A:.+]] = cir.alloca "a" +// CIR: cir.cleanup.scope { +// CIR: } cleanup normal { +// CIR-NEXT: cir.call @_ZN5SmallD1Ev(%[[A]]) // CIR: cir.return -// LLVM-LABEL: define {{.*}} void @_Z14testParamSmall5Small( -// LLVM: ret void -// OGCG-LABEL: define {{.*}} void @_Z14testParamSmall5Small(ptr %a.coerce) -// OGCG: call {{.*}} @_ZN5SmallD1Ev( -// OGCG: ret void +// CHECK-LABEL: define {{.*}} void @_Z14testParamSmall5Small(ptr %{{[^,)]+}}) +// CHECK: %[[A:.+]] = alloca %struct.Small +// CHECK: call void @_ZN5SmallD1Ev(ptr {{.*}} %[[A]]) +// CHECK: ret void // --- Case 3: testReturnSmall --- @@ -89,17 +92,21 @@ void testCallSmall0() { } // CIR-LABEL: cir.func {{.*}} @_Z14testCallSmall0v -// CIR: cir.call @_ZN5SmallC1Ev +// CIR: %[[T:.+]] = cir.alloca "t" +// CIR: cir.call @_ZN5SmallC1Ev(%[[T]]) // CIR: cir.call @_ZN5SmallC1ERKS_ // CIR: cir.call @_Z14testParamSmall5Small -// LLVM-LABEL: define {{.*}} void @_Z14testCallSmall0v( -// LLVM: call void @_ZN5SmallC1Ev( -// LLVM: call void @_ZN5SmallC1ERKS_( -// LLVM: call void @_Z14testParamSmall5Small( -// OGCG-LABEL: define {{.*}} void @_Z14testCallSmall0v( -// OGCG: call {{.*}} @_ZN5SmallC1Ev( -// OGCG: call {{.*}} @_ZN5SmallC1ERKS_( -// OGCG: call void @_Z14testParamSmall5Small( +// CIR: cir.call @_ZN5SmallD1Ev(%[[T]]) +// CIR-NOT: cir.call @_ZN5SmallD1Ev +// CIR: cir.return +// CHECK-LABEL: define {{.*}} void @_Z14testCallSmall0v( +// CHECK: %[[T:.+]] = alloca %struct.Small +// CHECK: call void @_ZN5SmallC1Ev(ptr {{.*}} %[[T]]) +// CHECK: call void @_ZN5SmallC1ERKS_( +// CHECK: call void @_Z14testParamSmall5Small( +// CHECK: call void @_ZN5SmallD1Ev(ptr {{.*}} %[[T]]) +// CHECK-NOT: call void @_ZN5SmallD1Ev( +// CHECK: ret void // --- Case 5: testCallSmall1 (pass returned value directly) --- @@ -138,12 +145,16 @@ void testIgnoredSmall() { void testParamLarge(Large a) noexcept {} // CIR-LABEL: cir.func {{.*}} @_Z14testParamLarge5Large +// CIR: %[[A:.+]] = cir.alloca "a" +// CIR: cir.cleanup.scope { +// CIR: } cleanup normal { +// CIR-NEXT: cir.call @_ZN5LargeD1Ev(%[[A]]) // CIR: cir.return -// LLVM-LABEL: define {{.*}} void @_Z14testParamLarge5Large( -// LLVM: ret void -// OGCG-LABEL: define {{.*}} void @_Z14testParamLarge5Large(ptr noundef byval(%struct.Large) align 8 %a) -// OGCG: call {{.*}} @_ZN5LargeD1Ev( -// OGCG: ret void +// CHECK-LABEL: define {{.*}} void @_Z14testParamLarge5Large(ptr noundef byval(%struct.Large) align 8 +// OGCG-SAME: %[[A:[^,)]+]]) +// LLVM: %[[A:.+]] = alloca %struct.Large +// CHECK: call void @_ZN5LargeD1Ev(ptr {{.*}} %[[A]]) +// CHECK: ret void // --- Case 8: testReturnLarge --- @@ -167,17 +178,21 @@ void testCallLarge0() { } // CIR-LABEL: cir.func {{.*}} @_Z14testCallLarge0v -// CIR: cir.call @_ZN5LargeC1Ev +// CIR: %[[T:.+]] = cir.alloca "t" +// CIR: cir.call @_ZN5LargeC1Ev(%[[T]]) // CIR: cir.call @_ZN5LargeC1ERKS_ // CIR: cir.call @_Z14testParamLarge5Large -// LLVM-LABEL: define {{.*}} void @_Z14testCallLarge0v( -// LLVM: call void @_ZN5LargeC1Ev( -// LLVM: call void @_ZN5LargeC1ERKS_( -// LLVM: call void @_Z14testParamLarge5Large( -// OGCG-LABEL: define {{.*}} void @_Z14testCallLarge0v( -// OGCG: call {{.*}} @_ZN5LargeC1Ev( -// OGCG: call {{.*}} @_ZN5LargeC1ERKS_( -// OGCG: call void @_Z14testParamLarge5Large( +// CIR: cir.call @_ZN5LargeD1Ev(%[[T]]) +// CIR-NOT: cir.call @_ZN5LargeD1Ev +// CIR: cir.return +// CHECK-LABEL: define {{.*}} void @_Z14testCallLarge0v( +// CHECK: %[[T:.+]] = alloca %struct.Large +// CHECK: call void @_ZN5LargeC1Ev(ptr {{.*}} %[[T]]) +// CHECK: call void @_ZN5LargeC1ERKS_( +// CHECK: call void @_Z14testParamLarge5Large( +// CHECK: call void @_ZN5LargeD1Ev(ptr {{.*}} %[[T]]) +// CHECK-NOT: call void @_ZN5LargeD1Ev( +// CHECK: ret void // --- Case 10: testCallLarge1 (pass returned value directly) --- @@ -304,6 +319,27 @@ void g(S* s) { new(s) S(f()); } // OGCG-LABEL: define {{.*}} void @_ZN7GH930401gEPNS_1SE( // OGCG: call void @_ZN7GH930401fEv(ptr {{.*}}sret( +// --- testParamSmallReturns: the callee destroys the parameter on every return path --- + +int testParamSmallReturns(Small a, bool c) noexcept { + if (c) + return 1; + return 2; +} + +// CIR-LABEL: cir.func {{.*}} @_Z21testParamSmallReturns5Smallb +// CIR: %[[A:.+]] = cir.alloca "a" +// CIR: cir.cleanup.scope { +// CIR: cir.return +// CIR: cir.return +// CIR: } cleanup normal { +// CIR-NEXT: cir.call @_ZN5SmallD1Ev(%[[A]]) +// CHECK-LABEL: define {{.*}} i32 @_Z21testParamSmallReturns5Smallb( +// CHECK: %[[A:.+]] = alloca %struct.Small +// CHECK: call void @_ZN5SmallD1Ev(ptr {{.*}} %[[A]]) +// CHECK-NOT: call void @_ZN5SmallD1Ev( +// CHECK: ret i32 + // Lambda __invoke comes last (internal linkage). // CIR-LABEL: cir.func {{.*}} @{{.*}}__invokeEv // CIR: cir.call @{{.*}}clEv _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
