Skip to content

Conversation

@boomanaiden154
Copy link
Contributor

The primary motivation for this is to set a fixed RNG seed for flaky
tests. This also has the bonus of adding debug logging for what seed
gets used which can make it much easier to reproduce issues that only
happen occasionally and are seed-dependent.

Created using spr 1.3.7
@llvmbot
Copy link
Member

llvmbot commented Nov 29, 2025

@llvm/pr-subscribers-tools-llvm-exegesis

Author: Aiden Grossman (boomanaiden154)

Changes

The primary motivation for this is to set a fixed RNG seed for flaky
tests. This also has the bonus of adding debug logging for what seed
gets used which can make it much easier to reproduce issues that only
happen occasionally and are seed-dependent.


Full diff: https://github.com/llvm/llvm-project/pull/170013.diff

2 Files Affected:

  • (added) llvm/test/tools/llvm-exegesis/X86/snippet-generator-seed.test (+16)
  • (modified) llvm/tools/llvm-exegesis/lib/SnippetGenerator.cpp (+14-1)
diff --git a/llvm/test/tools/llvm-exegesis/X86/snippet-generator-seed.test b/llvm/test/tools/llvm-exegesis/X86/snippet-generator-seed.test
new file mode 100644
index 0000000000000..54a0e4946fcd8
--- /dev/null
+++ b/llvm/test/tools/llvm-exegesis/X86/snippet-generator-seed.test
@@ -0,0 +1,16 @@
+# REQUIRES: exegesis-can-measure-latency, x86_64-linux
+
+# Check that the snippet we generate is exactly the same between runs when we
+# use a fixed RNG seed.
+
+# RUN: llvm-exegesis -mtriple=x86_64-unknown-unknown -mode=latency -opcode-name=ADD64rr --benchmark-phase=prepare-snippet -random-generator-seed=5 | FileCheck %s
+
+# CHECK: ---
+# CHECK: mode:            latency
+# CHECK: key:
+# CHECK:   instructions:
+# CHECK:     - 'ADD64rr RCX RCX RAX'
+# CHECK:   config:          ''
+# CHECK:   register_initial_values:
+# CHECK:     - 'RCX=0x0'
+# CHECK:     - 'RAX=0x0'
diff --git a/llvm/tools/llvm-exegesis/lib/SnippetGenerator.cpp b/llvm/tools/llvm-exegesis/lib/SnippetGenerator.cpp
index 7023f1bfae193..17cdf328af84c 100644
--- a/llvm/tools/llvm-exegesis/lib/SnippetGenerator.cpp
+++ b/llvm/tools/llvm-exegesis/lib/SnippetGenerator.cpp
@@ -21,9 +21,17 @@
 #include "llvm/Support/FormatVariadic.h"
 #include "llvm/Support/Program.h"
 
+#define DEBUG_TYPE "snippet-generator"
+
 namespace llvm {
 namespace exegesis {
 
+static cl::opt<unsigned>
+    RandomGeneratorSeed("random-generator-seed",
+                        cl::desc("The seed value to use for the random number "
+                                 "generator when generating snippets."),
+                        cl::init(0));
+
 std::vector<CodeTemplate> getSingleton(CodeTemplate &&CT) {
   std::vector<CodeTemplate> Result;
   Result.push_back(std::move(CT));
@@ -187,8 +195,13 @@ generateUnconstrainedCodeTemplates(const InstructionTemplate &Variant,
 }
 
 std::mt19937 &randomGenerator() {
+  unsigned RandomSeed = RandomGeneratorSeed;
   static std::random_device RandomDevice;
-  static std::mt19937 RandomGenerator(RandomDevice());
+  if (RandomSeed == 0) {
+    RandomSeed = RandomDevice();
+  }
+  LLVM_DEBUG(dbgs() << "Using random seed " << RandomSeed << ".\n");
+  static std::mt19937 RandomGenerator(RandomSeed);
   return RandomGenerator;
 }
 

}

std::mt19937 &randomGenerator() {
unsigned RandomSeed = RandomGeneratorSeed;
Copy link
Collaborator

Choose a reason for hiding this comment

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

RandomSeed = RandomGeneratorSeed.getNumOccurrences() ? RandomGeneratorSeed : RandomDevice();

This can allow a seed of 0 too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point. Fixed.

Created using spr 1.3.7
Copy link
Collaborator

@davemgreen davemgreen left a comment

Choose a reason for hiding this comment

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

Thanks. LGTM

@boomanaiden154 boomanaiden154 merged commit 73889c3 into main Dec 1, 2025
9 of 10 checks passed
@boomanaiden154 boomanaiden154 deleted the users/boomanaiden154/llvm-exegesis-add-cli-option-to-set-fixed-rng-seed branch December 1, 2025 15:12
llvm-sync bot pushed a commit to arm/arm-toolchain that referenced this pull request Dec 1, 2025
The primary motivation for this is to set a fixed RNG seed for flaky
tests. This also has the bonus of adding debug logging for what seed
gets used which can make it much easier to reproduce issues that only
happen occasionally and are seed-dependent.

Reviewers: sjoerdmeijer, davemgreen, mshockwave

Reviewed By: davemgreen

Pull Request: llvm/llvm-project#170013
augusto2112 pushed a commit to augusto2112/llvm-project that referenced this pull request Dec 3, 2025
The primary motivation for this is to set a fixed RNG seed for flaky
tests. This also has the bonus of adding debug logging for what seed
gets used which can make it much easier to reproduce issues that only
happen occasionally and are seed-dependent.

Reviewers: sjoerdmeijer, davemgreen, mshockwave

Reviewed By: davemgreen

Pull Request: llvm#170013
kcloudy0717 pushed a commit to kcloudy0717/llvm-project that referenced this pull request Dec 4, 2025
The primary motivation for this is to set a fixed RNG seed for flaky
tests. This also has the bonus of adding debug logging for what seed
gets used which can make it much easier to reproduce issues that only
happen occasionally and are seed-dependent.

Reviewers: sjoerdmeijer, davemgreen, mshockwave

Reviewed By: davemgreen

Pull Request: llvm#170013
honeygoyal pushed a commit to honeygoyal/llvm-project that referenced this pull request Dec 9, 2025
The primary motivation for this is to set a fixed RNG seed for flaky
tests. This also has the bonus of adding debug logging for what seed
gets used which can make it much easier to reproduce issues that only
happen occasionally and are seed-dependent.

Reviewers: sjoerdmeijer, davemgreen, mshockwave

Reviewed By: davemgreen

Pull Request: llvm#170013
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.

4 participants