Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions include/circt/Dialect/HW/InnerSymbolTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ class InnerSymbolTable {
InnerSymbolTable(InnerSymbolTable &&) = default;
InnerSymbolTable &operator=(InnerSymbolTable &&) = default;

// Add a fresh mapping, or fail if the symbol name already exists.
Copy link
Contributor

Choose a reason for hiding this comment

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

I feel like this isn't a thing, but should it be noted that this generates a diagnostic for the failure case?

LogicalResult add(StringAttr name, const InnerSymTarget &target);

/// Look up a symbol with the specified name, returning empty InnerSymTarget
/// if no such name exists. Names never include the @ on them.
InnerSymTarget lookup(StringRef name) const;
Expand Down
30 changes: 21 additions & 9 deletions lib/Dialect/HW/InnerSymbolTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,21 @@ InnerSymbolTable::InnerSymbolTable(Operation *op) {
});
}

template <typename TableTy>
static LogicalResult tryAddSymbol(TableTy &table, StringAttr name,
const InnerSymTarget &target) {

auto it = table.try_emplace(name, target);
if (it.second)
return success();
auto existing = it.first->second;
return target.getOp()
->emitError()
.append("redefinition of inner symbol named '", name.strref(), "'")
.attachNote(existing.getOp()->getLoc())
.append("see existing inner symbol definition here");
}

FailureOr<InnerSymbolTable> InnerSymbolTable::get(Operation *op) {
assert(op);
if (!op->hasTrait<OpTrait::InnerSymbolTable>())
Expand All @@ -44,21 +59,18 @@ FailureOr<InnerSymbolTable> InnerSymbolTable::get(Operation *op) {
TableTy table;
auto result = walkSymbols(
op, [&](StringAttr name, const InnerSymTarget &target) -> LogicalResult {
auto it = table.try_emplace(name, target);
if (it.second)
return success();
auto existing = it.first->second;
return target.getOp()
->emitError()
.append("redefinition of inner symbol named '", name.strref(), "'")
.attachNote(existing.getOp()->getLoc())
.append("see existing inner symbol definition here");
return tryAddSymbol<TableTy>(table, name, target);
});
if (failed(result))
return failure();
return InnerSymbolTable(op, std::move(table));
}

LogicalResult InnerSymbolTable::add(StringAttr name,
const InnerSymTarget &target) {
return tryAddSymbol<TableTy>(symbolTable, name, target);
}

LogicalResult InnerSymbolTable::walkSymbols(Operation *op,
InnerSymCallbackFn callback) {
auto walkSym = [&](StringAttr name, const InnerSymTarget &target) {
Expand Down
1 change: 1 addition & 0 deletions unittests/Dialect/HW/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
add_circt_unittest(CIRCTHWTests
GraphFixture.cpp
HWModuleTest.cpp
InnerSymbolTableTest.cpp
InstanceGraphTest.cpp
InstancePathTest.cpp
MaterializerTest.cpp
Expand Down
84 changes: 84 additions & 0 deletions unittests/Dialect/HW/InnerSymbolTableTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
//===- InnerSymbolTableTest.cpp - HW inner symbol table tests -------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice! Thanks for adding this! 💯


#include "circt/Dialect/HW/InnerSymbolTable.h"
#include "circt/Dialect/HW/HWDialect.h"
#include "mlir/IR/BuiltinOps.h"
#include "mlir/Parser/Parser.h"
#include "gtest/gtest.h"

using namespace mlir;
using namespace circt;
using namespace hw;

namespace {

constexpr StringLiteral testModuleString = R"mlir(
hw.module @foo(in %in : i1 {hw.exportPort = #hw<innerSym@port0>}) {
%wire0 = hw.wire %in sym @wire0 : i1
%wire1 = hw.wire %in : i1
hw.output
}
)mlir";

TEST(InnerSymbolTableTest, Create) {
MLIRContext context;
context.loadDialect<HWDialect>();

Block block;
LogicalResult parseResult =
parseSourceString(testModuleString, &block, &context);

ASSERT_TRUE(succeeded(parseResult));

Operation *testOp = &block.front();

InnerSymbolTable innerSymbolTable(testOp);

ASSERT_TRUE(innerSymbolTable.lookup("port0"));
ASSERT_TRUE(innerSymbolTable.lookup("wire0"));
}

TEST(InnerSymbolTableTest, Add) {
MLIRContext context;
context.loadDialect<HWDialect>();

Block block;
LogicalResult parseResult =
parseSourceString(testModuleString, &block, &context);

ASSERT_TRUE(succeeded(parseResult));

Operation *testOp = &block.front();

InnerSymbolTable innerSymbolTable(testOp);

auto name = StringAttr::get(&context, "wire1");

ASSERT_FALSE(innerSymbolTable.lookup(name));

Operation *wire1 = testOp->getRegions().front().front().front().getNextNode();

auto innerSymTarget = InnerSymTarget{wire1};

LogicalResult result1 = innerSymbolTable.add(name, innerSymTarget);
Copy link
Contributor

Choose a reason for hiding this comment

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

Probably we want to update wire1 so that it actually has the inner symbol?

Otherwise, constructing a new inner symbol table over testOp wouldn't have the new symbol.

I could see add defensively checking that this is the case (on debug builds)?

Related: The way to get a reasonable new inner symbol name is using ISN (InnerSymbolNamespace), or utilities like those in FIRRTLUtils.h (that use InnerSymbolNamespace). It seems a shame to not have those not capable of updating the IST should that be desired.

The tentative plan was to unify IST and ISN, so maybe not worth this until then, and users manually update their IST as desired?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Maybe I should just put effort into unifying IST and ISN... I don't need to get this in before that


ASSERT_TRUE(succeeded(result1));
ASSERT_TRUE(innerSymbolTable.lookup(name));

context.getDiagEngine().registerHandler([&](Diagnostic &diag) {
ASSERT_EQ(diag.getSeverity(), DiagnosticSeverity::Error);
ASSERT_EQ(diag.str(), "redefinition of inner symbol named 'wire1'");
});

LogicalResult result2 = innerSymbolTable.add(name, innerSymTarget);

ASSERT_FALSE(succeeded(result2));
}

} // namespace