File: ConstantEvaluatorTester.cpp

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (120 lines) | stat: -rw-r--r-- 4,028 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
//===-----ConstantEvaluatorTester.cpp - Test Constant Evaluator -----------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

#define DEBUG_TYPE "sil-constant-evaluation-tester"
#include "swift/AST/DiagnosticsSIL.h"
#include "swift/SIL/SILConstants.h"
#include "swift/SILOptimizer/PassManager/Passes.h"
#include "swift/SILOptimizer/PassManager/Transforms.h"
#include "swift/SILOptimizer/Utils/ConstExpr.h"

using namespace swift;

namespace {

template <typename... T, typename... U>
static InFlightDiagnostic diagnose(ASTContext &Context, SourceLoc loc,
                                   Diag<T...> diag, U &&... args) {
  return Context.Diags.diagnose(loc, diag, std::forward<U>(args)...);
}

/// A compiler pass for testing constant evaluator in the step-wise evaluation
/// mode. The pass evaluates SIL functions whose names start with "interpret"
/// and outputs the constant value returned by the function or diagnostics if
/// the evaluation fails.
class ConstantEvaluatorTester : public SILFunctionTransform {

  bool shouldInterpret() {
    auto *fun = getFunction();
    return fun->getName().starts_with("interpret");
  }

  bool shouldSkipInstruction(SILInstruction *inst) {
    auto *applyInst = dyn_cast<ApplyInst>(inst);
    if (!applyInst)
      return false;

    auto *callee = applyInst->getReferencedFunctionOrNull();
    if (!callee)
      return false;

    return callee->getName().starts_with("skip");
  }

  void run() override {
    SILFunction *fun = getFunction();

    if (!shouldInterpret() || fun->empty())
      return;

    llvm::errs() << "@" << fun->getName() << "\n";

    SymbolicValueBumpAllocator allocator;
    ConstExprStepEvaluator stepEvaluator(allocator, fun,
                                         getOptions().AssertConfig);

    for (auto currI = fun->getEntryBlock()->begin();;) {
      auto *inst = &(*currI);

      if (auto *returnInst = dyn_cast<ReturnInst>(inst)) {
        auto returnVal =
            stepEvaluator.lookupConstValue(returnInst->getOperand());

        if (!returnVal) {
          llvm::errs() << "Returns unknown"
                       << "\n";
          break;
        }
        llvm::errs() << "Returns " << returnVal.value() << "\n";
        break;
      }

      std::optional<SILBasicBlock::iterator> nextInstOpt;
      std::optional<SymbolicValue> errorVal;

      // If the instruction is marked as skip, skip it and make its effects
      // non-constant. Otherwise, try evaluating the instruction and if the
      // evaluation fails due to a previously skipped instruction,
      // skip the current instruction.
      if (shouldSkipInstruction(inst)) {
        std::tie(nextInstOpt, errorVal) =
            stepEvaluator.skipByMakingEffectsNonConstant(currI);
      } else {
        std::tie(nextInstOpt, errorVal) =
            stepEvaluator.tryEvaluateOrElseMakeEffectsNonConstant(currI);
      }

      // Diagnose errors in the evaluation. Unknown symbolic values produced
      // by skipping instructions are not considered errors.
      if (errorVal.has_value() &&
          !errorVal->isUnknownDueToUnevaluatedInstructions()) {
        errorVal->emitUnknownDiagnosticNotes(inst->getLoc());
        break;
      }

      if (!nextInstOpt) {
        diagnose(fun->getASTContext(), inst->getLoc().getSourceLoc(),
                 diag::constexpr_unknown_control_flow_due_to_skip);
        errorVal->emitUnknownDiagnosticNotes(inst->getLoc());
        break;
      }

      currI = nextInstOpt.value();
    }
  }
};

} // end anonymous namespace

SILTransform *swift::createConstantEvaluatorTester() {
  return new ConstantEvaluatorTester();
}