diff --git a/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp b/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp --- a/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp +++ b/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp @@ -739,6 +739,16 @@ if (failed(convertOperation(op, builder)) || !isa(lookupValue(op.getResult(0)))) return emitError(op.getLoc(), "unemittable constant value"); + // When emitting an LLVM constant, a new constant array is created and + // the old constant array becomes dangling and takes space. + // We should remove the dangling constant array to avoid memory + // explosion especially when the number of elements in the array is + // large. + for (Value v : op.getOperands()) { + if (auto cst = dyn_cast(lookupValue(v))) + if (isa(cst->getType()) && cst->hasZeroLiveUses()) + cst->destroyConstant(); + } } ReturnOp ret = cast(initializer->getTerminator()); llvm::Constant *cst =