@@ -823,60 +823,30 @@ LogicalResult ModuleType::verify(function_ref<InFlightDiagnostic()> emitError,
823
823
}
824
824
825
825
size_t ModuleType::getPortIdForInputId (size_t idx) {
826
- for (auto [i, p] : llvm::enumerate (getPorts ())) {
827
- if (p.dir != ModulePort::Direction::Output) {
828
- if (!idx)
829
- return i;
830
- --idx;
831
- }
832
- }
833
- assert (0 && " Out of bounds input port id" );
834
- return ~0UL ;
826
+ assert (idx < getImpl ()->inputToAbs .size () && " input port out of range" );
827
+ return getImpl ()->inputToAbs [idx];
835
828
}
836
829
837
830
size_t ModuleType::getPortIdForOutputId (size_t idx) {
838
- for (auto [i, p] : llvm::enumerate (getPorts ())) {
839
- if (p.dir == ModulePort::Direction::Output) {
840
- if (!idx)
841
- return i;
842
- --idx;
843
- }
844
- }
845
- assert (0 && " Out of bounds output port id" );
846
- return ~0UL ;
831
+ assert (idx < getImpl ()->outputToAbs .size () && " output port out of range" );
832
+ return getImpl ()->outputToAbs [idx];
847
833
}
848
834
849
835
size_t ModuleType::getInputIdForPortId (size_t idx) {
850
- auto ports = getPorts ();
851
- assert (ports[idx].dir != ModulePort::Direction::Output);
852
- size_t retval = 0 ;
853
- for (size_t i = 0 ; i < idx; ++i)
854
- if (ports[i].dir != ModulePort::Direction::Output)
855
- ++retval;
856
- return retval;
836
+ auto nIdx = getImpl ()->absToInput [idx];
837
+ assert (nIdx != ~0ULL );
838
+ return nIdx;
857
839
}
858
840
859
841
size_t ModuleType::getOutputIdForPortId (size_t idx) {
860
- auto ports = getPorts ();
861
- assert (ports[idx].dir == ModulePort::Direction::Output);
862
- size_t retval = 0 ;
863
- for (size_t i = 0 ; i < idx; ++i)
864
- if (ports[i].dir == ModulePort::Direction::Output)
865
- ++retval;
866
- return retval;
842
+ auto nIdx = getImpl ()->absToOutput [idx];
843
+ assert (nIdx != ~0ULL );
844
+ return nIdx;
867
845
}
868
846
869
- size_t ModuleType::getNumInputs () {
870
- return std::count_if (getPorts ().begin (), getPorts ().end (), [](auto &p) {
871
- return p.dir != ModulePort::Direction::Output;
872
- });
873
- }
847
+ size_t ModuleType::getNumInputs () { return getImpl ()->inputToAbs .size (); }
874
848
875
- size_t ModuleType::getNumOutputs () {
876
- return std::count_if (getPorts ().begin (), getPorts ().end (), [](auto &p) {
877
- return p.dir == ModulePort::Direction::Output;
878
- });
879
- }
849
+ size_t ModuleType::getNumOutputs () { return getImpl ()->outputToAbs .size (); }
880
850
881
851
size_t ModuleType::getNumPorts () { return getPorts ().size (); }
882
852
@@ -984,6 +954,10 @@ FunctionType ModuleType::getFuncType() {
984
954
return FunctionType::get (getContext (), inputs, outputs);
985
955
}
986
956
957
+ ArrayRef<ModulePort> ModuleType::getPorts () const {
958
+ return getImpl ()->getPorts ();
959
+ }
960
+
987
961
FailureOr<ModuleType> ModuleType::resolveParametricTypes (ArrayAttr parameters,
988
962
LocationAttr loc,
989
963
bool emitErrors) {
@@ -1021,7 +995,7 @@ static ModulePort::Direction strToDir(StringRef str) {
1021
995
}
1022
996
1023
997
// / Parse a list of field names and types within <>. E.g.:
1024
- // / <foo: i7, bar: i8>
998
+ // / <input foo: i7, output bar: i8>
1025
999
static ParseResult parsePorts (AsmParser &p,
1026
1000
SmallVectorImpl<ModulePort> &ports) {
1027
1001
return p.parseCommaSeparatedList (
@@ -1060,18 +1034,6 @@ void ModuleType::print(AsmPrinter &odsPrinter) const {
1060
1034
printPorts (odsPrinter, getPorts ());
1061
1035
}
1062
1036
1063
- namespace circt {
1064
- namespace hw {
1065
-
1066
- static bool operator ==(const ModulePort &a, const ModulePort &b) {
1067
- return a.dir == b.dir && a.name == b.name && a.type == b.type ;
1068
- }
1069
- static llvm::hash_code hash_value (const ModulePort &port) {
1070
- return llvm::hash_combine (port.dir , port.name , port.type );
1071
- }
1072
- } // namespace hw
1073
- } // namespace circt
1074
-
1075
1037
ModuleType circt::hw::detail::fnToMod (Operation *op,
1076
1038
ArrayRef<Attribute> inputNames,
1077
1039
ArrayRef<Attribute> outputNames) {
@@ -1109,6 +1071,25 @@ ModuleType circt::hw::detail::fnToMod(FunctionType fnty,
1109
1071
return ModuleType::get (fnty.getContext (), ports);
1110
1072
}
1111
1073
1074
+ detail::ModuleTypeStorage::ModuleTypeStorage (ArrayRef<ModulePort> inPorts)
1075
+ : ports(inPorts) {
1076
+ size_t nextInput = 0 ;
1077
+ size_t nextOutput = 0 ;
1078
+ for (auto [idx, p] : llvm::enumerate (ports)) {
1079
+ if (p.dir == ModulePort::Direction::Output) {
1080
+ outputToAbs.push_back (idx);
1081
+ absToOutput.push_back (nextOutput);
1082
+ absToInput.push_back (~0ULL );
1083
+ ++nextOutput;
1084
+ } else {
1085
+ inputToAbs.push_back (idx);
1086
+ absToInput.push_back (nextInput);
1087
+ absToOutput.push_back (~0ULL );
1088
+ ++nextInput;
1089
+ }
1090
+ }
1091
+ }
1092
+
1112
1093
// //////////////////////////////////////////////////////////////////////////////
1113
1094
// BoilerPlate
1114
1095
// //////////////////////////////////////////////////////////////////////////////
0 commit comments