Skip to content

[ImportVerilog] Bump slang #7792

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

Draft
wants to merge 21 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
32 changes: 18 additions & 14 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -556,28 +556,27 @@ llvm_canonicalize_cmake_booleans(CIRCT_SLANG_BUILD_FROM_SOURCE)
if(CIRCT_SLANG_FRONTEND_ENABLED)
message(STATUS "slang Verilog frontend is enabled")
if(CIRCT_SLANG_BUILD_FROM_SOURCE)
# slang requires C++20

Choose a reason for hiding this comment

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

Is this actually needed? slang's CMakeLists already sets these.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I get an error trying to build ImportVerilog due to public headers in slang requiring C++ 20 if I remove this, but perhaps there's a more principled way to solve this?

Choose a reason for hiding this comment

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

Ah I see, that makes sense. It's a little unfortunate that this is adding friction but I make extensive use of, e.g. std::span that would be hard to remove from the public headers. Hopefully LLVM / MLIR / CIRCT can upgrade to C++20 at some point across the board; it's coming up on 5 years old.

Copy link
Contributor

Choose a reason for hiding this comment

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

Drive by comment: in general, it's OK to require c++20 (or other non-llvm-standard things like exceptions) if they're optional and can be disabled. Since slang is an optional dependency, it's OK to require c++20 when slang is being used. We just need to be sure to run a ci build with c++17 and no slang.

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# Build slang as part of CIRCT (see https://sv-lang.com/building.html)
message(STATUS "Building slang from source")
include(FetchContent)
FetchContent_Declare(
slang
GIT_REPOSITORY https://github.com/MikePopoloski/slang.git
GIT_TAG v3.0
GIT_SHALLOW ON)
GIT_TAG v8.0)
set(FETCHCONTENT_TRY_FIND_PACKAGE_MODE "NEVER")

# Force Slang to be built as a static library to avoid messing around with
# RPATHs and installing a slang dylib alongside CIRCT. The static library
# will embed Slang into ImportVerilog.
# will embed Slang into ImportVerilog and circt-verilog-lsp-server.
set(ORIGINAL_CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})
set(ORIGINAL_BUILD_SHARED_LIBS ${BUILD_SHARED_LIBS})

if (MSVC)
set(CMAKE_CXX_FLAGS "/EHsc")
else ()
set(CMAKE_CXX_FLAGS "")
endif ()
set(BUILD_SHARED_LIBS OFF)
set(SLANG_USE_MIMALLOC OFF)
FetchContent_MakeAvailable(slang)

set(CMAKE_CXX_FLAGS ${ORIGINAL_CMAKE_CXX_FLAGS})
Expand All @@ -586,26 +585,31 @@ if(CIRCT_SLANG_FRONTEND_ENABLED)
if(BUILD_SHARED_LIBS)
set_target_properties(slang_slang PROPERTIES POSITION_INDEPENDENT_CODE ON)
set_target_properties(fmt PROPERTIES POSITION_INDEPENDENT_CODE ON)
set_target_properties(unordered_dense PROPERTIES POSITION_INDEPENDENT_CODE ON)
endif()

# The following feels *very* hacky, but CMake complains about the
# CIRCTImportVerilog target linking against slang_slang (even with PRIVATE
# linking) without the latter being in an export set. I think we'd want to
# statically link slang into the CIRCTImportVerilog library, but seems to be
# harder than it ought to be.
set_property(
GLOBAL APPEND PROPERTY CIRCT_EXPORTS slang_slang unordered_dense fmt)
get_target_property(slang_deps slang_slang INTERFACE_LINK_LIBRARIES)
foreach(alias ${slang_deps})
if (TARGET ${alias})
get_target_property(link ${alias} ALIASED_TARGET)
set_property(GLOBAL APPEND PROPERTY CIRCT_EXPORTS ${link})
install(TARGETS ${link} EXPORT CIRCTTargets)
endif()
endforeach()

# Disable the installation of headers coming from third-party libraries. We
# won't use those APIs directly. Just make them static libraries for the sake
# of running slang normally.
set_target_properties(fmt PROPERTIES PUBLIC_HEADER "")
set_target_properties(unordered_dense PROPERTIES PUBLIC_HEADER "")

install(TARGETS slang_slang unordered_dense fmt EXPORT CIRCTTargets)
set_property(GLOBAL APPEND PROPERTY CIRCT_EXPORTS slang_slang)
install(TARGETS slang_slang EXPORT CIRCTTargets)
else()
find_package(slang 3.0 REQUIRED)
find_package(slang 8.0 REQUIRED)
endif()
endif()

