CIRCT 23.0.0git
Loading...
Searching...
No Matches
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
24#include "mlir/IR/OpImplementation.h"
25#include "mlir/IR/SymbolTable.h"
26#include "mlir/Interfaces/CallInterfaces.h"
27#include "mlir/Interfaces/InferTypeOpInterface.h"
28#include "mlir/Interfaces/SideEffectInterfaces.h"
29
30namespace circt {
31namespace hw {
32class InstanceOp;
33class HWSymbolCache;
34class InnerRefAttr;
35} // namespace hw
36
37namespace sv {
38
39/// Return true if the specified operation is an expression.
40bool isExpression(Operation *op);
41
42/// Returns if the expression is known to be 2-state (binary)
43bool is2StateExpression(Value v);
44
45//===----------------------------------------------------------------------===//
46// CaseOp Support
47//===----------------------------------------------------------------------===//
48
49/// This describes the bit in a pattern, 0/1/x/z.
50enum class CasePatternBit { Zero = 0, One = 1, AnyX = 2, AnyZ = 3 };
51
52/// Return the letter for the specified pattern bit, e.g. "0", "1", "x" or "z".
54
55// This is provides convenient access to encode and decode a pattern.
57
58public:
61 virtual ~CasePattern() {}
62 CasePatternKind getKind() const { return kind; }
63
64 /// Return true if this pattern has an X.
65 virtual bool hasX() const { return false; }
66
67 /// Return true if this pattern has an Z.
68 virtual bool hasZ() const { return false; }
69
70 virtual Attribute attr() const = 0;
71
72private:
74};
75
77public:
78 using AttrType = mlir::UnitAttr;
79 CaseDefaultPattern(MLIRContext *ctx)
81 unitAttr = AttrType::get(ctx);
82 }
83
84 Attribute attr() const override { return unitAttr; }
85
86 static bool classof(const CasePattern *S) {
87 return S->getKind() == CPK_default;
88 }
89
90private:
91 // The default pattern is recognized by a UnitAttr. This is needed since we
92 // need to be able to determine the pattern type of a case based on an
93 // attribute attached to the sv.case op.
94 UnitAttr unitAttr;
95};
96
98public:
99 // Return the number of bits in the pattern.
100 size_t getWidth() const { return intAttr.getValue().getBitWidth() / 2; }
101
102 /// Return the specified bit, bit 0 is the least significant bit.
103 CasePatternBit getBit(size_t bitNumber) const;
104
105 bool hasX() const override;
106 bool hasZ() const override;
107
108 Attribute attr() const override { return intAttr; }
109
110 /// Get a CasePattern from a specified list of CasePatternBit. Bits are
111 /// specified in most least significant order - element zero is the least
112 /// significant bit.
113 CaseBitPattern(ArrayRef<CasePatternBit> bits, MLIRContext *context);
114
115 /// Get a CasePattern for the specified constant value.
116 CaseBitPattern(const APInt &value, MLIRContext *context);
117
118 /// Get a CasePattern with a correctly encoded attribute.
120
121 static bool classof(const CasePattern *S) { return S->getKind() == CPK_bit; }
122
123private:
124 IntegerAttr intAttr;
125};
126
128public:
129 // Get a CasePattern for the specified enum value attribute.
130 CaseEnumPattern(hw::EnumFieldAttr attr)
132
133 // Return the named value of this enumeration.
134 StringRef getFieldValue() const;
135
136 Attribute attr() const override { return enumAttr; }
137
138 static bool classof(const CasePattern *S) { return S->getKind() == CPK_enum; }
139
140private:
141 hw::EnumFieldAttr enumAttr;
142};
143
145public:
146 CaseExprPattern(MLIRContext *ctx)
148 exprAttr(CaseExprPatternAttr::get(ctx)) {}
149
150 Attribute attr() const override { return exprAttr; }
151
152 static bool classof(const CasePattern *S) { return S->getKind() == CPK_expr; }
153
154private:
155 // The expression pattern is recognized by a CaseExprPatternAttr
156 CaseExprPatternAttr exprAttr;
157};
158
159// This provides information about one case.
160struct CaseInfo {
161 std::unique_ptr<CasePattern> pattern;
162 Block *block;
163};
164
165//===----------------------------------------------------------------------===//
166// Other Supporting Logic
167//===----------------------------------------------------------------------===//
168
169/// This class provides a verifier for ops that are expecting their parent
170/// to be one of the given parent ops
171template <typename ConcreteType>
173 : public mlir::OpTrait::TraitBase<ConcreteType, VendorExtension> {
174public:
175 static LogicalResult verifyTrait(Operation *op) { return success(); }
176};
177
178/// Create nested ifdef operations for a list of macro symbols.
179/// For each macro, creates an ifdef with a then branch and an else branch.
180/// The then branch is provided by the thenCtor callback, which receives the
181/// index of the current macro (0-based).
182///
183/// Example:
184/// createNestedIfDefs({macro1Attr, macro2Attr}, ...});
185///
186/// Generates:
187/// `ifdef MACRO1
188/// // thenCtor(0)
189/// `else
190/// `ifdef MACRO2
191/// // thenCtor(1)
192/// `else
193/// // defaultCtor()
194/// `endif
195/// `endif
197 ArrayRef<StringAttr> macroSymbols,
198 llvm::function_ref<void(StringAttr, std::function<void()>,
199 std::function<void()>)>
200 ifdefCtor,
201 llvm::function_ref<void(size_t)> thenCtor,
202 llvm::function_ref<void()> defaultCtor);
203
204} // namespace sv
205} // namespace circt
206
207#define GET_OP_CLASSES
208#include "circt/Dialect/SV/SV.h.inc"
209
210#endif // CIRCT_DIALECT_SV_OPS_H
static std::unique_ptr< Context > context
CaseBitPattern(IntegerAttr attr)
Get a CasePattern with a correctly encoded attribute.
Definition SVOps.h:119
IntegerAttr intAttr
Definition SVOps.h:124
CasePatternBit getBit(size_t bitNumber) const
Return the specified bit, bit 0 is the least significant bit.
Definition SVOps.cpp:873
bool hasZ() const override
Return true if this pattern has an Z.
Definition SVOps.cpp:885
static bool classof(const CasePattern *S)
Definition SVOps.h:121
Attribute attr() const override
Definition SVOps.h:108
bool hasX() const override
Return true if this pattern has an X.
Definition SVOps.cpp:878
size_t getWidth() const
Definition SVOps.h:100
static bool classof(const CasePattern *S)
Definition SVOps.h:86
Attribute attr() const override
Definition SVOps.h:84
CaseDefaultPattern(MLIRContext *ctx)
Definition SVOps.h:79
mlir::UnitAttr AttrType
Definition SVOps.h:78
static bool classof(const CasePattern *S)
Definition SVOps.h:138
CaseEnumPattern(hw::EnumFieldAttr attr)
Definition SVOps.h:130
hw::EnumFieldAttr enumAttr
Definition SVOps.h:141
StringRef getFieldValue() const
Definition SVOps.cpp:952
Attribute attr() const override
Definition SVOps.h:136
static bool classof(const CasePattern *S)
Definition SVOps.h:152
CaseExprPatternAttr exprAttr
Definition SVOps.h:156
CaseExprPattern(MLIRContext *ctx)
Definition SVOps.h:146
Attribute attr() const override
Definition SVOps.h:150
CasePatternKind getKind() const
Definition SVOps.h:62
CasePattern(CasePatternKind kind)
Definition SVOps.h:60
const CasePatternKind kind
Definition SVOps.h:73
virtual ~CasePattern()
Definition SVOps.h:61
virtual bool hasZ() const
Return true if this pattern has an Z.
Definition SVOps.h:68
virtual Attribute attr() const =0
virtual bool hasX() const
Return true if this pattern has an X.
Definition SVOps.h:65
This class provides a verifier for ops that are expecting their parent to be one of the given parent ...
Definition SVOps.h:173
static LogicalResult verifyTrait(Operation *op)
Definition SVOps.h:175
CasePatternBit
This describes the bit in a pattern, 0/1/x/z.
Definition SVOps.h:50
bool isExpression(Operation *op)
Return true if the specified operation is an expression.
Definition SVOps.cpp:53
char getLetter(CasePatternBit bit)
Return the letter for the specified pattern bit, e.g. "0", "1", "x" or "z".
Definition SVOps.cpp:858
void createNestedIfDefs(ArrayRef< StringAttr > macroSymbols, llvm::function_ref< void(StringAttr, std::function< void()>, std::function< void()>)> ifdefCtor, llvm::function_ref< void(size_t)> thenCtor, llvm::function_ref< void()> defaultCtor)
Create nested ifdef operations for a list of macro symbols.
Definition SVOps.cpp:513
bool is2StateExpression(Value v)
Returns if the expression is known to be 2-state (binary)
Definition SVOps.cpp:43
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
Definition hw.py:1
Definition sv.py:1
std::unique_ptr< CasePattern > pattern
Definition SVOps.h:161