writetracking.nim 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. #
  2. #
  3. # The Nim Compiler
  4. # (c) Copyright 2015 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. ## This module implements the write tracking analysis. Read my block post for
  10. ## a basic description of the algorithm and ideas.
  11. ## The algorithm operates in 2 phases:
  12. ##
  13. ## * Collecting information about assignments (and pass-by-var calls).
  14. ## * Computing an aliasing relation based on the assignments. This relation
  15. ## is then used to compute the 'writes' and 'escapes' effects.
  16. import intsets, idents, ast, astalgo, trees, renderer, msgs, types
  17. const
  18. debug = false
  19. type
  20. AssignToResult = enum
  21. asgnNil, # 'nil' is fine
  22. asgnNew, # 'new(result)'
  23. asgnOther # result = fooBar # not a 'new' --> 'result' might not 'new'
  24. NewLocation = enum
  25. newNone,
  26. newLit,
  27. newCall
  28. RootInfo = enum
  29. rootIsResultOrParam,
  30. rootIsHeapAccess,
  31. rootIsSym,
  32. markAsWrittenTo,
  33. markAsEscaping
  34. Assignment = object # \
  35. # Note that the transitive closures MUST be computed in
  36. # phase 2 of the algorithm.
  37. dest, src: seq[ptr TSym] # we use 'ptr' here to save RC ops and GC cycles
  38. destNoTc, srcNoTc: int # length of 'dest', 'src' without the
  39. # transitive closure
  40. destInfo: set[RootInfo]
  41. info: TLineInfo
  42. W = object # WriteTrackContext
  43. owner: PSym
  44. returnsNew: AssignToResult # assignments to 'result'
  45. assignments: seq[Assignment] # list of all assignments in this proc
  46. proc allRoots(n: PNode; result: var seq[ptr TSym]; info: var set[RootInfo]) =
  47. case n.kind
  48. of nkSym:
  49. if n.sym.kind in {skParam, skVar, skTemp, skLet, skResult, skForVar}:
  50. if n.sym.kind in {skResult, skParam}: incl(info, rootIsResultOrParam)
  51. result.add(cast[ptr TSym](n.sym))
  52. of nkHiddenDeref, nkDerefExpr:
  53. incl(info, rootIsHeapAccess)
  54. allRoots(n.sons[0], result, info)
  55. of nkDotExpr, nkBracketExpr, nkCheckedFieldExpr,
  56. nkHiddenAddr, nkObjUpConv, nkObjDownConv:
  57. allRoots(n.sons[0], result, info)
  58. of nkExprEqExpr, nkExprColonExpr, nkHiddenStdConv, nkHiddenSubConv, nkConv,
  59. nkStmtList, nkStmtListExpr, nkBlockStmt, nkBlockExpr, nkOfBranch,
  60. nkElifBranch, nkElse, nkExceptBranch, nkFinally, nkCast:
  61. allRoots(n.lastSon, result, info)
  62. of nkCallKinds:
  63. if getMagic(n) == mSlice:
  64. allRoots(n.sons[1], result, info)
  65. else:
  66. # we do significantly better here by using the available escape
  67. # information:
  68. if n.sons[0].typ.isNil: return
  69. var typ = n.sons[0].typ
  70. if typ != nil:
  71. typ = skipTypes(typ, abstractInst)
  72. if typ.kind != tyProc: typ = nil
  73. else: assert(sonsLen(typ) == sonsLen(typ.n))
  74. for i in 1 ..< n.len:
  75. let it = n.sons[i]
  76. if typ != nil and i < sonsLen(typ):
  77. assert(typ.n.sons[i].kind == nkSym)
  78. let paramType = typ.n.sons[i]
  79. if paramType.typ.isCompileTimeOnly: continue
  80. if sfEscapes in paramType.sym.flags or paramType.typ.kind == tyVar:
  81. allRoots(it, result, info)
  82. else:
  83. allRoots(it, result, info)
  84. else:
  85. for i in 0..<n.safeLen:
  86. allRoots(n.sons[i], result, info)
  87. proc addAsgn(a: var Assignment; dest, src: PNode; destInfo: set[RootInfo]) =
  88. a.dest = @[]
  89. a.src = @[]
  90. a.destInfo = destInfo
  91. allRoots(dest, a.dest, a.destInfo)
  92. if dest.kind == nkSym: incl(a.destInfo, rootIsSym)
  93. if src != nil:
  94. var dummy: set[RootInfo]
  95. allRoots(src, a.src, dummy)
  96. a.destNoTc = a.dest.len
  97. a.srcNoTc = a.src.len
  98. a.info = dest.info
  99. #echo "ADDING ", dest.info, " ", a.destInfo
  100. proc srcHasSym(a: Assignment; x: ptr TSym): bool =
  101. for i in 0 ..< a.srcNoTc:
  102. if a.src[i] == x: return true
  103. proc returnsNewExpr*(n: PNode): NewLocation =
  104. case n.kind
  105. of nkCharLit..nkInt64Lit, nkStrLit..nkTripleStrLit,
  106. nkFloatLit..nkFloat64Lit, nkNilLit:
  107. result = newLit
  108. of nkExprEqExpr, nkExprColonExpr, nkHiddenStdConv, nkHiddenSubConv,
  109. nkStmtList, nkStmtListExpr, nkBlockStmt, nkBlockExpr, nkOfBranch,
  110. nkElifBranch, nkElse, nkExceptBranch, nkFinally, nkCast:
  111. result = returnsNewExpr(n.lastSon)
  112. of nkCurly, nkBracket, nkPar, nkObjConstr, nkClosure,
  113. nkIfExpr, nkIfStmt, nkWhenStmt, nkCaseStmt, nkTryStmt:
  114. result = newLit
  115. for i in ord(n.kind == nkObjConstr) .. <n.len:
  116. let x = returnsNewExpr(n.sons[i])
  117. case x
  118. of newNone: return newNone
  119. of newLit: discard
  120. of newCall: result = newCall
  121. of nkCallKinds:
  122. if n.sons[0].typ != nil and tfReturnsNew in n.sons[0].typ.flags:
  123. result = newCall
  124. else:
  125. result = newNone
  126. proc deps(w: var W; dest, src: PNode; destInfo: set[RootInfo]) =
  127. # let x = (localA, localB)
  128. # compute 'returnsNew' property:
  129. let retNew = if src.isNil: newNone else: returnsNewExpr(src)
  130. if dest.kind == nkSym and dest.sym.kind == skResult:
  131. if retNew != newNone:
  132. if w.returnsNew != asgnOther: w.returnsNew = asgnNew
  133. else:
  134. w.returnsNew = asgnOther
  135. # mark the dependency, but
  136. # rule out obviously innocent assignments like 'somebool = true'
  137. if dest.kind == nkSym and retNew == newLit: discard
  138. else:
  139. let L = w.assignments.len
  140. w.assignments.setLen(L+1)
  141. addAsgn(w.assignments[L], dest, src, destInfo)
  142. proc depsArgs(w: var W; n: PNode) =
  143. if n.sons[0].typ.isNil: return
  144. var typ = skipTypes(n.sons[0].typ, abstractInst)
  145. if typ.kind != tyProc: return
  146. # echo n.info, " ", n, " ", w.owner.name.s, " ", typeToString(typ)
  147. assert(sonsLen(typ) == sonsLen(typ.n))
  148. for i in 1 ..< n.len:
  149. let it = n.sons[i]
  150. if i < sonsLen(typ):
  151. assert(typ.n.sons[i].kind == nkSym)
  152. let paramType = typ.n.sons[i]
  153. if paramType.typ.isCompileTimeOnly: continue
  154. var destInfo: set[RootInfo] = {}
  155. if sfWrittenTo in paramType.sym.flags or paramType.typ.kind == tyVar:
  156. # p(f(x, y), X, g(h, z))
  157. destInfo.incl markAsWrittenTo
  158. if sfEscapes in paramType.sym.flags:
  159. destInfo.incl markAsEscaping
  160. if destInfo != {}:
  161. deps(w, it, nil, destInfo)
  162. proc deps(w: var W; n: PNode) =
  163. case n.kind
  164. of nkLetSection, nkVarSection:
  165. for child in n:
  166. let last = lastSon(child)
  167. if last.kind == nkEmpty: continue
  168. if child.kind == nkVarTuple and last.kind == nkPar:
  169. internalAssert child.len-2 == last.len
  170. for i in 0 .. child.len-3:
  171. deps(w, child.sons[i], last.sons[i], {})
  172. else:
  173. for i in 0 .. child.len-3:
  174. deps(w, child.sons[i], last, {})
  175. of nkAsgn, nkFastAsgn:
  176. deps(w, n.sons[0], n.sons[1], {})
  177. else:
  178. for i in 0 ..< n.safeLen:
  179. deps(w, n.sons[i])
  180. if n.kind in nkCallKinds:
  181. if getMagic(n) in {mNew, mNewFinalize, mNewSeq}:
  182. # may not look like an assignment, but it is:
  183. deps(w, n.sons[1], newNodeIT(nkObjConstr, n.info, n.sons[1].typ), {})
  184. else:
  185. depsArgs(w, n)
  186. proc possibleAliases(w: var W; result: var seq[ptr TSym]) =
  187. # this is an expensive fixpoint iteration. We could speed up this analysis
  188. # by a smarter data-structure but we wait until profiling shows us it's
  189. # expensive. Usually 'w.assignments' is small enough.
  190. var alreadySeen = initIntSet()
  191. template addNoDup(x) =
  192. if not alreadySeen.containsOrIncl(x.id): result.add x
  193. for x in result: alreadySeen.incl x.id
  194. var todo = 0
  195. while todo < result.len:
  196. let x = result[todo]
  197. inc todo
  198. for i in 0..<len(w.assignments):
  199. let a = addr(w.assignments[i])
  200. #if a.srcHasSym(x):
  201. # # y = f(..., x, ...)
  202. # for i in 0 ..< a.destNoTc: addNoDup a.dest[i]
  203. if a.destNoTc > 0 and a.dest[0] == x and rootIsSym in a.destInfo:
  204. # x = f(..., y, ....)
  205. for i in 0 ..< a.srcNoTc: addNoDup a.src[i]
  206. proc markWriteOrEscape(w: var W) =
  207. ## Both 'writes' and 'escapes' effects ultimately only care
  208. ## about *parameters*.
  209. ## However, due to aliasing, even locals that might not look as parameters
  210. ## have to count as parameters if they can alias a parameter:
  211. ##
  212. ## .. code-block:: nim
  213. ## proc modifies(n: Node) {.writes: [n].} =
  214. ## let x = n
  215. ## x.data = "abc"
  216. ##
  217. ## We call a symbol *parameter-like* if it is a parameter or can alias a
  218. ## parameter.
  219. ## Let ``p``, ``q`` be *parameter-like* and ``x``, ``y`` be general
  220. ## expressions.
  221. ##
  222. ## A write then looks like ``p[] = x``.
  223. ## An escape looks like ``p[] = q`` or more generally
  224. ## like ``p[] = f(q)`` where ``f`` can forward ``q``.
  225. for i in 0..<len(w.assignments):
  226. let a = addr(w.assignments[i])
  227. if a.destInfo != {}:
  228. possibleAliases(w, a.dest)
  229. if {rootIsHeapAccess, markAsWrittenTo} * a.destInfo != {}:
  230. for p in a.dest:
  231. if p.kind == skParam and p.owner == w.owner:
  232. incl(p.flags, sfWrittenTo)
  233. if w.owner.kind == skFunc and p.typ.kind != tyVar:
  234. localError(a.info, "write access to non-var parameter: " & p.name.s)
  235. if {rootIsResultOrParam, rootIsHeapAccess, markAsEscaping}*a.destInfo != {}:
  236. var destIsParam = false
  237. for p in a.dest:
  238. if p.kind in {skResult, skParam} and p.owner == w.owner:
  239. destIsParam = true
  240. break
  241. if destIsParam:
  242. possibleAliases(w, a.src)
  243. for p in a.src:
  244. if p.kind == skParam and p.owner == w.owner:
  245. incl(p.flags, sfEscapes)
  246. proc trackWrites*(owner: PSym; body: PNode) =
  247. var w: W
  248. w.owner = owner
  249. w.assignments = @[]
  250. # Phase 1: Collect and preprocess any assignments in the proc body:
  251. deps(w, body)
  252. # Phase 2: Compute the 'writes' and 'escapes' effects:
  253. markWriteOrEscape(w)
  254. if w.returnsNew != asgnOther and not isEmptyType(owner.typ.sons[0]) and
  255. containsGarbageCollectedRef(owner.typ.sons[0]):
  256. incl(owner.typ.flags, tfReturnsNew)