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 type checking true read-only visitor #4829

Merged
merged 18 commits into from
Sep 4, 2024
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
2 changes: 2 additions & 0 deletions frontends/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ set (P4_FRONTEND_SRCS
p4/typeChecking/typeSubstitution.cpp
p4/typeChecking/typeSubstitutionVisitor.cpp
p4/typeChecking/typeUnification.cpp
p4/typeChecking/typeInference.cpp
p4/typeChecking/readOnlyTypeInference.cpp
p4/typeMap.cpp
p4/uniqueNames.cpp
p4/unusedDeclarations.cpp
Expand Down
2 changes: 1 addition & 1 deletion frontends/p4/methodInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ MethodInstance *MethodInstance::resolve(const IR::MethodCallExpression *mce,
auto t = TypeInference::specialize(originalType, mce->typeArguments, ctxt);
CHECK_NULL(t);
actualType = t->to<IR::Type_MethodBase>();
TypeInference tc(typeMap, /* readOnly */ true);
ReadOnlyTypeInference tc(typeMap);
(void)actualType->apply(tc, ctxt); // may need to learn new type components
CHECK_NULL(actualType);
}
Expand Down
2 changes: 1 addition & 1 deletion frontends/p4/staticAssert.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class DoStaticAssert : public Transform, public ResolutionContext {
class StaticAssert : public PassManager {
public:
explicit StaticAssert(TypeMap *typeMap) {
passes.push_back(new TypeInference(typeMap));
passes.push_back(new ReadOnlyTypeInference(typeMap));
passes.push_back(new DoStaticAssert(typeMap));
setName("StaticAssert");
}
Expand Down
5 changes: 3 additions & 2 deletions frontends/p4/typeChecking/constantTypeSubstitution.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ namespace P4 {
class ConstantTypeSubstitution : public Transform, ResolutionContext {
TypeVariableSubstitution *subst;
TypeMap *typeMap;
TypeInference *tc;
TypeInferenceBase *tc;

public:
ConstantTypeSubstitution(TypeVariableSubstitution *subst, TypeMap *typeMap, TypeInference *tc)
ConstantTypeSubstitution(TypeVariableSubstitution *subst, TypeMap *typeMap,
TypeInferenceBase *tc)
: subst(subst), typeMap(typeMap), tc(tc) {
CHECK_NULL(subst);
CHECK_NULL(typeMap);
Expand Down
164 changes: 164 additions & 0 deletions frontends/p4/typeChecking/readOnlyTypeInference.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
/*
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.
*/

#include "typeChecker.h"

namespace P4 {
static void checkConstant(const IR::Node *orig, const IR::Node *transformed) {
BUG_CHECK(orig == transformed,
"At this point in the compilation typechecking should not infer new types anymore, "
"but it did: node %1% changed to %2%",
orig, transformed);
}

#define DEFINE_PREORDER(type) \
bool ReadOnlyTypeInference::preorder(const type *n) { \
auto [res, prune] = TypeInferenceBase::preorder(n); \
checkConstant(n, res); \
return !prune; \
}

DEFINE_PREORDER(IR::Function)
DEFINE_PREORDER(IR::P4Program)
DEFINE_PREORDER(IR::Declaration_Instance)
DEFINE_PREORDER(IR::EntriesList)
DEFINE_PREORDER(IR::Type_SerEnum)

#undef DEFINE_PREORDER
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Duplicated on line 162


#define DEFINE_POSTORDER(type) \
void ReadOnlyTypeInference::postorder(const type *n) { \
const auto *res = TypeInferenceBase::postorder(n); \
checkConstant(n, res); \
}

DEFINE_POSTORDER(IR::Declaration_MatchKind)
DEFINE_POSTORDER(IR::Declaration_Variable)
DEFINE_POSTORDER(IR::Declaration_Constant)
DEFINE_POSTORDER(IR::P4Control)
DEFINE_POSTORDER(IR::P4Parser)
DEFINE_POSTORDER(IR::Method)

DEFINE_POSTORDER(IR::Type_Type)
DEFINE_POSTORDER(IR::Type_Table)
DEFINE_POSTORDER(IR::Type_Error)
DEFINE_POSTORDER(IR::Type_InfInt)
DEFINE_POSTORDER(IR::Type_Method)
DEFINE_POSTORDER(IR::Type_Action)
DEFINE_POSTORDER(IR::Type_Name)
DEFINE_POSTORDER(IR::Type_Base)
DEFINE_POSTORDER(IR::Type_Var)
DEFINE_POSTORDER(IR::Type_Enum)
DEFINE_POSTORDER(IR::Type_Extern)
DEFINE_POSTORDER(IR::StructField)
DEFINE_POSTORDER(IR::Type_Header)
DEFINE_POSTORDER(IR::Type_Stack)
DEFINE_POSTORDER(IR::Type_Struct)
DEFINE_POSTORDER(IR::Type_HeaderUnion)
DEFINE_POSTORDER(IR::Type_Typedef)
DEFINE_POSTORDER(IR::Type_Specialized)
DEFINE_POSTORDER(IR::Type_SpecializedCanonical)
DEFINE_POSTORDER(IR::Type_Tuple)
DEFINE_POSTORDER(IR::Type_P4List)
DEFINE_POSTORDER(IR::Type_List)
DEFINE_POSTORDER(IR::Type_Set)
DEFINE_POSTORDER(IR::Type_ArchBlock)
DEFINE_POSTORDER(IR::Type_Newtype)
DEFINE_POSTORDER(IR::Type_Package)
DEFINE_POSTORDER(IR::Type_ActionEnum)
DEFINE_POSTORDER(IR::P4Table)
DEFINE_POSTORDER(IR::P4Action)
DEFINE_POSTORDER(IR::P4ValueSet)
DEFINE_POSTORDER(IR::Key)
DEFINE_POSTORDER(IR::Entry)

DEFINE_POSTORDER(IR::Dots)
DEFINE_POSTORDER(IR::Argument)
DEFINE_POSTORDER(IR::SerEnumMember)
DEFINE_POSTORDER(IR::Parameter)
DEFINE_POSTORDER(IR::Constant)
DEFINE_POSTORDER(IR::BoolLiteral)
DEFINE_POSTORDER(IR::StringLiteral)
DEFINE_POSTORDER(IR::Operation_Relation)
DEFINE_POSTORDER(IR::Concat)
DEFINE_POSTORDER(IR::ArrayIndex)
DEFINE_POSTORDER(IR::LAnd)
DEFINE_POSTORDER(IR::LOr)
DEFINE_POSTORDER(IR::Add)
DEFINE_POSTORDER(IR::Sub)
DEFINE_POSTORDER(IR::AddSat)
DEFINE_POSTORDER(IR::SubSat)
DEFINE_POSTORDER(IR::Mul)
DEFINE_POSTORDER(IR::Div)
DEFINE_POSTORDER(IR::Mod)
DEFINE_POSTORDER(IR::Shl)
DEFINE_POSTORDER(IR::Shr)
DEFINE_POSTORDER(IR::BXor)
DEFINE_POSTORDER(IR::BAnd)
DEFINE_POSTORDER(IR::BOr)
DEFINE_POSTORDER(IR::Mask)
DEFINE_POSTORDER(IR::Range)
DEFINE_POSTORDER(IR::LNot)
DEFINE_POSTORDER(IR::Neg)
DEFINE_POSTORDER(IR::UPlus)
DEFINE_POSTORDER(IR::Cmpl)
DEFINE_POSTORDER(IR::Cast)
DEFINE_POSTORDER(IR::Mux)
DEFINE_POSTORDER(IR::Slice)
DEFINE_POSTORDER(IR::PathExpression)
DEFINE_POSTORDER(IR::Member)
DEFINE_POSTORDER(IR::TypeNameExpression)
DEFINE_POSTORDER(IR::ListExpression)
DEFINE_POSTORDER(IR::InvalidHeader)
DEFINE_POSTORDER(IR::InvalidHeaderUnion)
DEFINE_POSTORDER(IR::Invalid)
DEFINE_POSTORDER(IR::P4ListExpression)
DEFINE_POSTORDER(IR::StructExpression)
DEFINE_POSTORDER(IR::HeaderStackExpression)
DEFINE_POSTORDER(IR::MethodCallStatement)
DEFINE_POSTORDER(IR::MethodCallExpression)
DEFINE_POSTORDER(IR::ConstructorCallExpression)
DEFINE_POSTORDER(IR::SelectExpression)
DEFINE_POSTORDER(IR::DefaultExpression)
DEFINE_POSTORDER(IR::This)
DEFINE_POSTORDER(IR::AttribLocal)
DEFINE_POSTORDER(IR::ActionList)

DEFINE_POSTORDER(IR::ReturnStatement)
DEFINE_POSTORDER(IR::IfStatement)
DEFINE_POSTORDER(IR::SwitchStatement)
DEFINE_POSTORDER(IR::AssignmentStatement)
DEFINE_POSTORDER(IR::ForInStatement)
DEFINE_POSTORDER(IR::ActionListElement)
DEFINE_POSTORDER(IR::KeyElement)
DEFINE_POSTORDER(IR::Property)
DEFINE_POSTORDER(IR::SelectCase)
DEFINE_POSTORDER(IR::Annotation)

Visitor::profile_t ReadOnlyTypeInference::init_apply(const IR::Node *node) {
auto rv = Inspector::init_apply(node);
TypeInferenceBase::start(node);

return rv;
}

void ReadOnlyTypeInference::end_apply(const IR::Node *node) {
TypeInferenceBase::finish(node);
Inspector::end_apply(node);
}

#undef DEFINE_POSTORDER
#undef DEFINE_PREORDER

} // namespace P4
Loading
Loading