Skip to content

Commit 0d7d891

Browse files
committed
[Test] Allow specifying instructions by offset.
Make @Instruction[+1] refer to the instruction after the "current" instruction, @Instruction[-1] the one before, and so on.
1 parent b9a8334 commit 0d7d891

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

docs/SIL.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3958,6 +3958,8 @@ The following types of test arguments are supported:
39583958
@{function}.{trace} <-- the indicated trace in the indicated function
39593959
Example: @function[bar].trace
39603960
- instruction: @instruction <-- the instruction after* the test_specification instruction
3961+
@instruction[+uint] <-- the instruction ``uint`` instructions after* the test_specification instruction
3962+
@instruction[-uint] <-- the instruction ``uint`` instructions before* the test_specification instruction
39613963
@instruction[uint] <-- the instruction at index ``uint``
39623964
@{function}.{instruction} <-- the indicated instruction in the indicated function
39633965
Example: @function[baz].instruction[19]
@@ -3969,7 +3971,7 @@ The following types of test arguments are supported:
39693971
Example: @block[19].instruction[2].operand[3]
39703972
Example: @function[2].instruction.operand
39713973

3972-
* The first instruction that isn't deleted when processing functions for tests.
3974+
* Not counting instructions that are deleted when processing functions for tests.
39733975
The following instructions currently are deleted:
39743976
test_specification
39753977
debug_value [trace]

lib/SILOptimizer/UtilityPasses/ParseTestSpecification.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,40 @@ SILInstruction *getInstruction(SILBasicBlock *block, unsigned long index) {
8484
llvm_unreachable("bad index!?");
8585
}
8686

87+
SILInstruction *getInstructionOffsetFrom(SILInstruction *base, long offset) {
88+
if (offset == 0) {
89+
return base;
90+
}
91+
if (offset > 0) {
92+
Optional<unsigned long> baseIndex;
93+
unsigned long index = 0;
94+
for (auto &block : *base->getFunction()) {
95+
for (auto &other : block) {
96+
if (baseIndex && index == (*baseIndex + offset)) {
97+
return &other;
98+
}
99+
if (&other == base) {
100+
baseIndex = index;
101+
}
102+
++index;
103+
}
104+
}
105+
llvm_unreachable("positive offset outside of function!?");
106+
}
107+
SmallVector<SILInstruction *, 64> instructions;
108+
unsigned long index = 0;
109+
for (auto &block : *base->getFunction()) {
110+
for (auto &other : block) {
111+
instructions.push_back(&other);
112+
if (&other == base) {
113+
return instructions[index + offset];
114+
}
115+
++index;
116+
}
117+
}
118+
llvm_unreachable("never found instruction in its own function!?");
119+
}
120+
87121
SILBasicBlock *getBlock(SILFunction *function, unsigned long index) {
88122
auto iterator = function->begin();
89123
for (unsigned long i = 0; i < index; ++i) {

test/SILOptimizer/unit_test.sil

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,14 @@ bb1:
9898
// CHECK: tuple
9999
// CHECK-LABEL: end running test 2 of {{[^,]+}} on test_contextual_arg_parsing: test-specification-parsing
100100
// CHECK-LABEL: begin running test 3 of {{[^,]+}} on test_contextual_arg_parsing: test-specification-parsing
101-
// CHECK: function: test_contextual_arg_parsing
101+
// CHECK: instruction: br bb2
102+
// CHECK: instruction: {{%[^,]+}} = tuple ()
103+
// CHECK: instruction: br bb3
104+
// CHECK: instruction: return
102105
// CHECK-LABEL: end running test 3 of {{[^,]+}} on test_contextual_arg_parsing: test-specification-parsing
106+
// CHECK-LABEL: begin running test 4 of {{[^,]+}} on test_contextual_arg_parsing: test-specification-parsing
107+
// CHECK: function: test_contextual_arg_parsing
108+
// CHECK-LABEL: end running test 4 of {{[^,]+}} on test_contextual_arg_parsing: test-specification-parsing
103109
sil [ossa] @test_contextual_arg_parsing : $() -> () {
104110
entry:
105111
br one
@@ -108,6 +114,7 @@ one:
108114
br two
109115
two:
110116
test_specification "test-specification-parsing I @instruction"
117+
test_specification "test-specification-parsing IIII @instruction[-1] @instruction[+0] @instruction[+1] @instruction[+2]"
111118
%retval = tuple ()
112119
br exit
113120
exit:

0 commit comments

Comments
 (0)