When I use c++ to create memref::SubViewOp, I get garbled code

The following code is written in Pass, but when I run it, I get garbled code.I don’t know if this is a bug or maybe my code is not written correctly.If anyone can help me, I would appreciate it.Thanks!

 auto loop = rewriter.create<scf::ForOp>(loc, lowerBound, batchSize, stride); 
 Value loopIdv = loop.getInductionVar();
 rewriter.setInsertionPointToStart(loop.getBody());
 ValueRange offsets = {loopIdv, constant0, constant0 };
 ValueRange sizes = {stride, input0Row, input0Col };
 ValueRange strides = {stride, stride, stride};
 MemRefType subViewType =  MemRefType::get({input0Shape[1], input0Shape[2]}, 
 rewriter.getF32Type());
 Value subInput0 = rewriter.create<memref::SubViewOp>(loc, subViewType, input0, offsets, sizes, strides);
 Pattern : 'linalg.batch_matmul -> ()' {
Trying to match "{anonymous}::BatchMatMulOpLower"
ImplicitTypeIDRegistry::lookupOrInsert(mlir::InferIntRangeInterface::Trait<mlir::TypeID::get() [with Trait = mlir::InferIntRangeInterface::Trait]::Empty>)
    ** Insert  : 'arith.constant'(0x560638451850)
    ** Insert  : 'arith.constant'(0x5606383fb350)
    ** Insert  : 'arith.constant'(0x56063840e040)
    ** Insert  : 'arith.constant'(0x56063840dbd0)
    ** Insert  : 'arith.constant'(0x5606384118f0)
    ** Insert  : 'arith.constant'(0x560638411460)
    ** Insert  : 'arith.constant'(0x5606383fc670)
    ** Insert  : 'arith.constant'(0x5606383fbc70)
    ** Insert  : 'arith.constant'(0x5606383fb7e0)
    ** Insert  : 'arith.constant'(0x5606383cc570)
ImplicitTypeIDRegistry::lookupOrInsert(mlir::OpTrait::HasParent<mlir::scf::ExecuteRegionOp, mlir::scf::ForOp, mlir::scf::IfOp, mlir::scf::IndexSwitchOp, mlir::scf::ParallelOp, mlir::scf::WhileOp>::Impl<mlir::TypeID::get() [with Trait = mlir::OpTrait::HasParent<mlir::scf::ExecuteRegionOp, mlir::scf::ForOp, mlir::scf::IfOp, mlir::scf::IndexSwitchOp, mlir::scf::ParallelOp, mlir::scf::WhileOp>::Impl]::Empty>)
    ** Insert  : 'scf.yield'(0x560638456ab0)
    ** Insert  : 'scf.for'(0x560638456b10)
    ** Insert  : '0yA8V�jE8VyA8V��<8V�������<8VHCD8V

vE8Vh�B8V��>8V�uE8V��>8V::8V�vE8V�uE8V��<8Vp<8V�vE8V�uE8V��<8V��>8V�vE8V�uE8VpQ� vE8V�vE8V�uE8V��<8V@vE8VwE8V�uE8V��<8V`vE8V wE8V�uE8VpQ��vE8V��<8V�uE8V��<8V�vE8V��<8V�uE8V��<8V�vE8VpQ��uE8VpQ���������������������������������������������������������������������������������������PlB8V aE8V��������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������lB8V$aE8V lB8VaE8V����������������������������������������������������������������������ђ'(0x5606384575b0)
    ** Erase   : 'linalg.batch_matmul'(0x560638428300)
"{anonymous}::BatchMatMulOpLower" result 1

    //===-------------------------------------------===//
    Legalizing operation : 'arith.constant'(0x560638451850) {
      %6 = "arith.constant"() {value = 2 : index} : () -> index

    } -> SUCCESS : operation marked legal by the target
    //===-------------------------------------------===//

    //===-------------------------------------------===//
    Legalizing operation : 'arith.constant'(0x5606383fb350) {
      %7 = "arith.constant"() {value = 3 : index} : () -> index

    } -> SUCCESS : operation marked legal by the target
    //===-------------------------------------------===//

    //===-------------------------------------------===//
    Legalizing operation : 'arith.constant'(0x56063840e040) {
      %8 = "arith.constant"() {value = 3 : index} : () -> index

    } -> SUCCESS : operation marked legal by the target
    //===-------------------------------------------===//

    //===-------------------------------------------===//
    Legalizing operation : 'arith.constant'(0x56063840dbd0) {
      %9 = "arith.constant"() {value = 3 : index} : () -> index

    } -> SUCCESS : operation marked legal by the target
    //===-------------------------------------------===//

    //===-------------------------------------------===//
    Legalizing operation : 'arith.constant'(0x5606384118f0) {
      %10 = "arith.constant"() {value = 3 : index} : () -> index

    } -> SUCCESS : operation marked legal by the target
    //===-------------------------------------------===//

    //===-------------------------------------------===//
    Legalizing operation : 'arith.constant'(0x560638411460) {
      %11 = "arith.constant"() {value = 3 : index} : () -> index

    } -> SUCCESS : operation marked legal by the target
    //===-------------------------------------------===//

    //===-------------------------------------------===//
    Legalizing operation : 'arith.constant'(0x5606383fc670) {
      %12 = "arith.constant"() {value = 3 : index} : () -> index

    } -> SUCCESS : operation marked legal by the target
    //===-------------------------------------------===//

    //===-------------------------------------------===//
    Legalizing operation : 'arith.constant'(0x5606383fbc70) {
      %13 = "arith.constant"() {value = 1 : index} : () -> index

    } -> SUCCESS : operation marked legal by the target
    //===-------------------------------------------===//

    //===-------------------------------------------===//
    Legalizing operation : 'arith.constant'(0x5606383fb7e0) {
      %14 = "arith.constant"() {value = 0 : index} : () -> index

    } -> SUCCESS : operation marked legal by the target
    //===-------------------------------------------===//

    //===-------------------------------------------===//
    Legalizing operation : 'arith.constant'(0x5606383cc570) {
      %15 = "arith.constant"() {value = 0 : index} : () -> index

    } -> SUCCESS : operation marked legal by the target
    //===-------------------------------------------===//

    //===-------------------------------------------===//
    Legalizing operation : 'scf.yield'(0x560638456ab0) {
      "scf.yield"() : () -> ()

    } -> SUCCESS : operation marked legal by the target
    //===-------------------------------------------===//

    //===-------------------------------------------===//
    Legalizing operation : 'scf.for'(0x560638456b10) {
    } -> SUCCESS : operation marked legal by the target
    //===-------------------------------------------===//

    //===-------------------------------------------===//
    Legalizing operation : '0yA8V�jE8VyA8V��<8V�������<8VHCD8V

vE8Vh�B8V��>8V�uE8V��>8V::8V�vE8V�uE8V��<8Vp<8V�vE8V�uE8V��<8V��>8V�vE8V�uE8VpQ� vE8V�vE8V�uE8V��<8V@vE8VwE8V�uE8V��<8V`vE8V wE8V�uE8VpQ��vE8V��<8V�uE8V��<8V�vE8V��<8V�uE8V��<8V�vE8VpQ��uE8VpQ��������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������� aE8V����������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������B8V��������������������������������������������������������������������������������������������������������������������������������������������q`28V�;8V��?8V��?8V�<8VР;8V`9@8V��=8V@�98V��;8V@�?8V1(�+�Z3�+�Z3�{E8VHE8V�{E8VHE8V|E8VHE8V|E8VHE8V�{E8V���������������������������������������������������������������@2�Z3ЋE8VЋE8V��������������������������������������������������������s>8V��E8V�����������������������������kB8V��E8V������������������������������������������������������������������������������������������������������������������������������������������������������������;8V�E8V�������������������������������������������������yA8�E8V�������������������������������������������������������������������������������������������������������������������������������+�Z3�E8V0(`28V����������������������uE8V�������`�<8V���������?8V��B8V��������A8V
з?8V��>8VPlB8V������>8V���������������������������������������������������������������@�?8V���������>8V�������P�B8V��������������������������������������������������������������������������������������?8V������������������������������������������@E8V���������@8V  �lB8V����`�?8V
��������������p/A8V����М?8VPA8V                                                                                        lB8V�����������`�?8V
                               ��������������h�+�Z3�{E8V����������<8V���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������!A8V�������������������������������������������������������������������������/A8V��������������������������������������������������������������������������������������������������PlB8V�E8V��������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������lB8V�E8V lB8V��E8V�����������������������������������p/A8V
                                �E8V�����������������������������['(0x5606384575b0) {
      %16 = "0yA8\06V\00\00\B0jE8\06V\00\00\10yA8\06V\00\00\D0\DC<8\06V\00\00\FF\FF\FF\FF\01\00\00\00\00\00\00\00\00\00\00\80\C8\CE<8\06V\00\00HCD8\06V\00\00\0A\00\00\00\0A\00\00\00\00vE8\06V\00\00h\83B8\06V\00\00\80\9D>8\06V\00\00\B0uE8\06V\00\00\80\9D>8\06V\00\00\10::8\06V\00\00\80vE8\06V\00\00\B0uE8\06V\00\00\D0\DC<8\06V\00\00p\04<8\06V\00\00\A0vE8\06V\00\00\B0uE8\06V\00\00\C8\CE<8\06V\00\00\80\9D>8\06V\00\00\C0vE8\06V\00\00\B0uE8\06V\00\00pQ\CE\1B\FD\7F\00\00 vE8\06V\00\00\E0vE8\06V\00\00\B0uE8\06V\00\00\D0\DC<8\06V\00\00@vE8\06V\00\00\00wE8\06V\00\00\B0uE8\06V\00\00\C8\CE<8\06V\00\00`vE8\06V\00\00 wE8\06V\00\00\B0uE8\06V\00\00pQ\CE\1B\FD\7F\00\00\80vE8\06V\00\00\D0\DC<8\06V\00\00\B0uE8\06V\00\00\D0\DC<8\06V\00\00\A0vE8\06V\00\00\C8\CE<8\06V\00\00\B0uE8\06V\00\00\C8\CE<8\06V\00\00\C0vE8\06V\00\00pQ\CE\1B\FD\7F\00\00\B0uE8\06V\00\00pQ\CE\1B\FD\7F\00\00\00\00\00\00\00\00\00\00\11\04\00\00\00\00\00\00\00\F0\FF\FF\FF\FF\FF\FF\00\00\00\00\00\00\00\00\00\F0\FF\FF\FF\FF\FF\FF\00\00\00\00\00\00\00\00\00\F0\FF\FF\FF\FF\FF\FF\00\F0\FF\FF\FF\FF\FF\FF\00\F0\FF\FF\FF\FF\FF\FF\00\00\00\00\00\00\00\00\00\F0\FF\FF\FF\FF\FF\FF\00\00\00\00\00\00\00\00\00\F0\FF\FF\FF\FF\FF\FF\00\F0\FF\FF\FF\FF\FF\FF\00\F0\FF\FF\FF\FF\FF\FF\00\00\00\00\00\00\00\00\00\F0\FF\FF\FF\FF\FF\FF\00\00\00\00\00\00\00\00\00\F0\FF\FF\FF\FF\FF\FF\00\F0\FF\FF\FF\FF\FF\FF\00\F0\FF\FF\FF\FF\FF\FF\00\00\00\00\00\00\00\00\00\F0\FF\FF\FF\FF\FF\FF\00\00\00\00\00\00\00\00\00\F0\FF\FF\FF\FF\FF\FF\00\F0\FF\FF\FF\FF\FF\FF\00\F0\FF\FF\FF\FF\FF\FF\00\00\00\00\00\00\00\00\00\F0\FF\FF\FF\FF\FF\FF\00\00\00\00\00\00\00\00\00\F0\FF\FF\FF\FF\FF\FF\00\F0\FF\FF\FF\FF\FF\FF\00\F0\FF\FF\FF\FF\FF\FF\00\00\00\00\00\00\00\00\00\F0\FF\FF\FF\FF\FF\FF\00\00\00\00\00\00\00\00\00\F0\FF\FF\FF\FF\FF\FF\00\F0\FF\FF\FF\FF\FF\FF\00\F0\FF\FF\FF\FF\FF\FF\00\00\00\00\00\00\00\00\00\F0\FF\FF\FF\FF\FF\FF aE8\06V\00\00\00\F0\FF\FF\FF\FF\FF\FF\00\F0\FF\FF\FF\FF\FF\FF\00\F0\FF\FF\FF\FF\FF\FF\00\00\00\00\00\00\00\00\00\F0\FF\FF\FF\FF\FF\FF\00\00\00\00\00\00\00\00\00\F0\FF\FF\FF\FF\FF\FF\00\F0\FF\FF\FF\FF\FF\FF\00\F0\FF\FF\FF\FF\FF\FF\00\00\00\00\00\00\00\00\00\F0\FF\FF\FF\FF\FF\FF\00\00\00\00\00\00\00\00\00\F0\FF\FF\FF\FF\FF\FF\00\F0\FF\FF\FF\FF\FF\FF\00\F0\FF\FF\FF\FF\FF\FF\00\00\00\00\00\00\00\00\00\F0\FF\FF\FF\FF\FF\FF\00\00\00\00\00\00\00\00\00\F0\FF\FF\FF\FF\FF\FF\00\F0\FF\FF\FF\FF\FF\FF\00\F0\FF\FF\FF\FF\FF\FF\00\00\00\00\00\00\00\00\00\F0\FF\FF\FF\FF\FF\FF\00\00\00\00\00\00\00\00\00\F0\FF\FF\FF\FF\FF\FF\00\F0\FF\FF\FF\FF\FF\FF\00\F0\FF\FF\FF\FF\FF\FF\00\00\00\00\00\00\00\00\00\F0\FF\FF\FF\FF\FF\FF\00\00\00\00\00\00\00\00\00\F0\FF\FF\FF\FF\FF\FF\00\F0\FF\FF\FF\FF\FF\FF\00\F0\FF\FF\FF\FF\FF\FF\00\00\00\00\00\00\00\00\00\F0\FF\FF\FF\FF\FF\FF\00\00\00\00\00\00\00\00\00\F0\FF\FF\FF\FF\FF\FF\00\F0\FF\FF\FF\FF\FF\FF\00\F0\FF\FF\FF\FF\FF\FF\00\00\00\00\00\00\00\00\00\F0\FF\FF\FF\FF\FF\FF\00\00\00\00\00\00\00\00\00\F0\FF\FF\FF\FF\FF\FF\00\F0\FF\FF\FF\FF\FF\FF\00\F0\FF\FF\FF\FF\FF\FF\00\00\00\00\00\00\00\00\00\F0\FF\FF\FF\FF\FF\FF\00\00\00\00\00\00\00\00\00\F0\FF\FF\FF\FF\FF\FF\00\F0\FF\FF\FF\FF\FF\FF\00\F0\FF\FF\FF\FF\FF\FF\00\00\00\00\00\00\00\00\00\F0\FF\FF\FF\FF\FF\FF\00\00\00\00\00\00\00\00\00\F0\FF\FF\FF\FF\FF\FF\00\F0\FF\FF\FF\FF\FF\FF\00\F0\FF\FF\FF\FF\FF\FF\00\00\00\00\00\00\00\00\00\83B8\06V\00\00\00\00\00\00\00\00\00\00\00\F0\FF\FF\FF\FF\FF\FF\00\F0\FF\FF\FF\FF\FF\FF\00\F0\FF\FF\FF\FF\FF\FF\00\00\00\00\00\00\00\00\00\F0\FF\FF\FF\FF\FF\FF\00\00\00\00\00\00\00\00\00\F0\FF\FF\FF\FF\FF\FF\00\F0\FF\FF\FF\FF\FF\FF\00\F0\FF\FF\FF\FF\FF\FF\00\00\00\00\00\00\00\00\00\F0\FF\FF\FF\FF\FF\FF\00\00\00\00\00\00\00\00\00\F0\FF\FF\FF\FF\FF\FF\00\F0\FF\FF\FF\FF\FF\FF\00\F0\FF\FF\FF\FF\FF\FF\00\00\00\00\00\00\00\00\00\F0\FF\FF\FF\FF\FF\FF\00\00\00\00\00\00\00\00\00\F0\FF\FF\FF\FF\FF\FF\00\F0\FF\FF\FF\FF\FF\FF\00\F0\FF\FF\FF\FF\FF\FF\00\00\00\00\00\00\00\00\00\F0\FF\FF\FF\FF\FF\FF\00\00\00\00\00\00\00\00\00\F0\FF\FF\FF\FF\FF\FF\00\F0\FF\FF\FF\FF\FF\FF\00\F0\FF\FF\FF\FF\FF\FF\00\00\00\00\00\00\00\00\00\F0\FF\FF\FF\FF\FF\FFq\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\10`28\06V\00\00\F0\9E;8\06V\00\00\90\A1?8\06V\00\00\A0\9F?8\06V\00\00\F0\BD<8\06V\00\00\D0\A0;8\06V\00\00`9@8\06V\00\00\E0\D7=8\06V\00\00@\D698\06V\00\00\80\9F;8\06V\00\00@\A0?8\06V\00\00\00\00\00\00\00\00\00\00\11\10\00\00\00\00\00\00\E0+\D7Z3\7F\00\00\E0+\D7Z3\7F\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\D0{E8\06V\00\00\00\00\00\00\00\00\00\00\18HE8\06V\00\00\E8{E8\06V\00\00\00\00\00\00\00\00\00\00\18HE8\06V\00\00\00|E8\06V\00\00\00\00\00\00\00\00\00\00\18HE8\06V\00\00\00|E8\06V\00\00\00\00\00\00\00\00\00\00\18HE8\06V\00\00\E8{E8\06V\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\F0\FF\FF\FF\FF\FF\FF\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\F0\FF\FF\FF\FF\FF\FF\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\F0\FF\FF\FF\FF\FF\FF\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\F0\FF\FF\FF\FF\FF\FF\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\F0\FF\FF\FF\FF\FF\FF\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\F0\FF\FF\FF\FF\FF\FF\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00

I suspect that the usage of ValueRange in this code is incorrect. ValueRange is a non-owning reference to a list of values. The code assigns a temporary initializer list to it, which no longer exists after the assignment statement is executed. ValueRange is therefore a dangling reference. Use SmallVector instead.

These kinds of memory errors should be easily caught by address or memory sanitizer, compile MLIR with -DLLVM_USE_SANITIZER=Address.

Thanks for you reply!This problem was solved.But I have encountered a new problem.I have a question to ask you.I have specified the source offsets, sizes, strides of the subview. why do I need static_offsets, static_sizes, static_strides here. I have read the relevant memrefType material but still can’t understand.

test.mlir:23:3: error: expected result type to be 'memref<?x?x?xf32, strided<[?, ?, ?], offset: ?>>' or a rank-reduced version. (mismatch of result sizes) 
  linalg.batch_matmul
  ^
test.mlir:23:3: note: see current operation: %16 = "memref.subview"(%0, %arg0, %15, %15, %13, %7, %8, %13, %13, %13) {operand_segment_sizes = array<i32: 1, 3, 3, 3>, static_offsets = array<i64: -9223372036854775808, -9223372036854775808, -9223372036854775808>, static_sizes = array<i64: -9223372036854775808, -9223372036854775808, -9223372036854775808>, static_strides = array<i64: -9223372036854775808, -9223372036854775808, -9223372036854775808>} : (memref<2x3x3xf32>, index, index, index, index, index, index, index, index, index) -> memref<3x3xf32>

I intend to insert linalg.matmul into the for loop to implement linalg.batch_matmul, and I have no way to take out the literals in the loop induced variables so that I can’t specify static_offsets(It needs int type).

Memrefs may have static or dynamic sizes. Static sizes are known at compile time and are printed as memref<3x3xf32>. Dynamic sizes are only known at runtime and are printed as memref<?x?xf32>. They can be mixed in the same type. Similarly, strides and the offset may be static or dynamic.

Same is true for many operations on memrefs. Specifically, subview may be producing a memref with static or dynamic shape components. Static components (size, stride, offset) of the shape are represented using attributes. When the components are dynamic, the attribute contains kDynamicDim, and the runtime value is stored in one of the operands passed to subview.

In the snippet above, the subview is being constructed with no static components and all dynamic components because. Furthermore, it is given three sizes. Therefore, the type is expected to be memref<?x?x?xf32, strided<[?, ?, ?], offset: ?>>, that is a 3D memref with all dynamic sizes, strides and offset. Instead, the snippet gives is a 2D memref with all static sizes and no dynamic components. Hence the error.

1 Like

I really appreciate your help and I think I understand exactly what you mean.

Hi, did you solved this problem. I wander to know how to do it?

I wonder how the correct one should be written, and if StridedLayoutAttr needs to be defined

Some questions about memref.subview - MLIR - LLVM Discussion Forums
Sorry it’s been a while since I’ve logged into Discourse, I hope this link helps.