Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hl prefetch #11300

Merged
merged 2 commits into from
Aug 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 37 additions & 4 deletions src/generators/genhl.ml
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ type access =
| AInstanceProto of texpr * field index
| AInstanceField of texpr * field index
| AArray of reg * (ttype * ttype) * reg
| ACArray of reg * ttype * reg
| AVirtualMethod of texpr * field index
| ADynamic of texpr * string index
| AEnum of tenum * field index
Expand Down Expand Up @@ -310,6 +311,12 @@ let unsigned_op e1 e2 =
in
is_unsigned e1 && is_unsigned e2

let rec get_const e =
match e.eexpr with
| TConst c -> c
| TParenthesis e | TCast (e,_) -> get_const e
| _ -> abort "Should be a constant" e.epos

let set_curpos ctx p =
ctx.m.mcurpos <- p

Expand Down Expand Up @@ -1377,6 +1384,13 @@ and get_access ctx e =
free ctx a;
let t = to_type ctx t in
AArray (a,(t,t),i)
| TInst ({ cl_path = ["hl"],"Abstract" },[TInst({ cl_kind = KExpr (EConst (String("hl_carray",_)),_) },_)]) ->
let a = eval_null_check ctx a in
hold ctx a;
let i = eval_to ctx i HI32 in
free ctx a;
let t = to_type ctx e.etype in
ACArray (a,t,i)
| TAbstract (a,pl) ->
loop (Abstract.get_underlying_type a pl)
| _ ->
Expand Down Expand Up @@ -1894,7 +1908,13 @@ and eval_expr ctx e =
r
| "$asize", [e] ->
let r = alloc_tmp ctx HI32 in
op ctx (OArraySize (r, eval_to ctx e HArray));
(match follow e.etype with
| TInst ({cl_path=["hl"],"Abstract"},[TInst({ cl_kind = KExpr (EConst (String("hl_carray",_)),_) },_)]) ->
let arr = eval_expr ctx e in
op ctx (ONullCheck arr);
op ctx (OArraySize (r, arr))
| _ ->
op ctx (OArraySize (r, eval_to ctx e HArray)));
r
| "$aalloc", [esize] ->
let et = (match follow e.etype with TAbstract ({ a_path = ["hl"],"NativeArray" },[t]) -> to_type ctx t | _ -> invalid()) in
Expand Down Expand Up @@ -2054,6 +2074,15 @@ and eval_expr ctx e =
free ctx rfile;
free ctx min;
r
| "$prefetch", [value; mode] ->
let mode = (match get_const mode with
| TInt m -> Int32.to_int m
| _ -> abort "Constant mode required" e.epos
) in
(match get_access ctx value with
| AInstanceField (f, index) -> op ctx (OPrefetch (eval_expr ctx f, index + 1, mode))
| _ -> op ctx (OPrefetch (eval_expr ctx value, 0, mode)));
alloc_tmp ctx HVoid
| _ ->
abort ("Unknown native call " ^ s) e.epos)
| TEnumIndex v ->
Expand Down Expand Up @@ -2208,7 +2237,7 @@ and eval_expr ctx e =
ignore(make_fun ctx ("","") fid f None None);
end;
op ctx (OStaticClosure (r,fid));
| ANone | ALocal _ | AArray _ | ACaptured _ ->
| ANone | ALocal _ | AArray _ | ACaptured _ | ACArray _ ->
abort "Invalid access" e.epos);
let to_t = to_type ctx e.etype in
(match to_t with
Expand Down Expand Up @@ -2452,7 +2481,7 @@ and eval_expr ctx e =
let r = value() in
op ctx (OSetEnumField (ctx.m.mcaptreg,index,r));
r
| AEnum _ | ANone | AInstanceFun _ | AInstanceProto _ | AStaticFun _ | AVirtualMethod _ ->
| AEnum _ | ANone | AInstanceFun _ | AInstanceProto _ | AStaticFun _ | AVirtualMethod _ | ACArray _ ->
die "" __LOC__)
| OpBoolOr ->
let r = alloc_tmp ctx HBool in
Expand Down Expand Up @@ -2727,6 +2756,10 @@ and eval_expr ctx e =
(match get_access ctx e with
| AArray (a,at,idx) ->
array_read ctx a at idx e.epos
| ACArray (a,t,idx) ->
let tmp = alloc_tmp ctx t in
op ctx (OGetArray (tmp,a,idx));
tmp
| _ ->
die "" __LOC__)
| TMeta (_,e) ->
Expand Down Expand Up @@ -3046,7 +3079,7 @@ and gen_assign_op ctx acc e1 f =
free ctx robj;
op ctx (ODynSet (robj,fid,r));
r
| ANone | ALocal _ | AStaticFun _ | AInstanceFun _ | AInstanceProto _ | AVirtualMethod _ | AEnum _ ->
| ANone | ALocal _ | AStaticFun _ | AInstanceFun _ | AInstanceProto _ | AVirtualMethod _ | AEnum _ | ACArray _ ->
die "" __LOC__

