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

Pass to get rid of action_run() calls #5053

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion backends/bmv2/pna_nic/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ int main(int argc, char *const argv[]) {

if (program == nullptr || ::P4::errorCount() > 0) return 1;
try {
P4::P4COptionPragmaParser optionsPragmaParser;
P4::P4COptionPragmaParser optionsPragmaParser(true);
program->apply(P4::ApplyOptionsPragmas(optionsPragmaParser));

P4::FrontEnd frontend;
Expand Down
2 changes: 1 addition & 1 deletion backends/bmv2/psa_switch/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ int main(int argc, char *const argv[]) {

if (program == nullptr || ::P4::errorCount() > 0) return 1;
try {
P4::P4COptionPragmaParser optionsPragmaParser;
P4::P4COptionPragmaParser optionsPragmaParser(true);
program->apply(P4::ApplyOptionsPragmas(optionsPragmaParser));

P4::FrontEnd frontend;
Expand Down
2 changes: 1 addition & 1 deletion backends/bmv2/simple_switch/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ int main(int argc, char *const argv[]) {

if (program == nullptr || ::P4::errorCount() > 0) return 1;
try {
P4::P4COptionPragmaParser optionsPragmaParser;
P4::P4COptionPragmaParser optionsPragmaParser(true);
program->apply(P4::ApplyOptionsPragmas(optionsPragmaParser));

P4::FrontEnd frontend;
Expand Down
2 changes: 1 addition & 1 deletion backends/dpdk/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ int main(int argc, char *const argv[]) {

if (program == nullptr || ::P4::errorCount() > 0) return 1;
try {
P4::P4COptionPragmaParser optionsPragmaParser;
P4::P4COptionPragmaParser optionsPragmaParser(true);
program->apply(P4::ApplyOptionsPragmas(optionsPragmaParser));

P4::FrontEnd frontend;
Expand Down
2 changes: 1 addition & 1 deletion backends/ebpf/p4c-ebpf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ void compile(EbpfOptions &options) {
program = P4::parseP4File(options);
if (::P4::errorCount() > 0) return;

P4::P4COptionPragmaParser optionsPragmaParser;
P4::P4COptionPragmaParser optionsPragmaParser(true);
program->apply(P4::ApplyOptionsPragmas(optionsPragmaParser));

P4::FrontEnd frontend;
Expand Down
2 changes: 1 addition & 1 deletion backends/graphs/p4c-graphs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ int main(int argc, char *const argv[]) {
if (program == nullptr || ::P4::errorCount() > 0) return 1;

try {
P4::P4COptionPragmaParser optionsPragmaParser;
P4::P4COptionPragmaParser optionsPragmaParser(false);
program->apply(P4::ApplyOptionsPragmas(optionsPragmaParser));

P4::FrontEnd fe;
Expand Down
1 change: 1 addition & 0 deletions backends/p4test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ set (P4TEST_SRCS
midend.cpp
)
set (P4TEST_HDRS
p4test.h
midend.h
)

Expand Down
6 changes: 4 additions & 2 deletions backends/p4test/midend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ limitations under the License.
#include "midend/complexComparison.h"
#include "midend/copyStructures.h"
#include "midend/def_use.h"
#include "midend/eliminateActionRun.h"
#include "midend/eliminateInvalidHeaders.h"
#include "midend/eliminateNewtype.h"
#include "midend/eliminateSerEnums.h"
Expand Down Expand Up @@ -76,7 +77,7 @@ class SkipControls : public P4::ActionSynthesisPolicy {
}
};

MidEnd::MidEnd(CompilerOptions &options, std::ostream *outStream) {
MidEnd::MidEnd(P4TestOptions &options, std::ostream *outStream) {
bool isv1 = options.langVersion == CompilerOptions::FrontendVersion::P4_14;
refMap.setIsV1(isv1);
auto evaluator = new P4::EvaluatorPass(&refMap, &typeMap);
Expand Down Expand Up @@ -125,7 +126,8 @@ MidEnd::MidEnd(CompilerOptions &options, std::ostream *outStream) {
new P4::SimplifyControlFlow(&typeMap),
new P4::CompileTimeOperations(),
new P4::TableHit(&typeMap),
new P4::EliminateSwitch(&typeMap),
!options.preferSwitch ? new P4::EliminateSwitch(&typeMap) : nullptr,
options.preferSwitch ? new P4::ElimActionRun() : nullptr,
new P4::ResolveReferences(&refMap),
new P4::TypeChecking(&refMap, &typeMap, true), // update types before ComputeDefUse
new PassRepeated({
Expand Down
3 changes: 2 additions & 1 deletion backends/p4test/midend.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ limitations under the License.
#include "frontends/common/options.h"
#include "frontends/p4/evaluator/evaluator.h"
#include "ir/ir.h"
#include "p4test.h"

namespace P4::P4Test {

Expand All @@ -32,7 +33,7 @@ class MidEnd : public PassManager {
IR::ToplevelBlock *toplevel = nullptr;

void addDebugHook(DebugHook hook) { hooks.push_back(hook); }
explicit MidEnd(CompilerOptions &options, std::ostream *outStream = nullptr);
explicit MidEnd(P4TestOptions &options, std::ostream *outStream = nullptr);
IR::ToplevelBlock *process(const IR::P4Program *&program) {
addDebugHooks(hooks, true);
program = program->apply(*this);
Expand Down
105 changes: 53 additions & 52 deletions backends/p4test/p4test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

#include "p4test.h"

#include <fstream> // IWYU pragma: keep
#include <iostream>

Expand All @@ -35,57 +37,56 @@ limitations under the License.
#include "lib/nullstream.h"
#include "midend.h"

using namespace P4;

class P4TestOptions : public CompilerOptions {
public:
bool parseOnly = false;
bool validateOnly = false;
bool loadIRFromJson = false;
P4TestOptions() {
registerOption(
"--listMidendPasses", nullptr,
[this](const char *) {
listMidendPasses = true;
loadIRFromJson = false;
P4Test::MidEnd MidEnd(*this, outStream);
exit(0);
return false;
},
"[p4test] Lists exact name of all midend passes.\n");
registerOption(
"--parse-only", nullptr,
[this](const char *) {
parseOnly = true;
return true;
},
"only parse the P4 input, without any further processing");
registerOption(
"--validate", nullptr,
[this](const char *) {
validateOnly = true;
return true;
},
"Validate the P4 input, running just the front-end");
registerOption(
"--fromJSON", "file",
[this](const char *arg) {
loadIRFromJson = true;
file = arg;
return true;
},
"read previously dumped json instead of P4 source code");
registerOption(
"--turn-off-logn", nullptr,
[](const char *) {
::P4::Log::Detail::enableLoggingGlobally = false;
return true;
},
"Turn off LOGN() statements in the compiler.\n"
"Use '@__debug' annotation to enable LOGN on "
"the annotated P4 object within the source code.\n");
}
};
P4TestOptions::P4TestOptions() {
registerOption(
"--listMidendPasses", nullptr,
[this](const char *) {
listMidendPasses = true;
loadIRFromJson = false;
P4Test::MidEnd MidEnd(*this, outStream);
exit(0);
return false;
},
"[p4test] Lists exact name of all midend passes.\n");
registerOption(
"--parse-only", nullptr,
[this](const char *) {
parseOnly = true;
return true;
},
"only parse the P4 input, without any further processing");
registerOption(
"--validate", nullptr,
[this](const char *) {
validateOnly = true;
return true;
},
"Validate the P4 input, running just the front-end");
registerOption(
"--fromJSON", "file",
[this](const char *arg) {
loadIRFromJson = true;
file = arg;
return true;
},
"read previously dumped json instead of P4 source code");
registerOption(
"--turn-off-logn", nullptr,
[](const char *) {
::P4::Log::Detail::enableLoggingGlobally = false;
return true;
},
"Turn off LOGN() statements in the compiler.\n"
"Use '@__debug' annotation to enable LOGN on "
"the annotated P4 object within the source code.\n");
registerOption(
"--preferSwitch", nullptr,
[this](const char *) {
preferSwitch = true;
return true;
},
"use passes that convert to switch instead eliminating them");
}

using P4TestContext = P4CContextWithOptions<P4TestOptions>;

Expand Down Expand Up @@ -135,7 +136,7 @@ int main(int argc, char *const argv[]) {
info.emitInfo("PARSER");

if (program != nullptr && ::P4::errorCount() == 0) {
P4::P4COptionPragmaParser optionsPragmaParser;
P4::P4COptionPragmaParser optionsPragmaParser(true);
program->apply(P4::ApplyOptionsPragmas(optionsPragmaParser));
info.emitInfo("PASS P4COptionPragmaParser");

Expand Down
33 changes: 33 additions & 0 deletions backends/p4test/p4test.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
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.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#ifndef BACKENDS_P4TEST_P4TEST_H_
#define BACKENDS_P4TEST_P4TEST_H_

#include "frontends/common/options.h"

using namespace P4;

class P4TestOptions : public CompilerOptions {
public:
bool parseOnly = false;
bool validateOnly = false;
bool loadIRFromJson = false;
bool preferSwitch = false;
P4TestOptions();
};

#endif /* BACKENDS_P4TEST_P4TEST_H_ */
2 changes: 1 addition & 1 deletion backends/p4tools/common/compiler/compiler_target.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ const IR::P4Program *CompilerTarget::runParser(const ParserOptions &options) {

const IR::P4Program *CompilerTarget::runFrontend(const CompilerOptions &options,
const IR::P4Program *program) const {
P4COptionPragmaParser optionsPragmaParser;
P4COptionPragmaParser optionsPragmaParser(false);
program->apply(ApplyOptionsPragmas(optionsPragmaParser));

auto frontEnd = mkFrontEnd();
Expand Down
2 changes: 1 addition & 1 deletion backends/tc/tc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ int main(int argc, char *const argv[]) {
return 1;
}
try {
P4::P4COptionPragmaParser optionsPragmaParser;
P4::P4COptionPragmaParser optionsPragmaParser(true);
program->apply(P4::ApplyOptionsPragmas(optionsPragmaParser));
P4::FrontEnd frontend;
frontend.addDebugHook(hook);
Expand Down
3 changes: 3 additions & 0 deletions backends/tofino/bf-p4c/bf-p4c-options.h
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,9 @@ class BFNOptionPragmaParser : public P4::P4COptionPragmaParser {
public:
std::optional<CommandLineOptions> tryToParse(const IR::Annotation *annotation) override;

BFNOptionPragmaParser() : P4::P4COptionPragmaParser(false) {}
// FIXME -- should remove the tofino @command_line stuff and use the base class

private:
std::optional<CommandLineOptions> parseCompilerOption(const IR::Annotation *annotation);
};
Expand Down
16 changes: 16 additions & 0 deletions frontends/common/applyOptionsPragmas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,22 @@ std::optional<IOptionPragmaParser::CommandLineOptions> P4COptionPragmaParser::tr
const IR::Annotation *annotation) {
auto pragmaName = annotation->name.name;
if (pragmaName == "diagnostic") return parseDiagnostic(annotation);
if (supportCommandLinePragma && pragmaName == "command_line") {
IOptionPragmaParser::CommandLineOptions options;
auto *args = annotation->needsParsing()
? P4ParserDriver::parseExpressionList(annotation->srcInfo,
annotation->getUnparsed())
: &annotation->getExpr();
for (auto *arg : *args) {
if (auto *a = arg->to<IR::StringLiteral>()) {
options.push_back(a->value.c_str());
} else {
// can this happen? annotation parser should require only strings
warning("ignoring non-string %1% in @command_line", a);
}
}
return options;
}
return std::nullopt;
}

Expand Down
4 changes: 4 additions & 0 deletions frontends/common/applyOptionsPragmas.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,13 @@ class ApplyOptionsPragmas : public Inspector {
* they're unable to parse a pragma.
*/
class P4COptionPragmaParser : public IOptionPragmaParser {
bool supportCommandLinePragma;

public:
std::optional<CommandLineOptions> tryToParse(const IR::Annotation *annotation) override;

explicit P4COptionPragmaParser(bool sCLP) : supportCommandLinePragma(sCLP) {}

private:
std::optional<CommandLineOptions> parseDiagnostic(const IR::Annotation *annotation);
};
Expand Down
3 changes: 3 additions & 0 deletions frontends/p4/parseAnnotations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ ParseAnnotations::HandlerMap ParseAnnotations::standardHandlers() {

// @match has an expression argument
PARSE(IR::Annotation::matchAnnotation, Expression),

// @command_line to add to the command line
PARSE_STRING_LITERAL_LIST("command_line"_cs),
};
}

Expand Down
2 changes: 2 additions & 0 deletions midend/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ set (MIDEND_SRCS
copyStructures.cpp
coverage.cpp
def_use.cpp
eliminateActionRun.cpp
eliminateInvalidHeaders.cpp
eliminateNewtype.cpp
eliminateSerEnums.cpp
Expand Down Expand Up @@ -74,6 +75,7 @@ set (MIDEND_HDRS
copyStructures.h
coverage.h
def_use.h
eliminateActionRun.h
eliminateInvalidHeaders.h
eliminateNewtype.h
eliminateSerEnums.h
Expand Down
Loading
Loading