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