Skip to content

Commit 8538f5d

Browse files
Chris DoddChrisDodd
Chris Dodd
authored andcommitted
Add inline in ir classes to reduce indirections
- simplifies some code; will allow many more simplifications
1 parent 8ca1c07 commit 8538f5d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+827
-927
lines changed

backends/bmv2/analyzer.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ class CFGBuilder : public Inspector {
199199
return false;
200200
}
201201
bool preorder(const IR::BlockStatement* statement) override {
202-
for (auto s : *statement->components) {
202+
for (auto s : statement->components) {
203203
auto stat = s->to<IR::Statement>();
204204
if (stat == nullptr) continue;
205205
visit(stat);

backends/bmv2/jsonconverter.cpp

+37-37
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ class SharedActionSelectorCheck : public Inspector {
176176

177177
auto key = table->getKey();
178178
Input input;
179-
for (auto ke : *key->keyElements) {
179+
for (auto ke : key->keyElements) {
180180
auto mt = refMap->getDeclaration(ke->matchType->path, true)->to<IR::Declaration_ID>();
181181
BUG_CHECK(mt != nullptr, "%1%: could not find declaration", ke->matchType);
182182
if (mt->name.name != v1model.selectorMatchType.name) continue;
@@ -631,7 +631,7 @@ class ExpressionConverter : public Inspector {
631631
return;
632632
}
633633

634-
for (auto e : *expression->components) {
634+
for (auto e : expression->components) {
635635
auto t = get(e);
636636
result->append(t);
637637
}
@@ -813,16 +813,16 @@ cstring JsonConverter::createCalculation(cstring algo, const IR::Expression* fie
813813
calc->emplace("algo", algo);
814814
if (!fields->is<IR::ListExpression>()) {
815815
// expand it into a list
816-
auto vec = new IR::Vector<IR::Expression>();
816+
auto list = new IR::ListExpression({});
817817
auto type = typeMap->getType(fields, true);
818818
BUG_CHECK(type->is<IR::Type_StructLike>(), "%1%: expected a struct", fields);
819-
for (auto f : *type->to<IR::Type_StructLike>()->fields) {
819+
for (auto f : type->to<IR::Type_StructLike>()->fields) {
820820
auto e = new IR::Member(fields, f->name);
821821
auto ftype = typeMap->getType(f);
822822
typeMap->setType(e, ftype);
823-
vec->push_back(e);
823+
list->push_back(e);
824824
}
825-
fields = new IR::ListExpression(vec);
825+
fields = list;
826826
typeMap->setType(fields, type);
827827
}
828828
auto jright = conv->convert(fields);
@@ -838,8 +838,8 @@ JsonConverter::convertActionBody(const IR::Vector<IR::StatOrDecl>* body,
838838
for (auto s : *body) {
839839
if (!s->is<IR::Statement>()) {
840840
continue;
841-
} else if (s->is<IR::BlockStatement>()) {
842-
convertActionBody(s->to<IR::BlockStatement>()->components, result, fieldLists,
841+
} else if (auto block = s->to<IR::BlockStatement>()) {
842+
convertActionBody(&block->components, result, fieldLists,
843843
calculations, learn_lists);
844844
continue;
845845
} else if (s->is<IR::ReturnStatement>()) {
@@ -982,7 +982,7 @@ JsonConverter::convertActionBody(const IR::Vector<IR::StatOrDecl>* body,
982982
if (ef->method->name == v1model.clone.name) {
983983
BUG_CHECK(mc->arguments->size() == 2, "Expected 2 arguments for %1%", mc);
984984
cstring name = refMap->newName("fl");
985-
auto emptylist = new IR::ListExpression(new IR::Vector<IR::Expression>());
985+
auto emptylist = new IR::ListExpression({});
986986
id = createFieldList(emptylist, "field_lists", name, fieldLists);
987987
} else {
988988
BUG_CHECK(mc->arguments->size() == 3, "Expected 3 arguments for %1%", mc);
@@ -1117,7 +1117,7 @@ JsonConverter::convertActionBody(const IR::Vector<IR::StatOrDecl>* body,
11171117
void JsonConverter::addToFieldList(const IR::Expression* expr, Util::JsonArray* fl) {
11181118
if (expr->is<IR::ListExpression>()) {
11191119
auto le = expr->to<IR::ListExpression>();
1120-
for (auto e : *le->components) {
1120+
for (auto e : le->components) {
11211121
addToFieldList(e, fl);
11221122
}
11231123
return;
@@ -1127,7 +1127,7 @@ void JsonConverter::addToFieldList(const IR::Expression* expr, Util::JsonArray*
11271127
if (type->is<IR::Type_StructLike>()) {
11281128
// recursively add all fields
11291129
auto st = type->to<IR::Type_StructLike>();
1130-
for (auto f : *st->fields) {
1130+
for (auto f : st->fields) {
11311131
auto member = new IR::Member(expr, f->name);
11321132
typeMap->setType(member, typeMap->getType(f, true));
11331133
addToFieldList(member, fl);
@@ -1197,7 +1197,7 @@ Util::JsonArray* JsonConverter::createActions(Util::JsonArray* fieldLists,
11971197
params->append(param);
11981198
}
11991199
auto body = mkArrayField(jact, "primitives");
1200-
convertActionBody(action->body->components, body, fieldLists, calculations, learn_lists);
1200+
convertActionBody(&action->body->components, body, fieldLists, calculations, learn_lists);
12011201
result->append(jact);
12021202
}
12031203
return result;
@@ -1246,15 +1246,15 @@ void JsonConverter::convertTableEntries(const IR::P4Table *table,
12461246

12471247
auto entries = mkArrayField(jsonTable, "entries");
12481248
int entryPriority = 1; // default priority is defined by index position
1249-
for (auto e : *entriesList->entries) {
1249+
for (auto e : entriesList->entries) {
12501250
auto entry = new Util::JsonObject();
12511251

12521252
auto keyset = e->getKeys();
12531253
auto matchKeys = mkArrayField(entry, "match_key");
12541254
int keyIndex = 0;
1255-
for (auto k : *keyset->components) {
1255+
for (auto k : keyset->components) {
12561256
auto key = new Util::JsonObject();
1257-
auto tableKey = table->getKey()->keyElements->at(keyIndex);
1257+
auto tableKey = table->getKey()->keyElements.at(keyIndex);
12581258
auto keyWidth = tableKey->expression->type->width_bits();
12591259
auto k8 = ROUNDUP(keyWidth, 8);
12601260
auto matchType = getKeyMatchType(tableKey);
@@ -1418,7 +1418,7 @@ bool JsonConverter::handleTableImplementation(const IR::Property* implementation
14181418
selector->emplace("algo", algo);
14191419
}
14201420
auto input = mkArrayField(selector, "input");
1421-
for (auto ke : *key->keyElements) {
1421+
for (auto ke : key->keyElements) {
14221422
auto mt = refMap->getDeclaration(ke->matchType->path, true)
14231423
->to<IR::Declaration_ID>();
14241424
BUG_CHECK(mt != nullptr, "%1%: could not find declaration", ke->matchType);
@@ -1532,7 +1532,7 @@ JsonConverter::convertTable(const CFG::TableNode* node,
15321532
conv->simpleExpressionsOnly = true;
15331533

15341534
if (key != nullptr) {
1535-
for (auto ke : *key->keyElements) {
1535+
for (auto ke : key->keyElements) {
15361536
auto match_type = getKeyMatchType(ke);
15371537
if (match_type == v1model.selectorMatchType.name)
15381538
continue;
@@ -1740,7 +1740,7 @@ JsonConverter::convertTable(const CFG::TableNode* node,
17401740
auto al = table->getActionList();
17411741

17421742
std::map<cstring, cstring> useActionName;
1743-
for (auto a : *al->actionList) {
1743+
for (auto a : al->actionList) {
17441744
if (a->expression->is<IR::MethodCallExpression>()) {
17451745
auto mce = a->expression->to<IR::MethodCallExpression>();
17461746
if (mce->arguments->size() > 0)
@@ -1804,7 +1804,7 @@ JsonConverter::convertTable(const CFG::TableNode* node,
18041804
// Generate labels which don't show up and send them to
18051805
// the nextLabel.
18061806
if (!hitMiss) {
1807-
for (auto a : *al->actionList) {
1807+
for (auto a : al->actionList) {
18081808
cstring name = a->getName().name;
18091809
cstring label = ::get(useActionName, name);
18101810
if (labelsDone.find(label) == labelsDone.end())
@@ -1914,7 +1914,7 @@ Util::IJson* JsonConverter::convertControl(const IR::ControlBlock* block, cstrin
19141914
}
19151915
}
19161916

1917-
for (auto c : *cont->controlLocals) {
1917+
for (auto c : cont->controlLocals) {
19181918
if (c->is<IR::Declaration_Constant>() ||
19191919
c->is<IR::Declaration_Variable>() ||
19201920
c->is<IR::P4Action>() ||
@@ -2077,7 +2077,7 @@ unsigned JsonConverter::nextId(cstring group) {
20772077
}
20782078

20792079
void JsonConverter::addHeaderStacks(const IR::Type_Struct* headersStruct) {
2080-
for (auto f : *headersStruct->fields) {
2080+
for (auto f : headersStruct->fields) {
20812081
auto ft = typeMap->getType(f, true);
20822082
auto stack = ft->to<IR::Type_Stack>();
20832083
if (stack == nullptr)
@@ -2381,7 +2381,7 @@ void JsonConverter::createForceArith(const IR::Type* meta, cstring name,
23812381
if (!meta->is<IR::Type_Struct>())
23822382
return;
23832383
auto st = meta->to<IR::Type_StructLike>();
2384-
for (auto f : *st->fields) {
2384+
for (auto f : st->fields) {
23852385
auto field = pushNewArray(force);
23862386
field->append(name);
23872387
field->append(f->name.name);
@@ -2392,7 +2392,7 @@ void JsonConverter::generateUpdate(const IR::BlockStatement *block,
23922392
Util::JsonArray* checksums, Util::JsonArray* calculations) {
23932393
// Currently this is very hacky to target the very limited support for checksums in BMv2
23942394
// This will work much better when BMv2 offers a checksum extern.
2395-
for (auto stat : *block->components) {
2395+
for (auto stat : block->components) {
23962396
if (stat->is<IR::IfStatement>()) {
23972397
// The way checksums work in Json, they always ignore the condition!
23982398
stat = stat->to<IR::IfStatement>()->ifTrue;
@@ -2438,7 +2438,7 @@ void JsonConverter::generateUpdate(const IR::P4Control* updateControl,
24382438
}
24392439

24402440
void JsonConverter::addTypesAndInstances(const IR::Type_StructLike* type, bool meta) {
2441-
for (auto f : *type->fields) {
2441+
for (auto f : type->fields) {
24422442
auto ft = typeMap->getType(f, true);
24432443
if (ft->is<IR::Type_StructLike>()) {
24442444
// The headers struct can not contain nested structures.
@@ -2451,7 +2451,7 @@ void JsonConverter::addTypesAndInstances(const IR::Type_StructLike* type, bool m
24512451
}
24522452
}
24532453

2454-
for (auto f : *type->fields) {
2454+
for (auto f : type->fields) {
24552455
auto ft = typeMap->getType(f, true);
24562456
if (ft->is<IR::Type_StructLike>()) {
24572457
auto json = new Util::JsonObject();
@@ -2492,7 +2492,7 @@ void JsonConverter::addTypesAndInstances(const IR::Type_StructLike* type, bool m
24922492

24932493
void JsonConverter::pushFields(cstring prefix, const IR::Type_StructLike *st,
24942494
Util::JsonArray *fields) {
2495-
for (auto f : *st->fields) {
2495+
for (auto f : st->fields) {
24962496
auto ftype = typeMap->getType(f, true);
24972497
if (ftype->to<IR::Type_StructLike>()) {
24982498
BUG("%1%: nested structure", st);
@@ -2545,8 +2545,8 @@ void JsonConverter::convertDeparserBody(const IR::Vector<IR::StatOrDecl>* body,
25452545
Util::JsonArray* result) {
25462546
conv->simpleExpressionsOnly = true;
25472547
for (auto s : *body) {
2548-
if (s->is<IR::BlockStatement>()) {
2549-
convertDeparserBody(s->to<IR::BlockStatement>()->components, result);
2548+
if (auto block = s->to<IR::BlockStatement>()) {
2549+
convertDeparserBody(&block->components, result);
25502550
continue;
25512551
} else if (s->is<IR::ReturnStatement>() || s->is<IR::ExitStatement>()) {
25522552
break;
@@ -2596,7 +2596,7 @@ Util::IJson* JsonConverter::convertDeparser(const IR::P4Control* ctrl) {
25962596
result->emplace("name", "deparser"); // at least in simple_router this name is hardwired
25972597
result->emplace("id", nextId("deparser"));
25982598
auto order = mkArrayField(result, "order");
2599-
convertDeparserBody(ctrl->body->components, order);
2599+
convertDeparserBody(&ctrl->body->components, order);
26002600
return result;
26012601
}
26022602

@@ -2607,7 +2607,7 @@ Util::IJson* JsonConverter::toJson(const IR::P4Parser* parser) {
26072607
result->emplace("init_state", IR::ParserState::start);
26082608
auto states = mkArrayField(result, "parse_states");
26092609

2610-
for (auto state : *parser->states) {
2610+
for (auto state : parser->states) {
26112611
auto json = toJson(state);
26122612
if (json != nullptr)
26132613
states->append(json);
@@ -2758,15 +2758,15 @@ unsigned JsonConverter::combine(const IR::Expression* keySet,
27582758
return totalWidth;
27592759
} else if (keySet->is<IR::ListExpression>()) {
27602760
auto le = keySet->to<IR::ListExpression>();
2761-
BUG_CHECK(le->components->size() == select->components->size(),
2761+
BUG_CHECK(le->components.size() == select->components.size(),
27622762
"%1%: mismatched select", select);
27632763
unsigned index = 0;
27642764

27652765
bool noMask = true;
2766-
for (auto it = select->components->begin();
2767-
it != select->components->end(); ++it) {
2766+
for (auto it = select->components.begin();
2767+
it != select->components.end(); ++it) {
27682768
auto e = *it;
2769-
auto keyElement = le->components->at(index);
2769+
auto keyElement = le->components.at(index);
27702770

27712771
auto type = typeMap->getType(e, true);
27722772
int width = type->width_bits();
@@ -2790,9 +2790,9 @@ unsigned JsonConverter::combine(const IR::Expression* keySet,
27902790
mask = -1;
27912791
return totalWidth;
27922792
} else {
2793-
BUG_CHECK(select->components->size() == 1, "%1%: mismatched select/label", select);
2793+
BUG_CHECK(select->components.size() == 1, "%1%: mismatched select/label", select);
27942794
convertSimpleKey(keySet, value, mask);
2795-
auto type = typeMap->getType(select->components->at(0), true);
2795+
auto type = typeMap->getType(select->components.at(0), true);
27962796
return type->width_bits() / 8;
27972797
}
27982798
}
@@ -2816,7 +2816,7 @@ Util::IJson* JsonConverter::toJson(const IR::ParserState* state) {
28162816
result->emplace("name", extVisibleName(state));
28172817
result->emplace("id", nextId("parse_states"));
28182818
auto operations = mkArrayField(result, "parser_ops");
2819-
for (auto s : *state->components) {
2819+
for (auto s : state->components) {
28202820
auto j = convertParserStatement(s);
28212821
operations->append(j);
28222822
}

backends/bmv2/lower.cpp

+8-9
Original file line numberDiff line numberDiff line change
@@ -421,10 +421,10 @@ RemoveComplexExpressions::simplifyExpressions(const IR::Vector<IR::Expression>*
421421
for (auto e : *vec) {
422422
if (e->is<IR::ListExpression>()) {
423423
auto list = e->to<IR::ListExpression>();
424-
auto simpl = simplifyExpressions(list->components);
425-
if (simpl != list->components) {
424+
auto simpl = simplifyExpressions(&list->components);
425+
if (simpl != &list->components) {
426426
changes = true;
427-
auto l = new IR::ListExpression(e->srcInfo, simpl);
427+
auto l = new IR::ListExpression(e->srcInfo, *simpl);
428428
result->push_back(l);
429429
} else {
430430
result->push_back(e);
@@ -449,9 +449,9 @@ RemoveComplexExpressions::simplifyExpressions(const IR::Vector<IR::Expression>*
449449

450450
const IR::Node*
451451
RemoveComplexExpressions::postorder(IR::SelectExpression* expression) {
452-
auto vec = simplifyExpressions(expression->select->components);
453-
if (vec != expression->select->components)
454-
expression->select = new IR::ListExpression(expression->select->srcInfo, vec);
452+
auto vec = simplifyExpressions(&expression->select->components);
453+
if (vec != &expression->select->components)
454+
expression->select = new IR::ListExpression(expression->select->srcInfo, *vec);
455455
return expression;
456456
}
457457

@@ -484,9 +484,8 @@ const IR::Node*
484484
RemoveComplexExpressions::postorder(IR::Statement* statement) {
485485
if (assignments.empty())
486486
return statement;
487-
auto vec = new IR::IndexedVector<IR::StatOrDecl>(assignments);
488-
vec->push_back(statement);
489-
auto block = new IR::BlockStatement(vec);
487+
auto block = new IR::BlockStatement(assignments);
488+
block->push_back(statement);
490489
assignments.clear();
491490
return block;
492491
}

backends/bmv2/lower.h

+3-15
Original file line numberDiff line numberDiff line change
@@ -94,31 +94,19 @@ class RemoveComplexExpressions : public Transform {
9494
const IR::Node* preorder(IR::ParserState* state) override
9595
{ assignments.clear(); return state; }
9696
const IR::Node* postorder(IR::ParserState* state) override {
97-
if (!assignments.empty()) {
98-
auto comp = new IR::IndexedVector<IR::StatOrDecl>(*state->components);
99-
comp->append(assignments);
100-
state->components = comp;
101-
}
97+
state->components.append(assignments);
10298
return state;
10399
}
104100
const IR::Node* postorder(IR::MethodCallExpression* expression) override;
105101
const IR::Node* preorder(IR::P4Parser* parser) override
106102
{ newDecls.clear(); return parser; }
107103
const IR::Node* postorder(IR::P4Parser* parser) override {
108-
if (!newDecls.empty()) {
109-
auto locals = new IR::IndexedVector<IR::Declaration>(*parser->parserLocals);
110-
locals->append(newDecls);
111-
parser->parserLocals = locals;
112-
}
104+
parser->parserLocals.append(newDecls);
113105
return parser;
114106
}
115107
const IR::Node* preorder(IR::P4Control* control) override;
116108
const IR::Node* postorder(IR::P4Control* control) override {
117-
if (!newDecls.empty()) {
118-
auto locals = new IR::IndexedVector<IR::Declaration>(*control->controlLocals);
119-
locals->append(newDecls);
120-
control->controlLocals = locals;
121-
}
109+
control->controlLocals.append(newDecls);
122110
return control;
123111
}
124112
const IR::Node* postorder(IR::Statement* statement) override;

backends/ebpf/codeGen.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ bool CodeGenInspector::preorder(const IR::BoolLiteral* b) {
156156

157157
bool CodeGenInspector::preorder(const IR::ListExpression* expression) {
158158
bool first = true;
159-
for (auto e : *expression->components) {
159+
for (auto e : expression->components) {
160160
if (!first)
161161
builder->append(", ");
162162
first = false;
@@ -242,7 +242,7 @@ bool CodeGenInspector::preorder(const IR::AssignmentStatement* a) {
242242
bool CodeGenInspector::preorder(const IR::BlockStatement* s) {
243243
builder->blockStart();
244244
setVecSep("\n", "\n");
245-
visit(s->components);
245+
s->components.visit_children(*this);
246246
doneVec();
247247
builder->blockEnd(false);
248248
return false;

0 commit comments

Comments
 (0)