Skip to content

Commit 388787b

Browse files
committed
Remove includeHierarchy from convention attr. Clean up.
1 parent 2bb4500 commit 388787b

File tree

3 files changed

+94
-64
lines changed

3 files changed

+94
-64
lines changed

docs/Dialects/FIRRTL/FIRRTLAnnotations.md

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -323,12 +323,11 @@ Example:
323323

324324
### Convention
325325

326-
| Property | Type | Description |
327-
| ---------------- | ------ | ---------------------------------------------------- |
328-
| class | string | `circt.ConventionAnnotation` |
329-
| convention | string | `scalarized` |
330-
| target | string | Reference target |
331-
| includeHierarchy | bool | Apply the convention to all modules in the hierarchy |
326+
| Property | Type | Description |
327+
| ---------- | ------ | --------------------------------------- |
328+
| class | string | `circt.ConventionAnnotation` |
329+
| convention | string | `scalarized` |
330+
| target | string | Reference target |
332331

333332
Specify the port convention for a module. The port convention controls how a
334333
module's ports are transformed, and how that module can be instantiated, in the
@@ -338,35 +337,33 @@ The options are:
338337
- `scalarized`: Convert aggregate ports (i.e. vector or bundles) into multiple
339338
ground-typed ports.
340339

341-
`includeHierarchy` is optional and defaults to `false`, meaning that the
342-
convention is applied only to the specified module. If `includeHierarchy` is
343-
`true`, the convention is applied to all modules in the hierarchy. If there are
344-
multiple annotation instances that specify conventions, the `scalarized` convention
345-
takes precedence over the `internal` convention.
346-
347340
```json
348341
{
349342
"class": "circt.ConventionAnnotation",
350343
"convention": "scalarized",
351-
"target": "~Foo|Bar",
352-
"includeHierarchy": true
344+
"target": "~Foo|Bar/d:Baz"
353345
}
354346
```
355347

356348
### BodyTypeLoweringAnnotation
357349

358-
| Property | Type | Description |
359-
| ---------------- | ------ | ---------------------------------- |
360-
| class | string | `circt.BodyTypeLoweringAnnotation` |
361-
| convention | string | See `Convention` annotation |
362-
| target | string | See `Convention` annotation |
363-
| includeHierarchy | bool | See `Convention` annotation |
350+
| Property | Type | Description |
351+
| ------------------- | ------ | ---------------------------------------------------- |
352+
| class | string | `circt.BodyTypeLoweringAnnotation` |
353+
| convention | string | See `Convention` annotation |
354+
| target | string | See `Convention` annotation |
355+
| includeHierarchy | bool | Apply the convention to all modules in the hierarchy |
364356

365357
Specify the type lowering option for module internal signals.
366358
This is similar to the `Convention` annotation, but for internal signals
367359
rather than module ports. Refer to the `Convention` annotation for each
368360
property description.
369361

