Skip to content

Commit 50717d5

Browse files
authored
Merge pull request swiftlang#35672 from gottesmm/pr-fb34b019957e7895581439913ff16e00b5ee5d1d
[pmo] Teach pmo to ignore debug instruction uses.
2 parents 3ec0ddc + 6a982de commit 50717d5

File tree

3 files changed

+134
-0
lines changed

3 files changed

+134
-0
lines changed

lib/SILOptimizer/Mandatory/PMOMemoryUseCollector.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,10 @@ bool ElementUseCollector::collectUses(SILValue Pointer) {
438438
if (isSanitizerInstrumentation(User))
439439
continue;
440440

441+
// We don't care about debug instructions.
442+
if (User->isDebugInstruction())
443+
continue;
444+
441445
// Otherwise, the use is something complicated, it escapes.
442446
Uses.emplace_back(User, PMOUseKind::Escape);
443447
}

test/SILOptimizer/predictable_memaccess_opts.sil

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1935,3 +1935,71 @@ bbEnd:
19351935
return %9999 : $()
19361936
}
19371937

1938+
class Foo {}
1939+
struct MyInt {
1940+
var _value: Builtin.Int64
1941+
}
1942+
struct Baz {
1943+
var x: MyInt
1944+
var b: Builtin.NativeObject
1945+
}
1946+
sil @foo_method : $@convention(method) (@guaranteed Foo) -> MyInt
1947+
1948+
// We promote all loads below.
1949+
//
1950+
// CHECK: sil [ossa] @promote_loads_despite_debug_uses : $@convention(thin) (MyInt, @guaranteed Foo, @guaranteed Baz) -> MyInt {
1951+
// CHECK-NOT: load
1952+
// CHECK: alloc_stack
1953+
// CHECK-NOT: load
1954+
// CHECK: alloc_stack
1955+
// CHECK-NOT: load
1956+
// CHECK: alloc_stack
1957+
// CHECK-NOT: load
1958+
// CHECK: alloc_stack
1959+
// CHECK-NOT: load
1960+
// CHECK: } // end sil function 'promote_loads_despite_debug_uses'
1961+
sil [ossa] @promote_loads_despite_debug_uses : $@convention(thin) (MyInt, @guaranteed Foo, @guaranteed Baz) -> MyInt {
1962+
bb0(%0 : $MyInt, %1 : @guaranteed $Foo, %2 : @guaranteed $Baz):
1963+
%3 = alloc_stack $MyInt
1964+
store %0 to [trivial] %3 : $*MyInt
1965+
%5 = alloc_stack $Foo
1966+
%6 = copy_value %1 : $Foo
1967+
store %6 to [init] %5 : $*Foo
1968+
%8 = alloc_stack $Baz
1969+
%9 = copy_value %2 : $Baz
1970+
store %9 to [init] %8 : $*Baz
1971+
debug_value_addr %3 : $*MyInt, var, name "x", argno 1
1972+
debug_value_addr %5 : $*Foo, var, name "y", argno 2
1973+
debug_value_addr %8 : $*Baz, var, name "z", argno 3
1974+
%14 = load [trivial] %3 : $*MyInt
1975+
%15 = load [copy] %5 : $*Foo
1976+
1977+
%16 = function_ref @foo_method : $@convention(method) (@guaranteed Foo) -> MyInt
1978+
%17 = begin_borrow %15 : $Foo
1979+
%18 = apply %16(%17) : $@convention(method) (@guaranteed Foo) -> MyInt
1980+
end_borrow %17 : $Foo
1981+
destroy_value %15 : $Foo
1982+
%21 = struct_extract %14 : $MyInt, #MyInt._value
1983+
%22 = struct_extract %18 : $MyInt, #MyInt._value
1984+
%23 = integer_literal $Builtin.Int1, -1
1985+
%24 = builtin "sadd_with_overflow_Int64"(%21 : $Builtin.Int64, %22 : $Builtin.Int64, %23 : $Builtin.Int1) : $(Builtin.Int64, Builtin.Int1)
1986+
(%25, %26) = destructure_tuple %24 : $(Builtin.Int64, Builtin.Int1)
1987+
cond_fail %26 : $Builtin.Int1, "arithmetic overflow"
1988+
%28 = struct $MyInt (%25 : $Builtin.Int64)
1989+
%29 = struct_element_addr %8 : $*Baz, #Baz.x
1990+
%30 = load [trivial] %29 : $*MyInt
1991+
%31 = struct_extract %28 : $MyInt, #MyInt._value
1992+
%32 = struct_extract %30 : $MyInt, #MyInt._value
1993+
%33 = integer_literal $Builtin.Int1, -1
1994+
%34 = builtin "sadd_with_overflow_Int64"(%31 : $Builtin.Int64, %32 : $Builtin.Int64, %33 : $Builtin.Int1) : $(Builtin.Int64, Builtin.Int1)
1995+
(%35, %36) = destructure_tuple %34 : $(Builtin.Int64, Builtin.Int1)
1996+
cond_fail %36 : $Builtin.Int1, "arithmetic overflow"
1997+
%38 = struct $MyInt (%35 : $Builtin.Int64)
1998+
destroy_addr %8 : $*Baz
1999+
dealloc_stack %8 : $*Baz
2000+
destroy_addr %5 : $*Foo
2001+
dealloc_stack %5 : $*Foo
2002+
destroy_addr %3 : $*MyInt
2003+
dealloc_stack %3 : $*MyInt
2004+
return %38 : $MyInt
2005+
} // end sil function '$s27capture_promotion_ownership05test_a1_B0SiycyFSiycfU_Tf2iii_n'

