tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

CompilationDependencyTracker.h (2982B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
      2 * vim: set ts=8 sts=2 et sw=2 tw=80:
      3 * This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this
      5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 #ifndef jit_CompilationDependencyTracker_h
      8 #define jit_CompilationDependencyTracker_h
      9 
     10 #include "jstypes.h"
     11 #include "NamespaceImports.h"
     12 
     13 #include "ds/InlineTable.h"
     14 #include "jit/JitAllocPolicy.h"
     15 
     16 struct JSContext;
     17 
     18 namespace js::jit {
     19 
     20 class IonScriptKey;
     21 class MIRGenerator;
     22 
     23 struct CompilationDependency : public TempObject {
     24  enum class Type {
     25    GetIterator,
     26    ArraySpecies,
     27    TypedArraySpecies,
     28    RegExpPrototype,
     29    EmulatesUndefined,
     30    ArrayExceedsInt32Length,
     31    ObjectFuseProperty,
     32    DefaultCaseMapping,
     33    Limit
     34  };
     35 
     36  Type type;
     37 
     38  CompilationDependency(Type type) : type(type) {}
     39 
     40  virtual HashNumber hash() const = 0;
     41  virtual bool operator==(const CompilationDependency& other) const = 0;
     42 
     43  // Return true iff this dependency still holds. May only be called on main
     44  // thread.
     45  virtual bool checkDependency(JSContext* cx) const = 0;
     46  [[nodiscard]] virtual bool registerDependency(
     47      JSContext* cx, const IonScriptKey& ionScript) = 0;
     48 
     49  virtual CompilationDependency* clone(TempAllocator& alloc) const = 0;
     50  virtual ~CompilationDependency() = default;
     51 
     52  struct Hasher {
     53    using Lookup = const CompilationDependency*;
     54    static HashNumber hash(const CompilationDependency* dep) {
     55      return dep->hash();
     56    }
     57    static bool match(const CompilationDependency* k,
     58                      const CompilationDependency* l) {
     59      return *k == *l;
     60    }
     61  };
     62 };
     63 
     64 // For a given Warp compilation keep track of the dependencies this compilation
     65 // is depending on. These dependencies will be checked on main thread during
     66 // link time, causing abandonment of a compilation if they no longer hold.
     67 struct CompilationDependencyTracker {
     68  using SetType = InlineSet<CompilationDependency*, 8,
     69                            CompilationDependency::Hasher, SystemAllocPolicy>;
     70  SetType dependencies;
     71 
     72  [[nodiscard]] bool addDependency(TempAllocator& alloc,
     73                                   const CompilationDependency& dep) {
     74    // Ensure we don't add duplicates.
     75    auto p = dependencies.lookupForAdd(&dep);
     76    if (p) {
     77      return true;
     78    }
     79    auto* clone = dep.clone(alloc);
     80    if (!clone) {
     81      return false;
     82    }
     83    return dependencies.add(p, clone);
     84  }
     85 
     86  // Check all dependencies. May only be checked on main thread.
     87  bool checkDependencies(JSContext* cx) {
     88    for (auto r(dependencies.all()); !r.empty(); r.popFront()) {
     89      const CompilationDependency* dep = r.front();
     90      if (!dep->checkDependency(cx)) {
     91        return false;
     92      }
     93    }
     94    return true;
     95  }
     96 
     97  void reset() { dependencies.clearAndCompact(); }
     98 };
     99 
    100 }  // namespace js::jit
    101 #endif /* jit_CompilationDependencyTracker_h */