inst_fingerprinter.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #ifndef CARBON_TOOLCHAIN_SEM_IR_INST_FINGERPRINTER_H_
  5. #define CARBON_TOOLCHAIN_SEM_IR_INST_FINGERPRINTER_H_
  6. #include "toolchain/base/fixed_size_value_store.h"
  7. #include "toolchain/sem_ir/file.h"
  8. #include "toolchain/sem_ir/ids.h"
  9. namespace Carbon::SemIR {
  10. // Computes fingerprints for instructions. These fingerprints are intended to be
  11. // stable across compilations and across minor changes to the compiler.
  12. class InstFingerprinter {
  13. public:
  14. explicit InstFingerprinter(int total_ir_count)
  15. : fingerprints_(FilesFingerprintStores::MakeWithExplicitSizeFrom(
  16. total_ir_count, [] {
  17. return FingerprintStore::MakeForOverwriteWithExplicitSize(0);
  18. })) {}
  19. // Gets or computes a fingerprint for the given instruction.
  20. auto GetOrCompute(const File* file, InstId inst_id) -> uint64_t;
  21. // Gets or computes a fingerprint for the given instruction block.
  22. auto GetOrCompute(const File* file, InstBlockId inst_block_id) -> uint64_t;
  23. // Gets or computes a fingerprint for the given impl.
  24. auto GetOrCompute(const File* file, ImplId impl_id) -> uint64_t;
  25. // Gets or computes a fingerprint for the given C++ overload set.
  26. auto GetOrCompute(const File* file, CppOverloadSetId overload_set_id)
  27. -> uint64_t;
  28. private:
  29. // The fingerprint for each instruction that has had its fingerprint computed,
  30. // indexed by the InstId's index.
  31. //
  32. // TODO: Experiment with also caching fingerprints for instruction blocks once
  33. // we can get realistic performance measurements for this. This would simplify
  34. // the `GetOrCompute` overload for `InstBlockId`s, and may save some work if
  35. // the same canonical inst block is used by multiple instructions, for example
  36. // as a specific argument list.
  37. using FingerprintStore = FixedSizeValueStore<InstId, uint64_t>;
  38. using FilesFingerprintStores =
  39. FixedSizeValueStore<CheckIRId, FingerprintStore>;
  40. FilesFingerprintStores fingerprints_;
  41. };
  42. } // namespace Carbon::SemIR
  43. #endif // CARBON_TOOLCHAIN_SEM_IR_INST_FINGERPRINTER_H_