Skip to content

Commit ca96a2b

Browse files
committed
[AST] Define @execution(concurrent | caller) attribute on function declarations
1 parent 6b2fb2e commit ca96a2b

File tree

5 files changed

+78
-1
lines changed

5 files changed

+78
-1
lines changed

include/swift/AST/Attr.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,10 @@ class DeclAttribute : public AttributeBase {
220220

221221
NumFeatures : 31
222222
);
223+
224+
SWIFT_INLINE_BITFIELD(ExecutionAttr, DeclAttribute, NumExecutionKindBits,
225+
Behavior : NumExecutionKindBits
226+
);
223227
} Bits;
224228
// clang-format on
225229

@@ -2922,6 +2926,30 @@ class ABIAttr : public DeclAttribute {
29222926
UNIMPLEMENTED_CLONE(ABIAttr)
29232927
};
29242928

2929+
class ExecutionAttr : public DeclAttribute {
2930+
public:
2931+
ExecutionAttr(SourceLoc AtLoc, SourceRange Range,
2932+
ExecutionKind behavior,
2933+
bool Implicit)
2934+
: DeclAttribute(DeclAttrKind::Execution, AtLoc, Range, Implicit) {
2935+
Bits.ExecutionAttr.Behavior = static_cast<uint8_t>(behavior);
2936+
}
2937+
2938+
ExecutionAttr(ExecutionKind behavior, bool Implicit)
2939+
: ExecutionAttr(/*AtLoc=*/SourceLoc(), /*Range=*/SourceRange(), behavior,
2940+
Implicit) {}
2941+
2942+
ExecutionKind getBehavior() const {
2943+
return static_cast<ExecutionKind>(Bits.ExecutionAttr.Behavior);
2944+
}
2945+
2946+
static bool classof(const DeclAttribute *DA) {
2947+
return DA->getKind() == DeclAttrKind::Execution;
2948+
}
2949+
2950+
UNIMPLEMENTED_CLONE(ExecutionAttr)
2951+
};
2952+
29252953
/// Attributes that may be applied to declarations.
29262954
class DeclAttributes {
29272955
/// Linked list of declaration attributes.

include/swift/AST/AttrKind.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,15 @@ enum class ExternKind: uint8_t {
130130
enum : unsigned { NumExternKindBits =
131131
countBitsUsed(static_cast<unsigned>(ExternKind::Last_ExternKind)) };
132132

133+
enum class ExecutionKind : uint8_t {
134+
Concurrent = 0,
135+
Caller,
136+
Last_ExecutionKind = Caller
137+
};
138+
139+
enum : unsigned { NumExecutionKindBits =
140+
countBitsUsed(static_cast<unsigned>(ExecutionKind::Last_ExecutionKind)) };
141+
133142
enum class DeclAttrKind : unsigned {
134143
#define DECL_ATTR(_, CLASS, ...) CLASS,
135144
#define LAST_DECL_ATTR(CLASS) Last_DeclAttr = CLASS,

include/swift/AST/DeclAttr.def

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,12 @@ DECL_ATTR(abi, ABI,
538538
165)
539539
DECL_ATTR_FEATURE_REQUIREMENT(ABI, ABIAttribute)
540540

541-
LAST_DECL_ATTR(ABI)
541+
DECL_ATTR(execution, Execution,
542+
OnFunc | ABIBreakingToAdd | ABIBreakingToRemove | APIBreakingToAdd | APIBreakingToRemove,
543+
166)
544+
DECL_ATTR_FEATURE_REQUIREMENT(Execution, NonIsolatedAsyncInheritsIsolationFromContext)
545+
546+
LAST_DECL_ATTR(Execution)
542547

543548
#undef DECL_ATTR_ALIAS
544549
#undef CONTEXTUAL_DECL_ATTR_ALIAS

lib/AST/ASTDumper.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,14 @@ static StringRef getDumpString(NonSendableKind kind) {
526526
return "Specific";
527527
}
528528
}
529+
static StringRef getDumpString(ExecutionKind kind) {
530+
switch (kind) {
531+
case ExecutionKind::Concurrent:
532+
return "concurrent";
533+
case ExecutionKind::Caller:
534+
return "caller";
535+
}
536+
}
529537
static StringRef getDumpString(StringRef s) {
530538
return s;
531539
}
@@ -3871,6 +3879,11 @@ class PrintAttribute : public AttributeVisitor<PrintAttribute, void, StringRef>,
38713879

38723880
#undef TRIVIAL_ATTR_PRINTER
38733881

3882+
void visitExecutionAttr(ExecutionAttr *Attr, StringRef label) {
3883+
printCommon(Attr, "execution_attr", label);
3884+
printField(Attr->getBehavior(), "behavior");
3885+
printFoot();
3886+
}
38743887
void visitABIAttr(ABIAttr *Attr, StringRef label) {
38753888
printCommon(Attr, "abi_attr", label);
38763889
printRec(Attr->abiDecl, "decl");

lib/AST/Attr.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1650,6 +1650,19 @@ bool DeclAttribute::printImpl(ASTPrinter &Printer, const PrintOptions &Options,
16501650
break;
16511651
}
16521652

1653+
case DeclAttrKind::Execution: {
1654+
auto *attr = cast<ExecutionAttr>(this);
1655+
switch (attr->getBehavior()) {
1656+
case ExecutionKind::Concurrent:
1657+
Printer << "@execution(concurrent)";
1658+
break;
1659+
case ExecutionKind::Caller:
1660+
Printer << "@execution(caller)";
1661+
break;
1662+
}
1663+
break;
1664+
}
1665+
16531666
#define SIMPLE_DECL_ATTR(X, CLASS, ...) case DeclAttrKind::CLASS:
16541667
#include "swift/AST/DeclAttr.def"
16551668
llvm_unreachable("handled above");
@@ -1861,6 +1874,15 @@ StringRef DeclAttribute::getAttrName() const {
18611874
}
18621875
case DeclAttrKind::Lifetime:
18631876
return "lifetime";
1877+
case DeclAttrKind::Execution: {
1878+
switch (cast<ExecutionAttr>(this)->getBehavior()) {
1879+
case ExecutionKind::Concurrent:
1880+
return "execution(concurrent)";
1881+
case ExecutionKind::Caller:
1882+
return "execution(caller)";
1883+
}
1884+
llvm_unreachable("Invalid execution kind");
1885+
}
18641886
}
18651887
llvm_unreachable("bad DeclAttrKind");
18661888
}

0 commit comments

Comments
 (0)