Skip to content

Commit

Permalink
[xlscc] Add hls_block pragma and attribute for multi-block designs
Browse files Browse the repository at this point in the history
Beginning of ping-pong implementation.

PiperOrigin-RevId: 698540382
  • Loading branch information
Sean Purser-Haskell authored and copybara-github committed Nov 20, 2024
1 parent cfee6d5 commit 4f09df4
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
54 changes: 54 additions & 0 deletions xls/contrib/xlscc/cc_parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,42 @@ struct XlsTopAttrInfo : public clang::ParsedAttrInfo {
static clang::ParsedAttrInfoRegistry::Add<XlsTopAttrInfo> hls_top(
"hls_top", "Marks a function as the top function of an HLS block.");

struct XlsBlockAttrInfo : public clang::ParsedAttrInfo {
XlsBlockAttrInfo() {
// GNU-style __attribute__(("hls_block")) and C++/C23-style [[hls_block]]
// and [[xlscc::hls_block]] supported.
static constexpr Spelling S[] = {
{clang::ParsedAttr::AS_GNU, "hls_block"},
{clang::ParsedAttr::AS_C23, "hls_block"},
{clang::ParsedAttr::AS_CXX11, "hls_block"},
{clang::ParsedAttr::AS_CXX11, "xlscc::hls_block"}};
Spellings = S;
}

bool diagAppertainsToDecl(clang::Sema& S, const clang::ParsedAttr& Attr,
const clang::Decl* D) const override {
// This attribute appertains to functions only.
if (!isa<clang::FunctionDecl>(D)) {
S.Diag(Attr.getLoc(), clang::diag::warn_attribute_wrong_decl_type_str)
<< Attr << Attr.isRegularKeywordAttribute() << "functions";
return false;
}
return true;
}

AttrHandling handleDeclAttribute(
clang::Sema& S, clang::Decl* D,
const clang::ParsedAttr& Attr) const override {
// Attach an annotate attribute to the Decl.
D->addAttr(clang::AnnotateAttr::Create(S.Context, "hls_block", nullptr, 0,
Attr.getRange()));
return AttributeApplied;
}
};
static clang::ParsedAttrInfoRegistry::Add<XlsBlockAttrInfo> hls_block(
"hls_block",
"Marks a function its own HLS block, below the top in the hierarchy.");

struct XlsAllowDefaultPadAttrInfo : public clang::ParsedAttrInfo {
XlsAllowDefaultPadAttrInfo() {
// GNU-style __attribute__(("hls_array_allow_default_pad")) and
Expand Down Expand Up @@ -777,6 +813,24 @@ static clang::PragmaHandlerRegistry::Add<HlsTopPragmaHandler> hls_top_pragma(
"hls_top",
"Pragma to mark a function or method as the entrypoint for translation.");

class HlsBlockPragmaHandler : public HlsArgsPragmaHandler {
public:
explicit HlsBlockPragmaHandler()
: HlsArgsPragmaHandler("hls_block", /*num_args=*/0) {}

void HandlePragma(clang::Preprocessor& PP, clang::PragmaIntroducer Introducer,
clang::Token& firstToken,
const std::vector<clang::Token>& toks) override {
GenerateAnnotation(PP, clang::PragmaHandler::getName(), firstToken,
/*arguments=*/{});
}
};

static clang::PragmaHandlerRegistry::Add<HlsBlockPragmaHandler>
hls_block_pragma("hls_block",
"Pragma to mark a function or method as the entrypoint "
"for translation.");

class HlsAllowDefaultPadPragmaHandler : public HlsArgsPragmaHandler {
public:
explicit HlsAllowDefaultPadPragmaHandler()
Expand Down
28 changes: 28 additions & 0 deletions xls/contrib/xlscc/unit_tests/cc_parser_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "absl/status/status_matchers.h"
#include "absl/status/statusor.h"
#include "clang/include/clang/AST/Attr.h"
#include "clang/include/clang/AST/AttrIterator.h"
#include "clang/include/clang/AST/Attrs.inc"
#include "clang/include/clang/AST/Decl.h"
#include "clang/include/clang/AST/Expr.h"
Expand Down Expand Up @@ -270,6 +271,33 @@ TEST_F(CCParserTest, TopNotFound) {
absl_testing::StatusIs(absl::StatusCode::kNotFound));
}

TEST_F(CCParserTest, Block) {
xlscc::CCParser parser;

const std::string cpp_src = R"(
#pragma hls_block
int bar(int a, int b) {
const int foo = a + b;
return foo+1;
}
)";

XLS_ASSERT_OK(
ScanTempFileWithContent(cpp_src, {}, &parser, /*top_name=*/"bar"));
XLS_ASSERT_OK_AND_ASSIGN(const auto* top_ptr, parser.GetTopFunction());
ASSERT_NE(top_ptr, nullptr);
EXPECT_EQ(top_ptr->getNameAsString(), "bar");
top_ptr->specific_attrs<clang::AnnotateAttr>();

const clang::AttrVec& attrs = top_ptr->getAttrs();
ASSERT_EQ(attrs.size(), 1);
const clang::Attr* attr = attrs.data()[0];
const clang::AnnotateAttr* annotate =
llvm::dyn_cast<clang::AnnotateAttr>(attr);
ASSERT_NE(annotate, nullptr);
EXPECT_EQ(annotate->getAnnotation(), "hls_block");
}

TEST_F(CCParserTest, SourceMeta) {
xlscc::CCParser parser;

Expand Down

0 comments on commit 4f09df4

Please sign in to comment.