Skip to content

Compile error in llvm-ir when fusing gather ops with other ops. #3705

Closed

Description

@hanhanW

Hit an issue in LLVM-IR compilation when enable fusion on gather ops and other ops in #3682

With release BUILD, there is a log LLVM IR fails to verify, but a module is still generated. Thus it doesn't crash in compilation time.

https://github.com/google/iree/blob/f52156c6ca0ddac6461998989cb50ff8a1206e13/iree/compiler/Dialect/HAL/Target/LLVM/IR/LLVMIRTarget.cpp#L59-L64

With debug BUILD, it hit an assertion :

iree-translate: ../third_party/llvm-project/llvm/lib/IR/Instructions.cpp:2296: void llvm::InsertValueInst::init(llvm::Value *, llvm::Value *, ArrayRef<unsigned int>, const llvm::Twine &): Assertion `ExtractValueInst::getIndexedType(Agg->getType(), Idxs) == Val->getType() && "Inserted value must match indexed type!"' failed.

To repro:

patch #3682
iree-translate -iree-mlir-to-vm-bytecode-module -iree-hal-target-backends=llvm-ir ~/x.mlir -o uni

Narrow down the test case to:

module {
  func @main_ex_dispatch_18(%arg0: tensor<1x10xf32>, %arg1: tensor<1x10xf32>, %arg2: tensor<5x1x1xf32>, %arg3: tensor<i32>) -> tensor<1x10xf32> {
    %cst = constant dense<1.000000e+01> : tensor<1x10xf32>
    %cst_0 = constant dense<-1.000000e+01> : tensor<1x10xf32>
    %cst_1 = constant dense<1.000000e+00> : tensor<1x10xf32>
    %cst_2 = constant dense<0.000000e+00> : tensor<1x10xf32>
    %cst_3 = constant dense<5.000000e-01> : tensor<1x10xf32>
    %0 = "mhlo.torch_index_select"(%arg2, %arg3) {batch_dims = 0 : i64, dim = 0 : i64} : (tensor<5x1x1xf32>, tensor<i32>) -> tensor<1x1xf32>
    %1 = "mhlo.reshape"(%0) : (tensor<1x1xf32>) -> tensor<1xf32>
    %2 = "mhlo.broadcast_in_dim"(%1) {broadcast_dimensions = dense<0> : tensor<1xi64>} : (tensor<1xf32>) -> tensor<1x10xf32>
    %3 = mhlo.multiply %2, %cst_1 : tensor<1x10xf32>
    %4 = "mhlo.compare"(%3, %cst_2) {comparison_direction = "GT"} : (tensor<1x10xf32>, tensor<1x10xf32>) -> tensor<1x10xi1>
    %5 = mhlo.multiply %arg1, %cst_3 : tensor<1x10xf32>
    %6 = "mhlo.tanh"(%5) : (tensor<1x10xf32>) -> tensor<1x10xf32>
    %7 = mhlo.multiply %6, %cst_3 : tensor<1x10xf32>
    %8 = mhlo.add %7, %cst_3 : tensor<1x10xf32>
    %9 = mhlo.multiply %8, %arg0 : tensor<1x10xf32>
    %10 = "mhlo.select"(%4, %arg0, %9) : (tensor<1x10xi1>, tensor<1x10xf32>, tensor<1x10xf32>) -> tensor<1x10xf32>
    return %10 : tensor<1x10xf32>
  }
}

The fusion on tensors looks good to me:

module {
  func @main_ex_dispatch_18_ex_dispatch_0__num_workgroups__(!shapex.ranked_shape<[1,10]>, !shapex.ranked_shape<[1,10]>, !shapex.ranked_shape<[5,1,1]>, !shapex.ranked_shape<[]>, !shapex.ranked_shape<[1,10]>) -> (index, index, index) attributes {sym_visibility = "private"}
  func @main_ex_dispatch_18_ex_dispatch_0() attributes {hal.num_workgroups_fn = @main_ex_dispatch_18_ex_dispatch_0__num_workgroups__} {
    %c0 = constant 0 : index
    %cst = constant 1.000000e+00 : f32
    %cst_0 = constant 0.000000e+00 : f32
    %cst_1 = constant 5.000000e-01 : f32
    %0 = hal.interface.load.tensor @legacy_io::@arg2, offset = %c0 {operand_result_index = 2 : i32} : tensor<5x1x1xf32>
    %1 = hal.interface.load.tensor @legacy_io::@arg3, offset = %c0 {operand_result_index = 3 : i32} : tensor<i32>
    %2 = hal.interface.load.tensor @legacy_io::@arg1, offset = %c0 {operand_result_index = 1 : i32} : tensor<10xf32>
    %3 = hal.interface.load.tensor @legacy_io::@arg0, offset = %c0 {operand_result_index = 0 : i32} : tensor<10xf32>
    %4 = hal.interface.load.tensor @legacy_io::@arg0, offset = %c0 {operand_result_index = 0 : i32} : tensor<10xf32>
    %5 = linalg.indexed_generic {indexing_maps = [affine_map<(d0) -> ()>, affine_map<(d0) -> (d0)>, affine_map<(d0) -> (d0)>, affine_map<(d0) -> (d0)>, affine_map<(d0) -> (d0)>], iterator_types = ["parallel"]} ins(%1, %4, %2, %3 : tensor<i32>, tensor<10xf32>, tensor<10xf32>, tensor<10xf32>) {
    ^bb0(%arg0: index, %arg1: i32, %arg2: f32, %arg3: f32, %arg4: f32):  // no predecessors
      %6 = mulf %arg3, %cst_1 : f32
      %7 = tanh %6 : f32
      %8 = mulf %7, %cst_1 : f32
      %9 = addf %8, %cst_1 : f32
      %10 = mulf %9, %arg4 : f32
      %11 = index_cast %arg1 : i32 to index
      %12 = extract_element %0[%11, %c0, %c0] : tensor<5x1x1xf32>
      %13 = mulf %12, %cst : f32
      %14 = cmpf "ogt", %13, %cst_0 : f32
      %15 = select %14, %arg2, %10 : f32
      linalg.yield %15 : f32
    } -> tensor<10xf32>
    hal.interface.store.tensor %5, @legacy_io::@ret0, offset = %c0 {operand_result_index = 4 : i32} : tensor<10xf32>
    return
  }

Lowering to buffer's world also looks good to me.

func @main_ex_dispatch_18_ex_dispatch_0() attributes {hal.num_workgroups_fn = @main_ex_dispatch_18_ex_dispatch_0__num_workgroups__} {
  %0 = iree.placeholder for "interface buffer" {binding = @legacy_io::@ret0, operand_result_index = 4 : i32} : memref<10xf32>
  %c0 = constant 0 : index
  %cst = constant 1.000000e+00 : f32
  %cst_0 = constant 0.000000e+00 : f32
  %cst_1 = constant 5.000000e-01 : f32
  %1 = iree.placeholder for "interface buffer" {binding = @legacy_io::@arg2, operand_result_index = 2 : i32} : memref<5x1x1xf32>
  %2 = iree.placeholder for "interface buffer" {binding = @legacy_io::@arg3, operand_result_index = 3 : i32} : memref<i32>
  %3 = iree.placeholder for "interface buffer" {binding = @legacy_io::@arg1, operand_result_index = 1 : i32} : memref<10xf32>
  %4 = iree.placeholder for "interface buffer" {binding = @legacy_io::@arg0, operand_result_index = 0 : i32} : memref<10xf32>
  %5 = iree.placeholder for "interface buffer" {binding = @legacy_io::@arg0, operand_result_index = 0 : i32} : memref<10xf32>
  linalg.indexed_generic {indexing_maps = [affine_map<(d0) -> ()>, affine_map<(d0) -> (d0)>, affine_map<(d0) -> (d0)>, affine_map<(d0) -> (d0)>, affine_map<(d0) -> (d0)>], iterator_types = ["parallel"]} ins(%2, %5, %3, %4 : memref<i32>, memref<10xf32>, memref<10xf32>, memref<10xf32>) outs(%0 : memref<10xf32>) {
  ^bb0(%arg0: index, %arg1: i32, %arg2: f32, %arg3: f32, %arg4: f32, %arg5: f32):  // no predecessors
    %6 = mulf %arg3, %cst_1 : f32
    %7 = tanh %6 : f32
    %8 = mulf %7, %cst_1 : f32
    %9 = addf %8, %cst_1 : f32
    %10 = mulf %9, %arg4 : f32
    %11 = index_cast %arg1 : i32 to index
    %12 = load %1[%11, %c0, %c0] : memref<5x1x1xf32>
    %13 = mulf %12, %cst : f32
    %14 = cmpf "ogt", %13, %cst_0 : f32
    %15 = select %14, %arg2, %10 : f32
    linalg.yield %15 : f32
  }
  return
}

@asaadaldien Could you help on this?

Activity

  1. added
    bug 🐞
    codegen
    on Nov 3, 2020
  2. hanhanW commented on Nov 3, 2020

    @hanhanW
    ContributorAuthor

    This is the only test (unidirectional_lstm.mlir test) that blocks the PR, btw.

  3. asaadaldien commented on Nov 4, 2020

    @asaadaldien
    Contributor

    Looking into this.....

  4. linked a pull request that will close this issueRun CSE after FusionOnTensor #3722on Nov 4, 2020
  5. asaadaldien commented on Nov 4, 2020

    @asaadaldien
    Contributor

    Should be fixed by running CSE after tensor fusion #3722 so we load once, resulting a hal interface on buffers that is indexable and don't violate the ABI contract.

       %0 = hal.interface.load.tensor @legacy_io::@arg2, offset = %c0 {operand_result_index = 2 : i32} : tensor<5x1x1xf32>
        %1 = hal.interface.load.tensor @legacy_io::@arg3, offset = %c0 {operand_result_index = 3 : i32} : tensor<i32>
        %2 = hal.interface.load.tensor @legacy_io::@arg1, offset = %c0 {operand_result_index = 1 : i32} : tensor<10xf32>
        %3 = hal.interface.load.tensor @legacy_io::@arg0, offset = %c0 {operand_result_index = 0 : i32} : tensor<10xf32>
        %4 = hal.interface.load.tensor @legacy_io::@arg0, offset = %c0 {operand_result_index = 0 : i32} : tensor<10xf32
    

    ->

       %0 = hal.interface.load.tensor @legacy_io::@arg2, offset = %c0 {operand_result_index = 2 : i32} : tensor<5x1x1xf32>
        %1 = hal.interface.load.tensor @legacy_io::@arg3, offset = %c0 {operand_result_index = 3 : i32} : tensor<i32>
        %2 = hal.interface.load.tensor @legacy_io::@arg1, offset = %c0 {operand_result_index = 1 : i32} : tensor<10xf32>
        %3 = hal.interface.load.tensor @legacy_io::@arg0, offset = %c0 {operand_result_index = 0 : i32} : tensor<10xf32>
    
  6. hanhanW commented on Nov 5, 2020

    @hanhanW
    ContributorAuthor

    Thanks for the quick fix, this does fix the issue!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bug 🐞codegen

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions