Skip to content

Commit b88566a

Browse files
authored
Merge pull request #73621 from atrick/fix-access-mark-uninit
Allow AddressUseDefWalker to continue past MarkUninitializedInst
2 parents 59232e0 + dfe62b4 commit b88566a

File tree

7 files changed

+92
-38
lines changed

7 files changed

+92
-38
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//===--- AccessUtils.swift - Utilities for analyzing memory accesses ------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
//
13+
// TODO: Move this to AccessUtils.swift when FunctionTest is available.
14+
//
15+
//===----------------------------------------------------------------------===//
16+
17+
let getAccessBaseTest = FunctionTest("swift_get_access_base") {
18+
function, arguments, context in
19+
let address = arguments.takeValue()
20+
print("Address: \(address)")
21+
let base = address.accessBase
22+
print("Base: \(base)")
23+
}

SwiftCompilerSources/Sources/Optimizer/Utilities/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
# See http://swift.org/CONTRIBUTORS.txt for Swift project authors
88

99
swift_compiler_sources(Optimizer
10+
AccessUtilsTest.swift
1011
AddressUtils.swift
1112
BorrowedFromUpdater.swift
1213
BorrowUtils.swift

SwiftCompilerSources/Sources/Optimizer/Utilities/Test.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ extension BridgedTestArguments {
153153
public func registerOptimizerTests() {
154154
// Register each test.
155155
registerFunctionTests(
156+
getAccessBaseTest,
156157
argumentConventionsTest,
157158
borrowIntroducersTest,
158159
enclosingValuesTest,

SwiftCompilerSources/Sources/SIL/Utilities/WalkUtils.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -777,8 +777,8 @@ extension AddressUseDefWalker {
777777
} else {
778778
return walkUp(address: ia.base, path: path.push(.anyIndexedElement, index: 0))
779779
}
780-
case let mdi as MarkDependenceInst:
781-
return walkUp(address: mdi.operands[0].value, path: path)
780+
case is MarkDependenceInst, is MarkUninitializedInst:
781+
return walkUp(address: (def as! Instruction).operands[0].value, path: path)
782782
case is MoveOnlyWrapperToCopyableAddrInst,
783783
is CopyableToMoveOnlyWrapperAddrInst:
784784
return walkUp(address: (def as! Instruction).operands[0].value, path: path)

lib/SIL/Utils/MemAccessUtils.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2087,13 +2087,14 @@ struct AccessUseTestVisitor : public AccessUseVisitor {
20872087
}
20882088
};
20892089

2090-
static FunctionTest AccessPathBaseTest("accesspath-base", [](auto &function,
2091-
auto &arguments,
2092-
auto &test) {
2090+
static FunctionTest AccessPathBaseTest("accesspath", [](auto &function,
2091+
auto &arguments,
2092+
auto &test) {
20932093
auto value = arguments.takeValue();
20942094
function.print(llvm::outs());
2095-
llvm::outs() << "Access path base: " << value;
2095+
llvm::outs() << "Access path for: " << value;
20962096
auto accessPathWithBase = AccessPathWithBase::compute(value);
2097+
llvm::outs() << " base: " << accessPathWithBase.base;
20972098
AccessUseTestVisitor visitor;
20982099
visitAccessPathBaseUses(visitor, accessPathWithBase, &function);
20992100
});

test/SILOptimizer/accessbase_unit.sil

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ class C {}
2727
// CHECK: Class [[BOX]] = argument of bb0
2828
// CHECK: Field: var value: T Index: 0
2929
// CHECK-LABEL: end running test {{.*}} on test_phi_nested_access: compute_access_storage with
30-
// CHECK-LABEL: begin running test 3 of 3 on test_phi_nested_access: accesspath-base
31-
// CHECK: Access path base: %6 = argument of bb1
30+
// CHECK-LABEL: begin running test 3 of 3 on test_phi_nested_access: accesspath
31+
// CHECK: Access path for: %6 = argument of bb1
3232
// CHECK: Exact Use: end_access %2
33-
// CHECK-LABEL: end running test 3 of 3 on test_phi_nested_access: accesspath-base
33+
// CHECK-LABEL: end running test 3 of 3 on test_phi_nested_access: accesspath
3434
sil @test_phi_nested_access : $@convention(method) (@guaranteed Box<C>) -> () {
3535
bb1(%box : $Box<C>):
3636
%value_addr = ref_element_addr %box : $Box<C>, #Box.value
@@ -42,7 +42,7 @@ bb1(%box : $Box<C>):
4242
exit(%phi : $Builtin.RawPointer):
4343
specify_test "get_access_base @argument"
4444
specify_test "compute_access_storage @argument"
45-
specify_test "accesspath-base @argument"
45+
specify_test "accesspath @argument"
4646
%retval = tuple ()
4747
return %retval : $()
4848
}
@@ -65,3 +65,24 @@ sil @test_access_global : $@convention(thin) () -> () {
6565
%retval = tuple ()
6666
return %retval : $()
6767
}
68+
69+
// CHECK-LABEL: begin running test {{.*}} on test_mark_uninitialized_inst: get_access_base
70+
// CHECK: Address: %{{.*}} = mark_uninitialized [var] [[ALLOC:%.*]] : $*C
71+
// CHECK: Base: [[ALLOC]] = alloc_stack [var_decl] $C
72+
// CHECK-LABEL: end running test {{.*}} on test_mark_uninitialized_inst: get_access_base
73+
// CHECK-LABEL: begin running test {{.*}} on test_mark_uninitialized_inst: swift_get_access_base
74+
// CHECK: Address: %{{.*}} = mark_uninitialized [var] [[ALLOC:%.*]] : $*C
75+
// CHECK: Base: stack - [[ALLOC]] = alloc_stack [var_decl] $C
76+
// CHECK-LABEL: end running test {{.*}} on test_mark_uninitialized_inst: swift_get_access_base
77+
sil [ossa] @test_mark_uninitialized_inst : $@convention(thin) (@owned C) -> () {
78+
bb0(%0 : @owned $C):
79+
%1 = alloc_stack [var_decl] $C, let
80+
%2 = mark_uninitialized [var] %1 : $*C
81+
specify_test "get_access_base %2"
82+
specify_test "swift_get_access_base %2"
83+
assign %0 to %2 : $*C
84+
destroy_addr %2 : $*C
85+
dealloc_stack %1 : $*C
86+
%9999 = tuple()
87+
return %9999 : $()
88+
}

test/SILOptimizer/accesspath_unit.sil

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,34 +20,36 @@ struct S2 {
2020
sil_global hidden @globalKlass : $Klass
2121
sil_global hidden @globalStruct : $S2
2222

23-
// CHECK-LABEL: begin running test 1 of 2 on testRefElement: accesspath-base with: @trace[0]
23+
// CHECK-LABEL: begin running test 1 of 2 on testRefElement: accesspath with: @trace[0]
2424
// CHECK: [[P1:%.*]] = ref_element_addr %0 : $Klass, #Klass.f
2525
// CHECK: [[A1:%.*]] = begin_access [read] [dynamic] [[P1]] : $*S
2626
// CHECK: [[P2:%.*]] = ref_element_addr %0 : $Klass, #Klass.f
2727
// CHECK: [[A2:%.*]] = begin_access [read] [dynamic] [[P2]] : $*S
28-
// CHECK: Access path base: [[P1]] = ref_element_addr %0 : $Klass, #Klass.f
28+
// CHECK: Access path for: [[P1]] = ref_element_addr %0 : $Klass, #Klass.f
29+
// CHECK-NEXT: base: [[P1]] = ref_element_addr %0 : $Klass, #Klass.f
2930
// CHECK-NEXT: Exact Use: %{{.*}} = load [trivial] [[A1]] : $*S
3031
// CHECK-NEXT: Exact Use: end_access [[A1]] : $*S
31-
// CHECK: end running test 1 of 2 on testRefElement: accesspath-base with: @trace[0]
32+
// CHECK: end running test 1 of 2 on testRefElement: accesspath with: @trace[0]
3233

33-
// CHECK-LABEL: begin running test 2 of 2 on testRefElement: accesspath-base with: @trace[1]
34+
// CHECK-LABEL: begin running test 2 of 2 on testRefElement: accesspath with: @trace[1]
3435
// CHECK: [[P1:%.*]] = ref_element_addr %0 : $Klass, #Klass.f
3536
// CHECK: [[A1:%.*]] = begin_access [read] [dynamic] [[P1]] : $*S
3637
// CHECK: [[P2:%.*]] = ref_element_addr %0 : $Klass, #Klass.f
3738
// CHECK: [[A2:%.*]] = begin_access [read] [dynamic] [[P2]] : $*S
38-
// CHECK: Access path base: [[P2]] = ref_element_addr %0 : $Klass, #Klass.f
39+
// CHECK: Access path for: [[P2]] = ref_element_addr %0 : $Klass, #Klass.f
40+
// CHECK: base: [[P2]] = ref_element_addr %0 : $Klass, #Klass.f
3941
// CHECK-NEXT: Exact Use: %{{.*}} = load [trivial] [[A2]] : $*S
4042
// CHECK-NEXT: Exact Use: end_access [[A2]] : $*S
41-
// CHECK: end running test 2 of 2 on testRefElement: accesspath-base with: @trace[1]
43+
// CHECK: end running test 2 of 2 on testRefElement: accesspath with: @trace[1]
4244
sil hidden [ossa] @testRefElement : $@convention(thin) (@guaranteed Klass) -> () {
4345
bb0(%0 : @guaranteed $Klass):
44-
specify_test "accesspath-base @trace[0]"
46+
specify_test "accesspath @trace[0]"
4547
%p1 = ref_element_addr %0 : $Klass, #Klass.f
4648
debug_value [trace] %p1 : $*S
4749
%a1 = begin_access [read] [dynamic] %p1 : $*S
4850
%l1 = load [trivial] %a1 : $*S
4951
end_access %a1 : $*S
50-
specify_test "accesspath-base @trace[1]"
52+
specify_test "accesspath @trace[1]"
5153
%p2 = ref_element_addr %0 : $Klass, #Klass.f
5254
debug_value [trace] %p2 : $*S
5355
%a2 = begin_access [read] [dynamic] %p2 : $*S
@@ -57,36 +59,38 @@ bb0(%0 : @guaranteed $Klass):
5759
return %99 : $()
5860
}
5961

60-
// CHECK-LABEL: begin running test 1 of 2 on testGlobalAddrKlass: accesspath-base with: @trace[0]
62+
// CHECK-LABEL: begin running test 1 of 2 on testGlobalAddrKlass: accesspath with: @trace[0]
6163
// CHECK: [[P1:%.*]] = global_addr @globalKlass : $*Klass
6264
// CHECK: [[A1:%.*]] = begin_access [read] [dynamic] [[P1]] : $*Klass
6365
// CHECK: [[P2:%.*]] = global_addr @globalKlass : $*Klass
6466
// CHECK: [[A2:%.*]] = begin_access [read] [dynamic] [[P2]] : $*Klass
65-
// CHECK: Access path base: [[P1]] = global_addr @globalKlass : $*Klass
67+
// CHECK: Access path for: [[P1]] = global_addr @globalKlass : $*Klass
68+
// CHECK: base: [[P1]] = global_addr @globalKlass : $*Klass
6669
// CHECK-NEXT: Exact Use: %{{.*}} = load_borrow [[A1]]
6770
// CHECK-NEXT: Exact Use: end_access [[A1]]
68-
// CHECK: end running test 1 of 2 on testGlobalAddrKlass: accesspath-base with: @trace[0]
71+
// CHECK: end running test 1 of 2 on testGlobalAddrKlass: accesspath with: @trace[0]
6972

70-
// CHECK-LABEL: begin running test 2 of 2 on testGlobalAddrKlass: accesspath-base with: @trace[1]
73+
// CHECK-LABEL: begin running test 2 of 2 on testGlobalAddrKlass: accesspath with: @trace[1]
7174
// CHECK: [[P1:%.*]] = global_addr @globalKlass : $*Klass
7275
// CHECK: [[A1:%.*]] = begin_access [read] [dynamic] [[P1]] : $*Klass
7376
// CHECK: [[P2:%.*]] = global_addr @globalKlass : $*Klass
7477
// CHECK: [[A2:%.*]] = begin_access [read] [dynamic] [[P2]] : $*Klass
75-
// CHECK: Access path base: [[P2]] = global_addr @globalKlass : $*Klass
78+
// CHECK: Access path for: [[P2]] = global_addr @globalKlass : $*Klass
79+
// CHECK: base: [[P2]] = global_addr @globalKlass : $*Klass
7680
// CHECK-NEXT: Exact Use: %{{.*}} = load_borrow [[A2]]
7781
// CHECK-NEXT: Exact Use: end_access [[A2]]
78-
// CHECK: end running test 2 of 2 on testGlobalAddrKlass: accesspath-base with: @trace[1]
82+
// CHECK: end running test 2 of 2 on testGlobalAddrKlass: accesspath with: @trace[1]
7983
sil [ossa] @testGlobalAddrKlass : $@convention(thin) () -> () {
8084
bb0:
81-
specify_test "accesspath-base @trace[0]"
85+
specify_test "accesspath @trace[0]"
8286
%p1 = global_addr @globalKlass : $*Klass
8387
debug_value [trace] %p1 : $*Klass
8488
%a1 = begin_access [read] [dynamic] %p1 : $*Klass
8589
%l1 = load_borrow %a1 : $*Klass
8690
end_borrow %l1 : $Klass
8791
end_access %a1 : $*Klass
8892

89-
specify_test "accesspath-base @trace[1]"
93+
specify_test "accesspath @trace[1]"
9094
%p2 = global_addr @globalKlass : $*Klass
9195
debug_value [trace] %p2 : $*Klass
9296
%a2 = begin_access [read] [dynamic] %p2 : $*Klass
@@ -98,7 +102,7 @@ bb0:
98102
return %9999 : $()
99103
}
100104

101-
// CHECK-LABEL: begin running test 1 of 3 on testGlobalAddrStruct: accesspath-base with: @trace[0]
105+
// CHECK-LABEL: begin running test 1 of 3 on testGlobalAddrStruct: accesspath with: @trace[0]
102106
// CHECK: [[P1:%.*]] = global_addr @globalStruct
103107
// CHECK: [[A1:%.*]] = begin_access [read] [dynamic] [[P1]]
104108
// CHECK: [[P2:%.*]] = global_addr @globalStruct
@@ -107,12 +111,13 @@ bb0:
107111
// CHECK: [[P3:%.*]] = global_addr @globalStruct
108112
// CHECK: [[A3:%.*]] = begin_access [read] [dynamic] [[P3]]
109113
// CHECK: [[GEP3:%.*]] = struct_element_addr [[A3]]
110-
// CHECK: Access path base: [[P1]] = global_addr @globalStruct
114+
// CHECK: Access path for: [[P1]] = global_addr @globalStruct
115+
// CHECK: base: [[P1]] = global_addr @globalStruct
111116
// CHECK-NEXT: Exact Use: %{{.*}} = load_borrow [[A1]]
112117
// CHECK-NEXT: Exact Use: end_access [[A1]]
113-
// CHECK: end running test 1 of 3 on testGlobalAddrStruct: accesspath-base with: @trace[0]
118+
// CHECK: end running test 1 of 3 on testGlobalAddrStruct: accesspath with: @trace[0]
114119

115-
// CHECK-LABEL: begin running test 2 of 3 on testGlobalAddrStruct: accesspath-base with: @trace[1]
120+
// CHECK-LABEL: begin running test 2 of 3 on testGlobalAddrStruct: accesspath with: @trace[1]
116121
// CHECK: [[P1:%.*]] = global_addr @globalStruct
117122
// CHECK: [[A1:%.*]] = begin_access [read] [dynamic] [[P1]]
118123
// CHECK: [[P2:%.*]] = global_addr @globalStruct
@@ -121,12 +126,13 @@ bb0:
121126
// CHECK: [[P3:%.*]] = global_addr @globalStruct
122127
// CHECK: [[A3:%.*]] = begin_access [read] [dynamic] [[P3]]
123128
// CHECK: [[GEP3:%.*]] = struct_element_addr [[A3]]
124-
// CHECK: Access path base: [[P2]] = global_addr @globalStruct
129+
// CHECK: Access path for: [[P2]] = global_addr @globalStruct
130+
// CHECK: base: [[P2]] = global_addr @globalStruct
125131
// CHECK-NEXT: Inner Use: %{{.*}} = load_borrow [[GEP2]]
126132
// CHECK-NEXT: Exact Use: end_access [[A2]]
127-
// CHECK: end running test 2 of 3 on testGlobalAddrStruct: accesspath-base with: @trace[1]
133+
// CHECK: end running test 2 of 3 on testGlobalAddrStruct: accesspath with: @trace[1]
128134

129-
// CHECK-LABEL: begin running test 3 of 3 on testGlobalAddrStruct: accesspath-base with: @trace[2]
135+
// CHECK-LABEL: begin running test 3 of 3 on testGlobalAddrStruct: accesspath with: @trace[2]
130136
// CHECK: [[P1:%.*]] = global_addr @globalStruct
131137
// CHECK: [[A1:%.*]] = begin_access [read] [dynamic] [[P1]]
132138
// CHECK: [[P2:%.*]] = global_addr @globalStruct
@@ -135,21 +141,22 @@ bb0:
135141
// CHECK: [[P3:%.*]] = global_addr @globalStruct
136142
// CHECK: [[A3:%.*]] = begin_access [read] [dynamic] [[P3]]
137143
// CHECK: [[GEP3:%.*]] = struct_element_addr [[A3]]
138-
// CHECK: Access path base: [[P3]] = global_addr @globalStruct
144+
// CHECK: Access path for: [[P3]] = global_addr @globalStruct
145+
// CHECK: base: [[P3]] = global_addr @globalStruct
139146
// CHECK-NEXT: Inner Use: %{{.*}} = load_borrow [[GEP3]]
140147
// CHECK-NEXT: Exact Use: end_access [[A3]]
141-
// CHECK: end running test 3 of 3 on testGlobalAddrStruct: accesspath-base with: @trace[2]
148+
// CHECK: end running test 3 of 3 on testGlobalAddrStruct: accesspath with: @trace[2]
142149
sil [ossa] @testGlobalAddrStruct : $@convention(thin) () -> () {
143150
bb0:
144-
specify_test "accesspath-base @trace[0]"
151+
specify_test "accesspath @trace[0]"
145152
%p3 = global_addr @globalStruct : $*S2
146153
debug_value [trace] %p3 : $*S2
147154
%a3 = begin_access [read] [dynamic] %p3 : $*S2
148155
%l3 = load_borrow %a3 : $*S2
149156
end_borrow %l3 : $S2
150157
end_access %a3 : $*S2
151158

152-
specify_test "accesspath-base @trace[1]"
159+
specify_test "accesspath @trace[1]"
153160
%p4 = global_addr @globalStruct : $*S2
154161
debug_value [trace] %p4 : $*S2
155162
%a4 = begin_access [read] [dynamic] %p4 : $*S2
@@ -158,7 +165,7 @@ bb0:
158165
end_borrow %l4 : $Klass
159166
end_access %a4 : $*S2
160167

161-
specify_test "accesspath-base @trace[2]"
168+
specify_test "accesspath @trace[2]"
162169
%p5 = global_addr @globalStruct : $*S2
163170
debug_value [trace] %p5 : $*S2
164171
%a5 = begin_access [read] [dynamic] %p5 : $*S2

0 commit comments

Comments
 (0)