Expand Down
7 changes: 0 additions & 7 deletions cmake/modules/SlangCompilerOptions.cmake
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
# slang uses exceptions
set(LLVM_REQUIRES_EH ON)
set(LLVM_REQUIRES_RTTI ON)

# For ABI compatibility, define the DEBUG macro in debug builds. Slang sets this
# internally. If we don't set this here as well, header-defined things like the
# destructor of `Driver`, which is generated in ImportVerilog's compilation
Expand All @@ -15,9 +11,6 @@ add_compile_definitions($<$<CONFIG:Debug>:DEBUG>)
if (MSVC)
# No idea what to put here
else ()
# slang uses exceptions; we intercept these in ImportVerilog
add_compile_options(-fexceptions)
add_compile_options(-frtti)
# slang has some classes with virtual funcs but non-virtual destructor.
add_compile_options(-Wno-non-virtual-dtor)
# some other warnings we've seen
Expand Down
4 changes: 2 additions & 2 deletions include/circt/Support/FVInt.h
Original file line number Diff line number Diff line change
Expand Up @@ -647,8 +647,8 @@ inline FVInt operator-(uint64_t a, const FVInt &b) {

inline FVInt operator-(const APInt &a, const FVInt &b) { return FVInt(a) - b; }

inline bool operator==(uint64_t a, const FVInt &b) { return b == a; }
inline bool operator!=(uint64_t a, const FVInt &b) { return b != a; }
inline bool operator==(uint64_t a, const FVInt &b) { return b.operator==(a); }

Choose a reason for hiding this comment

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

How is this related to the slang version? Is it picking up some overzealous global operator in slang somewhere?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a consequence of building circt code with C++ 20 for the first time, where https://timsong-cpp.github.io/cppwp/n4868/over.match#oper-3.4.4 has come into play. If my analysis is correct, operator resolution of == in { return b == a; } where a: uint64_t and b: const FVInt &b resolved to inline bool operator==(uint64_t a, const FVInt &b) { return b == a; } , i.e. itself, leading to infinite recursion.

Choose a reason for hiding this comment

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

I see, just an artifact of C++20 and not anything slang is doing. Maybe you can just ifdef out these overloads when compiling in C++20 mode.

inline bool operator!=(uint64_t a, const FVInt &b) { return b.operator!=(a); }
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Any tips on how to best deal with https://timsong-cpp.github.io/cppwp/n4868/over.match#oper-3.4.4 in a codebase that still needs to support older standards?


inline raw_ostream &operator<<(raw_ostream &os, const FVInt &value) {
value.print(os);
Expand Down
8 changes: 8 additions & 0 deletions lib/Conversion/ImportVerilog/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,11 @@ add_circt_translation_library(CIRCTImportVerilog
PRIVATE
slang_slang
)

# HACK: When `OBJECT` argument is passed to `llvm_add_library()`,
# `COMPILE_DEFINITIONS` are not correctly inherited. For that reason,
# we manually set it here.
if(TARGET obj.CIRCTImportVerilog AND TARGET Boost::headers)
get_target_property(compile_defs Boost::headers INTERFACE_COMPILE_DEFINITIONS)
target_compile_definitions(obj.CIRCTImportVerilog PRIVATE ${compile_defs})
endif()
33 changes: 18 additions & 15 deletions lib/Conversion/ImportVerilog/Expressions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//===----------------------------------------------------------------------===//

#include "ImportVerilogInternals.h"
#include "slang/ast/EvalContext.h"
#include "slang/ast/SystemSubroutine.h"
#include "slang/syntax/AllSyntax.h"

Expand Down Expand Up @@ -611,7 +612,7 @@ struct RvalueExprVisitor {
// The open range list on the right-hand side of the inside operator is a
// comma-separated list of expressions or ranges.
if (const auto *openRange =
listExpr->as_if<slang::ast::OpenRangeExpression>()) {
listExpr->as_if<slang::ast::ValueRangeExpression>()) {
// Handle ranges.
auto lowBound = context.convertToSimpleBitVector(
context.convertRvalueExpression(openRange->left()));
Expand Down Expand Up @@ -915,21 +916,21 @@ struct RvalueExprVisitor {
value = builder.create<moore::ConcatOp>(loc, operands).getResult();
}

if (expr.sliceSize == 0) {
if (expr.getSliceSize() == 0) {
return value;
}

auto type = cast<moore::IntType>(value.getType());
SmallVector<Value> slicedOperands;
auto iterMax = type.getWidth() / expr.sliceSize;
auto remainSize = type.getWidth() % expr.sliceSize;
auto iterMax = type.getWidth() / expr.getSliceSize();
auto remainSize = type.getWidth() % expr.getSliceSize();

for (size_t i = 0; i < iterMax; i++) {
auto extractResultType = moore::IntType::get(
context.getContext(), expr.sliceSize, type.getDomain());
context.getContext(), expr.getSliceSize(), type.getDomain());

auto extracted = builder.create<moore::ExtractOp>(
loc, extractResultType, value, i * expr.sliceSize);
loc, extractResultType, value, i * expr.getSliceSize());
slicedOperands.push_back(extracted);
}
// Handle other wire
Expand All @@ -938,7 +939,7 @@ struct RvalueExprVisitor {
context.getContext(), remainSize, type.getDomain());

auto extracted = builder.create<moore::ExtractOp>(
loc, extractResultType, value, iterMax * expr.sliceSize);
loc, extractResultType, value, iterMax * expr.getSliceSize());
slicedOperands.push_back(extracted);
}

Expand Down Expand Up @@ -1133,7 +1134,7 @@ struct LvalueExprVisitor {
value = builder.create<moore::ConcatRefOp>(loc, operands).getResult();
}

if (expr.sliceSize == 0) {
if (expr.getSliceSize() == 0) {
return value;
}

Expand All @@ -1142,15 +1143,15 @@ struct LvalueExprVisitor {
SmallVector<Value> slicedOperands;
auto widthSum = type.getWidth();
auto domain = type.getDomain();
auto iterMax = widthSum / expr.sliceSize;
auto remainSize = widthSum % expr.sliceSize;
auto iterMax = widthSum / expr.getSliceSize();
auto remainSize = widthSum % expr.getSliceSize();

for (size_t i = 0; i < iterMax; i++) {
auto extractResultType = moore::RefType::get(
moore::IntType::get(context.getContext(), expr.sliceSize, domain));
auto extractResultType = moore::RefType::get(moore::IntType::get(
context.getContext(), expr.getSliceSize(), domain));

auto extracted = builder.create<moore::ExtractRefOp>(
loc, extractResultType, value, i * expr.sliceSize);
loc, extractResultType, value, i * expr.getSliceSize());
slicedOperands.push_back(extracted);
}
// Handle other wire
Expand All @@ -1159,7 +1160,7 @@ struct LvalueExprVisitor {
moore::IntType::get(context.getContext(), remainSize, domain));

auto extracted = builder.create<moore::ExtractRefOp>(
loc, extractResultType, value, iterMax * expr.sliceSize);
loc, extractResultType, value, iterMax * expr.getSliceSize());
slicedOperands.push_back(extracted);
}

