Skip to content
Closed
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
7 changes: 6 additions & 1 deletion compiler/cpp/circt_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,11 @@ mlir::StringAttr StringToStringAttr(const std::string &str)
return mlir::StringAttr::get(g_compiler->GetMlirContext(), str);
}

mlir::StringAttr ClampedSymAttr(const std::string &str)
{
return StringToStringAttr(g_compiler->ClampStringLength(str));
}

mlir::IntegerType GetIntegerType(const size_t width, mlir::IntegerType::SignednessSemantics signedness)
{
return mlir::IntegerType::get(g_compiler->GetMlirContext(), width, signedness);
Expand Down Expand Up @@ -587,7 +592,7 @@ mlir::StringAttr GetFullyQualifiedStringAttr(const ObjectPath &containerPath, co
ObjectPath pathWithField = containerPath;
pathWithField.push_back("__field__" + fieldName);

return StringToStringAttr(FixupStringCirct(SerializePath(pathWithField, '_')));
return StringToStringAttr(g_compiler->ClampStringLength(FixupStringCirct(SerializePath(pathWithField, '_'))));
}

circt::hw::InnerSymAttr GetFullyQualifiedInnerSymAttr(const ObjectPath &containerPath, const std::string &fieldName)
Expand Down
2 changes: 2 additions & 0 deletions compiler/cpp/circt_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ mlir::Value LiteralToValue(const Literal& l, circt::OpBuilder& opb, const mlir::

mlir::StringAttr StringToStringAttr(const std::string& str);

mlir::StringAttr ClampedSymAttr(const std::string& str);

size_t GetMlirTypeWidth(const mlir::Type& type);

size_t GetMlirValueWidth(const mlir::Value& v);
Expand Down
44 changes: 41 additions & 3 deletions compiler/cpp/compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1896,12 +1896,49 @@ void Compiler::ReorderDeclarations()

std::string Compiler::ClampStringLength(const std::string& stringIn)
{
std::string result = stringIn;

const size_t limit = GetCodeGenConfig()._maxStringLength;

if (result.size() > limit)
if ((limit == 0) || (stringIn.size() <= limit))
{
return stringIn;
}

// Same input should always produce same output across passes (i.e. clamping from independent call sites)
if (const auto cached = _clampInputToOutput.find(stringIn); cached != _clampInputToOutput.end())
{
return cached->second;
}

std::string result = stringIn;

{
// If input already clamped, return a no-op. Avoids generating different strings for the same input on different passes
if (const size_t underscore = stringIn.rfind('_'); underscore != std::string::npos)
{
const std::string suffix = stringIn.substr(underscore + 1);
if (!suffix.empty() && std::all_of(suffix.begin(), suffix.end(), [](char c) { return std::isdigit(static_cast<unsigned char>(c)); }))
{
try
{
const size_t parsedHash = std::stoull(suffix);
const auto existing = _clampStringMap.find(parsedHash);
if (existing != _clampStringMap.end())
{
std::ostringstream expected;
expected << existing->second.substr(0, limit) << "_" << parsedHash;
if (expected.str() == stringIn)
{
return stringIn;
}
}
}
catch (const std::exception&)
{
// not a hash suffix; fall through
}
}
}

// Compute a hash code of the full string
std::hash<std::string> hasher;

Expand Down Expand Up @@ -1931,6 +1968,7 @@ std::string Compiler::ClampStringLength(const std::string& stringIn)
result = str.str();
}

_clampInputToOutput.emplace(stringIn, result);
return result;
}

Expand Down
1 change: 1 addition & 0 deletions compiler/cpp/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ class Compiler

// Maps hash to long string
std::map<size_t, std::string> _clampStringMap;
std::unordered_map<std::string, std::string> _clampInputToOutput;

std::map<std::string, ObjectPath> _nameToPath;

Expand Down
33 changes: 22 additions & 11 deletions compiler/cpp/verilog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,9 @@ std::string GetRegisterBaseName(const Program &program, const size_t registerInd
return prefix + std::to_string(registerIndex) + "_" + regDesc._name;
}

std::string GetBasicBlockInstanceName(const BasicBlock &basicBlock) { return GetBasicBlockName(basicBlock) + "Impl"; }
std::string GetBasicBlockInstanceName(const BasicBlock &basicBlock) {
return g_compiler->ClampStringLength(GetBasicBlockName(basicBlock) + "Impl");
}

class VerilogCompiler;

Expand Down Expand Up @@ -1641,8 +1643,11 @@ class VerilogCompiler
const mlir::Location location = RegDescToLocation(regDesc);

circt::kanagawa::ContainerInstanceOp::create(opb,
location, circt::hw::InnerSymAttr::get(StringToStringAttr(containerInstancePath.back())),
circt::hw::InnerRefAttr::get(StringToStringAttr(GetCirctDesignName()), leafContainerNameAttr));
location,
circt::hw::InnerSymAttr::get(ClampedSymAttr(containerInstancePath.back())),
circt::hw::InnerRefAttr::get(
StringToStringAttr(g_compiler->ClampStringLength(GetCirctDesignName())),
StringToStringAttr(g_compiler->ClampStringLength(leafContainerNameAttr.getValue().str()))));

// Write clock and reset ports
const auto writeInputPort =
Expand Down Expand Up @@ -2810,8 +2815,11 @@ class VerilogCompiler
opb.setInsertionPointToEnd(parentContainer.getBodyBlock());

circt::kanagawa::ContainerInstanceOp::create(opb,
location, circt::hw::InnerSymAttr::get(StringToStringAttr(containerInstancePath.back())),
circt::hw::InnerRefAttr::get(StringToStringAttr(GetCirctDesignName()), leafContainerNameAttr));
location,
circt::hw::InnerSymAttr::get(ClampedSymAttr(containerInstancePath.back())),
circt::hw::InnerRefAttr::get(
StringToStringAttr(g_compiler->ClampStringLength(GetCirctDesignName())),
StringToStringAttr(g_compiler->ClampStringLength(leafContainerNameAttr.getValue().str()))));

