Skip to content

Clean up reflection metadata emission #16085

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/swift/Runtime/RuntimeFunctions.def
Original file line number Diff line number Diff line change
Expand Up @@ -827,7 +827,7 @@ FUNCTION(AllocateGenericValueMetadata, swift_allocateGenericValueMetadata,
ATTRS(NoUnwind))

// MetadataResponse swift_checkMetadataState(MetadataRequest request,
// cosnt Metadata *type);
// const Metadata *type);
FUNCTION(CheckMetadataState, swift_checkMetadataState, SwiftCC,
RETURNS(TypeMetadataResponseTy),
ARGS(SizeTy, TypeMetadataPtrTy),
Expand Down
6 changes: 3 additions & 3 deletions lib/IRGen/GenMeta.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -365,8 +365,8 @@ namespace {
}

void addExtendedContext() {
auto string = getTypeRef(IGM,
E->getSelfInterfaceType()->getCanonicalType());
auto string = IGM.getTypeRef(
E->getSelfInterfaceType()->getCanonicalType());
B.addRelativeAddress(string);
}

Expand Down Expand Up @@ -3841,7 +3841,7 @@ GenericRequirementsMetadata irgen::addGenericRequirements(

auto flags = GenericRequirementFlags(abiKind, false, false);
auto typeName =
getTypeRef(IGM, requirement.getSecondType()->getCanonicalType());
IGM.getTypeRef(requirement.getSecondType()->getCanonicalType());

addGenericRequirement(IGM, B, metadata, sig, flags,
requirement.getFirstType(),
Expand Down
63 changes: 21 additions & 42 deletions lib/IRGen/GenReflection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,12 @@ class PrintMetadataSource
}
};

llvm::Constant *IRGenModule::getTypeRef(CanType type) {
IRGenMangler Mangler;
auto SymbolicName = Mangler.mangleTypeForReflection(*this, type);
return getAddrOfStringForTypeRef(SymbolicName);
}

class ReflectionMetadataBuilder {
protected:
IRGenModule &IGM;
Expand Down Expand Up @@ -203,46 +209,23 @@ class ReflectionMetadataBuilder {

/// Add a 32-bit relative offset to a mangled typeref string
/// in the typeref reflection section.
void addTypeRef(ModuleDecl *ModuleContext, CanType type,
CanGenericSignature Context = {}) {
assert(type);

// Generic parameters should be written in terms of interface types
// for the purposes of reflection metadata
assert(!type->hasArchetype() && "Forgot to map typeref out of context");

// TODO: As a compatibility hack, mangle single-field boxes with the legacy
// mangling in reflection metadata.
bool isSingleFieldOfBox = false;
auto boxTy = dyn_cast<SILBoxType>(type);
if (boxTy && boxTy->getLayout()->getFields().size() == 1) {
GenericContextScope scope(IGM, Context);
type = boxTy->getFieldLoweredType(IGM.getSILModule(), 0);
isSingleFieldOfBox = true;
}
IRGenMangler mangler;
auto MangledStr = mangler.mangleTypeForReflection(IGM, type,
ModuleContext,
isSingleFieldOfBox);
auto mangledName = IGM.getAddrOfStringForTypeRef(MangledStr);
B.addRelativeAddress(mangledName);
void addTypeRef(CanType type) {
B.addRelativeAddress(IGM.getTypeRef(type));
}

/// Add a 32-bit relative offset to a mangled nominal type string
/// in the typeref reflection section.
void addNominalRef(const NominalTypeDecl *nominal) {
IRGenMangler mangler;
SymbolicMangling mangledStr;
if (auto proto = dyn_cast<ProtocolDecl>(nominal)) {
IRGenMangler mangler;
SymbolicMangling mangledStr;
mangledStr.String = mangler.mangleBareProtocol(proto);
auto mangledName = IGM.getAddrOfStringForTypeRef(mangledStr);
B.addRelativeAddress(mangledName);
} else {
CanType type = nominal->getDeclaredType()->getCanonicalType();
mangledStr =
mangler.mangleTypeForReflection(IGM, type, nominal->getModuleContext(),
/*isSingleFieldOfBox=*/false);
B.addRelativeAddress(IGM.getTypeRef(type));
}
auto mangledName = IGM.getAddrOfStringForTypeRef(mangledStr);
B.addRelativeAddress(mangledName);
}

llvm::GlobalVariable *emit(Optional<LinkEntity> entity,
Expand Down Expand Up @@ -296,9 +279,7 @@ class AssociatedTypeMetadataBuilder : public ReflectionMetadataBuilder {
PrettyStackTraceDecl DebugStack("emitting associated type metadata",
Nominal);

auto *M = IGM.getSILModule().getSwiftModule();

addTypeRef(M, Nominal->getDeclaredType()->getCanonicalType());
addTypeRef(Nominal->getDeclaredType()->getCanonicalType());
addNominalRef(Conformance->getProtocol());

B.addInt32(AssociatedTypes.size());
Expand All @@ -308,7 +289,7 @@ class AssociatedTypeMetadataBuilder : public ReflectionMetadataBuilder {
auto NameGlobal = IGM.getAddrOfFieldName(AssocTy.first);
B.addRelativeAddress(NameGlobal);
addBuiltinTypeRefs(AssocTy.second);
addTypeRef(M, AssocTy.second);
addTypeRef(AssocTy.second);
}
}

Expand Down Expand Up @@ -342,7 +323,7 @@ class FieldTypeMetadataBuilder : public ReflectionMetadataBuilder {
if (!type) {
B.addInt32(0);
} else {
addTypeRef(value->getModuleContext(), type);
addTypeRef(type);
addBuiltinTypeRefs(type);
}

Expand Down Expand Up @@ -448,8 +429,7 @@ class FieldTypeMetadataBuilder : public ReflectionMetadataBuilder {

auto *CD = dyn_cast<ClassDecl>(NTD);
if (CD && CD->getSuperclass()) {
addTypeRef(NTD->getModuleContext(),
CD->getSuperclass()->getCanonicalType());
addTypeRef(CD->getSuperclass()->getCanonicalType());
} else {
B.addInt32(0);
}
Expand Down Expand Up @@ -511,7 +491,7 @@ class FixedTypeMetadataBuilder : public ReflectionMetadataBuilder {
}

void layout() override {
addTypeRef(module, type);
addTypeRef(type);

B.addInt32(ti->getFixedSize().getValue());
B.addInt32(ti->getFixedAlignment().getValue());
Expand Down Expand Up @@ -561,7 +541,7 @@ class BoxDescriptorBuilder : public ReflectionMetadataBuilder {
B.addInt32(0); // Number of sources
B.addInt32(0); // Number of generic bindings

addTypeRef(IGM.getSILModule().getSwiftModule(), BoxedType);
addTypeRef(BoxedType);
addBuiltinTypeRefs(BoxedType);
}

Expand Down Expand Up @@ -754,8 +734,7 @@ class CaptureDescriptorBuilder : public ReflectionMetadataBuilder {

// Now add typerefs of all of the captures.
for (auto CaptureType : CaptureTypes) {
addTypeRef(IGM.getSILModule().getSwiftModule(), CaptureType,
OrigCalleeType->getGenericSignature());
addTypeRef(CaptureType);
addBuiltinTypeRefs(CaptureType);
}

Expand All @@ -765,7 +744,7 @@ class CaptureDescriptorBuilder : public ReflectionMetadataBuilder {
auto GenericParam = GenericAndSource.first->getCanonicalType();
auto Source = GenericAndSource.second;

addTypeRef(nullptr, GenericParam);
addTypeRef(GenericParam);
addMetadataSource(Source);
}
}
Expand Down
8 changes: 2 additions & 6 deletions lib/IRGen/IRGenMangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,8 @@ std::string IRGenMangler::manglePartialApplyForwarder(StringRef FuncName) {

SymbolicMangling
IRGenMangler::mangleTypeForReflection(IRGenModule &IGM,
Type Ty,
ModuleDecl *Module,
bool isSingleFieldOfBox) {
Mod = Module;
Type Ty) {
Mod = IGM.getSwiftModule();
OptimizeProtocolNames = false;

llvm::SaveAndRestore<std::function<bool (const DeclContext *)>>
Expand All @@ -109,8 +107,6 @@ IRGenMangler::mangleTypeForReflection(IRGenModule &IGM,
SymbolicReferences.clear();

appendType(Ty);
if (isSingleFieldOfBox)
appendOperator("Xb");

return {finalize(), std::move(SymbolicReferences)};
}
Expand Down
4 changes: 1 addition & 3 deletions lib/IRGen/IRGenMangler.h
Original file line number Diff line number Diff line change
Expand Up @@ -354,9 +354,7 @@ class IRGenMangler : public Mangle::ASTMangler {
}

SymbolicMangling mangleTypeForReflection(IRGenModule &IGM,
Type Ty,
ModuleDecl *Module,
bool isSingleFieldOfBox);
Type Ty);

std::string mangleTypeForLLVMTypeName(CanType Ty);

Expand Down
1 change: 1 addition & 0 deletions lib/IRGen/IRGenModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -970,6 +970,7 @@ class IRGenModule {
/// reflection metadata.
llvm::SetVector<const StructDecl *> ImportedStructs;

llvm::Constant *getTypeRef(CanType type);
llvm::Constant *getAddrOfStringForTypeRef(StringRef mangling);
llvm::Constant *getAddrOfStringForTypeRef(const SymbolicMangling &mangling);
llvm::Constant *getAddrOfFieldName(StringRef Name);
Expand Down
12 changes: 0 additions & 12 deletions lib/IRGen/MetadataRequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -297,18 +297,6 @@ llvm::Constant *IRGenModule::getAddrOfStringForTypeRef(
return addr;
}

// FIXME: willBeRelativelyAddressed is only needed to work around an ld64 bug
// resolving relative references to coalesceable symbols.
// It should be removed when fixed. rdar://problem/22674524
llvm::Constant *irgen::getTypeRef(IRGenModule &IGM, CanType type) {
IRGenMangler Mangler;
auto SymbolicName = Mangler.mangleTypeForReflection(IGM, type,
IGM.getSwiftModule(),
/*single-field box*/ false);

return IGM.getAddrOfStringForTypeRef(SymbolicName);
}

llvm::Value *irgen::emitObjCMetadataRefForMetadata(IRGenFunction &IGF,
llvm::Value *classPtr) {
assert(IGF.IGM.Context.LangOpts.EnableObjCInterop);
Expand Down
2 changes: 0 additions & 2 deletions lib/IRGen/MetadataRequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -503,8 +503,6 @@ emitInPlaceTypeMetadataAccessFunctionBody(IRGenFunction &IGF,
llvm::Value *uniqueForeignTypeMetadataRef(IRGenFunction &IGF,
llvm::Value *candidate);

llvm::Constant *getTypeRef(IRGenModule &IGM, CanType type);

using LazyCacheEmitter =
llvm::function_ref<MetadataResponse(IRGenFunction &IGF, Explosion &params)>;

Expand Down
6 changes: 2 additions & 4 deletions test/Reflection/capture_descriptors.sil
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ bb0(%i: $Int, %p: $@thick P.Type):

// CHECK: - Capture types:
// CHECK-NEXT: (struct Swift.Int)
// CHECK-NEXT: (sil_box
// CHECK-NEXT: (struct Swift.Int))
// CHECK-NEXT: (builtin Builtin.NativeObject)
// CHECK-NEXT: (existential_metatype
// CHECK-NEXT: (protocol_composition
// CHECK-NEXT: (protocol capture_descriptors.P)))
Expand Down Expand Up @@ -76,8 +75,7 @@ bb0:

// CHECK: - Capture types:
// CHECK-NEXT: (struct Swift.Int)
// CHECK-NEXT: (sil_box
// CHECK-NEXT: (struct Swift.String))
// CHECK-NEXT: (builtin Builtin.NativeObject)
// CHECK-NEXT: - Metadata sources:


Expand Down
3 changes: 1 addition & 2 deletions test/Reflection/typeref_decoding.swift
Original file line number Diff line number Diff line change
Expand Up @@ -708,8 +708,7 @@
// CHECK: CAPTURE DESCRIPTORS:
// CHECK: ====================
// CHECK: - Capture types:
// CHECK: (sil_box
// CHECK: (generic_type_parameter depth=0 index=0))
// CHECK: (builtin Builtin.NativeObject)
// CHECK: - Metadata sources:
// CHECK: (generic_type_parameter depth=0 index=0)
// CHECK: (closure_binding index=0)
Expand Down
3 changes: 1 addition & 2 deletions test/Reflection/typeref_decoding_asan.swift
Original file line number Diff line number Diff line change
Expand Up @@ -708,8 +708,7 @@
// CHECK: CAPTURE DESCRIPTORS:
// CHECK: ====================
// CHECK: - Capture types:
// CHECK: (sil_box
// CHECK: (generic_type_parameter depth=0 index=0))
// CHECK: (builtin Builtin.NativeObject)
// CHECK: - Metadata sources:
// CHECK: (generic_type_parameter depth=0 index=0)
// CHECK: (closure_binding index=0)
Expand Down