|
| 1 | +//===--ConstantEvaluableSubsetChecker.cpp - Test Constant Evaluable Swift--===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +// This file implements a pass for checking the constant evaluability of Swift |
| 14 | +// code snippets. This pass is only used in tests and is not part of the |
| 15 | +// compilation pipeline. |
| 16 | + |
| 17 | +#define DEBUG_TYPE "sil-constant-evaluable-subset-checker" |
| 18 | +#include "swift/AST/DiagnosticsSIL.h" |
| 19 | +#include "swift/AST/Module.h" |
| 20 | +#include "swift/Demangling/Demangle.h" |
| 21 | +#include "swift/SIL/CFG.h" |
| 22 | +#include "swift/SIL/SILConstants.h" |
| 23 | +#include "swift/SIL/SILInstruction.h" |
| 24 | +#include "swift/SILOptimizer/PassManager/Passes.h" |
| 25 | +#include "swift/SILOptimizer/PassManager/Transforms.h" |
| 26 | +#include "swift/SILOptimizer/Utils/ConstExpr.h" |
| 27 | + |
| 28 | +using namespace swift; |
| 29 | + |
| 30 | +namespace { |
| 31 | + |
| 32 | +static const StringRef constantEvaluableSemanticsAttr = "constant_evaluable"; |
| 33 | +static const StringRef testDriverSemanticsAttr = "test_driver"; |
| 34 | + |
| 35 | +template <typename... T, typename... U> |
| 36 | +static InFlightDiagnostic diagnose(ASTContext &Context, SourceLoc loc, |
| 37 | + Diag<T...> diag, U &&... args) { |
| 38 | + return Context.Diags.diagnose(loc, diag, std::forward<U>(args)...); |
| 39 | +} |
| 40 | + |
| 41 | +static std::string demangleSymbolName(StringRef name) { |
| 42 | + Demangle::DemangleOptions options; |
| 43 | + options.QualifyEntities = false; |
| 44 | + return Demangle::demangleSymbolAsString(name, options); |
| 45 | +} |
| 46 | + |
| 47 | +/// A SILModule pass that invokes the constant evaluator on all functions in a |
| 48 | +/// SILModule with the semantics attribute "test_driver". Each "test_driver" |
| 49 | +/// must invoke one or more functions in the module annotated as |
| 50 | +/// "constant_evaluable" with constant arguments. |
| 51 | +class ConstantEvaluableSubsetChecker : public SILModuleTransform { |
| 52 | + |
| 53 | + llvm::SmallPtrSet<SILFunction *, 4> constantEvaluableFunctions; |
| 54 | + llvm::SmallPtrSet<SILFunction *, 4> evaluatedFunctions; |
| 55 | + |
| 56 | + /// Evaluate the body of \c fun with the constant evaluator. \c fun must be |
| 57 | + /// annotated as "test_driver" and must invoke one or more functions annotated |
| 58 | + /// as "constant_evaluable" with constant arguments. Emit diagnostics if the |
| 59 | + /// evaluation of any "constant_evaluable" function called in the body of |
| 60 | + /// \c fun fails. |
| 61 | + void constantEvaluateDriver(SILFunction *fun) { |
| 62 | + ASTContext &astContext = fun->getASTContext(); |
| 63 | + |
| 64 | + // Create a step evaluator and run it on the function. |
| 65 | + SymbolicValueBumpAllocator allocator; |
| 66 | + ConstExprStepEvaluator stepEvaluator(allocator, fun, |
| 67 | + /*trackCallees*/ true); |
| 68 | + |
| 69 | + for (auto currI = fun->getEntryBlock()->begin();;) { |
| 70 | + auto *inst = &(*currI); |
| 71 | + |
| 72 | + if (isa<ReturnInst>(inst)) |
| 73 | + break; |
| 74 | + |
| 75 | + auto *applyInst = dyn_cast<ApplyInst>(inst); |
| 76 | + SILFunction *callee = nullptr; |
| 77 | + if (applyInst) { |
| 78 | + callee = applyInst->getReferencedFunctionOrNull(); |
| 79 | + } |
| 80 | + |
| 81 | + Optional<SILBasicBlock::iterator> nextInstOpt; |
| 82 | + Optional<SymbolicValue> errorVal; |
| 83 | + |
| 84 | + if (!applyInst || !callee || |
| 85 | + !callee->hasSemanticsAttr(constantEvaluableSemanticsAttr)) { |
| 86 | + std::tie(nextInstOpt, errorVal) = |
| 87 | + stepEvaluator.tryEvaluateOrElseMakeEffectsNonConstant(currI); |
| 88 | + assert(nextInstOpt && "non-constant control flow in the test driver"); |
| 89 | + currI = nextInstOpt.getValue(); |
| 90 | + continue; |
| 91 | + } |
| 92 | + |
| 93 | + // Here, a function annotated as "constant_evaluable" is called. |
| 94 | + llvm::errs() << "@" << demangleSymbolName(callee->getName()) << "\n"; |
| 95 | + std::tie(nextInstOpt, errorVal) = |
| 96 | + stepEvaluator.tryEvaluateOrElseMakeEffectsNonConstant(currI); |
| 97 | + |
| 98 | + if (errorVal) { |
| 99 | + SourceLoc instLoc = inst->getLoc().getSourceLoc(); |
| 100 | + diagnose(astContext, instLoc, diag::not_constant_evaluable); |
| 101 | + errorVal->emitUnknownDiagnosticNotes(inst->getLoc()); |
| 102 | + } |
| 103 | + |
| 104 | + if (!nextInstOpt) { |
| 105 | + break; // This instruction should be the end of the test driver. |
| 106 | + } |
| 107 | + currI = nextInstOpt.getValue(); |
| 108 | + } |
| 109 | + |
| 110 | + // Record functions that were called during this evaluation to detect |
| 111 | + // whether the test drivers in the SILModule cover all function annotated |
| 112 | + // as "constant_evaluable". |
| 113 | + for (SILFunction *callee : stepEvaluator.getFuncsCalledDuringEvaluation()) { |
| 114 | + evaluatedFunctions.insert(callee); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + void run() override { |
| 119 | + SILModule *module = getModule(); |
| 120 | + assert(module); |
| 121 | + |
| 122 | + for (SILFunction &fun : *module) { |
| 123 | + // Record functions annotated as constant evaluable. |
| 124 | + if (fun.hasSemanticsAttr(constantEvaluableSemanticsAttr)) { |
| 125 | + constantEvaluableFunctions.insert(&fun); |
| 126 | + continue; |
| 127 | + } |
| 128 | + |
| 129 | + // Evaluate test drivers. |
| 130 | + if (!fun.hasSemanticsAttr(testDriverSemanticsAttr)) |
| 131 | + continue; |
| 132 | + constantEvaluateDriver(&fun); |
| 133 | + } |
| 134 | + |
| 135 | + // Assert that every function annotated as "constant_evaluable" was convered |
| 136 | + // by a test driver. |
| 137 | + bool error = false; |
| 138 | + for (SILFunction *constEvalFun : constantEvaluableFunctions) { |
| 139 | + if (!evaluatedFunctions.count(constEvalFun)) { |
| 140 | + llvm::errs() << "Error: function " |
| 141 | + << demangleSymbolName(constEvalFun->getName()); |
| 142 | + llvm::errs() << " annotated as constant evaluable"; |
| 143 | + llvm::errs() << " does not have a test driver" |
| 144 | + << "\n"; |
| 145 | + error = true; |
| 146 | + } |
| 147 | + } |
| 148 | + assert(!error); |
| 149 | + } |
| 150 | +}; |
| 151 | + |
| 152 | +} // end anonymous namespace |
| 153 | + |
| 154 | +SILTransform *swift::createConstantEvaluableSubsetChecker() { |
| 155 | + return new ConstantEvaluableSubsetChecker(); |
| 156 | +} |
0 commit comments