From 0eae59594f7bbfdfeacf8c455b2186213ebb2e3a Mon Sep 17 00:00:00 2001 From: Frederik Gossen Date: Tue, 21 May 2019 12:55:55 +0200 Subject: [PATCH] [wasm-hints] Add tool to inject compilation hints into Wasm modules The python script allows to inject hints into a Wasm module. Hints are injected into a newly created custom section named "compilationHints" that is used by the compiler to determine its compilation strategy. The section is placed after the functions section and before the code section. Bug: v8:9003 Change-Id: I531c57e4269ff9ae42b95be3515d2409627d6fb9 Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1619865 Commit-Queue: Frederik Gossen Reviewed-by: Michael Starzinger Reviewed-by: Clemens Hammacher Reviewed-by: Michael Achenbach Cr-Commit-Position: refs/heads/master@{#61797} --- .../inject-compilation-hints.py | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100755 tools/wasm-compilation-hints/inject-compilation-hints.py diff --git a/tools/wasm-compilation-hints/inject-compilation-hints.py b/tools/wasm-compilation-hints/inject-compilation-hints.py new file mode 100755 index 0000000000..fd4b65b8ff --- /dev/null +++ b/tools/wasm-compilation-hints/inject-compilation-hints.py @@ -0,0 +1,60 @@ +#!/usr/bin/env python + +# Copyright 2019 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 argparse +import io +import sys + +from wasm import * + +FUNCTION_SECTION_ID = 3 + +def parse_args(): + parser = argparse.ArgumentParser(\ + description="Inject compilation hints into a Wasm module.") + parser.add_argument("-i", "--in-wasm-file", \ + type=str, \ + help="original wasm module") + parser.add_argument("-o", "--out-wasm-file", \ + type=str, \ + help="wasm module with injected hints") + parser.add_argument("-x", "--hints-file", \ + type=str, required=True, \ + help="binary hints file to be injected as a custom section " + \ + "'compilationHints'") + return parser.parse_args() + +if __name__ == "__main__": + args = parse_args() + in_wasm_file = args.in_wasm_file if args.in_wasm_file else sys.stdin.fileno() + out_wasm_file = args.out_wasm_file if args.out_wasm_file else sys.stdout.fileno() + hints_bs = open(args.hints_file, "rb").read() + with io.open(in_wasm_file, "rb") as fin: + with io.open(out_wasm_file, "wb") as fout: + magic_number, bs = read_magic_number(fin); + fout.write(bs) + version, bs = read_version(fin); + fout.write(bs) + num_declared_functions = None + while True: + id, bs = read_varuintN(fin) + fout.write(bs) + if id == None: + break + payload_length, bs = read_varuintN(fin) + fout.write(bs) + + # Peek into function section for upcoming validity check. + if id == FUNCTION_SECTION_ID: + num_declared_functions, bs = peek_varuintN(fin) + + bs = fin.read(payload_length) + fout.write(bs) + + # Instert hint section after function section. + if id == FUNCTION_SECTION_ID: + assert len(hints_bs) == num_declared_functions, "unexpected number of hints" + write_compilation_hints_section(fout, hints_bs)