From d558e91d2f98f90770e0e0ebd37ba7be1a7be6b4 Mon Sep 17 00:00:00 2001 From: Yoshitomo Nakanishi Date: Mon, 11 Sep 2023 12:19:40 +0200 Subject: [PATCH] Fix gep result type calculation --- crates/ir/src/insn.rs | 11 ++++++++--- crates/parser/src/parser.rs | 5 +++-- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/crates/ir/src/insn.rs b/crates/ir/src/insn.rs index 886b0aa..b672701 100644 --- a/crates/ir/src/insn.rs +++ b/crates/ir/src/insn.rs @@ -770,13 +770,18 @@ fn get_gep_result_type(dfg: &DataFlowGraph, base: Value, indices: &[Value]) -> T }); let mut result_ty = base_ty; - for &index in indices { + for (i, &index) in indices.iter().enumerate() { let Type::Compound(compound) = result_ty else { - unreachable!() + if indices.len() - i == 1 { + break; + } else { + unreachable!() + } }; result_ty = ctx.with_ty_store(|s| match s.resolve_compound(compound) { - CompoundTypeData::Array { elem, .. } | CompoundTypeData::Ptr(elem) => *elem, + CompoundTypeData::Array { elem, .. } => *elem, + CompoundTypeData::Ptr(_) => result_ty, CompoundTypeData::Struct(s) => { let index = match dfg.value_data(index) { ValueData::Immediate { imm, .. } => imm.as_usize(), diff --git a/crates/parser/src/parser.rs b/crates/parser/src/parser.rs index 98a9be0..7c77acf 100644 --- a/crates/parser/src/parser.rs +++ b/crates/parser/src/parser.rs @@ -933,9 +933,10 @@ mod tests { #[test] fn test_gep() { assert!(test_func_parser( - "func public %test(v0.**i32) -> **i32: + "func public %test(v0.*i32, v1.*[*i64; 10]) -> *i32: block0: - v1.*i32 = gep v0 10.i32; + v2.*i32 = gep v0 10.i32; + v3.**i64 = gep v1 10.i32; return v1;" )); }