This is an archive of the discontinued LLVM Phabricator instance.

[mlir][core] Fix ValueRange printing in AsmPrinter
ClosedPublic

Authored by wpmed92 on Feb 26 2023, 1:33 PM.

Details

Summary

The ValueRange printing behaviour of OpAsmPrinter and AsmPrinter is different, as reported 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;
}

Diff Detail

Event Timeline

wpmed92 created this revision.Feb 26 2023, 1:33 PM
wpmed92 requested review of this revision.Feb 26 2023, 1:33 PM
rriddle accepted this revision.Feb 27 2023, 12:21 PM
This revision is now accepted and ready to land.Feb 27 2023, 12:21 PM

@rriddle Can you please commit it on my behalf? This is my first patch so I don't have commit access yet.

Sure, can you provide an author name and email to attach to the commit? (arc doesn't want to patch this commit automatically for me).

Sure, author name: wpmed92 email: ahmedharmouche92@gmail.com
Thank you!

since ValueRange is implicitly converted to TypeRange

This seems like the core issue to me, this doesn't seem like something that should happen implicitly. And the conversion is already supposed to be explicit: https://github.com/llvm/llvm-project/blob/5ac69674bf4fbe4adaca4170a2ad60c8a32613ed/mlir/include/mlir/IR/TypeRange.h#L42

lattner accepted this revision.Feb 27 2023, 2:01 PM
lattner added a subscriber: lattner.

Cool didn't know about enable_if = delete, TIL thank you for tackling this!

@rkayaith How about L43, where it takes a ValueTypeRange<ValueRange>? ValueRange to ValueTypeRange<ValueRange> can be implicit.

wpmed92 added a comment.EditedFeb 27 2023, 2:31 PM

Thank you @lattner, happy to be working on MLIR. I hope I can contribute more.

@rkayaith How about L43, where it takes a ValueTypeRange<ValueRange>? ValueRange to ValueTypeRange<ValueRange> can be implicit.

Can it be made explicit/hidden/disabled? The intended usage seems to be that ValueTypeRanges are created explicitly through {ValueRange,ResultRange,etc}::getTypes()

Mogball closed this revision.Jun 5 2023, 4:09 PM

It looks like this was already committed somehow. I'm seeing the changes in main.