Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@gtxzsxxk
Copy link
Contributor

@gtxzsxxk gtxzsxxk commented Dec 2, 2024

This commit refactors the Python script for generating C++ headers by replacing hard-coded print statements with Jinja templates. The template is now independently defined within the script, improving code readability and maintainability. The output remains consistent with the previous version.

The bare statements are also refactored into several functions. They are put into the if __name__ == "__main__": block to avoid being executed when imported by other modules.

A test has been run to see that this change does not make the output differ. Selected test sources include simple circuits, existing circuits in the arcilator's tests, and risc-v mini. The circt source code does not have a test for this Python script, so this test is not provided in the committed files.

#include <gtest/gtest.h>
#include <iostream>
#include <filesystem>
#include <vector>
#include <cstdio>

static const std::string arcNewCmd = "python3 ../tests/arcilator-header-cpp.py";
static const std::string arcOldCmd = "python3 ../tests/ahc_old.py";
static const std::string viewDepth = "--view-depth 1";

std::string executeCommand(const std::string &command) {
    std::array<char, 256> buffer{};
    std::string result{};

    std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(command.c_str(), "r"), pclose);
    if (!pipe) {
        throw std::runtime_error("popen() failed!");
    }

    while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) {
        result += std::string{buffer.data()};
    }
    return result;
}

std::pair<std::string, std::string> doNewOldTest(const std::string &newCmd, const std::string &oldCmd) {
    auto retNew = executeCommand(newCmd);
    auto retOld = executeCommand(oldCmd);

    return {retNew, retOld};
}

void doTest(const std::string &testFile) {
    auto res = doNewOldTest(arcNewCmd + " " + testFile + " " + viewDepth, arcOldCmd + " " + testFile + " " + viewDepth);
    EXPECT_EQ(res.first, res.second);
}

TEST(ArcilatorHeaderCppTests, MuxTestHeader) {
    const auto testFile = "../tests/mlir/mux_test.json";
    doTest(testFile);
}

TEST(ArcilatorHeaderCppTests, ArcilatorOfficialTestHeader) {
    const auto testFile = "../tests/mlir/arcilator.json";
    doTest(testFile);
}

TEST(ArcilatorHeaderCppTests, DecoupledGCDHeader) {
    const auto testFile = "../tests/mlir/decoupledGCD.json";
    doTest(testFile);
}

TEST(ArcilatorHeaderCppTests, RISCVMiniHeader) {
    const auto testFile = "../tests/mlir/riscv-mini.json";
    doTest(testFile);
}

@gtxzsxxk gtxzsxxk force-pushed the arcilator-header-cpp-refactor branch from 5a225c0 to 857bf78 Compare December 2, 2024 10:38
@fabianschuiki
Copy link
Contributor

This makes a lot of sense to me, and improves readability. Thanks for working on this! Could you check if the Rocket core in the github.com/circt/arc-tests repository still works with your changes, as a sanity check? (make -C rocket run in the arc-tests repository)

@gtxzsxxk
Copy link
Contributor Author

gtxzsxxk commented Dec 4, 2024

Hi, thanks for your review and suggestions! I have run the rocket core arc-tests (small-master, medium-master, and dual-medium) as requested, replacing the arcilator-header-cpp.py with my version. The tests have all passed. In addition, I compared the outputs of my version with the previous version. They are all identical.

And I noticed some formatting issues in my first commit and have amended them with a force push to keep the history clean.

Copy link
Contributor

@fabianschuiki fabianschuiki left a comment

Choose a reason for hiding this comment

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

Very cool, thanks a lot for trying that out! Looks good to me.

Feel free to obtain commit access if you'd like to land the PR yourself, or let me know if you want me to merge it πŸ™‚

@gtxzsxxk
Copy link
Contributor Author

gtxzsxxk commented Dec 4, 2024

Hi,

Thanks! I really appreciate your support. I would be grateful if you could merge the PR for me this time.

Thanks again for your help!

Best regards

@fabianschuiki
Copy link
Contributor

Thanks! Merging this now πŸ™‚

@fabianschuiki fabianschuiki merged commit 8cd1a5f into llvm:main Dec 4, 2024
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants