Skip to content

Adding hal.device.memoize op. - #17938

Merged
benvanik merged 1 commit into
mainfrom
users/benvanik/device-memoize
Jul 30, 2024
Merged

benvanik merged 1 commit into
mainfrom
users/benvanik/device-memoize

Conversation

@benvanik

@benvanik benvanik commented Jul 18, 2024 •

Copy link
Copy Markdown
Collaborator

This is an inline nested region that returns HAL resources for the given device and affinity pair. The region may only implicitly capture constants and global values in order for it to be outlined for initialization, though in the future we may extend this to support dynamic values by adding an LRU cache.

To start all memoize regions are inlined after conversion. This optional inlining will also serve as a useful debug feature for isolating memoization related issues. The initial outlining and proper memoization pass is present but disabled by default until we can verify all targets properly support reusable command buffers.

Progress on #17875. Nothing is generating these yet.

Example:

util.global private @device = #hal.device.target<"local"> : !hal.device
util.global private @executable : !hal.executable
util.global private @buffer : !hal.buffer
util.func public @memoize_command_buffer() -> !hal.command_buffer {
  %device = util.global.load immutable @device : !hal.device
  %affinity = arith.constant -1 : i64
  %executable = util.global.load immutable @executable : !hal.executable
  %buffer = util.global.load immutable @buffer : !hal.buffer
  %result = hal.device.memoize<%device : !hal.device> affinity(%affinity) -> !hal.command_buffer {
    %cmd = hal.command_buffer.create device(%device : !hal.device) mode(None) categories("Transfer|Dispatch") : !hal.command_buffer
    %dispatch_ordinal = arith.constant 123 : index
    %offset = arith.constant 100 : index
    hal.command_buffer.dispatch.indirect<%cmd : !hal.command_buffer>
        target(%executable : !hal.executable)[%dispatch_ordinal]
        workgroups(%buffer : !hal.buffer)[%offset]
    hal.command_buffer.execution_barrier<%cmd : !hal.command_buffer>
        source(CommandIssue) target(CommandProcess) flags(None)
    hal.command_buffer.finalize<%cmd : !hal.command_buffer>
    hal.return %cmd : !hal.command_buffer
  }
  util.return %result : !hal.command_buffer
}

->

util.global private @device = #hal.device.target<"local"> : !hal.device
util.global private @executable : !hal.executable
util.global private @global_resource : !hal.buffer
util.func private @__memoize_command_buffer_memoize_apply(%arg0: !hal.device, %arg1: i64, %arg2: !hal.executable, %arg3: !hal.buffer) -> !hal.command_buffer {
  %cmd = hal.command_buffer.create device(%arg0 : !hal.device) mode(OneShot) categories("Transfer|Dispatch") : !hal.command_buffer
  %c123 = arith.constant 123 : index
  %c100 = arith.constant 100 : index
  hal.command_buffer.dispatch.indirect<%cmd : !hal.command_buffer> target(%arg2 : !hal.executable)[%c123] workgroups(%arg3 : !hal.buffer)[%c100]
  hal.command_buffer.execution_barrier<%cmd : !hal.command_buffer> source(CommandIssue) target(CommandProcess) flags("None")
  hal.command_buffer.finalize<%cmd : !hal.command_buffer>
  util.return %cmd : !hal.command_buffer
}
util.global private @__memoize_command_buffer_memoize_result_0_device : !hal.command_buffer
util.initializer {
  %device = util.global.load immutable @device : !hal.device
  %c-1_i64 = arith.constant -1 : i64
  %executable = util.global.load immutable @executable : !hal.executable
  %global_resource = util.global.load immutable @global_resource : !hal.buffer
  %0 = util.call @__memoize_command_buffer_memoize_apply(%device, %c-1_i64, %executable, %global_resource) : (!hal.device, i64, !hal.executable, !hal.buffer) -> !hal.command_buffer
  util.global.store %0, @__memoize_command_buffer_memoize_result_0_device : !hal.command_buffer
  util.return
}
util.func private @__memoize_command_buffer_memoize_lookup(%arg0: !hal.device, %arg1: i64) -> !hal.command_buffer {
  %device = util.global.load @device : !hal.device
  %0 = util.cmp.eq %arg0, %device : !hal.device
  %1 = scf.if %0 -> (!hal.command_buffer) {
    %__memoize_command_buffer_memoize_result_0_device = util.global.load @__memoize_command_buffer_memoize_result_0_device : !hal.command_buffer
    scf.yield %__memoize_command_buffer_memoize_result_0_device : !hal.command_buffer
  } else {
    %2 = util.null : !hal.command_buffer
    scf.yield %2 : !hal.command_buffer
  }
  util.return %1 : !hal.command_buffer
}
util.func public @memoize_command_buffer() -> !hal.command_buffer {
  %device = util.global.load immutable @device : !hal.device
  %c-1_i64 = arith.constant -1 : i64
  %executable = util.global.load immutable @executable : !hal.executable
  %global_resource = util.global.load immutable @global_resource : !hal.buffer
  %0 = util.call @__memoize_command_buffer_memoize_lookup(%device, %c-1_i64) : (!hal.device, i64) -> !hal.command_buffer
  util.return %0 : !hal.command_buffer
}

