1 //===-- LLVMContext.cpp - Implement LLVMContext ---------------------------===//
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 implements LLVMContext, as a wrapper around the opaque
10 // class LLVMContextImpl.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/IR/LLVMContext.h"
15 #include "LLVMContextImpl.h"
16 #include "llvm/ADT/SmallVector.h"
17 #include "llvm/ADT/StringMap.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/ADT/Twine.h"
20 #include "llvm/IR/DiagnosticInfo.h"
21 #include "llvm/IR/DiagnosticPrinter.h"
22 #include "llvm/IR/Metadata.h"
23 #include "llvm/IR/Module.h"
24 #include "llvm/Support/Casting.h"
25 #include "llvm/Support/ErrorHandling.h"
26 #include "llvm/Support/raw_ostream.h"
34 LLVMContext::LLVMContext() : pImpl(new LLVMContextImpl(*this)) {
35 // Create the fixed metadata kinds. This is done in the same order as the
36 // MD_* enum values so that they correspond.
37 std::pair
<unsigned, StringRef
> MDKinds
[] = {
41 {MD_fpmath
, "fpmath"},
43 {MD_tbaa_struct
, "tbaa.struct"},
44 {MD_invariant_load
, "invariant.load"},
45 {MD_alias_scope
, "alias.scope"},
46 {MD_noalias
, "noalias"},
47 {MD_nontemporal
, "nontemporal"},
48 {MD_mem_parallel_loop_access
, "llvm.mem.parallel_loop_access"},
49 {MD_nonnull
, "nonnull"},
50 {MD_dereferenceable
, "dereferenceable"},
51 {MD_dereferenceable_or_null
, "dereferenceable_or_null"},
52 {MD_make_implicit
, "make.implicit"},
53 {MD_unpredictable
, "unpredictable"},
54 {MD_invariant_group
, "invariant.group"},
56 {MD_loop
, "llvm.loop"},
58 {MD_section_prefix
, "section_prefix"},
59 {MD_absolute_symbol
, "absolute_symbol"},
60 {MD_associated
, "associated"},
61 {MD_callees
, "callees"},
62 {MD_irr_loop
, "irr_loop"},
63 {MD_access_group
, "llvm.access.group"},
64 {MD_callback
, "callback"},
67 for (auto &MDKind
: MDKinds
) {
68 unsigned ID
= getMDKindID(MDKind
.second
);
69 assert(ID
== MDKind
.first
&& "metadata kind id drifted");
73 auto *DeoptEntry
= pImpl
->getOrInsertBundleTag("deopt");
74 assert(DeoptEntry
->second
== LLVMContext::OB_deopt
&&
75 "deopt operand bundle id drifted!");
78 auto *FuncletEntry
= pImpl
->getOrInsertBundleTag("funclet");
79 assert(FuncletEntry
->second
== LLVMContext::OB_funclet
&&
80 "funclet operand bundle id drifted!");
83 auto *GCTransitionEntry
= pImpl
->getOrInsertBundleTag("gc-transition");
84 assert(GCTransitionEntry
->second
== LLVMContext::OB_gc_transition
&&
85 "gc-transition operand bundle id drifted!");
86 (void)GCTransitionEntry
;
88 SyncScope::ID SingleThreadSSID
=
89 pImpl
->getOrInsertSyncScopeID("singlethread");
90 assert(SingleThreadSSID
== SyncScope::SingleThread
&&
91 "singlethread synchronization scope ID drifted!");
92 (void)SingleThreadSSID
;
94 SyncScope::ID SystemSSID
=
95 pImpl
->getOrInsertSyncScopeID("");
96 assert(SystemSSID
== SyncScope::System
&&
97 "system synchronization scope ID drifted!");
101 LLVMContext::~LLVMContext() { delete pImpl
; }
103 void LLVMContext::addModule(Module
*M
) {
104 pImpl
->OwnedModules
.insert(M
);
107 void LLVMContext::removeModule(Module
*M
) {
108 pImpl
->OwnedModules
.erase(M
);
111 //===----------------------------------------------------------------------===//
112 // Recoverable Backend Errors
113 //===----------------------------------------------------------------------===//
116 setInlineAsmDiagnosticHandler(InlineAsmDiagHandlerTy DiagHandler
,
118 pImpl
->InlineAsmDiagHandler
= DiagHandler
;
119 pImpl
->InlineAsmDiagContext
= DiagContext
;
122 /// getInlineAsmDiagnosticHandler - Return the diagnostic handler set by
123 /// setInlineAsmDiagnosticHandler.
124 LLVMContext::InlineAsmDiagHandlerTy
125 LLVMContext::getInlineAsmDiagnosticHandler() const {
126 return pImpl
->InlineAsmDiagHandler
;
129 /// getInlineAsmDiagnosticContext - Return the diagnostic context set by
130 /// setInlineAsmDiagnosticHandler.
131 void *LLVMContext::getInlineAsmDiagnosticContext() const {
132 return pImpl
->InlineAsmDiagContext
;
135 void LLVMContext::setDiagnosticHandlerCallBack(
136 DiagnosticHandler::DiagnosticHandlerTy DiagnosticHandler
,
137 void *DiagnosticContext
, bool RespectFilters
) {
138 pImpl
->DiagHandler
->DiagHandlerCallback
= DiagnosticHandler
;
139 pImpl
->DiagHandler
->DiagnosticContext
= DiagnosticContext
;
140 pImpl
->RespectDiagnosticFilters
= RespectFilters
;
143 void LLVMContext::setDiagnosticHandler(std::unique_ptr
<DiagnosticHandler
> &&DH
,
144 bool RespectFilters
) {
145 pImpl
->DiagHandler
= std::move(DH
);
146 pImpl
->RespectDiagnosticFilters
= RespectFilters
;
149 void LLVMContext::setDiagnosticsHotnessRequested(bool Requested
) {
150 pImpl
->DiagnosticsHotnessRequested
= Requested
;
152 bool LLVMContext::getDiagnosticsHotnessRequested() const {
153 return pImpl
->DiagnosticsHotnessRequested
;
156 void LLVMContext::setDiagnosticsHotnessThreshold(uint64_t Threshold
) {
157 pImpl
->DiagnosticsHotnessThreshold
= Threshold
;
159 uint64_t LLVMContext::getDiagnosticsHotnessThreshold() const {
160 return pImpl
->DiagnosticsHotnessThreshold
;
163 yaml::Output
*LLVMContext::getDiagnosticsOutputFile() {
164 return pImpl
->DiagnosticsOutputFile
.get();
167 void LLVMContext::setDiagnosticsOutputFile(std::unique_ptr
<yaml::Output
> F
) {
168 pImpl
->DiagnosticsOutputFile
= std::move(F
);
171 DiagnosticHandler::DiagnosticHandlerTy
172 LLVMContext::getDiagnosticHandlerCallBack() const {
173 return pImpl
->DiagHandler
->DiagHandlerCallback
;
176 void *LLVMContext::getDiagnosticContext() const {
177 return pImpl
->DiagHandler
->DiagnosticContext
;
180 void LLVMContext::setYieldCallback(YieldCallbackTy Callback
, void *OpaqueHandle
)
182 pImpl
->YieldCallback
= Callback
;
183 pImpl
->YieldOpaqueHandle
= OpaqueHandle
;
186 void LLVMContext::yield() {
187 if (pImpl
->YieldCallback
)
188 pImpl
->YieldCallback(this, pImpl
->YieldOpaqueHandle
);
191 void LLVMContext::emitError(const Twine
&ErrorStr
) {
192 diagnose(DiagnosticInfoInlineAsm(ErrorStr
));
195 void LLVMContext::emitError(const Instruction
*I
, const Twine
&ErrorStr
) {
196 assert (I
&& "Invalid instruction");
197 diagnose(DiagnosticInfoInlineAsm(*I
, ErrorStr
));
200 static bool isDiagnosticEnabled(const DiagnosticInfo
&DI
) {
201 // Optimization remarks are selective. They need to check whether the regexp
202 // pattern, passed via one of the -pass-remarks* flags, matches the name of
203 // the pass that is emitting the diagnostic. If there is no match, ignore the
204 // diagnostic and return.
206 // Also noisy remarks are only enabled if we have hotness information to sort
208 if (auto *Remark
= dyn_cast
<DiagnosticInfoOptimizationBase
>(&DI
))
209 return Remark
->isEnabled() &&
210 (!Remark
->isVerbose() || Remark
->getHotness());
216 LLVMContext::getDiagnosticMessagePrefix(DiagnosticSeverity Severity
) {
227 llvm_unreachable("Unknown DiagnosticSeverity");
230 void LLVMContext::diagnose(const DiagnosticInfo
&DI
) {
231 if (auto *OptDiagBase
= dyn_cast
<DiagnosticInfoOptimizationBase
>(&DI
)) {
232 yaml::Output
*Out
= getDiagnosticsOutputFile();
234 // For remarks the << operator takes a reference to a pointer.
235 auto *P
= const_cast<DiagnosticInfoOptimizationBase
*>(OptDiagBase
);
239 // If there is a report handler, use it.
240 if (pImpl
->DiagHandler
&&
241 (!pImpl
->RespectDiagnosticFilters
|| isDiagnosticEnabled(DI
)) &&
242 pImpl
->DiagHandler
->handleDiagnostics(DI
))
245 if (!isDiagnosticEnabled(DI
))
248 // Otherwise, print the message with a prefix based on the severity.
249 DiagnosticPrinterRawOStream
DP(errs());
250 errs() << getDiagnosticMessagePrefix(DI
.getSeverity()) << ": ";
253 if (DI
.getSeverity() == DS_Error
)
257 void LLVMContext::emitError(unsigned LocCookie
, const Twine
&ErrorStr
) {
258 diagnose(DiagnosticInfoInlineAsm(LocCookie
, ErrorStr
));
261 //===----------------------------------------------------------------------===//
262 // Metadata Kind Uniquing
263 //===----------------------------------------------------------------------===//
265 /// Return a unique non-zero ID for the specified metadata kind.
266 unsigned LLVMContext::getMDKindID(StringRef Name
) const {
267 // If this is new, assign it its ID.
268 return pImpl
->CustomMDKindNames
.insert(
270 Name
, pImpl
->CustomMDKindNames
.size()))
274 /// getHandlerNames - Populate client-supplied smallvector using custom
275 /// metadata name and ID.
276 void LLVMContext::getMDKindNames(SmallVectorImpl
<StringRef
> &Names
) const {
277 Names
.resize(pImpl
->CustomMDKindNames
.size());
278 for (StringMap
<unsigned>::const_iterator I
= pImpl
->CustomMDKindNames
.begin(),
279 E
= pImpl
->CustomMDKindNames
.end(); I
!= E
; ++I
)
280 Names
[I
->second
] = I
->first();
283 void LLVMContext::getOperandBundleTags(SmallVectorImpl
<StringRef
> &Tags
) const {
284 pImpl
->getOperandBundleTags(Tags
);
287 uint32_t LLVMContext::getOperandBundleTagID(StringRef Tag
) const {
288 return pImpl
->getOperandBundleTagID(Tag
);
291 SyncScope::ID
LLVMContext::getOrInsertSyncScopeID(StringRef SSN
) {
292 return pImpl
->getOrInsertSyncScopeID(SSN
);
295 void LLVMContext::getSyncScopeNames(SmallVectorImpl
<StringRef
> &SSNs
) const {
296 pImpl
->getSyncScopeNames(SSNs
);
299 void LLVMContext::setGC(const Function
&Fn
, std::string GCName
) {
300 auto It
= pImpl
->GCNames
.find(&Fn
);
302 if (It
== pImpl
->GCNames
.end()) {
303 pImpl
->GCNames
.insert(std::make_pair(&Fn
, std::move(GCName
)));
306 It
->second
= std::move(GCName
);
309 const std::string
&LLVMContext::getGC(const Function
&Fn
) {
310 return pImpl
->GCNames
[&Fn
];
313 void LLVMContext::deleteGC(const Function
&Fn
) {
314 pImpl
->GCNames
.erase(&Fn
);
317 bool LLVMContext::shouldDiscardValueNames() const {
318 return pImpl
->DiscardValueNames
;
321 bool LLVMContext::isODRUniquingDebugTypes() const { return !!pImpl
->DITypeMap
; }
323 void LLVMContext::enableDebugTypeODRUniquing() {
324 if (pImpl
->DITypeMap
)
327 pImpl
->DITypeMap
.emplace();
330 void LLVMContext::disableDebugTypeODRUniquing() { pImpl
->DITypeMap
.reset(); }
332 void LLVMContext::setDiscardValueNames(bool Discard
) {
333 pImpl
->DiscardValueNames
= Discard
;
336 OptPassGate
&LLVMContext::getOptPassGate() const {
337 return pImpl
->getOptPassGate();
340 void LLVMContext::setOptPassGate(OptPassGate
& OPG
) {
341 pImpl
->setOptPassGate(OPG
);
344 const DiagnosticHandler
*LLVMContext::getDiagHandlerPtr() const {
345 return pImpl
->DiagHandler
.get();
348 std::unique_ptr
<DiagnosticHandler
> LLVMContext::getDiagnosticHandler() {
349 return std::move(pImpl
->DiagHandler
);