Skip to content

[Stream] A scalar flow.tensor.load costs two device copies since stream.async.transfer has no source range #25280

Open

Description

@hamidelmaazouz

What happened?

Hi all, I noticed this while tracing how a scalar read from a device tensor is scheduled, and it looks like a small gap in stream.async.transfer more than a bug.

Reading one element of a tensor back to the host goes through a transient copy before it reaches staging. For the module below, the scheduled stream IR copies 4 bytes from the imported tensor into a 64-byte transient, copies them again into a 4-byte staging resource and then loads from staging:

%result, %result_timepoint = stream.resource.alloca uninitialized on(#hal.device.affinity<@__device_0>) : !stream.resource<staging>{%c4} => !stream.timepoint
%result_0, %result_timepoint_1 = stream.resource.alloca uninitialized on(#hal.device.affinity<@__device_0>) : !stream.resource<transient>{%c64} => !stream.timepoint
...
%2 = stream.cmd.execute ... with(%0 as %arg1: !stream.resource<external>{%c256}, %result as %arg2: !stream.resource<staging>{%c4}, %result_0 as %arg3: !stream.resource<transient>{%c64}) {
  stream.cmd.copy %arg1[%c0], %arg3[%c0], %c4 : !stream.resource<external>{%c256} -> !stream.resource<transient>{%c64}
  stream.cmd.copy %arg3[%c0], %arg2[%c0], %c4 : !stream.resource<transient>{%c64} -> !stream.resource<staging>{%c4}
  stream.cmd.flush %arg2[%c0 for %c4] : !stream.resource<staging>{%c4}
} => !stream.timepoint
%3 = stream.resource.dealloca ... %result_0 : !stream.resource<transient>{%c64} => !stream.timepoint
%4 = stream.timepoint.await %3 => %result : !stream.resource<staging>{%c4}
%5 = stream.resource.load %4[%c0] : !stream.resource<staging>{%c4} -> f32

A single copy from the tensor at the element offset into staging would do, with no transient allocation and no extra queue alloca and dealloca at the HAL level.

The pattern comes from ConvertTensorLoadOp in Conversion/FlowToStream/Patterns.cpp, which lowers flow.tensor.load to stream.async.slice, then stream.async.transfer to staging, then stream.async.load. stream.async.transfer copies its whole source and has no way to name a sub-range, so the slice has to materialise on its own. ElideAsyncCopies cannot help, since areSliceUsesSupported accepts only stream.async.copy and stream.async.dispatch as consumers of a slice, and the transfer itself changes lifetime. -debug-only=iree-stream-elide-async-copies reports both:

isSafeToElideSliceOp:
  - slice consumers not supported; cannot elide
isSafeToElideTransferOp:
  - transfer changes lifetime; cannot elide

ScheduleAllocation then lowers the slice and the transfer to one stream.cmd.copy each.

#24740 recently added a fold that rewrites clone, transfer, load into slice, transfer, load, so more reads now take this exact path. It already narrows the transfer to the element, and the remaining slice is what this issue is about.

A possible direction is an optional source range on stream.async.transfer, source_offset and source_end in the style of stream.async.copy. With it, a slice feeding a transfer folds into the range, either in ElideAsyncCopies or as a canonicalisation next to the #24740 fold in StreamOpFolders.cpp, and applyAsyncTransferOp in ScheduleAllocation.cpp applies the offset on the copy it already emits. ConvertTensorLoadOp could also emit the ranged transfer directly. The new storage policy from #25143 would need a rule for a ranged transfer under reuse, and keeping the fold to copy seems the simplest start. I am happy to send a PR along these lines if the direction sounds right.

Steps to reproduce your issue

extract.mlir:

func.func @extract(%t: tensor<8x8xf32>) -> f32 {
  %c0 = arith.constant 0 : index
  %e = tensor.extract %t[%c0, %c0] : tensor<8x8xf32>
  return %e : f32
}
iree-compile extract.mlir -o stream.mlir --compile-to=stream \
  --iree-hal-target-device=local --iree-hal-local-target-device-backends=llvm-cpu \
  --mlir-disable-threading --debug-only=iree-stream-elide-async-copies

The stream.cmd.execute in @extract holds two stream.cmd.copy ops. With --compile-to=hal the HAL IR shows two hal.device.queue.alloca calls, one of 4 bytes and one of 64.

Expected: one copy from the source at the element offset into staging, and no transient.

What component(s) does this issue relate to?

Compiler

Version information

Reproduced with a build of v3.13.0 (89ece87) on macOS arm64. ConvertTensorLoadOp, areSliceUsesSupported and applyAsyncTransferOp are unchanged on main at 5332c68, and stream.async.transfer there has gained the storage policy but no source range.

Additional context

#16647 asked for the same kind of elision for slices feeding dispatches, which stream.async.dispatch now supports as a slice consumer. #24930 and #25192 touch the same op for the multi-device zero-copy path but do not cover a sub-range of the source.

Thanks for taking a look!

Activity

  1. github-actions commented on Nov 15, 2026

    @github-actions

    Hello @hamidelmaazouz 👋

    Thank you for submitting an issue to IREE! It looks like it's your first one.

    The volume of issues is huge, and triage times may vary. If you need quicker assistance on the issue, consider posting it on IREE Discord and providing short context there.
    Feel free to identify the most frequent contributors to the corresponding part of the codebase via Git history/CODEOWNERS and (gently!) tag them below.


    Action required: acknowledge IREE project policies

    IREE is a Linux Foundation project. All participants are expected to follow the LF Projects Code of Conduct.

    Please note that all contributions to IREE, including GitHub issues, must follow our IREE AI Tool Use Policy. This means:

    • Issue reporters must fully understand the primary issue and the impact of the problem.
      For bug reports, specifics of the use-case, workflow and/or crash scenario usually suffice.
      For improvement suggestions, this usually implies deeper understanding of suboptimal code, IR design choices, assumed performance tradeoffs, etc.
    • Contributors must write the summary of the issue themselves, so that the exact impact and scenario is clear to other participants.
    • Issue authors are expected to review and understand all LLM/AI-assisted segments of the report, if any. E.g.: CLI commands and IR reproducers, model/framework details, lowering pipeline details, assumptions about problematic behaviors of IREE components.
    • Substantial use of LLM/generative AI tools must be disclaimed in the header or footer of the issue description.

    Before the issue can be triaged, we kindly ask you to reply to this message and confirm that you understand and accept the proposed policy. As required, please adjust your initial issue description.


    If you have any questions, feel free to leave a comment here, or ask away on IREE Discord.

    Thank you,
    The IREE Community

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions