spawn.nim 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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 threadpool's ``spawn``.
  10. import ast, types, idents, magicsys, msgs, options, modulegraphs,
  11. lowerings, liftdestructors, renderer
  12. from trees import getMagic, getRoot
  13. proc callProc(a: PNode): PNode =
  14. result = newNodeI(nkCall, a.info)
  15. result.add a
  16. result.typ = a.typ[0]
  17. # we have 4 cases to consider:
  18. # - a void proc --> nothing to do
  19. # - a proc returning GC'ed memory --> requires a flowVar
  20. # - a proc returning non GC'ed memory --> pass as hidden 'var' parameter
  21. # - not in a parallel environment --> requires a flowVar for memory safety
  22. type
  23. TSpawnResult* = enum
  24. srVoid, srFlowVar, srByVar
  25. TFlowVarKind = enum
  26. fvInvalid # invalid type T for 'FlowVar[T]'
  27. fvGC # FlowVar of a GC'ed type
  28. fvBlob # FlowVar of a blob type
  29. proc spawnResult*(t: PType; inParallel: bool): TSpawnResult =
  30. if t.isEmptyType: srVoid
  31. elif inParallel and not containsGarbageCollectedRef(t): srByVar
  32. else: srFlowVar
  33. proc flowVarKind(c: ConfigRef, t: PType): TFlowVarKind =
  34. if c.selectedGC in {gcArc, gcOrc}: fvBlob
  35. elif t.skipTypes(abstractInst).kind in {tyRef, tyString, tySequence}: fvGC
  36. elif containsGarbageCollectedRef(t): fvInvalid
  37. else: fvBlob
  38. proc typeNeedsNoDeepCopy(t: PType): bool =
  39. var t = t.skipTypes(abstractInst)
  40. # for the tconvexhull example (and others) we're a bit lax here and pretend
  41. # seqs and strings are *by value* only and 'shallow' doesn't exist!
  42. if t.kind == tyString: return true
  43. # note that seq[T] is fine, but 'var seq[T]' is not, so we need to skip 'var'
  44. # for the stricter check and likewise we can skip 'seq' for a less
  45. # strict check:
  46. if t.kind in {tyVar, tyLent, tySequence}: t = t.lastSon
  47. result = not containsGarbageCollectedRef(t)
  48. proc addLocalVar(g: ModuleGraph; varSection, varInit: PNode; idgen: IdGenerator; owner: PSym; typ: PType;
  49. v: PNode; useShallowCopy=false): PSym =
  50. result = newSym(skTemp, getIdent(g.cache, genPrefix), nextId idgen, owner, varSection.info,
  51. owner.options)
  52. result.typ = typ
  53. incl(result.flags, sfFromGeneric)
  54. var vpart = newNodeI(nkIdentDefs, varSection.info, 3)
  55. vpart[0] = newSymNode(result)
  56. vpart[1] = newNodeI(nkEmpty, varSection.info)
  57. vpart[2] = if varInit.isNil: v else: vpart[1]
  58. varSection.add vpart
  59. if varInit != nil:
  60. if g.config.selectedGC in {gcArc, gcOrc}:
  61. # inject destructors pass will do its own analysis
  62. varInit.add newFastMoveStmt(g, newSymNode(result), v)
  63. else:
  64. if useShallowCopy and typeNeedsNoDeepCopy(typ) or optTinyRtti in g.config.globalOptions:
  65. varInit.add newFastMoveStmt(g, newSymNode(result), v)
  66. else:
  67. let deepCopyCall = newNodeI(nkCall, varInit.info, 3)
  68. deepCopyCall[0] = newSymNode(getSysMagic(g, varSection.info, "deepCopy", mDeepCopy))
  69. deepCopyCall[1] = newSymNode(result)
  70. deepCopyCall[2] = v
  71. varInit.add deepCopyCall
  72. discard """
  73. We generate roughly this:
  74. proc f_wrapper(thread, args) =
  75. barrierEnter(args.barrier) # for parallel statement
  76. var a = args.a # thread transfer; deepCopy or shallowCopy or no copy
  77. # depending on whether we're in a 'parallel' statement
  78. var b = args.b
  79. var fv = args.fv
  80. fv.owner = thread # optional
  81. nimArgsPassingDone() # signal parent that the work is done
  82. #
  83. args.fv.blob = f(a, b, ...)
  84. nimFlowVarSignal(args.fv)
  85. # - or -
  86. f(a, b, ...)
  87. barrierLeave(args.barrier) # for parallel statement
  88. stmtList:
  89. var scratchObj
  90. scratchObj.a = a
  91. scratchObj.b = b
  92. nimSpawn(f_wrapper, addr scratchObj)
  93. scratchObj.fv # optional
  94. """
  95. proc createWrapperProc(g: ModuleGraph; f: PNode; threadParam, argsParam: PSym;
  96. varSection, varInit, call, barrier, fv: PNode;
  97. idgen: IdGenerator;
  98. spawnKind: TSpawnResult, result: PSym) =
  99. var body = newNodeI(nkStmtList, f.info)
  100. var threadLocalBarrier: PSym
  101. if barrier != nil:
  102. var varSection2 = newNodeI(nkVarSection, barrier.info)
  103. threadLocalBarrier = addLocalVar(g, varSection2, nil, idgen, result,
  104. barrier.typ, barrier)
  105. body.add varSection2
  106. body.add callCodegenProc(g, "barrierEnter", threadLocalBarrier.info,
  107. threadLocalBarrier.newSymNode)
  108. var threadLocalProm: PSym
  109. if spawnKind == srByVar:
  110. threadLocalProm = addLocalVar(g, varSection, nil, idgen, result, fv.typ, fv)
  111. elif fv != nil:
  112. internalAssert g.config, fv.typ.kind == tyGenericInst
  113. threadLocalProm = addLocalVar(g, varSection, nil, idgen, result, fv.typ, fv)
  114. body.add varSection
  115. body.add varInit
  116. if fv != nil and spawnKind != srByVar:
  117. # generate:
  118. # fv.owner = threadParam
  119. body.add newAsgnStmt(indirectAccess(threadLocalProm.newSymNode,
  120. "owner", fv.info, g.cache), threadParam.newSymNode)
  121. body.add callCodegenProc(g, "nimArgsPassingDone", threadParam.info,
  122. threadParam.newSymNode)
  123. if spawnKind == srByVar:
  124. body.add newAsgnStmt(genDeref(threadLocalProm.newSymNode), call)
  125. elif fv != nil:
  126. let fk = flowVarKind(g.config, fv.typ[1])
  127. if fk == fvInvalid:
  128. localError(g.config, f.info, "cannot create a flowVar of type: " &
  129. typeToString(fv.typ[1]))
  130. body.add newAsgnStmt(indirectAccess(threadLocalProm.newSymNode,
  131. if fk == fvGC: "data" else: "blob", fv.info, g.cache), call)
  132. if fk == fvGC:
  133. let incRefCall = newNodeI(nkCall, fv.info, 2)
  134. incRefCall[0] = newSymNode(getSysMagic(g, fv.info, "GCref", mGCref))
  135. incRefCall[1] = indirectAccess(threadLocalProm.newSymNode,
  136. "data", fv.info, g.cache)
  137. body.add incRefCall
  138. if barrier == nil:
  139. # by now 'fv' is shared and thus might have beeen overwritten! we need
  140. # to use the thread-local view instead:
  141. body.add callCodegenProc(g, "nimFlowVarSignal", threadLocalProm.info,
  142. threadLocalProm.newSymNode)
  143. else:
  144. body.add call
  145. if barrier != nil:
  146. body.add callCodegenProc(g, "barrierLeave", threadLocalBarrier.info,
  147. threadLocalBarrier.newSymNode)
  148. var params = newNodeI(nkFormalParams, f.info)
  149. params.add newNodeI(nkEmpty, f.info)
  150. params.add threadParam.newSymNode
  151. params.add argsParam.newSymNode
  152. var t = newType(tyProc, nextId idgen, threadParam.owner)
  153. t.rawAddSon nil
  154. t.rawAddSon threadParam.typ
  155. t.rawAddSon argsParam.typ
  156. t.n = newNodeI(nkFormalParams, f.info)
  157. t.n.add newNodeI(nkEffectList, f.info)
  158. t.n.add threadParam.newSymNode
  159. t.n.add argsParam.newSymNode
  160. let emptyNode = newNodeI(nkEmpty, f.info)
  161. result.ast = newProcNode(nkProcDef, f.info, body = body,
  162. params = params, name = newSymNode(result), pattern = emptyNode,
  163. genericParams = emptyNode, pragmas = emptyNode,
  164. exceptions = emptyNode)
  165. result.typ = t
  166. proc createCastExpr(argsParam: PSym; objType: PType; idgen: IdGenerator): PNode =
  167. result = newNodeI(nkCast, argsParam.info)
  168. result.add newNodeI(nkEmpty, argsParam.info)
  169. result.add newSymNode(argsParam)
  170. result.typ = newType(tyPtr, nextId idgen, objType.owner)
  171. result.typ.rawAddSon(objType)
  172. proc setupArgsForConcurrency(g: ModuleGraph; n: PNode; objType: PType;
  173. idgen: IdGenerator; owner: PSym; scratchObj: PSym,
  174. castExpr, call,
  175. varSection, varInit, result: PNode) =
  176. let formals = n[0].typ.n
  177. let tmpName = getIdent(g.cache, genPrefix)
  178. for i in 1..<n.len:
  179. # we pick n's type here, which hopefully is 'tyArray' and not
  180. # 'tyOpenArray':
  181. var argType = n[i].typ.skipTypes(abstractInst)
  182. if i < formals.len:
  183. if formals[i].typ.kind in {tyVar, tyLent}:
  184. localError(g.config, n[i].info, "'spawn'ed function cannot have a 'var' parameter")
  185. if formals[i].typ.kind in {tyTypeDesc, tyStatic}:
  186. continue
  187. #elif containsTyRef(argType):
  188. # localError(n[i].info, "'spawn'ed function cannot refer to 'ref'/closure")
  189. let fieldname = if i < formals.len: formals[i].sym.name else: tmpName
  190. var field = newSym(skField, fieldname, nextId idgen, objType.owner, n.info, g.config.options)
  191. field.typ = argType
  192. objType.addField(field, g.cache, idgen)
  193. result.add newFastAsgnStmt(newDotExpr(scratchObj, field), n[i])
  194. let temp = addLocalVar(g, varSection, varInit, idgen, owner, argType,
  195. indirectAccess(castExpr, field, n.info))
  196. call.add(newSymNode(temp))
  197. proc setupArgsForParallelism(g: ModuleGraph; n: PNode; objType: PType;
  198. idgen: IdGenerator;
  199. owner: PSym; scratchObj: PSym;
  200. castExpr, call,
  201. varSection, varInit, result: PNode) =
  202. let formals = n[0].typ.n
  203. let tmpName = getIdent(g.cache, genPrefix)
  204. # we need to copy the foreign scratch object fields into local variables
  205. # for correctness: These are called 'threadLocal' here.
  206. for i in 1..<n.len:
  207. let n = n[i]
  208. if i < formals.len and formals[i].typ.kind in {tyStatic, tyTypeDesc}:
  209. continue
  210. let argType = skipTypes(if i < formals.len: formals[i].typ else: n.typ,
  211. abstractInst)
  212. #if containsTyRef(argType):
  213. # localError(n.info, "'spawn'ed function cannot refer to 'ref'/closure")
  214. let fieldname = if i < formals.len: formals[i].sym.name else: tmpName
  215. var field = newSym(skField, fieldname, nextId idgen, objType.owner, n.info, g.config.options)
  216. if argType.kind in {tyVarargs, tyOpenArray}:
  217. # important special case: we always create a zero-copy slice:
  218. let slice = newNodeI(nkCall, n.info, 4)
  219. slice.typ = n.typ
  220. slice[0] = newSymNode(createMagic(g, "slice", mSlice))
  221. slice[0].typ = getSysType(g, n.info, tyInt) # fake type
  222. var fieldB = newSym(skField, tmpName, nextId idgen, objType.owner, n.info, g.config.options)
  223. fieldB.typ = getSysType(g, n.info, tyInt)
  224. objType.addField(fieldB, g.cache, idgen)
  225. if getMagic(n) == mSlice:
  226. let a = genAddrOf(n[1], idgen)
  227. field.typ = a.typ
  228. objType.addField(field, g.cache, idgen)
  229. result.add newFastAsgnStmt(newDotExpr(scratchObj, field), a)
  230. var fieldA = newSym(skField, tmpName, nextId idgen, objType.owner, n.info, g.config.options)
  231. fieldA.typ = getSysType(g, n.info, tyInt)
  232. objType.addField(fieldA, g.cache, idgen)
  233. result.add newFastAsgnStmt(newDotExpr(scratchObj, fieldA), n[2])
  234. result.add newFastAsgnStmt(newDotExpr(scratchObj, fieldB), n[3])
  235. let threadLocal = addLocalVar(g, varSection, nil, idgen, owner, fieldA.typ,
  236. indirectAccess(castExpr, fieldA, n.info),
  237. useShallowCopy=true)
  238. slice[2] = threadLocal.newSymNode
  239. else:
  240. let a = genAddrOf(n, idgen)
  241. field.typ = a.typ
  242. objType.addField(field, g.cache, idgen)
  243. result.add newFastAsgnStmt(newDotExpr(scratchObj, field), a)
  244. result.add newFastAsgnStmt(newDotExpr(scratchObj, fieldB), genHigh(g, n))
  245. slice[2] = newIntLit(g, n.info, 0)
  246. # the array itself does not need to go through a thread local variable:
  247. slice[1] = genDeref(indirectAccess(castExpr, field, n.info))
  248. let threadLocal = addLocalVar(g, varSection, nil, idgen, owner, fieldB.typ,
  249. indirectAccess(castExpr, fieldB, n.info),
  250. useShallowCopy=true)
  251. slice[3] = threadLocal.newSymNode
  252. call.add slice
  253. elif (let size = computeSize(g.config, argType); size < 0 or size > 16) and
  254. n.getRoot != nil:
  255. # it is more efficient to pass a pointer instead:
  256. let a = genAddrOf(n, idgen)
  257. field.typ = a.typ
  258. objType.addField(field, g.cache, idgen)
  259. result.add newFastAsgnStmt(newDotExpr(scratchObj, field), a)
  260. let threadLocal = addLocalVar(g, varSection, nil, idgen, owner, field.typ,
  261. indirectAccess(castExpr, field, n.info),
  262. useShallowCopy=true)
  263. call.add(genDeref(threadLocal.newSymNode))
  264. else:
  265. # boring case
  266. field.typ = argType
  267. objType.addField(field, g.cache, idgen)
  268. result.add newFastAsgnStmt(newDotExpr(scratchObj, field), n)
  269. let threadLocal = addLocalVar(g, varSection, varInit,
  270. idgen, owner, field.typ,
  271. indirectAccess(castExpr, field, n.info),
  272. useShallowCopy=true)
  273. call.add(threadLocal.newSymNode)
  274. proc wrapProcForSpawn*(g: ModuleGraph; idgen: IdGenerator; owner: PSym; spawnExpr: PNode; retType: PType;
  275. barrier, dest: PNode = nil): PNode =
  276. # if 'barrier' != nil, then it is in a 'parallel' section and we
  277. # generate quite different code
  278. let n = spawnExpr[^2]
  279. let spawnKind = spawnResult(retType, barrier!=nil)
  280. case spawnKind
  281. of srVoid:
  282. internalAssert g.config, dest == nil
  283. result = newNodeI(nkStmtList, n.info)
  284. of srFlowVar:
  285. internalAssert g.config, dest == nil
  286. result = newNodeIT(nkStmtListExpr, n.info, retType)
  287. of srByVar:
  288. if dest == nil: localError(g.config, n.info, "'spawn' must not be discarded")
  289. result = newNodeI(nkStmtList, n.info)
  290. if n.kind notin nkCallKinds:
  291. localError(g.config, n.info, "'spawn' takes a call expression; got " & $n)
  292. return
  293. if optThreadAnalysis in g.config.globalOptions:
  294. if {tfThread, tfNoSideEffect} * n[0].typ.flags == {}:
  295. localError(g.config, n.info, "'spawn' takes a GC safe call expression")
  296. var fn = n[0]
  297. let
  298. name = (if fn.kind == nkSym: fn.sym.name.s else: genPrefix) & "Wrapper"
  299. wrapperProc = newSym(skProc, getIdent(g.cache, name), nextId idgen, owner, fn.info, g.config.options)
  300. threadParam = newSym(skParam, getIdent(g.cache, "thread"), nextId idgen, wrapperProc, n.info, g.config.options)
  301. argsParam = newSym(skParam, getIdent(g.cache, "args"), nextId idgen, wrapperProc, n.info, g.config.options)
  302. wrapperProc.flags.incl sfInjectDestructors
  303. block:
  304. let ptrType = getSysType(g, n.info, tyPointer)
  305. threadParam.typ = ptrType
  306. argsParam.typ = ptrType
  307. argsParam.position = 1
  308. var objType = createObj(g, idgen, owner, n.info)
  309. incl(objType.flags, tfFinal)
  310. let castExpr = createCastExpr(argsParam, objType, idgen)
  311. var scratchObj = newSym(skVar, getIdent(g.cache, "scratch"), nextId idgen, owner, n.info, g.config.options)
  312. block:
  313. scratchObj.typ = objType
  314. incl(scratchObj.flags, sfFromGeneric)
  315. var varSectionB = newNodeI(nkVarSection, n.info)
  316. varSectionB.addVar(scratchObj.newSymNode)
  317. result.add varSectionB
  318. var call = newNodeIT(nkCall, n.info, n.typ)
  319. # templates and macros are in fact valid here due to the nature of
  320. # the transformation:
  321. if fn.kind == nkClosure or (fn.typ != nil and fn.typ.callConv == ccClosure):
  322. localError(g.config, n.info, "closure in spawn environment is not allowed")
  323. if not (fn.kind == nkSym and fn.sym.kind in {skProc, skTemplate, skMacro,
  324. skFunc, skMethod, skConverter}):
  325. # for indirect calls we pass the function pointer in the scratchObj
  326. var argType = n[0].typ.skipTypes(abstractInst)
  327. var field = newSym(skField, getIdent(g.cache, "fn"), nextId idgen, owner, n.info, g.config.options)
  328. field.typ = argType
  329. objType.addField(field, g.cache, idgen)
  330. result.add newFastAsgnStmt(newDotExpr(scratchObj, field), n[0])
  331. fn = indirectAccess(castExpr, field, n.info)
  332. elif fn.kind == nkSym and fn.sym.kind == skIterator:
  333. localError(g.config, n.info, "iterator in spawn environment is not allowed")
  334. elif fn.typ.callConv == ccClosure:
  335. localError(g.config, n.info, "closure in spawn environment is not allowed")
  336. call.add(fn)
  337. var varSection = newNodeI(nkVarSection, n.info)
  338. var varInit = newNodeI(nkStmtList, n.info)
  339. if barrier.isNil:
  340. setupArgsForConcurrency(g, n, objType, idgen, wrapperProc, scratchObj, castExpr, call,
  341. varSection, varInit, result)
  342. else:
  343. setupArgsForParallelism(g, n, objType, idgen, wrapperProc, scratchObj, castExpr, call,
  344. varSection, varInit, result)
  345. var barrierAsExpr: PNode = nil
  346. if barrier != nil:
  347. let typ = newType(tyPtr, nextId idgen, owner)
  348. typ.rawAddSon(magicsys.getCompilerProc(g, "Barrier").typ)
  349. var field = newSym(skField, getIdent(g.cache, "barrier"), nextId idgen, owner, n.info, g.config.options)
  350. field.typ = typ
  351. objType.addField(field, g.cache, idgen)
  352. result.add newFastAsgnStmt(newDotExpr(scratchObj, field), barrier)
  353. barrierAsExpr = indirectAccess(castExpr, field, n.info)
  354. var fvField, fvAsExpr: PNode = nil
  355. if spawnKind == srFlowVar:
  356. var field = newSym(skField, getIdent(g.cache, "fv"), nextId idgen, owner, n.info, g.config.options)
  357. field.typ = retType
  358. objType.addField(field, g.cache, idgen)
  359. fvField = newDotExpr(scratchObj, field)
  360. fvAsExpr = indirectAccess(castExpr, field, n.info)
  361. # create flowVar:
  362. result.add newFastAsgnStmt(fvField, callProc(spawnExpr[^1]))
  363. if barrier == nil:
  364. result.add callCodegenProc(g, "nimFlowVarCreateSemaphore", fvField.info, fvField)
  365. elif spawnKind == srByVar:
  366. var field = newSym(skField, getIdent(g.cache, "fv"), nextId idgen, owner, n.info, g.config.options)
  367. field.typ = newType(tyPtr, nextId idgen, objType.owner)
  368. field.typ.rawAddSon(retType)
  369. objType.addField(field, g.cache, idgen)
  370. fvAsExpr = indirectAccess(castExpr, field, n.info)
  371. result.add newFastAsgnStmt(newDotExpr(scratchObj, field), genAddrOf(dest, idgen))
  372. createTypeBoundOps(g, nil, objType, n.info, idgen)
  373. createWrapperProc(g, fn, threadParam, argsParam,
  374. varSection, varInit, call,
  375. barrierAsExpr, fvAsExpr, idgen, spawnKind, wrapperProc)
  376. result.add callCodegenProc(g, "nimSpawn" & $spawnExpr.len, wrapperProc.info,
  377. wrapperProc.newSymNode, genAddrOf(scratchObj.newSymNode, idgen), nil, spawnExpr)
  378. if spawnKind == srFlowVar: result.add fvField