From 796f9309bb30ef2f234f9549f6131cb501a57459 Mon Sep 17 00:00:00 2001 From: bruteforceboy Date: Thu, 27 Feb 2025 13:46:07 +0300 Subject: [PATCH 1/3] add synthetic fix --- clang/lib/CIR/CodeGen/CIRGenCall.cpp | 1 + clang/lib/CIR/CodeGen/CIRGenCleanup.cpp | 10 ++++++---- clang/test/CIR/CodeGen/string.cpp | 12 ++++++++++++ 3 files changed, 19 insertions(+), 4 deletions(-) create mode 100644 clang/test/CIR/CodeGen/string.cpp diff --git a/clang/lib/CIR/CodeGen/CIRGenCall.cpp b/clang/lib/CIR/CodeGen/CIRGenCall.cpp index 54c0e185648b..20500482cb7c 100644 --- a/clang/lib/CIR/CodeGen/CIRGenCall.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenCall.cpp @@ -507,6 +507,7 @@ emitCallLikeOp(CIRGenFunction &CGF, mlir::Location callLoc, // handler: unwind. auto *r = result.addRegion(); builder.createBlock(r); + builder.create(loc, mlir::Value{}, mlir::Value{}); }); op.setSynthetic(true); return op; diff --git a/clang/lib/CIR/CodeGen/CIRGenCleanup.cpp b/clang/lib/CIR/CodeGen/CIRGenCleanup.cpp index 534fc2a59968..5d4199fb42bb 100644 --- a/clang/lib/CIR/CodeGen/CIRGenCleanup.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenCleanup.cpp @@ -666,10 +666,12 @@ void CIRGenFunction::PopCleanupBlock(bool FallthroughIsBranchThrough) { if (nextAction == ehResumeBlock) { if (auto tryToPatch = currYield->getParentOp()->getParentOfType()) { - mlir::Block *resumeBlockToPatch = - tryToPatch.getCatchUnwindEntryBlock(); - emitEHResumeBlock(/*isCleanup=*/true, resumeBlockToPatch, - tryToPatch.getLoc()); + if (!tryToPatch.getSynthetic()) { + mlir::Block *resumeBlockToPatch = + tryToPatch.getCatchUnwindEntryBlock(); + emitEHResumeBlock(/*isCleanup=*/true, resumeBlockToPatch, + tryToPatch.getLoc()); + } } } diff --git a/clang/test/CIR/CodeGen/string.cpp b/clang/test/CIR/CodeGen/string.cpp new file mode 100644 index 000000000000..b7b7349dd304 --- /dev/null +++ b/clang/test/CIR/CodeGen/string.cpp @@ -0,0 +1,12 @@ +// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -fcxx-exceptions -fexceptions -I%S/../Inputs -fclangir -emit-cir %s -o %t.cir +// RUN: FileCheck --input-file=%t.cir %s + +#include "std-cxx.h" + +void foo(const char* path) { + std::string str = path; + str = path; + str = path; +} + +// CHECK: cir From cd8969e05707bb1ea75f96f36f4e8158da21995e Mon Sep 17 00:00:00 2001 From: bruteforceboy Date: Thu, 27 Feb 2025 14:54:28 +0300 Subject: [PATCH 2/3] update test --- clang/test/CIR/CodeGen/string.cpp | 41 ++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/clang/test/CIR/CodeGen/string.cpp b/clang/test/CIR/CodeGen/string.cpp index b7b7349dd304..0c93d22ecd07 100644 --- a/clang/test/CIR/CodeGen/string.cpp +++ b/clang/test/CIR/CodeGen/string.cpp @@ -3,10 +3,49 @@ #include "std-cxx.h" +// CHECK-LABEL: @_Z3fooPKc + void foo(const char* path) { std::string str = path; str = path; str = path; } -// CHECK: cir +// CHECK: cir.try synthetic cleanup { +// CHECK: cir.call exception @_ZNSbIcEC1EPKcRKNS_9AllocatorE({{.*}}, {{.*}}, {{.*}}) : (!cir.ptr, !cir.ptr, !cir.ptr) -> () cleanup { +// CHECK: cir.call @_ZNSbIcED1Ev({{.*}}) : (!cir.ptr) -> () +// CHECK: cir.yield +// CHECK: } +// CHECK: cir.yield +// CHECK: } catch [#cir.unwind { +// CHECK: cir.resume +// CHECK: }] +// CHECK: cir.try synthetic cleanup { +// CHECK: {{.*}} = cir.call exception @_ZNSbIcEaSERKS_({{.*}}, {{.*}}) : (!cir.ptr, !cir.ptr) -> !cir.ptr cleanup { +// CHECK: cir.call @_ZNSbIcED1Ev({{.*}}) : (!cir.ptr) -> () +// CHECK: cir.yield +// CHECK: } +// CHECK: cir.store {{.*}}, {{.*}} : !cir.ptr, !cir.ptr> +// CHECK: cir.yield +// CHECK: } catch [#cir.unwind { +// CHECK: cir.resume +// CHECK: }] +// CHECK: {{.*}} = cir.load {{.*}} : !cir.ptr>, !cir.ptr +// CHECK: cir.call @_ZNSbIcED1Ev({{.*}}) : (!cir.ptr) -> () +// CHECK: cir.try synthetic cleanup { +// CHECK: cir.call exception @_ZNSbIcEC1EPKcRKNS_9AllocatorE({{.*}}, {{.*}}, {{.*}}) : (!cir.ptr, !cir.ptr, !cir.ptr) -> () +// CHECK: cir.yield +// CHECK: } catch [#cir.unwind { +// CHECK: cir.resume +// CHECK: }] +// CHECK: cir.try synthetic cleanup { +// CHECK: {{.*}} = cir.call exception @_ZNSbIcEaSERKS_({{.*}}, {{.*}}) : (!cir.ptr, !cir.ptr) -> !cir.ptr cleanup { +// CHECK: cir.call @_ZNSbIcED1Ev({{.*}}) : (!cir.ptr) -> () +// CHECK: cir.call @_ZNSbIcED1Ev({{.*}}) : (!cir.ptr) -> () +// CHECK: cir.yield +// CHECK: } +// CHECK: cir.store {{.*}}, {{.*}} : !cir.ptr, !cir.ptr> +// CHECK: cir.yield +// CHECK: } catch [#cir.unwind { +// CHECK: cir.resume +// CHECK: }] From a19a558bb310351b064207299c7fa975d63bdb46 Mon Sep 17 00:00:00 2001 From: bruteforceboy Date: Fri, 28 Feb 2025 12:47:09 +0300 Subject: [PATCH 3/3] apply requested changes --- clang/test/CIR/CodeGen/string.cpp | 51 ----------- .../test/CIR/CodeGen/synthetic-try-resume.cpp | 89 +++++++++++++++++++ 2 files changed, 89 insertions(+), 51 deletions(-) delete mode 100644 clang/test/CIR/CodeGen/string.cpp create mode 100644 clang/test/CIR/CodeGen/synthetic-try-resume.cpp diff --git a/clang/test/CIR/CodeGen/string.cpp b/clang/test/CIR/CodeGen/string.cpp deleted file mode 100644 index 0c93d22ecd07..000000000000 --- a/clang/test/CIR/CodeGen/string.cpp +++ /dev/null @@ -1,51 +0,0 @@ -// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -fcxx-exceptions -fexceptions -I%S/../Inputs -fclangir -emit-cir %s -o %t.cir -// RUN: FileCheck --input-file=%t.cir %s - -#include "std-cxx.h" - -// CHECK-LABEL: @_Z3fooPKc - -void foo(const char* path) { - std::string str = path; - str = path; - str = path; -} - -// CHECK: cir.try synthetic cleanup { -// CHECK: cir.call exception @_ZNSbIcEC1EPKcRKNS_9AllocatorE({{.*}}, {{.*}}, {{.*}}) : (!cir.ptr, !cir.ptr, !cir.ptr) -> () cleanup { -// CHECK: cir.call @_ZNSbIcED1Ev({{.*}}) : (!cir.ptr) -> () -// CHECK: cir.yield -// CHECK: } -// CHECK: cir.yield -// CHECK: } catch [#cir.unwind { -// CHECK: cir.resume -// CHECK: }] -// CHECK: cir.try synthetic cleanup { -// CHECK: {{.*}} = cir.call exception @_ZNSbIcEaSERKS_({{.*}}, {{.*}}) : (!cir.ptr, !cir.ptr) -> !cir.ptr cleanup { -// CHECK: cir.call @_ZNSbIcED1Ev({{.*}}) : (!cir.ptr) -> () -// CHECK: cir.yield -// CHECK: } -// CHECK: cir.store {{.*}}, {{.*}} : !cir.ptr, !cir.ptr> -// CHECK: cir.yield -// CHECK: } catch [#cir.unwind { -// CHECK: cir.resume -// CHECK: }] -// CHECK: {{.*}} = cir.load {{.*}} : !cir.ptr>, !cir.ptr -// CHECK: cir.call @_ZNSbIcED1Ev({{.*}}) : (!cir.ptr) -> () -// CHECK: cir.try synthetic cleanup { -// CHECK: cir.call exception @_ZNSbIcEC1EPKcRKNS_9AllocatorE({{.*}}, {{.*}}, {{.*}}) : (!cir.ptr, !cir.ptr, !cir.ptr) -> () -// CHECK: cir.yield -// CHECK: } catch [#cir.unwind { -// CHECK: cir.resume -// CHECK: }] -// CHECK: cir.try synthetic cleanup { -// CHECK: {{.*}} = cir.call exception @_ZNSbIcEaSERKS_({{.*}}, {{.*}}) : (!cir.ptr, !cir.ptr) -> !cir.ptr cleanup { -// CHECK: cir.call @_ZNSbIcED1Ev({{.*}}) : (!cir.ptr) -> () -// CHECK: cir.call @_ZNSbIcED1Ev({{.*}}) : (!cir.ptr) -> () -// CHECK: cir.yield -// CHECK: } -// CHECK: cir.store {{.*}}, {{.*}} : !cir.ptr, !cir.ptr> -// CHECK: cir.yield -// CHECK: } catch [#cir.unwind { -// CHECK: cir.resume -// CHECK: }] diff --git a/clang/test/CIR/CodeGen/synthetic-try-resume.cpp b/clang/test/CIR/CodeGen/synthetic-try-resume.cpp new file mode 100644 index 000000000000..69a407dcb31e --- /dev/null +++ b/clang/test/CIR/CodeGen/synthetic-try-resume.cpp @@ -0,0 +1,89 @@ +// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fcxx-exceptions -fexceptions -I%S/../Inputs -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 -fcxx-exceptions -fexceptions -I%S/../Inputs -fclangir -emit-llvm %s -o %t.ll +// RUN: FileCheck --check-prefix=LLVM --input-file=%t.ll %s + +#include "std-cxx.h" + +// CIR-LABEL: @_Z3fooPKc +// LLVM-LABEL: @_Z3fooPKc + +void foo(const char* path) { + std::string str = path; + str = path; + str = path; +} + +// CIR: cir.try synthetic cleanup { +// CIR: cir.call exception @_ZNSbIcEC1EPKcRKNS_9AllocatorE({{.*}}, {{.*}}, {{.*}}) : (!cir.ptr, !cir.ptr, !cir.ptr) -> () cleanup { +// CIR: cir.call @_ZNSbIcED1Ev({{.*}}) : (!cir.ptr) -> () +// CIR: cir.yield +// CIR: } +// CIR: cir.yield +// CIR: } catch [#cir.unwind { +// CIR: cir.resume +// CIR: }] +// CIR: cir.try synthetic cleanup { +// CIR: {{.*}} = cir.call exception @_ZNSbIcEaSERKS_({{.*}}, {{.*}}) : (!cir.ptr, !cir.ptr) -> !cir.ptr cleanup { +// CIR: cir.call @_ZNSbIcED1Ev({{.*}}) : (!cir.ptr) -> () +// CIR: cir.yield +// CIR: } +// CIR: cir.store {{.*}}, {{.*}} : !cir.ptr, !cir.ptr> +// CIR: cir.yield +// CIR: } catch [#cir.unwind { +// CIR: cir.resume +// CIR: }] +// CIR: {{.*}} = cir.load {{.*}} : !cir.ptr>, !cir.ptr +// CIR: cir.call @_ZNSbIcED1Ev({{.*}}) : (!cir.ptr) -> () +// CIR: cir.try synthetic cleanup { +// CIR: cir.call exception @_ZNSbIcEC1EPKcRKNS_9AllocatorE({{.*}}, {{.*}}, {{.*}}) : (!cir.ptr, !cir.ptr, !cir.ptr) -> () +// CIR: cir.yield +// CIR: } catch [#cir.unwind { +// CIR: cir.resume +// CIR: }] +// CIR: cir.try synthetic cleanup { +// CIR: {{.*}} = cir.call exception @_ZNSbIcEaSERKS_({{.*}}, {{.*}}) : (!cir.ptr, !cir.ptr) -> !cir.ptr cleanup { +// CIR: cir.call @_ZNSbIcED1Ev({{.*}}) : (!cir.ptr) -> () +// CIR: cir.call @_ZNSbIcED1Ev({{.*}}) : (!cir.ptr) -> () +// CIR: cir.yield +// CIR: } +// CIR: cir.store {{.*}}, {{.*}} : !cir.ptr, !cir.ptr> +// CIR: cir.yield +// CIR: } catch [#cir.unwind { +// CIR: cir.resume +// CIR: }] + +// LLVM: invoke void @_ZNSbIcEC1EPKcRKNS_9AllocatorE(ptr {{.*}}, ptr {{.*}}, ptr {{.*}}) +// LLVM: to label {{.*}} unwind label %[[B18:.*]] +// LLVM: [[B18]] +// LLVM: call void @_ZNSbIcED1Ev(ptr {{.*}}) +// LLVM: br label %[[B22:.*]] +// LLVM: [[B22]] +// LLVM: resume { ptr, i32 } {{.*}} +// LLVM: {{.*}}: +// LLVM: {{.*}} = invoke ptr @_ZNSbIcEaSERKS_(ptr {{.*}}, ptr {{.*}}) +// LLVM: to label {{.*}} unwind label %[[B31:.*]] +// LLVM: [[B31]] +// LLVM: call void @_ZNSbIcED1Ev(ptr {{.*}}) +// LLVM: br label %[[B35:.*]] +// LLVM: [[B35]] +// LLVM: resume { ptr, i32 } {{.*}} +// LLVM: {{.*}}: +// LLVM: call void @_ZNSbIcED1Ev(ptr {{.*}}) +// LLVM: br label {{.*}} +// LLVM: {{.*}}: +// LLVM: invoke void @_ZNSbIcEC1EPKcRKNS_9AllocatorE(ptr {{.*}}, ptr {{.*}}, ptr {{.*}}) +// LLVM: to label {{.*}} unwind label %[[B46:.*]] +// LLVM: [[B46]] +// LLVM: br label %[[B50:.*]] +// LLVM: [[B50]] +// LLVM: resume { ptr, i32 } {{.*}} +// LLVM: {{.*}}: +// LLVM: {{.*}} = invoke ptr @_ZNSbIcEaSERKS_(ptr {{.*}}, ptr {{.*}}) +// LLVM: to label {{.*}} unwind label %[[B59:.*]] +// LLVM: [[B59]] +// LLVM: call void @_ZNSbIcED1Ev(ptr {{.*}}) +// LLVM: call void @_ZNSbIcED1Ev(ptr {{.*}}) +// LLVM: br label %[[B63:.*]] +// LLVM: [[B63]] +// LLVM: resume { ptr, i32 } {{.*}}