Skip to content

Mangle coroutine information when mangling SILFunctionTypes #30154

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 1 commit into from
Mar 2, 2020
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
5 changes: 4 additions & 1 deletion docs/ABI/Mangling.rst
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ mangled in to disambiguate.
impl-function-type ::= type* 'I' FUNC-ATTRIBUTES '_'
impl-function-type ::= type* generic-signature 'I' PSEUDO-GENERIC? FUNC-ATTRIBUTES '_'

FUNC-ATTRIBUTES ::= CALLEE-ESCAPE? CALLEE-CONVENTION FUNC-REPRESENTATION? PARAM-CONVENTION* RESULT-CONVENTION* ('z' RESULT-CONVENTION)
FUNC-ATTRIBUTES ::= CALLEE-ESCAPE? CALLEE-CONVENTION FUNC-REPRESENTATION? COROUTINE-KIND? PARAM-CONVENTION* RESULT-CONVENTION* ('Y' PARAM-CONVENTION)* ('z' RESULT-CONVENTION)?

PSEUDO-GENERIC ::= 'P'

Expand All @@ -603,6 +603,9 @@ mangled in to disambiguate.
FUNC-REPRESENTATION ::= 'K' // closure
FUNC-REPRESENTATION ::= 'W' // protocol witness

COROUTINE-KIND ::= 'A' // yield-once coroutine
COROUTINE-KIND ::= 'G' // yield-many coroutine

