| 1 | //===----------------------------------------------------------------------===// |
| 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 | #ifndef LLVM_LIB_DWARFLINKER_PARALLEL_MODULEPOOL_H |
| 10 | #define LLVM_LIB_DWARFLINKER_PARALLEL_MODULEPOOL_H |
| 11 | |
| 12 | #include "TypePool.h" |
| 13 | #include "llvm/ADT/StringMap.h" |
| 14 | #include <limits> |
| 15 | #include <mutex> |
| 16 | |
| 17 | namespace llvm { |
| 18 | namespace dwarf_linker { |
| 19 | namespace parallel { |
| 20 | |
| 21 | struct SectionDescriptor; |
| 22 | |
| 23 | /// Where the DW_TAG_module DIE describing a clang module ended up in the |
| 24 | /// output. Clang imposes a one-definition rule (ODR) on module names regardless |
| 25 | /// of the source language, so a module is described once and every importer |
| 26 | /// resolves to that description. |
| 27 | /// |
| 28 | /// A module DIE lands either in the artificial type unit, or in the plain DWARF |
| 29 | /// of the unit which emitted it. |
| 30 | struct ModuleAnchor { |
| 31 | bool isSet() const { return TypeName != nullptr || Section != nullptr; } |
| 32 | |
| 33 | TypeEntry *TypeName = nullptr; |
| 34 | SectionDescriptor *Section = nullptr; |
| 35 | uint64_t LocalOffset = 0; |
| 36 | |
| 37 | /// Priority of the unit which recorded this anchor. |
| 38 | uint64_t Priority = std::numeric_limits<uint64_t>::max(); |
| 39 | }; |
| 40 | |
| 41 | class ModulePool { |
| 42 | public: |
| 43 | ModuleAnchor *getOrCreate(StringRef Path) { |
| 44 | std::lock_guard<std::mutex> Guard(Mutex); |
| 45 | |
| 46 | // The underlying StringMap guarantees the pointer remains stable. The |
| 47 | // pointee is written while cloning, so it may only be read afterwards. |
| 48 | return &Anchors[Path]; |
| 49 | } |
| 50 | |
| 51 | void set(StringRef Path, const ModuleAnchor &Location) { |
| 52 | std::lock_guard<std::mutex> Guard(Mutex); |
| 53 | |
| 54 | // Anchors are keyed by module name while a unit is created per .pcm, so a |
| 55 | // module built more than once has a unit for each copy. The lowest priority |
| 56 | // wins, as in the type pool, guaranteeing determinism. |
| 57 | ModuleAnchor &Anchor = Anchors[Path]; |
| 58 | if (!Anchor.isSet() || Location.Priority < Anchor.Priority) |
| 59 | Anchor = Location; |
| 60 | } |
| 61 | |
| 62 | private: |
| 63 | /// Unlike the type pool this is not a per-DIE structure. It is accessed once |
| 64 | /// for each DW_TAG_module cloned and once for each DW_AT_import cloned. |
| 65 | std::mutex Mutex; |
| 66 | StringMap<ModuleAnchor> Anchors; |
| 67 | }; |
| 68 | |
| 69 | } // end of namespace parallel |
| 70 | } // end of namespace dwarf_linker |
| 71 | } // end of namespace llvm |
| 72 | |
| 73 | #endif // LLVM_LIB_DWARFLINKER_PARALLEL_MODULEPOOL_H |
| 74 | |