mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
As part of this I also had to change how we emit global_addr in SILGenLValue. Specifically, only for noncopyable types, we no longer emit a single global_addr at the beginning of the function (in a sense auto-CSEing) and instead always emit a new global_addr for each access. The reason why we do this is that otherwise, access base visitor will consider all accesses to the global to be for the same single access. In contrast, by always emitting the global_addr each time, we provide a new base for each access allowing us to emit the diagnostics that we want to. rdar://102794400
296 lines
11 KiB
C++
296 lines
11 KiB
C++
//===--- SILGenGlobalVariable.cpp - Lowering for global variables ---------===//
|
|
//
|
|
// 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
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "SILGenFunction.h"
|
|
#include "ExecutorBreadcrumb.h"
|
|
#include "ManagedValue.h"
|
|
#include "Scope.h"
|
|
#include "swift/AST/ASTMangler.h"
|
|
#include "swift/AST/GenericSignature.h"
|
|
#include "swift/SIL/FormalLinkage.h"
|
|
|
|
using namespace swift;
|
|
using namespace Lowering;
|
|
|
|
/// Get or create SILGlobalVariable for a given global VarDecl.
|
|
SILGlobalVariable *SILGenModule::getSILGlobalVariable(VarDecl *gDecl,
|
|
ForDefinition_t forDef) {
|
|
// First, get a mangled name for the declaration.
|
|
std::string mangledName;
|
|
|
|
{
|
|
auto SILGenName = gDecl->getAttrs().getAttribute<SILGenNameAttr>();
|
|
if (SILGenName && !SILGenName->Name.empty()) {
|
|
mangledName = SILGenName->Name.str();
|
|
} else {
|
|
Mangle::ASTMangler NewMangler;
|
|
mangledName = NewMangler.mangleGlobalVariableFull(gDecl);
|
|
}
|
|
}
|
|
|
|
// Get the linkage for SILGlobalVariable.
|
|
FormalLinkage formalLinkage;
|
|
if (gDecl->isResilient())
|
|
formalLinkage = FormalLinkage::Private;
|
|
else
|
|
formalLinkage = getDeclLinkage(gDecl);
|
|
auto silLinkage = getSILLinkage(formalLinkage, forDef);
|
|
|
|
// Check if it is already created, and update linkage if necessary.
|
|
if (auto gv = M.lookUpGlobalVariable(mangledName)) {
|
|
// Update the SILLinkage here if this is a definition.
|
|
if (forDef == ForDefinition) {
|
|
gv->setLinkage(silLinkage);
|
|
gv->setDeclaration(false);
|
|
}
|
|
return gv;
|
|
}
|
|
|
|
SILType silTy = SILType::getPrimitiveObjectType(
|
|
M.Types.getLoweredTypeOfGlobal(gDecl));
|
|
|
|
auto *silGlobal = SILGlobalVariable::create(M, silLinkage, IsNotSerialized,
|
|
mangledName, silTy,
|
|
None, gDecl);
|
|
silGlobal->setDeclaration(!forDef);
|
|
|
|
return silGlobal;
|
|
}
|
|
|
|
ManagedValue
|
|
SILGenFunction::emitGlobalVariableRef(SILLocation loc, VarDecl *var,
|
|
Optional<ActorIsolation> actorIso) {
|
|
assert(!VarLocs.count(var));
|
|
|
|
if (var->isLazilyInitializedGlobal()) {
|
|
// Call the global accessor to get the variable's address.
|
|
SILFunction *accessorFn = SGM.getFunction(
|
|
SILDeclRef(var, SILDeclRef::Kind::GlobalAccessor),
|
|
NotForDefinition);
|
|
SILValue accessor = B.createFunctionRefFor(loc, accessorFn);
|
|
|
|
// The accessor to obtain a global's address may need to initialize the
|
|
// variable first. So, we must call this accessor with the same
|
|
// isolation that the variable itself requires during access.
|
|
ExecutorBreadcrumb prevExecutor = emitHopToTargetActor(loc, actorIso,
|
|
/*base=*/None);
|
|
|
|
SILValue addr = B.createApply(loc, accessor, SubstitutionMap(), {});
|
|
|
|
// FIXME: often right after this, we will again hop to the actor to
|
|
// read from this address. it would be better to merge these two hops
|
|
// pairs of hops together. Alternatively, teaching optimizations to
|
|
// expand the scope of two nearly-adjacent pairs would be good.
|
|
prevExecutor.emit(*this, loc); // hop back after call.
|
|
|
|
// FIXME: It'd be nice if the result of the accessor was natively an
|
|
// address.
|
|
addr = B.createPointerToAddress(
|
|
loc, addr, getLoweredType(var->getInterfaceType()).getAddressType(),
|
|
/*isStrict*/ true, /*isInvariant*/ false);
|
|
return ManagedValue::forLValue(addr);
|
|
}
|
|
|
|
// Global variables can be accessed directly with global_addr. If we have a
|
|
// noncopyable type, just emit the global_addr so each individual access has
|
|
// its own base projection. This is important so that the checker can
|
|
// distinguish in between different accesses to the same global.
|
|
auto *silG = SGM.getSILGlobalVariable(var, NotForDefinition);
|
|
if (silG->getLoweredType().isMoveOnly()) {
|
|
SILValue addr = B.createGlobalAddr(var, silG);
|
|
return ManagedValue::forLValue(addr);
|
|
}
|
|
|
|
// If we have a copyable type, emit this instruction into the prolog of the
|
|
// function so we can memoize/CSE it via the VarLocs map.
|
|
auto *entryBB = &*getFunction().begin();
|
|
SILGenBuilder prologueB(*this, entryBB, entryBB->begin());
|
|
prologueB.setTrackingList(B.getTrackingList());
|
|
|
|
SILValue addr = prologueB.createGlobalAddr(var, silG);
|
|
|
|
VarLocs[var] = SILGenFunction::VarLoc::get(addr);
|
|
return ManagedValue::forLValue(addr);
|
|
}
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
// Global initialization
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
namespace {
|
|
|
|
/// A visitor for traversing a pattern, creating
|
|
/// global accessor functions for all of the global variables declared inside.
|
|
struct GenGlobalAccessors : public PatternVisitor<GenGlobalAccessors>
|
|
{
|
|
/// The module generator.
|
|
SILGenModule &SGM;
|
|
/// The Builtin.once token guarding the global initialization.
|
|
SILGlobalVariable *OnceToken;
|
|
/// The function containing the initialization code.
|
|
SILFunction *OnceFunc;
|
|
|
|
/// A reference to the Builtin.once declaration.
|
|
FuncDecl *BuiltinOnceDecl;
|
|
|
|
GenGlobalAccessors(SILGenModule &SGM,
|
|
SILGlobalVariable *OnceToken,
|
|
SILFunction *OnceFunc)
|
|
: SGM(SGM), OnceToken(OnceToken), OnceFunc(OnceFunc)
|
|
{
|
|
// Find Builtin.once.
|
|
auto &C = SGM.M.getASTContext();
|
|
SmallVector<ValueDecl*, 2> found;
|
|
C.TheBuiltinModule->lookupValue(C.getIdentifier("once"),
|
|
NLKind::QualifiedLookup, found);
|
|
|
|
assert(found.size() == 1 && "didn't find Builtin.once?!");
|
|
|
|
BuiltinOnceDecl = cast<FuncDecl>(found[0]);
|
|
}
|
|
|
|
// Walk through non-binding patterns.
|
|
void visitParenPattern(ParenPattern *P) {
|
|
return visit(P->getSubPattern());
|
|
}
|
|
void visitTypedPattern(TypedPattern *P) {
|
|
return visit(P->getSubPattern());
|
|
}
|
|
void visitBindingPattern(BindingPattern *P) {
|
|
return visit(P->getSubPattern());
|
|
}
|
|
void visitTuplePattern(TuplePattern *P) {
|
|
for (auto &elt : P->getElements())
|
|
visit(elt.getPattern());
|
|
}
|
|
void visitAnyPattern(AnyPattern *P) {}
|
|
|
|
// When we see a variable binding, emit its global accessor.
|
|
void visitNamedPattern(NamedPattern *P) {
|
|
SGM.emitGlobalAccessor(P->getDecl(), OnceToken, OnceFunc);
|
|
}
|
|
|
|
#define INVALID_PATTERN(Id, Parent) \
|
|
void visit##Id##Pattern(Id##Pattern *) { \
|
|
llvm_unreachable("pattern not valid in argument or var binding"); \
|
|
}
|
|
#define PATTERN(Id, Parent)
|
|
#define REFUTABLE_PATTERN(Id, Parent) INVALID_PATTERN(Id, Parent)
|
|
#include "swift/AST/PatternNodes.def"
|
|
#undef INVALID_PATTERN
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
/// Emit a global initialization.
|
|
void SILGenModule::emitGlobalInitialization(PatternBindingDecl *pd,
|
|
unsigned pbdEntry) {
|
|
// Generic and dynamic static properties require lazy initialization, which
|
|
// isn't implemented yet.
|
|
if (pd->isStatic()) {
|
|
assert(!pd->getDeclContext()->isGenericContext()
|
|
|| pd->getDeclContext()->getGenericSignatureOfContext()
|
|
->areAllParamsConcrete());
|
|
}
|
|
|
|
Mangle::ASTMangler TokenMangler;
|
|
std::string onceTokenBuffer = TokenMangler.mangleGlobalInit(pd, pbdEntry,
|
|
false);
|
|
|
|
auto onceTy = BuiltinIntegerType::getWordType(M.getASTContext());
|
|
auto onceSILTy
|
|
= SILType::getPrimitiveObjectType(onceTy->getCanonicalType());
|
|
|
|
// TODO: include the module in the onceToken's name mangling.
|
|
// Then we can make it fragile.
|
|
auto onceToken = SILGlobalVariable::create(M, SILLinkage::Private,
|
|
IsNotSerialized,
|
|
onceTokenBuffer, onceSILTy);
|
|
onceToken->setDeclaration(false);
|
|
|
|
// Emit the initialization code into a function.
|
|
Mangle::ASTMangler FuncMangler;
|
|
std::string onceFuncBuffer = FuncMangler.mangleGlobalInit(pd, pbdEntry,
|
|
true);
|
|
|
|
SILFunction *onceFunc = emitLazyGlobalInitializer(onceFuncBuffer, pd,
|
|
pbdEntry);
|
|
|
|
// Generate accessor functions for all of the declared variables, which
|
|
// Builtin.once the lazy global initializer we just generated then return
|
|
// the address of the individual variable.
|
|
GenGlobalAccessors(*this, onceToken, onceFunc)
|
|
.visit(pd->getPattern(pbdEntry));
|
|
}
|
|
|
|
void SILGenFunction::emitLazyGlobalInitializer(PatternBindingDecl *binding,
|
|
unsigned pbdEntry) {
|
|
MagicFunctionName = SILGenModule::getMagicFunctionName(binding->getDeclContext());
|
|
|
|
// Add unused context pointer argument required to pass to `Builtin.once`
|
|
SILBasicBlock &entry = *F.begin();
|
|
SILType rawPointerSILTy =
|
|
getLoweredLoadableType(getASTContext().TheRawPointerType);
|
|
entry.createFunctionArgument(rawPointerSILTy);
|
|
|
|
{
|
|
Scope scope(Cleanups, binding);
|
|
|
|
// Emit the initialization sequence.
|
|
emitPatternBinding(binding, pbdEntry);
|
|
}
|
|
|
|
// Return void.
|
|
auto ret = emitEmptyTuple(binding);
|
|
B.createReturn(ImplicitReturnLocation(binding), ret);
|
|
}
|
|
|
|
static void emitOnceCall(SILGenFunction &SGF, VarDecl *global,
|
|
SILGlobalVariable *onceToken, SILFunction *onceFunc) {
|
|
SILType rawPointerSILTy
|
|
= SGF.getLoweredLoadableType(SGF.getASTContext().TheRawPointerType);
|
|
|
|
// Emit a reference to the global token.
|
|
SILValue onceTokenAddr = SGF.B.createGlobalAddr(global, onceToken);
|
|
onceTokenAddr = SGF.B.createAddressToPointer(global, onceTokenAddr,
|
|
rawPointerSILTy,
|
|
/*needsStackProtection=*/ false);
|
|
|
|
// Emit a reference to the function to execute.
|
|
SILValue onceFuncRef = SGF.B.createFunctionRefFor(global, onceFunc);
|
|
|
|
// Call Builtin.once.
|
|
SILValue onceArgs[] = {onceTokenAddr, onceFuncRef};
|
|
SGF.B.createBuiltin(global, SGF.getASTContext().getIdentifier("once"),
|
|
SGF.SGM.Types.getEmptyTupleType(), {}, onceArgs);
|
|
}
|
|
|
|
void SILGenFunction::emitGlobalAccessor(VarDecl *global,
|
|
SILGlobalVariable *onceToken,
|
|
SILFunction *onceFunc) {
|
|
emitOnceCall(*this, global, onceToken, onceFunc);
|
|
|
|
// Return the address of the global variable.
|
|
// FIXME: It'd be nice to be able to return a SIL address directly.
|
|
auto *silG = SGM.getSILGlobalVariable(global, NotForDefinition);
|
|
SILValue addr = B.createGlobalAddr(global, silG);
|
|
|
|
SILType rawPointerSILTy
|
|
= getLoweredLoadableType(getASTContext().TheRawPointerType);
|
|
addr = B.createAddressToPointer(global, addr, rawPointerSILTy,
|
|
/*needsStackProtection=*/ false);
|
|
auto *ret = B.createReturn(global, addr);
|
|
(void)ret;
|
|
assert(ret->getDebugScope() && "instruction without scope");
|
|
}
|
|
|