PARAM-CONVENTION ::= 'i' // indirect in
PARAM-CONVENTION ::= 'c' // indirect in constant
PARAM-CONVENTION ::= 'l' // indirect inout
Expand Down
1 change: 1 addition & 0 deletions include/swift/Demangling/DemangleNodes.def
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ NODE(ImplSubstitutions)
CONTEXT_NODE(ImplicitClosure)
NODE(ImplParameter)
NODE(ImplResult)
NODE(ImplYield)
NODE(ImplErrorResult)
NODE(InOut)
NODE(InfixOperator)
Expand Down
2 changes: 1 addition & 1 deletion include/swift/Demangling/Demangler.h
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ class Demangler : public NodeFactory {
NodePointer popAnyProtocolConformanceList();
NodePointer demangleRetroactiveConformance();
NodePointer demangleInitializer();
NodePointer demangleImplParamConvention();
NodePointer demangleImplParamConvention(Node::Kind ConvKind);
NodePointer demangleImplResultConvention(Node::Kind ConvKind);
NodePointer demangleImplFunctionType();
NodePointer demangleMetatype();
Expand Down
19 changes: 19 additions & 0 deletions lib/AST/ASTMangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1499,6 +1499,18 @@ void ASTMangler::appendImplFunctionType(SILFunctionType *fn) {
OpArgs.push_back('W');
break;
}

// Coroutine kind. This is mangled in all pointer auth modes.
switch (fn->getCoroutineKind()) {
case SILCoroutineKind::None:
break;
case SILCoroutineKind::YieldOnce:
OpArgs.push_back('A');
break;
case SILCoroutineKind::YieldMany:
OpArgs.push_back('G');
break;
}

// Mangle the parameters.
for (auto param : fn->getParameters()) {
Expand All @@ -1512,6 +1524,13 @@ void ASTMangler::appendImplFunctionType(SILFunctionType *fn) {
appendType(result.getInterfaceType());
}

// Mangle the yields.
for (auto yield : fn->getYields()) {
OpArgs.push_back('Y');
OpArgs.push_back(getParamConvention(yield.getConvention()));
appendType(yield.getInterfaceType());
}

// Mangle the error result if present.
if (fn->hasErrorResult()) {
auto error = fn->getErrorResult();
Expand Down
23 changes: 20 additions & 3 deletions lib/Demangling/Demangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1692,7 +1692,7 @@ NodePointer Demangler::demangleBoundGenericArgs(NodePointer Nominal,
return createWithChildren(kind, createType(Nominal), args);
}

NodePointer Demangler::demangleImplParamConvention() {
NodePointer Demangler::demangleImplParamConvention(Node::Kind ConvKind) {
const char *attr = nullptr;
switch (nextChar()) {
case 'i': attr = "@in"; break;
Expand All @@ -1710,7 +1710,7 @@ NodePointer Demangler::demangleImplParamConvention() {
pushBack();
return nullptr;
}
return createWithChild(Node::Kind::ImplParameter,
return createWithChild(ConvKind,
createNode(Node::Kind::ImplConvention, attr));
}

Expand Down Expand Up @@ -1783,10 +1783,19 @@ NodePointer Demangler::demangleImplFunctionType() {
if (FAttr)
type->addChild(createNode(Node::Kind::ImplFunctionAttribute, FAttr), *this);

const char *CoroAttr = nullptr;
if (nextIf('A'))
CoroAttr = "@yield_once";
else if (nextIf('G'))
CoroAttr = "@yield_many";
if (CoroAttr)
type->addChild(createNode(Node::Kind::ImplFunctionAttribute, CoroAttr), *this);

addChild(type, GenSig);

int NumTypesToAdd = 0;
while (NodePointer Param = demangleImplParamConvention()) {
while (NodePointer Param =
demangleImplParamConvention(Node::Kind::ImplParameter)) {
type = addChild(type, Param);
NumTypesToAdd++;
}
Expand All @@ -1795,6 +1804,14 @@ NodePointer Demangler::demangleImplFunctionType() {
type = addChild(type, Result);
NumTypesToAdd++;
}
while (nextIf('Y')) {
NodePointer YieldResult =
demangleImplParamConvention(Node::Kind::ImplYield);
if (!YieldResult)
return nullptr;
type = addChild(type, YieldResult);
NumTypesToAdd++;
}
if (nextIf('z')) {
NodePointer ErrorResult = demangleImplResultConvention(
Node::Kind::ImplErrorResult);
Expand Down
9 changes: 8 additions & 1 deletion lib/Demangling/NodePrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,7 @@ class NodePrinter {
case Node::Kind::ImplicitClosure:
case Node::Kind::ImplParameter:
case Node::Kind::ImplResult:
case Node::Kind::ImplYield:
case Node::Kind::ImplErrorResult:
case Node::Kind::InOut:
case Node::Kind::InfixOperator:
Expand Down Expand Up @@ -766,6 +767,7 @@ class NodePrinter {
transitionTo(Inputs);
print(child);
} else if (child->getKind() == Node::Kind::ImplResult
|| child->getKind() == Node::Kind::ImplYield
|| child->getKind() == Node::Kind::ImplErrorResult) {
if (curState == Results) Printer << ", ";
transitionTo(Results);
Expand Down Expand Up @@ -2010,7 +2012,12 @@ NodePointer NodePrinter::print(NodePointer Node, bool asPrefixContext) {
return nullptr;
case Node::Kind::ImplErrorResult:
Printer << "@error ";
LLVM_FALLTHROUGH;
printChildren(Node, " ");
return nullptr;
case Node::Kind::ImplYield:
Printer << "@yields ";
printChildren(Node, " ");
return nullptr;
case Node::Kind::ImplParameter:
case Node::Kind::ImplResult:
printChildren(Node, " ");
Expand Down
10 changes: 10 additions & 0 deletions lib/Demangling/OldRemangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1227,6 +1227,10 @@ void Remangler::mangleImplFunctionAttribute(Node *node) {
Buffer << "CO";
} else if (text == "@convention(witness_method)") {
Buffer << "Cw";
} else if (text == "@yield_once") {
Buffer << "A";
} else if (text == "@yield_many") {
Buffer << "G";
} else {
unreachable("bad impl-function-attribute");
}
Expand All @@ -1248,6 +1252,12 @@ void Remangler::mangleImplResult(Node *node) {
mangleChildNodes(node); // impl convention, type
}

void Remangler::mangleImplYield(Node *node) {
assert(node->getNumChildren() == 2);
Buffer << 'Y';
mangleChildNodes(node); // impl convention, type
}

void Remangler::mangleImplEscaping(Node *node) {
// The old mangler does not encode escaping.
}
Expand Down
10 changes: 10 additions & 0 deletions lib/Demangling/Remangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1413,6 +1413,7 @@ void Remangler::mangleImplFunctionType(Node *node) {
switch (auto kind = Child->getKind()) {
case Node::Kind::ImplParameter:
case Node::Kind::ImplResult:
case Node::Kind::ImplYield:
case Node::Kind::ImplErrorResult:
mangleChildNode(Child, 1);
break;
Expand Down Expand Up @@ -1473,11 +1474,16 @@ void Remangler::mangleImplFunctionType(Node *node) {
.Case("@convention(objc_method)", 'O')
.Case("@convention(closure)", 'K')
.Case("@convention(witness_method)", 'W')
.Case("@yield_once", 'A')
.Case("@yield_many", 'G')
.Default(0);
assert(FuncAttr && "invalid impl function attribute");
Buffer << FuncAttr;
break;
}
case Node::Kind::ImplYield:
Buffer << 'Y';
LLVM_FALLTHROUGH;
case Node::Kind::ImplParameter: {
char ConvCh =
llvm::StringSwitch<char>(Child->getFirstChild()->getText())
Expand Down Expand Up @@ -1532,6 +1538,10 @@ void Remangler::mangleImplResult(Node *node) {
unreachable("handled inline");
}

void Remangler::mangleImplYield(Node *node) {
unreachable("handled inline");
}

void Remangler::mangleImplErrorResult(Node *node) {
unreachable("handled inline");
}
Expand Down
6 changes: 3 additions & 3 deletions test/IRGen/yield_once.sil
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ sil @marker : $(Builtin.Int32) -> ()
// CHECK-SAME: [[CORO_ATTRIBUTES:#[0-9]+]]
sil @test_simple : $@yield_once () -> () {
entry:
// CHECK-32: [[ID:%.*]] = call token @llvm.coro.id.retcon.once(i32 [[BUFFER_SIZE]], i32 [[BUFFER_ALIGN:4]], i8* %0, i8* bitcast (void (i8*, i1)* @"$sIet_TC" to i8*), i8* bitcast (i8* (i32)* @malloc to i8*), i8* bitcast (void (i8*)* @free to i8*))
// CHECK-64: [[ID:%.*]] = call token @llvm.coro.id.retcon.once(i32 [[BUFFER_SIZE]], i32 [[BUFFER_ALIGN:8]], i8* %0, i8* bitcast (void (i8*, i1)* @"$sIet_TC" to i8*), i8* bitcast (i8* (i64)* @malloc to i8*), i8* bitcast (void (i8*)* @free to i8*))
// CHECK-32: [[ID:%.*]] = call token @llvm.coro.id.retcon.once(i32 [[BUFFER_SIZE]], i32 [[BUFFER_ALIGN:4]], i8* %0, i8* bitcast (void (i8*, i1)* @"$sIetA_TC" to i8*), i8* bitcast (i8* (i32)* @malloc to i8*), i8* bitcast (void (i8*)* @free to i8*))
// CHECK-64: [[ID:%.*]] = call token @llvm.coro.id.retcon.once(i32 [[BUFFER_SIZE]], i32 [[BUFFER_ALIGN:8]], i8* %0, i8* bitcast (void (i8*, i1)* @"$sIetA_TC" to i8*), i8* bitcast (i8* (i64)* @malloc to i8*), i8* bitcast (void (i8*)* @free to i8*))
// CHECK-NEXT: [[BEGIN:%.*]] = call i8* @llvm.coro.begin(token [[ID]], i8* null)

// CHECK-NEXT: call swiftcc void @marker(i32 1000)
Expand Down Expand Up @@ -43,7 +43,7 @@ unwind:
// CHECK-NEXT: unreachable
}

// CHECK-LABEL: declare{{( dllimport)?}}{{( protected)?}} swiftcc void @"$sIet_TC"
// CHECK-LABEL: declare{{( dllimport)?}}{{( protected)?}} swiftcc void @"$sIetA_TC"
// CHECK-SAME: (i8* noalias dereferenceable([[BUFFER_SIZE]]), i1)

// CHECK-LABEL: define{{( dllexport)?}}{{( protected)?}} swiftcc void @test_simple_call(i1 %0)
Expand Down
6 changes: 3 additions & 3 deletions test/IRGen/yield_once_big.sil
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ entry:
// CHECK-64-SAME: , align 8

// Coroutine setup.
// CHECK-32-NEXT: [[ID:%.*]] = call token @llvm.coro.id.retcon.once(i32 [[BUFFER_SIZE]], i32 [[BUFFER_ALIGN:4]], i8* %0, i8* bitcast (void (i8*, i1)* @"$s14yield_once_big9SomeClassCRbzlIet_TC" to i8*), i8* bitcast (i8* (i32)* @malloc to i8*), i8* bitcast (void (i8*)* @free to i8*))
// CHECK-64-NEXT: [[ID:%.*]] = call token @llvm.coro.id.retcon.once(i32 [[BUFFER_SIZE]], i32 [[BUFFER_ALIGN:8]], i8* %0, i8* bitcast (void (i8*, i1)* @"$s14yield_once_big9SomeClassCRbzlIet_TC" to i8*), i8* bitcast (i8* (i64)* @malloc to i8*), i8* bitcast (void (i8*)* @free to i8*))
// CHECK-32-NEXT: [[ID:%.*]] = call token @llvm.coro.id.retcon.once(i32 [[BUFFER_SIZE]], i32 [[BUFFER_ALIGN:4]], i8* %0, i8* bitcast (void (i8*, i1)* @"$s14yield_once_big3BigVyxGAA9SomeClassCRbzlIetAYc_TC" to i8*), i8* bitcast (i8* (i32)* @malloc to i8*), i8* bitcast (void (i8*)* @free to i8*))
// CHECK-64-NEXT: [[ID:%.*]] = call token @llvm.coro.id.retcon.once(i32 [[BUFFER_SIZE]], i32 [[BUFFER_ALIGN:8]], i8* %0, i8* bitcast (void (i8*, i1)* @"$s14yield_once_big3BigVyxGAA9SomeClassCRbzlIetAYc_TC" to i8*), i8* bitcast (i8* (i64)* @malloc to i8*), i8* bitcast (void (i8*)* @free to i8*))
// CHECK-NEXT: [[BEGIN:%.*]] = call i8* @llvm.coro.begin(token [[ID]], i8* null)
// CHECK-NEXT: store %swift.type*

Expand Down Expand Up @@ -86,7 +86,7 @@ unwind:
// CHECK-NEXT: unreachable
}

// CHECK-LABEL: declare{{( dllimport)?}}{{( protected)?}} swiftcc void @"$s14yield_once_big9SomeClassCRbzlIet_TC"
// CHECK-LABEL: declare{{( dllimport)?}}{{( protected)?}} swiftcc void @"$s14yield_once_big3BigVyxGAA9SomeClassCRbzlIetAYc_TC"
// CHECK-SAME: (i8* noalias dereferenceable([[BUFFER_SIZE]]), i1)

// CHECK-LABEL: define{{( dllexport)?}}{{( protected)?}} swiftcc void @test_simple_call(i1 %0)
Expand Down
6 changes: 3 additions & 3 deletions test/IRGen/yield_once_biggish.sil
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ entry:
// CHECK-64-SAME: , align 8

// Coroutine setup.
// CHECK-32-NEXT: [[ID:%.*]] = call token @llvm.coro.id.retcon.once(i32 [[BUFFER_SIZE]], i32 [[BUFFER_ALIGN:4]], i8* %0, i8* bitcast (void (i8*, i1)* @"$s18yield_once_biggish9SomeClassCRbzlIet_TC" to i8*), i8* bitcast (i8* (i32)* @malloc to i8*), i8* bitcast (void (i8*)* @free to i8*))
// CHECK-64-NEXT: [[ID:%.*]] = call token @llvm.coro.id.retcon.once(i32 [[BUFFER_SIZE]], i32 [[BUFFER_ALIGN:8]], i8* %0, i8* bitcast (void (i8*, i1)* @"$s18yield_once_biggish9SomeClassCRbzlIet_TC" to i8*), i8* bitcast (i8* (i64)* @malloc to i8*), i8* bitcast (void (i8*)* @free to i8*))
// CHECK-32-NEXT: [[ID:%.*]] = call token @llvm.coro.id.retcon.once(i32 [[BUFFER_SIZE]], i32 [[BUFFER_ALIGN:4]], i8* %0, i8* bitcast (void (i8*, i1)* @"$s18yield_once_biggish7BiggishVyxGAA9SomeClassCRbzlIetAYx_TC" to i8*), i8* bitcast (i8* (i32)* @malloc to i8*), i8* bitcast (void (i8*)* @free to i8*))
// CHECK-64-NEXT: [[ID:%.*]] = call token @llvm.coro.id.retcon.once(i32 [[BUFFER_SIZE]], i32 [[BUFFER_ALIGN:8]], i8* %0, i8* bitcast (void (i8*, i1)* @"$s18yield_once_biggish7BiggishVyxGAA9SomeClassCRbzlIetAYx_TC" to i8*), i8* bitcast (i8* (i64)* @malloc to i8*), i8* bitcast (void (i8*)* @free to i8*))
// CHECK-NEXT: [[BEGIN:%.*]] = call i8* @llvm.coro.begin(token [[ID]], i8* null)
// CHECK-NEXT: store %swift.type*
// CHECK-NEXT: call swiftcc void @marker(i32 1000)
Expand Down Expand Up @@ -94,7 +94,7 @@ unwind:
// CHECK-NEXT: unreachable
}

// CHECK-LABEL: declare{{( dllimport)?}}{{( protected)?}} swiftcc void @"$s18yield_once_biggish9SomeClassCRbzlIet_TC"
// CHECK-LABEL: declare{{( dllimport)?}}{{( protected)?}} swiftcc void @"$s18yield_once_biggish7BiggishVyxGAA9SomeClassCRbzlIetAYx_TC"
// CHECK-SAME: (i8* noalias dereferenceable([[BUFFER_SIZE]]), i1)

// CHECK-LABEL: define{{( dllexport)?}}{{( protected)?}} swiftcc void @test_simple_call(i1 %0)
Expand Down
6 changes: 3 additions & 3 deletions test/IRGen/yield_once_indirect.sil
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ entry:
// CHECK-64-SAME: , align 8

// Coroutine setup.
// CHECK-32-NEXT: [[ID:%.*]] = call token @llvm.coro.id.retcon.once(i32 [[BUFFER_SIZE]], i32 [[BUFFER_ALIGN:4]], i8* %0, i8* bitcast (void (i8*, i1)* @"$s19yield_once_indirect9SomeClassCRbzlIet_TC" to i8*), i8* bitcast (i8* (i32)* @malloc to i8*), i8* bitcast (void (i8*)* @free to i8*))
// CHECK-64-NEXT: [[ID:%.*]] = call token @llvm.coro.id.retcon.once(i32 [[BUFFER_SIZE]], i32 [[BUFFER_ALIGN:8]], i8* %0, i8* bitcast (void (i8*, i1)* @"$s19yield_once_indirect9SomeClassCRbzlIet_TC" to i8*), i8* bitcast (i8* (i64)* @malloc to i8*), i8* bitcast (void (i8*)* @free to i8*))
// CHECK-32-NEXT: [[ID:%.*]] = call token @llvm.coro.id.retcon.once(i32 [[BUFFER_SIZE]], i32 [[BUFFER_ALIGN:4]], i8* %0, i8* bitcast (void (i8*, i1)* @"$s19yield_once_indirect8IndirectVyxGAA9SomeClassCRbzlIetAYi_TC" to i8*), i8* bitcast (i8* (i32)* @malloc to i8*), i8* bitcast (void (i8*)* @free to i8*))
// CHECK-64-NEXT: [[ID:%.*]] = call token @llvm.coro.id.retcon.once(i32 [[BUFFER_SIZE]], i32 [[BUFFER_ALIGN:8]], i8* %0, i8* bitcast (void (i8*, i1)* @"$s19yield_once_indirect8IndirectVyxGAA9SomeClassCRbzlIetAYi_TC" to i8*), i8* bitcast (i8* (i64)* @malloc to i8*), i8* bitcast (void (i8*)* @free to i8*))
// CHECK-NEXT: [[BEGIN:%.*]] = call i8* @llvm.coro.begin(token [[ID]], i8* null)
// CHECK-NEXT: store %swift.type*
// CHECK-NEXT: call swiftcc void @marker(i32 1000)
Expand Down Expand Up @@ -83,7 +83,7 @@ unwind:
// CHECK-NEXT: unreachable
}

// CHECK-LABEL: declare{{( dllimport)?}}{{( protected)?}} swiftcc void @"$s19yield_once_indirect9SomeClassCRbzlIet_TC"
// CHECK-LABEL: declare{{( dllimport)?}}{{( protected)?}} swiftcc void @"$s19yield_once_indirect8IndirectVyxGAA9SomeClassCRbzlIetAYi_TC"
// CHECK-SAME: (i8* noalias dereferenceable([[BUFFER_SIZE]]), i1)

// CHECK-LABEL: define{{( dllexport)?}}{{( protected)?}} swiftcc void @test_simple_call(i1 %0)
Expand Down