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 @@ -741,6 +741,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 other 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 = diff --git a/mlir/test/Target/LLVMIR/llvmir.mlir b/mlir/test/Target/LLVMIR/llvmir.mlir --- a/mlir/test/Target/LLVMIR/llvmir.mlir +++ b/mlir/test/Target/LLVMIR/llvmir.mlir @@ -53,6 +53,15 @@ llvm.return %gepinit : !llvm.ptr } +// CHECK: @string_array = internal constant [1 x ptr] [ptr @string_const] +llvm.mlir.global internal constant @string_array() : !llvm.array<1 x ptr> { + %0 = llvm.mlir.undef : !llvm.array<1 x ptr> + %1 = llvm.mlir.addressof @string_const: !llvm.ptr> + %2 = llvm.getelementptr %1[0, 0] : (!llvm.ptr>) -> !llvm.ptr + %3 = llvm.insertvalue %2, %0[0] : !llvm.array<1 x ptr> + llvm.return %3 : !llvm.array<1 x ptr> +} + // CHECK{LITERAL}: @dense_float_vector = internal global <3 x float> llvm.mlir.global internal @dense_float_vector(dense<[1.0, 2.0, 3.0]> : vector<3xf32>) : vector<3xf32>