Skip to content

Commit 653925b

Browse files
authored
Merge pull request #79476 from meg-gupta/lrfix
Fix loop rotate when header has instructions producing ownership results
2 parents 6e4f711 + 03038d2 commit 653925b

File tree

2 files changed

+44
-6
lines changed

2 files changed

+44
-6
lines changed

lib/SILOptimizer/LoopTransforms/LoopRotate.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ static llvm::cl::opt<int> LoopRotateSizeLimit("looprotate-size-limit",
4444
llvm::cl::init(20));
4545
static llvm::cl::opt<bool> RotateSingleBlockLoop("looprotate-single-block-loop",
4646
llvm::cl::init(false));
47+
static llvm::cl::opt<bool>
48+
LoopRotateInfiniteBudget("looprotate-infinite-budget",
49+
llvm::cl::init(false));
4750

4851
static bool rotateLoop(SILLoop *loop, DominanceInfo *domInfo,
4952
SILLoopInfo *loopInfo, bool rotateSingleBlockLoops,
@@ -118,9 +121,7 @@ canDuplicateOrMoveToPreheader(SILLoop *loop, SILBasicBlock *preheader,
118121
if (!inst->mayHaveSideEffects() && !inst->mayReadFromMemory() &&
119122
!isa<TermInst>(inst) &&
120123
!isa<AllocationInst>(inst) && /* not marked mayhavesideeffects */
121-
!isa<CopyValueInst>(inst) &&
122-
!isa<MoveValueInst>(inst) &&
123-
!isa<BeginBorrowInst>(inst) &&
124+
!hasOwnershipOperandsOrResults(inst) &&
124125
hasLoopInvariantOperands(inst, loop, invariants)) {
125126
moves.push_back(inst);
126127
invariants.insert(inst);
@@ -133,7 +134,7 @@ canDuplicateOrMoveToPreheader(SILLoop *loop, SILBasicBlock *preheader,
133134
cost += (int)instructionInlineCost(instRef);
134135
}
135136

136-
return cost < LoopRotateSizeLimit;
137+
return cost < LoopRotateSizeLimit || LoopRotateInfiniteBudget;
137138
}
138139

139140
static void mapOperands(SILInstruction *inst,

test/SILOptimizer/looprotate_nontrivial_ossa.sil

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
// RUN: %target-sil-opt -loop-rotate -update-borrowed-from -looprotate-single-block-loop=true %s | %FileCheck %s
1+
// RUN: %target-sil-opt -loop-rotate -update-borrowed-from -looprotate-single-block-loop=true -looprotate-infinite-budget %s | %FileCheck %s
22
sil_stage canonical
33

44
import Builtin
55
import Swift
66

77
class Klass {
8-
98
}
109

1110
struct BoxStruct {
@@ -244,3 +243,41 @@ bb8:
244243
bb9:
245244
unreachable
246245
}
246+
247+
// Ensure no verifier error
248+
249+
sil @foo : $@convention(thin) (@in_guaranteed UInt64, @in_guaranteed UInt64) -> (Bool, @error any Error)
250+
251+
sil [ossa] @looprotate_ownership_results : $@convention(thin) (Int32, @guaranteed Klass) -> Int32 {
252+
bb0(%0 : $Int32, %1 : @guaranteed $Klass):
253+
%2 = struct_extract %0, #Int32._value
254+
%3 = integer_literal $Builtin.Int32, 0
255+
br bb1(%2, %3)
256+
257+
bb1(%5 : $Builtin.Int32, %6 : $Builtin.Int32):
258+
%7 = function_ref @foo : $@convention(thin) (@in_guaranteed UInt64, @in_guaranteed UInt64) -> (Bool, @error any Error)
259+
%8 = thin_to_thick_function %7 to $@noescape @callee_guaranteed (@in_guaranteed UInt64, @in_guaranteed UInt64) -> (Bool, @error any Error)
260+
%9 = convert_function %8 to $@noescape @callee_guaranteed @substituted <τ_0_0, τ_0_1> (@in_guaranteed τ_0_0, @in_guaranteed τ_0_1) -> (Bool, @error any Error) for <UInt64, UInt64>, forwarding: @owned
261+
destroy_value %9
262+
%11 = struct $Int32 (%6)
263+
%12 = builtin "cmp_eq_Word"(%6, %2) : $Builtin.Int1
264+
cond_br %12, bb3, bb2
265+
266+
bb2:
267+
%14 = integer_literal $Builtin.Int32, 1
268+
%15 = integer_literal $Builtin.Int1, -1
269+
%16 = builtin "sadd_with_overflow_Word"(%6, %14, %15) : $(Builtin.Int32, Builtin.Int1)
270+
%17 = tuple_extract %16, 0
271+
%18 = enum $Optional<Int32>, #Optional.some!enumelt, %11
272+
%19 = unchecked_enum_data %18, #Optional.some!enumelt
273+
%20 = struct_extract %19, #Int32._value
274+
%21 = integer_literal $Builtin.Int1, -1
275+
%22 = builtin "sadd_with_overflow_Word"(%5, %20, %21) : $(Builtin.Int32, Builtin.Int1)
276+
%23 = tuple_extract %22, 0
277+
br bb1(%23, %17)
278+
279+
bb3:
280+
%25 = struct $Int32 (%5)
281+
return %25
282+
}
283+

0 commit comments

Comments
 (0)