Skip to content

Commit

Permalink
Allow binary reader to skip function bodies. NFC (#1871)
Browse files Browse the repository at this point in the history
This is purely a performance optimization for wasm-objdump since it
doesn't always need to decode function bodies.

For a very large wasm file that I was testing this takes the time taken
for `wasm-objdump -h` from 8.6s to 1.5s.
  • Loading branch information
sbc100 authored Mar 23, 2022
1 parent 41a4541 commit 27c5d11
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 31 deletions.
2 changes: 2 additions & 0 deletions src/binary-reader-objdump.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2354,6 +2354,7 @@ Result ReadBinaryObjdump(const uint8_t* data,

switch (options->mode) {
case ObjdumpMode::Prepass: {
read_options.skip_function_bodies = true;
BinaryReaderObjdumpPrepass reader(data, size, options, state);
return ReadBinary(data, size, &reader, read_options);
}
Expand All @@ -2362,6 +2363,7 @@ Result ReadBinaryObjdump(const uint8_t* data,
return ReadBinary(data, size, &reader, read_options);
}
default: {
read_options.skip_function_bodies = true;
BinaryReaderObjdump reader(data, size, options, state);
return ReadBinary(data, size, &reader, read_options);
}
Expand Down
6 changes: 5 additions & 1 deletion src/binary-reader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2717,7 +2717,11 @@ Result BinaryReader::ReadCodeSection(Offset section_size) {
CALLBACK(OnLocalDecl, k, num_local_types, local_type);
}

CHECK_RESULT(ReadFunctionBody(end_offset));
if (options_.skip_function_bodies) {
state_.offset = end_offset;
} else {
CHECK_RESULT(ReadFunctionBody(end_offset));
}

CALLBACK(EndFunctionBody, func_index);
}
Expand Down
1 change: 1 addition & 0 deletions src/binary-reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ struct ReadBinaryOptions {
bool read_debug_names = false;
bool stop_on_first_error = true;
bool fail_on_custom_section_error = true;
bool skip_function_bodies = false;
};

// TODO: Move somewhere else?
Expand Down
12 changes: 2 additions & 10 deletions test/binary/bad-opcode-prefix.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
;;; TOOL: run-objdump-gen-wasm
;;; ERROR: 1
;;; TOOL: run-gen-wasm-bad
magic
version
section(TYPE) { count[1] function params[0] results[1] i32 }
Expand All @@ -13,12 +12,5 @@ section(CODE) {
}
(;; STDERR ;;;
000001a: error: unexpected opcode: 0xfe 0x7f
000001a: error: unexpected opcode: 0xfe 0x7f
;;; STDERR ;;)
(;; STDOUT ;;;
bad-opcode-prefix.wasm: file format wasm 0x1
Code Disassembly:
000017 func[0]:
;;; STDOUT ;;)
22 changes: 2 additions & 20 deletions test/binary/bad-opcode.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
;;; TOOL: run-objdump-gen-wasm
;;; ARGS: -x
;;; ERROR: 1
;;; TOOL: run-gen-wasm-bad
magic
version
section(TYPE) { count[1] function params[0] results[1] i32 }
Expand All @@ -14,21 +12,5 @@ section(CODE) {
}
(;; STDERR ;;;
0000019: error: unexpected opcode: 0xff
0000019: error: unexpected opcode: 0xff
;;; STDERR ;;)
(;; STDOUT ;;;
bad-opcode.wasm: file format wasm 0x1
Section Details:
Type[1]:
- type[0] () -> i32
Function[1]:
- func[0] sig=0
Code[1]:
- func[0] size=3
Code Disassembly:
000017 func[0]:
;;; STDOUT ;;)

0 comments on commit 27c5d11

Please sign in to comment.