Caching.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- Caching.h - LLVM Local File Cache ------------------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file defines the CachedFileStream and the localCache function, which
  15. // simplifies caching files on the local filesystem in a directory whose
  16. // contents are managed by a CachePruningPolicy.
  17. //
  18. //===----------------------------------------------------------------------===//
  19. #ifndef LLVM_SUPPORT_CACHING_H
  20. #define LLVM_SUPPORT_CACHING_H
  21. #include "llvm/Support/Error.h"
  22. namespace llvm {
  23. class MemoryBuffer;
  24. /// This class wraps an output stream for a file. Most clients should just be
  25. /// able to return an instance of this base class from the stream callback, but
  26. /// if a client needs to perform some action after the stream is written to,
  27. /// that can be done by deriving from this class and overriding the destructor.
  28. class CachedFileStream {
  29. public:
  30. CachedFileStream(std::unique_ptr<raw_pwrite_stream> OS,
  31. std::string OSPath = "")
  32. : OS(std::move(OS)), ObjectPathName(OSPath) {}
  33. std::unique_ptr<raw_pwrite_stream> OS;
  34. std::string ObjectPathName;
  35. virtual ~CachedFileStream() = default;
  36. };
  37. /// This type defines the callback to add a file that is generated on the fly.
  38. ///
  39. /// Stream callbacks must be thread safe.
  40. using AddStreamFn = std::function<Expected<std::unique_ptr<CachedFileStream>>(
  41. unsigned Task, const Twine &ModuleName)>;
  42. /// This is the type of a file cache. To request an item from the cache, pass a
  43. /// unique string as the Key. For hits, the cached file will be added to the
  44. /// link and this function will return AddStreamFn(). For misses, the cache will
  45. /// return a stream callback which must be called at most once to produce
  46. /// content for the stream. The file stream produced by the stream callback will
  47. /// add the file to the link after the stream is written to. ModuleName is the
  48. /// unique module identifier for the bitcode module the cache is being checked
  49. /// for.
  50. ///
  51. /// Clients generally look like this:
  52. ///
  53. /// if (AddStreamFn AddStream = Cache(Task, Key, ModuleName))
  54. /// ProduceContent(AddStream);
  55. using FileCache = std::function<Expected<AddStreamFn>(
  56. unsigned Task, StringRef Key, const Twine &ModuleName)>;
  57. /// This type defines the callback to add a pre-existing file (e.g. in a cache).
  58. ///
  59. /// Buffer callbacks must be thread safe.
  60. using AddBufferFn = std::function<void(unsigned Task, const Twine &ModuleName,
  61. std::unique_ptr<MemoryBuffer> MB)>;
  62. /// Create a local file system cache which uses the given cache name, temporary
  63. /// file prefix, cache directory and file callback. This function does not
  64. /// immediately create the cache directory if it does not yet exist; this is
  65. /// done lazily the first time a file is added. The cache name appears in error
  66. /// messages for errors during caching. The temporary file prefix is used in the
  67. /// temporary file naming scheme used when writing files atomically.
  68. Expected<FileCache> localCache(
  69. const Twine &CacheNameRef, const Twine &TempFilePrefixRef,
  70. const Twine &CacheDirectoryPathRef,
  71. AddBufferFn AddBuffer = [](size_t Task, const Twine &ModuleName,
  72. std::unique_ptr<MemoryBuffer> MB) {});
  73. } // namespace llvm
  74. #endif
  75. #ifdef __GNUC__
  76. #pragma GCC diagnostic pop
  77. #endif