Skip to content

Commit 96a3c1e

Browse files
authored
[Specific coalescing] Do not update canonical when is exists. (#6196)
Only update the canonical for itself if it has no value, otherwise a "better" canonical was previously added and the chain will be followed when deleting specifics.
1 parent 0679b77 commit 96a3c1e

File tree

2 files changed

+211
-1
lines changed

2 files changed

+211
-1
lines changed

toolchain/lower/specific_coalescer.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,13 @@ auto SpecificCoalescer::ProcessSpecificEquivalence(
137137
// chains in `IsKnownEquivalence`.
138138
equivalent_specifics_.Set(specific_id1, canon_id1);
139139
equivalent_specifics_.Set(specific_id2, canon_id1);
140-
equivalent_specifics_.Set(canon_id1, canon_id1);
141140
equivalent_specifics_.Set(canon_id2, canon_id1);
141+
// Only update the canonical for itself if it has no value, otherwise a
142+
// "better" canonical was previously added and the chain will be followed
143+
// when deleting specifics, by calling `UpdateEquivalentSpecific`.
144+
if (!equivalent_specifics_.Get(canon_id1).has_value()) {
145+
equivalent_specifics_.Set(canon_id1, canon_id1);
146+
}
142147
}
143148

144149
auto SpecificCoalescer::UpdateEquivalentSpecific(SemIR::SpecificId specific_id)
Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
2+
// Exceptions. See /LICENSE for license information.
3+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4+
//
5+
// INCLUDE-FILE: toolchain/testing/testdata/min_prelude/destroy.carbon
6+
// AUTOUPDATE
7+
// TIP: To test this file alone, run:
8+
// TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/lower/testdata/function/generic/self_canonical.carbon
9+
// TIP: To dump output, run:
10+
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/lower/testdata/function/generic/self_canonical.carbon
11+
12+
// In this test, specific46 becomes canonical for others. Then, specific42 is
13+
// found to be equivalent to specific46 and it becomes its canonical.
14+
// Then through another equivalence from other specifics that point to
15+
// specific46, specific46 is picked as the canonical. This is fine, we keep the
16+
// chain of equivalences and eventually this is walked to get to specific42.
17+
// The canonical for specific46, should not be updated to self in this case.
18+
// Test that chains of equivalances are properly maintained and walked.
19+
20+
class class_name1 {}
21+
class class_name2 {}
22+
class class_name3 {}
23+
class class_name4 {}
24+
class class_name5 {}
25+
class class_name6 {}
26+
27+
fn third_function[T:! type, U:! type, V:! type](arg1: T, arg2: U, arg3: V);
28+
29+
fn fourth_function[T:! type](arg: T);
30+
31+
fn first_function[T:! type](arg: T);
32+
33+
fn second_function[T:! type, U:! type, V:! type](arg1: T, arg2: U, arg3: V);
34+
35+
fn third_function[T:! type, U:! type, V:! type](arg1: T, arg2: U, arg3: V) {
36+
var local1: class_name2*;
37+
var local2: class_name2*;
38+
39+
third_function(arg1, arg2, local2);
40+
fourth_function(local1);
41+
}
42+
43+
fn unused_1[T:! type, U:! type, V:! type](arg1: T, arg2: U, arg3: V) {
44+
var local1: class_name6*;
45+
46+
third_function(arg1, arg3, local1);
47+
}
48+
49+
fn fourth_function[T:! type](arg: T) {
50+
var local1: class_name5**;
51+
var local2: class_name6*;
52+
53+
first_function(local1);
54+
first_function(local2);
55+
}
56+
57+
fn first_function[T:! type](arg: T) {
58+
var local1: class_name4*;
59+
var local2: class_name3*;
60+
61+
second_function(local2, local1, arg);
62+
}
63+
64+
fn unused_2[T:! type, U:! type](arg1: T, arg2: U) {
65+
var local1: class_name6*;
66+
67+
second_function(arg1, arg2, local1);
68+
}
69+
70+
fn second_function[T:! type, U:! type, V:! type](arg1: T, arg2: U, arg3: V) {
71+
var local1: class_name3*;
72+
var local2: class_name4*;
73+
var local3: class_name2*;
74+
75+
second_function(arg3, local3, local2);
76+
third_function(local1, arg3, arg1);
77+
}
78+
79+
fn Main_method_no_template () {
80+
var local1: class_name1*;
81+
82+
first_function(local1);
83+
}
84+
85+
// CHECK:STDOUT: ; ModuleID = 'self_canonical.carbon'
86+
// CHECK:STDOUT: source_filename = "self_canonical.carbon"
87+
// CHECK:STDOUT:
88+
// CHECK:STDOUT: define void @_CMain_method_no_template.Main() !dbg !4 {
89+
// CHECK:STDOUT: entry:
90+
// CHECK:STDOUT: %local1.var = alloca ptr, align 8, !dbg !7
91+
// CHECK:STDOUT: call void @llvm.lifetime.start.p0(ptr %local1.var), !dbg !7
92+
// CHECK:STDOUT: %.loc82 = load ptr, ptr %local1.var, align 8, !dbg !8
93+
// CHECK:STDOUT: call void @_Cfirst_function.Main.308f7be7cd8574f7(ptr %.loc82), !dbg !9
94+
// CHECK:STDOUT: ret void, !dbg !10
95+
// CHECK:STDOUT: }
96+
// CHECK:STDOUT:
97+
// CHECK:STDOUT: ; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
98+
// CHECK:STDOUT: declare void @llvm.lifetime.start.p0(ptr captures(none)) #0
99+
// CHECK:STDOUT:
100+
// CHECK:STDOUT: define linkonce_odr void @_Cfourth_function.Main.a95d5636f2d020e8(ptr %arg) !dbg !11 {
101+
// CHECK:STDOUT: entry:
102+
// CHECK:STDOUT: %local1.var = alloca ptr, align 8, !dbg !12
103+
// CHECK:STDOUT: %local2.var = alloca ptr, align 8, !dbg !13
104+
// CHECK:STDOUT: call void @llvm.lifetime.start.p0(ptr %local1.var), !dbg !12
105+
// CHECK:STDOUT: call void @llvm.lifetime.start.p0(ptr %local2.var), !dbg !13
106+
// CHECK:STDOUT: %.loc53 = load ptr, ptr %local1.var, align 8, !dbg !14
107+
// CHECK:STDOUT: call void @_Cfirst_function.Main.308f7be7cd8574f7(ptr %.loc53), !dbg !15
108+
// CHECK:STDOUT: %.loc54 = load ptr, ptr %local2.var, align 8, !dbg !16
109+
// CHECK:STDOUT: call void @_Cfirst_function.Main.308f7be7cd8574f7(ptr %.loc54), !dbg !17
110+
// CHECK:STDOUT: ret void, !dbg !18
111+
// CHECK:STDOUT: }
112+
// CHECK:STDOUT:
113+
// CHECK:STDOUT: define linkonce_odr void @_Cfirst_function.Main.308f7be7cd8574f7(ptr %arg) !dbg !19 {
114+
// CHECK:STDOUT: entry:
115+
// CHECK:STDOUT: %local1.var = alloca ptr, align 8, !dbg !20
116+
// CHECK:STDOUT: %local2.var = alloca ptr, align 8, !dbg !21
117+
// CHECK:STDOUT: call void @llvm.lifetime.start.p0(ptr %local1.var), !dbg !20
118+
// CHECK:STDOUT: call void @llvm.lifetime.start.p0(ptr %local2.var), !dbg !21
119+
// CHECK:STDOUT: %.loc61_19 = load ptr, ptr %local2.var, align 8, !dbg !22
120+
// CHECK:STDOUT: %.loc61_27 = load ptr, ptr %local1.var, align 8, !dbg !23
121+
// CHECK:STDOUT: call void @_Csecond_function.Main.6e67709fb5f3d893(ptr %.loc61_19, ptr %.loc61_27, ptr %arg), !dbg !24
122+
// CHECK:STDOUT: ret void, !dbg !25
123+
// CHECK:STDOUT: }
124+
// CHECK:STDOUT:
125+
// CHECK:STDOUT: define linkonce_odr void @_Csecond_function.Main.6e67709fb5f3d893(ptr %arg1, ptr %arg2, ptr %arg3) !dbg !26 {
126+
// CHECK:STDOUT: entry:
127+
// CHECK:STDOUT: %local1.var = alloca ptr, align 8, !dbg !27
128+
// CHECK:STDOUT: %local2.var = alloca ptr, align 8, !dbg !28
129+
// CHECK:STDOUT: %local3.var = alloca ptr, align 8, !dbg !29
130+
// CHECK:STDOUT: call void @llvm.lifetime.start.p0(ptr %local1.var), !dbg !27
131+
// CHECK:STDOUT: call void @llvm.lifetime.start.p0(ptr %local2.var), !dbg !28
132+
// CHECK:STDOUT: call void @llvm.lifetime.start.p0(ptr %local3.var), !dbg !29
133+
// CHECK:STDOUT: %.loc75_25 = load ptr, ptr %local3.var, align 8, !dbg !30
134+
// CHECK:STDOUT: %.loc75_33 = load ptr, ptr %local2.var, align 8, !dbg !31
135+
// CHECK:STDOUT: call void @_Csecond_function.Main.6e67709fb5f3d893(ptr %arg3, ptr %.loc75_25, ptr %.loc75_33), !dbg !32
136+
// CHECK:STDOUT: %.loc76 = load ptr, ptr %local1.var, align 8, !dbg !33
137+
// CHECK:STDOUT: call void @_Cthird_function.Main.bd8cd068af0bdf76(ptr %.loc76, ptr %arg3, ptr %arg1), !dbg !34
138+
// CHECK:STDOUT: ret void, !dbg !35
139+
// CHECK:STDOUT: }
140+
// CHECK:STDOUT:
141+
// CHECK:STDOUT: define linkonce_odr void @_Cthird_function.Main.bd8cd068af0bdf76(ptr %arg1, ptr %arg2, ptr %arg3) !dbg !36 {
142+
// CHECK:STDOUT: entry:
143+
// CHECK:STDOUT: %local1.var = alloca ptr, align 8, !dbg !37
144+
// CHECK:STDOUT: %local2.var = alloca ptr, align 8, !dbg !38
145+
// CHECK:STDOUT: call void @llvm.lifetime.start.p0(ptr %local1.var), !dbg !37
146+
// CHECK:STDOUT: call void @llvm.lifetime.start.p0(ptr %local2.var), !dbg !38
147+
// CHECK:STDOUT: %.loc39 = load ptr, ptr %local2.var, align 8, !dbg !39
148+
// CHECK:STDOUT: call void @_Cthird_function.Main.bd8cd068af0bdf76(ptr %arg1, ptr %arg2, ptr %.loc39), !dbg !40
149+
// CHECK:STDOUT: %.loc40 = load ptr, ptr %local1.var, align 8, !dbg !41
150+
// CHECK:STDOUT: call void @_Cfourth_function.Main.a95d5636f2d020e8(ptr %.loc40), !dbg !42
151+
// CHECK:STDOUT: ret void, !dbg !43
152+
// CHECK:STDOUT: }
153+
// CHECK:STDOUT:
154+
// CHECK:STDOUT: ; uselistorder directives
155+
// CHECK:STDOUT: uselistorder ptr @_Cfirst_function.Main.308f7be7cd8574f7, { 1, 2, 0 }
156+
// CHECK:STDOUT:
157+
// CHECK:STDOUT: attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
158+
// CHECK:STDOUT:
159+
// CHECK:STDOUT: !llvm.module.flags = !{!0, !1}
160+
// CHECK:STDOUT: !llvm.dbg.cu = !{!2}
161+
// CHECK:STDOUT:
162+
// CHECK:STDOUT: !0 = !{i32 7, !"Dwarf Version", i32 5}
163+
// CHECK:STDOUT: !1 = !{i32 2, !"Debug Info Version", i32 3}
164+
// CHECK:STDOUT: !2 = distinct !DICompileUnit(language: DW_LANG_C, file: !3, producer: "carbon", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug)
165+
// CHECK:STDOUT: !3 = !DIFile(filename: "self_canonical.carbon", directory: "")
166+
// CHECK:STDOUT: !4 = distinct !DISubprogram(name: "Main_method_no_template", linkageName: "_CMain_method_no_template.Main", scope: null, file: !3, line: 79, type: !5, spFlags: DISPFlagDefinition, unit: !2)
167+
// CHECK:STDOUT: !5 = !DISubroutineType(types: !6)
168+
// CHECK:STDOUT: !6 = !{}
169+
// CHECK:STDOUT: !7 = !DILocation(line: 80, column: 3, scope: !4)
170+
// CHECK:STDOUT: !8 = !DILocation(line: 82, column: 18, scope: !4)
171+
// CHECK:STDOUT: !9 = !DILocation(line: 82, column: 3, scope: !4)
172+
// CHECK:STDOUT: !10 = !DILocation(line: 79, column: 1, scope: !4)
173+
// CHECK:STDOUT: !11 = distinct !DISubprogram(name: "fourth_function", linkageName: "_Cfourth_function.Main.a95d5636f2d020e8", scope: null, file: !3, line: 49, type: !5, spFlags: DISPFlagDefinition, unit: !2)
174+
// CHECK:STDOUT: !12 = !DILocation(line: 50, column: 3, scope: !11)
175+
// CHECK:STDOUT: !13 = !DILocation(line: 51, column: 3, scope: !11)
176+
// CHECK:STDOUT: !14 = !DILocation(line: 53, column: 18, scope: !11)
177+
// CHECK:STDOUT: !15 = !DILocation(line: 53, column: 3, scope: !11)
178+
// CHECK:STDOUT: !16 = !DILocation(line: 54, column: 18, scope: !11)
179+
// CHECK:STDOUT: !17 = !DILocation(line: 54, column: 3, scope: !11)
180+
// CHECK:STDOUT: !18 = !DILocation(line: 49, column: 1, scope: !11)
181+
// CHECK:STDOUT: !19 = distinct !DISubprogram(name: "first_function", linkageName: "_Cfirst_function.Main.308f7be7cd8574f7", scope: null, file: !3, line: 57, type: !5, spFlags: DISPFlagDefinition, unit: !2)
182+
// CHECK:STDOUT: !20 = !DILocation(line: 58, column: 3, scope: !19)
183+
// CHECK:STDOUT: !21 = !DILocation(line: 59, column: 3, scope: !19)
184+
// CHECK:STDOUT: !22 = !DILocation(line: 61, column: 19, scope: !19)
185+
// CHECK:STDOUT: !23 = !DILocation(line: 61, column: 27, scope: !19)
186+
// CHECK:STDOUT: !24 = !DILocation(line: 61, column: 3, scope: !19)
187+
// CHECK:STDOUT: !25 = !DILocation(line: 57, column: 1, scope: !19)
188+
// CHECK:STDOUT: !26 = distinct !DISubprogram(name: "second_function", linkageName: "_Csecond_function.Main.6e67709fb5f3d893", scope: null, file: !3, line: 70, type: !5, spFlags: DISPFlagDefinition, unit: !2)
189+
// CHECK:STDOUT: !27 = !DILocation(line: 71, column: 3, scope: !26)
190+
// CHECK:STDOUT: !28 = !DILocation(line: 72, column: 3, scope: !26)
191+
// CHECK:STDOUT: !29 = !DILocation(line: 73, column: 3, scope: !26)
192+
// CHECK:STDOUT: !30 = !DILocation(line: 75, column: 25, scope: !26)
193+
// CHECK:STDOUT: !31 = !DILocation(line: 75, column: 33, scope: !26)
194+
// CHECK:STDOUT: !32 = !DILocation(line: 75, column: 3, scope: !26)
195+
// CHECK:STDOUT: !33 = !DILocation(line: 76, column: 18, scope: !26)
196+
// CHECK:STDOUT: !34 = !DILocation(line: 76, column: 3, scope: !26)
197+
// CHECK:STDOUT: !35 = !DILocation(line: 70, column: 1, scope: !26)
198+
// CHECK:STDOUT: !36 = distinct !DISubprogram(name: "third_function", linkageName: "_Cthird_function.Main.bd8cd068af0bdf76", scope: null, file: !3, line: 35, type: !5, spFlags: DISPFlagDefinition, unit: !2)
199+
// CHECK:STDOUT: !37 = !DILocation(line: 36, column: 3, scope: !36)
200+
// CHECK:STDOUT: !38 = !DILocation(line: 37, column: 3, scope: !36)
201+
// CHECK:STDOUT: !39 = !DILocation(line: 39, column: 30, scope: !36)
202+
// CHECK:STDOUT: !40 = !DILocation(line: 39, column: 3, scope: !36)
203+
// CHECK:STDOUT: !41 = !DILocation(line: 40, column: 19, scope: !36)
204+
// CHECK:STDOUT: !42 = !DILocation(line: 40, column: 3, scope: !36)
205+
// CHECK:STDOUT: !43 = !DILocation(line: 35, column: 1, scope: !36)

0 commit comments

Comments
 (0)