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

C++ infra for bril #307

Open
wants to merge 19 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
1 change: 1 addition & 0 deletions benchmarks/long/dead-branch.bril
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
@main {
v1: int = const 1;
v2: int = const 0;
v4: int = const 100; # last use of v4 must have a dominating def :)
counter: int = const 0;

.loop_start:
Expand Down
10 changes: 10 additions & 0 deletions bril-cpp/.clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
BasedOnStyle: Google
ColumnLimit: 88

# Some folks prefer to write "int& foo" while others prefer "int &foo". The
# Google Style Guide only asks for consistency within a project, we chose
# "int& foo" for this project:
DerivePointerAlignment: false
PointerAlignment: Left
IndentWidth: 2
IndentCaseLabels: false
5 changes: 5 additions & 0 deletions bril-cpp/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
_build/
compile_commands.json
.cache

!.vscode/
6 changes: 6 additions & 0 deletions bril-cpp/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"cmake.sourceDirectory": "/Users/albert/Documents/school/fa2023/cs6120/bril-fp/bril-cpp",
"cmake.buildDirectory": "${workspaceFolder}/_build/${buildType}",
"cmake.copyCompileCommands": "${workspaceFolder}/compile_commands.json"
}

49 changes: 49 additions & 0 deletions bril-cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
cmake_minimum_required(VERSION 3.16)

project(brilcpp VERSION 1.0 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED True)

include(FetchContent)

########
# JSON #
########

FetchContent_Declare(
json
URL https://github.com/nlohmann/json/releases/download/v3.11.2/json.tar.xz
DOWNLOAD_EXTRACT_TIMESTAMP YES
)
FetchContent_MakeAvailable(json)
set(JSON_ImplicitConversions off)


#########
# BOOST #
#########

find_package(Boost REQUIRED)

####################
# COMPILER OPTIONS #
####################

add_library(compiler_opts INTERFACE)
if (MSVC)
target_compile_options(compiler_opts INTERFACE /W4 /w14640 /permissive-)
else ()
target_compile_options(compiler_opts INTERFACE -Wall -Werror -Wextra -Wshadow
-Wnon-virtual-dtor -Wconversion
-Wsign-conversion -Wnull-dereference
-pedantic)
endif ()
target_compile_options(compiler_opts INTERFACE $<$<CONFIG:Debug>:-DETAC_DEBUG>)

##################
# SUBDIRECTORIES #
##################

add_subdirectory(src)
add_subdirectory(scripts)
28 changes: 28 additions & 0 deletions bril-cpp/brench-tests/brili.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
extract = 'total_dyn_inst: (\d+)'
benchmarks = '../benchmarks/**/*.bril'

[runs.baseline]
pipeline = [
"bril2json",
"brili -p {args}",
]

[runs.cpp]
pipeline = [
"bril2json",
"./_build/Debug/scripts/brili -p {args}",
]

[runs.baseline-ssa]
pipeline = [
"bril2json",
"./_build/Debug/scripts/ssa-conv --no-leave",
"brili -p {args}",
]

[runs.cpp-ssa]
pipeline = [
"bril2json",
"./_build/Debug/scripts/ssa-conv --no-leave",
"./_build/Debug/scripts/brili -p {args}",
]
22 changes: 22 additions & 0 deletions bril-cpp/brench-tests/ssa_conv.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
extract = 'total_dyn_inst: (\d+)'
benchmarks = '../benchmarks/**/*.bril'

[runs.baseline]
pipeline = [
"bril2json",
"brili -p {args}",
]

[runs.to_ssa]
pipeline = [
"bril2json",
"./_build/Debug/scripts/ssa-conv --no-leave",
"brili -p {args}",
]

[runs.to_and_leave_ssa]
pipeline = [
"bril2json",
"./_build/Debug/scripts/ssa-conv",
"brili -p {args}",
]
11 changes: 11 additions & 0 deletions bril-cpp/scripts/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
add_executable(parse-end-to-end parse_end_to_end.cpp)
target_link_libraries(parse-end-to-end PRIVATE core conv compiler_opts)

add_executable(print-dom print_dom.cpp)
target_link_libraries(print-dom PRIVATE core conv compiler_opts)

add_executable(ssa-conv ssa_conv.cpp)
target_link_libraries(ssa-conv PRIVATE core conv compiler_opts)

add_executable(brili brili_main.cpp)
target_link_libraries(brili PRIVATE core conv brili-lib compiler_opts)
78 changes: 78 additions & 0 deletions bril-cpp/scripts/brili_main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#include <iostream>
#include <limits>
#include <nlohmann/json.hpp>

#include "brili.hpp"
#include "parse.hpp"

using json = nlohmann::json;

bril::Val parseAs(std::string_view s, bril::Type t) {
switch (t.kind) {
case bril::TypeKind::Int:
return bril::Val(std::stoll(std::string(s)));
case bril::TypeKind::Float:
return bril::Val(std::stod(std::string(s)));
case bril::TypeKind::Bool:
return bril::Val(s == "true");
case bril::TypeKind::Char:
return bril::Val(static_cast<char32_t>(s[0]));
default:
bril::unreachable();
}
}

