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,22 @@ 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 is created and the old + // constant may become dangling and take space. We should remove the + // dangling constants to avoid memory explosion especially for constant + // arrays whose number of elements is large. + // TODO: handle ops rather than InsertValueOp with ConstantArray. + if (auto ivOp = dyn_cast(op)) { + Value container = ivOp.getContainer(); + if (auto cst = + dyn_cast(lookupValue(container))) { + // GlobalValue shouldn't be treated like other constants. + if (isa(cst)) + continue; + if (cst->hasZeroLiveUses()) + cst->destroyConstant(); + } + } } ReturnOp ret = cast(initializer->getTerminator()); llvm::Constant *cst =