Skip to content

Commit e884b70

Browse files
authored
Merge pull request swiftlang#31113 from CodaFi/a-little-less-context-would-be-nice
Excise the Global LLVM Context
2 parents 9216489 + e2cab42 commit e884b70

File tree

9 files changed

+38
-29
lines changed

9 files changed

+38
-29
lines changed

include/swift/AST/ASTContext.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ namespace clang {
5050
class ObjCInterfaceDecl;
5151
}
5252

53+
namespace llvm {
54+
class LLVMContext;
55+
}
56+
5357
namespace swift {
5458
class AbstractFunctionDecl;
5559
class ASTContext;
@@ -949,6 +953,11 @@ class ASTContext final {
949953
GenericSignatureBuilder &&builder);
950954
friend class GenericSignatureBuilder;
951955

956+
private:
957+
friend class IntrinsicInfo;
958+
/// Retrieve an LLVMContext that is used for scratch space for intrinsic lookup.
959+
llvm::LLVMContext &getIntrinsicScratchContext() const;
960+
952961
public:
953962
/// Retrieve or create the stored generic signature builder for the given
954963
/// canonical generic signature and module.

include/swift/AST/Builtins.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ class IntrinsicInfo {
126126
public:
127127
llvm::Intrinsic::ID ID;
128128
SmallVector<Type, 4> Types;
129-
bool hasAttribute(llvm::Attribute::AttrKind Kind) const;
129+
const llvm::AttributeList &getOrCreateAttributes(ASTContext &Ctx) const;
130130
};
131131

132132
/// Turn a string like "release" into the LLVM enum.

include/swift/Subsystems.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
#include "swift/Basic/OptionSet.h"
2222
#include "swift/Basic/PrimarySpecificPaths.h"
2323
#include "swift/Basic/Version.h"
24-
#include "llvm/IR/LLVMContext.h"
2524
#include "llvm/ADT/ArrayRef.h"
2625
#include "llvm/ADT/Optional.h"
2726
#include "llvm/ADT/StringRef.h"

lib/AST/ASTContext.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
#include "llvm/ADT/Statistic.h"
5757
#include "llvm/ADT/StringMap.h"
5858
#include "llvm/ADT/StringSwitch.h"
59+
#include "llvm/IR/LLVMContext.h"
5960
#include "llvm/Support/Allocator.h"
6061
#include "llvm/Support/Compiler.h"
6162
#include <algorithm>
@@ -473,11 +474,15 @@ struct ASTContext::Implementation {
473474

474475
/// The IRGen specific SIL transforms that have been registered.
475476
SILTransformCtors IRGenSILPasses;
477+
478+
/// The scratch context used to allocate intrinsic data on behalf of \c swift::IntrinsicInfo
479+
std::unique_ptr<llvm::LLVMContext> IntrinsicScratchContext;
476480
};
477481

478482
ASTContext::Implementation::Implementation()
479483
: IdentifierTable(Allocator),
480-
TheSyntaxArena(new syntax::SyntaxArena()) {}
484+
TheSyntaxArena(new syntax::SyntaxArena()),
485+
IntrinsicScratchContext(new llvm::LLVMContext()) {}
481486
ASTContext::Implementation::~Implementation() {
482487
for (auto &cleanup : Cleanups)
483488
cleanup();
@@ -4792,3 +4797,8 @@ AutoDiffDerivativeFunctionIdentifier *AutoDiffDerivativeFunctionIdentifier::get(
47924797

47934798
return newNode;
47944799
}
4800+
4801+
llvm::LLVMContext &ASTContext::getIntrinsicScratchContext() const {
4802+
return *getImpl().IntrinsicScratchContext.get();
4803+
}
4804+

lib/AST/Builtins.cpp

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,11 @@
2626
#include "llvm/IR/Attributes.h"
2727
#include "llvm/IR/Instructions.h"
2828
#include "llvm/IR/Intrinsics.h"
29-
#include "llvm/IR/LLVMContext.h"
3029
#include "llvm/Support/ManagedStatic.h"
3130
#include <tuple>
3231

3332
using namespace swift;
3433

35-
// FIXME: The "Global Context" is a holdover from LLVM's removal of the global
36-
// context. LLVM's global context was used as scratch space to allocate some
37-
// attributes for the routines in this file, so we just made our own to work
38-
// around its removal. Really, it doesn't matter where we allocate these
39-
// attributes, but it's somewhat convenient for now that they're all in one
40-
// place instead of requiring a fresh context for every call. If we can
41-
// sequester ownership of a context (e.g. in SILModule) we could do away with
42-
// this, but there are callers of \c swift::getSwiftFunctionTypeForIntrinsic
43-
// all over the compiler.
44-
static llvm::ManagedStatic<llvm::LLVMContext> GlobalContext;
45-
static llvm::LLVMContext &getGlobalLLVMContext() { return *GlobalContext; }
46-
4734
struct BuiltinExtraInfoTy {
4835
const char *Attributes;
4936
};
@@ -58,13 +45,13 @@ bool BuiltinInfo::isReadNone() const {
5845
return strchr(BuiltinExtraInfo[(unsigned)ID].Attributes, 'n') != nullptr;
5946
}
6047

61-
bool IntrinsicInfo::hasAttribute(llvm::Attribute::AttrKind Kind) const {
48+
const llvm::AttributeList &
49+
IntrinsicInfo::getOrCreateAttributes(ASTContext &Ctx) const {
6250
using DenseMapInfo = llvm::DenseMapInfo<llvm::AttributeList>;
6351
if (DenseMapInfo::isEqual(Attrs, DenseMapInfo::getEmptyKey())) {
64-
// FIXME: We should not be relying on the global LLVM context.
65-
Attrs = llvm::Intrinsic::getAttributes(getGlobalLLVMContext(), ID);
52+
Attrs = llvm::Intrinsic::getAttributes(Ctx.getIntrinsicScratchContext(), ID);
6653
}
67-
return Attrs.hasFnAttribute(Kind);
54+
return Attrs;
6855
}
6956

7057
Type swift::getBuiltinType(ASTContext &Context, StringRef Name) {
@@ -1837,8 +1824,9 @@ getSwiftFunctionTypeForIntrinsic(llvm::Intrinsic::ID ID,
18371824
}
18381825

18391826
// Translate LLVM function attributes to Swift function attributes.
1840-
llvm::AttributeList attrs =
1841-
llvm::Intrinsic::getAttributes(getGlobalLLVMContext(), ID);
1827+
IntrinsicInfo II;
1828+
II.ID = ID;
1829+
auto attrs = II.getOrCreateAttributes(Context);
18421830
if (attrs.hasAttribute(llvm::AttributeList::FunctionIndex,
18431831
llvm::Attribute::NoReturn)) {
18441832
ResultTy = Context.getNeverType();

lib/LLVMPasses/LLVMMergeFunctions.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
#include "llvm/IR/IRBuilder.h"
4545
#include "llvm/IR/InlineAsm.h"
4646
#include "llvm/IR/Instructions.h"
47-
#include "llvm/IR/LLVMContext.h"
4847
#include "llvm/IR/Module.h"
4948
#include "llvm/IR/Operator.h"
5049
#include "llvm/IR/ValueHandle.h"

lib/SIL/IR/SILInstruction.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -978,13 +978,14 @@ SILInstruction::MemoryBehavior SILInstruction::getMemoryBehavior() const {
978978
// Handle LLVM intrinsic functions.
979979
const IntrinsicInfo &IInfo = BI->getIntrinsicInfo();
980980
if (IInfo.ID != llvm::Intrinsic::not_intrinsic) {
981+
auto IAttrs = IInfo.getOrCreateAttributes(getModule().getASTContext());
981982
// Read-only.
982-
if (IInfo.hasAttribute(llvm::Attribute::ReadOnly) &&
983-
IInfo.hasAttribute(llvm::Attribute::NoUnwind))
983+
if (IAttrs.hasFnAttribute(llvm::Attribute::ReadOnly) &&
984+
IAttrs.hasFnAttribute(llvm::Attribute::NoUnwind))
984985
return MemoryBehavior::MayRead;
985986
// Read-none?
986-
return IInfo.hasAttribute(llvm::Attribute::ReadNone) &&
987-
IInfo.hasAttribute(llvm::Attribute::NoUnwind)
987+
return IAttrs.hasFnAttribute(llvm::Attribute::ReadNone) &&
988+
IAttrs.hasFnAttribute(llvm::Attribute::NoUnwind)
988989
? MemoryBehavior::None
989990
: MemoryBehavior::MayHaveSideEffects;
990991
}

lib/SIL/IR/SILModule.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,9 @@ void SILModule::notifyDeleteHandlers(SILNode *node) {
646646
bool SILModule::isNoReturnBuiltinOrIntrinsic(Identifier Name) {
647647
const auto &IntrinsicInfo = getIntrinsicInfo(Name);
648648
if (IntrinsicInfo.ID != llvm::Intrinsic::not_intrinsic) {
649-
return IntrinsicInfo.hasAttribute(llvm::Attribute::NoReturn);
649+
return IntrinsicInfo
650+
.getOrCreateAttributes(getASTContext())
651+
.hasFnAttribute(llvm::Attribute::NoReturn);
650652
}
651653
const auto &BuiltinInfo = getBuiltinInfo(Name);
652654
switch (BuiltinInfo.ID) {

lib/SILOptimizer/Analysis/ARCAnalysis.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ static bool canApplyOfBuiltinUseNonTrivialValues(BuiltinInst *BInst) {
103103

104104
auto &II = BInst->getIntrinsicInfo();
105105
if (II.ID != llvm::Intrinsic::not_intrinsic) {
106-
if (II.hasAttribute(llvm::Attribute::ReadNone)) {
106+
auto attrs = II.getOrCreateAttributes(F->getASTContext());
107+
if (attrs.hasFnAttribute(llvm::Attribute::ReadNone)) {
107108
for (auto &Op : BInst->getAllOperands()) {
108109
if (!Op.get()->getType().isTrivial(*F)) {
109110
return true;

0 commit comments

Comments
 (0)