llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT--> @llvm/pr-subscribers-clang @llvm/pr-subscribers-clangir Author: Andy Kaylor (andykaylor) <details> <summary>Changes</summary> This implements EH cleanup handling that performs regular partial array destruction for array constructor loops with a destructed type. Because CIR represents array construction using an abstract operation, we do not go through the normal EH stack mechanism. Instead, we add a partial_dtor region to the cir.array.ctor operation indicating how a single element should be destructed. When the cir.array.ctor operation is expanded during LoweringPrepare, we create a cleanup scope with a partial array dtor loop in an EH cleanup region. This gets further expanded during CFG flattening to produce a control flow equivalent to that generated by classic codegen. Assisted-by: Cursor / claude-4.6-opus-high --- Patch is 26.52 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/190834.diff 6 Files Affected: - (modified) clang/include/clang/CIR/Dialect/IR/CIROps.td (+48-17) - (modified) clang/lib/CIR/CodeGen/CIRGenClass.cpp (+20-11) - (modified) clang/lib/CIR/Dialect/IR/CIRDialect.cpp (+88-1) - (modified) clang/lib/CIR/Dialect/Transforms/LoweringPrepare.cpp (+88-30) - (modified) clang/test/CIR/CodeGen/array-ctor.cpp (+13) - (added) clang/test/CIR/CodeGen/partial-array-cleanup.cpp (+163) ``````````diff diff --git a/clang/include/clang/CIR/Dialect/IR/CIROps.td b/clang/include/clang/CIR/Dialect/IR/CIROps.td index d2fd2df7ab7d1..bbe1ff71da9f3 100644 --- a/clang/include/clang/CIR/Dialect/IR/CIROps.td +++ b/clang/include/clang/CIR/Dialect/IR/CIROps.td @@ -4578,8 +4578,17 @@ def CIR_ArrayCtor : CIR_Op<"array.ctor"> { let summary = "Initialize array elements with C++ constructors"; let description = [{ Initialize each array element using the same C++ constructor. This - operation has one region, with one single block. The block has an - incoming argument for the current array element to initialize. + operation has a `body` region and an optional `partial_dtor` region. + Both regions have a single block whose argument is a pointer to the + current array element. + + The `body` region contains the constructor call for one element. + + The `partial_dtor` region, when non-empty, contains the destructor call + for one element. During lowering, it is used to build a cleanup that + destroys already-constructed elements if a constructor throws. When the + element type has a trivial destructor or exceptions are disabled, the + `partial_dtor` region is left empty. When `num_elements` is absent, `addr` must be a pointer to a fixed-size CIR array type and the element count is derived from that array type. @@ -4591,17 +4600,30 @@ def CIR_ArrayCtor : CIR_Op<"array.ctor"> { Examples: ```mlir + // Fixed size without partial destructor: cir.array.ctor(%0 : !cir.ptr<!cir.array<!rec_S x 42>>) { ^bb0(%arg0: !cir.ptr<!rec_S>): cir.call @some_ctor(%arg0) : (!cir.ptr<!rec_S>) -> () cir.yield } + // Variable size without partial destructor: cir.array.ctor(%ptr, %n : !cir.ptr<!rec_S>, !u64i) { ^bb0(%arg0: !cir.ptr<!rec_S>): cir.call @some_ctor(%arg0) : (!cir.ptr<!rec_S>) -> () cir.yield } + + // Fixed size with partial destructor: + cir.array.ctor(%0 : !cir.ptr<!cir.array<!rec_S x 42>>) { + ^bb0(%arg0: !cir.ptr<!rec_S>): + cir.call @some_ctor(%arg0) : (!cir.ptr<!rec_S>) -> () + cir.yield + } partial_dtor { + ^bb0(%arg0: !cir.ptr<!rec_S>): + cir.call @some_dtor(%arg0) : (!cir.ptr<!rec_S>) -> () + cir.yield + } ``` }]; @@ -4610,32 +4632,41 @@ def CIR_ArrayCtor : CIR_Op<"array.ctor"> { Optional<CIR_AnyIntType>:$num_elements ); - let regions = (region SizedRegion<1>:$body); - let assemblyFormat = [{ - $addr (`,` $num_elements^)? `:` qualified(type($addr)) - (`,` type($num_elements)^)? $body attr-dict - }]; + let regions = (region SizedRegion<1>:$body, AnyRegion:$partial_dtor); + let hasCustomAssemblyFormat = 1; let builders = [ // Static form: addr is ptr<array<T x N>>, no num_elements. OpBuilder<(ins "mlir::Value":$addr, - "llvm::function_ref<void(mlir::OpBuilder &, mlir::Location)>":$regionBuilder), [{ - assert(regionBuilder && "builder callback expected"); + "llvm::function_ref<void(mlir::OpBuilder &, mlir::Location)>":$bodyBuilder, + "llvm::function_ref<void(mlir::OpBuilder &, mlir::Location)>":$partialDtorBuilder), [{ + assert(bodyBuilder && "builder callback expected"); mlir::OpBuilder::InsertionGuard guard($_builder); - mlir::Region *r = $_state.addRegion(); $_state.addOperands(ValueRange{addr}); - $_builder.createBlock(r); - regionBuilder($_builder, $_state.location); + mlir::Region *body = $_state.addRegion(); + $_builder.createBlock(body); + bodyBuilder($_builder, $_state.location); + mlir::Region *partialDtor = $_state.addRegion(); + if (partialDtorBuilder) { + $_builder.createBlock(partialDtor); + partialDtorBuilder($_builder, $_state.location); + } }]>, // Dynamic form: addr is ptr<T>, num_elements is the runtime count. OpBuilder<(ins "mlir::Value":$addr, "mlir::Value":$num_elements, - "llvm::function_ref<void(mlir::OpBuilder &, mlir::Location)>":$regionBuilder), [{ - assert(regionBuilder && "builder callback expected"); + "llvm::function_ref<void(mlir::OpBuilder &, mlir::Location)>":$bodyBuilder, + "llvm::function_ref<void(mlir::OpBuilder &, mlir::Location)>":$partialDtorBuilder), [{ + assert(bodyBuilder && "builder callback expected"); mlir::OpBuilder::InsertionGuard guard($_builder); - mlir::Region *r = $_state.addRegion(); $_state.addOperands({addr, num_elements}); - $_builder.createBlock(r); - regionBuilder($_builder, $_state.location); + mlir::Region *body = $_state.addRegion(); + $_builder.createBlock(body); + bodyBuilder($_builder, $_state.location); + mlir::Region *partialDtor = $_state.addRegion(); + if (partialDtorBuilder) { + $_builder.createBlock(partialDtor); + partialDtorBuilder($_builder, $_state.location); + } }]> ]; diff --git a/clang/lib/CIR/CodeGen/CIRGenClass.cpp b/clang/lib/CIR/CodeGen/CIRGenClass.cpp index f8d65fa9a6d33..02e074200204d 100644 --- a/clang/lib/CIR/CodeGen/CIRGenClass.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenClass.cpp @@ -783,20 +783,15 @@ void CIRGenFunction::emitCXXAggrConstructorCall( { RunCleanupsScope scope(*this); - // Evaluate the constructor and its arguments in a regular - // partial-destroy cleanup. - if (getLangOpts().Exceptions && - !ctor->getParent()->hasTrivialDestructor()) { - cgm.errorNYI(e->getSourceRange(), "partial array cleanups"); - } + bool needsPartialArrayCleanup = + getLangOpts().Exceptions && + !ctor->getParent()->hasTrivialDestructor(); auto emitCtorBody = [&](mlir::OpBuilder &b, mlir::Location l) { mlir::BlockArgument arg = b.getInsertionBlock()->addArgument(ptrToElmType, l); Address curAddr = Address(arg, elementType, eltAlignment); assert(!cir::MissingFeatures::sanitizers()); - // Match CGClass::EmitCXXAggrConstructorCall: zero-initialize each element - // in the array-ctor loop before invoking the constructor for that slot. if (zeroInitialize) emitNullInitialization(l, curAddr, type); auto currAVS = AggValueSlot::forAddr( @@ -809,16 +804,30 @@ void CIRGenFunction::emitCXXAggrConstructorCall( cir::YieldOp::create(b, l); }; - // Emit the per-element initialization. + llvm::function_ref<void(mlir::OpBuilder &, mlir::Location)> + emitPartialDtorBody = nullptr; + auto partialDtorBuilder = [&](mlir::OpBuilder &b, mlir::Location l) { + mlir::BlockArgument arg = + b.getInsertionBlock()->addArgument(ptrToElmType, l); + Address curAddr = Address(arg, elementType, eltAlignment); + emitCXXDestructorCall(ctor->getParent()->getDestructor(), Dtor_Complete, + /*forVirtualBase=*/false, + /*delegating=*/false, curAddr, type); + cir::YieldOp::create(b, l); + }; + if (needsPartialArrayCleanup) + emitPartialDtorBody = partialDtorBuilder; + if (useDynamicArrayCtor) { cir::ArrayCtor::create(builder, loc, dynamicElPtr, numElements, - emitCtorBody); + emitCtorBody, emitPartialDtorBody); } else { cir::ArrayType arrayTy = cir::ArrayType::get(elementType, constElementCount); mlir::Value arrayOp = builder.createPtrBitcast(arrayBase.getPointer(), arrayTy); - cir::ArrayCtor::create(builder, loc, arrayOp, emitCtorBody); + cir::ArrayCtor::create(builder, loc, arrayOp, emitCtorBody, + emitPartialDtorBody); } } } diff --git a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp index ab1c0b453ac18..3e020e52ebece 100644 --- a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp +++ b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp @@ -341,7 +341,94 @@ template <typename Op> static LogicalResult verifyArrayCtorDtor(Op op) { return success(); } -LogicalResult cir::ArrayCtor::verify() { return verifyArrayCtorDtor(*this); } +ParseResult cir::ArrayCtor::parse(OpAsmParser &parser, OperationState &result) { + OpAsmParser::UnresolvedOperand addrOperand; + OpAsmParser::UnresolvedOperand numElementsOperand; + bool hasNumElements = false; + + if (parser.parseOperand(addrOperand)) + return failure(); + + if (succeeded(parser.parseOptionalComma())) { + if (parser.parseOperand(numElementsOperand)) + return failure(); + hasNumElements = true; + } + + if (parser.parseColon()) + return failure(); + + mlir::Type addrType; + if (parser.parseType(addrType)) + return failure(); + if (parser.resolveOperand(addrOperand, addrType, result.operands)) + return failure(); + + if (hasNumElements) { + if (parser.parseComma()) + return failure(); + mlir::Type numElementsType; + if (parser.parseType(numElementsType)) + return failure(); + if (parser.resolveOperand(numElementsOperand, numElementsType, + result.operands)) + return failure(); + } + + Region *body = result.addRegion(); + if (parser.parseRegion(*body)) + return failure(); + + Region *partialDtor = result.addRegion(); + if (!parser.parseOptionalKeyword("partial_dtor")) { + if (parser.parseRegion(*partialDtor)) + return failure(); + } + + if (parser.parseOptionalAttrDict(result.attributes)) + return failure(); + + return success(); +} + +void cir::ArrayCtor::print(OpAsmPrinter &p) { + p << " " << getAddr(); + if (getNumElements()) + p << ", " << getNumElements(); + + p << " : " << getAddr().getType(); + if (getNumElements()) + p << ", " << getNumElements().getType(); + + p << " "; + p.printRegion(getBody()); + + if (!getPartialDtor().empty()) { + p << " partial_dtor "; + p.printRegion(getPartialDtor()); + } + + p.printOptionalAttrDict(getOperation()->getAttrs()); +} + +LogicalResult cir::ArrayCtor::verify() { + if (failed(verifyArrayCtorDtor(*this))) + return failure(); + + mlir::Region &partialDtor = getPartialDtor(); + if (!partialDtor.empty()) { + mlir::Block &dtorBlock = partialDtor.front(); + if (dtorBlock.getNumArguments() != 1) + return emitOpError( + "partial_dtor must have exactly one block argument"); + + auto bodyArgTy = getBody().front().getArgument(0).getType(); + if (dtorBlock.getArgument(0).getType() != bodyArgTy) + return emitOpError("partial_dtor block argument type must match " + "the body block argument type"); + } + return success(); +} LogicalResult cir::ArrayDtor::verify() { return verifyArrayCtorDtor(*this); } //===----------------------------------------------------------------------===// diff --git a/clang/lib/CIR/Dialect/Transforms/LoweringPrepare.cpp b/clang/lib/CIR/Dialect/Transforms/LoweringPrepare.cpp index 91f082725711f..f5ab406789882 100644 --- a/clang/lib/CIR/Dialect/Transforms/LoweringPrepare.cpp +++ b/clang/lib/CIR/Dialect/Transforms/LoweringPrepare.cpp @@ -1369,13 +1369,16 @@ void LoweringPreparePass::buildCXXGlobalInitFunc() { cir::ReturnOp::create(builder, f.getLoc()); } +/// Lower a cir.array.ctor or cir.array.dtor into a do-while loop that +/// iterates over every element. For cir.array.ctor ops whose partial_dtor +/// region is non-empty, the ctor loop is wrapped in a cir.cleanup.scope whose +/// EH cleanup performs a reverse destruction loop using the partial dtor body. static void lowerArrayDtorCtorIntoLoop(cir::CIRBaseBuilderTy &builder, clang::ASTContext *astCtx, mlir::Operation *op, mlir::Type eltTy, mlir::Value addr, mlir::Value numElements, uint64_t arrayLen, bool isCtor) { - // Generate loop to call into ctor/dtor for every element. mlir::Location loc = op->getLoc(); bool isDynamic = numElements != nullptr; @@ -1432,7 +1435,7 @@ static void lowerArrayDtorCtorIntoLoop(cir::CIRBaseBuilderTy &builder, // Clone the region body (ctor/dtor call and any setup ops like per-element // zero-init) into the loop, remapping the block argument to the current - // element pointer. + // element pointer. auto cloneRegionBodyInto = [&](mlir::Block *srcBlock, mlir::Value replacement) { mlir::IRMapping map; @@ -1443,34 +1446,89 @@ static void lowerArrayDtorCtorIntoLoop(cir::CIRBaseBuilderTy &builder, } }; - builder.createDoWhile( - loc, - /*condBuilder=*/ - [&](mlir::OpBuilder &b, mlir::Location loc) { - auto currentElement = cir::LoadOp::create(b, loc, eltTy, tmpAddr); - auto cmp = cir::CmpOp::create(builder, loc, cir::CmpOpKind::ne, - currentElement, stop); - builder.createCondition(cmp); - }, - /*bodyBuilder=*/ - [&](mlir::OpBuilder &b, mlir::Location loc) { - auto currentElement = cir::LoadOp::create(b, loc, eltTy, tmpAddr); - if (isCtor) { - cloneRegionBodyInto(bodyBlock, currentElement); - mlir::Value stride = builder.getUnsignedInt(loc, 1, sizeTypeSize); - auto nextElement = cir::PtrStrideOp::create(builder, loc, eltTy, - currentElement, stride); - builder.createStore(loc, nextElement, tmpAddr); - } else { - mlir::Value stride = builder.getSignedInt(loc, -1, sizeTypeSize); - auto prevElement = cir::PtrStrideOp::create(builder, loc, eltTy, - currentElement, stride); - builder.createStore(loc, prevElement, tmpAddr); - cloneRegionBodyInto(bodyBlock, prevElement); - } - - builder.createYield(loc); - }); + mlir::Block *partialDtorBlock = nullptr; + if (auto arrayCtor = mlir::dyn_cast<cir::ArrayCtor>(op)) { + mlir::Region &partialDtor = arrayCtor.getPartialDtor(); + if (!partialDtor.empty()) + partialDtorBlock = &partialDtor.front(); + } + + auto emitCtorDtorLoop = [&]() { + builder.createDoWhile( + loc, + /*condBuilder=*/ + [&](mlir::OpBuilder &b, mlir::Location loc) { + auto currentElement = cir::LoadOp::create(b, loc, eltTy, tmpAddr); + auto cmp = cir::CmpOp::create(builder, loc, cir::CmpOpKind::ne, + currentElement, stop); + builder.createCondition(cmp); + }, + /*bodyBuilder=*/ + [&](mlir::OpBuilder &b, mlir::Location loc) { + auto currentElement = cir::LoadOp::create(b, loc, eltTy, tmpAddr); + if (isCtor) { + cloneRegionBodyInto(bodyBlock, currentElement); + mlir::Value stride = builder.getUnsignedInt(loc, 1, sizeTypeSize); + auto nextElement = cir::PtrStrideOp::create(builder, loc, eltTy, + currentElement, stride); + builder.createStore(loc, nextElement, tmpAddr); + } else { + mlir::Value stride = builder.getSignedInt(loc, -1, sizeTypeSize); + auto prevElement = cir::PtrStrideOp::create(builder, loc, eltTy, + currentElement, stride); + builder.createStore(loc, prevElement, tmpAddr); + cloneRegionBodyInto(bodyBlock, prevElement); + } + + builder.createYield(loc); + }); + }; + + if (partialDtorBlock) { + cir::CleanupScopeOp::create( + builder, loc, cir::CleanupKind::EH, + /*bodyBuilder=*/ + [&](mlir::OpBuilder &b, mlir::Location loc) { + emitCtorDtorLoop(); + builder.createYield(loc); + }, + /*cleanupBuilder=*/ + [&](mlir::OpBuilder &b, mlir::Location loc) { + auto cur = cir::LoadOp::create(b, loc, eltTy, tmpAddr); + auto cmp = cir::CmpOp::create(builder, loc, cir::CmpOpKind::ne, + cur, begin); + cir::IfOp::create( + builder, loc, cmp, /*withElseRegion=*/false, + [&](mlir::OpBuilder &b, mlir::Location loc) { + builder.createDoWhile( + loc, + /*condBuilder=*/ + [&](mlir::OpBuilder &b, mlir::Location loc) { + auto el = + cir::LoadOp::create(b, loc, eltTy, tmpAddr); + auto neq = cir::CmpOp::create( + builder, loc, cir::CmpOpKind::ne, el, begin); + builder.createCondition(neq); + }, + /*bodyBuilder=*/ + [&](mlir::OpBuilder &b, mlir::Location loc) { + auto el = + cir::LoadOp::create(b, loc, eltTy, tmpAddr); + mlir::Value negOne = + builder.getSignedInt(loc, -1, sizeTypeSize); + auto prev = cir::PtrStrideOp::create( + builder, loc, eltTy, el, negOne); + builder.createStore(loc, prev, tmpAddr); + cloneRegionBodyInto(partialDtorBlock, prev); + builder.createYield(loc); + }); + cir::YieldOp::create(builder, loc); + }); + builder.createYield(loc); + }); + } else { + emitCtorDtorLoop(); + } if (ifOp) cir::YieldOp::create(builder, loc); diff --git a/clang/test/CIR/CodeGen/array-ctor.cpp b/clang/test/CIR/CodeGen/array-ctor.cpp index 2233529a192c8..f58a46d4afc49 100644 --- a/clang/test/CIR/CodeGen/array-ctor.cpp +++ b/clang/test/CIR/CodeGen/array-ctor.cpp @@ -173,3 +173,16 @@ void multi_dimensional() { // OGCG: br i1 %[[DONE]], label %[[EXIT:.*]], label %[[LOOP]] // OGCG: [[EXIT]]: // OGCG: ret void + +struct HasDtor { + HasDtor(); + ~HasDtor(); +}; + +void test_partial_array_cleanup() { + HasDtor s[4]; +} + +// CIR-BEFORE-LPP: cir.func {{.*}} @_Z26test_partial_array_cleanupv() + +// CIR: cir.func {{.*}} @_Z26test_partial_array_cleanupv() diff --git a/clang/test/CIR/CodeGen/partial-array-cleanup.cpp b/clang/test/CIR/CodeGen/partial-array-cleanup.cpp new file mode 100644 index 0000000000000..f41f80ab601e0 --- /dev/null +++ b/clang/test/CIR/CodeGen/partial-array-cleanup.cpp @@ -0,0 +1,163 @@ +// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -Wno-unused-value -fclangir -fexceptions -fcxx-exceptions -emit-cir -mmlir --mlir-print-ir-before=cir-lowering-prepare %s -o %t.cir 2> %t-before-lp.cir +// RUN: FileCheck --input-file=%t-before-lp.cir %s -check-prefix=CIR-BEFORE-LPP +// RUN: FileCheck --input-file=%t.cir %s -check-prefix=CIR +// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -Wno-unused-value -fexceptions -fcxx-exceptions -fclangir -emit-llvm %s -o %t-cir.ll +// RUN: FileCheck --input-file=%t-cir.ll %s -check-prefix=LLVM +// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -Wno-unused-value -fexceptions -fcxx-exceptions -emit-llvm %s -o %t.ll +// RUN: FileCheck --input-file=%t.ll %s -check-prefix=OGCG + +struct S { + S(); + ~S(); +}; + +void test_partial_array_cleanup() { + S s[4]; +} + +// CIR-BEFORE-LPP: cir.func {{.*}} @_Z26test_partial_array_cleanupv() +// CIR-BEFORE-LPP: %[[ARRAY:.*]] = cir.alloca !cir.array<!rec_S x 4>, !cir.ptr<!cir.array<!rec_S x 4>>, ["s", init] +// CIR-BEFORE-LPP: cir.array.ctor %[[ARRAY]] : !cir.ptr<!cir.array<!rec_S x 4>> { +// CIR-BEFORE-LPP: ^bb0(%[[CTOR_ARG:.*]]: !cir.ptr<!rec_S>): +// CIR-BEFORE-LPP: cir.call @_ZN1SC1Ev(%[[CTOR_ARG]]) : (!cir.ptr<!rec_S>{{.*}}) -> () +// CIR-BEFORE-LPP: cir.yield +// CIR-BEFORE-LPP: } partial_dtor { +// CIR-BEFORE-LPP: ^bb0(%[[DTOR_ARG:.*]]: !cir.ptr<!rec_S>): +// CIR-BEFORE-LPP: cir.call @_ZN1SD1Ev(%[[DTOR_ARG]]){{.*}} : (!cir.ptr<!rec_S>{{.*}}) -> () +// CIR-BEFORE-LPP: cir.yield +// CIR-BEFORE-LPP: } + +// CIR: cir.func {{.*}} @_Z26test_partial_array_cleanupv() +// CIR: %[[ARRAY:.*]] = cir.alloca !cir.array<!rec_S x 4>, !cir.ptr<!cir.array<!rec_S x 4>>, ["s", init] +// CIR: %[[CONST4:.*]] = cir.... [truncated] `````````` </details> https://github.com/llvm/llvm-project/pull/190834 _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
