mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 12:51:04 +01:00
Fix lowering crash when a SpecificInst's inst_id doesn't emit a value (#7880)
This commit is contained in:
@@ -174,7 +174,8 @@ auto FunctionContext::IsConstant(SemIR::InstId inst_id) -> bool {
|
||||
.second.is_constant();
|
||||
}
|
||||
|
||||
auto FunctionContext::GetValue(SemIR::InstId inst_id) -> llvm::Value* {
|
||||
auto FunctionContext::GetValue(SemIR::InstId inst_id, bool require_value)
|
||||
-> llvm::Value* {
|
||||
// Singletons are types, as is the builtin TypeType, with the same empty
|
||||
// lowered value.
|
||||
if (SemIR::IsSingletonInstId(inst_id) ||
|
||||
@@ -193,9 +194,17 @@ auto FunctionContext::GetValue(SemIR::InstId inst_id) -> llvm::Value* {
|
||||
auto [const_ir, const_id] = GetConstantValueInSpecific(
|
||||
specific_sem_ir(), specific_id_, sem_ir(), inst_id);
|
||||
CARBON_CHECK(const_ir == &sem_ir() || const_ir == &specific_sem_ir());
|
||||
CARBON_CHECK(const_id.is_concrete(),
|
||||
"Missing value: {0} {1} in {2} has non-concrete value {3}",
|
||||
inst_id, sem_ir().insts().Get(inst_id), specific_id_, const_id);
|
||||
if (require_value) {
|
||||
CARBON_CHECK(const_id.is_concrete(),
|
||||
"Missing value: {0} {1} in {2} has non-concrete value {3}",
|
||||
inst_id, sem_ir().insts().Get(inst_id), specific_id_,
|
||||
const_id);
|
||||
}
|
||||
|
||||
if (!const_id.is_concrete()) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// We can only pass on the InstId if it refers to the file in which the
|
||||
// constant value was provided.
|
||||
auto* global = GetFileContext(const_ir).GetConstant(
|
||||
|
||||
@@ -116,7 +116,13 @@ class FunctionContext {
|
||||
auto IsConstant(SemIR::InstId) -> bool;
|
||||
|
||||
// Returns a value for the given instruction.
|
||||
auto GetValue(SemIR::InstId inst_id) -> llvm::Value*;
|
||||
//
|
||||
// If `require_value` is true, the instruction is required to have emitted a
|
||||
// value or have a concrete constant in the current specific, and will CHECK
|
||||
// if these conditions are not met. If `require_value` is false, `nullptr`
|
||||
// will be returned instead if there is no value for the instruction.
|
||||
auto GetValue(SemIR::InstId inst_id, bool require_value = true)
|
||||
-> llvm::Value*;
|
||||
|
||||
// Sets the value for the given instruction.
|
||||
auto SetLocal(SemIR::InstId inst_id, llvm::Value* value) -> void {
|
||||
|
||||
@@ -363,7 +363,13 @@ auto HandleInst(FunctionContext& context, SemIR::InstId inst_id,
|
||||
CARBON_CHECK(&context.sem_ir() == &context.specific_sem_ir() &&
|
||||
context.specific_id() == inst.specific_id,
|
||||
"Runtime specific_inst refers to a different specific");
|
||||
context.SetLocal(inst_id, context.GetValue(inst.inst_id));
|
||||
|
||||
// When lowering templates, the specific may contain instructions that
|
||||
// neither emit a value during lowering nor have a constant value, so set
|
||||
// require_value=false.
|
||||
if (auto* value = context.GetValue(inst.inst_id, /*require_value=*/false)) {
|
||||
context.SetLocal(inst_id, value);
|
||||
}
|
||||
}
|
||||
|
||||
auto HandleInst(FunctionContext& context, SemIR::InstId inst_id,
|
||||
|
||||
+178
@@ -0,0 +1,178 @@
|
||||
// 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/convert.carbon
|
||||
//
|
||||
// AUTOUPDATE
|
||||
// TIP: To test this file alone, run:
|
||||
// TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/lower/testdata/template/class.carbon
|
||||
// TIP: To dump output, run:
|
||||
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/lower/testdata/template/class.carbon
|
||||
|
||||
// --- init_class.carbon
|
||||
library "[[@TEST_NAME]]";
|
||||
|
||||
class B {
|
||||
impl as Core.Copy {
|
||||
fn Op(unused self) -> Self {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class A(template T: type) {
|
||||
fn Make(v: T) -> A(T) {
|
||||
return {
|
||||
.field = v,
|
||||
};
|
||||
}
|
||||
|
||||
var field: T;
|
||||
}
|
||||
|
||||
fn Run() {
|
||||
A(B).Make({});
|
||||
}
|
||||
|
||||
// CHECK:STDOUT: ; ---
|
||||
// CHECK:STDOUT: ; ModuleID = 'init_class.carbon'
|
||||
// CHECK:STDOUT: source_filename = "init_class.carbon"
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: @B.val = internal constant {} zeroinitializer
|
||||
// CHECK:STDOUT: @B.val.loc6_16 = internal constant {} zeroinitializer
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: ; Function Attrs: nounwind
|
||||
// CHECK:STDOUT: define void @"_COp.B.Main:Copy.Core"(ptr sret({}) %return, ptr %self) #0 !dbg !24 {
|
||||
// CHECK:STDOUT: entry:
|
||||
// CHECK:STDOUT: call void @llvm.memcpy.p0.p0.i64(ptr align 1 %return, ptr align 1 @B.val.loc6_16, i64 0, i1 false), !dbg !29
|
||||
// CHECK:STDOUT: ret void, !dbg !29
|
||||
// CHECK:STDOUT: }
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: ; Function Attrs: nounwind
|
||||
// CHECK:STDOUT: define i32 @main() #0 !dbg !31 {
|
||||
// CHECK:STDOUT: entry:
|
||||
// CHECK:STDOUT: %.loc22_15.1.temp = alloca { {} }, align 1, !dbg !32
|
||||
// CHECK:STDOUT: %.loc22_14.2.temp = alloca {}, align 1, !dbg !33
|
||||
// CHECK:STDOUT: call void @llvm.lifetime.start.p0(ptr %.loc22_15.1.temp), !dbg !32
|
||||
// CHECK:STDOUT: call void @llvm.lifetime.start.p0(ptr %.loc22_14.2.temp), !dbg !33
|
||||
// CHECK:STDOUT: call void @_CMake.A.Main.a6a60dfee2d7d273(ptr %.loc22_15.1.temp, ptr @B.val.loc6_16), !dbg !32
|
||||
// CHECK:STDOUT: call void @"_CSelfDestruct.d32110776493a13a:core.Destroy.Core"(ptr %.loc22_15.1.temp), !dbg !32
|
||||
// CHECK:STDOUT: call void @"_CSelfDestruct.8c4b83ecd900f1ea:core.Destroy.Core"(ptr @B.val), !dbg !33
|
||||
// CHECK:STDOUT: ret i32 0, !dbg !34
|
||||
// CHECK:STDOUT: }
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: ; Function Attrs: nounwind
|
||||
// CHECK:STDOUT: define weak_odr void @"_CSelfDestruct.cb8adced19392b99:core.Destroy.Core"(ptr %self) #0 !dbg !35 {
|
||||
// CHECK:STDOUT: entry:
|
||||
// CHECK:STDOUT: ret void, !dbg !37
|
||||
// CHECK:STDOUT: }
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: ; Function Attrs: nounwind
|
||||
// CHECK:STDOUT: define weak_odr void @"_CSelfDestruct.8c4b83ecd900f1ea:core.Destroy.Core"(ptr %self) #0 !dbg !39 {
|
||||
// CHECK:STDOUT: entry:
|
||||
// CHECK:STDOUT: ret void, !dbg !41
|
||||
// CHECK:STDOUT: }
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: ; Function Attrs: nounwind
|
||||
// CHECK:STDOUT: define weak_odr void @"_CSubobjectDestroy.8fbe0c6a865381a4:core.Destroy.Core"(ptr %self) #0 !dbg !43 {
|
||||
// CHECK:STDOUT: entry:
|
||||
// CHECK:STDOUT: %.loc22.field = getelementptr inbounds nuw { {} }, ptr %self, i32 0, i32 0, !dbg !45
|
||||
// CHECK:STDOUT: call void @"_CSelfDestruct.8c4b83ecd900f1ea:core.Destroy.Core"(ptr %.loc22.field), !dbg !45
|
||||
// CHECK:STDOUT: ret void, !dbg !45
|
||||
// CHECK:STDOUT: }
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: ; Function Attrs: nounwind
|
||||
// CHECK:STDOUT: define weak_odr void @"_CSelfDestruct.8fbe0c6a865381a4:core.Destroy.Core"(ptr %self) #0 !dbg !47 {
|
||||
// CHECK:STDOUT: entry:
|
||||
// CHECK:STDOUT: call void @"_CSubobjectDestroy.8fbe0c6a865381a4:core.Destroy.Core"(ptr %self), !dbg !49
|
||||
// CHECK:STDOUT: ret void, !dbg !49
|
||||
// CHECK:STDOUT: }
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: ; Function Attrs: nounwind
|
||||
// CHECK:STDOUT: define weak_odr void @"_CSubobjectDestroy.d32110776493a13a:core.Destroy.Core"(ptr %self) #0 !dbg !51 {
|
||||
// CHECK:STDOUT: entry:
|
||||
// CHECK:STDOUT: ret void, !dbg !53
|
||||
// CHECK:STDOUT: }
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: ; Function Attrs: nounwind
|
||||
// CHECK:STDOUT: define weak_odr void @"_CSelfDestruct.d32110776493a13a:core.Destroy.Core"(ptr %self) #0 !dbg !55 {
|
||||
// CHECK:STDOUT: entry:
|
||||
// CHECK:STDOUT: call void @"_CSubobjectDestroy.d32110776493a13a:core.Destroy.Core"(ptr %self), !dbg !57
|
||||
// CHECK:STDOUT: ret void, !dbg !57
|
||||
// CHECK:STDOUT: }
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: ; Function Attrs: nocallback nofree nosync 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) #1
|
||||
// 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: nounwind
|
||||
// CHECK:STDOUT: define linkonce_odr void @_CMake.A.Main.a6a60dfee2d7d273(ptr sret({ {} }) %return, ptr %v) #0 !dbg !59 {
|
||||
// CHECK:STDOUT: entry:
|
||||
// CHECK:STDOUT: %.b3e.field = getelementptr inbounds nuw { {} }, ptr %return, i32 0, i32 0, !dbg !65
|
||||
// CHECK:STDOUT: call void @"_COp.B.Main:Copy.Core"(ptr %.b3e.field, ptr %v), !dbg !64
|
||||
// CHECK:STDOUT: ret void, !dbg !66
|
||||
// CHECK:STDOUT: }
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: ; uselistorder directives
|
||||
// CHECK:STDOUT: uselistorder ptr @llvm.lifetime.start.p0, { 1, 0 }
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: attributes #0 = { nounwind }
|
||||
// CHECK:STDOUT: attributes #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: !llvm.dbg.cu = !{!15}
|
||||
// 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: !14 = !DIFile(filename: "init_class.carbon", directory: "")
|
||||
// CHECK:STDOUT: !15 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !14, producer: "carbon", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug)
|
||||
// CHECK:STDOUT: !16 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: null, size: 64)
|
||||
// CHECK:STDOUT: !17 = !{!16, !16}
|
||||
// CHECK:STDOUT: !18 = !DISubroutineType(types: !17)
|
||||
// CHECK:STDOUT: !19 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
|
||||
// CHECK:STDOUT: !20 = !{!19}
|
||||
// CHECK:STDOUT: !21 = !DISubroutineType(types: !20)
|
||||
// CHECK:STDOUT: !22 = !{null, !16}
|
||||
// CHECK:STDOUT: !23 = !DISubroutineType(types: !22)
|
||||
// CHECK:STDOUT: !24 = distinct !DISubprogram(name: "Op", linkageName: "_COp.B.Main:Copy.Core", scope: null, file: !14, line: 5, type: !18, spFlags: DISPFlagDefinition, unit: !15, retainedNodes: !30)
|
||||
// CHECK:STDOUT: !25 = !DILocalVariable(arg: 1, scope: !24, type: !16)
|
||||
// CHECK:STDOUT: !29 = !DILocation(line: 6, column: 7, scope: !24)
|
||||
// CHECK:STDOUT: !30 = !{!25}
|
||||
// CHECK:STDOUT: !31 = distinct !DISubprogram(name: "Run", linkageName: "main", scope: null, file: !14, line: 21, type: !21, spFlags: DISPFlagDefinition, unit: !15)
|
||||
// CHECK:STDOUT: !32 = !DILocation(line: 22, column: 3, scope: !31)
|
||||
// CHECK:STDOUT: !33 = !DILocation(line: 22, column: 13, scope: !31)
|
||||
// CHECK:STDOUT: !34 = !DILocation(line: 21, column: 1, scope: !31)
|
||||
// CHECK:STDOUT: !35 = distinct !DISubprogram(name: "SelfDestruct", linkageName: "_CSelfDestruct.cb8adced19392b99:core.Destroy.Core", scope: null, file: !14, line: 22, type: !23, spFlags: DISPFlagDefinition, unit: !15, retainedNodes: !38)
|
||||
// CHECK:STDOUT: !36 = !DILocalVariable(arg: 1, scope: !35, type: !16)
|
||||
// CHECK:STDOUT: !37 = !DILocation(line: 22, column: 3, scope: !35)
|
||||
// CHECK:STDOUT: !38 = !{!36}
|
||||
// CHECK:STDOUT: !39 = distinct !DISubprogram(name: "SelfDestruct", linkageName: "_CSelfDestruct.8c4b83ecd900f1ea:core.Destroy.Core", scope: null, file: !14, line: 22, type: !23, spFlags: DISPFlagDefinition, unit: !15, retainedNodes: !42)
|
||||
// CHECK:STDOUT: !40 = !DILocalVariable(arg: 1, scope: !39, type: !16)
|
||||
// CHECK:STDOUT: !41 = !DILocation(line: 22, column: 3, scope: !39)
|
||||
// CHECK:STDOUT: !42 = !{!40}
|
||||
// CHECK:STDOUT: !43 = distinct !DISubprogram(name: "SubobjectDestroy", linkageName: "_CSubobjectDestroy.8fbe0c6a865381a4:core.Destroy.Core", scope: null, file: !14, line: 22, type: !23, spFlags: DISPFlagDefinition, unit: !15, retainedNodes: !46)
|
||||
// CHECK:STDOUT: !44 = !DILocalVariable(arg: 1, scope: !43, type: !16)
|
||||
// CHECK:STDOUT: !45 = !DILocation(line: 22, column: 3, scope: !43)
|
||||
// CHECK:STDOUT: !46 = !{!44}
|
||||
// CHECK:STDOUT: !47 = distinct !DISubprogram(name: "SelfDestruct", linkageName: "_CSelfDestruct.8fbe0c6a865381a4:core.Destroy.Core", scope: null, file: !14, line: 22, type: !23, spFlags: DISPFlagDefinition, unit: !15, retainedNodes: !50)
|
||||
// CHECK:STDOUT: !48 = !DILocalVariable(arg: 1, scope: !47, type: !16)
|
||||
// CHECK:STDOUT: !49 = !DILocation(line: 22, column: 3, scope: !47)
|
||||
// CHECK:STDOUT: !50 = !{!48}
|
||||
// CHECK:STDOUT: !51 = distinct !DISubprogram(name: "SubobjectDestroy", linkageName: "_CSubobjectDestroy.d32110776493a13a:core.Destroy.Core", scope: null, file: !14, line: 22, type: !23, spFlags: DISPFlagDefinition, unit: !15, retainedNodes: !54)
|
||||
// CHECK:STDOUT: !52 = !DILocalVariable(arg: 1, scope: !51, type: !16)
|
||||
// CHECK:STDOUT: !53 = !DILocation(line: 22, column: 3, scope: !51)
|
||||
// CHECK:STDOUT: !54 = !{!52}
|
||||
// CHECK:STDOUT: !55 = distinct !DISubprogram(name: "SelfDestruct", linkageName: "_CSelfDestruct.d32110776493a13a:core.Destroy.Core", scope: null, file: !14, line: 22, type: !23, spFlags: DISPFlagDefinition, unit: !15, retainedNodes: !58)
|
||||
// CHECK:STDOUT: !56 = !DILocalVariable(arg: 1, scope: !55, type: !16)
|
||||
// CHECK:STDOUT: !57 = !DILocation(line: 22, column: 3, scope: !55)
|
||||
// CHECK:STDOUT: !58 = !{!56}
|
||||
// CHECK:STDOUT: !59 = distinct !DISubprogram(name: "Make", linkageName: "_CMake.A.Main.a6a60dfee2d7d273", scope: null, file: !14, line: 12, type: !18, spFlags: DISPFlagDefinition, unit: !15, retainedNodes: !67)
|
||||
// CHECK:STDOUT: !60 = !DILocalVariable(arg: 1, scope: !59, type: !16)
|
||||
// CHECK:STDOUT: !64 = !DILocation(line: 14, column: 16, scope: !59)
|
||||
// CHECK:STDOUT: !65 = !DILocation(line: 13, column: 12, scope: !59)
|
||||
// CHECK:STDOUT: !66 = !DILocation(line: 13, column: 5, scope: !59)
|
||||
// CHECK:STDOUT: !67 = !{!60}
|
||||
// CHECK:STDOUT:
|
||||
Reference in New Issue
Block a user