Loading [MathJax]/jax/output/HTML-CSS/config.js
CIRCT 21.0.0git
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ESIServices.cpp
Go to the documentation of this file.
1//===- ESIServices.cpp - Code related to ESI services ---------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "PassDetails.h"
10
21
22#include "mlir/IR/BuiltinTypes.h"
23#include "mlir/IR/ImplicitLocOpBuilder.h"
24
25#include <memory>
26#include <utility>
27
28namespace circt {
29namespace esi {
30#define GEN_PASS_DEF_ESICONNECTSERVICES
31#include "circt/Dialect/ESI/ESIPasses.h.inc"
32} // namespace esi
33} // namespace circt
34
35using namespace circt;
36using namespace circt::esi;
37
38//===----------------------------------------------------------------------===//
39// C++ service generators.
40//===----------------------------------------------------------------------===//
41
42/// The generator for the "cosim" impl_type.
43static LogicalResult
44instantiateCosimEndpointOps(ServiceImplementReqOp implReq,
45 ServiceDeclOpInterface,
46 ServiceImplRecordOp implRecord) {
47 auto *ctxt = implReq.getContext();
48 OpBuilder b(implReq);
49 Value clk = implReq.getOperand(0);
50 Value rst = implReq.getOperand(1);
51
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();
57 }
58 }
59
60 Block &connImplBlock = implRecord.getReqDetails().front();
61 implRecord.setIsEngine(true);
62 OpBuilder implRecords = OpBuilder::atBlockEnd(&connImplBlock);
63
64 // Assemble the name to use for an endpoint.
65 auto toStringAttr = [&](ArrayAttr strArr, StringAttr channelName) {
66 std::string buff;
67 llvm::raw_string_ostream os(buff);
68 llvm::interleave(
69 strArr.getAsRange<AppIDAttr>(), os,
70 [&](AppIDAttr appid) {
71 os << appid.getName().getValue();
72 if (appid.getIndex())
73 os << "[" << appid.getIndex() << "]";
74 },
75 ".");
76 os << "." << channelName.getValue();
77 return StringAttr::get(ctxt, os.str());
78 };
79
80 auto getAssignment = [&](StringAttr name, StringAttr channelName) {
81 DictionaryAttr assignment = b.getDictionaryAttr({
82 b.getNamedAttr("type", b.getStringAttr("cosim")),
83 b.getNamedAttr("name", channelName),
84 });
85 return b.getNamedAttr(name, assignment);
86 };
87
88 llvm::DenseMap<ServiceImplementConnReqOp, unsigned> toClientResultNum;
89 for (auto req : implReq.getOps<ServiceImplementConnReqOp>())
90 toClientResultNum[req] = toClientResultNum.size();
91
92 // Iterate through the requests, building a cosim endpoint for each channel in
93 // the bundle.
94 // TODO: The cosim op should probably be able to take a bundle type and get
95 // lowered to the SV primitive later on. The SV primitive will also need some
96 // work to suit this new world order, so let's put this off.
97 for (auto req : implReq.getOps<ServiceImplementConnReqOp>()) {
98 Location loc = req->getLoc();
99 ChannelBundleType bundleType = req.getToClient().getType();
100 SmallVector<NamedAttribute, 8> channelAssignments;
101
102 SmallVector<Value, 8> toServerValues;
103 for (BundledChannel ch : bundleType.getChannels()) {
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 /*stages=*/b.getIntegerAttr(b.getI64Type(), 1),
118 /*name=*/StringAttr())
119 .getOutput();
120 toServerValues.push_back(fromHost);
121 channelAssignments.push_back(getAssignment(ch.name, cosim.getIdAttr()));
122 }
123 }
124
125 auto pack =
126 b.create<PackBundleOp>(implReq.getLoc(), bundleType, toServerValues);
127 implReq.getResult(toClientResultNum[req])
128 .replaceAllUsesWith(pack.getBundle());
129
130 size_t chanIdx = 0;
131 for (BundledChannel ch : bundleType.getChannels()) {
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 /*stages=*/b.getIntegerAttr(b.getI64Type(), 1),
142 /*name=*/StringAttr())
143 .getOutput();
144 }
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()));
149 }
150 }
151
152 implRecords.create<ServiceImplClientRecordOp>(
153 req.getLoc(), req.getRelativeAppIDPathAttr(), req.getServicePortAttr(),
154 TypeAttr::get(bundleType), b.getDictionaryAttr(channelAssignments),
155 DictionaryAttr());
156 }
157
158 // Erase the generation request.
159 implReq.erase();
160 return success();
161}
162
163// Generator for "sv_mem" implementation type. Emits SV ops for an unpacked
164// array, hopefully inferred as a memory to the SV compiler.
165static LogicalResult
166instantiateSystemVerilogMemory(ServiceImplementReqOp implReq,
167 ServiceDeclOpInterface decl,
168 ServiceImplRecordOp) {
169 if (!decl)
170 return implReq.emitOpError(
171 "Must specify a service declaration to use 'sv_mem'.");
172
173 ImplicitLocOpBuilder b(implReq.getLoc(), implReq);
174 BackedgeBuilder bb(b, implReq.getLoc());
175
176 RandomAccessMemoryDeclOp ramDecl =
177 dyn_cast<RandomAccessMemoryDeclOp>(decl.getOperation());
178 if (!ramDecl)
179 return implReq.emitOpError(
180 "'sv_mem' implementation type can only be used to "
181 "implement RandomAccessMemory declarations");
182
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");
189 auto none = b.create<hw::ConstantOp>(
190 APInt(/*numBits*/ 0, /*val*/ 0, /*isSigned*/ false));
191 auto i1 = b.getI1Type();
192 auto c0 = b.create<hw::ConstantOp>(i1, 0);
193
194 // List of reqs which have a result.
195 SmallVector<ServiceImplementConnReqOp, 8> toClientReqs(
196 llvm::make_filter_range(
197 implReq.getOps<ServiceImplementConnReqOp>(),
198 [](auto req) { return req.getToClient() != nullptr; }));
199
200 // Assemble a mapping of toClient results to actual consumers.
201 DenseMap<Value, Value> outputMap;
202 for (auto [bout, reqout] :
203 llvm::zip_longest(toClientReqs, implReq.getResults())) {
204 assert(bout.has_value());
205 assert(reqout.has_value());
206 Value toClient = bout->getToClient();
207 outputMap[toClient] = *reqout;
208 }
209
210 // Create the SV memory.
211 hw::UnpackedArrayType memType =
212 hw::UnpackedArrayType::get(ramDecl.getInnerType(), ramDecl.getDepth());
213 auto mem =
214 b.create<sv::RegOp>(memType, implReq.getServiceSymbolAttr().getAttr())
215 .getResult();
216
217 // Do everything which doesn't actually write to the memory, store the signals
218 // needed for the actual memory writes for later.
219 SmallVector<std::tuple<Value, Value, Value>> writeGoAddressData;
220 for (auto req : implReq.getOps<ServiceImplementConnReqOp>()) {
221 auto port = req.getServicePort().getName();
222 Value toClientResp;
223
224 if (port == write) {
225 // If this pair is doing a write...
226
227 // Construct the response channel.
228 auto doneValid = bb.get(i1);
229 auto ackChannel = b.create<WrapValidReadyOp>(none, doneValid);
230
231 auto pack =
232 b.create<PackBundleOp>(implReq.getLoc(), req.getToClient().getType(),
233 ackChannel.getChanOutput());
234 Value toServer =
235 pack.getFromChannels()[RandomAccessMemoryDeclOp::ReqDirChannelIdx];
236 toClientResp = pack.getBundle();
237
238 // Unwrap the write request and 'explode' the struct.
239 auto unwrap =
240 b.create<UnwrapValidReadyOp>(toServer, ackChannel.getReady());
241
242 Value address = b.create<hw::StructExtractOp>(unwrap.getRawOutput(),
243 b.getStringAttr("address"));
244 Value data = b.create<hw::StructExtractOp>(unwrap.getRawOutput(),
245 b.getStringAttr("data"));
246
247 // Determine if the write should occur this cycle.
248 auto go = b.create<comb::AndOp>(unwrap.getValid(), unwrap.getReady());
249 go->setAttr("sv.namehint", b.getStringAttr("write_go"));
250 // Register the 'go' signal and use it as the done message.
251 doneValid.setValue(
252 b.create<seq::CompRegOp>(go, clk, rst, c0, "write_done"));
253 // Store the necessary data for the 'always' memory writing block.
254 writeGoAddressData.push_back(std::make_tuple(go, address, data));
255
256 } else if (port == read) {
257 // If it's a read...
258
259 // Construct the response channel.
260 auto dataValid = bb.get(i1);
261 auto data = bb.get(ramDecl.getInnerType());
262 auto dataChannel = b.create<WrapValidReadyOp>(data, dataValid);
263
264 auto pack =
265 b.create<PackBundleOp>(implReq.getLoc(), req.getToClient().getType(),
266 dataChannel.getChanOutput());
267 Value toServer =
268 pack.getFromChannels()[RandomAccessMemoryDeclOp::RespDirChannelIdx];
269 toClientResp = pack.getBundle();
270
271 // Unwrap the requested address and read from that memory location.
272 auto addressUnwrap =
273 b.create<UnwrapValidReadyOp>(toServer, dataChannel.getReady());
274 Value memLoc =
275 b.create<sv::ArrayIndexInOutOp>(mem, addressUnwrap.getRawOutput());
276 auto readData = b.create<sv::ReadInOutOp>(memLoc);
277
278 // Set the data on the response.
279 data.setValue(readData);
280 dataValid.setValue(addressUnwrap.getValid());
281 } else {
282 assert(false && "Port should be either 'read' or 'write'");
283 }
284
285 outputMap[req.getToClient()].replaceAllUsesWith(toClientResp);
286 }
287
288 // Now construct the memory writes.
289 auto hwClk = b.create<seq::FromClockOp>(clk);
290 b.create<sv::AlwaysFFOp>(
291 sv::EventControl::AtPosEdge, hwClk, sv::ResetType::SyncReset,
292 sv::EventControl::AtPosEdge, rst, [&] {
293 for (auto [go, address, data] : writeGoAddressData) {
294 Value a = address, d = data; // So the lambda can capture.
295 // If we're told to go, do the write.
296 b.create<sv::IfOp>(go, [&] {
297 Value memLoc = b.create<sv::ArrayIndexInOutOp>(mem, a);
298 b.create<sv::PAssignOp>(memLoc, d);
299 });
300 }
301 });
302
303 implReq.erase();
304 return success();
305}
306
307//===----------------------------------------------------------------------===//
308// Service generator dispatcher.
309//===----------------------------------------------------------------------===//
310
311LogicalResult
312ServiceGeneratorDispatcher::generate(ServiceImplementReqOp req,
313 ServiceDeclOpInterface decl) {
314 // Lookup based on 'impl_type' attribute and pass through the generate request
315 // if found.
316 auto genF = genLookupTable.find(req.getImplTypeAttr().getValue());
317 if (genF == genLookupTable.end()) {
318 if (failIfNotFound)
319 return req.emitOpError("Could not find service generator for attribute '")
320 << req.getImplTypeAttr() << "'";
321 return success();
322 }
323
324 // Since we always need a record of generation, create it here then pass it to
325 // the generator for possible modification.
326 OpBuilder b(req);
327 auto implRecord = b.create<ServiceImplRecordOp>(
328 req.getLoc(), req.getAppID(), /*isEngine=*/false,
329 req.getServiceSymbolAttr(), req.getStdServiceAttr(),
330 req.getImplTypeAttr(), b.getDictionaryAttr({}));
331 implRecord.getReqDetails().emplaceBlock();
332
333 return genF->second(req, decl, implRecord);
334}
335
337 DenseMap<StringRef, ServiceGeneratorDispatcher::ServiceGeneratorFunc>{
340 false);
341
345
348 genLookupTable[implType] = std::move(gen);
349}
350
351//===----------------------------------------------------------------------===//
352// Wire up services pass.
353//===----------------------------------------------------------------------===//
354
355namespace {
356/// Find all the modules and use the partial order of the instantiation DAG
357/// to sort them. If we use this order when "bubbling" up operations, we
358/// guarantee one-pass completeness. As a side-effect, populate the module to
359/// instantiation sites mapping.
360///
361/// Assumption (unchecked): there is not a cycle in the instantiation graph.
362struct ModuleSorter {
363protected:
364 SymbolCache topLevelSyms;
365 DenseMap<Operation *, SmallVector<igraph::InstanceOpInterface, 1>>
366 moduleInstantiations;
367
368 void getAndSortModules(ModuleOp topMod,
369 SmallVectorImpl<hw::HWModuleLike> &mods);
370 void getAndSortModulesVisitor(hw::HWModuleLike mod,
371 SmallVectorImpl<hw::HWModuleLike> &mods,
372 DenseSet<Operation *> &modsSeen);
373};
374} // namespace
375
376void ModuleSorter::getAndSortModules(ModuleOp topMod,
377 SmallVectorImpl<hw::HWModuleLike> &mods) {
378 // Add here _before_ we go deeper to prevent infinite recursion.
379 DenseSet<Operation *> modsSeen;
380 mods.clear();
381 moduleInstantiations.clear();
382 topMod.walk([&](hw::HWModuleLike mod) {
383 getAndSortModulesVisitor(mod, mods, modsSeen);
384 });
385}
386
387// Run a post-order DFS.
388void ModuleSorter::getAndSortModulesVisitor(
389 hw::HWModuleLike mod, SmallVectorImpl<hw::HWModuleLike> &mods,
390 DenseSet<Operation *> &modsSeen) {
391 if (modsSeen.contains(mod))
392 return;
393 modsSeen.insert(mod);
394
395 mod.walk([&](igraph::InstanceOpInterface inst) {
396 auto targetNameAttrs = inst.getReferencedModuleNamesAttr();
397 for (auto targetNameAttr : targetNameAttrs) {
398 Operation *modOp =
399 topLevelSyms.getDefinition(cast<StringAttr>(targetNameAttr));
400 assert(modOp);
401 moduleInstantiations[modOp].push_back(inst);
402 if (auto modLike = dyn_cast<hw::HWModuleLike>(modOp))
403 getAndSortModulesVisitor(modLike, mods, modsSeen);
404 }
405 });
406
407 mods.push_back(mod);
408}
409namespace {
410/// Implements a pass to connect up ESI services clients to the nearest server
411/// instantiation. Wires up the ports and generates a generation request to
412/// call a user-specified generator.
413struct ESIConnectServicesPass
414 : public circt::esi::impl::ESIConnectServicesBase<ESIConnectServicesPass>,
415 ModuleSorter {
416
417 ESIConnectServicesPass(const ServiceGeneratorDispatcher &gen)
418 : genDispatcher(gen) {}
419 ESIConnectServicesPass()
420 : genDispatcher(ServiceGeneratorDispatcher::globalDispatcher()) {}
421
422 void runOnOperation() override;
423
424 /// Convert connection requests to service implement connection requests,
425 /// which have a relative appid path instead of just an appid. Leave being a
426 /// record for the manifest of the original request.
427 void convertReq(RequestConnectionOp);
428
429 /// "Bubble up" the specified requests to all of the instantiations of the
430 /// module specified. Create and connect up ports to tunnel the ESI channels
431 /// through.
432 LogicalResult surfaceReqs(hw::HWMutableModuleLike,
433 ArrayRef<ServiceImplementConnReqOp>);
434
435 /// For any service which is "local" (provides the requested service) in a
436 /// module, replace it with a ServiceImplementOp. Said op is to be replaced
437 /// with an instantiation by a generator.
438 LogicalResult replaceInst(ServiceInstanceOp,
439 ArrayRef<ServiceImplementConnReqOp> portReqs);
440
441 /// Figure out which requests are "local" vs need to be surfaced. Call
442 /// 'surfaceReqs' and/or 'replaceInst' as appropriate.
443 LogicalResult process(hw::HWModuleLike);
444
445 /// If the servicePort is referring to a std service, return the name of it.
446 StringAttr getStdService(FlatSymbolRefAttr serviceSym);
447
448private:
449 ServiceGeneratorDispatcher genDispatcher;
450};
451} // anonymous namespace
452
453void ESIConnectServicesPass::runOnOperation() {
454 ModuleOp outerMod = getOperation();
455 topLevelSyms.addDefinitions(outerMod);
456
457 outerMod.walk([&](RequestConnectionOp req) { convertReq(req); });
458
459 // Get a partially-ordered list of modules based on the instantiation DAG.
460 // It's _very_ important that we process modules before their instantiations
461 // so that the modules where they're instantiated correctly process the
462 // surfaced connections.
463 SmallVector<hw::HWModuleLike, 64> sortedMods;
464 getAndSortModules(outerMod, sortedMods);
465
466 // Process each module.
467 for (auto mod : sortedMods) {
468 hw::HWModuleLike mutableMod = dyn_cast<hw::HWModuleLike>(*mod);
469 if (mutableMod && failed(process(mutableMod))) {
470 signalPassFailure();
471 return;
472 }
473 }
474}
475
476// Get the std service name, if any.
477StringAttr ESIConnectServicesPass::getStdService(FlatSymbolRefAttr svcSym) {
478 if (!svcSym)
479 return {};
480 Operation *svcDecl = topLevelSyms.getDefinition(svcSym);
481 if (!isa<CustomServiceDeclOp>(svcDecl))
482 return svcDecl->getName().getIdentifier();
483 return {};
484}
485
486void ESIConnectServicesPass::convertReq(RequestConnectionOp req) {
487 OpBuilder b(req);
488 auto newReq = b.create<ServiceImplementConnReqOp>(
489 req.getLoc(), req.getToClient().getType(), req.getServicePortAttr(),
490 ArrayAttr::get(&getContext(), {req.getAppIDAttr()}));
491 newReq->setDialectAttrs(req->getDialectAttrs());
492 req.getToClient().replaceAllUsesWith(newReq.getToClient());
493
494 // Emit a record of the original request.
495 b.create<ServiceRequestRecordOp>(
496 req.getLoc(), req.getAppID(), req.getServicePortAttr(),
497 getStdService(req.getServicePortAttr().getModuleRef()),
498 req.getToClient().getType());
499 req.erase();
500}
501
502LogicalResult ESIConnectServicesPass::process(hw::HWModuleLike mod) {
503 // If 'mod' doesn't have a body, assume it's an external module.
504 if (mod->getNumRegions() == 0 || mod->getRegion(0).empty())
505 return success();
506
507 Block &modBlock = mod->getRegion(0).front();
508
509 // The non-local reqs which need to be surfaced from this module.
510 SetVector<ServiceImplementConnReqOp> nonLocalReqs;
511 // Index the local services and create blocks in which to put the requests.
512 llvm::MapVector<SymbolRefAttr, llvm::SetVector<ServiceImplementConnReqOp>>
513 localImplReqs;
514 for (auto instOp : modBlock.getOps<ServiceInstanceOp>())
515 localImplReqs[instOp.getServiceSymbolAttr()] = {};
516 // AFTER we assemble the local services table (and it will not change the
517 // location of the values), get the pointer to the default service instance,
518 // if any.
519 llvm::SetVector<ServiceImplementConnReqOp> *anyServiceInst = nullptr;
520 if (auto defaultService = localImplReqs.find(SymbolRefAttr());
521 defaultService != localImplReqs.end())
522 anyServiceInst = &defaultService->second;
523
524 auto sortConnReqs = [&]() {
525 // Sort the various requests by destination.
526 for (auto req : llvm::make_early_inc_range(
527 mod.getBodyBlock()->getOps<ServiceImplementConnReqOp>())) {
528 auto service = req.getServicePort().getModuleRef();
529 auto reqListIter = localImplReqs.find(service);
530 if (reqListIter != localImplReqs.end())
531 reqListIter->second.insert(req);
532 else if (anyServiceInst)
533 anyServiceInst->insert(req);
534 else
535 nonLocalReqs.insert(req);
536 }
537 };
538 // Bootstrap the sorting.
539 sortConnReqs();
540
541 // Replace each service instance with a generation request. If a service
542 // generator is registered, generate the server.
543 for (auto instOp :
544 llvm::make_early_inc_range(modBlock.getOps<ServiceInstanceOp>())) {
545 auto portReqs = localImplReqs[instOp.getServiceSymbolAttr()];
546 if (failed(replaceInst(instOp, portReqs.getArrayRef())))
547 return failure();
548
549 // Find any new requests which were created by a generator.
550 for (RequestConnectionOp req : llvm::make_early_inc_range(
551 mod.getBodyBlock()->getOps<RequestConnectionOp>()))
552 convertReq(req);
553 sortConnReqs();
554 }
555
556 // Surface all of the requests which cannot be fulfilled locally.
557 if (nonLocalReqs.empty())
558 return success();
559
560 if (auto mutableMod = dyn_cast<hw::HWMutableModuleLike>(mod.getOperation()))
561 return surfaceReqs(mutableMod, nonLocalReqs.getArrayRef());
562 return mod.emitOpError(
563 "Cannot surface requests through module without mutable ports");
564}
565
566LogicalResult ESIConnectServicesPass::replaceInst(
567 ServiceInstanceOp instOp, ArrayRef<ServiceImplementConnReqOp> portReqs) {
568 auto declSym = instOp.getServiceSymbolAttr();
569 ServiceDeclOpInterface decl;
570 if (declSym) {
571 decl = dyn_cast_or_null<ServiceDeclOpInterface>(
572 topLevelSyms.getDefinition(declSym));
573 if (!decl)
574 return instOp.emitOpError("Could not find service declaration ")
575 << declSym;
576 }
577
578 // Compute the result types for the new op -- the instance op's output types
579 // + the to_client types.
580 SmallVector<Type, 8> resultTypes(instOp.getResultTypes().begin(),
581 instOp.getResultTypes().end());
582 for (auto req : portReqs)
583 resultTypes.push_back(req.getBundleType());
584
585 // Create the generation request op.
586 OpBuilder b(instOp);
587 auto implOp = b.create<ServiceImplementReqOp>(
588 instOp.getLoc(), resultTypes, instOp.getAppIDAttr(),
589 instOp.getServiceSymbolAttr(), instOp.getImplTypeAttr(),
590 getStdService(declSym), instOp.getImplOptsAttr(), instOp.getOperands());
591 implOp->setDialectAttrs(instOp->getDialectAttrs());
592 Block &reqBlock = implOp.getPortReqs().emplaceBlock();
593
594 // Update the users.
595 for (auto [n, o] : llvm::zip(implOp.getResults(), instOp.getResults()))
596 o.replaceAllUsesWith(n);
597 unsigned instOpNumResults = instOp.getNumResults();
598 for (size_t idx = 0, e = portReqs.size(); idx < e; ++idx) {
599 ServiceImplementConnReqOp req = portReqs[idx];
600 req.getToClient().replaceAllUsesWith(
601 implOp.getResult(idx + instOpNumResults));
602 }
603
604 for (auto req : portReqs)
605 req->moveBefore(&reqBlock, reqBlock.end());
606
607 // Erase the instance first in case it consumes any channels or bundles. If it
608 // does, the service generator will fail to verify the IR as there will be
609 // multiple uses.
610 instOp.erase();
611
612 // Try to generate the service provider.
613 if (failed(genDispatcher.generate(implOp, decl)))
614 return implOp.emitOpError("failed to generate server");
615
616 return success();
617}
618
619LogicalResult
620ESIConnectServicesPass::surfaceReqs(hw::HWMutableModuleLike mod,
621 ArrayRef<ServiceImplementConnReqOp> reqs) {
622 auto *ctxt = mod.getContext();
623 Block *body = &mod->getRegion(0).front();
624
625 // Track initial operand/result counts and the new IO.
626 unsigned origNumInputs = mod.getNumInputPorts();
627 SmallVector<std::pair<unsigned, hw::PortInfo>> newInputs;
628
629 // Assemble a port name from an array.
630 auto getPortName = [&](ArrayAttr namePath) {
631 std::string portName;
632 llvm::raw_string_ostream nameOS(portName);
633 llvm::interleave(
634 namePath.getAsRange<AppIDAttr>(), nameOS,
635 [&](AppIDAttr appid) {
636 nameOS << appid.getName().getValue();
637 if (appid.getIndex())
638 nameOS << "_" << appid.getIndex();
639 },
640 ".");
641 return StringAttr::get(ctxt, nameOS.str());
642 };
643
644 for (auto req : reqs)
645 if (req->getParentWithTrait<OpTrait::IsIsolatedFromAbove>() != mod)
646 return req.emitOpError(
647 "Cannot surface requests through isolated from above ops");
648
649 // Insert new module input ESI ports.
650 for (auto req : reqs) {
651 newInputs.push_back(std::make_pair(
652 origNumInputs,
653 hw::PortInfo{{getPortName(req.getRelativeAppIDPathAttr()),
654 req.getBundleType(), hw::ModulePort::Direction::Input},
655 origNumInputs,
656 {},
657 req->getLoc()}));
658
659 // Replace uses with new block args which will correspond to said ports.
660 Value replValue = body->addArgument(req.getBundleType(), req->getLoc());
661 req.getToClient().replaceAllUsesWith(replValue);
662 }
663 mod.insertPorts(newInputs, {});
664
665 // Prepend a name to the instance tracking array.
666 auto prependNamePart = [&](ArrayAttr appIDPath, AppIDAttr appID) {
667 SmallVector<Attribute, 8> newAppIDPath;
668 newAppIDPath.push_back(appID);
669 newAppIDPath.append(appIDPath.begin(), appIDPath.end());
670 return ArrayAttr::get(appIDPath.getContext(), newAppIDPath);
671 };
672
673 // Update the module instantiations.
674 SmallVector<igraph::InstanceOpInterface, 1> newModuleInstantiations;
675 for (auto inst : moduleInstantiations[mod]) {
676 OpBuilder b(inst);
677
678 // Add new inputs for the new bundles being requested.
679 SmallVector<Value, 16> newOperands;
680 for (auto req : reqs) {
681 // If the instance has an AppID, prepend it.
682 ArrayAttr appIDPath = req.getRelativeAppIDPathAttr();
683 if (auto instAppID = dyn_cast_or_null<AppIDAttr>(
684 inst->getDiscardableAttr(AppIDAttr::AppIDAttrName)))
685 appIDPath = prependNamePart(appIDPath, instAppID);
686
687 // Clone the request.
688 auto clone = b.create<ServiceImplementConnReqOp>(
689 req.getLoc(), req.getToClient().getType(), req.getServicePortAttr(),
690 appIDPath);
691 clone->setDialectAttrs(req->getDialectAttrs());
692 newOperands.push_back(clone.getToClient());
693 }
694 inst->insertOperands(inst->getNumOperands(), newOperands);
695 // Set the names, if we know how.
696 if (auto hwInst = dyn_cast<hw::InstanceOp>(*inst))
697 hwInst.setArgNamesAttr(b.getArrayAttr(mod.getInputNames()));
698 }
699
700 // Erase the original requests since they have been cloned into the proper
701 // destination modules.
702 for (auto req : reqs)
703 req.erase();
704 return success();
705}
706
707std::unique_ptr<OperationPass<ModuleOp>>
709 return std::make_unique<ESIConnectServicesPass>();
710}
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)
Definition OM.cpp:116
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...
Definition SymCache.h:85
Class which "dispatches" a service implementation request to its specified generator.
Definition ESIServices.h:24
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
Definition ESIServices.h:52
std::function< LogicalResult(ServiceImplementReqOp, ServiceDeclOpInterface, ServiceImplRecordOp)> ServiceGeneratorFunc
Definition ESIServices.h:28
create(data_type, value)
Definition hw.py:433
create(struct_value, str field_name)
Definition hw.py:556
Definition sv.py:68
std::unique_ptr< OperationPass< ModuleOp > > createESIConnectServicesPass()
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
Definition esi.py:1
This holds the name, type, direction of a module's ports.