CIRCT 24.0.0git
Loading...
Searching...
No Matches
FIRRTLDialect.cpp
Go to the documentation of this file.
1//===- FIRRTLDialect.cpp - Implement the FIRRTL dialect -------------------===//
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 implements the FIRRTL dialect.
10//
11//===----------------------------------------------------------------------===//
12
14
19#include "mlir/IR/DialectImplementation.h"
20#include "llvm/ADT/APSInt.h"
21#include "llvm/ADT/SmallString.h"
22#include "llvm/ADT/TypeSwitch.h"
23
24using namespace circt;
25using namespace firrtl;
26
27//===----------------------------------------------------------------------===//
28// Dialect specification.
29//===----------------------------------------------------------------------===//
30
31void FIRRTLDialect::initialize() {
32 // Register types and attributes.
33 registerTypes();
34 registerAttributes();
35
36 addInterface<FIRRTLIntrinsicLoweringDialectInterface>();
37}
38
39/// Registered hook to materialize a single constant operation from a given
40/// attribute value with the desired resultant type. This method should use
41/// the provided builder to create the operation without changing the
42/// insertion position. The generated operation is expected to be constant
43/// like, i.e. single result, zero operands, non side-effecting, etc. On
44/// success, this hook should return the value generated to represent the
45/// constant value. Otherwise, it should return null on failure.
46Operation *FIRRTLDialect::materializeConstant(OpBuilder &builder,
47 Attribute value, Type type,
48 Location loc) {
49
50 // Boolean constants. Boolean attributes are always a special constant type
51 // like ClockType and ResetType. Since BoolAttrs are also IntegerAttrs, its
52 // important that this goes first.
53 if (auto attrValue = dyn_cast<BoolAttr>(value)) {
54 if (isa<BoolType>(type))
55 return BoolConstantOp::create(builder, loc, type, attrValue);
56 assert((isa<ClockType, AsyncResetType, ResetType>(type) &&
57 "BoolAttrs can only be materialized for special constant types."));
58 return SpecialConstantOp::create(builder, loc, type, attrValue);
59 }
60
61 // Integer constants.
62 if (auto attrValue = dyn_cast<IntegerAttr>(value)) {
63 if (isa<FIntegerType>(type))
64 return FIntegerConstantOp::create(builder, loc, type, attrValue);
65 // Integer attributes (ui1) might still be special constant types.
66 if (attrValue.getValue().getBitWidth() == 1 &&
67 isa<ClockType, AsyncResetType, ResetType>(type))
68 return SpecialConstantOp::create(
69 builder, loc, type,
70 builder.getBoolAttr(attrValue.getValue().isAllOnes()));
71
72 assert((!type_cast<IntType>(type).hasWidth() ||
73 (unsigned)type_cast<IntType>(type).getWidthOrSentinel() ==
74 attrValue.getValue().getBitWidth()) &&
75 "type/value width mismatch materializing constant");
76 return ConstantOp::create(builder, loc, type, attrValue);
77 }
78
79 // Aggregate constants.
80 if (auto arrayAttr = dyn_cast<ArrayAttr>(value)) {
81 if (isa<BundleType, FVectorType>(type))
82 return AggregateConstantOp::create(builder, loc, type, arrayAttr);
83 }
84
85 // String constants.
86 if (auto stringAttr = dyn_cast<StringAttr>(value)) {
87 if (type_isa<StringType>(type))
88 return StringConstantOp::create(builder, loc, type, stringAttr);
89 }
90
91 return nullptr;
92}
93
94// Provide implementations for the enums we use.
95#include "circt/Dialect/FIRRTL/FIRRTLEnums.cpp.inc"
96
97#include "circt/Dialect/FIRRTL/FIRRTLDialect.cpp.inc"
assert(baseType &&"element must be base type")
BaseTy type_cast(Type type)
bool type_isa(Type type)
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.