Skip to content

[AST] Perf: targeted use of __builtin_assume() in dyn_cast wrappers #13670

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
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: 7 additions & 2 deletions include/swift/AST/Type.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "swift/AST/PrintOptions.h"
#include "swift/AST/TypeAlignments.h"
#include "swift/Basic/OptionSet.h"
#include "swift/Basic/Compiler.h"
#include <functional>
#include <string>

Expand Down Expand Up @@ -536,7 +537,9 @@ template <class X> inline CanTypeWrapper<X> cast_or_null(CanType type) {
return CanTypeWrapper<X>(cast_or_null<X>(type.getPointer()));
}
template <class X> inline CanTypeWrapper<X> dyn_cast(CanType type) {
return CanTypeWrapper<X>(dyn_cast<X>(type.getPointer()));
auto Ty = type.getPointer();
SWIFT_ASSUME(Ty != nullptr);
return CanTypeWrapper<X>(dyn_cast<X>(Ty));
}
template <class X> inline CanTypeWrapper<X> dyn_cast_or_null(CanType type) {
return CanTypeWrapper<X>(dyn_cast_or_null<X>(type.getPointer()));
Expand All @@ -554,7 +557,9 @@ inline CanTypeWrapper<X> cast(CanTypeWrapper<P> type) {
}
template <class X, class P>
inline CanTypeWrapper<X> dyn_cast(CanTypeWrapper<P> type) {
return CanTypeWrapper<X>(dyn_cast<X>(type.getPointer()));
auto Ty = type.getPointer();
SWIFT_ASSUME(Ty != nullptr);
return CanTypeWrapper<X>(dyn_cast<X>(Ty));
}
template <class X, class P>
inline CanTypeWrapper<X> dyn_cast_or_null(CanTypeWrapper<P> type) {
Expand Down
4 changes: 3 additions & 1 deletion include/swift/AST/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,9 @@ class alignas(1 << TypeAlignInBits) TypeBase {
template <typename T>
T *getAs() {
static_assert(!isSugaredType<T>(), "getAs desugars types");
return dyn_cast<T>(getDesugaredType());
auto Ty = getDesugaredType();
SWIFT_ASSUME(Ty != nullptr);
return dyn_cast<T>(Ty);
}

template <typename T>
Expand Down
12 changes: 12 additions & 0 deletions include/swift/Basic/Compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
#define SWIFT_COMPILER_IS_MSVC 0
#endif

// Workaround non-clang compilers
#ifndef __has_builtin
#define __has_builtin(x) 0
#endif

#if SWIFT_COMPILER_IS_MSVC && _MSC_VER < 1910
// Work around MSVC bug: attempting to reference a deleted function
// https://connect.microsoft.com/VisualStudio/feedback/details/3116505
Expand All @@ -28,4 +33,11 @@
#define SWIFT_DELETE_OPERATOR_DELETED = delete;
#endif

// __builtin_assume() is an optimization hint.
#if __has_builtin(__builtin_assume)
#define SWIFT_ASSUME(x) __builtin_assume(x)
#else
#define SWIFT_ASSUME(x)
#endif

#endif // SWIFT_BASIC_COMPILER_H