1212#include " mlir/IR/Region.h"
1313#include " clang/AST/ASTContext.h"
1414#include " clang/AST/CharUnits.h"
15+ #include " clang/AST/ExprCXX.h"
1516#include " clang/AST/Mangle.h"
1617#include " clang/Basic/Cuda.h"
1718#include " clang/Basic/Module.h"
@@ -89,6 +90,7 @@ struct LoweringPreparePass : public LoweringPrepareBase<LoweringPreparePass> {
8990 void lowerArrayDtor (ArrayDtor op);
9091 void lowerArrayCtor (ArrayCtor op);
9192 void lowerThrowOp (ThrowOp op);
93+ void lowerTrivialConstructorCall (cir::CallOp op);
9294
9395 // / Collect annotations of global values in the module
9496 void addGlobalAnnotations (mlir::Operation *op, mlir::ArrayAttr annotations);
@@ -1183,7 +1185,8 @@ std::optional<FuncOp> LoweringPreparePass::buildCUDARegisterGlobals() {
11831185 auto cudaPrefix = getCUDAPrefix (astCtx);
11841186
11851187 auto voidTy = VoidType::get (&getContext ());
1186- auto voidPtrPtrTy = PointerType::get (PointerType::get (voidTy));
1188+ auto voidPtrTy = PointerType::get (voidTy);
1189+ auto voidPtrPtrTy = PointerType::get (voidPtrTy);
11871190
11881191 // Create the function:
11891192 // void __cuda_register_globals(void **fatbinHandle)
@@ -1510,6 +1513,36 @@ void LoweringPreparePass::lowerThrowOp(ThrowOp op) {
15101513 }
15111514}
15121515
1516+ void LoweringPreparePass::lowerTrivialConstructorCall (cir::CallOp op) {
1517+ FuncOp funcOp = getCalledFunction (op);
1518+ if (!funcOp)
1519+ return ;
1520+ Attribute astAttr = funcOp.getAstAttr ();
1521+ if (!astAttr)
1522+ return ;
1523+ auto ctorDecl = dyn_cast<cir::ASTCXXConstructorDeclInterface>(astAttr);
1524+ if (!ctorDecl)
1525+ return ;
1526+ if (ctorDecl.isDefaultConstructor ())
1527+ return ;
1528+
1529+ if (ctorDecl.isCopyConstructor ()) {
1530+ // Additional safety checks: constructor calls should have no return value
1531+ if (op.getNumResults () > 0 )
1532+ return ;
1533+ auto operands = op.getOperands ();
1534+ if (operands.size () != 2 )
1535+ return ;
1536+ // Replace the trivial copy constructor call with a copy op
1537+ CIRBaseBuilderTy builder (getContext ());
1538+ mlir::Value dest = operands[0 ];
1539+ mlir::Value src = operands[1 ];
1540+ builder.setInsertionPoint (op);
1541+ builder.createCopy (dest, src);
1542+ op.erase ();
1543+ }
1544+ }
1545+
15131546void LoweringPreparePass::addGlobalAnnotations (mlir::Operation *op,
15141547 mlir::ArrayAttr annotations) {
15151548 auto globalValue = cast<mlir::SymbolOpInterface>(op);
@@ -1580,6 +1613,8 @@ void LoweringPreparePass::runOnOp(Operation *op) {
15801613 addGlobalAnnotations (fnOp, annotations.value ());
15811614 } else if (auto throwOp = dyn_cast<cir::ThrowOp>(op)) {
15821615 lowerThrowOp (throwOp);
1616+ } else if (auto callOp = dyn_cast<CallOp>(op)) {
1617+ lowerTrivialConstructorCall (callOp);
15831618 }
15841619}
15851620
@@ -1596,7 +1631,7 @@ void LoweringPreparePass::runOnOperation() {
15961631 op->walk ([&](Operation *op) {
15971632 if (isa<UnaryOp, BinOp, CastOp, ComplexBinOp, CmpThreeWayOp, VAArgOp,
15981633 GlobalOp, DynamicCastOp, StdFindOp, IterEndOp, IterBeginOp,
1599- ArrayCtor, ArrayDtor, cir::FuncOp, StoreOp, ThrowOp>(op))
1634+ ArrayCtor, ArrayDtor, cir::FuncOp, StoreOp, ThrowOp, CallOp >(op))
16001635 opsToTransform.push_back (op);
16011636 });
16021637
0 commit comments