Eugene Zelenko | de6cce2 | 2017-06-19 22:05:08 +0000 | [diff] [blame] | 1 | //===- LLVMContextImpl.cpp - Implement LLVMContextImpl --------------------===// |
Jeffrey Yasskin | 4cfb3a7 | 2010-03-21 21:17:34 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Jeffrey Yasskin | 4cfb3a7 | 2010-03-21 21:17:34 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file implements the opaque LLVMContextImpl. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "LLVMContextImpl.h" |
serge-sans-paille | e188aae | 2022-01-31 22:35:07 +0100 | [diff] [blame] | 14 | #include "AttributeImpl.h" |
Benjamin Kramer | 81f385b | 2020-01-21 15:54:48 +0100 | [diff] [blame] | 15 | #include "llvm/ADT/SetVector.h" |
serge-sans-paille | e188aae | 2022-01-31 22:35:07 +0100 | [diff] [blame] | 16 | #include "llvm/ADT/StringMapEntry.h" |
| 17 | #include "llvm/ADT/iterator.h" |
serge-sans-paille | e188aae | 2022-01-31 22:35:07 +0100 | [diff] [blame] | 18 | #include "llvm/IR/DiagnosticHandler.h" |
| 19 | #include "llvm/IR/LLVMRemarkStreamer.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 20 | #include "llvm/IR/Module.h" |
Andrew Kaylor | aa641a5 | 2016-04-22 22:06:11 +0000 | [diff] [blame] | 21 | #include "llvm/IR/OptBisect.h" |
Eugene Zelenko | de6cce2 | 2017-06-19 22:05:08 +0000 | [diff] [blame] | 22 | #include "llvm/IR/Type.h" |
serge-sans-paille | e188aae | 2022-01-31 22:35:07 +0100 | [diff] [blame] | 23 | #include "llvm/IR/Use.h" |
| 24 | #include "llvm/IR/User.h" |
| 25 | #include "llvm/Remarks/RemarkStreamer.h" |
serge-sans-paille | e188aae | 2022-01-31 22:35:07 +0100 | [diff] [blame] | 26 | #include "llvm/Support/Compiler.h" |
| 27 | #include "llvm/Support/ErrorHandling.h" |
Eugene Zelenko | de6cce2 | 2017-06-19 22:05:08 +0000 | [diff] [blame] | 28 | #include <cassert> |
| 29 | #include <utility> |
| 30 | |
Dan Gohman | b29cda9 | 2010-04-15 17:08:50 +0000 | [diff] [blame] | 31 | using namespace llvm; |
Jeffrey Yasskin | 4cfb3a7 | 2010-03-21 21:17:34 +0000 | [diff] [blame] | 32 | |
| 33 | LLVMContextImpl::LLVMContextImpl(LLVMContext &C) |
Arthur Eubanks | 4c8174f | 2021-06-24 08:21:24 -0700 | [diff] [blame] | 34 | : DiagHandler(std::make_unique<DiagnosticHandler>()), |
| 35 | VoidTy(C, Type::VoidTyID), LabelTy(C, Type::LabelTyID), |
| 36 | HalfTy(C, Type::HalfTyID), BFloatTy(C, Type::BFloatTyID), |
| 37 | FloatTy(C, Type::FloatTyID), DoubleTy(C, Type::DoubleTyID), |
| 38 | MetadataTy(C, Type::MetadataTyID), TokenTy(C, Type::TokenTyID), |
| 39 | X86_FP80Ty(C, Type::X86_FP80TyID), FP128Ty(C, Type::FP128TyID), |
James Y Knight | dfeb399 | 2024-07-25 09:19:22 -0400 | [diff] [blame] | 40 | PPC_FP128Ty(C, Type::PPC_FP128TyID), X86_AMXTy(C, Type::X86_AMXTyID), |
| 41 | Int1Ty(C, 1), Int8Ty(C, 8), Int16Ty(C, 16), Int32Ty(C, 32), |
| 42 | Int64Ty(C, 64), Int128Ty(C, 128) {} |
Jeffrey Yasskin | 4cfb3a7 | 2010-03-21 21:17:34 +0000 | [diff] [blame] | 43 | |
| 44 | LLVMContextImpl::~LLVMContextImpl() { |
Jeremy Morse | f1b0a54 | 2023-11-08 14:58:34 +0000 | [diff] [blame] | 45 | #ifndef NDEBUG |
| 46 | // Check that any variable location records that fell off the end of a block |
| 47 | // when it's terminator was removed were eventually replaced. This assertion |
Stephen Tozer | ffd08c7 | 2024-03-19 20:07:07 +0000 | [diff] [blame] | 48 | // firing indicates that DbgVariableRecords went missing during the lifetime |
| 49 | // of the LLVMContext. |
Stephen Tozer | 360da83 | 2024-03-13 16:39:35 +0000 | [diff] [blame] | 50 | assert(TrailingDbgRecords.empty() && "DbgRecords in blocks not cleaned"); |
Jeremy Morse | f1b0a54 | 2023-11-08 14:58:34 +0000 | [diff] [blame] | 51 | #endif |
| 52 | |
David Blaikie | 4c82a80 | 2014-04-21 21:27:19 +0000 | [diff] [blame] | 53 | // NOTE: We need to delete the contents of OwnedModules, but Module's dtor |
| 54 | // will call LLVMContextImpl::removeModule, thus invalidating iterators into |
| 55 | // the container. Avoid iterators during this operation: |
| 56 | while (!OwnedModules.empty()) |
| 57 | delete *OwnedModules.begin(); |
Duncan P. N. Exon Smith | e16d587 | 2015-01-14 21:58:17 +0000 | [diff] [blame] | 58 | |
Vedant Kumar | 69ee62c | 2018-06-29 20:13:13 +0000 | [diff] [blame] | 59 | #ifndef NDEBUG |
Serge Pavlov | 7975b8c | 2019-09-14 00:21:24 +0700 | [diff] [blame] | 60 | // Check for metadata references from leaked Values. |
| 61 | for (auto &Pair : ValueMetadata) |
Vedant Kumar | 69ee62c | 2018-06-29 20:13:13 +0000 | [diff] [blame] | 62 | Pair.first->dump(); |
Serge Pavlov | 7975b8c | 2019-09-14 00:21:24 +0700 | [diff] [blame] | 63 | assert(ValueMetadata.empty() && "Values with metadata have been leaked"); |
Vedant Kumar | 69ee62c | 2018-06-29 20:13:13 +0000 | [diff] [blame] | 64 | #endif |
| 65 | |
Duncan P. N. Exon Smith | e16d587 | 2015-01-14 21:58:17 +0000 | [diff] [blame] | 66 | // Drop references for MDNodes. Do this before Values get deleted to avoid |
| 67 | // unnecessary RAUW when nodes are still unresolved. |
Stephen Tozer | f99a020 | 2023-11-17 17:52:24 +0000 | [diff] [blame] | 68 | for (auto *I : DistinctMDNodes) |
Duncan P. N. Exon Smith | e16d587 | 2015-01-14 21:58:17 +0000 | [diff] [blame] | 69 | I->dropAllReferences(); |
Duncan P. N. Exon Smith | 55ca964 | 2015-08-03 17:26:41 +0000 | [diff] [blame] | 70 | #define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \ |
Duncan P. N. Exon Smith | 408f5a2 | 2015-01-20 01:18:32 +0000 | [diff] [blame] | 71 | for (auto *I : CLASS##s) \ |
Duncan P. N. Exon Smith | e16d587 | 2015-01-14 21:58:17 +0000 | [diff] [blame] | 72 | I->dropAllReferences(); |
Duncan P. N. Exon Smith | 408f5a2 | 2015-01-20 01:18:32 +0000 | [diff] [blame] | 73 | #include "llvm/IR/Metadata.def" |
Duncan P. N. Exon Smith | e16d587 | 2015-01-14 21:58:17 +0000 | [diff] [blame] | 74 | |
| 75 | // Also drop references that come from the Value bridges. |
| 76 | for (auto &Pair : ValuesAsMetadata) |
| 77 | Pair.second->dropUsers(); |
| 78 | for (auto &Pair : MetadataAsValues) |
| 79 | Pair.second->dropUse(); |
Stephen Tozer | f99a020 | 2023-11-17 17:52:24 +0000 | [diff] [blame] | 80 | // Do not untrack ValueAsMetadata references for DIArgLists, as they have |
| 81 | // already been more efficiently untracked above. |
| 82 | for (DIArgList *AL : DIArgLists) { |
| 83 | AL->dropAllReferences(/* Untrack */ false); |
| 84 | delete AL; |
| 85 | } |
| 86 | DIArgLists.clear(); |
Duncan P. N. Exon Smith | e16d587 | 2015-01-14 21:58:17 +0000 | [diff] [blame] | 87 | |
| 88 | // Destroy MDNodes. |
Duncan P. N. Exon Smith | 2bc00f4 | 2015-01-19 23:13:14 +0000 | [diff] [blame] | 89 | for (MDNode *I : DistinctMDNodes) |
Duncan P. N. Exon Smith | e16d587 | 2015-01-14 21:58:17 +0000 | [diff] [blame] | 90 | I->deleteAsSubclass(); |
Haopeng Liu | 5ece35d | 2024-06-21 12:09:00 -0700 | [diff] [blame] | 91 | |
| 92 | for (auto *ConstantRangeListAttribute : ConstantRangeListAttributes) |
| 93 | ConstantRangeListAttribute->~ConstantRangeListAttributeImpl(); |
Duncan P. N. Exon Smith | 55ca964 | 2015-08-03 17:26:41 +0000 | [diff] [blame] | 94 | #define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \ |
| 95 | for (CLASS * I : CLASS##s) \ |
Duncan P. N. Exon Smith | e16d587 | 2015-01-14 21:58:17 +0000 | [diff] [blame] | 96 | delete I; |
Duncan P. N. Exon Smith | 408f5a2 | 2015-01-20 01:18:32 +0000 | [diff] [blame] | 97 | #include "llvm/IR/Metadata.def" |
Duncan P. N. Exon Smith | e16d587 | 2015-01-14 21:58:17 +0000 | [diff] [blame] | 98 | |
Benjamin Kramer | cb36bec | 2015-01-22 21:43:01 +0000 | [diff] [blame] | 99 | // Free the constants. |
Duncan P. N. Exon Smith | ef06d44 | 2016-04-06 17:56:08 +0000 | [diff] [blame] | 100 | for (auto *I : ExprConstants) |
| 101 | I->dropAllReferences(); |
| 102 | for (auto *I : ArrayConstants) |
| 103 | I->dropAllReferences(); |
| 104 | for (auto *I : StructConstants) |
| 105 | I->dropAllReferences(); |
| 106 | for (auto *I : VectorConstants) |
| 107 | I->dropAllReferences(); |
Jeffrey Yasskin | 4cfb3a7 | 2010-03-21 21:17:34 +0000 | [diff] [blame] | 108 | ExprConstants.freeConstants(); |
| 109 | ArrayConstants.freeConstants(); |
| 110 | StructConstants.freeConstants(); |
| 111 | VectorConstants.freeConstants(); |
Jeffrey Yasskin | 4cfb3a7 | 2010-03-21 21:17:34 +0000 | [diff] [blame] | 112 | InlineAsms.freeConstants(); |
Justin Lebar | 611c5c2 | 2016-10-10 16:26:13 +0000 | [diff] [blame] | 113 | |
| 114 | CAZConstants.clear(); |
| 115 | CPNConstants.clear(); |
Joshua Cranmer | e6b0221 | 2022-12-20 11:02:11 -0500 | [diff] [blame] | 116 | CTNConstants.clear(); |
Justin Lebar | 611c5c2 | 2016-10-10 16:26:13 +0000 | [diff] [blame] | 117 | UVConstants.clear(); |
Zhengyang Liu | 345fccc | 2020-11-26 00:10:36 -0700 | [diff] [blame] | 118 | PVConstants.clear(); |
Arthur Eubanks | c316675 | 2023-02-23 21:16:27 -0800 | [diff] [blame] | 119 | IntZeroConstants.clear(); |
Arthur Eubanks | 86bdcdf | 2023-02-23 21:47:03 -0800 | [diff] [blame] | 120 | IntOneConstants.clear(); |
Justin Lebar | 611c5c2 | 2016-10-10 16:26:13 +0000 | [diff] [blame] | 121 | IntConstants.clear(); |
Paul Walker | cbb24e1 | 2024-02-22 14:07:16 +0000 | [diff] [blame] | 122 | IntSplatConstants.clear(); |
Justin Lebar | 611c5c2 | 2016-10-10 16:26:13 +0000 | [diff] [blame] | 123 | FPConstants.clear(); |
Paul Walker | cbb24e1 | 2024-02-22 14:07:16 +0000 | [diff] [blame] | 124 | FPSplatConstants.clear(); |
Chris Lattner | 3756b91 | 2012-01-23 22:57:10 +0000 | [diff] [blame] | 125 | CDSConstants.clear(); |
Bill Wendling | e38b804 | 2012-09-26 21:07:29 +0000 | [diff] [blame] | 126 | |
Bill Wendling | 164a4fb | 2013-01-24 00:14:46 +0000 | [diff] [blame] | 127 | // Destroy attribute node lists. |
| 128 | for (FoldingSetIterator<AttributeSetNode> I = AttrsSetNodes.begin(), |
| 129 | E = AttrsSetNodes.end(); I != E; ) { |
| 130 | FoldingSetIterator<AttributeSetNode> Elem = I++; |
| 131 | delete &*Elem; |
| 132 | } |
| 133 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 134 | // Destroy MetadataAsValues. |
| 135 | { |
| 136 | SmallVector<MetadataAsValue *, 8> MDVs; |
| 137 | MDVs.reserve(MetadataAsValues.size()); |
| 138 | for (auto &Pair : MetadataAsValues) |
| 139 | MDVs.push_back(Pair.second); |
| 140 | MetadataAsValues.clear(); |
| 141 | for (auto *V : MDVs) |
| 142 | delete V; |
| 143 | } |
| 144 | |
| 145 | // Destroy ValuesAsMetadata. |
| 146 | for (auto &Pair : ValuesAsMetadata) |
| 147 | delete Pair.second; |
Jeffrey Yasskin | 4cfb3a7 | 2010-03-21 21:17:34 +0000 | [diff] [blame] | 148 | } |
David Blaikie | a379b181 | 2011-12-20 02:50:00 +0000 | [diff] [blame] | 149 | |
Duncan P. N. Exon Smith | 93e983e | 2015-01-19 22:53:18 +0000 | [diff] [blame] | 150 | namespace llvm { |
Eugene Zelenko | de6cce2 | 2017-06-19 22:05:08 +0000 | [diff] [blame] | 151 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 152 | /// Make MDOperand transparent for hashing. |
Duncan P. N. Exon Smith | 93e983e | 2015-01-19 22:53:18 +0000 | [diff] [blame] | 153 | /// |
| 154 | /// This overload of an implementation detail of the hashing library makes |
| 155 | /// MDOperand hash to the same value as a \a Metadata pointer. |
| 156 | /// |
| 157 | /// Note that overloading \a hash_value() as follows: |
| 158 | /// |
| 159 | /// \code |
| 160 | /// size_t hash_value(const MDOperand &X) { return hash_value(X.get()); } |
| 161 | /// \endcode |
| 162 | /// |
| 163 | /// does not cause MDOperand to be transparent. In particular, a bare pointer |
| 164 | /// doesn't get hashed before it's combined, whereas \a MDOperand would. |
| 165 | static const Metadata *get_hashable_data(const MDOperand &X) { return X.get(); } |
Eugene Zelenko | de6cce2 | 2017-06-19 22:05:08 +0000 | [diff] [blame] | 166 | |
| 167 | } // end namespace llvm |
Duncan P. N. Exon Smith | 93e983e | 2015-01-19 22:53:18 +0000 | [diff] [blame] | 168 | |
Duncan P. N. Exon Smith | fed199a | 2015-01-20 00:01:43 +0000 | [diff] [blame] | 169 | unsigned MDNodeOpsKey::calculateHash(MDNode *N, unsigned Offset) { |
| 170 | unsigned Hash = hash_combine_range(N->op_begin() + Offset, N->op_end()); |
Duncan P. N. Exon Smith | 93e983e | 2015-01-19 22:53:18 +0000 | [diff] [blame] | 171 | #ifndef NDEBUG |
| 172 | { |
Kazu Hirata | 19aacdb | 2021-01-16 09:40:53 -0800 | [diff] [blame] | 173 | SmallVector<Metadata *, 8> MDs(drop_begin(N->operands(), Offset)); |
Duncan P. N. Exon Smith | 93e983e | 2015-01-19 22:53:18 +0000 | [diff] [blame] | 174 | unsigned RawHash = calculateHash(MDs); |
| 175 | assert(Hash == RawHash && |
| 176 | "Expected hash of MDOperand to equal hash of Metadata*"); |
| 177 | } |
| 178 | #endif |
| 179 | return Hash; |
| 180 | } |
| 181 | |
| 182 | unsigned MDNodeOpsKey::calculateHash(ArrayRef<Metadata *> Ops) { |
Kazu Hirata | b01e25d | 2025-04-20 16:36:03 -0700 | [diff] [blame] | 183 | return hash_combine_range(Ops); |
Duncan P. N. Exon Smith | 93e983e | 2015-01-19 22:53:18 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Sanjoy Das | 9303c24 | 2015-09-24 19:14:18 +0000 | [diff] [blame] | 186 | StringMapEntry<uint32_t> *LLVMContextImpl::getOrInsertBundleTag(StringRef Tag) { |
| 187 | uint32_t NewIdx = BundleTagCache.size(); |
| 188 | return &*(BundleTagCache.insert(std::make_pair(Tag, NewIdx)).first); |
| 189 | } |
| 190 | |
| 191 | void LLVMContextImpl::getOperandBundleTags(SmallVectorImpl<StringRef> &Tags) const { |
| 192 | Tags.resize(BundleTagCache.size()); |
| 193 | for (const auto &T : BundleTagCache) |
| 194 | Tags[T.second] = T.first(); |
| 195 | } |
| 196 | |
| 197 | uint32_t LLVMContextImpl::getOperandBundleTagID(StringRef Tag) const { |
| 198 | auto I = BundleTagCache.find(Tag); |
| 199 | assert(I != BundleTagCache.end() && "Unknown tag!"); |
| 200 | return I->second; |
| 201 | } |
| 202 | |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 203 | SyncScope::ID LLVMContextImpl::getOrInsertSyncScopeID(StringRef SSN) { |
| 204 | auto NewSSID = SSC.size(); |
| 205 | assert(NewSSID < std::numeric_limits<SyncScope::ID>::max() && |
| 206 | "Hit the maximum number of synchronization scopes allowed!"); |
| 207 | return SSC.insert(std::make_pair(SSN, SyncScope::ID(NewSSID))).first->second; |
| 208 | } |
| 209 | |
| 210 | void LLVMContextImpl::getSyncScopeNames( |
| 211 | SmallVectorImpl<StringRef> &SSNs) const { |
| 212 | SSNs.resize(SSC.size()); |
| 213 | for (const auto &SSE : SSC) |
| 214 | SSNs[SSE.second] = SSE.first(); |
| 215 | } |
| 216 | |
gonzalobg | 0f52193 | 2024-09-25 20:13:56 +0200 | [diff] [blame] | 217 | std::optional<StringRef> |
| 218 | LLVMContextImpl::getSyncScopeName(SyncScope::ID Id) const { |
| 219 | for (const auto &SSE : SSC) { |
| 220 | if (SSE.second != Id) |
| 221 | continue; |
| 222 | return SSE.first(); |
| 223 | } |
| 224 | return std::nullopt; |
| 225 | } |
| 226 | |
Samuel Eubanks | 47dbee6 | 2020-12-20 13:47:46 -0800 | [diff] [blame] | 227 | /// Gets the OptPassGate for this LLVMContextImpl, which defaults to the |
| 228 | /// singleton OptBisect if not explicitly set. |
Fedor Sergeev | d29884c | 2018-04-05 10:29:37 +0000 | [diff] [blame] | 229 | OptPassGate &LLVMContextImpl::getOptPassGate() const { |
| 230 | if (!OPG) |
Evgeniy Brevnov | 721f975 | 2022-11-01 13:42:07 +0700 | [diff] [blame] | 231 | OPG = &getGlobalPassGate(); |
Fedor Sergeev | d29884c | 2018-04-05 10:29:37 +0000 | [diff] [blame] | 232 | return *OPG; |
| 233 | } |
| 234 | |
| 235 | void LLVMContextImpl::setOptPassGate(OptPassGate& OPG) { |
| 236 | this->OPG = &OPG; |
Andrew Kaylor | aa641a5 | 2016-04-22 22:06:11 +0000 | [diff] [blame] | 237 | } |