Skip to content

Commit afb67be

Browse files
committed
fix: madhyasthal: enable escape analysis pass; do not alias copy if source is an escapee
1 parent 01f6797 commit afb67be

2 files changed

Lines changed: 11 additions & 2 deletions

File tree

src/bali/runtime/compiler/amd64/midtier.nim

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,10 @@ proc compile*(
323323

324324
var pipeline = Pipeline(fn: &lowered)
325325
pipeline.optimize(
326-
{Passes.NaiveDeadCodeElim, Passes.AlgebraicSimplification, Passes.CopyPropagation}
326+
{
327+
Passes.NaiveDeadCodeElim, Passes.AlgebraicSimplification, Passes.EscapeAnalysis,
328+
Passes.CopyPropagation,
329+
}
327330
)
328331

329332
return compileLowered(cgen, pipeline)

src/bali/runtime/compiler/madhyasthal/copy_propagation.nim

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
##
33
## The basic idea behind this pass is that the middle-end (AST -> Bytecode / "Niche") generates a lot of unnecessary
44
## copy instructions. We can safely* eliminate these unnecessary copies if we can prove that the copied destination
5-
## is unworthy of copying (it is never mutated)
5+
## is unworthy of copying (it is never mutated AND it is locally defined)
66
##
77
## Copyright (C) 2025 Trayambak Rai (xtrayambak at disroot dot org)
88
import std/[sets]
@@ -53,6 +53,12 @@ func eliminateUnmutatedCopies*(
5353
# Any attempts to eliminate this copy will cause semantic breakage.
5454
continue
5555

56+
if copy.dest notin pipeline.info.esc.locals:
57+
# If the register is not locally owned, we cannot
58+
# safely alias the copy as it might end up mutating
59+
# a global state, which'd cause semantic breakage.
60+
continue
61+
5662
unmutated.incl(copy)
5763

5864
ensureMove(unmutated)

0 commit comments

Comments
 (0)