22#include "mlir/IR/BuiltinTypes.h"
23#include "mlir/IR/ImplicitLocOpBuilder.h"
30#define GEN_PASS_DEF_ESICONNECTSERVICES
31#include "circt/Dialect/ESI/ESIPasses.h.inc"
45 ServiceDeclOpInterface,
46 ServiceImplRecordOp implRecord) {
47 auto *ctxt = implReq.getContext();
49 Value clk = implReq.getOperand(0);
50 Value rst = implReq.getOperand(1);
52 if (implReq.getImplOpts()) {
53 auto opts = implReq.getImplOpts()->getValue();
54 for (
auto nameAttr : opts) {
55 return implReq.emitOpError(
"did not recognize option name ")
56 << nameAttr.getName();
60 Block &connImplBlock = implRecord.getReqDetails().front();
61 implRecord.setIsEngine(
true);
62 OpBuilder implRecords = OpBuilder::atBlockEnd(&connImplBlock);
65 auto toStringAttr = [&](ArrayAttr strArr, StringAttr channelName) {
67 llvm::raw_string_ostream os(buff);
69 strArr.getAsRange<AppIDAttr>(), os,
70 [&](AppIDAttr appid) {
71 os << appid.getName().getValue();
73 os <<
"[" << appid.getIndex() <<
"]";
76 os <<
"." << channelName.getValue();
77 return StringAttr::get(ctxt, os.str());
80 auto getAssignment = [&](StringAttr name, StringAttr channelName) {
81 DictionaryAttr assignment = b.getDictionaryAttr({
82 b.getNamedAttr(
"type", b.getStringAttr(
"cosim")),
83 b.getNamedAttr(
"name", channelName),
85 return b.getNamedAttr(name, assignment);
88 llvm::DenseMap<ServiceImplementConnReqOp, unsigned> toClientResultNum;
89 for (
auto req : implReq.getOps<ServiceImplementConnReqOp>())
90 toClientResultNum[req] = toClientResultNum.size();
97 for (
auto req : implReq.getOps<ServiceImplementConnReqOp>()) {
98 Location loc = req->getLoc();
99 ChannelBundleType bundleType = req.getToClient().getType();
100 SmallVector<NamedAttribute, 8> channelAssignments;
102 SmallVector<Value, 8> toServerValues;
104 if (ch.direction == ChannelDirection::to) {
105 ChannelType fromHostType = ch.type;
106 if (fromHostType.getSignaling() == ChannelSignaling::FIFO)
107 fromHostType = b.getType<ChannelType>(fromHostType.getInner(),
108 ChannelSignaling::ValidReady,
109 fromHostType.getDataDelay());
110 auto cosim = b.create<CosimFromHostEndpointOp>(
111 loc, fromHostType, clk, rst,
112 toStringAttr(req.getRelativeAppIDPathAttr(), ch.name));
113 mlir::TypedValue<ChannelType> fromHost = cosim.getFromHost();
114 if (fromHostType.getSignaling() == ChannelSignaling::FIFO)
115 fromHost = b.create<ChannelBufferOp>(
116 loc, ch.type, clk, rst, fromHost,
117 b.getIntegerAttr(b.getI64Type(), 1),
120 toServerValues.push_back(fromHost);
121 channelAssignments.push_back(getAssignment(ch.name, cosim.getIdAttr()));
126 b.create<PackBundleOp>(implReq.getLoc(), bundleType, toServerValues);
127 implReq.getResult(toClientResultNum[req])
128 .replaceAllUsesWith(pack.getBundle());
132 if (ch.direction == ChannelDirection::from) {
133 Value fromChannel = pack.getFromChannels()[chanIdx++];
134 auto chType = cast<ChannelType>(fromChannel.getType());
135 if (chType.getSignaling() == ChannelSignaling::FIFO) {
136 auto cosimType = b.getType<ChannelType>(chType.getInner(),
137 ChannelSignaling::ValidReady,
138 chType.getDataDelay());
139 fromChannel = b.create<ChannelBufferOp>(
140 loc, cosimType, clk, rst, fromChannel,
141 b.getIntegerAttr(b.getI64Type(), 1),
145 auto cosim = b.create<CosimToHostEndpointOp>(
146 loc, clk, rst, fromChannel,
147 toStringAttr(req.getRelativeAppIDPathAttr(), ch.name));
148 channelAssignments.push_back(getAssignment(ch.name, cosim.getIdAttr()));
152 implRecords.create<ServiceImplClientRecordOp>(
153 req.getLoc(), req.getRelativeAppIDPathAttr(), req.getServicePortAttr(),
154 TypeAttr::get(bundleType), b.getDictionaryAttr(channelAssignments),
167 ServiceDeclOpInterface decl,
168 ServiceImplRecordOp) {
170 return implReq.emitOpError(
171 "Must specify a service declaration to use 'sv_mem'.");
173 ImplicitLocOpBuilder b(implReq.getLoc(), implReq);
176 RandomAccessMemoryDeclOp ramDecl =
177 dyn_cast<RandomAccessMemoryDeclOp>(decl.getOperation());
179 return implReq.emitOpError(
180 "'sv_mem' implementation type can only be used to "
181 "implement RandomAccessMemory declarations");
183 if (implReq.getNumOperands() != 2)
184 return implReq.emitOpError(
"Implementation requires clk and rst operands");
185 auto clk = implReq.getOperand(0);
186 auto rst = implReq.getOperand(1);
187 auto write = b.getStringAttr(
"write");
188 auto read = b.getStringAttr(
"read");
190 APInt( 0, 0,
false));
191 auto i1 = b.getI1Type();
195 SmallVector<ServiceImplementConnReqOp, 8> toClientReqs(
196 llvm::make_filter_range(
197 implReq.getOps<ServiceImplementConnReqOp>(),
198 [](
auto req) { return req.getToClient() != nullptr; }));
201 DenseMap<Value, Value> outputMap;
202 for (
auto [bout, reqout] :
203 llvm::zip_longest(toClientReqs, implReq.getResults())) {
205 assert(reqout.has_value());
206 Value toClient = bout->getToClient();
207 outputMap[toClient] = *reqout;
211 hw::UnpackedArrayType memType =
212 hw::UnpackedArrayType::get(ramDecl.getInnerType(), ramDecl.getDepth());
214 b.create<
sv::RegOp>(memType, implReq.getServiceSymbolAttr().getAttr())
219 SmallVector<std::tuple<Value, Value, Value>> writeGoAddressData;
220 for (
auto req : implReq.getOps<ServiceImplementConnReqOp>()) {
221 auto port = req.getServicePort().getName();
228 auto doneValid = bb.
get(i1);
229 auto ackChannel = b.create<WrapValidReadyOp>(none, doneValid);
232 b.create<PackBundleOp>(implReq.getLoc(), req.getToClient().getType(),
233 ackChannel.getChanOutput());
235 pack.getFromChannels()[RandomAccessMemoryDeclOp::ReqDirChannelIdx];
236 toClientResp = pack.getBundle();
240 b.create<UnwrapValidReadyOp>(toServer, ackChannel.getReady());
243 b.getStringAttr(
"address"));
245 b.getStringAttr(
"data"));
249 go->setAttr(
"sv.namehint", b.getStringAttr(
"write_go"));
254 writeGoAddressData.push_back(std::make_tuple(go, address, data));
256 }
else if (port == read) {
260 auto dataValid = bb.
get(i1);
261 auto data = bb.
get(ramDecl.getInnerType());
262 auto dataChannel = b.create<WrapValidReadyOp>(data, dataValid);
265 b.create<PackBundleOp>(implReq.getLoc(), req.getToClient().getType(),
266 dataChannel.getChanOutput());
268 pack.getFromChannels()[RandomAccessMemoryDeclOp::RespDirChannelIdx];
269 toClientResp = pack.getBundle();
273 b.create<UnwrapValidReadyOp>(toServer, dataChannel.getReady());
274 Value unsignedAddress = addressUnwrap.getRawOutput();
276 b.getIntegerType(llvm::Log2_64_Ceil(ramDecl.getDepth())),
278 Value memLoc = b.
create<sv::ArrayIndexInOutOp>(mem, signlessAddress);
282 data.setValue(readData);
283 dataValid.setValue(addressUnwrap.getValid());
285 assert(
false &&
"Port should be either 'read' or 'write'");
288 outputMap[req.getToClient()].replaceAllUsesWith(toClientResp);
292 auto hwClk = b.create<seq::FromClockOp>(clk);
293 b.create<sv::AlwaysFFOp>(
294 sv::EventControl::AtPosEdge, hwClk, sv::ResetType::SyncReset,
295 sv::EventControl::AtPosEdge, rst, [&] {
296 for (
auto [go, address, data] : writeGoAddressData) {
297 Value a = address, d = data;
299 b.create<sv::IfOp>(go, [&] {
301 b.getIntegerType(llvm::Log2_64_Ceil(ramDecl.getDepth())), a);
303 b.create<sv::ArrayIndexInOutOp>(mem, signlessAddress);
304 b.create<sv::PAssignOp>(memLoc, d);
319 ServiceDeclOpInterface decl) {
322 auto genF =
genLookupTable.find(req.getImplTypeAttr().getValue());
325 return req.emitOpError(
"Could not find service generator for attribute '")
326 << req.getImplTypeAttr() <<
"'";
333 auto implRecord = b.create<ServiceImplRecordOp>(
334 req.getLoc(), req.getAppID(),
false,
335 req.getServiceSymbolAttr(), req.getStdServiceAttr(),
336 req.getImplTypeAttr(), b.getDictionaryAttr({}));
337 implRecord.getReqDetails().emplaceBlock();
339 return genF->second(req, decl, implRecord);
343 DenseMap<StringRef, ServiceGeneratorDispatcher::ServiceGeneratorFunc>{
349 return ::globalDispatcher;
371 DenseMap<Operation *, SmallVector<igraph::InstanceOpInterface, 1>>
372 moduleInstantiations;
374 void getAndSortModules(ModuleOp topMod,
375 SmallVectorImpl<hw::HWModuleLike> &mods);
376 void getAndSortModulesVisitor(hw::HWModuleLike mod,
377 SmallVectorImpl<hw::HWModuleLike> &mods,
378 DenseSet<Operation *> &modsSeen);
382void ModuleSorter::getAndSortModules(ModuleOp topMod,
383 SmallVectorImpl<hw::HWModuleLike> &mods) {
385 DenseSet<Operation *> modsSeen;
387 moduleInstantiations.clear();
388 topMod.walk([&](hw::HWModuleLike mod) {
389 getAndSortModulesVisitor(mod, mods, modsSeen);
394void ModuleSorter::getAndSortModulesVisitor(
395 hw::HWModuleLike mod, SmallVectorImpl<hw::HWModuleLike> &mods,
396 DenseSet<Operation *> &modsSeen) {
397 if (modsSeen.contains(mod))
399 modsSeen.insert(mod);
401 mod.walk([&](igraph::InstanceOpInterface inst) {
402 auto targetNameAttrs = inst.getReferencedModuleNamesAttr();
403 for (
auto targetNameAttr : targetNameAttrs) {
405 topLevelSyms.getDefinition(cast<StringAttr>(targetNameAttr));
407 moduleInstantiations[modOp].push_back(inst);
408 if (
auto modLike = dyn_cast<hw::HWModuleLike>(modOp))
409 getAndSortModulesVisitor(modLike, mods, modsSeen);
419struct ESIConnectServicesPass
420 :
public circt::esi::impl::ESIConnectServicesBase<ESIConnectServicesPass>,
424 : genDispatcher(gen) {}
425 ESIConnectServicesPass()
428 void runOnOperation()
override;
433 void convertReq(RequestConnectionOp);
438 LogicalResult surfaceReqs(hw::HWMutableModuleLike,
439 ArrayRef<ServiceImplementConnReqOp>);
444 LogicalResult replaceInst(ServiceInstanceOp,
445 ArrayRef<ServiceImplementConnReqOp> portReqs);
449 LogicalResult process(hw::HWModuleLike);
452 StringAttr getStdService(FlatSymbolRefAttr serviceSym);
459void ESIConnectServicesPass::runOnOperation() {
460 ModuleOp outerMod = getOperation();
461 topLevelSyms.addDefinitions(outerMod);
463 outerMod.walk([&](RequestConnectionOp req) { convertReq(req); });
469 SmallVector<hw::HWModuleLike, 64> sortedMods;
470 getAndSortModules(outerMod, sortedMods);
473 for (
auto mod : sortedMods) {
474 hw::HWModuleLike mutableMod = dyn_cast<hw::HWModuleLike>(*mod);
475 if (mutableMod && failed(process(mutableMod))) {
483StringAttr ESIConnectServicesPass::getStdService(FlatSymbolRefAttr svcSym) {
486 Operation *svcDecl = topLevelSyms.getDefinition(svcSym);
487 if (!isa<CustomServiceDeclOp>(svcDecl))
488 return svcDecl->getName().getIdentifier();
492void ESIConnectServicesPass::convertReq(RequestConnectionOp req) {
494 auto newReq = b.create<ServiceImplementConnReqOp>(
495 req.getLoc(), req.getToClient().getType(), req.getServicePortAttr(),
496 ArrayAttr::get(&getContext(), {req.getAppIDAttr()}));
497 newReq->setDialectAttrs(req->getDialectAttrs());
498 req.getToClient().replaceAllUsesWith(newReq.getToClient());
501 b.create<ServiceRequestRecordOp>(
502 req.getLoc(), req.getAppID(), req.getServicePortAttr(),
503 getStdService(req.getServicePortAttr().getModuleRef()),
504 req.getToClient().getType());
508LogicalResult ESIConnectServicesPass::process(hw::HWModuleLike mod) {
510 if (mod->getNumRegions() == 0 || mod->getRegion(0).empty())
513 Block &modBlock = mod->getRegion(0).front();
516 SetVector<ServiceImplementConnReqOp> nonLocalReqs;
518 llvm::MapVector<SymbolRefAttr, llvm::SetVector<ServiceImplementConnReqOp>>
520 for (
auto instOp : modBlock.getOps<ServiceInstanceOp>())
521 localImplReqs[instOp.getServiceSymbolAttr()] = {};
525 llvm::SetVector<ServiceImplementConnReqOp> *anyServiceInst =
nullptr;
526 if (
auto defaultService = localImplReqs.find(SymbolRefAttr());
527 defaultService != localImplReqs.end())
528 anyServiceInst = &defaultService->second;
530 auto sortConnReqs = [&]() {
532 for (
auto req :
llvm::make_early_inc_range(
533 mod.
getBodyBlock()->getOps<ServiceImplementConnReqOp>())) {
534 auto service = req.getServicePort().getModuleRef();
535 auto reqListIter = localImplReqs.find(service);
536 if (reqListIter != localImplReqs.end())
537 reqListIter->second.insert(req);
538 else if (anyServiceInst)
539 anyServiceInst->insert(req);
541 nonLocalReqs.insert(req);
550 llvm::make_early_inc_range(modBlock.getOps<ServiceInstanceOp>())) {
551 auto portReqs = localImplReqs[instOp.getServiceSymbolAttr()];
552 if (failed(replaceInst(instOp, portReqs.getArrayRef())))
556 for (RequestConnectionOp req :
llvm::make_early_inc_range(
563 if (nonLocalReqs.empty())
566 if (
auto mutableMod = dyn_cast<hw::HWMutableModuleLike>(mod.getOperation()))
567 return surfaceReqs(mutableMod, nonLocalReqs.getArrayRef());
568 return mod.emitOpError(
569 "Cannot surface requests through module without mutable ports");
572LogicalResult ESIConnectServicesPass::replaceInst(
573 ServiceInstanceOp instOp, ArrayRef<ServiceImplementConnReqOp> portReqs) {
574 auto declSym = instOp.getServiceSymbolAttr();
575 ServiceDeclOpInterface decl;
577 decl = dyn_cast_or_null<ServiceDeclOpInterface>(
578 topLevelSyms.getDefinition(declSym));
580 return instOp.emitOpError(
"Could not find service declaration ")
586 SmallVector<Type, 8> resultTypes(instOp.getResultTypes().begin(),
587 instOp.getResultTypes().end());
588 for (
auto req : portReqs)
589 resultTypes.push_back(req.getBundleType());
593 auto implOp = b.create<ServiceImplementReqOp>(
594 instOp.getLoc(), resultTypes, instOp.getAppIDAttr(),
595 instOp.getServiceSymbolAttr(), instOp.getImplTypeAttr(),
596 getStdService(declSym), instOp.getImplOptsAttr(), instOp.getOperands());
597 implOp->setDialectAttrs(instOp->getDialectAttrs());
598 Block &reqBlock = implOp.getPortReqs().emplaceBlock();
601 for (
auto [n, o] :
llvm::zip(implOp.getResults(), instOp.getResults()))
602 o.replaceAllUsesWith(n);
603 unsigned instOpNumResults = instOp.getNumResults();
604 for (
size_t idx = 0, e = portReqs.size(); idx < e; ++idx) {
605 ServiceImplementConnReqOp req = portReqs[idx];
606 req.getToClient().replaceAllUsesWith(
607 implOp.getResult(idx + instOpNumResults));
610 for (
auto req : portReqs)
611 req->moveBefore(&reqBlock, reqBlock.
end());
619 if (failed(genDispatcher.generate(implOp, decl)))
620 return implOp.emitOpError(
"failed to generate server");
626ESIConnectServicesPass::surfaceReqs(hw::HWMutableModuleLike mod,
627 ArrayRef<ServiceImplementConnReqOp> reqs) {
628 auto *
ctxt = mod.getContext();
629 Block *body = &mod->getRegion(0).front();
632 unsigned origNumInputs = mod.getNumInputPorts();
633 SmallVector<std::pair<unsigned, hw::PortInfo>> newInputs;
636 auto getPortName = [&](ArrayAttr namePath) {
637 std::string portName;
638 llvm::raw_string_ostream nameOS(portName);
640 namePath.getAsRange<AppIDAttr>(), nameOS,
641 [&](AppIDAttr appid) {
642 nameOS << appid.getName().getValue();
643 if (appid.getIndex())
644 nameOS <<
"_" << appid.getIndex();
647 return StringAttr::get(ctxt, nameOS.str());
650 for (
auto req : reqs)
651 if (req->getParentWithTrait<
OpTrait::IsIsolatedFromAbove>() != mod)
652 return req.emitOpError(
653 "Cannot surface requests through isolated from above ops");
656 for (
auto req : reqs) {
657 newInputs.push_back(std::make_pair(
659 hw::PortInfo{{getPortName(req.getRelativeAppIDPathAttr()),
666 Value replValue = body->addArgument(req.getBundleType(), req->getLoc());
667 req.getToClient().replaceAllUsesWith(replValue);
669 mod.insertPorts(newInputs, {});
672 auto prependNamePart = [&](ArrayAttr appIDPath, AppIDAttr appID) {
673 SmallVector<Attribute, 8> newAppIDPath;
674 newAppIDPath.push_back(appID);
675 newAppIDPath.append(appIDPath.begin(), appIDPath.end());
676 return ArrayAttr::get(appIDPath.getContext(), newAppIDPath);
680 SmallVector<igraph::InstanceOpInterface, 1> newModuleInstantiations;
681 for (
auto inst : moduleInstantiations[mod]) {
685 SmallVector<Value, 16> newOperands;
686 for (
auto req : reqs) {
688 ArrayAttr appIDPath = req.getRelativeAppIDPathAttr();
689 if (
auto instAppID = dyn_cast_or_null<AppIDAttr>(
690 inst->getDiscardableAttr(AppIDAttr::AppIDAttrName)))
691 appIDPath = prependNamePart(appIDPath, instAppID);
694 auto clone = b.create<ServiceImplementConnReqOp>(
695 req.getLoc(), req.getToClient().getType(), req.getServicePortAttr(),
697 clone->setDialectAttrs(req->getDialectAttrs());
698 newOperands.push_back(clone.getToClient());
700 inst->insertOperands(inst->getNumOperands(), newOperands);
702 if (
auto hwInst = dyn_cast<hw::InstanceOp>(*inst))
703 hwInst.setArgNamesAttr(b.getArrayAttr(mod.getInputNames()));
708 for (
auto req : reqs)
713std::unique_ptr<OperationPass<ModuleOp>>
715 return std::make_unique<ESIConnectServicesPass>();
assert(baseType &&"element must be base type")
static ServiceGeneratorDispatcher globalDispatcher(DenseMap< StringRef, ServiceGeneratorDispatcher::ServiceGeneratorFunc >{ {"cosim", instantiateCosimEndpointOps}, {"sv_mem", instantiateSystemVerilogMemory}}, false)
static LogicalResult instantiateCosimEndpointOps(ServiceImplementReqOp implReq, ServiceDeclOpInterface, ServiceImplRecordOp implRecord)
The generator for the "cosim" impl_type.
static LogicalResult instantiateSystemVerilogMemory(ServiceImplementReqOp implReq, ServiceDeclOpInterface decl, ServiceImplRecordOp)
static EvaluatorValuePtr unwrap(OMEvaluatorValue c)
static Block * getBodyBlock(FModuleLike mod)
Instantiate one of these and use it to build typed backedges.
Backedge get(mlir::Type resultType, mlir::LocationAttr optionalLoc={})
Create a typed backedge.
Default symbol cache implementation; stores associations between names (StringAttr's) to mlir::Operat...
Class which "dispatches" a service implementation request to its specified generator.
void registerGenerator(StringRef implType, ServiceGeneratorFunc gen)
Add a generator to this registry.
LogicalResult generate(ServiceImplementReqOp, ServiceDeclOpInterface)
Generate a service implementation if a generator exists in this registry.
static ServiceGeneratorDispatcher & globalDispatcher()
Get the global dispatcher.
DenseMap< StringRef, ServiceGeneratorFunc > genLookupTable
std::function< LogicalResult(ServiceImplementReqOp, ServiceDeclOpInterface, ServiceImplRecordOp)> ServiceGeneratorFunc
std::unique_ptr< OperationPass< ModuleOp > > createESIConnectServicesPass()
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
This holds the name, type, direction of a module's ports.