Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug-fixes in specialization #95

Merged
merged 10 commits into from
Oct 6, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion backends/bmv2/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ ir_DEF_FILES += $(srcdir)/backends/bmv2/bmv2.def

CLEANFILES += bmv2tests.mk

bmv2tests.mk: $(srcdir)/testdata/p4_16_samples/*-bmv2.p4 $(srcdir)/testdata/p4_14_samples/*.p4 \
bmv2tests.mk: $(srcdir)/testdata/p4_16_samples/*-bmv2.p4 \
$(srcdir)/testdata/p4_16_bmv_errors/*-bmv2.p4 \
$(srcdir)/testdata/p4_14_samples/*.p4 \
$(srcdir)/testdata/p4_14_samples/switch_*/switch.p4 \
$(srcdir)/testdata/p4_16_samples $(srcdir)/testdata/p4_14_samples
@$(GENTESTS) $(srcdir) bmv2 $(srcdir)/backends/bmv2/run-bmv2-test.py $^ >$@
Expand Down
16 changes: 14 additions & 2 deletions backends/bmv2/jsonconverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1868,7 +1868,10 @@ void JsonConverter::addTypesAndInstances(const IR::Type_StructLike* type, bool m
// Done elsewhere
continue;
} else {
BUG("%1%: Unexpected header type", ft);
if (!meta)
::error("Type %1% should only contain headers or stacks", type);
else
BUG("%1%: Unhandled type for %2%", ft, f);
}
}
}
Expand Down Expand Up @@ -1934,9 +1937,12 @@ void JsonConverter::convertDeparserBody(const IR::Vector<IR::StatOrDecl>* body,
ref += "[" + Util::toString(i) + "]";
result->append(ref);
}
} else {
} else if (type->is<IR::Type_Header>()) {
auto j = conv->convert(arg);
result->append(j->to<Util::JsonObject>()->get("value"));
} else {
::error("%1%: emit only supports header and stack arguments, not %2%",
arg, type);
}
}
continue;
Expand Down Expand Up @@ -1994,6 +2000,12 @@ Util::IJson* JsonConverter::convertParserStatement(const IR::StatOrDecl* stat) {
result->emplace("op", "extract");
if (mce->arguments->size() == 1) {
auto arg = mce->arguments->at(0);
auto argtype = typeMap->getType(arg, true);
if (!argtype->is<IR::Type_Header>()) {
::error("%1%: extract only accepts arguments with header types, not %2%",
arg, argtype);
return result;
}
auto param = new Util::JsonObject();
params->append(param);
cstring type;
Expand Down
2 changes: 2 additions & 0 deletions backends/bmv2/midend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ limitations under the License.
#include "midend/removeLeftSlices.h"
#include "midend/convertEnums.h"
#include "midend/simplifyKey.h"
#include "midend/parserControlFlow.h"
#include "midend/simplifySelect.h"
#include "frontends/p4/uniqueNames.h"
#include "frontends/p4/simplifyParsers.h"
Expand Down Expand Up @@ -82,6 +83,7 @@ void MidEnd::setup_for_P4_16(CompilerOptions&) {
// we may come through this path even if the program is actually a P4 v1.0 program
auto evaluator = new P4::EvaluatorPass(&refMap, &typeMap);
addPasses({
new P4::RemoveParserControlFlow(&refMap, &typeMap),
new P4::ConvertEnums(&refMap, &typeMap,
new EnumOn32Bits()),
new P4::RemoveReturns(&refMap),
Expand Down
3 changes: 0 additions & 3 deletions backends/bmv2/run-bmv2-test.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,9 +296,6 @@ def process_file(options, argv):
else:
result = SUCCESS

#if result == SUCCESS:
# result = check_generated_files(options, tmpdir, expected_dirname);

if result == SUCCESS:
result = run_model(options, tmpdir, jsonfile);

Expand Down
7 changes: 3 additions & 4 deletions backends/p4test/midend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ limitations under the License.
#include "midend/local_copyprop.h"
#include "midend/simplifyKey.h"
#include "midend/parserUnroll.h"
#include "midend/specialize.h"
#include "midend/simplifySelect.h"
#include "midend/parserControlFlow.h"
#include "frontends/p4/simplifyParsers.h"
#include "frontends/p4/typeMap.h"
#include "frontends/p4/evaluator/evaluator.h"
Expand All @@ -47,14 +47,14 @@ MidEnd::MidEnd(CompilerOptions& options) {
auto evaluator = new P4::EvaluatorPass(&refMap, &typeMap);
setName("MidEnd");

// TODO: def-use analysis and related optimizations
// TODO: def-use-related optimizations
// TODO: parser loop unrolling
// TODO: improve copy propagation
// TODO: simplify actions which are too complex
// TODO: lower errors to integers
// TODO: handle bit-slices as out arguments
// TODO: remove control-flow from parsers
addPasses({
new P4::RemoveParserControlFlow(&refMap, &typeMap),
new P4::RemoveReturns(&refMap),
new P4::MoveConstructors(&refMap),
new P4::RemoveAllUnusedDeclarations(&refMap),
Expand All @@ -68,7 +68,6 @@ MidEnd::MidEnd(CompilerOptions& options) {
return root; }),
new P4::Inline(&refMap, &typeMap, evaluator),
new P4::InlineActions(&refMap, &typeMap),
new P4::SpecializeAll(&refMap, &typeMap),
// Parser loop unrolling: TODO
// new P4::ParsersUnroll(true, &refMap, &typeMap),
new P4::LocalizeAllActions(&refMap),
Expand Down
1 change: 1 addition & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ p4c
├── p4_16_errors -- P4_16 negative input test programs
├── p4_16_samples_outputs -- Expected outputs from P4_16 tests
├── p4_16_errors_outputs -- Expected outputs from P4_16 negative tests
├── p4_16_bmv_errors -- P4_16 negative input tests for the bmv2 backend
├── v1_1_samples -- P4 v1.1 sample programs
├── p4_14_samples -- P4_14 input test programs
├── p4_14_samples_outputs -- Expected outputs from P4_14 tests
Expand Down
4 changes: 3 additions & 1 deletion frontends/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ p4_frontend_SOURCES = \
frontends/p4/resetHeaders.cpp \
frontends/p4/resetHeaders.h \
frontends/p4/moveDeclarations.cpp \
frontends/p4/moveDeclarations.h
frontends/p4/moveDeclarations.h \
frontends/p4/specialize.h \
frontends/p4/specialize.cpp

common_frontend_SOURCES = \
frontends/common/options.h \
Expand Down
4 changes: 3 additions & 1 deletion frontends/p4/def_use.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,8 @@ bool Definitions::operator==(const Definitions& other) const {
//////////////////////////////////////////////////////////////////////////////////////////////
// ComputeWriteSet implementation

// This assumes that all variable declarations have been pushed to the top
// This assumes that all variable declarations have been pushed to the top.
// We could remove this constraint if we also scanned variable declaration initializers.
void ComputeWriteSet::initialize(const IR::IApply* block,
const IR::IndexedVector<IR::Declaration>* locals,
ProgramPoint startPoint) {
Expand Down Expand Up @@ -590,6 +591,7 @@ bool ComputeWriteSet::preorder(const IR::P4Parser* parser) {
// We need a new visitor to visit the state,
// but we use the same data structures
ProgramPoint pt(state);
currentDefinitions = definitions->get(pt);
ComputeWriteSet cws(this, pt, currentDefinitions);
(void)state->apply(cws);

Expand Down
4 changes: 3 additions & 1 deletion frontends/p4/frontend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ limitations under the License.
#include "sideEffects.h"
#include "simplifyDefUse.h"
#include "simplifyParsers.h"
#include "specialize.h"

namespace P4 {

Expand Down Expand Up @@ -91,7 +92,7 @@ FrontEnd::run(const CompilerOptions &options, const IR::P4Program* program) {
// explicit casts where implicit casts exist.
new ResolveReferences(&refMap),
new TypeInference(&refMap, &typeMap),
new BindTypeVariables(&refMap, &typeMap),
new BindTypeVariables(&typeMap),
// Another round of constant folding, using type information.
new ClearTypeMap(&typeMap),
new ConstantFolding(&refMap, &typeMap),
Expand All @@ -106,6 +107,7 @@ FrontEnd::run(const CompilerOptions &options, const IR::P4Program* program) {
new SideEffectOrdering(&refMap, &typeMap),
new SimplifyControlFlow(&refMap, &typeMap),
new SimplifyDefUse(&refMap, &typeMap),
new P4::SpecializeAll(&refMap, &typeMap),
};

passes.setName("FrontEnd");
Expand Down
1 change: 1 addition & 0 deletions frontends/p4/p4-lex.l
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ std::string stringLiteral;
"this" { BEGIN(NORMAL); return THIS; }
"transition" { BEGIN(NORMAL); return TRANSITION; }
"true" { BEGIN(NORMAL); return TRUE; }
"tuple" { BEGIN(NORMAL); return TUPLE; }
"typedef" { BEGIN(NORMAL); return TYPEDEF; }
"varbit" { BEGIN(NORMAL); return VARBIT; }
"void" { BEGIN(NORMAL); return VOID; }
Expand Down
51 changes: 29 additions & 22 deletions frontends/p4/p4-parse.ypp
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ static int yylex();
DEFAULT DONTCARE ELSE ENUM T_ERROR // There is an ERROR macro elsewhere
EXIT EXTERN HEADER HEADER_UNION IF IN INOUT INT KEY MASK SELECT MATCH_KIND OUT
PACKAGE PARSER RANGE RETURN STATE STRUCT SWITCH TABLE TRANSITION
TYPEDEF VARBIT VOID
TUPLE TYPEDEF VARBIT VOID
%token<str> IDENTIFIER TYPE NAMESPACE STRING_LITERAL
%token<Constant> INTEGER

Expand All @@ -131,7 +131,7 @@ static int yylex();
keysetExpression selectExpression stateExpression optInitializer initializer
argument simpleKeysetExpression transitionStatement switchLabel
%type<PathPrefix> pathPrefix relativePathPrefix nonEmptyRelativePathPrefix
%type<TypePtr> baseType typeOrVoid specializedType headerStackType typeRef typeArg
%type<TypePtr> baseType typeOrVoid specializedType headerStackType typeRef tupleType typeArg
%type<Type_Name> typeName
%type<Parameter> parameter
%type<annos> optAnnotations
Expand Down Expand Up @@ -249,11 +249,11 @@ declaration
;

name
: IDENTIFIER { $$ = new IR::ID(@1, $1); }
| APPLY { $$ = new IR::ID(@1, "apply"); }
| KEY { $$ = new IR::ID(@1, "key"); }
| ACTIONS { $$ = new IR::ID(@1, "actions"); }
| STATE { $$ = new IR::ID(@1, "state"); }
: IDENTIFIER { $$ = new IR::ID(@1, $1, $1); }
| APPLY { $$ = new IR::ID(@1, "apply", "apply"); }
| KEY { $$ = new IR::ID(@1, "key", "key"); }
| ACTIONS { $$ = new IR::ID(@1, "actions", "actions"); }
| STATE { $$ = new IR::ID(@1, "state", "state"); }
;

optAnnotations
Expand Down Expand Up @@ -345,12 +345,12 @@ pathPrefix
relativePathPrefix
: /* empty */ { $$ = new IR::PathPrefix(); }
| relativePathPrefix NAMESPACE '.' { structure.pathAppendNamespace($2);
$$ = new IR::PathPrefix(@1 + @3, $1, IR::ID(@2, $2)); }
$$ = new IR::PathPrefix(@1 + @3, $1, IR::ID(@2, $2, $2)); }
;

nonEmptyRelativePathPrefix
: relativePathPrefix NAMESPACE '.' { structure.pathAppendNamespace($2);
$$ = new IR::PathPrefix(@1 + @3, $1, IR::ID(@2, $2)); }
$$ = new IR::PathPrefix(@1 + @3, $1, IR::ID(@2, $2, $2)); }
;

/**************************** PARSER ******************************/
Expand Down Expand Up @@ -431,8 +431,7 @@ selectCase
;

keysetExpression
: DEFAULT { $$ = new IR::DefaultExpression(@1); }
| tupleKeysetExpression { $$ = new IR::ListExpression(@1, $1); }
: tupleKeysetExpression { $$ = new IR::ListExpression(@1, $1); }
| simpleKeysetExpression { $$ = $1; }
;

Expand All @@ -451,6 +450,7 @@ simpleKeysetExpression
: expression { $$ = $1; }
| expression MASK expression { $$ = new IR::Mask(@1 + @3, $1, $3); }
| expression RANGE expression { $$ = new IR::Range(@1 + @3, $1, $3); }
| DEFAULT { $$ = new IR::DefaultExpression(@1); }
;

/*************************** CONTROL ************************/
Expand Down Expand Up @@ -521,7 +521,7 @@ methodPrototype
'(' parameterList ')' ';' { structure.pop(); }
{ auto par = new IR::ParameterList(@5, $5);
auto mt = new IR::Type_Method(@1, $2, nullptr, par);
$$ = new IR::Method(@1, IR::ID(@1, $1), mt, false); }
$$ = new IR::Method(@1, IR::ID(@1, $1, $1), mt, false); }
;

/************************** TYPES ****************************/
Expand All @@ -531,11 +531,16 @@ typeRef
| typeName { $$ = $1; }
| specializedType { $$ = $1; }
| headerStackType { $$ = $1; }
| tupleType { $$ = $1; }
;

typeName
: TYPE { $$ = new IR::Type_Name(@1, new IR::Path(IR::ID(@1, $1))); }
| pathPrefix TYPE { $$ = new IR::Type_Name(@1, new IR::Path($1, IR::ID(@2, $2))); }
: TYPE { $$ = new IR::Type_Name(@1, new IR::Path(IR::ID(@1, $1, $1))); }
| pathPrefix TYPE { $$ = new IR::Type_Name(@1, new IR::Path($1, IR::ID(@2, $2, $2))); }
;

tupleType
: TUPLE '<' typeArgumentList '>' { $$ = new IR::Type_Tuple(@1+@4, $3); }
;

headerStackType
Expand All @@ -544,10 +549,10 @@ headerStackType

specializedType
: pathPrefix TYPE '<' typeArgumentList '>'
{ auto id = IR::ID(@2, $2);
{ auto id = IR::ID(@2, $2, $2);
auto type = new IR::Type_Name(@1, new IR::Path($1, id));
$$ = new IR::Type_Specialized(@1 + @5, type, $4); }
| TYPE '<' typeArgumentList '>' { auto id = IR::ID(@1, $1);
| TYPE '<' typeArgumentList '>' { auto id = IR::ID(@1, $1, $1);
auto type = new IR::Type_Name(@1, new IR::Path(id));
$$ = new IR::Type_Specialized(@1 + @4, type, $3); }
;
Expand Down Expand Up @@ -626,7 +631,7 @@ structFieldList

structField
: optAnnotations typeRef name ';' { $$ = new IR::StructField(@3, *$3, $1, $2); }
| optAnnotations typeRef TYPE ';' { $$ = new IR::StructField(@3, IR::ID(@3, $3), $1, $2); }
| optAnnotations typeRef TYPE ';' { $$ = new IR::StructField(@3, IR::ID(@3, $3, $3), $1, $2); }
;

enumDeclaration
Expand Down Expand Up @@ -761,21 +766,23 @@ tablePropertyList
tableProperty
: KEY '=' '{' keyElementList '}' { auto v = new IR::Key(@4, $4);
auto id = IR::ID(
@1, IR::TableProperties::keyPropertyName);
@1, IR::TableProperties::keyPropertyName,
IR::TableProperties::keyPropertyName);
$$ = new IR::TableProperty(
@1 + @5, id, IR::Annotations::empty, v, false); }
| ACTIONS '=' '{' actionList '}' { auto v = new IR::ActionList(@4, $4);
auto id = IR::ID(
@1, IR::TableProperties::actionsPropertyName);
@1, IR::TableProperties::actionsPropertyName,
IR::TableProperties::actionsPropertyName);
$$ = new IR::TableProperty(
@1 + @5, id, IR::Annotations::empty, v, false); }
| optAnnotations CONST IDENTIFIER '=' initializer ';'
{ auto v = new IR::ExpressionValue(@5, $5);
auto id = IR::ID(@3, $3);
auto id = IR::ID(@3, $3, $3);
$$ = new IR::TableProperty(@3, id, $1, v, true); }
| optAnnotations IDENTIFIER '=' initializer ';'
{ auto v = new IR::ExpressionValue(@4, $4);
auto id = IR::ID(@2, $2);
auto id = IR::ID(@2, $2, $2);
$$ = new IR::TableProperty(@2, id, $1, v, false); }
;

Expand Down Expand Up @@ -870,7 +877,7 @@ expressionList

member
: name
| TYPE { $$ = new IR::ID(@1, $1); }
| TYPE { $$ = new IR::ID(@1, $1, $1); }
;

lvalue
Expand Down
Loading