Skip to content

Commit

Permalink
Run typechecking after front and mid end. (#4834)
Browse files Browse the repository at this point in the history
Signed-off-by: fruffy <[email protected]>
  • Loading branch information
fruffy authored Oct 31, 2024
1 parent e795b84 commit 8ab8b7d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
13 changes: 8 additions & 5 deletions backends/p4tools/common/compiler/compiler_target.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include "frontends/common/parseInput.h"
#include "frontends/common/parser_options.h"
#include "frontends/p4/frontend.h"
#include "lib/compile_context.h"
#include "lib/error.h"

namespace P4::P4Tools {
Expand All @@ -27,7 +26,7 @@ CompilerResultOrError CompilerTarget::runCompiler(const CompilerOptions &options
CompilerResultOrError CompilerTarget::runCompiler(const CompilerOptions &options,
std::string_view toolName,
const std::string &source) {
const auto *program = P4::parseP4String(source, options.langVersion);
const auto *program = parseP4String(source, options.langVersion);
if (program == nullptr) {
return std::nullopt;
}
Expand Down Expand Up @@ -57,7 +56,7 @@ CompilerResultOrError CompilerTarget::runCompilerImpl(const CompilerOptions &opt
}

const IR::P4Program *CompilerTarget::runParser(const ParserOptions &options) {
const auto *program = P4::parseP4File(options);
const auto *program = parseP4File(options);
if (errorCount() > 0) {
return nullptr;
}
Expand All @@ -66,12 +65,16 @@ const IR::P4Program *CompilerTarget::runParser(const ParserOptions &options) {

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

auto frontEnd = mkFrontEnd();
frontEnd.addDebugHook(options.getDebugHook());
program = frontEnd.run(options, program);
ReferenceMap refMap;
TypeMap typeMap;
// Perform a last round of type checking.
program = program->apply(TypeChecking(&refMap, &typeMap, true));
if ((program == nullptr) || errorCount() > 0) {
return nullptr;
}
Expand Down
12 changes: 10 additions & 2 deletions backends/p4tools/common/compiler/midend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,15 +156,23 @@ void MidEnd::addDefaultPasses() {
new P4::EliminateTuples(&typeMap),
new P4::ConstantFolding(&typeMap),
new P4::SimplifyControlFlow(&typeMap),
// Perform a last round of type-checking before passes which do not type-check begin.
new P4::TypeChecking(&refMap, &typeMap, true),
});
addNonTypeCheckingPasses();
}

void MidEnd::addNonTypeCheckingPasses() {
addPasses({
// Simplify header stack assignments with runtime indices into conditional statements.
new P4::HSIndexSimplifier(&typeMap),
// Convert Type_Varbits into a type that contains information about the assigned width.
// Convert Type_Varbits into a type that contains information about the assigned
// width.
new ConvertVarbits(),
// Convert any StructExpressions with Type_Header into a HeaderExpression.
new ConvertStructExpr(&typeMap),
// Cast all boolean table keys with a bit<1>.
new P4::CastBooleanTableKeys(),
});
}

} // namespace P4::P4Tools
6 changes: 5 additions & 1 deletion backends/p4tools/common/compiler/midend.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@ class MidEnd : public PassManager {

/// Add the list of default passes to the mid end. This is not part of the initializer because
/// some targets may add their own passes to the beginning of the pass list.
void addDefaultPasses();
virtual void addDefaultPasses();

/// Add passes that break type checking. These passes may involve IR modifications the front
/// end type checker does not recognize.
virtual void addNonTypeCheckingPasses();
};

} // namespace P4::P4Tools
Expand Down

0 comments on commit 8ab8b7d

Please sign in to comment.