Skip to content

Commit

Permalink
Add threadtest tool
Browse files Browse the repository at this point in the history
  • Loading branch information
MikePopoloski committed Feb 13, 2025
1 parent aac0ed2 commit 7bf61e4
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ set(DOXYGEN_EXAMPLE_DIR_LIST
"${PROJECT_SOURCE_DIR}/tools/netlist/README.md"
"${PROJECT_SOURCE_DIR}/tools/reflect/README.md"
"${PROJECT_SOURCE_DIR}/tools/rewriter/README.md"
"${PROJECT_SOURCE_DIR}/tools/threadtest/README.md"
"${PROJECT_SOURCE_DIR}/tools/tidy/README.md")
set(DOXYGEN_EXTERNAL_DIR "${PROJECT_SOURCE_DIR}/external")
set(DOXYGEN_STRIP_PATH "${DOXYGEN_INPUT_DIR}")
Expand Down
1 change: 1 addition & 0 deletions docs/tools.dox
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ well supported as the main slang library.
\include{doc} tools/netlist/README.md
\include{doc} tools/reflect/README.md
\include{doc} tools/rewriter/README.md
\include{doc} tools/threadtest/README.md
\include{doc} tools/tidy/README.md

*/
1 change: 1 addition & 0 deletions tools/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ add_subdirectory(hier)
add_subdirectory(netlist)
add_subdirectory(reflect)
add_subdirectory(rewriter)
add_subdirectory(threadtest)
add_subdirectory(tidy)
23 changes: 23 additions & 0 deletions tools/threadtest/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# ~~~
# SPDX-FileCopyrightText: Michael Popoloski
# SPDX-License-Identifier: MIT
# ~~~

add_executable(slang_threadtest threadtest.cpp)
add_executable(slang::threadtest ALIAS slang_threadtest)

target_link_libraries(slang_threadtest PRIVATE slang::slang)

set_target_properties(slang_threadtest PROPERTIES OUTPUT_NAME
"slang-threadtest")

if(CMAKE_SYSTEM_NAME MATCHES "Windows")
target_sources(slang_threadtest
PRIVATE ${PROJECT_SOURCE_DIR}/scripts/win32.manifest)
endif()

if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang" OR CMAKE_CXX_COMPILER_ID STREQUAL
"GNU")
target_compile_options(slang_threadtest PRIVATE "-fsanitize=thread")
target_link_libraries(slang_threadtest PRIVATE "-fsanitize=thread")
endif()
12 changes: 12 additions & 0 deletions tools/threadtest/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
slang-threadtest
================
A simple tool that allows testing the thread safety invariants of the AST.
The AST, once an initial visitation has occurred and all diagnostics are collected,
should then be able to be visited from multiple threads without issue. This tool
runs many such visitations across a design to see if any asserts fire.

Usage:

```
slang-threadtest [-n num_iterations] <all-other-slang-args>
```
70 changes: 70 additions & 0 deletions tools/threadtest/threadtest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
//------------------------------------------------------------------------------
// slang_main.cpp
// Testing tool for multithreaded AST visitation
//
// SPDX-FileCopyrightText: Michael Popoloski
// SPDX-License-Identifier: MIT
//------------------------------------------------------------------------------
#include "slang/ast/ASTSerializer.h"
#include "slang/ast/Compilation.h"
#include "slang/ast/symbols/CompilationUnitSymbols.h"
#include "slang/driver/Driver.h"
#include "slang/text/Json.h"
#include "slang/util/ThreadPool.h"

using namespace slang;
using namespace slang::ast;
using namespace slang::driver;

int main(int argc, char** argv) {
SLANG_TRY {
OS::setupConsole();
OS::tryEnableColors();

Driver driver;
driver.addStandardArgs();

std::optional<int> count;
driver.cmdLine.add("-n,--count", count, "Number of iterations to perform");

if (!driver.parseCommandLine(argc, argv) || !driver.processOptions() ||
!driver.parseAllSources()) {
return 1;
}

auto compilation = driver.createCompilation();
if (!driver.reportCompilation(*compilation, true))
return 2;

ThreadPool threadPool;
threadPool.pushLoop(0, count.value_or(1000), [&](int from, int to) {
SLANG_TRY {
JsonWriter writer;
ASTSerializer serializer(*compilation, writer);
serializer.setTryConstantFold(false);

serializer.startArray();
for (int i = from; i < to; i++)
serializer.serialize(compilation->getRoot());
serializer.endArray();
}
SLANG_CATCH(...) {
#if defined(SLANG_USE_CPPTRACE)
cpptrace::from_current_exception().print();
#endif
throw;
}
});

return 0;
}
SLANG_CATCH(const std::exception& e) {
#if __cpp_exceptions
OS::printE(fmt::format("{}\n", e.what()));
# if defined(SLANG_USE_CPPTRACE)
cpptrace::from_current_exception().print();
# endif
#endif
}
return 3;
}

0 comments on commit 7bf61e4

Please sign in to comment.