Skip to content

Commit 2010579

Browse files
committed
[sil] Add basic test support for variable name inference.
1 parent fef1b5e commit 2010579

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

lib/SILOptimizer/Utils/VariableNameUtils.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
#include "swift/SILOptimizer/Utils/VariableNameUtils.h"
14+
#include "swift/SIL/Test.h"
1415

1516
using namespace swift;
1617

@@ -191,3 +192,30 @@ void VariableNameInferrer::drainVariableNamePath() {
191192
resultingString += '.';
192193
}
193194
}
195+
196+
//===----------------------------------------------------------------------===//
197+
// MARK: Tests
198+
//===----------------------------------------------------------------------===//
199+
200+
namespace swift::test {
201+
202+
// Arguments:
203+
// - SILValue: value to emit a name for.
204+
// Dumps:
205+
// - The inferred name
206+
// - The inferred value.
207+
static FunctionTest VariableNameInferrerTests(
208+
"variable-name-inference", [](auto &function, auto &arguments, auto &test) {
209+
auto value = arguments.takeValue();
210+
SmallString<64> finalString;
211+
VariableNameInferrer inferrer(&function, finalString);
212+
SILValue rootValue =
213+
inferrer.inferByWalkingUsesToDefsReturningRoot(value);
214+
llvm::outs() << "Input Value: " << *value;
215+
if (!rootValue) {
216+
llvm::outs() << "Name: 'unknown'\nRoot: 'unknown'\n";
217+
return;
218+
}
219+
llvm::outs() << "Name: '" << finalString << "'\nRoot: " << rootValue;
220+
});
221+
} // namespace swift::test
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// RUN: %target-sil-opt -test-runner %s 2>&1 | %FileCheck %s
2+
3+
import Builtin
4+
5+
class Klass {}
6+
7+
sil @getKlass : $@convention(thin) () -> @owned Klass
8+
sil @useIndirect : $@convention(thin) <T> (@in_guaranteed T) -> ()
9+
10+
// CHECK-LABEL: begin running test {{[0-9]+}} of {{[0-9]+}} on simple_test_case: variable-name-inference with: @trace[0]
11+
// CHECK: Input Value: %1 = apply %0() : $@convention(thin) () -> @owned Klass
12+
// CHECK: Name: 'MyName'
13+
// CHECK: Root: %1 = apply %0() : $@convention(thin) () -> @owned Klass
14+
// CHECK: end running test {{[0-9]+}} of {{[0-9]+}} on simple_test_case: variable-name-inference with: @trace[0]
15+
sil [ossa] @simple_test_case : $@convention(thin) () -> () {
16+
bb0:
17+
specify_test "variable-name-inference @trace[0]"
18+
%0 = function_ref @getKlass : $@convention(thin) () -> @owned Klass
19+
%1 = apply %0() : $@convention(thin) () -> @owned Klass
20+
debug_value [trace] %1 : $Klass
21+
debug_value %1 : $Klass, let, name "MyName"
22+
destroy_value %1 : $Klass
23+
%9999 = tuple ()
24+
return %9999 : $()
25+
}
26+
27+
// CHECK-LABEL: begin running test {{[0-9]+}} of {{[0-9]+}} on temporary_init_with_copy_addr: variable-name-inference with: @trace[0]
28+
// CHECK: Input Value: %4 = alloc_stack $Klass
29+
// CHECK: Name: 'MyName'
30+
// CHECK: Root: %2 = alloc_stack $Klass, var, name "MyName"
31+
// CHECK: end running test {{[0-9]+}} of {{[0-9]+}} on temporary_init_with_copy_addr: variable-name-inference with: @trace[0]
32+
sil [ossa] @temporary_init_with_copy_addr : $@convention(thin) () -> () {
33+
bb0:
34+
specify_test "variable-name-inference @trace[0]"
35+
%0 = function_ref @getKlass : $@convention(thin) () -> @owned Klass
36+
%1 = apply %0() : $@convention(thin) () -> @owned Klass
37+
%2 = alloc_stack $Klass, name "MyName"
38+
store %1 to [init] %2 : $*Klass
39+
40+
%temp = alloc_stack $Klass
41+
copy_addr %2 to [init] %temp : $*Klass
42+
debug_value [trace] %temp : $*Klass
43+
%use = function_ref @useIndirect : $@convention(thin) <T> (@in_guaranteed T) -> ()
44+
apply %use<Klass>(%temp) : $@convention(thin) <T> (@in_guaranteed T) -> ()
45+
46+
destroy_addr %temp : $*Klass
47+
dealloc_stack %temp : $*Klass
48+
destroy_addr %2 : $*Klass
49+
dealloc_stack %2 : $*Klass
50+
%9999 = tuple ()
51+
return %9999 : $()
52+
}

0 commit comments

Comments
 (0)