CIRCT  19.0.0git
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 
18 #include "circt/Support/FieldRef.h"
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 
24 using namespace circt;
25 using namespace firrtl;
26 
27 //===----------------------------------------------------------------------===//
28 // Dialect specification.
29 //===----------------------------------------------------------------------===//
30 
31 void 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.
52 Operation *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 builder.create<BoolConstantOp>(loc, type, attrValue);
62  assert((isa<ClockType, AsyncResetType, ResetType>(type) &&
63  "BoolAttrs can only be materialized for special constant types."));
64  return builder.create<SpecialConstantOp>(loc, type, attrValue);
65  }
66 
67  // Integer constants.
68  if (auto attrValue = dyn_cast<IntegerAttr>(value)) {
69  if (isa<FIntegerType>(type))
70  return builder.create<FIntegerConstantOp>(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 builder.create<SpecialConstantOp>(
75  loc, type, builder.getBoolAttr(attrValue.getValue().isAllOnes()));
76 
77  assert((!type_cast<IntType>(type).hasWidth() ||
78  (unsigned)type_cast<IntType>(type).getWidthOrSentinel() ==
79  attrValue.getValue().getBitWidth()) &&
80  "type/value width mismatch materializing constant");
81  return builder.create<ConstantOp>(loc, type, attrValue);
82  }
83 
84  // Aggregate constants.
85  if (auto arrayAttr = dyn_cast<ArrayAttr>(value)) {
86  if (isa<BundleType, FVectorType>(type))
87  return builder.create<AggregateConstantOp>(loc, type, arrayAttr);
88  }
89 
90  // String constants.
91  if (auto stringAttr = dyn_cast<StringAttr>(value)) {
92  if (type_isa<StringType>(type))
93  return builder.create<StringConstantOp>(loc, type, stringAttr);
94  }
95 
96  return nullptr;
97 }
98 
99 // Provide implementations for the enums we use.
100 #include "circt/Dialect/FIRRTL/FIRRTLEnums.cpp.inc"
101 
102 #include "circt/Dialect/FIRRTL/FIRRTLDialect.cpp.inc"
assert(baseType &&"element must be base type")
Builder builder
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
Definition: DebugAnalysis.h:21