Skip to content

ConverToSPIRV Pass fails to lower when f16 types are involved #3536

Closed

Description

@MaheshRavishankar

iree-codegen-convert-to-spirv-pass fails on this input even though Float16 capability is set.

module attributes {spv.target_env = #spv.target_env<#spv.vce<v1.3, [Float16, Shader, CooperativeMatrixNV], [SPV_KHR_storage_buffer_storage_class, SPV_NV_cooperative_matrix]>, {max_compute_workgroup_invo\
cations = 512 : i32, max_compute_workgroup_size = dense<[128, 128, 64]> : vector<3xi32>}>} {
  func @matmul_static_shape() attributes {spv.entry_point_abi = {local_size = dense<[32, 16, 1]> : vector<3xi32>}, vkspv.num_workgroups_fn = @matmul_static_shape__num_workgroups__} {
    %c64 = constant 64 : index
    %c0 = constant 0 : index
    %c1 = constant 1 : index
    %c16 = constant 16 : index
    %c32 = constant 32 : index
    %0 = iree.placeholder for "interface buffer" {binding = @legacy_io::@arg0, operand_result_num = 0 : i64} : memref<128x64xf16>
    %1 = iree.placeholder for "interface buffer" {binding = @legacy_io::@arg1, operand_result_num = 1 : i64} : memref<64x256xf16>
    %2 = iree.placeholder for "interface buffer" {binding = @legacy_io::@ret0, operand_result_num = 2 : i64} : memref<128x256xf16>
    %3 = "gpu.block_id"() {dimension = "x"} : () -> index
    %4 = "gpu.block_id"() {dimension = "y"} : () -> index
    %5 = muli %4, %c16 : index
    %6 = muli %3, %c32 : index
    %7 = "gpu.thread_id"() {dimension = "x"} : () -> index
    %8 = "gpu.thread_id"() {dimension = "y"} : () -> index
    %9 = cmpi "slt", %8, %c16 : index
    %10 = cmpi "slt", %7, %c32 : index
    %11 = and %9, %10 : i1
    scf.if %11 {
      scf.for %arg0 = %c0 to %c64 step %c1 {
        %12 = addi %5, %8 : index
        %13 = load %0[%12, %arg0] : memref<128x64xf16>
        %14 = addi %6, %7 : index
        %15 = load %1[%arg0, %14] : memref<64x256xf16>
        %16 = load %2[%12, %14] : memref<128x256xf16>
        %17 = mulf %13, %15 : f16
        %18 = addf %16, %17 : f16
        store %18, %2[%12, %14] : memref<128x256xf16>
      }
    }
    return
  }
  hal.interface @legacy_io attributes {sym_visibility = "private"} {
    hal.interface.binding @arg0, set=0, binding=0, type="StorageBuffer", access="Read"
    hal.interface.binding @arg1, set=0, binding=1, type="StorageBuffer", access="Read"
    hal.interface.binding @ret0, set=0, binding=2, type="StorageBuffer", access="Write|Discard"
  }
}

