Skip to content

Conversation

@Scheremo
Copy link
Contributor

First time I try to add a new Op, so let me know if I went wrong somewhere!

This PR adds a new builtin in moore for the $urandom system call.
Per the newest version of the spec, $urandom may be called with or with a integer seed argument, and always returns an integer produced by a pseudo-random number generator.

As such I added an importer conversion and tests for both cases; the allowed expressions are

moore.builtin.urandom
moore.builtin.urandom %arg : !moore.i32

I struggled a bit with constraining the output type of moore.builtin.urandom, since it is constant. I added

def MooreI32 :
  Type<CPred<
    "(::llvm::isa<::circt::moore::IntType>($_self) && "
    "::llvm::cast<::circt::moore::IntType>($_self).getWidth() == 32 && "
    "::llvm::cast<::circt::moore::IntType>($_self).getDomain() == "
    "::circt::moore::Domain::TwoValued)"
  >, "!moore.i32"> {
  let builderCall =
    "::circt::moore::IntType::getInt($_builder.getContext(), 32)";
}

in place to constrain an IntType to be exactly a moore.i32 for this purpose, but please let me know if there is a smarter way; this seems a bit cumbersome.

@Scheremo Scheremo marked this pull request as ready for review September 15, 2025 20:27
Copy link
Contributor

@fabianschuiki fabianschuiki left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Just a minor nit about moving the type constraint into MooreTypes.td. Thanks for adding this! 🙂

@Scheremo
Copy link
Contributor Author

Move the new Type to MooreTypes.td and renamed to TwoValuedI32 and used it to construct the seed optional argument. Thanks for the quick and very helpful feedback!
If there is no other comments, I am okay with merging whenever you get to it.

@fabianschuiki fabianschuiki merged commit d409ccd into llvm:main Sep 16, 2025
6 checks passed
Scheremo pushed a commit to Scheremo/circt that referenced this pull request Sep 17, 2025
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.
fabianschuiki pushed a commit that referenced this pull request Sep 17, 2025
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](#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.
@Scheremo Scheremo deleted the pr-urandom branch October 21, 2025 07:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants