CIRCT 22.0.0git
Loading...
Searching...
No Matches
AnnotateInputOnlyModules.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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// This file defines the AnnotateInputOnlyModules pass.
10//
11//===----------------------------------------------------------------------===//
12
18#include "mlir/Pass/Pass.h"
19
20#define DEBUG_TYPE "firrtl-annotate-input-only-modules"
21
22namespace circt {
23namespace firrtl {
24#define GEN_PASS_DEF_ANNOTATEINPUTONLYMODULES
25#include "circt/Dialect/FIRRTL/Passes.h.inc"
26} // namespace firrtl
27} // namespace circt
28
29using namespace circt;
30using namespace firrtl;
31
32namespace {
33struct AnnotateInputOnlyModulesPass
34 : public circt::firrtl::impl::AnnotateInputOnlyModulesBase<
35 AnnotateInputOnlyModulesPass> {
36 void runOnOperation() override;
37
38 LogicalResult initialize(MLIRContext *context) override {
39 // Cache the inline annotation.
40 inlineAnno = DictionaryAttr::getWithSorted(
41 context, {{StringAttr::get(context, "class"),
42 StringAttr::get(context, inlineAnnoClass)}});
43 return success();
44 }
45
46 mlir::DictionaryAttr inlineAnno;
47};
48
49} // end anonymous namespace
50
51void AnnotateInputOnlyModulesPass::runOnOperation() {
52 auto circuit = getOperation();
53 bool changed = false;
54 auto &instanceInfo = getAnalysis<InstanceInfo>();
55 for (auto module : circuit.getOps<FModuleOp>()) {
56 // Input only modules.
57 if (!instanceInfo.anyInstanceInEffectiveDesign(module) || module.isPublic())
58 continue;
59
60 // Check if the module has only input ports (no output ports)
61 bool hasHardwareOutputPort =
62 llvm::any_of(module.getPorts(), [&](auto port) {
63 return port.direction == Direction::Out &&
64 type_isa<FIRRTLBaseType>(port.type);
65 });
66
67 // If the module has only input ports, add InlineAnnotation
68 if (hasHardwareOutputPort)
69 continue;
70
71 AnnotationSet annos(module);
72
73 // Check if InlineAnnotation or DontTouchAnnotation exists
74 if (annos.hasAnnotation(inlineAnnoClass) || annos.hasDontTouch())
75 continue;
76
77 LLVM_DEBUG(llvm::dbgs() << "Annotating inline annotation "
78 << module.getModuleName() << "\n");
79
80 // Create InlineAnnotation
81 annos.addAnnotations(ArrayRef<Attribute>{inlineAnno});
82 annos.applyToOperation(module);
83
84 ++numAnnotated;
85 changed = true;
86 }
87
88 if (!changed)
89 return markAllAnalysesPreserved();
90
91 markAnalysesPreserved<igraph::InstanceGraph, InstanceInfo>();
92}
static std::unique_ptr< Context > context
This class provides a read-only projection over the MLIR attributes that represent a set of annotatio...
constexpr const char * inlineAnnoClass
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.