[BUILD] Add v8_use_zlib flag

This allows V8 to be compiled without zlib.

Currently we use zlib for 3 features:
1. Snapshot compression. The cl asserts v8_snapshot_compression
   implies v8_use_zlib.
2. Compression of translation arrays (experimental flag). The runtime
   flag is only enabled if v8_use_zlib.
3. Snapshot checksums. We fallback to a simple Fletcher algorithm if
   v8_use_zlib is false.

Change-Id: If043c3c21bba4d734573d7e1199d3ddf17b84f41
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3833817
Commit-Queue: Victor Gomes <victorgomes@chromium.org>
Reviewed-by: Jakob Linke <jgruber@chromium.org>
Reviewed-by: Camillo Bruni <cbruni@chromium.org>
Cr-Commit-Position: refs/heads/main@{#82527}
This commit is contained in:
Victor Gomes 2022-08-17 09:35:35 +02:00 committed by V8 LUCI CQ
parent 0c9083b59e
commit 980b623494
5 changed files with 63 additions and 14 deletions

View File

@ -377,6 +377,10 @@ declare_args() {
# Enables pointer compression for 8GB heaps.
# Sets -DV8_COMPRESS_POINTERS_8GB.
v8_enable_pointer_compression_8gb = ""
# Compile V8 using zlib as dependency.
# Sets -DV8_USE_ZLIB
v8_use_zlib = true
}
# Derived defaults.
@ -622,6 +626,9 @@ if (v8_fuchsia_use_vmex_resource) {
assert(target_os == "fuchsia", "VMEX resource only available on Fuchsia")
}
assert(!v8_enable_snapshot_compression || v8_use_zlib,
"Snapshot compression requires zlib")
v8_random_seed = "314159265"
v8_toolset_for_shell = "host"
@ -1080,6 +1087,9 @@ config("features") {
if (v8_enable_pointer_compression_8gb) {
defines += [ "V8_COMPRESS_POINTERS_8GB" ]
}
if (v8_use_zlib) {
defines += [ "V8_USE_ZLIB" ]
}
}
config("toolchain") {
@ -3544,7 +3554,6 @@ v8_header_set("v8_internal_headers") {
"src/snapshot/serializer.h",
"src/snapshot/shared-heap-deserializer.h",
"src/snapshot/shared-heap-serializer.h",
"src/snapshot/snapshot-compression.h",
"src/snapshot/snapshot-data.h",
"src/snapshot/snapshot-source-sink.h",
"src/snapshot/snapshot-utils.h",
@ -3607,6 +3616,10 @@ v8_header_set("v8_internal_headers") {
"src/zone/zone.h",
]
if (v8_enable_snapshot_compression) {
sources += [ "src/snapshot/snapshot-compression.h" ]
}
if (v8_use_perfetto) {
sources -= [ "//base/trace_event/common/trace_event_common.h" ]
}
@ -4751,7 +4764,6 @@ v8_source_set("v8_base_without_compiler") {
"src/snapshot/serializer.cc",
"src/snapshot/shared-heap-deserializer.cc",
"src/snapshot/shared-heap-serializer.cc",
"src/snapshot/snapshot-compression.cc",
"src/snapshot/snapshot-data.cc",
"src/snapshot/snapshot-source-sink.cc",
"src/snapshot/snapshot-utils.cc",
@ -4790,6 +4802,10 @@ v8_source_set("v8_base_without_compiler") {
"src/zone/zone.cc",
]
if (v8_enable_snapshot_compression) {
sources += [ "src/snapshot/snapshot-compression.cc" ]
}
if (v8_enable_maglev) {
sources += [
"src/maglev/maglev-code-generator.cc",
@ -5226,10 +5242,12 @@ v8_source_set("v8_base_without_compiler") {
]
}
deps += [
"//third_party/zlib",
"//third_party/zlib/google:compression_utils_portable",
]
if (v8_use_zlib) {
deps += [
"//third_party/zlib",
"//third_party/zlib/google:compression_utils_portable",
]
}
if (v8_postmortem_support) {
sources += [ "$target_gen_dir/debug-support.cc" ]

View File

@ -7,13 +7,17 @@
#include "src/base/vlq.h"
#include "src/deoptimizer/translated-state.h"
#include "src/objects/fixed-array-inl.h"
#ifdef V8_USE_ZLIB
#include "third_party/zlib/google/compression_utils_portable.h"
#endif // V8_USE_ZLIB
namespace v8 {
namespace internal {
namespace {
#ifdef V8_USE_ZLIB
// Constants describing compressed TranslationArray layout. Only relevant if
// --turbo-compress-translation-arrays is enabled.
constexpr int kUncompressedSizeOffset = 0;
@ -21,12 +25,14 @@ constexpr int kUncompressedSizeSize = kInt32Size;
constexpr int kCompressedDataOffset =
kUncompressedSizeOffset + kUncompressedSizeSize;
constexpr int kTranslationArrayElementSize = kInt32Size;
#endif // V8_USE_ZLIB
} // namespace
TranslationArrayIterator::TranslationArrayIterator(TranslationArray buffer,
int index)
: buffer_(buffer), index_(index) {
#ifdef V8_USE_ZLIB
if (V8_UNLIKELY(FLAG_turbo_compress_translation_arrays)) {
const int size = buffer_.get_int(kUncompressedSizeOffset);
uncompressed_contents_.insert(uncompressed_contents_.begin(), size, 0);
@ -41,9 +47,11 @@ TranslationArrayIterator::TranslationArrayIterator(TranslationArray buffer,
buffer_.DataSize()),
Z_OK);
DCHECK(index >= 0 && index < size);
} else {
DCHECK(index >= 0 && index < buffer.length());
return;
}
#endif // V8_USE_ZLIB
DCHECK(!FLAG_turbo_compress_translation_arrays);
DCHECK(index >= 0 && index < buffer.length());
}
int32_t TranslationArrayIterator::Next() {
@ -121,6 +129,7 @@ void TranslationArrayBuilder::AddDoubleRegister(DoubleRegister reg) {
Handle<TranslationArray> TranslationArrayBuilder::ToTranslationArray(
Factory* factory) {
#ifdef V8_USE_ZLIB
if (V8_UNLIKELY(FLAG_turbo_compress_translation_arrays)) {
const int input_size = SizeInBytes();
uLongf compressed_data_size = compressBound(input_size);
@ -144,13 +153,14 @@ Handle<TranslationArray> TranslationArrayBuilder::ToTranslationArray(
compressed_data.data(), compressed_data_size);
return result;
} else {
Handle<TranslationArray> result =
factory->NewByteArray(SizeInBytes(), AllocationType::kOld);
memcpy(result->GetDataStartAddress(), contents_.data(),
contents_.size() * sizeof(uint8_t));
return result;
}
#endif
DCHECK(!FLAG_turbo_compress_translation_arrays);
Handle<TranslationArray> result =
factory->NewByteArray(SizeInBytes(), AllocationType::kOld);
memcpy(result->GetDataStartAddress(), contents_.data(),
contents_.size() * sizeof(uint8_t));
return result;
}
void TranslationArrayBuilder::BeginBuiltinContinuationFrame(

View File

@ -957,8 +957,13 @@ DEFINE_BOOL(
stress_gc_during_compilation, false,
"simulate GC/compiler thread race related to https://crbug.com/v8/8520")
DEFINE_BOOL(turbo_fast_api_calls, true, "enable fast API calls from TurboFan")
#ifdef V8_USE_ZLIB
DEFINE_BOOL(turbo_compress_translation_arrays, false,
"compress translation arrays (experimental)")
#else
DEFINE_BOOL_READONLY(turbo_compress_translation_arrays, false,
"compress translation arrays (experimental)")
#endif // V8_USE_ZLIB
DEFINE_BOOL(turbo_inline_js_wasm_calls, true, "inline JS->Wasm calls")
DEFINE_BOOL(turbo_use_mid_tier_regalloc_for_huge_functions, true,
"fall back to the mid-tier register allocator for huge functions")

View File

@ -5,7 +5,10 @@
#include "src/snapshot/snapshot-utils.h"
#include "src/base/sanitizer/msan.h"
#ifdef V8_USE_ZLIB
#include "third_party/zlib/zlib.h"
#endif
namespace v8 {
namespace internal {
@ -16,9 +19,20 @@ uint32_t Checksum(base::Vector<const byte> payload) {
// Mark every object as initialized in the code serializer.
MSAN_MEMORY_IS_INITIALIZED(payload.begin(), payload.length());
#endif // MEMORY_SANITIZER
#ifdef V8_USE_ZLIB
// Priming the adler32 call so it can see what CPU features are available.
adler32(0, nullptr, 0);
return static_cast<uint32_t>(adler32(0, payload.begin(), payload.length()));
#else
// Simple Fletcher-32.
uint32_t sum1 = 0, sum2 = 0;
for (auto data : payload) {
sum1 = (sum1 + data) % 65535;
sum2 = (sum2 + sum1) % 65535;
}
return (sum2 << 16 | sum1);
#endif
}
} // namespace internal

View File

@ -437,6 +437,7 @@ static void SerializeContext(base::Vector<const byte>* startup_blob_out,
v8_isolate->Dispose();
}
#ifdef SNAPSHOT_COMPRESSION
UNINITIALIZED_TEST(SnapshotCompression) {
DisableAlwaysOpt();
base::Vector<const byte> startup_blob;
@ -457,6 +458,7 @@ UNINITIALIZED_TEST(SnapshotCompression) {
shared_space_blob.Dispose();
context_blob.Dispose();
}
#endif // SNAPSHOT_COMPRESSION
UNINITIALIZED_TEST(ContextSerializerContext) {
DisableAlwaysOpt();