From d2719fa4f6ef69f79979b53d24abbd7b438c19d3 Mon Sep 17 00:00:00 2001 From: Maisam Arif Date: Sat, 25 May 2024 00:00:00 -0500 Subject: [PATCH] Temporary generator.py patch for amdsmi_bdf_t Signed-off-by: Maisam Arif Change-Id: I93157127524b87e2e1445dcdc8588e62530bf68c [ROCm/amdsmi commit: b39b39ef3ca9a7e13873ddad3ce3794798b69651] --- .../amdsmi/py-interface/amdsmi_wrapper.py | 32 +++++++------ projects/amdsmi/tools/generator.py | 47 ++++++++++++++++++- 2 files changed, 63 insertions(+), 16 deletions(-) diff --git a/projects/amdsmi/py-interface/amdsmi_wrapper.py b/projects/amdsmi/py-interface/amdsmi_wrapper.py index bb431ec0ef..3c41d5a140 100644 --- a/projects/amdsmi/py-interface/amdsmi_wrapper.py +++ b/projects/amdsmi/py-interface/amdsmi_wrapper.py @@ -736,6 +736,7 @@ struct_amdsmi_bdf_t._fields_ = [ ] union_amdsmi_bdf_t._pack_ = 1 # source:False + union_amdsmi_bdf_t._fields_ = [ ('struct_amdsmi_bdf_t', struct_amdsmi_bdf_t), ('as_uint', ctypes.c_uint64), @@ -758,19 +759,6 @@ amdsmi_card_form_factor_t = ctypes.c_uint32 # enum class struct_amdsmi_pcie_info_t(Structure): pass -class struct_pcie_static_(Structure): - pass - -struct_pcie_static_._pack_ = 1 # source:False -struct_pcie_static_._fields_ = [ - ('max_pcie_width', ctypes.c_uint16), - ('PADDING_0', ctypes.c_ubyte * 2), - ('max_pcie_speed', ctypes.c_uint32), - ('pcie_interface_version', ctypes.c_uint32), - ('slot_type', amdsmi_card_form_factor_t), - ('reserved', ctypes.c_uint64 * 10), -] - class struct_pcie_metric_(Structure): pass @@ -789,6 +777,19 @@ struct_pcie_metric_._fields_ = [ ('reserved', ctypes.c_uint64 * 13), ] +class struct_pcie_static_(Structure): + pass + +struct_pcie_static_._pack_ = 1 # source:False +struct_pcie_static_._fields_ = [ + ('max_pcie_width', ctypes.c_uint16), + ('PADDING_0', ctypes.c_ubyte * 2), + ('max_pcie_speed', ctypes.c_uint32), + ('pcie_interface_version', ctypes.c_uint32), + ('slot_type', amdsmi_card_form_factor_t), + ('reserved', ctypes.c_uint64 * 10), +] + struct_amdsmi_pcie_info_t._pack_ = 1 # source:False struct_amdsmi_pcie_info_t._fields_ = [ ('pcie_static', struct_pcie_static_), @@ -2756,5 +2757,6 @@ __all__ = \ 'struct_amdsmi_xgmi_info_t', 'struct_cache_', 'struct_engine_usage_', 'struct_fw_info_list_', 'struct_memory_usage_', 'struct_pcie_metric_', - 'struct_pcie_static_', 'uint32_t', 'uint64_t', 'uint8_t', - 'union_amdsmi_bdf_t', 'struct_amdsmi_bdf_t'] \ No newline at end of file + 'struct_pcie_static_', 'struct_amdsmi_bdf_t', + 'uint32_t', 'uint64_t', 'uint8_t', + 'union_amdsmi_bdf_t'] diff --git a/projects/amdsmi/tools/generator.py b/projects/amdsmi/tools/generator.py index cc3db2f92b..d9b6459d6e 100644 --- a/projects/amdsmi/tools/generator.py +++ b/projects/amdsmi/tools/generator.py @@ -73,7 +73,6 @@ def parseArgument(): def replace_line(full_path_file_name, string_to_repalce, new_string): fh, abs_path = tempfile.mkstemp() with os.fdopen(fh, 'w') as new_file: - new_file.write(HEADER) with open(full_path_file_name, 'r+', encoding='UTF-8') as old_file: for line in old_file: new_file.write(line.replace(string_to_repalce, new_string)) @@ -83,6 +82,19 @@ def replace_line(full_path_file_name, string_to_repalce, new_string): shutil.move(abs_path, full_path_file_name) +def write_header(full_path_file_name): + fh, abs_path = tempfile.mkstemp() + with os.fdopen(fh, 'w') as new_file: + new_file.write(HEADER) + with open(full_path_file_name, 'r+', encoding='UTF-8') as old_file: + for line in old_file: + new_file.write(line) + + shutil.copymode(full_path_file_name, abs_path) + os.remove(full_path_file_name) + shutil.move(abs_path, full_path_file_name) + + def main(): output_file, input_file, library, clang_extra_args = parseArgument() @@ -134,8 +146,41 @@ except OSError as error: arguments.append("--clang-args=-I" + clang_include_dir + clang_extra_args) clangToPy(arguments) + write_header(output_file) replace_line(output_file, line_to_replace, new_line) + # Custom handling for anonymous struct within amdsmi_bdf_t in Linux + if os_platform == "Linux": + # Get line number for anonymous error in struct_amdsmi_bdf_t + reference_line = "uint64_t function_number :" + line_number = -1 + with open(input_file, 'r') as file: + for input_file_line_number, line in enumerate(file, 1): + if reference_line in line: + line_number = input_file_line_number - 1 # Anonymous line will error on the line before this + break + + if line_number == -1: + print("Could not find reference line in amdsmi.h for amdsmi_bdf_t struct. Skipping anonymous struct replacement.") + else: + print(f"Found reference line in amdsmi.h for amdsmi_bdf_t struct at line {line_number}") + union_anon_line = "union_amdsmi_bdf_t._anonymous_ = ('_0',)" + replace_line(output_file, union_anon_line, "") + + internal_union_anon_line = f"('_0', struct_struct (anonymous at amdsmi.h:{line_number}:3))" + internal_union_struct_line = "('struct_amdsmi_bdf_t', struct_amdsmi_bdf_t)" + replace_line(output_file, internal_union_anon_line, internal_union_struct_line) + + struct_anon_line = f"struct_struct (anonymous at amdsmi.h:{line_number}:3)" + struct_amdsmi_bdf_t_line = "struct_amdsmi_bdf_t" + replace_line(output_file, struct_anon_line, struct_amdsmi_bdf_t_line) + + struct_anon_all_line = "'struct_struct (anonymous at" + struct_amdsmi_bdf_t_line = "'struct_amdsmi_bdf_t'," + replace_line(output_file, struct_anon_all_line, struct_amdsmi_bdf_t_line) + + struct_anon_all_line = f"amdsmi.h:{line_number}:3)', " + replace_line(output_file, struct_anon_all_line, "") if __name__ == "__main__": main()