Skip to content

Commit acb16cf

Browse files
authored
[AutoDiff] Dedupe array semantic call utilities. (swiftlang#32266)
Delete differentiation array semantic call utilities: - `bool isArrayLiteralIntrinsic(FullApplySite applySite)` - `ApplyInst *getAllocateUninitializedArrayIntrinsic(SILValue v)` Use ArraySemanticsCall from ArraySemantics.h instead. Resolves SR-12894.
1 parent eeb7b3d commit acb16cf

File tree

7 files changed

+25
-38
lines changed

7 files changed

+25
-38
lines changed

include/swift/SILOptimizer/Differentiation/Common.h

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,6 @@ namespace autodiff {
3737
/// This is being used to print short debug messages within the AD pass.
3838
raw_ostream &getADDebugStream();
3939

40-
/// Returns true if this is an full apply site whose callee has
41-
/// `array.uninitialized_intrinsic` semantics.
42-
bool isArrayLiteralIntrinsic(FullApplySite applySite);
43-
44-
/// If the given value `v` corresponds to an `ApplyInst` with
45-
/// `array.uninitialized_intrinsic` semantics, returns the corresponding
46-
/// `ApplyInst`. Otherwise, returns `nullptr`.
47-
ApplyInst *getAllocateUninitializedArrayIntrinsic(SILValue v);
48-
4940
/// Given an element address from an `array.uninitialized_intrinsic` `apply`
5041
/// instruction, returns the `apply` instruction. The element address is either
5142
/// a `pointer_to_address` or `index_addr` instruction to the `RawPointer`
@@ -57,6 +48,8 @@ ApplyInst *getAllocateUninitializedArrayIntrinsic(SILValue v);
5748
/// %index_1 = integer_literal $Builtin.Word, 1
5849
/// %elt1 = index_addr %elt0, %index_1 // element address
5950
/// ...
51+
// TODO(SR-12894): Find a better name and move this general utility to
52+
// ArraySemantic.h.
6053
ApplyInst *getAllocateUninitializedArrayIntrinsicElementAddress(SILValue v);
6154

6255
/// Given a value, finds its single `destructure_tuple` user if the value is

lib/SILOptimizer/Analysis/DifferentiableActivityAnalysis.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,9 @@ void DifferentiableActivityInfo::setUsefulThroughArrayInitialization(
403403
SILValue value, unsigned dependentVariableIndex) {
404404
// Array initializer syntax is lowered to an intrinsic and one or more
405405
// stores to a `RawPointer` returned by the intrinsic.
406-
auto *uai = getAllocateUninitializedArrayIntrinsic(value);
406+
ArraySemanticsCall uninitCall(value,
407+
semantics::ARRAY_UNINITIALIZED_INTRINSIC);
408+
ApplyInst *uai = uninitCall;
407409
if (!uai)
408410
return;
409411
for (auto use : value->getUses()) {

lib/SILOptimizer/Differentiation/Common.cpp

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,6 @@ raw_ostream &getADDebugStream() { return llvm::dbgs() << "[AD] "; }
2727
// Helpers
2828
//===----------------------------------------------------------------------===//
2929

30-
bool isArrayLiteralIntrinsic(FullApplySite applySite) {
31-
return doesApplyCalleeHaveSemantics(applySite.getCalleeOrigin(),
32-
"array.uninitialized_intrinsic");
33-
}
34-
35-
ApplyInst *getAllocateUninitializedArrayIntrinsic(SILValue v) {
36-
if (auto *ai = dyn_cast<ApplyInst>(v))
37-
if (isArrayLiteralIntrinsic(ai))
38-
return ai;
39-
return nullptr;
40-
}
41-
4230
ApplyInst *getAllocateUninitializedArrayIntrinsicElementAddress(SILValue v) {
4331
// Find the `pointer_to_address` result, peering through `index_addr`.
4432
auto *ptai = dyn_cast<PointerToAddressInst>(v);
@@ -48,10 +36,9 @@ ApplyInst *getAllocateUninitializedArrayIntrinsicElementAddress(SILValue v) {
4836
return nullptr;
4937
// Return the `array.uninitialized_intrinsic` application, if it exists.
5038
if (auto *dti = dyn_cast<DestructureTupleInst>(
51-
ptai->getOperand()->getDefiningInstruction())) {
52-
if (auto *ai = getAllocateUninitializedArrayIntrinsic(dti->getOperand()))
53-
return ai;
54-
}
39+
ptai->getOperand()->getDefiningInstruction()))
40+
return ArraySemanticsCall(dti->getOperand(),
41+
semantics::ARRAY_UNINITIALIZED_INTRINSIC);
5542
return nullptr;
5643
}
5744

lib/SILOptimizer/Differentiation/JVPEmitter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1181,7 +1181,7 @@ void JVPEmitter::visitApplyInst(ApplyInst *ai) {
11811181
// If the function should not be differentiated or its the array literal
11821182
// initialization intrinsic, just do standard cloning.
11831183
if (!differentialInfo.shouldDifferentiateApplySite(ai) ||
1184-
isArrayLiteralIntrinsic(ai)) {
1184+
ArraySemanticsCall(ai, semantics::ARRAY_UNINITIALIZED_INTRINSIC)) {
11851185
LLVM_DEBUG(getADDebugStream() << "No active results:\n" << *ai << '\n');
11861186
TypeSubstCloner::visitApplyInst(ai);
11871187
return;

lib/SILOptimizer/Differentiation/LinearMapInfo.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,8 @@ void LinearMapInfo::generateDifferentiationDataStructures(
412412
// Add linear map field to struct for active `apply` instructions.
413413
// Skip array literal intrinsic applications since array literal
414414
// initialization is linear and handled separately.
415-
if (!shouldDifferentiateApplySite(ai) || isArrayLiteralIntrinsic(ai))
415+
if (!shouldDifferentiateApplySite(ai) ||
416+
ArraySemanticsCall(ai, semantics::ARRAY_UNINITIALIZED_INTRINSIC))
416417
continue;
417418
if (ArraySemanticsCall(ai, semantics::ARRAY_FINALIZE_INTRINSIC))
418419
continue;
@@ -476,7 +477,9 @@ bool LinearMapInfo::shouldDifferentiateApplySite(FullApplySite applySite) {
476477

477478
// TODO: Pattern match to make sure there is at least one `store` to the
478479
// array's active buffer.
479-
if (isArrayLiteralIntrinsic(applySite) && hasActiveResults)
480+
if (ArraySemanticsCall(applySite.getInstruction(),
481+
semantics::ARRAY_UNINITIALIZED_INTRINSIC) &&
482+
hasActiveResults)
480483
return true;
481484

482485
auto arguments = applySite.getArgumentsWithoutIndirectResults();

lib/SILOptimizer/Differentiation/PullbackEmitter.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,8 @@ void PullbackEmitter::accumulateArrayLiteralElementAddressAdjoints(
272272
originalValue->getDefiningInstruction());
273273
if (!dti)
274274
return;
275-
if (!getAllocateUninitializedArrayIntrinsic(dti->getOperand()))
275+
if (!ArraySemanticsCall(dti->getOperand(),
276+
semantics::ARRAY_UNINITIALIZED_INTRINSIC))
276277
return;
277278
if (originalValue != dti->getResult(0))
278279
return;
@@ -1420,9 +1421,9 @@ PullbackEmitter::getArrayAdjointElementBuffer(SILValue arrayAdjoint,
14201421

14211422
void PullbackEmitter::visitApplyInst(ApplyInst *ai) {
14221423
assert(getPullbackInfo().shouldDifferentiateApplySite(ai));
1423-
// Skip `array.uninitialized_intrinsic` intrinsic applications, which have
1424-
// special `store` and `copy_addr` support.
1425-
if (isArrayLiteralIntrinsic(ai))
1424+
// Skip `array.uninitialized_intrinsic` applications, which have special
1425+
// `store` and `copy_addr` support.
1426+
if (ArraySemanticsCall(ai, semantics::ARRAY_UNINITIALIZED_INTRINSIC))
14261427
return;
14271428
auto loc = ai->getLoc();
14281429
auto *bb = ai->getParent();

lib/SILOptimizer/Differentiation/VJPEmitter.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -533,11 +533,12 @@ void VJPEmitter::visitApplyInst(ApplyInst *ai) {
533533
TypeSubstCloner::visitApplyInst(ai);
534534
return;
535535
}
536-
// If callee is the array literal initialization intrinsic, do standard
537-
// cloning. Array literal differentiation is handled separately.
538-
if (isArrayLiteralIntrinsic(ai)) {
539-
LLVM_DEBUG(getADDebugStream() << "Cloning array literal intrinsic `apply`\n"
540-
<< *ai << '\n');
536+
// If callee is `array.uninitialized_intrinsic`, do standard cloning.
537+
// `array.unininitialized_intrinsic` differentiation is handled separately.
538+
if (ArraySemanticsCall(ai, semantics::ARRAY_UNINITIALIZED_INTRINSIC)) {
539+
LLVM_DEBUG(getADDebugStream()
540+
<< "Cloning `array.unininitialized_intrinsic` `apply`:\n"
541+
<< *ai << '\n');
541542
TypeSubstCloner::visitApplyInst(ai);
542543
return;
543544
}

0 commit comments

Comments
 (0)