thavlak_orc_stress.nim 11 KB

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