Skip to content

Commit

Permalink
Do not unroll loops with a nesting higher than 3
Browse files Browse the repository at this point in the history
  • Loading branch information
karoliineh committed Oct 4, 2024
1 parent e625b56 commit 761282b
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions src/util/loopUnrolling.ml
Original file line number Diff line number Diff line change
Expand Up @@ -402,16 +402,19 @@ let copy_and_patch_labels break_target current_continue_target stmts =
let patchLabelsVisitor = new patchLabelsGotosVisitor(StatementHashTable.find_opt gotos) in
List.map (visitCilStmt patchLabelsVisitor) stmts'

class loopUnrollingVisitor(func, totalLoops) = object
class loopUnrollingVisitor (func, totalLoops) = object
(* Labels are simply handled by giving them a fresh name. Jumps coming from outside will still always go to the original label! *)
inherit nopCilVisitor

method! vstmt s =
let duplicate_and_rem_labels s =
match s.skind with
| Loop (b,loc, loc2, break , continue) ->
let factor = loop_unrolling_factor s func totalLoops in
if(factor > 0) then (
val mutable nests = 0

method! vstmt stmt =
let duplicate_and_rem_labels stmt =
match stmt.skind with
| Loop (b, loc, loc2, break, continue) ->
nests <- nests - 1; Logs.debug "nests: %i" nests;
let factor = loop_unrolling_factor stmt func totalLoops in
if factor > 0 then (
Logs.info "unrolling loop at %a with factor %d" CilType.Location.pretty loc factor;
annotateArrays b;
(* top-level breaks should immediately go to the end of the loop, and not just break out of the current iteration *)
Expand All @@ -423,11 +426,14 @@ class loopUnrollingVisitor(func, totalLoops) = object
one_copy_stmts @ [current_continue_target]
)
in
mkStmt (Block (mkBlock (List.flatten copies @ [s; break_target])))
) else s (*no change*)
| _ -> s
mkStmt (Block (mkBlock (List.flatten copies @ [stmt; break_target])))
) else stmt (*no change*)
| _ -> stmt
in
ChangeDoChildrenPost(s, duplicate_and_rem_labels)
match stmt.skind with
| Loop _ when nests + 1 < 4 -> nests <- nests + 1; ChangeDoChildrenPost(stmt, duplicate_and_rem_labels)
| Loop _ -> SkipChildren
| _ -> ChangeDoChildrenPost(stmt, duplicate_and_rem_labels)
end

let unroll_loops fd totalLoops =
Expand Down

0 comments on commit 761282b

Please sign in to comment.