[PATCH RISU] risugen: Add --seed option

Philippe Mathieu-Daudé posted 1 patch 3 days, 22 hours ago
README                 | 4 ++++
risugen                | 4 ++++
risugen_arm.pm         | 2 +-
risugen_loongarch64.pm | 2 +-
risugen_m68k.pm        | 2 +-
risugen_ppc64.pm       | 2 +-
risugen_s390x.pm       | 2 +-
7 files changed, 13 insertions(+), 5 deletions(-)
[PATCH RISU] risugen: Add --seed option
Posted by Philippe Mathieu-Daudé 3 days, 22 hours ago
Every architecture backend hard-codes srand(0), so all
generated test binaries for a given pattern set are identical.

Add a '--seed N' option (default 0, so the default output
is unchanged) and pass it down to the backends through the
parameters hash, to get different random sequences from the
same patterns.

Signed-off-by: Philippe Mathieu-Daudé <philmd@oss.qualcomm.com>
---
 README                 | 4 ++++
 risugen                | 4 ++++
 risugen_arm.pm         | 2 +-
 risugen_loongarch64.pm | 2 +-
 risugen_m68k.pm        | 2 +-
 risugen_ppc64.pm       | 2 +-
 risugen_s390x.pm       | 2 +-
 7 files changed, 13 insertions(+), 5 deletions(-)

diff --git a/README b/README
index 35e8b29..58691e8 100644
--- a/README
+++ b/README
@@ -85,6 +85,10 @@ An alternative to using regular expression patterns is to use the
 --group specifier. This relies on the configuration file having been
 annotated with suitable @ markers.
 
+The generated instructions and register values are pseudo-random but
+deterministic: the same options always produce the same binary. Use
+--seed N (default 0) to get a different random sequence.
+
 This binary can then be passed to the risu program, which is
 written in C. You need to run risu on both an ARM native target
 and on the program under test. The ARM native system is the 'master'
diff --git a/risugen b/risugen
index 2800b8b..a8a82fa 100755
--- a/risugen
+++ b/risugen
@@ -293,6 +293,7 @@ and outputfile is the generated raw binary file.
 
 Valid options:
     --numinsns n : generate n instructions (default is 10000)
+    --seed n     : seed for the random number generator (default is 0)
     --fpscr n    : set initial FPSCR (arm) or FPCR (aarch64) value (default is 0)
     --condprob p : [ARM only] make instructions conditional with probability p
                    (default is 0, ie all instructions are always executed)
@@ -319,6 +320,7 @@ sub main()
     my $numinsns = 10000;
     my $condprob = 0;
     my $fpscr = 0;
+    my $seed = 0;
     my $fp_enabled = 1;
     my $sve_enabled = 0;
     my $big_endian = 0;
