v8/tools/wasm/code-size-factors.py
Clemens Backes 4e983705e5 [wasm] Tweak constants for estimating code space size
It turned out that on arm and arm64 we over-estimated the code size of a
Wasm module quite a bit. This CL adds some more output for the
--trace-wasm-compilation-times flag, and adds a script to compute the
factors we use for code size estimates from that output.
I ran the script on a few benchmarks (an older Epic module, the current
Photoshop module, and the benchmark from the linked bug), and adjusted
the constants accordingly.

Also, simplify the API of {ReservationSize} to only return a single
number, and fail internally if we need to allocate more than the engine
supports (which would only fail for artificially large modules).

R=jkummerow@chromium.org

Bug: chromium:1302310
Change-Id: I5b2c27ff3e360fb6738cf5dd697bcee09e106b6d
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3522067
Reviewed-by: Jakob Kummerow <jkummerow@chromium.org>
Reviewed-by: Maya Lekova <mslekova@chromium.org>
Commit-Queue: Clemens Backes <clemensb@chromium.org>
Cr-Commit-Position: refs/heads/main@{#79482}
2022-03-15 14:41:13 +00:00

80 lines
2.2 KiB
Python
Executable File

#!/usr/bin/env python3
# vim:fenc=utf-8:ts=2:sw=2:softtabstop=2:expandtab:
# Copyright 2022 the V8 project authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import sys
import re
liftoff_regex = re.compile('^Compiled function .* using Liftoff, '
'.*bodysize ([0-9]+) codesize ([0-9]+)$')
turbofan_regex = re.compile('^Compiled function .* using TurboFan, '
'.*bodysize ([0-9]+) codesize ([0-9]+) ')
wasm2js_regex = re.compile('^Compiled WasmToJS wrapper .* '
'codesize ([0-9]+)$')
def main():
print('Reading --trace-wasm-compilation-times lines from stdin...')
liftoff_values = []
turbofan_values = []
wasm2js_values = []
for line in sys.stdin:
match(line, liftoff_regex, liftoff_values)
match(line, turbofan_regex, turbofan_values)
match_wasm2js(line, wasm2js_values)
evaluate('Liftoff', liftoff_values)
evaluate('TurboFan', turbofan_values)
evaluate_wasm2js(wasm2js_values)
def match(line, regex, array):
m = regex.match(line)
if m:
array.append([int(m.group(1)), int(m.group(2))])
def match_wasm2js(line, array):
m = wasm2js_regex.match(line)
if m:
array.append(int(m.group(1)))
def evaluate(name, values):
n = len(values)
if n == 0:
print(f'No values for {name}')
return
print(f'Computing base and factor for {name} based on {n} values')
sum_xy = sum(x * y for [x, y] in values)
sum_x = sum(x for [x, y] in values)
sum_y = sum(y for [x, y] in values)
sum_xx = sum(x * x for [x, y] in values)
factor = (n * sum_xy - sum_x * sum_y) / (n * sum_xx - sum_x * sum_x)
base = (sum_y - factor * sum_x) / n
print(f'--> [{name}] Trend line: base: {base:.2f}, factor {factor:.2f}')
min_y = min(y for [x, y] in values)
simple_factor = (sum_y - n * min_y) / sum_x
print(f'--> [{name}] Simple analysis: Min {min_y}, '
f'factor {simple_factor:.2f}')
def evaluate_wasm2js(values):
n = len(values)
if n == 0:
print('No wasm2js wrappers')
return
print(f'--> [Wasm2js wrappers] {n} compiled, size min {min(values)}, '
f'max {max(values)}, avg {(sum(values) / n):.2f}')
main()