Skip to content

Commit 441c015

Browse files
author
MoritzScherer
authored
[Moore] Add builtins for simulation time measurements (llvm#8970)
This PR adds builtins for `$time`, `$stime` and `$realtime` in the `moore` dialect. All these builtins correspond to System Verilog system tasks and only slightly differ in their functionality. To represent the return value of `$time`, this PR adds `TwoValueI64`, which represents a 64-Bit integer used to express the simulation time value in.
1 parent a2d0301 commit 441c015

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

include/circt/Dialect/Moore/MooreOps.td

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1761,6 +1761,25 @@ def UrandomBIOp : Builtin<"urandom"> {
17611761
let assemblyFormat = "($seed^)? attr-dict";
17621762
}
17631763

1764+
//===----------------------------------------------------------------------===//
1765+
// Simulation Time Measurement Builtins
1766+
//===----------------------------------------------------------------------===//
1767+
1768+
def TimeBIOp : Builtin<"time"> {
1769+
let summary = "Return the current simulation time";
1770+
let description = [{
1771+
Corresponds to the `$time` system function. Returns a int-rounded 64-bit
1772+
integer corresponding to the elapsed simulation time scaled by the
1773+
simulation's timescale.
1774+
1775+
See IEEE 1800-2023 § 20.3 "Simulation time system functions".
1776+
}];
1777+
1778+
// Optional positional attribute for the seed
1779+
let results = (outs TimeType:$result);
1780+
let assemblyFormat = "attr-dict";
1781+
}
1782+
17641783
//===----------------------------------------------------------------------===//
17651784
// Simulation Control Builtins
17661785
//===----------------------------------------------------------------------===//

lib/Conversion/ImportVerilog/Expressions.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1594,6 +1594,15 @@ Context::convertSystemCallArity0(const slang::ast::SystemSubroutine &subroutine,
15941594
[&]() -> Value {
15951595
return moore::UrandomBIOp::create(builder, loc, nullptr);
15961596
})
1597+
.Case(
1598+
"$time",
1599+
[&]() -> Value { return moore::TimeBIOp::create(builder, loc); })
1600+
.Case(
1601+
"$stime",
1602+
[&]() -> Value { return moore::TimeBIOp::create(builder, loc); })
1603+
.Case(
1604+
"$realtime",
1605+
[&]() -> Value { return moore::TimeBIOp::create(builder, loc); })
15971606
.Default([&]() -> Value { return {}; });
15981607
return systemCallRes();
15991608
}

test/Conversion/ImportVerilog/builtins.sv

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,3 +274,15 @@ function RandomBuiltins(int x);
274274
// CHECK-NEXT: call @dummyA([[RAND1]]) : (!moore.i32) -> ()
275275
dummyA($urandom(x));
276276
endfunction
277+
278+
// CHECK-LABEL: func.func private @TimeBuiltins(
279+
function TimeBuiltins();
280+
// CHECK: [[TIME:%.+]] = moore.builtin.time
281+
// CHECK-NEXT: [[TIMETOLOGIC:%.+]] = moore.time_to_logic [[TIME]]
282+
dummyA($time());
283+
// CHECK: [[STIME:%.+]] = moore.builtin.time
284+
dummyA($stime());
285+
// CHECK: [[REALTIME:%.+]] = moore.builtin.time
286+
// TODO: There is no int-to-real conversion yet; change this to dummyB once int-to-real works!
287+
dummyA($realtime());
288+
endfunction

0 commit comments

Comments
 (0)