Skip to content

Commit 9154082

Browse files
committed
[sil] Teach VariableNameInferrer how to look through /all/ accessors.
1 parent e4bd7f7 commit 9154082

File tree

3 files changed

+85
-9
lines changed

3 files changed

+85
-9
lines changed

include/swift/SILOptimizer/Utils/VariableNameUtils.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#ifndef SWIFT_SILOPTIMIZER_UTILS_VARIABLENAMEUTILS_H
1818
#define SWIFT_SILOPTIMIZER_UTILS_VARIABLENAMEUTILS_H
1919

20+
#include "swift/Basic/OptionSet.h"
2021
#include "swift/SIL/ApplySite.h"
2122
#include "swift/SIL/DebugUtils.h"
2223
#include "swift/SIL/MemAccessUtils.h"
@@ -26,6 +27,19 @@
2627
namespace swift {
2728

2829
class VariableNameInferrer {
30+
public:
31+
enum class Flag {
32+
/// If set then we should look through get and set accessors and infer their
33+
/// name from self.
34+
///
35+
/// DISCUSSION: This may not be the correct semantics for all name inference
36+
/// since we may want to consider computed properties to be tied to self.
37+
InferSelfThroughAllAccessors = 0x1,
38+
};
39+
40+
using Options = OptionSet<Flag>;
41+
42+
private:
2943
/// The stacklist that we use to process from use->
3044
StackList<PointerUnion<SILInstruction *, SILValue>> variableNamePath;
3145

@@ -37,10 +51,21 @@ class VariableNameInferrer {
3751
/// The final string we computed.
3852
SmallString<64> &resultingString;
3953

54+
/// Options that control how we do our walk.
55+
///
56+
/// Example: In certain cases we may want to impute self as a name for
57+
/// computed getters/setters and in other cases we may not want to.
58+
Options options;
59+
4060
public:
4161
VariableNameInferrer(SILFunction *fn, SmallString<64> &resultingString)
4262
: variableNamePath(fn), resultingString(resultingString) {}
4363

64+
VariableNameInferrer(SILFunction *fn, Options options,
65+
SmallString<64> &resultingString)
66+
: variableNamePath(fn), resultingString(resultingString),
67+
options(options) {}
68+
4469
/// Attempts to infer a name from just uses of \p searchValue.
4570
///
4671
/// Returns true if we found a name.

lib/SILOptimizer/Utils/VariableNameUtils.cpp

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -124,30 +124,40 @@ VariableNameInferrer::findDebugInfoProvidingValue(SILValue searchValue) {
124124
}
125125

126126
auto getNamePathComponentFromCallee =
127-
[&](FullApplySite call) -> std::optional<SILValue> {
127+
[&](FullApplySite call) -> SILValue {
128128
// Use the name of the property being accessed if we can get to it.
129129
if (isa<FunctionRefBaseInst>(call.getCallee()) ||
130130
isa<MethodInst>(call.getCallee())) {
131-
variableNamePath.push_back(call.getCallee()->getDefiningInstruction());
132-
// Try to name the base of the property if this is a method.
133131
if (call.getSubstCalleeType()->hasSelfParam()) {
132+
variableNamePath.push_back(call.getCallee()->getDefiningInstruction());
134133
return call.getSelfArgument();
135134
}
136135

137136
return SILValue();
138137
}
139-
return {};
138+
139+
return SILValue();
140140
};
141141

142142
// Read or modify accessor.
143143
if (auto bai = dyn_cast_or_null<BeginApplyInst>(
144144
searchValue->getDefiningInstruction())) {
145145
if (auto selfParam = getNamePathComponentFromCallee(bai)) {
146-
searchValue = *selfParam;
146+
searchValue = selfParam;
147147
continue;
148148
}
149149
}
150150

151+
if (options.contains(Flag::InferSelfThroughAllAccessors)) {
152+
if (auto fas =
153+
FullApplySite::isa(searchValue->getDefiningInstruction())) {
154+
if (auto selfParam = getNamePathComponentFromCallee(fas)) {
155+
searchValue = selfParam;
156+
continue;
157+
}
158+
}
159+
}
160+
151161
// Addressor accessor.
152162
if (auto ptrToAddr =
153163
dyn_cast<PointerToAddressInst>(stripAccessMarkers(searchValue))) {
@@ -164,7 +174,7 @@ VariableNameInferrer::findDebugInfoProvidingValue(SILValue searchValue) {
164174
if (addressorInvocation) {
165175
if (auto selfParam =
166176
getNamePathComponentFromCallee(addressorInvocation)) {
167-
searchValue = *selfParam;
177+
searchValue = selfParam;
168178
continue;
169179
}
170180
}
@@ -263,7 +273,9 @@ static FunctionTest VariableNameInferrerTests(
263273
"variable-name-inference", [](auto &function, auto &arguments, auto &test) {
264274
auto value = arguments.takeValue();
265275
SmallString<64> finalString;
266-
VariableNameInferrer inferrer(&function, finalString);
276+
VariableNameInferrer::Options options;
277+
options |= VariableNameInferrer::Flag::InferSelfThroughAllAccessors;
278+
VariableNameInferrer inferrer(&function, options, finalString);
267279
SILValue rootValue =
268280
inferrer.inferByWalkingUsesToDefsReturningRoot(value);
269281
llvm::outs() << "Input Value: " << *value;

test/SILOptimizer/variable_name_inference.sil

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,29 @@
1-
// RUN: %target-sil-opt -test-runner %s 2>&1 | %FileCheck %s
1+
// RUN: %target-sil-opt -module-name infer -test-runner %s 2>&1 | %FileCheck %s
22

33
import Builtin
44

5-
class Klass {}
5+
//===----------------------------------------------------------------------===//
6+
// MARK: Declarations
7+
//===----------------------------------------------------------------------===//
8+
9+
class Klass {
10+
}
11+
12+
class ContainsKlass {
13+
var computedKlass: Klass { get }
14+
final var klass: Klass
15+
16+
init()
17+
}
618

719
sil @getKlass : $@convention(thin) () -> @owned Klass
20+
sil @getContainsKlass : $@convention(thin) () -> @owned ContainsKlass
821
sil @useIndirect : $@convention(thin) <T> (@in_guaranteed T) -> ()
922

23+
//===----------------------------------------------------------------------===//
24+
// MARK: Tests
25+
//===----------------------------------------------------------------------===//
26+
1027
// CHECK-LABEL: begin running test {{[0-9]+}} of {{[0-9]+}} on simple_test_case: variable-name-inference with: @trace[0]
1128
// CHECK: Input Value: %1 = apply %0() : $@convention(thin) () -> @owned Klass
1229
// CHECK: Name: 'MyName'
@@ -74,3 +91,25 @@ bb0:
7491
%9999 = tuple ()
7592
return %9999 : $()
7693
}
94+
95+
// CHECK-LABEL: begin running test {{.*}} of {{.*}} on look_through_accessors_get: variable-name-inference with: @trace[0]
96+
// CHECK: Name: 'myName.computedKlass'
97+
// CHECK: Root: %2 = move_value [lexical] [var_decl]
98+
// CHECK: end running test {{.*}} of {{.*}} on look_through_accessors_get: variable-name-inference with: @trace[0]
99+
sil [ossa] @look_through_accessors_get : $@convention(thin) () -> () {
100+
bb0:
101+
specify_test "variable-name-inference @trace[0]"
102+
%0 = function_ref @getContainsKlass : $@convention(thin) () -> @owned ContainsKlass
103+
%1 = apply %0() : $@convention(thin) () -> @owned ContainsKlass
104+
%3 = move_value [lexical] [var_decl] %1 : $ContainsKlass
105+
debug_value %3 : $ContainsKlass, let, name "myName"
106+
%5 = begin_borrow %3 : $ContainsKlass
107+
%6 = class_method %5 : $ContainsKlass, #ContainsKlass.computedKlass!getter : (ContainsKlass) -> () -> Klass, $@convention(method) (@guaranteed ContainsKlass) -> @owned Klass
108+
%7 = apply %6(%5) : $@convention(method) (@guaranteed ContainsKlass) -> @owned Klass
109+
debug_value [trace] %7 : $Klass
110+
end_borrow %5 : $ContainsKlass
111+
destroy_value %7 : $Klass
112+
destroy_value %3 : $ContainsKlass
113+
%13 = tuple ()
114+
return %13 : $()
115+
}

0 commit comments

Comments
 (0)