Mlir - Emitc: Translate to C

Hi everyone, I am trying to translate a small code snippet based on emitc and func to C, however I am having some troubles.
The code snippet is the following:

module {
  emitc.include <"stdio.h">
  emitc.include <"stdint.h">
  emitc.global @input_data : !emitc.array<2x2xf32> = dense<[[2.000000e+00, 1.000000e+00], [0.000000e+00, 0.000000e+00]]>
  func.func public @main() -> i32 {
    %0 = "emitc.constant"() <{value = 0 : i32}> : () -> i32
    %1 = emitc.get_global @input_data : !emitc.array<2x2xf32>
    %2 = emitc.subscript %1[%0, %0] : (!emitc.array<2x2xf32>, i32, i32) -> f32
    emitc.call_opaque "printf"(%2) {args = [#emitc.opaque<"\22%f\\n\22">, 0 : index]} : (f32) -> ()
    return %0 : i32
  }
}

As expected, when I translate to C code with mlir-translate --mlir-to-cpp, the output is 2.0000, as I am printing the first element of the input_data array.

The issue : If I use mlir-translate --mlir-to-cpp --declare-variables-at-top to translate the above snippet, I get the following C code:

#include <stdio.h>
#include <stdint.h>
float input_data[2][2] = {2.000000000e+00f, 1.000000000e+00f, 0.0e+00f, 0.0e+00f};
int32_t main() {
  int32_t v1;
  float input_data[2][2];
  v1 = 0;
  printf("%f\n", input_data[v1][v1]);
  return v1;
}

so, the global variable is present in the main function, with the output becoming 0.000. I know in this case the declare-variables-at-top is not necessary, but I’d like to understand the source of the problem (as I am quite new to MLIR).

Please excuse the delayed reply.

What you get with -mlir-to-cpp might be an issue which was recently fixed with [mlir][EmitC] Unify handling of operations which are emitted in a def… · llvm/llvm-project@d521324 · GitHub. Please give it a try, wasn’t able yet to test myself.

Hi, thanks for your reply, the issue appears to be fixed with the latest MLIR version