Skip to content

Commit ea8120f

Browse files
author
mbudiu-vmw
committed
Refactored passes to establish their own precondition; new passes to simplify parsers
1 parent 5e765d4 commit ea8120f

File tree

141 files changed

+1677
-700
lines changed

Some content is hidden

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

141 files changed

+1677
-700
lines changed

backends/bmv2/midend.cpp

+28-63
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ limitations under the License.
3030
#include "midend/convertEnums.h"
3131
#include "midend/simplifyKey.h"
3232
#include "midend/simplifyExpressions.h"
33-
#include "midend/unreachableStates.h"
33+
#include "midend/simplifyParsers.h"
34+
#include "midend/resetHeaders.h"
3435
#include "frontends/p4/strengthReduction.h"
3536
#include "frontends/p4/typeMap.h"
3637
#include "frontends/p4/evaluator/evaluator.h"
@@ -53,12 +54,12 @@ void MidEnd::setup_for_P4_14(CompilerOptions&) {
5354
// human-readable results.
5455

5556
addPasses({
56-
new P4::TypeChecking(&refMap, &typeMap, false, isv1),
57+
new P4::TypeChecking(&refMap, &typeMap, isv1),
5758
evaluator,
5859
new P4::DiscoverInlining(&controlsToInline, &refMap, &typeMap, evaluator),
5960
new P4::InlineDriver(&controlsToInline, new SimpleControlsInliner(&refMap), isv1),
6061
new P4::RemoveAllUnusedDeclarations(&refMap, isv1),
61-
new P4::TypeChecking(&refMap, &typeMap, false, isv1),
62+
new P4::TypeChecking(&refMap, &typeMap, isv1),
6263
new P4::DiscoverActionsInlining(&actionsToInline, &refMap, &typeMap),
6364
new P4::InlineActionsDriver(&actionsToInline, new SimpleActionsInliner(&refMap), isv1),
6465
new P4::RemoveAllUnusedDeclarations(&refMap, isv1),
@@ -85,78 +86,45 @@ class EnumOn32Bits : public P4::ChooseEnumRepresentation {
8586
void MidEnd::setup_for_P4_16(CompilerOptions& options) {
8687
// we may come through this path even if the program is actually a P4 v1.0 program
8788
bool isv1 = options.isv1();
88-
auto evaluator = new P4::Evaluator(&refMap, &typeMap);
89+
auto evaluator = new P4::EvaluatorPass(&refMap, &typeMap, isv1);
8990

9091
addPasses({
91-
new P4::ResolveReferences(&refMap, isv1),
92-
new P4::UnreachableParserStates(&refMap),
93-
new P4::TypeChecking(&refMap, &typeMap, false, isv1),
94-
new P4::ConvertEnums(new EnumOn32Bits(), &typeMap),
95-
// Proper semantics for uninitialzed local variables in parser states:
96-
// headers must be invalidated. Must recompute all types after ConvertEnums
97-
new P4::TypeChecking(&refMap, &typeMap, true, isv1),
98-
new P4::ResetHeaders(&typeMap),
99-
// Give each local declaration a unique internal name
92+
new P4::SimplifyParsers(&refMap, isv1),
93+
new P4::ConvertEnums(&refMap, &typeMap, isv1,
94+
new EnumOn32Bits()),
95+
new P4::ResetHeaders(&refMap, &typeMap, isv1),
10096
new P4::UniqueNames(&refMap, isv1),
101-
// Move all local declarations to the beginning
10297
new P4::MoveDeclarations(),
10398
new P4::MoveInitializers(),
104-
new P4::TypeChecking(&refMap, &typeMap, false, isv1),
105-
new P4::SimplifyExpressions(&refMap, &typeMap),
106-
new P4::ResolveReferences(&refMap, isv1),
107-
new P4::RemoveReturns(&refMap),
108-
// Move some constructor calls into temporaries
99+
new P4::SimplifyExpressions(&refMap, &typeMap, isv1),
100+
new P4::RemoveReturns(&refMap, isv1),
109101
new P4::MoveConstructors(&refMap, isv1),
110102
new P4::RemoveAllUnusedDeclarations(&refMap, isv1),
111-
new P4::TypeChecking(&refMap, &typeMap, true, isv1),
103+
new P4::ClearTypeMap(&typeMap),
112104
evaluator,
113-
114105
new VisitFunctor([evaluator](const IR::Node *root) -> const IR::Node * {
115106
auto toplevel = evaluator->getToplevelBlock();
116107
if (toplevel->getMain() == nullptr)
117108
// nothing further to do
118109
return nullptr;
119110
return root; }),
120-
121-
// Inlining
122-
new P4::DiscoverInlining(&controlsToInline, &refMap, &typeMap, evaluator),
123-
new P4::InlineDriver(&controlsToInline, new P4::GeneralInliner(isv1), isv1),
124-
new P4::RemoveAllUnusedDeclarations(&refMap, isv1),
125-
new P4::TypeChecking(&refMap, &typeMap, false, isv1),
126-
new P4::DiscoverActionsInlining(&actionsToInline, &refMap, &typeMap),
127-
new P4::InlineActionsDriver(&actionsToInline, new P4::ActionsInliner(), isv1),
128-
new P4::RemoveAllUnusedDeclarations(&refMap, isv1),
129-
// TODO: simplify statements and expressions.
130-
// This is required for the correctness of some of the following passes.
131-
132-
// Clone an action for each use, so we can specialize the action
133-
// per user (e.g., for each table or direct invocation).
111+
new P4::Inline(&refMap, &typeMap, evaluator, isv1),
112+
new P4::InlineActions(&refMap, &typeMap, isv1),
134113
new P4::LocalizeAllActions(&refMap, isv1),
135-
new P4::RemoveAllUnusedDeclarations(&refMap, isv1),
136-
// Table and action parameters also get unique names
137114
new P4::UniqueParameters(&refMap, isv1),
138-
// Clear types after LocalizeAllActions
139-
new P4::TypeChecking(&refMap, &typeMap, true, isv1),
140-
new P4::SimplifyControlFlow(&refMap, &typeMap),
115+
new P4::ClearTypeMap(&typeMap),
116+
new P4::SimplifyControlFlow(&refMap, &typeMap, isv1),
141117
new P4::RemoveParameters(&refMap, &typeMap, isv1),
142-
new P4::TypeChecking(&refMap, &typeMap, true, isv1),
143-
new P4::SimplifyKey(&refMap, &typeMap,
118+
new P4::ClearTypeMap(&typeMap),
119+
new P4::SimplifyKey(&refMap, &typeMap, isv1,
144120
new P4::NonLeftValue(&refMap, &typeMap)),
145-
// Final simplifications
146-
new P4::TypeChecking(&refMap, &typeMap, false, isv1),
147-
new P4::SimplifyControlFlow(&refMap, &typeMap),
148-
new P4::TypeChecking(&refMap, &typeMap, false, isv1),
149-
new P4::ConstantFolding(&refMap, &typeMap),
121+
new P4::ConstantFolding(&refMap, &typeMap, isv1),
150122
new P4::StrengthReduction(),
151-
new P4::TypeChecking(&refMap, &typeMap, false, isv1),
152-
new P4::LocalCopyPropagation(&typeMap),
123+
new P4::LocalCopyPropagation(&refMap, &typeMap, isv1),
153124
new P4::MoveDeclarations(),
154-
// Create actions for statements that can't be done in control blocks.
155-
new P4::TypeChecking(&refMap, &typeMap, false, isv1),
156-
new P4::SynthesizeActions(&refMap, &typeMap),
157-
// Move all stand-alone actions to custom tables
158-
new P4::TypeChecking(&refMap, &typeMap, false, isv1),
159-
new P4::MoveActionsToTables(&refMap, &typeMap),
125+
new P4::SimplifyControlFlow(&refMap, &typeMap, isv1),
126+
new P4::SynthesizeActions(&refMap, &typeMap, isv1),
127+
new P4::MoveActionsToTables(&refMap, &typeMap, isv1),
160128
});
161129
}
162130

@@ -172,17 +140,14 @@ MidEnd::MidEnd(CompilerOptions& options) {
172140
setup_for_P4_16(options);
173141

174142
// BMv2-specific passes
175-
auto evaluator = new P4::Evaluator(&refMap, &typeMap);
143+
auto evaluator = new P4::EvaluatorPass(&refMap, &typeMap, isv1);
176144
addPasses({
177-
new P4::TypeChecking(&refMap, &typeMap, false, isv1),
178-
new P4::SimplifyControlFlow(&refMap, &typeMap),
179-
new P4::TypeChecking(&refMap, &typeMap, false, isv1),
145+
new P4::SimplifyControlFlow(&refMap, &typeMap, isv1),
146+
new P4::TypeChecking(&refMap, &typeMap, isv1),
180147
new P4::RemoveLeftSlices(&typeMap),
181-
new P4::TypeChecking(&refMap, &typeMap, false, isv1),
148+
new P4::TypeChecking(&refMap, &typeMap, isv1),
182149
new LowerExpressions(&typeMap),
183-
new P4::TypeChecking(&refMap, &typeMap, false, isv1),
184-
new P4::ConstantFolding(&refMap, &typeMap),
185-
new P4::TypeChecking(&refMap, &typeMap, false, isv1),
150+
new P4::ConstantFolding(&refMap, &typeMap, isv1),
186151
evaluator,
187152
new VisitFunctor([this, evaluator]() { toplevel = evaluator->getToplevelBlock(); })
188153
});

backends/ebpf/midend.cpp

+21-46
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ limitations under the License.
2626
#include "midend/removeParameters.h"
2727
#include "midend/local_copyprop.h"
2828
#include "midend/simplifyExpressions.h"
29-
#include "midend/unreachableStates.h"
29+
#include "midend/simplifyParsers.h"
30+
#include "midend/resetHeaders.h"
31+
#include "midend/simplifyKey.h"
3032
#include "frontends/p4/typeMap.h"
3133
#include "frontends/p4/evaluator/evaluator.h"
3234
#include "frontends/p4/typeChecking/typeChecker.h"
@@ -44,29 +46,19 @@ const IR::ToplevelBlock* MidEnd::run(EbpfOptions& options, const IR::P4Program*
4446
return nullptr;
4547

4648
bool isv1 = options.langVersion == CompilerOptions::FrontendVersion::P4_14;
47-
auto evaluator = new P4::Evaluator(&refMap, &typeMap);
49+
auto evaluator = new P4::EvaluatorPass(&refMap, &typeMap, isv1);
4850

4951
PassManager simplify = {
50-
new P4::ResolveReferences(&refMap, isv1),
51-
new P4::UnreachableParserStates(&refMap),
52-
// Proper semantics for uninitialzed local variables in parser states:
53-
// headers must be invalidated
54-
new P4::TypeChecking(&refMap, &typeMap, false, isv1),
55-
new P4::ResetHeaders(&typeMap),
56-
// Give each local declaration a unique internal name
52+
new P4::SimplifyParsers(&refMap, isv1),
53+
new P4::ResetHeaders(&refMap, &typeMap, isv1),
5754
new P4::UniqueNames(&refMap, isv1),
58-
// Move all local declarations to the beginning
5955
new P4::MoveDeclarations(),
6056
new P4::MoveInitializers(),
61-
new P4::TypeChecking(&refMap, &typeMap, false, isv1),
62-
new P4::SimplifyExpressions(&refMap, &typeMap),
63-
new P4::ResolveReferences(&refMap, isv1),
64-
new P4::RemoveReturns(&refMap), // necessary for inlining
65-
// Move some constructor calls into temporaries
57+
new P4::SimplifyExpressions(&refMap, &typeMap, isv1),
58+
new P4::RemoveReturns(&refMap, isv1),
6659
new P4::MoveConstructors(&refMap, isv1),
67-
new P4::ResolveReferences(&refMap, isv1),
68-
new P4::RemoveUnusedDeclarations(&refMap),
69-
new P4::TypeChecking(&refMap, &typeMap, true, isv1),
60+
new P4::RemoveAllUnusedDeclarations(&refMap, isv1),
61+
new P4::ClearTypeMap(&typeMap),
7062
evaluator,
7163
};
7264

@@ -84,39 +76,22 @@ const IR::ToplevelBlock* MidEnd::run(EbpfOptions& options, const IR::P4Program*
8476
P4::ActionsInlineList actionsToInline;
8577

8678
PassManager midEnd = {
87-
// Perform inlining for controls and parsers (parsers not yet implemented)
88-
new P4::DiscoverInlining(&toInline, &refMap, &typeMap, evaluator),
89-
new P4::InlineDriver(&toInline, new P4::GeneralInliner(isv1), isv1),
90-
new P4::RemoveAllUnusedDeclarations(&refMap, isv1),
91-
// Perform inlining for actions calling other actions
92-
new P4::TypeChecking(&refMap, &typeMap, false, isv1),
93-
new P4::DiscoverActionsInlining(&actionsToInline, &refMap, &typeMap),
94-
new P4::InlineActionsDriver(&actionsToInline, new P4::ActionsInliner(), isv1),
95-
new P4::RemoveAllUnusedDeclarations(&refMap, isv1),
96-
// TODO: simplify statements and expressions.
97-
// This is required for the correctness of some of the following passes.
98-
99-
// Clone an action for each use, so we can specialize the action
100-
// per user (e.g., for each table or direct invocation).
79+
new P4::Inline(&refMap, &typeMap, evaluator, isv1),
80+
new P4::InlineActions(&refMap, &typeMap, isv1),
10181
new P4::LocalizeAllActions(&refMap, isv1),
102-
new P4::RemoveAllUnusedDeclarations(&refMap, isv1),
103-
// Table and action parameters also get unique names
10482
new P4::UniqueParameters(&refMap, isv1),
105-
// Must clear types after LocalizeAllActions
106-
new P4::TypeChecking(&refMap, &typeMap, true, isv1),
107-
new P4::SimplifyControlFlow(&refMap, &typeMap),
83+
new P4::ClearTypeMap(&typeMap),
84+
new P4::SimplifyControlFlow(&refMap, &typeMap, isv1),
10885
new P4::RemoveParameters(&refMap, &typeMap, isv1),
109-
// Exit statements are transformed into control-flow
110-
new P4::TypeChecking(&refMap, &typeMap, true, isv1),
111-
new P4::RemoveExits(&refMap, &typeMap),
112-
new P4::TypeChecking(&refMap, &typeMap, false, isv1),
113-
new P4::ConstantFolding(&refMap, &typeMap),
86+
new P4::ClearTypeMap(&typeMap),
87+
new P4::SimplifyKey(&refMap, &typeMap, isv1,
88+
new P4::NonLeftValue(&refMap, &typeMap)),
89+
new P4::RemoveExits(&refMap, &typeMap, isv1),
90+
new P4::ConstantFolding(&refMap, &typeMap, isv1),
11491
new P4::StrengthReduction(),
115-
new P4::TypeChecking(&refMap, &typeMap, false, isv1),
116-
new P4::LocalCopyPropagation(&typeMap),
92+
new P4::LocalCopyPropagation(&refMap, &typeMap, isv1),
11793
new P4::MoveDeclarations(), // more may have been introduced
118-
new P4::TypeChecking(&refMap, &typeMap, false, isv1),
119-
new P4::SimplifyControlFlow(&refMap, &typeMap),
94+
new P4::SimplifyControlFlow(&refMap, &typeMap, isv1),
12095
evaluator,
12196
};
12297
midEnd.setName("MidEnd");

backends/p4test/midend.cpp

+23-56
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ limitations under the License.
2929
#include "midend/parserUnroll.h"
3030
#include "midend/specialize.h"
3131
#include "midend/simplifyExpressions.h"
32-
#include "midend/unreachableStates.h"
32+
#include "midend/simplifyParsers.h"
33+
#include "midend/resetHeaders.h"
3334
#include "frontends/p4/typeMap.h"
3435
#include "frontends/p4/evaluator/evaluator.h"
35-
#include "frontends/p4/typeChecking/typeChecker.h"
3636
#include "frontends/common/resolveReferences/resolveReferences.h"
3737
#include "frontends/p4/toP4/toP4.h"
3838
#include "frontends/p4/simplify.h"
@@ -44,87 +44,54 @@ namespace P4Test {
4444

4545
MidEnd::MidEnd(CompilerOptions& options) {
4646
bool isv1 = options.langVersion == CompilerOptions::FrontendVersion::P4_14;
47-
auto evaluator = new P4::Evaluator(&refMap, &typeMap);
47+
auto evaluator = new P4::EvaluatorPass(&refMap, &typeMap, isv1);
4848
setName("MidEnd");
4949

5050
// TODO: def-use analysis and related optimizations
5151
// TODO: remove unnecessary parser transitions
52+
// TODO: detect labels after default in select expressions
5253
// TODO: parser loop unrolling
5354
// TODO: simplify actions which are too complex
5455
// TODO: lower errors to integers
5556
addPasses({
56-
new P4::ResolveReferences(&refMap, isv1),
57-
new P4::UnreachableParserStates(&refMap),
58-
// Proper semantics for uninitialzed local variables in parser states:
59-
// headers must be invalidated
60-
new P4::TypeChecking(&refMap, &typeMap, false, isv1),
61-
new P4::ResetHeaders(&typeMap),
62-
// Give each local declaration a unique internal name
63-
new P4::UniqueNames(&refMap, isv1),
64-
// Move all local declarations to the beginning
65-
new P4::MoveDeclarations(),
57+
new P4::SimplifyParsers(&refMap, isv1),
58+
new P4::ResetHeaders(&refMap, &typeMap, isv1),
59+
new P4::UniqueNames(&refMap, isv1), // Give each local declaration a unique internal name
60+
new P4::MoveDeclarations(), // Move all local declarations to the beginning
6661
new P4::MoveInitializers(),
67-
// Simplify expressions
68-
new P4::TypeChecking(&refMap, &typeMap, false, isv1),
69-
new P4::SimplifyExpressions(&refMap, &typeMap),
70-
new P4::ResolveReferences(&refMap, isv1),
71-
new P4::RemoveReturns(&refMap),
72-
// Move some constructor calls into temporaries
62+
new P4::SimplifyExpressions(&refMap, &typeMap, isv1),
63+
new P4::RemoveReturns(&refMap, isv1),
7364
new P4::MoveConstructors(&refMap, isv1),
7465
new P4::RemoveAllUnusedDeclarations(&refMap, isv1),
75-
new P4::TypeChecking(&refMap, &typeMap, true, isv1),
66+
new P4::ClearTypeMap(&typeMap),
7667
evaluator,
77-
7868
new VisitFunctor([evaluator](const IR::Node *root) -> const IR::Node * {
7969
auto toplevel = evaluator->getToplevelBlock();
8070
if (toplevel->getMain() == nullptr)
8171
// nothing further to do
8272
return nullptr;
8373
return root; }),
84-
85-
// Perform inlining for controls and parsers
86-
new P4::DiscoverInlining(&toInline, &refMap, &typeMap, evaluator),
87-
new P4::InlineDriver(&toInline, new P4::GeneralInliner(isv1), isv1),
88-
new P4::RemoveAllUnusedDeclarations(&refMap, isv1),
89-
// Perform inlining for actions calling other actions
90-
new P4::TypeChecking(&refMap, &typeMap, false, isv1),
91-
new P4::DiscoverActionsInlining(&actionsToInline, &refMap, &typeMap),
92-
new P4::InlineActionsDriver(&actionsToInline, new P4::ActionsInliner(), isv1),
93-
new P4::RemoveAllUnusedDeclarations(&refMap, isv1),
74+
new P4::Inline(&refMap, &typeMap, evaluator, isv1),
75+
new P4::InlineActions(&refMap, &typeMap, isv1),
9476
new P4::SpecializeAll(&refMap, &typeMap, isv1),
95-
new P4::RemoveAllUnusedDeclarations(&refMap, isv1),
9677
// Parser loop unrolling: TODO
9778
// new P4::ParsersUnroll(true, &refMap, &typeMap, isv1),
98-
// Clone an action for each use, so we can specialize the action
99-
// per user (e.g., for each table or direct invocation).
10079
new P4::LocalizeAllActions(&refMap, isv1),
101-
new P4::RemoveAllUnusedDeclarations(&refMap, isv1),
102-
// Table and action parameters also get unique names
10380
new P4::UniqueParameters(&refMap, isv1),
104-
// Must clear types after LocalizeAllActions
105-
new P4::TypeChecking(&refMap, &typeMap, true, isv1),
106-
new P4::SimplifyControlFlow(&refMap, &typeMap),
81+
new P4::ClearTypeMap(&typeMap), // table types have changed
82+
new P4::SimplifyControlFlow(&refMap, &typeMap, isv1),
10783
new P4::RemoveParameters(&refMap, &typeMap, isv1),
108-
new P4::TypeChecking(&refMap, &typeMap, true, isv1),
109-
new P4::SimplifyKey(&refMap, &typeMap,
84+
new P4::ClearTypeMap(&typeMap), // table types have changed
85+
new P4::SimplifyKey(&refMap, &typeMap, isv1,
11086
new P4::NonLeftValue(&refMap, &typeMap)),
111-
// Exit statements are transformed into control-flow
112-
new P4::TypeChecking(&refMap, &typeMap, false, isv1),
113-
new P4::RemoveExits(&refMap, &typeMap),
114-
new P4::TypeChecking(&refMap, &typeMap, false, isv1),
115-
new P4::ConstantFolding(&refMap, &typeMap),
87+
new P4::RemoveExits(&refMap, &typeMap, isv1),
88+
new P4::ConstantFolding(&refMap, &typeMap, isv1),
11689
new P4::StrengthReduction(),
117-
new P4::TypeChecking(&refMap, &typeMap, false, isv1),
118-
new P4::LocalCopyPropagation(&typeMap),
90+
new P4::LocalCopyPropagation(&refMap, &typeMap, isv1),
11991
new P4::MoveDeclarations(), // more may have been introduced
120-
new P4::TypeChecking(&refMap, &typeMap, false, isv1),
121-
new P4::SimplifyControlFlow(&refMap, &typeMap),
122-
// Create actions for statements that can't be done in control blocks.
123-
new P4::TypeChecking(&refMap, &typeMap, false, isv1),
124-
new P4::SynthesizeActions(&refMap, &typeMap),
125-
// Move all stand-alone action invocations to custom tables
126-
new P4::TypeChecking(&refMap, &typeMap, false, isv1),
127-
new P4::MoveActionsToTables(&refMap, &typeMap),
92+
new P4::SimplifyControlFlow(&refMap, &typeMap, isv1),
93+
new P4::SynthesizeActions(&refMap, &typeMap, isv1),
94+
new P4::MoveActionsToTables(&refMap, &typeMap, isv1),
12895
evaluator,
12996
new VisitFunctor([this, evaluator]() { toplevel = evaluator->getToplevelBlock(); })
13097
});

backends/p4test/midend.h

-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ class MidEnd : public PassManager {
2929
P4::ReferenceMap refMap;
3030
P4::TypeMap typeMap;
3131
IR::ToplevelBlock *toplevel = nullptr;
32-
P4::InlineWorkList toInline;
33-
P4::ActionsInlineList actionsToInline;
3432

3533
public:
3634
explicit MidEnd(CompilerOptions& options);

0 commit comments

Comments
 (0)