@benvanik benvanik added the compiler/dialects Relating to the IREE compiler dialects (flow, hal, vm) label Jul 18, 2024
@benvanik
benvanik requested a review from ScottTodd July 18, 2024 00:54
@benvanik
benvanik force-pushed the users/benvanik/device-memoize branch from bbb7cbb to cb7c794 Compare July 18, 2024 03:53
@benvanik
benvanik force-pushed the users/benvanik/device-attrs branch from 50bdb46 to 2a9e223 Compare July 18, 2024 03:57
@benvanik
benvanik force-pushed the users/benvanik/device-memoize branch from cb7c794 to 7e1b6bb Compare July 18, 2024 03:57
@benvanik
benvanik marked this pull request as ready for review July 18, 2024 03:58
@benvanik
benvanik force-pushed the users/benvanik/device-attrs branch from 2a9e223 to 608d477 Compare July 18, 2024 20:22
@benvanik
benvanik force-pushed the users/benvanik/device-memoize branch from 7e1b6bb to d9039cf Compare July 18, 2024 20:22
@benvanik
benvanik force-pushed the users/benvanik/device-attrs branch from 608d477 to 827bf40 Compare July 20, 2024 23:03
@benvanik
benvanik force-pushed the users/benvanik/device-memoize branch from d9039cf to 9a9bff8 Compare July 20, 2024 23:03
@benvanik
benvanik force-pushed the users/benvanik/device-attrs branch from 827bf40 to 261150d Compare July 20, 2024 23:04
@benvanik
benvanik force-pushed the users/benvanik/device-memoize branch 2 times, most recently from 2eead66 to 1ecdcd5 Compare July 20, 2024 23:42
@benvanik
benvanik force-pushed the users/benvanik/device-attrs branch 2 times, most recently from c957a86 to 3383143 Compare July 22, 2024 17:14
@benvanik
benvanik requested review from hanhanW and kuhar as code owners July 22, 2024 17:14
@benvanik
benvanik force-pushed the users/benvanik/device-attrs branch from 3383143 to ddf26ba Compare July 22, 2024 17:16
@benvanik
benvanik force-pushed the users/benvanik/device-memoize branch from 1ecdcd5 to b745b8f Compare July 22, 2024 17:16
@benvanik
benvanik changed the base branch from users/benvanik/device-attrs to shared/multi-device July 22, 2024 17:18
@benvanik
benvanik force-pushed the users/benvanik/device-memoize branch from b745b8f to 35ef89f Compare July 22, 2024 17:21
@benvanik
benvanik force-pushed the shared/multi-device branch from ddf26ba to 1cf9b44 Compare July 22, 2024 17:21
Comment on lines +213 to +221
%result = hal.device.memoize<%device : !hal.device> affinity(%affinity) -> !hal.command_buffer {
// CHECK-NEXT: hal.command_buffer.create
%cmd = hal.command_buffer.create device(%device : !hal.device)
mode(OneShot)
categories("Transfer|Dispatch") : !hal.command_buffer
// CHECK-NEXT: hal.return
hal.return %cmd : !hal.command_buffer
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Progress on #17875. Nothing is generating these yet.

Are you planning on creating these hal.device.memoize ops with regions explicitly or by having some pass go through and wrap the currently generated IR in those ops after some analysis (e.g. looking for create -> ... -> finalize op sequences)?

On that issue you say

Now conversion will (where profitable) nest the command buffer creation in a hal.device.memoize op

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Explicitly only to start - we could start to use them elsewhere now that they exist, though, such as by replacing the MemoizeDeviceQueries pass by just finding device queries and fusing them into memoize regions or creating memoize regions. MaterializeResourceCaches may also be able to use memoize regions instead of having its special per-device logic but changing to just identify the lookup pseudo ops and replacing them with memoize ops that do the loading in-place (though that would rely on the deduplication being improved in the outlining pass here).

@benvanik
benvanik force-pushed the shared/multi-device branch from 1cf9b44 to 9d6cb08 Compare July 23, 2024 15:56
@benvanik
benvanik force-pushed the users/benvanik/device-memoize branch 2 times, most recently from 957816b to cbd13dc Compare July 23, 2024 22:59
@benvanik
benvanik force-pushed the shared/multi-device branch from 9d6cb08 to 609933f Compare July 23, 2024 22:59
@benvanik
benvanik force-pushed the users/benvanik/device-memoize branch from cbd13dc to 910ba67 Compare July 24, 2024 01:29
@benvanik
benvanik force-pushed the shared/multi-device branch 2 times, most recently from 363bcb6 to cacae7d Compare July 24, 2024 17:33
@benvanik
benvanik force-pushed the shared/multi-device branch 3 times, most recently from 9cd1a55 to b359de0 Compare July 30, 2024 00:44
@benvanik
benvanik requested a review from IanWood1 as a code owner July 30, 2024 00:44
@benvanik
benvanik force-pushed the users/benvanik/device-memoize branch from 910ba67 to 3dd77fc Compare July 30, 2024 00:45
@benvanik
benvanik force-pushed the shared/multi-device branch 3 times, most recently from 8f2f8f5 to f721fd0 Compare July 30, 2024 03:32
Base automatically changed from shared/multi-device to main July 30, 2024 04:24
@benvanik
benvanik force-pushed the users/benvanik/device-memoize branch from 3dd77fc to 2cef10f Compare July 30, 2024 04:25
@benvanik
benvanik enabled auto-merge (squash) July 30, 2024 04:26
@benvanik
benvanik disabled auto-merge July 30, 2024 15:32
This is an inline nested region that returns HAL resources for the
given device and affinity pair. The region may only implicitly capture
constants and global values in order for it to be outlined for
initialization, though in the future we may extend this to support
dynamic values by adding an LRU cache.

To start all memoize regions are inlined after conversion until the op
has a proper pass to perform the memoization. This will also serve as
a useful debug feature for isolating memoization related issues. The
initial outlining and proper memoization pass is present but disabled
by default until we can verify all targets properly support reusable
command buffers.
@benvanik
benvanik force-pushed the users/benvanik/device-memoize branch from 2cef10f to 5b997af Compare July 30, 2024 16:11
@benvanik
benvanik enabled auto-merge (squash) July 30, 2024 16:32
@benvanik
benvanik merged commit 7361340 into main Jul 30, 2024
@benvanik
benvanik deleted the users/benvanik/device-memoize branch July 30, 2024 16:39
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