LLVM Discussion Forums

Zero initialization for globals with common linkage

In LLVM there is a feature for zero-initializing aggregate types like arrays. It’s called zeroinitializer (https://llvm.org/docs/LangRef.html#complex-constants).
In my MLIR dialect I have created a tensor-like type which I use for global declarations that have common linkage (common linkage requires a zero initializer).

Is there currently a way to use the zeroinitializer feature in the LLVM MLIR dialect ?

Not directly, and it is unclear to me whether we want a cross-type zero initializer or prefer something more structured. In the meantime, it is possible to create globals filled with zeros by using attributes with splat elements attributes:

llvm.mlir.global internal @a2(dense<0.0> : tensor<3xf32>) : !llvm.array<3xfloat>

llvm.mlir.global @cplx() : !llvm.struct<(array<2xi32>, array<3xfloat>)> {
  %0 = llvm.mlir.undef : !llvm.struct<(array<2xi32>, array<3xfloat>)>
  %1 = llvm.mlir.constant(dense<0> : tensor<2xi32>) : !llvm.array<2xi32>
  %2 = llvm.insertvalue %1, %0[0] : !llvm.struct<(array<2xi32>, array<3xfloat>)>
  %3 = llvm.mlir.constant(dense<0.0> : tensor<3xf32>) : !llvm.array<3xfloat>
  %4 = llvm.insertvalue %3, %2[1] :!llvm.struct<(array<2xi32>, array<3xfloat>)>
  llvm.return %4 : !llvm.struct<(array<2xi32>, array<3xfloat>)>

1 Like