Skip to content

Change dispatch ops to use nested references #1890

Closed

Description

@benvanik

Dispatch ops (such as flow.dispatch and hal.command_buffer.dispatch) don't use nested references to specify the executable and entry point target. This makes work such as #1144 and #1587 that may need to update references much more difficult. We should switch to nested references across all of the dialects.

Today, things like flow.dispatch take a FlatSymbolRefAttr for the executable and the entry point, so the signature is something like flow.dispatch @executable, @entry_point, .... This prevents fancy MLIR features. Instead, we should be using the nested references like @executable::@entry_point. This means changing the FlatSymbolRefAttr (to just SymbolRefAttr maybe?) and places that are performing the lookup changing to using the right SymbolTable methods (like lookupNearest...). We may also need to adjust some symbol visibility; for example some public symbols (such as entry points in flow.executable/hal.executable) should probably be nested.

Example of badness:

The HAL one is interesting as it has an executable instance as an arg (%foo) as well as the entry point; what's currently just an ordinal should instead be a symbol reference.

Before: hal.command_buffer.dispatch %cmd, %executable, entry_point = 0, ...
After: hal.command_buffer.dispatch %cmd, %executable, entry_point = @MyExecutable::EntryPoint, ...

This will allow us to merge/split executables after generating command buffers and is required for linking support (#1587).

Activity

  1. added
    compiler/dialects
    enhancement ➕
    on May 12, 2020
  2. added this to the milestone on May 12, 2020
  3. modified the milestones: , on Jun 23, 2020
  4. self-assigned this
    on Jul 6, 2020
  5. ScottTodd commented on Jul 20, 2020

    @ScottTodd
    Member

    I've made progress on using nested references in hal.command_buffer.dispatch for the VMLA backend (see code here). I'm still trying to figure out how to update Vulkan-SPIRV though. #1816 introduced a concept of generating multiple entry points for a single dispatch function, without also generating multiple hal.executable.entry_point ops. Should the hal.executable.entry_point ops correspond 1:1 with spv.EntryPoint ops?

    Click to expand `fullyconnected.mlir` snippet

    // *** IR Dump After mlir::iree_compiler::IREE::HAL::TranslateExecutablesPass ***
    hal.executable @main_ex_dispatch_1 attributes {sym_visibility = "private"} {
      hal.interface @legacy_io {
        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"
      }
      hal.executable.entry_point @main_ex_dispatch_1 attributes {interface = @legacy_io, ordinal = 0 : i32, signature = (tensor<5x3xf32>, tensor<3x5xf32>) -> tensor<5x5xf32>}
      hal.executable.target "vulkan*" {
        module attributes {spv.target_env = #spv.target_env<#spv.vce<v1.3, [Shader], [SPV_KHR_storage_buffer_storage_class]>, {max_compute_workgroup_invocations = 128 : i32, max_compute_workgroup_size = dense<[128, 128, 64]> : vector<3xi32>}>, vkspv.entry_point_schedule = ["main_ex_dispatch_1_dispatch_0", "main_ex_dispatch_1_dispatch_1"]} {
          spv.module Logical GLSL450 requires #spv.vce<v1.0, [Shader], [SPV_KHR_storage_buffer_storage_class]> {
            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_1__ bind(0, 1) : !spv.ptr<!spv.struct<!spv.array<15 x f32, stride=4> [0]>, StorageBuffer>
            spv.globalVariable @__resource_var_0_0__ bind(0, 0) : !spv.ptr<!spv.struct<!spv.array<15 x f32, stride=4> [0]>, StorageBuffer>
            spv.globalVariable @__resource_var_0_2__ bind(0, 2) : !spv.ptr<!spv.struct<!spv.array<25 x f32, stride=4> [0]>, StorageBuffer>
            spv.func @main_ex_dispatch_1_dispatch_1() "None" attributes {vkspv.workgroup_count_from_result_shape = 2 : i32} {
              %0 = spv.constant 4 : i32
              %1 = spv.constant 8 : i32
              %2 = spv.constant -1 : i32
              %3 = spv.constant 1 : i32
              %4 = spv.constant 3 : i32
              %5 = spv.constant 0 : i32
              %6 = spv.constant 5 : i32
              %7 = spv._address_of @__resource_var_0_2__ : !spv.ptr<!spv.struct<!spv.array<25 x f32, stride=4> [0]>, StorageBuffer>
              %8 = spv._address_of @__resource_var_0_0__ : !spv.ptr<!spv.struct<!spv.array<15 x f32, stride=4> [0]>, StorageBuffer>
              %9 = spv._address_of @__resource_var_0_1__ : !spv.ptr<!spv.struct<!spv.array<15 x f32, stride=4> [0]>, StorageBuffer>
              %10 = spv._address_of @__builtin_var_WorkgroupId__ : !spv.ptr<vector<3xi32>, Input>
              %11 = spv.Load "Input" %10 : vector<3xi32>
              %12 = spv.CompositeExtract %11[0 : i32] : vector<3xi32>
              %13 = spv.Load "Input" %10 : vector<3xi32>
              %14 = spv.CompositeExtract %13[1 : i32] : vector<3xi32>
              %15 = spv.IMul %14, %1 : i32
              %16 = spv.IMul %12, %1 : i32
              spv.loop {
                spv.Branch ^bb1(%5 : i32)
              ^bb1(%17: i32):  // 2 preds: ^bb0, ^bb2
                %18 = spv.SLessThan %17, %4 : i32
                spv.BranchConditional %18, ^bb2, ^bb3
              ^bb2:  // pred: ^bb1
                %19 = spv.IMul %15, %2 : i32
                %20 = spv.IAdd %19, %6 : i32
                %21 = spv.SLessThan %1, %20 : i32
                %22 = spv.Select %21, %1, %20 : i1, i32
                %23 = spv.IMul %17, %2 : i32
                %24 = spv.IAdd %23, %4 : i32
                %25 = spv.SLessThan %0, %24 : i32
                %26 = spv.Select %25, %0, %24 : i1, i32
                %27 = spv.IMul %16, %2 : i32
                %28 = spv.IAdd %27, %6 : i32
                %29 = spv.SLessThan %1, %28 : i32
                %30 = spv.Select %29, %1, %28 : i1, i32
                %31 = spv._address_of @__builtin_var_LocalInvocationId__ : !spv.ptr<vector<3xi32>, Input>
                %32 = spv.Load "Input" %31 : vector<3xi32>
                %33 = spv.CompositeExtract %32[0 : i32] : vector<3xi32>
                %34 = spv.Load "Input" %31 : vector<3xi32>
                %35 = spv.CompositeExtract %34[1 : i32] : vector<3xi32>
                %36 = spv.SLessThan %35, %22 : i32
                %37 = spv.SLessThan %33, %30 : i32
                %38 = spv.LogicalAnd %36, %37 : i1
                spv.selection {
                  spv.BranchConditional %38, ^bb1, ^bb2
                ^bb1:  // pred: ^bb0
                  spv.loop {
                    spv.Branch ^bb1(%5 : i32)
                  ^bb1(%40: i32):  // 2 preds: ^bb0, ^bb2
                    %41 = spv.SLessThan %40, %26 : i32
                    spv.BranchConditional %41, ^bb2, ^bb3
                  ^bb2:  // pred: ^bb1
                    %42 = spv.IAdd %15, %35 : i32
                    %43 = spv.IAdd %17, %40 : i32
                    %44 = spv.IMul %42, %4 : i32
                    %45 = spv.IAdd %44, %43 : i32
                    %46 = spv.AccessChain %8[%5, %45] : !spv.ptr<!spv.struct<!spv.array<15 x f32, stride=4> [0]>, StorageBuffer>, i32, i32
                    %47 = spv.Load "StorageBuffer" %46 : f32
                    %48 = spv.IAdd %16, %33 : i32
                    %49 = spv.IMul %43, %6 : i32
                    %50 = spv.IAdd %49, %48 : i32
                    %51 = spv.AccessChain %9[%5, %50] : !spv.ptr<!spv.struct<!spv.array<15 x f32, stride=4> [0]>, StorageBuffer>, i32, i32
                    %52 = spv.Load "StorageBuffer" %51 : f32
                    %53 = spv.IMul %42, %6 : i32
                    %54 = spv.IAdd %53, %48 : i32
                    %55 = spv.AccessChain %7[%5, %54] : !spv.ptr<!spv.struct<!spv.array<25 x f32, stride=4> [0]>, StorageBuffer>, i32, i32
                    %56 = spv.Load "StorageBuffer" %55 : f32
                    %57 = spv.FMul %47, %52 : f32
                    %58 = spv.FAdd %56, %57 : f32
                    spv.Store "StorageBuffer" %55, %58 : f32
                    %59 = spv.IAdd %40, %3 : i32
                    spv.Branch ^bb1(%59 : i32)
                  ^bb3:  // pred: ^bb1
                    spv._merge
                  }
                  spv.Branch ^bb2
                ^bb2:  // 2 preds: ^bb0, ^bb1
                  spv._merge
                }
                %39 = spv.IAdd %17, %0 : i32
                spv.Branch ^bb1(%39 : i32)
              ^bb3:  // pred: ^bb1
                spv._merge
              }
              spv.Return
            }
            spv.func @main_ex_dispatch_1_dispatch_0() "None" attributes {vkspv.workgroup_count_from_result_shape = 1 : i32} {
              %0 = spv.constant 0.000000e+00 : f32
              %1 = spv.constant 25 : i32
              %2 = spv.constant 32 : i32
              %3 = spv.constant 0 : i32
              %4 = spv.constant 5 : i32
              %5 = spv._address_of @__resource_var_0_2__ : !spv.ptr<!spv.struct<!spv.array<25 x f32, stride=4> [0]>, StorageBuffer>
              %6 = spv._address_of @__builtin_var_WorkgroupId__ : !spv.ptr<vector<3xi32>, Input>
              %7 = spv.Load "Input" %6 : vector<3xi32>
              %8 = spv.CompositeExtract %7[0 : i32] : vector<3xi32>
              %9 = spv._address_of @__builtin_var_LocalInvocationId__ : !spv.ptr<vector<3xi32>, Input>
              %10 = spv.Load "Input" %9 : vector<3xi32>
              %11 = spv.CompositeExtract %10[0 : i32] : vector<3xi32>
              %12 = spv.IMul %8, %2 : i32
              %13 = spv.IAdd %12, %11 : i32
              %14 = spv.SLessThan %13, %1 : i32
              spv.selection {
                spv.BranchConditional %14, ^bb1, ^bb2
              ^bb1:  // pred: ^bb0
                %15 = spv.SDiv %13, %4 : i32
                %16 = spv.GLSL.SAbs %13 : i32
                %17 = spv.GLSL.SAbs %4 : i32
                %18 = spv.UMod %16, %17 : i32
                %19 = spv.IEqual %13, %16 : i32
                %20 = spv.SNegate %18 : i32
                %21 = spv.Select %19, %18, %20 : i1, i32
                %22 = spv.IMul %15, %4 : i32
                %23 = spv.IAdd %22, %21 : i32
                %24 = spv.AccessChain %5[%3, %23] : !spv.ptr<!spv.struct<!spv.array<25 x f32, stride=4> [0]>, StorageBuffer>, i32, i32
                spv.Store "StorageBuffer" %24, %0 : f32
                spv.Branch ^bb2
              ^bb2:  // 2 preds: ^bb0, ^bb1
                spv._merge
              }
              spv.Return
            }
            spv.EntryPoint "GLCompute" @main_ex_dispatch_1_dispatch_1, @__builtin_var_WorkgroupId__, @__builtin_var_LocalInvocationId__
            spv.ExecutionMode @main_ex_dispatch_1_dispatch_1 "LocalSize", 8, 8, 1
            spv.EntryPoint "GLCompute" @main_ex_dispatch_1_dispatch_0, @__builtin_var_WorkgroupId__, @__builtin_var_LocalInvocationId__
            spv.ExecutionMode @main_ex_dispatch_1_dispatch_0 "LocalSize", 32, 1, 1
          }
          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"
          }
        }
      }
    }

  6. benvanik commented on Jul 21, 2020

    @benvanik
    CollaboratorAuthor

    Good question - that flow is a little messed up right now. I'll take a peek and see if any solution sticks out and otherwise we may need to rework things a bit (as they wouldn't be compatible with linking as-is anyway).

  7. ScottTodd commented on Jul 21, 2020

    @ScottTodd
    Member

    otherwise we may need to rework things a bit (as they wouldn't be compatible with linking as-is anyway)

    That was my concern too. I could hack around the Vulkan-SPIRV lowering by keeping an ordinal index on hal.command_buffer.dispatch or I could insert additional hal.executable.entry_point ops, but I want to make sure that whatever changes here helps with the linking changes that we want to come next.

    This

    The SPIR-V CodeGen pipeline incorporates the pass to split
    kernels if there are multiple Linalg ops. If multiple kernels
    are created, a special attribute spv.entry_point_schedule,
    is attached to the module to indicate the scheduling of entry
    points. Then the Vulkan backend queries this attribute and
    dispatches entry points as scheduled.

    seems like special SPIR-V logic that could be replaced with core hal.executable concepts? Or is it complementary?

  8. ScottTodd commented on Jul 22, 2020

    @ScottTodd
    Member

    Chatted with @antiagainst and learned some more about how the Vulkan-SPIRV lowering is working:

    1. iree-hal-materialize-interfaces goes from flow.executable ops to hal.executable ops. As part of that it creates hal.executable.entry_point ops.

    2. iree-codegen-split-dispatch-function later runs on each module, optionally splitting each workload up further depending on "GPU lowering dependencies". It also adds vkspv.entry_point_schedule attributes for use later.

    3. SPIRVLowerABIAttributes adds spv.EntryPoint ops

    4. ConvertFlowToHALPass pulls the SPIRV and hal ops together and has a mismatch between hal.executable.entry_point ops and spv.EntryPoint ops. It uses the vkspv.entry_point_schedule attribute to reconcile.

    Letting backends split up work as needed like in (2) makes sense, but at some point they should create corresponding hal.executable.entry_point ops (and update existing ones as needed?). I could try creating new entry point ops in ConvertFlowToHALPass, but would that be too late? Should there be a hook earlier in the lowerings that lets HAL target backends manipulate entry points / work granularity?

  9. ScottTodd commented on Jul 22, 2020

    @ScottTodd
    Member

    Also important: the VulkanSPIRVTarget lowering inserts execution barriers between its split dispatch ops. This ordering relationship between calling entry points should be preserved after any linking (if it isn't naturally).

  10. ScottTodd commented on Jul 28, 2020

    @ScottTodd
    Member

    After some more discussion with @benvanik , another refactoring that may help here would be to move hal.executable.entry_point ops into hal.executable.target modules and defer their creation from IREE::HAL::MaterializeInterfacesPass to IREE::HAL::TranslateExecutablesPass. By that point, each target backend should be ready to define its entry points, even after lowering through other dialects and splitting dispatches as needed.

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

Metadata

Metadata

Assignees

Labels

compiler/dialectsenhancement ➕good first issue 🌱

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions