[OpenFPGA Tool] Update fabric key data structure to support regions

This commit is contained in:
tangxifan 2020-09-27 20:08:11 -06:00
parent bbdea4a46b
commit 48b2bff0d9
4 changed files with 142 additions and 10 deletions

View File

@ -1,7 +1,9 @@
<fabric_key> <fabric_key>
<region id="0">
<key id="0" name="sb" value="0"/> <key id="0" name="sb" value="0"/>
<key id="2" name="cb" value="10"/> <key id="2" name="cb" value="10"/>
<key id="1" name="sb" value="7"/> <key id="1" name="sb" value="7"/>
<key id="3" name="clb" value="53"/> <key id="3" name="clb" value="53"/>
<key id="4" name="bram" value="32"/> <key id="4" name="bram" value="32"/>
</region>
</fabric_key> </fabric_key>

View File

@ -1,4 +1,7 @@
#include <algorithm>
#include "vtr_assert.h" #include "vtr_assert.h"
#include "vtr_log.h"
#include "fabric_key.h" #include "fabric_key.h"
@ -20,24 +23,31 @@ FabricKey::fabric_key_range FabricKey::keys() const {
return vtr::make_range(key_ids_.begin(), key_ids_.end()); return vtr::make_range(key_ids_.begin(), key_ids_.end());
} }
FabricKey::fabric_region_range FabricKey::regions() const {
return vtr::make_range(region_ids_.begin(), region_ids_.end());
}
/************************************************************************ /************************************************************************
* Public Accessors : Basic data query * Public Accessors : Basic data query
***********************************************************************/ ***********************************************************************/
/* Access the name of a key */ std::vector<FabricKeyId> FabricKey::region_keys(const FabricRegionId& region_id) const {
/* validate the region_id */
VTR_ASSERT(valid_region_id(region_id));
return region_key_ids_[region_id];
}
std::string FabricKey::key_name(const FabricKeyId& key_id) const { std::string FabricKey::key_name(const FabricKeyId& key_id) const {
/* validate the key_id */ /* validate the key_id */
VTR_ASSERT(valid_key_id(key_id)); VTR_ASSERT(valid_key_id(key_id));
return key_names_[key_id]; return key_names_[key_id];
} }
/* Access the value of a key */
size_t FabricKey::key_value(const FabricKeyId& key_id) const { size_t FabricKey::key_value(const FabricKeyId& key_id) const {
/* validate the key_id */ /* validate the key_id */
VTR_ASSERT(valid_key_id(key_id)); VTR_ASSERT(valid_key_id(key_id));
return key_values_[key_id]; return key_values_[key_id];
} }
/* Access the alias of a key */
std::string FabricKey::key_alias(const FabricKeyId& key_id) const { std::string FabricKey::key_alias(const FabricKeyId& key_id) const {
/* validate the key_id */ /* validate the key_id */
VTR_ASSERT(valid_key_id(key_id)); VTR_ASSERT(valid_key_id(key_id));
@ -51,20 +61,78 @@ bool FabricKey::empty() const {
/************************************************************************ /************************************************************************
* Public Mutators * Public Mutators
***********************************************************************/ ***********************************************************************/
void FabricKey::reserve_regions(const size_t& num_regions) {
region_ids_.reserve(num_regions);
region_key_ids_.reserve(num_regions);
}
FabricRegionId FabricKey::create_region() {
/* Create a new id */
FabricRegionId region = FabricRegionId(region_ids_.size());
region_ids_.push_back(region);
region_key_ids_.emplace_back();
return region;
}
void FabricKey::reserve_region_keys(const FabricRegionId& region_id,
const size_t& num_keys) {
/* validate the region_id */
VTR_ASSERT(valid_region_id(region_id));
region_key_ids_[region_id].reserve(num_keys);
}
void FabricKey::add_key_to_region(const FabricRegionId& region_id,
const FabricKeyId& key_id) {
/* validate the key_id */
VTR_ASSERT(valid_key_id(key_id));
/* validate the region_id */
VTR_ASSERT(valid_region_id(region_id));
/* Check if the key is already in the region */
if (region_key_ids_[region_id].end() != std::find(region_key_ids_[region_id].begin(),
region_key_ids_[region_id].end(),
key_id)) {
VTR_LOG_WARN("Try to add a key '%s' which is already in the region '%lu'!\n",
key_name(key_id).c_str(),
size_t(region_id));
VTR_ASSERT(region_id == key_regions_[key_id]);
return; /* Nothing to do but leave a warning! */
}
/* Register the key in the region */
region_key_ids_[region_id].push_back(key_id);
/* If the key is already in another region, we will error out */
if ( (true == valid_region_id(key_regions_[key_id]))
&& (region_id != key_regions_[key_id])) {
VTR_LOG_ERROR("Try to add a key '%s' to region '%lu' but it is already in another region '%lu'!\n",
key_name(key_id).c_str(),
size_t(key_regions_[key_id]),
size_t(region_id));
exit(1);
}
key_regions_[key_id] = region_id;
}
void FabricKey::reserve_keys(const size_t& num_keys) { void FabricKey::reserve_keys(const size_t& num_keys) {
key_ids_.reserve(num_keys); key_ids_.reserve(num_keys);
key_names_.reserve(num_keys); key_names_.reserve(num_keys);
key_values_.reserve(num_keys); key_values_.reserve(num_keys);
key_regions_.reserve(num_keys);
key_alias_.reserve(num_keys); key_alias_.reserve(num_keys);
} }
/* Create a new key and add it to the library, return an id */
FabricKeyId FabricKey::create_key() { FabricKeyId FabricKey::create_key() {
/* Create a new id */ /* Create a new id */
FabricKeyId key = FabricKeyId(key_ids_.size()); FabricKeyId key = FabricKeyId(key_ids_.size());
key_ids_.push_back(key); key_ids_.push_back(key);
key_names_.emplace_back(); key_names_.emplace_back();
key_values_.emplace_back(); key_values_.emplace_back();
key_regions_.emplace_back(FabricRegionId::INVALID());
key_alias_.emplace_back(); key_alias_.emplace_back();
return key; return key;
@ -98,6 +166,10 @@ void FabricKey::set_key_alias(const FabricKeyId& key_id,
* Internal invalidators/validators * Internal invalidators/validators
***********************************************************************/ ***********************************************************************/
/* Validators */ /* Validators */
bool FabricKey::valid_region_id(const FabricRegionId& region_id) const {
return ( size_t(region_id) < region_ids_.size() ) && ( region_id == region_ids_[region_id] );
}
bool FabricKey::valid_key_id(const FabricKeyId& key_id) const { bool FabricKey::valid_key_id(const FabricKeyId& key_id) const {
return ( size_t(key_id) < key_ids_.size() ) && ( key_id == key_ids_[key_id] ); return ( size_t(key_id) < key_ids_.size() ) && ( key_id == key_ids_[key_id] );
} }

View File

@ -15,41 +15,94 @@
/******************************************************************** /********************************************************************
* A data structure to describe a secure key for fabric organization * A data structure to describe a secure key for fabric organization
* A fabric may consist of multiple regions
* Each region contains a number of keys
*
* Note that:
* - each key can only be defined in one unique region
* *
* Typical usage: * Typical usage:
* -------------- * --------------
* // Create an empty fabric key * // Create an empty fabric key
* FabricKey fabric_key; * FabricKey fabric_key;
* // Create a region
* FabricRegionId region = fabric_key.create_region();
* // Add a key with name and value * // Add a key with name and value
* FabricKeyId key = fabic_key.create_key(key_name, key_value); * FabricKeyId key = fabric_key.create_key(key_name, key_value);
* // Affilate a key to a region
* fabric_key.add_key_to_region(region, key);
* *
*******************************************************************/ *******************************************************************/
class FabricKey { class FabricKey {
public: /* Types */ public: /* Types */
typedef vtr::vector<FabricKeyId, FabricKeyId>::const_iterator fabric_key_iterator; typedef vtr::vector<FabricKeyId, FabricKeyId>::const_iterator fabric_key_iterator;
typedef vtr::vector<FabricRegionId, FabricRegionId>::const_iterator fabric_region_iterator;
/* Create range */ /* Create range */
typedef vtr::Range<fabric_region_iterator> fabric_region_range;
typedef vtr::Range<fabric_key_iterator> fabric_key_range; typedef vtr::Range<fabric_key_iterator> fabric_key_range;
public: /* Constructors */ public: /* Constructors */
FabricKey(); FabricKey();
public: /* Accessors: aggregates */ public: /* Accessors: aggregates */
fabric_key_range keys() const; fabric_key_range keys() const;
fabric_region_range regions() const;
public: /* Public Accessors: Basic data query */ public: /* Public Accessors: Basic data query */
/* Access all the keys of a region */
std::vector<FabricKeyId> region_keys(const FabricRegionId& region_id) const;
/* Access the name of a key */
std::string key_name(const FabricKeyId& key_id) const; std::string key_name(const FabricKeyId& key_id) const;
/* Access the value of a key */
size_t key_value(const FabricKeyId& key_id) const; size_t key_value(const FabricKeyId& key_id) const;
/* Access the alias of a key */
std::string key_alias(const FabricKeyId& key_id) const; std::string key_alias(const FabricKeyId& key_id) const;
/* Check if there are any keys */
bool empty() const; bool empty() const;
public: /* Public Mutators: model-related */ public: /* Public Mutators: model-related */
/* Reserve a number of regions to be memory efficent */
void reserve_regions(const size_t& num_regions);
/* Create a new region and add it to the library, return an id */
FabricRegionId create_region();
/* Reserve the memory space for keys under a region, to be memory efficient */
void reserve_region_keys(const FabricRegionId& region_id,
const size_t& num_keys);
/* Add a key to a region */
void add_key_to_region(const FabricRegionId& region_id,
const FabricKeyId& key_id);
/* Reserve a number of keys to be memory efficent */
void reserve_keys(const size_t& num_keys); void reserve_keys(const size_t& num_keys);
/* Create a new key and add it to the library, return an id */
FabricKeyId create_key(); FabricKeyId create_key();
/* Configure attributes of a key */
void set_key_name(const FabricKeyId& key_id, void set_key_name(const FabricKeyId& key_id,
const std::string& name); const std::string& name);
void set_key_value(const FabricKeyId& key_id, void set_key_value(const FabricKeyId& key_id,
const size_t& value); const size_t& value);
void set_key_alias(const FabricKeyId& key_id, void set_key_alias(const FabricKeyId& key_id,
const std::string& alias); const std::string& alias);
public: /* Public invalidators/validators */ public: /* Public invalidators/validators */
bool valid_region_id(const FabricRegionId& region_id) const;
bool valid_key_id(const FabricKeyId& key_id) const; bool valid_key_id(const FabricKeyId& key_id) const;
private: /* Internal data */ private: /* Internal data */
/* Unique ids for each region */
vtr::vector<FabricRegionId, FabricRegionId> region_ids_;
/* Key ids for each region */
vtr::vector<FabricRegionId, std::vector<FabricKeyId>> region_key_ids_;
/* Unique ids for each key */ /* Unique ids for each key */
vtr::vector<FabricKeyId, FabricKeyId> key_ids_; vtr::vector<FabricKeyId, FabricKeyId> key_ids_;
@ -59,6 +112,9 @@ class FabricKey {
/* Values for each key */ /* Values for each key */
vtr::vector<FabricKeyId, size_t> key_values_; vtr::vector<FabricKeyId, size_t> key_values_;
/* Region for each key */
vtr::vector<FabricKeyId, FabricRegionId> key_regions_;
/* Optional alias for each key, with which a key can also be represented */ /* Optional alias for each key, with which a key can also be represented */
vtr::vector<FabricKeyId, std::string> key_alias_; vtr::vector<FabricKeyId, std::string> key_alias_;
}; };

View File

@ -12,8 +12,10 @@
#include "vtr_strong_id.h" #include "vtr_strong_id.h"
struct fabric_region_id_tag;
struct fabric_key_id_tag; struct fabric_key_id_tag;
typedef vtr::StrongId<fabric_region_id_tag> FabricRegionId;
typedef vtr::StrongId<fabric_key_id_tag> FabricKeyId; typedef vtr::StrongId<fabric_key_id_tag> FabricKeyId;
/* Short declaration of class */ /* Short declaration of class */