dart-sdk/runtime/vm/closure_functions_cache.h
Martin Kustermann 8f7cf7724c [gardening] Ensure functions use stable hashes across reload
Root cause is that `Function::Hash()` depends on the owner class
id. This causes two issues

a) Issue with reload:

During reload the reloader code will change `Function::owner()`
from a `Class` to a `PatchClass`. This in return will cause a change in
the function's hash, which means we won't find the function in the
closure cache anymore.

b) Issue with app-jit:

As part of app-jit we re-number class ids. This re-numbering would also
render the closure cache as invalid - as the function hash code would
change. So instead the closure cache was cleared entirely. While this
works for AOT, in app-jit we may serialize closure functions and their
code. Though at app-jit runtime we then fail to find the functions in
the closure function cache (as it was cleared during snapshoting)

(This will surface by us triggering an optimizing compile of a function
in the background compiler which will try to make a new `Function`
object for a closure, even though the app-jit snapshot already has one)

To fix these issues we make the `Function::Hash()` stable across reload

  * we use `Function::Owner()` instead of `Function::owner()` (the
    former will always return the actual class, the ladder can return
    the `PatchClass`)

  * we avoid re-hashing the closure cache by making `Function::Hash()`
    use the name of the class instead of the class id

  * we clear out the closure cache array during AOT snapshoting, as this
    is not used in AOT runtime.

Closes https://github.com/dart-lang/sdk/issues/52803

TEST=Fixes various issues on CI.

Change-Id: I352d0a768df0f29d504cdd80e3533cbedc437b9b
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/311744
Commit-Queue: Martin Kustermann <kustermann@google.com>
Reviewed-by: Slava Egorov <vegorov@google.com>
2023-06-28 11:48:26 +00:00

67 lines
2.3 KiB
C++

// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
#ifndef RUNTIME_VM_CLOSURE_FUNCTIONS_CACHE_H_
#define RUNTIME_VM_CLOSURE_FUNCTIONS_CACHE_H_
#include <functional>
#include "vm/allocation.h"
#include "vm/token_position.h"
namespace dart {
class Class;
class Function;
class FunctionPtr;
// Implementation of cache for inner closure functions.
//
// This cache is populated lazily by the compiler: When compiling a function,
// the flow graph builder will recursively traverse the kernel AST for the
// function and any inner functions. This will cause the lazy-creation of inner
// closure functions.
//
// The cache is currently implemented as a 2-level
// Map<OutermostMemberFunction, Map<FunctionNodeKernelOffset, Function>>.
//
// The function is also added to the growable list in order to
// satisfy the following requirements:
// * closure functions list can grow while iterating
// * the index of closure function must be stable
//
class ClosureFunctionsCache : public AllStatic {
public:
static FunctionPtr LookupClosureFunction(const Function& member_function,
intptr_t kernel_offset);
static FunctionPtr LookupClosureFunctionLocked(
const Function& member_function,
intptr_t kernel_offset);
// Normally implicit closure functions are not added to this cache, however
// during AOT compilation we might add those implicit closure functions
// that have their original functions shaken to allow ProgramWalker to
// discover them.
static void AddClosureFunctionLocked(
const Function& function,
bool allow_implicit_closure_functions = false);
static intptr_t FindClosureIndex(const Function& needle);
static FunctionPtr ClosureFunctionFromIndex(intptr_t idx);
// Visits all closure functions registered in the object store.
//
// Iterates in-order, thereby allowing new closures being added during the
// iteration.
//
// The iteration continues until either [callback] returns `false` or all
// closure functions have been visited.
static void ForAllClosureFunctions(
std::function<bool(const Function&)> callback);
};
} // namespace dart
#endif // RUNTIME_VM_CLOSURE_FUNCTIONS_CACHE_H_