@@ -29,8 +29,8 @@ struct LoweringPreparePass : public LoweringPrepareBase<LoweringPreparePass> {
2929 void runOnOp (mlir::Operation *op);
3030 void lowerCastOp (cir::CastOp op);
3131 void lowerUnaryOp (cir::UnaryOp op);
32- void lowerArrayDtor (ArrayDtor op);
33- void lowerArrayCtor (ArrayCtor op);
32+ void lowerArrayDtor (cir:: ArrayDtor op);
33+ void lowerArrayCtor (cir:: ArrayCtor op);
3434
3535 // /
3636 // / AST related
@@ -182,13 +182,14 @@ static void lowerArrayDtorCtorIntoLoop(cir::CIRBaseBuilderTy &builder,
182182 // PtrDiffTy and unify with CIRGen stuff.
183183 const unsigned sizeTypeSize =
184184 astCtx->getTypeSize (astCtx->getSignedSizeType ());
185- mlir::Value numArrayElementsConst =
186- builder.getUnsignedInt (loc, arrayLen, sizeTypeSize);
187-
188- auto begin = builder.create <cir::CastOp>(
189- loc, eltTy, cir::CastKind::array_to_ptrdecay, arrayAddr);
190- mlir::Value end = builder.create <cir::PtrStrideOp>(loc, eltTy, begin,
191- numArrayElementsConst);
185+ uint64_t endOffset = isCtor ? arrayLen : arrayLen - 1 ;
186+ mlir::Value endOffsetVal =
187+ builder.getUnsignedInt (loc, endOffset, sizeTypeSize);
188+
189+ auto begin = cir::CastOp::create (builder, loc, eltTy,
190+ cir::CastKind::array_to_ptrdecay, arrayAddr);
191+ mlir::Value end =
192+ cir::PtrStrideOp::create (builder, loc, eltTy, begin, endOffsetVal);
192193 mlir::Value start = isCtor ? begin : end;
193194 mlir::Value stop = isCtor ? end : begin;
194195
@@ -216,20 +217,16 @@ static void lowerArrayDtorCtorIntoLoop(cir::CIRBaseBuilderTy &builder,
216217 assert (ctorCall && " expected ctor call" );
217218
218219 // Array elements get constructed in order but destructed in reverse.
219- cir::PtrStrideOp nextElement;
220- if (isCtor) {
221- mlir::Value stride = builder.getUnsignedInt (loc, 1 , sizeTypeSize);
222- ctorCall->moveBefore (stride.getDefiningOp ());
223- ctorCall->setOperand (0 , currentElement);
224- nextElement = builder.create <cir::PtrStrideOp>(
225- loc, eltTy, currentElement, stride);
226- } else {
227- mlir::Value stride = builder.getSignedInt (loc, -1 , sizeTypeSize);
228- nextElement = builder.create <cir::PtrStrideOp>(
229- loc, eltTy, currentElement, stride);
230- ctorCall->moveAfter (nextElement);
231- ctorCall->setOperand (0 , nextElement);
232- }
220+ mlir::Value stride;
221+ if (isCtor)
222+ stride = builder.getUnsignedInt (loc, 1 , sizeTypeSize);
223+ else
224+ stride = builder.getSignedInt (loc, -1 , sizeTypeSize);
225+
226+ ctorCall->moveBefore (stride.getDefiningOp ());
227+ ctorCall->setOperand (0 , currentElement);
228+ auto nextElement = cir::PtrStrideOp::create (builder, loc, eltTy,
229+ currentElement, stride);
233230
234231 // Store the element pointer to the temporary variable
235232 builder.createStore (loc, nextElement, tmpAddr);
@@ -240,11 +237,12 @@ static void lowerArrayDtorCtorIntoLoop(cir::CIRBaseBuilderTy &builder,
240237 op->erase ();
241238}
242239
243- void LoweringPreparePass::lowerArrayDtor (ArrayDtor op) {
240+ void LoweringPreparePass::lowerArrayDtor (cir:: ArrayDtor op) {
244241 CIRBaseBuilderTy builder (getContext ());
245242 builder.setInsertionPointAfter (op.getOperation ());
246243
247244 mlir::Type eltTy = op->getRegion (0 ).getArgument (0 ).getType ();
245+ assert (!cir::MissingFeatures::vlas ());
248246 auto arrayLen =
249247 mlir::cast<cir::ArrayType>(op.getAddr ().getType ().getPointee ()).getSize ();
250248 lowerArrayDtorCtorIntoLoop (builder, astCtx, op, eltTy, op.getAddr (), arrayLen,
@@ -266,12 +264,12 @@ void LoweringPreparePass::lowerArrayCtor(cir::ArrayCtor op) {
266264void LoweringPreparePass::runOnOp (mlir::Operation *op) {
267265 if (auto arrayCtor = dyn_cast<ArrayCtor>(op))
268266 lowerArrayCtor (arrayCtor);
267+ else if (auto arrayDtor = dyn_cast<cir::ArrayDtor>(op))
268+ lowerArrayDtor (arrayDtor);
269269 else if (auto cast = mlir::dyn_cast<cir::CastOp>(op))
270270 lowerCastOp (cast);
271271 else if (auto unary = mlir::dyn_cast<cir::UnaryOp>(op))
272272 lowerUnaryOp (unary);
273- else if (auto arrayDtor = dyn_cast<ArrayDtor>(op))
274- lowerArrayDtor (arrayDtor);
275273}
276274
277275void LoweringPreparePass::runOnOperation () {
0 commit comments