Loading [MathJax]/extensions/tex2jax.js
CIRCT 22.0.0git
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
TimingControls.cpp
Go to the documentation of this file.
1//===- TimingControl.cpp - Slang timing control conversion ----------------===//
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
10#include "slang/ast/TimingControl.h"
11#include "llvm/ADT/ScopeExit.h"
12
13using namespace circt;
14using namespace ImportVerilog;
15
16static ltl::ClockEdge convertEdgeKindLTL(const slang::ast::EdgeKind edge) {
17 using slang::ast::EdgeKind;
18 switch (edge) {
19 case EdgeKind::NegEdge:
20 return ltl::ClockEdge::Neg;
21 case EdgeKind::PosEdge:
22 return ltl::ClockEdge::Pos;
23 case EdgeKind::None:
24 // TODO: SV 16.16, what to do when no edge is specified?
25 // For now, assume all changes (two-valued should be the same as both
26 // edges)
27 case EdgeKind::BothEdges:
28 return ltl::ClockEdge::Both;
29 }
30 llvm_unreachable("all edge kinds handled");
31}
32
33static moore::Edge convertEdgeKind(const slang::ast::EdgeKind edge) {
34 using slang::ast::EdgeKind;
35 switch (edge) {
36 case EdgeKind::None:
37 return moore::Edge::AnyChange;
38 case EdgeKind::PosEdge:
39 return moore::Edge::PosEdge;
40 case EdgeKind::NegEdge:
41 return moore::Edge::NegEdge;
42 case EdgeKind::BothEdges:
43 return moore::Edge::BothEdges;
44 }
45 llvm_unreachable("all edge kinds handled");
46}
47
48// NOLINTBEGIN(misc-no-recursion)
49namespace {
50
51// Handle any of the event control constructs.
52struct EventControlVisitor {
53 Context &context;
54 Location loc;
55 OpBuilder &builder;
56
57 // Handle single signal events like `posedge x`, `negedge y iff z`, or `w`.
58 LogicalResult visit(const slang::ast::SignalEventControl &ctrl) {
59 auto edge = convertEdgeKind(ctrl.edge);
60 auto expr = context.convertRvalueExpression(ctrl.expr);
61 if (!expr)
62 return failure();
63 Value condition;
64 if (ctrl.iffCondition) {
65 condition = context.convertRvalueExpression(*ctrl.iffCondition);
66 condition = context.convertToBool(condition, Domain::TwoValued);
67 if (!condition)
68 return failure();
69 }
70 builder.create<moore::DetectEventOp>(loc, edge, expr, condition);
71 return success();
72 }
73
74 // Handle a list of signal events.
75 LogicalResult visit(const slang::ast::EventListControl &ctrl) {
76 for (const auto *event : ctrl.events) {
77 auto visitor = *this;
78 visitor.loc = context.convertLocation(event->sourceRange);
79 if (failed(event->visit(visitor)))
80 return failure();
81 }
82 return success();
83 }
84
85 // Emit an error for all other timing controls.
86 template <typename T>
87 LogicalResult visit(T &&ctrl) {
88 return mlir::emitError(loc)
89 << "unsupported event control: " << slang::ast::toString(ctrl.kind);
90 }
91};
92
93// Handle any of the delay control constructs.
94struct DelayControlVisitor {
95 Context &context;
96 Location loc;
97 OpBuilder &builder;
98
99 // Emit an error for all other timing controls.
100 template <typename T>
101 LogicalResult visit(T &&ctrl) {
102 return mlir::emitError(loc)
103 << "unsupported delay control: " << slang::ast::toString(ctrl.kind);
104 }
105};
106
107struct LTLClockControlVisitor {
108 Context &context;
109 Location loc;
110 OpBuilder &builder;
111 Value seqOrPro;
112
113 Value visit(const slang::ast::SignalEventControl &ctrl) {
114 auto edge = convertEdgeKindLTL(ctrl.edge);
115 auto expr = context.convertRvalueExpression(ctrl.expr);
116 if (!expr)
117 return Value{};
118 Value condition;
119 if (ctrl.iffCondition) {
120 condition = context.convertRvalueExpression(*ctrl.iffCondition);
121 condition = context.convertToBool(condition, Domain::TwoValued);
122 if (!condition)
123 return Value{};
124 }
125 expr = context.convertToI1(expr);
126 if (!expr)
127 return Value{};
128 return builder.create<ltl::ClockOp>(loc, seqOrPro, edge, expr);
129 }
130
131 template <typename T>
132 Value visit(T &&ctrl) {
133 mlir::emitError(loc, "unsupported LTL clock control: ")
134 << slang::ast::toString(ctrl.kind);
135 return Value{};
136 }
137};
138
139} // namespace
140
141// Entry point to timing control handling. This deals with the layer of repeats
142// that a timing control may be wrapped in, and also handles the implicit event
143// control which may appear at that point. For any event control a `WaitEventOp`
144// will be created and populated by `handleEventControl`. Any delay control will
145// be handled by `handleDelayControl`.
146static LogicalResult handleRoot(Context &context,
147 const slang::ast::TimingControl &ctrl,
148 moore::WaitEventOp &implicitWaitOp) {
149 auto &builder = context.builder;
150 auto loc = context.convertLocation(ctrl.sourceRange);
151
152 using slang::ast::TimingControlKind;
153 switch (ctrl.kind) {
154 // TODO: Actually implement a lowering for repeated event control. The main
155 // way to trigger this is through an intra-assignment timing control, which
156 // is not yet supported:
157 //
158 // a = repeat(3) @(posedge b) c;
159 //
160 // This will want to recursively call this function at the right insertion
161 // point to handle the timing control being repeated.
162 case TimingControlKind::RepeatedEvent:
163 return mlir::emitError(loc) << "unsupported repeated event control";
164
165 // Handle implicit events, i.e. `@*` and `@(*)`. This implicitly includes
166 // all variables read within the statement that follows after the event
167 // control. Since we haven't converted that statement yet, simply create and
168 // empty wait op and let `Context::convertTimingControl` populate it once
169 // the statement has been lowered.
170 case TimingControlKind::ImplicitEvent:
171 implicitWaitOp = builder.create<moore::WaitEventOp>(loc);
172 return success();
173
174 // Handle event control.
175 case TimingControlKind::SignalEvent:
176 case TimingControlKind::EventList: {
177 auto waitOp = builder.create<moore::WaitEventOp>(loc);
178 OpBuilder::InsertionGuard guard(builder);
179 builder.setInsertionPointToStart(&waitOp.getBody().emplaceBlock());
180 EventControlVisitor visitor{context, loc, builder};
181 return ctrl.visit(visitor);
182 }
183
184 // Handle delay control.
185 case TimingControlKind::Delay:
186 case TimingControlKind::Delay3:
187 case TimingControlKind::OneStepDelay:
188 case TimingControlKind::CycleDelay: {
189 DelayControlVisitor visitor{context, loc, builder};
190 return ctrl.visit(visitor);
191 }
192
193 default:
194 return mlir::emitError(loc, "unsupported timing control: ")
195 << slang::ast::toString(ctrl.kind);
196 }
197}
198
199LogicalResult
200Context::convertTimingControl(const slang::ast::TimingControl &ctrl,
201 const slang::ast::Statement &stmt) {
202 // Convert the timing control. Implicit event control will create a new empty
203 // `WaitEventOp` and assign it to `implicitWaitOp`. This op will be populated
204 // further down.
205 moore::WaitEventOp implicitWaitOp;
206 {
207 auto previousCallback = rvalueReadCallback;
208 auto done =
209 llvm::make_scope_exit([&] { rvalueReadCallback = previousCallback; });
210 // Reads happening as part of the event control should not be added to a
211 // surrounding implicit event control's list of implicitly observed
212 // variables.
213 rvalueReadCallback = nullptr;
214 if (failed(handleRoot(*this, ctrl, implicitWaitOp)))
215 return failure();
216 }
217
218 // Convert the statement. In case `implicitWaitOp` is set, we register a
219 // callback to collect all the variables read by the statement into
220 // `readValues`, such that we can populate the op with implicitly observed
221 // variables afterwards.
222 llvm::SmallSetVector<Value, 8> readValues;
223 {
224 auto previousCallback = rvalueReadCallback;
225 auto done =
226 llvm::make_scope_exit([&] { rvalueReadCallback = previousCallback; });
227 if (implicitWaitOp) {
228 rvalueReadCallback = [&](moore::ReadOp readOp) {
229 readValues.insert(readOp.getInput());
230 if (previousCallback)
231 previousCallback(readOp);
232 };
233 }
234 if (failed(convertStatement(stmt)))
235 return failure();
236 }
237
238 // Populate the implicit wait op with reads from the variables read by the
239 // statement.
240 if (implicitWaitOp) {
241 OpBuilder::InsertionGuard guard(builder);
242 builder.setInsertionPointToStart(&implicitWaitOp.getBody().emplaceBlock());
243 for (auto readValue : readValues) {
244 auto value =
245 builder.create<moore::ReadOp>(implicitWaitOp.getLoc(), readValue);
246 builder.create<moore::DetectEventOp>(
247 implicitWaitOp.getLoc(), moore::Edge::AnyChange, value, Value{});
248 }
249 }
250
251 return success();
252}
253
254Value Context::convertLTLTimingControl(const slang::ast::TimingControl &ctrl,
255 const Value &seqOrPro) {
256 auto &builder = this->builder;
257 auto loc = this->convertLocation(ctrl.sourceRange);
258 LTLClockControlVisitor visitor{*this, loc, builder, seqOrPro};
259 return ctrl.visit(visitor);
260}
261// NOLINTEND(misc-no-recursion)
static moore::Edge convertEdgeKind(const slang::ast::EdgeKind edge)
static ltl::ClockEdge convertEdgeKindLTL(const slang::ast::EdgeKind edge)
static LogicalResult handleRoot(Context &context, const slang::ast::TimingControl &ctrl, moore::WaitEventOp &implicitWaitOp)
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
A helper class to facilitate the conversion from a Slang AST to MLIR operations.
Value convertToI1(Value value)
Helper function to convert a value to a MLIR I1 value.
Value convertLTLTimingControl(const slang::ast::TimingControl &ctrl, const Value &seqOrPro)
OpBuilder builder
The builder used to create IR operations.
std::function< void(moore::ReadOp)> rvalueReadCallback
A listener called for every variable or net being read.
Value convertToBool(Value value)
Helper function to convert a value to its "truthy" boolean value.
Value convertRvalueExpression(const slang::ast::Expression &expr, Type requiredType={})
LogicalResult convertStatement(const slang::ast::Statement &stmt)
Location convertLocation(slang::SourceLocation loc)
Convert a slang SourceLocation into an MLIR Location.