Fix lowering of imported global variables. (#6567)

*   When a C++ static data member is imported, evaluate its address to a
    constant like we would for a namespace-scope variable.
*   When an imported variable is used in a way that doesn't require its
    type to be complete, emit the variable with an opaque type instead
    of skipping it (and potentially crashing later).
This commit is contained in:
Richard Smith
2026-01-11 19:22:43 +00:00
committed by GitHub
parent 64fa9cc6ae
commit 935ccce2a6
9 changed files with 247 additions and 26 deletions
+10 -4
View File
@@ -742,10 +742,16 @@ auto EvalConstantInst(Context& context, SemIR::InstId inst_id,
}
auto scope_id = context.entity_names().Get(entity_name_id).parent_scope_id;
if (!scope_id.has_value() ||
!context.insts().Is<SemIR::Namespace>(
context.name_scopes().Get(scope_id).inst_id())) {
// Only namespace-scope variables are reference constants.
if (!scope_id.has_value()) {
return ConstantEvalResult::NotConstant;
}
auto scope_inst =
context.insts().Get(context.name_scopes().Get(scope_id).inst_id());
if (!scope_inst.Is<SemIR::Namespace>() &&
!scope_inst.Is<SemIR::ClassDecl>()) {
// Only namespace-scope and class-scope variables are reference constants.
// Class-scope variables cannot currently be declared directly, but can
// occur when static data members are imported from C++.
return ConstantEvalResult::NotConstant;
}
+8 -8
View File
@@ -1599,7 +1599,7 @@ fn Call(var instance: Cpp.PublicPrivate) {
// CHECK:STDOUT: %S.decl: type = class_decl @S [concrete = constants.%S] {} {}
// CHECK:STDOUT: %static_data.patt: %pattern_type.7ce = ref_binding_pattern static_data [concrete]
// CHECK:STDOUT: %static_data.var_patt: %pattern_type.7ce = var_pattern %static_data.patt [concrete]
// CHECK:STDOUT: %static_data.var: ref %i32 = var %static_data.var_patt
// CHECK:STDOUT: %static_data.var: ref %i32 = var %static_data.var_patt [concrete]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: fn @F(%s.param: %S) {
@@ -1622,7 +1622,7 @@ fn Call(var instance: Cpp.PublicPrivate) {
// CHECK:STDOUT: %Cpp.ref.loc9: <namespace> = name_ref Cpp, imports.%Cpp [concrete = imports.%Cpp]
// CHECK:STDOUT: %S.ref.loc9: type = name_ref S, imports.%S.decl [concrete = constants.%S]
// CHECK:STDOUT: <elided>
// CHECK:STDOUT: %static_data.ref: ref %i32 = name_ref static_data, imports.%static_data.var
// CHECK:STDOUT: %static_data.ref: ref %i32 = name_ref static_data, imports.%static_data.var [concrete = imports.%static_data.var]
// CHECK:STDOUT: %.loc9_20: type = splice_block %i32.loc9 [concrete = constants.%i32] {
// CHECK:STDOUT: %int_32.loc9: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
// CHECK:STDOUT: %i32.loc9: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
@@ -1646,7 +1646,7 @@ fn Call(var instance: Cpp.PublicPrivate) {
// CHECK:STDOUT: imports {
// CHECK:STDOUT: %static_data.patt: %pattern_type.7ce = ref_binding_pattern static_data [concrete]
// CHECK:STDOUT: %static_data.var_patt: %pattern_type.7ce = var_pattern %static_data.patt [concrete]
// CHECK:STDOUT: %static_data.var: ref %i32 = var %static_data.var_patt
// CHECK:STDOUT: %static_data.var: ref %i32 = var %static_data.var_patt [concrete]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: fn @F(%d.param: %Derived) {
@@ -1670,7 +1670,7 @@ fn Call(var instance: Cpp.PublicPrivate) {
// CHECK:STDOUT: }
// CHECK:STDOUT: %Derived.ref.loc13: type = name_ref Derived, file.%Derived.decl [concrete = constants.%Derived]
// CHECK:STDOUT: <elided>
// CHECK:STDOUT: %static_data.ref: ref %i32 = name_ref static_data, imports.%static_data.var
// CHECK:STDOUT: %static_data.ref: ref %i32 = name_ref static_data, imports.%static_data.var [concrete = imports.%static_data.var]
// CHECK:STDOUT: %.loc13_20: type = splice_block %i32.loc13 [concrete = constants.%i32] {
// CHECK:STDOUT: %int_32.loc13: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
// CHECK:STDOUT: %i32.loc13: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
@@ -1699,7 +1699,7 @@ fn Call(var instance: Cpp.PublicPrivate) {
// CHECK:STDOUT: %C.decl: type = class_decl @C [concrete = constants.%C] {} {}
// CHECK:STDOUT: %static_data.patt: %pattern_type.7ce = ref_binding_pattern static_data [concrete]
// CHECK:STDOUT: %static_data.var_patt: %pattern_type.7ce = var_pattern %static_data.patt [concrete]
// CHECK:STDOUT: %static_data.var: ref %i32 = var %static_data.var_patt
// CHECK:STDOUT: %static_data.var: ref %i32 = var %static_data.var_patt [concrete]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: class @Derived {
@@ -1736,7 +1736,7 @@ fn Call(var instance: Cpp.PublicPrivate) {
// CHECK:STDOUT: %unqualified_static_data.patt: %pattern_type.7ce = value_binding_pattern unqualified_static_data [concrete]
// CHECK:STDOUT: }
// CHECK:STDOUT: <elided>
// CHECK:STDOUT: %static_data.ref.loc11: ref %i32 = name_ref static_data, imports.%static_data.var
// CHECK:STDOUT: %static_data.ref.loc11: ref %i32 = name_ref static_data, imports.%static_data.var [concrete = imports.%static_data.var]
// CHECK:STDOUT: %.loc11_34: type = splice_block %i32.loc11 [concrete = constants.%i32] {
// CHECK:STDOUT: %int_32.loc11: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
// CHECK:STDOUT: %i32.loc11: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
@@ -1747,7 +1747,7 @@ fn Call(var instance: Cpp.PublicPrivate) {
// CHECK:STDOUT: %derived_static_data.patt: %pattern_type.7ce = value_binding_pattern derived_static_data [concrete]
// CHECK:STDOUT: }
// CHECK:STDOUT: %Derived.ref: type = name_ref Derived, file.%Derived.decl [concrete = constants.%Derived]
// CHECK:STDOUT: %static_data.ref.loc12: ref %i32 = name_ref static_data, imports.%static_data.var
// CHECK:STDOUT: %static_data.ref.loc12: ref %i32 = name_ref static_data, imports.%static_data.var [concrete = imports.%static_data.var]
// CHECK:STDOUT: %.loc12_30: type = splice_block %i32.loc12 [concrete = constants.%i32] {
// CHECK:STDOUT: %int_32.loc12: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
// CHECK:STDOUT: %i32.loc12: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
@@ -1759,7 +1759,7 @@ fn Call(var instance: Cpp.PublicPrivate) {
// CHECK:STDOUT: }
// CHECK:STDOUT: %Cpp.ref: <namespace> = name_ref Cpp, imports.%Cpp [concrete = imports.%Cpp]
// CHECK:STDOUT: %C.ref: type = name_ref C, imports.%C.decl [concrete = constants.%C]
// CHECK:STDOUT: %static_data.ref.loc13: ref %i32 = name_ref static_data, imports.%static_data.var
// CHECK:STDOUT: %static_data.ref.loc13: ref %i32 = name_ref static_data, imports.%static_data.var [concrete = imports.%static_data.var]
// CHECK:STDOUT: %.loc13_27: type = splice_block %i32.loc13 [concrete = constants.%i32] {
// CHECK:STDOUT: %int_32.loc13: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
// CHECK:STDOUT: %i32.loc13: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
+2 -2
View File
@@ -375,7 +375,7 @@ fn MyF(bar: Cpp.Bar(Cpp.X)*);
// CHECK:STDOUT: %Bar.decl: type = class_decl @Bar [concrete = constants.%Bar] {} {}
// CHECK:STDOUT: %foo.patt: %pattern_type = ref_binding_pattern foo [concrete]
// CHECK:STDOUT: %foo.var_patt: %pattern_type = var_pattern %foo.patt [concrete]
// CHECK:STDOUT: %foo.var: ref %ptr.f68 = var %foo.var_patt
// CHECK:STDOUT: %foo.var: ref %ptr.f68 = var %foo.var_patt [concrete]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: fn @MyF() {
@@ -385,7 +385,7 @@ fn MyF(bar: Cpp.Bar(Cpp.X)*);
// CHECK:STDOUT: }
// CHECK:STDOUT: %Cpp.ref.loc8_23: <namespace> = name_ref Cpp, imports.%Cpp [concrete = imports.%Cpp]
// CHECK:STDOUT: %Bar.ref.loc8_26: type = name_ref Bar, imports.%Bar.decl [concrete = constants.%Bar]
// CHECK:STDOUT: %foo.ref: ref %ptr.f68 = name_ref foo, imports.%foo.var
// CHECK:STDOUT: %foo.ref: ref %ptr.f68 = name_ref foo, imports.%foo.var [concrete = imports.%foo.var]
// CHECK:STDOUT: %.loc8_19: type = splice_block %ptr [concrete = constants.%ptr.f68] {
// CHECK:STDOUT: %Cpp.ref.loc8_12: <namespace> = name_ref Cpp, imports.%Cpp [concrete = imports.%Cpp]
// CHECK:STDOUT: %Bar.ref.loc8_15: type = name_ref Bar, imports.%Bar.decl [concrete = constants.%Bar]
+2 -2
View File
@@ -324,7 +324,7 @@ fn MyF(bar: Cpp.Bar(Cpp.X)*);
// CHECK:STDOUT: %Bar.decl: type = class_decl @Bar [concrete = constants.%Bar] {} {}
// CHECK:STDOUT: %foo.patt: %pattern_type = ref_binding_pattern foo [concrete]
// CHECK:STDOUT: %foo.var_patt: %pattern_type = var_pattern %foo.patt [concrete]
// CHECK:STDOUT: %foo.var: ref %ptr.f68 = var %foo.var_patt
// CHECK:STDOUT: %foo.var: ref %ptr.f68 = var %foo.var_patt [concrete]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: fn @MyF() {
@@ -334,7 +334,7 @@ fn MyF(bar: Cpp.Bar(Cpp.X)*);
// CHECK:STDOUT: }
// CHECK:STDOUT: %Cpp.ref.loc8_23: <namespace> = name_ref Cpp, imports.%Cpp [concrete = imports.%Cpp]
// CHECK:STDOUT: %Bar.ref.loc8_26: type = name_ref Bar, imports.%Bar.decl [concrete = constants.%Bar]
// CHECK:STDOUT: %foo.ref: ref %ptr.f68 = name_ref foo, imports.%foo.var
// CHECK:STDOUT: %foo.ref: ref %ptr.f68 = name_ref foo, imports.%foo.var [concrete = imports.%foo.var]
// CHECK:STDOUT: %.loc8_19: type = splice_block %ptr [concrete = constants.%ptr.f68] {
// CHECK:STDOUT: %Cpp.ref.loc8_12: <namespace> = name_ref Cpp, imports.%Cpp [concrete = imports.%Cpp]
// CHECK:STDOUT: %Bar.ref.loc8_15: type = name_ref Bar, imports.%Bar.decl [concrete = constants.%Bar]
@@ -52,6 +52,8 @@ let pwb: Cpp.B* = &Cpp.Wrap.r#var(Cpp.B);
// CHECK:STDOUT: %Wrap: type = class_type @Wrap [concrete]
// CHECK:STDOUT: %var.type.17b: type = cpp_type_template_type r#var [concrete]
// CHECK:STDOUT: %var.template.df3: %var.type.17b = struct_value () [concrete]
// CHECK:STDOUT: %addr.113: %ptr.270 = addr_of imports.%var.var.2e6 [concrete]
// CHECK:STDOUT: %addr.594: %ptr.a04 = addr_of imports.%var.var.bab [concrete]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: imports {
@@ -75,10 +77,10 @@ let pwb: Cpp.B* = &Cpp.Wrap.r#var(Cpp.B);
// CHECK:STDOUT: %var.template.df3: %var.type.17b = struct_value () [concrete = constants.%var.template.df3]
// CHECK:STDOUT: %var.patt.022: %pattern_type.9de = ref_binding_pattern r#var [concrete]
// CHECK:STDOUT: %var.var_patt.0cb: %pattern_type.9de = var_pattern %var.patt.022 [concrete]
// CHECK:STDOUT: %var.var.2e6: ref %A = var %var.var_patt.0cb
// CHECK:STDOUT: %var.var.2e6: ref %A = var %var.var_patt.0cb [concrete]
// CHECK:STDOUT: %var.patt.605: %pattern_type.d0f = ref_binding_pattern r#var [concrete]
// CHECK:STDOUT: %var.var_patt.efb: %pattern_type.d0f = var_pattern %var.patt.605 [concrete]
// CHECK:STDOUT: %var.var.bab: ref %B = var %var.var_patt.efb
// CHECK:STDOUT: %var.var.bab: ref %B = var %var.var_patt.efb [concrete]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: file {
@@ -137,13 +139,13 @@ let pwb: Cpp.B* = &Cpp.Wrap.r#var(Cpp.B);
// CHECK:STDOUT: %var.ref.loc9: %var.type.17b = name_ref r#var, imports.%var.template.df3 [concrete = constants.%var.template.df3]
// CHECK:STDOUT: %Cpp.ref.loc9_35: <namespace> = name_ref Cpp, imports.%Cpp [concrete = imports.%Cpp]
// CHECK:STDOUT: %A.ref.loc9: type = name_ref A, imports.%A.decl [concrete = constants.%A]
// CHECK:STDOUT: %addr.loc9: %ptr.270 = addr_of imports.%var.var.2e6
// CHECK:STDOUT: %addr.loc9: %ptr.270 = addr_of imports.%var.var.2e6 [concrete = constants.%addr.113]
// CHECK:STDOUT: %Cpp.ref.loc10_20: <namespace> = name_ref Cpp, imports.%Cpp [concrete = imports.%Cpp]
// CHECK:STDOUT: %Wrap.ref.loc10: type = name_ref Wrap, imports.%Wrap.decl [concrete = constants.%Wrap]
// CHECK:STDOUT: %var.ref.loc10: %var.type.17b = name_ref r#var, imports.%var.template.df3 [concrete = constants.%var.template.df3]
// CHECK:STDOUT: %Cpp.ref.loc10_35: <namespace> = name_ref Cpp, imports.%Cpp [concrete = imports.%Cpp]
// CHECK:STDOUT: %B.ref.loc10: type = name_ref B, imports.%B.decl [concrete = constants.%B]
// CHECK:STDOUT: %addr.loc10: %ptr.a04 = addr_of imports.%var.var.bab
// CHECK:STDOUT: %addr.loc10: %ptr.a04 = addr_of imports.%var.var.bab [concrete = constants.%addr.594]
// CHECK:STDOUT: <elided>
// CHECK:STDOUT: }
// CHECK:STDOUT:
+5 -3
View File
@@ -10,6 +10,7 @@
#include "llvm/IR/Value.h"
#include "toolchain/base/kind_switch.h"
#include "toolchain/lower/file_context.h"
#include "toolchain/sem_ir/expr_info.h"
#include "toolchain/sem_ir/inst.h"
#include "toolchain/sem_ir/typed_insts.h"
@@ -345,9 +346,10 @@ auto LowerConstants(FileContext& file_context,
auto inst = file_context.sem_ir().insts().Get(inst_id);
if (inst.type_id().has_value() &&
!file_context.sem_ir().types().IsComplete(inst.type_id())) {
// If a constant doesn't have a complete type, that means we imported it
// but didn't actually use it.
!file_context.sem_ir().types().IsComplete(inst.type_id()) &&
!IsRefCategory(SemIR::GetExprCategory(context.sem_ir(), inst_id))) {
// If a non-reference constant doesn't have a complete type, that means we
// imported it but didn't actually use it.
continue;
}
+12
View File
@@ -102,6 +102,15 @@ class Context {
return form_type_;
}
// Returns the opaque LLVM struct type used to represent an incomplete type.
auto GetOpaqueType() -> llvm::StructType* {
if (!opaque_type_) {
// `type` is lowered to an empty LLVM StructType.
opaque_type_ = llvm::StructType::create(*llvm_context_, "opaque");
}
return opaque_type_;
}
auto llvm_context() -> llvm::LLVMContext& { return *llvm_context_; }
auto llvm_module() -> llvm::Module& { return *llvm_module_; }
auto file_system() -> llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem>& {
@@ -170,6 +179,9 @@ class Context {
// Lowered version of the builtin type `Core.Form`.
llvm::StructType* form_type_ = nullptr;
// An opaque type, used for external globals with incomplete types.
llvm::StructType* opaque_type_ = nullptr;
// Global format string for `printf.int.format` used by the PrintInt builtin.
llvm::Value* printf_int_format_string_ = nullptr;
+3 -2
View File
@@ -71,8 +71,9 @@ class FileContext {
CARBON_CHECK(type_id.is_concrete(), "Lowering symbolic type {0}: {1}",
type_id, sem_ir().types().GetAsInst(type_id));
auto result = types_.Get(type_id);
CARBON_CHECK(result.llvm_ir_type, "Missing type {0}: {1}", type_id,
sem_ir().types().GetAsInst(type_id));
if (!result.llvm_ir_type) {
result.llvm_ir_type = context_->GetOpaqueType();
}
return result;
}
+199 -1
View File
@@ -11,7 +11,7 @@
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/lower/testdata/interop/cpp/globals.carbon
// ============================================================================
// Global
// Global variables
// ============================================================================
// --- global.h
@@ -80,6 +80,80 @@ fn ThreadLocalTriggerInit() -> i32 {
return Cpp.thread_local_global_with_init;
}
// ============================================================================
// Static data members
// ============================================================================
// --- static.h
struct X {
static inline int n = 42;
};
// --- import_static.carbon
library "[[@TEST_NAME]]";
import Cpp library "static.h";
fn ReadStatic() -> i32 {
return Cpp.X.n;
}
// ============================================================================
// Variable templates
// ============================================================================
// --- var_template.h
template<typename T> T tmpl = T();
struct Y {
Y();
};
// --- import_var_template.carbon
library "[[@TEST_NAME]]";
import Cpp library "var_template.h";
fn ReadVarTemplate() -> i32 {
return Cpp.tmpl(i32);
}
fn ReadVarTemplateWithConstructor() -> Cpp.Y* {
return &Cpp.tmpl(Cpp.Y);
}
// ============================================================================
// Static data member templates
// ============================================================================
// --- static_template.h
struct X {
template<typename T> static T static_tmpl = T();
};
struct Y {
Y();
};
// --- import_static_template.carbon
library "[[@TEST_NAME]]";
import Cpp library "static_template.h";
fn ReadStaticVarTemplate() -> i32 {
return Cpp.X.static_tmpl(i32);
}
fn ReadStaticVarTemplateWithConstructor() -> Cpp.Y* {
return &Cpp.X.static_tmpl(Cpp.Y);
}
// CHECK:STDOUT: ; ModuleID = 'import_global.carbon'
// CHECK:STDOUT: source_filename = "import_global.carbon"
// CHECK:STDOUT: target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
@@ -315,3 +389,127 @@ fn ThreadLocalTriggerInit() -> i32 {
// CHECK:STDOUT: !16 = !DILocation(line: 14, column: 10, scope: !12)
// CHECK:STDOUT: !17 = !DILocation(line: 14, column: 3, scope: !12)
// CHECK:STDOUT: !18 = !{!"branch_weights", i32 1, i32 1023}
// CHECK:STDOUT: ; ModuleID = 'import_static.carbon'
// CHECK:STDOUT: source_filename = "import_static.carbon"
// CHECK:STDOUT: target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
// CHECK:STDOUT: target triple = "x86_64-unknown-linux-gnu"
// CHECK:STDOUT:
// CHECK:STDOUT: @_ZN1X1nE = external global i32
// CHECK:STDOUT:
// CHECK:STDOUT: ; Function Attrs: nounwind
// CHECK:STDOUT: define i32 @_CReadStatic.Main() #0 !dbg !7 {
// CHECK:STDOUT: entry:
// CHECK:STDOUT: %.loc7 = load i32, ptr @_ZN1X1nE, align 4, !dbg !11
// CHECK:STDOUT: ret i32 %.loc7, !dbg !12
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: attributes #0 = { nounwind }
// CHECK:STDOUT:
// CHECK:STDOUT: !llvm.module.flags = !{!0, !1, !2, !3, !4}
// CHECK:STDOUT: !llvm.dbg.cu = !{!5}
// CHECK:STDOUT:
// CHECK:STDOUT: !0 = !{i32 7, !"Dwarf Version", i32 5}
// CHECK:STDOUT: !1 = !{i32 2, !"Debug Info Version", i32 3}
// CHECK:STDOUT: !2 = !{i32 1, !"wchar_size", i32 4}
// CHECK:STDOUT: !3 = !{i32 8, !"PIC Level", i32 0}
// CHECK:STDOUT: !4 = !{i32 7, !"PIE Level", i32 2}
// CHECK:STDOUT: !5 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !6, producer: "carbon", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug)
// CHECK:STDOUT: !6 = !DIFile(filename: "import_static.carbon", directory: "")
// CHECK:STDOUT: !7 = distinct !DISubprogram(name: "ReadStatic", linkageName: "_CReadStatic.Main", scope: null, file: !6, line: 6, type: !8, spFlags: DISPFlagDefinition, unit: !5)
// CHECK:STDOUT: !8 = !DISubroutineType(types: !9)
// CHECK:STDOUT: !9 = !{!10}
// CHECK:STDOUT: !10 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
// CHECK:STDOUT: !11 = !DILocation(line: 7, column: 10, scope: !7)
// CHECK:STDOUT: !12 = !DILocation(line: 7, column: 3, scope: !7)
// CHECK:STDOUT: ; ModuleID = 'import_var_template.carbon'
// CHECK:STDOUT: source_filename = "import_var_template.carbon"
// CHECK:STDOUT: target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
// CHECK:STDOUT: target triple = "x86_64-unknown-linux-gnu"
// CHECK:STDOUT:
// CHECK:STDOUT: %opaque = type opaque
// CHECK:STDOUT:
// CHECK:STDOUT: @_Z4tmplIiE = external global i32
// CHECK:STDOUT: @_Z4tmplI1YE = external global %opaque
// CHECK:STDOUT:
// CHECK:STDOUT: ; Function Attrs: nounwind
// CHECK:STDOUT: define i32 @_CReadVarTemplate.Main() #0 !dbg !7 {
// CHECK:STDOUT: entry:
// CHECK:STDOUT: %.loc7 = load i32, ptr @_Z4tmplIiE, align 4, !dbg !11
// CHECK:STDOUT: ret i32 %.loc7, !dbg !12
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: ; Function Attrs: nounwind
// CHECK:STDOUT: define ptr @_CReadVarTemplateWithConstructor.Main() #0 !dbg !13 {
// CHECK:STDOUT: entry:
// CHECK:STDOUT: ret ptr @_Z4tmplI1YE, !dbg !17
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: attributes #0 = { nounwind }
// CHECK:STDOUT:
// CHECK:STDOUT: !llvm.module.flags = !{!0, !1, !2, !3, !4}
// CHECK:STDOUT: !llvm.dbg.cu = !{!5}
// CHECK:STDOUT:
// CHECK:STDOUT: !0 = !{i32 7, !"Dwarf Version", i32 5}
// CHECK:STDOUT: !1 = !{i32 2, !"Debug Info Version", i32 3}
// CHECK:STDOUT: !2 = !{i32 1, !"wchar_size", i32 4}
// CHECK:STDOUT: !3 = !{i32 8, !"PIC Level", i32 0}
// CHECK:STDOUT: !4 = !{i32 7, !"PIE Level", i32 2}
// CHECK:STDOUT: !5 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !6, producer: "carbon", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug)
// CHECK:STDOUT: !6 = !DIFile(filename: "import_var_template.carbon", directory: "")
// CHECK:STDOUT: !7 = distinct !DISubprogram(name: "ReadVarTemplate", linkageName: "_CReadVarTemplate.Main", scope: null, file: !6, line: 6, type: !8, spFlags: DISPFlagDefinition, unit: !5)
// CHECK:STDOUT: !8 = !DISubroutineType(types: !9)
// CHECK:STDOUT: !9 = !{!10}
// CHECK:STDOUT: !10 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
// CHECK:STDOUT: !11 = !DILocation(line: 7, column: 10, scope: !7)
// CHECK:STDOUT: !12 = !DILocation(line: 7, column: 3, scope: !7)
// CHECK:STDOUT: !13 = distinct !DISubprogram(name: "ReadVarTemplateWithConstructor", linkageName: "_CReadVarTemplateWithConstructor.Main", scope: null, file: !6, line: 10, type: !14, spFlags: DISPFlagDefinition, unit: !5)
// CHECK:STDOUT: !14 = !DISubroutineType(types: !15)
// CHECK:STDOUT: !15 = !{!16}
// CHECK:STDOUT: !16 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: null, size: 8)
// CHECK:STDOUT: !17 = !DILocation(line: 11, column: 3, scope: !13)
// CHECK:STDOUT: ; ModuleID = 'import_static_template.carbon'
// CHECK:STDOUT: source_filename = "import_static_template.carbon"
// CHECK:STDOUT: target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
// CHECK:STDOUT: target triple = "x86_64-unknown-linux-gnu"
// CHECK:STDOUT:
// CHECK:STDOUT: %opaque = type opaque
// CHECK:STDOUT:
// CHECK:STDOUT: @_ZN1X11static_tmplIiEE = external global i32
// CHECK:STDOUT: @_ZN1X11static_tmplI1YEE = external global %opaque
// CHECK:STDOUT:
// CHECK:STDOUT: ; Function Attrs: nounwind
// CHECK:STDOUT: define i32 @_CReadStaticVarTemplate.Main() #0 !dbg !7 {
// CHECK:STDOUT: entry:
// CHECK:STDOUT: %.loc7 = load i32, ptr @_ZN1X11static_tmplIiEE, align 4, !dbg !11
// CHECK:STDOUT: ret i32 %.loc7, !dbg !12
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: ; Function Attrs: nounwind
// CHECK:STDOUT: define ptr @_CReadStaticVarTemplateWithConstructor.Main() #0 !dbg !13 {
// CHECK:STDOUT: entry:
// CHECK:STDOUT: ret ptr @_ZN1X11static_tmplI1YEE, !dbg !17
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: attributes #0 = { nounwind }
// CHECK:STDOUT:
// CHECK:STDOUT: !llvm.module.flags = !{!0, !1, !2, !3, !4}
// CHECK:STDOUT: !llvm.dbg.cu = !{!5}
// CHECK:STDOUT:
// CHECK:STDOUT: !0 = !{i32 7, !"Dwarf Version", i32 5}
// CHECK:STDOUT: !1 = !{i32 2, !"Debug Info Version", i32 3}
// CHECK:STDOUT: !2 = !{i32 1, !"wchar_size", i32 4}
// CHECK:STDOUT: !3 = !{i32 8, !"PIC Level", i32 0}
// CHECK:STDOUT: !4 = !{i32 7, !"PIE Level", i32 2}
// CHECK:STDOUT: !5 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !6, producer: "carbon", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug)
// CHECK:STDOUT: !6 = !DIFile(filename: "import_static_template.carbon", directory: "")
// CHECK:STDOUT: !7 = distinct !DISubprogram(name: "ReadStaticVarTemplate", linkageName: "_CReadStaticVarTemplate.Main", scope: null, file: !6, line: 6, type: !8, spFlags: DISPFlagDefinition, unit: !5)
// CHECK:STDOUT: !8 = !DISubroutineType(types: !9)
// CHECK:STDOUT: !9 = !{!10}
// CHECK:STDOUT: !10 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
// CHECK:STDOUT: !11 = !DILocation(line: 7, column: 10, scope: !7)
// CHECK:STDOUT: !12 = !DILocation(line: 7, column: 3, scope: !7)
// CHECK:STDOUT: !13 = distinct !DISubprogram(name: "ReadStaticVarTemplateWithConstructor", linkageName: "_CReadStaticVarTemplateWithConstructor.Main", scope: null, file: !6, line: 10, type: !14, spFlags: DISPFlagDefinition, unit: !5)
// CHECK:STDOUT: !14 = !DISubroutineType(types: !15)
// CHECK:STDOUT: !15 = !{!16}
// CHECK:STDOUT: !16 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: null, size: 8)
// CHECK:STDOUT: !17 = !DILocation(line: 11, column: 3, scope: !13)