Fix C++ thunk triggering for functions with default args which return a simple type (#6152)

Before this change, we wrongly ignore the decision to generate a thunk
for a function with default args by overriding this decision with the
fact the return type by itself doesn't require a thunk.
This causes not generating a thunk which leads to crashing in lowering.
Add tests that show that now thunk is generated in `check` and it no
longer crashes in `lower`.

Follow up of #6108.
This commit is contained in:
Boaz Brickner
2025-10-02 15:16:28 +00:00
committed by GitHub
parent 16999a79cc
commit 57c0fde145
3 changed files with 491 additions and 295 deletions
+60 -18
View File
@@ -112,7 +112,8 @@ fn MyF() {
// --- with_default_args.h
void F(int a = 1, int b = 2);
void NoReturnValue(int a = 1, int b = 2);
int SimpleReturnValue(int a = 1, int b = 2);
// --- call_with_default_args.carbon
@@ -121,9 +122,13 @@ library "[[@TEST_NAME]]";
import Cpp library "with_default_args.h";
fn MyF() {
Cpp.F();
Cpp.F(3);
Cpp.F(3, 4);
Cpp.NoReturnValue();
Cpp.NoReturnValue(3);
Cpp.NoReturnValue(3, 4);
var value: i32 = Cpp.SimpleReturnValue();
value = Cpp.SimpleReturnValue(3);
value = Cpp.SimpleReturnValue(3, 4);
}
// CHECK:STDOUT: ; ModuleID = 'import_function_decl.carbon'
@@ -334,32 +339,63 @@ fn MyF() {
// CHECK:STDOUT:
// CHECK:STDOUT: define void @_CMyF.Main() !dbg !7 {
// CHECK:STDOUT: entry:
// CHECK:STDOUT: call void @_Z1Fii.carbon_thunk0(), !dbg !10
// CHECK:STDOUT: call void @_Z1Fii.carbon_thunk1(i32 3), !dbg !11
// CHECK:STDOUT: call void @_Z1Fii(i32 3, i32 4), !dbg !12
// CHECK:STDOUT: ret void, !dbg !13
// CHECK:STDOUT: %value.var = alloca i32, align 4, !dbg !10
// CHECK:STDOUT: call void @_Z13NoReturnValueii.carbon_thunk0(), !dbg !11
// CHECK:STDOUT: call void @_Z13NoReturnValueii.carbon_thunk1(i32 3), !dbg !12
// CHECK:STDOUT: call void @_Z13NoReturnValueii(i32 3, i32 4), !dbg !13
// CHECK:STDOUT: call void @llvm.lifetime.start.p0(ptr %value.var), !dbg !10
// CHECK:STDOUT: %SimpleReturnValue__carbon_thunk.call.loc11 = call i32 @_Z17SimpleReturnValueii.carbon_thunk0(), !dbg !14
// CHECK:STDOUT: store i32 %SimpleReturnValue__carbon_thunk.call.loc11, ptr %value.var, align 4, !dbg !10
// CHECK:STDOUT: %SimpleReturnValue__carbon_thunk.call.loc12 = call i32 @_Z17SimpleReturnValueii.carbon_thunk1(i32 3), !dbg !15
// CHECK:STDOUT: store i32 %SimpleReturnValue__carbon_thunk.call.loc12, ptr %value.var, align 4, !dbg !16
// CHECK:STDOUT: %SimpleReturnValue.call = call i32 @_Z17SimpleReturnValueii(i32 3, i32 4), !dbg !17
// CHECK:STDOUT: store i32 %SimpleReturnValue.call, ptr %value.var, align 4, !dbg !18
// CHECK:STDOUT: ret void, !dbg !19
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: declare void @_Z1Fii(i32, i32)
// CHECK:STDOUT: declare void @_Z13NoReturnValueii(i32, i32)
// CHECK:STDOUT:
// CHECK:STDOUT: declare i32 @_Z17SimpleReturnValueii(i32, i32)
// CHECK:STDOUT:
// CHECK:STDOUT: ; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
// CHECK:STDOUT: declare void @llvm.lifetime.start.p0(ptr captures(none)) #0
// CHECK:STDOUT:
// CHECK:STDOUT: ; Function Attrs: alwaysinline mustprogress
// CHECK:STDOUT: define dso_local void @_Z1Fii.carbon_thunk0() #0 {
// CHECK:STDOUT: define dso_local void @_Z13NoReturnValueii.carbon_thunk0() #1 {
// CHECK:STDOUT: entry:
// CHECK:STDOUT: call void @_Z1Fii(i32 1, i32 2)
// CHECK:STDOUT: call void @_Z13NoReturnValueii(i32 1, i32 2)
// CHECK:STDOUT: ret void
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: ; Function Attrs: alwaysinline mustprogress
// CHECK:STDOUT: define dso_local void @_Z1Fii.carbon_thunk1(i32 %a) #0 {
// CHECK:STDOUT: define dso_local void @_Z13NoReturnValueii.carbon_thunk1(i32 %a) #1 {
// CHECK:STDOUT: entry:
// CHECK:STDOUT: %a.addr = alloca i32, align 4
// CHECK:STDOUT: store i32 %a, ptr %a.addr, align 4
// CHECK:STDOUT: %0 = load i32, ptr %a.addr, align 4
// CHECK:STDOUT: call void @_Z1Fii(i32 %0, i32 2)
// CHECK:STDOUT: call void @_Z13NoReturnValueii(i32 %0, i32 2)
// CHECK:STDOUT: ret void
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: attributes #0 = { alwaysinline mustprogress "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="0" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
// CHECK:STDOUT: ; Function Attrs: alwaysinline mustprogress
// CHECK:STDOUT: define dso_local i32 @_Z17SimpleReturnValueii.carbon_thunk0() #1 {
// CHECK:STDOUT: entry:
// CHECK:STDOUT: %call = call i32 @_Z17SimpleReturnValueii(i32 1, i32 2)
// CHECK:STDOUT: ret i32 %call
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: ; Function Attrs: alwaysinline mustprogress
// CHECK:STDOUT: define dso_local i32 @_Z17SimpleReturnValueii.carbon_thunk1(i32 %a) #1 {
// CHECK:STDOUT: entry:
// CHECK:STDOUT: %a.addr = alloca i32, align 4
// CHECK:STDOUT: store i32 %a, ptr %a.addr, align 4
// CHECK:STDOUT: %0 = load i32, ptr %a.addr, align 4
// CHECK:STDOUT: %call = call i32 @_Z17SimpleReturnValueii(i32 %0, i32 2)
// CHECK:STDOUT: ret i32 %call
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
// CHECK:STDOUT: attributes #1 = { alwaysinline mustprogress "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="0" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
// CHECK:STDOUT:
// CHECK:STDOUT: !llvm.module.flags = !{!0, !1, !2, !3, !4}
// CHECK:STDOUT: !llvm.dbg.cu = !{!5}
@@ -374,7 +410,13 @@ fn MyF() {
// CHECK:STDOUT: !7 = distinct !DISubprogram(name: "MyF", linkageName: "_CMyF.Main", scope: null, file: !6, line: 6, type: !8, spFlags: DISPFlagDefinition, unit: !5)
// CHECK:STDOUT: !8 = !DISubroutineType(types: !9)
// CHECK:STDOUT: !9 = !{}
// CHECK:STDOUT: !10 = !DILocation(line: 7, column: 3, scope: !7)
// CHECK:STDOUT: !11 = !DILocation(line: 8, column: 3, scope: !7)
// CHECK:STDOUT: !12 = !DILocation(line: 9, column: 3, scope: !7)
// CHECK:STDOUT: !13 = !DILocation(line: 6, column: 1, scope: !7)
// CHECK:STDOUT: !10 = !DILocation(line: 11, column: 3, scope: !7)
// CHECK:STDOUT: !11 = !DILocation(line: 7, column: 3, scope: !7)
// CHECK:STDOUT: !12 = !DILocation(line: 8, column: 3, scope: !7)
// CHECK:STDOUT: !13 = !DILocation(line: 9, column: 3, scope: !7)
// CHECK:STDOUT: !14 = !DILocation(line: 11, column: 20, scope: !7)
// CHECK:STDOUT: !15 = !DILocation(line: 12, column: 11, scope: !7)
// CHECK:STDOUT: !16 = !DILocation(line: 12, column: 3, scope: !7)
// CHECK:STDOUT: !17 = !DILocation(line: 13, column: 11, scope: !7)
// CHECK:STDOUT: !18 = !DILocation(line: 13, column: 3, scope: !7)
// CHECK:STDOUT: !19 = !DILocation(line: 6, column: 1, scope: !7)