Skip to content

Commit 8d1fabd

Browse files
Merge pull request #25073 from aschwaighofer/sil_getReferencedFunctionOrNull_getInitialReferencedFunction
SIL: Replace uses of getReferencedFunction() by getReferencedFunction…
2 parents 9d9fb85 + 57362a8 commit 8d1fabd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+236
-158
lines changed

include/swift/SIL/ApplySite.h

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,21 @@ class ApplySite {
143143

144144
/// Return the referenced function if the callee is a function_ref
145145
/// instruction.
146-
SILFunction *getReferencedFunction() const {
147-
FOREACH_IMPL_RETURN(getReferencedFunction());
146+
SILFunction *getReferencedFunctionOrNull() const {
147+
FOREACH_IMPL_RETURN(getReferencedFunctionOrNull());
148+
}
149+
150+
/// Return the referenced function if the callee is a function_ref like
151+
/// instruction.
152+
///
153+
/// WARNING: This not necessarily the function that will be called at runtime.
154+
/// If the callee is a (prev_)dynamic_function_ref the actual function called
155+
/// might be different because it could be dynamically replaced at runtime.
156+
///
157+
/// If the client of this API wants to look at the content of the returned SIL
158+
/// function it should call getReferencedFunctionOrNull() instead.
159+
SILFunction *getInitiallyReferencedFunction() const {
160+
FOREACH_IMPL_RETURN(getInitiallyReferencedFunction());
148161
}
149162

150163
/// Should we optimize this call.

include/swift/SIL/PatternMatch.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ struct Callee_match<SILFunction &> {
518518
if (!AI)
519519
return false;
520520

521-
return AI->getReferencedFunction() == &Fun;
521+
return AI->getReferencedFunctionOrNull() == &Fun;
522522
}
523523
};
524524

include/swift/SIL/SILCloner.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -950,7 +950,8 @@ SILCloner<ImplClass>::visitEndApplyInst(EndApplyInst *Inst) {
950950
template<typename ImplClass>
951951
void
952952
SILCloner<ImplClass>::visitFunctionRefInst(FunctionRefInst *Inst) {
953-
SILFunction *OpFunction = getOpFunction(Inst->getReferencedFunction());
953+
SILFunction *OpFunction =
954+
getOpFunction(Inst->getInitiallyReferencedFunction());
954955
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));
955956
recordClonedInstruction(Inst, getBuilder().createFunctionRef(
956957
getOpLocation(Inst->getLoc()), OpFunction));
@@ -959,7 +960,8 @@ SILCloner<ImplClass>::visitFunctionRefInst(FunctionRefInst *Inst) {
959960
template<typename ImplClass>
960961
void
961962
SILCloner<ImplClass>::visitDynamicFunctionRefInst(DynamicFunctionRefInst *Inst) {
962-
SILFunction *OpFunction = getOpFunction(Inst->getReferencedFunction());
963+
SILFunction *OpFunction =
964+
getOpFunction(Inst->getInitiallyReferencedFunction());
963965
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));
964966
recordClonedInstruction(Inst, getBuilder().createDynamicFunctionRef(
965967
getOpLocation(Inst->getLoc()), OpFunction));
@@ -968,7 +970,8 @@ SILCloner<ImplClass>::visitDynamicFunctionRefInst(DynamicFunctionRefInst *Inst)
968970
template <typename ImplClass>
969971
void SILCloner<ImplClass>::visitPreviousDynamicFunctionRefInst(
970972
PreviousDynamicFunctionRefInst *Inst) {
971-
SILFunction *OpFunction = getOpFunction(Inst->getReferencedFunction());
973+
SILFunction *OpFunction =
974+
getOpFunction(Inst->getInitiallyReferencedFunction());
972975
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));
973976
recordClonedInstruction(Inst, getBuilder().createPreviousDynamicFunctionRef(
974977
getOpLocation(Inst->getLoc()), OpFunction));

