thavlak.nim 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. discard """
  2. output: '''Welcome to LoopTesterApp, Nim edition
  3. Constructing Simple CFG...
  4. 5000 dummy loops
  5. Constructing CFG...
  6. Performing Loop Recognition
  7. 1 Iteration
  8. Another 3 iterations...
  9. ...
  10. Found 1 loops (including artificial root node) (3)'''
  11. """
  12. # bug #3184
  13. import tables, sets
  14. when not declared(withScratchRegion):
  15. template withScratchRegion(body: untyped) = body
  16. type
  17. BasicBlock = ref object
  18. inEdges: seq[BasicBlock]
  19. outEdges: seq[BasicBlock]
  20. name: int
  21. proc newBasicBlock(name: int): BasicBlock =
  22. result = BasicBlock(
  23. inEdges: newSeq[BasicBlock](),
  24. outEdges: newSeq[BasicBlock](),
  25. name: name
  26. )
  27. proc hash(x: BasicBlock): int {.inline.} =
  28. result = x.name
  29. type
  30. BasicBlockEdge = object
  31. fr: BasicBlock
  32. to: BasicBlock
  33. Cfg = object
  34. basicBlockMap: Table[int, BasicBlock]
  35. edgeList: seq[BasicBlockEdge]
  36. startNode: BasicBlock
  37. proc newCfg(): Cfg =
  38. result = Cfg(
  39. basicBlockMap: initTable[int, BasicBlock](),
  40. edgeList: newSeq[BasicBlockEdge](),
  41. startNode: nil)
  42. proc createNode(self: var Cfg, name: int): BasicBlock =
  43. result = self.basicBlockMap.getOrDefault(name)
  44. if result == nil:
  45. result = newBasicBlock(name)
  46. self.basicBlockMap.add name, result
  47. if self.startNode == nil:
  48. self.startNode = result
  49. proc newBasicBlockEdge(cfg: var Cfg, fromName, toName: int) =
  50. var result = BasicBlockEdge(
  51. fr: cfg.createNode(fromName),
  52. to: cfg.createNode(toName)
  53. )
  54. result.fr.outEdges.add(result.to)
  55. result.to.inEdges.add(result.fr)
  56. cfg.edgeList.add(result)
  57. type
  58. SimpleLoop = ref object
  59. basicBlocks: seq[BasicBlock] # TODO: set here
  60. children: seq[SimpleLoop] # TODO: set here
  61. parent: SimpleLoop
  62. header: BasicBlock
  63. isRoot, isReducible: bool
  64. counter, nestingLevel, depthLevel: int
  65. proc setParent(self: SimpleLoop, parent: SimpleLoop) =
  66. self.parent = parent
  67. self.parent.children.add self
  68. proc setHeader(self: SimpleLoop, bb: BasicBlock) =
  69. self.basicBlocks.add(bb)
  70. self.header = bb
  71. proc setNestingLevel(self: SimpleLoop, level: int) =
  72. self.nestingLevel = level
  73. if level == 0: self.isRoot = true
  74. var loopCounter: int = 0
  75. type
  76. Lsg = object
  77. loops: seq[SimpleLoop]
  78. root: SimpleLoop
  79. proc createNewLoop(self: var Lsg): SimpleLoop =
  80. result = SimpleLoop(
  81. basicBlocks: newSeq[BasicBlock](),
  82. children: newSeq[SimpleLoop](),
  83. isReducible: true)
  84. loopCounter += 1
  85. result.counter = loopCounter
  86. proc addLoop(self: var Lsg, l: SimpleLoop) =
  87. self.loops.add l
  88. proc newLsg(): Lsg =
  89. result = Lsg(loops: newSeq[SimpleLoop](),
  90. root: result.createNewLoop())
  91. result.root.setNestingLevel(0)
  92. result.addLoop(result.root)
  93. type
  94. UnionFindNode = ref object
  95. parent {.cursor.}: UnionFindNode
  96. bb: BasicBlock
  97. l: SimpleLoop
  98. dfsNumber: int
  99. proc initNode(self: UnionFindNode, bb: BasicBlock, dfsNumber: int) =
  100. self.parent = self
  101. self.bb = bb
  102. self.dfsNumber = dfsNumber
  103. proc findSet(self: UnionFindNode): UnionFindNode =
  104. var nodeList = newSeq[UnionFindNode]()
  105. var it {.cursor.} = self
  106. while it != it.parent:
  107. var parent {.cursor.} = it.parent
  108. if parent != parent.parent: nodeList.add it
  109. it = parent
  110. for iter in nodeList: iter.parent = it.parent
  111. result = it
  112. proc union(self: UnionFindNode, unionFindNode: UnionFindNode) =
  113. self.parent = unionFindNode
  114. const
  115. BB_NONHEADER = 1 # a regular BB
  116. BB_REDUCIBLE = 2 # reducible loop
  117. BB_SELF = 3 # single BB loop
  118. BB_IRREDUCIBLE = 4 # irreducible loop
  119. BB_DEAD = 5 # a dead BB
  120. # # Marker for uninitialized nodes.
  121. UNVISITED = -1
  122. # # Safeguard against pathologic algorithm behavior.
  123. MAXNONBACKPREDS = (32 * 1024)
  124. type
  125. HavlakLoopFinder = object
  126. cfg: Cfg
  127. lsg: Lsg
  128. proc newHavlakLoopFinder(cfg: Cfg, lsg: sink Lsg): HavlakLoopFinder =
  129. result = HavlakLoopFinder(cfg: cfg, lsg: lsg)
  130. proc isAncestor(w, v: int, last: seq[int]): bool =
  131. w <= v and v <= last[w]
  132. proc dfs(currentNode: BasicBlock, nodes: var seq[UnionFindNode],
  133. number: var Table[BasicBlock, int],
  134. last: var seq[int], current: int) =
  135. var stack = @[(currentNode, current)]
  136. while stack.len > 0:
  137. let (currentNode, current) = stack.pop()
  138. nodes[current].initNode(currentNode, current)
  139. number[currentNode] = current
  140. for target in currentNode.outEdges:
  141. if number[target] == UNVISITED:
  142. stack.add((target, current+1))
  143. #result = dfs(target, nodes, number, last, result + 1)
  144. last[number[currentNode]] = current
  145. proc findLoops(self: var HavlakLoopFinder): int =
  146. var startNode = self.cfg.startNode
  147. if startNode == nil: return 0
  148. var size = self.cfg.basicBlockMap.len
  149. var nonBackPreds = newSeq[HashSet[int]]()
  150. var backPreds = newSeq[seq[int]]()
  151. var number = initTable[BasicBlock, int]()
  152. var header = newSeq[int](size)
  153. var types = newSeq[int](size)
  154. var last = newSeq[int](size)
  155. var nodes = newSeq[UnionFindNode]()
  156. for i in 1..size:
  157. nonBackPreds.add initHashSet[int](1)
  158. backPreds.add newSeq[int]()
  159. nodes.add(UnionFindNode())
  160. # Step a:
  161. # - initialize all nodes as unvisited.
  162. # - depth-first traversal and numbering.
  163. # - unreached BB's are marked as dead.
  164. #
  165. for v in self.cfg.basicBlockMap.values: number[v] = UNVISITED
  166. dfs(startNode, nodes, number, last, 0)
  167. # Step b:
  168. # - iterate over all nodes.
  169. #
  170. # A backedge comes from a descendant in the DFS tree, and non-backedges
  171. # from non-descendants (following Tarjan).
  172. #
  173. # - check incoming edges 'v' and add them to either
  174. # - the list of backedges (backPreds) or
  175. # - the list of non-backedges (nonBackPreds)
  176. #
  177. for w in 0 ..< size:
  178. header[w] = 0
  179. types[w] = BB_NONHEADER
  180. var nodeW = nodes[w].bb
  181. if nodeW != nil:
  182. for nodeV in nodeW.inEdges:
  183. var v = number[nodeV]
  184. if v != UNVISITED:
  185. if isAncestor(w, v, last):
  186. backPreds[w].add v
  187. else:
  188. nonBackPreds[w].incl v
  189. else:
  190. types[w] = BB_DEAD
  191. # Start node is root of all other loops.
  192. header[0] = 0
  193. # Step c:
  194. #
  195. # The outer loop, unchanged from Tarjan. It does nothing except
  196. # for those nodes which are the destinations of backedges.
  197. # For a header node w, we chase backward from the sources of the
  198. # backedges adding nodes to the set P, representing the body of
  199. # the loop headed by w.
  200. #
  201. # By running through the nodes in reverse of the DFST preorder,
  202. # we ensure that inner loop headers will be processed before the
  203. # headers for surrounding loops.
  204. for w in countdown(size - 1, 0):
  205. # this is 'P' in Havlak's paper
  206. var nodePool = newSeq[UnionFindNode]()
  207. var nodeW = nodes[w].bb
  208. if nodeW != nil: # dead BB
  209. # Step d:
  210. for v in backPreds[w]:
  211. if v != w:
  212. nodePool.add nodes[v].findSet
  213. else:
  214. types[w] = BB_SELF
  215. # Copy nodePool to workList.
  216. #
  217. var workList = newSeq[UnionFindNode]()
  218. for x in nodePool: workList.add x
  219. if nodePool.len != 0: types[w] = BB_REDUCIBLE
  220. # work the list...
  221. #
  222. while workList.len > 0:
  223. let x = workList[0]
  224. workList.del(0)
  225. # Step e:
  226. #
  227. # Step e represents the main difference from Tarjan's method.
  228. # Chasing upwards from the sources of a node w's backedges. If
  229. # there is a node y' that is not a descendant of w, w is marked
  230. # the header of an irreducible loop, there is another entry
  231. # into this loop that avoids w.
  232. #
  233. # The algorithm has degenerated. Break and
  234. # return in this case.
  235. #
  236. var nonBackSize = nonBackPreds[x.dfsNumber].len
  237. if nonBackSize > MAXNONBACKPREDS: return 0
  238. for iter in nonBackPreds[x.dfsNumber]:
  239. var y = nodes[iter]
  240. var ydash = y.findSet
  241. if not isAncestor(w, ydash.dfsNumber, last):
  242. types[w] = BB_IRREDUCIBLE
  243. nonBackPreds[w].incl ydash.dfsNumber
  244. else:
  245. if ydash.dfsNumber != w and not nodePool.contains(ydash):
  246. workList.add ydash
  247. nodePool.add ydash
  248. # Collapse/Unionize nodes in a SCC to a single node
  249. # For every SCC found, create a loop descriptor and link it in.
  250. #
  251. if nodePool.len > 0 or types[w] == BB_SELF:
  252. var l = self.lsg.createNewLoop
  253. l.setHeader(nodeW)
  254. l.isReducible = types[w] != BB_IRREDUCIBLE
  255. # At this point, one can set attributes to the loop, such as:
  256. #
  257. # the bottom node:
  258. # iter = backPreds(w).begin();
  259. # loop bottom is: nodes(iter).node;
  260. #
  261. # the number of backedges:
  262. # backPreds(w).size()
  263. #
  264. # whether this loop is reducible:
  265. # types(w) != BB_IRREDUCIBLE
  266. #
  267. nodes[w].l = l
  268. for node in nodePool:
  269. # Add nodes to loop descriptor.
  270. header[node.dfsNumber] = w
  271. node.union(nodes[w])
  272. # Nested loops are not added, but linked together.
  273. var nodeL = node.l
  274. if nodeL != nil:
  275. nodeL.setParent(l)
  276. else:
  277. l.basicBlocks.add node.bb
  278. self.lsg.addLoop(l)
  279. result = self.lsg.loops.len
  280. type
  281. LoopTesterApp = object
  282. cfg: Cfg
  283. lsg: Lsg
  284. proc newLoopTesterApp(): LoopTesterApp =
  285. result.cfg = newCfg()
  286. result.lsg = newLsg()
  287. proc buildDiamond(self: var LoopTesterApp, start: int): int =
  288. newBasicBlockEdge(self.cfg, start, start + 1)
  289. newBasicBlockEdge(self.cfg, start, start + 2)
  290. newBasicBlockEdge(self.cfg, start + 1, start + 3)
  291. newBasicBlockEdge(self.cfg, start + 2, start + 3)
  292. result = start + 3
  293. proc buildConnect(self: var LoopTesterApp, start1, end1: int) =
  294. newBasicBlockEdge(self.cfg, start1, end1)
  295. proc buildStraight(self: var LoopTesterApp, start, n: int): int =
  296. for i in 0..n-1:
  297. self.buildConnect(start + i, start + i + 1)
  298. result = start + n
  299. proc buildBaseLoop(self: var LoopTesterApp, from1: int): int =
  300. let header = self.buildStraight(from1, 1)
  301. let diamond1 = self.buildDiamond(header)
  302. let d11 = self.buildStraight(diamond1, 1)
  303. let diamond2 = self.buildDiamond(d11)
  304. let footer = self.buildStraight(diamond2, 1)
  305. self.buildConnect(diamond2, d11)
  306. self.buildConnect(diamond1, header)
  307. self.buildConnect(footer, from1)
  308. result = self.buildStraight(footer, 1)
  309. proc run(self: var LoopTesterApp) =
  310. echo "Welcome to LoopTesterApp, Nim edition"
  311. echo "Constructing Simple CFG..."
  312. discard self.cfg.createNode(0)
  313. discard self.buildBaseLoop(0)
  314. discard self.cfg.createNode(1)
  315. self.buildConnect(0, 2)
  316. echo "5000 dummy loops"
  317. for i in 1..5000:
  318. withScratchRegion:
  319. var h = newHavlakLoopFinder(self.cfg, newLsg())
  320. discard h.findLoops
  321. echo "Constructing CFG..."
  322. var n = 2
  323. when true: # not defined(gcOrc):
  324. # currently cycle detection is so slow that we disable this part
  325. for parlooptrees in 1..10:
  326. discard self.cfg.createNode(n + 1)
  327. self.buildConnect(2, n + 1)
  328. n += 1
  329. for i in 1..100:
  330. var top = n
  331. n = self.buildStraight(n, 1)
  332. for j in 1..25: n = self.buildBaseLoop(n)
  333. var bottom = self.buildStraight(n, 1)
  334. self.buildConnect n, top
  335. n = bottom
  336. self.buildConnect(n, 1)
  337. echo "Performing Loop Recognition\n1 Iteration"
  338. var h = newHavlakLoopFinder(self.cfg, newLsg())
  339. var loops = h.findLoops
  340. echo "Another 3 iterations..."
  341. var sum = 0
  342. for i in 1..3:
  343. withScratchRegion:
  344. write stdout, "."
  345. flushFile(stdout)
  346. var hlf = newHavlakLoopFinder(self.cfg, newLsg())
  347. sum += hlf.findLoops
  348. #echo getOccupiedMem()
  349. echo "\nFound ", loops, " loops (including artificial root node) (", sum, ")"
  350. when false:
  351. echo("Total memory available: " & formatSize(getTotalMem()) & " bytes")
  352. echo("Free memory: " & formatSize(getFreeMem()) & " bytes")
  353. proc main =
  354. var l = newLoopTesterApp()
  355. l.run
  356. let mem = getOccupiedMem()
  357. main()
  358. when defined(gcOrc):
  359. GC_fullCollect()
  360. doAssert getOccupiedMem() == mem