Skip to content

Commit

Permalink
Ensure variable IDs and slots numbers are deterministic
Browse files Browse the repository at this point in the history
Remove the dependence on Dict iteration for var_id and slot number
generation by sorting by VarKey when generating VarId, and VarId when
generating slots. This ensures that integer identifiers in the IR are
always deterministic.
  • Loading branch information
c42f committed Jun 21, 2024
1 parent 6ffab5c commit 0e96d27
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/linear_ir.jl
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ function compile_lambda(outer_ctx, ex)
compile_body(ctx, ex[1])
slot_rewrites = Dict{VarId,Int}()
_add_slots!(slot_rewrites, ctx.var_info, (arg.var_id for arg in lambda_info.args))
_add_slots!(slot_rewrites, ctx.var_info, ex.lambda_locals)
_add_slots!(slot_rewrites, ctx.var_info, sort(collect(ex.lambda_locals)))
code = renumber_body(ctx, ctx.code, slot_rewrites)
makenode(ctx, ex, K"lambda",
makenode(ctx, ex[1], K"block", code),
Expand Down
18 changes: 15 additions & 3 deletions src/scope_analysis.jl
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@ function find_scope_vars(ex)
for e in children(ex)
_find_scope_vars!(assignments, locals, globals, used_names, e)
end

# Sort by key so that id generation is deterministic
assignments = sort(collect(pairs(assignments)), by=first)
locals = sort(collect(pairs(locals)), by=first)
globals = sort(collect(pairs(globals)), by=first)
used_names = sort(collect(used_names))

return assignments, locals, globals, used_names
end

Expand All @@ -98,6 +105,10 @@ struct VarKey
layer::LayerId
end

function Base.isless(a::VarKey, b::VarKey)
(a.name, a.layer) < (b.name, b.layer)
end

# Identifiers produced by lowering will have the following layer by default.
#
# To make new mutable variables without colliding names, lowering can
Expand Down Expand Up @@ -227,9 +238,10 @@ function analyze_scope(ctx, ex, scope_type, lambda_info)
end
end

global_keys = Set(first(g) for g in globals)
# Add explicit locals
for (varkey,e) in pairs(locals)
if haskey(globals, varkey)
for (varkey,e) in locals
if varkey in global_keys
throw(LoweringError(e, "Variable `$(varkey.name)` declared both local and global"))
elseif haskey(var_ids, varkey)
vk = ctx.var_info[var_ids[varkey]].kind
Expand All @@ -245,7 +257,7 @@ function analyze_scope(ctx, ex, scope_type, lambda_info)
end

# Add explicit globals
for (varkey,e) in pairs(globals)
for (varkey,e) in globals
if haskey(var_ids, varkey)
vk = ctx.var_info[var_ids[varkey]].kind
if vk === :argument && is_outer_lambda_scope
Expand Down
88 changes: 88 additions & 0 deletions test/branching.jl
Original file line number Diff line number Diff line change
Expand Up @@ -209,4 +209,92 @@ let a = false, b = false
end
""") === 3

#-------------------------------------------------------------------------------
# Detailed lowering

@test ir_as_text(test_mod, """
begin
local a, b
if a
b
end
end
""") == """
slot.₁/a
(gotoifnot ssa.₁ label.₅)
slot.₂/b
(return ssa.₃)
core.nothing
(return ssa.₅)"""

@test ir_as_text(test_mod, """
begin
local a, b, c
if a
b
end
c
end
""") == """
slot.₁/a
(gotoifnot ssa.₁ label.₄)
slot.₂/b
slot.₃/c
(return ssa.₄)"""

@test ir_as_text(test_mod, """
begin
local a, b, c
if a
b
else
c
end
end
""") == """
slot.₁/a
(gotoifnot ssa.₁ label.₅)
slot.₂/b
(return ssa.₃)
slot.₃/c
(return ssa.₅)"""

@test ir_as_text(test_mod, """
begin
local a, b, c, d
if a
b
else
c
end
d
end
""") == """
slot.₁/a
(gotoifnot ssa.₁ label.₅)
slot.₂/b
(goto label.₆)
slot.₃/c
slot.₄/d
(return ssa.₆)"""

# Blocks compile directly to branches
@test ir_as_text(test_mod, """
begin
local a, b, c, d
if (a; b && c)
d
end
end
""") == """
slot.₁/a
slot.₂/b
(gotoifnot ssa.₂ label.₈)
slot.₃/c
(gotoifnot ssa.₄ label.₈)
slot.₄/d
(return ssa.₆)
core.nothing
(return ssa.₈)"""

end
24 changes: 13 additions & 11 deletions test/demo.jl
Original file line number Diff line number Diff line change
Expand Up @@ -207,24 +207,26 @@ M.@recursive 3
# end
# """

# src = """
# let
# y = 0
# x = 1
# let x = x + 1
# y = x
# end
# (x, y)
# end
# """
src = """
let
y = 0
x = 1
let x = x + 1
y = x
end
(x, y)
end
"""

#src = """M.@outer"""

src = """
begin
local a, b
local a, b, c
if a
b
else
c
end
end
"""
Expand Down

0 comments on commit 0e96d27

Please sign in to comment.