mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-11-10 12:08:29 +00:00
If multiple files lower the same variable, use the same GlobalVariable. (#8101)
We can lower the same variable from multiple `SemIR::File`s if we reach it through a specific for a generic in a different file. When this happens, reuse the same global variable in lowering. This matches how we handle the same problem for functions. Assisted-by: Claude via Antigravity
This commit is contained in:
1 parent
ca1898506b
commit
19f1bf94db
3 files changed
+123
-4
No files matched your search
@@ -460,7 +460,7 @@ auto FileContext::GetOrCreateLLVMFunction(
|
||||
// file. That's OK.
|
||||
// TODO: If the prior function was inexact and the new one is not, we should
|
||||
// lower this new one and replace the existing function with it.
|
||||
// TODO: Check-fail or maybe diagnose if the two LLVM functions are not
|
||||
// TODO: CHECK-fail or maybe diagnose if the two LLVM functions are not
|
||||
// produced by declarations of the same Carbon function. Name collisions
|
||||
// between non-private members of the same library should have been
|
||||
// diagnosed by check if detected, but it's not clear that check will
|
||||
@@ -883,6 +883,19 @@ auto FileContext::BuildNonCppGlobalVariableDecl(SemIR::VarStorage var_storage)
|
||||
("var.anon" + inst_namer_->GetUnscopedNameFor(var_storage.pattern_id))
|
||||
.str();
|
||||
}
|
||||
} else if (auto* existing = llvm_module().getNamedGlobal(mangled_name)) {
|
||||
// We might have already declared this variable while lowering a different
|
||||
// file, such as when lowering a specific of a generic function from the
|
||||
// file that defines the variable. Reuse that declaration rather than
|
||||
// creating a new one, which LLVM would rename to avoid the collision.
|
||||
//
|
||||
// TODO: CHECK-fail or maybe diagnose if the two LLVM variables are not
|
||||
// produced by declarations of the same Carbon variable. Name collisions
|
||||
// between non-private members of the same library should have been
|
||||
// diagnosed by check if detected, but it's not clear that check will
|
||||
// always be able to see this problem. In theory, name collisions could
|
||||
// also occur due to fingerprint collision.
|
||||
return existing;
|
||||
}
|
||||
|
||||
auto* type = GetType(var_storage.type_id);
|
||||
|
||||
-3
@@ -158,9 +158,6 @@ fn Z() -> i32 { return z; }
|
||||
// CHECK:STDOUT: @_Cv.Main = external global { i32, i32 }
|
||||
// CHECK:STDOUT: @_Cy.Main = external global i32
|
||||
// CHECK:STDOUT: @_Cz.Main = external global { i32, i32 }
|
||||
// CHECK:STDOUT: @_Cv.Main.1 = external global { i32, i32 }
|
||||
// CHECK:STDOUT: @_Cy.Main.2 = external global i32
|
||||
// CHECK:STDOUT: @_Cz.Main.3 = external global { i32, i32 }
|
||||
// CHECK:STDOUT: @_Cx.Main = external global i32
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: ; Function Attrs: nounwind
|
||||
|
||||
@@ -0,0 +1,109 @@
|
||||
// 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/int.carbon
|
||||
//
|
||||
// AUTOUPDATE
|
||||
// TIP: To test this file alone, run:
|
||||
// TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/lower/testdata/var/import_from_generic.carbon
|
||||
// TIP: To dump output, run:
|
||||
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/lower/testdata/var/import_from_generic.carbon
|
||||
|
||||
// --- lib.carbon
|
||||
|
||||
library "[[@TEST_NAME]]";
|
||||
|
||||
var g: i32 = 1;
|
||||
|
||||
fn Get[T: type](_: T) -> i32 { return g; }
|
||||
|
||||
// --- use.carbon
|
||||
|
||||
library "[[@TEST_NAME]]";
|
||||
|
||||
import library "lib";
|
||||
|
||||
// The direct reference to `g` and the reference from the specific `Get(())`
|
||||
// should both refer to the same global.
|
||||
fn F() -> i32 { return g; }
|
||||
fn G() -> i32 { return Get(()); }
|
||||
|
||||
// CHECK:STDOUT: ; ---
|
||||
// CHECK:STDOUT: ; ModuleID = 'lib.carbon'
|
||||
// CHECK:STDOUT: source_filename = "lib.carbon"
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: @_Cg.Main = global i32 0
|
||||
// CHECK:STDOUT: @llvm.global_ctors = appending global [1 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 0, ptr @_C__global_init.Main, ptr null }]
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: ; Function Attrs: nounwind
|
||||
// CHECK:STDOUT: define internal void @_C__global_init.Main() #0 !dbg !23 {
|
||||
// CHECK:STDOUT: entry:
|
||||
// CHECK:STDOUT: store i32 1, ptr @_Cg.Main, align 4, !dbg !24
|
||||
// CHECK:STDOUT: ret void, !dbg !25
|
||||
// CHECK:STDOUT: }
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: attributes #0 = { nounwind }
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: !llvm.dbg.cu = !{!19}
|
||||
// CHECK:STDOUT: !llvm.module.flags = !{!2, !3}
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: !2 = !{i32 7, !"Dwarf Version", i32 5}
|
||||
// CHECK:STDOUT: !3 = !{i32 2, !"Debug Info Version", i32 3}
|
||||
// CHECK:STDOUT: !18 = !DIFile(filename: "lib.carbon", directory: "")
|
||||
// CHECK:STDOUT: !19 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !18, producer: "carbon", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug)
|
||||
// CHECK:STDOUT: !21 = !{null}
|
||||
// CHECK:STDOUT: !22 = !DISubroutineType(types: !21)
|
||||
// CHECK:STDOUT: !23 = distinct !DISubprogram(name: "__global_init", linkageName: "_C__global_init.Main", scope: null, file: !18, type: !22, spFlags: DISPFlagDefinition, unit: !19)
|
||||
// CHECK:STDOUT: !24 = !DILocation(line: 4, column: 1, scope: !23)
|
||||
// CHECK:STDOUT: !25 = !DILocation(line: 0, scope: !23)
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: ; ---
|
||||
// CHECK:STDOUT: ; ModuleID = 'use.carbon'
|
||||
// CHECK:STDOUT: source_filename = "use.carbon"
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: @_Cg.Main = external global i32
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: ; Function Attrs: nounwind
|
||||
// CHECK:STDOUT: define i32 @_CF.Main() #0 !dbg !30 {
|
||||
// CHECK:STDOUT: entry:
|
||||
// CHECK:STDOUT: %.loc8_24 = load i32, ptr @_Cg.Main, align 4, !dbg !33
|
||||
// CHECK:STDOUT: ret i32 %.loc8_24, !dbg !34
|
||||
// CHECK:STDOUT: }
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: ; Function Attrs: nounwind
|
||||
// CHECK:STDOUT: define i32 @_CG.Main() #0 !dbg !35 {
|
||||
// CHECK:STDOUT: entry:
|
||||
// CHECK:STDOUT: %Get.call = call i32 @_CGet.Main.e43630e9a6c38c3f(), !dbg !38
|
||||
// CHECK:STDOUT: ret i32 %Get.call, !dbg !39
|
||||
// CHECK:STDOUT: }
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: ; Function Attrs: nounwind
|
||||
// CHECK:STDOUT: define linkonce_odr i32 @_CGet.Main.e43630e9a6c38c3f() #0 !dbg !40 {
|
||||
// CHECK:STDOUT: %1 = load i32, ptr @_Cg.Main, align 4, !dbg !44
|
||||
// CHECK:STDOUT: ret i32 %1, !dbg !45
|
||||
// CHECK:STDOUT: }
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: attributes #0 = { nounwind }
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: !llvm.dbg.cu = !{!27}
|
||||
// CHECK:STDOUT: !llvm.module.flags = !{!2, !3}
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: !2 = !{i32 7, !"Dwarf Version", i32 5}
|
||||
// CHECK:STDOUT: !3 = !{i32 2, !"Debug Info Version", i32 3}
|
||||
// CHECK:STDOUT: !18 = !DIFile(filename: "lib.carbon", directory: "")
|
||||
// CHECK:STDOUT: !20 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
|
||||
// CHECK:STDOUT: !26 = !DIFile(filename: "use.carbon", directory: "")
|
||||
// CHECK:STDOUT: !27 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !26, producer: "carbon", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug)
|
||||
// CHECK:STDOUT: !28 = !{!20}
|
||||
// CHECK:STDOUT: !29 = !DISubroutineType(types: !28)
|
||||
// CHECK:STDOUT: !30 = distinct !DISubprogram(name: "F", linkageName: "_CF.Main", scope: null, file: !26, line: 8, type: !29, spFlags: DISPFlagDefinition, unit: !27)
|
||||
// CHECK:STDOUT: !33 = !DILocation(line: 8, column: 24, scope: !30)
|
||||
// CHECK:STDOUT: !34 = !DILocation(line: 8, column: 17, scope: !30)
|
||||
// CHECK:STDOUT: !35 = distinct !DISubprogram(name: "G", linkageName: "_CG.Main", scope: null, file: !26, line: 9, type: !29, spFlags: DISPFlagDefinition, unit: !27)
|
||||
// CHECK:STDOUT: !38 = !DILocation(line: 9, column: 24, scope: !35)
|
||||
// CHECK:STDOUT: !39 = !DILocation(line: 9, column: 17, scope: !35)
|
||||
// CHECK:STDOUT: !40 = distinct !DISubprogram(name: "Get", linkageName: "_CGet.Main.e43630e9a6c38c3f", scope: null, file: !18, line: 6, type: !29, spFlags: DISPFlagDefinition, unit: !27)
|
||||
// CHECK:STDOUT: !44 = !DILocation(line: 6, column: 39, scope: !40)
|
||||
// CHECK:STDOUT: !45 = !DILocation(line: 6, column: 32, scope: !40)
|
||||
// CHECK:STDOUT:
|
||||
Reference in new issue
Block a user