Skip to content

Commit 98856b1

Browse files
Merge pull request #63170 from nate-chandler/rdar104021173
[Test] Print helpful info on malformed unit test.
2 parents 62bc0ea + d563f9f commit 98856b1

File tree

5 files changed

+94
-20
lines changed

5 files changed

+94
-20
lines changed

include/swift/SILOptimizer/Utils/ParseTestSpecification.h

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,12 @@
1919

2020
#include "swift/Basic/TaggedUnion.h"
2121
#include "swift/SIL/SILArgument.h"
22+
#include "swift/SIL/SILBasicBlock.h"
23+
#include "swift/SIL/SILFunction.h"
2224
#include "swift/SIL/SILInstruction.h"
2325
#include "swift/SIL/SILValue.h"
2426
#include "llvm/ADT/StringRef.h"
27+
#include "llvm/Support/FormattedStream.h"
2528

2629
namespace llvm {
2730
template <class T>
@@ -68,10 +71,15 @@ struct Argument {
6871

6972
public:
7073
Kind getKind() const { return kind; }
74+
void print(llvm::raw_ostream &os);
75+
#ifndef NDEBUG
76+
void dump() { print(llvm::errs()); }
77+
#endif
7178
};
7279

73-
template <typename Stored, Argument::Kind TheKind>
80+
template <typename _Stored, Argument::Kind TheKind>
7481
struct ConcreteArgument : Argument {
82+
using Stored = _Stored;
7583
using Super = ConcreteArgument<Stored, TheKind>;
7684
ConcreteArgument(Stored stored) : Argument(Union{stored}, TheKind) {}
7785
Stored getValue() { return storage.get<Stored>(); }
@@ -142,13 +150,28 @@ struct Arguments {
142150
Arguments &operator=(Arguments const &) = delete;
143151
~Arguments() { assertUsed(); }
144152
Argument &takeArgument() { return storage[untakenIndex++]; }
145-
StringRef takeString() {
146-
return cast<StringArgument>(takeArgument()).getValue();
153+
154+
private:
155+
template <typename Subtype>
156+
typename Subtype::Stored getInstance(StringRef name, Argument &argument) {
157+
if (isa<Subtype>(argument)) {
158+
auto string = cast<Subtype>(argument).getValue();
159+
return string;
160+
}
161+
llvm::errs() << "Attempting to take a " << name << " argument but have\n";
162+
argument.print(llvm::errs());
163+
llvm::report_fatal_error("Bad unit test");
147164
}
148-
bool takeBool() { return cast<BoolArgument>(takeArgument()).getValue(); }
149-
unsigned long long takeUInt() {
150-
return cast<UIntArgument>(takeArgument()).getValue();
165+
template <typename Subtype>
166+
typename Subtype::Stored takeInstance(StringRef name) {
167+
auto argument = takeArgument();
168+
return getInstance<Subtype>(name, argument);
151169
}
170+
171+
public:
172+
StringRef takeString() { return takeInstance<StringArgument>("string"); }
173+
bool takeBool() { return takeInstance<BoolArgument>("bool"); }
174+
unsigned long long takeUInt() { return takeInstance<UIntArgument>("uint"); }
152175
SILValue takeValue() {
153176
auto argument = takeArgument();
154177
if (isa<InstructionArgument>(argument)) {
@@ -159,22 +182,18 @@ struct Arguments {
159182
auto *arg = cast<BlockArgumentArgument>(argument).getValue();
160183
return arg;
161184
}
162-
return cast<ValueArgument>(argument).getValue();
163-
}
164-
Operand *takeOperand() {
165-
return cast<OperandArgument>(takeArgument()).getValue();
185+
return getInstance<ValueArgument>("value", argument);
166186
}
187+
Operand *takeOperand() { return takeInstance<OperandArgument>("operand"); }
167188
SILInstruction *takeInstruction() {
168-
return cast<InstructionArgument>(takeArgument()).getValue();
189+
return takeInstance<InstructionArgument>("instruction");
169190
}
170191
SILArgument *takeBlockArgument() {
171-
return cast<BlockArgumentArgument>(takeArgument()).getValue();
172-
}
173-
SILBasicBlock *takeBlock() {
174-
return cast<BlockArgument>(takeArgument()).getValue();
192+
return takeInstance<BlockArgumentArgument>("block argument");
175193
}
194+
SILBasicBlock *takeBlock() { return takeInstance<BlockArgument>("block"); }
176195
SILFunction *takeFunction() {
177-
return cast<FunctionArgument>(takeArgument()).getValue();
196+
return takeInstance<FunctionArgument>("block");
178197
}
179198
};
180199

lib/SILOptimizer/UtilityPasses/ParseTestSpecification.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,46 @@ SILValue ParseArgumentSpecification::getTraceValue(unsigned index,
584584

585585
} // anonymous namespace
586586

587+
// Member function implementations
588+
589+
void Argument::print(llvm::raw_ostream &os) {
590+
switch (kind) {
591+
case Kind::Value:
592+
llvm::errs() << "value:\n";
593+
cast<ValueArgument>(*this).getValue()->print(os);
594+
break;
595+
case Kind::Operand:
596+
os << "operand:\n";
597+
cast<OperandArgument>(*this).getValue()->print(os);
598+
break;
599+
case Kind::Instruction:
600+
os << "instruction:\n";
601+
cast<InstructionArgument>(*this).getValue()->print(os);
602+
break;
603+
case Kind::BlockArgument:
604+
os << "block argument:\n";
605+
cast<BlockArgumentArgument>(*this).getValue()->print(os);
606+
break;
607+
case Kind::Block:
608+
os << "block:\n";
609+
cast<BlockArgument>(*this).getValue()->print(os);
610+
break;
611+
case Kind::Function:
612+
os << "function:\n";
613+
cast<FunctionArgument>(*this).getValue()->print(os);
614+
break;
615+
case Kind::Bool:
616+
os << "bool: " << cast<BoolArgument>(*this).getValue() << "\n";
617+
break;
618+
case Kind::UInt:
619+
os << "uint: " << cast<UIntArgument>(*this).getValue() << "\n";
620+
break;
621+
case Kind::String:
622+
os << "string: " << cast<StringArgument>(*this).getValue() << "\n";
623+
break;
624+
}
625+
}
626+
587627
// API
588628

589629
void swift::test::getTestSpecifications(

lib/SILOptimizer/UtilityPasses/UnitTestRunner.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -501,9 +501,11 @@ struct SimplifyCFGSimplifyArgument : UnitTest {
501501
auto *passToRun = cast<SILFunctionTransform>(createSimplifyCFG());
502502
passToRun->injectPassManager(getPass()->getPassManager());
503503
passToRun->injectFunction(getFunction());
504+
auto *block = arguments.takeBlock();
505+
auto index = arguments.takeUInt();
504506
SimplifyCFG(*getFunction(), *passToRun, /*VerifyAll=*/false,
505507
/*EnableJumpThread=*/false)
506-
.simplifyArgument(arguments.takeBlock(), arguments.takeUInt());
508+
.simplifyArgument(block, index);
507509
}
508510
};
509511

test/SILOptimizer/simplify_cfg_ossa_bbargs.sil

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
// RUN: %target-sil-opt -unit-test-runner %s | %FileCheck %s
22

3-
// Test fails in Windows CI - rdar://104021173
4-
// UNSUPPORTED: OS=windows-msvc
5-
63
import Builtin
74

85
class Klass {}

test/SILOptimizer/unit_test.sil

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,3 +205,19 @@ exit(%owned : @owned $C, %guaranteed_1 : @guaranteed $C, %guaranteed_2 : @guaran
205205
%retval = tuple ()
206206
return %retval : $()
207207
}
208+
209+
// CHECK-LABEL: begin running test 1 of {{[^,]+}} on test_arg_parsing_2
210+
// CHECK: block:
211+
// CHECK: {{bb[0-9]+}}:
212+
// CHECK: {{%[^,]+}} = tuple ()
213+
// CHECK: return {{%[^,]+}}
214+
// CHECK: uint: 0
215+
// CHECK-LABEL: end running test 1 of {{[^,]+}} on test_arg_parsing_2
216+
sil @test_arg_parsing_2 : $() -> () {
217+
entry:
218+
test_specification "test-specification-parsing Bu @block[1] 0"
219+
br exit
220+
exit:
221+
%retval = tuple ()
222+
return %retval : $()
223+
}

0 commit comments

Comments
 (0)