-
Notifications
You must be signed in to change notification settings - Fork 15.4k
[llvm-exegesis] Add CLI Option to set Fixed RNG seed #170013
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[llvm-exegesis] Add CLI Option to set Fixed RNG seed #170013
Conversation
Created using spr 1.3.7
|
@llvm/pr-subscribers-tools-llvm-exegesis Author: Aiden Grossman (boomanaiden154) ChangesThe primary motivation for this is to set a fixed RNG seed for flaky Full diff: https://github.com/llvm/llvm-project/pull/170013.diff 2 Files Affected:
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; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point. Fixed.
davemgreen
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. LGTM
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
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
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
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
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.