Skip to content

Commit

Permalink
Merge pull request YosysHQ#145 from whitequark/wasi
Browse files Browse the repository at this point in the history
Add WASI platform support
  • Loading branch information
gatecat committed Jun 26, 2020
2 parents f9815d5 + 74f2c76 commit cada34d
Show file tree
Hide file tree
Showing 11 changed files with 127 additions and 8 deletions.
26 changes: 25 additions & 1 deletion libtrellis/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,24 @@ else()
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
endif()
endif()
set(boost_libs filesystem thread program_options system)
if (WASI)
set(USE_THREADS OFF)
add_definitions(
-DBOOST_EXCEPTION_DISABLE
-DBOOST_NO_EXCEPTIONS
-DBOOST_SP_NO_ATOMIC_ACCESS
-DBOOST_AC_DISABLE_THREADS
-DBOOST_NO_CXX11_HDR_MUTEX
)
else()
set(USE_THREADS ON)
endif()
set(boost_libs filesystem program_options system)
if (USE_THREADS)
list(APPEND boost_libs thread)
else()
add_definitions(-DNO_THREADS)
endif()
set(Boost_NO_BOOST_CMAKE ON)
find_package(PythonInterp 3.5 REQUIRED)

Expand Down Expand Up @@ -180,6 +197,13 @@ target_compile_definitions(${PROGRAM_PREFIX}ecpmulti PRIVATE TRELLIS_RPATH_DATAD
target_link_libraries(${PROGRAM_PREFIX}ecpmulti trellis ${Boost_LIBRARIES} ${CMAKE_DL_LIBS} ${link_param})
setup_rpath(${PROGRAM_PREFIX}ecpmulti)

if (WASI)
foreach (tool ecpbram ecppack ecpunpack ecppll ecpmulti)
# set(CMAKE_EXECUTABLE_SUFFIX) breaks CMake tests for some reason
set_property(TARGET ${PROGRAM_PREFIX}${tool} PROPERTY SUFFIX ".wasm")
endforeach()
endif()

if (BUILD_SHARED)
install(TARGETS trellis ${PROGRAM_PREFIX}ecpbram ${PROGRAM_PREFIX}ecppack ${PROGRAM_PREFIX}ecppll ${PROGRAM_PREFIX}ecpunpack ${PROGRAM_PREFIX}ecpmulti ${PythonInstallTarget}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}/${PROGRAM_PREFIX}trellis
Expand Down
6 changes: 6 additions & 0 deletions libtrellis/include/BitDatabase.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
#include <cstdint>
#include <boost/optional.hpp>
#include <mutex>
#ifndef NO_THREADS
#include <boost/thread/shared_mutex.hpp>
#include <atomic>
#endif
#include <set>
#include <unordered_set>
#include "Util.hpp"
Expand Down Expand Up @@ -331,8 +333,12 @@ class TileBitDatabase
private:
explicit TileBitDatabase(const string &filename);

#ifdef NO_THREADS
bool dirty = false;
#else
mutable boost::shared_mutex db_mutex;
atomic<bool> dirty{false};
#endif
map<string, MuxBits> muxes;
map<string, WordSettingBits> words;
map<string, EnumSettingBits> enums;
Expand Down
11 changes: 11 additions & 0 deletions libtrellis/include/DatabasePath.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@

#if BOOST_VERSION >= 106100

#if defined(__wasm)

std::string get_database_path()
{
return "/share/trellis/database";
}

#else

#include <boost/dll/runtime_symbol_info.hpp>

std::string get_database_path()
Expand All @@ -16,6 +25,8 @@ std::string get_database_path()
return database_folder;
}

#endif

#else

