Skip to content

Commit d92f181

Browse files
authored
Create two versions (for caller and callee) of the functions that answer questions about parameter convention (#74124)
Create two versions of the following functions: isConsumedParameter isGuaranteedParameter SILParameterInfo::isConsumed SILParameterInfo::isGuaranteed SILArgumentConvention::isOwnedConvention SILArgumentConvention::isGuaranteedConvention These changes will be needed when we add a new convention for non-trivial C++ types as the functions will return different answers depending on whether they are called for the caller or the callee. This commit doesn't change any functionality.
1 parent 13bf2ec commit d92f181

35 files changed

+136
-90
lines changed

include/swift/AST/Types.h

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4117,7 +4117,9 @@ inline bool isIndirectFormalParameter(ParameterConvention conv) {
41174117
}
41184118
llvm_unreachable("covered switch isn't covered?!");
41194119
}
4120-
inline bool isConsumedParameter(ParameterConvention conv) {
4120+
4121+
template <bool InCallee>
4122+
bool isConsumedParameter(ParameterConvention conv) {
41214123
switch (conv) {
41224124
case ParameterConvention::Indirect_In:
41234125
case ParameterConvention::Direct_Owned:
@@ -4136,10 +4138,19 @@ inline bool isConsumedParameter(ParameterConvention conv) {
41364138
llvm_unreachable("bad convention kind");
41374139
}
41384140

4141+
inline bool isConsumedParameterInCallee(ParameterConvention conv) {
4142+
return isConsumedParameter<true>(conv);
4143+
}
4144+
4145+
inline bool isConsumedParameterInCaller(ParameterConvention conv) {
4146+
return isConsumedParameter<false>(conv);
4147+
}
4148+
41394149
/// Returns true if conv is a guaranteed parameter. This may look unnecessary
41404150
/// but this will allow code to generalize to handle Indirect_Guaranteed
41414151
/// parameters when they are added.
4142-
inline bool isGuaranteedParameter(ParameterConvention conv) {
4152+
template <bool InCallee>
4153+
bool isGuaranteedParameter(ParameterConvention conv) {
41434154
switch (conv) {
41444155
case ParameterConvention::Direct_Guaranteed:
41454156
case ParameterConvention::Indirect_In_Guaranteed:
@@ -4158,6 +4169,14 @@ inline bool isGuaranteedParameter(ParameterConvention conv) {
41584169
llvm_unreachable("bad convention kind");
41594170
}
41604171

4172+
inline bool isGuaranteedParameterInCallee(ParameterConvention conv) {
4173+
return isGuaranteedParameter<true>(conv);
4174+
}
4175+
4176+
inline bool isGuaranteedParameterInCaller(ParameterConvention conv) {
4177+
return isGuaranteedParameter<false>(conv);
4178+
}
4179+
41614180
inline bool isMutatingParameter(ParameterConvention conv) {
41624181
switch (conv) {
41634182
case ParameterConvention::Indirect_Inout:
@@ -4285,14 +4304,22 @@ class SILParameterInfo {
42854304

42864305
/// True if this parameter is consumed by the callee, either
42874306
/// indirectly or directly.
4288-
bool isConsumed() const {
4289-
return isConsumedParameter(getConvention());
4307+
bool isConsumedInCallee() const {
4308+
return isConsumedParameterInCallee(getConvention());
4309+
}
4310+
4311+
bool isConsumedInCaller() const {
4312+
return isConsumedParameterInCaller(getConvention());
42904313
}
42914314

42924315
/// Returns true if this parameter is guaranteed, either indirectly or
42934316
/// directly.
4294-
bool isGuaranteed() const {
4295-
return isGuaranteedParameter(getConvention());
4317+
bool isGuaranteedInCallee() const {
4318+
return isGuaranteedParameterInCallee(getConvention());
4319+
}
4320+
4321+
bool isGuaranteedInCaller() const {
4322+
return isGuaranteedParameterInCaller(getConvention());
42964323
}
42974324

42984325
bool hasOption(Flag flag) const { return options.contains(flag); }

include/swift/SIL/SILArgumentConvention.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ struct SILArgumentConvention {
103103
llvm_unreachable("covered switch isn't covered?!");
104104
}
105105

106+
template <bool InCallee>
106107
bool isOwnedConvention() const {
107108
switch (Value) {
108109
case SILArgumentConvention::Indirect_In:
@@ -123,6 +124,11 @@ struct SILArgumentConvention {
123124
llvm_unreachable("covered switch isn't covered?!");
124125
}
125126

127+
bool isOwnedConventionInCallee() const { return isOwnedConvention<true>(); }
128+
129+
bool isOwnedConventionInCaller() const { return isOwnedConvention<false>(); }
130+
131+
template <bool InCallee>
126132
bool isGuaranteedConvention() const {
127133
switch (Value) {
128134
case SILArgumentConvention::Indirect_In_Guaranteed:
@@ -143,6 +149,14 @@ struct SILArgumentConvention {
143149
llvm_unreachable("covered switch isn't covered?!");
144150
}
145151

152+
bool isGuaranteedConventionInCallee() const {
153+
return isGuaranteedConvention<true>();
154+
}
155+
156+
bool isGuaranteedConventionInCaller() const {
157+
return isGuaranteedConvention<false>();
158+
}
159+
146160
/// Returns true if \p Value is a non-aliasing indirect parameter.
147161
bool isExclusiveIndirectParameter() {
148162
switch (Value) {

include/swift/SIL/SILFunction.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -811,7 +811,7 @@ class SILFunction
811811
// callee.
812812
bool hasOwnedParameters() const {
813813
for (auto &ParamInfo : getLoweredFunctionType()->getParameters()) {
814-
if (ParamInfo.isConsumed())
814+
if (ParamInfo.isConsumedInCallee())
815815
return true;
816816
}
817817
return false;

lib/AST/ClangTypeConverter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ ClangTypeConverter::getFunctionType(ArrayRef<SILParameterInfo> params,
178178
if (pc.isNull())
179179
return nullptr;
180180
clang::FunctionProtoType::ExtParameterInfo extParamInfo;
181-
if (p.isConsumed()) {
181+
if (p.isConsumedInCallee()) {
182182
someParamIsConsumed = true;
183183
extParamInfo = extParamInfo.withIsConsumed(true);
184184
}

lib/AST/LifetimeDependence.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ std::optional<LifetimeDependenceInfo> LifetimeDependenceInfo::fromTypeRepr(
353353
auto kind = specifier.getParsedLifetimeDependenceKind();
354354

355355
if (kind == ParsedLifetimeDependenceKind::Scope &&
356-
(!isGuaranteedParameter(paramConvention) &&
356+
(!isGuaranteedParameterInCallee(paramConvention) &&
357357
!isMutatingParameter(paramConvention))) {
358358
diags.diagnose(loc, diag::lifetime_dependence_cannot_use_kind, "_scope",
359359
getStringForParameterConvention(paramConvention));

lib/IRGen/LoadableByAddress.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2612,7 +2612,7 @@ void LoadableByAddress::recreateSingleApply(
26122612
} else if (newValue->getType().isTrivial(*F)) {
26132613
ownership = LoadOwnershipQualifier::Trivial;
26142614
} else {
2615-
assert(oldYields[i].isConsumed() &&
2615+
assert(oldYields[i].isConsumedInCaller() &&
26162616
"borrowed yields not yet supported here");
26172617
ownership = LoadOwnershipQualifier::Take;
26182618
}

lib/PrintAsClang/PrintClangFunction.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1269,12 +1269,12 @@ void DeclAndTypeClangFunctionPrinter::printCxxThunkBody(
12691269
signature.visitParameterList(
12701270
[&](const LoweredFunctionSignature::IndirectResultValue &) {},
12711271
[&](const LoweredFunctionSignature::DirectParameter &param) {
1272-
if (isConsumedParameter(param.getConvention()))
1272+
if (isConsumedParameterInCaller(param.getConvention()))
12731273
emitParamCopyForConsume(param.getParamDecl());
12741274
++paramIndex;
12751275
},
12761276
[&](const LoweredFunctionSignature::IndirectParameter &param) {
1277-
if (isConsumedParameter(param.getConvention()))
1277+
if (isConsumedParameterInCaller(param.getConvention()))
12781278
emitParamCopyForConsume(param.getParamDecl());
12791279
++paramIndex;
12801280
},
@@ -1338,12 +1338,12 @@ void DeclAndTypeClangFunctionPrinter::printCxxThunkBody(
13381338
},
13391339
[&](const LoweredFunctionSignature::DirectParameter &param) {
13401340
printParamUse(param.getParamDecl(), /*isIndirect=*/false,
1341-
isConsumedParameter(param.getConvention()),
1341+
isConsumedParameterInCaller(param.getConvention()),
13421342
encodeTypeInfo(param, moduleContext, typeMapping));
13431343
},
13441344
[&](const LoweredFunctionSignature::IndirectParameter &param) {
13451345
printParamUse(param.getParamDecl(), /*isIndirect=*/true,
1346-
isConsumedParameter(param.getConvention()),
1346+
isConsumedParameterInCaller(param.getConvention()),
13471347
/*directTypeEncoding=*/"");
13481348
},
13491349
[&](const LoweredFunctionSignature::GenericRequirementParameter

lib/SIL/Verifier/LoadBorrowImmutabilityChecker.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ bool GatherWritesVisitor::visitUse(Operand *op, AccessUseType useTy) {
226226
if (info.isIndirectInGuaranteed()) {
227227
return true;
228228
}
229-
if (info.isIndirectMutating() || info.isConsumed()) {
229+
if (info.isIndirectMutating() || info.isConsumedInCaller()) {
230230
writeAccumulator.push_back(op);
231231
return true;
232232
}
@@ -259,7 +259,7 @@ bool GatherWritesVisitor::visitUse(Operand *op, AccessUseType useTy) {
259259
writeAccumulator.push_back(op);
260260
return true;
261261
}
262-
if (argConv.isOwnedConvention()) {
262+
if (argConv.isOwnedConventionInCaller()) {
263263
writeAccumulator.push_back(op);
264264
return true;
265265
}

lib/SIL/Verifier/MemoryLifetimeVerifier.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -849,10 +849,10 @@ void MemoryLifetimeVerifier::checkBlock(SILBasicBlock *block, Bits &bits) {
849849
fnType->getYields()[index].getConvention());
850850
if (argConv.isIndirectConvention()) {
851851
if (argConv.isInoutConvention() ||
852-
argConv.isGuaranteedConvention()) {
852+
argConv.isGuaranteedConventionInCaller()) {
853853
requireBitsSet(bits | ~nonTrivialLocations, yieldedValues[index],
854854
&I);
855-
} else if (argConv.isOwnedConvention()) {
855+
} else if (argConv.isOwnedConventionInCaller()) {
856856
requireBitsClear(bits & nonTrivialLocations, yieldedValues[index],
857857
&I);
858858
}

lib/SIL/Verifier/SILVerifier.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2194,8 +2194,9 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
21942194
"applied argument types do not match suffix of function type's "
21952195
"inputs");
21962196
if (PAI->isOnStack()) {
2197-
require(!substConv.getSILArgumentConvention(argIdx).isOwnedConvention(),
2198-
"on-stack closures do not support owned arguments");
2197+
require(!substConv.getSILArgumentConvention(argIdx)
2198+
.isOwnedConventionInCaller(),
2199+
"on-stack closures do not support owned arguments");
21992200
}
22002201
}
22012202

0 commit comments

Comments
 (0)