Files
swift-mirror/include/swift/Serialization/SerializedSILLoader.h
Doug Gregor f267f62f65 [SILGen] Consistently use SIL asmname for foreign function/variable references
Whenever we have a reference to a foreign function/variable in SIL, use
a mangled name at the SIL level with the C name in the asmname
attribute. The expands the use of asmname to three kinds of cases that
it hadn't been used in yet:

* Declarations imported from C headers/modules
* @_cdecl @implementation of C headers/modules
* @_cdecl functions in general

Some code within the SIL pipeline makes assumptions that the C names of
various runtime functions are reflected at the SIL level. For example,
the linking of Embedded Swift runtime functions is done by-name, and
some of those names refer to C functions (like `swift_retain`) and
others refer to Swift functions that use `@_silgen_name` (like
`swift_getDefaultExecutor`). Extend the serialized module format to
include a table that maps from the asmname of functions/variables over
to their mangled names, so we can look up functions by asmname if we
want. These tables could also be used for checking for declarations
that conflict on their asmname in the future. Right now, we leave it
up to LLVM or the linker to do the checking.

`@_silgen_name` is not affected by these changes, nor should it be:
that hidden feature is specifically meant to affect the name at the
SIL level.

The vast majority of test changes are SIL tests where we had expected
to see the C/C++/Objective-C names in the tests for references to
foreign entities, and now we see Swift mangled names (ending in To).
The SIL declarations themselves will have a corresponding asmname.

Notably, the IRGen tests have *not* changed, because we generally the
same IR as before. It's only the modeling at the SIL lever that has
changed.

Another part of rdar://137014448.
2025-10-29 19:35:55 -07:00

131 lines
4.6 KiB
C++

//===--- SerializedSILLoader.h - Handle SIL section in modules --*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#ifndef SWIFT_SERIALIZATION_SILLOADER_H
#define SWIFT_SERIALIZATION_SILLOADER_H
#include "swift/AST/AutoDiff.h"
#include "swift/AST/Decl.h"
#include "swift/AST/Identifier.h"
#include "swift/SIL/Notifications.h"
#include "swift/SIL/SILDeclRef.h"
#include "swift/SIL/SILLinkage.h"
#include "swift/SIL/SILMoveOnlyDeinit.h"
#include <memory>
#include <vector>
namespace swift {
class ASTContext;
class FileUnit;
class ModuleDecl;
class SILDeserializer;
class SILFunction;
class SILGlobalVariable;
class SILProperty;
class SILModule;
class SILVTable;
class SILWitnessTable;
class SILDefaultWitnessTable;
class SILDifferentiabilityWitness;
/// Maintains a list of SILDeserializer, one for each serialized modules
/// in ASTContext. It provides lookupSILFunction that will perform lookup
/// on each SILDeserializer.
class SerializedSILLoader {
private:
ASTContext &Context;
std::vector<std::unique_ptr<SILDeserializer>> LoadedSILSections;
explicit SerializedSILLoader(
ASTContext &ctx, SILModule *SILMod,
DeserializationNotificationHandlerSet *callbacks);
public:
/// Create a new loader.
///
/// \param callbacks - not owned by the loader
static std::unique_ptr<SerializedSILLoader>
create(ASTContext &ctx, SILModule *SILMod,
DeserializationNotificationHandlerSet *callbacks) {
return std::unique_ptr<SerializedSILLoader>(
new SerializedSILLoader(ctx, SILMod, callbacks));
}
~SerializedSILLoader();
SILFunction *lookupSILFunction(SILFunction *Callee, bool onlyUpdateLinkage);
SILFunction *lookupSILFunction(StringRef Name,
std::optional<SILLinkage> linkage,
bool byAsmName = false);
SILGlobalVariable *lookupSILGlobalVariable(StringRef Name,
bool byAsmName = false);
bool hasSILFunction(StringRef Name,
std::optional<SILLinkage> linkage = std::nullopt);
SILVTable *lookupVTable(const ClassDecl *C);
SILMoveOnlyDeinit *lookupMoveOnlyDeinit(const NominalTypeDecl *nomDecl);
SILWitnessTable *lookupWitnessTable(SILWitnessTable *C);
SILDefaultWitnessTable *lookupDefaultWitnessTable(SILDefaultWitnessTable *C);
SILDefaultOverrideTable *
lookupDefaultOverrideTable(SILDefaultOverrideTable *);
SILDifferentiabilityWitness *
lookupDifferentiabilityWitness(SILDifferentiabilityWitnessKey key);
/// Invalidate the cached entries for deserialized state. Must be
/// called when erasing deserialized state in the SILModule.
void invalidateAllCaches();
bool invalidateFunction(SILFunction *f);
bool invalidateGlobalVariable(SILGlobalVariable *gv);
bool invalidateVTable(SILVTable *vt);
bool invalidateWitnessTable(SILWitnessTable *wt);
bool invalidateDefaultWitnessTable(SILDefaultWitnessTable *wt);
bool invalidateProperty(SILProperty *p);
bool invalidateDifferentiabilityWitness(SILDifferentiabilityWitness *w);
/// Deserialize all SILFunctions, VTables, and WitnessTables for
/// a given Module.
///
/// If PrimaryFile is nullptr, all definitions are brought in with
/// definition linkage.
///
/// Otherwise, definitions not in the primary file are brought in
/// with external linkage.
void getAllForModule(Identifier Mod, FileUnit *PrimaryFile);
/// Deserialize all SILFunctions in all SILModules.
void getAllSILFunctions();
/// Deserialize all VTables in all SILModules.
void getAllVTables();
/// Deserialize all WitnessTables in all SILModules.
void getAllWitnessTables();
/// Deserialize all DefaultWitnessTables in all SILModules.
void getAllDefaultWitnessTables();
/// Deserialize all DefaultOverrideTables in all SILModules.
void getAllDefaultOverrideTables();
/// Deserialize all Properties in all SILModules.
void getAllProperties();
/// Deserialize all DifferentiabilityWitnesses in all SILModules.
void getAllDifferentiabilityWitnesses();
SerializedSILLoader(const SerializedSILLoader &) = delete;
SerializedSILLoader(SerializedSILLoader &&) = delete;
SerializedSILLoader &operator=(const SerializedSILLoader &) = delete;
SerializedSILLoader &operator=(SerializedSILLoader &&) = delete;
};
} // end namespace swift
#endif