LLVM  14.0.0git
Caching.h
Go to the documentation of this file.
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.
29 public:
30  CachedFileStream(std::unique_ptr<raw_pwrite_stream> OS) : OS(std::move(OS)) {}
31  std::unique_ptr<raw_pwrite_stream> OS;
32  virtual ~CachedFileStream() = default;
33 };
34 
35 /// This type defines the callback to add a file that is generated on the fly.
36 ///
37 /// Stream callbacks must be thread safe.
38 using AddStreamFn =
39  std::function<Expected<std::unique_ptr<CachedFileStream>>(unsigned Task)>;
40 
41 /// This is the type of a file cache. To request an item from the cache, pass a
42 /// unique string as the Key. For hits, the cached file will be added to the
43 /// link and this function will return AddStreamFn(). For misses, the cache will
44 /// return a stream callback which must be called at most once to produce
45 /// content for the stream. The file stream produced by the stream callback will
46 /// add the file to the link after the stream is written to.
47 ///
48 /// Clients generally look like this:
49 ///
50 /// if (AddStreamFn AddStream = Cache(Task, Key))
51 /// ProduceContent(AddStream);
52 using FileCache =
53  std::function<Expected<AddStreamFn>(unsigned Task, StringRef Key)>;
54 
55 /// This type defines the callback to add a pre-existing file (e.g. in a cache).
56 ///
57 /// Buffer callbacks must be thread safe.
58 using AddBufferFn =
59  std::function<void(unsigned Task, std::unique_ptr<MemoryBuffer> MB)>;
60 
61 /// Create a local file system cache which uses the given cache name, temporary
62 /// file prefix, cache directory and file callback. This function also creates
63 /// the cache directory if it does not already exist. The cache name appears in
64 /// error messages for errors during caching. The temporary file prefix is used
65 /// in the temporary file naming scheme used when writing files atomically.
67  Twine CacheNameRef, Twine TempFilePrefixRef, Twine CacheDirectoryPathRef,
68  AddBufferFn AddBuffer = [](size_t Task, std::unique_ptr<MemoryBuffer> MB) {
69  });
70 } // namespace llvm
71 
72 #endif
llvm::FileCache
std::function< Expected< AddStreamFn >(unsigned Task, StringRef Key)> FileCache
This is the type of a file cache.
Definition: Caching.h:53
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AllocatorList.h:23
llvm::CachedFileStream::OS
std::unique_ptr< raw_pwrite_stream > OS
Definition: Caching.h:31
llvm::CachedFileStream
This class wraps an output stream for a file.
Definition: Caching.h:28
llvm::AddStreamFn
std::function< Expected< std::unique_ptr< CachedFileStream > >(unsigned Task)> AddStreamFn
This type defines the callback to add a file that is generated on the fly.
Definition: Caching.h:39
llvm::AddBufferFn
std::function< void(unsigned Task, std::unique_ptr< MemoryBuffer > MB)> AddBufferFn
This type defines the callback to add a pre-existing file (e.g.
Definition: Caching.h:59
Error.h
llvm::Expected
Tagged union holding either a T or a Error.
Definition: APFloat.h:42
llvm::AMDGPU::PALMD::Key
Key
PAL metadata keys.
Definition: AMDGPUMetadata.h:481
llvm::localCache
Expected< FileCache > localCache(Twine CacheNameRef, Twine TempFilePrefixRef, Twine CacheDirectoryPathRef, AddBufferFn AddBuffer=[](size_t Task, std::unique_ptr< MemoryBuffer > MB) { })
Create a local file system cache which uses the given cache name, temporary file prefix,...
Definition: Caching.cpp:29
llvm::CachedFileStream::CachedFileStream
CachedFileStream(std::unique_ptr< raw_pwrite_stream > OS)
Definition: Caching.h:30
llvm::move
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1658
llvm::CachedFileStream::~CachedFileStream
virtual ~CachedFileStream()=default
function
print Print MemDeps of function
Definition: MemDepPrinter.cpp:83
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:57
llvm::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:83
std
Definition: BitVector.h:838