From 297cd026b9e87ce40ebf781cb86608b43cfbdd55 Mon Sep 17 00:00:00 2001 From: "D. Shah" Date: Thu, 4 Feb 2021 11:57:08 +0000 Subject: [PATCH] Add default implementation of bel bucket functions Signed-off-by: D. Shah --- common/nextpnr.h | 94 +++++++++++++++++++++++++++++++++++++++--- ecp5/arch.cc | 14 +------ ecp5/arch.h | 43 +------------------ ecp5/arch_pybindings.h | 12 ------ ecp5/archdefs.h | 19 +-------- 5 files changed, 94 insertions(+), 88 deletions(-) diff --git a/common/nextpnr.h b/common/nextpnr.h index a97deaa3..e950ad69 100644 --- a/common/nextpnr.h +++ b/common/nextpnr.h @@ -1012,6 +1012,7 @@ struct BaseCtx void attributesToArchInfo(); }; +namespace { // For several functions; such as bel/wire/pip attributes; the trivial implementation is to return an empty vector // But an arch might want to do something fancy with a custom range type that doesn't provide a constructor // So some cursed C++ is needed to return an empty object if possible; or error out if not; is needed @@ -1025,6 +1026,44 @@ template typename std::enable_if::value "default constructor!"); } +// Provide a default implementation of bel bucket name if typedef'd to IdString +template +typename std::enable_if::value, IdString>::type bbid_to_name(Tbbid id) +{ + return id; +} +template +typename std::enable_if::value, IdString>::type bbid_to_name(Tbbid id) +{ + NPNR_ASSERT_FALSE("getBelBucketName must be implemented when BelBucketId is a type other than IdString!"); +} +template +typename std::enable_if::value, BelBucketId>::type bbid_from_name(IdString name) +{ + return name; +} +template +typename std::enable_if::value, BelBucketId>::type bbid_from_name(IdString name) +{ + NPNR_ASSERT_FALSE("getBelBucketByName must be implemented when BelBucketId is a type other than IdString!"); +} + +// For the cell type and bel type ranges; we want to return our stored vectors only if the type matches +template +typename std::enable_if::value, Tret>::type return_if_match(Tret r) +{ + return r; +} + +template +typename std::enable_if::value, Tret>::type return_if_match(Tret r) +{ + NPNR_ASSERT_FALSE("default implementations of cell type and bel bucket range functions only available when the " + "respective range types are 'const std::vector&'"); +} + +} // namespace + template struct ArchBase : BaseCtx { // -------------------------------------------------------------- @@ -1235,15 +1274,27 @@ template struct ArchBase : BaseCtx // Placement validity checks virtual bool isValidBelForCellType(IdString cell_type, BelId bel) const { return cell_type == getBelType(bel); } - virtual IdString getBelBucketName(BelBucketId bucket) const = 0; - virtual BelBucketId getBelBucketByName(IdString name) const = 0; + virtual IdString getBelBucketName(BelBucketId bucket) const { return bbid_to_name(bucket); } + virtual BelBucketId getBelBucketByName(IdString name) const { return bbid_from_name(name); } virtual BelBucketId getBelBucketForBel(BelId bel) const { return getBelBucketForCellType(getBelType(bel)); }; virtual BelBucketId getBelBucketForCellType(IdString cell_type) const { return getBelBucketByName(cell_type); }; virtual bool isValidBelForCell(CellInfo *cell, BelId bel) const { return true; } virtual bool isBelLocationValid(BelId bel) const { return true; } - virtual typename R::CellTypeRange getCellTypes() const = 0; - virtual typename R::BelBucketRange getBelBuckets() const = 0; - virtual typename R::BucketBelRange getBelsInBucket(BelBucketId bucket) const = 0; + virtual typename R::CellTypeRange getCellTypes() const + { + NPNR_ASSERT(cell_types_initialised); + return return_if_match &, typename R::CellTypeRange>(cell_types); + } + virtual typename R::BelBucketRange getBelBuckets() const + { + NPNR_ASSERT(bel_buckets_initialised); + return return_if_match &, typename R::BelBucketRange>(bel_buckets); + } + virtual typename R::BucketBelRange getBelsInBucket(BelBucketId bucket) const + { + NPNR_ASSERT(bel_buckets_initialised); + return return_if_match &, typename R::BucketBelRange>(bucket_bels.at(bucket)); + } // Flow methods virtual bool pack() = 0; @@ -1258,6 +1309,39 @@ template struct ArchBase : BaseCtx std::unordered_map base_bel2cell; std::unordered_map base_wire2net; std::unordered_map base_pip2net; + + // For the default cell/bel bucket implementations + std::vector cell_types; + std::vector bel_buckets; + std::unordered_map> bucket_bels; + + // Arches that want to use the default cell types and bel buckets *must* call these functions in their constructor + bool cell_types_initialised = false; + bool bel_buckets_initialised = false; + void init_cell_types() + { + std::unordered_set bel_types; + for (auto bel : getBels()) + bel_types.insert(getBelType(bel)); + std::copy(bel_types.begin(), bel_types.end(), std::back_inserter(cell_types)); + std::sort(cell_types.begin(), cell_types.end()); + cell_types_initialised = true; + } + void init_bel_buckets() + { + for (auto cell_type : getCellTypes()) { + auto bucket = getBelBucketForCellType(cell_type); + bucket_bels[bucket]; // create empty bucket + } + for (auto bel : getBels()) { + auto bucket = getBelBucketForBel(bel); + bucket_bels[bucket].push_back(bel); + } + for (auto &b : bucket_bels) + bel_buckets.push_back(b.first); + std::sort(bel_buckets.begin(), bel_buckets.end()); + bel_buckets_initialised = true; + } }; NEXTPNR_NAMESPACE_END diff --git a/ecp5/arch.cc b/ecp5/arch.cc index dfe47032..18fae1e2 100644 --- a/ecp5/arch.cc +++ b/ecp5/arch.cc @@ -105,18 +105,8 @@ Arch::Arch(ArchArgs args) : args(args) bel_to_cell.resize(chip_info->height * chip_info->width * max_loc_bels, nullptr); - std::unordered_set bel_types; - for (BelId bel : getBels()) { - bel_types.insert(getBelType(bel)); - } - - for (IdString bel_type : bel_types) { - cell_types.push_back(bel_type); - - BelBucketId bucket; - bucket.name = bel_type; - buckets.push_back(bucket); - } + ArchBase::init_cell_types(); + ArchBase::init_bel_buckets(); for (int i = 0; i < chip_info->width; i++) x_ids.push_back(id(stringf("X%d", i))); diff --git a/ecp5/arch.h b/ecp5/arch.h index 5ea5206b..0d93a7a0 100644 --- a/ecp5/arch.h +++ b/ecp5/arch.h @@ -461,8 +461,8 @@ struct ArchRanges using DecalGfxRange = std::vector; // Placement validity using CellTypeRange = const std::vector &; - using BelBucketRange = std::vector; - using BucketBelRange = std::vector; + using BelBucketRange = const std::vector &; + using BucketBelRange = const std::vector &; }; struct Arch : ArchBase @@ -878,45 +878,6 @@ struct Arch : ArchBase // ------------------------------------------------- // Placement validity checks - - const std::vector &getCellTypes() const override { return cell_types; } - - std::vector getBelBuckets() const override { return buckets; } - - IdString getBelBucketName(BelBucketId bucket) const override { return bucket.name; } - - BelBucketId getBelBucketByName(IdString name) const override - { - BelBucketId bucket; - bucket.name = name; - return bucket; - } - - BelBucketId getBelBucketForBel(BelId bel) const override - { - BelBucketId bucket; - bucket.name = getBelType(bel); - return bucket; - } - - BelBucketId getBelBucketForCellType(IdString cell_type) const override - { - BelBucketId bucket; - bucket.name = cell_type; - return bucket; - } - - std::vector getBelsInBucket(BelBucketId bucket) const override - { - std::vector bels; - for (BelId bel : getBels()) { - if (getBelType(bel) == bucket.name) { - bels.push_back(bel); - } - } - return bels; - } - bool isValidBelForCell(CellInfo *cell, BelId bel) const override; bool isBelLocationValid(BelId bel) const override; diff --git a/ecp5/arch_pybindings.h b/ecp5/arch_pybindings.h index 4228f12b..f2a7dabe 100644 --- a/ecp5/arch_pybindings.h +++ b/ecp5/arch_pybindings.h @@ -76,18 +76,6 @@ template <> struct string_converter } }; -template <> struct string_converter -{ - BelBucketId from_str(Context *ctx, std::string name) { return ctx->getBelBucketByName(ctx->id(name)); } - - std::string to_str(Context *ctx, BelBucketId id) - { - if (id == BelBucketId()) - throw bad_wrap(); - return ctx->getBelBucketName(id).str(ctx); - } -}; - template <> struct string_converter { BelPin from_str(Context *ctx, std::string name) diff --git a/ecp5/archdefs.h b/ecp5/archdefs.h index 3bc75ab4..c8fdf892 100644 --- a/ecp5/archdefs.h +++ b/ecp5/archdefs.h @@ -126,14 +126,7 @@ struct PipId } }; -struct BelBucketId -{ - IdString name; - - bool operator==(const BelBucketId &other) const { return (name == other.name); } - bool operator!=(const BelBucketId &other) const { return (name != other.name); } - bool operator<(const BelBucketId &other) const { return name < other.name; } -}; +typedef IdString BelBucketId; struct GroupId { @@ -271,14 +264,4 @@ template <> struct hash } }; -template <> struct hash -{ - std::size_t operator()(const NEXTPNR_NAMESPACE_PREFIX BelBucketId &partition) const noexcept - { - std::size_t seed = 0; - boost::hash_combine(seed, hash()(partition.name)); - return seed; - } -}; - } // namespace std