llvm / llvm/llvm-project

When converting to C code, the EmitC printer fails to handle builtin.unrealized_conversion_cast

Open
#159,746 0 comments 0 reactions 0 assignees View on GitHub
mlir:emitc
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

When lowering to the EmitC dialect, MLIR inserts a builtin.unrealized_conversion_cast to connect types that do not have a direct conversion.
In this case, the lowering produces a cast from !emitc.ptr to !emitc.array<...xf32>.
When trying to emit C++ code, the CppEmitter fails

**ERROR :**

error: 'builtin.unrealized_conversion_cast' op unable to find printer for op
%9 = builtin.unrealized_conversion_cast %8 :
!emitc.ptr to !emitc.array<1x3x4x4xf32>

**Input :**

```
module {
func.func @main(%arg0: tensor<1x3x4x4xf32>, %arg1: tensor<1x3x4x4xf32>)
-> tensor<1x3x4x4xf32> {
%3 = "tosa.add"(%arg0, %arg1)
: (tensor<1x3x4x4xf32>, tensor<1x3x4x4xf32>)
-> tensor<1x3x4x4xf32>

%4 = "tosa.sub"(%3, %arg1)
: (tensor<1x3x4x4xf32>, tensor<1x3x4x4xf32>)
-> tensor<1x3x4x4xf32>

return %4 : tensor<1x3x4x4xf32>
}
}

```

**Run With :**

```
mlir-opt input.mlir -pass-pipeline="builtin.module(
func.func(tosa-to-linalg),
one-shot-bufferize{bufferize-function-boundaries function-boundary-type-conversion=identity-layout-map buffer-alignment=0},
buffer-results-to-out-params{hoist-static-allocs=true},
func.func(convert-linalg-to-loops),
canonicalize,
convert-to-emitc
)" -o output.mlir

```

**PRODUCED EMITC IR :**

[EMitcIR.txt](https://github.com/user-attachments/files/22424795/EMitcIR.txt)

**Steps to reproduce**

Save the input MLIR program above.

Run the pipeline shown.

Attempt to translate the resulting EmitC IR to C++.

Observe that printing fails on the unrealized_conversion_cast.

Contributor guide

Open the contributing guide

Research direction

Reproduce the failure with input.mlir using the listed mlir-opt pass pipeline, then inspect the CppEmitter entry point for handling EmitC operations and builtin.unrealized_conversion_cast. Done means the produced EmitC IR translates to C++ without the unable-to-find-printer error for the shown cast.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.