test/SILOptimizer/predictable_memopt_ownership.sil

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1340,3 +1340,65 @@ bb5:
13401340
bbEnd(%8 : @owned $Optional<Builtin.NativeObject>):
13411341
return %8 : $Optional<Builtin.NativeObject>
13421342
}
1343+
1344+
class Foo {}
1345+
struct MyInt {
1346+
var _value: Builtin.Int64
1347+
}
1348+
struct Baz {
1349+
var x: MyInt
1350+
var b: Builtin.NativeObject
1351+
}
1352+
sil @foo_method : $@convention(method) (@guaranteed Foo) -> MyInt
1353+
1354+
// We completely eliminate the alloc_stack here since the alloc_stack are not
1355+
// viewed as user code by dead allocation elimination.
1356+
//
1357+
// CHECK: sil [ossa] @promote_loads_despite_debug_uses : $@convention(thin) (MyInt, @guaranteed Foo, @guaranteed Baz) -> MyInt {
1358+
// CHECK-NOT: alloc_stack
1359+
// CHECK: } // end sil function 'promote_loads_despite_debug_uses'
1360+
sil [ossa] @promote_loads_despite_debug_uses : $@convention(thin) (MyInt, @guaranteed Foo, @guaranteed Baz) -> MyInt {
1361+
bb0(%0 : $MyInt, %1 : @guaranteed $Foo, %2 : @guaranteed $Baz):
1362+
%3 = alloc_stack $MyInt
1363+
store %0 to [trivial] %3 : $*MyInt
1364+
%5 = alloc_stack $Foo
1365+
%6 = copy_value %1 : $Foo
1366+
store %6 to [init] %5 : $*Foo
1367+
%8 = alloc_stack $Baz
1368+
%9 = copy_value %2 : $Baz
1369+
store %9 to [init] %8 : $*Baz
1370+
debug_value_addr %3 : $*MyInt, var, name "x", argno 1
1371+
debug_value_addr %5 : $*Foo, var, name "y", argno 2
1372+
debug_value_addr %8 : $*Baz, var, name "z", argno 3
1373+
%14 = load [trivial] %3 : $*MyInt
1374+
%15 = load [copy] %5 : $*Foo
1375+
1376+
%16 = function_ref @foo_method : $@convention(method) (@guaranteed Foo) -> MyInt
1377+
%17 = begin_borrow %15 : $Foo
1378+
%18 = apply %16(%17) : $@convention(method) (@guaranteed Foo) -> MyInt
1379+
end_borrow %17 : $Foo
1380+
destroy_value %15 : $Foo
1381+
%21 = struct_extract %14 : $MyInt, #MyInt._value
1382+
%22 = struct_extract %18 : $MyInt, #MyInt._value
1383+
%23 = integer_literal $Builtin.Int1, -1
1384+
%24 = builtin "sadd_with_overflow_Int64"(%21 : $Builtin.Int64, %22 : $Builtin.Int64, %23 : $Builtin.Int1) : $(Builtin.Int64, Builtin.Int1)
1385+
(%25, %26) = destructure_tuple %24 : $(Builtin.Int64, Builtin.Int1)
1386+
cond_fail %26 : $Builtin.Int1, "arithmetic overflow"
1387+
%28 = struct $MyInt (%25 : $Builtin.Int64)
1388+
%29 = struct_element_addr %8 : $*Baz, #Baz.x
1389+
%30 = load [trivial] %29 : $*MyInt
1390+
%31 = struct_extract %28 : $MyInt, #MyInt._value
1391+
%32 = struct_extract %30 : $MyInt, #MyInt._value
1392+
%33 = integer_literal $Builtin.Int1, -1
1393+
%34 = builtin "sadd_with_overflow_Int64"(%31 : $Builtin.Int64, %32 : $Builtin.Int64, %33 : $Builtin.Int1) : $(Builtin.Int64, Builtin.Int1)
1394+
(%35, %36) = destructure_tuple %34 : $(Builtin.Int64, Builtin.Int1)
1395+
cond_fail %36 : $Builtin.Int1, "arithmetic overflow"
1396+
%38 = struct $MyInt (%35 : $Builtin.Int64)
1397+
destroy_addr %8 : $*Baz
1398+
dealloc_stack %8 : $*Baz
1399+
destroy_addr %5 : $*Foo
1400+
dealloc_stack %5 : $*Foo
1401+
destroy_addr %3 : $*MyInt
1402+
dealloc_stack %3 : $*MyInt
1403+
return %38 : $MyInt
1404+
} // end sil function '$s27capture_promotion_ownership05test_a1_B0SiycyFSiycfU_Tf2iii_n'

0 commit comments

Comments
 (0)