Skip to content

Commit

Permalink
Correct index to offset calculation for matrix compound literals
Browse files Browse the repository at this point in the history
  • Loading branch information
gingerBill committed Nov 2, 2021
1 parent f03e0be commit c202305
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
6 changes: 3 additions & 3 deletions src/llvm_backend_const.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1013,7 +1013,7 @@ lbValue lb_const_value(lbModule *m, Type *type, ExactValue value, bool allow_loc
TypeAndValue tav = fv->value->tav;
LLVMValueRef val = lb_const_value(m, elem_type, tav.value, allow_local).value;
for (i64 k = lo; k < hi; k++) {
i64 offset = matrix_index_to_offset(type, k);
i64 offset = matrix_row_major_index_to_offset(type, k);
GB_ASSERT(values[offset] == nullptr);
values[offset] = val;
}
Expand All @@ -1023,7 +1023,7 @@ lbValue lb_const_value(lbModule *m, Type *type, ExactValue value, bool allow_loc
i64 index = exact_value_to_i64(index_tav.value);
TypeAndValue tav = fv->value->tav;
LLVMValueRef val = lb_const_value(m, elem_type, tav.value, allow_local).value;
i64 offset = matrix_index_to_offset(type, index);
i64 offset = matrix_row_major_index_to_offset(type, index);
GB_ASSERT(values[offset] == nullptr);
values[offset] = val;
}
Expand All @@ -1045,7 +1045,7 @@ lbValue lb_const_value(lbModule *m, Type *type, ExactValue value, bool allow_loc
for_array(i, cl->elems) {
TypeAndValue tav = cl->elems[i]->tav;
GB_ASSERT(tav.mode != Addressing_Invalid);
i64 offset = matrix_index_to_offset(type, i);
i64 offset = matrix_row_major_index_to_offset(type, i);
values[offset] = lb_const_value(m, elem_type, tav.value, allow_local).value;
}
for (isize i = 0; i < total_count; i++) {
Expand Down
6 changes: 3 additions & 3 deletions src/llvm_backend_expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4465,7 +4465,7 @@ lbAddr lb_build_addr(lbProcedure *p, Ast *expr) {
lbCompoundLitElemTempData data = {};
data.value = value;

data.elem_index = cast(i32)matrix_index_to_offset(bt, k);
data.elem_index = cast(i32)matrix_row_major_index_to_offset(bt, k);
array_add(&temp_data, data);
}

Expand All @@ -4479,7 +4479,7 @@ lbAddr lb_build_addr(lbProcedure *p, Ast *expr) {
data.value = lb_emit_conv(p, value, et);
data.expr = fv->value;

data.elem_index = cast(i32)matrix_index_to_offset(bt, index);
data.elem_index = cast(i32)matrix_row_major_index_to_offset(bt, index);
array_add(&temp_data, data);
}

Expand All @@ -4489,7 +4489,7 @@ lbAddr lb_build_addr(lbProcedure *p, Ast *expr) {
}
lbCompoundLitElemTempData data = {};
data.expr = elem;
data.elem_index = cast(i32)matrix_index_to_offset(bt, i);
data.elem_index = cast(i32)matrix_row_major_index_to_offset(bt, i);
array_add(&temp_data, data);
}
}
Expand Down
7 changes: 4 additions & 3 deletions src/types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1408,12 +1408,13 @@ i64 matrix_indices_to_offset(Type *t, i64 row_index, i64 column_index) {
i64 stride_elems = matrix_type_stride_in_elems(t);
return stride_elems*column_index + row_index;
}
i64 matrix_index_to_offset(Type *t, i64 index) {

i64 matrix_row_major_index_to_offset(Type *t, i64 index) {
t = base_type(t);
GB_ASSERT(t->kind == Type_Matrix);

i64 row_index = index%t->Matrix.row_count;
i64 column_index = index/t->Matrix.row_count;
i64 column_index = index%t->Matrix.column_count;
i64 row_index = index/t->Matrix.column_count;
return matrix_indices_to_offset(t, row_index, column_index);
}

Expand Down

0 comments on commit c202305

Please sign in to comment.