diff --git a/mlir/lib/Conversion/MemRefToLLVM/MemRefToLLVM.cpp b/mlir/lib/Conversion/MemRefToLLVM/MemRefToLLVM.cpp --- a/mlir/lib/Conversion/MemRefToLLVM/MemRefToLLVM.cpp +++ b/mlir/lib/Conversion/MemRefToLLVM/MemRefToLLVM.cpp @@ -454,9 +454,17 @@ initialValue = elementsAttr.getValue({}); } - rewriter.replaceOpWithNewOp( + auto newGlobal = rewriter.replaceOpWithNewOp( global, arrayTy, global.constant(), linkage, global.sym_name(), initialValue, /*alignment=*/0, type.getMemorySpaceAsInt()); + if (!global.isExternal() && global.isUninitialized()) { + Block *blk = new Block(); + newGlobal.getInitializerRegion().push_back(blk); + rewriter.setInsertionPointToStart(blk); + Value undef[] = { + rewriter.create(global.getLoc(), arrayTy)}; + rewriter.create(global.getLoc(), undef); + } return success(); } }; diff --git a/mlir/test/Conversion/MemRefToLLVM/memref-to-llvm.mlir b/mlir/test/Conversion/MemRefToLLVM/memref-to-llvm.mlir --- a/mlir/test/Conversion/MemRefToLLVM/memref-to-llvm.mlir +++ b/mlir/test/Conversion/MemRefToLLVM/memref-to-llvm.mlir @@ -623,7 +623,10 @@ // ----- -// CHECK: llvm.mlir.global external @gv0() : !llvm.array<2 x f32> +// CHECK: llvm.mlir.global external @gv0() : !llvm.array<2 x f32> { +// CHECK-NEXT: %0 = llvm.mlir.undef : !llvm.array<2 x f32> +// CHECK-NEXT: llvm.return %0 : !llvm.array<2 x f32> +// CHECK-NEXT: } memref.global @gv0 : memref<2xf32> = uninitialized // CHECK: llvm.mlir.global private @gv1() : !llvm.array<2 x f32>