From 39e35de7201042779d99252ccb910faf211a11dd Mon Sep 17 00:00:00 2001 From: James Stine Date: Tue, 8 Apr 2025 14:55:43 -0500 Subject: [PATCH 1/6] add Python script used to analyze an ELF file to see instructions. Used it for benchmarks to see which instructions are being utilized. --- bin/analyze_riscv_elf.py | 61 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100755 bin/analyze_riscv_elf.py diff --git a/bin/analyze_riscv_elf.py b/bin/analyze_riscv_elf.py new file mode 100755 index 000000000..24546ca4a --- /dev/null +++ b/bin/analyze_riscv_elf.py @@ -0,0 +1,61 @@ +#!/usr/bin/env python3 + +""" +james.stine@okstate.edu 8 April 2025 + +Purpose: Analyze a RISC-V ELF file and report which instructions are used, + along with their frequency. The script disassembles the ELF using + riscv64-unknown-elf-objdump, filters real instructions (excluding + pseudo-ops and section headers), and displays a histogram of + instruction use. +""" + +import subprocess +import sys +import re +from collections import Counter + +def disassemble_elf(elf_path): + try: + result = subprocess.run( + ["riscv64-unknown-elf-objdump", "-d", "-M", "no-aliases", elf_path], + capture_output=True, + text=True, + check=True + ) + return result.stdout + except subprocess.CalledProcessError as e: + print(f"Error running objdump: {e.stderr}") + sys.exit(1) + +def extract_instructions(disassembly): + instructions = [] + for line in disassembly.splitlines(): + # Match instruction lines only: address: machine-code instruction [operands...] + match = re.match(r'^\s*[0-9a-f]+:\s+([0-9a-f]{8}|\s{8})\s+(\S+)', line) + if match: + instr = match.group(2) + instructions.append(instr) + return instructions + +def main(): + if len(sys.argv) != 2: + print("Usage: python3 analyze_riscv_elf.py ") + sys.exit(1) + + elf_path = sys.argv[1] + disassembly = disassemble_elf(elf_path) + instructions = extract_instructions(disassembly) + counter = Counter(instructions) + + print(f"\nInstruction usage in {elf_path}:\n") + if not counter: + print("No instructions found (did you use the correct target ELF file?)") + else: + for instr, count in counter.most_common(): + count_str = f"{count:,}" # Add comma as thousands separator + print(f"{instr:<10} {count_str}") + +if __name__ == "__main__": + main() + From 91e2c150e2e35bfaec1e5a268144a49db6a138d4 Mon Sep 17 00:00:00 2001 From: James Stine Date: Tue, 8 Apr 2025 17:06:55 -0500 Subject: [PATCH 2/6] add SPDX header + change order for lint --- bin/analyze_riscv_elf.py | 41 ++++++++++++++++++++++++++++++---------- 1 file changed, 31 insertions(+), 10 deletions(-) diff --git a/bin/analyze_riscv_elf.py b/bin/analyze_riscv_elf.py index 24546ca4a..fa0a26b94 100755 --- a/bin/analyze_riscv_elf.py +++ b/bin/analyze_riscv_elf.py @@ -1,19 +1,40 @@ #!/usr/bin/env python3 -""" -james.stine@okstate.edu 8 April 2025 - -Purpose: Analyze a RISC-V ELF file and report which instructions are used, - along with their frequency. The script disassembles the ELF using - riscv64-unknown-elf-objdump, filters real instructions (excluding - pseudo-ops and section headers), and displays a histogram of - instruction use. -""" +########################################### +## analyze_riscv_elf.py +## +## Written: james.stine@okstate.edu +## Created: April 7, 2025 +## +## Purpose: Analyze a RISC-V ELF file and report which instructions are used, +## along with their frequency. The script disassembles the ELF using +## riscv64-unknown-elf-objdump, filters real instructions (excluding +## pseudo-ops and section headers), and displays a histogram of +## instruction use.Simulate a L1 D$ or I$ for comparison with Wally +## +## A component of the CORE-V-WALLY configurable RISC-V project. +## https://github.com/openhwgroup/cvw +## +## Copyright (C) 2021-25 Harvey Mudd College & Oklahoma State University +## +## SPDX-License-Identifier: Apache-2.0 WITH SHL-2.1 +## +## Licensed under the Solderpad Hardware License v 2.1 (the “License”); you may not use this file +## except in compliance with the License, or, at your option, the Apache License version 2.0. You +## may obtain a copy of the License at +## +## https:##solderpad.org/licenses/SHL-2.1/ +## +## Unless required by applicable law or agreed to in writing, any work distributed under the +## License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, +## either express or implied. See the License for the specific language governing permissions +## and limitations under the License. +################################################################################################ +from collections import Counter import subprocess import sys import re -from collections import Counter def disassemble_elf(elf_path): try: From 4f09d5a5d25bd480f67b51bc62cd9c63ca8987f4 Mon Sep 17 00:00:00 2001 From: James Stine Date: Tue, 8 Apr 2025 17:10:43 -0500 Subject: [PATCH 3/6] update order of standard grouping for lint --- bin/analyze_riscv_elf.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bin/analyze_riscv_elf.py b/bin/analyze_riscv_elf.py index fa0a26b94..a5a9ce2e1 100755 --- a/bin/analyze_riscv_elf.py +++ b/bin/analyze_riscv_elf.py @@ -31,10 +31,10 @@ ## and limitations under the License. ################################################################################################ -from collections import Counter +import re import subprocess import sys -import re +from collections import Counter def disassemble_elf(elf_path): try: From 588c11d993772bbc68706b4ac9461c56a3b6514a Mon Sep 17 00:00:00 2001 From: James Stine Date: Tue, 8 Apr 2025 17:13:30 -0500 Subject: [PATCH 4/6] add import os to fix lint --- bin/analyze_riscv_elf.py | 1 + 1 file changed, 1 insertion(+) diff --git a/bin/analyze_riscv_elf.py b/bin/analyze_riscv_elf.py index a5a9ce2e1..74925758b 100755 --- a/bin/analyze_riscv_elf.py +++ b/bin/analyze_riscv_elf.py @@ -34,6 +34,7 @@ import re import subprocess import sys +import os from collections import Counter def disassemble_elf(elf_path): From 66a56888f6097c8c4d58bf5cc3dde73db5c2b1c3 Mon Sep 17 00:00:00 2001 From: James Stine Date: Tue, 8 Apr 2025 17:14:27 -0500 Subject: [PATCH 5/6] fix order for lint --- bin/analyze_riscv_elf.py | 1 - 1 file changed, 1 deletion(-) diff --git a/bin/analyze_riscv_elf.py b/bin/analyze_riscv_elf.py index 74925758b..a5a9ce2e1 100755 --- a/bin/analyze_riscv_elf.py +++ b/bin/analyze_riscv_elf.py @@ -34,7 +34,6 @@ import re import subprocess import sys -import os from collections import Counter def disassemble_elf(elf_path): From f42900a6dce155a92e368795d95de46a91eacbb0 Mon Sep 17 00:00:00 2001 From: James Stine Date: Tue, 8 Apr 2025 17:15:47 -0500 Subject: [PATCH 6/6] fix with ruff command --- bin/analyze_riscv_elf.py | 1 + 1 file changed, 1 insertion(+) diff --git a/bin/analyze_riscv_elf.py b/bin/analyze_riscv_elf.py index a5a9ce2e1..6794073a3 100755 --- a/bin/analyze_riscv_elf.py +++ b/bin/analyze_riscv_elf.py @@ -36,6 +36,7 @@ import subprocess import sys from collections import Counter + def disassemble_elf(elf_path): try: result = subprocess.run(