|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +#include "CaptureAnalysis.h" |
| 10 | +#include "slang/ast/ASTVisitor.h" |
| 11 | +#include "llvm/ADT/MapVector.h" |
| 12 | +#include "llvm/Support/SaveAndRestore.h" |
| 13 | + |
| 14 | +using namespace slang::ast; |
| 15 | +using namespace circt; |
| 16 | +using namespace circt::ImportVerilog; |
| 17 | + |
| 18 | +/// Check whether `var` is local to `func`. Walk up from the variable's parent |
| 19 | +/// scope; if we reach `func` before hitting another function boundary, the |
| 20 | +/// variable is local. |
| 21 | +static bool isLocalToFunction(const ValueSymbol &var, |
| 22 | + const SubroutineSymbol &func) { |
| 23 | + for (const Scope *scope = var.getParentScope(); scope; |
| 24 | + scope = scope->asSymbol().getParentScope()) { |
| 25 | + if (&scope->asSymbol() == &func) |
| 26 | + return true; |
| 27 | + if (scope->asSymbol().kind == SymbolKind::Subroutine) |
| 28 | + return false; |
| 29 | + } |
| 30 | + return false; |
| 31 | +} |
| 32 | + |
| 33 | +/// Check whether `var` is a global variable. Walk up from the variable's parent |
| 34 | +/// scope; if we hit a function or instance body, it's not global. Otherwise |
| 35 | +/// (package, compilation unit, root) it is. |
| 36 | +static bool isGlobalVariable(const ValueSymbol &var) { |
| 37 | + for (const Scope *scope = var.getParentScope(); scope; |
| 38 | + scope = scope->asSymbol().getParentScope()) { |
| 39 | + switch (scope->asSymbol().kind) { |
| 40 | + case SymbolKind::Subroutine: |
| 41 | + case SymbolKind::InstanceBody: |
| 42 | + return false; |
| 43 | + default: |
| 44 | + break; |
| 45 | + } |
| 46 | + } |
| 47 | + return true; |
| 48 | +} |
| 49 | + |
| 50 | +namespace { |
| 51 | + |
| 52 | +/// Walk the entire AST to collect captured variables and the call graph for |
| 53 | +/// each function. Uses slang's `ASTVisitor` with both statement and expression |
| 54 | +/// visiting enabled so that we recurse into all function bodies. |
| 55 | +struct CaptureWalker |
| 56 | + : public ASTVisitor<CaptureWalker, /*VisitStatements=*/true, |
| 57 | + /*VisitExpressions=*/true> { |
| 58 | + |
| 59 | + /// The function whose body we are currently inside, or nullptr if we are at |
| 60 | + /// a scope outside any function. |
| 61 | + const SubroutineSymbol *currentFunc = nullptr; |
| 62 | + |
| 63 | + /// Captured variables per function. |
| 64 | + CaptureMap capturedVars; |
| 65 | + |
| 66 | + /// Inverse call graph: maps each callee to the set of callers that call it. |
| 67 | + /// Used to propagate captures from callees to their callers. Uses MapVector |
| 68 | + /// for deterministic iteration order during propagation. |
| 69 | + MapVector<const SubroutineSymbol *, |
| 70 | + SmallSetVector<const SubroutineSymbol *, 4>> |
| 71 | + callers; |
| 72 | + |
| 73 | + /// When we enter a function body, record it as the current function and |
| 74 | + /// recurse into its members and body statements. |
| 75 | + void handle(const SubroutineSymbol &func) { |
| 76 | + llvm::SaveAndRestore guard(currentFunc, &func); |
| 77 | + visitDefault(func); |
| 78 | + } |
| 79 | + |
| 80 | + /// When we see a named value reference inside a function, check if it needs |
| 81 | + /// to be captured. |
| 82 | + void handle(const NamedValueExpression &expr) { |
| 83 | + if (!currentFunc) |
| 84 | + return; |
| 85 | + |
| 86 | + auto &var = expr.symbol; |
| 87 | + |
| 88 | + // Class properties are accessed through `this`, not captured. |
| 89 | + if (var.kind == SymbolKind::ClassProperty) |
| 90 | + return; |
| 91 | + |
| 92 | + // Function arguments are local by definition. |
| 93 | + if (var.kind == SymbolKind::FormalArgument) |
| 94 | + return; |
| 95 | + |
| 96 | + // Only capture variables that are non-local and non-global. |
| 97 | + if (isLocalToFunction(var, *currentFunc) || isGlobalVariable(var)) |
| 98 | + return; |
| 99 | + |
| 100 | + capturedVars[currentFunc].insert(&var); |
| 101 | + } |
| 102 | + |
| 103 | + /// Record call graph edges when we see a function call. |
| 104 | + void handle(const CallExpression &expr) { |
| 105 | + if (currentFunc) |
| 106 | + if (auto *const *callee = |
| 107 | + std::get_if<const SubroutineSymbol *>(&expr.subroutine)) |
| 108 | + callers[*callee].insert(currentFunc); |
| 109 | + visitDefault(expr); |
| 110 | + } |
| 111 | + |
| 112 | + /// Propagate captures transitively through the call graph. For each callee |
| 113 | + /// that has captures, push each captured variable upward through all |
| 114 | + /// transitive callers using a worklist. A captured variable is only |
| 115 | + /// propagated to a caller if it is not local to that caller. |
| 116 | + void propagateCaptures() { |
| 117 | + using WorkItem = std::pair<const SubroutineSymbol *, const ValueSymbol *>; |
| 118 | + SmallSetVector<WorkItem, 16> worklist; |
| 119 | + |
| 120 | + for (auto &[func, _] : callers) { |
| 121 | + // Check if this function captures any variables. Nothing to do if it |
| 122 | + // doesn't. |
| 123 | + auto it = capturedVars.find(func); |
| 124 | + if (it == capturedVars.end()) |
| 125 | + continue; |
| 126 | + |
| 127 | + // Prime the worklist with the captured variables. |
| 128 | + for (auto *var : it->second) |
| 129 | + worklist.insert({func, var}); |
| 130 | + |
| 131 | + // Push each captured variables to the func's callers transitively. |
| 132 | + while (!worklist.empty()) { |
| 133 | + auto [func, cap] = worklist.pop_back_val(); |
| 134 | + auto callersIt = callers.find(func); |
| 135 | + if (callersIt == callers.end()) |
| 136 | + continue; |
| 137 | + for (auto *caller : callersIt->second) |
| 138 | + if (!isLocalToFunction(*cap, *caller)) |
| 139 | + if (capturedVars[caller].insert(cap)) |
| 140 | + worklist.insert({caller, cap}); |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | +}; |
| 145 | + |
| 146 | +} // namespace |
| 147 | + |
| 148 | +CaptureMap ImportVerilog::analyzeFunctionCaptures(const RootSymbol &root) { |
| 149 | + CaptureWalker walker; |
| 150 | + root.visit(walker); |
| 151 | + walker.propagateCaptures(); |
| 152 | + return std::move(walker.capturedVars); |
| 153 | +} |
0 commit comments