Initialization of a mlir.global with an array of pointers

In LLVM IR is possible to statically initialize a global array with addresses. Examples:

; Initialize an array of integer pointers
@i1 = global i32 1, align 4
@i2 = global i32 2, align 4
@arr = global [2 x i32*] [i32* @i1, i32* @i2], align 16
; Initialize an array of strings.
@.str = private unnamed_addr constant [4 x i8] c"cat\00", align 1
@.str.1 = private unnamed_addr constant [4 x i8] c"dog\00", align 1
@arr = global [2 x i8*] [i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i32 0, i32 0)], align 16

What would be the corresponding constructs in the LLVV MLIR Dialect? I tried to translate the LLVM IR examples above using the mlir-translate tool, but it crashed. I am not sure whether this is a limitation of the tool (currently) or something more fundamental ?

% mlir-translate --import-llvm int_ptr.ll
imported-bitcode:0:0: error: unhandled constant: [2 x i32*] [i32* @i1, i32* @i2]

% mlir-translate --import-llvm ptr_array.ll
imported-bitcode:0:0: error: unhandled constant: [2 x i8*] [i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i32 0, i32 0)]

Are you eventually interested in representing this in MLIR LLVM dialect and then translating to LLVM or importing existing LLVM into MLIR?

@bondhugula yes eventually I would like to represent it in the MLIR LLVM dialect. I run that experiment because I wanted to ‘see’ what would be the MLIR program that corresponds to the input LLVM IR I passed to mlir-translate.

You should be able to use an llvm.mlir.global. Is this array destined for the data segment or .rodata? An uninitialized global array would work right away.

 llvm.mlir.global internal @array() : !llvm.array<2 x !llvm.ptr<i32>>

To initialize it, you’ll have to attach a region and this will be non-trivial; just try to return the array type comprising addresses of @i1 and @i2 from the initializer region. See:

It may or may not work/be supported for translation to LLVM IR. If not, the support can be completed.

Import from LLVM is by and large incomplete and the code is highly experimental.

Otherwise, what @bondhugula said should work.

Thanks @bondhugula for the suggestion. I will give that a try and follow up on this thread as necessary.