Skip to content

Commit 92a5c09

Browse files
committed
Swift support: add __attribute__((swift_async_context)) to args.
This translates to "swiftasync" LLVM attribute.
1 parent 74634a0 commit 92a5c09

File tree

12 files changed

+59
-1
lines changed

12 files changed

+59
-1
lines changed

clang/include/clang/AST/Attr.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,8 @@ class ParameterABIAttr : public InheritableParamAttr {
209209
switch (getKind()) {
210210
case attr::SwiftContext:
211211
return ParameterABI::SwiftContext;
212+
case attr::SwiftAsyncContext:
213+
return ParameterABI::SwiftAsyncContext;
212214
case attr::SwiftErrorResult:
213215
return ParameterABI::SwiftErrorResult;
214216
case attr::SwiftIndirectResult:

clang/include/clang/Basic/Attr.td

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2418,6 +2418,11 @@ def SwiftContext : ParameterABIAttr {
24182418
let Documentation = [SwiftContextDocs];
24192419
}
24202420

2421+
def SwiftAsyncContext : ParameterABIAttr {
2422+
let Spellings = [Clang<"swift_async_context">];
2423+
let Documentation = [SwiftAsyncContextDocs];
2424+
}
2425+
24212426
def SwiftErrorResult : ParameterABIAttr {
24222427
let Spellings = [Clang<"swift_error_result">];
24232428
let Documentation = [SwiftErrorResultDocs];

clang/include/clang/Basic/AttrDocs.td

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4551,6 +4551,19 @@ A context parameter must have pointer or reference type.
45514551
}];
45524552
}
45534553

