Skip to content

Commit fdc91a4

Browse files
Sean Purser-Haskellcopybara-github
authored andcommitted
Support for using and typedef on channels and memories.
PiperOrigin-RevId: 721522097
1 parent aa86d2d commit fdc91a4

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

xls/contrib/xlscc/translate_io.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,11 @@ absl::StatusOr<bool> Translator::TypeIsChannel(clang::QualType param,
225225
}
226226
}
227227

228+
if (type->getTypeClass() == clang::Type::TypeClass::Typedef) {
229+
return TypeIsChannel(
230+
clang::QualType(type->getUnqualifiedDesugaredType(), 0), loc);
231+
}
232+
228233
if (auto record = clang::dyn_cast<const clang::RecordType>(type)) {
229234
clang::RecordDecl* decl = record->getDecl();
230235

@@ -283,6 +288,12 @@ absl::StatusOr<std::shared_ptr<CChannelType>> Translator::GetChannelType(
283288
if (template_spec->isTypeAlias()) {
284289
return GetChannelType(template_spec->getAliasedType(), ctx, loc);
285290
}
291+
} else if (auto typedef_type = clang::dyn_cast<const clang::TypedefType>(
292+
stripped.base.getTypePtr());
293+
typedef_type != nullptr) {
294+
const clang::Type* type = stripped.base.getTypePtr();
295+
return GetChannelType(
296+
clang::QualType(type->getUnqualifiedDesugaredType(), 0), ctx, loc);
286297
} else if (auto record = clang::dyn_cast<const clang::RecordType>(
287298
stripped.base.getTypePtr());
288299
record != nullptr) {

xls/contrib/xlscc/unit_tests/translator_memory_test.cc

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1378,5 +1378,86 @@ TEST_F(TranslatorMemoryTest, Size) {
13781378
content);
13791379
}
13801380

1381+
TEST_F(TranslatorMemoryTest, PassMemoryAliased) {
1382+
const std::string content = R"(
1383+
using StoreType = __xls_memory<int, 32>;
1384+
1385+
void sub(StoreType& memory,
1386+
__xls_channel<int>& out,
1387+
int addr) {
1388+
const int val = memory[addr];
1389+
out.write(3*val);
1390+
}
1391+
1392+
#pragma hls_top
1393+
void my_package(__xls_channel<int>& in,
1394+
StoreType& memory,
1395+
__xls_channel<int>& out) {
1396+
const int addr = in.read();
1397+
sub(memory, out, addr);
1398+
})";
1399+
1400+
IOTest(
1401+
content,
1402+
/*inputs=*/{IOOpTest("in", 7, true), IOOpTest("memory__read", 10, true)},
1403+
/*outputs=*/
1404+
{IOOpTest("memory__read", xls::Value(xls::UBits(7, 5)), true),
1405+
IOOpTest("out", 30, true)});
1406+
}
1407+
1408+
TEST_F(TranslatorMemoryTest, PassMemoryTypedef) {
1409+
const std::string content = R"(
1410+
typedef __xls_memory<int, 32> StoreType;
1411+
1412+
void sub(StoreType& memory,
1413+
__xls_channel<int>& out,
1414+
int addr) {
1415+
const int val = memory[addr];
1416+
out.write(3*val);
1417+
}
1418+
1419+
#pragma hls_top
1420+
void my_package(__xls_channel<int>& in,
1421+
StoreType& memory,
1422+
__xls_channel<int>& out) {
1423+
const int addr = in.read();
1424+
sub(memory, out, addr);
1425+
})";
1426+
1427+
IOTest(
1428+
content,
1429+
/*inputs=*/{IOOpTest("in", 7, true), IOOpTest("memory__read", 10, true)},
1430+
/*outputs=*/
1431+
{IOOpTest("memory__read", xls::Value(xls::UBits(7, 5)), true),
1432+
IOOpTest("out", 30, true)});
1433+
}
1434+
1435+
TEST_F(TranslatorMemoryTest, PassChannelAliased) {
1436+
const std::string content = R"(
1437+
using ChannelType = __xls_channel<int>;
1438+
1439+
void sub(__xls_memory<int, 32>& memory,
1440+
ChannelType& out,
1441+
int addr) {
1442+
const int val = memory[addr];
1443+
out.write(3*val);
1444+
}
1445+
1446+
#pragma hls_top
1447+
void my_package(__xls_channel<int>& in,
1448+
__xls_memory<int, 32>& memory,
1449+
ChannelType& out) {
1450+
const int addr = in.read();
1451+
sub(memory, out, addr);
1452+
})";
1453+
1454+
IOTest(
1455+
content,
1456+
/*inputs=*/{IOOpTest("in", 7, true), IOOpTest("memory__read", 10, true)},
1457+
/*outputs=*/
1458+
{IOOpTest("memory__read", xls::Value(xls::UBits(7, 5)), true),
1459+
IOOpTest("out", 30, true)});
1460+
}
1461+
13811462
} // namespace
13821463
} // namespace xlscc

0 commit comments

Comments
 (0)