Skip to content

Commit

Permalink
Fix gep result type calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
Y-Nak committed Sep 11, 2023
1 parent ecb3f59 commit d558e91
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
11 changes: 8 additions & 3 deletions crates/ir/src/insn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
5 changes: 3 additions & 2 deletions crates/parser/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;"
));
}
Expand Down

0 comments on commit d558e91

Please sign in to comment.