23#define DEBUG_TYPE "cg-data"
29 const std::string &ErrMsg =
"") {
41 OS <<
"invalid codegen data (bad magic)";
44 OS <<
"invalid codegen data (file header is corrupt)";
47 OS <<
"empty codegen data";
50 OS <<
"malformed codegen data";
53 OS <<
"unsupported codegen data version";
69class CGDataErrorCategoryType :
public std::error_category {
70 const char *
name()
const noexcept
override {
return "llvm.cgdata"; }
72 std::string message(
int IE)
const override {
80 static CGDataErrorCategoryType ErrorCategory;
92const char *CodeGenDataSectNameCommon[] = {
93#define CG_DATA_SECT_ENTRY(Kind, SectNameCommon, SectNameCoff, Prefix) \
98const char *CodeGenDataSectNameCoff[] = {
99#define CG_DATA_SECT_ENTRY(Kind, SectNameCommon, SectNameCoff, Prefix) \
104const char *CodeGenDataSectNamePrefix[] = {
105#define CG_DATA_SECT_ENTRY(Kind, SectNameCommon, SectNameCoff, Prefix) Prefix,
113 return LibraryOptions.
get<CGDataOptions>().codegen_data_thinlto_two_rounds;
120 bool AddSegmentInfo) {
121 std::string SectName;
124 SectName = CodeGenDataSectNamePrefix[CGSK];
127 SectName += CodeGenDataSectNameCoff[CGSK];
129 SectName += CodeGenDataSectNameCommon[CGSK];
134std::unique_ptr<CodeGenData> CodeGenData::Instance =
nullptr;
135std::once_flag CodeGenData::OnceFlag;
139 std::call_once(CodeGenData::OnceFlag, [&LibraryOptions]() {
140 Instance = std::unique_ptr<CodeGenData>(
new CodeGenData());
142 const CGDataOptions &Opts = LibraryOptions.
get<CGDataOptions>();
143 if (Opts.codegen_data_generate || Opts.codegen_data_thinlto_two_rounds)
144 Instance->EmitCGData =
true;
145 else if (!Opts.codegen_data_use_path.empty()) {
152 LibraryOptions, Opts.codegen_data_use_path, *FS,
153 Opts.indexed_codegen_data_lazy_loading);
154 if (
Error E = ReaderOrErr.takeError()) {
155 warn(std::move(E), Opts.codegen_data_use_path);
159 auto Reader = ReaderOrErr->get();
160 if (Reader->hasOutlinedHashTree())
161 Instance->publishOutlinedHashTree(Reader->releaseOutlinedHashTree());
162 if (Reader->hasStableFunctionMap())
163 Instance->publishStableFunctionMap(Reader->releaseStableFunctionMap());
174 static_assert(std::is_standard_layout_v<llvm::IndexedCGData::Header>,
175 "The header should be standard layout type since we use offset "
176 "of fields to read.");
187 "Please update the offset computation below if a new field has "
188 "been added to the header.");
189 H.OutlinedHashTreeOffset =
192 H.StableFunctionMapOffset =
205 errs() << Whence <<
": ";
206 errs() << Message <<
"\n";
222 <<
" in Task " << Task <<
"\n");
227 std::unique_ptr<CachedFileStream> &Stream = *StreamOrErr;
232 if (
Error Err = Stream->commit())
241 <<
" in Task " << Task <<
"\n");
243 IRFiles[Task],
"in-memory IR file",
false);
247 Twine(
"Failed to parse optimized bitcode loaded for Task: ") +
252 return std::move(*RestoredModule);
261 for (
auto File : ObjFiles) {
265 File,
"in-memory object file",
false);
271 std::unique_ptr<object::ObjectFile> &Obj = BinOrErr.
get();
273 Obj.get(), GlobalOutlineRecord, GlobalStableFunctionMapRecord,
278 GlobalStableFunctionMapRecord.
finalize(LibraryOptions);
280 if (!GlobalOutlineRecord.
empty())
282 std::move(GlobalOutlineRecord.
HashTree));
283 if (!GlobalStableFunctionMapRecord.
empty())
285 LibraryOptions, std::move(GlobalStableFunctionMapRecord.
FunctionMap));
static std::string getCGDataErrString(cgdata_error Err, const std::string &ErrMsg="")
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Represents a module in a bitcode file.
StringRef getModuleIdentifier() const
std::string message() const override
Return the error message as a string.
static LLVM_ABI Expected< std::unique_ptr< CodeGenDataReader > > create(const cl::LibraryOptionsContext &LibraryOptions, const Twine &Path, vfs::FileSystem &FS, bool LazyLoading=false)
Factory method to create an appropriately typed reader for the given codegen data file path and file ...
static LLVM_ABI Error mergeFromObjectFile(const object::ObjectFile *Obj, OutlinedHashTreeRecord &GlobalOutlineRecord, StableFunctionMapRecord &GlobalFunctionMapRecord, stable_hash *CombinedHash=nullptr)
Extract the cgdata embedded in sections from the given object file and merge them into the GlobalOutl...
static LLVM_ABI CodeGenData & getInstance(const cl::LibraryOptionsContext &LibraryOptions)
The first call's LibraryOptions configure the singleton.
Lightweight error class with error context and mandatory checking.
Tagged union holding either a T or a Error.
Error takeError()
Take ownership of the stored error.
reference get()
Returns a reference to the stored T value.
This is an important class for using LLVM in a threaded context.
static std::unique_ptr< MemoryBuffer > getMemBuffer(StringRef InputData, StringRef BufferName="", bool RequiresNullTerminator=true)
Open the specified memory range as a MemoryBuffer.
A Module instance is used to store all the information related to an LLVM module.
const std::string & getModuleIdentifier() const
Get the module identifier which is, essentially, the name of the module.
Represent a constant reference to a string, i.e.
constexpr bool empty() const
Check if the string is empty.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
static LLVM_ABI raw_ostream & warning()
Convenience method for printing "warning: " to stderr.
static LLVM_ABI raw_ostream & note()
Convenience method for printing "note: " to stderr.
static Expected< OwningBinary< ObjectFile > > createObjectFile(StringRef ObjectPath)
A raw_ostream that writes to an std::string.
std::string & str()
Returns the string's reference.
LLVM_ABI bool thinLTOTwoRounds(const cl::LibraryOptionsContext &LibraryOptions)
Returns whether ThinLTO runs two rounds of code generation (-codegen-data-thinlto-two-rounds).
void publishStableFunctionMap(const cl::LibraryOptionsContext &LibraryOptions, std::unique_ptr< StableFunctionMap > FunctionMap)
LLVM_ABI void warn(Error E, StringRef Whence="")
void publishOutlinedHashTree(const cl::LibraryOptionsContext &LibraryOptions, std::unique_ptr< OutlinedHashTree > HashTree)
LLVM_ABI void saveModuleForTwoRounds(const Module &TheModule, unsigned Task, AddStreamFn AddStream)
Save TheModule before the first codegen round.
LLVM_ABI Expected< stable_hash > mergeCodeGenData(const cl::LibraryOptionsContext &LibraryOptions, ArrayRef< StringRef > ObjectFiles)
Merge the codegen data from the scratch objects ObjectFiles from the first codegen round.
LLVM_ABI std::unique_ptr< Module > loadModuleForTwoRounds(BitcodeModule &OrigModule, unsigned Task, LLVMContext &Context, ArrayRef< StringRef > IRFiles)
Load the optimized bitcode module for the second codegen round.
value_type readNext(const CharT *&memory, endianness endian)
Read a value of a particular endianness from a buffer, and increment the buffer past that value.
LLVM_ABI IntrusiveRefCntPtr< FileSystem > getRealFileSystem()
Gets an vfs::FileSystem for the 'real' file system, as seen by the operating system.
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI Expected< std::unique_ptr< Module > > parseBitcodeFile(MemoryBufferRef Buffer, LLVMContext &Context, ParserCallbacks Callbacks={})
Read the specified bitcode file, returning the module.
LLVM_ABI void WriteBitcodeToFile(const Module &M, raw_ostream &Out, bool ShouldPreserveUseListOrder=false, const ModuleSummaryIndex *Index=nullptr, bool GenerateHash=false, ModuleHash *ModHash=nullptr)
Write the specified module to the specified raw output stream.
void handleAllErrors(Error E, HandlerTs &&... Handlers)
Behaves the same as handleErrors, except that by contract all errors must be handled by the given han...
uint64_t stable_hash
An opaque object representing a stable hash code.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
LLVM_ABI const std::error_category & cgdata_category()
Error make_error(ArgTs &&... Args)
Make a Error instance representing failure using the given error info type.
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
std::function< Expected< std::unique_ptr< CachedFileStream > >( unsigned Task, const Twine &ModuleName)> AddStreamFn
This type defines the callback to add a file that is generated on the fly.
LLVM_ABI std::string getCodeGenDataSectionName(CGDataSectKind CGSK, Triple::ObjectFormatType OF, bool AddSegmentInfo=true)
std::unique_ptr< OutlinedHashTree > HashTree
The structure of the serialized stable function map is as follows:
std::unique_ptr< StableFunctionMap > FunctionMap
void finalize(const cl::LibraryOptionsContext &LibraryOptions, bool SkipTrim=false)
Finalize the stable function map by trimming content.