This is an archive of the discontinued LLVM Phabricator instance.

[MLIR] Allow `Idempotent` trait to be applied to binary ops.
ClosedPublic

Authored by chr1sj0nes on Nov 25 2021, 1:23 AM.

Details

Diff Detail

Event Timeline

chr1sj0nes created this revision.Nov 25 2021, 1:23 AM

Formatting fix.

chr1sj0nes published this revision for review.Nov 25 2021, 5:04 AM
mehdi_amini accepted this revision.Nov 25 2021, 4:19 PM
mehdi_amini added inline comments.
mlir/lib/IR/Operation.cpp
638

Nit: no need for else after return

This revision is now accepted and ready to land.Nov 25 2021, 4:19 PM

Thanks for the review Mehdi! Can you please submit if you are happy, as I don't have permissions.

mlir/lib/IR/Operation.cpp
638

I think the else is necessary here, as the if branch doesn't always return. Without the else, it might call op->getOperand(1) on an op with only one operand.

Cool. Is there any reason that idompotence be limited to unary and binary ops? Variadic "or" operations like comb.or in CIRCT seem like they should be able to participate as well.

Cool. Is there any reason that idompotence be limited to unary and binary ops? Variadic "or" operations like comb.or in CIRCT seem like they should be able to participate as well.

I guess the question for me there would be, is that stretching the standard terminology too much? The Wikipedia article (https://en.wikipedia.org/wiki/Idempotence), for example, makes no mention of variadic ops that I can see. Perhaps a question for the maths / compsci theorists.

Cool. Is there any reason that idompotence be limited to unary and binary ops? Variadic "or" operations like comb.or in CIRCT seem like they should be able to participate as well.

I guess the question for me there would be, is that stretching the standard terminology too much? The Wikipedia article (https://en.wikipedia.org/wiki/Idempotence), for example, makes no mention of variadic ops that I can see. Perhaps a question for the maths / compsci theorists.

I think technically it is only defined on unary operations, for the binary case we require both operands be the same, so the function that is idempotent is rather the unary function with value x that invokes the binary op with x,x and in that sense variadic is merely a unary function with a vector of values (but being a repetition of 1 input) as input. So the same generalization seem to apply, now it is stretching the term slightly, but I can't think of one that is less confusing while accurate.

We can argue about specific definitions, but (for example) the wikipedia definition doesn't discuss arity at all: " is the property of certain operations in mathematics and computer science whereby they can be applied multiple times without changing the result beyond the initial application." If you're going to define this for binary, then you need to define what it means for multiple operands. Given that, I don't see how 2 is more special than N :-)

Going beyond theory, I think we should also be guided by practical considerations - is it useful for MLIR-based compilers to have one definition or the other?

The generalization to variadic makes sense to me, in particular for an op like or which seems like it can be expressed like a compressed form of a chain of the binary op as long as it is associative : or(x, y, z) is equivalent to or(or(x, y), z) or or(x, or(y, z)).

Asked differently: I'm not sure what we get from limiting the Idempotent trait to unary and binary ops?

The generalization to variadic makes sense to me, in particular for an op like or which seems like it can be expressed like a compressed form of a chain of the binary op as long as it is associative : or(x, y, z) is equivalent to or(or(x, y), z) or or(x, or(y, z)).

Asked differently: I'm not sure what we get from limiting the Idempotent trait to unary and binary ops?

+1. As long as our definition in the face of variadic is clear, variadic operations that don't conform to that definition could just not add the trait (which would be the same if we limited it to unary/binary anyways).

We can argue about specific definitions, but (for example) the wikipedia definition doesn't discuss arity at all

To be fair: while the intro gives a fairly generic definition, if you continue in the "definition" section it actually only talks about binary and not unary at all I believe. The examples are full of mostly binary operators as well, until you get to the "function" section where unary examples are provided again.

I don't think we should anchor to closely to wikipedia here, the article isn't entirely consistent and looks quite informal and context dependent as far as I can tell.