CIRCT  19.0.0git
SVOps.h
Go to the documentation of this file.
1 //===- SVOps.h - Declare SV dialect operations ------------------*- C++ -*-===//
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 declares the operation classes for the SV dialect.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef CIRCT_DIALECT_SV_OPS_H
14 #define CIRCT_DIALECT_SV_OPS_H
15 
17 #include "circt/Dialect/HW/HWOps.h"
21 #include "mlir/IR/OpImplementation.h"
22 #include "mlir/IR/SymbolTable.h"
23 #include "mlir/Interfaces/InferTypeOpInterface.h"
24 #include "mlir/Interfaces/SideEffectInterfaces.h"
25 
26 namespace circt {
27 namespace hw {
28 class InstanceOp;
29 class HWSymbolCache;
30 class InnerRefAttr;
31 } // namespace hw
32 
33 namespace sv {
34 
35 /// Return true if the specified operation is an expression.
36 bool isExpression(Operation *op);
37 
38 /// Returns if the expression is known to be 2-state (binary)
39 bool is2StateExpression(Value v);
40 
41 //===----------------------------------------------------------------------===//
42 // CaseOp Support
43 //===----------------------------------------------------------------------===//
44 
45 /// This describes the bit in a pattern, 0/1/x/z.
46 enum class CasePatternBit { Zero = 0, One = 1, AnyX = 2, AnyZ = 3 };
47 
48 /// Return the letter for the specified pattern bit, e.g. "0", "1", "x" or "z".
49 char getLetter(CasePatternBit bit);
50 
51 // This is provides convenient access to encode and decode a pattern.
52 class CasePattern {
53 
54 public:
57  virtual ~CasePattern() {}
58  CasePatternKind getKind() const { return kind; }
59 
60  /// Return true if this pattern has an X.
61  virtual bool hasX() const { return false; }
62 
63  /// Return true if this pattern has an Z.
64  virtual bool hasZ() const { return false; }
65 
66  virtual Attribute attr() const = 0;
67 
68 private:
70 };
71 
73 public:
74  using AttrType = mlir::UnitAttr;
75  CaseDefaultPattern(MLIRContext *ctx)
77  unitAttr = AttrType::get(ctx);
78  }
79 
80  Attribute attr() const override { return unitAttr; }
81 
82  static bool classof(const CasePattern *S) {
83  return S->getKind() == CPK_default;
84  }
85 
86 private:
87  // The default pattern is recognized by a UnitAttr. This is needed since we
88  // need to be able to determine the pattern type of a case based on an
89  // attribute attached to the sv.case op.
90  UnitAttr unitAttr;
91 };
92 
93 class CaseBitPattern : public CasePattern {
94 public:
95  // Return the number of bits in the pattern.
96  size_t getWidth() const { return intAttr.getValue().getBitWidth() / 2; }
97 
98  /// Return the specified bit, bit 0 is the least significant bit.
99  CasePatternBit getBit(size_t bitNumber) const;
100 
101  bool hasX() const override;
102  bool hasZ() const override;
103 
104  Attribute attr() const override { return intAttr; }
105 
106  /// Get a CasePattern from a specified list of CasePatternBit. Bits are
107  /// specified in most least significant order - element zero is the least
108  /// significant bit.
109  CaseBitPattern(ArrayRef<CasePatternBit> bits, MLIRContext *context);
110 
111  /// Get a CasePattern for the specified constant value.
112  CaseBitPattern(const APInt &value, MLIRContext *context);
113 
114  /// Get a CasePattern with a correctly encoded attribute.
116 
117  static bool classof(const CasePattern *S) { return S->getKind() == CPK_bit; }
118 
119 private:
120  IntegerAttr intAttr;
121 };
122 
123 class CaseEnumPattern : public CasePattern {
124 public:
125  // Get a CasePattern for the specified enum value attribute.
126  CaseEnumPattern(hw::EnumFieldAttr attr)
128 
129  // Return the named value of this enumeration.
130  StringRef getFieldValue() const;
131 
132  Attribute attr() const override { return enumAttr; }
133 
134  static bool classof(const CasePattern *S) { return S->getKind() == CPK_enum; }
135 
136 private:
137  hw::EnumFieldAttr enumAttr;
138 };
139 
140 // This provides information about one case.
141 struct CaseInfo {
142  std::unique_ptr<CasePattern> pattern;
143  Block *block;
144 };
145 
146 //===----------------------------------------------------------------------===//
147 // Other Supporting Logic
148 //===----------------------------------------------------------------------===//
149 
150 /// Return true if the specified operation is in a procedural region.
151 LogicalResult verifyInProceduralRegion(Operation *op);
152 /// Return true if the specified operation is not in a procedural region.
153 LogicalResult verifyInNonProceduralRegion(Operation *op);
154 
155 /// Signals that an operations regions are procedural.
156 template <typename ConcreteType>
158  : public mlir::OpTrait::TraitBase<ConcreteType, ProceduralRegion> {
159  static LogicalResult verifyTrait(Operation *op) {
160  return mlir::OpTrait::impl::verifyAtLeastNRegions(op, 1);
161  }
162 };
163 
164 /// This class verifies that the specified op is located in a procedural region.
165 template <typename ConcreteType>
167  : public mlir::OpTrait::TraitBase<ConcreteType, ProceduralOp> {
168 public:
169  static LogicalResult verifyTrait(Operation *op) {
170  return verifyInProceduralRegion(op);
171  }
172 };
173 
174 /// This class verifies that the specified op is not located in a procedural
175 /// region.
176 template <typename ConcreteType>
178  : public mlir::OpTrait::TraitBase<ConcreteType, NonProceduralOp> {
179 public:
180  static LogicalResult verifyTrait(Operation *op) {
181  return verifyInNonProceduralRegion(op);
182  }
183 };
184 
185 /// This class provides a verifier for ops that are expecting their parent
186 /// to be one of the given parent ops
187 template <typename ConcreteType>
189  : public mlir::OpTrait::TraitBase<ConcreteType, VendorExtension> {
190 public:
191  static LogicalResult verifyTrait(Operation *op) { return success(); }
192 };
193 
194 } // namespace sv
195 } // namespace circt
196 
197 #define GET_OP_CLASSES
198 #include "circt/Dialect/SV/SV.h.inc"
199 
200 #endif // CIRCT_DIALECT_SV_OPS_H
CaseBitPattern(IntegerAttr attr)
Get a CasePattern with a correctly encoded attribute.
Definition: SVOps.h:115
IntegerAttr intAttr
Definition: SVOps.h:120
CasePatternBit getBit(size_t bitNumber) const
Return the specified bit, bit 0 is the least significant bit.
Definition: SVOps.cpp:743
bool hasZ() const override
Return true if this pattern has an Z.
Definition: SVOps.cpp:755
static bool classof(const CasePattern *S)
Definition: SVOps.h:117
Attribute attr() const override
Definition: SVOps.h:104
CaseBitPattern(ArrayRef< CasePatternBit > bits, MLIRContext *context)
Get a CasePattern from a specified list of CasePatternBit.
Definition: SVOps.cpp:779
bool hasX() const override
Return true if this pattern has an X.
Definition: SVOps.cpp:748
size_t getWidth() const
Definition: SVOps.h:96
static bool classof(const CasePattern *S)
Definition: SVOps.h:82
Attribute attr() const override
Definition: SVOps.h:80
CaseDefaultPattern(MLIRContext *ctx)
Definition: SVOps.h:75
mlir::UnitAttr AttrType
Definition: SVOps.h:74
static bool classof(const CasePattern *S)
Definition: SVOps.h:134
CaseEnumPattern(hw::EnumFieldAttr attr)
Definition: SVOps.h:126
hw::EnumFieldAttr enumAttr
Definition: SVOps.h:137
StringRef getFieldValue() const
Definition: SVOps.cpp:818
Attribute attr() const override
Definition: SVOps.h:132
CasePatternKind getKind() const
Definition: SVOps.h:58
CasePattern(CasePatternKind kind)
Definition: SVOps.h:56
const CasePatternKind kind
Definition: SVOps.h:69
virtual ~CasePattern()
Definition: SVOps.h:57
virtual bool hasZ() const
Return true if this pattern has an Z.
Definition: SVOps.h:64
virtual Attribute attr() const =0
virtual bool hasX() const
Return true if this pattern has an X.
Definition: SVOps.h:61
This class verifies that the specified op is not located in a procedural region.
Definition: SVOps.h:178
static LogicalResult verifyTrait(Operation *op)
Definition: SVOps.h:180
This class verifies that the specified op is located in a procedural region.
Definition: SVOps.h:167
static LogicalResult verifyTrait(Operation *op)
Definition: SVOps.h:169
Signals that an operations regions are procedural.
Definition: SVOps.h:158
static LogicalResult verifyTrait(Operation *op)
Definition: SVOps.h:159
This class provides a verifier for ops that are expecting their parent to be one of the given parent ...
Definition: SVOps.h:189
static LogicalResult verifyTrait(Operation *op)
Definition: SVOps.h:191
Direction get(bool isOutput)
Returns an output direction if isOutput is true, otherwise returns an input direction.
Definition: CalyxOps.cpp:54
LogicalResult verifyInNonProceduralRegion(Operation *op)
Return true if the specified operation is not in a procedural region.
Definition: SVOps.cpp:63
CasePatternBit
This describes the bit in a pattern, 0/1/x/z.
Definition: SVOps.h:46
bool isExpression(Operation *op)
Return true if the specified operation is an expression.
Definition: SVOps.cpp:50
char getLetter(CasePatternBit bit)
Return the letter for the specified pattern bit, e.g. "0", "1", "x" or "z".
Definition: SVOps.cpp:728
bool is2StateExpression(Value v)
Returns if the expression is known to be 2-state (binary)
Definition: SVOps.cpp:40
LogicalResult verifyInProceduralRegion(Operation *op)
Return true if the specified operation is in a procedural region.
Definition: SVOps.cpp:56
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
Definition: DebugAnalysis.h:21
Definition: hw.py:1
Definition: sv.py:1
Block * block
Definition: SVOps.h:143
std::unique_ptr< CasePattern > pattern
Definition: SVOps.h:142