CIRCT  19.0.0git
BuiltinEmissionPatterns.cpp
Go to the documentation of this file.
1 //===- BuiltinEmissionPatterns.cpp - Builtin Dialect Emission Patterns ----===//
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 implements the emission patterns for the builtin dialect.
10 //
11 //===----------------------------------------------------------------------===//
12 
14 #include "../EmissionPrinter.h"
15 #include "mlir/IR/BuiltinOps.h"
16 #include "llvm/ADT/SmallString.h"
17 
18 using namespace mlir;
19 using namespace circt;
20 using namespace circt::ExportSystemC;
21 
22 //===----------------------------------------------------------------------===//
23 // Operation emission patterns.
24 //===----------------------------------------------------------------------===//
25 
26 namespace {
27 
28 /// Emit the builtin module op by emitting all children in sequence. As a
29 /// result, we don't have to hard-code the behavior in ExportSytemC.
30 struct ModuleEmitter : OpEmissionPattern<ModuleOp> {
31  using OpEmissionPattern::OpEmissionPattern;
32  void emitStatement(ModuleOp op, EmissionPrinter &p) override {
33  auto scope = p.getOstream().scope("", "", false);
34  p.emitRegion(op.getRegion(), scope);
35  }
36 };
37 
38 } // namespace
39 
40 //===----------------------------------------------------------------------===//
41 // Type emission patterns.
42 //===----------------------------------------------------------------------===//
43 
44 namespace {
45 
46 /// Emit the builtin integer type to native C integer types.
47 struct IntegerTypeEmitter : TypeEmissionPattern<IntegerType> {
48  bool match(Type type) override {
49  if (!isa<IntegerType>(type))
50  return false;
51 
52  unsigned bw = type.getIntOrFloatBitWidth();
53  return bw == 1 || bw == 8 || bw == 16 || bw == 32 || bw == 64;
54  }
55 
56  void emitType(IntegerType type, EmissionPrinter &p) override {
57  unsigned bitWidth = type.getIntOrFloatBitWidth();
58  switch (bitWidth) {
59  case 1:
60  p << "bool";
61  break;
62  case 8:
63  case 16:
64  case 32:
65  case 64:
66  p << (type.isSigned() ? "" : "u") << "int" << bitWidth << "_t";
67  break;
68  default:
69  p.emitError("in the IntegerType emitter all cases allowed by the 'match' "
70  "function must be covered")
71  << ", got uncovered case " << type;
72  }
73  }
74 };
75 
76 /// Emit a builtin index type as 'size_t'.
77 struct IndexTypeEmitter : TypeEmissionPattern<IndexType> {
78  void emitType(IndexType type, EmissionPrinter &p) override { p << "size_t"; }
79 };
80 
81 } // namespace
82 
83 namespace {
84 
85 /// Emit a builtin string attribute as a C string literal including the
86 /// quotation marks.
87 struct StringAttrEmitter : AttrEmissionPattern<StringAttr> {
88  void emitAttr(StringAttr attr, EmissionPrinter &p) override {
89  attr.print(p.getOstream());
90  }
91 };
92 
93 /// Emit a builtin integer attribute as an integer literal. Integers with a
94 /// bitwidth of one are emitted as boolean literals 'true' and 'false'.
95 struct IntegerAttrEmitter : AttrEmissionPattern<IntegerAttr> {
96  void emitAttr(IntegerAttr attr, EmissionPrinter &p) override {
97  auto val = attr.getValue();
98 
99  if (val.getBitWidth() == 1) {
100  p << (val.getBoolValue() ? "true" : "false");
101  } else {
102  bool isSigned = false;
103  if (auto integer = dyn_cast<IntegerType>(attr.getType()))
104  isSigned = integer.isSigned();
105 
106  SmallString<128> strValue;
107  val.toString(strValue, 10, isSigned);
108  p << strValue;
109  }
110  }
111 };
112 
113 } // namespace
114 
115 //===----------------------------------------------------------------------===//
116 // Register Operation and Type emission patterns.
117 //===----------------------------------------------------------------------===//
118 
120  OpEmissionPatternSet &patterns, MLIRContext *context) {
121  patterns.add<ModuleEmitter>(context);
122 }
123 
126  patterns.add<IntegerTypeEmitter, IndexTypeEmitter>();
127 }
128 
131  patterns.add<StringAttrEmitter, IntegerAttrEmitter>();
132 }
This class collects a set of emission patterns with base type 'PatternTy'.
This is intended to be the driving class for all pattern-based IR emission.
void emitRegion(Region &region)
Emit the given region to the ostream associated with this printer.
mlir::raw_indented_ostream & getOstream() const
InFlightDiagnostic emitError(Operation *op, const Twine &message)
Emit an error on the operation and fail emission.
void populateBuiltinOpEmitters(OpEmissionPatternSet &patterns, MLIRContext *context)
Register Builtin operation emission patterns.
void populateBuiltinAttrEmitters(AttrEmissionPatternSet &patterns)
Register Builtin attribute emission patterns.
void populateBuiltinTypeEmitters(TypeEmissionPatternSet &patterns)
Register Builtin type emission patterns.
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
Definition: DebugAnalysis.h:21
This is a convenience class providing default implementations for attribute emission patterns.
This is a convenience class providing default implementations for operation emission patterns.
This is a convenience class providing default implementations for type emission patterns.