[VM] Fix ref ownership in branch remaps (#24781) Branch remaps selected MOVE using `isLastRealValueUse`, whose result accounts for uses across all successors. A use on one successor therefore prevented ownership transfer on another mutually exclusive successor, leaving an extra owning reference on the taken path. Use target-block liveness to select MOVE independently for each successor. If one occurrence of a repeated operand is coalesced with its destination, prevent the other remaps from using MOVE; otherwise a MOVE would clear the coalesced register. Add register allocation tests for both cases and an LLVM CPU end-to-end regression that executes both selector paths with indirect command buffers disabled. Fixes #24753 Signed-off-by: zhangpan <98025960+zhangpan2001@users.noreply.github.com> Co-authored-by: OpenAI <noreply@openai.com>
diff --git a/compiler/src/iree/compiler/Dialect/VM/Analysis/RegisterAllocation.cpp b/compiler/src/iree/compiler/Dialect/VM/Analysis/RegisterAllocation.cpp index c5d4f20..83810ba 100644 --- a/compiler/src/iree/compiler/Dialect/VM/Analysis/RegisterAllocation.cpp +++ b/compiler/src/iree/compiler/Dialect/VM/Analysis/RegisterAllocation.cpp
@@ -52,50 +52,21 @@ SmallVector<std::string, 8> operandRegStrs; operandRegStrs.reserve(op.getNumOperands()); - // For branch operations, use remapSuccessorRegisters to get the - // correct MOVE bits for branch arguments. - // Build a map from operand index to the correct register with MOVE bit. + // Use the same per-successor MOVE bits as branch register remapping, + // including operands coalesced with their destination registers. llvm::DenseMap<unsigned, Register> branchOperandRegs; if (auto branchOp = dyn_cast<BranchOpInterface>(&op)) { for (unsigned i = 0; i < op.getNumSuccessors(); ++i) { - auto srcDstRegs = - registerAllocation.remapSuccessorRegisters(&op, i); auto succOperands = branchOp.getSuccessorOperands(i).getForwardedOperands(); if (succOperands.empty()) { continue; } unsigned baseIdx = succOperands.getBeginOperandIndex(); - // remapSuccessorRegisters only returns pairs where src != dst. - // For display, we need ALL operands with correct MOVE bits. - // Re-compute with the same logic as remapSuccessorRegisters. - (void)srcDstRegs; // Unused - we recompute for display. - - // Build lastOccurrence map: only the last occurrence of a ref value - // in this successor's operand list can get MOVE (matches - // remapSuccessorRegisters logic). - llvm::DenseMap<Value, unsigned> lastOccurrence; - for (auto [idx, operand] : llvm::enumerate(succOperands)) { - if (isa<IREE::VM::RefType>(operand.getType())) { - lastOccurrence[operand] = idx; - } - } - - for (auto [localIdx, operand] : llvm::enumerate(succOperands)) { - unsigned globalIdx = baseIdx + localIdx; - auto srcReg = registerAllocation.mapToRegister(operand); - // Apply same MOVE logic as remapSuccessorRegisters. - if (srcReg.isRef()) { - bool isLastOccurrence = - (lastOccurrence.lookup(operand) == localIdx); - bool isLastUse = - registerAllocation.liveness_.isLastRealValueUse( - operand, &op, globalIdx); - if (isLastOccurrence && isLastUse) { - srcReg.setMove(true); - } - } - branchOperandRegs[globalIdx] = srcReg; + auto operandRegs = + registerAllocation.getSuccessorOperandRegisters(&op, i); + for (auto [localIdx, reg] : llvm::enumerate(operandRegs)) { + branchOperandRegs[baseIdx + localIdx] = reg; } } } @@ -759,106 +730,119 @@ } }; +// Computes registers and MOVE bits for all forwarded successor operands, +// including operands coalesced with their destination registers. +// +// For a remap between distinct registers, MOVE transfers ownership and clears +// the source register. Without MOVE, the runtime retains a reference for the +// destination. +// +// A ref operand receives MOVE only when all four conditions hold: +// 1. It is the last occurrence of this value in the successor's operand list. +// 2. It is the last real use across all successors, or the remap is +// non-coalesced and the original value is not live-in to this target block. +// 3. The branch op supports MOVE on this operand via RefMoveInterface. +// 4. No duplicate occurrence is coalesced with its destination block argument; +// moving another occurrence would clear that coalesced register. +// +// These conditions also apply to back-edges. When they hold for a remap to a +// loop header, MOVE transfers ownership to the next iteration's block argument. +// The edge being a back-edge does not itself require retaining the source ref. +SmallVector<Register, 8> +RegisterAllocation::getSuccessorOperandRegisters(Operation *branchOp, + unsigned successorIndex) { + auto targetOperands = cast<BranchOpInterface>(branchOp) + .getSuccessorOperands(successorIndex) + .getForwardedOperands(); + if (targetOperands.empty()) { + return {}; + } + Block *targetBlock = branchOp->getSuccessor(successorIndex); + unsigned baseOperandIndex = targetOperands.getBeginOperandIndex(); + + struct RefOperandInfo { + unsigned lastIndex = 0; + unsigned count = 0; + bool coalesced = false; + }; + llvm::DenseMap<Value, RefOperandInfo> refOperands; + SmallVector<Register, 8> operandRegisters; + operandRegisters.reserve(targetOperands.size()); + for (auto [idx, operand] : llvm::enumerate(targetOperands)) { + auto srcReg = mapToRegister(operand); + operandRegisters.push_back(srcReg); + if (!srcReg.isRef()) { + continue; + } + auto &info = refOperands[operand]; + info.lastIndex = idx; + ++info.count; + info.coalesced |= srcReg == mapToRegister(targetBlock->getArgument(idx)); + } + + auto moveOp = dyn_cast<IREE::VM::RefMoveInterface>(branchOp); + for (auto [idx, operand] : llvm::enumerate(targetOperands)) { + auto &srcReg = operandRegisters[idx]; + if (!srcReg.isRef()) { + continue; + } + auto info = refOperands.lookup(operand); + bool isLastOccurrence = info.lastIndex == idx; + unsigned globalOperandIndex = baseOperandIndex + idx; + bool isLastUseAcrossSuccessors = + liveness_.isLastRealValueUse(operand, branchOp, globalOperandIndex); + + // A ref may remain live on one successor while ownership transfers on + // another. A non-coalesced remap can MOVE when this target does not use + // the original value directly, even if another successor does. + auto dstReg = mapToRegister(targetBlock->getArgument(idx)); + bool transfersOnTargetEdge = + srcReg != dstReg && + !llvm::is_contained(liveness_.getBlockLiveIns(targetBlock), operand); + bool canTransferOwnership = + isLastUseAcrossSuccessors || transfersOnTargetEdge; + bool isMovable = moveOp && moveOp.isRefOperandMovable(globalOperandIndex); + + // Moving another occurrence would clear the coalesced destination register. + // A single coalesced operand emits no remap pair, so its MOVE bit only + // affects IR annotation. + bool hasCoalescedDuplicate = info.count > 1 && info.coalesced; + if (isLastOccurrence && canTransferOwnership && isMovable && + !hasCoalescedDuplicate) { + srcReg.setMove(true); + } + } + return operandRegisters; +} + SmallVector<std::pair<Register, Register>, 8> RegisterAllocation::remapSuccessorRegisters(Operation *op, int successorIndex) { - auto branchOp = cast<BranchOpInterface>(op); - auto *targetBlock = op->getSuccessor(successorIndex); - auto targetOperands = - branchOp.getSuccessorOperands(successorIndex).getForwardedOperands(); - - // Get the base operand index for this successor's operands in the branch op. - // This is needed to query isLastRealValueUse with the correct global index. - // Note: getBeginOperandIndex() asserts on empty ranges, so we handle that. - unsigned baseOperandIndex = - targetOperands.empty() ? 0 : targetOperands.getBeginOperandIndex(); - - return remapSuccessorRegisters(op, targetBlock, targetOperands, - baseOperandIndex); + auto operandRegisters = getSuccessorOperandRegisters(op, successorIndex); + return remapSuccessorRegisters(op, op->getSuccessor(successorIndex), + operandRegisters); } SmallVector<std::pair<Register, Register>, 8> RegisterAllocation::remapSuccessorRegisters(Location loc, Block *targetBlock, OperandRange targetOperands) { - // Legacy overload - called without branch op context, so no MOVE bits. - // This path is only used for testing/annotation where we don't have the op. + // Without branch op context, leave MOVE bits unset. + auto operandRegisters = llvm::map_to_vector<8>( + targetOperands, [&](Value operand) { return mapToRegister(operand); }); return remapSuccessorRegisters(/*branchOp=*/nullptr, targetBlock, - targetOperands, /*baseOperandIndex=*/0); + operandRegisters); } -// Computes register remapping for branch operands from source block to target. -// -// This function handles the MOVE bit logic for ref operands: -// - MOVE transfers ownership (runtime nulls source after copy) -// - No MOVE retains (runtime increments refcount of destination) -// -// MOVE is set when all three conditions are true: -// 1. This is the last occurrence of the ref value in this successor's list -// (handles same ref appearing multiple times like `^bb(%ref, %ref)`) -// 2. The ref is not live after this branch (checked via isLastRealValueUse) -// 3. The branch op supports MOVE on this operand (RefMoveInterface) -// -// MOVE on back-edges (loops) is CORRECT: -// - Ownership transfers to the next iteration's block argument -// - NOT MOVE would leak: source retains AND destination retains = refcount++ -// - At loop exit, source register would be non-null causing spurious release -// -// Returns a list of (src, dst) register pairs that need to be remapped. -// Pairs where src == dst (coalesced) are omitted. SmallVector<std::pair<Register, Register>, 8> -RegisterAllocation::remapSuccessorRegisters(Operation *branchOp, - Block *targetBlock, - OperandRange targetOperands, - unsigned baseOperandIndex) { +RegisterAllocation::remapSuccessorRegisters( + Operation *branchOp, Block *targetBlock, + ArrayRef<Register> operandRegisters) { // Compute the initial directed graph of register movements. // This may contain cycles ([reg 0->1], [reg 1->0], ...) that would not be - // possible to evaluate as a direct remapping. + // possible to evaluate as a direct remapping. Coalesced operands need no + // remap. SmallVector<std::pair<Register, Register>, 8> srcDstRegs; - - // Build last-occurrence map for ref operands in this successor's list. - // Only the last occurrence of a value can get MOVE (for multi-use cases like - // passing same ref to both branches of cond_br). - llvm::DenseMap<Value, unsigned> lastOccurrence; - for (auto [idx, operand] : llvm::enumerate(targetOperands)) { - if (isa<IREE::VM::RefType>(operand.getType())) { - lastOccurrence[operand] = idx; - } - } - - for (auto [idx, operand] : llvm::enumerate(targetOperands)) { - auto srcReg = mapToRegister(operand); - BlockArgument targetArg = targetBlock->getArgument(idx); - auto dstReg = mapToRegister(targetArg); - - // Apply MOVE bit logic for ref registers. - if (branchOp && srcReg.isRef()) { - // Check if this is the last occurrence of this value in the operand list. - bool isLastOccurrence = (lastOccurrence.lookup(operand) == idx); - - // Compute global operand index for liveness query. - unsigned globalOperandIndex = baseOperandIndex + idx; - - // Check if this is the last real use of the value (not live after branch, - // not used elsewhere in the function after this point). - bool isLastUse = - liveness_.isLastRealValueUse(operand, branchOp, globalOperandIndex); - - // Check if the branch op supports MOVE on this operand. - bool isMovable = false; - if (auto moveOp = dyn_cast<IREE::VM::RefMoveInterface>(branchOp)) { - isMovable = moveOp.isRefOperandMovable(globalOperandIndex); - } - - // Set MOVE if: - // 1. This is the last occurrence in this successor's operand list - // 2. This is the last real use of the value (not live after branch) - // 3. The branch op supports MOVE on this operand - // Note: MOVE is correct even on back-edges. The ref ownership transfers - // to the destination block arg, which is correct for loop-carried values. - if (isLastOccurrence && isLastUse && isMovable) { - srcReg.setMove(true); - } - } - + for (auto [idx, srcReg] : llvm::enumerate(operandRegisters)) { + auto dstReg = mapToRegister(targetBlock->getArgument(idx)); if (srcReg != dstReg) { srcDstRegs.push_back({srcReg, dstReg}); }
diff --git a/compiler/src/iree/compiler/Dialect/VM/Analysis/RegisterAllocation.h b/compiler/src/iree/compiler/Dialect/VM/Analysis/RegisterAllocation.h index 2ebabde..eadd8d2 100644 --- a/compiler/src/iree/compiler/Dialect/VM/Analysis/RegisterAllocation.h +++ b/compiler/src/iree/compiler/Dialect/VM/Analysis/RegisterAllocation.h
@@ -9,6 +9,7 @@ #include "iree/compiler/Dialect/VM/Analysis/ValueLiveness.h" #include "iree/compiler/Dialect/VM/IR/VMOps.h" +#include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/DenseMap.h" #include "llvm/ADT/DenseMapInfo.h" #include "llvm/ADT/DenseSet.h" @@ -208,11 +209,17 @@ OperandRange targetOperands); private: - // Internal implementation that handles MOVE bit computation. + // Returns registers for all forwarded successor operands, including those + // coalesced with their destinations. Ref registers carry edge-specific MOVE + // bits shared by IR annotation and branch register remapping. + SmallVector<Register, 8> + getSuccessorOperandRegisters(Operation *branchOp, unsigned successorIndex); + + // Orders register remaps and resolves cycles using precomputed source + // registers and their MOVE bits. SmallVector<std::pair<Register, Register>, 8> remapSuccessorRegisters(Operation *branchOp, Block *targetBlock, - OperandRange targetOperands, - unsigned baseOperandIndex); + ArrayRef<Register> operandRegisters); public: // Returns true if the given discard op can be fully elided because all its
diff --git a/compiler/src/iree/compiler/Dialect/VM/Analysis/test/register_allocation_refs.mlir b/compiler/src/iree/compiler/Dialect/VM/Analysis/test/register_allocation_refs.mlir index 872307a..44c11a0 100644 --- a/compiler/src/iree/compiler/Dialect/VM/Analysis/test/register_allocation_refs.mlir +++ b/compiler/src/iree/compiler/Dialect/VM/Analysis/test/register_allocation_refs.mlir
@@ -216,6 +216,24 @@ vm.return } + // CHECK-LABEL: @diamond_forward_or_use + // A ref can transfer on one edge while remaining live on another. The + // non-coalesced forwarding edge must MOVE to avoid retaining a stale source. + vm.func @diamond_forward_or_use(%cond : i32, %buf : !vm.buffer) { + // CHECK: vm.cond_br + // CHECK-SAME: operand_registers = ["i0", "R0"] + // CHECK-SAME: remap_registers = {{\[}}{{\[}}"R0->r1"{{\]}}, {{\[}}{{\]}}{{\]}} + vm.cond_br %cond, ^forward(%buf : !vm.buffer), ^use_original + ^forward(%forwarded : !vm.buffer): + vm.call @consume_buffer(%forwarded) : (!vm.buffer) -> () + vm.discard.refs %forwarded : !vm.buffer + vm.return + ^use_original: + vm.call @consume_buffer(%buf) : (!vm.buffer) -> () + vm.discard.refs %buf : !vm.buffer + vm.return + } + // CHECK-LABEL: @diamond_asymmetric_call // Diamond where one path calls with ref, other doesn't use it. vm.func @diamond_asymmetric_call(%cond : i32, %buf : !vm.buffer) { @@ -683,6 +701,7 @@ vm.module @module_same_target { vm.import private @consume_buffer(%buf : !vm.buffer) + vm.import private @use_two_buffers(%a : !vm.buffer, %b : !vm.buffer) // CHECK-LABEL: @same_ref_both_edges_same_target // cond_br where both edges go to same block with same ref. @@ -701,6 +720,23 @@ vm.discard.refs %b : !vm.buffer vm.return } + + // CHECK-LABEL: @same_ref_multiple_args_same_target + // When one destination coalesces with the source register, another + // destination must retain instead of moving and clearing that register. + vm.func @same_ref_multiple_args_same_target(%buf : !vm.buffer) { + // CHECK: vm.br + // CHECK-SAME: operand_registers = ["r0", "r0"] + // CHECK-SAME: remap_registers = {{\[}}{{\[}}"r0->r1"{{\]}}{{\]}} + vm.br ^merge(%buf, %buf : !vm.buffer, !vm.buffer) + ^merge(%a : !vm.buffer, %b : !vm.buffer): + // CHECK: vm.call @use_two_buffers + // CHECK-SAME: operand_registers = ["R0", "R1"] + vm.call @use_two_buffers(%a, %b) : (!vm.buffer, !vm.buffer) -> () + // CHECK: vm.discard.refs + vm.discard.refs %a, %b : !vm.buffer, !vm.buffer + vm.return + } } // ----- @@ -802,8 +838,8 @@ vm.func @br_table_ref_some_cases(%idx: i32, %ref: !vm.buffer) { // CHECK: vm.br_table // Ref forwarded to default and case 0, but NOT case 1. - // Case 0 gets MOVE since case 1 doesn't use the ref. - // CHECK: operand_registers = ["i0", "r0", "R0"] + // Each forwarding edge gets MOVE since only one edge executes. + // CHECK: operand_registers = ["i0", "R0", "R0"] vm.br_table %idx { default: ^bb_default(%ref : !vm.buffer), 0: ^bb0(%ref : !vm.buffer),
diff --git a/tests/e2e/regression/BUILD.bazel b/tests/e2e/regression/BUILD.bazel index 86659f7..79227fb 100644 --- a/tests/e2e/regression/BUILD.bazel +++ b/tests/e2e/regression/BUILD.bazel
@@ -38,6 +38,7 @@ "trace_dispatch_tensors.mlir", "transients_test.mlir", "unused_args.mlir", + "vm_ref_branch_ownership.mlir", ], cfg = "//tests:lit.cfg.py", tags = [ @@ -48,6 +49,7 @@ "//tools:iree-compile", "//tools:iree-opt", "//tools:iree-run-mlir", + "//tools:iree-run-module", "@llvm-project//lld", "@llvm-project//llvm:FileCheck", ],
diff --git a/tests/e2e/regression/CMakeLists.txt b/tests/e2e/regression/CMakeLists.txt index 5d2dd17..9d6799c 100644 --- a/tests/e2e/regression/CMakeLists.txt +++ b/tests/e2e/regression/CMakeLists.txt
@@ -23,12 +23,14 @@ "trace_dispatch_tensors.mlir" "transients_test.mlir" "unused_args.mlir" + "vm_ref_branch_ownership.mlir" TOOLS ${IREE_LLD_TARGET} FileCheck iree-compile iree-opt iree-run-mlir + iree-run-module LABELS "driver=local-task" "hostonly"
diff --git a/tests/e2e/regression/vm_ref_branch_ownership.mlir b/tests/e2e/regression/vm_ref_branch_ownership.mlir new file mode 100644 index 0000000..8e86f5e --- /dev/null +++ b/tests/e2e/regression/vm_ref_branch_ownership.mlir
@@ -0,0 +1,139 @@ +// RUN: iree-compile %s \ +// RUN: --iree-hal-target-device=local \ +// RUN: --iree-hal-local-target-device-backends=llvm-cpu \ +// RUN: --iree-llvmcpu-target-cpu=generic \ +// RUN: --iree-hal-indirect-command-buffers=false \ +// RUN: -o %t.vmfb +// RUN: iree-run-module \ +// RUN: --device=local-sync \ +// RUN: --module=%t.vmfb \ +// RUN: --function=main \ +// RUN: --input=7xi32=0 \ +// RUN: --input=7xi32=0 \ +// RUN: --input=7xi32=0 \ +// RUN: --input=i32=0 \ +// RUN: --input=i32=0 \ +// RUN: --input=i1=00 \ +// RUN: --input=i32=2 \ +// RUN: --expected_output=7xi32=1,1,1,1,1,1,1 \ +// RUN: --expected_output=7xi32=0,0,0,0,0,0,0 \ +// RUN: --expected_output=7xi32=1,1,1,1,1,1,1 \ +// RUN: --expected_output=i32=0 \ +// RUN: --expected_output=i32=0 \ +// RUN: --expected_output=i1=00 +// RUN: iree-run-module \ +// RUN: --device=local-sync \ +// RUN: --module=%t.vmfb \ +// RUN: --function=main \ +// RUN: --input=7xi32=0 \ +// RUN: --input=7xi32=0 \ +// RUN: --input=7xi32=0 \ +// RUN: --input=i32=0 \ +// RUN: --input=i32=0 \ +// RUN: --input=i1=01 \ +// RUN: --input=i32=2 \ +// RUN: --expected_output=7xi32=1,1,1,1,1,1,1 \ +// RUN: --expected_output=7xi32=0,0,0,0,0,0,0 \ +// RUN: --expected_output=7xi32=1,1,1,1,1,1,1 \ +// RUN: --expected_output=i32=0 \ +// RUN: --expected_output=i32=0 \ +// RUN: --expected_output=i1=01 + +// Regression test for https://github.com/iree-org/iree/issues/24753. +// With indirect command buffers disabled, both selector paths must execute +// successfully and preserve every returned value. +module { + func.func public @main( + %arg0: tensor<7xi32>, %arg1: tensor<7xi32>, %arg2: tensor<7xi32>, + %arg3: tensor<i32>, %arg4: tensor<i32>, %arg5: tensor<i1>, + %arg6: tensor<i32>) + -> (tensor<7xi32>, tensor<7xi32>, tensor<7xi32>, tensor<i32>, + tensor<i32>, tensor<i1>) { + %false = stablehlo.constant dense<false> : tensor<i1> + %c1 = stablehlo.constant dense<1> : tensor<i32> + %c0 = stablehlo.constant dense<0> : tensor<i32> + %is_zero = stablehlo.compare EQ, %arg6, %c0, SIGNED + : (tensor<i32>, tensor<i32>) -> tensor<i1> + %case_index = stablehlo.convert %is_zero + : (tensor<i1>) -> tensor<i32> + %0:4 = "stablehlo.case"(%case_index) ({ + %is_one = stablehlo.compare EQ, %arg6, %c1, SIGNED + : (tensor<i32>, tensor<i32>) -> tensor<i1> + %nested_case_index = stablehlo.convert %is_one + : (tensor<i1>) -> tensor<i32> + %1:4 = "stablehlo.case"(%nested_case_index) ({ + %splat1_a = stablehlo.broadcast_in_dim %c1, dims = [] + : (tensor<i32>) -> tensor<7xi32> + %result0 = stablehlo.add %arg0, %splat1_a : tensor<7xi32> + %splat1_b = stablehlo.broadcast_in_dim %c1, dims = [] + : (tensor<i32>) -> tensor<7xi32> + %result2 = stablehlo.add %arg2, %splat1_b : tensor<7xi32> + %not_arg5 = stablehlo.not %arg5 : tensor<i1> + %inner_case_index = stablehlo.convert %not_arg5 + : (tensor<i1>) -> tensor<i32> + %result3 = "stablehlo.case"(%inner_case_index) ({ + %splat0 = stablehlo.broadcast_in_dim %c0, dims = [] + : (tensor<i32>) -> tensor<7xi32> + %positive = stablehlo.compare GT, %result0, %splat0, SIGNED + : (tensor<7xi32>, tensor<7xi32>) -> tensor<7xi1> + %any_positive = stablehlo.reduce(%positive init: %false) + applies stablehlo.or across dimensions = [0] + : (tensor<7xi1>, tensor<i1>) -> tensor<i1> + %sum_case_index = stablehlo.convert %any_positive + : (tensor<i1>) -> tensor<i32> + %selected = "stablehlo.case"(%sum_case_index) ({ + stablehlo.return %arg3 : tensor<i32> + }, { + %sum = stablehlo.reduce(%arg1 init: %c0) + applies stablehlo.add across dimensions = [0] + : (tensor<7xi32>, tensor<i32>) -> tensor<i32> + stablehlo.return %sum : tensor<i32> + }) : (tensor<i32>) -> tensor<i32> + stablehlo.return %selected : tensor<i32> + }, { + stablehlo.return %arg3 : tensor<i32> + }) : (tensor<i32>) -> tensor<i32> + stablehlo.return %result0, %result2, %result3, %c0 + : tensor<7xi32>, tensor<7xi32>, tensor<i32>, tensor<i32> + }, { + stablehlo.return %arg0, %arg2, %arg3, %arg4 + : tensor<7xi32>, tensor<7xi32>, tensor<i32>, tensor<i32> + }) : (tensor<i32>) -> + (tensor<7xi32>, tensor<7xi32>, tensor<i32>, tensor<i32>) + stablehlo.return %1#0, %1#1, %1#2, %1#3 + : tensor<7xi32>, tensor<7xi32>, tensor<i32>, tensor<i32> + }, { + %not_arg5 = stablehlo.not %arg5 : tensor<i1> + %inner_case_index = stablehlo.convert %not_arg5 + : (tensor<i1>) -> tensor<i32> + %result3 = "stablehlo.case"(%inner_case_index) ({ + %splat0 = stablehlo.broadcast_in_dim %c0, dims = [] + : (tensor<i32>) -> tensor<7xi32> + %positive = stablehlo.compare GT, %arg0, %splat0, SIGNED + : (tensor<7xi32>, tensor<7xi32>) -> tensor<7xi1> + %any_positive = stablehlo.reduce(%positive init: %false) + applies stablehlo.or across dimensions = [0] + : (tensor<7xi1>, tensor<i1>) -> tensor<i1> + %sum_case_index = stablehlo.convert %any_positive + : (tensor<i1>) -> tensor<i32> + %selected = "stablehlo.case"(%sum_case_index) ({ + stablehlo.return %arg3 : tensor<i32> + }, { + %sum = stablehlo.reduce(%arg1 init: %c0) + applies stablehlo.add across dimensions = [0] + : (tensor<7xi32>, tensor<i32>) -> tensor<i32> + stablehlo.return %sum : tensor<i32> + }) : (tensor<i32>) -> tensor<i32> + stablehlo.return %selected : tensor<i32> + }, { + stablehlo.return %arg3 : tensor<i32> + }) : (tensor<i32>) -> tensor<i32> + stablehlo.return %arg0, %arg2, %result3, %arg4 + : tensor<7xi32>, tensor<7xi32>, tensor<i32>, tensor<i32> + }) : (tensor<i32>) -> + (tensor<7xi32>, tensor<7xi32>, tensor<i32>, tensor<i32>) + return %0#0, %arg1, %0#1, %0#2, %0#3, %arg5 + : tensor<7xi32>, tensor<7xi32>, tensor<7xi32>, tensor<i32>, + tensor<i32>, tensor<i1> + } +}