Skip to content

Commit 7a2e6d8

Browse files
authored
Merge pull request #60056 from eeckstein/ignore-malloc-size-in-analysis
SIL optimizer: ignore `_swift_stdlib_malloc_size` and `_swift_stdlib_has_malloc_size` runtime calls in analysis
2 parents 7349383 + 692db6d commit 7a2e6d8

File tree

6 files changed

+68
-8
lines changed

6 files changed

+68
-8
lines changed

include/swift/SILOptimizer/Analysis/AccessStorageAnalysis.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -295,10 +295,7 @@ class FunctionAccessStorage {
295295
/// the callee.
296296
///
297297
/// TODO: Summarize ArraySemanticsCall accesses.
298-
bool summarizeCall(FullApplySite fullApply) {
299-
assert(accessResult.isEmpty() && "expected uninitialized results.");
300-
return false;
301-
}
298+
bool summarizeCall(FullApplySite fullApply);
302299

303300
/// Merge effects directly from \p RHS.
304301
bool mergeFrom(const FunctionAccessStorage &RHS) {

lib/SILOptimizer/Analysis/AccessStorageAnalysis.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,19 @@ bool FunctionAccessStorage::summarizeFunction(SILFunction *F) {
380380
return true;
381381
}
382382

383+
bool FunctionAccessStorage::summarizeCall(FullApplySite fullApply) {
384+
assert(accessResult.isEmpty() && "expected uninitialized results.");
385+
386+
if (SILFunction *callee = fullApply.getReferencedFunctionOrNull()) {
387+
if (callee->getName() == "_swift_stdlib_malloc_size" ||
388+
callee->getName() == "_swift_stdlib_has_malloc_size") {
389+
return true;
390+
}
391+
}
392+
393+
return false;
394+
}
395+
383396
SILAnalysis *swift::createAccessStorageAnalysis(SILModule *) {
384397
return new AccessStorageAnalysis();
385398
}

lib/SILOptimizer/Analysis/SideEffectAnalysis.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,12 @@ bool FunctionSideEffects::summarizeCall(FullApplySite fullApply) {
498498
// Does the function have any @_effects?
499499
if (setDefinedEffects(SingleCallee))
500500
return true;
501+
502+
if (SingleCallee->getName() == "_swift_stdlib_malloc_size" ||
503+
SingleCallee->getName() == "_swift_stdlib_has_malloc_size") {
504+
GlobalEffects.Reads = true;
505+
return true;
506+
}
501507
}
502508
return false;
503509
}

test/SILOptimizer/access_enforcement_opts_ossa.sil

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1874,3 +1874,23 @@ bb0(%0 : @owned $NonTrivial):
18741874
destroy_value %0 : $NonTrivial
18751875
return undef : $()
18761876
}
1877+
1878+
sil shared [clang _swift_stdlib_has_malloc_size] @_swift_stdlib_has_malloc_size : $@convention(c) () -> Bool
1879+
sil shared [clang _swift_stdlib_malloc_size] @_swift_stdlib_malloc_size : $@convention(c) (UnsafeRawPointer) -> Int
1880+
1881+
// CHECK-LABEL: sil [ossa] @ignoreMallocSize :
1882+
// CHECK: begin_access [read] [dynamic] [no_nested_conflict]
1883+
// CHECK-LABEL: } // end sil function 'ignoreMallocSize'
1884+
sil [ossa] @ignoreMallocSize : $@convention(thin) (UnsafeRawPointer) -> () {
1885+
bb0(%0 : $UnsafeRawPointer):
1886+
%2 = global_addr @globalX: $*X
1887+
%3 = begin_access [read] [dynamic] %2 : $*X
1888+
%4 = function_ref @_swift_stdlib_has_malloc_size : $@convention(c) () -> Bool
1889+
%5 = apply %4() : $@convention(c) () -> Bool
1890+
%6 = function_ref @_swift_stdlib_malloc_size : $@convention(c) (UnsafeRawPointer) -> Int
1891+
%7 = apply %6(%0) : $@convention(c) (UnsafeRawPointer) -> Int
1892+
end_access %3 : $*X
1893+
%9 = tuple ()
1894+
return %9 : $()
1895+
}
1896+

