Implement thunking in terms of constant evaluation (#7332)

The bulk of this change is changing most pattern insts to be `Always`
rather than `AlwaysUnique` constants, so that they can be wrapped in
`SpecificConstant`s to perform substitution. That then lets thunking
rely much more on `SpecificConstant` wrappers instead of deep-copying
the inst tree with modified types.

This approach to thunking should scale better, particularly as things
like form generics make function signatures more complex, because we can
leverage the existing support for constant evaluation and substitution.
Unfortunately, applying this approach to binding patterns will require
more work; see the TODO near the top of `thunk.cpp` for details.

---------

Co-authored-by: Richard Smith <richard@metafoo.co.uk>
This commit is contained in:
Geoff Romer
2026-06-13 00:22:00 +00:00
committed by GitHub
co-authored by Richard Smith
parent 40d2d8f68c
commit e023f75254
589 changed files with 22583 additions and 11517 deletions
+24 -24
View File
@@ -147,20 +147,20 @@ fn Driver(x: Cpp.A, y: Cpp.A) {
// CHECK:STDOUT: ; Function Attrs: alwaysinline nounwind
// CHECK:STDOUT: define i1 @"_CEqual:thunk:EqWith.ffb401eeee8c4872.Core:Cpp"(ptr %self, ptr %other) #2 !dbg !28 {
// CHECK:STDOUT: entry:
// CHECK:STDOUT: %.3.temp = alloca i1, align 1, !dbg !34
// CHECK:STDOUT: call void @llvm.lifetime.start.p0(ptr %.3.temp), !dbg !34
// CHECK:STDOUT: call void @_Zeq1AS_.carbon_thunk.__(ptr %self, ptr %other, ptr %.3.temp), !dbg !34
// CHECK:STDOUT: %0 = load i1, ptr %.3.temp, align 1, !dbg !34
// CHECK:STDOUT: %.6.temp = alloca i1, align 1, !dbg !34
// CHECK:STDOUT: call void @llvm.lifetime.start.p0(ptr %.6.temp), !dbg !34
// CHECK:STDOUT: call void @_Zeq1AS_.carbon_thunk.__(ptr %self, ptr %other, ptr %.6.temp), !dbg !34
// CHECK:STDOUT: %0 = load i1, ptr %.6.temp, align 1, !dbg !34
// CHECK:STDOUT: ret i1 %0, !dbg !34
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: ; Function Attrs: alwaysinline nounwind
// CHECK:STDOUT: define i1 @"_CNotEqual:thunk:EqWith.ffb401eeee8c4872.Core:Cpp"(ptr %self, ptr %other) #2 !dbg !35 {
// CHECK:STDOUT: entry:
// CHECK:STDOUT: %.3.temp = alloca i1, align 1, !dbg !39
// CHECK:STDOUT: call void @llvm.lifetime.start.p0(ptr %.3.temp), !dbg !39
// CHECK:STDOUT: call void @_Zne1AS_.carbon_thunk.__(ptr %self, ptr %other, ptr %.3.temp), !dbg !39
// CHECK:STDOUT: %0 = load i1, ptr %.3.temp, align 1, !dbg !39
// CHECK:STDOUT: %.6.temp = alloca i1, align 1, !dbg !39
// CHECK:STDOUT: call void @llvm.lifetime.start.p0(ptr %.6.temp), !dbg !39
// CHECK:STDOUT: call void @_Zne1AS_.carbon_thunk.__(ptr %self, ptr %other, ptr %.6.temp), !dbg !39
// CHECK:STDOUT: %0 = load i1, ptr %.6.temp, align 1, !dbg !39
// CHECK:STDOUT: ret i1 %0, !dbg !39
// CHECK:STDOUT: }
// CHECK:STDOUT:
@@ -409,40 +409,40 @@ fn Driver(x: Cpp.A, y: Cpp.A) {
// CHECK:STDOUT: ; Function Attrs: alwaysinline nounwind
// CHECK:STDOUT: define i1 @"_CLess:thunk:OrderedWith.92c0df05bd55f97a.Core:Cpp"(ptr %self, ptr %other) #2 !dbg !28 {
// CHECK:STDOUT: entry:
// CHECK:STDOUT: %.3.temp = alloca i1, align 1, !dbg !34
// CHECK:STDOUT: call void @llvm.lifetime.start.p0(ptr %.3.temp), !dbg !34
// CHECK:STDOUT: call void @_Zlt1AS_.carbon_thunk.__(ptr %self, ptr %other, ptr %.3.temp), !dbg !34
// CHECK:STDOUT: %0 = load i1, ptr %.3.temp, align 1, !dbg !34
// CHECK:STDOUT: %.6.temp = alloca i1, align 1, !dbg !34
// CHECK:STDOUT: call void @llvm.lifetime.start.p0(ptr %.6.temp), !dbg !34
// CHECK:STDOUT: call void @_Zlt1AS_.carbon_thunk.__(ptr %self, ptr %other, ptr %.6.temp), !dbg !34
// CHECK:STDOUT: %0 = load i1, ptr %.6.temp, align 1, !dbg !34
// CHECK:STDOUT: ret i1 %0, !dbg !34
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: ; Function Attrs: alwaysinline nounwind
// CHECK:STDOUT: define i1 @"_CLessOrEquivalent:thunk:OrderedWith.92c0df05bd55f97a.Core:Cpp"(ptr %self, ptr %other) #2 !dbg !35 {
// CHECK:STDOUT: entry:
// CHECK:STDOUT: %.3.temp = alloca i1, align 1, !dbg !39
// CHECK:STDOUT: call void @llvm.lifetime.start.p0(ptr %.3.temp), !dbg !39
// CHECK:STDOUT: call void @_Zle1AS_.carbon_thunk.__(ptr %self, ptr %other, ptr %.3.temp), !dbg !39
// CHECK:STDOUT: %0 = load i1, ptr %.3.temp, align 1, !dbg !39
// CHECK:STDOUT: %.6.temp = alloca i1, align 1, !dbg !39
// CHECK:STDOUT: call void @llvm.lifetime.start.p0(ptr %.6.temp), !dbg !39
// CHECK:STDOUT: call void @_Zle1AS_.carbon_thunk.__(ptr %self, ptr %other, ptr %.6.temp), !dbg !39
// CHECK:STDOUT: %0 = load i1, ptr %.6.temp, align 1, !dbg !39
// CHECK:STDOUT: ret i1 %0, !dbg !39
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: ; Function Attrs: alwaysinline nounwind
// CHECK:STDOUT: define i1 @"_CGreater:thunk:OrderedWith.92c0df05bd55f97a.Core:Cpp"(ptr %self, ptr %other) #2 !dbg !40 {
// CHECK:STDOUT: entry:
// CHECK:STDOUT: %.3.temp = alloca i1, align 1, !dbg !44
// CHECK:STDOUT: call void @llvm.lifetime.start.p0(ptr %.3.temp), !dbg !44
// CHECK:STDOUT: call void @_Zgt1AS_.carbon_thunk.__(ptr %self, ptr %other, ptr %.3.temp), !dbg !44
// CHECK:STDOUT: %0 = load i1, ptr %.3.temp, align 1, !dbg !44
// CHECK:STDOUT: %.6.temp = alloca i1, align 1, !dbg !44
// CHECK:STDOUT: call void @llvm.lifetime.start.p0(ptr %.6.temp), !dbg !44
// CHECK:STDOUT: call void @_Zgt1AS_.carbon_thunk.__(ptr %self, ptr %other, ptr %.6.temp), !dbg !44
// CHECK:STDOUT: %0 = load i1, ptr %.6.temp, align 1, !dbg !44
// CHECK:STDOUT: ret i1 %0, !dbg !44
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: ; Function Attrs: alwaysinline nounwind
// CHECK:STDOUT: define i1 @"_CGreaterOrEquivalent:thunk:OrderedWith.92c0df05bd55f97a.Core:Cpp"(ptr %self, ptr %other) #2 !dbg !45 {
// CHECK:STDOUT: entry:
// CHECK:STDOUT: %.3.temp = alloca i1, align 1, !dbg !49
// CHECK:STDOUT: call void @llvm.lifetime.start.p0(ptr %.3.temp), !dbg !49
// CHECK:STDOUT: call void @_Zge1AS_.carbon_thunk.__(ptr %self, ptr %other, ptr %.3.temp), !dbg !49
// CHECK:STDOUT: %0 = load i1, ptr %.3.temp, align 1, !dbg !49
// CHECK:STDOUT: %.6.temp = alloca i1, align 1, !dbg !49
// CHECK:STDOUT: call void @llvm.lifetime.start.p0(ptr %.6.temp), !dbg !49
// CHECK:STDOUT: call void @_Zge1AS_.carbon_thunk.__(ptr %self, ptr %other, ptr %.6.temp), !dbg !49
// CHECK:STDOUT: %0 = load i1, ptr %.6.temp, align 1, !dbg !49
// CHECK:STDOUT: ret i1 %0, !dbg !49
// CHECK:STDOUT: }
// CHECK:STDOUT: