Skip to content

[MLIR] Invalid LLVM dialect generated after lowering #168837

@RJerrica

Description

@RJerrica

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

// 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.

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).

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

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions