<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/168837>168837</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [MLIR] Invalid LLVM dialect generated after lowering
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            mlir
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          RJerrica
      </td>
    </tr>
</table>

<pre>
    Hi, I used MLIR to lower a program to LLVM IR. However, the process generated an invalid LLVM dialect program that cannot be translated to LLVM IR. Here is the code example.

### Input Program
```llvm
// vector_add.mlir
module {
  func.func @vector_add(%A: memref<100xf32>, %B: memref<100xf32>, %C: memref<100xf32>) {
    %c0 = index.constant 0
    %c1 = index.constant 1
    %c100 = index.constant 100
    %c256 = index.constant 256
    
    gpu.launch blocks(%block_x, %block_y, %block_z) in (%grid_x = %c1, %grid_y = %c1, %grid_z = %c1)
                 threads(%thread_x, %thread_y, %thread_z) in (%block_size_x = %c256, %block_size_y = %c1, %block_size_z = %c1) {
      
      %temp_global_id = index.mul %block_x, %block_size_x
      %global_id = index.add %temp_global_id, %thread_x
      
      %within_bounds = index.cmp slt(%global_id, %c100)
      
      scf.if %within_bounds {
        %a_val = memref.load %A[%global_id] : memref<100xf32>
        %b_val = memref.load %B[%global_id] : memref<100xf32>
        
        %sum = arith.addf %a_val, %b_val : f32
        
        memref.store %sum, %C[%global_id] : memref<100xf32>
      }
      
      gpu.terminator
    }
    
    func.return
  }
}
```

### MLIR Command
I used the following command to lower the program to​ the LLVM dialect and then to​ LLVM IR.
```bash
mlir-opt vector_add.mlir \
  --convert-scf-to-cf \
  --convert-gpu-to-nvvm \
  --convert-cf-to-llvm \
  --convert-index-to-llvm \
  --convert-arith-to-llvm \
  --convert-math-to-llvm \
  --convert-func-to-llvm \
  --finalize-memref-to-llvm \
  -convert-nvvm-to-llvm \
  --reconcile-unrealized-casts \
  -o vector_add_llvm.mlir
```

Then, I use `mlir-translate` to convert the LLVM dialect module to LLVM IR. However, the generated LLVM dialect program is invalid because it contains numerous `unrealized_conversion_cast` operations, such as `%20 = builtin.unrealized_conversion_cast %19 : i64 to index` (See below for the logs).

```bash
mlir-translate --mlir-to-llvmir vector_add_llvm.mlir -o vector_add.ll
vector_add_llvm.mlir:6:10: error: LLVM Translation failed for operation: builtin.unrealized_conversion_cast
    %3 = builtin.unrealized_conversion_cast %2 : i64 to index
         ^
vector_add_llvm.mlir:6:10: note: see current operation: %3 = "builtin.unrealized_conversion_cast"(%2) : (i64) -> index
```

### Version
```bash
mlir-opt --version
LLVM (http://llvm.org/):
  LLVM version 22.0.0git
  Optimized build with assertions.
```

</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJycV11v4ygX_jXkBtkiuHadi1ykX5q-mtG7mh3NbYTh2GYXQwQ4_fj1K7DjOK3bndnKqgPn6znPAQ5mzslGA2xRfoPyuxXrfWvs9vv_wFrJ2aoy4mX7RSJ6ix9x70Dgb18fv2NvsDJPYDHDB2say7ow9fXrz2_48XuKv5gnOIINVr6FoMLBOdyABss8CMw0lvrIlBSDkZBMAfdnZy3zmDOtjccVYG-ZdipaXoQBC1i6GIMbARieWXdQkCKyCw_Nhgc_6kPv8R-D8yApyPAodYxj-oDoAz4C98bumRBpp6RFZNcZ0SvA6PoGkR3Gda95Gv5hdEXO2oiWiOY7lO1wB52FGmW3a0Ke64yi7D6wgGh-87n49kPxZgqPgyInGGV3WGoBzyk32nmmPSYzhfWSwnquQBZ9rMncC82LJSWaFyel4dUc-lSxXvMWV8rwv93ARvy9fx6zG0YvF6PXkJrUeNBvrBT75xgxQhxV4_TL8vTrfHozwrn4860FJkZEw2CCNA5fLoeXoAagTr7CDFpgYJ5HFL-HOBNeAp2VcyIxUu6hO-wbZSqm9lLMyO96hT-gdMA297LkgAnxPsBl4s-LkJ6kb6XeV6bXws2XQ3fATvmxdG88hvU1L8j0w_E6lfWC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