llvmllvm-irllvm-c++-api

How to create mlir constant vector using OpBuilder


MLIR allows create constant vector such as

%1 = arith.constant dense<[1.1 2.2 3.3]> : vector<3xf32>

But how can I create this mlir operation using OpBuilder? I tried using codes like this

builder.create<arith::ConstantOp>(loc, builder.getDenseF32ArrayAttr(llvm::ArrayRef<float>{1.1, 2.2, 3.3}));

and then I found that arith::ConstantOp can only create scalar(int, float and index).

I want to know if there's a way to create constant vector using mlir class OpBuilder in c++.


Solution

  • use

    std::vector<float> x{1.1, 2.2, 3.3};
    // builder is an OpBuilder
    Value xVec = builder.create<arith::ConstantOp>(loc, DenseFPElementsAttr::get(VectorType::get(3, FloatType::getF32(builder.getContext())), ArrayRef<float>(x)));