From a5055e9d26e9b0a2f6da1e7c70260d7b11a08d76 Mon Sep 17 00:00:00 2001 From: tangxifan Date: Fri, 12 Jun 2020 13:03:11 -0600 Subject: [PATCH] add support about loading external fabric key --- libopenfpga/libfabrickey/src/fabric_key.cpp | 4 ++ libopenfpga/libfabrickey/src/fabric_key.h | 1 + openfpga/src/base/openfpga_build_fabric.cpp | 15 +++++++ openfpga/src/fabric/build_device_module.cpp | 4 +- openfpga/src/fabric/build_device_module.h | 2 + openfpga/src/fabric/build_top_module.cpp | 22 +++++++--- openfpga/src/fabric/build_top_module.h | 2 + .../src/fabric/build_top_module_memory.cpp | 44 +++++++++++++++++++ openfpga/src/fabric/build_top_module_memory.h | 5 +++ 9 files changed, 92 insertions(+), 7 deletions(-) diff --git a/libopenfpga/libfabrickey/src/fabric_key.cpp b/libopenfpga/libfabrickey/src/fabric_key.cpp index 187150800..0c2c508bd 100644 --- a/libopenfpga/libfabrickey/src/fabric_key.cpp +++ b/libopenfpga/libfabrickey/src/fabric_key.cpp @@ -37,6 +37,10 @@ size_t FabricKey::key_value(const FabricKeyId& key_id) const { return key_values_[key_id]; } +bool FabricKey::empty() const { + return 0 == key_ids_.size(); +} + /************************************************************************ * Public Mutators ***********************************************************************/ diff --git a/libopenfpga/libfabrickey/src/fabric_key.h b/libopenfpga/libfabrickey/src/fabric_key.h index 7488923df..053e32e56 100644 --- a/libopenfpga/libfabrickey/src/fabric_key.h +++ b/libopenfpga/libfabrickey/src/fabric_key.h @@ -36,6 +36,7 @@ class FabricKey { public: /* Public Accessors: Basic data query */ std::string key_name(const FabricKeyId& key_id) const; size_t key_value(const FabricKeyId& key_id) const; + bool empty() const; public: /* Public Mutators: model-related */ void reserve_keys(const size_t& num_keys); FabricKeyId create_key(); diff --git a/openfpga/src/base/openfpga_build_fabric.cpp b/openfpga/src/base/openfpga_build_fabric.cpp index f5c007a09..adbe8fce6 100644 --- a/openfpga/src/base/openfpga_build_fabric.cpp +++ b/openfpga/src/base/openfpga_build_fabric.cpp @@ -8,6 +8,9 @@ /* Headers from openfpgashell library */ #include "command_exit_codes.h" +/* Headers from fabrickey library */ +#include "read_xml_fabric_key.h" + #include "device_rr_gsb.h" #include "device_rr_gsb_utils.h" #include "build_device_module.h" @@ -68,6 +71,7 @@ int build_fabric(OpenfpgaContext& openfpga_ctx, CommandOptionId opt_duplicate_grid_pin = cmd.option("duplicate_grid_pin"); CommandOptionId opt_gen_random_fabric_key = cmd.option("generate_random_fabric_key"); CommandOptionId opt_write_fabric_key = cmd.option("write_fabric_key"); + CommandOptionId opt_load_fabric_key = cmd.option("load_fabric_key"); CommandOptionId opt_verbose = cmd.option("verbose"); if (true == cmd_context.option_enable(cmd, opt_compress_routing)) { @@ -78,12 +82,23 @@ int build_fabric(OpenfpgaContext& openfpga_ctx, VTR_LOG("\n"); + /* Load fabric key from file */ + FabricKey predefined_fabric_key; + if (true == cmd_context.option_enable(cmd, opt_load_fabric_key)) { + std::string fkey_fname = cmd_context.option_value(cmd, opt_load_fabric_key); + VTR_ASSERT(false == fkey_fname.empty()); + predefined_fabric_key = read_xml_fabric_key(fkey_fname.c_str()); + } + + VTR_LOG("\n"); + openfpga_ctx.mutable_module_graph() = build_device_module_graph(openfpga_ctx.mutable_io_location_map(), openfpga_ctx.mutable_decoder_lib(), const_cast(openfpga_ctx), g_vpr_ctx.device(), cmd_context.option_enable(cmd, opt_compress_routing), cmd_context.option_enable(cmd, opt_duplicate_grid_pin), + predefined_fabric_key, cmd_context.option_enable(cmd, opt_gen_random_fabric_key), cmd_context.option_enable(cmd, opt_verbose)); diff --git a/openfpga/src/fabric/build_device_module.cpp b/openfpga/src/fabric/build_device_module.cpp index 36769636a..b988e1e29 100644 --- a/openfpga/src/fabric/build_device_module.cpp +++ b/openfpga/src/fabric/build_device_module.cpp @@ -32,6 +32,7 @@ ModuleManager build_device_module_graph(IoLocationMap& io_location_map, const DeviceContext& vpr_device_ctx, const bool& compress_routing, const bool& duplicate_grid_pin, + const FabricKey& fabric_key, const bool& generate_random_fabric_key, const bool& verbose) { vtr::ScopedStartFinishTimer timer("Build fabric module graph"); @@ -117,7 +118,8 @@ ModuleManager build_device_module_graph(IoLocationMap& io_location_map, openfpga_ctx.arch().arch_direct, openfpga_ctx.arch().config_protocol.type(), sram_model, - compress_routing, duplicate_grid_pin, generate_random_fabric_key); + compress_routing, duplicate_grid_pin, + fabric_key, generate_random_fabric_key); /* Now a critical correction has to be done! * In the module construction, we always use prefix of ports because they are binded diff --git a/openfpga/src/fabric/build_device_module.h b/openfpga/src/fabric/build_device_module.h index 7b8a6bd2b..34d30b997 100644 --- a/openfpga/src/fabric/build_device_module.h +++ b/openfpga/src/fabric/build_device_module.h @@ -6,6 +6,7 @@ *******************************************************************/ #include "vpr_context.h" #include "openfpga_context.h" +#include "fabric_key.h" /******************************************************************** * Function declaration @@ -20,6 +21,7 @@ ModuleManager build_device_module_graph(IoLocationMap& io_location_map, const DeviceContext& vpr_device_ctx, const bool& compress_routing, const bool& duplicate_grid_pin, + const FabricKey& fabric_key, const bool& generate_random_fabric_key, const bool& verbose); diff --git a/openfpga/src/fabric/build_top_module.cpp b/openfpga/src/fabric/build_top_module.cpp index a751834ef..6c2d94a00 100644 --- a/openfpga/src/fabric/build_top_module.cpp +++ b/openfpga/src/fabric/build_top_module.cpp @@ -322,6 +322,7 @@ void build_top_module(ModuleManager& module_manager, const CircuitModelId& sram_model, const bool& compact_routing_hierarchy, const bool& duplicate_grid_pin, + const FabricKey& fabric_key, const bool& generate_random_fabric_key) { vtr::ScopedStartFinishTimer timer("Build FPGA fabric module"); @@ -363,12 +364,21 @@ void build_top_module(ModuleManager& module_manager, */ add_module_gpio_ports_from_child_modules(module_manager, top_module); - /* Organize the list of memory modules and instances */ - organize_top_module_memory_modules(module_manager, top_module, - circuit_lib, sram_orgz_type, sram_model, - grids, grid_instance_ids, - device_rr_gsb, sb_instance_ids, cb_instance_ids, - compact_routing_hierarchy); + /* Organize the list of memory modules and instances + * If we have an empty fabric key, we organize the memory modules as routine + * Otherwise, we will load the fabric key directly + */ + if (true == fabric_key.empty()) { + organize_top_module_memory_modules(module_manager, top_module, + circuit_lib, sram_orgz_type, sram_model, + grids, grid_instance_ids, + device_rr_gsb, sb_instance_ids, cb_instance_ids, + compact_routing_hierarchy); + } else { + VTR_ASSERT_SAFE(false == fabric_key.empty()); + load_top_module_memory_modules_from_fabric_key(module_manager, top_module, + fabric_key); + } /* Shuffle the configurable children in a random sequence */ if (true == generate_random_fabric_key) { diff --git a/openfpga/src/fabric/build_top_module.h b/openfpga/src/fabric/build_top_module.h index fca3ea841..50bf6e611 100644 --- a/openfpga/src/fabric/build_top_module.h +++ b/openfpga/src/fabric/build_top_module.h @@ -16,6 +16,7 @@ #include "arch_direct.h" #include "module_manager.h" #include "io_location_map.h" +#include "fabric_key.h" /******************************************************************** * Function declaration @@ -37,6 +38,7 @@ void build_top_module(ModuleManager& module_manager, const CircuitModelId& sram_model, const bool& compact_routing_hierarchy, const bool& duplicate_grid_pin, + const FabricKey& fabric_key, const bool& generate_random_fabric_key); } /* end namespace openfpga */ diff --git a/openfpga/src/fabric/build_top_module_memory.cpp b/openfpga/src/fabric/build_top_module_memory.cpp index 2ac546218..158a0f44a 100644 --- a/openfpga/src/fabric/build_top_module_memory.cpp +++ b/openfpga/src/fabric/build_top_module_memory.cpp @@ -401,6 +401,50 @@ void shuffle_top_module_configurable_children(ModuleManager& module_manager, } } +/******************************************************************** + * Load configurable children from a fabric key to top-level module + * + * Note: + * - This function will overwrite any exisiting configurable children + * under the top module + * + * Return 0 - Success + * Return 1 - Fatal errors + ********************************************************************/ +int load_top_module_memory_modules_from_fabric_key(ModuleManager& module_manager, + const ModuleId& top_module, + const FabricKey& fabric_key) { + /* Ensure a clean start */ + module_manager.clear_configurable_children(top_module); + + for (const FabricKeyId& key : fabric_key.keys()) { + /* Find if the module name exist */ + ModuleId child_module = module_manager.find_module(fabric_key.key_name(key)); + if (false == module_manager.valid_module_id(child_module)) { + VTR_LOGF_ERROR(__FILE__, __LINE__, + "Invalid key name '%s'!\n", + fabric_key.key_name(key).c_str()); + return 1; + } + + /* Find if instance id is valid */ + size_t child_instance = fabric_key.key_value(key); + if (child_instance >= module_manager.num_instance(top_module, child_module)) { + VTR_LOGF_ERROR(__FILE__, __LINE__, + "Invalid key value '%ld'!\n", + child_instance); + return 1; + } + + /* Now we can add the child to configurable children of the top module */ + module_manager.add_configurable_child(top_module, + child_module, + child_instance); + } + + return 0; +} + /******************************************************************** * Add a list of ports that are used for SRAM configuration to the FPGA * top-level module diff --git a/openfpga/src/fabric/build_top_module_memory.h b/openfpga/src/fabric/build_top_module_memory.h index 1c6dd56e5..c4a1ff7f6 100644 --- a/openfpga/src/fabric/build_top_module_memory.h +++ b/openfpga/src/fabric/build_top_module_memory.h @@ -14,6 +14,7 @@ #include "decoder_library.h" #include "device_grid.h" #include "device_rr_gsb.h" +#include "fabric_key.h" /******************************************************************** * Function declaration @@ -37,6 +38,10 @@ void organize_top_module_memory_modules(ModuleManager& module_manager, void shuffle_top_module_configurable_children(ModuleManager& module_manager, const ModuleId& top_module); +int load_top_module_memory_modules_from_fabric_key(ModuleManager& module_manager, + const ModuleId& top_module, + const FabricKey& fabric_key); + void add_top_module_sram_ports(ModuleManager& module_manager, const ModuleId& module_id, const CircuitLibrary& circuit_lib,