Skip to content

Commit b3ca057

Browse files
authored
Merge pull request #21006 from jrose-apple/intrinsick-day
Cache LLVM attributes for intrinsics
2 parents ea37177 + 7e8c007 commit b3ca057

File tree

3 files changed

+21
-23
lines changed

3 files changed

+21
-23
lines changed

include/swift/AST/Builtins.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@ StringRef getBuiltinBaseName(ASTContext &C, StringRef Name,
8080
SmallVectorImpl<Type> &Types);
8181

8282
/// Given an LLVM IR intrinsic name with argument types remove (e.g. like
83-
/// "bswap") return the LLVM IR IntrinsicID for the intrinsic or 0 if the
84-
/// intrinsic name doesn't match anything.
85-
unsigned getLLVMIntrinsicID(StringRef Name);
83+
/// "bswap") return the LLVM IR IntrinsicID for the intrinsic or not_intrinsic
84+
/// (0) if the intrinsic name doesn't match anything.
85+
llvm::Intrinsic::ID getLLVMIntrinsicID(StringRef Name);
8686

8787
/// Get the LLVM intrinsic ID that corresponds to the given builtin with
8888
/// overflow.
@@ -107,7 +107,10 @@ class BuiltinInfo {
107107
};
108108

109109
/// \brief The information identifying the llvm intrinsic - its id and types.
110-
struct IntrinsicInfo {
110+
class IntrinsicInfo {
111+
mutable llvm::AttributeList Attrs =
112+
llvm::DenseMapInfo<llvm::AttributeList>::getEmptyKey();
113+
public:
111114
llvm::Intrinsic::ID ID;
112115
SmallVector<Type, 4> Types;
113116
bool hasAttribute(llvm::Attribute::AttrKind Kind) const;

lib/AST/Builtins.cpp

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,12 @@ bool BuiltinInfo::isReadNone() const {
4545
}
4646

4747
bool IntrinsicInfo::hasAttribute(llvm::Attribute::AttrKind Kind) const {
48-
// FIXME: We should not be relying on the global LLVM context.
49-
llvm::AttributeList attrs =
50-
llvm::Intrinsic::getAttributes(getGlobalLLVMContext(), ID);
51-
return (attrs.hasAttribute(llvm::AttributeList::FunctionIndex, Kind));
48+
using DenseMapInfo = llvm::DenseMapInfo<llvm::AttributeList>;
49+
if (DenseMapInfo::isEqual(Attrs, DenseMapInfo::getEmptyKey())) {
50+
// FIXME: We should not be relying on the global LLVM context.
51+
Attrs = llvm::Intrinsic::getAttributes(getGlobalLLVMContext(), ID);
52+
}
53+
return Attrs.hasFnAttribute(Kind);
5254
}
5355

5456
Type swift::getBuiltinType(ASTContext &Context, StringRef Name) {
@@ -1223,10 +1225,7 @@ static const char *const IntrinsicNameTable[] = {
12231225
#include "llvm/IR/IntrinsicImpl.inc"
12241226
#undef GET_INTRINSIC_TARGET_DATA
12251227

1226-
/// getLLVMIntrinsicID - Given an LLVM IR intrinsic name with argument types
1227-
/// removed (e.g. like "bswap") return the LLVM IR IntrinsicID for the intrinsic
1228-
/// or 0 if the intrinsic name doesn't match anything.
1229-
unsigned swift::getLLVMIntrinsicID(StringRef InName) {
1228+
llvm::Intrinsic::ID swift::getLLVMIntrinsicID(StringRef InName) {
12301229
using namespace llvm;
12311230

12321231
// Swift intrinsic names start with int_.
@@ -1319,12 +1318,11 @@ static Type DecodeIntrinsicType(ArrayRef<llvm::Intrinsic::IITDescriptor> &Table,
13191318

13201319
/// \returns true on success, false on failure.
13211320
static bool
1322-
getSwiftFunctionTypeForIntrinsic(unsigned iid, ArrayRef<Type> TypeArgs,
1321+
getSwiftFunctionTypeForIntrinsic(llvm::Intrinsic::ID ID,
1322+
ArrayRef<Type> TypeArgs,
13231323
ASTContext &Context,
13241324
SmallVectorImpl<Type> &ArgElts,
1325-
Type &ResultTy, FunctionType::ExtInfo &Info) {
1326-
llvm::Intrinsic::ID ID = (llvm::Intrinsic::ID)iid;
1327-
1325+
Type &ResultTy) {
13281326
typedef llvm::Intrinsic::IITDescriptor IITDescriptor;
13291327
SmallVector<IITDescriptor, 8> Table;
13301328
getIntrinsicInfoTableEntries(ID, Table);
@@ -1346,7 +1344,6 @@ getSwiftFunctionTypeForIntrinsic(unsigned iid, ArrayRef<Type> TypeArgs,
13461344
// Translate LLVM function attributes to Swift function attributes.
13471345
llvm::AttributeList attrs =
13481346
llvm::Intrinsic::getAttributes(getGlobalLLVMContext(), ID);
1349-
Info = FunctionType::ExtInfo();
13501347
if (attrs.hasAttribute(llvm::AttributeList::FunctionIndex,
13511348
llvm::Attribute::NoReturn)) {
13521349
ResultTy = Context.getNeverType();
@@ -1437,13 +1434,11 @@ ValueDecl *swift::getBuiltinValueDecl(ASTContext &Context, Identifier Id) {
14371434

14381435
// If this is the name of an LLVM intrinsic, cons up a swift function with a
14391436
// type that matches the IR types.
1440-
if (unsigned ID = getLLVMIntrinsicID(OperationName)) {
1437+
if (llvm::Intrinsic::ID ID = getLLVMIntrinsicID(OperationName)) {
14411438
SmallVector<Type, 8> ArgElts;
14421439
Type ResultTy;
1443-
FunctionType::ExtInfo Info;
1444-
if (getSwiftFunctionTypeForIntrinsic(ID, Types, Context, ArgElts, ResultTy,
1445-
Info))
1446-
return getBuiltinFunction(Id, ArgElts, ResultTy, Info);
1440+
if (getSwiftFunctionTypeForIntrinsic(ID, Types, Context, ArgElts, ResultTy))
1441+
return getBuiltinFunction(Id, ArgElts, ResultTy);
14471442
}
14481443

14491444
// If this starts with fence, we have special suffixes to handle.

lib/SIL/SILModule.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ const IntrinsicInfo &SILModule::getIntrinsicInfo(Identifier ID) {
275275

276276
// Otherwise, lookup the ID and Type and store them in the map.
277277
StringRef NameRef = getBuiltinBaseName(getASTContext(), ID.str(), Info.Types);
278-
Info.ID = (llvm::Intrinsic::ID)getLLVMIntrinsicID(NameRef);
278+
Info.ID = getLLVMIntrinsicID(NameRef);
279279

280280
return Info;
281281
}

0 commit comments

Comments
 (0)