[Stream] Copy resources stored into multiple mutable globals before in-place updates (#25030)
Mutable globals are updated in place: once the copy on a
`util.global.load` is
elided, tied dispatches write directly into the global's resource. That
is
only correct if no other global holds the same resource. CSE breaks this
assumption when two mutable globals have the same splat initializer:
after
`CombineInitializersPass`, one `stream.tensor.splat` is stored into both
globals. `ElideAsyncCopiesPass` then removes the clones before the
in-place
updates ("clone source SSA value has one use"), so updates to one global
show
up in the other. `FoldGlobals` and `FuseGlobals` later merge the two
globals
outright.
`MaterializeCopyOnWritePass` now treats `util.global.store` into a
mutable
global as a mutating use. If the stored resource has other uses, it is
cloned
before the store. As with the other clones this pass inserts,
`ElideAsyncCopiesPass` removes the ones that are not needed. In the
bug's case,
one clone survives in the initializer and canonicalizes into a second
fill.
Stores into immutable globals are untouched, so CSE'd constants stay
shared.
Impact, measured on 19 programs with mutable globals (llvm-cpu, local):
- 15 compile to byte-identical output;
- the other 4 gain one initializer fill and no per-call copies;
- 3 of those 4 were previously miscompiled.
Details are in #25029.
Fixes #25029
Assisted-by: Claude Code (Anthropic)
Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
diff --git a/compiler/src/iree/compiler/Dialect/Stream/Transforms/MaterializeCopyOnWrite.cpp b/compiler/src/iree/compiler/Dialect/Stream/Transforms/MaterializeCopyOnWrite.cpp
index cb730ae..496df9e 100644
--- a/compiler/src/iree/compiler/Dialect/Stream/Transforms/MaterializeCopyOnWrite.cpp
+++ b/compiler/src/iree/compiler/Dialect/Stream/Transforms/MaterializeCopyOnWrite.cpp
@@ -142,12 +142,52 @@
return didChange;
}
+// Materializes a copy for a resource stored into a mutable global if required.
+// Mutable globals are updated in place after being loaded, so each must own
+// its storage: the stored value may be deemed unsafe for copy elision, which
+// would mean there's a risk that it gets stored into another mutable global
+// that would also get overwritten.
+// Returns true if a copy was materialized.
+static bool
+materializeGlobalStoreCOW(IREE::Util::GlobalStoreOpInterface storeOp) {
+ Value value = storeOp.getStoredGlobalValue();
+ auto resourceType = dyn_cast<IREE::Stream::ResourceType>(value.getType());
+ if (!resourceType) {
+ return false;
+ }
+ auto globalOp =
+ SymbolTable::lookupNearestSymbolFrom<IREE::Util::GlobalOpInterface>(
+ storeOp, storeOp.getGlobalAttr());
+ if (!globalOp || !globalOp.isGlobalMutable()) {
+ return false;
+ }
+ if (isSafeToElideCOW(value, resourceType)) {
+ return false;
+ }
+ IREE::Stream::AffinityAttr affinity;
+ if (auto affinityOp = dyn_cast_if_present<IREE::Stream::AffinityOpInterface>(
+ value.getDefiningOp())) {
+ affinity = affinityOp.getAffinityAttr();
+ }
+ OpBuilder builder(storeOp);
+ auto sizeAwareType = cast<IREE::Util::SizeAwareTypeInterface>(resourceType);
+ auto size = sizeAwareType.queryValueSize(storeOp.getLoc(), value, builder);
+ Value clone = IREE::Stream::AsyncCloneOp::create(
+ builder, storeOp.getLoc(), resourceType, value, size, size, affinity);
+ storeOp.setStoredGlobalValue(clone);
+ return true;
+}
+
// Materializes copies on writes within |region|.
// Returns true if any copy was required and materialized.
static bool materializeRegionCOW(Region ®ion) {
bool didChange = false;
for (auto &block : region.getBlocks()) {
for (auto &op : block) {
+ if (auto storeOp = dyn_cast<IREE::Util::GlobalStoreOpInterface>(op)) {
+ didChange |= materializeGlobalStoreCOW(storeOp);
+ continue;
+ }
if (!op.hasTrait<OpTrait::IREE::Stream::AsyncPhaseOp>()) {
continue;
}
diff --git a/compiler/src/iree/compiler/Dialect/Stream/Transforms/test/materialize_copy_on_write.mlir b/compiler/src/iree/compiler/Dialect/Stream/Transforms/test/materialize_copy_on_write.mlir
index a1509e3..3890866 100644
--- a/compiler/src/iree/compiler/Dialect/Stream/Transforms/test/materialize_copy_on_write.mlir
+++ b/compiler/src/iree/compiler/Dialect/Stream/Transforms/test/materialize_copy_on_write.mlir
@@ -189,3 +189,37 @@
^bb2(%bb2_0: !stream.resource<*>, %bb2_1: !stream.resource<*>):
util.return %bb2_0, %bb2_1 : !stream.resource<*>, !stream.resource<*>
}
+
+// -----
+
+// Tests that a resource stored into more than one mutable global is copied for
+// each store: mutable globals are updated in place, so each must own its
+// storage. Stores of a single-use value and stores into immutable globals are
+// left alone.
+
+util.global private mutable @mutableA : !stream.resource<variable>
+util.global private mutable @mutableB : !stream.resource<variable>
+util.global private mutable @mutableC : !stream.resource<variable>
+util.global private @immutable : !stream.resource<variable>
+
+// CHECK-LABEL: @storeSharedValueToGlobals
+util.func public @storeSharedValueToGlobals(%size: index) {
+ %c0_i32 = arith.constant 0 : i32
+ // CHECK: %[[SHARED:.+]] = stream.async.splat %c0_i32
+ %shared = stream.async.splat %c0_i32 : i32 -> !stream.resource<variable>{%size}
+ // CHECK: %[[CLONE_A:.+]] = stream.async.clone %[[SHARED]]
+ // CHECK: util.global.store %[[CLONE_A]], @mutableA
+ util.global.store %shared, @mutableA : !stream.resource<variable>
+ // CHECK: %[[CLONE_B:.+]] = stream.async.clone %[[SHARED]]
+ // CHECK: util.global.store %[[CLONE_B]], @mutableB
+ util.global.store %shared, @mutableB : !stream.resource<variable>
+ // CHECK-NOT: stream.async.clone
+ // CHECK: util.global.store %[[SHARED]], @immutable
+ util.global.store %shared, @immutable : !stream.resource<variable>
+ // CHECK: %[[UNIQUE:.+]] = stream.async.splat %c0_i32
+ %unique = stream.async.splat %c0_i32 : i32 -> !stream.resource<variable>{%size}
+ // CHECK-NOT: stream.async.clone
+ // CHECK: util.global.store %[[UNIQUE]], @mutableC
+ util.global.store %unique, @mutableC : !stream.resource<variable>
+ util.return
+}