Skip to content

Commit

Permalink
Liberty support (#91)
Browse files Browse the repository at this point in the history
* Initiating Liberty support

* restructuring code

* Liberty construction in SNL and first tests

* Cleaning licenses

* clean license

* fixing tests

* Fix leak

* Leak fix

* install snl_liberty lib

* more testing

* clean licensing

* fix license

* leak cleaning

* Fix coverage
  • Loading branch information
xtofalex authored Aug 13, 2024
1 parent bdd4828 commit 2de2f06
Show file tree
Hide file tree
Showing 29 changed files with 72,652 additions and 12 deletions.
11 changes: 10 additions & 1 deletion .reuse/dep5
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,20 @@ Files: src/apps/naja_edit/examples/*
Copyright: 2022 The Naja Authors.
License: Apache-2.0

Files: test/snl/formats/verilog/backend/references/*
Files: test/snl/formats/verilog/backend/references/* test/snl/formats/liberty/benchmarks/errors/* thirdparty/yosys-liberty/test/benchmarks/test0.lib
Copyright: 2022 The Naja Authors.
License: CC0-1.0

Files: thirdparty/spdlog-1.14.1/*
Source: https://github.com/gabime/spdlog
Copyright: 2016 Gabi Melman.
License: MIT

Files: thirdparty/yosys-liberty/src/YosysLibertyParser.cpp thirdparty/yosys-liberty/src/YosysLibertyParser.h
Source: https://github.com/YosysHQ/yosys/tree/main/passes/techmap
Copyright: 2012-2022 The Yosys Authors.
License: ISC

FILES: thirdparty/yosys-liberty/test/benchmarks/test1.lib test/snl/formats/liberty/benchmarks/asap7_excerpt/test0.lib
Copyright: Copyright 2020 Lawrence T. Clark, Vinay Vashishtha, or Arizona State University
License: BSD-3-Clause
14 changes: 7 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,6 @@ find_package(Python3 3.9...<3.99 COMPONENTS Development Interpreter REQUIRED)
find_package(CapnProto CONFIG REQUIRED)
endif(NOT BUILD_ONLY_DOC)

if(NOT BUILD_ONLY_DOC)
add_subdirectory(thirdparty)
add_subdirectory(cmake)
add_subdirectory(src)
add_subdirectory(primitives)
endif(NOT BUILD_ONLY_DOC)

if(PROJECT_IS_TOP_LEVEL)
option(LONG_TESTS "Enable long tests" OFF)
include(CTest)
Expand All @@ -99,3 +92,10 @@ message(SEND_ERROR "Doxygen and Sphinx must be available to build Sphinx documen
endif(NOT (DOXYGEN_FOUND AND SPHINX_FOUND))
endif(BUILD_SPHINX_DOCUMENTATION)
endif(PROJECT_IS_TOP_LEVEL)

if(NOT BUILD_ONLY_DOC)
add_subdirectory(thirdparty)
add_subdirectory(cmake)
add_subdirectory(src)
add_subdirectory(primitives)
endif(NOT BUILD_ONLY_DOC)
9 changes: 9 additions & 0 deletions LICENSES/BSD-3-Clause.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

Copyright (c) <year> <owner>.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
7 changes: 7 additions & 0 deletions LICENSES/ISC.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
ISC License

Copyright <YEAR> <OWNER>

Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
3 changes: 2 additions & 1 deletion src/apps/naja_edit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ add_executable(naja_edit NajaEdit.cpp)
target_include_directories(naja_edit SYSTEM BEFORE PUBLIC ${Boost_INCLUDE_DIR})
target_include_directories(naja_edit PUBLIC ${ARGPARSE_DIR})
target_link_libraries(naja_edit
naja_snl_verilog naja_snl_pyloader naja_snl_dump naja_dnl naja_opt)
naja_snl_verilog naja_snl_pyloader naja_snl_liberty
naja_snl_dump naja_dnl naja_opt)
install(TARGETS naja_edit DESTINATION bin)
18 changes: 15 additions & 3 deletions src/apps/naja_edit/NajaEdit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "SNLPyEdit.h"
#include "SNLPyLoader.h"
#include "SNLUtils.h"
#include "SNLLibertyConstructor.h"
#include "SNLVRLConstructor.h"
#include "SNLVRLDumper.h"

Expand All @@ -35,10 +36,9 @@
#include "Reduction.h"
#include "Utils.h"

using namespace naja::DNL;
using namespace naja::SNL;
using namespace naja::DNL;
using namespace naja::NAJA_OPT;
using namespace naja::SNL;

namespace {

Expand Down Expand Up @@ -231,7 +231,19 @@ int main(int argc, char* argv[]) {
db = SNLDB::create(SNLUniverse::get());
primitivesLibrary = SNLLibrary::create(db, SNLLibrary::Type::Primitives,
SNLName("PRIMS"));
SNLPyLoader::loadPrimitives(primitivesLibrary, primitivesPath);
auto extension = primitivesPath.extension();
if (extension.empty()) {
SPDLOG_CRITICAL("Primitives path should end with an extension");
std::exit(EXIT_FAILURE);
} else if (extension == ".py") {
SNLPyLoader::loadPrimitives(primitivesLibrary, primitivesPath);
} else if (extension == ".lib") {
SNLLibertyConstructor constructor(primitivesLibrary);
constructor.construct(primitivesPath);
} else {
SPDLOG_CRITICAL("Unknow extension in Primitives path");
std::exit(EXIT_FAILURE);
}

auto designLibrary = SNLLibrary::create(db, SNLName("DESIGN"));
SNLVRLConstructor constructor(designLibrary);
Expand Down
1 change: 1 addition & 0 deletions src/snl/formats/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
#
# SPDX-License-Identifier: Apache-2.0

add_subdirectory(liberty)
add_subdirectory(verilog)
17 changes: 17 additions & 0 deletions src/snl/formats/liberty/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# SPDX-FileCopyrightText: 2024 The Naja authors <https://github.com/najaeda/naja/blob/main/AUTHORS>
#
# SPDX-License-Identifier: Apache-2.0

set(SOURCES
SNLLibertyConstructor.cpp
)

SET(HEADERS SNLLibertyConstructor.h)

add_library(naja_snl_liberty SHARED ${SOURCES})
target_link_libraries(naja_snl_liberty naja_snl yosys_liberty)

target_include_directories(naja_snl_liberty PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})

set_target_properties(naja_snl_liberty PROPERTIES PUBLIC_HEADER "${HEADERS}")
install(TARGETS naja_snl_liberty LIBRARY DESTINATION lib PUBLIC_HEADER DESTINATION include)
90 changes: 90 additions & 0 deletions src/snl/formats/liberty/SNLLibertyConstructor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Copyright 2024 The Naja Authors.
// SPDX-FileCopyrightText: 2024 The Naja authors <https://github.com/najaeda/naja/blob/main/AUTHORS>
//
// SPDX-License-Identifier: Apache-2.0

#include "SNLLibertyConstructor.h"

#include <fstream>
#include "YosysLibertyParser.h"

#include "SNLLibrary.h"
#include "SNLScalarTerm.h"
#include "SNLLibertyConstructorException.h"

namespace {

using namespace naja::SNL;

SNLTerm::Direction findDirection(const Yosys::LibertyAst* cell) {
for (auto child: cell->children) {
if (child->id == "direction") {
auto direction = child->value;
if (direction == "input") {
return SNLTerm::Direction::Input;
} else if (direction == "output") {
return SNLTerm::Direction::Output;
} else {
throw SNLLibertyConstructorException("Unknown direction");
}
}
}
throw SNLLibertyConstructorException("Direction not found");
}

void parseTerms(SNLDesign* primitive, const Yosys::LibertyAst* cell) {
for (auto child: cell->children) {
if (child->id == "pin") {
auto pinName = child->args[0];
auto direction = findDirection(child);
auto term = SNLScalarTerm::create(primitive, direction, SNLName(pinName));
}
}
}

void parseCell(SNLLibrary* library, const Yosys::LibertyAst* cell) {
auto cellName = cell->args[0];
auto primitive = SNLDesign::create(library, SNLDesign::Type::Primitive, SNLName(cellName));
parseTerms(primitive, cell);
}

void parseCells(SNLLibrary* library, const Yosys::LibertyAst* ast) {
for (auto child: ast->children) {
if (child->id == "cell") {
parseCell(library, child);
}
}
}

}

namespace naja { namespace SNL {

SNLLibertyConstructor::SNLLibertyConstructor(SNLLibrary* library):
library_(library)
{}

void SNLLibertyConstructor::construct(const std::filesystem::path& path) {
if (not std::filesystem::exists(path)) {
std::string reason(path.string() + " does not exist");
throw SNLLibertyConstructorException(reason);
}
std::ifstream inFile(path);
if (not inFile.good()) {
std::string reason(path.string() + " is not a readable file");
throw SNLLibertyConstructorException(reason);
}
auto parser = std::make_unique<Yosys::LibertyParser>(inFile);
auto ast = parser->ast;
if (ast == nullptr) {
//LCOV_EXCL_START
std::string reason("Failed to parse the file");
throw SNLLibertyConstructorException(reason);
//LCOV_EXCL_STOP
}
auto libraryName = ast->args[0];
library_->setName(SNLName(libraryName));
parseCells(library_, ast);
}

}} // namespace SNL // namespace naja
29 changes: 29 additions & 0 deletions src/snl/formats/liberty/SNLLibertyConstructor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2024 The Naja Authors.
// SPDX-FileCopyrightText: 2023 The Naja authors <https://github.com/najaeda/naja/blob/main/AUTHORS>
//
// SPDX-License-Identifier: Apache-2.0

#ifndef __SNL_LIBERTY_CONSTRUCTOR_H_
#define __SNL_LIBERTY_CONSTRUCTOR_H_

#include <filesystem>

namespace naja { namespace SNL {

class SNLLibrary;

class SNLLibertyConstructor {
public:
SNLLibertyConstructor() = delete;
SNLLibertyConstructor(const SNLLibertyConstructor&) = delete;
SNLLibertyConstructor(SNLLibrary* library);

void construct(const std::filesystem::path& path);
private:
SNLLibrary* library_;

};

}} // namespace SNL // namespace naja

#endif // __SNL_LIBERTY_CONSTRUCTOR_H_
25 changes: 25 additions & 0 deletions src/snl/formats/liberty/SNLLibertyConstructorException.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright 2024 The Naja Authors.
// SPDX-FileCopyrightText: 2024 The Naja authors <https://github.com/najaeda/naja/blob/main/AUTHORS>
//
// SPDX-License-Identifier: Apache-2.0

#ifndef __SNL_LIBERTY_CONSTRUCTOR_EXCEPTION_H_
#define __SNL_LIBERTY_CONSTRUCTOR_EXCEPTION_H_

#include "SNLException.h"

namespace naja { namespace SNL {

struct SNLLibertyConstructorException: public SNLException {
public:
SNLLibertyConstructorException() = delete;
SNLLibertyConstructorException(const SNLLibertyConstructorException&) = default;

SNLLibertyConstructorException(const std::string& reason):
SNLException(reason)
{}
};

}} // namespace SNL // namespace naja

#endif // __SNL_LIBERTY_CONSTRUCTOR_EXCEPTION_H_
1 change: 1 addition & 0 deletions test/snl/formats/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
#
# SPDX-License-Identifier: Apache-2.0

add_subdirectory(liberty)
add_subdirectory(verilog)
17 changes: 17 additions & 0 deletions test/snl/formats/liberty/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# SPDX-FileCopyrightText: 2024 The Naja liberty authors <https://github.com/najaeda/naja/blob/main/AUTHORS>
#
# SPDX-License-Identifier: Apache-2.0

include(GoogleTest)

set(tests
SNLLibertyConstructorTest0.cpp
)

add_executable(snl_liberty_tests ${tests})
target_compile_definitions(snl_liberty_tests PRIVATE
SNL_LIBERTY_BENCHMARKS="${CMAKE_CURRENT_SOURCE_DIR}")

target_link_libraries(snl_liberty_tests naja_snl_liberty gmock gtest_main)

GTEST_DISCOVER_TESTS(snl_liberty_tests)
Loading

0 comments on commit 2de2f06

Please sign in to comment.