writetracking.nim 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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, trees, msgs, types, options, lineinfos
  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[0], result, info)
  55. of nkDotExpr, nkBracketExpr, nkCheckedFieldExpr,
  56. nkHiddenAddr, nkObjUpConv, nkObjDownConv:
  57. allRoots(n[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[1], result, info)
  65. else:
  66. # we do significantly better here by using the available escape
  67. # information:
  68. if n[0].typ.isNil: return
  69. var typ = n[0].typ
  70. if typ != nil:
  71. typ = skipTypes(typ, abstractInst)
  72. if typ.kind != tyProc: typ = nil
  73. else: assert(typ.len == typ.n.len)
  74. for i in 1..<n.len:
  75. let it = n[i]
  76. if typ != nil and i < typ.len:
  77. assert(typ.n[i].kind == nkSym)
  78. let paramType = typ.n[i]
  79. if paramType.typ.isCompileTimeOnly: continue
  80. if sfEscapes in paramType.sym.flags or paramType.typ.kind in {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[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, nkTupleConstr, nkObjConstr, nkClosure,
  113. nkIfExpr, nkIfStmt, nkWhenStmt, nkCaseStmt, nkTryStmt, nkHiddenTryStmt:
  114. result = newLit
  115. for i in ord(n.kind == nkObjConstr)..<n.len:
  116. let x = returnsNewExpr(n[i])
  117. case x
  118. of newNone: return newNone
  119. of newLit: discard
  120. of newCall: result = newCall
  121. of nkCallKinds:
  122. if n[0].typ != nil and tfReturnsNew in n[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. w.assignments.setLen(w.assignments.len+1)
  140. addAsgn(w.assignments[^1], dest, src, destInfo)
  141. proc depsArgs(w: var W; n: PNode) =
  142. if n[0].typ.isNil: return
  143. var typ = skipTypes(n[0].typ, abstractInst)
  144. if typ.kind != tyProc: return
  145. # echo n.info, " ", n, " ", w.owner.name.s, " ", typeToString(typ)
  146. assert(typ.len == typ.n.len)
  147. for i in 1..<n.len:
  148. let it = n[i]
  149. if i < typ.len:
  150. assert(typ.n[i].kind == nkSym)
  151. let paramType = typ.n[i]
  152. if paramType.typ.isCompileTimeOnly: continue
  153. var destInfo: set[RootInfo] = {}
  154. if sfWrittenTo in paramType.sym.flags or paramType.typ.kind in {tyVar}:
  155. # p(f(x, y), X, g(h, z))
  156. destInfo.incl markAsWrittenTo
  157. if sfEscapes in paramType.sym.flags:
  158. destInfo.incl markAsEscaping
  159. if destInfo != {}:
  160. deps(w, it, nil, destInfo)
  161. proc deps(w: var W; n: PNode) =
  162. case n.kind
  163. of nkLetSection, nkVarSection:
  164. for child in n:
  165. let last = lastSon(child)
  166. if last.kind == nkEmpty: continue
  167. if child.kind == nkVarTuple and last.kind in {nkPar, nkTupleConstr}:
  168. if child.len-2 != last.len: return
  169. for i in 0..<child.len-2:
  170. deps(w, child[i], last[i], {})
  171. else:
  172. for i in 0..<child.len-2:
  173. deps(w, child[i], last, {})
  174. of nkAsgn, nkFastAsgn:
  175. deps(w, n[0], n[1], {})
  176. else:
  177. for i in 0..<n.safeLen:
  178. deps(w, n[i])
  179. if n.kind in nkCallKinds:
  180. if getMagic(n) in {mNew, mNewFinalize, mNewSeq}:
  181. # may not look like an assignment, but it is:
  182. deps(w, n[1], newNodeIT(nkObjConstr, n.info, n[1].typ), {})
  183. else:
  184. depsArgs(w, n)
  185. proc possibleAliases(w: var W; result: var seq[ptr TSym]) =
  186. # this is an expensive fixpoint iteration. We could speed up this analysis
  187. # by a smarter data-structure but we wait until profiling shows us it's
  188. # expensive. Usually 'w.assignments' is small enough.
  189. var alreadySeen = initIntSet()
  190. template addNoDup(x) =
  191. if not alreadySeen.containsOrIncl(x.id): result.add x
  192. for x in result: alreadySeen.incl x.id
  193. var todo = 0
  194. while todo < result.len:
  195. let x = result[todo]
  196. inc todo
  197. for i in 0..<w.assignments.len:
  198. let a = addr(w.assignments[i])
  199. #if a.srcHasSym(x):
  200. # # y = f(..., x, ...)
  201. # for i in 0..<a.destNoTc: addNoDup a.dest[i]
  202. if a.destNoTc > 0 and a.dest[0] == x and rootIsSym in a.destInfo:
  203. # x = f(..., y, ....)
  204. for i in 0..<a.srcNoTc: addNoDup a.src[i]
  205. proc markWriteOrEscape(w: var W; conf: ConfigRef) =
  206. ## Both 'writes' and 'escapes' effects ultimately only care
  207. ## about *parameters*.
  208. ## However, due to aliasing, even locals that might not look as parameters
  209. ## have to count as parameters if they can alias a parameter:
  210. ##
  211. ##..code-block:: nim
  212. ## proc modifies(n: Node) {.writes: [n].} =
  213. ## let x = n
  214. ## x.data = "abc"
  215. ##
  216. ## We call a symbol *parameter-like* if it is a parameter or can alias a
  217. ## parameter.
  218. ## Let ``p``, ``q`` be *parameter-like* and ``x``, ``y`` be general
  219. ## expressions.
  220. ##
  221. ## A write then looks like ``p[] = x``.
  222. ## An escape looks like ``p[] = q`` or more generally
  223. ## like ``p[] = f(q)`` where ``f`` can forward ``q``.
  224. for i in 0..<w.assignments.len:
  225. let a = addr(w.assignments[i])
  226. if a.destInfo != {}:
  227. possibleAliases(w, a.dest)
  228. if {rootIsHeapAccess, markAsWrittenTo} * a.destInfo != {}:
  229. for p in a.dest:
  230. if p.kind == skParam and p.owner == w.owner:
  231. incl(p.flags, sfWrittenTo)
  232. if w.owner.kind == skFunc and p.typ.kind notin {tyVar}:
  233. localError(conf, a.info, "write access to non-var parameter: " & p.name.s)
  234. if {rootIsResultOrParam, rootIsHeapAccess, markAsEscaping}*a.destInfo != {}:
  235. var destIsParam = false
  236. for p in a.dest:
  237. if p.kind in {skResult, skParam} and p.owner == w.owner:
  238. destIsParam = true
  239. break
  240. if destIsParam:
  241. possibleAliases(w, a.src)
  242. for p in a.src:
  243. if p.kind == skParam and p.owner == w.owner:
  244. incl(p.flags, sfEscapes)
  245. proc trackWrites*(owner: PSym; body: PNode; conf: ConfigRef) =
  246. var w: W
  247. w.owner = owner
  248. w.assignments = @[]
  249. # Phase 1: Collect and preprocess any assignments in the proc body:
  250. deps(w, body)
  251. # Phase 2: Compute the 'writes' and 'escapes' effects:
  252. markWriteOrEscape(w, conf)
  253. if w.returnsNew != asgnOther and not isEmptyType(owner.typ[0]) and
  254. containsGarbageCollectedRef(owner.typ[0]):
  255. incl(owner.typ.flags, tfReturnsNew)