Skip to content

Commit 19e86c4

Browse files
committed
Fix argument index calculation and reduce indentation.
1 parent d38f69b commit 19e86c4

File tree

1 file changed

+25
-22
lines changed

1 file changed

+25
-22
lines changed

lib/SILOptimizer/Mandatory/Differentiation.cpp

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1950,37 +1950,40 @@ reapplyFunctionConversion(SILValue newFunc, SILValue oldFunc,
19501950
for (auto *alloc : reversed(copiedIndirectParams))
19511951
builder.createDeallocStack(loc, alloc);
19521952
};
1953-
for (auto arg : pai->getArguments()) {
1953+
// Collect new arguments to for a new `partial_apply`.
1954+
auto conv = pai->getSubstCalleeConv();
1955+
unsigned argIndex = conv.getNumSILArguments() - pai->getNumArguments();
1956+
for (auto argIt = pai->getArguments().begin();
1957+
argIt != pai->getArguments().end(); ++argIt, ++argIndex) {
1958+
auto arg = *argIt;
19541959
// Retain the argument if it's to be owned by the newly created
19551960
// closure.
19561961
// Objects are to be retained.
19571962
if (arg->getType().isObject()) {
19581963
builder.createRetainValue(loc, arg, builder.getDefaultAtomicity());
19591964
newArgs.push_back(arg);
1965+
continue;
19601966
}
19611967
// Addresses depend on argument conventions.
1962-
else {
1963-
auto conv = pai->getCalleeFunction()->getConventions();
1964-
auto argConv =
1965-
conv.getSILArgumentConvention(conv.getNumSILArguments() - 1);
1966-
// If the argument is an aliasable inout reference, do not retain the
1967-
// argument since it's a `@noescape` capture.
1968-
if (argConv == SILArgumentConvention::Indirect_InoutAliasable) {
1969-
newArgs.push_back(arg);
1970-
}
1971-
// Otherwise, retain/copy the underlying value.
1972-
else if (arg->getType().isLoadable(builder.getFunction())) {
1973-
builder.createRetainValueAddr(loc, arg,
1974-
builder.getDefaultAtomicity());
1975-
newArgs.push_back(arg);
1976-
} else {
1977-
auto *argCopy = builder.createAllocStack(loc, arg->getType());
1978-
copiedIndirectParams.push_back(argCopy);
1979-
builder.createCopyAddr(loc, arg, argCopy, IsNotTake,
1980-
IsInitialization);
1981-
newArgs.push_back(argCopy);
1982-
}
1968+
// If the argument is an aliasable inout reference, do not retain the
1969+
// argument since it's a `@noescape` capture.
1970+
auto argConv = conv.getSILArgumentConvention(argIndex);
1971+
if (argConv == SILArgumentConvention::Indirect_InoutAliasable) {
1972+
newArgs.push_back(arg);
1973+
continue;
1974+
}
1975+
// If it's a loadable address, perform a `retain_value_addr`.
1976+
if (arg->getType().isLoadable(builder.getFunction())) {
1977+
builder.createRetainValueAddr(loc, arg, builder.getDefaultAtomicity());
1978+
newArgs.push_back(arg);
1979+
continue;
19831980
}
1981+
// Otherwise, it must be address-only. Create a new buffer and perform
1982+
// `copy_addr`.
1983+
auto *argCopy = builder.createAllocStack(loc, arg->getType());
1984+
copiedIndirectParams.push_back(argCopy);
1985+
builder.createCopyAddr(loc, arg, argCopy, IsNotTake, IsInitialization);
1986+
newArgs.push_back(argCopy);
19841987
}
19851988
auto innerNewFunc = reapplyFunctionConversion(
19861989
newFunc, oldFunc, pai->getCallee(), builder, loc, newFuncGenSig);

0 commit comments

Comments
 (0)