Cannot create `tensor.from_elements` operation from python bindings

Hey everyone,

I’ve been trying to create tensor.from_elements operation from python bindings but I can’t get it to work. Here is what I thought should work:

from mlir.dialects import arith, tensor
from mlir.ir import Context, Location, IntegerType, RankedTensorType

with Context(), Location.unknown():
    x_type = IntegerType.get_signless(32)
    x = arith.ConstantOp(x_type, 42)

    y_type = RankedTensorType.get((1,), x_type)  # I couldn't find a way to use this
    y = tensor.FromElementsOp(x)  # and this does nothing at all

    # I expected y to be  %cst = tensor.from_elements 42 :  tensor<1xi32> but

    print(x)       # %c42_i32 = arith.constant 42 : i32
    print(y)       # %c42_i32 = arith.constant 42 : i32
    print(x == y)  # True

Not sure what I’m missing. Could you help me out?

Thanks
Umut

y_type = RankedTensorType.get([1], x_type) works fine.

FromElementsOp doesn’t have a programmatic constructor due to llvm-project/mlir/include/mlir/Dialect/Tensor/IR/TensorOps.td at d2da1a2f400ab54a874c1ba5430adc07a5249563 · llvm/llvm-project · GitHub. Somebody has to write an equivalent in Python to what it has in C++ using the mixin mechanism.

A cast-constructor gets called here, it should probably raise because of operation kind mismatch. Please file a bug for this. (cc @stellaraccident)

Done https://bugs.llvm.org/show_bug.cgi?id=52521 :+1:

Thanks for the reply. Happy coding!

Why do we need that again? E.g., default builders with infer return type interface (or infer tensor type, which probably will work better), should be able to handle this, but not sure if I’m missing something