13
13
#define DEBUG_TYPE " generic-specializer"
14
14
15
15
#include " swift/SILOptimizer/Utils/Generics.h"
16
- #include " swift/AST/GenericEnvironment.h"
17
- #include " swift/AST/TypeMatcher.h"
18
16
#include " swift/AST/DiagnosticEngine.h"
19
17
#include " swift/AST/DiagnosticsSIL.h"
18
+ #include " swift/AST/GenericEnvironment.h"
20
19
#include " swift/AST/SemanticAttrs.h"
21
- #include " swift/Basic/Statistic.h"
22
20
#include " swift/AST/TypeCheckRequests.h"
23
- #include " swift/Serialization/SerializedSILLoader.h"
21
+ #include " swift/AST/TypeMatcher.h"
22
+ #include " swift/Basic/Statistic.h"
23
+ #include " swift/Demangling/ManglingMacros.h"
24
24
#include " swift/SIL/DebugUtils.h"
25
25
#include " swift/SIL/InstructionUtils.h"
26
26
#include " swift/SIL/OptimizationRemark.h"
27
- #include " swift/SILOptimizer/Utils/SILOptFunctionBuilder .h"
27
+ #include " swift/SIL/PrettyStackTrace .h"
28
28
#include " swift/SILOptimizer/Utils/GenericCloner.h"
29
+ #include " swift/SILOptimizer/Utils/SILOptFunctionBuilder.h"
29
30
#include " swift/SILOptimizer/Utils/SpecializationMangler.h"
30
- #include " swift/Demangling/ManglingMacros .h"
31
+ #include " swift/Serialization/SerializedSILLoader .h"
31
32
#include " swift/Strings.h"
32
33
33
34
using namespace swift ;
@@ -58,6 +59,12 @@ llvm::cl::opt<bool> PrintGenericSpecializationLoops(
58
59
llvm::cl::desc(" Print detected infinite generic specialization loops that "
59
60
" were prevented" ));
60
61
62
+ llvm::cl::opt<bool > VerifyFunctionsAfterSpecialization (
63
+ " sil-generic-verify-after-specialization" , llvm::cl::init(false ),
64
+ llvm::cl::desc(
65
+ " Verify functions after they are specialized "
66
+ " 'PrettyStackTraceFunction'-ing the original function if we fail." ));
67
+
61
68
static bool OptimizeGenericSubstitutions = false ;
62
69
63
70
// / Max depth of a type which can be processed by the generic
@@ -1871,6 +1878,15 @@ SILFunction *GenericFuncSpecializer::tryCreateSpecialization() {
1871
1878
SpecializedF->setClassSubclassScope (SubclassScope::NotApplicable);
1872
1879
SpecializedF->setSpecializationInfo (
1873
1880
GenericSpecializationInformation::create (Caller, GenericFunc, Subs));
1881
+
1882
+ if (VerifyFunctionsAfterSpecialization) {
1883
+ PrettyStackTraceSILFunction SILFunctionDumper (
1884
+ llvm::Twine (" Generic function: " ) + GenericFunc->getName () +
1885
+ " . Specialized Function: " + SpecializedF->getName (),
1886
+ GenericFunc);
1887
+ SpecializedF->verify ();
1888
+ }
1889
+
1874
1890
return SpecializedF;
1875
1891
}
1876
1892
@@ -2443,6 +2459,20 @@ void swift::trySpecializeApplyOfGeneric(
2443
2459
<< NV (" FuncType" , OS.str ());
2444
2460
});
2445
2461
2462
+ // Verify our function after we have finished fixing up call sites/etc. Dump
2463
+ // the generic function if there is an assertion failure (or a crash) to make
2464
+ // it easier to debug such problems since the original generic function is
2465
+ // easily at hand.
2466
+ SWIFT_DEFER {
2467
+ if (VerifyFunctionsAfterSpecialization) {
2468
+ PrettyStackTraceSILFunction SILFunctionDumper (
2469
+ llvm::Twine (" Generic function: " ) + RefF->getName () +
2470
+ " . Specialized Function: " + SpecializedF->getName (),
2471
+ RefF);
2472
+ SpecializedF->verify ();
2473
+ }
2474
+ };
2475
+
2446
2476
assert (ReInfo.getSpecializedType ()
2447
2477
== SpecializedF->getLoweredFunctionType () &&
2448
2478
" Previously specialized function does not match expected type." );
@@ -2457,6 +2487,13 @@ void swift::trySpecializeApplyOfGeneric(
2457
2487
SILFunction *Thunk =
2458
2488
ReabstractionThunkGenerator (FuncBuilder, ReInfo, PAI, SpecializedF)
2459
2489
.createThunk ();
2490
+ if (VerifyFunctionsAfterSpecialization) {
2491
+ PrettyStackTraceSILFunction SILFunctionDumper (
2492
+ llvm::Twine (" Thunk For Generic function: " ) + RefF->getName () +
2493
+ " . Specialized Function: " + SpecializedF->getName (),
2494
+ RefF);
2495
+ Thunk->verify ();
2496
+ }
2460
2497
NewFunctions.push_back (Thunk);
2461
2498
SILBuilderWithScope Builder (PAI);
2462
2499
auto *FRI = Builder.createFunctionRef (PAI->getLoc (), Thunk);
0 commit comments