[mlir][core] Fix ValueRange printing in AsmPrinter
authorwpmed92 <ahmedharmouche92@gmail.com>
Mon, 27 Feb 2023 20:51:28 +0000 (12:51 -0800)
committerRiver Riddle <riddleriver@gmail.com>
Mon, 27 Feb 2023 22:53:59 +0000 (14:53 -0800)
commit964997a143cfa0356d1cd21eb3e6d2170c975c38
tree881d2cf21f9f30c7ea174e284dd1225b7f13d473
parent3a51eed94846bee4b1e0ad1f160e1de6d5a7a9d0
[mlir][core] Fix ValueRange printing in AsmPrinter

The ValueRange printing behaviour of `OpAsmPrinter` and `AsmPrinter` is different, as reported [[ https://github.com/llvm/llvm-project/issues/59334 | here ]]

```
static void testPrint(AsmPrinter &p, Operation *op, ValueRange operands) {
  p << '(' << operands << ')';
}
```
Although the base `AsmPrinter` is passed as the first parameter (and not `OpAsmPrinter`), the code compiles fine. However, instead of the SSA values, the types for the operands will be printed. This is a violation of the Liskov Substitution Principle.
The desired behaviour would be that the above code does not compile. The reason it compiles, is that for the above code, the `TypeRange` version will be selected for the `<<` operator, since `ValueRange` is implicitly converted to `TypeRange`:
```
template <typename AsmPrinterT>
inline std::enable_if_t<std::is_base_of<AsmPrinter, AsmPrinterT>::value,
                        AsmPrinterT &>
operator<<(AsmPrinterT &p, const TypeRange &types) {
  llvm::interleaveComma(types, p);
  return p;
}
```
mlir/include/mlir/IR/OpImplementation.h