Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.byref always optimizes sink call into a bitwise memcopy if =sink is disabled #24327

Open
wants to merge 2 commits into
base: devel
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion compiler/ccgcalls.nim
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ proc cleanupTemp(p: BProc; returnType: PType, tmp: TLoc): bool =
let dtor = getAttachedOp(p.module.g.graph, returnType, attachedDestructor)
var op = initLocExpr(p, newSymNode(dtor))
var callee = rdLoc(op)
let destroy = if dtor.typ.firstParamType.kind == tyVar:
let destroy = if dtor.typ.firstParamType.kind == tyVar or
tfByRef in dtor.typ.firstParamType.flags:
callee & "(&" & rdLoc(tmp) & ")"
else:
callee & "(" & rdLoc(tmp) & ")"
Expand Down
7 changes: 5 additions & 2 deletions compiler/injectdestructors.nim
Original file line number Diff line number Diff line change
Expand Up @@ -275,13 +275,16 @@ proc deepAliases(dest, ri: PNode): bool =
return aliases(dest, ri) != no

proc genSink(c: var Con; s: var Scope; dest, ri: PNode; flags: set[MoveOrCopyFlag] = {}): PNode =
let t = dest.typ.skipTypes({tyGenericInst, tyAlias, tySink})
if (c.inLoopCond == 0 and (isUnpackedTuple(dest) or IsDecl in flags or
(isAnalysableFieldAccess(dest, c.owner) and isFirstWrite(dest, c)))) or
isNoInit(dest) or IsReturn in flags:
isNoInit(dest) or IsReturn in flags or
(tfByRef in dest.typ.flags and getAttachedOp(c.graph, t, attachedSink) != nil and
sfError in getAttachedOp(c.graph, t, attachedSink).flags
):
# optimize sink call into a bitwise memcopy
result = newTree(nkFastAsgn, dest, ri)
else:
let t = dest.typ.skipTypes({tyGenericInst, tyAlias, tySink})
if getAttachedOp(c.graph, t, attachedSink) != nil:
result = c.genOp(t, attachedSink, dest, ri)
result.add ri
Expand Down
33 changes: 31 additions & 2 deletions tests/arc/tarc_orc.nim
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ type

proc imageCopy*(image: Image): Image {.nodestroy.}

proc `=destroy`*(x: var Image) =
proc `=destroy`*(x: Image) =
discard
proc `=sink`*(dest: var Image; source: Image) =
`=destroy`(dest)
Expand All @@ -111,7 +111,7 @@ proc `=dup`*(source: Image): Image {.nodestroy.} =
proc `=copy`*(dest: var Image; source: Image) =
dest = imageCopy(source) # calls =sink implicitly

proc `=destroy`*(x: var EmbeddedImage) = discard
proc `=destroy`*(x: EmbeddedImage) = discard

proc `=dup`*(source: EmbeddedImage): EmbeddedImage {.nodestroy.} = source

Expand Down Expand Up @@ -184,3 +184,32 @@ block: # bug #24147
let oo = OO(val: "hello world")
var ooCopy : OO
`=copy`(ooCopy, oo)

block:
type MyObj {.byref.} = object
value: int

proc `=copy`(a: var MyObj, b: MyObj) {.error.}
proc `=sink`(a: var MyObj, b: MyObj) {.error.}

proc createMyObj(value: int): MyObj =
result.value = value

var x: MyObj
x = createMyObj(3)

block:
type MyObj {.byref.} = object
value: int

proc `=copy`(a: var MyObj, b: MyObj) {.error.}
proc `=sink`(a: var MyObj, b: MyObj) {.error.}

proc createMyObj(value: int): MyObj =
result.value = value

proc foo =
var x: MyObj
x = createMyObj(3)

foo()