Skip to content

Commit 96d329e

Browse files
committed
Added a test for the movement of loops and loop cloning.
1 parent 18d67f9 commit 96d329e

File tree

2 files changed

+134
-1
lines changed

2 files changed

+134
-1
lines changed

test/TensorFlow/sese_loop_canonicalization.sil

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-sil-opt -tf-xla-cfg-canonicalize -tf-ensure-single-loop-exit -assume-parsing-unqualified-ownership-sil %s -o /dev/null | %FileCheck %s
1+
// RUN: %target-sil-opt -tf-dump-intermediates -tf-xla-cfg-canonicalize -tf-ensure-single-loop-exit -assume-parsing-unqualified-ownership-sil %s -o /dev/null | %FileCheck %s
22

33
import Builtin
44
import Swift
@@ -361,3 +361,88 @@ bb8:
361361
bb9(%18 : $Builtin.Int32):
362362
return %18 : $Builtin.Int32
363363
}
364+
365+
// The following example is similar to loopThatRequiresNodeCloning, where the
366+
// nodes that lead to the common post dominator of all exit nodes is also
367+
// reachable from nodes outside of the loop. The only difference in this example
368+
// is that some of these nodes also belong to a loop. Therefore, it is required
369+
// for us to update the LoopInfo in addition to cloning. The loop in question
370+
// consists of the single node bb3.
371+
372+
// CHECK-LABEL:--- XLA CFG Loops Before Canonicalize: $loopThatRequiresNodeAndLoopCloning
373+
//----The following loop will be cloned in the second loop.
374+
// CHECK:Loop at depth 1 containing: {{bb[0-9]+}}<header><latch><exiting>
375+
// CHECK:Loop at depth 1 containing: {{bb[0-9]+}}<header><exiting>,{{bb[0-9]+}}<exiting>,{{bb[0-9]+}}<latch>
376+
377+
// CHECK-LABEL:--- XLA CFG Loops After Canonicalize: $loopThatRequiresNodeAndLoopCloning
378+
// CHECK:Loop at depth 1 containing: [[L1HDR:bb[0-9]+]]<header><exiting>,[[L1LATCH:bb[0-9]+]]<latch>
379+
// CHECK:Loop at depth 1 containing: [[L2HDR:bb[0-9]+]]<header><exiting>,{{.*}},[[L2LATCH:bb[0-9]+]]<latch>
380+
//----Following is the clone of the first loop.
381+
// CHECK: Loop at depth 2 containing: [[L3HDR:bb[0-9]+]]<header><exiting>,[[L3LATCH:bb[0-9]+]]<latch>
382+
383+
// CHECK-LABEL:--- XLA CFG Canonicalize: $loopThatRequiresNodeAndLoopCloning
384+
// CHECK:[sequence
385+
// CHECK: {condition Header: {{bb[0-9]+}}
386+
// CHECK: [sequence
387+
// CHECK: <while Preheader: {{bb[0-9]+}}, Header: [[L1HDR]], exit: {{bb[0-9]+}}
388+
// CHECK: block [[L1LATCH]]>
389+
// CHECK: block {{bb[0-9]+}}]
390+
// CHECK: [sequence
391+
// CHECK: <while Preheader: {{bb[0-9]+}}, Header: [[L2HDR]], exit: {{bb[0-9]+}}
392+
// CHECK: [sequence
393+
// CHECK: {condition Header: {{bb[0-9]+}}
394+
// CHECK: block {{bb[0-9]+}}
395+
// CHECK: {condition Header: {{bb[0-9]+}}
396+
// CHECK: [sequence
397+
// CHECK: <while Preheader: {{bb[0-9]+}}, Header: [[L3HDR]], exit: {{bb[0-9]+}}
398+
// CHECK: block [[L3LATCH]]>
399+
// CHECK: block {{bb[0-9]+}}]
400+
// CHECK: block {{bb[0-9]+}}}}
401+
// CHECK: block [[L2LATCH]]]>
402+
// CHECK: block {{bb[0-9]+}}]}
403+
// CHECK: block {{bb[0-9]+}}]
404+
405+
sil @$loopThatRequiresNodeAndLoopCloning : $@convention(thin) (Builtin.Int32, Builtin.Int32) -> Builtin.Int32 {
406+
bb0(%0 : $Builtin.Int32, %1 : $Builtin.Int32):
407+
%2 = integer_literal $Builtin.Int32, 1
408+
%3 = integer_literal $Builtin.Int32, 100
409+
%4 = builtin "sadd_with_overflow_Int32"(%0 : $Builtin.Int32, %2 : $Builtin.Int32) : $Builtin.Int32
410+
%5 = builtin "cmp_slt_Int32"(%4 : $Builtin.Int32, %3 : $Builtin.Int32) : $Builtin.Int1
411+
cond_br %5, bb1, bb2
412+
413+
bb1:
414+
// First arg is sum, the second arg is loop counter.
415+
br bb3(%4 : $Builtin.Int32, %0 : $Builtin.Int32)
416+
417+
bb2:
418+
br bb4(%4 : $Builtin.Int32, %0 : $Builtin.Int32)
419+
420+
bb3(%6 : $Builtin.Int32, %7 : $Builtin.Int32):
421+
%8 = builtin "ssub_with_overflow_Int32"(%6 : $Builtin.Int32, %7 : $Builtin.Int32) : $Builtin.Int32
422+
%100 = builtin "cmp_slt_Int32"(%8 : $Builtin.Int32, %7 : $Builtin.Int32) : $Builtin.Int1
423+
cond_br %100, bb3(%7: $Builtin.Int32, %8: $Builtin.Int32), bb9(%8 : $Builtin.Int32)
424+
425+
bb4(%9 : $Builtin.Int32, %10 : $Builtin.Int32):
426+
%11 = builtin "sadd_with_overflow_Int32"(%10 : $Builtin.Int32, %2 : $Builtin.Int32) : $Builtin.Int32
427+
%12 = builtin "sadd_with_overflow_Int32"(%9 : $Builtin.Int32, %10 : $Builtin.Int32) : $Builtin.Int32
428+
%13 = builtin "cmp_slt_Int32"(%11 : $Builtin.Int32, %1 : $Builtin.Int32) : $Builtin.Int1
429+
cond_br %13, bb5, bb6
430+
431+
bb5:
432+
br bb9(%12 : $Builtin.Int32)
433+
434+
bb6:
435+
%14 = builtin "sadd_with_overflow_Int32"(%11 : $Builtin.Int32, %2 : $Builtin.Int32) : $Builtin.Int32
436+
%15 = builtin "sadd_with_overflow_Int32"(%12 : $Builtin.Int32, %14 : $Builtin.Int32) : $Builtin.Int32
437+
%16 = builtin "cmp_slt_Int32"(%14 : $Builtin.Int32, %1 : $Builtin.Int32) : $Builtin.Int1
438+
cond_br %16, bb8, bb7
439+
440+
bb7:
441+
br bb4(%15 : $Builtin.Int32, %14 : $Builtin.Int32)
442+
443+
bb8:
444+
br bb3(%15 : $Builtin.Int32, %14 : $Builtin.Int32)
445+
446+
bb9(%18 : $Builtin.Int32):
447+
return %18 : $Builtin.Int32
448+
}

