mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-30 15:55:03 +01:00
Non-entry-block allocas will allocate new stack memory each time they're reached, resulting in leaking stack memory over time for allocas in a loop. Move all such allocas to the entry block instead, and use an LLVM intrinsic to mark when the lifetime of the variable actually begins.
140 lines
6.5 KiB
Plaintext
140 lines
6.5 KiB
Plaintext
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
|
|
// Exceptions. See /LICENSE for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
//
|
|
// AUTOUPDATE
|
|
// TIP: To test this file alone, run:
|
|
// TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/lower/testdata/class/adapt.carbon
|
|
// TIP: To dump output, run:
|
|
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/lower/testdata/class/adapt.carbon
|
|
|
|
// --- adapt_class.carbon
|
|
|
|
library "[[@TEST_NAME]]";
|
|
|
|
class PairOfInts {
|
|
var a: i32;
|
|
var b: i32;
|
|
|
|
fn Make() -> Self {
|
|
return {.a = 1, .b = 2};
|
|
}
|
|
}
|
|
|
|
class PairAdapter {
|
|
adapt PairOfInts;
|
|
|
|
fn Make() -> Self {
|
|
return PairOfInts.Make() as Self;
|
|
}
|
|
|
|
fn GetB[self: Self]() -> i32 {
|
|
let pi: PairOfInts = self as PairOfInts;
|
|
return pi.b;
|
|
}
|
|
}
|
|
|
|
fn Use() -> i32 {
|
|
var pa: PairAdapter = PairAdapter.Make();
|
|
return pa.GetB();
|
|
}
|
|
|
|
// --- adapt_int.carbon
|
|
|
|
library "[[@TEST_NAME]]";
|
|
|
|
class Int {
|
|
adapt i32;
|
|
}
|
|
|
|
fn DoStuff(a: Int) -> Int {
|
|
return a;
|
|
}
|
|
|
|
// CHECK:STDOUT: ; ModuleID = 'adapt_class.carbon'
|
|
// CHECK:STDOUT: source_filename = "adapt_class.carbon"
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: @PairOfInts.val.loc9_28 = internal constant { i32, i32 } { i32 1, i32 2 }
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: define void @_CMake.PairOfInts.Main(ptr sret({ i32, i32 }) %return) !dbg !4 {
|
|
// CHECK:STDOUT: entry:
|
|
// CHECK:STDOUT: %.loc9_27.3.a = getelementptr inbounds nuw { i32, i32 }, ptr %return, i32 0, i32 0, !dbg !7
|
|
// CHECK:STDOUT: %.loc9_27.6.b = getelementptr inbounds nuw { i32, i32 }, ptr %return, i32 0, i32 1, !dbg !7
|
|
// CHECK:STDOUT: call void @llvm.memcpy.p0.p0.i64(ptr align 4 %return, ptr align 4 @PairOfInts.val.loc9_28, i64 8, i1 false), !dbg !8
|
|
// CHECK:STDOUT: ret void, !dbg !8
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: define void @_CMake.PairAdapter.Main(ptr sret({ i32, i32 }) %return) !dbg !9 {
|
|
// CHECK:STDOUT: entry:
|
|
// CHECK:STDOUT: call void @_CMake.PairOfInts.Main(ptr %return), !dbg !10
|
|
// CHECK:STDOUT: ret void, !dbg !11
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: define i32 @_CGetB.PairAdapter.Main(ptr %self) !dbg !12 {
|
|
// CHECK:STDOUT: entry:
|
|
// CHECK:STDOUT: %.loc22_14.1.b = getelementptr inbounds nuw { i32, i32 }, ptr %self, i32 0, i32 1, !dbg !13
|
|
// CHECK:STDOUT: %.loc22_14.2 = load i32, ptr %.loc22_14.1.b, align 4, !dbg !13
|
|
// CHECK:STDOUT: ret i32 %.loc22_14.2, !dbg !14
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: define i32 @_CUse.Main() !dbg !15 {
|
|
// CHECK:STDOUT: entry:
|
|
// CHECK:STDOUT: %pa.var = alloca { i32, i32 }, align 8, !dbg !16
|
|
// CHECK:STDOUT: call void @llvm.lifetime.start.p0(i64 8, ptr %pa.var), !dbg !16
|
|
// CHECK:STDOUT: call void @_CMake.PairAdapter.Main(ptr %pa.var), !dbg !17
|
|
// CHECK:STDOUT: %GetB.call = call i32 @_CGetB.PairAdapter.Main(ptr %pa.var), !dbg !18
|
|
// CHECK:STDOUT: ret i32 %GetB.call, !dbg !19
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: ; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
|
|
// CHECK:STDOUT: declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #0
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: ; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
|
|
// CHECK:STDOUT: declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: attributes #0 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
|
|
// CHECK:STDOUT: attributes #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: !llvm.module.flags = !{!0, !1}
|
|
// CHECK:STDOUT: !llvm.dbg.cu = !{!2}
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: !0 = !{i32 7, !"Dwarf Version", i32 5}
|
|
// CHECK:STDOUT: !1 = !{i32 2, !"Debug Info Version", i32 3}
|
|
// CHECK:STDOUT: !2 = distinct !DICompileUnit(language: DW_LANG_C, file: !3, producer: "carbon", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug)
|
|
// CHECK:STDOUT: !3 = !DIFile(filename: "adapt_class.carbon", directory: "")
|
|
// CHECK:STDOUT: !4 = distinct !DISubprogram(name: "Make", linkageName: "_CMake.PairOfInts.Main", scope: null, file: !3, line: 8, type: !5, spFlags: DISPFlagDefinition, unit: !2)
|
|
// CHECK:STDOUT: !5 = !DISubroutineType(types: !6)
|
|
// CHECK:STDOUT: !6 = !{}
|
|
// CHECK:STDOUT: !7 = !DILocation(line: 9, column: 12, scope: !4)
|
|
// CHECK:STDOUT: !8 = !DILocation(line: 9, column: 5, scope: !4)
|
|
// CHECK:STDOUT: !9 = distinct !DISubprogram(name: "Make", linkageName: "_CMake.PairAdapter.Main", scope: null, file: !3, line: 16, type: !5, spFlags: DISPFlagDefinition, unit: !2)
|
|
// CHECK:STDOUT: !10 = !DILocation(line: 17, column: 12, scope: !9)
|
|
// CHECK:STDOUT: !11 = !DILocation(line: 17, column: 5, scope: !9)
|
|
// CHECK:STDOUT: !12 = distinct !DISubprogram(name: "GetB", linkageName: "_CGetB.PairAdapter.Main", scope: null, file: !3, line: 20, type: !5, spFlags: DISPFlagDefinition, unit: !2)
|
|
// CHECK:STDOUT: !13 = !DILocation(line: 22, column: 12, scope: !12)
|
|
// CHECK:STDOUT: !14 = !DILocation(line: 22, column: 5, scope: !12)
|
|
// CHECK:STDOUT: !15 = distinct !DISubprogram(name: "Use", linkageName: "_CUse.Main", scope: null, file: !3, line: 26, type: !5, spFlags: DISPFlagDefinition, unit: !2)
|
|
// CHECK:STDOUT: !16 = !DILocation(line: 27, column: 7, scope: !15)
|
|
// CHECK:STDOUT: !17 = !DILocation(line: 27, column: 25, scope: !15)
|
|
// CHECK:STDOUT: !18 = !DILocation(line: 28, column: 10, scope: !15)
|
|
// CHECK:STDOUT: !19 = !DILocation(line: 28, column: 3, scope: !15)
|
|
// CHECK:STDOUT: ; ModuleID = 'adapt_int.carbon'
|
|
// CHECK:STDOUT: source_filename = "adapt_int.carbon"
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: define i32 @_CDoStuff.Main(i32 %a) !dbg !4 {
|
|
// CHECK:STDOUT: entry:
|
|
// CHECK:STDOUT: ret i32 %a, !dbg !7
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: !llvm.module.flags = !{!0, !1}
|
|
// CHECK:STDOUT: !llvm.dbg.cu = !{!2}
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: !0 = !{i32 7, !"Dwarf Version", i32 5}
|
|
// CHECK:STDOUT: !1 = !{i32 2, !"Debug Info Version", i32 3}
|
|
// CHECK:STDOUT: !2 = distinct !DICompileUnit(language: DW_LANG_C, file: !3, producer: "carbon", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug)
|
|
// CHECK:STDOUT: !3 = !DIFile(filename: "adapt_int.carbon", directory: "")
|
|
// CHECK:STDOUT: !4 = distinct !DISubprogram(name: "DoStuff", linkageName: "_CDoStuff.Main", scope: null, file: !3, line: 8, type: !5, spFlags: DISPFlagDefinition, unit: !2)
|
|
// CHECK:STDOUT: !5 = !DISubroutineType(types: !6)
|
|
// CHECK:STDOUT: !6 = !{}
|
|
// CHECK:STDOUT: !7 = !DILocation(line: 9, column: 3, scope: !4)
|