Skip to content

[6.2] GenericSpecializer: remove some kind of instructions if their operands become trivial after specialization #82184

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 12, 2025
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
28 changes: 28 additions & 0 deletions include/swift/SIL/TypeSubstCloner.h
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,16 @@ class TypeSubstCloner : public SILClonerWithScopes<ImplClass> {
super::visitCopyValueInst(Copy);
}

void visitExplicitCopyValueInst(ExplicitCopyValueInst *Copy) {
// If the substituted type is trivial, ignore the copy.
SILType copyTy = getOpType(Copy->getType());
if (copyTy.isTrivial(*Copy->getFunction())) {
recordFoldedValue(SILValue(Copy), getOpValue(Copy->getOperand()));
return;
}
super::visitExplicitCopyValueInst(Copy);
}

void visitDestroyValueInst(DestroyValueInst *Destroy) {
// If the substituted type is trivial, ignore the destroy.
SILType destroyTy = getOpType(Destroy->getOperand()->getType());
Expand All @@ -293,6 +303,24 @@ class TypeSubstCloner : public SILClonerWithScopes<ImplClass> {
super::visitDestroyValueInst(Destroy);
}

void visitEndLifetimeInst(EndLifetimeInst *endLifetime) {
// If the substituted type is trivial, ignore the end_lifetime.
SILType ty = getOpType(endLifetime->getOperand()->getType());
if (ty.isTrivial(*endLifetime->getFunction())) {
return;
}
super::visitEndLifetimeInst(endLifetime);
}

void visitExtendLifetimeInst(ExtendLifetimeInst *extendLifetime) {
// If the substituted type is trivial, ignore the extend_lifetime.
SILType ty = getOpType(extendLifetime->getOperand()->getType());
if (ty.isTrivial(*extendLifetime->getFunction())) {
return;
}
super::visitExtendLifetimeInst(extendLifetime);
}

void visitDifferentiableFunctionExtractInst(
DifferentiableFunctionExtractInst *dfei) {
// If the extractee is the original function, do regular cloning.
Expand Down
6 changes: 5 additions & 1 deletion test/SILOptimizer/specialize_opaque.sil
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,20 @@ import Builtin
// CHECK: %{{.*}} = apply [[F]](%0, %1) : $@convention(thin) (Builtin.Int64, Builtin.Int64) -> ()
// CHECK-NOT: copy_value
// CHECK-NOT: destroy_value
// CHECK-NOT: extend_lifetime
// CHECK-NOT: end_lifetime
// CHECK: return %{{.*}} : $()
// CHECK-LABEL: } // end sil function '$s3fooBi64__Tg5'
sil hidden [ossa] @foo : $@convention(thin) <T> (@in T, @in T) -> () {
bb0(%0 : @owned $T, %1 : @owned $T):
%f = function_ref @foo : $@convention(thin) <τ_0_0> (@in τ_0_0, @in τ_0_0) -> ()
%cp0 = copy_value %0 : $T
%cp1 = copy_value %1 : $T
%cp3 = explicit_copy_value %1 : $T
%call = apply %f<T>(%cp0, %cp1) : $@convention(thin) <τ_0_0> (@in τ_0_0, @in τ_0_0) -> ()
destroy_value %1 : $T
destroy_value %0 : $T
end_lifetime %0 : $T
extend_lifetime %cp3 : $T
%10 = tuple ()
return %10 : $()
}
Expand Down