layout.carbon 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. //
  5. // INCLUDE-FILE: toolchain/testing/testdata/min_prelude/int.carbon
  6. //
  7. // AUTOUPDATE
  8. // TIP: To test this file alone, run:
  9. // TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/lower/testdata/array/layout.carbon
  10. // TIP: To dump output, run:
  11. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/lower/testdata/array/layout.carbon
  12. // --- element_tail_padding.carbon
  13. library "[[@TEST_NAME]]";
  14. fn Check(p: i32*);
  15. fn Test() {
  16. // We insert tail padding after each element (including the last) to keep the
  17. // array elements properly aligned and to ensure that a past-the-end address
  18. // points within the overall object.
  19. var v: array({.a: ()*, .b: i32}, 3);
  20. Check(&v[0].b);
  21. Check(&v[1].b);
  22. Check(&v[2].b);
  23. }
  24. // --- constant_with_padding.carbon
  25. library "[[@TEST_NAME]]";
  26. var c: array(Core.Int(257), 5) = (1, 2, 3, 4, 5);
  27. // CHECK:STDOUT: ; ModuleID = 'element_tail_padding.carbon'
  28. // CHECK:STDOUT: source_filename = "element_tail_padding.carbon"
  29. // CHECK:STDOUT:
  30. // CHECK:STDOUT: declare void @_CCheck.Main(ptr)
  31. // CHECK:STDOUT:
  32. // CHECK:STDOUT: ; Function Attrs: nounwind
  33. // CHECK:STDOUT: define void @_CTest.Main() #0 !dbg !4 {
  34. // CHECK:STDOUT: entry:
  35. // CHECK:STDOUT: %v.var = alloca [3 x <{ <{ ptr, i32 }>, [4 x i8] }>], align 8, !dbg !7
  36. // CHECK:STDOUT: call void @llvm.lifetime.start.p0(ptr %v.var), !dbg !7
  37. // CHECK:STDOUT: %.loc10_13.array.index = getelementptr inbounds [3 x <{ <{ ptr, i32 }>, [4 x i8] }>], ptr %v.var, i32 0, i32 0, !dbg !8
  38. // CHECK:STDOUT: %.loc10_14.b = getelementptr inbounds nuw <{ ptr, i32 }>, ptr %.loc10_13.array.index, i32 0, i32 1, !dbg !8
  39. // CHECK:STDOUT: call void @_CCheck.Main(ptr %.loc10_14.b), !dbg !9
  40. // CHECK:STDOUT: %.loc11_13.array.index = getelementptr inbounds [3 x <{ <{ ptr, i32 }>, [4 x i8] }>], ptr %v.var, i32 0, i32 1, !dbg !10
  41. // CHECK:STDOUT: %.loc11_14.b = getelementptr inbounds nuw <{ ptr, i32 }>, ptr %.loc11_13.array.index, i32 0, i32 1, !dbg !10
  42. // CHECK:STDOUT: call void @_CCheck.Main(ptr %.loc11_14.b), !dbg !11
  43. // CHECK:STDOUT: %.loc12_13.array.index = getelementptr inbounds [3 x <{ <{ ptr, i32 }>, [4 x i8] }>], ptr %v.var, i32 0, i32 2, !dbg !12
  44. // CHECK:STDOUT: %.loc12_14.b = getelementptr inbounds nuw <{ ptr, i32 }>, ptr %.loc12_13.array.index, i32 0, i32 1, !dbg !12
  45. // CHECK:STDOUT: call void @_CCheck.Main(ptr %.loc12_14.b), !dbg !13
  46. // CHECK:STDOUT: call void @"_COp.876098026a9fba08:core.Destroy.Core"(ptr %v.var), !dbg !7
  47. // CHECK:STDOUT: ret void, !dbg !14
  48. // CHECK:STDOUT: }
  49. // CHECK:STDOUT:
  50. // CHECK:STDOUT: ; Function Attrs: nounwind
  51. // CHECK:STDOUT: define weak_odr void @"_COp.7e389eab4a7e5487:core.Destroy.Core"(ptr %self) #0 !dbg !15 {
  52. // CHECK:STDOUT: entry:
  53. // CHECK:STDOUT: ret void, !dbg !21
  54. // CHECK:STDOUT: }
  55. // CHECK:STDOUT:
  56. // CHECK:STDOUT: ; Function Attrs: nounwind
  57. // CHECK:STDOUT: define weak_odr void @"_COp.5e9a928496027250:core.Destroy.Core"(ptr %self) #0 !dbg !22 {
  58. // CHECK:STDOUT: entry:
  59. // CHECK:STDOUT: ret void, !dbg !28
  60. // CHECK:STDOUT: }
  61. // CHECK:STDOUT:
  62. // CHECK:STDOUT: ; Function Attrs: nounwind
  63. // CHECK:STDOUT: define weak_odr void @"_COp.876098026a9fba08:core.Destroy.Core"(ptr %self) #0 !dbg !29 {
  64. // CHECK:STDOUT: entry:
  65. // CHECK:STDOUT: ret void, !dbg !32
  66. // CHECK:STDOUT: }
  67. // CHECK:STDOUT:
  68. // CHECK:STDOUT: ; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
  69. // CHECK:STDOUT: declare void @llvm.lifetime.start.p0(ptr captures(none)) #1
  70. // CHECK:STDOUT:
  71. // CHECK:STDOUT: attributes #0 = { nounwind }
  72. // CHECK:STDOUT: attributes #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
  73. // CHECK:STDOUT:
  74. // CHECK:STDOUT: !llvm.module.flags = !{!0, !1}
  75. // CHECK:STDOUT: !llvm.dbg.cu = !{!2}
  76. // CHECK:STDOUT:
  77. // CHECK:STDOUT: !0 = !{i32 7, !"Dwarf Version", i32 5}
  78. // CHECK:STDOUT: !1 = !{i32 2, !"Debug Info Version", i32 3}
  79. // CHECK:STDOUT: !2 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !3, producer: "carbon", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug)
  80. // CHECK:STDOUT: !3 = !DIFile(filename: "element_tail_padding.carbon", directory: "")
  81. // CHECK:STDOUT: !4 = distinct !DISubprogram(name: "Test", linkageName: "_CTest.Main", scope: null, file: !3, line: 5, type: !5, spFlags: DISPFlagDefinition, unit: !2)
  82. // CHECK:STDOUT: !5 = !DISubroutineType(types: !6)
  83. // CHECK:STDOUT: !6 = !{null}
  84. // CHECK:STDOUT: !7 = !DILocation(line: 9, column: 3, scope: !4)
  85. // CHECK:STDOUT: !8 = !DILocation(line: 10, column: 10, scope: !4)
  86. // CHECK:STDOUT: !9 = !DILocation(line: 10, column: 3, scope: !4)
  87. // CHECK:STDOUT: !10 = !DILocation(line: 11, column: 10, scope: !4)
  88. // CHECK:STDOUT: !11 = !DILocation(line: 11, column: 3, scope: !4)
  89. // CHECK:STDOUT: !12 = !DILocation(line: 12, column: 10, scope: !4)
  90. // CHECK:STDOUT: !13 = !DILocation(line: 12, column: 3, scope: !4)
  91. // CHECK:STDOUT: !14 = !DILocation(line: 5, column: 1, scope: !4)
  92. // CHECK:STDOUT: !15 = distinct !DISubprogram(name: "Op", linkageName: "_COp.7e389eab4a7e5487:core.Destroy.Core", scope: null, file: !3, line: 9, type: !16, spFlags: DISPFlagDefinition, unit: !2, retainedNodes: !19)
  93. // CHECK:STDOUT: !16 = !DISubroutineType(types: !17)
  94. // CHECK:STDOUT: !17 = !{null, !18}
  95. // CHECK:STDOUT: !18 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
  96. // CHECK:STDOUT: !19 = !{!20}
  97. // CHECK:STDOUT: !20 = !DILocalVariable(arg: 1, scope: !15, type: !18)
  98. // CHECK:STDOUT: !21 = !DILocation(line: 9, column: 3, scope: !15)
  99. // CHECK:STDOUT: !22 = distinct !DISubprogram(name: "Op", linkageName: "_COp.5e9a928496027250:core.Destroy.Core", scope: null, file: !3, line: 9, type: !23, spFlags: DISPFlagDefinition, unit: !2, retainedNodes: !26)
  100. // CHECK:STDOUT: !23 = !DISubroutineType(types: !24)
  101. // CHECK:STDOUT: !24 = !{null, !25}
  102. // CHECK:STDOUT: !25 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: null, size: 64)
  103. // CHECK:STDOUT: !26 = !{!27}
  104. // CHECK:STDOUT: !27 = !DILocalVariable(arg: 1, scope: !22, type: !25)
  105. // CHECK:STDOUT: !28 = !DILocation(line: 9, column: 3, scope: !22)
  106. // CHECK:STDOUT: !29 = distinct !DISubprogram(name: "Op", linkageName: "_COp.876098026a9fba08:core.Destroy.Core", scope: null, file: !3, line: 9, type: !23, spFlags: DISPFlagDefinition, unit: !2, retainedNodes: !30)
  107. // CHECK:STDOUT: !30 = !{!31}
  108. // CHECK:STDOUT: !31 = !DILocalVariable(arg: 1, scope: !29, type: !25)
  109. // CHECK:STDOUT: !32 = !DILocation(line: 9, column: 3, scope: !29)
  110. // CHECK:STDOUT: ; ModuleID = 'constant_with_padding.carbon'
  111. // CHECK:STDOUT: source_filename = "constant_with_padding.carbon"
  112. // CHECK:STDOUT:
  113. // CHECK:STDOUT: @_Cc.Main = global [5 x <{ i257, [28 x i8] }>] zeroinitializer
  114. // CHECK:STDOUT: @array.loc3_1 = internal constant [5 x <{ i257, [28 x i8] }>] [<{ i257, [28 x i8] }> <{ i257 1, [28 x i8] poison }>, <{ i257, [28 x i8] }> <{ i257 2, [28 x i8] poison }>, <{ i257, [28 x i8] }> <{ i257 3, [28 x i8] poison }>, <{ i257, [28 x i8] }> <{ i257 4, [28 x i8] poison }>, <{ i257, [28 x i8] }> <{ i257 5, [28 x i8] poison }>]
  115. // CHECK:STDOUT: @llvm.global_ctors = appending global [1 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 0, ptr @_C__global_init.Main, ptr null }]
  116. // CHECK:STDOUT:
  117. // CHECK:STDOUT: ; Function Attrs: nounwind
  118. // CHECK:STDOUT: define internal void @_C__global_init.Main() #0 !dbg !4 {
  119. // CHECK:STDOUT: entry:
  120. // CHECK:STDOUT: call void @llvm.memcpy.p0.p0.i64(ptr align 1 @_Cc.Main, ptr align 1 @array.loc3_1, i64 320, i1 false), !dbg !7
  121. // CHECK:STDOUT: ret void, !dbg !8
  122. // CHECK:STDOUT: }
  123. // CHECK:STDOUT:
  124. // CHECK:STDOUT: ; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
  125. // CHECK:STDOUT: declare void @llvm.memcpy.p0.p0.i64(ptr noalias writeonly captures(none), ptr noalias readonly captures(none), i64, i1 immarg) #1
  126. // CHECK:STDOUT:
  127. // CHECK:STDOUT: attributes #0 = { nounwind }
  128. // CHECK:STDOUT: attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
  129. // CHECK:STDOUT:
  130. // CHECK:STDOUT: !llvm.module.flags = !{!0, !1}
  131. // CHECK:STDOUT: !llvm.dbg.cu = !{!2}
  132. // CHECK:STDOUT:
  133. // CHECK:STDOUT: !0 = !{i32 7, !"Dwarf Version", i32 5}
  134. // CHECK:STDOUT: !1 = !{i32 2, !"Debug Info Version", i32 3}
  135. // CHECK:STDOUT: !2 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !3, producer: "carbon", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug)
  136. // CHECK:STDOUT: !3 = !DIFile(filename: "constant_with_padding.carbon", directory: "")
  137. // CHECK:STDOUT: !4 = distinct !DISubprogram(name: "__global_init", linkageName: "_C__global_init.Main", scope: null, file: !3, type: !5, spFlags: DISPFlagDefinition, unit: !2)
  138. // CHECK:STDOUT: !5 = !DISubroutineType(types: !6)
  139. // CHECK:STDOUT: !6 = !{null}
  140. // CHECK:STDOUT: !7 = !DILocation(line: 3, column: 1, scope: !4)
  141. // CHECK:STDOUT: !8 = !DILocation(line: 0, scope: !4)