| 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 |
* |
| 4 |
* Copyright 2021 Mozilla Foundation |
| 5 |
* |
| 6 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 |
* you may not use this file except in compliance with the License. |
| 8 |
* You may obtain a copy of the License at |
| 9 |
* |
| 10 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
* |
| 12 |
* Unless required by applicable law or agreed to in writing, software |
| 13 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 14 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 |
* See the License for the specific language governing permissions and |
| 16 |
* limitations under the License. |
| 17 |
*/ |
| 18 |
|
| 19 |
#ifndef wasm_instance_data_h |
| 20 |
#define wasm_instance_data_h |
| 21 |
|
| 22 |
#include <stdint.h> |
| 23 |
|
| 24 |
#include "NamespaceImports.h" |
| 25 |
|
| 26 |
#include "gc/Pretenuring.h" |
| 27 |
#include "js/Utility.h" |
| 28 |
#include "vm/JSFunction.h" |
| 29 |
#include "wasm/WasmInstance.h" |
| 30 |
#include "wasm/WasmMemory.h" |
| 31 |
#include "wasm/WasmTypeDecls.h" |
| 32 |
|
| 33 |
namespace js { |
| 34 |
namespace wasm { |
| 35 |
|
| 36 |
// ExportArg holds the unboxed operands to the wasm entry trampoline which can |
| 37 |
// be called through an ExportFuncPtr. |
| 38 |
|
| 39 |
struct ExportArg { |
| 40 |
uint64_t lo; |
| 41 |
uint64_t hi; |
| 42 |
}; |
| 43 |
|
| 44 |
using ExportFuncPtr = int32_t (*)(ExportArg*, Instance*); |
| 45 |
|
| 46 |
// TypeDefInstanceData describes the runtime information associated with a |
| 47 |
// module's type definition. This is accessed directly from JIT code and the |
| 48 |
// Instance. |
| 49 |
|
| 50 |
struct TypeDefInstanceData { |
| 51 |
TypeDefInstanceData() |
| 52 |
: typeDef(nullptr), |
| 53 |
superTypeVector(nullptr), |
| 54 |
shape(nullptr), |
| 55 |
clasp(nullptr), |
| 56 |
allocKind(gc::AllocKind::LIMIT), |
| 57 |
unused(0) {} |
| 58 |
|
| 59 |
// The canonicalized pointer to this type definition. This is kept alive by |
| 60 |
// the type context associated with the instance. |
| 61 |
const wasm::TypeDef* typeDef; |
| 62 |
|
| 63 |
// The supertype vector for this type definition. This is also kept alive |
| 64 |
// by the type context associated with the instance. |
| 65 |
// |
| 66 |
const wasm::SuperTypeVector* superTypeVector; |
| 67 |
|
| 68 |
// The next four fields are only meaningful for, and used by, structs and |
| 69 |
// arrays. |
| 70 |
GCPtr<Shape*> shape; |
| 71 |
const JSClass* clasp; |
| 72 |
// Only valid for structs. |
| 73 |
gc::AllocKind allocKind; |
| 74 |
|
| 75 |
// This union is only meaningful for structs and arrays, and should |
| 76 |
// otherwise be set to zero: |
| 77 |
// |
| 78 |
// * if `typeDef` refers to a struct type, then it caches the value of |
| 79 |
// `typeDef->structType().size_` (a size in bytes) |
| 80 |
// |
| 81 |
// * if `typeDef` refers to an array type, then it caches the value of |
| 82 |
// `typeDef->arrayType().elementType_.size()` (also a size in bytes) |
| 83 |
// |
| 84 |
// This is so that allocators of structs and arrays don't need to chase from |
| 85 |
// this TypeDefInstanceData through `typeDef` to find the value. |
| 86 |
union { |
| 87 |
uint32_t structTypeSize; |
| 88 |
uint32_t arrayElemSize; |
| 89 |
uint32_t unused; |
| 90 |
}; |
| 91 |
|
| 92 |
static constexpr size_t offsetOfShape() { |
| 93 |
return offsetof(TypeDefInstanceData, shape); |
| 94 |
} |
| 95 |
static constexpr size_t offsetOfSuperTypeVector() { |
| 96 |
return offsetof(TypeDefInstanceData, superTypeVector); |
| 97 |
} |
| 98 |
static constexpr size_t offsetOfArrayElemSize() { |
| 99 |
return offsetof(TypeDefInstanceData, arrayElemSize); |
| 100 |
} |
| 101 |
}; |
| 102 |
|
| 103 |
// FuncDefInstanceData maintains the per-instance hotness state for a locally |
| 104 |
// defined wasm function. This is a signed-int32 value that counts downwards |
| 105 |
// from an initially non-negative value. At the point where the value |
| 106 |
// transitions below zero (not *to* zero), we deem the owning function to |
| 107 |
// have become hot. Transitions from one negative value to any other (even |
| 108 |
// more) negative value are meaningless and should not happen. |
| 109 |
struct FuncDefInstanceData { |
| 110 |
int32_t hotnessCounter; |
| 111 |
}; |
| 112 |
|
| 113 |
// FuncExportInstanceData maintains the exported function JS wrapper for an |
| 114 |
// exported function. |
| 115 |
struct FuncExportInstanceData { |
| 116 |
GCPtr<JSFunction*> func; |
| 117 |
}; |
| 118 |
|
| 119 |
// FuncImportInstanceData describes the region of wasm global memory allocated |
| 120 |
// in the instance's thread-local storage for a function import. This is |
| 121 |
// accessed directly from JIT code and mutated by Instance as exits become |
| 122 |
// optimized and deoptimized. |
| 123 |
|
| 124 |
struct FuncImportInstanceData { |
| 125 |
// The code to call at an import site: a wasm callee, a thunk into C++, or a |
| 126 |
// thunk into JIT code. |
| 127 |
void* code; |
| 128 |
|
| 129 |
// The callee's Instance pointer, which must be loaded to InstanceReg |
| 130 |
// (along with any pinned registers) before calling 'code'. |
| 131 |
Instance* instance; |
| 132 |
|
| 133 |
// The callee function's realm. |
| 134 |
JS::Realm* realm; |
| 135 |
|
| 136 |
// A GC pointer which keeps the callee alive and is used to recover import |
| 137 |
// values for lazy table initialization. |
| 138 |
GCPtr<JSObject*> callable; |
| 139 |
static_assert(sizeof(GCPtr<JSObject*>) == sizeof(void*), "for JIT access"); |
| 140 |
}; |
| 141 |
|
| 142 |
struct MemoryInstanceData { |
| 143 |
// Pointer to the memory object. |
| 144 |
GCPtr<WasmMemoryObject*> memory; |
| 145 |
|
| 146 |
// Pointer to the base of the memory. |
| 147 |
uint8_t* base; |
| 148 |
|
| 149 |
// Bounds check limit in bytes (or zero if there is no memory). This is |
| 150 |
// 64-bits on 64-bit systems so as to allow for heap lengths up to and beyond |
| 151 |
// 4GB, and 32-bits on 32-bit systems, where heaps are limited to 2GB. |
| 152 |
// |
| 153 |
// See "Linear memory addresses and bounds checking" in WasmMemory.cpp. |
| 154 |
uintptr_t boundsCheckLimit; |
| 155 |
|
| 156 |
// Whether this memory is shared or not. |
| 157 |
bool isShared; |
| 158 |
}; |
| 159 |
|
| 160 |
// TableInstanceData describes the region of wasm global memory allocated in the |
| 161 |
// instance's thread-local storage which is accessed directly from JIT code |
| 162 |
// to bounds-check and index the table. |
| 163 |
|
| 164 |
struct TableInstanceData { |
| 165 |
// Length of the table in number of elements (not bytes). |
| 166 |
uint32_t length; |
| 167 |
|
| 168 |
// Pointer to the array of elements (which can have various representations). |
| 169 |
// For tables of anyref this is null. |
| 170 |
// For tables of functions, this is a pointer to the array of code pointers. |
| 171 |
void* elements; |
| 172 |
}; |
| 173 |
|
| 174 |
// TagInstanceData describes the instance state associated with a tag. |
| 175 |
|
| 176 |
struct TagInstanceData { |
| 177 |
GCPtr<WasmTagObject*> object; |
| 178 |
}; |
| 179 |
|
| 180 |
// Table element for TableRepr::Func which carries both the code pointer and |
| 181 |
// a instance pointer (and thus anything reachable through the instance). |
| 182 |
|
| 183 |
struct FunctionTableElem { |
| 184 |
// The code to call when calling this element. The table ABI is the system |
| 185 |
// ABI with the additional ABI requirements that: |
| 186 |
// - InstanceReg and any pinned registers have been loaded appropriately |
| 187 |
// - if this is a heterogeneous table that requires a signature check, |
| 188 |
// WasmTableCallSigReg holds the signature id. |
| 189 |
void* code; |
| 190 |
|
| 191 |
// The pointer to the callee's instance's Instance. This must be loaded into |
| 192 |
// InstanceReg before calling 'code'. |
| 193 |
Instance* instance; |
| 194 |
}; |
| 195 |
|
| 196 |
// A collection of metrics for a `call_ref` instruction. This is tracked by |
| 197 |
// baseline when we are using lazy tiering to perform speculative inlining. |
| 198 |
// |
| 199 |
// See MacroAssembler::updateCallRefMetrics for how this is written into. |
| 200 |
// |
| 201 |
// Because it contains thread-local data and is written into without |
| 202 |
// synchronization, we cannot access this directly from our function compilers |
| 203 |
// and so we use CallRefHints for that (see WasmModuleTypes.h). |
| 204 |
struct CallRefMetrics { |
| 205 |
// We track up to NUM_SLOTS targets with associated count, plus a count for |
| 206 |
// "all other" targets, including cross-instance calls. This facilitates |
| 207 |
// knowing the total number of calls made by the instruction, which is |
| 208 |
// required in order to know whether the hottest tracked function is more |
| 209 |
// than some percentage (eg, 50%) of all calls. |
| 210 |
// |
| 211 |
// In order to keep search costs low (in the baseline code), we rely on the |
| 212 |
// fact that most call sites have distributions which are heavily skewed |
| 213 |
// towards one target. This struct is updated by the code generated in |
| 214 |
// GenerateUpdateCallRefMetricsStub. That tries to make |
| 215 |
// `targets[0]`/`counts[0]` be the hottest target, so that for most calls, |
| 216 |
// the monitoring code will only check `targets[0]` for a match. |
| 217 |
// |
| 218 |
// For NUM_SLOTS <= 2, GenerateUpdateCallRefMetricsStub's incremental-sort |
| 219 |
// heuristic maintains the `counts` array in strictly non-decreasing order. |
| 220 |
// For NUM_SLOTS > 2, in the worst case we will have counts[N+1] at most |
| 221 |
// (NUM_SLOTS - 2) larger than counts[N]. In practice the incremental |
| 222 |
// sorting heuristic is very effective and so counts are in decreasing order, |
| 223 |
// as we desire. |
| 224 |
// |
| 225 |
// Once NUM_SLOTS targets are being tracked, all new targets will be lumped |
| 226 |
// together in the `countOther` bucket. This can lead to the unfortunate |
| 227 |
// case of having NUM_SLOTS different cold targets show up first, after which |
| 228 |
// follows a different target that is hot, but cannot be inlined because it |
| 229 |
// goes in the `countOther` bucket, so its identity is unknown. This is |
| 230 |
// unlikely but could happen. The only known fix is to increase NUM_SLOTS. |
| 231 |
// |
| 232 |
// The `targets` values may be nullptr only to indicate that the slot is not |
| 233 |
// in use. No legitimate target can be nullptr. Given that the state is |
| 234 |
// updated by generated code and that code isn't entirely simple, we place |
| 235 |
// emphasis on checking invariants carefully. |
| 236 |
// |
| 237 |
// Stores of funcrefs in `targets[]`: These CallRefMetrics structs logically |
| 238 |
// belong to the Instance data, and do not require any GC barriers for two |
| 239 |
// reasons: |
| 240 |
// |
| 241 |
// 1. The pre-write barrier protects against an unmarked object being stored |
| 242 |
// into a marked object during an incremental GC. However this funcref is |
| 243 |
// from the Instance we're storing it into (see above) and so if the |
| 244 |
// instance has already been traced, this function will already have been |
| 245 |
// traced (all exported functions are kept alive by an instance cache). |
| 246 |
// |
| 247 |
// 2. The post-write barrier tracks edges from tenured objects to nursery |
| 248 |
// objects. However wasm exported functions are not nursery allocated and |
| 249 |
// so no new edge can be created. |
| 250 |
// |
| 251 |
// Overflows in `counts[]` and `countOther`: increments of these values are |
| 252 |
// not checked for overflow and so could wrap around from 2^32-1 to zero. |
| 253 |
// We ignore but tolerate this, because: |
| 254 |
// |
| 255 |
// 1. This is extremely unlikely to happen in practice, since the function |
| 256 |
// containing the call site is almost certain to get tiered up long before |
| 257 |
// any of these counters gets anywhere near the limit. |
| 258 |
// |
| 259 |
// 2. Performing saturating increments is possible, but has a minimum extra |
| 260 |
// cost of two instructions, and given (1.) it is pointless. |
| 261 |
// |
| 262 |
// This does however require that interpretation of the `counts[]` and |
| 263 |
// `countOther` values needs to be aware that zeroes could mean 2^32 or any |
| 264 |
// multiple of it. Hence a zero in `counts[]` does not necessarily mean that |
| 265 |
// the corresponding `target[]` was never called, nor is it the case that a |
| 266 |
// `countsOther` of zero means no "other" targets were observed. |
| 267 |
|
| 268 |
static constexpr size_t NUM_SLOTS = 3; |
| 269 |
static_assert(NUM_SLOTS >= 1); // 1 slot + others is the minimal config |
| 270 |
|
| 271 |
// An array of pairs of (target, count) .. |
| 272 |
GCPtr<JSFunction*> targets[NUM_SLOTS]; |
| 273 |
uint32_t counts[NUM_SLOTS]; |
| 274 |
// .. and a count for all other targets. |
| 275 |
uint32_t countOther; |
| 276 |
|
| 277 |
// Generated code assumes this |
| 278 |
static_assert(sizeof(GCPtr<JSFunction*>) == sizeof(void*)); |
| 279 |
static_assert(sizeof(uint32_t) == 4); |
| 280 |
|
| 281 |
CallRefMetrics() { |
| 282 |
for (size_t i = 0; i < NUM_SLOTS; i++) { |
| 283 |
targets[i] = nullptr; |
| 284 |
counts[i] = 0; |
| 285 |
} |
| 286 |
countOther = 0; |
| 287 |
MOZ_ASSERT(checkInvariants()); |
| 288 |
} |
| 289 |
|
| 290 |
[[nodiscard]] bool checkInvariants() const { |
| 291 |
// If targets[N] is null, then this slot is not in use and so counts[N] |
| 292 |
// must be zero. Per comments above about overflow, the implication in the |
| 293 |
// other direction does not hold. |
| 294 |
size_t i; |
| 295 |
for (i = 0; i < NUM_SLOTS; i++) { |
| 296 |
if (targets[i] == nullptr && counts[i] != 0) { |
| 297 |
return false; |
| 298 |
} |
| 299 |
} |
| 300 |
// The targets/counts slots must be filled in in sequence. |
| 301 |
for (i = 0; i < NUM_SLOTS; i++) { |
| 302 |
if (targets[i] == nullptr) { |
| 303 |
break; |
| 304 |
} |
| 305 |
} |
| 306 |
size_t numUsed = i; |
| 307 |
for (/*keepgoing*/; i < NUM_SLOTS; i++) { |
| 308 |
if (targets[i] != nullptr) { |
| 309 |
return false; |
| 310 |
} |
| 311 |
} |
| 312 |
// For the slots in use, the target values must be different |
| 313 |
for (i = 0; i < numUsed; i++) { |
| 314 |
for (size_t j = i + 1; j < numUsed; j++) { |
| 315 |
if (targets[j] == targets[i]) { |
| 316 |
return false; |
| 317 |
} |
| 318 |
} |
| 319 |
} |
| 320 |
// Note we don't say anything about `countOther`. This gets incremented in |
| 321 |
// the cases when we (1) see a new target when all slots are already in |
| 322 |
// use, or (2) have a cross-instance call. The effect of (2) is that |
| 323 |
// `countOther` can be non-zero regardless of how many slots are in use. |
| 324 |
return true; |
| 325 |
} |
| 326 |
|
| 327 |
static size_t offsetOfTarget(size_t n) { |
| 328 |
MOZ_ASSERT(n < NUM_SLOTS); |
| 329 |
return offsetof(CallRefMetrics, targets) + n * sizeof(GCPtr<JSFunction*>); |
| 330 |
} |
| 331 |
static size_t offsetOfCount(size_t n) { |
| 332 |
MOZ_ASSERT(n < NUM_SLOTS); |
| 333 |
return offsetof(CallRefMetrics, counts) + n * sizeof(uint32_t); |
| 334 |
} |
| 335 |
static size_t offsetOfCountOther() { |
| 336 |
return offsetof(CallRefMetrics, countOther); |
| 337 |
} |
| 338 |
}; |
| 339 |
|
| 340 |
} // namespace wasm |
| 341 |
} // namespace js |
| 342 |
|
| 343 |
#endif // wasm_instance_data_h |