inst_fingerprinter.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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(
  18. 0, CheckIRId::None);
  19. })) {}
  20. // Gets or computes a fingerprint for the given instruction.
  21. auto GetOrCompute(const File* file, InstId inst_id) -> uint64_t;
  22. // Gets or computes a fingerprint for the given instruction block.
  23. auto GetOrCompute(const File* file, InstBlockId inst_block_id) -> uint64_t;
  24. // Gets or computes a fingerprint for the given impl.
  25. auto GetOrCompute(const File* file, ImplId impl_id) -> uint64_t;
  26. // Gets or computes a fingerprint for the given C++ overload set.
  27. auto GetOrCompute(const File* file, CppOverloadSetId overload_set_id)
  28. -> uint64_t;
  29. private:
  30. // The fingerprint for each instruction that has had its fingerprint computed,
  31. // indexed by the InstId's index.
  32. //
  33. // TODO: Experiment with also caching fingerprints for instruction blocks once
  34. // we can get realistic performance measurements for this. This would simplify
  35. // the `GetOrCompute` overload for `InstBlockId`s, and may save some work if
  36. // the same canonical inst block is used by multiple instructions, for example
  37. // as a specific argument list.
  38. using FingerprintStore =
  39. FixedSizeValueStore<InstId, uint64_t, Tag<CheckIRId>>;
  40. using FilesFingerprintStores =
  41. FixedSizeValueStore<CheckIRId, FingerprintStore>;
  42. FilesFingerprintStores fingerprints_;
  43. };
  44. } // namespace Carbon::SemIR
  45. #endif // CARBON_TOOLCHAIN_SEM_IR_INST_FINGERPRINTER_H_