Skip to content

Commit 8f41873

Browse files
committed
Simplify v1model model include.
Signed-off-by: fruffy <fruffy@nyu.edu>
1 parent 79654b5 commit 8f41873

File tree

22 files changed

+97
-101
lines changed

22 files changed

+97
-101
lines changed

backends/bmv2/common/extern.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -221,14 +221,16 @@ cstring ExternConverter::createCalculation(ConversionContext *ctxt, cstring algo
221221
}
222222

223223
cstring ExternConverter::convertHashAlgorithm(cstring algorithm) {
224-
if (algorithm == P4V1::V1Model::instance.algorithm.crc32.name) return "crc32"_cs;
225-
if (algorithm == P4V1::V1Model::instance.algorithm.crc32_custom.name) return "crc32_custom"_cs;
226-
if (algorithm == P4V1::V1Model::instance.algorithm.crc16.name) return "crc16"_cs;
227-
if (algorithm == P4V1::V1Model::instance.algorithm.crc16_custom.name) return "crc16_custom"_cs;
228-
if (algorithm == P4V1::V1Model::instance.algorithm.random.name) return "random"_cs;
229-
if (algorithm == P4V1::V1Model::instance.algorithm.identity.name) return "identity"_cs;
230-
if (algorithm == P4V1::V1Model::instance.algorithm.csum16.name) return "csum16"_cs;
231-
if (algorithm == P4V1::V1Model::instance.algorithm.xor16.name) return "xor16"_cs;
224+
if (algorithm == P4V1::V1Model::instance().algorithm.crc32.name) return "crc32"_cs;
225+
if (algorithm == P4V1::V1Model::instance().algorithm.crc32_custom.name)
226+
return "crc32_custom"_cs;
227+
if (algorithm == P4V1::V1Model::instance().algorithm.crc16.name) return "crc16"_cs;
228+
if (algorithm == P4V1::V1Model::instance().algorithm.crc16_custom.name)
229+
return "crc16_custom"_cs;
230+
if (algorithm == P4V1::V1Model::instance().algorithm.random.name) return "random"_cs;
231+
if (algorithm == P4V1::V1Model::instance().algorithm.identity.name) return "identity"_cs;
232+
if (algorithm == P4V1::V1Model::instance().algorithm.csum16.name) return "csum16"_cs;
233+
if (algorithm == P4V1::V1Model::instance().algorithm.xor16.name) return "xor16"_cs;
232234

233235
::P4::error(ErrorType::ERR_UNSUPPORTED, "Unsupported algorithm %1%", algorithm);
234236
return cstring::empty;

backends/bmv2/common/extern.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ class ExternConverter {
7272

7373
#define EXTERN_CONVERTER_W_FUNCTION_AND_MODEL(extern_name, model_type, model_name) \
7474
class ExternConverter_##extern_name : public ExternConverter { \
75-
model_type &model_name; \
76-
ExternConverter_##extern_name() : model_name(model_type::instance) { \
75+
const model_type &model_name; \
76+
ExternConverter_##extern_name() : model_name(model_type::instance()) { \
7777
registerExternConverter(cstring(#extern_name), this); \
7878
} \
7979
static ExternConverter_##extern_name singleton; \
@@ -95,8 +95,8 @@ class ExternConverter {
9595

9696
#define EXTERN_CONVERTER_W_INSTANCE_AND_MODEL(extern_name, model_type, model_name) \
9797
class ExternConverter_##extern_name : public ExternConverter { \
98-
model_type &model_name; \
99-
ExternConverter_##extern_name() : model_name(model_type::instance) { \
98+
const model_type &model_name; \
99+
ExternConverter_##extern_name() : model_name(model_type::instance()) { \
100100
registerExternConverter(cstring(#extern_name), this); \
101101
} \
102102
static ExternConverter_##extern_name singleton; \
@@ -114,8 +114,8 @@ class ExternConverter {
114114

115115
#define EXTERN_CONVERTER_W_OBJECT_AND_INSTANCE_AND_MODEL(extern_name, type, name) \
116116
class ExternConverter_##extern_name : public ExternConverter { \
117-
type &name; \
118-
ExternConverter_##extern_name() : name(type::instance) { \
117+
const type &name; \
118+
ExternConverter_##extern_name() : name(type::instance()) { \
119119
registerExternConverter(cstring(#extern_name), this); \
120120
} \
121121
static ExternConverter_##extern_name singleton; \

backends/bmv2/common/helpers.h

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ limitations under the License.
2222
#include "controlFlowGraph.h"
2323
#include "expression.h"
2424
#include "frontends/common/model.h"
25+
#include "frontends/p4-14/fromv1.0/v1model.h"
2526
#include "ir/ir.h"
2627
#include "lib/cstring.h"
2728
#include "lib/json.h"
2829
#include "lib/ordered_map.h"
29-
#include "frontends/p4-14/fromv1.0/v1model.h"
3030

3131
namespace P4::BMV2 {
3232

@@ -90,9 +90,9 @@ template <>
9090
struct ActionProfileTraits<Arch::V1MODEL> {
9191
static const cstring name() { return "action profile"_cs; }
9292
static const cstring propertyName() {
93-
return P4V1::V1Model::instance.tableAttributes.tableImplementation.name;
93+
return P4V1::V1Model::instance().tableAttributes.tableImplementation.name;
9494
}
95-
static const cstring typeName() { return P4V1::V1Model::instance.action_profile.name; }
95+
static const cstring typeName() { return P4V1::V1Model::instance().action_profile.name; }
9696
static const cstring sizeParamName() { return "size"_cs; }
9797
};
9898

@@ -118,7 +118,7 @@ struct ActionSelectorTraits;
118118
template <>
119119
struct ActionSelectorTraits<Arch::V1MODEL> : public ActionProfileTraits<Arch::V1MODEL> {
120120
static const cstring name() { return "action selector"_cs; }
121-
static const cstring typeName() { return P4V1::V1Model::instance.action_selector.name; }
121+
static const cstring typeName() { return P4V1::V1Model::instance().action_selector.name; }
122122
};
123123

124124
template <>
@@ -140,7 +140,7 @@ struct RegisterTraits;
140140
template <>
141141
struct RegisterTraits<Arch::V1MODEL> {
142142
static const cstring name() { return "register"_cs; }
143-
static const cstring typeName() { return P4V1::V1Model::instance.registers.name; }
143+
static const cstring typeName() { return P4V1::V1Model::instance().registers.name; }
144144
static const cstring sizeParamName() { return "size"_cs; }
145145
/// the index of the type parameter for the data stored in the register, in
146146
/// the type parameter list of the extern type declaration.
@@ -198,10 +198,10 @@ template <>
198198
struct CounterlikeTraits<Standard::CounterExtern<Standard::Arch::V1MODEL>> {
199199
static const cstring name() { return "counter"_cs; }
200200
static const cstring directPropertyName() {
201-
return P4V1::V1Model::instance.tableAttributes.counters.name;
201+
return P4V1::V1Model::instance().tableAttributes.counters.name;
202202
}
203-
static const cstring typeName() { return P4V1::V1Model::instance.counter.name; }
204-
static const cstring directTypeName() { return P4V1::V1Model::instance.directCounter.name; }
203+
static const cstring typeName() { return P4V1::V1Model::instance().counter.name; }
204+
static const cstring directTypeName() { return P4V1::V1Model::instance().directCounter.name; }
205205
static const cstring sizeParamName() { return "size"_cs; }
206206
static std::optional<size_t> indexTypeParamIdx() { return std::nullopt; }
207207
};
@@ -210,10 +210,10 @@ template <>
210210
struct CounterlikeTraits<Standard::CounterExtern<Standard::Arch::V1MODEL2020>> {
211211
static const cstring name() { return "counter"_cs; }
212212
static const cstring directPropertyName() {
213-
return P4V1::V1Model::instance.tableAttributes.counters.name;
213+
return P4V1::V1Model::instance().tableAttributes.counters.name;
214214
}
215-
static const cstring typeName() { return P4V1::V1Model::instance.counter.name; }
216-
static const cstring directTypeName() { return P4V1::V1Model::instance.directCounter.name; }
215+
static const cstring typeName() { return P4V1::V1Model::instance().counter.name; }
216+
static const cstring directTypeName() { return P4V1::V1Model::instance().directCounter.name; }
217217
static const cstring sizeParamName() { return "size"_cs; }
218218
static std::optional<size_t> indexTypeParamIdx() { return 0; }
219219
};
@@ -243,10 +243,10 @@ template <>
243243
struct CounterlikeTraits<Standard::MeterExtern<Standard::Arch::V1MODEL>> {
244244
static const cstring name() { return "meter"_cs; }
245245
static const cstring directPropertyName() {
246-
return P4V1::V1Model::instance.tableAttributes.meters.name;
246+
return P4V1::V1Model::instance().tableAttributes.meters.name;
247247
}
248-
static const cstring typeName() { return P4V1::V1Model::instance.meter.name; }
249-
static const cstring directTypeName() { return P4V1::V1Model::instance.directMeter.name; }
248+
static const cstring typeName() { return P4V1::V1Model::instance().meter.name; }
249+
static const cstring directTypeName() { return P4V1::V1Model::instance().directMeter.name; }
250250
static const cstring sizeParamName() { return "size"_cs; }
251251
static std::optional<size_t> indexTypeParamIdx() { return std::nullopt; }
252252
};
@@ -255,10 +255,10 @@ template <>
255255
struct CounterlikeTraits<Standard::MeterExtern<Standard::Arch::V1MODEL2020>> {
256256
static const cstring name() { return "meter"_cs; }
257257
static const cstring directPropertyName() {
258-
return P4V1::V1Model::instance.tableAttributes.meters.name;
258+
return P4V1::V1Model::instance().tableAttributes.meters.name;
259259
}
260-
static const cstring typeName() { return P4V1::V1Model::instance.meter.name; }
261-
static const cstring directTypeName() { return P4V1::V1Model::instance.directMeter.name; }
260+
static const cstring typeName() { return P4V1::V1Model::instance().meter.name; }
261+
static const cstring directTypeName() { return P4V1::V1Model::instance().directMeter.name; }
262262
static const cstring sizeParamName() { return "size"_cs; }
263263
static std::optional<size_t> indexTypeParamIdx() { return 0; }
264264
};

backends/bmv2/common/lower.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ const IR::Node *RemoveComplexExpressions::postorder(IR::MethodCallExpression *ex
175175
if (mi->isApply() || mi->is<P4::BuiltInMethod>()) return expression;
176176

177177
if (auto ef = mi->to<P4::ExternFunction>()) {
178-
if (ef->method->name == P4V1::V1Model::instance.digest_receiver.name) {
178+
if (ef->method->name == P4V1::V1Model::instance().digest_receiver.name) {
179179
// Special handling for digest; the semantics on bmv2 is to
180180
// execute the digest at the very end of the pipeline, and to
181181
// pass a reference to the fields, so fields can be modified

backends/bmv2/common/parser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ Util::IJson *ParserConverter::convertParserStatement(const IR::StatOrDecl *stat)
273273
paramsArray->append(expr);
274274
paramValue->emplace("op", extFuncName);
275275
paramValue->emplace_non_null("source_info"_cs, mce->sourceInfoJsonObj());
276-
} else if (extFuncName == P4V1::V1Model::instance.log_msg.name) {
276+
} else if (extFuncName == P4V1::V1Model::instance().log_msg.name) {
277277
BUG_CHECK(mce->arguments->size() == 2 || mce->arguments->size() == 1,
278278
"%1%: Expected 1 or 2 arguments", mce);
279279
result->emplace("op", "primitive");

backends/bmv2/simple_switch/simpleSwitch.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,18 +127,18 @@ class SimpleSwitchExpressionConverter : public ExpressionConverter {
127127

128128
class ParseV1Architecture : public Inspector {
129129
V1ProgramStructure *structure;
130-
P4V1::V1Model &v1model;
130+
const P4V1::V1Model &v1model;
131131

132132
public:
133133
explicit ParseV1Architecture(V1ProgramStructure *structure)
134-
: structure(structure), v1model(P4V1::V1Model::instance) {}
134+
: structure(structure), v1model(P4V1::V1Model::instance()) {}
135135
void modelError(const char *format, const IR::Node *node);
136136
bool preorder(const IR::PackageBlock *block) override;
137137
};
138138

139139
class SimpleSwitchBackend : public Backend {
140140
BMV2Options &options;
141-
P4V1::V1Model &v1model;
141+
const P4V1::V1Model &v1model;
142142
V1ProgramStructure *structure = nullptr;
143143
ExpressionConverter *conv = nullptr;
144144

@@ -159,7 +159,7 @@ class SimpleSwitchBackend : public Backend {
159159
P4::ConvertEnums::EnumMapping *enumMap)
160160
: Backend(options, refMap, typeMap, enumMap),
161161
options(options),
162-
v1model(P4V1::V1Model::instance) {}
162+
v1model(P4V1::V1Model::instance()) {}
163163
};
164164

165165
EXTERN_CONVERTER_W_FUNCTION(clone)

backends/p4fmt/p4formatter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ bool P4Formatter::preorder(const IR::P4Program *program) {
6666
if (includesEmitted.count(sourceFile.string()) == 0) {
6767
if (sourceFile.parent_path() == p4includePath) {
6868
std::filesystem::path p = sourceFile.filename();
69-
if (P4V1::V1Model::instance.file.name == p) {
69+
if (P4V1::V1Model::instance().file.name == p) {
7070
P4V1::GetV1ModelVersion g;
7171
program->apply(g);
7272
builder.append("#define V1MODEL_VERSION ");

backends/p4test/midend.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,12 @@ MidEnd::MidEnd(P4TestOptions &options, std::ostream *outStream) {
151151
return root;
152152
}
153153
// Special handling when compiling for v1model.p4
154-
if (main->type->name == P4V1::V1Model::instance.sw.name) {
154+
if (main->type->name == P4V1::V1Model::instance().sw.name) {
155155
if (main->getConstructorParameters()->size() != 6) return root;
156-
auto verify = main->getParameterValue(P4V1::V1Model::instance.sw.verify.name);
157-
auto update = main->getParameterValue(P4V1::V1Model::instance.sw.compute.name);
158-
auto deparser = main->getParameterValue(P4V1::V1Model::instance.sw.deparser.name);
156+
auto verify = main->getParameterValue(P4V1::V1Model::instance().sw.verify.name);
157+
auto update = main->getParameterValue(P4V1::V1Model::instance().sw.compute.name);
158+
auto deparser =
159+
main->getParameterValue(P4V1::V1Model::instance().sw.deparser.name);
159160
if (verify == nullptr || update == nullptr || deparser == nullptr ||
160161
!verify->is<IR::ControlBlock>() || !update->is<IR::ControlBlock>() ||
161162
!deparser->is<IR::ControlBlock>()) {

backends/tofino/bf-p4c/arch/fromv1.0/phase0.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -280,17 +280,17 @@ struct FindPhase0Table : public Inspector {
280280
bool hasNoSideEffects(const IR::P4Table *table, cstring &errStr) const {
281281
// Actions profiles aren't allowed.
282282
errStr = "Action profiles not allowed on phase 0 table"_cs;
283-
auto implProp = P4V1::V1Model::instance.tableAttributes.tableImplementation.name;
283+
auto implProp = P4V1::V1Model::instance().tableAttributes.tableImplementation.name;
284284
if (table->properties->getProperty(implProp) != nullptr) return false;
285285

286286
errStr = "Counters not allowed on phase 0 table"_cs;
287287
// Counters aren't allowed.
288-
auto counterProp = P4V1::V1Model::instance.tableAttributes.counters.name;
288+
auto counterProp = P4V1::V1Model::instance().tableAttributes.counters.name;
289289
if (table->properties->getProperty(counterProp) != nullptr) return false;
290290

291291
// Meters aren't allowed.
292292
errStr = "Meters not allowed on phase 0 table"_cs;
293-
auto meterProp = P4V1::V1Model::instance.tableAttributes.meters.name;
293+
auto meterProp = P4V1::V1Model::instance().tableAttributes.meters.name;
294294
if (table->properties->getProperty(meterProp) != nullptr) return false;
295295

296296
// Statefuls aren't allowed.

control-plane/p4RuntimeArchHandler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ int64_t getTableSize(const IR::P4Table *table) {
9292
// TODO(antonin): we should not be referring to v1model in this
9393
// architecture-independent code; each architecture may have a different
9494
// default table size.
95-
const int64_t defaultTableSize = P4V1::V1Model::instance.tableAttributes.defaultTableSize;
95+
const int64_t defaultTableSize = P4V1::V1Model::instance().tableAttributes.defaultTableSize;
9696

9797
auto sizeProperty = table->properties->getProperty("size");
9898
if (sizeProperty == nullptr) {

0 commit comments

Comments
 (0)