1//===- CodeGenData.h --------------------------------------------*- C++ -*-===// 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 7//===----------------------------------------------------------------------===// 9// This file contains support for codegen data that has stable summary which 10// can be used to optimize the code in the subsequent codegen. 12//===----------------------------------------------------------------------===// 14#ifndef LLVM_CGDATA_CODEGENDATA_H 15#define LLVM_CGDATA_CODEGENDATA_H 33#define CG_DATA_SECT_ENTRY(Kind, SectNameCommon, SectNameCoff, Prefix) Kind, 39bool AddSegmentInfo =
true);
43// A function outlining info. 45// A function merging info. 69 : Err(Err), Msg(ErrStr.str()) {
73 std::string
message()
const override;
84 /// Consume an Error and return the raw enum value contained within it, and 85 /// the optional error message. The Error must either be a success value, or 86 /// contain a single CGDataError. 112 /// Global outlined hash tree that has oulined hash sequences across modules. 113 std::unique_ptr<OutlinedHashTree> PublishedHashTree;
114 /// Global stable function map that has stable function info across modules. 115 std::unique_ptr<StableFunctionMap> PublishedStableFunctionMap;
117 /// This flag is set when -fcodegen-data-generate is passed. 118 /// Or, it can be mutated with -fcodegen-data-thinlto-two-rounds. 121 /// This is a singleton instance which is thread-safe. Unlike profile data 122 /// which is largely function-based, codegen data describes the whole module. 123 /// Therefore, this can be initialized once, and can be used across modules 124 /// instead of constructing the same one for each codegen backend. 125static std::unique_ptr<CodeGenData> Instance;
126static std::once_flag OnceFlag;
135 /// Returns true if we have a valid outlined hash tree. 137return PublishedHashTree && !PublishedHashTree->empty();
140return PublishedStableFunctionMap && !PublishedStableFunctionMap->empty();
143 /// Returns the outlined hash tree. This can be globally used in a read-only 146return PublishedHashTree.get();
149return PublishedStableFunctionMap.get();
152 /// Returns true if we should write codegen data. 155 /// Publish the (globally) merged or read outlined hash tree. 157 PublishedHashTree = std::move(HashTree);
158// Ensure we disable emitCGData as we do not want to read and write both. 163 PublishedStableFunctionMap = std::move(FunctionMap);
164// Ensure we disable emitCGData as we do not want to read and write both. 200 /// Backing buffer for serialized data stream. 202 /// Callback function to add serialized data to the stream. 204 /// Backing buffer for cached data. 206 /// Cache mechanism for storing data. 210constTwine &CachePrefix)
213return std::make_unique<CachedFileStream>(
214 std::make_unique<raw_svector_ostream>(
Outputs[Task]));
221 std::unique_ptr<MemoryBuffer> MB) {
222 Files[Task] = std::move(MB);
224if (
Error Err = CGCacheOrErr.takeError())
226Cache = std::move(*CGCacheOrErr);
231 /// Retrieve results from either the cache or the stream. 233unsigned NumOutputs =
Outputs.size();
234auto Result = std::make_unique<SmallVector<StringRef>>(NumOutputs);
235for (
unsignedI = 0;
I < NumOutputs; ++
I)
237 (*Result)[
I] =
Files[
I]->getBuffer();
244/// Save \p TheModule before the first codegen round. 245/// \p Task represents the partition number in the parallel code generation 246/// process. \p AddStream is the callback used to add the serialized module to 251/// Load the optimized bitcode module for the second codegen round. 252/// \p OrigModule is the original bitcode module. 253/// \p Task identifies the partition number in the parallel code generation 254/// process. \p Context provides the environment settings for module operations. 255/// \p IRFiles contains optimized bitcode module files needed for loading. 256/// \return A unique_ptr to the loaded Module, or nullptr if loading fails. 262/// Merge the codegen data from the scratch objects \p ObjectFiles from the 263/// first codegen round. 264/// \return the combined hash of the merged codegen data. 268voidwarn(
Twine Message, std::string Whence =
"", std::string Hint =
"");
270}
// end namespace cgdata 272namespaceIndexedCGData {
274// A signature for data validation, representing "\xffcgdata\x81" in 275// little-endian order 279// Version 1 is the first version. This version supports the outlined 282// Version 2 supports the stable function merging map. 295// New fields should only be added at the end to ensure that the size 296// computation is correct. The methods below need to be updated to ensure that 297// the new field is read correctly. 299// Reads a header struct from the buffer. 303}
// end namespace IndexedCGData 305}
// end namespace llvm 307#endif// LLVM_CODEGEN_PREPARE_H static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
Module.h This file contains the declarations for the Module class.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Represents a module in a bitcode file.
const std::string & getMessage() const
std::error_code convertToErrorCode() const override
Convert this error to a std::error_code.
static std::pair< cgdata_error, std::string > take(Error E)
Consume an Error and return the raw enum value contained within it, and the optional error message.
std::string message() const override
Return the error message as a string.
void log(raw_ostream &OS) const override
Print an error message to an output stream.
CGDataError(cgdata_error Err, const Twine &ErrStr=Twine())
bool hasStableFunctionMap()
const StableFunctionMap * getStableFunctionMap()
bool emitCGData()
Returns true if we should write codegen data.
void publishOutlinedHashTree(std::unique_ptr< OutlinedHashTree > HashTree)
Publish the (globally) merged or read outlined hash tree.
bool hasOutlinedHashTree()
Returns true if we have a valid outlined hash tree.
const OutlinedHashTree * getOutlinedHashTree()
Returns the outlined hash tree.
void publishStableFunctionMap(std::unique_ptr< StableFunctionMap > FunctionMap)
static CodeGenData & getInstance()
Base class for user error types.
Lightweight error class with error context and mandatory checking.
Tagged union holding either a T or a Error.
This is an important class for using LLVM in a threaded context.
A Module instance is used to store all the information related to an LLVM module.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
This class implements an extremely fast bulk output stream that can only output to a stream.
Expected< stable_hash > mergeCodeGenData(ArrayRef< StringRef > ObjectFiles)
Merge the codegen data from the scratch objects ObjectFiles from the first codegen round.
void publishOutlinedHashTree(std::unique_ptr< OutlinedHashTree > HashTree)
bool hasOutlinedHashTree()
bool hasStableFunctionMap()
void warn(Error E, StringRef Whence="")
const OutlinedHashTree * getOutlinedHashTree()
void publishStableFunctionMap(std::unique_ptr< StableFunctionMap > FunctionMap)
void saveModuleForTwoRounds(const Module &TheModule, unsigned Task, AddStreamFn AddStream)
Save TheModule before the first codegen round.
const StableFunctionMap * getStableFunctionMap()
std::unique_ptr< Module > loadModuleForTwoRounds(BitcodeModule &OrigModule, unsigned Task, LLVMContext &Context, ArrayRef< StringRef > IRFiles)
Load the optimized bitcode module for the second codegen round.
This is an optimization pass for GlobalISel generic memory operations.
@ StableFunctionMergingMap
@ FunctionOutlinedHashTree
std::error_code make_error_code(BitcodeError E)
void handleAllErrors(Error E, HandlerTs &&... Handlers)
Behaves the same as handleErrors, except that by contract all errors must be handled by the given han...
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.
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
const std::error_category & cgdata_category()
@ LLVM_MARK_AS_BITMASK_ENUM
Expected< FileCache > localCache(const Twine &CacheNameRef, const Twine &TempFilePrefixRef, const Twine &CacheDirectoryPathRef, AddBufferFn AddBuffer=[](size_t Task, const Twine &ModuleName, std::unique_ptr< MemoryBuffer > MB) {})
Create a local file system cache which uses the given cache name, temporary file prefix,...
std::string getCodeGenDataSectionName(CGDataSectKind CGSK, Triple::ObjectFormatType OF, bool AddSegmentInfo=true)
This type represents a file cache system that manages caching of files.
const std::string & getCacheDirectoryPath() const
uint64_t StableFunctionMapOffset
static Expected< Header > readFromBuffer(const unsigned char *Curr)
uint64_t OutlinedHashTreeOffset
SmallVector< SmallString< 0 > > Outputs
Backing buffer for serialized data stream.
FileCache Cache
Cache mechanism for storing data.
SmallVector< std::unique_ptr< MemoryBuffer > > Files
Backing buffer for cached data.
std::unique_ptr< SmallVector< StringRef > > getResult()
Retrieve results from either the cache or the stream.
AddStreamFn AddStream
Callback function to add serialized data to the stream.
StreamCacheData(unsigned Size, const FileCache &OrigCache, const Twine &CachePrefix)