9#include "slang/ast/expressions/AssertionExpr.h"
16#include "mlir/IR/BuiltinAttributes.h"
17#include "mlir/Support/LLVM.h"
18#include "slang/analysis/AnalysisManager.h"
19#include "slang/analysis/AnalyzedAssertion.h"
20#include "slang/ast/ASTVisitor.h"
21#include "slang/ast/SystemSubroutine.h"
22#include "slang/parsing/KnownSystemName.h"
32struct AssertionExprVisitor {
37 AssertionExprVisitor(Context &context, Location loc)
38 : context(context), loc(loc), builder(context.builder) {}
41 std::pair<mlir::IntegerAttr, mlir::IntegerAttr>
42 convertRangeToAttrs(uint32_t min,
43 std::optional<uint32_t> max = std::nullopt) {
44 auto minAttr = builder.getI64IntegerAttr(min);
45 mlir::IntegerAttr rangeAttr;
46 if (max.has_value()) {
47 rangeAttr = builder.getI64IntegerAttr(max.value() - min);
49 return {minAttr, rangeAttr};
53 Value createRepetition(Location loc,
54 const slang::ast::SequenceRepetition &repetition,
55 Value &inputSequence) {
57 auto [minRepetitions, repetitionRange] =
58 convertRangeToAttrs(repetition.range.min, repetition.range.max);
60 using slang::ast::SequenceRepetition;
63 if ((repetition.kind == SequenceRepetition::Nonconsecutive ||
64 repetition.kind == SequenceRepetition::GoTo) &&
67 repetition.kind == SequenceRepetition::Nonconsecutive
68 ?
"Nonconsecutive repetition requires a maximum value"
69 :
"GoTo repetition requires a maximum value");
73 switch (repetition.kind) {
74 case SequenceRepetition::Consecutive:
75 return ltl::RepeatOp::create(builder, loc, inputSequence, minRepetitions,
77 case SequenceRepetition::Nonconsecutive:
78 return ltl::NonConsecutiveRepeatOp::create(
79 builder, loc, inputSequence, minRepetitions, repetitionRange);
80 case SequenceRepetition::GoTo:
81 return ltl::GoToRepeatOp::create(builder, loc, inputSequence,
82 minRepetitions, repetitionRange);
84 llvm_unreachable(
"All enum values handled in switch");
87 Value visit(
const slang::ast::SimpleAssertionExpr &expr) {
89 auto value = context.convertRvalueExpression(expr.expr);
92 auto loc = context.convertLocation(expr.expr.sourceRange);
93 auto valueType = value.getType();
96 if (!mlir::isa<ltl::SequenceType, ltl::PropertyType, mlir::IntegerType>(
98 value = context.convertToBool(value);
99 value = context.convertToI1(value);
106 if (!expr.repetition.has_value()) {
112 return createRepetition(loc, expr.repetition.value(), value);
115 Value visit(
const slang::ast::SequenceConcatExpr &expr) {
117 assert(!expr.elements.empty());
119 SmallVector<Value> sequenceElements;
121 for (
const auto &concatElement : expr.elements) {
122 Value sequenceValue =
123 context.convertAssertionExpression(*concatElement.sequence, loc);
127 [[maybe_unused]] Type valueType = sequenceValue.getType();
128 assert(valueType.isInteger(1) || mlir::isa<ltl::SequenceType>(valueType));
130 auto [delayMin, delayRange] =
131 convertRangeToAttrs(concatElement.delay.min, concatElement.delay.max);
132 auto delayedSequence = ltl::DelayOp::create(builder, loc, sequenceValue,
133 delayMin, delayRange);
134 sequenceElements.push_back(delayedSequence);
137 return builder.createOrFold<ltl::ConcatOp>(loc, sequenceElements);
140 Value visit(
const slang::ast::UnaryAssertionExpr &expr) {
141 auto value = context.convertAssertionExpression(expr.expr, loc);
144 using slang::ast::UnaryAssertionOperator;
146 case UnaryAssertionOperator::Not:
147 return ltl::NotOp::create(builder, loc, value);
148 case UnaryAssertionOperator::SEventually:
149 if (expr.range.has_value()) {
150 mlir::emitError(loc,
"Strong eventually with range not supported");
153 return ltl::EventuallyOp::create(builder, loc, value);
155 case UnaryAssertionOperator::Always: {
156 std::pair<mlir::IntegerAttr, mlir::IntegerAttr> attr = {
157 builder.getI64IntegerAttr(0), mlir::IntegerAttr{}};
158 if (expr.range.has_value()) {
160 convertRangeToAttrs(expr.range.value().min, expr.range.value().max);
162 return ltl::RepeatOp::create(builder, loc, value, attr.first,
165 case UnaryAssertionOperator::NextTime: {
166 auto minRepetitions = builder.getI64IntegerAttr(1);
167 if (expr.range.has_value()) {
168 minRepetitions = builder.getI64IntegerAttr(expr.range.value().min);
170 return ltl::DelayOp::create(builder, loc, value, minRepetitions,
171 builder.getI64IntegerAttr(0));
173 case UnaryAssertionOperator::Eventually:
174 case UnaryAssertionOperator::SNextTime:
175 case UnaryAssertionOperator::SAlways:
176 mlir::emitError(loc,
"unsupported unary operator: ")
177 << slang::ast::toString(expr.op);
180 llvm_unreachable(
"All enum values handled in switch");
183 Value visit(
const slang::ast::BinaryAssertionExpr &expr) {
184 auto lhs = context.convertAssertionExpression(expr.left, loc);
185 auto rhs = context.convertAssertionExpression(expr.right, loc);
188 SmallVector<Value, 2> operands = {lhs, rhs};
189 using slang::ast::BinaryAssertionOperator;
191 case BinaryAssertionOperator::And:
192 return ltl::AndOp::create(builder, loc, operands);
193 case BinaryAssertionOperator::Or:
194 return ltl::OrOp::create(builder, loc, operands);
195 case BinaryAssertionOperator::Intersect:
196 return ltl::IntersectOp::create(builder, loc, operands);
197 case BinaryAssertionOperator::Throughout: {
198 auto lhsRepeat = ltl::RepeatOp::create(
199 builder, loc, lhs, builder.getI64IntegerAttr(0), mlir::IntegerAttr{});
200 return ltl::IntersectOp::create(builder, loc,
201 SmallVector<Value, 2>{lhsRepeat, rhs});
203 case BinaryAssertionOperator::Within: {
206 auto oneRepeat = ltl::RepeatOp::create(builder, loc, constOne,
207 builder.getI64IntegerAttr(0),
208 mlir::IntegerAttr{});
209 auto repeatDelay = ltl::DelayOp::create(builder, loc, oneRepeat,
210 builder.getI64IntegerAttr(1),
211 builder.getI64IntegerAttr(0));
213 ltl::DelayOp::create(builder, loc, lhs, builder.getI64IntegerAttr(1),
214 builder.getI64IntegerAttr(0));
215 auto combined = ltl::ConcatOp::create(
216 builder, loc, SmallVector<Value, 3>{repeatDelay, lhsDelay, constOne});
217 return ltl::IntersectOp::create(builder, loc,
218 SmallVector<Value, 2>{combined, rhs});
220 case BinaryAssertionOperator::Iff: {
221 auto ored = ltl::OrOp::create(builder, loc, operands);
222 auto notOred = ltl::NotOp::create(builder, loc, ored);
223 auto anded = ltl::AndOp::create(builder, loc, operands);
224 return ltl::OrOp::create(builder, loc,
225 SmallVector<Value, 2>{notOred, anded});
227 case BinaryAssertionOperator::Until:
228 return ltl::UntilOp::create(builder, loc, operands);
229 case BinaryAssertionOperator::UntilWith: {
230 auto untilOp = ltl::UntilOp::create(builder, loc, operands);
231 auto andOp = ltl::AndOp::create(builder, loc, operands);
232 auto notUntil = ltl::NotOp::create(builder, loc, untilOp);
233 return ltl::OrOp::create(builder, loc,
234 SmallVector<Value, 2>{notUntil, andOp});
236 case BinaryAssertionOperator::Implies: {
237 auto notLhs = ltl::NotOp::create(builder, loc, lhs);
238 return ltl::OrOp::create(builder, loc,
239 SmallVector<Value, 2>{notLhs, rhs});
241 case BinaryAssertionOperator::OverlappedImplication:
242 return ltl::ImplicationOp::create(builder, loc, operands);
243 case BinaryAssertionOperator::NonOverlappedImplication: {
246 auto oneDelay = ltl::DelayOp::create(builder, loc, constOne,
247 builder.getI64IntegerAttr(1),
248 builder.getI64IntegerAttr(0));
249 auto antecedent = ltl::ConcatOp::create(
250 builder, loc, SmallVector<Value, 2>{lhs, oneDelay});
251 return ltl::ImplicationOp::create(builder, loc,
252 SmallVector<Value, 2>{antecedent, rhs});
254 case BinaryAssertionOperator::OverlappedFollowedBy: {
255 auto notRhs = ltl::NotOp::create(builder, loc, rhs);
256 auto implication = ltl::ImplicationOp::create(
257 builder, loc, SmallVector<Value, 2>{lhs, notRhs});
258 return ltl::NotOp::create(builder, loc, implication);
260 case BinaryAssertionOperator::NonOverlappedFollowedBy: {
263 auto notRhs = ltl::NotOp::create(builder, loc, rhs);
264 auto oneDelay = ltl::DelayOp::create(builder, loc, constOne,
265 builder.getI64IntegerAttr(1),
266 builder.getI64IntegerAttr(0));
267 auto antecedent = ltl::ConcatOp::create(
268 builder, loc, SmallVector<Value, 2>{lhs, oneDelay});
269 auto implication = ltl::ImplicationOp::create(
270 builder, loc, SmallVector<Value, 2>{antecedent, notRhs});
271 return ltl::NotOp::create(builder, loc, implication);
273 case BinaryAssertionOperator::SUntil:
274 case BinaryAssertionOperator::SUntilWith:
275 mlir::emitError(loc,
"unsupported binary operator: ")
276 << slang::ast::toString(expr.op);
279 llvm_unreachable(
"All enum values handled in switch");
282 Value visit(
const slang::ast::ClockingAssertionExpr &expr) {
283 auto assertionExpr = context.convertAssertionExpression(expr.expr, loc);
286 return context.convertLTLTimingControl(expr.clocking, assertionExpr);
290 template <
typename T>
291 Value visit(T &&node) {
292 mlir::emitError(loc,
"unsupported expression: ")
293 << slang::ast::toString(node.kind);
297 Value visitInvalid(
const slang::ast::AssertionExpr &expr) {
298 mlir::emitError(loc,
"invalid expression");
306 Type originalType, Value v) {
307 if (
auto ty = dyn_cast<moore::IntType>(originalType)) {
308 v = moore::FromBuiltinIntOp::create(builder, loc, v);
310 v = moore::IntToLogicOp::create(builder, loc, v);
316 const slang::ast::SystemSubroutine &subroutine, Location loc, Value value,
317 Type originalType, Value clockVal) {
318 using ksn = slang::parsing::KnownSystemName;
319 auto nameId = subroutine.knownNameId;
322 auto castToTwoValued = [&](Value v) -> Value {
323 return moore::FromBuiltinIntOp::create(
builder, loc, v);
329 ltl::SampledOp::create(
builder, loc, value));
334 ltl::PastOp::create(
builder, loc, value, 1, clockVal).getResult();
335 return castToTwoValued(comb::ICmpOp::create(
336 builder, loc, comb::ICmpPredicate::ugt, past, value,
false));
342 ltl::PastOp::create(
builder, loc, value, 1, clockVal).getResult();
343 return castToTwoValued(comb::ICmpOp::create(
344 builder, loc, comb::ICmpPredicate::ult, past, value,
false));
350 ltl::PastOp::create(
builder, loc, value, 1, clockVal).getResult();
351 return castToTwoValued(comb::ICmpOp::create(
352 builder, loc, comb::ICmpPredicate::ne, past, value,
false));
358 ltl::PastOp::create(
builder, loc, value, 1, clockVal).getResult();
359 return castToTwoValued(comb::ICmpOp::create(
360 builder, loc, comb::ICmpPredicate::eq, past, value,
false));
365 ltl::PastOp::create(
builder, loc, value, 1, clockVal));
373 const slang::ast::SystemSubroutine &subroutine, Location loc,
374 Value firstArgVal, Type originalType, Value clockVal,
375 const slang::ast::Expression &secondArgExpr) {
376 using ksn = slang::parsing::KnownSystemName;
377 auto nameId = subroutine.knownNameId;
383 assert(ticks && *ticks >= 1 &&
"slang should reject invalid tick counts");
387 ltl::PastOp::create(
builder, loc, firstArgVal, *ticks, clockVal));
391 return mlir::emitError(loc) <<
"second argument for `" << subroutine.name
392 <<
"` is not yet supported";
397 const slang::ast::CallExpression &expr,
398 const slang::ast::CallExpression::SystemCallInfo &info, Location loc) {
400 const slang::ast::TimingControl *clock =
nullptr;
403 clock = clockIt->second;
407 if (!clock && !info.scope->isProceduralContext())
409 info.scope->getCompilation().getDefaultClocking(*info.scope))
410 clock = &defClk->as<slang::ast::ClockingBlockSymbol>().getEvent();
413 mlir::emitError(loc) <<
"could not determine clocking event for `"
414 << expr.getSubroutineName() <<
"`";
419 const slang::ast::SignalEventControl *signal =
nullptr;
420 if (clock->kind == slang::ast::TimingControlKind::SignalEvent) {
421 signal = &clock->as<slang::ast::SignalEventControl>();
422 }
else if (clock->kind == slang::ast::TimingControlKind::EventList) {
423 mlir::emitError(loc,
"sampled value functions with multiple event "
424 "triggers are not supported");
427 llvm_unreachable(
"unexpected clock kind for assertion");
430 if (signal->edge != slang::ast::EdgeKind::PosEdge) {
432 loc,
"sampled value functions are only supported with posedge clocks");
441 const auto &subroutine = *info.subroutine;
442 auto args = expr.arguments();
444 FailureOr<Value> result;
446 Value firstArgIntVal;
448 moore::IntType firstArgTy;
451 assert(!args.empty() &&
"sampled value call must have at least 1 argument");
455 if (isa<moore::PackedType>(firstArgVal.getType())) {
461 firstArgTy = dyn_cast<moore::IntType>(firstArgVal.getType());
463 mlir::emitError(loc) <<
"expected integer argument for `" << subroutine.name
470 originalType = firstArgTy;
474 firstArgVal =
builder.createOrFold<moore::LogicToIntOp>(loc, firstArgVal);
477 builder.createOrFold<moore::ToBuiltinIntOp>(loc, firstArgVal);
482 if (args.size() == 1) {
484 subroutine, loc, firstArgIntVal, originalType, clockVal);
485 }
else if (args.size() == 2) {
487 subroutine, loc, firstArgIntVal, originalType, clockVal, *args[1]);
489 mlir::emitError(loc) <<
"`" << subroutine.name <<
"` with " << args.size()
490 <<
" arguments is not yet supported";
502 mlir::emitError(loc) <<
"unsupported system call `" << subroutine.name <<
"`";
508 AssertionExprVisitor visitor{*
this, loc};
509 return expr.visit(visitor);
517 auto loc = value.getLoc();
518 auto type = dyn_cast<moore::IntType>(value.getType());
519 if (!type || type.getBitSize() != 1) {
520 mlir::emitError(loc,
"expected a 1-bit integer");
524 value = moore::LogicToIntOp::create(
builder, loc, value);
526 return moore::ToBuiltinIntOp::create(
builder, loc, value);
530struct AssertionClockVisitor
531 : slang::ast::ASTVisitor<AssertionClockVisitor,
532 slang::ast::VisitFlags::AllGood> {
534 const slang::analysis::AnalyzedAssertion &assertion;
535 const slang::ast::TimingControl *currentClock =
nullptr;
538 const slang::analysis::AnalyzedAssertion &assertion)
541 void handle(
const slang::ast::CallExpression &node) {
543 context.sampledValueCallClocks[&node] = currentClock;
547 template <
typename T>
548 std::enable_if_t<std::is_base_of_v<slang::ast::AssertionExpr, T>>
549 handle(
const T &node) {
550 auto *prevClock = currentClock;
551 if (
auto *clk = assertion.getClock(node))
554 currentClock = prevClock;
561 slang::analysis::AnalysisManager am;
562 am.addListener([
this](
const slang::analysis::AnalyzedAssertion &assertion) {
563 AssertionClockVisitor visitor{*
this, assertion};
564 assertion.getRoot().visit(visitor);
567 am.addListener([
this](
const slang::analysis::AnalyzedProcedure &procedure) {
568 if (
const auto clock = procedure.getInferredClock())
569 for (
const auto *call : procedure.getCallExpressions())
static Value castToMoore(OpBuilder &builder, Location loc, Type originalType, Value v)
Helper to cast a builtin integer result back to Moore integer types.
assert(baseType &&"element must be base type")
static std::unique_ptr< Context > context
@ FourValued
Four-valued types such as logic or integer.
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.
void populateSampledValueClocks()
Generates a map from sampled value system calls to clocks using Slang's analysis.
slang::ConstantValue evaluateConstant(const slang::ast::Expression &expr)
Evaluate the constant value of an expression.
FailureOr< Value > convertSampledValueCallArity2(const slang::ast::SystemSubroutine &subroutine, Location loc, Value firstArgVal, Type originalType, Value clockVal, const slang::ast::Expression &secondArgExpr)
Convert sampled value system function calls with two arguments.
slang::ast::Compilation & compilation
OpBuilder builder
The builder used to create IR operations.
Value convertSampledValueCallExpression(const slang::ast::CallExpression &expr, const slang::ast::CallExpression::SystemCallInfo &info, Location loc)
Type convertType(const slang::ast::Type &type, LocationAttr loc={})
Convert a slang type into an MLIR type.
Value materializePackedToSBVConversion(Value value, Location loc, bool fallible)
Helper function to convert a PackedType value to its simple bit vector representation,...
Value convertRvalueExpression(const slang::ast::Expression &expr, Type requiredType={})
DenseMap< const slang::ast::CallExpression *, const slang::ast::TimingControl * > sampledValueCallClocks
Maps sampled value system calls to their corresponding clocks.
Value materializeConversion(Type type, Value value, bool isSigned, Location loc, bool fallible=false)
Helper function to insert the necessary operations to cast a value from one type to another.
Value convertAssertionExpression(const slang::ast::AssertionExpr &expr, Location loc)
FailureOr< Value > convertSampledValueCallArity1(const slang::ast::SystemSubroutine &subroutine, Location loc, Value value, Type originalType, Value clockVal)
Convert sampled value system function calls with a single argument.