4554+
def SwiftAsyncContextDocs : Documentation {
4555+
let Category = DocCatVariable;
4556+
let Content = [{
4557+
The ``swift_async_context`` attribute marks a parameter as having the
4558+
special asynchronous context-parameter ABI treatment.
4559+
4560+
This treatment generally passes the context value in a special register
4561+
which is normally callee-preserved.
4562+
4563+
A context parameter must have pointer or reference type.
4564+
}];
4565+
}
4566+
45544567
def SwiftErrorResultDocs : Documentation {
45554568
let Category = DocCatVariable;
45564569
let Content = [{

clang/include/clang/Basic/Specifiers.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,12 @@ namespace clang {
344344
/// This parameter (which must have pointer type) uses the special
345345
/// Swift context-pointer ABI treatment. There can be at
346346
/// most one parameter on a given function that uses this treatment.
347-
SwiftContext
347+
SwiftContext,
348+
349+
/// This parameter (which must have pointer type) uses the special
350+
/// Swift asynchronous context-pointer ABI treatment. There can be at
351+
/// most one parameter on a given function that uses this treatment.
352+
SwiftAsyncContext,
348353
};
349354

350355
/// Assigned inheritance model for a class in the MS C++ ABI. Must match order

clang/lib/AST/ItaniumMangle.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2941,6 +2941,7 @@ CXXNameMangler::mangleExtParameterInfo(FunctionProtoType::ExtParameterInfo PI) {
29412941

29422942
// All of these start with "swift", so they come before "ns_consumed".
29432943
case ParameterABI::SwiftContext:
2944+
case ParameterABI::SwiftAsyncContext:
29442945
case ParameterABI::SwiftErrorResult:
29452946
case ParameterABI::SwiftIndirectResult:
29462947
mangleVendorQualifier(getParameterABISpelling(PI.getABI()));

clang/lib/AST/TypePrinter.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -847,6 +847,8 @@ StringRef clang::getParameterABISpelling(ParameterABI ABI) {
847847
llvm_unreachable("asking for spelling of ordinary parameter ABI");
848848
case ParameterABI::SwiftContext:
849849
return "swift_context";
850+
case ParameterABI::SwiftAsyncContext:
851+
return "swift_async_context";
850852
case ParameterABI::SwiftErrorResult:
851853
return "swift_error_result";
852854
case ParameterABI::SwiftIndirectResult:

clang/lib/CodeGen/CGCall.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2356,6 +2356,10 @@ void CodeGenModule::ConstructAttributeList(
23562356
case ParameterABI::SwiftContext:
23572357
Attrs.addAttribute(llvm::Attribute::SwiftSelf);
23582358
break;
2359+
2360+
case ParameterABI::SwiftAsyncContext:
2361+
Attrs.addAttribute(llvm::Attribute::SwiftAsync);
2362+
break;
23592363
}
23602364

23612365
if (FI.getExtParameterInfo(ArgNo).isNoEscape())

clang/lib/Sema/SemaDeclAttr.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4958,6 +4958,14 @@ void Sema::AddParameterABIAttr(Decl *D, const AttributeCommonInfo &CI,
49584958
D->addAttr(::new (Context) SwiftContextAttr(Context, CI));
49594959
return;
49604960

4961+
case ParameterABI::SwiftAsyncContext:
4962+
if (!isValidSwiftContextType(type)) {
4963+
Diag(CI.getLoc(), diag::err_swift_abi_parameter_wrong_type)
4964+
<< getParameterABISpelling(abi) << /*pointer to pointer */ 0 << type;
4965+
}
4966+
D->addAttr(::new (Context) SwiftAsyncContextAttr(Context, CI));
4967+
return;
4968+
49614969
case ParameterABI::SwiftErrorResult:
49624970
if (!isValidSwiftErrorResultType(type)) {
49634971
Diag(CI.getLoc(), diag::err_swift_abi_parameter_wrong_type)
@@ -8020,6 +8028,9 @@ static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
80208028
case ParsedAttr::AT_SwiftContext:
80218029
S.AddParameterABIAttr(D, AL, ParameterABI::SwiftContext);
80228030
break;
8031+
case ParsedAttr::AT_SwiftAsyncContext:
8032+
S.AddParameterABIAttr(D, AL, ParameterABI::SwiftAsyncContext);
8033+
break;
80238034
case ParsedAttr::AT_SwiftErrorResult:
80248035
S.AddParameterABIAttr(D, AL, ParameterABI::SwiftErrorResult);
80258036
break;

clang/lib/Sema/SemaType.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2783,6 +2783,10 @@ static void checkExtParameterInfos(Sema &S, ArrayRef<QualType> paramTypes,
27832783
checkForSwiftCC(paramIndex);
27842784
continue;
27852785

2786+
case ParameterABI::SwiftAsyncContext:
2787+
// FIXME: might want to require swiftasynccc when it exists
2788+
continue;
2789+
27862790
// swift_error parameters must be preceded by a swift_context parameter.
27872791
case ParameterABI::SwiftErrorResult:
27882792
checkForSwiftCC(paramIndex);

clang/test/CodeGen/arm-swiftcall.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#define OUT __attribute__((swift_indirect_result))
77
#define ERROR __attribute__((swift_error_result))
88
#define CONTEXT __attribute__((swift_context))
9+
#define ASYNC_CONTEXT __attribute__((swift_async_context))
910

1011
/*****************************************************************************/
1112
/****************************** PARAMETER ABIS *******************************/
@@ -53,6 +54,9 @@ void test_context_error_1() {
5354
SWIFTCALL void context_error_2(short s, CONTEXT int *self, ERROR float **error) {}
5455
// CHECK-LABEL: define{{.*}} void @context_error_2(i16{{.*}}, i32* swiftself{{.*}}, float** swifterror %0)
5556

57+
SWIFTCALL void async_context_1(ASYNC_CONTEXT void *self) {}
58+
// CHECK-LABEL: define {{.*}} void @async_context_1(i8* swiftasync
59+
5660
/*****************************************************************************/
5761
/********************************** LOWERING *********************************/
5862
/*****************************************************************************/

clang/test/Misc/pragma-attribute-supported-attributes-list.test

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@
153153
// CHECK-NEXT: SetTypestate (SubjectMatchRule_function_is_member)
154154
// CHECK-NEXT: SpeculativeLoadHardening (SubjectMatchRule_function, SubjectMatchRule_objc_method)
155155
// CHECK-NEXT: SwiftAsync (SubjectMatchRule_function, SubjectMatchRule_objc_method)
156+
// CHECK-NEXT: SwiftAsyncContext (SubjectMatchRule_variable_is_parameter)
156157
// CHECK-NEXT: SwiftAsyncName (SubjectMatchRule_objc_method, SubjectMatchRule_function)
157158
// CHECK-NEXT: SwiftBridgedTypedef (SubjectMatchRule_type_alias)
158159
// CHECK-NEXT: SwiftContext (SubjectMatchRule_variable_is_parameter)

clang/test/Sema/attr-swiftcall.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#define INDIRECT_RESULT __attribute__((swift_indirect_result))
66
#define ERROR_RESULT __attribute__((swift_error_result))
77
#define CONTEXT __attribute__((swift_context))
8+
#define ASYNC_CONTEXT __attribute__((swift_async_context))
89

910
int notAFunction SWIFTCALL; // expected-warning {{'swiftcall' only applies to function types; type here is 'int'}}
1011
void variadic(int x, ...) SWIFTCALL; // expected-error {{variadic function cannot use swiftcall calling convention}}
@@ -29,3 +30,8 @@ void context_nonswift(CONTEXT void *context); // expected-error {{'swift_context
2930
void context_bad_type(CONTEXT int context) SWIFTCALL; // expected-error {{'swift_context' parameter must have pointer type; type here is 'int'}}
3031
void context_okay(CONTEXT void *context) SWIFTCALL;
3132
void context_okay2(CONTEXT void *context, void *selfType, char **selfWitnessTable) SWIFTCALL;
33+
34+
void async_context_okay_for_now(ASYNC_CONTEXT void *context);
35+
void async_context_bad_type(ASYNC_CONTEXT int context) SWIFTCALL; // expected-error {{'swift_async_context' parameter must have pointer type; type here is 'int'}}
36+
void async_context_okay(ASYNC_CONTEXT void *context) SWIFTCALL;
37+
void async_context_okay2(ASYNC_CONTEXT void *context, void *selfType, char **selfWitnessTable) SWIFTCALL;

0 commit comments

Comments
 (0)