test/SILOptimizer/merge_exclusivity.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -377,13 +377,13 @@ private struct EscapedTransforme<T>: WriteProt {
377377

378378
// TESTSIL-LABEL: sil [noinline] @$s17merge_exclusivity14run_MergeTest9yySiF : $@convention(thin)
379379
// TESTSIL: [[REFADDR:%.*]] = ref_element_addr {{.*}} : $StreamClass, #StreamClass.buffer
380-
// TESTSIL-NEXT: [[B1:%.*]] = begin_access [modify] [dynamic] [no_nested_conflict] [[REFADDR]]
380+
// TESTSIL-NEXT: [[B1:%.*]] = begin_access [modify] [{{.*}}] [no_nested_conflict] [[REFADDR]]
381381
// TESTSIL: end_access [[B1]]
382-
// TESTSIL: [[BCONF:%.*]] = begin_access [modify] [dynamic] [[REFADDR]]
382+
// TESTSIL: [[BCONF:%.*]] = begin_access [modify] [{{.*}}] [[REFADDR]]
383383
// TESTSIL: end_access [[BCONF]]
384-
// TESTSIL: [[BCONF:%.*]] = begin_access [modify] [dynamic] [[REFADDR]]
384+
// TESTSIL: [[BCONF:%.*]] = begin_access [modify] [{{.*}}] [[REFADDR]]
385385
// TESTSIL: end_access [[BCONF]]
386-
// TESTSIL: [[BCONF:%.*]] = begin_access [modify] [dynamic] [[REFADDR]]
386+
// TESTSIL: [[BCONF:%.*]] = begin_access [modify] [{{.*}}] [[REFADDR]]
387387
// TESTSIL: end_access [[BCONF]]
388388
// TESTSIL-LABEL: } // end sil function '$s17merge_exclusivity14run_MergeTest9yySiF'
389389
@inline(never)

test/SILOptimizer/redundant_load_elim_ossa.sil

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ sil_stage canonical
55

66
import Builtin
77
import Swift
8+
import SwiftShims
89

910
///////////////////////
1011
// Type Declarations //
@@ -1348,6 +1349,29 @@ bb0(%0 : $*Int):
13481349
}
13491350
// CHECK-LABEL: } // end sil function 'test_rle_in_guaranteed_entry'
13501351

1352+
sil shared [clang _swift_stdlib_has_malloc_size] @_swift_stdlib_has_malloc_size : $@convention(c) () -> Bool
1353+
sil shared [clang _swift_stdlib_malloc_size] @_swift_stdlib_malloc_size : $@convention(c) (UnsafeRawPointer) -> Int
1354+
1355+
// CHECK-LABEL: sil [ossa] @ignoreMallocSize :
1356+
// CHECK: [[VAL:%.*]] = load
1357+
// CHECK: [[UI:%.*]] = function_ref @use_Int
1358+
// CHECK: apply [[UI]]([[VAL]])
1359+
// CHECK: return [[VAL]]
1360+
// CHECK-LABEL: } // end sil function 'ignoreMallocSize'
1361+
sil [ossa] @ignoreMallocSize : $@convention(thin) (@guaranteed AB, UnsafeRawPointer) -> Int {
1362+
bb0(%0 : @guaranteed $AB, %1 : $UnsafeRawPointer):
1363+
%2 = ref_element_addr %0 : $AB, #AB.value
1364+
%3 = load [trivial] %2 : $*Int
1365+
%4 = function_ref @_swift_stdlib_has_malloc_size : $@convention(c) () -> Bool
1366+
%5 = apply %4() : $@convention(c) () -> Bool
1367+
%6 = function_ref @_swift_stdlib_malloc_size : $@convention(c) (UnsafeRawPointer) -> Int
1368+
%7 = apply %6(%1) : $@convention(c) (UnsafeRawPointer) -> Int
1369+
%8 = load [trivial] %2 : $*Int
1370+
%9 = function_ref @use_Int : $@convention(thin) (Int) -> ()
1371+
apply %9(%3) : $@convention(thin) (Int) -> ()
1372+
return %8 : $Int
1373+
}
1374+
13511375
// Check that begin_access, end_access, strong_release, set_deallocating, and dealloc_ref don't prevent optimization.
13521376
// CHECK-LABEL: ignore_read_write :
13531377
// CHECK: bb0

0 commit comments

Comments
 (0)