Attributes in MLIR

What are attributes in MLIR and why are they used?

In “Toy” tutorial chapter 2, the ‘toy.constant’ operation is given as follows.

%4 = "toy.constant"() {value = dense<1.0> : tensor<2x3xf64>} : () -> tensor<2x3xf64>

Here, an attribute with name ‘value’ is passed as an argument to the operation. It is described in the tutorial as ’ a dense elements attribute". The documentation defines ‘DenseIntOrFPElementsAttr’ as "An Attribute containing a dense multi-dimensional array of integer or floating-point values "
What information does this attribute provide?
Also what do the given examples: dense<10> : tensor<2xi32>


dense<[10.0, 11.0]> : tensor<2xf32>** in the documentation mean?** (I didn’t get the description given in the form of comments)

While defining this ‘toy.constant’ operation in ODS format, the ‘arguments’ field is overwritten as follows:

  let arguments = (ins F64ElementsAttr:$value);

If instead of ‘$value’ I use ‘$valueattr’ in the above expression, then would the ‘toy.constant’ operation be used as follows in MLIR?

%4 = "toy.constant"() {valueattr = dense<1.0> : tensor<2x3xf64>} : () -> tensor<2x3xf64>

What if I don’t specify a name for the attribute in ODS format, for example:

  let arguments = (ins F64ElementsAttr);

How would I use ‘toy.constant’ operation in MLIR in this case?


Attribute are here to store constant data. For DenseIntOrFPElementsAttr, this is an attribute which stores a multi-dimensional array containing float or integer elements. The type associated can be tensor or vector.
The information this attribute provide is operation dependent, on the constant operation is store the actual data materialized by the constant operation as an SSA value.

Both are single-dimensional array with 2 elements of type f32. The first one contains [10.0, 10,0] and the second [10.0, 11.0]. The “splat” means that all elements are the same so we can write it once and the repetition is implicit.


  let arguments = (ins F64ElementsAttr);

Did you try? I would think it wouldn’t work and names are mandatory.

1 Like