driver: add --hash-seed

This commit is contained in:
Emil J. Tywoniak 2024-10-01 16:02:41 +02:00
parent d071489ab1
commit 953508f6d2
5 changed files with 21 additions and 35 deletions

View File

@ -18,6 +18,7 @@
*/ */
#include "kernel/yosys.h" #include "kernel/yosys.h"
#include "kernel/hashlib.h"
#include "libs/sha1/sha1.h" #include "libs/sha1/sha1.h"
#include "libs/cxxopts/include/cxxopts.hpp" #include "libs/cxxopts/include/cxxopts.hpp"
#include <iostream> #include <iostream>
@ -282,6 +283,8 @@ int main(int argc, char **argv)
("M,randomize-pointers", "will slightly randomize allocated pointer addresses. for debugging") ("M,randomize-pointers", "will slightly randomize allocated pointer addresses. for debugging")
("autoidx", "start counting autoidx up from <seed>, similar effect to --hash-seed", ("autoidx", "start counting autoidx up from <seed>, similar effect to --hash-seed",
cxxopts::value<uint64_t>(), "<idx>") cxxopts::value<uint64_t>(), "<idx>")
("hash-seed", "mix up hashing values with <seed>, for extreme optimization and testing",
cxxopts::value<uint64_t>(), "<seed>")
("A,abort", "will call abort() at the end of the script. for debugging") ("A,abort", "will call abort() at the end of the script. for debugging")
("x,experimental", "do not print warnings for the experimental <feature>", ("x,experimental", "do not print warnings for the experimental <feature>",
cxxopts::value<std::vector<std::string>>(), "<feature>") cxxopts::value<std::vector<std::string>>(), "<feature>")
@ -437,6 +440,10 @@ int main(int argc, char **argv)
int idx = result["autoidx"].as<uint64_t>(); int idx = result["autoidx"].as<uint64_t>();
autoidx = idx; autoidx = idx;
} }
if (result.count("hash-seed")) {
int seed = result["hash-seed"].as<uint64_t>();
Hasher::set_fudge((Hasher::hash_t)seed);
}
if (log_errfile == NULL) { if (log_errfile == NULL) {
log_files.push_back(stdout); log_files.push_back(stdout);

View File

@ -863,39 +863,7 @@ public:
bool try_append(DriveBit const &bit); bool try_append(DriveBit const &bit);
bool try_append(DriveChunk const &chunk); bool try_append(DriveChunk const &chunk);
<<<<<<< HEAD
unsigned int hash() const
{
unsigned int inner = 0;
switch (type_)
{
case DriveType::NONE:
inner = 0;
break;
case DriveType::CONSTANT:
inner = constant_.hash();
break;
case DriveType::WIRE:
inner = wire_.hash();
break;
case DriveType::PORT:
inner = port_.hash();
break;
case DriveType::MARKER:
inner = marker_.hash();
break;
case DriveType::MULTIPLE:
inner = multiple_.hash();
break;
default:
log_abort();
break;
}
return mkhash((unsigned int)type_, inner);
}
=======
Hasher hash_acc(Hasher h) const; Hasher hash_acc(Hasher h) const;
>>>>>>> 898d04260 (hashlib: redo interface for flexibility)
bool operator==(const DriveChunk &other) const bool operator==(const DriveChunk &other) const
{ {

View File

@ -81,13 +81,20 @@ class Hasher {
// traditionally 5381 is used as starting value for the djb2 hash // traditionally 5381 is used as starting value for the djb2 hash
state = 5381; state = 5381;
} }
static void set_fudge(uint32_t f) {
fudge = f;
}
private: private:
uint32_t state; uint32_t state;
static uint32_t fudge;
// The XOR version of DJB2 // The XOR version of DJB2
[[nodiscard]] [[nodiscard]]
static uint32_t mkhash(uint32_t a, uint32_t b) { static uint32_t mkhash(uint32_t a, uint32_t b) {
return ((a << 5) + a) ^ b; uint32_t hash = ((a << 5) + a) ^ b;
if (fudge)
hash = fudge ^ mkhash_xorshift(hash);
return hash;
} }
public: public:
void hash32(uint32_t i) { void hash32(uint32_t i) {

View File

@ -169,8 +169,11 @@ public:
return !(*this == other); return !(*this == other);
} }
int hash() const { Hasher hash_acc(Hasher h) const
return mkhash(scope_name.hash(), hash_ptr_ops::hash(target)); {
h.acc(scope_name);
h.acc(target);
return h;
} }
bool valid() const { bool valid() const {

View File

@ -93,6 +93,7 @@ std::set<std::string> yosys_input_files, yosys_output_files;
bool memhasher_active = false; bool memhasher_active = false;
uint32_t memhasher_rng = 123456; uint32_t memhasher_rng = 123456;
std::vector<void*> memhasher_store; std::vector<void*> memhasher_store;
uint32_t Hasher::fudge = 0;
std::string yosys_share_dirname; std::string yosys_share_dirname;
std::string yosys_abc_executable; std::string yosys_abc_executable;