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 @@ ...@@ -20,6 +20,10 @@
#include "src/objects/smi.h" #include "src/objects/smi.h"
#include "src/utils/address-map.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 v8 {
namespace internal { namespace internal {
namespace compiler { namespace compiler {
...@@ -41,16 +45,14 @@ class CodeGenerator::JumpTable final : public ZoneObject { ...@@ -41,16 +45,14 @@ class CodeGenerator::JumpTable final : public ZoneObject {
size_t const target_count_; size_t const target_count_;
}; };
CodeGenerator::CodeGenerator(Zone* codegen_zone, Frame* frame, Linkage* linkage, CodeGenerator::CodeGenerator(
InstructionSequence* instructions, Zone* codegen_zone, Frame* frame, Linkage* linkage,
OptimizedCompilationInfo* info, Isolate* isolate, InstructionSequence* instructions, OptimizedCompilationInfo* info,
base::Optional<OsrHelper> osr_helper, Isolate* isolate, base::Optional<OsrHelper> osr_helper,
int start_source_position, int start_source_position, JumpOptimizationInfo* jump_opt,
JumpOptimizationInfo* jump_opt, const AssemblerOptions& options, wasm::AssemblerBufferCache* buffer_cache,
const AssemblerOptions& options, Builtin builtin, Builtin builtin, size_t max_unoptimized_frame_height,
size_t max_unoptimized_frame_height, size_t max_pushed_argument_count, const char* debug_name)
size_t max_pushed_argument_count,
const char* debug_name)
: zone_(codegen_zone), : zone_(codegen_zone),
isolate_(isolate), isolate_(isolate),
frame_access_state_(nullptr), frame_access_state_(nullptr),
...@@ -63,7 +65,13 @@ CodeGenerator::CodeGenerator(Zone* codegen_zone, Frame* frame, Linkage* linkage, ...@@ -63,7 +65,13 @@ CodeGenerator::CodeGenerator(Zone* codegen_zone, Frame* frame, Linkage* linkage,
current_block_(RpoNumber::Invalid()), current_block_(RpoNumber::Invalid()),
start_source_position_(start_source_position), start_source_position_(start_source_position),
current_source_position_(SourcePosition::Unknown()), 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), resolver_(this),
safepoints_(codegen_zone), safepoints_(codegen_zone),
handlers_(codegen_zone), handlers_(codegen_zone),
......
...@@ -23,10 +23,11 @@ ...@@ -23,10 +23,11 @@
#include "src/trap-handler/trap-handler.h" #include "src/trap-handler/trap-handler.h"
#endif // V8_ENABLE_WEBASSEMBLY #endif // V8_ENABLE_WEBASSEMBLY
namespace v8 { namespace v8::internal::wasm {
namespace internal { class AssemblerBufferCache;
}
namespace compiler { namespace v8::internal::compiler {
// Forward declarations. // Forward declarations.
class DeoptimizationExit; class DeoptimizationExit;
...@@ -122,16 +123,14 @@ struct TurbolizerInstructionStartInfo { ...@@ -122,16 +123,14 @@ struct TurbolizerInstructionStartInfo {
// Generates native code for a sequence of instructions. // Generates native code for a sequence of instructions.
class V8_EXPORT_PRIVATE CodeGenerator final : public GapResolver::Assembler { class V8_EXPORT_PRIVATE CodeGenerator final : public GapResolver::Assembler {
public: public:
explicit CodeGenerator(Zone* codegen_zone, Frame* frame, Linkage* linkage, explicit CodeGenerator(
InstructionSequence* instructions, Zone* codegen_zone, Frame* frame, Linkage* linkage,
OptimizedCompilationInfo* info, Isolate* isolate, InstructionSequence* instructions, OptimizedCompilationInfo* info,
base::Optional<OsrHelper> osr_helper, Isolate* isolate, base::Optional<OsrHelper> osr_helper,
int start_source_position, int start_source_position, JumpOptimizationInfo* jump_opt,
JumpOptimizationInfo* jump_opt, const AssemblerOptions& options, wasm::AssemblerBufferCache* buffer_cache,
const AssemblerOptions& options, Builtin builtin, Builtin builtin, size_t max_unoptimized_frame_height,
size_t max_unoptimized_frame_height, size_t max_pushed_argument_count, const char* debug_name = nullptr);
size_t max_pushed_argument_count,
const char* debug_name = nullptr);
// Generate native code. After calling AssembleCode, call FinalizeCode to // Generate native code. After calling AssembleCode, call FinalizeCode to
// produce the actual code object. If an error occurs during either phase, // 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 { ...@@ -476,8 +475,6 @@ class V8_EXPORT_PRIVATE CodeGenerator final : public GapResolver::Assembler {
const char* debug_name_ = nullptr; const char* debug_name_ = nullptr;
}; };
} // namespace compiler } // namespace v8::internal::compiler
} // namespace internal
} // namespace v8
#endif // V8_COMPILER_BACKEND_CODE_GENERATOR_H_ #endif // V8_COMPILER_BACKEND_CODE_GENERATOR_H_
...@@ -201,9 +201,11 @@ class PipelineData { ...@@ -201,9 +201,11 @@ class PipelineData {
PipelineStatistics* pipeline_statistics, PipelineStatistics* pipeline_statistics,
SourcePositionTable* source_positions, SourcePositionTable* source_positions,
NodeOriginTable* node_origins, NodeOriginTable* node_origins,
const AssemblerOptions& assembler_options) const AssemblerOptions& assembler_options,
wasm::AssemblerBufferCache* buffer_cache)
: isolate_(nullptr), : isolate_(nullptr),
wasm_engine_(wasm_engine), wasm_engine_(wasm_engine),
assembler_buffer_cache_(buffer_cache),
allocator_(wasm_engine->allocator()), allocator_(wasm_engine->allocator()),
info_(info), info_(info),
debug_name_(info_->GetDebugName()), debug_name_(info_->GetDebugName()),
...@@ -559,11 +561,15 @@ class PipelineData { ...@@ -559,11 +561,15 @@ class PipelineData {
void InitializeCodeGenerator(Linkage* linkage) { void InitializeCodeGenerator(Linkage* linkage) {
DCHECK_NULL(code_generator_); 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( code_generator_ = new CodeGenerator(
codegen_zone(), frame(), linkage, sequence(), info(), isolate(), codegen_zone(), frame(), linkage, sequence(), info(), isolate(),
osr_helper_, start_source_position_, jump_optimization_info_, osr_helper_, start_source_position_, jump_optimization_info_,
assembler_options(), info_->builtin(), max_unoptimized_frame_height(), assembler_options(), buffer_cache, info_->builtin(),
max_pushed_argument_count(), max_unoptimized_frame_height(), max_pushed_argument_count(),
FLAG_trace_turbo_stack_accesses ? debug_name_.get() : nullptr); FLAG_trace_turbo_stack_accesses ? debug_name_.get() : nullptr);
} }
...@@ -606,6 +612,7 @@ class PipelineData { ...@@ -606,6 +612,7 @@ class PipelineData {
Isolate* const isolate_; Isolate* const isolate_;
#if V8_ENABLE_WEBASSEMBLY #if V8_ENABLE_WEBASSEMBLY
wasm::WasmEngine* const wasm_engine_ = nullptr; wasm::WasmEngine* const wasm_engine_ = nullptr;
wasm::AssemblerBufferCache* assembler_buffer_cache_ = nullptr;
#endif // V8_ENABLE_WEBASSEMBLY #endif // V8_ENABLE_WEBASSEMBLY
AccountingAllocator* const allocator_; AccountingAllocator* const allocator_;
OptimizedCompilationInfo* const info_; OptimizedCompilationInfo* const info_;
...@@ -3075,8 +3082,10 @@ wasm::WasmCompilationResult Pipeline::GenerateCodeForWasmNativeStub( ...@@ -3075,8 +3082,10 @@ wasm::WasmCompilationResult Pipeline::GenerateCodeForWasmNativeStub(
wasm::WasmEngine* wasm_engine = wasm::GetWasmEngine(); wasm::WasmEngine* wasm_engine = wasm::GetWasmEngine();
ZoneStats zone_stats(wasm_engine->allocator()); ZoneStats zone_stats(wasm_engine->allocator());
NodeOriginTable* node_positions = graph->zone()->New<NodeOriginTable>(graph); 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, 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; std::unique_ptr<PipelineStatistics> pipeline_statistics;
if (FLAG_turbo_stats || FLAG_turbo_stats_nvp) { if (FLAG_turbo_stats || FLAG_turbo_stats_nvp) {
pipeline_statistics.reset(new PipelineStatistics( pipeline_statistics.reset(new PipelineStatistics(
...@@ -3172,7 +3181,8 @@ void Pipeline::GenerateCodeForWasmFunction( ...@@ -3172,7 +3181,8 @@ void Pipeline::GenerateCodeForWasmFunction(
CallDescriptor* call_descriptor, SourcePositionTable* source_positions, CallDescriptor* call_descriptor, SourcePositionTable* source_positions,
NodeOriginTable* node_origins, wasm::FunctionBody function_body, NodeOriginTable* node_origins, wasm::FunctionBody function_body,
const wasm::WasmModule* module, int function_index, 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(); auto* wasm_engine = wasm::GetWasmEngine();
base::TimeTicks start_time; base::TimeTicks start_time;
if (V8_UNLIKELY(FLAG_trace_wasm_compilation_times)) { if (V8_UNLIKELY(FLAG_trace_wasm_compilation_times)) {
...@@ -3183,7 +3193,7 @@ void Pipeline::GenerateCodeForWasmFunction( ...@@ -3183,7 +3193,7 @@ void Pipeline::GenerateCodeForWasmFunction(
CreatePipelineStatistics(function_body, module, info, &zone_stats)); CreatePipelineStatistics(function_body, module, info, &zone_stats));
PipelineData data(&zone_stats, wasm_engine, info, mcgraph, PipelineData data(&zone_stats, wasm_engine, info, mcgraph,
pipeline_statistics.get(), source_positions, node_origins, pipeline_statistics.get(), source_positions, node_origins,
WasmAssemblerOptions()); WasmAssemblerOptions(), buffer_cache);
PipelineImpl pipeline(&data); PipelineImpl pipeline(&data);
......
...@@ -23,11 +23,10 @@ class ProfileDataFromFile; ...@@ -23,11 +23,10 @@ class ProfileDataFromFile;
class RegisterConfiguration; class RegisterConfiguration;
namespace wasm { namespace wasm {
class AssemblerBufferCache;
struct CompilationEnv; struct CompilationEnv;
struct FunctionBody; struct FunctionBody;
class NativeModule;
struct WasmCompilationResult; struct WasmCompilationResult;
class WasmEngine;
struct WasmModule; struct WasmModule;
class WireBytesStorage; class WireBytesStorage;
} // namespace wasm } // namespace wasm
...@@ -61,7 +60,8 @@ class Pipeline : public AllStatic { ...@@ -61,7 +60,8 @@ class Pipeline : public AllStatic {
CallDescriptor* call_descriptor, SourcePositionTable* source_positions, CallDescriptor* call_descriptor, SourcePositionTable* source_positions,
NodeOriginTable* node_origins, wasm::FunctionBody function_body, NodeOriginTable* node_origins, wasm::FunctionBody function_body,
const wasm::WasmModule* module, int function_index, 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. // Run the pipeline on a machine graph and generate code.
static wasm::WasmCompilationResult GenerateCodeForWasmNativeStub( static wasm::WasmCompilationResult GenerateCodeForWasmNativeStub(
......
...@@ -8450,7 +8450,7 @@ base::Vector<const char> GetDebugName(Zone* zone, ...@@ -8450,7 +8450,7 @@ base::Vector<const char> GetDebugName(Zone* zone,
wasm::WasmCompilationResult ExecuteTurbofanWasmCompilation( wasm::WasmCompilationResult ExecuteTurbofanWasmCompilation(
wasm::CompilationEnv* env, const wasm::WireBytesStorage* wire_byte_storage, wasm::CompilationEnv* env, const wasm::WireBytesStorage* wire_byte_storage,
const wasm::FunctionBody& func_body, int func_index, Counters* counters, 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 // Check that we do not accidentally compile a Wasm function to TurboFan if
// --liftoff-only is set. // --liftoff-only is set.
DCHECK(!FLAG_liftoff_only); DCHECK(!FLAG_liftoff_only);
...@@ -8510,9 +8510,10 @@ wasm::WasmCompilationResult ExecuteTurbofanWasmCompilation( ...@@ -8510,9 +8510,10 @@ wasm::WasmCompilationResult ExecuteTurbofanWasmCompilation(
return wasm::WasmCompilationResult{}; return wasm::WasmCompilationResult{};
} }
Pipeline::GenerateCodeForWasmFunction( Pipeline::GenerateCodeForWasmFunction(&info, env, wire_byte_storage, mcgraph,
&info, env, wire_byte_storage, mcgraph, call_descriptor, source_positions, call_descriptor, source_positions,
node_origins, func_body, env->module, func_index, &loop_infos); node_origins, func_body, env->module,
func_index, &loop_infos, buffer_cache);
if (counters) { if (counters) {
int zone_bytes = int zone_bytes =
......
...@@ -48,6 +48,7 @@ enum class BranchHint : uint8_t; ...@@ -48,6 +48,7 @@ enum class BranchHint : uint8_t;
} // namespace compiler } // namespace compiler
namespace wasm { namespace wasm {
class AssemblerBufferCache;
struct DecodeStruct; struct DecodeStruct;
// Expose {Node} and {Graph} opaquely as {wasm::TFNode} and {wasm::TFGraph}. // Expose {Node} and {Graph} opaquely as {wasm::TFNode} and {wasm::TFGraph}.
using TFNode = compiler::Node; using TFNode = compiler::Node;
...@@ -64,7 +65,7 @@ namespace compiler { ...@@ -64,7 +65,7 @@ namespace compiler {
wasm::WasmCompilationResult ExecuteTurbofanWasmCompilation( wasm::WasmCompilationResult ExecuteTurbofanWasmCompilation(
wasm::CompilationEnv*, const wasm::WireBytesStorage* wire_bytes_storage, wasm::CompilationEnv*, const wasm::WireBytesStorage* wire_bytes_storage,
const wasm::FunctionBody&, int func_index, Counters*, 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 // 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 // type of the target function/callable and whether the signature matches the
......
...@@ -144,7 +144,8 @@ WasmCompilationResult WasmCompilationUnit::ExecuteFunctionCompilation( ...@@ -144,7 +144,8 @@ WasmCompilationResult WasmCompilationUnit::ExecuteFunctionCompilation(
case ExecutionTier::kTurbofan: case ExecutionTier::kTurbofan:
result = compiler::ExecuteTurbofanWasmCompilation( 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_; result.for_debugging = for_debugging_;
break; break;
} }
......
...@@ -998,13 +998,14 @@ class CodeGeneratorTester { ...@@ -998,13 +998,14 @@ class CodeGeneratorTester {
i++; i++;
} }
static constexpr size_t kMaxUnoptimizedFrameHeight = 0; constexpr size_t kMaxUnoptimizedFrameHeight = 0;
static constexpr size_t kMaxPushedArgumentCount = 0; constexpr size_t kMaxPushedArgumentCount = 0;
constexpr wasm::AssemblerBufferCache* kNoBufferCache = nullptr;
generator_ = new CodeGenerator( generator_ = new CodeGenerator(
environment->main_zone(), &frame_, &linkage_, environment->main_zone(), &frame_, &linkage_,
environment->instructions(), &info_, environment->main_isolate(), environment->instructions(), &info_, environment->main_isolate(),
base::Optional<OsrHelper>(), kNoSourcePosition, nullptr, base::Optional<OsrHelper>(), kNoSourcePosition, nullptr,
AssemblerOptions::Default(environment->main_isolate()), AssemblerOptions::Default(environment->main_isolate()), kNoBufferCache,
Builtin::kNoBuiltinId, kMaxUnoptimizedFrameHeight, Builtin::kNoBuiltinId, kMaxUnoptimizedFrameHeight,
kMaxPushedArgumentCount); 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