llvm / Polygeist

C/C++ frontend for MLIR. Also features polyhedral optimizations, parallel optimizations, and more!
https://polygeist.llvm.org
Other
484 stars 115 forks source link

Simple C++ Function #46

Closed kumasento closed 3 years ago

kumasento commented 3 years ago

I tried to play around with mlir-clang on C++ input, and I found out it is a bit difficult.

void MatrixMult(float A[100][200], float B[200][300], float C[100][300]) {
  int i, j, k;

  for (i = 0; i < 100; i ++) {
    for (j = 0; j < 300; j ++) {
      C[i][j] = 0;
      for (k = 0; k < 200; k ++) {
        C[i][j] += A[i][k] * B[k][j];
      }
    }
  }
}

For example, still a simple matmul input, with the file name ended in .cc, I have output like:

» mlir-clang --function=MatrixMult --raise-scf-to-affine SimpleCXXFunc.cc
unhandled icmpunhandled icmpunhandled icmpunhandled icmpunhandled icmpunhandled icmpunhandled icmpunhandled icmpunhandled icmpunhandled icmpunhandled icmpunhandled icmpunhandled icmpunhandled icmpunhandled icmpunhandled icmpunhandled icmpunhandled icmpunhandled icmpunhandled icmpunhandled icmpunhandled icmpunhandled icmpunhandled icmpunhandled icmpunhandled icmpunhandled icmpmodule attributes {llvm.data_layout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128", llvm.target_triple = "x86_64-unknown-linux-gnu"}  {
  func @_Z10MatrixMultPA200_fPA300_fS2_(%arg0: memref<?x200xf32>, %arg1: memref<?x300xf32>, %arg2: memref<?x300xf32>) {
    %c0_i32 = constant 0 : i32
    %c100_i32 = constant 100 : i32
    %c300_i32 = constant 300 : i32
    %c200_i32 = constant 200 : i32
    %c1_i32 = constant 1 : i32
    %0 = scf.while (%arg3 = %c0_i32) : (i32) -> i32 {
      %1 = cmpi ult, %arg3, %c100_i32 : i32
      scf.condition(%1) %arg3 : i32
    } do {
    ^bb0(%arg3: i32):  // no predecessors
      %1 = index_cast %arg3 : i32 to index
      %2 = scf.while (%arg4 = %c0_i32) : (i32) -> i32 {
        %4 = cmpi ult, %arg4, %c300_i32 : i32
        scf.condition(%4) %arg4 : i32
      } do {
      ^bb0(%arg4: i32):  // no predecessors
        %4 = index_cast %arg4 : i32 to index
        %5 = sitofp %c0_i32 : i32 to f32
        memref.store %5, %arg2[%1, %4] : memref<?x300xf32>
        %6 = scf.while (%arg5 = %c0_i32) : (i32) -> i32 {
          %8 = cmpi ult, %arg5, %c200_i32 : i32
          scf.condition(%8) %arg5 : i32
        } do {
        ^bb0(%arg5: i32):  // no predecessors
          %8 = index_cast %arg5 : i32 to index
          %9 = memref.load %arg0[%1, %8] : memref<?x200xf32>
          %10 = memref.load %arg1[%8, %4] : memref<?x300xf32>
          %11 = mulf %9, %10 : f32
          %12 = memref.load %arg2[%1, %4] : memref<?x300xf32>
          %13 = addf %12, %11 : f32
          memref.store %13, %arg2[%1, %4] : memref<?x300xf32>
          %14 = addi %arg5, %c1_i32 : i32
          scf.yield %14 : i32
        }
        %7 = addi %arg4, %c1_i32 : i32
        scf.yield %7 : i32
      }
      %3 = addi %arg3, %c1_i32 : i32
      scf.yield %3 : i32
    }
    return
  }
}

It is pretty weird to find these icmpunhandled things, and raising seems not working as well. It is not an urgent request, but I'm curious to know why (and happy to help fixing!)

chelini commented 3 years ago

@kumasento Thanks. I will have a look.

chelini commented 3 years ago

Fixed in 922e45f4b51efa9a660c4e16242c03ff9852c4c9. Thanks for reporting.