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/Compiler.h" 19 #include "llvm/Support/Error.h" 20 21 namespace llvm { 22 23 class MemoryBuffer; 24 25 /// This class wraps an output stream for a file. Most clients should just be 26 /// able to return an instance of this base class from the stream callback, but 27 /// if a client needs to perform some action after the stream is written to, 28 /// that can be done by deriving from this class and overriding the destructor 29 /// or the commit() method. 30 class CachedFileStream { 31 public: 32 CachedFileStream(std::unique_ptr<raw_pwrite_stream> OS, 33 std::string OSPath = "") OS(std::move (OS))34 : OS(std::move(OS)), ObjectPathName(OSPath) {} 35 36 /// Must be called exactly once after the writes to OS have been completed 37 /// but before the CachedFileStream object is destroyed. commit()38 virtual Error commit() { 39 if (Committed) 40 return createStringError(make_error_code(std::errc::invalid_argument), 41 Twine("CacheStream already committed.")); 42 Committed = true; 43 44 return Error::success(); 45 } 46 47 bool Committed = false; 48 std::unique_ptr<raw_pwrite_stream> OS; 49 std::string ObjectPathName; ~CachedFileStream()50 virtual ~CachedFileStream() { 51 if (!Committed) 52 report_fatal_error("CachedFileStream was not committed.\n"); 53 } 54 }; 55 56 /// This type defines the callback to add a file that is generated on the fly. 57 /// 58 /// Stream callbacks must be thread safe. 59 using AddStreamFn = std::function<Expected<std::unique_ptr<CachedFileStream>>( 60 unsigned Task, const Twine &ModuleName)>; 61 62 /// This is a callable that manages file caching operations. It accepts a task 63 /// ID \p Task, a unique key \p Key, and a module name \p ModuleName, and 64 /// returns AddStreamFn(). This function determines whether a cache hit or miss 65 /// occurs and handles the appropriate actions. 66 using FileCacheFunction = std::function<Expected<AddStreamFn>( 67 unsigned Task, StringRef Key, const Twine &ModuleName)>; 68 69 /// This type represents a file cache system that manages caching of files. 70 /// It encapsulates a caching function and the directory path where the cache is 71 /// stored. To request an item from the cache, pass a unique string as the Key. 72 /// For hits, the cached file will be added to the link and this function will 73 /// return AddStreamFn(). For misses, the cache will return a stream callback 74 /// which must be called at most once to produce content for the stream. The 75 /// file stream produced by the stream callback will add the file to the link 76 /// after the stream is written to. ModuleName is the unique module identifier 77 /// for the bitcode module the cache is being checked for. 78 /// 79 /// Clients generally look like this: 80 /// 81 /// if (AddStreamFn AddStream = Cache(Task, Key, ModuleName)) 82 /// ProduceContent(AddStream); 83 /// 84 /// CacheDirectoryPath stores the directory path where cached files are kept. 85 struct FileCache { FileCacheFileCache86 FileCache(FileCacheFunction CacheFn, const std::string &DirectoryPath) 87 : CacheFunction(std::move(CacheFn)), CacheDirectoryPath(DirectoryPath) {} 88 FileCache() = default; 89 operatorFileCache90 Expected<AddStreamFn> operator()(unsigned Task, StringRef Key, 91 const Twine &ModuleName) { 92 assert(isValid() && "Invalid cache function"); 93 return CacheFunction(Task, Key, ModuleName); 94 } getCacheDirectoryPathFileCache95 const std::string &getCacheDirectoryPath() const { 96 return CacheDirectoryPath; 97 } isValidFileCache98 bool isValid() const { return static_cast<bool>(CacheFunction); } 99 100 private: 101 FileCacheFunction CacheFunction = nullptr; 102 std::string CacheDirectoryPath; 103 }; 104 105 /// This type defines the callback to add a pre-existing file (e.g. in a cache). 106 /// 107 /// Buffer callbacks must be thread safe. 108 using AddBufferFn = std::function<void(unsigned Task, const Twine &ModuleName, 109 std::unique_ptr<MemoryBuffer> MB)>; 110 111 /// Create a local file system cache which uses the given cache name, temporary 112 /// file prefix, cache directory and file callback. This function does not 113 /// immediately create the cache directory if it does not yet exist; this is 114 /// done lazily the first time a file is added. The cache name appears in error 115 /// messages for errors during caching. The temporary file prefix is used in the 116 /// temporary file naming scheme used when writing files atomically. 117 LLVM_ABI Expected<FileCache> localCache( 118 const Twine &CacheNameRef, const Twine &TempFilePrefixRef, 119 const Twine &CacheDirectoryPathRef, 120 AddBufferFn AddBuffer = [](size_t Task, const Twine &ModuleName, 121 std::unique_ptr<MemoryBuffer> MB) {}); 122 } // namespace llvm 123 124 #endif 125