@@ -161,6 +161,11 @@ llvm::cl::opt<bool> SILForceVerifyAll(
161161 llvm::cl::desc(" For all passes, precompute analyses before the pass and "
162162 " verify analyses after the pass" ));
163163
164+ llvm::cl::opt<bool > DisableSwiftVerification (
165+ " disable-swift-verification" , llvm::cl::init(false ),
166+ llvm::cl::desc(" Disable verification which is implemented in the SwiftCompilerSources" ));
167+
168+
164169static llvm::ManagedStatic<std::vector<unsigned >> DebugPassNumbers;
165170
166171namespace {
@@ -611,6 +616,7 @@ void SILPassManager::runPassOnFunction(unsigned TransIdx, SILFunction *F) {
611616 MatchFun))) {
612617 F->verify (getAnalysis<BasicCalleeAnalysis>()->getCalleeCache ());
613618 verifyAnalyses ();
619+ runSwiftFunctionVerification (F);
614620 }
615621
616622 if (SILPrintPassName)
@@ -706,6 +712,7 @@ void SILPassManager::runPassOnFunction(unsigned TransIdx, SILFunction *F) {
706712 (CurrentPassHasInvalidated || SILVerifyWithoutInvalidation)) {
707713 F->verify (getAnalysis<BasicCalleeAnalysis>()->getCalleeCache ());
708714 verifyAnalyses (F);
715+ runSwiftFunctionVerification (F);
709716 } else {
710717 if ((SILVerifyAfterPass.end () != std::find_if (SILVerifyAfterPass.begin (),
711718 SILVerifyAfterPass.end (),
@@ -715,6 +722,7 @@ void SILPassManager::runPassOnFunction(unsigned TransIdx, SILFunction *F) {
715722 MatchFun))) {
716723 F->verify (getAnalysis<BasicCalleeAnalysis>()->getCalleeCache ());
717724 verifyAnalyses ();
725+ runSwiftFunctionVerification (F);
718726 }
719727 }
720728
@@ -821,6 +829,7 @@ void SILPassManager::runModulePass(unsigned TransIdx) {
821829 MatchFun))) {
822830 Mod->verify (getAnalysis<BasicCalleeAnalysis>()->getCalleeCache ());
823831 verifyAnalyses ();
832+ runSwiftModuleVerification ();
824833 }
825834
826835 swiftPassInvocation.startModulePassRun (SMT);
@@ -855,6 +864,7 @@ void SILPassManager::runModulePass(unsigned TransIdx) {
855864 (CurrentPassHasInvalidated || !SILVerifyWithoutInvalidation)) {
856865 Mod->verify (getAnalysis<BasicCalleeAnalysis>()->getCalleeCache ());
857866 verifyAnalyses ();
867+ runSwiftModuleVerification ();
858868 } else {
859869 if ((SILVerifyAfterPass.end () != std::find_if (SILVerifyAfterPass.begin (),
860870 SILVerifyAfterPass.end (),
@@ -864,6 +874,7 @@ void SILPassManager::runModulePass(unsigned TransIdx) {
864874 MatchFun))) {
865875 Mod->verify (getAnalysis<BasicCalleeAnalysis>()->getCalleeCache ());
866876 verifyAnalyses ();
877+ runSwiftModuleVerification ();
867878 }
868879 }
869880}
@@ -1483,6 +1494,26 @@ void SwiftPassInvocation::endTransformFunction() {
14831494 assert (numNodeSetsAllocated == 0 && " Not all NodeSets deallocated" );
14841495}
14851496
1497+ void SwiftPassInvocation::beginVerifyFunction (SILFunction *function) {
1498+ if (transform) {
1499+ assert (this ->function == function);
1500+ } else {
1501+ assert (!this ->function );
1502+ this ->function = function;
1503+ }
1504+ }
1505+
1506+ void SwiftPassInvocation::endVerifyFunction () {
1507+ assert (function);
1508+ if (!transform) {
1509+ assert (changeNotifications == SILAnalysis::InvalidationKind::Nothing &&
1510+ " verifyication must not change the SIL of a function" );
1511+ assert (numBlockSetsAllocated == 0 && " Not all BasicBlockSets deallocated" );
1512+ assert (numNodeSetsAllocated == 0 && " Not all NodeSets deallocated" );
1513+ function = nullptr ;
1514+ }
1515+ }
1516+
14861517SwiftPassInvocation::~SwiftPassInvocation () {}
14871518
14881519// ===----------------------------------------------------------------------===//
@@ -1864,3 +1895,29 @@ void BridgedCloner::clone(BridgedInstruction inst) {
18641895 cloner->cloneInst (inst.unbridged ());
18651896}
18661897
1898+ static BridgedUtilities::VerifyFunctionFn verifyFunctionFunction;
1899+
1900+ void BridgedUtilities::registerVerifier (VerifyFunctionFn verifyFunctionFn) {
1901+ verifyFunctionFunction = verifyFunctionFn;
1902+ }
1903+
1904+ void SILPassManager::runSwiftFunctionVerification (SILFunction *f) {
1905+ if (!verifyFunctionFunction)
1906+ return ;
1907+
1908+ if (f->getModule ().getOptions ().VerifyNone )
1909+ return ;
1910+
1911+ if (DisableSwiftVerification)
1912+ return ;
1913+
1914+ getSwiftPassInvocation ()->beginVerifyFunction (f);
1915+ verifyFunctionFunction ({getSwiftPassInvocation ()}, {f});
1916+ getSwiftPassInvocation ()->endVerifyFunction ();
1917+ }
1918+
1919+ void SILPassManager::runSwiftModuleVerification () {
1920+ for (SILFunction &f : *Mod) {
1921+ runSwiftFunctionVerification (&f);
1922+ }
1923+ }
0 commit comments