int main(int argc, char** argv) {
std::vector<std::string_view> sv_args;
bool print_total_dyn_inst = false;
for (int i = 1; i < argc; i++) {
if (argv[i] == std::string_view("-p")) {
print_total_dyn_inst = true;
continue;
}
sv_args.emplace_back(argv[i]);
}

json j = json::parse(std::cin);
auto prog = j.template get<bril::Prog>();

bril::Func* main = nullptr;
for (bril::Func& fn : prog.fns) {
if (fn.name == "main") {
main = &fn;
break;
}
}

if (main == nullptr) {
std::cerr << "error: could not find a main function." << std::endl;
return 1;
}
if (main->args.size() != sv_args.size()) {
std::cerr << "error: expected " << main->args.size() << " arguments, got "
<< sv_args.size() << std::endl;
return 1;
}
if (!main->ret_type.isVoid()) {
std::cerr << "error: expected main to not have a return type" << std::endl;
return 1;
}

std::vector<bril::Val> args;
for (size_t i = 0; i < sv_args.size(); i++) {
try {
args.emplace_back(parseAs(sv_args[i], main->args[i].type));
} catch (std::invalid_argument& e) {
std::cerr << "error: could not parse argument " << sv_args[i] << " as "
<< main->args[i].type << std::endl;
return 1;
}
}

bril::Brili brili(prog);
auto res = brili.run(args);

if (print_total_dyn_inst) {
std::cerr << "total_dyn_inst: " << res.total_dyn_inst << std::endl;
}
}
17 changes: 17 additions & 0 deletions bril-cpp/scripts/parse_end_to_end.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// this program is an identity: it takes in bril, parses it into C++ types, and
// converts these back into json bril

#include <iostream>
#include <nlohmann/json.hpp>

#include "parse.hpp"
#include "types.hpp"

using json = nlohmann::json;

int main() {
json j = json::parse(std::cin);
auto prog = j.template get<bril::Prog>();
json out = prog;
std::cout << out << std::endl;
}
49 changes: 49 additions & 0 deletions bril-cpp/scripts/print_dom.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include <iostream>
#include <nlohmann/json.hpp>

#include "dom.hpp"
#include "parse.hpp"
#include "types.hpp"

using json = nlohmann::json;

int main() {
json j = json::parse(std::cin);
auto prog = j.template get<bril::Prog>();

for (auto& fn : prog.fns) {
bril::DomAnalysis doma(fn);
doma.computeDomBy();
auto& bbsa = *fn.bbsv;

// prints the dom by set for each bb
std::cout << fn.name << '\n';
for (auto& bb : fn.bbs) {
std::cout << bril::bbNameToStr(fn, bb) << ": ";
for (size_t i = 0; i < fn.bbs.size(); i++) {
if (bb.domInfo()->dom_by.test(i)) {
std::cout << bril::bbNameToStr(fn, *bbsa[i]) << ", ";
}
}
std::cout << '\n';
}
std::cout << '\n';

doma.computeDomTree();
bril::domTreeGV(fn, std::cout);
std::cout << '\n';

doma.computeDomFront();
for (auto& bb : fn.bbs) {
std::cout << bril::bbNameToStr(fn, bb) << ": ";
for (size_t i = 0; i < fn.bbs.size(); i++) {
if (bb.domInfo()->dfront.test(i)) {
std::cout << bril::bbNameToStr(fn, *bbsa[i]) << ", ";
}
}
std::cout << '\n';
}

std::cout << std::endl;
}
}
46 changes: 46 additions & 0 deletions bril-cpp/scripts/ssa_conv.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include <iostream>
#include <nlohmann/json.hpp>

#include "leave_ssa.hpp"
#include "parse.hpp"
#include "to_ssa.hpp"

using json = nlohmann::json;

int main(int argc, char* argv[]) {
bool no_leave = false, no_to = false;
if (argc == 2) {
std::string arg = argv[1];
if (arg == "--no-leave") {
no_leave = true;
} else if (arg == "--no-to") {
no_to = true;
} else {
std::cerr << "Unknown argument: " << arg << std::endl;
return 1;
}
} else {
if (argc != 1) {
std::cerr << "Usage: " << argv[0] << " [--no-leave|--no-to]" << std::endl;
return 1;
}
}

json j = json::parse(std::cin);
auto prog = j.template get<bril::Prog>();

if (!no_to) {
for (auto& fn : prog.fns) {
bril::ToSSA(fn, true).toSSA();
}
}
if (!no_leave) {
for (auto& fn : prog.fns) {
bril::leaveSSA(fn);
fn.populateBBsV();
}
}

json out = prog;
std::cout << out << std::endl;
}
29 changes: 29 additions & 0 deletions bril-cpp/src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
add_library(core)
target_sources(core
PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/types.cpp
${CMAKE_CURRENT_SOURCE_DIR}/dom.cpp
${CMAKE_CURRENT_SOURCE_DIR}/to_ssa.cpp
${CMAKE_CURRENT_SOURCE_DIR}/leave_ssa.cpp
)
target_link_libraries(core PUBLIC Boost::boost util PRIVATE compiler_opts)
target_include_directories(core PUBLIC ${CMAKE_SOURCE_DIR}/src)

add_library(conv)
target_sources(conv
PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/parse.cpp
${CMAKE_CURRENT_SOURCE_DIR}/cfg.cpp
)
target_link_libraries(conv PUBLIC core nlohmann_json::nlohmann_json PRIVATE compiler_opts)
target_include_directories(conv PUBLIC ${CMAKE_SOURCE_DIR}/src)

add_library(brili-lib)
target_sources(brili-lib
PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/brili.cpp
)
target_link_libraries(brili-lib PUBLIC core PRIVATE compiler_opts)
target_include_directories(brili-lib PUBLIC ${CMAKE_SOURCE_DIR}/src)

add_subdirectory(util)
Loading