Skip to content

Commit 82fe19e

Browse files
authored
Remove redundant deduced specifics (#5107)
When deduction has to substitute binding parameters into further generic parameters, we do conversion of the argument to the substituted type. Then we replace the argument instruction id with that Converted instruction. This causes a redundant specific to be created for the Converted instruction which is not needed. What we want is the specific for its constant value. So when we replace the argument instruction id, replace it with the instruction from the constant value of the converted argument. This was raised in [discord #toolchain](https://discord.com/channels/655572317891461132/655578254970716160/1349067541070217306).
1 parent 201a4dc commit 82fe19e

File tree

5 files changed

+16
-65
lines changed

5 files changed

+16
-65
lines changed

toolchain/check/deduce.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -587,8 +587,10 @@ auto DeductionContext::CheckDeductionIsComplete() -> bool {
587587
// Replace the deduced arg with its value converted to the parameter
588588
// type. The conversion of the argument type must produce a constant value
589589
// to be used in deduction.
590-
if (context().constant_values().Get(converted_arg_id).is_constant()) {
591-
deduced_arg_id = converted_arg_id;
590+
if (auto const_inst_id =
591+
context().constant_values().GetConstantInstId(converted_arg_id);
592+
const_inst_id.has_value()) {
593+
deduced_arg_id = const_inst_id;
592594
} else {
593595
if (diagnose_) {
594596
CARBON_DIAGNOSTIC(RuntimeConversionDuringCompTimeDeduction, Error,

toolchain/check/testdata/builtin_conversions/no_prelude/convert_class_type_to_generic_facet_value.carbon

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ fn G() {
348348
// CHECK:STDOUT: %ImplsGeneric.ref: type = name_ref ImplsGeneric, file.%ImplsGeneric.decl [concrete = constants.%ImplsGeneric]
349349
// CHECK:STDOUT: %Generic.facet: %Generic.type.769 = facet_value constants.%ImplsGeneric, (constants.%impl_witness) [concrete = constants.%Generic.facet]
350350
// CHECK:STDOUT: %.loc20: %Generic.type.769 = converted constants.%ImplsGeneric, %Generic.facet [concrete = constants.%Generic.facet]
351-
// CHECK:STDOUT: %CallGenericMethod.specific_fn: <specific function> = specific_function %CallGenericMethod.ref, @CallGenericMethod(constants.%GenericParam, %.loc20) [concrete = constants.%CallGenericMethod.specific_fn.934]
351+
// CHECK:STDOUT: %CallGenericMethod.specific_fn: <specific function> = specific_function %CallGenericMethod.ref, @CallGenericMethod(constants.%GenericParam, constants.%Generic.facet) [concrete = constants.%CallGenericMethod.specific_fn.934]
352352
// CHECK:STDOUT: %CallGenericMethod.call: init %empty_tuple.type = call %CallGenericMethod.specific_fn()
353353
// CHECK:STDOUT: return
354354
// CHECK:STDOUT: }
@@ -381,7 +381,7 @@ fn G() {
381381
// CHECK:STDOUT: %ImplsGeneric.ref: type = name_ref ImplsGeneric, file.%ImplsGeneric.decl [concrete = constants.%ImplsGeneric]
382382
// CHECK:STDOUT: %Generic.facet: %Generic.type.769 = facet_value constants.%ImplsGeneric, (constants.%impl_witness) [concrete = constants.%Generic.facet]
383383
// CHECK:STDOUT: %.loc28: %Generic.type.769 = converted constants.%ImplsGeneric, %Generic.facet [concrete = constants.%Generic.facet]
384-
// CHECK:STDOUT: %PassThroughToGenericMethod.specific_fn: <specific function> = specific_function %PassThroughToGenericMethod.ref, @PassThroughToGenericMethod(constants.%GenericParam, %.loc28) [concrete = constants.%PassThroughToGenericMethod.specific_fn]
384+
// CHECK:STDOUT: %PassThroughToGenericMethod.specific_fn: <specific function> = specific_function %PassThroughToGenericMethod.ref, @PassThroughToGenericMethod(constants.%GenericParam, constants.%Generic.facet) [concrete = constants.%PassThroughToGenericMethod.specific_fn]
385385
// CHECK:STDOUT: %PassThroughToGenericMethod.call: init %empty_tuple.type = call %PassThroughToGenericMethod.specific_fn()
386386
// CHECK:STDOUT: return
387387
// CHECK:STDOUT: }
@@ -427,7 +427,7 @@ fn G() {
427427
// CHECK:STDOUT:
428428
// CHECK:STDOUT: specific @Generic(@CallGenericMethod.%T.loc17_22.2) {}
429429
// CHECK:STDOUT:
430-
// CHECK:STDOUT: specific @CallGenericMethod(constants.%GenericParam, @G.%.loc20) {
430+
// CHECK:STDOUT: specific @CallGenericMethod(constants.%GenericParam, constants.%Generic.facet) {
431431
// CHECK:STDOUT: %T.loc17_22.2 => constants.%GenericParam
432432
// CHECK:STDOUT: %T.patt.loc17_22.2 => constants.%GenericParam
433433
// CHECK:STDOUT: %Generic.type.loc17_45.2 => constants.%Generic.type.769
@@ -437,14 +437,6 @@ fn G() {
437437
// CHECK:STDOUT: !definition:
438438
// CHECK:STDOUT: }
439439
// CHECK:STDOUT:
440-
// CHECK:STDOUT: specific @CallGenericMethod(constants.%GenericParam, constants.%Generic.facet) {
441-
// CHECK:STDOUT: %T.loc17_22.2 => constants.%GenericParam
442-
// CHECK:STDOUT: %T.patt.loc17_22.2 => constants.%GenericParam
443-
// CHECK:STDOUT: %Generic.type.loc17_45.2 => constants.%Generic.type.769
444-
// CHECK:STDOUT: %U.loc17_32.2 => constants.%Generic.facet
445-
// CHECK:STDOUT: %U.patt.loc17_32.2 => constants.%Generic.facet
446-
// CHECK:STDOUT: }
447-
// CHECK:STDOUT:
448440
// CHECK:STDOUT: specific @PassThroughToGenericMethod(constants.%T, constants.%U) {
449441
// CHECK:STDOUT: %T.loc23_31.2 => constants.%T
450442
// CHECK:STDOUT: %T.patt.loc23_31.2 => constants.%T
@@ -457,7 +449,7 @@ fn G() {
457449
// CHECK:STDOUT:
458450
// CHECK:STDOUT: specific @CallGenericMethod(@PassThroughToGenericMethod.%T.loc23_31.2, @PassThroughToGenericMethod.%U.loc23_41.2) {}
459451
// CHECK:STDOUT:
460-
// CHECK:STDOUT: specific @PassThroughToGenericMethod(constants.%GenericParam, @H.%.loc28) {
452+
// CHECK:STDOUT: specific @PassThroughToGenericMethod(constants.%GenericParam, constants.%Generic.facet) {
461453
// CHECK:STDOUT: %T.loc23_31.2 => constants.%GenericParam
462454
// CHECK:STDOUT: %T.patt.loc23_31.2 => constants.%GenericParam
463455
// CHECK:STDOUT: %Generic.type.loc23_54.2 => constants.%Generic.type.769
@@ -468,14 +460,6 @@ fn G() {
468460
// CHECK:STDOUT: %CallGenericMethod.specific_fn.loc24_3.2 => constants.%CallGenericMethod.specific_fn.934
469461
// CHECK:STDOUT: }
470462
// CHECK:STDOUT:
471-
// CHECK:STDOUT: specific @PassThroughToGenericMethod(constants.%GenericParam, constants.%Generic.facet) {
472-
// CHECK:STDOUT: %T.loc23_31.2 => constants.%GenericParam
473-
// CHECK:STDOUT: %T.patt.loc23_31.2 => constants.%GenericParam
474-
// CHECK:STDOUT: %Generic.type.loc23_54.2 => constants.%Generic.type.769
475-
// CHECK:STDOUT: %U.loc23_41.2 => constants.%Generic.facet
476-
// CHECK:STDOUT: %U.patt.loc23_41.2 => constants.%Generic.facet
477-
// CHECK:STDOUT: }
478-
// CHECK:STDOUT:
479463
// CHECK:STDOUT: --- generic_facet_type_from_implicit_param.carbon
480464
// CHECK:STDOUT:
481465
// CHECK:STDOUT: constants {
@@ -652,7 +636,7 @@ fn G() {
652636
// CHECK:STDOUT: %.loc20_38.1: ref %GenericParam = converted %.loc20_36.1, %.loc20_36.4
653637
// CHECK:STDOUT: %Generic.facet: %Generic.type.769 = facet_value constants.%ImplsGeneric, (constants.%impl_witness) [concrete = constants.%Generic.facet]
654638
// CHECK:STDOUT: %.loc20_53: %Generic.type.769 = converted constants.%ImplsGeneric, %Generic.facet [concrete = constants.%Generic.facet]
655-
// CHECK:STDOUT: %CallGenericMethod.specific_fn: <specific function> = specific_function %CallGenericMethod.ref, @CallGenericMethod(constants.%GenericParam, %.loc20_53) [concrete = constants.%CallGenericMethod.specific_fn]
639+
// CHECK:STDOUT: %CallGenericMethod.specific_fn: <specific function> = specific_function %CallGenericMethod.ref, @CallGenericMethod(constants.%GenericParam, constants.%Generic.facet) [concrete = constants.%CallGenericMethod.specific_fn]
656640
// CHECK:STDOUT: %.loc20_38.2: %GenericParam = bind_value %.loc20_38.1
657641
// CHECK:STDOUT: %CallGenericMethod.call: init %empty_tuple.type = call %CallGenericMethod.specific_fn(%.loc20_38.2)
658642
// CHECK:STDOUT: return
@@ -697,7 +681,7 @@ fn G() {
697681
// CHECK:STDOUT:
698682
// CHECK:STDOUT: specific @Generic(@CallGenericMethod.%T.loc17_22.2) {}
699683
// CHECK:STDOUT:
700-
// CHECK:STDOUT: specific @CallGenericMethod(constants.%GenericParam, @G.%.loc20_53) {
684+
// CHECK:STDOUT: specific @CallGenericMethod(constants.%GenericParam, constants.%Generic.facet) {
701685
// CHECK:STDOUT: %T.loc17_22.2 => constants.%GenericParam
702686
// CHECK:STDOUT: %T.patt.loc17_22.2 => constants.%GenericParam
703687
// CHECK:STDOUT: %Generic.type.loc17_45.2 => constants.%Generic.type.769
@@ -708,11 +692,3 @@ fn G() {
708692
// CHECK:STDOUT: %require_complete => constants.%complete_type
709693
// CHECK:STDOUT: }
710694
// CHECK:STDOUT:
711-
// CHECK:STDOUT: specific @CallGenericMethod(constants.%GenericParam, constants.%Generic.facet) {
712-
// CHECK:STDOUT: %T.loc17_22.2 => constants.%GenericParam
713-
// CHECK:STDOUT: %T.patt.loc17_22.2 => constants.%GenericParam
714-
// CHECK:STDOUT: %Generic.type.loc17_45.2 => constants.%Generic.type.769
715-
// CHECK:STDOUT: %U.loc17_32.2 => constants.%Generic.facet
716-
// CHECK:STDOUT: %U.patt.loc17_32.2 => constants.%Generic.facet
717-
// CHECK:STDOUT: }
718-
// CHECK:STDOUT:

toolchain/check/testdata/builtin_conversions/no_prelude/convert_class_value_to_generic_facet_value_value.carbon

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ fn B() {
432432
// CHECK:STDOUT: %.loc22_44.1: ref %GenericParam = converted %.loc22_42.1, %.loc22_42.4
433433
// CHECK:STDOUT: %Generic.facet: %Generic.type.769 = facet_value constants.%ImplsGeneric, (constants.%impl_witness) [concrete = constants.%Generic.facet.8bd]
434434
// CHECK:STDOUT: %.loc22_59: %Generic.type.769 = converted constants.%ImplsGeneric, %Generic.facet [concrete = constants.%Generic.facet.8bd]
435-
// CHECK:STDOUT: %CallGenericMethod.specific_fn: <specific function> = specific_function %CallGenericMethod.ref, @CallGenericMethod(constants.%GenericParam, %.loc22_59) [concrete = constants.%CallGenericMethod.specific_fn]
435+
// CHECK:STDOUT: %CallGenericMethod.specific_fn: <specific function> = specific_function %CallGenericMethod.ref, @CallGenericMethod(constants.%GenericParam, constants.%Generic.facet.8bd) [concrete = constants.%CallGenericMethod.specific_fn]
436436
// CHECK:STDOUT: %.loc22_24.2: %ImplsGeneric = bind_value %.loc22_24.1
437437
// CHECK:STDOUT: %.loc22_44.2: %GenericParam = bind_value %.loc22_44.1
438438
// CHECK:STDOUT: %CallGenericMethod.call: init %empty_tuple.type = call %CallGenericMethod.specific_fn(%.loc22_24.2, %.loc22_44.2)
@@ -491,7 +491,7 @@ fn B() {
491491
// CHECK:STDOUT:
492492
// CHECK:STDOUT: specific @F.1(@CallGenericMethod.%T.loc17_22.2, @CallGenericMethod.%Generic.facet) {}
493493
// CHECK:STDOUT:
494-
// CHECK:STDOUT: specific @CallGenericMethod(constants.%GenericParam, @G.%.loc22_59) {
494+
// CHECK:STDOUT: specific @CallGenericMethod(constants.%GenericParam, constants.%Generic.facet.8bd) {
495495
// CHECK:STDOUT: %T.loc17_22.2 => constants.%GenericParam
496496
// CHECK:STDOUT: %T.patt.loc17_22.2 => constants.%GenericParam
497497
// CHECK:STDOUT: %Generic.type.loc17_45.2 => constants.%Generic.type.769
@@ -513,15 +513,6 @@ fn B() {
513513
// CHECK:STDOUT: %specific_fn.loc18_4.2 => constants.%F.specific_fn
514514
// CHECK:STDOUT: }
515515
// CHECK:STDOUT:
516-
// CHECK:STDOUT: specific @CallGenericMethod(constants.%GenericParam, constants.%Generic.facet.8bd) {
517-
// CHECK:STDOUT: %T.loc17_22.2 => constants.%GenericParam
518-
// CHECK:STDOUT: %T.patt.loc17_22.2 => constants.%GenericParam
519-
// CHECK:STDOUT: %Generic.type.loc17_45.2 => constants.%Generic.type.769
520-
// CHECK:STDOUT: %U.loc17_32.2 => constants.%Generic.facet.8bd
521-
// CHECK:STDOUT: %U.patt.loc17_32.2 => constants.%Generic.facet.8bd
522-
// CHECK:STDOUT: %U.as_type.loc17_51.2 => constants.%ImplsGeneric
523-
// CHECK:STDOUT: }
524-
// CHECK:STDOUT:
525516
// CHECK:STDOUT: --- multiple_generic_params_one_fixed_one_deduced.carbon
526517
// CHECK:STDOUT:
527518
// CHECK:STDOUT: constants {

toolchain/check/testdata/builtin_conversions/no_prelude/convert_facet_value_value_to_generic_facet_value_value.carbon

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ fn F() {
452452
// CHECK:STDOUT: %.loc23_76.7: %Animal.type = converted constants.%T.as_type.2ad, constants.%T.fd4 [symbolic = %T.loc23_17.2 (constants.%T.fd4)]
453453
// CHECK:STDOUT: %Eats.facet.loc23_76.1: @HandleAnimal.%Eats.type (%Eats.type.f54c3d.2) = facet_value constants.%T.as_type.2ad, (constants.%impl_witness.c7c36b.2) [symbolic = %Eats.facet.loc23_76.2 (constants.%Eats.facet.97e)]
454454
// CHECK:STDOUT: %.loc23_76.8: @HandleAnimal.%Eats.type (%Eats.type.f54c3d.2) = converted constants.%T.as_type.2ad, %Eats.facet.loc23_76.1 [symbolic = %Eats.facet.loc23_76.2 (constants.%Eats.facet.97e)]
455-
// CHECK:STDOUT: %Feed.specific_fn.loc23_64.1: <specific function> = specific_function %Feed.ref, @Feed(constants.%Food.5fe, %.loc23_76.8) [symbolic = %Feed.specific_fn.loc23_64.2 (constants.%Feed.specific_fn.387)]
455+
// CHECK:STDOUT: %Feed.specific_fn.loc23_64.1: <specific function> = specific_function %Feed.ref, @Feed(constants.%Food.5fe, constants.%Eats.facet.97e) [symbolic = %Feed.specific_fn.loc23_64.2 (constants.%Feed.specific_fn.387)]
456456
// CHECK:STDOUT: %Feed.call: init %empty_tuple.type = call %Feed.specific_fn.loc23_64.1(%a.ref, %food.ref)
457457
// CHECK:STDOUT: return
458458
// CHECK:STDOUT: }
@@ -569,7 +569,7 @@ fn F() {
569569
// CHECK:STDOUT: !definition:
570570
// CHECK:STDOUT: }
571571
// CHECK:STDOUT:
572-
// CHECK:STDOUT: specific @Feed(constants.%Food.5fe, @HandleAnimal.%.loc23_76.8) {
572+
// CHECK:STDOUT: specific @Feed(constants.%Food.5fe, constants.%Eats.facet.97e) {
573573
// CHECK:STDOUT: %Food.loc22_9.2 => constants.%Food.5fe
574574
// CHECK:STDOUT: %Food.patt.loc22_9.2 => constants.%Food.5fe
575575
// CHECK:STDOUT: %Food.as_type.loc22_37.2 => constants.%Food.as_type.fae
@@ -583,16 +583,6 @@ fn F() {
583583
// CHECK:STDOUT: %require_complete.loc22_50 => constants.%require_complete.444
584584
// CHECK:STDOUT: }
585585
// CHECK:STDOUT:
586-
// CHECK:STDOUT: specific @Feed(constants.%Food.5fe, constants.%Eats.facet.97e) {
587-
// CHECK:STDOUT: %Food.loc22_9.2 => constants.%Food.5fe
588-
// CHECK:STDOUT: %Food.patt.loc22_9.2 => constants.%Food.5fe
589-
// CHECK:STDOUT: %Food.as_type.loc22_37.2 => constants.%Food.as_type.fae
590-
// CHECK:STDOUT: %Eats.type.loc22_37.2 => constants.%Eats.type.f54c3d.2
591-
// CHECK:STDOUT: %T.loc22_24.2 => constants.%Eats.facet.97e
592-
// CHECK:STDOUT: %T.patt.loc22_24.2 => constants.%Eats.facet.97e
593-
// CHECK:STDOUT: %T.as_type.loc22_43.2 => constants.%T.as_type.2ad
594-
// CHECK:STDOUT: }
595-
// CHECK:STDOUT:
596586
// CHECK:STDOUT: specific @Eats(@HandleAnimal.%Food.as_type.loc23_56.2) {}
597587
// CHECK:STDOUT:
598588
// CHECK:STDOUT: specific @impl.009(@HandleAnimal.%T.loc23_17.2, @HandleAnimal.%Food.loc23_29.2) {}

toolchain/check/testdata/function/generic/call_method_on_generic_facet.carbon

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ fn G() {
273273
// CHECK:STDOUT: %ImplsGeneric.ref: type = name_ref ImplsGeneric, file.%ImplsGeneric.decl [concrete = constants.%ImplsGeneric]
274274
// CHECK:STDOUT: %Generic.facet: %Generic.type.769 = facet_value constants.%ImplsGeneric, (constants.%impl_witness.b42) [concrete = constants.%Generic.facet.8bd]
275275
// CHECK:STDOUT: %.loc34: %Generic.type.769 = converted constants.%ImplsGeneric, %Generic.facet [concrete = constants.%Generic.facet.8bd]
276-
// CHECK:STDOUT: %CallGenericMethod.specific_fn: <specific function> = specific_function %CallGenericMethod.ref, @CallGenericMethod(constants.%GenericParam, %.loc34) [concrete = constants.%CallGenericMethod.specific_fn]
276+
// CHECK:STDOUT: %CallGenericMethod.specific_fn: <specific function> = specific_function %CallGenericMethod.ref, @CallGenericMethod(constants.%GenericParam, constants.%Generic.facet.8bd) [concrete = constants.%CallGenericMethod.specific_fn]
277277
// CHECK:STDOUT: %CallGenericMethod.call: init %empty_tuple.type = call %CallGenericMethod.specific_fn()
278278
// CHECK:STDOUT: return
279279
// CHECK:STDOUT: }
@@ -333,7 +333,7 @@ fn G() {
333333
// CHECK:STDOUT:
334334
// CHECK:STDOUT: specific @F.1(@CallGenericMethod.%T.loc29_22.2, @CallGenericMethod.%Generic.facet) {}
335335
// CHECK:STDOUT:
336-
// CHECK:STDOUT: specific @CallGenericMethod(constants.%GenericParam, @G.3.%.loc34) {
336+
// CHECK:STDOUT: specific @CallGenericMethod(constants.%GenericParam, constants.%Generic.facet.8bd) {
337337
// CHECK:STDOUT: %T.loc29_22.2 => constants.%GenericParam
338338
// CHECK:STDOUT: %T.patt.loc29_22.2 => constants.%GenericParam
339339
// CHECK:STDOUT: %Generic.type.loc29_45.2 => constants.%Generic.type.769
@@ -353,11 +353,3 @@ fn G() {
353353
// CHECK:STDOUT: %specific_fn.loc30_4.2 => constants.%F.specific_fn
354354
// CHECK:STDOUT: }
355355
// CHECK:STDOUT:
356-
// CHECK:STDOUT: specific @CallGenericMethod(constants.%GenericParam, constants.%Generic.facet.8bd) {
357-
// CHECK:STDOUT: %T.loc29_22.2 => constants.%GenericParam
358-
// CHECK:STDOUT: %T.patt.loc29_22.2 => constants.%GenericParam
359-
// CHECK:STDOUT: %Generic.type.loc29_45.2 => constants.%Generic.type.769
360-
// CHECK:STDOUT: %U.loc29_32.2 => constants.%Generic.facet.8bd
361-
// CHECK:STDOUT: %U.patt.loc29_32.2 => constants.%Generic.facet.8bd
362-
// CHECK:STDOUT: }
363-
// CHECK:STDOUT:

0 commit comments

Comments
 (0)