-
Notifications
You must be signed in to change notification settings - Fork 466
Expand file tree
/
Copy pathAssertionExpr.cpp
More file actions
525 lines (470 loc) · 19.1 KB
/
AssertionExpr.cpp
File metadata and controls
525 lines (470 loc) · 19.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
//===- AssertionExpr.cpp - Slang assertion expression conversion ----------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "slang/ast/expressions/AssertionExpr.h"
#include "ImportVerilogInternals.h"
#include "circt/Dialect/Comb/CombDialect.h"
#include "circt/Dialect/Comb/CombOps.h"
#include "circt/Dialect/LTL/LTLOps.h"
#include "circt/Dialect/Moore/MooreOps.h"
#include "circt/Support/FVInt.h"
#include "circt/Support/LLVM.h"
#include "mlir/IR/BuiltinAttributes.h"
#include "mlir/Support/LLVM.h"
#include "slang/analysis/AnalysisManager.h"
#include "slang/analysis/AnalyzedAssertion.h"
#include "slang/ast/ASTVisitor.h"
#include "slang/ast/SystemSubroutine.h"
#include "slang/parsing/KnownSystemName.h"
#include <optional>
#include <utility>
using namespace circt;
using namespace ImportVerilog;
// NOLINTBEGIN(misc-no-recursion)
namespace {
struct AssertionExprVisitor {
Context &context;
Location loc;
OpBuilder &builder;
AssertionExprVisitor(Context &context, Location loc)
: context(context), loc(loc), builder(context.builder) {}
/// Helper to convert a range (min, optional max) to MLIR integer attributes
std::pair<mlir::IntegerAttr, mlir::IntegerAttr>
convertRangeToAttrs(uint32_t min,
std::optional<uint32_t> max = std::nullopt) {
auto minAttr = builder.getI64IntegerAttr(min);
mlir::IntegerAttr rangeAttr;
if (max.has_value()) {
rangeAttr = builder.getI64IntegerAttr(max.value() - min);
}
return {minAttr, rangeAttr};
}
/// Add repetition operation to a sequence
Value createRepetition(Location loc,
const slang::ast::SequenceRepetition &repetition,
Value &inputSequence) {
// Extract cycle range
auto [minRepetitions, repetitionRange] =
convertRangeToAttrs(repetition.range.min, repetition.range.max);
using slang::ast::SequenceRepetition;
// Check if repetition range is required
if ((repetition.kind == SequenceRepetition::Nonconsecutive ||
repetition.kind == SequenceRepetition::GoTo) &&
!repetitionRange) {
mlir::emitError(loc,
repetition.kind == SequenceRepetition::Nonconsecutive
? "Nonconsecutive repetition requires a maximum value"
: "GoTo repetition requires a maximum value");
return {};
}
switch (repetition.kind) {
case SequenceRepetition::Consecutive:
return ltl::RepeatOp::create(builder, loc, inputSequence, minRepetitions,
repetitionRange);
case SequenceRepetition::Nonconsecutive:
return ltl::NonConsecutiveRepeatOp::create(
builder, loc, inputSequence, minRepetitions, repetitionRange);
case SequenceRepetition::GoTo:
return ltl::GoToRepeatOp::create(builder, loc, inputSequence,
minRepetitions, repetitionRange);
}
llvm_unreachable("All enum values handled in switch");
}
Value visit(const slang::ast::SimpleAssertionExpr &expr) {
// Handle expression
auto value = context.convertRvalueExpression(expr.expr);
if (!value)
return {};
auto loc = context.convertLocation(expr.expr.sourceRange);
auto valueType = value.getType();
// For assertion instances the value is already the expected type, convert
// boolean value
if (!mlir::isa<ltl::SequenceType, ltl::PropertyType, mlir::IntegerType>(
valueType)) {
value = context.convertToI1(value);
}
if (!value)
return {};
// Handle repetition
// The optional repetition is empty, return the converted expression
if (!expr.repetition.has_value()) {
return value;
}
// There is a repetition, embed the expression into the kind of given
// repetition
return createRepetition(loc, expr.repetition.value(), value);
}
Value visit(const slang::ast::SequenceConcatExpr &expr) {
// Create a sequence of delayed operations, combined with a concat operation
assert(!expr.elements.empty());
SmallVector<Value> sequenceElements;
for (const auto &concatElement : expr.elements) {
Value sequenceValue =
context.convertAssertionExpression(*concatElement.sequence, loc);
if (!sequenceValue)
return {};
[[maybe_unused]] Type valueType = sequenceValue.getType();
assert(valueType.isInteger(1) || mlir::isa<ltl::SequenceType>(valueType));
auto [delayMin, delayRange] =
convertRangeToAttrs(concatElement.delay.min, concatElement.delay.max);
auto delayedSequence = ltl::DelayOp::create(builder, loc, sequenceValue,
delayMin, delayRange);
sequenceElements.push_back(delayedSequence);
}
return builder.createOrFold<ltl::ConcatOp>(loc, sequenceElements);
}
Value visit(const slang::ast::UnaryAssertionExpr &expr) {
auto value = context.convertAssertionExpression(expr.expr, loc);
if (!value)
return {};
using slang::ast::UnaryAssertionOperator;
switch (expr.op) {
case UnaryAssertionOperator::Not:
return ltl::NotOp::create(builder, loc, value);
case UnaryAssertionOperator::SEventually:
if (expr.range.has_value()) {
mlir::emitError(loc, "Strong eventually with range not supported");
return {};
} else {
return ltl::EventuallyOp::create(builder, loc, value);
}
case UnaryAssertionOperator::Always: {
std::pair<mlir::IntegerAttr, mlir::IntegerAttr> attr = {
builder.getI64IntegerAttr(0), mlir::IntegerAttr{}};
if (expr.range.has_value()) {
attr =
convertRangeToAttrs(expr.range.value().min, expr.range.value().max);
}
return ltl::RepeatOp::create(builder, loc, value, attr.first,
attr.second);
}
case UnaryAssertionOperator::NextTime: {
auto minRepetitions = builder.getI64IntegerAttr(1);
if (expr.range.has_value()) {
minRepetitions = builder.getI64IntegerAttr(expr.range.value().min);
}
return ltl::DelayOp::create(builder, loc, value, minRepetitions,
builder.getI64IntegerAttr(0));
}
case UnaryAssertionOperator::Eventually:
case UnaryAssertionOperator::SNextTime:
case UnaryAssertionOperator::SAlways:
mlir::emitError(loc, "unsupported unary operator: ")
<< slang::ast::toString(expr.op);
return {};
}
llvm_unreachable("All enum values handled in switch");
}
Value visit(const slang::ast::BinaryAssertionExpr &expr) {
auto lhs = context.convertAssertionExpression(expr.left, loc);
auto rhs = context.convertAssertionExpression(expr.right, loc);
if (!lhs || !rhs)
return {};
SmallVector<Value, 2> operands = {lhs, rhs};
using slang::ast::BinaryAssertionOperator;
switch (expr.op) {
case BinaryAssertionOperator::And:
return ltl::AndOp::create(builder, loc, operands);
case BinaryAssertionOperator::Or:
return ltl::OrOp::create(builder, loc, operands);
case BinaryAssertionOperator::Intersect:
return ltl::IntersectOp::create(builder, loc, operands);
case BinaryAssertionOperator::Throughout: {
auto lhsRepeat = ltl::RepeatOp::create(
builder, loc, lhs, builder.getI64IntegerAttr(0), mlir::IntegerAttr{});
return ltl::IntersectOp::create(builder, loc,
SmallVector<Value, 2>{lhsRepeat, rhs});
}
case BinaryAssertionOperator::Within: {
auto constOne =
hw::ConstantOp::create(builder, loc, builder.getI1Type(), 1);
auto oneRepeat = ltl::RepeatOp::create(builder, loc, constOne,
builder.getI64IntegerAttr(0),
mlir::IntegerAttr{});
auto repeatDelay = ltl::DelayOp::create(builder, loc, oneRepeat,
builder.getI64IntegerAttr(1),
builder.getI64IntegerAttr(0));
auto lhsDelay =
ltl::DelayOp::create(builder, loc, lhs, builder.getI64IntegerAttr(1),
builder.getI64IntegerAttr(0));
auto combined = ltl::ConcatOp::create(
builder, loc, SmallVector<Value, 3>{repeatDelay, lhsDelay, constOne});
return ltl::IntersectOp::create(builder, loc,
SmallVector<Value, 2>{combined, rhs});
}
case BinaryAssertionOperator::Iff: {
auto ored = ltl::OrOp::create(builder, loc, operands);
auto notOred = ltl::NotOp::create(builder, loc, ored);
auto anded = ltl::AndOp::create(builder, loc, operands);
return ltl::OrOp::create(builder, loc,
SmallVector<Value, 2>{notOred, anded});
}
case BinaryAssertionOperator::Until:
return ltl::UntilOp::create(builder, loc, operands);
case BinaryAssertionOperator::UntilWith: {
auto untilOp = ltl::UntilOp::create(builder, loc, operands);
auto andOp = ltl::AndOp::create(builder, loc, operands);
auto notUntil = ltl::NotOp::create(builder, loc, untilOp);
return ltl::OrOp::create(builder, loc,
SmallVector<Value, 2>{notUntil, andOp});
}
case BinaryAssertionOperator::Implies: {
auto notLhs = ltl::NotOp::create(builder, loc, lhs);
return ltl::OrOp::create(builder, loc,
SmallVector<Value, 2>{notLhs, rhs});
}
case BinaryAssertionOperator::OverlappedImplication:
return ltl::ImplicationOp::create(builder, loc, operands);
case BinaryAssertionOperator::NonOverlappedImplication: {
auto constOne =
hw::ConstantOp::create(builder, loc, builder.getI1Type(), 1);
auto lhsDelay =
ltl::DelayOp::create(builder, loc, lhs, builder.getI64IntegerAttr(1),
builder.getI64IntegerAttr(0));
auto antecedent = ltl::ConcatOp::create(
builder, loc, SmallVector<Value, 2>{lhsDelay, constOne});
return ltl::ImplicationOp::create(builder, loc,
SmallVector<Value, 2>{antecedent, rhs});
}
case BinaryAssertionOperator::OverlappedFollowedBy: {
auto notRhs = ltl::NotOp::create(builder, loc, rhs);
auto implication = ltl::ImplicationOp::create(
builder, loc, SmallVector<Value, 2>{lhs, notRhs});
return ltl::NotOp::create(builder, loc, implication);
}
case BinaryAssertionOperator::NonOverlappedFollowedBy: {
auto constOne =
hw::ConstantOp::create(builder, loc, builder.getI1Type(), 1);
auto notRhs = ltl::NotOp::create(builder, loc, rhs);
auto lhsDelay =
ltl::DelayOp::create(builder, loc, lhs, builder.getI64IntegerAttr(1),
builder.getI64IntegerAttr(0));
auto antecedent = ltl::ConcatOp::create(
builder, loc, SmallVector<Value, 2>{lhsDelay, constOne});
auto implication = ltl::ImplicationOp::create(
builder, loc, SmallVector<Value, 2>{antecedent, notRhs});
return ltl::NotOp::create(builder, loc, implication);
}
case BinaryAssertionOperator::SUntil:
case BinaryAssertionOperator::SUntilWith:
mlir::emitError(loc, "unsupported binary operator: ")
<< slang::ast::toString(expr.op);
return {};
}
llvm_unreachable("All enum values handled in switch");
}
Value visit(const slang::ast::ClockingAssertionExpr &expr) {
auto assertionExpr = context.convertAssertionExpression(expr.expr, loc);
if (!assertionExpr)
return {};
return context.convertLTLTimingControl(expr.clocking, assertionExpr);
}
/// Emit an error for all other expressions.
template <typename T>
Value visit(T &&node) {
mlir::emitError(loc, "unsupported expression: ")
<< slang::ast::toString(node.kind);
return {};
}
Value visitInvalid(const slang::ast::AssertionExpr &expr) {
mlir::emitError(loc, "invalid expression");
return {};
}
};
} // namespace
FailureOr<Value> Context::convertAssertionSystemCallArity1(
const slang::ast::SystemSubroutine &subroutine, Location loc, Value value,
Type originalType, Value clockVal) {
using ksn = slang::parsing::KnownSystemName;
auto nameId = subroutine.knownNameId;
// Helper to cast a builtin integer result back to Moore integer types.
auto castToMoore = [&](Value v) -> Value {
if (auto ty = dyn_cast<moore::IntType>(originalType)) {
v = moore::FromBuiltinIntOp::create(builder, loc, v);
if (ty.getDomain() == Domain::FourValued)
v = moore::IntToLogicOp::create(builder, loc, v);
}
return v;
};
switch (nameId) {
case ksn::Sampled:
return castToMoore(ltl::SampledOp::create(builder, loc, value));
// Translate $fell to ¬x[0] ∧ x[-1]
case ksn::Fell: {
auto past =
ltl::PastOp::create(builder, loc, value, 1, clockVal).getResult();
return castToMoore(comb::ICmpOp::create(
builder, loc, comb::ICmpPredicate::ugt, past, value, false));
}
// Translate $rose to x[0] ∧ ¬x[-1]
case ksn::Rose: {
auto past =
ltl::PastOp::create(builder, loc, value, 1, clockVal).getResult();
return castToMoore(comb::ICmpOp::create(
builder, loc, comb::ICmpPredicate::ult, past, value, false));
}
// Translate $changed to x[0] ≠ x[-1]
case ksn::Changed: {
auto past =
ltl::PastOp::create(builder, loc, value, 1, clockVal).getResult();
return castToMoore(comb::ICmpOp::create(
builder, loc, comb::ICmpPredicate::ne, past, value, false));
}
// Translate $stable to x[0] = x[-1]
case ksn::Stable: {
auto past =
ltl::PastOp::create(builder, loc, value, 1, clockVal).getResult();
return castToMoore(comb::ICmpOp::create(
builder, loc, comb::ICmpPredicate::eq, past, value, false));
}
case ksn::Past:
return castToMoore(ltl::PastOp::create(builder, loc, value, 1, clockVal));
default:
return Value{};
}
}
Value Context::convertAssertionCallExpression(
const slang::ast::CallExpression &expr,
const slang::ast::CallExpression::SystemCallInfo &info, Location loc) {
const slang::ast::TimingControl *clock = nullptr;
auto clockIt = assertionCallClocks.find(&expr);
if (clockIt != assertionCallClocks.end())
clock = clockIt->second;
Value clockVal;
if (clock) {
const slang::ast::SignalEventControl *signal = nullptr;
if (clock->kind == slang::ast::TimingControlKind::SignalEvent) {
signal = &clock->as<slang::ast::SignalEventControl>();
} else if (clock->kind == slang::ast::TimingControlKind::EventList) {
mlir::emitError(loc, "sampled value functions with multiple event "
"triggers are not supported");
return {};
} else {
llvm_unreachable("unexpected clock kind for assertion");
}
if (signal->edge != slang::ast::EdgeKind::PosEdge) {
mlir::emitError(
loc,
"sampled value functions are only supported with posedge clocks");
return {};
}
clockVal = convertRvalueExpression(signal->expr);
if (clockVal)
clockVal = convertToI1(clockVal);
if (!clockVal)
return {};
}
const auto &subroutine = *info.subroutine;
auto args = expr.arguments();
FailureOr<Value> result;
Value value;
Value intVal;
Type originalType;
moore::IntType valTy;
switch (args.size()) {
case (1):
value = this->convertRvalueExpression(*args[0]);
originalType = value.getType();
valTy = dyn_cast<moore::IntType>(value.getType());
if (!valTy) {
mlir::emitError(loc) << "expected integer argument for `"
<< subroutine.name << "`";
return {};
}
// IsUnknown is handled here rather than below with the others as below it
// would have already been converted to an integer type rather than bool
// type as here.
if (subroutine.knownNameId == slang::parsing::KnownSystemName::IsUnknown) {
Value bitVal = value;
if (valTy.getWidth() > 1) {
auto mooreI1Type =
moore::IntType::get(getContext(), 1, valTy.getDomain());
bitVal = moore::ReduceXorOp::create(builder, loc, mooreI1Type, value);
}
auto xType =
moore::IntType::get(getContext(), 1, moore::Domain::FourValued);
auto xValue = FVInt::getAllX(1);
auto xConst = moore::ConstantOp::create(builder, loc, xType, xValue);
return moore::CaseEqOp::create(builder, loc, bitVal, xConst).getResult();
}
// If the value is four-valued, we need to map it to two-valued before we
// cast it to a builtin int
if (valTy.getDomain() == Domain::FourValued) {
value = builder.createOrFold<moore::LogicToIntOp>(loc, value);
}
intVal = builder.createOrFold<moore::ToBuiltinIntOp>(loc, value);
if (!intVal)
return {};
result = this->convertAssertionSystemCallArity1(subroutine, loc, intVal,
originalType, clockVal);
break;
default:
break;
}
if (failed(result))
return {};
if (*result)
return *result;
mlir::emitError(loc) << "unsupported system call `" << subroutine.name << "`";
return {};
}
Value Context::convertAssertionExpression(const slang::ast::AssertionExpr &expr,
Location loc) {
AssertionExprVisitor visitor{*this, loc};
return expr.visit(visitor);
}
// NOLINTEND(misc-no-recursion)
/// Helper function to convert a value to an i1 value.
Value Context::convertToI1(Value value) {
if (!value)
return {};
auto loc = value.getLoc();
auto type = dyn_cast<moore::IntType>(value.getType());
if (!type || type.getBitSize() != 1) {
mlir::emitError(loc, "expected a 1-bit integer");
return {};
}
if (type.getDomain() == Domain::FourValued) {
value = moore::LogicToIntOp::create(builder, loc, value);
}
return moore::ToBuiltinIntOp::create(builder, loc, value);
}
namespace {
struct AssertionClockVisitor
: slang::ast::ASTVisitor<AssertionClockVisitor, true, true> {
Context &context;
const slang::analysis::AnalyzedAssertion &assertion;
const slang::ast::TimingControl *currentClock = nullptr;
AssertionClockVisitor(Context &context,
const slang::analysis::AnalyzedAssertion &assertion)
: context(context), assertion(assertion) {}
void handle(const slang::ast::CallExpression &node) {
if (currentClock)
context.assertionCallClocks[&node] = currentClock;
visitDefault(node);
}
template <typename T>
std::enable_if_t<std::is_base_of_v<slang::ast::AssertionExpr, T>>
handle(const T &node) {
auto *prevClock = currentClock;
if (auto *clk = assertion.getClock(node))
currentClock = clk;
visitDefault(node);
currentClock = prevClock;
}
};
} // namespace
void Context::populateAssertionClocks() {
compilation.freeze();
slang::analysis::AnalysisManager am;
am.addListener([this](const slang::analysis::AnalyzedAssertion &assertion) {
AssertionClockVisitor visitor{*this, assertion};
assertion.getRoot().visit(visitor);
});
am.analyze(compilation);
compilation.unfreeze();
}