Expand Down Expand Up @@ -1263,7 +1264,9 @@ slang::ConstantValue
Context::evaluateConstant(const slang::ast::Expression &expr) {
using slang::ast::EvalFlags;
slang::ast::EvalContext evalContext(
compilation, EvalFlags::CacheResults | EvalFlags::SpecparamsAllowed);
slang::ast::ASTContext(compilation.getRoot(),
slang::ast::LookupLocation::max),
EvalFlags::CacheResults | EvalFlags::SpecparamsAllowed);
return expr.eval(evalContext);
}

Expand Down
8 changes: 4 additions & 4 deletions lib/Conversion/ImportVerilog/FormatStrings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
//===----------------------------------------------------------------------===//

#include "ImportVerilogInternals.h"
#include "slang/text/SFormat.h"
#include "slang/ast/SFormat.h"

using namespace mlir;
using namespace circt;
using namespace ImportVerilog;
using moore::IntAlign;
using moore::IntFormat;
using moore::IntPadding;
using slang::SFormat::FormatOptions;
using slang::ast::SFormat::FormatOptions;

namespace {
struct FormatStringParser {
Expand Down Expand Up @@ -87,7 +87,7 @@ struct FormatStringParser {
auto onError = [&](auto, auto, auto, auto) {
assert(false && "Slang should have already reported all errors");
};
slang::SFormat::parse(format, onText, onArg, onError);
slang::ast::SFormat::parse(format, onText, onArg, onError);
return failure(anyFailure);
}

Expand Down Expand Up @@ -191,7 +191,7 @@ struct FormatStringParser {
} // namespace

FailureOr<Value> Context::convertFormatString(
slang::span<const slang::ast::Expression *const> arguments, Location loc,
std::span<const slang::ast::Expression *const> arguments, Location loc,
IntFormat defaultFormat, bool appendNewline) {
FormatStringParser parser(*this, ArrayRef(arguments.data(), arguments.size()),
loc, defaultFormat);
Expand Down
Loading