[clang] [CIR] Add support for array cleanups (PR #150499)

Henrich Lauko via cfe-commits cfe-commits at lists.llvm.org
Fri Jul 25 06:21:16 PDT 2025


================
@@ -649,6 +649,41 @@ void CIRGenFunction::emitNullabilityCheck(LValue lhs, mlir::Value rhs,
   assert(!cir::MissingFeatures::sanitizers());
 }
 
+/// Destroys all the elements of the given array, beginning from last to first.
+/// The array cannot be zero-length.
+///
+/// \param begin - a type* denoting the first element of the array
+/// \param end - a type* denoting one past the end of the array
+/// \param elementType - the element type of the array
+/// \param destroyer - the function to call to destroy elements
+void CIRGenFunction::emitArrayDestroy(mlir::Value begin, mlir::Value end,
+                                      QualType elementType,
+                                      CharUnits elementAlign,
+                                      Destroyer *destroyer,
+                                      bool checkZeroLength) {
+  assert(!elementType->isArrayType());
+  if (checkZeroLength)
+    cgm.errorNYI("emitArrayDestroy: check for zero length");
+
+  // Differently from LLVM traditional codegen, use a higher level
+  // representation instead of lowering directly to a loop.
+  mlir::Type cirElementType = convertTypeForMem(elementType);
+  cir::PointerType ptrToElmType = builder.getPointerTo(cirElementType);
+
+  // Emit the dtor call that will execute for every array element.
+  builder.create<cir::ArrayDtor>(
----------------
xlauko wrote:

We should probably align with mlir work of transforming from `builder.create` to `Op::create`
At some point I will give passthrough what is upstreamed and fix it, but new stuff can be fixed on the fly directly. So this should be `cir::ArrayDtor::create(builder, ...`

See more info here https://github.com/llvm/llvm-project/pull/147168




https://github.com/llvm/llvm-project/pull/150499


More information about the cfe-commits mailing list