|
| 1 | +//--------------------------------------------------------------------*- C++ -// |
| 2 | +// clad - the C++ Clang-based Automatic Differentiator |
| 3 | +// |
| 4 | +// See ASTIntegrity.h for the rationale. |
| 5 | +//----------------------------------------------------------------------------// |
| 6 | + |
| 7 | +#include "ASTIntegrity.h" |
| 8 | + |
| 9 | +#include "clang/AST/Expr.h" |
| 10 | +#include "clang/AST/ExprCXX.h" |
| 11 | +#include "clang/AST/RecursiveASTVisitor.h" |
| 12 | +#include "clang/AST/Stmt.h" |
| 13 | +#include "clang/AST/StmtOpenMP.h" |
| 14 | + |
| 15 | +#include "llvm/ADT/DenseMap.h" |
| 16 | +#include "llvm/ADT/SmallPtrSet.h" |
| 17 | + |
| 18 | +using namespace clang; |
| 19 | + |
| 20 | +namespace clad { |
| 21 | + |
| 22 | +const Stmt* findSharedNode(const Stmt* Root) { |
| 23 | + if (!Root) |
| 24 | + return nullptr; |
| 25 | + // clad's hand-built body must be a tree in its Stmt CHILD-edge structure: no |
| 26 | + // node is a child (Stmt::children()) of two parents. Count child edges, NOT |
| 27 | + // RAV visits -- RecursiveASTVisitor also follows type and template-argument |
| 28 | + // edges, and Clang legitimately shares nodes across those (e.g. the |
| 29 | + // substituted `N` in a `Foo<5>` instantiation is one ConstantExpr reachable |
| 30 | + // from both the type and the body). Counting visits would flag those valid |
| 31 | + // shares; counting child edges flags only genuine multiple-parent reuse, |
| 32 | + // which is what clad must not produce. |
| 33 | + struct Counter : RecursiveASTVisitor<Counter> { |
| 34 | + llvm::DenseMap<const Stmt*, unsigned> ParentEdges; |
| 35 | + llvm::SmallPtrSet<const Stmt*, 32> SeenParents; |
| 36 | + // Visit implicit code: some genuine reuses have a second parent reachable |
| 37 | + // only through implicit nodes. |
| 38 | + bool shouldVisitImplicitCode() const { return true; } |
| 39 | + // But visit only the semantic form of an InitListExpr: with implicit code |
| 40 | + // on, RAV visits both the syntactic and semantic forms, which legitimately |
| 41 | + // share their element nodes (Clang's representation, not a reuse). |
| 42 | + bool TraverseInitListExpr(InitListExpr* ILE, |
| 43 | + DataRecursionQueue* = nullptr) { |
| 44 | + InitListExpr* Sem = ILE->isSemanticForm() ? ILE : ILE->getSemanticForm(); |
| 45 | + if (!Sem) |
| 46 | + Sem = ILE; |
| 47 | + WalkUpFromInitListExpr(Sem); |
| 48 | + for (Stmt* Ch : Sem->children()) |
| 49 | + if (Ch) |
| 50 | + TraverseStmt(Ch); |
| 51 | + return true; |
| 52 | + } |
| 53 | + // Skip OpenMP clauses. Lowering `reduction(+:x)` makes Sema synthesize a |
| 54 | + // combiner (`.reduction.lhs = .reduction.lhs + .reduction.rhs`) that |
| 55 | + // references one helper DeclRefExpr from two parents -- Clang's own AST, |
| 56 | + // not clad reuse. TraverseOMPExecutableDirective walks only the clauses; |
| 57 | + // the associated loop body clad generates is still traversed by the |
| 58 | + // DEF_TRAVERSE_STMT children walk. |
| 59 | + bool TraverseOMPExecutableDirective(OMPExecutableDirective* /*D*/) { |
| 60 | + return true; |
| 61 | + } |
| 62 | + // An OpaqueValueExpr is Clang's mechanism for sharing a common |
| 63 | + // subexpression -- e.g. the `threadIdx` base of a `threadIdx.x` |
| 64 | + // __declspec(property) access, which Clang models as a PseudoObjectExpr |
| 65 | + // that references one OpaqueValueExpr from both its syntactic form and its |
| 66 | + // semantic expressions. It is shared by design, not clad reuse, so do not |
| 67 | + // descend into its source (which the OVE also shares); VisitStmt likewise |
| 68 | + // does not count edges to it. |
| 69 | + bool TraverseOpaqueValueExpr(OpaqueValueExpr* /*OVE*/, |
| 70 | + DataRecursionQueue* = nullptr) { |
| 71 | + return true; |
| 72 | + } |
| 73 | + // Skip default-argument expressions. Clang stores a call's default argument |
| 74 | + // once on the ParmVarDecl and every call site references that one |
| 75 | + // expression through a CXXDefaultArgExpr (e.g. the `0` in |
| 76 | + // thrust::reduce_by_key's default operators, shared across clad's cloned |
| 77 | + // calls). That is Clang's representation, not clad reuse. |
| 78 | + bool TraverseCXXDefaultArgExpr(CXXDefaultArgExpr* /*E*/, |
| 79 | + DataRecursionQueue* = nullptr) { |
| 80 | + return true; |
| 81 | + } |
| 82 | + bool TraverseCXXDefaultInitExpr(CXXDefaultInitExpr* /*E*/, |
| 83 | + DataRecursionQueue* = nullptr) { |
| 84 | + return true; |
| 85 | + } |
| 86 | + // Do not descend into type locations. A generated VarDecl's type can embed |
| 87 | + // expressions (a template argument, an array bound, a `std::enable_if<N < |
| 88 | + // _Dt>` SFINAE condition) that Clang shares across same-typed decls -- e.g. |
| 89 | + // the mersenne engine's `_Dt` word-size constant across the pushforward's |
| 90 | + // ValueAndPushforward<> temporaries. Those are Clang's shared AST, not a |
| 91 | + // clad reuse; only the statement tree is clad's hand-built output. |
| 92 | + // clang-22 added a trailing bool (TraverseQualifier) to these; a defaulted |
| 93 | + // parameter matches both the older one-argument call and the new one. |
| 94 | + bool TraverseTypeLoc(TypeLoc /*TL*/, bool = false) { return true; } |
| 95 | + bool TraverseType(QualType /*T*/, bool = false) { return true; } |
| 96 | + bool TraverseTemplateArgumentLoc(const TemplateArgumentLoc& /*ArgLoc*/) { |
| 97 | + return true; |
| 98 | + } |
| 99 | + bool TraverseTemplateArgument(const TemplateArgument& /*Arg*/) { |
| 100 | + return true; |
| 101 | + } |
| 102 | + bool VisitStmt(Stmt* S) { |
| 103 | + // RAV can visit one node twice when it is embedded in two type locs |
| 104 | + // (e.g. a template-argument ConstantExpr shared by several same-typed |
| 105 | + // VarDecls). Count each parent's children once, so a single parent seen |
| 106 | + // twice is not mistaken for two parents. |
| 107 | + if (!SeenParents.insert(S).second) |
| 108 | + return true; |
| 109 | + for (const Stmt* Ch : S->children()) |
| 110 | + if (Ch && !isa<OpaqueValueExpr>(Ch)) |
| 111 | + ++ParentEdges[Ch]; |
| 112 | + return true; |
| 113 | + } |
| 114 | + } C; |
| 115 | + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast) |
| 116 | + C.TraverseStmt(const_cast<Stmt*>(Root)); |
| 117 | + for (const auto& Entry : C.ParentEdges) |
| 118 | + if (Entry.second >= 2) |
| 119 | + return Entry.first; |
| 120 | + return nullptr; |
| 121 | +} |
| 122 | + |
| 123 | +} // namespace clad |
0 commit comments