Commit c33257e1 authored by Clemens Backes's avatar Clemens Backes Committed by V8 LUCI CQ

[wasm] Use AssemblerBufferCache also for TurboFan

The AssemblerBufferCache was so far only used for Liftoff compilation.
Hence all TurboFan compilation was using unprotected assembler buffers.

This CL passes the AssemblerBufferCache from the function compiler down
to the TurboFan PipelineData. From there it is used when instantiating
the CodeGenerator to generate the AssemblerBuffer for the
TurboAssembler. This will protect the assembler buffers used for
TurboFan Wasm compilation via PKU, if available.

Since PipelineData has a single constructor for all Wasm compilation, we
have a single choke point to ensure that an AssemblerBufferCache is
passed down. For Wasm stub compilation (import wrappers etc) we
currently explicitly pass a nullptr, this will be fixed in a follow-up
CL.

R=thibaudm@chromium.org, tebbi@chromium.org

Bug: v8:12809
Change-Id: I268bd21047adbd7f0aab78e8b0a4b4df1d1f8ddf
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3596172Reviewed-by: 's avatarTobias Tebbi <tebbi@chromium.org>
Reviewed-by: 's avatarThibaud Michaud <thibaudm@chromium.org>
Commit-Queue: Clemens Backes <clemensb@chromium.org>
Cr-Commit-Position: refs/heads/main@{#80210}
parent f196c878
......@@ -20,6 +20,10 @@
#include "src/objects/smi.h"
#include "src/utils/address-map.h"
#if V8_ENABLE_WEBASSEMBLY
#include "src/wasm/assembler-buffer-cache.h"
#endif // V8_ENABLE_WEBASSEMBLY
namespace v8 {
namespace internal {
namespace compiler {
......@@ -41,16 +45,14 @@ class CodeGenerator::JumpTable final : public ZoneObject {
size_t const target_count_;
};
CodeGenerator::CodeGenerator(Zone* codegen_zone, Frame* frame, Linkage* linkage,
InstructionSequence* instructions,
OptimizedCompilationInfo* info, Isolate* isolate,
base::Optional<OsrHelper> osr_helper,
int start_source_position,
JumpOptimizationInfo* jump_opt,
const AssemblerOptions& options, Builtin builtin,
size_t max_unoptimized_frame_height,
size_t max_pushed_argument_count,
const char* debug_name)
CodeGenerator::CodeGenerator(
Zone* codegen_zone, Frame* frame, Linkage* linkage,
InstructionSequence* instructions, OptimizedCompilationInfo* info,
Isolate* isolate, base::Optional<OsrHelper> osr_helper,
int start_source_position, JumpOptimizationInfo* jump_opt,
const AssemblerOptions& options, wasm::AssemblerBufferCache* buffer_cache,
Builtin builtin, size_t max_unoptimized_frame_height,
size_t max_pushed_argument_count, const char* debug_name)
: zone_(codegen_zone),
isolate_(isolate),
frame_access_state_(nullptr),
......@@ -63,7 +65,13 @@ CodeGenerator::CodeGenerator(Zone* codegen_zone, Frame* frame, Linkage* linkage,
current_block_(RpoNumber::Invalid()),
start_source_position_(start_source_position),
current_source_position_(SourcePosition::Unknown()),
tasm_(isolate, options, CodeObjectRequired::kNo),
tasm_(isolate, options, CodeObjectRequired::kNo,
#if V8_ENABLE_WEBASSEMBLY
buffer_cache ? buffer_cache->GetAssemblerBuffer(
AssemblerBase::kDefaultBufferSize)
:
#endif // V8_ENABLE_WEBASSEMBLY
std::unique_ptr<AssemblerBuffer>{}),
resolver_(this),
safepoints_(codegen_zone),
handlers_(codegen_zone),
......
......@@ -23,10 +23,11 @@
#include "src/trap-handler/trap-handler.h"
#endif // V8_ENABLE_WEBASSEMBLY
namespace v8 {
namespace internal {
namespace v8::internal::wasm {
class AssemblerBufferCache;
}
namespace compiler {
namespace v8::internal::compiler {
// Forward declarations.
class DeoptimizationExit;
......@@ -122,16 +123,14 @@ struct TurbolizerInstructionStartInfo {
// Generates native code for a sequence of instructions.
class V8_EXPORT_PRIVATE CodeGenerator final : public GapResolver::Assembler {
public:
explicit CodeGenerator(Zone* codegen_zone, Frame* frame, Linkage* linkage,
InstructionSequence* instructions,
OptimizedCompilationInfo* info, Isolate* isolate,
base::Optional<OsrHelper> osr_helper,
int start_source_position,
JumpOptimizationInfo* jump_opt,
const AssemblerOptions& options, Builtin builtin,
size_t max_unoptimized_frame_height,
size_t max_pushed_argument_count,
const char* debug_name = nullptr);
explicit CodeGenerator(
Zone* codegen_zone, Frame* frame, Linkage* linkage,
InstructionSequence* instructions, OptimizedCompilationInfo* info,
Isolate* isolate, base::Optional<OsrHelper> osr_helper,
int start_source_position, JumpOptimizationInfo* jump_opt,
const AssemblerOptions& options, wasm::AssemblerBufferCache* buffer_cache,
Builtin builtin, size_t max_unoptimized_frame_height,
size_t max_pushed_argument_count, const char* debug_name = nullptr);
// Generate native code. After calling AssembleCode, call FinalizeCode to
// produce the actual code object. If an error occurs during either phase,
......@@ -476,8 +475,6 @@ class V8_EXPORT_PRIVATE CodeGenerator final : public GapResolver::Assembler {
const char* debug_name_ = nullptr;
};
} // namespace compiler
} // namespace internal
} // namespace v8
} // namespace v8::internal::compiler
#endif // V8_COMPILER_BACKEND_CODE_GENERATOR_H_
......@@ -201,9 +201,11 @@ class PipelineData {
PipelineStatistics* pipeline_statistics,
SourcePositionTable* source_positions,
NodeOriginTable* node_origins,
const AssemblerOptions& assembler_options)
const AssemblerOptions& assembler_options,
wasm::AssemblerBufferCache* buffer_cache)
: isolate_(nullptr),
wasm_engine_(wasm_engine),
assembler_buffer_cache_(buffer_cache),
allocator_(wasm_engine->allocator()),
info_(info),
debug_name_(info_->GetDebugName()),
......@@ -559,11 +561,15 @@ class PipelineData {
void InitializeCodeGenerator(Linkage* linkage) {
DCHECK_NULL(code_generator_);
wasm::AssemblerBufferCache* buffer_cache = nullptr;
#if V8_ENABLE_WEBASSEMBLY
buffer_cache = assembler_buffer_cache_;
#endif // V8_ENABLE_WEBASSEMBLY
code_generator_ = new CodeGenerator(
codegen_zone(), frame(), linkage, sequence(), info(), isolate(),
osr_helper_, start_source_position_, jump_optimization_info_,
assembler_options(), info_->builtin(), max_unoptimized_frame_height(),
max_pushed_argument_count(),
assembler_options(), buffer_cache, info_->builtin(),
max_unoptimized_frame_height(), max_pushed_argument_count(),
FLAG_trace_turbo_stack_accesses ? debug_name_.get() : nullptr);
}
......@@ -606,6 +612,7 @@ class PipelineData {
Isolate* const isolate_;
#if V8_ENABLE_WEBASSEMBLY
wasm::WasmEngine* const wasm_engine_ = nullptr;
wasm::AssemblerBufferCache* assembler_buffer_cache_ = nullptr;
#endif // V8_ENABLE_WEBASSEMBLY
AccountingAllocator* const allocator_;
OptimizedCompilationInfo* const info_;
......@@ -3075,8 +3082,10 @@ wasm::WasmCompilationResult Pipeline::GenerateCodeForWasmNativeStub(
wasm::WasmEngine* wasm_engine = wasm::GetWasmEngine();
ZoneStats zone_stats(wasm_engine->allocator());
NodeOriginTable* node_positions = graph->zone()->New<NodeOriginTable>(graph);
// TODO(12809): Use the assembler buffer cache to also protect wasm stubs.
constexpr wasm::AssemblerBufferCache* kNoBufferCache = nullptr;
PipelineData data(&zone_stats, wasm_engine, &info, mcgraph, nullptr,
source_positions, node_positions, options);
source_positions, node_positions, options, kNoBufferCache);
std::unique_ptr<PipelineStatistics> pipeline_statistics;
if (FLAG_turbo_stats || FLAG_turbo_stats_nvp) {
pipeline_statistics.reset(new PipelineStatistics(
......@@ -3172,7 +3181,8 @@ void Pipeline::GenerateCodeForWasmFunction(
CallDescriptor* call_descriptor, SourcePositionTable* source_positions,
NodeOriginTable* node_origins, wasm::FunctionBody function_body,
const wasm::WasmModule* module, int function_index,
std::vector<compiler::WasmLoopInfo>* loop_info) {
std::vector<compiler::WasmLoopInfo>* loop_info,
wasm::AssemblerBufferCache* buffer_cache) {
auto* wasm_engine = wasm::GetWasmEngine();
base::TimeTicks start_time;
if (V8_UNLIKELY(FLAG_trace_wasm_compilation_times)) {
......@@ -3183,7 +3193,7 @@ void Pipeline::GenerateCodeForWasmFunction(
CreatePipelineStatistics(function_body, module, info, &zone_stats));
PipelineData data(&zone_stats, wasm_engine, info, mcgraph,
pipeline_statistics.get(), source_positions, node_origins,
WasmAssemblerOptions());
WasmAssemblerOptions(), buffer_cache);
PipelineImpl pipeline(&data);
......
......@@ -23,11 +23,10 @@ class ProfileDataFromFile;
class RegisterConfiguration;
namespace wasm {
class AssemblerBufferCache;
struct CompilationEnv;
struct FunctionBody;
class NativeModule;
struct WasmCompilationResult;
class WasmEngine;
struct WasmModule;
class WireBytesStorage;
} // namespace wasm
......@@ -61,7 +60,8 @@ class Pipeline : public AllStatic {
CallDescriptor* call_descriptor, SourcePositionTable* source_positions,
NodeOriginTable* node_origins, wasm::FunctionBody function_body,
const wasm::WasmModule* module, int function_index,
std::vector<compiler::WasmLoopInfo>* loop_infos);
std::vector<compiler::WasmLoopInfo>* loop_infos,
wasm::AssemblerBufferCache* buffer_cache);
// Run the pipeline on a machine graph and generate code.
static wasm::WasmCompilationResult GenerateCodeForWasmNativeStub(
......
......@@ -8450,7 +8450,7 @@ base::Vector<const char> GetDebugName(Zone* zone,
wasm::WasmCompilationResult ExecuteTurbofanWasmCompilation(
wasm::CompilationEnv* env, const wasm::WireBytesStorage* wire_byte_storage,
const wasm::FunctionBody& func_body, int func_index, Counters* counters,
wasm::WasmFeatures* detected) {
wasm::AssemblerBufferCache* buffer_cache, wasm::WasmFeatures* detected) {
// Check that we do not accidentally compile a Wasm function to TurboFan if
// --liftoff-only is set.
DCHECK(!FLAG_liftoff_only);
......@@ -8510,9 +8510,10 @@ wasm::WasmCompilationResult ExecuteTurbofanWasmCompilation(
return wasm::WasmCompilationResult{};
}
Pipeline::GenerateCodeForWasmFunction(
&info, env, wire_byte_storage, mcgraph, call_descriptor, source_positions,
node_origins, func_body, env->module, func_index, &loop_infos);
Pipeline::GenerateCodeForWasmFunction(&info, env, wire_byte_storage, mcgraph,
call_descriptor, source_positions,
node_origins, func_body, env->module,
func_index, &loop_infos, buffer_cache);
if (counters) {
int zone_bytes =
......
......@@ -48,6 +48,7 @@ enum class BranchHint : uint8_t;
} // namespace compiler
namespace wasm {
class AssemblerBufferCache;
struct DecodeStruct;
// Expose {Node} and {Graph} opaquely as {wasm::TFNode} and {wasm::TFGraph}.
using TFNode = compiler::Node;
......@@ -64,7 +65,7 @@ namespace compiler {
wasm::WasmCompilationResult ExecuteTurbofanWasmCompilation(
wasm::CompilationEnv*, const wasm::WireBytesStorage* wire_bytes_storage,
const wasm::FunctionBody&, int func_index, Counters*,
wasm::WasmFeatures* detected);
wasm::AssemblerBufferCache* buffer_cache, wasm::WasmFeatures* detected);
// Calls to Wasm imports are handled in several different ways, depending on the
// type of the target function/callable and whether the signature matches the
......
......@@ -144,7 +144,8 @@ WasmCompilationResult WasmCompilationUnit::ExecuteFunctionCompilation(
case ExecutionTier::kTurbofan:
result = compiler::ExecuteTurbofanWasmCompilation(
env, wire_bytes_storage, func_body, func_index_, counters, detected);
env, wire_bytes_storage, func_body, func_index_, counters,
buffer_cache, detected);
result.for_debugging = for_debugging_;
break;
}
......
......@@ -998,13 +998,14 @@ class CodeGeneratorTester {
i++;
}
static constexpr size_t kMaxUnoptimizedFrameHeight = 0;
static constexpr size_t kMaxPushedArgumentCount = 0;
constexpr size_t kMaxUnoptimizedFrameHeight = 0;
constexpr size_t kMaxPushedArgumentCount = 0;
constexpr wasm::AssemblerBufferCache* kNoBufferCache = nullptr;
generator_ = new CodeGenerator(
environment->main_zone(), &frame_, &linkage_,
environment->instructions(), &info_, environment->main_isolate(),
base::Optional<OsrHelper>(), kNoSourcePosition, nullptr,
AssemblerOptions::Default(environment->main_isolate()),
AssemblerOptions::Default(environment->main_isolate()), kNoBufferCache,
Builtin::kNoBuiltinId, kMaxUnoptimizedFrameHeight,
kMaxPushedArgumentCount);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment