v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
compilation-environment.h
Go to the documentation of this file.
1// Copyright 2018 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef V8_WASM_COMPILATION_ENVIRONMENT_H_
6#define V8_WASM_COMPILATION_ENVIRONMENT_H_
7
8#if !V8_ENABLE_WEBASSEMBLY
9#error This header should only be included if WebAssembly is enabled.
10#endif // !V8_ENABLE_WEBASSEMBLY
11
12#include <memory>
13#include <optional>
14
18#include "src/wasm/wasm-tier.h"
19
20namespace v8 {
21
22class CFunctionInfo;
23class JobHandle;
24
25namespace internal {
26
27class Counters;
28
29namespace wasm {
30
31class NativeModule;
32struct UnpublishedWasmCode;
33class WasmCode;
34class WasmEngine;
35class WasmError;
36
37// The Arm architecture does not specify the results in memory of
38// partially-in-bound writes, which does not align with the wasm spec. This
39// affects when trap handlers can be used for OOB detection; however, Mac
40// systems with Apple silicon currently do provide trapping beahviour for
41// partially-out-of-bound writes, so we assume we can rely on that on MacOS,
42// since doing so provides better performance for writes.
43#if V8_TARGET_ARCH_ARM64 && !V8_OS_MACOS
44constexpr bool kPartialOOBWritesAreNoops = false;
45#else
46constexpr bool kPartialOOBWritesAreNoops = true;
47#endif
48
49// The {CompilationEnv} encapsulates the module data that is used during
50// compilation. CompilationEnvs are shareable across multiple compilations.
52 // A pointer to the decoded module's static representation.
53 const WasmModule* const module;
54
55 // Features enabled for this compilation.
57
58 const std::atomic<Address>* fast_api_targets;
59
60 std::atomic<const MachineSignature*>* fast_api_signatures;
61
62 // Create a {CompilationEnv} object for compilation. The caller has to ensure
63 // that the {WasmModule} pointer stays valid while the {CompilationEnv} is
64 // being used.
65 static inline CompilationEnv ForModule(const NativeModule* native_module);
66
68
69 private:
78};
79
80// The wire bytes are either owned by the StreamingDecoder, or (after streaming)
81// by the NativeModule. This class abstracts over the storage location.
83 public:
84 virtual ~WireBytesStorage() = default;
86 // Returns the ModuleWireBytes corresponding to the underlying module if
87 // available. Not supported if the wire bytes are owned by a StreamingDecoder.
88 virtual std::optional<ModuleWireBytes> GetModuleBytes() const = 0;
89};
90
91// Callbacks will receive either {kFailedCompilation} or
92// {kFinishedBaselineCompilation}.
98
100 public:
101 virtual ~CompilationEventCallback() = default;
102
103 virtual void call(CompilationEvent event) = 0;
104
106 kReleaseAfterFinalEvent = true,
107 kKeepAfterFinalEvent = false
108 };
109
110 // Tells the module compiler whether to keep or to release a callback when the
111 // compilation state finishes all compilation units. Most callbacks should be
112 // released, that's why there is a default implementation, but the callback
113 // for code caching with dynamic tiering has to stay alive.
115 return kReleaseAfterFinalEvent;
116 }
117};
118
119// The implementation of {CompilationState} lives in module-compiler.cc.
120// This is the PIMPL interface to that private class.
122 public:
124
125 // Override {operator delete} to avoid implicit instantiation of {operator
126 // delete} with {size_t} argument. The {size_t} argument would be incorrect.
127 void operator delete(void* ptr) { ::operator delete(ptr); }
128
130
131 void InitCompileJob();
132
133 void CancelCompilation();
134
135 void CancelInitialCompilation();
136
137 void SetError();
138
139 void SetWireBytesStorage(std::shared_ptr<WireBytesStorage>);
140
141 std::shared_ptr<WireBytesStorage> GetWireBytesStorage() const;
142
143 void AddCallback(std::unique_ptr<CompilationEventCallback> callback);
144
145 void InitializeAfterDeserialization(base::Vector<const int> lazy_functions,
146 base::Vector<const int> eager_functions);
147
148 // Set a higher priority for the compilation job.
150
151 void TierUpAllFunctions();
152
153 // By default, only one top-tier compilation task will be executed for each
154 // function. These functions allow resetting that counter, to be used when
155 // optimized code is intentionally thrown away and should be re-created.
156 void AllowAnotherTopTierJob(uint32_t func_index);
157 void AllowAnotherTopTierJobForAllFunctions();
158
159 bool failed() const;
160 bool baseline_compilation_finished() const;
161
162 void set_compilation_id(int compilation_id);
163
164 size_t EstimateCurrentMemoryConsumption() const;
165
166 std::vector<WasmCode*> PublishCode(
167 base::Vector<UnpublishedWasmCode> unpublished_code);
168
169 WasmDetectedFeatures detected_features() const;
170
171 // Update the set of detected features. Returns any features that were not
172 // detected previously.
174 UpdateDetectedFeatures(WasmDetectedFeatures);
175
176 private:
177 // NativeModule is allowed to call the static {New} method.
178 friend class NativeModule;
179
180 // The CompilationState keeps a {std::weak_ptr} back to the {NativeModule}
181 // such that it can keep it alive (by regaining a {std::shared_ptr}) in
182 // certain scopes.
183 static std::unique_ptr<CompilationState> New(
184 const std::shared_ptr<NativeModule>&, std::shared_ptr<Counters>,
185 WasmDetectedFeatures detected_features);
186};
187
188} // namespace wasm
189} // namespace internal
190} // namespace v8
191
192#endif // V8_WASM_COMPILATION_ENVIRONMENT_H_
virtual void call(CompilationEvent event)=0
virtual ReleaseAfterFinalEvent release_after_final_event()
virtual std::optional< ModuleWireBytes > GetModuleBytes() const =0
virtual base::Vector< const uint8_t > GetCode(WireBytesRef) const =0
TNode< Object > callback
v8::JobHandle JobHandle
Definition platform.h:19
constexpr bool kPartialOOBWritesAreNoops
Definition c-api.cc:87
#define V8_EXPORT_PRIVATE
Definition macros.h:460
std::atomic< const MachineSignature * > * fast_api_signatures
const std::atomic< Address > * fast_api_targets
static CompilationEnv ForModule(const NativeModule *native_module)
constexpr CompilationEnv(const WasmModule *module, WasmEnabledFeatures enabled_features, std::atomic< Address > *fast_api_targets, std::atomic< const MachineSignature * > *fast_api_signatures)
static constexpr CompilationEnv NoModuleAllFeaturesForTesting()
#define V8_WARN_UNUSED_RESULT
Definition v8config.h:671