xref: /llvm-project/llvm/include/llvm/Support/Caching.h (revision ed59d571f2704aff4dfdeace57c286a2c6deac74)
1 //===- Caching.h - LLVM Local File Cache ------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines the CachedFileStream and the localCache function, which
10 // simplifies caching files on the local filesystem in a directory whose
11 // contents are managed by a CachePruningPolicy.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_SUPPORT_CACHING_H
16 #define LLVM_SUPPORT_CACHING_H
17 
18 #include "llvm/Support/Error.h"
19 
20 namespace llvm {
21 
22 class MemoryBuffer;
23 
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 
38 /// This type defines the callback to add a file that is generated on the fly.
39 ///
40 /// Stream callbacks must be thread safe.
41 using AddStreamFn = std::function<Expected<std::unique_ptr<CachedFileStream>>(
42     unsigned Task, const Twine &ModuleName)>;
43 
44 /// This is a callable that manages file caching operations. It accepts a task
45 /// ID \p Task, a unique key \p Key, and a module name \p ModuleName, and
46 /// returns AddStreamFn(). This function determines whether a cache hit or miss
47 /// occurs and handles the appropriate actions.
48 using FileCacheFunction = std::function<Expected<AddStreamFn>(
49     unsigned Task, StringRef Key, const Twine &ModuleName)>;
50 
51 /// This type represents a file cache system that manages caching of files.
52 /// It encapsulates a caching function and the directory path where the cache is
53 /// stored. To request an item from the cache, pass a unique string as the Key.
54 /// For hits, the cached file will be added to the link and this function will
55 /// return AddStreamFn(). For misses, the cache will return a stream callback
56 /// which must be called at most once to produce content for the stream. The
57 /// file stream produced by the stream callback will add the file to the link
58 /// after the stream is written to. ModuleName is the unique module identifier
59 /// for the bitcode module the cache is being checked for.
60 ///
61 /// Clients generally look like this:
62 ///
63 /// if (AddStreamFn AddStream = Cache(Task, Key, ModuleName))
64 ///   ProduceContent(AddStream);
65 ///
66 /// CacheDirectoryPath stores the directory path where cached files are kept.
67 struct FileCache {
68   FileCache(FileCacheFunction CacheFn, const std::string &DirectoryPath)
69       : CacheFunction(std::move(CacheFn)), CacheDirectoryPath(DirectoryPath) {}
70   FileCache() = default;
71 
72   Expected<AddStreamFn> operator()(unsigned Task, StringRef Key,
73                                    const Twine &ModuleName) {
74     assert(isValid() && "Invalid cache function");
75     return CacheFunction(Task, Key, ModuleName);
76   }
77   const std::string &getCacheDirectoryPath() const {
78     return CacheDirectoryPath;
79   }
80   bool isValid() const { return static_cast<bool>(CacheFunction); }
81 
82 private:
83   FileCacheFunction CacheFunction = nullptr;
84   std::string CacheDirectoryPath;
85 };
86 
87 /// This type defines the callback to add a pre-existing file (e.g. in a cache).
88 ///
89 /// Buffer callbacks must be thread safe.
90 using AddBufferFn = std::function<void(unsigned Task, const Twine &ModuleName,
91                                        std::unique_ptr<MemoryBuffer> MB)>;
92 
93 /// Create a local file system cache which uses the given cache name, temporary
94 /// file prefix, cache directory and file callback.  This function does not
95 /// immediately create the cache directory if it does not yet exist; this is
96 /// done lazily the first time a file is added.  The cache name appears in error
97 /// messages for errors during caching. The temporary file prefix is used in the
98 /// temporary file naming scheme used when writing files atomically.
99 Expected<FileCache> localCache(
100     const Twine &CacheNameRef, const Twine &TempFilePrefixRef,
101     const Twine &CacheDirectoryPathRef,
102     AddBufferFn AddBuffer = [](size_t Task, const Twine &ModuleName,
103                                std::unique_ptr<MemoryBuffer> MB) {});
104 } // namespace llvm
105 
106 #endif
107