Skip to content

Commit

Permalink
Memory accesses & direct array accesses make no assumptions on alignment
Browse files Browse the repository at this point in the history
  • Loading branch information
vbgl committed Mar 20, 2024
1 parent 6de2499 commit 0471e14
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 11 deletions.
2 changes: 1 addition & 1 deletion compiler/safety/fail/bad_align.jazz
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ fn main(reg u64 pt) -> reg u64 {
reg u64 tmp;

tmp = (u64)[pt + 0];
tmp = (u64)[pt + 1];
tmp = (u64)[#aligned pt + 1];

return tmp;
}
15 changes: 9 additions & 6 deletions compiler/src/pretyping.ml
Original file line number Diff line number Diff line change
Expand Up @@ -934,9 +934,12 @@ let ensure_int loc i ty =
| _ -> rs_tyerror ~loc (TypeMismatch (ty, P.tint))

(* -------------------------------------------------------------------- *)
let tt_al = function
| Some `Unaligned -> Memory_model.Unaligned
| Some `Aligned | None -> Memory_model.Aligned
let tt_al aa =
let open Memory_model in
function
| None -> (match aa with Warray_.AAdirect -> Unaligned | AAscale -> Aligned)
| Some `Unaligned -> Unaligned
| Some `Aligned -> Aligned

(* -------------------------------------------------------------------- *)
let rec tt_expr pd ?(mode=`AllVar) (env : 'asm Env.env) pe =
Expand All @@ -961,7 +964,7 @@ let rec tt_expr pd ?(mode=`AllVar) (env : 'asm Env.env) pe =
| S.PEGet (al, aa, ws, ({ L.pl_loc = xlc } as x), pi, olen) ->
let x, ty = tt_var_global mode env x in
let ty, _ = tt_as_array (xlc, ty) in
let al = tt_al al in
let al = tt_al aa al in
let ws = Option.map_default tt_ws (P.ws_of_ty ty) ws in
let ty = P.tu ws in
let i,ity = tt_expr ~mode pd env pi in
Expand Down Expand Up @@ -1067,7 +1070,7 @@ and tt_mem_access pd ?(mode=`AllVar) (env : 'asm Env.env)
| `Add -> e
| `Sub -> Papp1(E.Oneg (E.Op_w pd), e) in
let ct = ct |> Option.map_default tt_ws pd in
let al = tt_al al in
let al = tt_al AAdirect al in
(ct,L.mk_loc xlc x,e, al)

(* -------------------------------------------------------------------- *)
Expand Down Expand Up @@ -1139,7 +1142,7 @@ let tt_lvalue pd (env : 'asm Env.env) { L.pl_desc = pl; L.pl_loc = loc; } =
| S.PLArray (al, aa, ws, ({ pl_loc = xlc } as x), pi, olen) ->
let x = tt_var `NoParam env x in
reject_constant_pointers xlc x ;
let al = tt_al al in
let al = tt_al aa al in
let ty,_ = tt_as_array (xlc, x.P.v_ty) in
let ws = Option.map_default tt_ws (P.ws_of_ty ty) ws in
let ty = P.tu ws in
Expand Down
3 changes: 2 additions & 1 deletion compiler/src/printCommon.ml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ let pp_wsize fmt sz = fprintf fmt "%a" pp_string0 (string_of_wsize sz)

let pp_aligned fmt =
function
| Memory_model.Aligned -> ()
| Memory_model.Aligned ->
Format.fprintf fmt "#aligned "
| Unaligned ->
Format.fprintf fmt "#unaligned "

Expand Down
2 changes: 1 addition & 1 deletion compiler/tests/success/x86-64/unaligned.jazz
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fn sopndest() -> reg u64 {
stack u128[2] a;
reg u128 x;
x = #set0_128();
a.[#unaligned 8] = x;
a.[#unaligned u128 8] = x;
reg u64 r;
r = a.[u64 8];
return r;
Expand Down
4 changes: 2 additions & 2 deletions compiler/tests/success/x86-64/vector.jazz
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ export
fn test_vmovdqa(reg u64 p) {
reg u128 r;

r = #VMOVDQA_128((u128)[p + 16 * 0]);
(u128)[p + 16 * 1] = #VMOVDQA_128(r);
r = #VMOVDQA_128((u128)[#aligned p + 16 * 0]);
(u128)[#aligned p + 16 * 1] = #VMOVDQA_128(r);
}

export
Expand Down

0 comments on commit 0471e14

Please sign in to comment.