Skip to content

Commit f0f5ad5

Browse files
committed
[thunk-lowering] Add support for mangling identity thunks.
This is just again using identity thunks to show that the behavior works before I add in hop to main actor.
1 parent b1750ba commit f0f5ad5

File tree

14 files changed

+224
-117
lines changed

14 files changed

+224
-117
lines changed

include/swift/AST/ASTMangler.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#include "swift/AST/Decl.h"
1717
#include "swift/AST/FreestandingMacroExpansion.h"
18+
#include "swift/AST/SILThunkKind.h"
1819
#include "swift/AST/Types.h"
1920
#include "swift/Basic/Mangler.h"
2021
#include "swift/Basic/TaggedUnion.h"
@@ -314,6 +315,9 @@ class ASTMangler : public Mangler {
314315
DifferentiabilityKind kind,
315316
const AutoDiffConfig &config);
316317

318+
std::string mangleSILThunkKind(StringRef originalName,
319+
SILThunkKind thunkKind);
320+
317321
/// Mangle the AutoDiff generated declaration for the given:
318322
/// - Generated declaration kind: linear map struct or branching trace enum.
319323
/// - Mangled original function name.

include/swift/AST/SILThunkKind.h

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
//===--- SILThunkKind.h ---------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2024 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+
/// \file This file contains a kind that defines the types of thunks that can be
14+
/// generated using a thunk inst. It provides an AST level interface that lets
15+
/// one generate the derived function kind and is also used to make adding such
16+
/// kinds to the mangler trivial.
17+
///
18+
//===----------------------------------------------------------------------===//
19+
20+
#ifndef SWIFT_AST_SILTHUNKKIND_H
21+
#define SWIFT_AST_SILTHUNKKIND_H
22+
23+
#include "swift/AST/Types.h"
24+
25+
namespace swift {
26+
27+
class SILType;
28+
29+
struct SILThunkKind {
30+
enum InnerTy {
31+
Invalid = 0,
32+
33+
/// A thunk that just calls the passed in function. Used for testing
34+
/// purposes of the underlying thunking machinery.
35+
Identity = 1,
36+
37+
MaxValue = Identity,
38+
};
39+
40+
InnerTy innerTy;
41+
42+
SILThunkKind() : innerTy(InnerTy::Invalid) {}
43+
SILThunkKind(InnerTy innerTy) : innerTy(innerTy) {}
44+
SILThunkKind(unsigned inputInnerTy) : innerTy(InnerTy(inputInnerTy)) {
45+
assert(inputInnerTy <= MaxValue && "Invalid value");
46+
}
47+
48+
operator InnerTy() const { return innerTy; }
49+
50+
/// Given the current enum state returned the derived output function from
51+
/// \p inputFunction.
52+
///
53+
/// Defined in Instructions.cpp
54+
CanSILFunctionType getDerivedFunctionType(SILFunction *fn,
55+
CanSILFunctionType inputFunction,
56+
SubstitutionMap subMap) const;
57+
58+
/// Given the current enum state returned the derived output function from
59+
/// \p inputFunction.
60+
///
61+
/// Defined in Instructions.cpp
62+
SILType getDerivedFunctionType(SILFunction *fn, SILType inputFunctionType,
63+
SubstitutionMap subMap) const;
64+
65+
Demangle::MangledSILThunkKind getMangledKind() const {
66+
switch (innerTy) {
67+
case Invalid:
68+
return Demangle::MangledSILThunkKind::Invalid;
69+
case Identity:
70+
return Demangle::MangledSILThunkKind::Identity;
71+
}
72+
}
73+
};
74+
75+
} // namespace swift
76+
77+
#endif

include/swift/Demangling/Demangle.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,11 @@ enum class MangledDifferentiabilityKind : char {
136136
Linear = 'l',
137137
};
138138

