Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disassembler: Core Optimization 1-1 (Hash table and Caching) #88

Open
wants to merge 3 commits into
base: riscv-dis-opt1-req-reorganize
Choose a base branch
from

Conversation

a4lg
Copy link
Owner

@a4lg a4lg commented Nov 17, 2022

@a4lg a4lg force-pushed the riscv-dis-opt1-req-reorganize branch from 6c23d80 to 992c0f5 Compare November 18, 2022 00:12
@a4lg a4lg force-pushed the riscv-dis-opt1-1-hashtable-and-caching branch 2 times, most recently from 444fbaa to b83e54a Compare November 18, 2022 04:58
@a4lg a4lg force-pushed the riscv-dis-opt1-req-reorganize branch from 76a3a4e to d312be0 Compare November 19, 2022 02:38
@a4lg a4lg force-pushed the riscv-dis-opt1-1-hashtable-and-caching branch 2 times, most recently from 9fdf0f8 to f99c8ca Compare November 19, 2022 07:15
@a4lg a4lg force-pushed the riscv-dis-opt1-req-reorganize branch from 68d5006 to 452c070 Compare November 19, 2022 11:57
@a4lg a4lg force-pushed the riscv-dis-opt1-1-hashtable-and-caching branch from f99c8ca to 297008a Compare November 19, 2022 11:57
@a4lg a4lg added the enhancement New feature or request label Nov 19, 2022
@a4lg a4lg force-pushed the riscv-dis-opt1-req-reorganize branch from 452c070 to f3fcf98 Compare November 20, 2022 01:02
@a4lg a4lg force-pushed the riscv-dis-opt1-1-hashtable-and-caching branch 2 times, most recently from 844db36 to f3378df Compare November 21, 2022 02:21
@a4lg a4lg force-pushed the riscv-dis-opt1-req-reorganize branch from 8712c11 to d4b20c6 Compare November 22, 2022 01:32
@a4lg a4lg force-pushed the riscv-dis-opt1-1-hashtable-and-caching branch from f3378df to a452e06 Compare November 22, 2022 01:32
@a4lg a4lg force-pushed the riscv-dis-opt1-req-reorganize branch from d4b20c6 to 9b08b66 Compare November 22, 2022 09:00
@a4lg a4lg force-pushed the riscv-dis-opt1-1-hashtable-and-caching branch 2 times, most recently from 515d02e to 29236db Compare November 23, 2022 03:04
@a4lg a4lg force-pushed the riscv-dis-opt1-req-reorganize branch from 1e28527 to 5d5bbd6 Compare November 23, 2022 08:44
@a4lg a4lg force-pushed the riscv-dis-opt1-1-hashtable-and-caching branch from 29236db to 3ddff53 Compare November 23, 2022 08:44
@a4lg a4lg force-pushed the riscv-dis-opt1-req-reorganize branch from 5d5bbd6 to d98213b Compare November 24, 2022 03:38
@a4lg a4lg force-pushed the riscv-dis-opt1-1-hashtable-and-caching branch 5 times, most recently from 8c9fc20 to 340fee3 Compare November 25, 2022 02:39
@a4lg a4lg force-pushed the riscv-dis-opt1-req-reorganize branch from 0cd8ace to fe45631 Compare November 25, 2022 12:46
@a4lg a4lg force-pushed the riscv-dis-opt1-1-hashtable-and-caching branch from 340fee3 to 8478c28 Compare November 25, 2022 12:46
@a4lg a4lg force-pushed the riscv-dis-opt1-req-reorganize branch from fe45631 to 7c9633c Compare November 26, 2022 00:28
@a4lg a4lg force-pushed the riscv-dis-opt1-1-hashtable-and-caching branch 2 times, most recently from 0e92186 to e679d61 Compare November 27, 2022 09:10
@a4lg a4lg force-pushed the riscv-dis-opt1-1-hashtable-and-caching branch 3 times, most recently from f8d4788 to d08c88a Compare August 7, 2023 01:23
@a4lg a4lg force-pushed the riscv-dis-opt1-req-reorganize branch from 1ad9136 to 899020c Compare August 7, 2023 02:55
@a4lg a4lg force-pushed the riscv-dis-opt1-1-hashtable-and-caching branch from d08c88a to 3c7f4fd Compare August 7, 2023 02:56
@a4lg a4lg force-pushed the riscv-dis-opt1-req-reorganize branch from 899020c to cbd4e63 Compare August 8, 2023 03:19
@a4lg a4lg force-pushed the riscv-dis-opt1-1-hashtable-and-caching branch from 3c7f4fd to 3795893 Compare August 8, 2023 03:19
@a4lg a4lg force-pushed the riscv-dis-opt1-req-reorganize branch from cbd4e63 to 44387b5 Compare August 8, 2023 04:11
@a4lg a4lg force-pushed the riscv-dis-opt1-1-hashtable-and-caching branch from 3795893 to 36bfa28 Compare August 8, 2023 04:11
@a4lg a4lg force-pushed the riscv-dis-opt1-req-reorganize branch from 44387b5 to ce839d4 Compare August 11, 2023 01:53
@a4lg a4lg force-pushed the riscv-dis-opt1-1-hashtable-and-caching branch 2 times, most recently from 6921cd1 to ebdb18b Compare August 11, 2023 03:56
@a4lg a4lg force-pushed the riscv-dis-opt1-req-reorganize branch from 58f4e93 to 612847a Compare August 15, 2023 06:37
@a4lg a4lg force-pushed the riscv-dis-opt1-1-hashtable-and-caching branch from ebdb18b to 28513f5 Compare August 15, 2023 06:37
@a4lg a4lg force-pushed the riscv-dis-opt1-req-reorganize branch from 612847a to 7d54ac5 Compare August 15, 2023 07:27
@a4lg a4lg force-pushed the riscv-dis-opt1-1-hashtable-and-caching branch from 28513f5 to 491c911 Compare August 15, 2023 07:27
@a4lg a4lg force-pushed the riscv-dis-opt1-1-hashtable-and-caching branch from 491c911 to f1b6d8a Compare September 3, 2023 02:26
@a4lg a4lg force-pushed the riscv-dis-opt1-req-reorganize branch from 43c246b to da19323 Compare September 3, 2023 02:41
@a4lg a4lg force-pushed the riscv-dis-opt1-1-hashtable-and-caching branch from f1b6d8a to b83efe1 Compare September 3, 2023 02:42
@a4lg a4lg force-pushed the riscv-dis-opt1-req-reorganize branch from da19323 to fe6fd92 Compare September 5, 2023 07:58
@a4lg a4lg force-pushed the riscv-dis-opt1-1-hashtable-and-caching branch from b83efe1 to 03c8645 Compare September 5, 2023 07:58
@a4lg a4lg force-pushed the riscv-dis-opt1-req-reorganize branch from fe6fd92 to a170be4 Compare September 7, 2023 09:35
@a4lg a4lg force-pushed the riscv-dis-opt1-1-hashtable-and-caching branch from 03c8645 to 37af5cd Compare September 7, 2023 09:35
@a4lg a4lg force-pushed the riscv-dis-opt1-1-hashtable-and-caching branch from 37af5cd to 84734db Compare October 16, 2023 09:05
@a4lg a4lg force-pushed the riscv-dis-opt1-req-reorganize branch from 58372fa to 537ca14 Compare October 19, 2023 03:17
@a4lg a4lg force-pushed the riscv-dis-opt1-1-hashtable-and-caching branch from 84734db to 30b8505 Compare October 19, 2023 03:17
a4lg added 3 commits October 19, 2023 06:58
This commit improves performance on disassembling RISC-V code.
It replaces riscv_hash (in opcodes/riscv-dis.c) with much faster data
structure: a sorted and partitioned hash table.

