CIRCT  20.0.0git
HWDialect.cpp
Go to the documentation of this file.
1 //===- HWDialect.cpp - Implement the HW 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 HW dialect.
10 //
11 //===----------------------------------------------------------------------===//
12 
15 #include "circt/Dialect/HW/HWOps.h"
17 #include "mlir/IR/Builders.h"
18 #include "mlir/IR/BuiltinAttributes.h"
19 #include "mlir/IR/BuiltinTypes.h"
20 #include "mlir/IR/DialectImplementation.h"
21 #include "mlir/Transforms/InliningUtils.h"
22 
23 using namespace circt;
24 using namespace hw;
25 
26 //===----------------------------------------------------------------------===//
27 // Dialect specification.
28 //===----------------------------------------------------------------------===//
29 
30 // Pull in the dialect definition.
31 #include "circt/Dialect/HW/HWDialect.cpp.inc"
32 
33 namespace {
34 
35 // We implement the OpAsmDialectInterface so that HW dialect operations
36 // automatically interpret the name attribute on operations as their SSA name.
37 struct HWOpAsmDialectInterface : public OpAsmDialectInterface {
38  using OpAsmDialectInterface::OpAsmDialectInterface;
39 
40  /// Get a special name to use when printing the given operation. See
41  /// OpAsmInterface.td#getAsmResultNames for usage details and documentation.
42  void getAsmResultNames(Operation *op, OpAsmSetValueNameFn setNameFn) const {}
43 };
44 } // end anonymous namespace
45 
46 namespace {
47 /// This class defines the interface for handling inlining with HW operations.
48 struct HWInlinerInterface : public mlir::DialectInlinerInterface {
49  using mlir::DialectInlinerInterface::DialectInlinerInterface;
50 
51  bool isLegalToInline(Operation *op, Region *, bool,
52  mlir::IRMapping &) const final {
53  return isa<ConstantOp>(op) || isa<AggregateConstantOp>(op) ||
54  isa<EnumConstantOp>(op) || isa<BitcastOp>(op) ||
55  isa<ArrayCreateOp>(op) || isa<ArrayConcatOp>(op) ||
56  isa<ArraySliceOp>(op) || isa<ArrayGetOp>(op) ||
57  isa<StructCreateOp>(op) || isa<StructExplodeOp>(op) ||
58  isa<StructExtractOp>(op) || isa<StructInjectOp>(op) ||
59  isa<UnionCreateOp>(op) || isa<UnionExtractOp>(op);
60  }
61 
62  bool isLegalToInline(Region *, Region *, bool,
63  mlir::IRMapping &) const final {
64  return false;
65  }
66 };
67 } // end anonymous namespace
68 
69 void HWDialect::initialize() {
70  // Register types and attributes.
71  registerTypes();
72  registerAttributes();
73 
74  // Register operations.
75  addOperations<
76 #define GET_OP_LIST
77 #include "circt/Dialect/HW/HW.cpp.inc"
78  >();
79 
80  // Register interface implementations.
81  addInterfaces<HWOpAsmDialectInterface, HWInlinerInterface>();
82 }
83 
84 // Registered hook to materialize a single constant operation from a given
85 /// attribute value with the desired resultant type. This method should use
86 /// the provided builder to create the operation without changing the
87 /// insertion position. The generated operation is expected to be constant
88 /// like, i.e. single result, zero operands, non side-effecting, etc. On
89 /// success, this hook should return the value generated to represent the
90 /// constant value. Otherwise, it should return null on failure.
91 Operation *HWDialect::materializeConstant(OpBuilder &builder, Attribute value,
92  Type type, Location loc) {
93  // Integer constants can materialize into hw.constant
94  if (auto intType = dyn_cast<IntegerType>(type))
95  if (auto attrValue = dyn_cast<IntegerAttr>(value))
96  return builder.create<ConstantOp>(loc, type, attrValue);
97 
98  // Aggregate constants.
99  if (auto arrayAttr = dyn_cast<ArrayAttr>(value)) {
100  if (isa<StructType, ArrayType, UnpackedArrayType>(type))
101  return builder.create<AggregateConstantOp>(loc, type, arrayAttr);
102  }
103 
104  // Parameter expressions materialize into hw.param.value.
105  auto parentOp = builder.getBlock()->getParentOp();
106  auto curModule = dyn_cast<HWModuleOp>(parentOp);
107  if (!curModule)
108  curModule = parentOp->getParentOfType<HWModuleOp>();
109  if (curModule && isValidParameterExpression(value, curModule))
110  return builder.create<ParamValueOp>(loc, type, value);
111 
112  return nullptr;
113 }
def create(data_type, value)
Definition: hw.py:393
void getAsmResultNames(OpAsmSetValueNameFn setNameFn, StringRef instanceName, ArrayAttr resultNames, ValueRange results)
Suggest a name for each result value based on the saved result names attribute.
bool isValidParameterExpression(Attribute attr, Operation *module)
Return true if the specified attribute tree is made up of nodes that are valid in a parameter express...
Definition: HWOps.cpp:221
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
Definition: DebugAnalysis.h:21
Definition: hw.py:1
function_ref< void(Value, StringRef)> OpAsmSetValueNameFn
Definition: LLVM.h:182