Skip to content

Commit

Permalink
set type of statement when processing GlobalRefs
Browse files Browse the repository at this point in the history
Because we previously didn't set the type when processing `GlobalRef`,
the optimizer was inserting unnecessary `PiNode`s.

Before:

```julia
julia> x::Int = 1
1

julia> function f()
           global x = 0
           while x<10
               x += 1
           end
           x
       end
f (generic function with 1 method)

julia> @code_typed f()
CodeInfo(
1 ──       (Main.x = 0)::Any
2 ┄─ %2  = Main.x::Any
│    %3  = (isa)(%2, Int64)::Bool
└───       goto #4 if not %3
3 ── %5  = π (%2, Int64)
│    %6  = Base.slt_int(%5, 10)::Bool
└───       goto #5
4 ── %8  = (%2 < 10)::Bool
└───       goto #5
5 ┄─ %10 = φ (#3 => %6, #4 => %8)::Bool
└───       goto #10 if not %10
6 ── %12 = Main.x::Any
│    %13 = (isa)(%12, Int64)::Bool
└───       goto #8 if not %13
7 ── %15 = π (%12, Int64)
│    %16 = Base.add_int(%15, 1)::Int64
└───       goto #9
8 ── %18 = (%12 + 1)::Int64
└───       goto #9
9 ┄─ %20 = φ (#7 => %16, #8 => %18)::Int64
│          (Main.x = %20)::Any
└───       goto #2
10 ─ %23 = Main.x::Any
└───       return %23
) => Int64
```

This PR:

```julia
julia> @code_typed f()
CodeInfo(
1 ─      (Main.x = 0)::Any
2 ┄ %2 = Main.x::Int64
│   %3 = Base.slt_int(%2, 10)::Bool
└──      goto #4 if not %3
3 ─ %5 = Main.x::Int64
│   %6 = Base.add_int(%5, 1)::Int64
│        (Main.x = %6)::Any
└──      goto #2
4 ─ %9 = Main.x::Int64
└──      return %9
) => Int64
```
  • Loading branch information
simeonschaub committed Feb 16, 2022
1 parent 983598a commit 9858c43
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
4 changes: 4 additions & 0 deletions base/compiler/ssair/ir.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1001,6 +1001,10 @@ function process_node!(compact::IncrementalCompact, result_idx::Int, inst::Instr
result[result_idx][:inst] = GotoNode(compact.bb_rename_succ[stmt.label])
result_idx += 1
elseif isa(stmt, GlobalRef) || isa(stmt, GotoNode)
result[result_idx][:inst] = stmt
result[result_idx][:type] = argextype(stmt, compact)
result_idx += 1
elseif isa(stmt, GotoNode)
result[result_idx][:inst] = stmt
result_idx += 1
elseif isa(stmt, GotoIfNot) && compact.cfg_transforms_enabled
Expand Down
15 changes: 15 additions & 0 deletions test/compiler/inline.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1069,3 +1069,18 @@ end
@test fully_eliminated() do
issue41694(2)
end

let m = Module()
@eval m begin
global x::Int = 0
function f()
global x = 0
while x<10
x += 1
end
x
end
end
src = code_typed1(f)
@test count(x -> isa(x, Core.PiNode), src.code) == 0
end

0 comments on commit 9858c43

Please sign in to comment.