Issue 242839
Summary # [mlir][Test] `FoldToCallOpPattern` creates an invalid `func.call` when the callee is undefined
Labels mlir
Assignees
Reporter lin334141-cell
    ## Summary

The test-dialect canonicalization pattern `FoldToCallOpPattern` rewrites `test.fold_to_call_op` into `func.call` without checking whether the referenced callee exists.

The original operation passes MLIR verification because its `callee` attribute is not subject to symbol resolution by the test operation's verifier. After running `canonicalize`, the resulting `func.call` fails verification because it references an undefined function.

This issue is specific to the Test dialect's rewrite pattern, not the generic canonicalization driver.

## Minimal Reproducer

```mlir
"builtin.module"() ({
  "func.func"() <{function_type = () -> (), sym_name = "f"}> ({
 "test.fold_to_call_op"() <{callee = @undefined_sym}> : () -> ()
 "func.return"() : () -> ()
  }) : () -> ()
}) : () -> ()
```

The reproducer requires an `mlir-opt` build with the Test dialect registered.

First, verify the input:

```bash
mlir-opt min.mlir -o /dev/null
```

The original module is accepted.

Then run:

```bash
mlir-opt min.mlir \
 --pass-pipeline="builtin.module(canonicalize)" \
  -o /dev/null
```

## Actual Behavior

The canonicalization pattern creates:

```mlir
"func.call"() <{callee = @undefined_sym}> : () -> ()
```

The verifier rejects the resulting operation:

```text
error: 'func.call' op 'undefined_sym'
does not reference a valid function
```

The command exits with code 1.

## Expected Behavior

Canonicalization should preserve verifier-valid IR.

The Test dialect should either validate the callee symbol before applying this rewrite or reject the transformation when the referenced function cannot be resolved.

## Root Cause

The relevant implementation is in:

`mlir/test/lib/Dialect/Test/TestOpDefs.cpp`

```cpp
LogicalResult matchAndRewrite(FoldToCallOp op,
 PatternRewriter &rewriter) const override {
 rewriter.replaceOpWithNewOp<func::CallOp>(
      op, TypeRange(), op.getCalleeAttr(), ValueRange());
  return success();
}
```

The pattern unconditionally creates `func::CallOp` without validating the symbol reference.

This behavior was observed in LLVM `24.0.0git` at commit `8635f9042c2a42d15844b0bc4b2e8e27b06f3f54`. The relevant pattern implementation is also present in the upstream main branch.

A positive control with a defined callee completes successfully.

## Scope

This is a low-severity Test dialect issue. It affects verification correctness of test-only canonicalization patterns; no production-dialect miscompilation is claimed.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to