include/swift/SIL/SILInstruction.h

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1844,9 +1844,26 @@ class ApplyInstBase<Impl, Base, false> : public Base {
18441844
bool isCalleeDynamicallyReplaceable() const;
18451845

18461846
/// Gets the referenced function if the callee is a function_ref instruction.
1847-
SILFunction *getReferencedFunction() const {
1847+
/// Returns null if the callee is dynamic or a (prev_)dynamic_function_ref
1848+
/// instruction.
1849+
SILFunction *getReferencedFunctionOrNull() const {
18481850
if (auto *FRI = dyn_cast<FunctionRefBaseInst>(getCallee()))
1849-
return FRI->getReferencedFunction();
1851+
return FRI->getReferencedFunctionOrNull();
1852+
return nullptr;
1853+
}
1854+
1855+
/// Return the referenced function if the callee is a function_ref like
1856+
/// instruction.
1857+
///
1858+
/// WARNING: This not necessarily the function that will be called at runtime.
1859+
/// If the callee is a (prev_)dynamic_function_ref the actual function called
1860+
/// might be different because it could be dynamically replaced at runtime.
1861+
///
1862+
/// If the client of this API wants to look at the content of the returned SIL
1863+
/// function it should call getReferencedFunctionOrNull() instead.
1864+
SILFunction *getInitiallyReferencedFunction() const {
1865+
if (auto *FRI = dyn_cast<FunctionRefBaseInst>(getCallee()))
1866+
return FRI->getInitiallyReferencedFunction();
18501867
return nullptr;
18511868
}
18521869

@@ -2294,8 +2311,27 @@ class FunctionRefBaseInst : public LiteralInst {
22942311
public:
22952312
~FunctionRefBaseInst();
22962313

2297-
/// Return the referenced function.
2298-
SILFunction *getReferencedFunction() const { return f; }
2314+
/// Return the referenced function if this is a function_ref instruction and
2315+
/// therefore a client can rely on the dynamically called function being equal
2316+
/// to the returned value and null otherwise.
2317+
SILFunction *getReferencedFunctionOrNull() const {
2318+
auto kind = getKind();
2319+
if (kind == SILInstructionKind::FunctionRefInst)
2320+
return f;
2321+
assert(kind == SILInstructionKind::DynamicFunctionRefInst ||
2322+
kind == SILInstructionKind::PreviousDynamicFunctionRefInst);
2323+
return nullptr;
2324+
}
2325+
2326+
/// Return the initially referenced function.
2327+
///
2328+
/// WARNING: This not necessarily the function that will be called at runtime.
2329+
/// If the callee is a (prev_)dynamic_function_ref the actual function called
2330+
/// might be different because it could be dynamically replaced at runtime.
2331+
///
2332+
/// If the client of this API wants to look at the content of the returned SIL
2333+
/// function it should call getReferencedFunctionOrNull() instead.
2334+
SILFunction *getInitiallyReferencedFunction() const { return f; }
22992335

23002336
void dropReferencedFunction();
23012337

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

78107846
if (auto *PAI = dyn_cast<PartialApplyInst>(Callee)) {
78117847
Callee = PAI->getCalleeOrigin();

include/swift/SIL/TypeSubstCloner.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class TypeSubstCloner : public SILClonerWithScopes<ImplClass> {
6969

7070
if (!Cloner.Inlining) {
7171
FunctionRefInst *FRI = dyn_cast<FunctionRefInst>(AI.getCallee());
72-
if (FRI && FRI->getReferencedFunction() == AI.getFunction() &&
72+
if (FRI && FRI->getInitiallyReferencedFunction() == AI.getFunction() &&
7373
Subs == Cloner.SubsMap) {
7474
// Handle recursions by replacing the apply to the callee with an
7575
// apply to the newly specialized function, but only if substitutions

lib/IRGen/AllocStackHoisting.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ bool indicatesDynamicAvailabilityCheckUse(SILInstruction *I) {
352352
return false;
353353
if (Apply->hasSemantics("availability.osversion"))
354354
return true;
355-
auto *FunRef = Apply->getReferencedFunction();
355+
auto *FunRef = Apply->getReferencedFunctionOrNull();
356356
if (!FunRef)
357357
return false;
358358
if (FunRef->getName().equals("_swift_stdlib_operatingSystemVersion"))

lib/IRGen/IRGenSIL.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1842,7 +1842,7 @@ void IRGenSILFunction::visitSILBasicBlock(SILBasicBlock *BB) {
18421842
}
18431843

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

18471847
llvm::Constant *fnPtr = IGM.getAddrOfSILFunction(
18481848
fn, NotForDefinition, false /*isDynamicallyReplaceableImplementation*/,

lib/IRGen/LoadableByAddress.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2790,7 +2790,7 @@ void LoadableByAddress::run() {
27902790
for (SILBasicBlock &BB : CurrF) {
27912791
for (SILInstruction &I : BB) {
27922792
if (auto *FRI = dyn_cast<FunctionRefBaseInst>(&I)) {
2793-
SILFunction *RefF = FRI->getReferencedFunction();
2793+
SILFunction *RefF = FRI->getInitiallyReferencedFunction();
27942794
if (modFuncs.count(RefF) != 0) {
27952795
// Go over the uses and add them to lists to modify
27962796
//
@@ -2895,7 +2895,7 @@ void LoadableByAddress::run() {
28952895
// They just contain a pointer to the function
28962896
// The pointer does not change
28972897
for (auto *instr : funcRefs) {
2898-
SILFunction *F = instr->getReferencedFunction();
2898+
SILFunction *F = instr->getInitiallyReferencedFunction();
28992899
SILBuilderWithScope refBuilder(instr);
29002900
SingleValueInstruction *newInstr =
29012901
refBuilder.createFunctionRef(instr->getLoc(), F, instr->getKind());

lib/SIL/InstructionUtils.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ SILValue swift::isPartialApplyOfReabstractionThunk(PartialApplyInst *PAI) {
358358
PAI->getNumArguments() != 2)
359359
return SILValue();
360360

361-
auto *Fun = PAI->getReferencedFunction();
361+
auto *Fun = PAI->getReferencedFunctionOrNull();
362362
if (!Fun)
363363
return SILValue();
364364

lib/SIL/Linker.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,17 +170,17 @@ void SILLinkerVisitor::visitPartialApplyInst(PartialApplyInst *PAI) {
170170
}
171171

172172
void SILLinkerVisitor::visitFunctionRefInst(FunctionRefInst *FRI) {
173-
maybeAddFunctionToWorklist(FRI->getReferencedFunction());
173+
maybeAddFunctionToWorklist(FRI->getInitiallyReferencedFunction());
174174
}
175175

176176
void SILLinkerVisitor::visitDynamicFunctionRefInst(
177177
DynamicFunctionRefInst *FRI) {
178-
maybeAddFunctionToWorklist(FRI->getReferencedFunction());
178+
maybeAddFunctionToWorklist(FRI->getInitiallyReferencedFunction());
179179
}
180180

181181
void SILLinkerVisitor::visitPreviousDynamicFunctionRefInst(
182182
PreviousDynamicFunctionRefInst *FRI) {
183-
maybeAddFunctionToWorklist(FRI->getReferencedFunction());
183+
maybeAddFunctionToWorklist(FRI->getInitiallyReferencedFunction());
184184
}
185185

186186
// Eagerly visiting all used conformances leads to a large blowup

lib/SIL/MemAccessUtils.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ AccessedStorage::Kind AccessedStorage::classify(SILValue base) {
3333
return Global;
3434
case ValueKind::ApplyInst: {
3535
FullApplySite apply(cast<ApplyInst>(base));
36-
if (auto *funcRef = apply.getReferencedFunction()) {
36+
if (auto *funcRef = apply.getReferencedFunctionOrNull()) {
3737
if (getVariableOfGlobalInit(funcRef))
3838
return Global;
3939
}
@@ -91,7 +91,7 @@ AccessedStorage::AccessedStorage(SILValue base, Kind kind) {
9191
global = GAI->getReferencedGlobal();
9292
else {
9393
FullApplySite apply(cast<ApplyInst>(base));
94-
auto *funcRef = apply.getReferencedFunction();
94+
auto *funcRef = apply.getReferencedFunctionOrNull();
9595
assert(funcRef);
9696
global = getVariableOfGlobalInit(funcRef);
9797
assert(global);
@@ -197,7 +197,7 @@ void AccessedStorage::dump() const { print(llvm::dbgs()); }
197197
// module.
198198
static bool isExternalGlobalAddressor(ApplyInst *AI) {
199199
FullApplySite apply(AI);
200-
auto *funcRef = apply.getReferencedFunction();
200+
auto *funcRef = apply.getReferencedFunctionOrNull();
201201
if (!funcRef)
202202
return false;
203203

lib/SIL/SILGlobalVariable.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ SILFunction *swift::getCalleeOfOnceCall(BuiltinInst *BI) {
233233
"Expected C function representation!");
234234

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

238238
return nullptr;
239239
}

lib/SIL/SILInstruction.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ void SILInstruction::dropAllReferences() {
158158
// If we have a function ref inst, we need to especially drop its function
159159
// argument so that it gets a proper ref decrement.
160160
if (auto *FRI = dyn_cast<FunctionRefBaseInst>(this)) {
161-
if (!FRI->getReferencedFunction())
161+
if (!FRI->getInitiallyReferencedFunction())
162162
return;
163163
FRI->dropReferencedFunction();
164164
return;
@@ -465,16 +465,19 @@ namespace {
465465

466466
bool visitFunctionRefInst(const FunctionRefInst *RHS) {
467467
auto *X = cast<FunctionRefInst>(LHS);
468-
return X->getReferencedFunction() == RHS->getReferencedFunction();
468+
return X->getInitiallyReferencedFunction() ==
469+
RHS->getInitiallyReferencedFunction();
469470
}
470471
bool visitDynamicFunctionRefInst(const DynamicFunctionRefInst *RHS) {
471472
auto *X = cast<DynamicFunctionRefInst>(LHS);
472-
return X->getReferencedFunction() == RHS->getReferencedFunction();
473+
return X->getInitiallyReferencedFunction() ==
474+
RHS->getInitiallyReferencedFunction();
473475
}
474476
bool visitPreviousDynamicFunctionRefInst(
475477
const PreviousDynamicFunctionRefInst *RHS) {
476478
auto *X = cast<PreviousDynamicFunctionRefInst>(LHS);
477-
return X->getReferencedFunction() == RHS->getReferencedFunction();
479+
return X->getInitiallyReferencedFunction() ==
480+
RHS->getInitiallyReferencedFunction();
478481
}
479482

480483
bool visitAllocGlobalInst(const AllocGlobalInst *RHS) {

lib/SIL/SILInstructions.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ BeginApplyInst::create(SILDebugLocation loc, SILValue callee,
489489

490490
bool swift::doesApplyCalleeHaveSemantics(SILValue callee, StringRef semantics) {
491491
if (auto *FRI = dyn_cast<FunctionRefBaseInst>(callee))
492-
if (auto *F = FRI->getReferencedFunction())
492+
if (auto *F = FRI->getReferencedFunctionOrNull())
493493
return F->hasSemanticsAttr(semantics);
494494
return false;
495495
}
@@ -573,14 +573,14 @@ FunctionRefBaseInst::FunctionRefBaseInst(SILInstructionKind Kind,
573573
}
574574

575575
void FunctionRefBaseInst::dropReferencedFunction() {
576-
if (auto *Function = getReferencedFunction())
576+
if (auto *Function = getInitiallyReferencedFunction())
577577
Function->decrementRefCount();
578578
f = nullptr;
579579
}
580580

581581
FunctionRefBaseInst::~FunctionRefBaseInst() {
582-
if (getReferencedFunction())
583-
getReferencedFunction()->decrementRefCount();
582+
if (getInitiallyReferencedFunction())
583+
getInitiallyReferencedFunction()->decrementRefCount();
584584
}
585585

586586
FunctionRefInst::FunctionRefInst(SILDebugLocation Loc, SILFunction *F)

lib/SIL/SILPrinter.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -834,15 +834,15 @@ class SILPrinter : public SILInstructionVisitor<SILPrinter> {
834834
void print(const SILInstruction *I) {
835835
if (auto *FRI = dyn_cast<FunctionRefInst>(I))
836836
*this << " // function_ref "
837-
<< demangleSymbol(FRI->getReferencedFunction()->getName())
837+
<< demangleSymbol(FRI->getInitiallyReferencedFunction()->getName())
838838
<< "\n";
839839
else if (auto *FRI = dyn_cast<DynamicFunctionRefInst>(I))
840840
*this << " // dynamic_function_ref "
841-
<< demangleSymbol(FRI->getReferencedFunction()->getName())
841+
<< demangleSymbol(FRI->getInitiallyReferencedFunction()->getName())
842842
<< "\n";
843843
else if (auto *FRI = dyn_cast<PreviousDynamicFunctionRefInst>(I))
844844
*this << " // prev_dynamic_function_ref "
845-
<< demangleSymbol(FRI->getReferencedFunction()->getName())
845+
<< demangleSymbol(FRI->getInitiallyReferencedFunction()->getName())
846846
<< "\n";
847847

848848
*this << " ";
@@ -1141,16 +1141,16 @@ class SILPrinter : public SILInstructionVisitor<SILPrinter> {
11411141
}
11421142

11431143
void visitFunctionRefInst(FunctionRefInst *FRI) {
1144-
FRI->getReferencedFunction()->printName(PrintState.OS);
1144+
FRI->getInitiallyReferencedFunction()->printName(PrintState.OS);
11451145
*this << " : " << FRI->getType();
11461146
}
11471147
void visitDynamicFunctionRefInst(DynamicFunctionRefInst *FRI) {
1148-
FRI->getReferencedFunction()->printName(PrintState.OS);
1148+
FRI->getInitiallyReferencedFunction()->printName(PrintState.OS);
11491149
*this << " : " << FRI->getType();
11501150
}
11511151
void
11521152
visitPreviousDynamicFunctionRefInst(PreviousDynamicFunctionRefInst *FRI) {
1153-
FRI->getReferencedFunction()->printName(PrintState.OS);
1153+
FRI->getInitiallyReferencedFunction()->printName(PrintState.OS);
11541154
*this << " : " << FRI->getType();
11551155
}
11561156

lib/SIL/SILVerifier.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1642,7 +1642,7 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
16421642
// Note: in SingleFunction mode, we relax some of these checks because
16431643
// we may not have linked everything yet.
16441644

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

16471647
if (isa<FunctionRefInst>(FRI))
16481648
require(

lib/SILOptimizer/ARC/RCStateTransition.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ static bool isAutoreleasePoolCall(SILInstruction *I) {
2828
if (!AI)
2929
return false;
3030

31-
auto *Fn = AI->getReferencedFunction();
31+
auto *Fn = AI->getReferencedFunctionOrNull();
3232
if (!Fn)
3333
return false;
3434

0 commit comments

Comments
 (0)