// Write clock and reset ports
const auto writeInputPort =
Expand Down Expand Up @@ -5621,7 +5629,7 @@ class VerilogCompiler

ModuleInstanceHelper instance(*this, LocationToCirctLocation(basicBlock._location));

instance.SetModuleName(GetModuleNamePrefix() + GetBasicBlockName(basicBlock));
instance.SetModuleName(g_compiler->ClampStringLength(GetModuleNamePrefix() + GetBasicBlockName(basicBlock)));
instance.SetInstanceName(GetBasicBlockInstanceName(basicBlock));

instance.AddPort("clk", circt::hw::ModulePort::Direction::Input, GetClockType(), "clk");
Expand Down Expand Up @@ -7635,7 +7643,7 @@ class VerilogCompiler
const std::set<size_t> acquiredSemaphores = GetAcquiredSemaphores(basicBlock);
const std::set<size_t> releasedSemaphores = GetReleasedSemaphores(basicBlock);

const std::string fullModuleName = GetModuleNamePrefix() + GetBasicBlockName(basicBlock);
const std::string fullModuleName = g_compiler->ClampStringLength(GetModuleNamePrefix() + GetBasicBlockName(basicBlock));

JsonValue jsonBasicBlock = JsonValue::CreateObject();
JsonValue jsonPorts = JsonValue::CreateArray();
Expand Down Expand Up @@ -11426,7 +11434,7 @@ class VerilogCompiler
basicBlockPorts.push_back(pi._portInfo);
}

const std::string moduleName = GetModuleNamePrefix() + GetBasicBlockName(basicBlock);
const std::string moduleName = g_compiler->ClampStringLength(GetModuleNamePrefix() + GetBasicBlockName(basicBlock));

_compileContext._hwModule =
circt::hw::HWModuleOp::create(opb, mlirBbLocation, opb.getStringAttr(moduleName), basicBlockPorts);
Expand Down Expand Up @@ -11999,8 +12007,11 @@ class VerilogCompiler
opb.setInsertionPointToEnd(parentContainer.getBodyBlock());

circt::kanagawa::ContainerInstanceOp instance = circt::kanagawa::ContainerInstanceOp::create(opb,
GetUnknownLocation(), circt::hw::InnerSymAttr::get(StringToStringAttr(path.back())),
circt::hw::InnerRefAttr::get(StringToStringAttr(GetCirctDesignName()), containerNameAttr));
GetUnknownLocation(),
circt::hw::InnerSymAttr::get(ClampedSymAttr(path.back())),
circt::hw::InnerRefAttr::get(
StringToStringAttr(g_compiler->ClampStringLength(GetCirctDesignName())),
StringToStringAttr(g_compiler->ClampStringLength(containerNameAttr.getValue().str()))));

SafeInsert(_pathToContainerInstance, path, ContainerAndInstance(container, instance));
}
Expand Down Expand Up @@ -12090,7 +12101,7 @@ class VerilogCompiler

std::string GenericContainerName(const ObjectPath &path)
{
return path.empty() ? _coreModule->Name() : FixupString(SerializePath(path));
return path.empty() ? _coreModule->Name() : g_compiler->ClampStringLength(FixupString(SerializePath(path)));
}
};

Expand Down
Loading