Skip to content

Refactor semaphore use in async and sync public ABI functions. - #3118

Merged
ScottTodd merged 1 commit into
iree-org:mainfrom
ScottTodd:semaphore-abi-flip
Sep 11, 2020
Merged

ScottTodd merged 1 commit into
iree-org:mainfrom
ScottTodd:semaphore-abi-flip

Conversation

@ScottTodd

Copy link
Copy Markdown
Member

This makes async the "default", with sync wrapping async, instead of the other way around. Now we can push semaphores down further into the compiler (e.g. to remove hal.ex.submit_and_wait).

Progress on #1285


If the test is hard to read, here's a sample from simple.mlir's translation:

  • Note how the $sync function creates a semaphore to pass into the $async function and waits for it to be incremented from 0 to 1 before returning. The $async function still waits and signals, but could instead pass the semaphores into the raw function for a hal.submit, removing the need for hal.ex.submit_and_wait.

Before:

  func @abs$sync(%arg0: !hal.buffer_view) -> !hal.buffer_view attributes {iree.abi.stub, iree.module.export = "abs", iree.reflection = {f = "I6!B3!t6R6!B3!t6", fv = "1"}} {
    %buffer = hal.buffer_view.buffer %arg0 : !hal.buffer
    %0 = call @abs(%buffer) : (!hal.buffer) -> !hal.buffer
    %view = hal.buffer_view.create %0, shape = [], element_type = 16777248 : !hal.buffer_view
    return %view : !hal.buffer_view
  }
  func @abs$async(%arg0: !hal.semaphore, %arg1: index, %arg2: !hal.buffer_view, %arg3: !hal.semaphore, %arg4: index) -> !hal.buffer_view attributes {iree.module.export = "abs$async"} {
    %0 = hal.semaphore.await %arg0, min_value = %arg1 : i32
    hal.check_success %0, "semaphore wait failed"
    %1 = call @abs$sync(%arg2) : (!hal.buffer_view) -> !hal.buffer_view
    hal.semaphore.signal %arg3, value = %arg4
    return %1 : !hal.buffer_view
  }

After:

  func @abs$async(%arg0: !hal.semaphore, %arg1: index, %arg2: !hal.buffer_view, %arg3: !hal.semaphore, %arg4: index) -> !hal.buffer_view attributes {iree.module.export = "abs$async"} {
    %0 = hal.semaphore.await %arg0, min_value = %arg1 : i32
    hal.check_success %0, "semaphore wait failed"
    %buffer = hal.buffer_view.buffer %arg2 : !hal.buffer
    %1 = call @abs(%buffer) : (!hal.buffer) -> !hal.buffer
    %view = hal.buffer_view.create %1, shape = [], element_type = 16777248 : !hal.buffer_view
    hal.semaphore.signal %arg3, value = %arg4
    return %view : !hal.buffer_view
  }
  func @abs$sync(%arg0: !hal.buffer_view) -> !hal.buffer_view attributes {iree.abi.stub, iree.module.export = "abs", iree.reflection = {f = "I6!B3!t6R6!B3!t6", fv = "1"}} {
    %c0 = constant 0 : index
    %c1 = constant 1 : index
    %dev = hal.ex.shared_device : !hal.device
    %semaphore = hal.semaphore.create %dev, initial_value = %c0 : !hal.semaphore
    %0 = call @abs$async(%semaphore, %c0, %arg0, %semaphore, %c1) : (!hal.semaphore, index, !hal.buffer_view, !hal.semaphore, index) -> !hal.buffer_view
    %1 = hal.semaphore.await %semaphore, min_value = %c1 : i32
    hal.check_success %1, "semaphore wait failed"
    return %0 : !hal.buffer_view
  }

This makes async the "default", with sync wrapping async, instead of the other way around. Now we can push semaphores down further into the compiler (e.g. to remove hal.ex.submit_and_wait).
@ScottTodd ScottTodd added the compiler/dialects Relating to the IREE compiler dialects (flow, hal, vm) label Sep 11, 2020
@ScottTodd
ScottTodd requested a review from benvanik September 11, 2020 00:14
@google-cla google-cla Bot added the cla: yes label Sep 11, 2020
@ScottTodd
ScottTodd merged commit fa16b40 into iree-org:main Sep 11, 2020
@ScottTodd
ScottTodd deleted the semaphore-abi-flip branch September 11, 2020 15:07
@hanhanW hanhanW mentioned this pull request Sep 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

compiler/dialects Relating to the IREE compiler dialects (flow, hal, vm)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants