Skip to content

Commit edd2fac

Browse files
committed
Merge remote-tracking branch 'origin/master' into master-next
2 parents e0da654 + c925ce5 commit edd2fac

File tree

7 files changed

+100
-41
lines changed

7 files changed

+100
-41
lines changed

include/swift/Reflection/ReflectionContext.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -838,6 +838,18 @@ class ReflectionContext
838838
return 0;
839839
}
840840

841+
/// Get the name of a metadata tag, if known.
842+
llvm::Optional<std::string> metadataAllocationTagName(int Tag) {
843+
switch (Tag) {
844+
#define TAG(name, value) \
845+
case value: \
846+
return std::string(#name);
847+
#include "../../../stdlib/public/runtime/MetadataAllocatorTags.def"
848+
default:
849+
return llvm::None;
850+
}
851+
}
852+
841853
/// Iterate the metadata allocations in the target process, calling Call with
842854
/// each allocation found. Returns None on success, and a string describing
843855
/// the error on failure.

include/swift/Runtime/Metadata.h

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -29,29 +29,9 @@ namespace swift {
2929
// allocator. This is encoded in a header on each allocation when metadata
3030
// iteration is enabled, and allows tools to know where each allocation came
3131
// from.
32-
//
33-
// Some of these values are also declared in SwiftRemoteMirrorTypes.h. Those
34-
// values must be kept stable to preserve compatibility.
3532
enum MetadataAllocatorTags : uint16_t {
36-
UnusedTag = 0,
37-
BoxesTag,
38-
ObjCClassWrappersTag,
39-
FunctionTypesTag,
40-
MetatypeTypesTag,
41-
ExistentialMetatypeValueWitnessTablesTag,
42-
ExistentialMetatypesTag,
43-
ExistentialTypesTag,
44-
OpaqueExistentialValueWitnessTablesTag,
45-
ClassExistentialValueWitnessTablesTag,
46-
ForeignWitnessTablesTag,
47-
ResilientMetadataAllocatorTag,
48-
MetadataTag,
49-
TupleCacheTag,
50-
GenericMetadataCacheTag,
51-
ForeignMetadataCacheTag,
52-
GenericWitnessTableCacheTag,
53-
GenericClassMetadataTag,
54-
GenericValueMetadataTag,
33+
#define TAG(name, value) name##Tag = value,
34+
#include "../../../stdlib/public/runtime/MetadataAllocatorTags.def"
5535
};
5636

5737
template <typename Runtime> struct MetadataAllocationBacktraceHeader {

include/swift/SwiftRemoteMirror/SwiftRemoteMirror.h

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -324,16 +324,24 @@ const char *swift_reflection_iterateMetadataAllocations(
324324
/// Given a metadata allocation, return the metadata it points to. Returns NULL
325325
/// on failure. Despite the name, not all allocations point to metadata.
326326
/// Currently, this will return a metadata only for allocations with tag
327-
/// SWIFT_GENERIC_METADATA_CACHE_ALLOCATION. Support for additional tags may be
328-
/// added in the future. The caller must gracefully handle failure.
327+
/// GenericMetadataCache. Support for additional tags may be added in the
328+
/// future. The caller must gracefully handle failure.
329329
SWIFT_REMOTE_MIRROR_LINKAGE
330330
swift_reflection_ptr_t swift_reflection_allocationMetadataPointer(
331331
SwiftReflectionContextRef ContextRef,
332332
swift_metadata_allocation_t Allocation);
333333

334+
/// Return the name of a metadata allocation tag, or NULL if the tag is unknown.
335+
/// This pointer remains valid until the next swift_reflection call on the given
336+
/// context.
337+
SWIFT_REMOTE_MIRROR_LINKAGE
338+
const char *
339+
swift_reflection_metadataAllocationTagName(SwiftReflectionContextRef ContextRef,
340+
swift_metadata_allocation_tag_t Tag);
341+
334342
/// Backtrace iterator callback passed to
335343
/// swift_reflection_iterateMetadataAllocationBacktraces
336-
typedef void (*swift_metadataAllocationIterator)(
344+
typedef void (*swift_metadataAllocationBacktraceIterator)(
337345
swift_reflection_ptr_t AllocationPtr, size_t Count,
338346
const swift_reflection_ptr_t Ptrs[], void *ContextPtr);
339347

@@ -350,8 +358,8 @@ typedef void (*swift_metadataAllocationIterator)(
350358
/// swift_reflection call on the given context.
351359
SWIFT_REMOTE_MIRROR_LINKAGE
352360
const char *swift_reflection_iterateMetadataAllocationBacktraces(
353-
SwiftReflectionContextRef ContextRef, swift_metadataAllocationIterator Call,
354-
void *ContextPtr);
361+
SwiftReflectionContextRef ContextRef,
362+
swift_metadataAllocationBacktraceIterator Call, void *ContextPtr);
355363

356364
#ifdef __cplusplus
357365
} // extern "C"

lib/Sema/CSSolver.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,12 @@ void ConstraintSystem::shrink(Expr *expr) {
800800
return {false, expr};
801801
}
802802

803+
// Similar to 'ClosureExpr', 'TapExpr' has a 'VarDecl' the type of which
804+
// is determined by type checking the parent interpolated string literal.
805+
if (isa<TapExpr>(expr)) {
806+
return {false, expr};
807+
}
808+
803809
if (auto coerceExpr = dyn_cast<CoerceExpr>(expr)) {
804810
if (coerceExpr->isLiteralInit())
805811
ApplyExprs.push_back({coerceExpr, 1});

stdlib/public/SwiftRemoteMirror/SwiftRemoteMirror.cpp

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ struct SwiftReflectionContext {
4040
NativeReflectionContext *nativeContext;
4141
std::vector<std::function<void()>> freeFuncs;
4242
std::vector<std::tuple<swift_addr_t, swift_addr_t>> dataSegments;
43-
std::string lastError;
44-
43+
std::string lastString;
44+
4545
SwiftReflectionContext(MemoryReaderImpl impl) {
4646
auto Reader = std::make_shared<CMemoryReader>(impl);
4747
nativeContext = new NativeReflectionContext(Reader);
@@ -423,11 +423,11 @@ static swift_childinfo_t convertChild(const TypeInfo *TI, unsigned Index) {
423423
};
424424
}
425425

426-
static const char *convertError(SwiftReflectionContextRef ContextRef,
427-
llvm::Optional<std::string> Error) {
428-
if (Error) {
429-
ContextRef->lastError = *Error;
430-
return ContextRef->lastError.c_str();
426+
static const char *returnableCString(SwiftReflectionContextRef ContextRef,
427+
llvm::Optional<std::string> String) {
428+
if (String) {
429+
ContextRef->lastString = *String;
430+
return ContextRef->lastString.c_str();
431431
}
432432
return nullptr;
433433
}
@@ -605,7 +605,7 @@ const char *swift_reflection_iterateConformanceCache(
605605
auto Error = Context->iterateConformances([&](auto Type, auto Proto) {
606606
Call(Type, Proto, ContextPtr);
607607
});
608-
return convertError(ContextRef, Error);
608+
return returnableCString(ContextRef, Error);
609609
}
610610

611611
const char *swift_reflection_iterateMetadataAllocations(
@@ -621,10 +621,9 @@ const char *swift_reflection_iterateMetadataAllocations(
621621
CAllocation.Size = Allocation.Size;
622622
Call(CAllocation, ContextPtr);
623623
});
624-
return convertError(ContextRef, Error);
624+
return returnableCString(ContextRef, Error);
625625
}
626626

627-
SWIFT_REMOTE_MIRROR_LINKAGE
628627
swift_reflection_ptr_t swift_reflection_allocationMetadataPointer(
629628
SwiftReflectionContextRef ContextRef,
630629
swift_metadata_allocation_t Allocation) {
@@ -636,9 +635,16 @@ swift_reflection_ptr_t swift_reflection_allocationMetadataPointer(
636635
return Context->allocationMetadataPointer(NativeAllocation);
637636
}
638637

638+
const char *swift_reflection_metadataAllocationTagName(
639+
SwiftReflectionContextRef ContextRef, swift_metadata_allocation_tag_t Tag) {
640+
auto Context = ContextRef->nativeContext;
641+
auto Result = Context->metadataAllocationTagName(Tag);
642+
return returnableCString(ContextRef, Result);
643+
}
644+
639645
const char *swift_reflection_iterateMetadataAllocationBacktraces(
640-
SwiftReflectionContextRef ContextRef, swift_metadataAllocationIterator Call,
641-
void *ContextPtr) {
646+
SwiftReflectionContextRef ContextRef,
647+
swift_metadataAllocationBacktraceIterator Call, void *ContextPtr) {
642648
auto Context = ContextRef->nativeContext;
643649
auto Error = Context->iterateMetadataAllocationBacktraces(
644650
[&](auto AllocationPtr, auto Count, auto Ptrs) {
@@ -651,5 +657,5 @@ const char *swift_reflection_iterateMetadataAllocationBacktraces(
651657
&Ptrs[Count]};
652658
Call(AllocationPtr, Count, ConvertedPtrs.data(), ContextPtr);
653659
});
654-
return convertError(ContextRef, Error);
660+
return returnableCString(ContextRef, Error);
655661
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//===--- MetadataAllocatorTags.def - Metadata allocator tags. ---*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2020 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+
// This file defines x-macros used for metaprogramming with the set of tags used
14+
// to mark metadata allocations when
15+
// SWIFT_DEBUG_ENABLE_METADATA_ALLOCATION_ITERATION is enabled.
16+
//
17+
//===----------------------------------------------------------------------===//
18+
19+
// #define TAG(name, value)
20+
21+
#ifndef TAG
22+
#error "Must define TAG to include MetadataAllocatorTags.def"
23+
#endif
24+
25+
TAG(NotSet, 0)
26+
TAG(Boxes, 1)
27+
TAG(ObjCClassWrappers, 2)
28+
TAG(FunctionTypes, 3)
29+
TAG(MetatypeTypes, 4)
30+
TAG(ExistentialMetatypeValueWitnessTables, 5)
31+
TAG(ExistentialMetatypes, 6)
32+
TAG(ExistentialTypes, 7)
33+
TAG(OpaqueExistentialValueWitnessTables, 8)
34+
TAG(ClassExistentialValueWitnessTables, 9)
35+
TAG(ForeignWitnessTables, 10)
36+
TAG(ResilientMetadataAllocator, 11)
37+
TAG(Metadata, 12)
38+
TAG(TupleCache, 13)
39+
TAG(GenericMetadataCache, 14)
40+
TAG(ForeignMetadataCache, 15)
41+
TAG(GenericWitnessTableCache, 16)
42+
TAG(GenericClassMetadata, 17)
43+
TAG(GenericValueMetadata, 18)
44+
45+
#undef TAG

tools/swift-inspect/Sources/swift-inspect/main.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,10 @@ func dumpRawMetadata(
4040
) throws {
4141
let backtraces = backtraceStyle != nil ? context.allocationBacktraces : [:]
4242
for allocation in context.allocations {
43+
let tagNameC = swift_reflection_metadataAllocationTagName(context, allocation.tag)
44+
let tagName = tagNameC.map(String.init) ?? "<unknown>"
4345
print("Metadata allocation at: \(hex: allocation.ptr) " +
44-
"size: \(allocation.size) tag: \(allocation.tag)")
46+
"size: \(allocation.size) tag: \(allocation.tag) (\(tagName))")
4547
printBacktrace(style: backtraceStyle, for: allocation.ptr, in: backtraces, inspector: inspector)
4648
}
4749
}

0 commit comments

Comments
 (0)