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

Make side-effect order explicit #86

Merged
merged 3 commits into from
Sep 24, 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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
12 changes: 6 additions & 6 deletions backends/bmv2/analyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,11 @@ namespace {
// This visitor "converts" IR::Node* into EdgeSets
// Since we cannot return EdgeSets, we place them in the 'after' map.
class CFGBuilder : public Inspector {
CFG* cfg;
const CFG::EdgeSet* current; // predecessors of current node
CFG* cfg;
const CFG::EdgeSet* current; // predecessors of current node
std::map<const IR::Statement*, const CFG::EdgeSet*> after; // successors of each statement
const P4::ReferenceMap* refMap;
const P4::TypeMap* typeMap;
P4::ReferenceMap* refMap;
P4::TypeMap* typeMap;

void setAfter(const IR::Statement* statement, const CFG::EdgeSet* value) {
LOG1("After " << statement << " " << value);
Expand Down Expand Up @@ -216,7 +216,7 @@ class CFGBuilder : public Inspector {
}

public:
CFGBuilder(CFG* cfg, const P4::ReferenceMap* refMap, const P4::TypeMap* typeMap) :
CFGBuilder(CFG* cfg, P4::ReferenceMap* refMap, P4::TypeMap* typeMap) :
cfg(cfg), current(nullptr), refMap(refMap), typeMap(typeMap) {}
const CFG::EdgeSet* run(const IR::Statement* startNode, const CFG::EdgeSet* predecessors) {
CHECK_NULL(startNode); CHECK_NULL(predecessors);
Expand All @@ -228,7 +228,7 @@ class CFGBuilder : public Inspector {
} // end anonymous namespace

void CFG::build(const IR::P4Control* cc,
const P4::ReferenceMap* refMap, const P4::TypeMap* typeMap) {
P4::ReferenceMap* refMap, P4::TypeMap* typeMap) {
container = cc;
entryPoint = makeNode(cc->name + ".entry");
exitPoint = makeNode(cc->name + ".exit");
Expand Down
2 changes: 1 addition & 1 deletion backends/bmv2/analyzer.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ class CFG : public IHasDbPrint {
return result;
}
void build(const IR::P4Control* cc,
const P4::ReferenceMap* refMap, const P4::TypeMap* typeMap);
P4::ReferenceMap* refMap, P4::TypeMap* typeMap);
void setEntry(Node* entry) {
BUG_CHECK(entryPoint == nullptr, "Entry already set");
entryPoint = entry;
Expand Down
2 changes: 2 additions & 0 deletions backends/bmv2/bmv2stf.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,8 @@ def do_cli_command(self, cmd):
self.cli_stdin.flush()
self.packetDelay = 1
def do_command(self, cmd):
if self.options.verbose:
print("STF Command:", cmd)
first, cmd = nextWord(cmd)
if first == "":
pass
Expand Down
120 changes: 99 additions & 21 deletions backends/bmv2/jsonconverter.cpp

Large diffs are not rendered by default.

21 changes: 7 additions & 14 deletions backends/bmv2/midend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ limitations under the License.
#include "lower.h"
#include "inlining.h"
#include "midend/actionsInlining.h"
#include "midend/uniqueNames.h"
#include "midend/moveDeclarations.h"
#include "midend/removeReturns.h"
#include "midend/moveConstructors.h"
#include "midend/localizeActions.h"
Expand All @@ -29,10 +27,9 @@ limitations under the License.
#include "midend/removeLeftSlices.h"
#include "midend/convertEnums.h"
#include "midend/simplifyKey.h"
#include "midend/simplifyExpressions.h"
#include "midend/simplifyParsers.h"
#include "midend/resetHeaders.h"
#include "midend/simplifySelect.h"
#include "frontends/p4/uniqueNames.h"
#include "frontends/p4/simplifyParsers.h"
#include "frontends/p4/strengthReduction.h"
#include "frontends/p4/typeMap.h"
#include "frontends/p4/evaluator/evaluator.h"
Expand All @@ -41,6 +38,7 @@ limitations under the License.
#include "frontends/p4/toP4/toP4.h"
#include "frontends/p4/simplify.h"
#include "frontends/p4/unusedDeclarations.h"
#include "frontends/p4/moveDeclarations.h"
#include "frontends/common/constantFolding.h"
#include "frontends/p4/fromv1.0/v1model.h"

Expand Down Expand Up @@ -84,14 +82,8 @@ 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::SimplifyParsers(&refMap),
new P4::ConvertEnums(&refMap, &typeMap,
new EnumOn32Bits()),
new P4::ResetHeaders(&refMap, &typeMap),
new P4::UniqueNames(&refMap),
new P4::MoveDeclarations(),
new P4::MoveInitializers(),
new P4::SimplifyExpressions(&refMap, &typeMap),
new P4::RemoveReturns(&refMap),
new P4::MoveConstructors(&refMap),
new P4::RemoveAllUnusedDeclarations(&refMap),
Expand All @@ -106,16 +98,17 @@ void MidEnd::setup_for_P4_16(CompilerOptions&) {
new P4::Inline(&refMap, &typeMap, evaluator),
new P4::InlineActions(&refMap, &typeMap),
new P4::LocalizeAllActions(&refMap),
new P4::UniqueNames(&refMap),
new P4::UniqueParameters(&refMap),
new P4::ClearTypeMap(&typeMap),
new P4::SimplifyControlFlow(&refMap, &typeMap),
new P4::RemoveParameters(&refMap, &typeMap),
new P4::ClearTypeMap(&typeMap),
new P4::RemoveTableParameters(&refMap, &typeMap),
new P4::RemoveActionParameters(&refMap, &typeMap),
new P4::SimplifyKey(&refMap, &typeMap,
new P4::NonLeftValue(&refMap, &typeMap)),
new P4::ConstantFolding(&refMap, &typeMap),
new P4::StrengthReduction(),
new P4::SimplifySelect(&refMap, &typeMap, true), // constant keysets
new P4::SimplifySelect(&refMap, &typeMap, true), // require constant keysets
new P4::SimplifyParsers(&refMap),
new P4::LocalCopyPropagation(&refMap, &typeMap),
new P4::MoveDeclarations(),
Expand Down
4 changes: 2 additions & 2 deletions backends/ebpf/ebpfBackend.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2013-present Barefoot Networks, Inc.
Copyright 2013-present Barefoot Networks, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -25,7 +25,7 @@ limitations under the License.
namespace EBPF {

void run_ebpf_backend(const EbpfOptions& options, const IR::ToplevelBlock* toplevel,
P4::ReferenceMap* refMap, const P4::TypeMap* typeMap) {
P4::ReferenceMap* refMap, P4::TypeMap* typeMap) {
if (toplevel == nullptr)
return;

Expand Down
4 changes: 2 additions & 2 deletions backends/ebpf/ebpfBackend.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2013-present Barefoot Networks, Inc.
Copyright 2013-present Barefoot Networks, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -25,7 +25,7 @@ limitations under the License.
namespace EBPF {

void run_ebpf_backend(const EbpfOptions& options, const IR::ToplevelBlock* toplevel,
P4::ReferenceMap* refMap, const P4::TypeMap* typeMap);
P4::ReferenceMap* refMap, P4::TypeMap* typeMap);

} // namespace EBPF

Expand Down
4 changes: 2 additions & 2 deletions backends/ebpf/ebpfControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ void ControlBodyTranslationVisitor::processApply(const P4::ApplyMethod* method)
BUG_CHECK(table != nullptr, "No table for %1%", method->expr);

P4::ParameterSubstitution binding;
binding.populate(method->getParameters(), method->expr->arguments);
binding.populate(method->getActualParameters(), method->expr->arguments);
cstring actionVariableName = saveAction.at(saveAction.size() - 1);
if (!actionVariableName.isNullOrEmpty()) {
builder->appendFormat("%s %s;\n", table->actionEnumName, actionVariableName);
Expand All @@ -118,7 +118,7 @@ void ControlBodyTranslationVisitor::processApply(const P4::ApplyMethod* method)
if (!binding.empty()) {
builder->emitIndent();
builder->appendLine("/* bind parameters */");
for (auto p : *method->getParameters()->getEnumerator()) {
for (auto p : *method->getActualParameters()->getEnumerator()) {
toDereference.emplace(p);
auto etype = EBPFTypeFactory::instance->create(p->type);
builder->emitIndent();
Expand Down
4 changes: 2 additions & 2 deletions backends/ebpf/ebpfObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class EBPFProgram : public EBPFObject {
const IR::P4Program* program;
const IR::ToplevelBlock* toplevel;
P4::ReferenceMap* refMap;
const P4::TypeMap* typeMap;
P4::TypeMap* typeMap;
EBPFParser* parser;
EBPFControl* control;
EBPFModel &model;
Expand All @@ -70,7 +70,7 @@ class EBPFProgram : public EBPFObject {
bool build(); // return 'true' on success

EBPFProgram(const IR::P4Program* program, P4::ReferenceMap* refMap,
const P4::TypeMap* typeMap, const IR::ToplevelBlock* toplevel) :
P4::TypeMap* typeMap, const IR::ToplevelBlock* toplevel) :
program(program), toplevel(toplevel),
refMap(refMap), typeMap(typeMap),
parser(nullptr), control(nullptr), model(EBPFModel::instance) {
Expand Down
19 changes: 6 additions & 13 deletions backends/ebpf/midend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,16 @@ limitations under the License.
#include "midend.h"
#include "midend/actionsInlining.h"
#include "midend/inlining.h"
#include "midend/moveDeclarations.h"
#include "midend/uniqueNames.h"
#include "midend/removeReturns.h"
#include "midend/moveConstructors.h"
#include "midend/actionSynthesis.h"
#include "midend/localizeActions.h"
#include "midend/removeParameters.h"
#include "midend/local_copyprop.h"
#include "midend/simplifyExpressions.h"
#include "midend/simplifyParsers.h"
#include "midend/resetHeaders.h"
#include "midend/simplifyKey.h"
#include "midend/simplifySelect.h"
#include "frontends/p4/uniqueNames.h"
#include "frontends/p4/moveDeclarations.h"
#include "frontends/p4/typeMap.h"
#include "frontends/p4/evaluator/evaluator.h"
#include "frontends/p4/typeChecking/typeChecker.h"
Expand All @@ -39,6 +36,7 @@ limitations under the License.
#include "frontends/p4/unusedDeclarations.h"
#include "frontends/common/constantFolding.h"
#include "frontends/p4/strengthReduction.h"
#include "frontends/p4/simplifyParsers.h"

namespace EBPF {

Expand All @@ -51,12 +49,6 @@ const IR::ToplevelBlock* MidEnd::run(EbpfOptions& options, const IR::P4Program*
auto evaluator = new P4::EvaluatorPass(&refMap, &typeMap);

PassManager simplify = {
new P4::SimplifyParsers(&refMap),
new P4::ResetHeaders(&refMap, &typeMap),
new P4::UniqueNames(&refMap),
new P4::MoveDeclarations(),
new P4::MoveInitializers(),
new P4::SimplifyExpressions(&refMap, &typeMap),
new P4::RemoveReturns(&refMap),
new P4::MoveConstructors(&refMap),
new P4::RemoveAllUnusedDeclarations(&refMap),
Expand All @@ -81,11 +73,12 @@ const IR::ToplevelBlock* MidEnd::run(EbpfOptions& options, const IR::P4Program*
new P4::Inline(&refMap, &typeMap, evaluator),
new P4::InlineActions(&refMap, &typeMap),
new P4::LocalizeAllActions(&refMap),
new P4::UniqueNames(&refMap),
new P4::UniqueParameters(&refMap),
new P4::ClearTypeMap(&typeMap),
new P4::SimplifyControlFlow(&refMap, &typeMap),
new P4::RemoveParameters(&refMap, &typeMap),
new P4::ClearTypeMap(&typeMap),
new P4::RemoveTableParameters(&refMap, &typeMap),
new P4::RemoveActionParameters(&refMap, &typeMap),
new P4::SimplifyKey(&refMap, &typeMap,
new P4::NonLeftValue(&refMap, &typeMap)),
new P4::RemoveExits(&refMap, &typeMap),
Expand Down
23 changes: 8 additions & 15 deletions backends/p4test/midend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ limitations under the License.
#include "midend.h"
#include "midend/actionsInlining.h"
#include "midend/inlining.h"
#include "midend/moveDeclarations.h"
#include "midend/uniqueNames.h"
#include "midend/removeReturns.h"
#include "midend/removeParameters.h"
#include "midend/moveConstructors.h"
Expand All @@ -28,19 +26,18 @@ limitations under the License.
#include "midend/simplifyKey.h"
#include "midend/parserUnroll.h"
#include "midend/specialize.h"
#include "midend/simplifyExpressions.h"
#include "midend/simplifyParsers.h"
#include "midend/resetHeaders.h"
#include "midend/simplifySelect.h"
#include "midend/simplifyDefUse.h"
#include "frontends/p4/simplifyParsers.h"
#include "frontends/p4/typeMap.h"
#include "frontends/p4/evaluator/evaluator.h"
#include "frontends/common/resolveReferences/resolveReferences.h"
#include "frontends/p4/toP4/toP4.h"
#include "frontends/p4/simplify.h"
#include "frontends/p4/unusedDeclarations.h"
#include "frontends/p4/moveDeclarations.h"
#include "frontends/common/constantFolding.h"
#include "frontends/p4/strengthReduction.h"
#include "frontends/p4/uniqueNames.h"

namespace P4Test {

Expand All @@ -55,14 +52,9 @@ MidEnd::MidEnd(CompilerOptions& options) {
// 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::SimplifyParsers(&refMap),
new P4::ResetHeaders(&refMap, &typeMap),
new P4::UniqueNames(&refMap), // Give each local declaration a unique internal name
new P4::MoveDeclarations(), // Move all local declarations to the beginning
new P4::MoveInitializers(),
new P4::SimplifyDefUse(&refMap, &typeMap),
new P4::SimplifyExpressions(&refMap, &typeMap),
new P4::RemoveReturns(&refMap),
new P4::MoveConstructors(&refMap),
new P4::RemoveAllUnusedDeclarations(&refMap),
Expand All @@ -80,11 +72,12 @@ MidEnd::MidEnd(CompilerOptions& options) {
// Parser loop unrolling: TODO
// new P4::ParsersUnroll(true, &refMap, &typeMap),
new P4::LocalizeAllActions(&refMap),
new P4::UniqueNames(&refMap),
new P4::UniqueParameters(&refMap),
new P4::ClearTypeMap(&typeMap), // table types have changed
new P4::SimplifyControlFlow(&refMap, &typeMap),
new P4::RemoveParameters(&refMap, &typeMap),
new P4::ClearTypeMap(&typeMap), // table types have changed
new P4::RemoveTableParameters(&refMap, &typeMap),
new P4::RemoveActionParameters(&refMap, &typeMap),
new P4::SimplifyKey(&refMap, &typeMap,
new P4::NonLeftValue(&refMap, &typeMap)),
new P4::RemoveExits(&refMap, &typeMap),
Expand Down
30 changes: 13 additions & 17 deletions backends/p4test/run-p4-sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ def check_generated_files(options, tmpdir, expecteddir):
return SUCCESS

def file_name(tmpfolder, base, suffix, ext):
return tmpfolder + "/" + base + suffix + ext
return tmpfolder + "/" + base + "-" + suffix + ext

def process_file(options, argv):
assert isinstance(options, Options)
Expand All @@ -179,10 +179,15 @@ def process_file(options, argv):
if not os.path.exists(expected_dirname):
os.makedirs(expected_dirname)

# We rely on the fact that these keys are in alphabetical order.
rename = { "FrontEnd_11_SimplifyControlFlow": "first",
"FrontEnd_20_SimplifyDefUse": "frontend",
"MidEnd_27_Evaluator": "midend" }

if options.verbose:
print("Writing temporary files into ", tmpdir)
ppfile = tmpdir + "/" + basename # after parsing
referenceOutputs = "FrontEnd_12_Simplify,FrontEnd_13_Remove,MidEnd_32_Evaluator"
referenceOutputs = ",".join(rename.keys())
stderr = tmpdir + "/" + basename + "-stderr"

if not os.path.isfile(options.p4filename):
Expand All @@ -207,21 +212,12 @@ def process_file(options, argv):

# Canonicalize the generated file names
lastFile = None
firstFile = file_name(tmpdir, base, "-FrontEnd_12_SimplifyControlFlow", ext)
if os.path.isfile(firstFile):
newName = file_name(tmpdir, base, "-first", ext)
os.rename(firstFile, newName)
lastFile = newName
midFile = file_name(tmpdir, base, "-FrontEnd_13_RemoveAllUnusedDeclarations", ext)
if os.path.isfile(midFile):
newName = file_name(tmpdir, base, "-frontend", ext)
os.rename(midFile, newName)
lastFile = newName
endFile = file_name(tmpdir, base, "-MidEnd_32_Evaluator", ext)
if os.path.isfile(endFile):
newName = file_name(tmpdir, base, "-midend", ext)
os.rename(endFile, newName)
lastFile = newName
for k in sorted(rename.keys()):
file = file_name(tmpdir, base, k, ext)
if os.path.isfile(file):
newName = file_name(tmpdir, base, rename[k], ext)
os.rename(file, newName)
lastFile = newName

if (result == SUCCESS):
result = check_generated_files(options, tmpdir, expected_dirname);
Expand Down
16 changes: 13 additions & 3 deletions frontends/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ p4_frontend_SOURCES = \
frontends/p4/p4-parse.ypp \
frontends/p4/frontend.h \
frontends/p4/frontend.cpp \
frontends/p4/checkAliasing.h \
frontends/p4/checkAliasing.cpp \
frontends/p4/createBuiltins.h \
frontends/p4/createBuiltins.cpp \
frontends/p4/def_use.h \
Expand Down Expand Up @@ -71,7 +69,19 @@ p4_frontend_SOURCES = \
frontends/p4/fromv1.0/converters.h \
frontends/p4/fromv1.0/converters.cpp \
frontends/p4/typeMap.h \
frontends/p4/typeMap.cpp
frontends/p4/typeMap.cpp \
frontends/p4/simplifyDefUse.cpp \
frontends/p4/simplifyDefUse.h \
frontends/p4/sideEffects.cpp \
frontends/p4/sideEffects.h \
frontends/p4/simplifyParsers.cpp \
frontends/p4/simplifyParsers.h \
frontends/p4/uniqueNames.cpp \
frontends/p4/uniqueNames.h \
frontends/p4/resetHeaders.cpp \
frontends/p4/resetHeaders.h \
frontends/p4/moveDeclarations.cpp \
frontends/p4/moveDeclarations.h

common_frontend_SOURCES = \
frontends/common/options.h \
Expand Down
Loading