This is an archive of the discontinued LLVM Phabricator instance.

Add support for custom op parser/printer hooks to know about result names.
ClosedPublic

Authored by lattner on Mar 15 2020, 5:14 PM.

Details

Summary

This allows the custom parser/printer hooks to do interesting things with
the SSA names. This patch:

  • Adds a new 'getResultName' method to OpAsmParser that allows a parser implementation to get information about its result names.
  • Adds a OpAsmPrinter::printOperand overload that takes an explicit stream.
  • Adds a test.string_attr_pretty_name operation that uses these hooks to do fancy things with the result name.

Diff Detail

Event Timeline

lattner created this revision.Mar 15 2020, 5:14 PM
Herald added a project: Restricted Project. · View Herald Transcript

Ok, build 53570 looks like it has two unrelated failures, but the parser.mlir failure does look like a problem. This:

/mnt/disks/ssd0/agent/workspace/amd64_debian_testing_clang8/mlir/test/IR/parser.mlir:1198:16: error: CHECK-NOT: excluded string found in input
// CHECK-NOT: attributes

^

<stdin>:564:36: note: found here
%x = test.string_attr_pretty_name attributes {name = "x"}

^~~~~~~~~~

Failure means that the logic in "print(OpAsmPrinter &p, StringAttrPrettyNameOp op) " is not working. I don't see how it can fail given that the result name is printing correctly as 'x'. The logic in question is this:

if (tmpStream.str().drop_front() == op.name())

elidedAttrs = {"name"};

Does anyone have any ideas? Needless to say, this works fine on my machine (a mac).

flaub added a subscriber: flaub.Mar 15 2020, 8:41 PM
lattner updated this revision to Diff 251258.Mar 18 2020, 8:44 PM

Change to raw_string_ostream to see if it fixes the linux builder, also
add some debug info to catch the problem if it doesn't work.

lattner updated this revision to Diff 251270.Mar 18 2020, 9:33 PM

Remove the test logic. This patch is good to review.

lattner updated this revision to Diff 251276.Mar 18 2020, 10:50 PM

Fix a clang tidy warning, fix the memory issue that caused it to fail on some builders not others.

Ok, this patch is good to go, @rriddle can you take a look when you get a chance?

mehdi_amini added inline comments.Mar 19 2020, 9:30 AM
mlir/lib/Parser/Parser.cpp
4020

I don't get this loop, in the example you git in the comment above: %x, %y:2, %z = foo.op

I expect to loop over { {"x", 1}, {"y", 2}, {"z", 1} }, but your test if (resultNo < std::get<1>(entry)) { will compare against 1, 2, and 1 ; so getResultName(2) and getResultName(3) would never match, what do I miss?

lattner added inline comments.Mar 19 2020, 12:38 PM
mlir/lib/Parser/Parser.cpp
4020

You're not missing anything, this is a bug! Thank you!

lattner updated this revision to Diff 251711.Mar 20 2020, 11:52 AM

Fix a bug that Mehdi noticed in patch review in getResultName(). Expand
the testcase to handle and show the multiresult case explicitly, testing
the fix. Add a new OpAsmParser::getNumResults() method.

The later allows ops with variadic results to know how many they have, which
seems like a widely useful thing. IIRC, something in TFRT had to have an
explicit result count passed in as an attribute because we didn't have this
before.

Thank you again so much for noticing this bug Mehdi. I've expanded out the test dialect op to be more interesting (allowing testing the multi-result case). I believe this is ready for review again, thanks!

lattner marked an inline comment as done.Mar 20 2020, 11:54 AM

Sorry for the delay. Looks good, just some nits

mlir/include/mlir/IR/OpImplementation.h
250

nit: sub-element

250

typo: empty string and

264

nit: getResultName

mlir/lib/IR/AsmPrinter.cpp
1994

nit: Remove the semi-colon here.

2206

nit: is it cleaner to just inline as streamOverride ? streamOverride : &os?

mlir/lib/Parser/Parser.cpp
4019

nit: auto -> StringRef

mlir/test/lib/TestDialect/TestDialect.cpp
399

nit: Remove the extra newline here.

412

llvm::any_of(result.attributes, [](NamedAttribute attr) { return attr.first.is("names"); })
?

420

nit: Can we early exit here instead?

422

You could collect a vector of StringRefs and use Builder::getStrArrayAttr

428

nit: Drop trivial braces

lattner updated this revision to Diff 251779.Mar 20 2020, 2:59 PM
lattner marked 11 inline comments as done.

Incorporate all the great suggestions from River's review.

Thank you so much for the fantastic review River! I've incorporated all of the suggested changes. I'll wait for Mehdi's ack before landing since he expressed concerns on the discourse thread. Thanks!

mehdi_amini accepted this revision.Mar 22 2020, 9:04 PM
mehdi_amini added inline comments.Mar 22 2020, 9:09 PM
mlir/lib/Parser/Parser.cpp
4033

FYI: clang-tidy flags this and suggests a const auto &entry here.

This revision was not accepted when it landed; it landed in state Needs Review.Mar 23 2020, 9:16 AM
This revision was automatically updated to reflect the committed changes.