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,24 @@ 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. + for (Value v : op.getOperands()) { + if (auto cst = dyn_cast(lookupValue(v))) { + // GlobalValue shouldn't be treated like other constants. + if (isa(cst)) + continue; + // ConstantInt, ConstantFP, ConstantTokenNone and + // ConstantPointerNull are not allowed to destroy. + if (isa(cst)) + continue; + if (cst->hasZeroLiveUses()) + cst->destroyConstant(); + } + } } ReturnOp ret = cast(initializer->getTerminator()); llvm::Constant *cst =