Skip to content

Commit 699996b

Browse files
committed
[sil-opaque-values] Teach the SIL cloner to skip trivial copy/destroy.
This reduces the amount of SIL generated by 14x.
1 parent c95598d commit 699996b

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

include/swift/SIL/TypeSubstCloner.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,25 @@ class TypeSubstCloner : public SILClonerWithScopes<ImplClass> {
262262
super::visitUpcastInst(Upcast);
263263
}
264264

265+
void visitCopyValueInst(CopyValueInst *Copy) {
266+
// If the substituted type is trivial, ignore the copy.
267+
SILType copyTy = getOpType(Copy->getType());
268+
if (copyTy.isTrivial(Copy->getModule())) {
269+
ValueMap.insert({SILValue(Copy), getOpValue(Copy->getOperand())});
270+
return;
271+
}
272+
super::visitCopyValueInst(Copy);
273+
}
274+
275+
void visitDestroyValueInst(DestroyValueInst *Destroy) {
276+
// If the substituted type is trivial, ignore the destroy.
277+
SILType destroyTy = getOpType(Destroy->getOperand()->getType());
278+
if (destroyTy.isTrivial(Destroy->getModule())) {
279+
return;
280+
}
281+
super::visitDestroyValueInst(Destroy);
282+
}
283+
265284
/// The Swift module that the cloned function belongs to.
266285
ModuleDecl *SwiftMod;
267286
/// The substitutions list for the specialization.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// RUN: %target-sil-opt -enable-sil-opaque-values -assume-parsing-unqualified-ownership-sil -enable-sil-verify-all -generic-specializer %s | %FileCheck %s
2+
3+
sil_stage canonical
4+
5+
import Builtin
6+
7+
// Test that foo is specialized on Builtin.Int64 and the copy_values and destroy_values are dropped.
8+
//
9+
// CHECK-LABEL: sil shared @_T03fooBi64__Tg5 : $@convention(thin) (Builtin.Int64, Builtin.Int64) -> () {
10+
// CHECK: bb0(%0 : $Builtin.Int64, %1 : $Builtin.Int64):
11+
// CHECK: [[F:%.*]] = function_ref @_T03fooBi64__Tg5 : $@convention(thin) (Builtin.Int64, Builtin.Int64) -> ()
12+
// CHECK: %{{.*}} = apply [[F]](%0, %1) : $@convention(thin) (Builtin.Int64, Builtin.Int64) -> ()
13+
// CHECK-NOT: copy_value
14+
// CHECK-NOT: destroy_value
15+
// CHECK: return %{{.*}} : $()
16+
// CHECK-LABEL: } // end sil function '_T03fooBi64__Tg5'
17+
sil hidden @foo : $@convention(thin) <T> (@in T, @in T) -> () {
18+
bb0(%0 : $T, %1 : $T):
19+
%f = function_ref @foo : $@convention(thin) <τ_0_0> (@in τ_0_0, @in τ_0_0) -> ()
20+
%cp0 = copy_value %0 : $T
21+
%cp1 = copy_value %1 : $T
22+
%call = apply %f<T>(%cp0, %cp1) : $@convention(thin) <τ_0_0> (@in τ_0_0, @in τ_0_0) -> ()
23+
destroy_value %1 : $T
24+
destroy_value %0 : $T
25+
%10 = tuple ()
26+
return %10 : $()
27+
}
28+
29+
sil @testSpecialize : $@convention(thin) (Builtin.Int64) -> () {
30+
bb0(%0: $Builtin.Int64):
31+
%f = function_ref @foo : $@convention(thin) <T> (@in T, @in T) -> ()
32+
%call = apply %f<Builtin.Int64>(%0, %0) : $@convention(thin) <τ_0_0> (@in τ_0_0, @in τ_0_0) -> ()
33+
%999 = tuple ()
34+
return %999 : $()
35+
}

0 commit comments

Comments
 (0)