/*
Expand Down
40 changes: 40 additions & 0 deletions libtrellis/src/BitDatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@

#include <algorithm>
#include <fstream>
#ifndef NO_THREADS
#include <boost/thread/shared_lock_guard.hpp>
#include <boost/thread/lock_guard.hpp>
#endif
#include <boost/range/algorithm/copy.hpp>
#include <boost/range/adaptors.hpp>

Expand Down Expand Up @@ -335,7 +337,9 @@ TileBitDatabase::TileBitDatabase(const string &filename) : filename(filename)

void TileBitDatabase::config_to_tile_cram(const TileConfig &cfg, CRAMView &tile, bool is_tilegroup, set<string> *tg_matches) const
{
#ifndef NO_THREADS
boost::shared_lock_guard<boost::shared_mutex> guard(db_mutex);
#endif
for (auto arc : cfg.carcs)
muxes.at(arc.sink).set_driver(tile, arc.source);
set<string> found_words, found_enums;
Expand Down Expand Up @@ -397,7 +401,9 @@ void TileBitDatabase::config_to_tile_cram(const TileConfig &cfg, CRAMView &tile,

TileConfig TileBitDatabase::tile_cram_to_config(const CRAMView &tile) const
{
#ifndef NO_THREADS
boost::shared_lock_guard<boost::shared_mutex> guard(db_mutex);
#endif
TileConfig cfg;
BitSet coverage;
for (auto mux : muxes) {
Expand Down Expand Up @@ -431,7 +437,9 @@ TileConfig TileBitDatabase::tile_cram_to_config(const CRAMView &tile) const

void TileBitDatabase::load()
{
#ifndef NO_THREADS
boost::lock_guard<boost::shared_mutex> guard(db_mutex);
#endif
ifstream in(filename);
if (!in) {
throw runtime_error("failed to open tilebit database file " + filename);
Expand Down Expand Up @@ -466,7 +474,9 @@ void TileBitDatabase::load()

void TileBitDatabase::save()
{
#ifndef NO_THREADS
boost::lock_guard<boost::shared_mutex> guard(db_mutex);
#endif
ofstream out(filename);
if (!out) {
throw runtime_error("failed to open tilebit database file " + filename + " for writing");
Expand All @@ -488,49 +498,63 @@ void TileBitDatabase::save()

vector<string> TileBitDatabase::get_sinks() const
{
#ifndef NO_THREADS
boost::shared_lock_guard<boost::shared_mutex> guard(db_mutex);
#endif
vector<string> result;
boost::copy(muxes | boost::adaptors::map_keys, back_inserter(result));
return result;
}

MuxBits TileBitDatabase::get_mux_data_for_sink(const string &sink) const
{
#ifndef NO_THREADS
boost::shared_lock_guard<boost::shared_mutex> guard(db_mutex);
#endif
return muxes.at(sink);
}

vector<string> TileBitDatabase::get_settings_words() const
{
#ifndef NO_THREADS
boost::shared_lock_guard<boost::shared_mutex> guard(db_mutex);
#endif
vector<string> result;
boost::copy(words | boost::adaptors::map_keys, back_inserter(result));
return result;
}

WordSettingBits TileBitDatabase::get_data_for_setword(const string &name) const
{
#ifndef NO_THREADS
boost::shared_lock_guard<boost::shared_mutex> guard(db_mutex);
#endif
return words.at(name);
}

vector<string> TileBitDatabase::get_settings_enums() const
{
#ifndef NO_THREADS
boost::shared_lock_guard<boost::shared_mutex> guard(db_mutex);
#endif
vector<string> result;
boost::copy(enums | boost::adaptors::map_keys, back_inserter(result));
return result;
}

EnumSettingBits TileBitDatabase::get_data_for_enum(const string &name) const
{
#ifndef NO_THREADS
boost::shared_lock_guard<boost::shared_mutex> guard(db_mutex);
#endif
return enums.at(name);
}

vector<FixedConnection> TileBitDatabase::get_fixed_conns() const
{
#ifndef NO_THREADS
boost::shared_lock_guard<boost::shared_mutex> guard(db_mutex);
#endif
vector<FixedConnection> result;
for (const auto &csink : fixed_conns) {
for (const auto &conn : csink.second) {
Expand Down Expand Up @@ -560,7 +584,9 @@ vector<pair<string, bool>> TileBitDatabase::get_downhill_wires(const string &wir

void TileBitDatabase::add_routing(const TileInfo &tile, RoutingGraph &graph) const
{
#ifndef NO_THREADS
boost::shared_lock_guard<boost::shared_mutex> guard(db_mutex);
#endif
int row, col;
tie(row, col) = tile.get_row_col();
Location loc(col, row);
Expand Down Expand Up @@ -603,7 +629,9 @@ void TileBitDatabase::add_routing(const TileInfo &tile, RoutingGraph &graph) con

void TileBitDatabase::add_mux_arc(const ArcData &arc)
{
#ifndef NO_THREADS
boost::lock_guard<boost::shared_mutex> guard(db_mutex);
#endif
dirty = true;
if (muxes.find(arc.sink) == muxes.end()) {
MuxBits mux;
Expand All @@ -630,7 +658,9 @@ void TileBitDatabase::add_mux_arc(const ArcData &arc)

void TileBitDatabase::add_setting_word(const WordSettingBits &wsb)
{
#ifndef NO_THREADS
boost::lock_guard<boost::shared_mutex> guard(db_mutex);
#endif
dirty = true;
if (words.find(wsb.name) != words.end()) {
WordSettingBits &curr = words.at(wsb.name);
Expand All @@ -653,7 +683,9 @@ void TileBitDatabase::add_setting_word(const WordSettingBits &wsb)

void TileBitDatabase::add_setting_enum(const EnumSettingBits &esb)
{
#ifndef NO_THREADS
boost::lock_guard<boost::shared_mutex> guard(db_mutex);
#endif
dirty = true;
if (enums.find(esb.name) != enums.end()) {
EnumSettingBits &curr = enums.at(esb.name);
Expand All @@ -677,7 +709,9 @@ void TileBitDatabase::add_setting_enum(const EnumSettingBits &esb)

void TileBitDatabase::add_fixed_conn(const Trellis::FixedConnection &conn)
{
#ifndef NO_THREADS
boost::lock_guard<boost::shared_mutex> guard(db_mutex);
#endif
fixed_conns[conn.sink].insert(conn);
dirty = true;
}
Expand All @@ -691,19 +725,25 @@ TileBitDatabase::TileBitDatabase(const TileBitDatabase &other)

void TileBitDatabase::remove_fixed_sink(const string &sink)
{
#ifndef NO_THREADS
boost::lock_guard<boost::shared_mutex> guard(db_mutex);
#endif
fixed_conns.erase(sink);
}

void TileBitDatabase::remove_setting_enum(const string &enum_name)
{
#ifndef NO_THREADS
boost::lock_guard<boost::shared_mutex> guard(db_mutex);
#endif
enums.erase(enum_name);
}

void TileBitDatabase::remove_setting_word(const string &word_name)
{
#ifndef NO_THREADS
boost::lock_guard<boost::shared_mutex> guard(db_mutex);
#endif
words.erase(word_name);
}

Expand Down
8 changes: 8 additions & 0 deletions libtrellis/src/Database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ static pt::ptree devices_info;

// Cache Tilegrid data, to save time parsing it again
static map<string, pt::ptree> tilegrid_cache;
#ifndef NO_THREADS
static mutex tilegrid_cache_mutex;
#endif

void load_database(string root) {
db_root = root;
Expand Down Expand Up @@ -124,7 +126,9 @@ vector<TileInfo> get_device_tilegrid(const DeviceLocator &part) {
string tilegrid_path = db_root + "/" + part.family + "/" + part.device + "/tilegrid.json";
{
ChipInfo info = get_chip_info(part);
#ifndef NO_THREADS
lock_guard <mutex> lock(tilegrid_cache_mutex);
#endif
if (tilegrid_cache.find(part.device) == tilegrid_cache.end()) {
pt::ptree tg_parsed;
pt::read_json(tilegrid_path, tg_parsed);
Expand Down Expand Up @@ -160,10 +164,14 @@ vector<TileInfo> get_device_tilegrid(const DeviceLocator &part) {
}

static unordered_map<TileLocator, shared_ptr<TileBitDatabase>> bitdb_store;
#ifndef NO_THREADS
static mutex bitdb_store_mutex;
#endif

shared_ptr<TileBitDatabase> get_tile_bitdata(const TileLocator &tile) {
#ifndef NO_THREADS
lock_guard <mutex> bitdb_store_lg(bitdb_store_mutex);
#endif
if (bitdb_store.find(tile) == bitdb_store.end()) {
assert(!db_root.empty());
string bitdb_path = db_root + "/" + tile.family + "/tiledata/" + tile.tiletype + "/bits.db";
Expand Down
20 changes: 14 additions & 6 deletions libtrellis/tools/ecpbram.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include "Chip.hpp"
#include "Database.hpp"
#include "DatabasePath.hpp"
#include "wasmexcept.hpp"

using std::map;
using std::pair;
Expand Down Expand Up @@ -124,11 +125,6 @@ void parse_hexfile_line(const char *filename, int linenr, vector<vector<bool>> &
int main(int argc, char **argv)
{
bool verbose = false;
#ifdef _WIN32
uint32_t seed_nr = GetCurrentProcessId();
#else
uint32_t seed_nr = getpid();
#endif
namespace po = boost::program_options;

std::string database_folder = get_database_path();;
Expand Down Expand Up @@ -197,14 +193,26 @@ int main(int argc, char **argv)
return 1;
}

// If -s is provided: seed with the given value.
// If -s is not provided: seed with the PID and current time, which are unlikely
// to repeat simultaneously.
uint32_t seed_nr;
if (vm.count("seed")) {
seed_nr = vm.at("seed").as<int>();

if (verbose)
fprintf(stderr, "Seed: %d\n", seed_nr);
} else {
#if defined(__wasm)
seed_nr = 0;
#elif defined(_WIN32)
seed_nr = GetCurrentProcessId();
#else
seed_nr = getpid();
#endif
}

x = uint64_t(seed_nr) << 32;
x = uint64_t(seed_nr) << 32;
x ^= uint64_t(seed_nr) << 20;
x ^= uint64_t(seed_nr);

Expand Down
1 change: 1 addition & 0 deletions libtrellis/tools/ecpmulti.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "DatabasePath.hpp"
#include "Tile.hpp"
#include "version.hpp"
#include "wasmexcept.hpp"
#include <iostream>
#include <boost/program_options.hpp>
#include <stdexcept>
Expand Down
2 changes: 1 addition & 1 deletion libtrellis/tools/ecppack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include "Tile.hpp"
#include "BitDatabase.hpp"
#include "version.hpp"

#include "wasmexcept.hpp"
#include <iostream>
#include <boost/program_options.hpp>
#include <stdexcept>
Expand Down
1 change: 1 addition & 0 deletions libtrellis/tools/ecppll.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ f_out = f_vco / output
#include <string>
#include <boost/program_options.hpp>
#include "version.hpp"
#include "wasmexcept.hpp"
using namespace std;

enum class pll_mode{
Expand Down
1 change: 1 addition & 0 deletions libtrellis/tools/ecpunpack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "Database.hpp"
#include "DatabasePath.hpp"
#include "version.hpp"
#include "wasmexcept.hpp"
#include <iostream>
#include <boost/optional.hpp>
#include <boost/program_options.hpp>
Expand Down

0 comments on commit cada34d

Please sign in to comment.