Rudimentary name mangling support (#4267)

This seems to be enough to avoid naming collisions for functions in any
of the current test cases (verified by asserting that the name of the
`llvm::Function` matches the name passed to create it - not triggering
LLVM's numbering that happens when names collide)

It currently implements mangling for namespace scopes, class scopes, and
impls.
Nothing generic is mangled yet - haven't looked at how that works,
though evidently it's not covered by existing testing, I guess.

Follow-up change will document the current mangling algorithm in
`toolchain/docs/lower.md`

---------

Co-authored-by: Jon Ross-Perkins <jperkins@google.com>
Co-authored-by: Carbon Infra Bot <carbon-external-infra@google.com>
This commit is contained in:
David Blaikie
2024-09-06 23:10:21 +00:00
committed by GitHub
co-authored by Jon Ross-Perkins Carbon Infra Bot
parent 49f2136325
commit a548eff0bb
94 changed files with 657 additions and 498 deletions
+5 -5
View File
@@ -30,16 +30,16 @@ fn Call(a: A) -> i32 {
// CHECK:STDOUT: ; ModuleID = 'assoc_fn_alias.carbon'
// CHECK:STDOUT: source_filename = "assoc_fn_alias.carbon"
// CHECK:STDOUT:
// CHECK:STDOUT: define i32 @F(ptr %self) !dbg !4 {
// CHECK:STDOUT: define i32 @"_CF.A.Main:I.Main"(ptr %self) !dbg !4 {
// CHECK:STDOUT: entry:
// CHECK:STDOUT: %.loc22_16.1.n = getelementptr inbounds nuw { i32 }, ptr %self, i32 0, i32 0, !dbg !7
// CHECK:STDOUT: %.loc22_16.2 = load i32, ptr %.loc22_16.1.n, align 4, !dbg !7
// CHECK:STDOUT: ret i32 %.loc22_16.2, !dbg !8
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: define i32 @Call(ptr %a) !dbg !9 {
// CHECK:STDOUT: define i32 @_CCall.Main(ptr %a) !dbg !9 {
// CHECK:STDOUT: entry:
// CHECK:STDOUT: %F.call = call i32 @F(ptr %a), !dbg !10
// CHECK:STDOUT: %F.call = call i32 @"_CF.A.Main:I.Main"(ptr %a), !dbg !10
// CHECK:STDOUT: ret i32 %F.call, !dbg !11
// CHECK:STDOUT: }
// CHECK:STDOUT:
@@ -50,11 +50,11 @@ fn Call(a: A) -> i32 {
// 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: "assoc_fn_alias.carbon", directory: "")
// CHECK:STDOUT: !4 = distinct !DISubprogram(name: "F", linkageName: "F", scope: null, file: !3, line: 21, type: !5, spFlags: DISPFlagDefinition, unit: !2)
// CHECK:STDOUT: !4 = distinct !DISubprogram(name: "F", linkageName: "_CF.A.Main:I.Main", scope: null, file: !3, line: 21, type: !5, spFlags: DISPFlagDefinition, unit: !2)
// CHECK:STDOUT: !5 = !DISubroutineType(types: !6)
// CHECK:STDOUT: !6 = !{}
// CHECK:STDOUT: !7 = !DILocation(line: 22, column: 12, scope: !4)
// CHECK:STDOUT: !8 = !DILocation(line: 22, column: 5, scope: !4)
// CHECK:STDOUT: !9 = distinct !DISubprogram(name: "Call", linkageName: "Call", scope: null, file: !3, line: 26, type: !5, spFlags: DISPFlagDefinition, unit: !2)
// CHECK:STDOUT: !9 = distinct !DISubprogram(name: "Call", linkageName: "_CCall.Main", scope: null, file: !3, line: 26, type: !5, spFlags: DISPFlagDefinition, unit: !2)
// CHECK:STDOUT: !10 = !DILocation(line: 27, column: 10, scope: !9)
// CHECK:STDOUT: !11 = !DILocation(line: 27, column: 3, scope: !9)
+8 -8
View File
@@ -31,20 +31,20 @@ fn InstanceAccess(a: A) -> i32 {
// CHECK:STDOUT: ; ModuleID = 'extend_impl.carbon'
// CHECK:STDOUT: source_filename = "extend_impl.carbon"
// CHECK:STDOUT:
// CHECK:STDOUT: define i32 @F() !dbg !4 {
// CHECK:STDOUT: define i32 @"_CF.A.Main:I.Main"() !dbg !4 {
// CHECK:STDOUT: entry:
// CHECK:STDOUT: ret i32 0, !dbg !7
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: define i32 @TypeAccess() !dbg !8 {
// CHECK:STDOUT: define i32 @_CTypeAccess.Main() !dbg !8 {
// CHECK:STDOUT: entry:
// CHECK:STDOUT: %F.call = call i32 @F(), !dbg !9
// CHECK:STDOUT: %F.call = call i32 @"_CF.A.Main:I.Main"(), !dbg !9
// CHECK:STDOUT: ret i32 %F.call, !dbg !10
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: define i32 @InstanceAccess(ptr %a) !dbg !11 {
// CHECK:STDOUT: define i32 @_CInstanceAccess.Main(ptr %a) !dbg !11 {
// CHECK:STDOUT: entry:
// CHECK:STDOUT: %F.call = call i32 @F(), !dbg !12
// CHECK:STDOUT: %F.call = call i32 @"_CF.A.Main:I.Main"(), !dbg !12
// CHECK:STDOUT: ret i32 %F.call, !dbg !13
// CHECK:STDOUT: }
// CHECK:STDOUT:
@@ -55,13 +55,13 @@ fn InstanceAccess(a: A) -> i32 {
// 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: "extend_impl.carbon", directory: "")
// CHECK:STDOUT: !4 = distinct !DISubprogram(name: "F", linkageName: "F", scope: null, file: !3, line: 17, type: !5, spFlags: DISPFlagDefinition, unit: !2)
// CHECK:STDOUT: !4 = distinct !DISubprogram(name: "F", linkageName: "_CF.A.Main:I.Main", scope: null, file: !3, line: 17, type: !5, spFlags: DISPFlagDefinition, unit: !2)
// CHECK:STDOUT: !5 = !DISubroutineType(types: !6)
// CHECK:STDOUT: !6 = !{}
// CHECK:STDOUT: !7 = !DILocation(line: 18, column: 7, scope: !4)
// CHECK:STDOUT: !8 = distinct !DISubprogram(name: "TypeAccess", linkageName: "TypeAccess", scope: null, file: !3, line: 23, type: !5, spFlags: DISPFlagDefinition, unit: !2)
// CHECK:STDOUT: !8 = distinct !DISubprogram(name: "TypeAccess", linkageName: "_CTypeAccess.Main", scope: null, file: !3, line: 23, type: !5, spFlags: DISPFlagDefinition, unit: !2)
// CHECK:STDOUT: !9 = !DILocation(line: 24, column: 10, scope: !8)
// CHECK:STDOUT: !10 = !DILocation(line: 24, column: 3, scope: !8)
// CHECK:STDOUT: !11 = distinct !DISubprogram(name: "InstanceAccess", linkageName: "InstanceAccess", scope: null, file: !3, line: 27, type: !5, spFlags: DISPFlagDefinition, unit: !2)
// CHECK:STDOUT: !11 = distinct !DISubprogram(name: "InstanceAccess", linkageName: "_CInstanceAccess.Main", scope: null, file: !3, line: 27, type: !5, spFlags: DISPFlagDefinition, unit: !2)
// CHECK:STDOUT: !12 = !DILocation(line: 28, column: 10, scope: !11)
// CHECK:STDOUT: !13 = !DILocation(line: 28, column: 3, scope: !11)
+5 -5
View File
@@ -29,16 +29,16 @@ fn Call(a: A) -> i32 {
// CHECK:STDOUT: ; ModuleID = 'impl.carbon'
// CHECK:STDOUT: source_filename = "impl.carbon"
// CHECK:STDOUT:
// CHECK:STDOUT: define i32 @F(ptr %self) !dbg !4 {
// CHECK:STDOUT: define i32 @"_CF.A.Main:I.Main"(ptr %self) !dbg !4 {
// CHECK:STDOUT: entry:
// CHECK:STDOUT: %.loc21_16.1.n = getelementptr inbounds nuw { i32 }, ptr %self, i32 0, i32 0, !dbg !7
// CHECK:STDOUT: %.loc21_16.2 = load i32, ptr %.loc21_16.1.n, align 4, !dbg !7
// CHECK:STDOUT: ret i32 %.loc21_16.2, !dbg !8
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: define i32 @Call(ptr %a) !dbg !9 {
// CHECK:STDOUT: define i32 @_CCall.Main(ptr %a) !dbg !9 {
// CHECK:STDOUT: entry:
// CHECK:STDOUT: %F.call = call i32 @F(ptr %a), !dbg !10
// CHECK:STDOUT: %F.call = call i32 @"_CF.A.Main:I.Main"(ptr %a), !dbg !10
// CHECK:STDOUT: ret i32 %F.call, !dbg !11
// CHECK:STDOUT: }
// CHECK:STDOUT:
@@ -49,11 +49,11 @@ fn Call(a: A) -> i32 {
// 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: "impl.carbon", directory: "")
// CHECK:STDOUT: !4 = distinct !DISubprogram(name: "F", linkageName: "F", scope: null, file: !3, line: 20, type: !5, spFlags: DISPFlagDefinition, unit: !2)
// CHECK:STDOUT: !4 = distinct !DISubprogram(name: "F", linkageName: "_CF.A.Main:I.Main", scope: null, file: !3, line: 20, type: !5, spFlags: DISPFlagDefinition, unit: !2)
// CHECK:STDOUT: !5 = !DISubroutineType(types: !6)
// CHECK:STDOUT: !6 = !{}
// CHECK:STDOUT: !7 = !DILocation(line: 21, column: 12, scope: !4)
// CHECK:STDOUT: !8 = !DILocation(line: 21, column: 5, scope: !4)
// CHECK:STDOUT: !9 = distinct !DISubprogram(name: "Call", linkageName: "Call", scope: null, file: !3, line: 25, type: !5, spFlags: DISPFlagDefinition, unit: !2)
// CHECK:STDOUT: !9 = distinct !DISubprogram(name: "Call", linkageName: "_CCall.Main", scope: null, file: !3, line: 25, type: !5, spFlags: DISPFlagDefinition, unit: !2)
// CHECK:STDOUT: !10 = !DILocation(line: 26, column: 10, scope: !9)
// CHECK:STDOUT: !11 = !DILocation(line: 26, column: 3, scope: !9)
+5 -5
View File
@@ -29,14 +29,14 @@ fn Call(a: A*) -> A* {
// CHECK:STDOUT: ; ModuleID = 'instance_method.carbon'
// CHECK:STDOUT: source_filename = "instance_method.carbon"
// CHECK:STDOUT:
// CHECK:STDOUT: define ptr @Get(ptr %self) !dbg !4 {
// CHECK:STDOUT: define ptr @"_CGet.A.Main:GetSelf.Main"(ptr %self) !dbg !4 {
// CHECK:STDOUT: entry:
// CHECK:STDOUT: ret ptr %self, !dbg !7
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: define ptr @Call(ptr %a) !dbg !8 {
// CHECK:STDOUT: define ptr @_CCall.Main(ptr %a) !dbg !8 {
// CHECK:STDOUT: entry:
// CHECK:STDOUT: %Get.call = call ptr @Get(ptr %a), !dbg !9
// CHECK:STDOUT: %Get.call = call ptr @"_CGet.A.Main:GetSelf.Main"(ptr %a), !dbg !9
// CHECK:STDOUT: ret ptr %Get.call, !dbg !10
// CHECK:STDOUT: }
// CHECK:STDOUT:
@@ -47,10 +47,10 @@ fn Call(a: A*) -> A* {
// 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: "instance_method.carbon", directory: "")
// CHECK:STDOUT: !4 = distinct !DISubprogram(name: "Get", linkageName: "Get", scope: null, file: !3, line: 19, type: !5, spFlags: DISPFlagDefinition, unit: !2)
// CHECK:STDOUT: !4 = distinct !DISubprogram(name: "Get", linkageName: "_CGet.A.Main:GetSelf.Main", scope: null, file: !3, line: 19, type: !5, spFlags: DISPFlagDefinition, unit: !2)
// CHECK:STDOUT: !5 = !DISubroutineType(types: !6)
// CHECK:STDOUT: !6 = !{}
// CHECK:STDOUT: !7 = !DILocation(line: 20, column: 7, scope: !4)
// CHECK:STDOUT: !8 = distinct !DISubprogram(name: "Call", linkageName: "Call", scope: null, file: !3, line: 25, type: !5, spFlags: DISPFlagDefinition, unit: !2)
// CHECK:STDOUT: !8 = distinct !DISubprogram(name: "Call", linkageName: "_CCall.Main", scope: null, file: !3, line: 25, type: !5, spFlags: DISPFlagDefinition, unit: !2)
// CHECK:STDOUT: !9 = !DILocation(line: 26, column: 10, scope: !8)
// CHECK:STDOUT: !10 = !DILocation(line: 26, column: 3, scope: !8)