and build_capture_vars ctx f =
Expand Down
9 changes: 9 additions & 0 deletions src/generators/hl2c.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1087,6 +1087,15 @@ let generate_function ctx f =
sexpr "%s = %s + %s" (reg r) (reg r2) (reg off)
| ONop _ ->
()
| OPrefetch (r,fid,mode) ->
let expr = (if fid = 0 then reg r else (match rtype r with
| HObj o | HStruct o ->
let name, t = resolve_field o (fid - 1) in
Printf.sprintf "%s->%s" (reg r) name
| _ ->
Globals.die "" __LOC__
)) in
sexpr "__hl_prefetch_m%d(%s)" mode expr
) f.code;
flush_options (Array.length f.code);
unblock();
Expand Down
3 changes: 3 additions & 0 deletions src/generators/hlcode.ml
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ type opcode =
| ORefData of reg * reg
| ORefOffset of reg * reg * reg
| ONop of string
| OPrefetch of reg * field index * int

type fundecl = {
fpath : string * string;
Expand Down Expand Up @@ -572,6 +573,8 @@ let ostr fstr o =
| ORefData (r,d) -> Printf.sprintf "refdata %d, %d" r d
| ORefOffset (r,r2,off) -> Printf.sprintf "refoffset %d, %d, %d" r r2 off
| ONop s -> if s = "" then "nop" else "nop " ^ s
| OPrefetch (r,f,mode) -> Printf.sprintf "prefetch %d[%d] %d" r f mode

let fundecl_name f = if snd f.fpath = "" then "fun$" ^ (string_of_int f.findex) else (fst f.fpath) ^ "." ^ (snd f.fpath)

let dump pr code =
Expand Down
8 changes: 5 additions & 3 deletions src/generators/hlinterp.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1154,7 +1154,7 @@ let interp ctx f args =
(match get r2, get off with
| VRef (RArray (a,pos),t), VInt i -> set r (VRef (RArray (a,pos + Int32.to_int i),t))
| _ -> Globals.die "" __LOC__)
| ONop _ ->
| ONop _ | OPrefetch _ ->
()
);
loop()
Expand Down Expand Up @@ -2436,7 +2436,7 @@ let check code macros =
| ORethrow r ->
reg r HDyn
| OGetArray (v,a,i) ->
reg a HArray;
(match rtype a with HAbstract ("hl_carray",_) -> () | _ -> reg a HArray);
reg i HI32;
ignore(rtype v);
| OGetUI8 (r,b,p) | OGetUI16(r,b,p) ->
Expand Down Expand Up @@ -2466,7 +2466,7 @@ let check code macros =
ignore(rtype a);
ignore(rtype b);
| OArraySize (r,a) ->
reg a HArray;
(match rtype a with HAbstract ("hl_carray",_) -> () | _ -> reg a HArray);
reg r HI32
| OType (r,_) ->
reg r HType
Expand Down Expand Up @@ -2547,6 +2547,8 @@ let check code macros =
reg off HI32;
| ONop _ ->
();
| OPrefetch (r,f,_) ->
if f = 0 then ignore(rtype r) else ignore(tfield r (f - 1) false)
) f.code
(* TODO : check that all path correctly initialize NULL values and reach a return *)
in
Expand Down
10 changes: 10 additions & 0 deletions src/generators/hlopt.ml
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ let opcode_fx frw op =
write r;
| ONop _ ->
()
| OPrefetch (r,_,_) ->
read r

let opcode_eq a b =
match a, b with
Expand Down Expand Up @@ -432,6 +434,9 @@ let opcode_map read write op =
ORefOffset (write r,r2,off);
| ONop _ ->
op
| OPrefetch (r, fid, mode) ->
let r2 = read r in
OPrefetch (r2, fid, mode)

(* build code graph *)

Expand Down Expand Up @@ -875,6 +880,11 @@ let _optimize (f:fundecl) =
| OGetThis (r,fid) when (match f.regs.(r) with HStruct _ -> true | _ -> false) ->
do_write r;
if is_packed_field 0 fid then state.(r).rnullcheck <- true;
| OGetArray (r,arr,idx) ->
do_read arr;
do_read idx;
do_write r;
(match f.regs.(arr) with HAbstract _ -> state.(r).rnullcheck <- true | _ -> ());
| _ ->
opcode_fx (fun r read ->
if read then do_read r else do_write r
Expand Down
7 changes: 6 additions & 1 deletion std/hl/CArray.hx
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,14 @@ abstract CArray<T>(Abstract<"hl_carray">) {

public var length(get,never) : Int;

inline function get_length() return getLen(cast this);

#if (hl_ver >= version("1.14.0"))
inline function get_length() return untyped $asize(this);
@:arrayAccess inline function get( index : Int ) : T return untyped this[index];
#else
inline function get_length() return getLen(cast this);
@:arrayAccess inline function get( index : Int ) : T return getIndex(cast this, index);
#end

public static function alloc<T>( cl : Class<T>, size : Int ) : CArray<T> {
return cast alloc_carray( (cast cl:BaseType).__type__ , size );
Expand Down
Loading