LLVM: lib/Support/FileOutputBuffer.cpp Source File (original) (raw)
1
2
3
4
5
6
7
8
9
10
11
12
18#include <system_error>
19
20#if !defined(_MSC_VER) && !defined(__MINGW32__)
21#include <unistd.h>
22#else
23#include <io.h>
24#endif
25
26using namespace llvm;
28
29namespace {
30
31
32
34public:
35 OnDiskBuffer(StringRef Path, fs::TempFile Temp, fs::mapped_file_region Buf)
36 : FileOutputBuffer(Path), Buffer(std::move(Buf)), Temp(std::move(Temp)) {}
37
38 uint8_t *getBufferStart() const override { return (uint8_t *)Buffer.data(); }
39
40 uint8_t *getBufferEnd() const override {
41 return (uint8_t *)Buffer.data() + Buffer.size();
42 }
43
44 size_t getBufferSize() const override { return Buffer.size(); }
45
46 Error commit() override {
47 llvm::TimeTraceScope timeScope("Commit buffer to disk");
48
49
50 Buffer.unmap();
51
52
53 return Temp.keep(FinalPath);
54 }
55
56 ~OnDiskBuffer() override {
57
58
59 Buffer.unmap();
61 }
62
63 void discard() override {
64
65
67 }
68
69private:
70 fs::mapped_file_region Buffer;
71 fs::TempFile Temp;
72};
73
74
75
77public:
78 InMemoryBuffer(StringRef Path, MemoryBlock Buf, std::size_t BufSize,
79 unsigned Mode)
80 : FileOutputBuffer(Path), Buffer(Buf), BufferSize(BufSize),
81 Mode(Mode) {}
82
83 uint8_t *getBufferStart() const override { return (uint8_t *)Buffer.base(); }
84
85 uint8_t *getBufferEnd() const override {
86 return (uint8_t *)Buffer.base() + BufferSize;
87 }
88
89 size_t getBufferSize() const override { return BufferSize; }
90
91 Error commit() override {
92 if (FinalPath == "-") {
93 llvm::outs() << StringRef((const char *)Buffer.base(), BufferSize);
96 }
97
98 using namespace sys::fs;
99 int FD;
100 std::error_code EC;
101 if (auto EC =
102 openFileForWrite(FinalPath, FD, CD_CreateAlways, OF_Delete, Mode))
104 raw_fd_ostream OS(FD, true, true);
105 OS << StringRef((const char *)Buffer.base(), BufferSize);
107 }
108
109private:
110
111 OwningMemoryBlock Buffer;
112 size_t BufferSize;
113 unsigned Mode;
114};
115}
116
119 std::error_code EC;
122 if (EC)
124 return std::make_unique(Path, MB, Size, Mode);
125}
126
131 if (!FileOrErr)
134
138 }
139
140
141 std::error_code EC;
145
146
147
148 if (EC) {
151 }
152
153 return std::make_unique(Path, std::move(File),
154 std::move(MappedFile));
155}
156
157
160
161 if (Path == "-")
163
167
168
169 if (Size == 0)
171
174
175
176
177
178
179
180
181
182
183 switch (Stat.type()) {
191 else
193 default:
195 }
196}
static Expected< std::unique_ptr< FileOutputBuffer > > createOnDiskBuffer(StringRef Path, size_t Size, unsigned Mode)
Definition FileOutputBuffer.cpp:128
static Expected< std::unique_ptr< InMemoryBuffer > > createInMemoryBuffer(StringRef Path, size_t Size, unsigned Mode)
Definition FileOutputBuffer.cpp:118
static cl::opt< RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode > Mode("regalloc-enable-advisor", cl::Hidden, cl::init(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default), cl::desc("Enable regalloc advisor mode"), cl::values(clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default, "default", "Default"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Release, "release", "precompiled"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Development, "development", "for training")))
static ErrorSuccess success()
Create a success value.
Tagged union holding either a T or a Error.
Error takeError()
Take ownership of the stored error.
FileOutputBuffer - This interface provides simple way to create an in-memory buffer which will be wri...
static LLVM_ABI Expected< std::unique_ptr< FileOutputBuffer > > create(StringRef FilePath, size_t Size, unsigned Flags=0)
Factory method to create an OutputBuffer object which manages a read/write buffer of the specified si...
Definition FileOutputBuffer.cpp:159
@ F_mmap
Use mmap for in-memory file buffer.
@ F_executable
Set the 'x' bit on the resulting file.
StringRef - Represent a constant reference to a string, i.e.
This class encapsulates the notion of a memory block which has an address and a size.
static LLVM_ABI MemoryBlock allocateMappedMemory(size_t NumBytes, const MemoryBlock *const NearBlock, unsigned Flags, std::error_code &EC)
This method allocates a block of memory that is suitable for loading dynamically generated code (e....
Represents a temporary file.
static LLVM_ABI Expected< TempFile > create(const Twine &Model, unsigned Mode=all_read|all_write, OpenFlags ExtraFlags=OF_None)
This creates a temporary file with createUniqueFile and schedules it for deletion with sys::RemoveFil...
Represents the result of a call to sys::fs::status().
This class represents a memory mapped file.
@ readwrite
May access map via data and modify it. Written to path.
std::error_code resize_file_before_mapping_readwrite(int FD, uint64_t Size)
Resize FD to Size before mapping mapped_file_region::readwrite.
std::error_code openFileForWrite(const Twine &Name, int &ResultFD, CreationDisposition Disp=CD_CreateAlways, OpenFlags Flags=OF_None, unsigned Mode=0666)
Opens the file with the given name in a write-only or read-write mode, returning its open file descri...
LLVM_ABI file_t convertFDToNativeFile(int FD)
Converts from a Posix file descriptor number to a native file handle.
LLVM_ABI std::error_code status(const Twine &path, file_status &result, bool follow=true)
Get file status as if by POSIX stat().
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI raw_fd_ostream & outs()
This returns a reference to a raw_fd_ostream for standard output.
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
LLVM_ABI Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
void consumeError(Error Err)
Consume a Error without doing anything.