Skip to content

Commit 2b2a4d9

Browse files
author
MoritzScherer
authored
[Moore] Add support for $random system task (llvm#8982)
This PR adds support for `$random` system calls and maps them to a new Moore builtin-op `RandomBIOp`. Implementation is very much along the lines of [the $urandom implementation](llvm#8968). I chose to not map $random and $urandom to the same builtin as they are semantically different, $urandom being pseudo-random, $random being true-random.
1 parent 2296be7 commit 2b2a4d9

File tree

3 files changed

+40
-7
lines changed

3 files changed

+40
-7
lines changed

include/circt/Dialect/Moore/MooreOps.td

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1763,9 +1763,28 @@ class Builtin<string mnemonic, list<Trait> traits = []> :
17631763
MooreOp<"builtin." # mnemonic, traits>;
17641764

17651765
//===----------------------------------------------------------------------===//
1766-
// Pseudo-Random Generators
1766+
// True-Random and Pseudo-Random Generators
17671767
//===----------------------------------------------------------------------===//
17681768

1769+
def RandomBIOp : Builtin<"random"> {
1770+
let summary = "Generate a true random signed integer (optionally seeded)";
1771+
let description = [{
1772+
Corresponds to the `$random` system function. Returns a 32-bit
1773+
true random integer. The seed is optional; when provided, it initializes
1774+
the generator. If not provided, treat it as 0 in semantics/lowering.
1775+
1776+
`$random` is largely considered to be deprecated since it leads to
1777+
non-reproducible simulation results. Consider using `$urandom` instead.
1778+
1779+
See IEEE 1800-2023 § 20.14 "Probablistic distribution functions".
1780+
}];
1781+
1782+
// Optional positional attribute for the seed
1783+
let arguments = (ins Optional<TwoValuedI32>:$seed);
1784+
let results = (outs TwoValuedI32:$result);
1785+
let assemblyFormat = "($seed^)? attr-dict";
1786+
}
1787+
17691788
def UrandomBIOp : Builtin<"urandom"> {
17701789
let summary = "Generate a pseudo-random unsigned integer (optionally seeded)";
17711790
let description = [{

lib/Conversion/ImportVerilog/Expressions.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1594,6 +1594,10 @@ Context::convertSystemCallArity0(const slang::ast::SystemSubroutine &subroutine,
15941594
[&]() -> Value {
15951595
return moore::UrandomBIOp::create(builder, loc, nullptr);
15961596
})
1597+
.Case("$random",
1598+
[&]() -> Value {
1599+
return moore::RandomBIOp::create(builder, loc, nullptr);
1600+
})
15971601
.Case(
15981602
"$time",
15991603
[&]() -> Value { return moore::TimeBIOp::create(builder, loc); })
@@ -1700,6 +1704,10 @@ Context::convertSystemCallArity1(const slang::ast::SystemSubroutine &subroutine,
17001704
[&]() -> Value {
17011705
return moore::UrandomBIOp::create(builder, loc, value);
17021706
})
1707+
.Case("$random",
1708+
[&]() -> Value {
1709+
return moore::RandomBIOp::create(builder, loc, value);
1710+
})
17031711
.Default([&]() -> Value { return {}; });
17041712
return systemCallRes();
17051713
}

test/Conversion/ImportVerilog/builtins.sv

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -267,12 +267,18 @@ endfunction
267267
// CHECK-LABEL: func.func private @RandomBuiltins(
268268
// CHECK-SAME: [[X:%.+]]: !moore.i32
269269
function RandomBuiltins(int x);
270-
// CHECK: [[RAND0:%.+]] = moore.builtin.urandom
271-
// CHECK-NEXT: call @dummyA([[RAND0]]) : (!moore.i32) -> ()
272-
dummyA($urandom());
273-
// CHECK: [[RAND1:%.+]] = moore.builtin.urandom [[X]]
274-
// CHECK-NEXT: call @dummyA([[RAND1]]) : (!moore.i32) -> ()
275-
dummyA($urandom(x));
270+
// CHECK: [[URAND0:%.+]] = moore.builtin.urandom
271+
// CHECK-NEXT: call @dummyA([[URAND0]]) : (!moore.i32) -> ()
272+
dummyA($urandom());
273+
// CHECK: [[URAND1:%.+]] = moore.builtin.urandom [[X]]
274+
// CHECK-NEXT: call @dummyA([[URAND1]]) : (!moore.i32) -> ()
275+
dummyA($urandom(x));
276+
// CHECK: [[RAND0:%.+]] = moore.builtin.random
277+
// CHECK-NEXT: call @dummyA([[RAND0]]) : (!moore.i32) -> ()
278+
dummyA($random());
279+
// CHECK: [[RAND1:%.+]] = moore.builtin.random [[X]]
280+
// CHECK-NEXT: call @dummyA([[RAND1]]) : (!moore.i32) -> ()
281+
dummyA($random(x));
276282
endfunction
277283

278284
// CHECK-LABEL: func.func private @TimeBuiltins(

0 commit comments

Comments
 (0)