LLVM 20.0.0git
LLVMContext.cpp
Go to the documentation of this file.
1//===-- LLVMContext.cpp - Implement LLVMContext ---------------------------===//
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 implements LLVMContext, as a wrapper around the opaque
10// class LLVMContextImpl.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/IR/LLVMContext.h"
15#include "LLVMContextImpl.h"
17#include "llvm/ADT/StringMap.h"
18#include "llvm/ADT/StringRef.h"
19#include "llvm/ADT/Twine.h"
27#include <cassert>
28#include <cstdlib>
29#include <string>
30#include <utility>
31
32using namespace llvm;
33
34static StringRef knownBundleName(unsigned BundleTagID) {
35 switch (BundleTagID) {
37 return "deopt";
39 return "funclet";
41 return "gc-transition";
43 return "cfguardtarget";
45 return "preallocated";
47 return "gc-live";
49 return "clang.arc.attachedcall";
51 return "ptrauth";
53 return "kcfi";
55 return "convergencectrl";
56 default:
57 llvm_unreachable("unknown bundle id");
58 }
59
60 llvm_unreachable("covered switch");
61}
62
64 // Create the fixed metadata kinds. This is done in the same order as the
65 // MD_* enum values so that they correspond.
66 std::pair<unsigned, StringRef> MDKinds[] = {
67#define LLVM_FIXED_MD_KIND(EnumID, Name, Value) {EnumID, Name},
68#include "llvm/IR/FixedMetadataKinds.def"
69#undef LLVM_FIXED_MD_KIND
70 };
71
72 for (auto &MDKind : MDKinds) {
73 unsigned ID = getMDKindID(MDKind.second);
74 assert(ID == MDKind.first && "metadata kind id drifted");
75 (void)ID;
76 }
77
78 for (unsigned BundleTagID = LLVMContext::OB_deopt;
79 BundleTagID <= LLVMContext::OB_convergencectrl; ++BundleTagID) {
80 [[maybe_unused]] const auto *Entry =
82 assert(Entry->second == BundleTagID && "operand bundle id drifted!");
83 }
84
85 SyncScope::ID SingleThreadSSID =
86 pImpl->getOrInsertSyncScopeID("singlethread");
87 assert(SingleThreadSSID == SyncScope::SingleThread &&
88 "singlethread synchronization scope ID drifted!");
89 (void)SingleThreadSSID;
90
91 SyncScope::ID SystemSSID =
93 assert(SystemSSID == SyncScope::System &&
94 "system synchronization scope ID drifted!");
95 (void)SystemSSID;
96}
97
99
100void LLVMContext::addModule(Module *M) {
102}
103
104void LLVMContext::removeModule(Module *M) {
106 pImpl->MachineFunctionNums.erase(M);
107}
108
110 Module *M = F.getParent();
111 assert(pImpl->OwnedModules.contains(M) && "Unexpected module!");
112 return pImpl->MachineFunctionNums[M]++;
113}
114
115//===----------------------------------------------------------------------===//
116// Recoverable Backend Errors
117//===----------------------------------------------------------------------===//
118
121 void *DiagnosticContext, bool RespectFilters) {
122 pImpl->DiagHandler->DiagHandlerCallback = DiagnosticHandler;
123 pImpl->DiagHandler->DiagnosticContext = DiagnosticContext;
124 pImpl->RespectDiagnosticFilters = RespectFilters;
125}
126
127void LLVMContext::setDiagnosticHandler(std::unique_ptr<DiagnosticHandler> &&DH,
128 bool RespectFilters) {
129 pImpl->DiagHandler = std::move(DH);
130 pImpl->RespectDiagnosticFilters = RespectFilters;
131}
132
135}
138}
139
140void LLVMContext::setDiagnosticsHotnessThreshold(std::optional<uint64_t> Threshold) {
142}
144 pImpl->MisExpectWarningRequested = Requested;
145}
148}
151}
153 std::optional<uint32_t> Tolerance) {
155}
157 return pImpl->DiagnosticsMisExpectTolerance.value_or(0);
158}
159
161 return !pImpl->DiagnosticsHotnessThreshold.has_value();
162}
163
165 return pImpl->MainRemarkStreamer.get();
166}
168 return const_cast<LLVMContext *>(this)->getMainRemarkStreamer();
169}
171 std::unique_ptr<remarks::RemarkStreamer> RemarkStreamer) {
172 pImpl->MainRemarkStreamer = std::move(RemarkStreamer);
173}
174
176 return pImpl->LLVMRS.get();
177}
179 return const_cast<LLVMContext *>(this)->getLLVMRemarkStreamer();
180}
182 std::unique_ptr<LLVMRemarkStreamer> RemarkStreamer) {
183 pImpl->LLVMRS = std::move(RemarkStreamer);
184}
185
188 return pImpl->DiagHandler->DiagHandlerCallback;
189}
190
192 return pImpl->DiagHandler->DiagnosticContext;
193}
194
195void LLVMContext::setYieldCallback(YieldCallbackTy Callback, void *OpaqueHandle)
196{
197 pImpl->YieldCallback = Callback;
198 pImpl->YieldOpaqueHandle = OpaqueHandle;
199}
200
202 if (pImpl->YieldCallback)
204}
205
206void LLVMContext::emitError(const Twine &ErrorStr) {
208}
209
210void LLVMContext::emitError(const Instruction *I, const Twine &ErrorStr) {
211 assert(I && "Invalid instruction");
212 diagnose(DiagnosticInfoGeneric(I, ErrorStr));
213}
214
215static bool isDiagnosticEnabled(const DiagnosticInfo &DI) {
216 // Optimization remarks are selective. They need to check whether the regexp
217 // pattern, passed via one of the -pass-remarks* flags, matches the name of
218 // the pass that is emitting the diagnostic. If there is no match, ignore the
219 // diagnostic and return.
220 //
221 // Also noisy remarks are only enabled if we have hotness information to sort
222 // them.
223 if (auto *Remark = dyn_cast<DiagnosticInfoOptimizationBase>(&DI))
224 return Remark->isEnabled() &&
225 (!Remark->isVerbose() || Remark->getHotness());
226
227 return true;
228}
229
230const char *
232 switch (Severity) {
233 case DS_Error:
234 return "error";
235 case DS_Warning:
236 return "warning";
237 case DS_Remark:
238 return "remark";
239 case DS_Note:
240 return "note";
241 }
242 llvm_unreachable("Unknown DiagnosticSeverity");
243}
244
246 if (auto *OptDiagBase = dyn_cast<DiagnosticInfoOptimizationBase>(&DI))
248 RS->emit(*OptDiagBase);
249
250 // If there is a report handler, use it.
251 if (pImpl->DiagHandler) {
252 if (DI.getSeverity() == DS_Error)
253 pImpl->DiagHandler->HasErrors = true;
255 pImpl->DiagHandler->handleDiagnostics(DI))
256 return;
257 }
258
259 if (!isDiagnosticEnabled(DI))
260 return;
261
262 // Otherwise, print the message with a prefix based on the severity.
265 DI.print(DP);
266 errs() << "\n";
267 if (DI.getSeverity() == DS_Error)
268 exit(1);
269}
270
271//===----------------------------------------------------------------------===//
272// Metadata Kind Uniquing
273//===----------------------------------------------------------------------===//
274
275/// Return a unique non-zero ID for the specified metadata kind.
277 // If this is new, assign it its ID.
279 std::make_pair(
281 .first->second;
282}
283
284/// getHandlerNames - Populate client-supplied smallvector using custom
285/// metadata name and ID.
289 E = pImpl->CustomMDKindNames.end(); I != E; ++I)
290 Names[I->second] = I->first();
291}
292
295}
296
299 return pImpl->getOrInsertBundleTag(TagName);
300}
301
304}
305
307 return pImpl->getOrInsertSyncScopeID(SSN);
308}
309
312}
313
314std::optional<StringRef> LLVMContext::getSyncScopeName(SyncScope::ID Id) const {
315 return pImpl->getSyncScopeName(Id);
316}
317
318void LLVMContext::setGC(const Function &Fn, std::string GCName) {
319 pImpl->GCNames[&Fn] = std::move(GCName);
320}
321
322const std::string &LLVMContext::getGC(const Function &Fn) {
323 return pImpl->GCNames[&Fn];
324}
325
327 pImpl->GCNames.erase(&Fn);
328}
329
331 return pImpl->DiscardValueNames;
332}
333
335
337 if (pImpl->DITypeMap)
338 return;
339
340 pImpl->DITypeMap.emplace();
341}
342
344
346 pImpl->DiscardValueNames = Discard;
347}
348
350 return pImpl->getOptPassGate();
351}
352
354 pImpl->setOptPassGate(OPG);
355}
356
358 return pImpl->DiagHandler.get();
359}
360
361std::unique_ptr<DiagnosticHandler> LLVMContext::getDiagnosticHandler() {
362 return std::move(pImpl->DiagHandler);
363}
364
366 return pImpl->DefaultTargetCPU;
367}
368
370 pImpl->DefaultTargetCPU = CPU;
371}
372
375}
376
378 pImpl->DefaultTargetFeatures = Features;
379}
This file defines the StringMap class.
std::string Name
static bool isDiagnosticEnabled(const DiagnosticInfo &DI)
static StringRef knownBundleName(unsigned BundleTagID)
Definition: LLVMContext.cpp:34
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the SmallVector class.
This is the base abstract class for diagnostic reporting in the backend.
DiagnosticSeverity getSeverity() const
virtual void print(DiagnosticPrinter &DP) const =0
Print using the given DP a user-friendly message.
Basic diagnostic printer that uses an underlying raw_ostream.
void getSyncScopeNames(SmallVectorImpl< StringRef > &SSNs) const
getSyncScopeNames - Populates client supplied SmallVector with synchronization scope names registered...
SmallPtrSet< Module *, 4 > OwnedModules
OwnedModules - The set of modules instantiated in this context, and which will be automatically delet...
std::optional< uint32_t > DiagnosticsMisExpectTolerance
The percentage of difference between profiling branch weights and llvm.expect branch weights to toler...
SyncScope::ID getOrInsertSyncScopeID(StringRef SSN)
getOrInsertSyncScopeID - Maps synchronization scope name to synchronization scope ID.
void setOptPassGate(OptPassGate &)
Set the object which can disable optional passes and individual optimizations at compile time.
std::unique_ptr< LLVMRemarkStreamer > LLVMRS
The specialized remark streamer used by LLVM's OptimizationRemarkEmitter.
bool DiscardValueNames
Flag to indicate if Value (other than GlobalValue) retains their name or not.
DenseMap< const Function *, std::string > GCNames
Maintain the GC name for each function.
OptPassGate & getOptPassGate() const
Access the object which can disable optional passes and individual optimizations at compile time.
std::string DefaultTargetFeatures
DenseMap< Module *, unsigned > MachineFunctionNums
MachineFunctionNums - Keep the next available unique number available for a MachineFunction in given ...
StringMap< unsigned > CustomMDKindNames
CustomMDKindNames - Map to hold the metadata string to ID mapping.
StringMapEntry< uint32_t > * getOrInsertBundleTag(StringRef Tag)
std::unique_ptr< DiagnosticHandler > DiagHandler
std::unique_ptr< remarks::RemarkStreamer > MainRemarkStreamer
The main remark streamer used by all the other streamers (e.g.
void getOperandBundleTags(SmallVectorImpl< StringRef > &Tags) const
std::optional< uint64_t > DiagnosticsHotnessThreshold
The minimum hotness value a diagnostic needs in order to be included in optimization diagnostics.
uint32_t getOperandBundleTagID(StringRef Tag) const
LLVMContext::YieldCallbackTy YieldCallback
std::optional< StringRef > getSyncScopeName(SyncScope::ID Id) const
getSyncScopeName - Returns the name of a SyncScope::ID registered with LLVMContext,...
std::optional< DenseMap< const MDString *, DICompositeType * > > DITypeMap
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
void setMisExpectWarningRequested(bool Requested)
std::optional< StringRef > getSyncScopeName(SyncScope::ID Id) const
getSyncScopeName - Returns the name of a SyncScope::ID registered with LLVMContext,...
remarks::RemarkStreamer * getMainRemarkStreamer()
The "main remark streamer" used by all the specialized remark streamers.
uint32_t getOperandBundleTagID(StringRef Tag) const
getOperandBundleTagID - Maps a bundle tag to an integer ID.
void disableDebugTypeODRUniquing()
void setYieldCallback(YieldCallbackTy Callback, void *OpaqueHandle)
Registers a yield callback with the given context.
void deleteGC(const Function &Fn)
Remove the GC for a function.
const std::string & getGC(const Function &Fn)
Return the GC for a function.
DiagnosticHandler::DiagnosticHandlerTy getDiagnosticHandlerCallBack() const
getDiagnosticHandlerCallBack - Return the diagnostic handler call back set by setDiagnosticHandlerCal...
void setLLVMRemarkStreamer(std::unique_ptr< LLVMRemarkStreamer > RemarkStreamer)
unsigned getMDKindID(StringRef Name) const
getMDKindID - Return a unique non-zero ID for the specified metadata kind.
void emitError(const Instruction *I, const Twine &ErrorStr)
emitError - Emit an error message to the currently installed error handler with optional location inf...
static const char * getDiagnosticMessagePrefix(DiagnosticSeverity Severity)
Get the prefix that should be printed in front of a diagnostic of the given Severity.
void enableDebugTypeODRUniquing()
std::unique_ptr< DiagnosticHandler > getDiagnosticHandler()
getDiagnosticHandler - transfers ownership of DiagnosticHandler unique_ptr to caller.
void setDefaultTargetCPU(StringRef CPU)
bool getDiagnosticsHotnessRequested() const
Return if a code hotness metric should be included in optimization diagnostics.
bool getMisExpectWarningRequested() const
void yield()
Calls the yield callback (if applicable).
void setDiagnosticsHotnessThreshold(std::optional< uint64_t > Threshold)
Set the minimum hotness value a diagnostic needs in order to be included in optimization diagnostics.
void setMainRemarkStreamer(std::unique_ptr< remarks::RemarkStreamer > MainRemarkStreamer)
bool isDiagnosticsHotnessThresholdSetFromPSI() const
Return if hotness threshold is requested from PSI.
void setOptPassGate(OptPassGate &)
Set the object which can disable optional passes and individual optimizations at compile time.
StringMapEntry< uint32_t > * getOrInsertBundleTag(StringRef TagName) const
getOrInsertBundleTag - Returns the Tag to use for an operand bundle of name TagName.
void setDiagnosticsHotnessRequested(bool Requested)
Set if a code hotness metric should be included in optimization diagnostics.
void setGC(const Function &Fn, std::string GCName)
Define the GC for a function.
bool shouldDiscardValueNames() const
Return true if the Context runtime configuration is set to discard all value names.
void(*)(LLVMContext *Context, void *OpaqueHandle) YieldCallbackTy
Defines the type of a yield callback.
Definition: LLVMContext.h:168
bool isODRUniquingDebugTypes() const
Whether there is a string map for uniquing debug info identifiers across the context.
unsigned generateMachineFunctionNum(Function &)
generateMachineFunctionNum - Get a unique number for MachineFunction that associated with the given F...
StringRef getDefaultTargetFeatures()
Similar to {get,set}DefaultTargetCPU() but for default target-features.
void setDiagnosticsMisExpectTolerance(std::optional< uint32_t > Tolerance)
LLVMContextImpl *const pImpl
Definition: LLVMContext.h:69
void diagnose(const DiagnosticInfo &DI)
Report a message to the currently installed diagnostic handler.
void getOperandBundleTags(SmallVectorImpl< StringRef > &Result) const
getOperandBundleTags - Populate client supplied SmallVector with the bundle tags registered in this L...
void setDiagnosticHandlerCallBack(DiagnosticHandler::DiagnosticHandlerTy DiagHandler, void *DiagContext=nullptr, bool RespectFilters=false)
setDiagnosticHandlerCallBack - This method sets a handler call back that is invoked when the backend ...
void setDiscardValueNames(bool Discard)
Set the Context runtime configuration to discard all value name (but GlobalValue).
StringRef getDefaultTargetCPU()
Get or set the current "default" target CPU (target-cpu function attribute).
void getMDKindNames(SmallVectorImpl< StringRef > &Result) const
getMDKindNames - Populate client supplied SmallVector with the name for custom metadata IDs registere...
void * getDiagnosticContext() const
getDiagnosticContext - Return the diagnostic context set by setDiagnosticContext.
SyncScope::ID getOrInsertSyncScopeID(StringRef SSN)
getOrInsertSyncScopeID - Maps synchronization scope name to synchronization scope ID.
uint64_t getDiagnosticsHotnessThreshold() const
Return the minimum hotness value a diagnostic would need in order to be included in optimization diag...
void setDefaultTargetFeatures(StringRef Features)
const DiagnosticHandler * getDiagHandlerPtr() const
getDiagHandlerPtr - Returns const raw pointer of DiagnosticHandler set by setDiagnosticHandler.
void setDiagnosticHandler(std::unique_ptr< DiagnosticHandler > &&DH, bool RespectFilters=false)
setDiagnosticHandler - This method sets unique_ptr to object of DiagnosticHandler to provide custom d...
OptPassGate & getOptPassGate() const
Access the object which can disable optional passes and individual optimizations at compile time.
LLVMRemarkStreamer * getLLVMRemarkStreamer()
The "LLVM remark streamer" used by LLVM to serialize remark diagnostics comming from IR and MIR passe...
void getSyncScopeNames(SmallVectorImpl< StringRef > &SSNs) const
getSyncScopeNames - Populates client supplied SmallVector with synchronization scope names registered...
uint32_t getDiagnosticsMisExpectTolerance() const
Streamer for LLVM remarks which has logic for dealing with DiagnosticInfo objects.
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
Extensions to this class implement mechanisms to disable passes and individual optimizations at compi...
Definition: OptBisect.h:24
bool erase(PtrType Ptr)
Remove pointer from the set.
Definition: SmallPtrSet.h:401
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:384
bool contains(ConstPtrType Ptr) const
Definition: SmallPtrSet.h:458
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:573
void resize(size_type N)
Definition: SmallVector.h:638
StringMapEntry - This is used to represent one value that is inserted into a StringMap.
unsigned size() const
Definition: StringMap.h:104
iterator end()
Definition: StringMap.h:220
iterator begin()
Definition: StringMap.h:219
bool insert(MapEntryTy *KeyValue)
insert - Insert the specified key/value pair into the map.
Definition: StringMap.h:308
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
#define UINT64_MAX
Definition: DataTypes.h:77
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ SingleThread
Synchronized with respect to signal handlers executing in the same thread.
Definition: LLVMContext.h:54
@ System
Synchronized with respect to all concurrently executing threads.
Definition: LLVMContext.h:57
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
DiagnosticSeverity
Defines the different supported severity of a diagnostic.
@ DS_Remark
@ DS_Warning
@ DS_Error
This is the base class for diagnostic handling in LLVM.
void(*)(const DiagnosticInfo *DI, void *Context) DiagnosticHandlerTy