362+
When `includeHierarchy` is `false`, it indicates the convention is applied only to
363+
the specified module. If `includeHierarchy` is `true`, the convention is applied to
364+
all modules in the hierarchy. If there are multiple annotation instances that specify
365+
conventions, the `scalarized` convention takes precedence over the `internal` convention.
366+
370367
```json
371368
{
372369
"class": "circt.BodyTypeLoweringAnnotation",

lib/Dialect/FIRRTL/Transforms/LowerAnnotations.cpp

Lines changed: 74 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -268,17 +268,14 @@ static std::optional<Convention> parseConvention(llvm::StringRef str) {
268268
.Default(std::nullopt);
269269
}
270270

271-
template <bool IsConventionAnno>
272-
static LogicalResult
273-
applyConventionOrTypeLoweringAnno(const AnnoPathValue &target,
274-
DictionaryAttr anno, ApplyState &state) {
271+
static LogicalResult applyConventionAnno(const AnnoPathValue &target,
272+
DictionaryAttr anno,
273+
ApplyState &state) {
275274
auto *op = target.ref.getOp();
276275
auto loc = op->getLoc();
277276
auto error = [&]() {
278277
auto diag = mlir::emitError(loc);
279-
diag << (IsConventionAnno ? "circuit.ConventionAnnotation "
280-
: "circuit.TypeLoweringAnnotation ")
281-
<< " ";
278+
diag << "circuit.ConventionAnnotation ";
282279
return diag;
283280
};
284281

@@ -301,47 +298,85 @@ applyConventionOrTypeLoweringAnno(const AnnoPathValue &target,
301298

302299
auto convention = *conventionOpt;
303300

304-
if (convention == Convention::Internal)
305-
// Convention is internal by default so there is nothing to change
306-
return success();
307-
308-
auto includeHierarchy = anno.getAs<BoolAttr>("includeHierarchy");
309-
auto conventionAttr = ConventionAttr::get(op->getContext(), convention);
310-
auto setConvention = [&](Operation *moduleOp) {
311-
TypeSwitch<Operation *>(moduleOp)
312-
.Case<FModuleOp, FExtModuleOp>([&](auto moduleOp) {
313-
if (IsConventionAnno)
314-
moduleOp.setConventionAttr(conventionAttr);
315-
else
316-
moduleOp->setDiscardableAttr("body_type_lowering", conventionAttr);
317-
})
318-
.Default([](auto) {});
319-
};
320-
321301
if (auto moduleOp = dyn_cast<FModuleOp>(op)) {
322-
if (includeHierarchy && includeHierarchy.getValue()) {
323-
// If includeHierarchy is true, update the convention for all modules in
324-
// the hierarchy.
325-
for (auto *node :
326-
llvm::post_order(state.instancePathCache.instanceGraph[moduleOp])) {
327-
if (node && isa<FModuleOp, FExtModuleOp>(*node->getModule()))
328-
setConvention(node->getModule());
329-
}
330-
} else {
331-
// Update the convention.
332-
setConvention(moduleOp);
333-
}
302+
moduleOp.setConvention(convention);
334303
return success();
335304
}
336305

337306
if (auto extModuleOp = dyn_cast<FExtModuleOp>(op)) {
338-
setConvention(extModuleOp);
307+
extModuleOp.setConvention(convention);
339308
return success();
340309
}
341310

342311
return error() << "can only target to a module or extmodule";
343312
}
344313

314+
static LogicalResult applyBodyTypeLoweringAnno(const AnnoPathValue &target,
315+
DictionaryAttr anno,
316+
ApplyState &state) {
317+
auto *op = target.ref.getOp();
318+
auto loc = op->getLoc();
319+
auto error = [&]() {
320+
auto diag = mlir::emitError(loc);
321+
diag << typeLoweringAnnoClass;
322+
return diag;
323+
};
324+
325+
auto opTarget = dyn_cast<OpAnnoTarget>(target.ref);
326+
if (!opTarget)
327+
return error() << "must target a module object";
328+
329+
if (!target.isLocal())
330+
return error() << "must be local";
331+
332+
auto moduleOp = dyn_cast<FModuleOp>(op);
333+
334+
if (!moduleOp)
335+
return error() << "can only target to a module";
336+
337+
auto conventionStrAttr =
338+
tryGetAs<StringAttr>(anno, anno, "convention", loc, conventionAnnoClass);
339+
340+
if (!conventionStrAttr)
341+
return failure();
342+
343+
auto conventionStr = conventionStrAttr.getValue();
344+
auto conventionOpt = parseConvention(conventionStr);
345+
if (!conventionOpt)
346+
return error() << "unknown convention " << conventionStr;
347+
348+
auto convention = *conventionOpt;
349+
350+
if (convention == Convention::Internal)
351+
// Convention is internal by default so there is nothing to change
352+
return success();
353+
354+
auto conventionAttr = ConventionAttr::get(op->getContext(), convention);
355+
356+
// `includeHierarchy` only valid in BodyTypeLowering.
357+
bool includeHierarchy = false;
358+
if (auto includeHierarchyAttr = tryGetAs<BoolAttr>(
359+
anno, anno, "includeHierarchy", loc, conventionAnnoClass))
360+
includeHierarchy = includeHierarchyAttr.getValue();
361+
362+
if (includeHierarchy) {
363+
// If includeHierarchy is true, update the convention for all modules in
364+
// the hierarchy.
365+
for (auto *node :
366+
llvm::post_order(state.instancePathCache.instanceGraph[moduleOp])) {
367+
if (!node)
368+
continue;
369+
if (auto fmodule = dyn_cast<FModuleOp>(*node->getModule()))
370+
fmodule->setAttr("body_type_lowering", conventionAttr);
371+
}
372+
} else {
373+
// Update the convention.
374+
moduleOp->setAttr("body_type_lowering", conventionAttr);
375+
}
376+
377+
return success();
378+
}
379+
345380
static LogicalResult applyModulePrefixAnno(const AnnoPathValue &target,
346381
DictionaryAttr anno,
347382
ApplyState &state) {
@@ -583,10 +618,8 @@ static llvm::StringMap<AnnoRecord> annotationRecords{{
583618
{memTapPortClass, {stdResolve, applyWithoutTarget<true>}},
584619
{memTapBlackboxClass, {stdResolve, applyWithoutTarget<true>}},
585620
// Miscellaneous Annotations
586-
{conventionAnnoClass,
587-
{stdResolve, applyConventionOrTypeLoweringAnno<true>}},
588-
{typeLoweringAnnoClass,
589-
{stdResolve, applyConventionOrTypeLoweringAnno<false>}},
621+
{conventionAnnoClass, {stdResolve, applyConventionAnno}},
622+
{typeLoweringAnnoClass, {stdResolve, applyBodyTypeLoweringAnno}},
590623
{dontTouchAnnoClass,
591624
{stdResolve, applyWithoutTarget<true, true, WireOp, NodeOp, RegOp,
592625
RegResetOp, InstanceOp, MemOp, CombMemOp,

test/Dialect/FIRRTL/annotations.mlir

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -735,7 +735,7 @@ firrtl.circuit "Test" attributes {rawAnnotations = [
735735

736736
firrtl.circuit "Test" attributes {rawAnnotations =[
737737
{class = "circt.ConventionAnnotation", target = "~Test|Test", convention = "scalarized"},
738-
{class = "circt.BodyTypeLoweringAnnotation", target = "~Test|Test", convention = "scalarized"}
738+
{class = "circt.BodyTypeLoweringAnnotation", target = "~Test|Test", convention = "scalarized", includeHierarchy = false}
739739
]} {
740740
// CHECK: attributes {body_type_lowering = #firrtl<convention scalarized>, convention = #firrtl<convention scalarized>}
741741
firrtl.module @Test() attributes {convention = #firrtl<convention internal>} {}
@@ -744,15 +744,15 @@ firrtl.circuit "Test" attributes {rawAnnotations =[
744744
// -----
745745

746746
firrtl.circuit "Test" attributes {rawAnnotations = [
747-
{class = "circt.ConventionAnnotation", target = "~Test|Test", convention = "scalarized", includeHierarchy = true},
747+
{class = "circt.ConventionAnnotation", target = "~Test|Test", convention = "scalarized"},
748748
{class = "circt.BodyTypeLoweringAnnotation", target = "~Test|Test", convention = "scalarized", includeHierarchy = true}
749749
]} {
750750
// CHECK: @Test() attributes {body_type_lowering = #firrtl<convention scalarized>, convention = #firrtl<convention scalarized>}
751751
firrtl.module @Test() attributes {convention = #firrtl<convention internal>} {
752752
firrtl.instance child @Child()
753753
}
754754

755-
// CHECK: @Child() attributes {body_type_lowering = #firrtl<convention scalarized>, convention = #firrtl<convention scalarized>}
755+
// CHECK: @Child() attributes {body_type_lowering = #firrtl<convention scalarized>}
756756
firrtl.module @Child() attributes {convention = #firrtl<convention internal>} {}
757757

758758
// CHECK: @Child2() {

0 commit comments

Comments
 (0)