This is a technique actually used on SPARC architecture
(opcodes/sparc-dis.c) and the author simplified the algorithm even further.
Unlike SPARC, RISC-V's hashed opcode table is not a table to linked lists,
it's just a table, pointing "start" elements in the sorted opcode list
(per hash code) and a global tail.

It is expected to have 20-40% performance improvements when disassembling
linked RISC-V ELF programs using objdump.  That is a significant improvement
and pretty nice for such a small modification
(with about 12KB heap memory allocation on 64-bit environment).

This is not the end.  This structure significantly improves plain binary
file handling (on objdump, "objdump -b binary -m riscv:rv[32|64] -D $FILE").

The author tested on various binary files including random one and big
vmlinux images and confirmed significant performance improvements (>70%
on many cases).  This is partially due to the fact that, disassembling about
one quarter of invalid "instruction" words required iterating over one
thousand opcode entries (348 or more being vector instructions with OP-V,
that can be easily skipped with this new data structure).  Another reason
for this significance is it doesn't have various ELF overhead.

opcodes/ChangeLog:

	* riscv-dis.c (init_riscv_dis_state_for_arch_and_options): Build
	the hash table on the first run.
	(OP_HASH_LEN): Move from riscv_disassemble_insn.
	(OP_HASH_IDX): Move from riscv_disassemble_insn and mask by
	OP_MASK_OP2 == 0x03 for only real 16-bit instructions.
	(riscv_hash): New sorted and partitioned hash table.
	(riscv_opcodes_sorted): New sorted opcode table.
	(compare_opcodes): New function to compare RISC-V opcode entries.
	(build_riscv_opcodes_hash_table): New function to build faster
	hash table to disassemble.
	(riscv_disassemble_insn): Use sorted and partitioned hash table.
Although it does not have a problem on current GNU Binutils implementation,
if the custom vendor implements an instruction which spans across multiple
major opcodes (e.g. uses both CUSTOM_0 and CUSTOM_1 in a *single* custom
instruction), the original assumption of the sorted hash table breaks.

In this case, this commit enables the fallback mode to disable all
optimizations except filtering macros out.

Note that, if a such instruction (that disables this disassembler
optimization) is upstreamed to Binutils, a separate solution will be
required to avoid major performance degradation when such instruction is
not used.  The intent of this commit is to make a room for custom vendors
to implement such instructions in *their* tree without causing
disassembler problems.

opcodes/ChangeLog:

	* riscv-dis.c (is_riscv_hash_fallback) New.
	(build_riscv_opcodes_hash_table): If an instruction spans across
	multiple major opcodes, enable fallback mode and disable sorting.
	(riscv_disassemble_insn): If the fallback mode is enabled, scan
	through all instructions instead of scanning only instruction
	entries matching the hash value.
Calling riscv_subset_supports repeatedly harms the performance in a
measurable way (about 3-13% in total on the most cases).

As a simple solution, this commit now caches instruction class support
(whether specific instruction class is supported) as a signed char array.

It is expected to have 5-7% performance improvements when disassembling
linked RISC-V ELF programs using objdump but this is particularly effective
with programs with many CSR instructions (up to ~42% on the author's PC).

include/ChangeLog:

	* opcode/riscv.h (enum riscv_insn_class): Add NUM_INSN_CLASSES.

opcodes/ChangeLog:

	* riscv-dis.c (riscv_insn_support_cache) New.
	(init_riscv_dis_state_for_arch): Clear the instruction support
	cache.  (riscv_disassemble_insn): Cache the instruction support.
@a4lg a4lg force-pushed the riscv-dis-opt1-1-hashtable-and-caching branch from 30b8505 to 496ea7d Compare October 19, 2023 07:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant