llvm / clangir

A new (MLIR based) high-level IR for clang.
https://clangir.org
Other
327 stars 86 forks source link

`cir.vec.create` syntax is a bit confusing #541

Open bcardosolopes opened 4 months ago

bcardosolopes commented 4 months ago

Take this example:

%1 = ...
%2 = cir.vec.create(%1, %1 : !s32i, !s32i) : !cir.vector<!s32i x 2>

This is building a vector with two elements [%1, %1], right? Assuming I'm right, this is a bit confusing to read for two reasons:

It's possible we add constraints as to only print %2 = cir.vec.create(%1, %1) : !cir.vector<!s32i x 2>, since !cir.vector's elt type has all the info for logic to figure out. Alternatively, we could only add the type for the first operand, since all the others have to be the same: %2 = cir.vec.create(%1 : !s32i, %1) : !cir.vector<!s32i x 2>.

Thoughts?

cc: @gitoleg @dkolsen-pgi

dkolsen-pgi commented 4 months ago

Those are valid criticisms of the cir.vec.create syntax. I fully agree that the syntax is overly redundant. That was the first new operation that I added to ClangIR, so my skills at constraints and assembly format were still developing. The syntax (%1, %1 : !s32i, !s32i) is just the assembly format

`(` ($elements^ `:` type($elements))? `)`

which was the simplest way I could find to get it to work.

I like the suggestion of cir.vec.create(%1, %1) : !cir.vector<!s32i x 2>, leaving out the types of the element values since they can be inferred from the type of the vector. I will try to make this change, to help with my learning of constraints on operations.

dkolsen-pgi commented 4 months ago

Now I remember what the complication was. All of the element values must be of the same type. But I couldn't find an example of another operation like that. I know how to add a constraint to the operation definition that requires the type of an input to match the element type of a vector. But I don't know how to specify that all the items in a variadic list of inputs need to match the element type of a vector.

Is there an MLIR operation that has similar constraints that I can use as an example? If not, do you have any pointers about how to go about this?

bcardosolopes commented 4 months ago

@dkolsen-pgi thanks for looking at this. By the way, I think you did a great job introducing this, I couldn't notice the output during code review and the approach is totally fine, just a note that we can probably make it a bit better (feel free to leave this a "good-first-issue" to a newcomer if you prefer). I learned about this while looking at inline asm in https://github.com/llvm/clangir/pull/512

Is there an MLIR operation that has similar constraints that I can use as an example?

The most similar ones seems to be SameOperandsShape and SameOperandsElementType from OpBase.td, but not sure how well they apply - sometimes these are more tricky than it seems.

If not, do you have any pointers about how to go about this?

I started introducing a few CIR specific ones, see SameFirstOperandAndResultType, SameSecondOperandAndResultType and SameFirstSecondOperandAndResultType. Probably something along these lines should work too.