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

Add control-flow based optimizer passes #1697

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
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
10 changes: 10 additions & 0 deletions 3rdparty/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,13 @@ check_cxx_compiler_flag("-Wno-changes-meaning" _has_no_changes_meaning_flag)
if (_has_no_changes_meaning_flag)
set_property(TARGET reproc++ PROPERTY COMPILE_OPTIONS "-Wno-changes-meaning")
endif ()

include(FetchContent)
FetchContent_Declare(
cxxgraph
GIT_REPOSITORY https://github.com/ZigRazor/CXXGraph.git
GIT_TAG v3.1.0
)
FetchContent_MakeAvailable(cxxgraph)
add_library(CXXGraph INTERFACE)
target_include_directories(CXXGraph SYSTEM INTERFACE ${cxxgraph_SOURCE_DIR}/include)
Comment on lines +53 to +61
Copy link
Member Author

Choose a reason for hiding this comment

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

This is a temporary solution until we decide on a proper graph library (or implement one ourselves); we cannot use this library as it is licensed under AGPL-3.0. It also seems to be of poor quality (e.g., major bugs around directed graphs, poor test suite).

6 changes: 5 additions & 1 deletion hilti/toolchain/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ set(SOURCES
src/compiler/cxx/formatter.cc
src/compiler/cxx/linker.cc
src/compiler/cxx/unit.cc
src/compiler/cfg.cc
src/compiler/driver.cc
src/compiler/init.cc
src/compiler/jit.cc
Expand Down Expand Up @@ -134,6 +135,7 @@ target_include_directories(hilti-objects BEFORE
PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>)
target_include_directories(hilti-objects BEFORE
PUBLIC $<BUILD_INTERFACE:${PROJECT_BINARY_DIR}/include>)
target_link_libraries(hilti-objects PUBLIC CXXGraph)

# Unclear why we need this: Without it, the generated Bison/Flex get a broken
# include path on some systems. (Seen on Ubuntu 19.10).
Expand Down Expand Up @@ -221,8 +223,10 @@ install(CODE "file(REMOVE \"\$ENV\{DESTDIR\}${CMAKE_INSTALL_FULL_INCLUDEDIR}/hil

##### Tests

add_executable(hilti-toolchain-tests tests/main.cc tests/id-base.cc tests/visitor.cc tests/util.cc)
add_executable(hilti-toolchain-tests tests/main.cc tests/id-base.cc tests/visitor.cc tests/util.cc
tests/cfg.cc)
hilti_link_executable_in_tree(hilti-toolchain-tests PRIVATE)
target_link_libraries(hilti-toolchain-tests PRIVATE doctest)
target_link_libraries(hilti-toolchain-tests PRIVATE CXXGraph)
target_compile_options(hilti-toolchain-tests PRIVATE "-Wall")
add_test(NAME hilti-toolchain-tests COMMAND ${PROJECT_BINARY_DIR}/bin/hilti-toolchain-tests)
2 changes: 1 addition & 1 deletion hilti/toolchain/include/ast/builder/builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ class ExtendedBuilderTemplate : public Builder {
auto addWhileElse(Expression* cond, const Meta& m = Meta()) {
auto body = Builder::statementBlock();
auto else_ = Builder::statementBlock();
Builder::block()->_add(Builder::context(), statementWhile(cond, body, else_, m));
Builder::block()->_add(Builder::context(), Builder::statementWhile(cond, body, else_, m));
return std::make_pair(_newBuilder(body), _newBuilder(else_));
}

Expand Down
3 changes: 0 additions & 3 deletions hilti/toolchain/include/ast/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,6 @@ class Node {
public:
virtual ~Node();

/** Returns the node tag associated with the instance's class. */
auto nodeTag() const { return _node_tags.back(); }

/** Returns true if the node has a parent (i.e., it's part of an AST). */
bool hasParent() const { return _parent; }

Expand Down
96 changes: 96 additions & 0 deletions hilti/toolchain/include/compiler/detail/cfg.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// Copyright (c) 2020-2023 by the Zeek Project. See LICENSE for details.

#pragma once

#include <cstdint>
#include <memory>
#include <string>
#include <type_traits>
#include <unordered_set>
#include <utility>

#include <hilti/ast/ast-context.h>
#include <hilti/ast/node.h>

#include <CXXGraph/CXXGraph.hpp>

namespace hilti {

// Needed for CXXGraph, but left unimplemented.
std::istream& operator>>(std::istream&, Node*);

namespace node::tag {
enum : uint16_t {
MetaNode = 10000,
Start,
End,
Flow,
};
}

namespace detail::cfg {
struct MetaNode : Node {
MetaNode(node::Tags node_tags) : Node(nullptr, node_tags, {}, {}) {}
HILTI_NODE_0(MetaNode, override);
};

// A meta node for the start of a control flow.
struct Start : MetaNode {
Start() : MetaNode(NodeTags) {}
HILTI_NODE_1(Start, MetaNode, final);
};

// A meta node for the end of a control flow.
struct End : MetaNode {
End() : MetaNode(NodeTags) {}
HILTI_NODE_1(End, MetaNode, final);
};

// A meta node joining or splitting control flow with no matching source statement.
struct Flow : MetaNode {
Flow() : MetaNode(NodeTags) {}
HILTI_NODE_1(Flow, MetaNode, final);
};

class CFG {
public:
using N = Node*;
using NodeP = std::shared_ptr<const CXXGraph::Node<N>>;

CFG(const N& root);


template<typename T, typename = std::enable_if_t<std::is_base_of_v<MetaNode, T>>>
N create_meta_node() {
auto n = std::make_unique<T>();
auto* r = n.get();
meta_nodes.insert(std::move(n));
return r;
}

NodeP get_or_add_node(const N& n);
void add_edge(NodeP from, NodeP to);

NodeP add_block(NodeP parent, const Nodes& stmts);
NodeP add_while(NodeP parent, const statement::While& while_);
NodeP add_if(NodeP parent, const statement::If& if_);
NodeP add_try_catch(const NodeP& parent, const statement::Try& try_);
NodeP add_return(const NodeP& parent, const N& expression);

const auto& edges() const { return g.getEdgeSet(); }
auto nodes() const { return g.getNodeSet(); }

CXXGraph::T_NodeSet<N> unreachable_nodes() const;

std::string dot() const;

private:
CXXGraph::Graph<N> g;

std::unordered_set<std::unique_ptr<MetaNode>> meta_nodes;
NodeP begin;
NodeP end;
};
} // namespace detail::cfg

} // namespace hilti
241 changes: 241 additions & 0 deletions hilti/toolchain/src/compiler/cfg.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,241 @@
// Copyright (c) 2020-2023 by the Zeek Project. See LICENSE for details.

#include "hilti/compiler/detail/cfg.h"

#include <algorithm>
#include <iterator>
#include <utility>

#include <hilti/ast/node.h>
#include <hilti/ast/statement.h>
#include <hilti/ast/statements/block.h>
#include <hilti/ast/statements/expression.h>
#include <hilti/ast/statements/if.h>
#include <hilti/ast/statements/return.h>
#include <hilti/ast/statements/throw.h>
#include <hilti/ast/statements/try.h>
#include <hilti/ast/statements/while.h>
#include <hilti/base/util.h>

namespace hilti {
std::istream& operator>>(std::istream&, Node*) { util::cannotBeReached(); }

std::string node_id(const Node* n) { return util::fmt("%d", n ? n->identity() : 0); }

namespace detail::cfg {

CFG::CFG(const N& root)
: begin(get_or_add_node(create_meta_node<Start>())), end(get_or_add_node(create_meta_node<End>())) {
assert(root && root->isA<statement::Block>() && "only building from blocks currently supported");

auto last = add_block(begin, root->children());
add_edge(last, end);
}

CFG::NodeP CFG::add_block(NodeP parent, const Nodes& stmts) {
// If `children` directly has any statements which change control flow like
// `throw` or `return` any statements after that are unreachable. To model
// such ASTs we add a flow with all statements up to the "last" semantic
// statement (either the last child or the control flow statement) to the
// CFG under `parent`. Statements after that are added as children without
// parents, and mixed with the previous flow.

// After this block `last` is the last reachable statement, either end of
// children or a control flow statement.
auto last = std::find_if(stmts.begin(), stmts.end(), [](auto&& c) {
return c && (c->template isA<statement::Return>() || c->template isA<statement::Throw>());
});
const bool has_dead_flow = last != stmts.end();
if ( has_dead_flow )
last = std::next(last);

// Add all statements which are part of the normal flow.
for ( auto&& c : (last != stmts.end() ? Nodes(stmts.begin(), last) : stmts) ) {
if ( ! c || ! c->isA<Statement>() )
continue;

if ( auto&& while_ = c->tryAs<statement::While>() )
parent = add_while(parent, *while_);

else if ( auto&& if_ = c->tryAs<statement::If>() )
parent = add_if(parent, *if_);

else if ( auto&& try_catch = c->tryAs<statement::Try>() )
parent = add_try_catch(parent, *try_catch);

else if ( auto&& return_ = c->tryAs<statement::Return>() )
parent = add_return(parent, return_->expression());

else if ( auto&& throw_ = c->tryAs<statement::Throw>() )
parent = add_return(parent, throw_->expression());

else {
auto cc = get_or_add_node(c);

add_edge(parent, cc);
add_block(parent, c->children());

// Update `last` so sibling nodes get chained.
parent = std::move(cc);
}
}

// Add unreachable flows.
if ( has_dead_flow && last != stmts.end() ) {
auto next = add_block(nullptr, Nodes{last, stmts.end()});
auto mix = get_or_add_node(create_meta_node<Flow>());
add_edge(parent, mix);
add_edge(next, mix);
parent = std::move(mix);
}

return parent;
}

CFG::NodeP CFG::add_while(NodeP parent, const statement::While& while_) {
auto&& condition = get_or_add_node(while_.condition());
add_edge(std::move(parent), condition);

auto body_end = add_block(condition, while_.body()->children());
add_edge(body_end, condition);
if ( auto&& else_ = while_.else_() ) {
auto&& else_end = add_block(condition, else_->children());

auto mix = get_or_add_node(create_meta_node<Flow>());

add_edge(else_end, mix);
add_edge(condition, mix);

return mix;
}

return condition;
}

CFG::NodeP CFG::add_if(NodeP parent, const statement::If& if_) {
auto&& condition = get_or_add_node(if_.condition());
add_edge(std::move(parent), condition);

auto true_end = add_block(condition, if_.true_()->children());
if ( auto false_ = if_.false_() ) {
auto false_end = add_block(condition, false_->children());
auto mix = get_or_add_node(create_meta_node<Flow>());

add_edge(false_end, mix);
add_edge(true_end, mix);

return mix;
}

return true_end;
}

CFG::NodeP CFG::add_try_catch(const NodeP& parent, const statement::Try& try_catch) {
auto try_ = add_block(parent, try_catch.body()->children());
auto mix = get_or_add_node(create_meta_node<Flow>());
add_edge(try_, mix);

for ( auto&& catch_ : try_catch.catches() ) {
auto catch_end = add_block(parent, catch_->body()->children());
add_edge(catch_end, mix);
}

return mix;
}

CFG::NodeP CFG::add_return(const NodeP& parent, const N& expression) {
if ( expression ) {
auto r = get_or_add_node(expression);
add_edge(parent, r);
return r;
}

return parent;
}

std::shared_ptr<const CXXGraph::Node<CFG::N>> CFG::get_or_add_node(const N& n) {
const auto& id = node_id(n);
if ( auto x = g.getNode(id) )
return *x;

auto y = std::make_shared<CXXGraph::Node<N>>(id, n);
g.addNode(y);
return y;
}

void CFG::add_edge(NodeP from, NodeP to) {
if ( ! from || ! to )
return;

if ( const auto& xs = g.outEdges(from);
xs.end() != std::find_if(xs.begin(), xs.end(), [&](const auto& e) { return e->getNodePair().second == to; }) )
return;
else {
auto e =
std::make_shared<CXXGraph::DirectedEdge<CFG::N>>(g.getEdgeSet().size(), std::move(from), std::move(to));
g.addEdge(std::move(e));
return;
}
}

std::string CFG::dot() const {
std::stringstream ss;

ss << "digraph {\n";

for ( auto&& n : g.getNodeSet() ) {
auto&& data = n->getData();
if ( auto&& meta = data->tryAs<MetaNode>() ) {
if ( data->isA<Start>() )
ss << util::fmt("\t%s [label=start shape=Mdiamond];\n", n->getId());

else if ( data->isA<End>() )
ss << util::fmt("\t%s [label=end shape=Msquare];\n", n->getId());

else if ( data->isA<Flow>() )
ss << util::fmt("\t%s [shape=point];\n", n->getId());

else
util::cannotBeReached();
}

else
ss << util::fmt("\t%s [label=\"%s\"];\n", n->getId(), rt::escapeUTF8(data->print(), true));
}

for ( auto&& e : g.getEdgeSet() ) {
auto&& [from, to] = e->getNodePair();
ss << util::fmt("\t%s -> %s [label=\"%s\"];\n", from->getId(), to->getId(), e->getId());
}

ss << "}";

return ss.str();
}

CXXGraph::T_NodeSet<CFG::N> CFG::unreachable_nodes() const {
auto xs = nodes();

// We cannot use `inOutEdges` to get a list of unreachable non-meta nodes
// since it is completely broken for directed graphs,
// https://github.com/ZigRazor/CXXGraph/issues/406.

std::unordered_set<CXXGraph::id_t> has_in_edge;
for ( auto&& e : g.getEdgeSet() ) {
auto&& [_, to] = e->getNodePair();
has_in_edge.insert(to->getId());
}

CXXGraph::T_NodeSet<N> result;
for ( auto&& n : xs ) {
auto&& data = n->getData();
if ( data && (! has_in_edge.count(n->getId()) && ! data->isA<MetaNode>()) )
result.insert(n);
}

return result;
}

} // namespace detail::cfg

} // namespace hilti
Loading