LLVM 22.0.0git
HashingOutputBackend.h
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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/// \file
10/// This file contains the declarations of the HashingOutputBackend class, which
11/// is the VirtualOutputBackend that only produces the hashes for the output
12/// files. This is useful for checking if the outputs are deterministic without
13/// storing output files in memory or on disk.
14///
15//===----------------------------------------------------------------------===//
16
17#ifndef LLVM_SUPPORT_HASHINGOUTPUTBACKEND_H
18#define LLVM_SUPPORT_HASHINGOUTPUTBACKEND_H
19
21#include "llvm/ADT/StringMap.h"
26#include <mutex>
27
28namespace llvm::vfs {
29
30/// raw_pwrite_stream that writes to a hasher.
31template <typename HasherT>
33private:
34 SmallVector<char> Buffer;
36
38 HashBuilderT Builder;
39
40 void write_impl(const char *Ptr, size_t Size) override {
41 OS.write(Ptr, Size);
42 }
43
44 void pwrite_impl(const char *Ptr, size_t Size, uint64_t Offset) override {
45 OS.pwrite(Ptr, Size, Offset);
46 }
47
48 uint64_t current_pos() const override { return OS.str().size(); }
49
50public:
51 HashingStream() : OS(Buffer) { SetUnbuffered(); }
52
53 auto final() {
54 Builder.update(OS.str());
55 return Builder.final();
56 }
57};
58
59template <typename HasherT> class HashingOutputFile;
60
61/// An output backend that only generates the hash for outputs.
62template <typename HasherT> class HashingOutputBackend : public OutputBackend {
63private:
64 friend class HashingOutputFile<HasherT>;
65 void addOutputFile(StringRef Path, StringRef Hash) {
66 std::lock_guard<std::mutex> Lock(OutputHashLock);
67 OutputHashes[Path] = Hash.str();
68 }
69
70protected:
72 return const_cast<HashingOutputBackend<HasherT> *>(this);
73 }
74
76 createFileImpl(StringRef Path, std::optional<OutputConfig> Config) override {
77 return std::make_unique<HashingOutputFile<HasherT>>(Path, *this);
78 }
79
80public:
81 /// Iterator for all the output file names.
82 ///
83 /// Not thread safe. Should be queried after all outputs are written.
84 auto outputFiles() const { return OutputHashes.keys(); }
85
86 /// Get hash value for the output files in hex representation.
87 /// Return None if the requested path is not generated.
88 ///
89 /// Not thread safe. Should be queried after all outputs are written.
90 std::optional<std::string> getHashValueForFile(StringRef Path) {
91 auto F = OutputHashes.find(Path);
92 if (F == OutputHashes.end())
93 return std::nullopt;
94 return toHex(F->second);
95 }
96
97private:
98 std::mutex OutputHashLock;
99 StringMap<std::string> OutputHashes;
100};
101
102/// HashingOutputFile.
103template <typename HasherT>
104class HashingOutputFile final : public OutputFileImpl {
105public:
106 Error keep() override {
107 auto Result = OS.final();
108 Backend.addOutputFile(OutputPath, toStringRef(Result));
109 return Error::success();
110 }
111 Error discard() override { return Error::success(); }
112 raw_pwrite_stream &getOS() override { return OS; }
113
116 : OutputPath(OutputPath.str()), Backend(Backend) {}
117
118private:
119 const std::string OutputPath;
122};
123
124} // namespace llvm::vfs
125
126#endif // LLVM_SUPPORT_HASHINGOUTPUTBACKEND_H
This file defines the StringMap class.
#define F(x, y, z)
Definition MD5.cpp:55
This file contains some functions that are useful when dealing with strings.
This file contains the declarations of the VirtualOutputBackend class, which can be used to virtualiz...
This file contains the declarations of the OutputConfig class.
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
static ErrorSuccess success()
Create a success value.
Definition Error.h:336
Tagged union holding either a T or a Error.
Definition Error.h:485
Interface to help hash various types through a hasher type.
A smart pointer to a reference-counted object that inherits from RefCountedBase or ThreadSafeRefCount...
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition StringMap.h:133
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
std::string str() const
str - Get the contents as an std::string.
Definition StringRef.h:233
void SetUnbuffered()
Set the stream to be unbuffered.
An abstract base class for streams implementations that also support a pwrite operation.
A raw_ostream that writes to an SmallVector or SmallString.
An output backend that only generates the hash for outputs.
std::optional< std::string > getHashValueForFile(StringRef Path)
Get hash value for the output files in hex representation.
IntrusiveRefCntPtr< OutputBackend > cloneImpl() const override
Must be thread-safe.
Expected< std::unique_ptr< OutputFileImpl > > createFileImpl(StringRef Path, std::optional< OutputConfig > Config) override
Create a file for Path.
auto outputFiles() const
Iterator for all the output file names.
HashingOutputFile(StringRef OutputPath, HashingOutputBackend< HasherT > &Backend)
raw_pwrite_stream & getOS() override
raw_pwrite_stream that writes to a hasher.
@ Offset
Definition DWP.cpp:477
void toHex(ArrayRef< uint8_t > Input, bool LowerCase, SmallVectorImpl< char > &Output)
Convert buffer Input to its hexadecimal representation. The returned string is double the size of Inp...
StringRef toStringRef(bool B)
Construct a string ref from a boolean.