llvm / llvm/llvm-project

Segfault when using type inference for op with attributes

Open
#193,284 1 comment 0 reactions 0 assignees View on GitHub
mlir
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

The problem arises precisely when all of the following are true:

1. The op has an attribute
2. The op has type inference
3. Type inference requires the attribute
4. The op does not explicitly require the type of output in its IR

```
def Foo_GetSliceOp : Foo_Op<"get_slice", [Pure, DeclareOpInterfaceMethods]> {
let arguments = (ins
FooType:$input,
IndexAttr:$start,
IndexAttr:$size
);
let results = (outs FooType:$output);
let assemblyFormat = "$input attr-dict `:` type($input) `->` type($output)";
}
```

Under these conditions, the generated parser runs type inference before it fully parses the attribute, so trying to write the following fails:

```c++
LogicalResult GetSliceOp::inferReturnTypes(
MLIRContext* ctx, std::optional, ValueRange operands,
DictionaryAttr attrs, mlir::PropertyRef properties,
mlir::RegionRange regions, SmallVectorImpl& results) {
GetSliceOpAdaptor op(operands, attrs, properties, regions);
auto inputType = cast(op.getInput().getType());
int64_t start = op->getStart().getZExtValue(); // segfault here
int64_t size = op->getSize().getZExtValue();
...
}
```

#88506 seems related, but this issue is still happening as of [this commit](https://github.com/llvm/llvm-project/commit/2cf353b5e8560723409f3f9164bddec76f499963).

Contributor guide

Open the contributing guide

Research direction

Start with the generated parser for an operation using InferTypeOpInterface and compare its attribute parsing order with inferReturnTypes. Reproduce the Foo_GetSliceOp case, inspect issue #88506 and the referenced commit, and verify that inference no longer accesses attributes before they are available without changing valid parsing behavior.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.