|
| 1 | +//===--- AlwaysEmitConformanceMetadataPreservation.cpp -------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | +/// |
| 13 | +/// Some frameworks may rely on conformances to protocols they provide |
| 14 | +/// to be present in binary product they are compiled into even if |
| 15 | +/// such conformances are not otherwise referenced in user code. |
| 16 | +/// Such conformances may then, for example, be queried and used by the |
| 17 | +/// runtime. |
| 18 | +/// |
| 19 | +/// The developer may not ever explicitly reference or instantiate this type in |
| 20 | +/// their code, as it is effectively defining an XPC endpoint. However, when |
| 21 | +/// optimizations are enabled, the type may be stripped from the binary as it is |
| 22 | +/// never referenced. `@_alwaysEmitConformanceMetadata` can be used to mark |
| 23 | +/// a protocol to ensure that its conformances are always marked as externally |
| 24 | +/// visible even if not `public` to ensure they do not get optimized away. |
| 25 | +/// This mandatory pass makes it so. |
| 26 | +/// |
| 27 | +//===----------------------------------------------------------------------===// |
| 28 | + |
| 29 | +#include "swift/AST/Attr.h" |
| 30 | +#define DEBUG_TYPE "always-emit-conformance-metadata-preservation" |
| 31 | +#include "swift/AST/ASTWalker.h" |
| 32 | +#include "swift/AST/NameLookup.h" |
| 33 | +#include "swift/AST/ProtocolConformance.h" |
| 34 | +#include "swift/AST/SourceFile.h" |
| 35 | +#include "swift/SIL/SILModule.h" |
| 36 | +#include "swift/SILOptimizer/PassManager/Passes.h" |
| 37 | +#include "swift/SILOptimizer/PassManager/Transforms.h" |
| 38 | + |
| 39 | +using namespace swift; |
| 40 | + |
| 41 | +namespace { |
| 42 | + |
| 43 | +/// A helper class to collect all nominal type declarations that conform to |
| 44 | +/// `@_alwaysEmitConformanceMetadata` protocols. |
| 45 | +class AlwaysEmitMetadataConformanceCollector : public ASTWalker { |
| 46 | + std::vector<NominalTypeDecl *> &AlwaysEmitMetadataConformanceDecls; |
| 47 | + |
| 48 | +public: |
| 49 | + AlwaysEmitMetadataConformanceCollector( |
| 50 | + std::vector<NominalTypeDecl *> &AlwaysEmitMetadataConformanceDecls) |
| 51 | + : AlwaysEmitMetadataConformanceDecls(AlwaysEmitMetadataConformanceDecls) { |
| 52 | + } |
| 53 | + |
| 54 | + PreWalkAction walkToDeclPre(Decl *D) override { |
| 55 | + auto hasAlwaysEmitMetadataConformance = |
| 56 | + [&](llvm::PointerUnion<const TypeDecl *, const ExtensionDecl *> Decl) { |
| 57 | + bool anyObject = false; |
| 58 | + InvertibleProtocolSet Inverses; |
| 59 | + for (const auto &found : |
| 60 | + getDirectlyInheritedNominalTypeDecls(Decl, Inverses, anyObject)) |
| 61 | + if (auto Protocol = dyn_cast<ProtocolDecl>(found.Item)) |
| 62 | + if (Protocol->getAttrs() |
| 63 | + .hasAttribute<AlwaysEmitConformanceMetadataAttr>()) |
| 64 | + return true; |
| 65 | + return false; |
| 66 | + }; |
| 67 | + |
| 68 | + if (auto *NTD = dyn_cast<NominalTypeDecl>(D)) { |
| 69 | + if (hasAlwaysEmitMetadataConformance(NTD)) |
| 70 | + AlwaysEmitMetadataConformanceDecls.push_back(NTD); |
| 71 | + } else if (auto *ETD = dyn_cast<ExtensionDecl>(D)) { |
| 72 | + if (hasAlwaysEmitMetadataConformance(ETD)) |
| 73 | + AlwaysEmitMetadataConformanceDecls.push_back(ETD->getExtendedNominal()); |
| 74 | + } |
| 75 | + |
| 76 | + return Action::Continue(); |
| 77 | + } |
| 78 | +}; |
| 79 | + |
| 80 | +class AlwaysEmitConformanceMetadataPreservation : public SILModuleTransform { |
| 81 | + void run() override { |
| 82 | + auto &M = *getModule(); |
| 83 | + std::vector<NominalTypeDecl *> AlwaysEmitMetadataConformanceDecls; |
| 84 | + AlwaysEmitMetadataConformanceCollector Walker( |
| 85 | + AlwaysEmitMetadataConformanceDecls); |
| 86 | + |
| 87 | + SmallVector<Decl *> TopLevelDecls; |
| 88 | + if (M.getSwiftModule()->isMainModule()) { |
| 89 | + if (M.isWholeModule()) { |
| 90 | + for (const auto File : M.getSwiftModule()->getFiles()) |
| 91 | + File->getTopLevelDecls(TopLevelDecls); |
| 92 | + } else { |
| 93 | + for (const auto Primary : M.getSwiftModule()->getPrimarySourceFiles()) |
| 94 | + Primary->getTopLevelDecls(TopLevelDecls); |
| 95 | + } |
| 96 | + } |
| 97 | + for (auto *TLD : TopLevelDecls) |
| 98 | + TLD->walk(Walker); |
| 99 | + |
| 100 | + for (auto &NTD : AlwaysEmitMetadataConformanceDecls) |
| 101 | + M.addExternallyVisibleDecl(NTD); |
| 102 | + } |
| 103 | +}; |
| 104 | +} // end anonymous namespace |
| 105 | + |
| 106 | +SILTransform *swift::createAlwaysEmitConformanceMetadataPreservation() { |
| 107 | + return new AlwaysEmitConformanceMetadataPreservation(); |
| 108 | +} |
0 commit comments