mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
TLDR: This patch introduces a new kind of builtin, "a polymorphic builtin". One calls it like any other builtin, e.x.: ``` Builtin.generic_add(x, y) ``` but it has a contract: it must be specialized to a concrete builtin by the time we hit Lowered SIL. In this commit, I add support for the following generic operations: Type | Op ------------------------ FloatOrVector |FAdd FloatOrVector |FDiv FloatOrVector |FMul FloatOrVector |FRem FloatOrVector |FSub IntegerOrVector|AShr IntegerOrVector|Add IntegerOrVector|And IntegerOrVector|ExactSDiv IntegerOrVector|ExactUDiv IntegerOrVector|LShr IntegerOrVector|Mul IntegerOrVector|Or IntegerOrVector|SDiv IntegerOrVector|SRem IntegerOrVector|Shl IntegerOrVector|Sub IntegerOrVector|UDiv IntegerOrVector|Xor Integer |URem NOTE: I only implemented support for the builtins in SIL and in SILGen. I am going to implement the optimizer parts of this in a separate series of commits. DISCUSSION ---------- Today there are polymorphic like instructions in LLVM-IR. Yet, at the swift and SIL level we represent these operations instead as Builtins whose names are resolved by splatting the builtin into the name. For example, adding two things in LLVM: ``` %2 = add i64 %0, %1 %2 = add <2 x i64> %0, %1 %2 = add <4 x i64> %0, %1 %2 = add <8 x i64> %0, %1 ``` Each of the add operations are done by the same polymorphic instruction. In constrast, we splat out these Builtins in swift today, i.e.: ``` let x, y: Builtin.Int32 Builtin.add_Int32(x, y) let x, y: Builtin.Vec4xInt32 Builtin.add_Vec4xInt32(x, y) ... ``` In SIL, we translate these verbatim and then IRGen just lowers them to the appropriate polymorphic instruction. Beyond being verbose, these prevent these Builtins (which need static types) from being used in polymorphic contexts where we can guarantee that eventually a static type will be provided. In contrast, the polymorphic builtins introduced in this commit can be passed any type, with the proviso that the expert user using this feature can guarantee that before we reach Lowered SIL, the generic_add has been eliminated. This is enforced by IRGen asserting if passed such a builtin and by the SILVerifier checking that the underlying builtin is never called once the module is in Lowered SIL. In forthcoming commits, I am going to add two optimizations that give the stdlib tool writer the tools needed to use this builtin: 1. I am going to add an optimization to constant propagation that changes a "generic_*" op to the type of its argument if the argument is a type that is valid for the builtin (i.e. integer or vector). 2. I am going to teach the SILCloner how to specialize these as it inlines. This ensures that when we transparent inline, we specialize the builtin automatically and can then form SSA at -Onone using predictable memory access operations. The main implication around these polymorphic builtins are that if an author is not able to specialize the builtin, they need to ensure that after constant propagation, the generic builtin has been DCEed. The general rules are that the -Onone optimizer will constant fold branches with constant integer operands. So if one can use a bool of some sort to trigger the operation, one can be guaranteed that the code will not codegen. I am considering putting in some sort of diagnostic to ensure that the stdlib writer has a good experience (e.x. get an error instead of crashing the compiler).
138 lines
4.3 KiB
C++
138 lines
4.3 KiB
C++
//===--- Builtins.h - Swift Builtin Functions -------------------*- C++ -*-===//
|
|
//
|
|
// This source file is part of the Swift.org open source project
|
|
//
|
|
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
|
|
// Licensed under Apache License v2.0 with Runtime Library Exception
|
|
//
|
|
// See https://swift.org/LICENSE.txt for license information
|
|
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This file defines the interface to builtin functions.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#ifndef SWIFT_AST_BUILTINS_H
|
|
#define SWIFT_AST_BUILTINS_H
|
|
|
|
#include "swift/AST/Type.h"
|
|
#include "swift/AST/Types.h"
|
|
#include "swift/Basic/LLVM.h"
|
|
#include "llvm/ADT/SmallVector.h"
|
|
#include "llvm/IR/Attributes.h"
|
|
#include "llvm/IR/Intrinsics.h"
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
namespace llvm {
|
|
enum class AtomicOrdering;
|
|
}
|
|
|
|
namespace swift {
|
|
|
|
class ASTContext;
|
|
class Identifier;
|
|
class ValueDecl;
|
|
|
|
enum class BuiltinTypeKind : std::underlying_type<TypeKind>::type {
|
|
#define TYPE(id, parent)
|
|
#define BUILTIN_TYPE(id, parent) \
|
|
id = std::underlying_type<TypeKind>::type(TypeKind::id),
|
|
#include "swift/AST/TypeNodes.def"
|
|
};
|
|
|
|
/// Get the builtin type for the given name.
|
|
///
|
|
/// Returns a null type if the name is not a known builtin type name.
|
|
Type getBuiltinType(ASTContext &Context, StringRef Name);
|
|
|
|
/// OverloadedBuiltinKind - Whether and how a builtin is overloaded.
|
|
enum class OverloadedBuiltinKind : uint8_t {
|
|
/// The builtin is not overloaded.
|
|
None,
|
|
|
|
/// The builtin is overloaded over all integer types.
|
|
Integer,
|
|
|
|
/// The builtin is overloaded over all integer types and vectors of integers.
|
|
IntegerOrVector,
|
|
|
|
/// The builtin is overloaded over all integer types and the raw pointer type.
|
|
IntegerOrRawPointer,
|
|
|
|
/// The builtin is overloaded over all integer types, the raw pointer type,
|
|
/// and vectors of integers.
|
|
IntegerOrRawPointerOrVector,
|
|
|
|
/// The builtin is overloaded over all floating-point types.
|
|
Float,
|
|
|
|
/// The builtin is overloaded over all floating-point types and vectors of
|
|
/// floating-point types.
|
|
FloatOrVector,
|
|
|
|
/// The builtin has custom processing.
|
|
Special
|
|
};
|
|
|
|
/// BuiltinValueKind - The set of (possibly overloaded) builtin functions.
|
|
enum class BuiltinValueKind {
|
|
None = 0,
|
|
#define BUILTIN(Id, Name, Attrs) Id,
|
|
#include "swift/AST/Builtins.def"
|
|
};
|
|
|
|
/// Returns true if this is a polymorphic builtin that is only valid
|
|
/// in raw sil and thus must be resolved to have concrete types by the
|
|
/// time we are in canonical SIL.
|
|
bool isPolymorphicBuiltin(BuiltinValueKind Id);
|
|
|
|
/// Decode the type list of a builtin (e.g. mul_Int32) and return the base
|
|
/// name (e.g. "mul").
|
|
StringRef getBuiltinBaseName(ASTContext &C, StringRef Name,
|
|
SmallVectorImpl<Type> &Types);
|
|
|
|
/// Given an LLVM IR intrinsic name with argument types remove (e.g. like
|
|
/// "bswap") return the LLVM IR IntrinsicID for the intrinsic or not_intrinsic
|
|
/// (0) if the intrinsic name doesn't match anything.
|
|
llvm::Intrinsic::ID getLLVMIntrinsicID(StringRef Name);
|
|
|
|
/// Get the LLVM intrinsic ID that corresponds to the given builtin with
|
|
/// overflow.
|
|
llvm::Intrinsic::ID
|
|
getLLVMIntrinsicIDForBuiltinWithOverflow(BuiltinValueKind ID);
|
|
|
|
/// Create a ValueDecl for the builtin with the given name.
|
|
///
|
|
/// Returns null if the name does not identifier a known builtin value.
|
|
ValueDecl *getBuiltinValueDecl(ASTContext &Context, Identifier Name);
|
|
|
|
/// Returns the name of a builtin declaration given a builtin ID.
|
|
StringRef getBuiltinName(BuiltinValueKind ID);
|
|
|
|
/// The information identifying the builtin - its kind and types.
|
|
class BuiltinInfo {
|
|
public:
|
|
BuiltinValueKind ID;
|
|
SmallVector<Type, 4> Types;
|
|
bool isReadNone() const;
|
|
};
|
|
|
|
/// The information identifying the llvm intrinsic - its id and types.
|
|
class IntrinsicInfo {
|
|
mutable llvm::AttributeList Attrs =
|
|
llvm::DenseMapInfo<llvm::AttributeList>::getEmptyKey();
|
|
public:
|
|
llvm::Intrinsic::ID ID;
|
|
SmallVector<Type, 4> Types;
|
|
bool hasAttribute(llvm::Attribute::AttrKind Kind) const;
|
|
};
|
|
|
|
/// Turn a string like "release" into the LLVM enum.
|
|
llvm::AtomicOrdering decodeLLVMAtomicOrdering(StringRef O);
|
|
|
|
}
|
|
|
|
#endif
|