Skip to content

Commit

Permalink
Add TypeSimplifier
Browse files Browse the repository at this point in the history
Add minimizer that would replace references to complex, user-defined
types with simple "int"
  • Loading branch information
sgizler committed Oct 3, 2024
1 parent e39d6a2 commit 762efb8
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ FetchContent_MakeAvailable(slang)

add_executable(sv-bugpoint source/SvBugpoint.cpp source/Utils.cpp source/PairRemovers.cpp source/BodyRemover.cpp
source/BodyPartsRemover.cpp source/DeclRemover.cpp source/InstantationRemover.cpp source/ModportRemover.cpp source/ContAssignRemover.cpp source/ParamAssignRemover.cpp
source/StatementsRemover.cpp source/MemberRemover.cpp source/ImportsRemover.cpp)
source/StatementsRemover.cpp source/MemberRemover.cpp source/ImportsRemover.cpp source/TypeSimplifier.cpp)

target_link_libraries(sv-bugpoint PRIVATE slang::slang)
target_precompile_headers(sv-bugpoint PUBLIC
Expand Down
11 changes: 11 additions & 0 deletions source/OneTimeRewriter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,17 @@ class OneTimeRewriter : public SyntaxRewriter<TDerived> {
}
}

template <typename TOrig, typename TNew>
void replaceNode(const TOrig& originalNode, TNew& newNode) {
logType<TOrig>();
// NOTE: this may look weird in multiline cases
std::cerr << "-" << originalNode.toString() << "\n";
std::cerr << "+" << newNode.toString() << "\n";
DERIVED->replace(originalNode, newNode);
rewritePoint = originalNode.sourceRange();
state = REGISTER_CHILD;
}

std::shared_ptr<SyntaxTree> transform(const std::shared_ptr<SyntaxTree> tree,
bool& traversalDone,
AttemptStats& stats) {
Expand Down
1 change: 1 addition & 0 deletions source/SvBugpoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ bool pass(std::shared_ptr<SyntaxTree>& tree, const std::string& passIdx = "-") {
commited |= rewriteLoop(makeExternRemover(tree), tree, "externRemover", passIdx);
commited |= rewriteLoop<DeclRemover>(tree, "declRemover", passIdx);
commited |= rewriteLoop<StatementsRemover>(tree, "statementsRemover", passIdx);
commited |= rewriteLoop<TypeSimplifier>(tree, "typeSimplifier", passIdx);
commited |= rewriteLoop<ImportsRemover>(tree, "importsRemover", passIdx);
commited |= rewriteLoop<ParamAssignRemover>(tree, "paramAssignRemover", passIdx);
commited |= rewriteLoop<ContAssignRemover>(tree, "contAssignRemover", passIdx);
Expand Down
53 changes: 53 additions & 0 deletions source/TypeSimplifier.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include "OneTimeRewriter.hpp"

class IsPrimitive : public SyntaxVisitor<IsPrimitive> {
public:
// We don't visit the children, so it might not be obvious why we're using a Visitor.
// We do it so we can use SyntaxNode::visit for dynamic type dispatch.
bool isPrimitive;

template <typename T>
void handle(const T& t) {
isPrimitive = false;
}

void handle(const IntegerTypeSyntax& t) {
isPrimitive = true;
}

void handle(const KeywordTypeSyntax& t) {
isPrimitive = true;
}

void handle(const ImplicitTypeSyntax& t) {
isPrimitive = true;
}
};


class TypeSimplifier : public OneTimeRewriter<TypeSimplifier> {
// replace references to user-defined types with int
public:
bool canSimplify(not_null<const DataTypeSyntax*> node) {
IsPrimitive checker;
node->visit(checker);
return !checker.isPrimitive;
}

IntegerTypeSyntax* makeIntNode(SourceLocation location) {
auto token = Token(alloc, parsing::TokenKind::IntKeyword, {&SingleSpace, 1}, "int", location);
auto node = IntegerTypeSyntax(SyntaxKind::IntType, token, {}, SyntaxList<VariableDimensionSyntax>({}));
return alloc.emplace<IntegerTypeSyntax>(node);
}

ShouldVisitChildren handle(const DataTypeSyntax& node, bool isNodeRemovable) {
if (state == REMOVAL_ALLOWED && canSimplify(&node)) {
replaceNode(node, *makeIntNode(node.sourceRange().start()));
}
return VISIT_CHILDREN;
}
};

template bool rewriteLoop<TypeSimplifier>(std::shared_ptr<SyntaxTree>& tree,
std::string stageName,
std::string passIdx);

0 comments on commit 762efb8

Please sign in to comment.