18#include "mlir/IR/Threading.h"
19#include "mlir/Support/FileUtilities.h"
20#include "mlir/Support/IndentedOstream.h"
21#include "llvm/Support/Debug.h"
22#include "llvm/Support/FileSystem.h"
23#include "llvm/Support/JSON.h"
24#include "llvm/Support/Path.h"
25#include "llvm/Support/ToolOutputFile.h"
27#define DEBUG_TYPE "di"
59 SmallVectorImpl<FileLineColLoc> &locs) {
60 if (
auto nameLoc = dyn_cast<NameLoc>(loc)) {
61 if (nameLoc.getName() ==
"emitted")
65 }
else if (
auto fusedLoc = dyn_cast<FusedLoc>(loc)) {
66 auto strAttr = dyn_cast_or_null<StringAttr>(fusedLoc.getMetadata());
67 if (strAttr && strAttr.getValue() ==
"verilogLocations")
70 for (
auto innerLoc : fusedLoc.getLocations())
72 }
else if (
auto fileLoc = dyn_cast<FileLineColLoc>(loc)) {
74 locs.push_back(fileLoc);
83 SmallVector<FileLineColLoc> locs;
87 for (
unsigned head = 0, end = locs.size(); head != end; ++head)
88 if (llvm::sys::fs::exists(locs[head].getFilename().getValue()))
89 locs[tail++] = locs[head];
93 if (!loc.getFilename().getValue().ends_with(
".fir"))
96 if (loc.getFilename().getValue().ends_with(
".fir"))
108 SmallString<128> buffer;
109 llvm::raw_svector_ostream(buffer) << x;
122 SmallVectorImpl<char> &relativePath) {
123 using namespace llvm::sys;
124 auto sourceIt = path::begin(path);
125 auto outputIt = path::begin(relativeTo);
126 auto sourceEnd = path::end(path);
127 auto outputEnd = path::end(relativeTo);
134 while (outputIt != outputEnd && sourceIt != sourceEnd &&
135 *outputIt == *sourceIt) {
149 for (; outputIt != outputEnd && *outputIt !=
".."; ++outputIt)
150 path::append(relativePath,
"..");
151 for (; sourceIt != sourceEnd; ++sourceIt)
152 path::append(relativePath, *sourceIt);
158 return outputIt == outputEnd;
171 llvm::StringMap<size_t> &nextGeneratedNameIDs) {
188 const EmitHGLDDOptions &options;
192 hw::HWSymbolCache symbolCache;
194 SmallDenseMap<DIModule *, StringRef> moduleNames;
197 llvm::StringMap<size_t> objectNamespace;
199 GlobalState(Operation *op,
const EmitHGLDDOptions &options)
200 : op(op), options(options), di(op) {
201 symbolCache.addDefinitions(op);
202 symbolCache.freeze();
209 SmallVector<int64_t, 1> packedDims;
210 SmallVector<int64_t, 1> unpackedDims;
212 EmittedType() =
default;
213 EmittedType(StringRef name) : name(name) {}
214 EmittedType(Type type) {
215 type = hw::getCanonicalType(type);
216 if (
auto handleType = sv::getHandleElementType(type))
217 type = hw::getCanonicalType(handleType);
218 if (hw::isHWIntegerType(type)) {
220 addPackedDim(hw::getBitWidth(type));
224 void addPackedDim(int64_t dim) { packedDims.push_back(dim); }
225 void addUnpackedDim(int64_t dim) { unpackedDims.push_back(dim); }
227 operator bool()
const {
return !name.empty(); }
229 bool operator==(
const EmittedType &other)
const {
230 return name == other.name && packedDims == other.packedDims &&
231 unpackedDims == other.unpackedDims;
233 bool operator!=(
const EmittedType &other)
const {
return !(*
this == other); }
235 static JArray emitDims(ArrayRef<int64_t> dims,
bool skipFirstLen1Dim) {
237 if (skipFirstLen1Dim && !dims.empty() && dims[0] == 1)
238 dims = dims.drop_front();
239 for (
auto dim : llvm::reverse(dims)) {
240 json.push_back(dim - 1);
245 JArray emitPackedDims()
const {
return emitDims(packedDims,
true); }
246 JArray emitUnpackedDims()
const {
return emitDims(unpackedDims,
false); }
253 operator bool()
const {
return expr !=
nullptr && type; }
256[[maybe_unused]]
static llvm::raw_ostream &
operator<<(llvm::raw_ostream &os,
257 const EmittedType &type) {
259 return os <<
"<null>";
261 for (
auto dim : type.packedDims)
262 os <<
'[' << dim <<
']';
263 if (!type.unpackedDims.empty()) {
265 for (
auto dim : type.unpackedDims)
266 os <<
'[' << dim <<
']';
271[[maybe_unused]]
static llvm::raw_ostream &
operator<<(llvm::raw_ostream &os,
272 const EmittedExpr &expr) {
274 return os <<
"<null>";
275 return os << expr.expr <<
" : " << expr.type;
282 FileEmitter(GlobalState &state, StringAttr hdlFile)
283 : state(state), hdlFile(hdlFile) {}
286 SmallVector<DIModule *> modules;
288 SmallString<64> outputFileName;
290 llvm::StringMap<size_t> moduleNamespace;
291 SmallMapVector<StringAttr, std::pair<StringAttr, unsigned>, 8> sourceFiles;
292 SmallMapVector<JValue, StringRef, 8> structDefs;
293 SmallString<128> structNameHint;
295 void emit(llvm::raw_ostream &os);
297 JValue emitLoc(FileLineColLoc loc, FileLineColLoc endLoc,
bool emitted);
300 void emitInstance(
JOStream &
json, DIInstance *instance);
301 void emitVariable(
JOStream &
json, DIVariable *variable);
302 EmittedExpr emitExpression(Value value);
304 unsigned getSourceFile(StringAttr sourceFile,
bool emitted);
307 return ::findBestLocation(loc, emitted, state.options.onlyExistingFileLocs);
312 void findAndEmitLoc(
JOStream &
json, StringRef fieldName, Location loc,
315 json.attribute(fieldName, emitLoc(fileLoc, {}, emitted));
321 void findAndEmitLocOrGuess(
JOStream &
json, StringRef fieldName, Operation *op,
324 json.attribute(fieldName, emitLoc(fileLoc, {}, emitted));
330 SmallMapVector<StringAttr, std::pair<SmallVector<FileLineColLoc>,
unsigned>,
333 op->walk([&](Operation *subop) {
336 locsByFile[fileLoc.getFilename()].first.push_back(fileLoc);
339 for (
auto ®ion : subop->getRegions())
340 for (
auto &block : region)
341 for (
auto arg : block.getArguments())
343 locsByFile[fileLoc.getFilename()].first.push_back(fileLoc);
347 for (
auto ®ion : op->getRegions())
348 for (
auto &block : region)
349 for (
auto arg : block.getArguments())
351 locsByFile[fileLoc.getFilename()].second += 10;
353 if (locsByFile.empty())
357 llvm::sort(locsByFile, [](
auto &a,
auto &b) {
358 return (a.second.first.size() + a.second.second) >
359 (b.second.first.size() + a.second.second);
362 auto &locs = locsByFile.front().second.first;
363 llvm::sort(locs, [](
auto &a,
auto &b) {
364 if (a.getLine() < b.getLine())
366 if (a.getLine() > b.getLine())
368 if (a.getColumn() < b.getColumn())
373 json.attribute(fieldName, emitLoc(locs.front(), locs.back(), emitted));
378 void findAndSetLocs(
JObject &into, Location loc) {
380 into[
"hgl_loc"] = emitLoc(fileLoc, {},
false);
382 into[
"hdl_loc"] = emitLoc(fileLoc, {},
true);
387 StringRef getModuleName(DIModule *module) {
388 auto name = state.moduleNames.lookup(module);
390 "moduleNames should contain a name for every module");
394 StringRef legalizeInModuleNamespace(StringRef name) {
403unsigned FileEmitter::getSourceFile(StringAttr sourceFile,
bool emitted) {
404 using namespace llvm::sys;
409 auto &slot = sourceFiles[sourceFile];
412 slot.second = sourceFiles.size();
415 if (path::is_absolute(sourceFile.getValue())) {
416 slot.first = sourceFile;
423 StringRef filePrefix =
424 emitted ? state.options.outputFilePrefix : state.options.sourceFilePrefix;
425 if (!filePrefix.empty()) {
426 SmallString<64> buffer = filePrefix;
427 path::append(buffer, sourceFile.getValue());
428 slot.first = StringAttr::get(sourceFile.getContext(), buffer);
437 SmallString<64> sourcePath = sourceFile.getValue();
438 path::remove_dots(sourcePath,
true);
442 StringRef relativeToDir = path::parent_path(outputFileName);
443 if (!path::is_absolute(outputFileName)) {
444 SmallString<64> buffer;
446 slot.first = StringAttr::get(sourceFile.getContext(), buffer);
457 SmallString<64> outputPath = relativeToDir;
458 fs::make_absolute(sourcePath);
459 fs::make_absolute(outputPath);
460 if (path::is_absolute(sourcePath) && path::is_absolute(outputPath)) {
461 auto firstSourceComponent = *path::begin(path::relative_path(sourcePath));
462 auto firstOutputComponent = *path::begin(path::relative_path(outputPath));
463 if (firstSourceComponent == firstOutputComponent) {
464 SmallString<64> buffer;
466 slot.first = StringAttr::get(sourceFile.getContext(), buffer);
473 slot.first = StringAttr::get(sourceFile.getContext(), sourcePath);
487 SmallVector<std::string, 16> rawObjects;
488 for (
auto *module : modules) {
489 llvm::raw_string_ostream objectOS(rawObjects.emplace_back());
491 objectJson.arrayBegin();
492 objectJson.arrayBegin();
493 emitModule(objectJson, module);
494 objectJson.arrayEnd();
495 objectJson.arrayEnd();
498 std::optional<unsigned> hdlFileIndex;
500 hdlFileIndex = getSourceFile(hdlFile,
true);
503 json.attributeObject(
"HGLDD", [&] {
504 json.attribute(
"version",
"1.0");
505 json.attributeArray(
"file_info", [&] {
506 for (
auto [key, fileAndId] : sourceFiles)
507 json.value(fileAndId.first.getValue());
510 json.attribute(
"hdl_file_index", *hdlFileIndex);
512 json.attributeArray(
"objects", [&] {
513 for (
auto &[structDef, name] : structDefs)
514 json.value(structDef);
515 for (
auto &rawObject : rawObjects) {
526 json.rawValue(StringRef(rawObject)
538JValue FileEmitter::emitLoc(FileLineColLoc loc, FileLineColLoc endLoc,
541 obj[
"file"] = getSourceFile(loc.getFilename(), emitted);
543 obj[
"begin_line"] = loc.getLine();
544 obj[
"end_line"] = loc.getLine();
546 if (loc.getColumn()) {
547 obj[
"begin_column"] = loc.getColumn();
548 obj[
"end_column"] = loc.getColumn();
551 if (endLoc.getLine())
552 obj[
"end_line"] = endLoc.getLine();
553 if (endLoc.getColumn())
554 obj[
"end_column"] = endLoc.getColumn();
560 if (
auto *op = module.
op)
561 if (
auto attr = op->getAttrOfType<StringAttr>(
"verilogName"))
567 if (
auto *op = inst.
op)
568 if (
auto attr = op->getAttrOfType<StringAttr>(
"hw.verilogName"))
575 moduleNamespace = state.objectNamespace;
576 structNameHint =
module->name.getValue();
578 json.attribute(
"kind",
"module");
579 json.attribute(
"obj_name", getModuleName(module));
580 json.attribute(
"module_name",
583 json.attribute(
"isExtModule", 1);
584 if (
auto *op = module->
op) {
585 findAndEmitLocOrGuess(
json,
"hgl_loc", op,
false);
586 findAndEmitLoc(
json,
"hdl_loc", op->getLoc(),
true);
588 emitModuleBody(
json, module);
594 json.attributeArray(
"port_vars", [&] {
596 emitVariable(
json, var);
598 json.attributeArray(
"children", [&] {
600 emitInstance(
json, instance);
609 auto instanceName = legalizeInModuleNamespace(instance->
name.getValue());
610 json.attribute(
"name", instanceName);
613 if (verilogName != instanceName)
614 json.attribute(
"hdl_obj_name", verilogName.getValue());
616 json.attribute(
"obj_name",
617 getModuleName(instance->
module));
618 json.attribute(
"module_name",
622 if (
auto *op = instance->
op) {
623 findAndEmitLoc(
json,
"hgl_loc", op->getLoc(),
false);
624 findAndEmitLoc(
json,
"hdl_loc", op->getLoc(),
true);
629 auto structNameHintLen = structNameHint.size();
631 structNameHint +=
'_';
632 structNameHint += instance->
module->
name.getValue();
633 }
else if (!instance->
name.empty()) {
634 structNameHint +=
'_';
635 structNameHint += instanceName;
638 structNameHint.resize(structNameHintLen);
647 auto variableName = legalizeInModuleNamespace(variable->
name.getValue());
648 json.attribute(
"var_name", variableName);
649 findAndEmitLoc(
json,
"hgl_loc", variable->
loc,
false);
650 findAndEmitLoc(
json,
"hdl_loc", variable->
loc,
true);
653 if (
auto value = variable->
value) {
654 auto structNameHintLen = structNameHint.size();
655 structNameHint +=
'_';
656 structNameHint += variableName;
657 emitted = emitExpression(value);
658 structNameHint.resize(structNameHintLen);
661 LLVM_DEBUG(llvm::dbgs() <<
"- " << variable->
name <<
": " << emitted <<
"\n");
663 json.attributeBegin(
"value");
664 json.rawValue([&](
auto &os) { os << emitted.expr; });
666 json.attribute(
"type_name", emitted.type.name);
667 if (
auto dims = emitted.type.emitPackedDims(); !dims.empty())
668 json.attribute(
"packed_range", std::move(dims));
669 if (
auto dims = emitted.type.emitUnpackedDims(); !dims.empty())
670 json.attribute(
"unpacked_range", std::move(dims));
677EmittedExpr FileEmitter::emitExpression(Value value) {
680 auto hglddSigName = [](StringRef sigName) ->
JObject {
681 return JObject{{
"sig_name", sigName}};
683 auto hglddOperator = [](StringRef opcode,
JValue args) ->
JObject {
686 {
"operands", std::move(args)},
689 auto hglddInt32 = [](uint32_t value) ->
JObject {
690 return JObject({{
"integer_num", value}});
693 if (
auto blockArg = dyn_cast<BlockArgument>(value)) {
694 auto module = dyn_cast<hw::HWModuleOp>(blockArg.getOwner()->getParentOp());
697 auto name =
module.getInputNameAttr(blockArg.getArgNumber());
700 return {hglddSigName(name), value.getType()};
703 auto result = cast<OpResult>(value);
704 auto *op = result.getOwner();
707 if (isa<hw::WireOp, sv::WireOp, sv::RegOp, sv::LogicOp, sv::VarOp>(op)) {
708 auto name = op->getAttrOfType<StringAttr>(
"hw.verilogName");
709 if (!name || name.empty())
710 name = op->getAttrOfType<StringAttr>(
"name");
711 if (name && !name.empty())
712 return {hglddSigName(name), result.getType()};
716 if (
auto constOp = dyn_cast<hw::ConstantOp>(op)) {
718 auto type = constOp.getType();
719 auto width = hw::getBitWidth(type);
725 return {
JObject({{
"bit_vector",
"0"}}),
726 IntegerType::get(op->getContext(), 1)};
729 SmallString<64> buffer;
730 buffer.reserve(width);
731 constOp.getValue().toStringUnsigned(buffer, 2);
736 std::reverse(buffer.begin(), buffer.end());
737 while (buffer.size() < (
size_t)width)
739 std::reverse(buffer.begin(), buffer.end());
740 assert(buffer.size() == (
size_t)width);
742 return {
JObject({{
"bit_vector", buffer}}), type};
747 if (
auto structOp = dyn_cast<debug::StructOp>(op)) {
749 auto structNameHintLen = structNameHint.size();
750 std::vector<JValue> values;
751 SmallVector<std::tuple<EmittedType, StringAttr, Location>> types;
752 for (
auto [nameAttr, field] :
753 llvm::zip(structOp.getNamesAttr(), structOp.getFields())) {
754 auto name = cast<StringAttr>(nameAttr);
755 structNameHint +=
'_';
756 structNameHint += name.getValue();
757 if (
auto value = emitExpression(field)) {
758 values.push_back(value.expr);
759 types.push_back({value.type, name, field.getLoc()});
761 structNameHint.resize(structNameHintLen);
766 return {hglddInt32(0), EmittedType(
"bit")};
770 llvm::StringMap<size_t> structNamespace;
771 for (
auto [type, name, loc] : types) {
773 fieldDef[
"var_name"] =
774 std::string(
legalizeName(name.getValue(), structNamespace));
775 fieldDef[
"type_name"] = type.name;
776 if (
auto dims = type.emitPackedDims(); !dims.empty())
777 fieldDef[
"packed_range"] = std::move(dims);
778 if (
auto dims = type.emitUnpackedDims(); !dims.empty())
779 fieldDef[
"unpacked_range"] = std::move(dims);
780 findAndSetLocs(fieldDef, loc);
781 fieldDefs.push_back(std::move(fieldDef));
783 auto structName =
legalizeName(structNameHint, state.objectNamespace);
785 structDef[
"kind"] =
"struct";
786 structDef[
"obj_name"] = structName;
787 structDef[
"port_vars"] = std::move(fieldDefs);
788 findAndSetLocs(structDef, structOp.getLoc());
790 StringRef structNameFinal =
791 structDefs.insert({std::move(structDef), structName}).first->second;
793 return {hglddOperator(
"'{", values), EmittedType(structNameFinal)};
797 if (
auto arrayOp = dyn_cast<debug::ArrayOp>(op)) {
798 std::vector<JValue> values;
800 for (
auto element : arrayOp.getElements()) {
801 if (
auto value = emitExpression(element)) {
802 values.push_back(value.expr);
803 if (type && type != value.type)
811 return {hglddInt32(0), EmittedType(
"bit")};
813 type.addUnpackedDim(values.size());
814 return {hglddOperator(
"'{", values), type};
818 if (
auto readOp = dyn_cast<sv::ReadInOutOp>(op))
819 return emitExpression(readOp.getInput());
823 StringRef unaryOpcode = TypeSwitch<Operation *, StringRef>(op)
824 .Case<comb::ParityOp>([](
auto) {
return "^"; })
825 .Default([](
auto) {
return ""; });
826 if (!unaryOpcode.empty() && op->getNumOperands() == 1) {
827 auto arg = emitExpression(op->getOperand(0));
830 return {hglddOperator(unaryOpcode,
JArray{arg.expr}), result.getType()};
833 StringRef binaryOpcode =
834 TypeSwitch<Operation *, StringRef>(op)
835 .Case<comb::AndOp>([](
auto) {
return "&"; })
836 .Case<comb::OrOp>([](
auto) {
return "|"; })
837 .Case<comb::XorOp>([](
auto) {
return "^"; })
838 .Case<comb::AddOp>([](
auto) {
return "+"; })
839 .Case<comb::SubOp>([](
auto) {
return "-"; })
840 .Case<comb::MulOp>([](
auto) {
return "*"; })
841 .Case<comb::DivUOp, comb::DivSOp>([](
auto) {
return "/"; })
842 .Case<comb::ModUOp, comb::ModSOp>([](
auto) {
return "%"; })
843 .Case<comb::ShlOp>([](
auto) {
return "<<"; })
844 .Case<comb::ShrUOp>([](
auto) {
return ">>"; })
845 .Case<comb::ShrSOp>([](
auto) {
return ">>>"; })
846 .Case<comb::ICmpOp>([](
auto cmpOp) -> StringRef {
847 switch (cmpOp.getPredicate()) {
848 case comb::ICmpPredicate::eq:
850 case comb::ICmpPredicate::ne:
852 case comb::ICmpPredicate::ceq:
854 case comb::ICmpPredicate::cne:
856 case comb::ICmpPredicate::weq:
858 case comb::ICmpPredicate::wne:
860 case comb::ICmpPredicate::ult:
861 case comb::ICmpPredicate::slt:
863 case comb::ICmpPredicate::ugt:
864 case comb::ICmpPredicate::sgt:
866 case comb::ICmpPredicate::ule:
867 case comb::ICmpPredicate::sle:
869 case comb::ICmpPredicate::uge:
870 case comb::ICmpPredicate::sge:
875 .Default([](
auto) {
return ""; });
876 if (!binaryOpcode.empty()) {
877 if (op->getNumOperands() != 2) {
878 op->emitOpError(
"must have two operands for HGLDD emission");
881 auto lhs = emitExpression(op->getOperand(0));
882 auto rhs = emitExpression(op->getOperand(1));
885 return {hglddOperator(binaryOpcode, {lhs.expr, rhs.expr}),
890 if (
auto concatOp = dyn_cast<comb::ConcatOp>(op)) {
891 std::vector<JValue> args;
892 for (
auto operand : concatOp.getOperands()) {
893 auto value = emitExpression(operand);
896 args.push_back(value.expr);
898 return {hglddOperator(
"{}", args), concatOp.getType()};
902 if (
auto replicateOp = dyn_cast<comb::ReplicateOp>(op)) {
903 auto arg = emitExpression(replicateOp.getInput());
906 return {hglddOperator(
"R{}",
908 hglddInt32(replicateOp.getMultiple()),
911 replicateOp.getType()};
915 if (
auto extractOp = dyn_cast<comb::ExtractOp>(op)) {
916 auto arg = emitExpression(extractOp.getInput());
919 auto lowBit = extractOp.getLowBit();
920 auto highBit = lowBit + extractOp.getType().getIntOrFloatBitWidth() - 1;
921 return {hglddOperator(
"[]",
927 extractOp.getType()};
931 if (
auto muxOp = dyn_cast<comb::MuxOp>(op)) {
932 auto cond = emitExpression(muxOp.getCond());
933 auto lhs = emitExpression(muxOp.getTrueValue());
934 auto rhs = emitExpression(muxOp.getFalseValue());
935 if (!cond || !lhs || !rhs)
937 return {hglddOperator(
"?:", {cond.expr, lhs.expr, rhs.expr}),
946 for (
auto &use : result.getUses()) {
947 auto *user = use.getOwner();
949 if (
auto wireOp = dyn_cast<hw::WireOp>(user))
950 if (wireOp.getInput() == result)
951 return emitExpression(wireOp);
953 if (
auto assignOp = dyn_cast<sv::AssignOp>(user))
954 if (assignOp.getSrc() == result)
955 return emitExpression(assignOp.getDest());
957 if (
auto varAssignOp = dyn_cast<sv::VarAssignOp>(user))
958 if (varAssignOp.getSrc() == result)
959 return emitExpression(varAssignOp.getDest());
961 if (isa<hw::OutputOp>(user)) {
962 auto mod = cast<hw::HWModuleLike>(user->getParentOp());
963 auto portName = mod.getPort(mod.getHWModuleType().getPortIdForOutputId(
964 use.getOperandNumber()))
966 return {hglddSigName(portName), result.getType()};
984 SmallVector<FileEmitter, 0> files;
985 Emitter(Operation *module,
const EmitHGLDDOptions &options);
990Emitter::Emitter(Operation *module,
const EmitHGLDDOptions &options)
991 : state(module, options) {
996 MapVector<StringAttr, FileEmitter> groups;
997 for (
auto [moduleName, module] : state.di.
moduleNodes) {
1000 if (auto fileLoc = findBestLocation(module->op->getLoc(), true, false))
1001 hdlFile = fileLoc.getFilename();
1003 groups.try_emplace(hdlFile, state, hdlFile).first->second;
1004 fileEmitter.modules.push_back(module);
1005 state.moduleNames[module] =
1006 legalizeName(module->name.getValue(), state.objectNamespace);
1011 files.reserve(groups.size());
1012 for (
auto &[hdlFile, emitter] : groups) {
1013 emitter.outputFileName = options.outputDirectory;
1014 StringRef fileName = hdlFile ? hdlFile.getValue() :
"global";
1015 if (llvm::sys::path::is_absolute(fileName))
1016 emitter.outputFileName = fileName;
1018 llvm::sys::path::append(emitter.outputFileName, fileName);
1019 llvm::sys::path::replace_extension(emitter.outputFileName,
"dd");
1020 llvm::sys::path::remove_dots(emitter.outputFileName,
true);
1021 files.push_back(std::move(emitter));
1026 llvm::dbgs() <<
"HGLDD files:\n";
1027 for (
auto &emitter : files) {
1028 llvm::dbgs() <<
"- " << emitter.outputFileName <<
" (from "
1029 << emitter.hdlFile <<
")\n";
1030 for (
auto *module : emitter.modules)
1031 llvm::dbgs() <<
" - " <<
module->name << "\n";
1042 Emitter emitter(module, options);
1043 for (
auto &fileEmitter : emitter.files) {
1044 os <<
"\n// ----- 8< ----- FILE \"" + fileEmitter.outputFileName +
1045 "\" ----- 8< -----\n\n";
1046 fileEmitter.emit(os);
1053 Emitter emitter(module, options);
1055 auto emit = [&](
auto &fileEmitter) {
1057 std::string errorMessage;
1059 mlir::openOutputFile(fileEmitter.outputFileName, &errorMessage);
1061 module->emitError(errorMessage);
1066 fileEmitter.emit(output->os());
1071 return mlir::failableParallelForEach(module->getContext(), emitter.files,
assert(baseType &&"element must be base type")
static void emitDims(ArrayRef< Attribute > dims, raw_ostream &os, Location loc, ModuleEmitter &emitter)
Emit a list of packed dimensions.
StringAttr getVerilogInstanceName(DIInstance &inst)
llvm::json::OStream JOStream
static void findLocations(Location loc, unsigned level, SmallVectorImpl< FileLineColLoc > &locs)
Walk the given loc and collect file-line-column locations that we want to report as source ("HGL") lo...
static bool makePathRelative(StringRef path, StringRef relativeTo, SmallVectorImpl< char > &relativePath)
Make the given path relative to the relativeTo path and store the result in relativePath.
static FileLineColLoc findBestLocation(Location loc, bool emitted, bool fileMustExist)
Find the best location to report as source location ("HGL", emitted = false) or as emitted location (...
static StringRef legalizeName(StringRef name, llvm::StringMap< size_t > &nextGeneratedNameIDs)
Legalize the given name such that it only consists of valid identifier characters in Verilog and does...
llvm::json::Object JObject
false return failure()
Parse an optional keyword or string and set instance into 'result'.
nlohmann::json json
Get a JSON representation of a type.
FileEmitter(VerilogEmitterState &state)
void emit(emit::FileOp op)
LogicalResult emitHGLDD(Operation *module, llvm::raw_ostream &os, const EmitHGLDDOptions &options={})
Serialize the debug information in the given module into the HGLDD format and writes it to output.
LogicalResult emitSplitHGLDD(Operation *module, const EmitHGLDDOptions &options={})
Serialize the debug information in the given module into the HGLDD format and emit one companion HGLD...
StringRef getVerilogModuleName(Operation *module)
OS & operator<<(OS &os, const InnerSymTarget &target)
Printing InnerSymTarget's.
static bool operator==(const ModulePort &a, const ModulePort &b)
StringRef legalizeName(llvm::StringRef name, llvm::StringMap< size_t > &nextGeneratedNameIDs, bool caseInsensitiveKeywords)
Legalize the specified name for use in SV output.
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
bool operator!=(uint64_t a, const FVInt &b)
llvm::hash_code hash_value(const DenseSet< T > &set)
Operation * op
The operation that generated this instance.
StringAttr name
The name of this instance.
DIModule * module
The instantiated module.
bool isInline
If this is an inline scope created by a dbg.scope operation.
Operation * op
The operation that generated this level of hierarchy.
SmallVector< DIInstance *, 0 > instances
Levels of hierarchy nested under this module.
SmallVector< DIVariable *, 0 > variables
Variables declared within this module.
bool isExtern
If this is an extern declaration.
StringAttr name
The name of this level of hierarchy.
Value value
The SSA value representing the value of this variable.
StringAttr name
The name of this variable.
LocationAttr loc
The location of the variable's declaration.
llvm::MapVector< StringAttr, DIModule * > moduleNodes
A mapping from module name to module debug info.
Options for HGLDD emission.
static unsigned getHashValue(const JValue &x)
static bool isEqual(const JValue &a, const JValue &b)