| 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_MIRGenerator_h |
| 8 |
#define jit_MIRGenerator_h |
| 9 |
|
| 10 |
// This file declares the data structures used to build a control-flow graph |
| 11 |
// containing MIR. |
| 12 |
|
| 13 |
#include "mozilla/Assertions.h" |
| 14 |
#include "mozilla/Atomics.h" |
| 15 |
#include "mozilla/Attributes.h" |
| 16 |
#include "mozilla/Result.h" |
| 17 |
|
| 18 |
#include <stdarg.h> |
| 19 |
#include <stddef.h> |
| 20 |
#include <stdint.h> |
| 21 |
|
| 22 |
#include "jit/CompilationDependencyTracker.h" |
| 23 |
#include "jit/CompileInfo.h" |
| 24 |
#include "jit/CompileWrappers.h" |
| 25 |
#include "jit/JitAllocPolicy.h" |
| 26 |
#include "jit/JitContext.h" |
| 27 |
#include "jit/JitSpewer.h" |
| 28 |
#include "jit/PerfSpewer.h" |
| 29 |
#include "js/Utility.h" |
| 30 |
#include "vm/GeckoProfiler.h" |
| 31 |
|
| 32 |
namespace js { |
| 33 |
namespace jit { |
| 34 |
|
| 35 |
class JitRuntime; |
| 36 |
class MIRGraph; |
| 37 |
class OptimizationInfo; |
| 38 |
|
| 39 |
class MIRGenerator final { |
| 40 |
public: |
| 41 |
MIRGenerator(CompileRealm* realm, const JitCompileOptions& options, |
| 42 |
TempAllocator* alloc, MIRGraph* graph, |
| 43 |
const CompileInfo* outerInfo, |
| 44 |
const OptimizationInfo* optimizationInfo, |
| 45 |
const wasm::CodeMetadata* wasmCodeMeta = nullptr); |
| 46 |
|
| 47 |
void initMinWasmMemory0Length(uint64_t init) { minWasmMemory0Length_ = init; } |
| 48 |
|
| 49 |
TempAllocator& alloc() { return *alloc_; } |
| 50 |
MIRGraph& graph() { return *graph_; } |
| 51 |
[[nodiscard]] bool ensureBallast() { return alloc().ensureBallast(); } |
| 52 |
const JitRuntime* jitRuntime() const { return runtime->jitRuntime(); } |
| 53 |
const CompileInfo& outerInfo() const { return *outerInfo_; } |
| 54 |
const OptimizationInfo& optimizationInfo() const { |
| 55 |
return *optimizationInfo_; |
| 56 |
} |
| 57 |
bool hasProfilingScripts() const { |
| 58 |
return runtime && runtime->profilingScripts(); |
| 59 |
} |
| 60 |
|
| 61 |
template <typename T> |
| 62 |
T* allocate(size_t count = 1) { |
| 63 |
size_t bytes; |
| 64 |
if (MOZ_UNLIKELY(!CalculateAllocSize<T>(count, &bytes))) { |
| 65 |
return nullptr; |
| 66 |
} |
| 67 |
return static_cast<T*>(alloc().allocate(bytes)); |
| 68 |
} |
| 69 |
|
| 70 |
// Set an error state and prints a message. Returns false so errors can be |
| 71 |
// propagated up. |
| 72 |
mozilla::GenericErrorResult<AbortReason> abort(AbortReason r); |
| 73 |
mozilla::GenericErrorResult<AbortReason> abort(AbortReason r, |
| 74 |
const char* message, ...) |
| 75 |
MOZ_FORMAT_PRINTF(3, 4); |
| 76 |
|
| 77 |
mozilla::GenericErrorResult<AbortReason> abortFmt(AbortReason r, |
| 78 |
const char* message, |
| 79 |
va_list ap) |
| 80 |
MOZ_FORMAT_PRINTF(3, 0); |
| 81 |
|
| 82 |
// Collect the evaluation result of phases after WarpOracle, such that |
| 83 |
// off-thread compilation can report what error got encountered. |
| 84 |
void setOffThreadStatus(AbortReasonOr<Ok>&& result) { |
| 85 |
MOZ_ASSERT(offThreadStatus_.isOk()); |
| 86 |
offThreadStatus_ = std::move(result); |
| 87 |
} |
| 88 |
const AbortReasonOr<Ok>& getOffThreadStatus() const { |
| 89 |
return offThreadStatus_; |
| 90 |
} |
| 91 |
|
| 92 |
[[nodiscard]] bool instrumentedProfiling() { |
| 93 |
if (!instrumentedProfilingIsCached_) { |
| 94 |
instrumentedProfiling_ = runtime->geckoProfiler().enabled(); |
| 95 |
instrumentedProfilingIsCached_ = true; |
| 96 |
} |
| 97 |
return instrumentedProfiling_; |
| 98 |
} |
| 99 |
|
| 100 |
bool isProfilerInstrumentationEnabled() { |
| 101 |
return !compilingWasm() && instrumentedProfiling(); |
| 102 |
} |
| 103 |
|
| 104 |
gc::Heap initialStringHeap() const { |
| 105 |
return stringsCanBeInNursery_ ? gc::Heap::Default : gc::Heap::Tenured; |
| 106 |
} |
| 107 |
|
| 108 |
gc::Heap initialBigIntHeap() const { |
| 109 |
return bigIntsCanBeInNursery_ ? gc::Heap::Default : gc::Heap::Tenured; |
| 110 |
} |
| 111 |
|
| 112 |
// Whether the main thread is trying to cancel this build. |
| 113 |
bool shouldCancel(const char* why) const { return cancelBuild_; } |
| 114 |
void cancel() { cancelBuild_ = true; } |
| 115 |
|
| 116 |
bool compilingWasm() const { return outerInfo_->compilingWasm(); } |
| 117 |
|
| 118 |
uint32_t wasmMaxStackArgBytes() const { |
| 119 |
MOZ_ASSERT(compilingWasm()); |
| 120 |
return wasmMaxStackArgBytes_; |
| 121 |
} |
| 122 |
void accumulateWasmMaxStackArgBytes(uint32_t n) { |
| 123 |
MOZ_ASSERT(compilingWasm()); |
| 124 |
wasmMaxStackArgBytes_ = std::max(n, wasmMaxStackArgBytes_); |
| 125 |
} |
| 126 |
uint64_t minWasmMemory0Length() const { return minWasmMemory0Length_; } |
| 127 |
|
| 128 |
void setNeedsOverrecursedCheck() { needsOverrecursedCheck_ = true; } |
| 129 |
bool needsOverrecursedCheck() const { return needsOverrecursedCheck_; } |
| 130 |
|
| 131 |
void setNeedsStaticStackAlignment() { needsStaticStackAlignment_ = true; } |
| 132 |
bool needsStaticStackAlignment() const { return needsStaticStackAlignment_; } |
| 133 |
|
| 134 |
public: |
| 135 |
CompileRealm* realm; |
| 136 |
CompileRuntime* runtime; |
| 137 |
|
| 138 |
private: |
| 139 |
// The CompileInfo for the outermost script. |
| 140 |
const CompileInfo* outerInfo_; |
| 141 |
|
| 142 |
const OptimizationInfo* optimizationInfo_; |
| 143 |
TempAllocator* alloc_; |
| 144 |
MIRGraph* graph_; |
| 145 |
AbortReasonOr<Ok> offThreadStatus_; |
| 146 |
mozilla::Atomic<bool, mozilla::Relaxed> cancelBuild_; |
| 147 |
|
| 148 |
uint32_t wasmMaxStackArgBytes_; |
| 149 |
bool needsOverrecursedCheck_; |
| 150 |
bool needsStaticStackAlignment_; |
| 151 |
|
| 152 |
bool instrumentedProfiling_; |
| 153 |
bool instrumentedProfilingIsCached_; |
| 154 |
bool stringsCanBeInNursery_; |
| 155 |
bool bigIntsCanBeInNursery_; |
| 156 |
|
| 157 |
bool disableLICM_ = false; |
| 158 |
|
| 159 |
public: |
| 160 |
void disableLICM() { disableLICM_ = true; } |
| 161 |
bool licmEnabled() const; |
| 162 |
bool branchHintingEnabled() const; |
| 163 |
|
| 164 |
private: |
| 165 |
uint64_t minWasmMemory0Length_; |
| 166 |
|
| 167 |
IonPerfSpewer wasmPerfSpewer_; |
| 168 |
|
| 169 |
public: |
| 170 |
IonPerfSpewer& perfSpewer() { return wasmPerfSpewer_; } |
| 171 |
|
| 172 |
public: |
| 173 |
const JitCompileOptions options; |
| 174 |
|
| 175 |
private: |
| 176 |
GraphSpewer gs_; |
| 177 |
|
| 178 |
public: |
| 179 |
GraphSpewer& graphSpewer() { return gs_; } |
| 180 |
CompilationDependencyTracker tracker; |
| 181 |
}; |
| 182 |
|
| 183 |
} // namespace jit |
| 184 |
} // namespace js |
| 185 |
|
| 186 |
#endif /* jit_MIRGenerator_h */ |