test/TensorFlow/sese_loop_canonicalization.swift

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,54 @@
55

66
import TensorFlow
77

8+
//expected-warning @+1 {{value implicitly copied to the host}}
9+
public func testLoopMovementFromOutside(_ breakIndex: Int32) -> Tensor<Int32> {
10+
var i: Int32 = 1
11+
var sum = Tensor<Int32>(0)
12+
let maxCount: Int32 = 100
13+
while i <= maxCount {
14+
sum += i
15+
if (i == breakIndex) {
16+
var k: Int32 = 0
17+
while (k < i) {
18+
sum += i
19+
k += 1
20+
}
21+
break // expected-note {{value used here}}
22+
}
23+
i += 1
24+
}
25+
return sum
26+
}
27+
28+
// CHECK-LABEL: --- XLA CFG Loops Before Canonicalize: {{.*}}testLoopMovementFromOutside{{.*}}
29+
// CHECK: Loop at depth 1 containing: {{.*}}
30+
// CHECK: Loop at depth 1 containing: {{.*}}
31+
32+
// CHECK-LABEL: --- XLA CFG Loops After Canonicalize: {{.*}}testLoopMovementFromOutside{{.*}}
33+
// CHECK: Loop at depth 1 containing: {{.*}}
34+
// CHECK: Loop at depth 2 containing: {{.*}}
35+
36+
// CHECK: --- XLA CFG Canonicalize: {{.*}}testLoopMovementFromOutside{{.*}}
37+
// CHECK: [sequence
38+
// CHECK: <while Preheader: {{.*}}, Header: {{.*}}, exit: {{.*}}
39+
// CHECK: [sequence
40+
// CHECK: {condition Header: {{.*}}
41+
// CHECK: {condition Header: {{.*}}
42+
// CHECK: [sequence
43+
// CHECK: <while Preheader: {{.*}}, Header: {{.*}}, exit: {{.*}}
44+
// CHECK: [sequence
45+
// CHECK: {condition Header: {{.*}}
46+
// CHECK: block {{.*}}
47+
// CHECK: block {{.*}}}
48+
// CHECK: block {{.*}}]>
49+
// CHECK: block {{.*}}]
50+
// CHECK: block {{.*}}}
51+
// CHECK: block {{.*}}}
52+
// CHECK: block {{.*}}]>
53+
// CHECK: block {{.*}}]
54+
55+
856
// This example checks that the loop structure is preserved when we unroll
957
// the body of a loop during canonicalization.
1058
//expected-warning @+1 {{value implicitly copied to the host}}

0 commit comments

Comments
 (0)