2
0
Эх сурвалжийг харах

add `-D loop_unroll_max_cost=int` flag and cancel loop unrolling at 250 by default

Simon Krajewski 10 жил өмнө
parent
commit
fa4e0af33d
2 өөрчлөгдсөн 13 нэмэгдсэн , 1 устгасан
  1. 2 0
      common.ml
  2. 11 1
      optimizer.ml

+ 2 - 0
common.ml

@@ -197,6 +197,7 @@ module Define = struct
 		| JsEs5
 		| JsFlatten
 		| KeepOldOutput
+		| LoopUnrollMaxCost
 		| Macro
 		| MacroTimes
 		| NekoSource
@@ -277,6 +278,7 @@ module Define = struct
 		| JsEs5 -> ("js_es5","Generate JS for ES5-compliant runtimes")
 		| JsFlatten -> ("js_flatten","Generate classes to use fewer object property lookups")
 		| KeepOldOutput -> ("keep_old_output","Keep old source files in the output directory (for C#/Java)")
+		| LoopUnrollMaxCost -> ("loop_unroll_max_cost","Maximum cost (number of expressions * iterations) before loop unrolling is canceled (default 250)")
 		| Macro -> ("macro","Defined when we compile code in the macro context")
 		| MacroTimes -> ("macro_times","Display per-macro timing when used with --times")
 		| NetVer -> ("net_ver", "<version:20-45> Sets the .NET version to be targeted")

+ 11 - 1
optimizer.ml

@@ -669,11 +669,21 @@ let rec optimize_for_loop ctx (i,pi) e1 e2 p =
 		begin try
 			let v = add_local ctx i pt in
 			let e2 = type_expr ctx e2 NoValue in
-			let rec loop e = match e.eexpr with
+			let i = ref 0 in
+			let rec loop e =
+				incr i;
+				match e.eexpr with
 				| TBreak | TContinue -> raise Exit
 				| _ -> Type.iter loop e
 			in
 			loop e2;
+			let cost = (List.length el) * !i in
+			let max_cost = try
+				int_of_string (Common.defined_value ctx.com Define.LoopUnrollMaxCost)
+			with Not_found ->
+				250
+			in
+			if cost > max_cost then raise Exit;
 			let eloc = mk (TLocal v) v.v_type p in
 			let el = List.map (fun e ->
 				let e_assign = mk (TBinop(OpAssign,eloc,e)) e.etype e.epos in