Skip to content
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
17 changes: 15 additions & 2 deletions include/swift/SIL/ApplySite.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,21 @@ class ApplySite {

/// Return the referenced function if the callee is a function_ref
/// instruction.
SILFunction *getReferencedFunction() const {
FOREACH_IMPL_RETURN(getReferencedFunction());
SILFunction *getReferencedFunctionOrNull() const {
FOREACH_IMPL_RETURN(getReferencedFunctionOrNull());
}

/// Return the referenced function if the callee is a function_ref like
/// instruction.
///
/// WARNING: This not necessarily the function that will be called at runtime.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you put a newline here before, WARNING.

/// If the callee is a (prev_)dynamic_function_ref the actual function called
/// might be different because it could be dynamically replaced at runtime.
///
/// If the client of this API wants to look at the content of the returned SIL
/// function it should call getReferencedFunctionOrNull() instead.
SILFunction *getInitiallyReferencedFunction() const {
FOREACH_IMPL_RETURN(getInitiallyReferencedFunction());
}

/// Should we optimize this call.
Expand Down
2 changes: 1 addition & 1 deletion include/swift/SIL/PatternMatch.h
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ struct Callee_match<SILFunction &> {
if (!AI)
return false;

return AI->getReferencedFunction() == &Fun;
return AI->getReferencedFunctionOrNull() == &Fun;
}
};

Expand Down
9 changes: 6 additions & 3 deletions include/swift/SIL/SILCloner.h
Original file line number Diff line number Diff line change
Expand Up @@ -950,7 +950,8 @@ SILCloner<ImplClass>::visitEndApplyInst(EndApplyInst *Inst) {
template<typename ImplClass>
void
SILCloner<ImplClass>::visitFunctionRefInst(FunctionRefInst *Inst) {
SILFunction *OpFunction = getOpFunction(Inst->getReferencedFunction());
SILFunction *OpFunction =
getOpFunction(Inst->getInitiallyReferencedFunction());
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));
recordClonedInstruction(Inst, getBuilder().createFunctionRef(
getOpLocation(Inst->getLoc()), OpFunction));
Expand All @@ -959,7 +960,8 @@ SILCloner<ImplClass>::visitFunctionRefInst(FunctionRefInst *Inst) {
template<typename ImplClass>
void
SILCloner<ImplClass>::visitDynamicFunctionRefInst(DynamicFunctionRefInst *Inst) {
SILFunction *OpFunction = getOpFunction(Inst->getReferencedFunction());
SILFunction *OpFunction =
getOpFunction(Inst->getInitiallyReferencedFunction());
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));
recordClonedInstruction(Inst, getBuilder().createDynamicFunctionRef(
getOpLocation(Inst->getLoc()), OpFunction));
Expand All @@ -968,7 +970,8 @@ SILCloner<ImplClass>::visitDynamicFunctionRefInst(DynamicFunctionRefInst *Inst)
template <typename ImplClass>
void SILCloner<ImplClass>::visitPreviousDynamicFunctionRefInst(
PreviousDynamicFunctionRefInst *Inst) {
SILFunction *OpFunction = getOpFunction(Inst->getReferencedFunction());
SILFunction *OpFunction =
getOpFunction(Inst->getInitiallyReferencedFunction());
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));
recordClonedInstruction(Inst, getBuilder().createPreviousDynamicFunctionRef(
getOpLocation(Inst->getLoc()), OpFunction));
Expand Down
46 changes: 41 additions & 5 deletions include/swift/SIL/SILInstruction.h
Original file line number Diff line number Diff line change
Expand Up @@ -1844,9 +1844,26 @@ class ApplyInstBase<Impl, Base, false> : public Base {
bool isCalleeDynamicallyReplaceable() const;

/// Gets the referenced function if the callee is a function_ref instruction.
SILFunction *getReferencedFunction() const {
/// Returns null if the callee is dynamic or a (prev_)dynamic_function_ref
/// instruction.
SILFunction *getReferencedFunctionOrNull() const {
if (auto *FRI = dyn_cast<FunctionRefBaseInst>(getCallee()))
return FRI->getReferencedFunction();
return FRI->getReferencedFunctionOrNull();
return nullptr;
}

/// Return the referenced function if the callee is a function_ref like
/// instruction.
///
/// WARNING: This not necessarily the function that will be called at runtime.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same spelling issue here.

/// If the callee is a (prev_)dynamic_function_ref the actual function called
/// might be different because it could be dynamically replaced at runtime.
///
/// If the client of this API wants to look at the content of the returned SIL
/// function it should call getReferencedFunctionOrNull() instead.
SILFunction *getInitiallyReferencedFunction() const {
if (auto *FRI = dyn_cast<FunctionRefBaseInst>(getCallee()))
return FRI->getInitiallyReferencedFunction();
return nullptr;
}

Expand Down Expand Up @@ -2294,8 +2311,27 @@ class FunctionRefBaseInst : public LiteralInst {
public:
~FunctionRefBaseInst();

/// Return the referenced function.
SILFunction *getReferencedFunction() const { return f; }
/// Return the referenced function if this is a function_ref instruction and
/// therefore a client can rely on the dynamically called function being equal
/// to the returned value and null otherwise.
SILFunction *getReferencedFunctionOrNull() const {
auto kind = getKind();
if (kind == SILInstructionKind::FunctionRefInst)
return f;
assert(kind == SILInstructionKind::DynamicFunctionRefInst ||
kind == SILInstructionKind::PreviousDynamicFunctionRefInst);
return nullptr;
}

/// Return the initially referenced function.
///
/// WARNING: This not necessarily the function that will be called at runtime.
/// If the callee is a (prev_)dynamic_function_ref the actual function called
/// might be different because it could be dynamically replaced at runtime.
///
/// If the client of this API wants to look at the content of the returned SIL
/// function it should call getReferencedFunctionOrNull() instead.
SILFunction *getInitiallyReferencedFunction() const { return f; }

void dropReferencedFunction();

Expand Down Expand Up @@ -7805,7 +7841,7 @@ SILFunction *ApplyInstBase<Impl, Base, false>::getCalleeFunction() const {
// previous_dynamic_function_ref as the target of those functions is not
// statically known.
if (auto *FRI = dyn_cast<FunctionRefInst>(Callee))
return FRI->getReferencedFunction();
return FRI->getReferencedFunctionOrNull();

if (auto *PAI = dyn_cast<PartialApplyInst>(Callee)) {
Callee = PAI->getCalleeOrigin();
Expand Down
2 changes: 1 addition & 1 deletion include/swift/SIL/TypeSubstCloner.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class TypeSubstCloner : public SILClonerWithScopes<ImplClass> {

if (!Cloner.Inlining) {
FunctionRefInst *FRI = dyn_cast<FunctionRefInst>(AI.getCallee());
if (FRI && FRI->getReferencedFunction() == AI.getFunction() &&
if (FRI && FRI->getInitiallyReferencedFunction() == AI.getFunction() &&
Subs == Cloner.SubsMap) {
// Handle recursions by replacing the apply to the callee with an
// apply to the newly specialized function, but only if substitutions
Expand Down
2 changes: 1 addition & 1 deletion lib/IRGen/AllocStackHoisting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ bool indicatesDynamicAvailabilityCheckUse(SILInstruction *I) {
return false;
if (Apply->hasSemantics("availability.osversion"))
return true;
auto *FunRef = Apply->getReferencedFunction();
auto *FunRef = Apply->getReferencedFunctionOrNull();
if (!FunRef)
return false;
if (FunRef->getName().equals("_swift_stdlib_operatingSystemVersion"))
Expand Down
2 changes: 1 addition & 1 deletion lib/IRGen/IRGenSIL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1842,7 +1842,7 @@ void IRGenSILFunction::visitSILBasicBlock(SILBasicBlock *BB) {
}

void IRGenSILFunction::visitFunctionRefBaseInst(FunctionRefBaseInst *i) {
auto fn = i->getReferencedFunction();
auto fn = i->getInitiallyReferencedFunction();

llvm::Constant *fnPtr = IGM.getAddrOfSILFunction(
fn, NotForDefinition, false /*isDynamicallyReplaceableImplementation*/,
Expand Down
4 changes: 2 additions & 2 deletions lib/IRGen/LoadableByAddress.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2790,7 +2790,7 @@ void LoadableByAddress::run() {
for (SILBasicBlock &BB : CurrF) {
for (SILInstruction &I : BB) {
if (auto *FRI = dyn_cast<FunctionRefBaseInst>(&I)) {
SILFunction *RefF = FRI->getReferencedFunction();
SILFunction *RefF = FRI->getInitiallyReferencedFunction();
if (modFuncs.count(RefF) != 0) {
// Go over the uses and add them to lists to modify
//
Expand Down Expand Up @@ -2895,7 +2895,7 @@ void LoadableByAddress::run() {
// They just contain a pointer to the function
// The pointer does not change
for (auto *instr : funcRefs) {
SILFunction *F = instr->getReferencedFunction();
SILFunction *F = instr->getInitiallyReferencedFunction();
SILBuilderWithScope refBuilder(instr);
SingleValueInstruction *newInstr =
refBuilder.createFunctionRef(instr->getLoc(), F, instr->getKind());
Expand Down
2 changes: 1 addition & 1 deletion lib/SIL/InstructionUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ SILValue swift::isPartialApplyOfReabstractionThunk(PartialApplyInst *PAI) {
PAI->getNumArguments() != 2)
return SILValue();

auto *Fun = PAI->getReferencedFunction();
auto *Fun = PAI->getReferencedFunctionOrNull();
if (!Fun)
return SILValue();

Expand Down
6 changes: 3 additions & 3 deletions lib/SIL/Linker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,17 +170,17 @@ void SILLinkerVisitor::visitPartialApplyInst(PartialApplyInst *PAI) {
}

void SILLinkerVisitor::visitFunctionRefInst(FunctionRefInst *FRI) {
maybeAddFunctionToWorklist(FRI->getReferencedFunction());
maybeAddFunctionToWorklist(FRI->getInitiallyReferencedFunction());
}

void SILLinkerVisitor::visitDynamicFunctionRefInst(
DynamicFunctionRefInst *FRI) {
maybeAddFunctionToWorklist(FRI->getReferencedFunction());
maybeAddFunctionToWorklist(FRI->getInitiallyReferencedFunction());
}

void SILLinkerVisitor::visitPreviousDynamicFunctionRefInst(
PreviousDynamicFunctionRefInst *FRI) {
maybeAddFunctionToWorklist(FRI->getReferencedFunction());
maybeAddFunctionToWorklist(FRI->getInitiallyReferencedFunction());
}

// Eagerly visiting all used conformances leads to a large blowup
Expand Down
6 changes: 3 additions & 3 deletions lib/SIL/MemAccessUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ AccessedStorage::Kind AccessedStorage::classify(SILValue base) {
return Global;
case ValueKind::ApplyInst: {
FullApplySite apply(cast<ApplyInst>(base));
if (auto *funcRef = apply.getReferencedFunction()) {
if (auto *funcRef = apply.getReferencedFunctionOrNull()) {
if (getVariableOfGlobalInit(funcRef))
return Global;
}
Expand Down Expand Up @@ -91,7 +91,7 @@ AccessedStorage::AccessedStorage(SILValue base, Kind kind) {
global = GAI->getReferencedGlobal();
else {
FullApplySite apply(cast<ApplyInst>(base));
auto *funcRef = apply.getReferencedFunction();
auto *funcRef = apply.getReferencedFunctionOrNull();
assert(funcRef);
global = getVariableOfGlobalInit(funcRef);
assert(global);
Expand Down Expand Up @@ -197,7 +197,7 @@ void AccessedStorage::dump() const { print(llvm::dbgs()); }
// module.
static bool isExternalGlobalAddressor(ApplyInst *AI) {
FullApplySite apply(AI);
auto *funcRef = apply.getReferencedFunction();
auto *funcRef = apply.getReferencedFunctionOrNull();
if (!funcRef)
return false;

Expand Down
2 changes: 1 addition & 1 deletion lib/SIL/SILGlobalVariable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ SILFunction *swift::getCalleeOfOnceCall(BuiltinInst *BI) {
"Expected C function representation!");

if (auto *FR = dyn_cast<FunctionRefInst>(Callee))
return FR->getReferencedFunction();
return FR->getReferencedFunctionOrNull();

return nullptr;
}
Expand Down
11 changes: 7 additions & 4 deletions lib/SIL/SILInstruction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ void SILInstruction::dropAllReferences() {
// If we have a function ref inst, we need to especially drop its function
// argument so that it gets a proper ref decrement.
if (auto *FRI = dyn_cast<FunctionRefBaseInst>(this)) {
if (!FRI->getReferencedFunction())
if (!FRI->getInitiallyReferencedFunction())
return;
FRI->dropReferencedFunction();
return;
Expand Down Expand Up @@ -465,16 +465,19 @@ namespace {

bool visitFunctionRefInst(const FunctionRefInst *RHS) {
auto *X = cast<FunctionRefInst>(LHS);
return X->getReferencedFunction() == RHS->getReferencedFunction();
return X->getInitiallyReferencedFunction() ==
RHS->getInitiallyReferencedFunction();
}
bool visitDynamicFunctionRefInst(const DynamicFunctionRefInst *RHS) {
auto *X = cast<DynamicFunctionRefInst>(LHS);
return X->getReferencedFunction() == RHS->getReferencedFunction();
return X->getInitiallyReferencedFunction() ==
RHS->getInitiallyReferencedFunction();
}
bool visitPreviousDynamicFunctionRefInst(
const PreviousDynamicFunctionRefInst *RHS) {
auto *X = cast<PreviousDynamicFunctionRefInst>(LHS);
return X->getReferencedFunction() == RHS->getReferencedFunction();
return X->getInitiallyReferencedFunction() ==
RHS->getInitiallyReferencedFunction();
}

bool visitAllocGlobalInst(const AllocGlobalInst *RHS) {
Expand Down
8 changes: 4 additions & 4 deletions lib/SIL/SILInstructions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ BeginApplyInst::create(SILDebugLocation loc, SILValue callee,

bool swift::doesApplyCalleeHaveSemantics(SILValue callee, StringRef semantics) {
if (auto *FRI = dyn_cast<FunctionRefBaseInst>(callee))
if (auto *F = FRI->getReferencedFunction())
if (auto *F = FRI->getReferencedFunctionOrNull())
return F->hasSemanticsAttr(semantics);
return false;
}
Expand Down Expand Up @@ -573,14 +573,14 @@ FunctionRefBaseInst::FunctionRefBaseInst(SILInstructionKind Kind,
}

void FunctionRefBaseInst::dropReferencedFunction() {
if (auto *Function = getReferencedFunction())
if (auto *Function = getInitiallyReferencedFunction())
Function->decrementRefCount();
f = nullptr;
}

FunctionRefBaseInst::~FunctionRefBaseInst() {
if (getReferencedFunction())
getReferencedFunction()->decrementRefCount();
if (getInitiallyReferencedFunction())
getInitiallyReferencedFunction()->decrementRefCount();
}

FunctionRefInst::FunctionRefInst(SILDebugLocation Loc, SILFunction *F)
Expand Down
12 changes: 6 additions & 6 deletions lib/SIL/SILPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -834,15 +834,15 @@ class SILPrinter : public SILInstructionVisitor<SILPrinter> {
void print(const SILInstruction *I) {
if (auto *FRI = dyn_cast<FunctionRefInst>(I))
*this << " // function_ref "
<< demangleSymbol(FRI->getReferencedFunction()->getName())
<< demangleSymbol(FRI->getInitiallyReferencedFunction()->getName())
<< "\n";
else if (auto *FRI = dyn_cast<DynamicFunctionRefInst>(I))
*this << " // dynamic_function_ref "
<< demangleSymbol(FRI->getReferencedFunction()->getName())
<< demangleSymbol(FRI->getInitiallyReferencedFunction()->getName())
<< "\n";
else if (auto *FRI = dyn_cast<PreviousDynamicFunctionRefInst>(I))
*this << " // prev_dynamic_function_ref "
<< demangleSymbol(FRI->getReferencedFunction()->getName())
<< demangleSymbol(FRI->getInitiallyReferencedFunction()->getName())
<< "\n";

*this << " ";
Expand Down Expand Up @@ -1141,16 +1141,16 @@ class SILPrinter : public SILInstructionVisitor<SILPrinter> {
}

void visitFunctionRefInst(FunctionRefInst *FRI) {
FRI->getReferencedFunction()->printName(PrintState.OS);
FRI->getInitiallyReferencedFunction()->printName(PrintState.OS);
*this << " : " << FRI->getType();
}
void visitDynamicFunctionRefInst(DynamicFunctionRefInst *FRI) {
FRI->getReferencedFunction()->printName(PrintState.OS);
FRI->getInitiallyReferencedFunction()->printName(PrintState.OS);
*this << " : " << FRI->getType();
}
void
visitPreviousDynamicFunctionRefInst(PreviousDynamicFunctionRefInst *FRI) {
FRI->getReferencedFunction()->printName(PrintState.OS);
FRI->getInitiallyReferencedFunction()->printName(PrintState.OS);
*this << " : " << FRI->getType();
}

Expand Down
2 changes: 1 addition & 1 deletion lib/SIL/SILVerifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1642,7 +1642,7 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
// Note: in SingleFunction mode, we relax some of these checks because
// we may not have linked everything yet.

SILFunction *RefF = FRI->getReferencedFunction();
SILFunction *RefF = FRI->getInitiallyReferencedFunction();

if (isa<FunctionRefInst>(FRI))
require(
Expand Down
2 changes: 1 addition & 1 deletion lib/SILOptimizer/ARC/RCStateTransition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ static bool isAutoreleasePoolCall(SILInstruction *I) {
if (!AI)
return false;

auto *Fn = AI->getReferencedFunction();
auto *Fn = AI->getReferencedFunctionOrNull();
if (!Fn)
return false;

Expand Down
Loading