Activity

  1. added
    codegen
    codegen/spirv
    on Oct 19, 2020
  2. MaheshRavishankar commented on Oct 19, 2020

    @MaheshRavishankar
    CollaboratorAuthor

    Assigning this to @antiagainst for now. This is the error

    module attributes {spv.target_env = #spv.target_env<#spv.vce<v1.3, [Float16, Shader, CooperativeMatrixNV], [SPV_KHR_storage_buffer_storage_class, SPV_NV_cooperative_matrix]>, {max_compute_workgroup_invo\
    cations = 512 : i32, max_compute_workgroup_size = dense<[128, 128, 64]> : vector<3xi32>}>} {
      spv.module Logical GLSL450 {
        spv.globalVariable @__builtin_var_LocalInvocationId__ built_in("LocalInvocationId") : !spv.ptr<vector<3xi32>, Input>
        spv.globalVariable @__builtin_var_WorkgroupId__ built_in("WorkgroupId") : !spv.ptr<vector<3xi32>, Input>
        spv.globalVariable @__resource_var_0_2__ bind(0, 2) : !spv.ptr<!spv.struct<(!spv.array<32768 x f32, stride=4> [0])>, StorageBuffer>
        spv.globalVariable @__resource_var_0_1__ bind(0, 1) : !spv.ptr<!spv.struct<(!spv.array<16384 x f32, stride=4> [0])>, StorageBuffer>
        spv.globalVariable @__resource_var_0_0__ bind(0, 0) : !spv.ptr<!spv.struct<(!spv.array<8192 x f32, stride=4> [0])>, StorageBuffer>
        spv.func @matmul_static_shape() "None" attributes {spv.entry_point_abi = {local_size = dense<[32, 16, 1]> : vector<3xi32>}, vkspv.num_workgroups_fn = @matmul_static_shape__num_workgroups__} {
          %0 = spv.constant 64 : i32
          %1 = spv.constant 0 : i32
          %2 = spv.constant 1 : i32
          %3 = spv.constant 16 : i32
          %4 = spv.constant 32 : i32
          %5 = spv._address_of @__resource_var_0_0__ : !spv.ptr<!spv.struct<(!spv.array<8192 x f32, stride=4> [0])>, StorageBuffer>
          %6 = spv._address_of @__resource_var_0_1__ : !spv.ptr<!spv.struct<(!spv.array<16384 x f32, stride=4> [0])>, StorageBuffer>
          %7 = spv._address_of @__resource_var_0_2__ : !spv.ptr<!spv.struct<(!spv.array<32768 x f32, stride=4> [0])>, StorageBuffer>
          %8 = spv._address_of @__builtin_var_WorkgroupId__ : !spv.ptr<vector<3xi32>, Input>
          %9 = spv.Load "Input" %8 : vector<3xi32>
          %10 = spv.CompositeExtract %9[0 : i32] : vector<3xi32>
          %11 = spv._address_of @__builtin_var_WorkgroupId__ : !spv.ptr<vector<3xi32>, Input>
          %12 = spv.Load "Input" %11 : vector<3xi32>
          %13 = spv.CompositeExtract %12[1 : i32] : vector<3xi32>
          %14 = spv.IMul %13, %3 : i32
          %15 = spv.IMul %10, %4 : i32
          %16 = spv._address_of @__builtin_var_LocalInvocationId__ : !spv.ptr<vector<3xi32>, Input>
          %17 = spv.Load "Input" %16 : vector<3xi32>
          %18 = spv.CompositeExtract %17[0 : i32] : vector<3xi32>
          %19 = spv._address_of @__builtin_var_LocalInvocationId__ : !spv.ptr<vector<3xi32>, Input>
          %20 = spv.Load "Input" %19 : vector<3xi32>
          %21 = spv.CompositeExtract %20[1 : i32] : vector<3xi32>
          %22 = spv.SLessThan %21, %3 : i32
          %23 = spv.SLessThan %18, %4 : i32
          %24 = spv.LogicalAnd %22, %23 : i1
          spv.selection {
            spv.BranchConditional %24, ^bb1, ^bb2
          ^bb1:  // pred: ^bb0
            spv.loop {
              spv.Branch ^bb1(%1 : i32)
            ^bb1(%25: i32):  // 2 preds: ^bb0, ^bb2
              %26 = spv.SLessThan %25, %0 : i32
              spv.BranchConditional %26, ^bb2, ^bb3
            ^bb2:  // pred: ^bb1
              %27 = spv.IAdd %14, %21 : i32
              %28 = spv.constant 0 : i32
              %29 = spv.constant 0 : i32
              %30 = spv.constant 64 : i32
              %31 = spv.IMul %30, %27 : i32
              %32 = spv.IAdd %29, %31 : i32
              %33 = spv.constant 1 : i32
              %34 = spv.IMul %33, %25 : i32
              %35 = spv.IAdd %32, %34 : i32
              %36 = spv.AccessChain %5[%28, %35] : !spv.ptr<!spv.struct<(!spv.array<8192 x f32, stride=4> [0])>, StorageBuffer>, i32, i32
              %37 = spv.Load "StorageBuffer" %36 : f32
              %38 = spv.IAdd %15, %18 : i32
              %39 = spv.constant 0 : i32
              %40 = spv.constant 0 : i32
              %41 = spv.constant 256 : i32
              %42 = spv.IMul %41, %25 : i32
              %43 = spv.IAdd %40, %42 : i32
              %44 = spv.constant 1 : i32
              %45 = spv.IMul %44, %38 : i32
              %46 = spv.IAdd %43, %45 : i32
              %47 = spv.AccessChain %6[%39, %46] : !spv.ptr<!spv.struct<(!spv.array<16384 x f32, stride=4> [0])>, StorageBuffer>, i32, i32
              %48 = spv.Load "StorageBuffer" %47 : f32
              %49 = spv.constant 0 : i32
              %50 = spv.constant 0 : i32
              %51 = spv.constant 256 : i32
              %52 = spv.IMul %51, %27 : i32
              %53 = spv.IAdd %50, %52 : i32
              %54 = spv.constant 1 : i32
              %55 = spv.IMul %54, %38 : i32
              %56 = spv.IAdd %53, %55 : i32
              %57 = spv.AccessChain %7[%49, %56] : !spv.ptr<!spv.struct<(!spv.array<32768 x f32, stride=4> [0])>, StorageBuffer>, i32, i32
              %58 = spv.Load "StorageBuffer" %57 : f32
              %59 = "spv.FMul"(%37, %48) : (f32, f32) -> f16
              %60 = "spv.FAdd"(%58, %59) : (f32, f16) -> f16
              %61 = spv.constant 0 : i32
              %62 = spv.constant 0 : i32
              %63 = spv.constant 256 : i32
              %64 = spv.IMul %63, %27 : i32
              %65 = spv.IAdd %62, %64 : i32
              %66 = spv.constant 1 : i32
              %67 = spv.IMul %66, %38 : i32
             %68 = spv.IAdd %65, %67 : i32
              %69 = spv.AccessChain %7[%61, %68] : !spv.ptr<!spv.struct<(!spv.array<32768 x f32, stride=4> [0])>, StorageBuffer>, i32, i32
              spv.Store "StorageBuffer" %69, %60 : f16
              %70 = spv.IAdd %25, %2 : i32
              spv.Branch ^bb1(%70 : i32)
            ^bb3:  // pred: ^bb1
              spv._merge
            }
            spv.Branch ^bb2
          ^bb2:  // 2 preds: ^bb0, ^bb1
            spv._merge
          }
          spv.Return
        }
      }
      func @matmul_static_shape__num_workgroups__(%arg0: !shapex.ranked_shape<[128,64]>, %arg1: !shapex.ranked_shape<[64,256]>, %arg2: !shapex.ranked_shape<[128,256]>) -> (index, index, index) attributes {s\
    ym_visibility = "private"} {
        %c8 = constant 8 : index
        %c1 = constant 1 : index
        return %c8, %c8, %c1 : index, index, index
      }
      hal.interface @legacy_io attributes {sym_visibility = "private"} {
        hal.interface.binding @arg0, set=0, binding=0, type="StorageBuffer", access="Read"
        hal.interface.binding @arg1, set=0, binding=1, type="StorageBuffer", access="Read"
        hal.interface.binding @ret0, set=0, binding=2, type="StorageBuffer", access="Write|Discard"
      }
    }
    
    <unknown>:0: error: 'spv.FMul' op requires the same type for all operands and results
    <unknown>:0: note: see current operation: %59 = "spv.FMul"(%37, %48) : (f32, f32) -> f16
    

    For whatever reason the spv.FAdd ends up with f16 and f32 operands

  3. antiagainst commented on Oct 20, 2020

    @antiagainst
    Member

    The input IR lacks the StorageBuffer16BitAccess, which allows using 16bit floats in StorageBuffer storage class. (The Float16 capability only allows using 16-bit arithmetics.

    I assume you are going from NVIDIA target triple. Seems some extensions are not properly registered in the target triple. I'll update it.

  4. antiagainst commented on Oct 20, 2020

    @antiagainst
    Member

    #3546 is landed. @MaheshRavishankar please try it out and close the issue if it's the proper fix.

  5. MaheshRavishankar commented on Oct 21, 2020

    @MaheshRavishankar
    CollaboratorAuthor

    Fixed now. Thanks!

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

Metadata

Metadata

Assignees

Labels

codegencodegen/spirv

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions