From 2b0f60163eecfd47cc7cb4cadc635336ddf83a24 Mon Sep 17 00:00:00 2001 From: Muxianesty Date: Thu, 15 Aug 2024 23:00:03 +0300 Subject: [PATCH 1/3] .dot output format support was added. --- README.md | 1 + config.json | 3 +- src/model/dfcxx/CMakeLists.txt | 2 +- src/model/dfcxx/include/dfcxx/kernel.h | 8 ++ src/model/dfcxx/include/dfcxx/typedefs.h | 1 + src/model/dfcxx/includeDev/dfcxx/converter.h | 1 + src/model/dfcxx/lib/dfcxx/CMakeLists.txt | 9 ++ src/model/dfcxx/lib/dfcxx/kernel.cpp | 108 ++++++++++++++++++- src/model/dfcxx/templates/dot.tpl | 15 +++ src/options.h | 6 ++ test/model/dfcxx/output_formats.cpp | 11 +- 11 files changed, 159 insertions(+), 6 deletions(-) create mode 100644 src/model/dfcxx/templates/dot.tpl diff --git a/README.md b/README.md index 4d5de9c..9f0453f 100644 --- a/README.md +++ b/README.md @@ -221,6 +221,7 @@ The list of arguments for `hls`-mode is presented below: * `--out-sv-lib `: *optional* filesystem-path option; used to specify the output SystemVerilog file for generated operations library. * `--out-dfcir `: *optional* filesystem-path option; used to specify the output DFCIR file. * `--out-firrtl `: *optional* filesystem-path option; used to specify the output FIRRTL file. +* `--out-dot `: *optional* filesystem-path option; used to specify the output .dot file. * `-a` or `-l`: *required* flag; used to specify the chosen scheduling strategy - either as-soon-as-possible or linear programming. **Exactly one of these flags has to be specified**. **At least one of the `out-*` options has to be specified.** diff --git a/config.json b/config.json index 4142592..4504a0d 100644 --- a/config.json +++ b/config.json @@ -6,6 +6,7 @@ "out_sv" : "", "out_sv_lib" : "", "out_dfcir" : "", - "out_firrtl" : "" + "out_firrtl" : "", + "out_dot" : "" } } diff --git a/src/model/dfcxx/CMakeLists.txt b/src/model/dfcxx/CMakeLists.txt index a3aac0a..fbe1eca 100644 --- a/src/model/dfcxx/CMakeLists.txt +++ b/src/model/dfcxx/CMakeLists.txt @@ -1,7 +1,7 @@ cmake_minimum_required(VERSION 3.20 FATAL_ERROR) project(DFCXX LANGUAGES C CXX) set(CMAKE_CXX_STANDARD 17) - +find_package(CTemplate REQUIRED COMPONENTS nothreads) add_subdirectory(include) add_subdirectory(includeDev) add_subdirectory(lib) diff --git a/src/model/dfcxx/include/dfcxx/kernel.h b/src/model/dfcxx/include/dfcxx/kernel.h index 1282ba6..d9e2c09 100644 --- a/src/model/dfcxx/include/dfcxx/kernel.h +++ b/src/model/dfcxx/include/dfcxx/kernel.h @@ -24,6 +24,12 @@ #include #include +// This forward declaration is needed to avoid +// users having to include LLVM headers. +namespace llvm { + class raw_fd_ostream; +} + namespace dfcxx { class DFCIRBuilder; @@ -36,6 +42,8 @@ class Kernel { TypeBuilder typeBuilder; VarBuilder varBuilder; Graph graph; + + bool compileDot(llvm::raw_fd_ostream *stream); protected: IO io; diff --git a/src/model/dfcxx/include/dfcxx/typedefs.h b/src/model/dfcxx/include/dfcxx/typedefs.h index 250add7..8cab1aa 100644 --- a/src/model/dfcxx/include/dfcxx/typedefs.h +++ b/src/model/dfcxx/include/dfcxx/typedefs.h @@ -65,6 +65,7 @@ enum class OutputFormatID : uint8_t { SVLibrary, DFCIR, FIRRTL, + Dot, // Utility value. Constains the number of elements in the enum. COUNT }; diff --git a/src/model/dfcxx/includeDev/dfcxx/converter.h b/src/model/dfcxx/includeDev/dfcxx/converter.h index c16bcc6..7162796 100644 --- a/src/model/dfcxx/includeDev/dfcxx/converter.h +++ b/src/model/dfcxx/includeDev/dfcxx/converter.h @@ -12,6 +12,7 @@ #include "dfcxx/typedefs.h" #include "dfcir/conversions/DFCIRPasses.h" +#include "llvm/Support/raw_ostream.h" #include "mlir/IR/BuiltinOps.h" #include diff --git a/src/model/dfcxx/lib/dfcxx/CMakeLists.txt b/src/model/dfcxx/lib/dfcxx/CMakeLists.txt index a82e35a..d8e1be7 100644 --- a/src/model/dfcxx/lib/dfcxx/CMakeLists.txt +++ b/src/model/dfcxx/lib/dfcxx/CMakeLists.txt @@ -40,6 +40,13 @@ target_include_directories(DFCXX $ ) +set(TEMPLATES_PATH "${PROJECT_SOURCE_DIR}/templates") + +add_compile_definitions( + TEMPLATES_PATH="${TEMPLATES_PATH}" + DOT_TEMPLATE_PATH="${TEMPLATES_PATH}/dot.tpl" +) + ## MLIRDFCIR is ensured to be compiled beforehand. target_link_libraries(DFCXX PRIVATE $ @@ -49,6 +56,8 @@ target_link_libraries(DFCXX PRIVATE $ PRIVATE $ PRIVATE $ + PRIVATE $ + PRIVATE $ ) add_library(Utopia::DFCXX ALIAS DFCXX) diff --git a/src/model/dfcxx/lib/dfcxx/kernel.cpp b/src/model/dfcxx/lib/dfcxx/kernel.cpp index b508fc0..cf7651c 100644 --- a/src/model/dfcxx/lib/dfcxx/kernel.cpp +++ b/src/model/dfcxx/lib/dfcxx/kernel.cpp @@ -9,8 +9,15 @@ #include "dfcxx/converter.h" #include "dfcxx/IRbuilders/builder.h" #include "dfcxx/kernel.h" +#include "dfcxx/vars/constant.h" +#include "ctemplate/template.h" +#include "llvm/Support/raw_ostream.h" + +#include #include +#include +#include namespace dfcxx { @@ -40,6 +47,94 @@ DFType Kernel::dfBool() { return DFType(storage.addType(type)); } +bool Kernel::compileDot(llvm::raw_fd_ostream *stream) { + using ctemplate::TemplateDictionary; + + uint64_t counter = 0; + std::unordered_map idMapping; + auto getName = [&idMapping, &counter] (const Node &node) -> std::string { + // If the node is named - just return the name. + auto name = DFVariable(node.var).getName(); + if (!name.empty()) { + return name.data(); + } + // If the mapping contains the node name - return it. + auto it = idMapping.find(node); + if (it != idMapping.end()) { + return it->second; + } + // Otherwise create and return the new node name mapping. + return (idMapping[node] = "node" + std::to_string(counter++)); + }; + + std::string result; + TemplateDictionary *dict = new TemplateDictionary("dot"); + dict->SetValue("KERNEL_NAME", this->getName().data()); + auto time = std::time(nullptr); + auto *localTime = std::localtime(&time); + dict->SetFormattedValue("GEN_TIME", + "%d-%d-%d %d:%d:%d", + localTime->tm_mday, + localTime->tm_mon + 1, + localTime->tm_year + 1900, + localTime->tm_hour, + localTime->tm_min, + localTime->tm_sec); + + for (Node node : graph.nodes) { + TemplateDictionary *elem = dict->AddSectionDictionary("ELEMENTS"); + auto name = getName(node); + elem->SetValue("NAME", name); + std::string shape = "box"; + std::string label = name; + auto data = node.data; + switch (node.type) { + case OFFSET: + shape = "diamond"; + label = std::to_string(data.offset); + break; + case IN: shape = "invtriangle"; break; + case OUT: shape = "triangle"; break; + case MUX: shape = "invtrapezium"; break; + case ADD: label = "+"; break; + case SUB: label = "-"; break; + case MUL: label = "*"; break; + case DIV: label = "/"; break; + case AND: label = "&"; break; + case OR: label = "|"; break; + case XOR: label = "^"; break; + case NOT: label = "!"; break; + case NEG: label = "-"; break; + case LESS: label = "<"; break; + case LESSEQ: label = "<="; break; + case GREATER: label = ">"; break; + case GREATEREQ: label = ">="; break; + case EQ: label = "=="; break; + case NEQ: label = "!="; break; + case SHL: label = "<< " + std::to_string(data.bitShift); break; + case SHR: label = ">> " + std::to_string(data.bitShift); break; + default: break; // Silences -Wswitch warning for "CONST". + } + + elem->SetValue("SHAPE", shape); + elem->SetValue("LABEL", label); + + unsigned i = 0; + for (Channel chan : graph.inputs[node]) { + TemplateDictionary *conn = elem->AddSectionDictionary("CONNECTIONS"); + conn->SetValue("SRC_NAME", getName(chan.source)); + conn->SetValue("TRG_NAME", name); + conn->SetValue("CON_LABEL", std::to_string(i++)); + } + } + + ctemplate::ExpandTemplate(DOT_TEMPLATE_PATH, ctemplate::DO_NOT_STRIP, + dict, &result); + delete dict; + + *stream << result; + return true; +} bool Kernel::compile(const DFLatencyConfig &config, const std::vector &outputPaths, @@ -55,9 +150,16 @@ bool Kernel::compile(const DFLatencyConfig &config, ? new llvm::raw_fd_ostream(outputPaths[i], ec) : nullptr; } - bool result = DFCIRConverter(config).convertAndPrint(compiled, - outputStreams, - sched); + bool result = true; + // Compile the kernel to .dot if such stream is specified. + if (auto *stream = outputStreams[OUT_FORMAT_ID_INT(Dot)]) { + result &= compileDot(stream); + } + if (result) { + result &= DFCIRConverter(config).convertAndPrint(compiled, + outputStreams, + sched); + } // Every created output stream has to be closed explicitly. for (llvm::raw_fd_ostream *stream : outputStreams) { if (stream) { diff --git a/src/model/dfcxx/templates/dot.tpl b/src/model/dfcxx/templates/dot.tpl new file mode 100644 index 0000000..8d4268a --- /dev/null +++ b/src/model/dfcxx/templates/dot.tpl @@ -0,0 +1,15 @@ +{{#LICENSE}} +{{!===---------------------------------------------------------------------===}} +{{! }} +{{! Part of the Utopia HLS Project, under the Apache License v2.0 }} +{{! SPDX-License-Identifier: Apache-2.0 }} +{{! Copyright 2024 ISP RAS (http://www.ispras.ru) }} +{{! }} +{{!===---------------------------------------------------------------------===}} +{{/LICENSE}}// This file has been automatically generated by Utopia HLS at {{GEN_TIME}}. +digraph {{KERNEL_NAME}} { + {{#ELEMENTS}}{{NAME}} [shape={{SHAPE}}, label="{{LABEL}}"] + {{#CONNECTIONS}}{{SRC_NAME}} -> {{TRG_NAME}} [label="{{CON_LABEL}}"] + {{/CONNECTIONS}} + {{/ELEMENTS}} +} diff --git a/src/options.h b/src/options.h index 1ad7e61..5693dcc 100644 --- a/src/options.h +++ b/src/options.h @@ -39,6 +39,7 @@ #define OUT_SV_LIB_JSON "out_sv_lib" #define OUT_DFCIR_JSON "out_dfcir" #define OUT_FIRRTL_JSON "out_firrtl" +#define OUT_DOT_JSON "out_dot" //===----------------------------------------------------------------------===// // CLI args/flags definitions @@ -53,6 +54,7 @@ #define OUT_SV_LIB_ARG CLI_ARG("out-sv-lib") #define OUT_DFCIR_ARG CLI_ARG("out-dfcir") #define OUT_FIRRTL_ARG CLI_ARG("out-firrtl") +#define OUT_DOT_ARG CLI_ARG("out-dot") //===----------------------------------------------------------------------===// @@ -195,6 +197,9 @@ struct HlsOptions final : public AppOptions { outputGroup->add_option(OUT_FIRRTL_ARG, outNames[OUT_FORMAT_ID_INT(FIRRTL)], "Path to output scheduled FIRRTL"); + outputGroup->add_option(OUT_DOT_ARG, + outNames[OUT_FORMAT_ID_INT(Dot)], + "Path to output a DFCxx kernel in .dot format."); outputGroup->require_option(); } @@ -206,6 +211,7 @@ struct HlsOptions final : public AppOptions { get(json, OUT_SV_LIB_JSON, outNames[OUT_FORMAT_ID_INT(SVLibrary)]); get(json, OUT_DFCIR_JSON, outNames[OUT_FORMAT_ID_INT(DFCIR)]); get(json, OUT_FIRRTL_JSON, outNames[OUT_FORMAT_ID_INT(FIRRTL)]); + get(json, OUT_DOT_JSON, outNames[OUT_FORMAT_ID_INT(Dot)]); } std::string latConfigFile; diff --git a/test/model/dfcxx/output_formats.cpp b/test/model/dfcxx/output_formats.cpp index 63c342c..f3b2667 100644 --- a/test/model/dfcxx/output_formats.cpp +++ b/test/model/dfcxx/output_formats.cpp @@ -47,13 +47,22 @@ TEST(DFCxxOutputFormats, FIRRTL) { EXPECT_EQ(kernel.compile(config, paths, dfcxx::ASAP), true); } +TEST(DFCxxOutputFormats, Dot) { + Polynomial2 kernel; + DFOutputPaths paths = { + {dfcxx::OutputFormatID::Dot, NULLDEVICE} + }; + EXPECT_EQ(kernel.compile(config, paths, dfcxx::ASAP), true); +} + TEST(DFCxxOutputFormats, All) { Polynomial2 kernel; DFOutputPaths paths = { {dfcxx::OutputFormatID::SystemVerilog, NULLDEVICE}, {dfcxx::OutputFormatID::SVLibrary, NULLDEVICE}, {dfcxx::OutputFormatID::DFCIR, NULLDEVICE}, - {dfcxx::OutputFormatID::FIRRTL, NULLDEVICE} + {dfcxx::OutputFormatID::FIRRTL, NULLDEVICE}, + {dfcxx::OutputFormatID::Dot, NULLDEVICE} }; EXPECT_EQ(kernel.compile(config, paths, dfcxx::ASAP), true); } From 1d14d673214a7d48e487b854e0e6682a13d8aa29 Mon Sep 17 00:00:00 2001 From: Muxianesty Date: Mon, 19 Aug 2024 13:17:29 +0300 Subject: [PATCH 2/3] .dot/Dot -> DOT . --- README.md | 2 +- src/model/dfcxx/include/dfcxx/typedefs.h | 2 +- src/model/dfcxx/lib/dfcxx/kernel.cpp | 4 ++-- src/options.h | 6 +++--- test/model/dfcxx/output_formats.cpp | 6 +++--- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 9f0453f..6913873 100644 --- a/README.md +++ b/README.md @@ -221,7 +221,7 @@ The list of arguments for `hls`-mode is presented below: * `--out-sv-lib `: *optional* filesystem-path option; used to specify the output SystemVerilog file for generated operations library. * `--out-dfcir `: *optional* filesystem-path option; used to specify the output DFCIR file. * `--out-firrtl `: *optional* filesystem-path option; used to specify the output FIRRTL file. -* `--out-dot `: *optional* filesystem-path option; used to specify the output .dot file. +* `--out-dot `: *optional* filesystem-path option; used to specify the output DOT file. * `-a` or `-l`: *required* flag; used to specify the chosen scheduling strategy - either as-soon-as-possible or linear programming. **Exactly one of these flags has to be specified**. **At least one of the `out-*` options has to be specified.** diff --git a/src/model/dfcxx/include/dfcxx/typedefs.h b/src/model/dfcxx/include/dfcxx/typedefs.h index 8cab1aa..2ac86a1 100644 --- a/src/model/dfcxx/include/dfcxx/typedefs.h +++ b/src/model/dfcxx/include/dfcxx/typedefs.h @@ -65,7 +65,7 @@ enum class OutputFormatID : uint8_t { SVLibrary, DFCIR, FIRRTL, - Dot, + DOT, // Utility value. Constains the number of elements in the enum. COUNT }; diff --git a/src/model/dfcxx/lib/dfcxx/kernel.cpp b/src/model/dfcxx/lib/dfcxx/kernel.cpp index cf7651c..914356c 100644 --- a/src/model/dfcxx/lib/dfcxx/kernel.cpp +++ b/src/model/dfcxx/lib/dfcxx/kernel.cpp @@ -151,8 +151,8 @@ bool Kernel::compile(const DFLatencyConfig &config, : nullptr; } bool result = true; - // Compile the kernel to .dot if such stream is specified. - if (auto *stream = outputStreams[OUT_FORMAT_ID_INT(Dot)]) { + // Compile the kernel to DOT if such stream is specified. + if (auto *stream = outputStreams[OUT_FORMAT_ID_INT(DOT)]) { result &= compileDot(stream); } if (result) { diff --git a/src/options.h b/src/options.h index 5693dcc..e762926 100644 --- a/src/options.h +++ b/src/options.h @@ -198,8 +198,8 @@ struct HlsOptions final : public AppOptions { outNames[OUT_FORMAT_ID_INT(FIRRTL)], "Path to output scheduled FIRRTL"); outputGroup->add_option(OUT_DOT_ARG, - outNames[OUT_FORMAT_ID_INT(Dot)], - "Path to output a DFCxx kernel in .dot format."); + outNames[OUT_FORMAT_ID_INT(DOT)], + "Path to output a DFCxx kernel in DOT format."); outputGroup->require_option(); } @@ -211,7 +211,7 @@ struct HlsOptions final : public AppOptions { get(json, OUT_SV_LIB_JSON, outNames[OUT_FORMAT_ID_INT(SVLibrary)]); get(json, OUT_DFCIR_JSON, outNames[OUT_FORMAT_ID_INT(DFCIR)]); get(json, OUT_FIRRTL_JSON, outNames[OUT_FORMAT_ID_INT(FIRRTL)]); - get(json, OUT_DOT_JSON, outNames[OUT_FORMAT_ID_INT(Dot)]); + get(json, OUT_DOT_JSON, outNames[OUT_FORMAT_ID_INT(DOT)]); } std::string latConfigFile; diff --git a/test/model/dfcxx/output_formats.cpp b/test/model/dfcxx/output_formats.cpp index f3b2667..337af74 100644 --- a/test/model/dfcxx/output_formats.cpp +++ b/test/model/dfcxx/output_formats.cpp @@ -47,10 +47,10 @@ TEST(DFCxxOutputFormats, FIRRTL) { EXPECT_EQ(kernel.compile(config, paths, dfcxx::ASAP), true); } -TEST(DFCxxOutputFormats, Dot) { +TEST(DFCxxOutputFormats, DOT) { Polynomial2 kernel; DFOutputPaths paths = { - {dfcxx::OutputFormatID::Dot, NULLDEVICE} + {dfcxx::OutputFormatID::DOT, NULLDEVICE} }; EXPECT_EQ(kernel.compile(config, paths, dfcxx::ASAP), true); } @@ -62,7 +62,7 @@ TEST(DFCxxOutputFormats, All) { {dfcxx::OutputFormatID::SVLibrary, NULLDEVICE}, {dfcxx::OutputFormatID::DFCIR, NULLDEVICE}, {dfcxx::OutputFormatID::FIRRTL, NULLDEVICE}, - {dfcxx::OutputFormatID::Dot, NULLDEVICE} + {dfcxx::OutputFormatID::DOT, NULLDEVICE} }; EXPECT_EQ(kernel.compile(config, paths, dfcxx::ASAP), true); } From 186a5e6a25341fd8f357f2e4feb8d30c86043d13 Mon Sep 17 00:00:00 2001 From: Muxianesty Date: Mon, 19 Aug 2024 14:43:46 +0300 Subject: [PATCH 3/3] Lexicographical include order was fixed. --- src/model/dfcxx/lib/dfcxx/kernel.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/model/dfcxx/lib/dfcxx/kernel.cpp b/src/model/dfcxx/lib/dfcxx/kernel.cpp index 914356c..fe81172 100644 --- a/src/model/dfcxx/lib/dfcxx/kernel.cpp +++ b/src/model/dfcxx/lib/dfcxx/kernel.cpp @@ -16,8 +16,8 @@ #include #include -#include #include +#include namespace dfcxx {