139+
enum class MangledSILThunkKind : char {
140+
Invalid = 0,
141+
Identity = 'I',
142+
};
143+
139144
/// The pass that caused the specialization to occur. We use this to make sure
140145
/// that two passes that generate similar changes do not yield the same
141146
/// mangling. This currently cannot happen, so this is just a safety measure

include/swift/Demangling/DemangleNodes.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@ NODE(ReflectionMetadataAssocTypeDescriptor)
291291
NODE(ReflectionMetadataSuperclassDescriptor)
292292
NODE(GenericTypeParamDecl)
293293
NODE(CurryThunk)
294+
NODE(SILThunkIdentity)
294295
NODE(DispatchThunk)
295296
NODE(MethodDescriptor)
296297
NODE(ProtocolRequirementsBaseDescriptor)

include/swift/SIL/SILInstruction.h

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "swift/AST/Decl.h"
2424
#include "swift/AST/GenericSignature.h"
2525
#include "swift/AST/ProtocolConformanceRef.h"
26+
#include "swift/AST/SILThunkKind.h"
2627
#include "swift/AST/SubstitutionMap.h"
2728
#include "swift/AST/TypeAlignments.h"
2829
#include "swift/Basic/Compiler.h"
@@ -5887,41 +5888,7 @@ class ThunkInst final
58875888
: public UnaryInstructionWithTypeDependentOperandsBase<
58885889
SILInstructionKind::ThunkInst, ThunkInst, SingleValueInstruction> {
58895890
public:
5890-
struct Kind {
5891-
enum InnerTy {
5892-
Invalid = 0,
5893-
5894-
/// A thunk that just calls the passed in function. Used for testing
5895-
/// purposes of the underlying thunking machinery.
5896-
Identity = 1,
5897-
5898-
MaxValue = Identity,
5899-
};
5900-
5901-
InnerTy innerTy;
5902-
5903-
Kind() : innerTy(InnerTy::Invalid) {}
5904-
Kind(InnerTy innerTy) : innerTy(innerTy) {}
5905-
Kind(unsigned inputInnerTy) : innerTy(InnerTy(inputInnerTy)) {
5906-
assert(inputInnerTy <= MaxValue && "Invalid value");
5907-
}
5908-
5909-
operator InnerTy() const { return innerTy; }
5910-
5911-
/// Given the current enum state returned the derived output function from
5912-
/// \p inputFunction.
5913-
CanSILFunctionType getDerivedFunctionType(SILFunction *fn,
5914-
CanSILFunctionType inputFunction,
5915-
SubstitutionMap subMap) const;
5916-
5917-
SILType getDerivedFunctionType(SILFunction *fn, SILType inputFunctionType,
5918-
SubstitutionMap subMap) const {
5919-
auto fType = inputFunctionType.castTo<SILFunctionType>();
5920-
return SILType::getPrimitiveType(
5921-
getDerivedFunctionType(fn, fType, subMap),
5922-
inputFunctionType.getCategory());
5923-
}
5924-
};
5891+
using Kind = SILThunkKind;
59255892

59265893
/// The type of thunk we are supposed to produce.
59275894
Kind kind;

lib/AST/ASTMangler.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,16 @@ std::string ASTMangler::mangleSILDifferentiabilityWitness(StringRef originalName
636636
return finalize();
637637
}
638638

639+
std::string ASTMangler::mangleSILThunkKind(StringRef originalName,
640+
SILThunkKind thunkKind) {
641+
beginManglingWithoutPrefix();
642+
appendOperator(originalName);
643+
// Prefix for thunk inst based thunks
644+
auto code = (char)thunkKind.getMangledKind();
645+
appendOperator("TT", StringRef(&code, 1));
646+
return finalize();
647+
}
648+
639649
std::string ASTMangler::mangleAutoDiffGeneratedDeclaration(
640650
AutoDiffGeneratedDeclarationKind declKind, StringRef origFnName,
641651
unsigned bbId, AutoDiffLinearMapKind linearMapKind,

lib/Demangling/Demangler.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2791,6 +2791,15 @@ NodePointer Demangler::popProtocolConformance() {
27912791

27922792
NodePointer Demangler::demangleThunkOrSpecialization() {
27932793
switch (char c = nextChar()) {
2794+
// Thunks that are from a thunk inst. We take the TT namespace.
2795+
case 'T': {
2796+
switch (char c = nextChar()) {
2797+
case 'I':
2798+
return createWithChild(Node::Kind::SILThunkIdentity, popNode(isEntity));
2799+
default:
2800+
return nullptr;
2801+
}
2802+
}
27942803
case 'c': return createWithChild(Node::Kind::CurryThunk, popNode(isEntity));
27952804
case 'j': return createWithChild(Node::Kind::DispatchThunk, popNode(isEntity));
27962805
case 'q': return createWithChild(Node::Kind::MethodDescriptor, popNode(isEntity));

lib/Demangling/NodePrinter.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,7 @@ class NodePrinter {
367367
case Node::Kind::Constructor:
368368
case Node::Kind::CoroutineContinuationPrototype:
369369
case Node::Kind::CurryThunk:
370+
case Node::Kind::SILThunkIdentity:
370371
case Node::Kind::DispatchThunk:
371372
case Node::Kind::Deallocator:
372373
case Node::Kind::IsolatedDeallocator:
@@ -1425,6 +1426,10 @@ NodePointer NodePrinter::print(NodePointer Node, unsigned depth,
14251426
Printer << "curry thunk of ";
14261427
print(Node->getChild(0), depth + 1);
14271428
return nullptr;
1429+
case Node::Kind::SILThunkIdentity:
1430+
Printer << "identity thunk of ";
1431+
print(Node->getChild(0), depth + 1);
1432+
return nullptr;
14281433
case Node::Kind::DispatchThunk:
14291434
Printer << "dispatch thunk of ";
14301435
print(Node->getChild(0), depth + 1);

lib/Demangling/OldRemangler.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2525,6 +2525,11 @@ ManglingError Remangler::mangleCurryThunk(Node *node, unsigned depth) {
25252525
return ManglingError::Success;
25262526
}
25272527

2528+
ManglingError Remangler::mangleSILThunkIdentity(Node *node, unsigned depth) {
2529+
Buffer << "<sil-identity-thunk>";
2530+
return ManglingError::Success;
2531+
}
2532+
25282533
ManglingError Remangler::mangleDispatchThunk(Node *node, unsigned depth) {
25292534
Buffer << "<dispatch-thunk>";
25302535
return ManglingError::Success;

lib/Demangling/Remangler.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,9 @@ class Remangler : public RemanglerBase {
374374
ManglingError mangleKeyPathThunkHelper(Node *node, StringRef op,
375375
unsigned depth);
376376

377+
ManglingError mangleSILThunkIdentity(Node *node, StringRef op,
378+
unsigned depth);
379+
377380
ManglingError mangleAutoDiffFunctionOrSimpleThunk(Node *node, StringRef op,
378381
unsigned depth);
379382

@@ -3371,6 +3374,14 @@ ManglingError Remangler::mangleCurryThunk(Node *node, unsigned depth) {
33713374
return ManglingError::Success;
33723375
}
33733376

3377+
ManglingError Remangler::mangleSILThunkIdentity(Node *node, unsigned depth) {
3378+
RETURN_IF_ERROR(mangleSingleChildNode(node, depth + 1)); // type
3379+
// TT is for a thunk that is for a thunk inst... I is for identity.
3380+
Buffer << "TT"
3381+
<< "I";
3382+
return ManglingError::Success;
3383+
}
3384+
33743385
ManglingError Remangler::mangleDispatchThunk(Node *node, unsigned depth) {
33753386
RETURN_IF_ERROR(mangleSingleChildNode(node, depth + 1));
33763387
Buffer << "Tj";

0 commit comments

Comments
 (0)