mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 19:20:13 +01:00
Instead of allowing lower to pick whatever type layout it desires, compute the layouts of types as part of completing the type, and make lower build types that match that representation. For now we assume that all pointers are 64-bit, since we don't have access to target information. We allow tail padding reuse for structs and tuple types (and by extension, for classes, since they use structs as their object representation), but not for arrays. In order to build matching LLVM types, we create LLVM packed structs where necessary, and we insert inter-field padding on the end of the previous field so that GEP indexes still always match Carbon's ElementIndexes. We don't yet use the computed alignment much in LLVM IR generation -- in particular, `alloca`s, `load`s, and `store`s should probably use the computed type alignment, but don't. Assisted-by: Gemini via Antigravity
115 lines
6.9 KiB
Plaintext
115 lines
6.9 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
|
|
//
|
|
// INCLUDE-FILE: toolchain/testing/testdata/min_prelude/primitives.carbon
|
|
//
|
|
// AUTOUPDATE
|
|
// TIP: To test this file alone, run:
|
|
// TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/lower/testdata/struct/member_access.carbon
|
|
// TIP: To dump output, run:
|
|
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/lower/testdata/struct/member_access.carbon
|
|
|
|
fn Run() -> i32 {
|
|
var x: {.a: f64, .b: i32} = {.a = 0.0, .b = 1};
|
|
var y: i32 = x.b;
|
|
var _: i32 = y;
|
|
return 0;
|
|
}
|
|
|
|
// CHECK:STDOUT: ; ModuleID = 'member_access.carbon'
|
|
// CHECK:STDOUT: source_filename = "member_access.carbon"
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: @struct.454.loc14_3 = internal constant <{ double, i32 }> <{ double 0.000000e+00, i32 1 }>
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: ; Function Attrs: nounwind
|
|
// CHECK:STDOUT: define i32 @main() #0 !dbg !4 {
|
|
// CHECK:STDOUT: entry:
|
|
// CHECK:STDOUT: %x.var = alloca <{ double, i32 }>, align 8, !dbg !8
|
|
// CHECK:STDOUT: %y.var = alloca i32, align 4, !dbg !9
|
|
// CHECK:STDOUT: %_.var = alloca i32, align 4, !dbg !10
|
|
// CHECK:STDOUT: call void @llvm.lifetime.start.p0(ptr %x.var), !dbg !8
|
|
// CHECK:STDOUT: %.loc14_48.3.a = getelementptr inbounds nuw <{ double, i32 }>, ptr %x.var, i32 0, i32 0, !dbg !11
|
|
// CHECK:STDOUT: %.loc14_48.6.b = getelementptr inbounds nuw <{ double, i32 }>, ptr %x.var, i32 0, i32 1, !dbg !11
|
|
// CHECK:STDOUT: call void @llvm.memcpy.p0.p0.i64(ptr align 1 %x.var, ptr align 1 @struct.454.loc14_3, i64 12, i1 false), !dbg !8
|
|
// CHECK:STDOUT: call void @llvm.lifetime.start.p0(ptr %y.var), !dbg !9
|
|
// CHECK:STDOUT: %.loc15_17.1.b = getelementptr inbounds nuw <{ double, i32 }>, ptr %x.var, i32 0, i32 1, !dbg !12
|
|
// CHECK:STDOUT: %.loc15_17.2 = load i32, ptr %.loc15_17.1.b, align 4, !dbg !12
|
|
// CHECK:STDOUT: store i32 %.loc15_17.2, ptr %y.var, align 4, !dbg !9
|
|
// CHECK:STDOUT: call void @llvm.lifetime.start.p0(ptr %_.var), !dbg !10
|
|
// CHECK:STDOUT: %.loc16 = load i32, ptr %y.var, align 4, !dbg !13
|
|
// CHECK:STDOUT: store i32 %.loc16, ptr %_.var, align 4, !dbg !10
|
|
// CHECK:STDOUT: call void @"_COp.7e389eab4a7e5487:core.Destroy.Core"(ptr %_.var), !dbg !10
|
|
// CHECK:STDOUT: call void @"_COp.7e389eab4a7e5487:core.Destroy.Core"(ptr %y.var), !dbg !9
|
|
// CHECK:STDOUT: call void @"_COp.c873a7d633fbed8d:core.Destroy.Core"(ptr %x.var), !dbg !8
|
|
// CHECK:STDOUT: ret i32 0, !dbg !14
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: ; Function Attrs: nounwind
|
|
// CHECK:STDOUT: define weak_odr void @"_COp.7e389eab4a7e5487:core.Destroy.Core"(ptr %self) #0 !dbg !15 {
|
|
// CHECK:STDOUT: entry:
|
|
// CHECK:STDOUT: ret void, !dbg !20
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: ; Function Attrs: nounwind
|
|
// CHECK:STDOUT: define weak_odr void @"_COp.98006a366c2ffd6c:core.Destroy.Core"(ptr %self) #0 !dbg !21 {
|
|
// CHECK:STDOUT: entry:
|
|
// CHECK:STDOUT: ret void, !dbg !27
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: ; Function Attrs: nounwind
|
|
// CHECK:STDOUT: define weak_odr void @"_COp.c873a7d633fbed8d:core.Destroy.Core"(ptr %self) #0 !dbg !28 {
|
|
// CHECK:STDOUT: entry:
|
|
// CHECK:STDOUT: ret void, !dbg !31
|
|
// CHECK:STDOUT: }
|
|
// 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)) #1
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: ; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
|
|
// CHECK:STDOUT: declare void @llvm.memcpy.p0.p0.i64(ptr noalias writeonly captures(none), ptr noalias readonly captures(none), i64, i1 immarg) #2
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: ; uselistorder directives
|
|
// CHECK:STDOUT: uselistorder ptr @"_COp.7e389eab4a7e5487:core.Destroy.Core", { 1, 0 }
|
|
// CHECK:STDOUT: uselistorder ptr @llvm.lifetime.start.p0, { 2, 1, 0 }
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: attributes #0 = { nounwind }
|
|
// CHECK:STDOUT: attributes #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
|
|
// CHECK:STDOUT: attributes #2 = { nocallback nofree 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_plus_plus, file: !3, producer: "carbon", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug)
|
|
// CHECK:STDOUT: !3 = !DIFile(filename: "member_access.carbon", directory: "")
|
|
// CHECK:STDOUT: !4 = distinct !DISubprogram(name: "Run", linkageName: "main", scope: null, file: !3, line: 13, type: !5, spFlags: DISPFlagDefinition, unit: !2)
|
|
// CHECK:STDOUT: !5 = !DISubroutineType(types: !6)
|
|
// CHECK:STDOUT: !6 = !{!7}
|
|
// CHECK:STDOUT: !7 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
|
|
// CHECK:STDOUT: !8 = !DILocation(line: 14, column: 3, scope: !4)
|
|
// CHECK:STDOUT: !9 = !DILocation(line: 15, column: 3, scope: !4)
|
|
// CHECK:STDOUT: !10 = !DILocation(line: 16, column: 3, scope: !4)
|
|
// CHECK:STDOUT: !11 = !DILocation(line: 14, column: 31, scope: !4)
|
|
// CHECK:STDOUT: !12 = !DILocation(line: 15, column: 16, scope: !4)
|
|
// CHECK:STDOUT: !13 = !DILocation(line: 16, column: 16, scope: !4)
|
|
// CHECK:STDOUT: !14 = !DILocation(line: 17, column: 3, scope: !4)
|
|
// CHECK:STDOUT: !15 = distinct !DISubprogram(name: "Op", linkageName: "_COp.7e389eab4a7e5487:core.Destroy.Core", scope: null, file: !3, line: 16, type: !16, spFlags: DISPFlagDefinition, unit: !2, retainedNodes: !18)
|
|
// CHECK:STDOUT: !16 = !DISubroutineType(types: !17)
|
|
// CHECK:STDOUT: !17 = !{null, !7}
|
|
// CHECK:STDOUT: !18 = !{!19}
|
|
// CHECK:STDOUT: !19 = !DILocalVariable(arg: 1, scope: !15, type: !7)
|
|
// CHECK:STDOUT: !20 = !DILocation(line: 16, column: 3, scope: !15)
|
|
// CHECK:STDOUT: !21 = distinct !DISubprogram(name: "Op", linkageName: "_COp.98006a366c2ffd6c:core.Destroy.Core", scope: null, file: !3, line: 14, type: !22, spFlags: DISPFlagDefinition, unit: !2, retainedNodes: !25)
|
|
// CHECK:STDOUT: !22 = !DISubroutineType(types: !23)
|
|
// CHECK:STDOUT: !23 = !{null, !24}
|
|
// CHECK:STDOUT: !24 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: null, size: 64)
|
|
// CHECK:STDOUT: !25 = !{!26}
|
|
// CHECK:STDOUT: !26 = !DILocalVariable(arg: 1, scope: !21, type: !24)
|
|
// CHECK:STDOUT: !27 = !DILocation(line: 14, column: 3, scope: !21)
|
|
// CHECK:STDOUT: !28 = distinct !DISubprogram(name: "Op", linkageName: "_COp.c873a7d633fbed8d:core.Destroy.Core", scope: null, file: !3, line: 14, type: !22, spFlags: DISPFlagDefinition, unit: !2, retainedNodes: !29)
|
|
// CHECK:STDOUT: !29 = !{!30}
|
|
// CHECK:STDOUT: !30 = !DILocalVariable(arg: 1, scope: !28, type: !24)
|
|
// CHECK:STDOUT: !31 = !DILocation(line: 14, column: 3, scope: !28)
|