CIRCT 22.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 // Register operations.
37 addOperations<
38#define GET_OP_LIST
39#include "circt/Dialect/FIRRTL/FIRRTL.cpp.inc"
40 >();
41
42 addInterface<FIRRTLIntrinsicLoweringDialectInterface>();
43}
44
45/// Registered hook to materialize a single constant operation from a given
46/// attribute value with the desired resultant type. This method should use
47/// the provided builder to create the operation without changing the
48/// insertion position. The generated operation is expected to be constant
49/// like, i.e. single result, zero operands, non side-effecting, etc. On
50/// success, this hook should return the value generated to represent the
51/// constant value. Otherwise, it should return null on failure.
52Operation *FIRRTLDialect::materializeConstant(OpBuilder &builder,
53 Attribute value, Type type,
54 Location loc) {
55
56 // Boolean constants. Boolean attributes are always a special constant type
57 // like ClockType and ResetType. Since BoolAttrs are also IntegerAttrs, its
58 // important that this goes first.
59 if (auto attrValue = dyn_cast<BoolAttr>(value)) {
60 if (isa<BoolType>(type))
61 return BoolConstantOp::create(builder, loc, type, attrValue);
62 assert((isa<ClockType, AsyncResetType, ResetType>(type) &&
63 "BoolAttrs can only be materialized for special constant types."));
64 return SpecialConstantOp::create(builder, loc, type, attrValue);
65 }
66
67 // Integer constants.
68 if (auto attrValue = dyn_cast<IntegerAttr>(value)) {
69 if (isa<FIntegerType>(type))
70 return FIntegerConstantOp::create(builder, loc, type, attrValue);
71 // Integer attributes (ui1) might still be special constant types.
72 if (attrValue.getValue().getBitWidth() == 1 &&
73 isa<ClockType, AsyncResetType, ResetType>(type))
74 return SpecialConstantOp::create(
75 builder, loc, type,
76 builder.getBoolAttr(attrValue.getValue().isAllOnes()));
77
78 assert((!type_cast<IntType>(type).hasWidth() ||
79 (unsigned)type_cast<IntType>(type).getWidthOrSentinel() ==
80 attrValue.getValue().getBitWidth()) &&
81 "type/value width mismatch materializing constant");
82 return ConstantOp::create(builder, loc, type, attrValue);
83 }
84
85 // Aggregate constants.
86 if (auto arrayAttr = dyn_cast<ArrayAttr>(value)) {
87 if (isa<BundleType, FVectorType>(type))
88 return AggregateConstantOp::create(builder, loc, type, arrayAttr);
89 }
90
91 // String constants.
92 if (auto stringAttr = dyn_cast<StringAttr>(value)) {
93 if (type_isa<StringType>(type))
94 return StringConstantOp::create(builder, loc, type, stringAttr);
95 }
96
97 return nullptr;
98}
99
100// Provide implementations for the enums we use.
101#include "circt/Dialect/FIRRTL/FIRRTLEnums.cpp.inc"
102
103#include "circt/Dialect/FIRRTL/FIRRTLDialect.cpp.inc"
assert(baseType &&"element must be base type")
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.