Skip to content

First prototype of Performance Annotations #39902

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 10 commits into from
Oct 29, 2021
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
9 changes: 9 additions & 0 deletions docs/ReferenceGuides/UnderscoredAttributes.md
Original file line number Diff line number Diff line change
Expand Up @@ -625,3 +625,12 @@ for more details.

Marks a var decl as a variable that must be copied explicitly using the builtin
function Builtin.copy.

## `@_noAllocation`, `@_noLocks`

These attributes are performance annotations. If a function is annotated with
such an attribute, the compiler issues a diagnostic message if the function
calls a runtime function which allocates memory or locks, respectively.
The `@_noLocks` attribute implies `@_noAllocation` because a memory allocation
also locks.

9 changes: 9 additions & 0 deletions docs/SIL.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1107,6 +1107,15 @@ Specifies for which types specialized code should be generated.
sil-function-attribute ::= '[clang "' identifier '"]'

The clang node owner.
::

sil-function-attribute ::= '[' performance-constraint ']'
performance-constraint :: 'no_locks'
performance-constraint :: 'no_allocation'

Specifies the performance constraints for the function, which defines which type
of runtime functions are allowed to be called from the function.


Basic Blocks
~~~~~~~~~~~~
Expand Down
10 changes: 10 additions & 0 deletions include/swift/AST/Attr.def
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,16 @@ SIMPLE_DECL_ATTR(_noImplicitCopy, NoImplicitCopy,
OnVar,
122)

SIMPLE_DECL_ATTR(_noLocks, NoLocks,
OnAbstractFunction | OnSubscript | UserInaccessible |
ABIStableToAdd | ABIStableToRemove | APIStableToAdd | APIStableToRemove,
123)

SIMPLE_DECL_ATTR(_noAllocation, NoAllocation,
OnAbstractFunction | OnSubscript | UserInaccessible |
ABIStableToAdd | ABIStableToRemove | APIStableToAdd | APIStableToRemove,
124)

// If you're adding a new underscored attribute here, please document it in
// docs/ReferenceGuides/UnderscoredAttributes.md.

Expand Down
28 changes: 28 additions & 0 deletions include/swift/AST/DiagnosticsSIL.def
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,34 @@ WARNING(warn_dead_weak_store,none,
"weak reference will always be nil because the referenced object is "
"deallocated here", ())

// performance diagnostics
ERROR(performance_annotations_not_enabled,none,
"use -experimental-performance-annotations to enable performance annotations", ())
ERROR(performance_dynamic_casting,none,
"dynamic casting can lock or allocate", ())
ERROR(performance_metadata,none,
"%0 can cause metadata allocation or locks", (StringRef))
ERROR(performance_metadata_type,none,
"Using type %0 can cause metadata allocation or locks", (Type))
ERROR(performance_allocating,none,
"%0 can cause an allocation", (StringRef))
ERROR(performance_deallocating,none,
"%0 can cause an deallocation", (StringRef))
ERROR(performance_deallocating_type,none,
"%0 a value of type %1 can cause a deallocation", (StringRef, Type))
ERROR(performance_locking,none,
"%0 can cause locking", (StringRef))
ERROR(performance_arc,none,
"this code performs reference counting operations which can cause locking", ())
ERROR(performance_objectivec,none,
"calls of Objective-C methods can have unpredictable performance", ())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

objc_msgSend will also take a lock on the slow path, and the slow path can happen unpredictably due to cache invalidation. Not sure if that's also worth calling out.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think "unpredictable performance" is a strong enough term which IMO includes locks, etc.

ERROR(performance_unknown_callees,none,
"called function is not known at compile time and can have unpredictable performance", ())
ERROR(performance_callee_unavailable,none,
"called function is not availbale in this module and can have unpredictable performance", ())
NOTE(performance_called_from,none,
"called from here", ())

// 'transparent' diagnostics
ERROR(circular_transparent,none,
"inlining 'transparent' functions forms circular loop", ())
Expand Down
5 changes: 4 additions & 1 deletion include/swift/AST/SILOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,10 @@ class SILOptions {

/// Controls whether cross module optimization is enabled.
bool CrossModuleOptimization = false;


/// Enables experimental performance annotations.
bool EnablePerformanceAnnotations = false;

/// Controls whether or not paranoid verification checks are run.
bool VerifyAll = false;

Expand Down
2 changes: 2 additions & 0 deletions include/swift/AST/SemanticAttrs.def
Original file line number Diff line number Diff line change
Expand Up @@ -115,5 +115,7 @@ SEMANTICS_ATTR(OBJC_FORBID_ASSOCIATED_OBJECTS, "objc.forbidAssociatedObjects")

SEMANTICS_ATTR(LIFETIMEMANAGEMENT_MOVE, "lifetimemanagement.move")

SEMANTICS_ATTR(NO_PERFORMANCE_ANALYSIS, "no_performance_analysis")

#undef SEMANTICS_ATTR

4 changes: 4 additions & 0 deletions include/swift/Option/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,10 @@ def CrossModuleOptimization : Flag<["-"], "cross-module-optimization">,
Flags<[HelpHidden, FrontendOption]>,
HelpText<"Perform cross-module optimization">;

def ExperimentalPerformanceAnnotations : Flag<["-"], "experimental-performance-annotations">,
Flags<[HelpHidden, FrontendOption]>,
HelpText<"Perform cross-module optimization">;

def RemoveRuntimeAsserts : Flag<["-"], "remove-runtime-asserts">,
Flags<[FrontendOption]>,
HelpText<"Remove runtime safety checks.">;
Expand Down
1 change: 1 addition & 0 deletions include/swift/Runtime/RuntimeFnWrappersGen.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#ifndef SWIFT_RUNTIME_RUNTIMEFNWRAPPERSGEN_H
#define SWIFT_RUNTIME_RUNTIMEFNWRAPPERSGEN_H

#include "swift/SIL/RuntimeEffect.h"
#include "llvm/IR/Module.h"
#include "llvm/ADT/ArrayRef.h"

Expand Down
Loading