@@ -327,6 +329,7 @@ sub main()
     GetOptions( "help" => sub { usage(); exit(0); },
                 "numinsns=i" => \$numinsns,
                 "fpscr=o" => \$fpscr,
+                "seed=i" => \$seed,
                 "group=s" => \@groups,
                 "pattern=s" => \@pattern_re,
                 "not-pattern=s" => \@not_pattern_re,
@@ -364,6 +367,7 @@ sub main()
     my %params = (
         'condprob' => $condprob,
         'fpscr' => $fpscr,
+        'seed' => $seed,
         'numinsns' => $numinsns,
         'fp_enabled' => $fp_enabled,
         'sve_enabled' => $sve_enabled,
diff --git a/risugen_arm.pm b/risugen_arm.pm
index 8d423b1..2fbac78 100644
--- a/risugen_arm.pm
+++ b/risugen_arm.pm
@@ -1104,7 +1104,7 @@ sub write_test_code($$$$$$$$)
     $condprob = 1 - $condprob;
 
     # TODO better random number generator?
-    srand(0);
+    srand($params->{ 'seed' });
 
     print "Generating code using patterns: @keys...\n";
     progress_start(78, $numinsns);
diff --git a/risugen_loongarch64.pm b/risugen_loongarch64.pm
index 98fbd48..6263888 100644
--- a/risugen_loongarch64.pm
+++ b/risugen_loongarch64.pm
@@ -495,7 +495,7 @@ sub write_test_code($)
     $condprob = 1 - $condprob;
 
     # TODO better random number generator?
-    srand(0);
+    srand($params->{ 'seed' });
 
     print "Generating code using patterns: @keys...\n";
     progress_start(78, $numinsns);
diff --git a/risugen_m68k.pm b/risugen_m68k.pm
index 7d62b13..acf5bb2 100644
--- a/risugen_m68k.pm
+++ b/risugen_m68k.pm
@@ -173,7 +173,7 @@ sub write_test_code($)
     $condprob = 1 - $condprob;
 
     # TODO better random number generator?
-    srand(0);
+    srand($params->{ 'seed' });
 
     print "Generating code using patterns: @keys...\n";
     progress_start(78, $numinsns);
diff --git a/risugen_ppc64.pm b/risugen_ppc64.pm
index b241172..3372b73 100644
--- a/risugen_ppc64.pm
+++ b/risugen_ppc64.pm
@@ -385,7 +385,7 @@ sub write_test_code($)
     $condprob = 1 - $condprob;
 
     # TODO better random number generator?
-    srand(0);
+    srand($params->{ 'seed' });
 
     print "Generating code using patterns: @keys...\n";
     progress_start(78, $numinsns);
diff --git a/risugen_s390x.pm b/risugen_s390x.pm
index 260e2dd..6cc453a 100644
--- a/risugen_s390x.pm
+++ b/risugen_s390x.pm
@@ -153,7 +153,7 @@ sub write_test_code($)
     $condprob = 1 - $condprob;
 
     # TODO better random number generator?
-    srand(0);
+    srand($params->{ 'seed' });
 
     print "Generating code using patterns: @keys...\n";
     progress_start(78, $numinsns);
-- 
2.55.0


Re: [PATCH RISU] risugen: Add --seed option
Posted by Thomas Huth 3 days, 12 hours ago
Am 02.12.26 um 22:51 schrieb Philippe Mathieu-Daudé:
> Every architecture backend hard-codes srand(0), so all
> generated test binaries for a given pattern set are identical.
> 
> Add a '--seed N' option (default 0, so the default output
> is unchanged) and pass it down to the backends through the
> parameters hash, to get different random sequences from the
> same patterns.
> 
> Signed-off-by: Philippe Mathieu-Daudé <philmd@oss.qualcomm.com>
> ---
>   README                 | 4 ++++
>   risugen                | 4 ++++
>   risugen_arm.pm         | 2 +-
>   risugen_loongarch64.pm | 2 +-
>   risugen_m68k.pm        | 2 +-
>   risugen_ppc64.pm       | 2 +-
>   risugen_s390x.pm       | 2 +-
>   7 files changed, 13 insertions(+), 5 deletions(-)
> 
> diff --git a/README b/README
> index 35e8b29..58691e8 100644
> --- a/README
> +++ b/README
> @@ -85,6 +85,10 @@ An alternative to using regular expression patterns is to use the
>   --group specifier. This relies on the configuration file having been
>   annotated with suitable @ markers.
>   
> +The generated instructions and register values are pseudo-random but
> +deterministic: the same options always produce the same binary. Use
> +--seed N (default 0) to get a different random sequence.
> +
>   This binary can then be passed to the risu program, which is
>   written in C. You need to run risu on both an ARM native target
>   and on the program under test. The ARM native system is the 'master'
> diff --git a/risugen b/risugen
> index 2800b8b..a8a82fa 100755
> --- a/risugen
> +++ b/risugen
> @@ -293,6 +293,7 @@ and outputfile is the generated raw binary file.
>   
>   Valid options:
>       --numinsns n : generate n instructions (default is 10000)
> +    --seed n     : seed for the random number generator (default is 0)
>       --fpscr n    : set initial FPSCR (arm) or FPCR (aarch64) value (default is 0)
>       --condprob p : [ARM only] make instructions conditional with probability p
>                      (default is 0, ie all instructions are always executed)
> @@ -319,6 +320,7 @@ sub main()
>       my $numinsns = 10000;
>       my $condprob = 0;
>       my $fpscr = 0;
> +    my $seed = 0;
>       my $fp_enabled = 1;
>       my $sve_enabled = 0;
>       my $big_endian = 0;
> @@ -327,6 +329,7 @@ sub main()
>       GetOptions( "help" => sub { usage(); exit(0); },
>                   "numinsns=i" => \$numinsns,
>                   "fpscr=o" => \$fpscr,
> +                "seed=i" => \$seed,
>                   "group=s" => \@groups,
>                   "pattern=s" => \@pattern_re,
>                   "not-pattern=s" => \@not_pattern_re,
> @@ -364,6 +367,7 @@ sub main()
>       my %params = (
>           'condprob' => $condprob,
>           'fpscr' => $fpscr,
> +        'seed' => $seed,
>           'numinsns' => $numinsns,
>           'fp_enabled' => $fp_enabled,
>           'sve_enabled' => $sve_enabled,
> diff --git a/risugen_arm.pm b/risugen_arm.pm
> index 8d423b1..2fbac78 100644
> --- a/risugen_arm.pm
> +++ b/risugen_arm.pm
> @@ -1104,7 +1104,7 @@ sub write_test_code($$$$$$$$)
>       $condprob = 1 - $condprob;
>   
>       # TODO better random number generator?

Would it make sense to remove all those TODO comments now?

Apart from that, patch looks fine to me, so:
Reviewed-by: Thomas Huth <thuth@redhat.com>


> -    srand(0);
> +    srand($params->{ 'seed' });
>   
>       print "Generating code using patterns: @keys...\n";
>       progress_start(78, $numinsns);[...]