Skip to content

Commit

Permalink
Bugfix: define eachinstruction for CompiledBasicBlock
Browse files Browse the repository at this point in the history
  • Loading branch information
felipenoris committed Apr 27, 2021
1 parent 7ed3242 commit 1ec53d5
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 26 deletions.
1 change: 1 addition & 0 deletions src/build.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ end
Base.iterate(itr::InstructionIterator) = iterate(itr.instructions)
Base.iterate(itr::InstructionIterator, state) = iterate(itr.instructions, state)
eachinstruction(bb::BasicBlock) = InstructionIterator(bb.instructions)
eachinstruction(bb::CompiledBasicBlock) = InstructionIterator(bb.instructions)

"""
has_symbol(bb::BasicBlock, sym::Symbol) :: Bool
Expand Down
13 changes: 0 additions & 13 deletions src/interpreter.jl
Original file line number Diff line number Diff line change
@@ -1,17 +1,4 @@

"""
A CompiledBasicBlock mirrors BasicBlock
but the instructions LookupTable is replaced
with Vector to save memory.
"""
struct CompiledBasicBlock <: Program
instructions::Vector{LinearInstruction}
inputs::LookupTable{ImmutableVariable}
mutable_locals::LookupTable{MutableVariable}
immutable_locals::Dict{ImmutableVariable, ImmutableValue}
outputs::LookupTable{Variable}
end

function CompiledBasicBlock(b::BasicBlock)
return CompiledBasicBlock(b.instructions.entries, b.inputs, b.mutable_locals, b.immutable_locals, b.outputs)
end
Expand Down
13 changes: 13 additions & 0 deletions src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,19 @@ mutable struct BasicBlock <: Program
# cfg::Union{Nothing, Program}
end

"""
A CompiledBasicBlock mirrors BasicBlock
but the instructions LookupTable is replaced
with Vector to save memory.
"""
struct CompiledBasicBlock <: Program
instructions::Vector{LinearInstruction}
inputs::LookupTable{ImmutableVariable}
mutable_locals::LookupTable{MutableVariable}
immutable_locals::Dict{ImmutableVariable, ImmutableValue}
outputs::LookupTable{Variable}
end

#struct Goto <: BranchInstruction
# target::BasicBlock
#end
Expand Down
26 changes: 13 additions & 13 deletions test/benchmarks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,28 @@
MBP Julia v1.6.1
[ Info: Benchmarks
Compile Native:
0.000851 seconds (338 allocations: 22.781 KiB, 0.06% compilation time)
0.000830 seconds (338 allocations: 22.781 KiB, 0.05% compilation time)
Compile BasicBlockInterpreter
0.000006 seconds (7 allocations: 960 bytes)
Compilation Overhead: Native / BasicBlockInterpreter: 158.9x
Compilation Overhead: Native / BasicBlockInterpreter: 96.9x
F Call Native 1st
0.029588 seconds (46.66 k allocations: 2.884 MiB, 99.94% compilation time)
0.040432 seconds (46.67 k allocations: 2.885 MiB, 25.46% gc time, 99.96% compilation time)
F Call Native 2nd
0.000008 seconds (4 allocations: 176 bytes)
0.000006 seconds (4 allocations: 176 bytes)
F Call Interpreter 1st
0.073772 seconds (122.28 k allocations: 7.433 MiB, 10.88% gc time, 99.62% compilation time)
0.052958 seconds (122.28 k allocations: 7.433 MiB, 99.53% compilation time)
F Call Interpreter 2nd
0.000065 seconds (37 allocations: 976 bytes)
0.000062 seconds (37 allocations: 976 bytes)
F Call Julia 1st
0.010354 seconds (16.20 k allocations: 966.099 KiB, 99.78% compilation time)
0.010146 seconds (16.20 k allocations: 966.099 KiB, 99.81% compilation time)
F Call Julia 2nd
0.000004 seconds (4 allocations: 176 bytes)
F Call Overhead: BasicBlockInterpreter / julia = 33.3x
F Call Overhead: Native / julia = 1.2x
0.000005 seconds (4 allocations: 176 bytes)
F Call Overhead: BasicBlockInterpreter / julia = 28.9x
F Call Overhead: Native / julia = 0.6x
[ Info: Compilation + F Call
BasicBlockInterpreter: 77.9µs
Native: 793.4µs
Native / BasicBlockInterpreter = 10.2x
BasicBlockInterpreter: 171.7µs
Native: 714.2µs
Native / BasicBlockInterpreter = 4.2x
=#

function benchmark_julia(x, y)
Expand Down
16 changes: 16 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -383,13 +383,29 @@ end
@test cnst == 1.0
end

let
f = OIR.compile(OIR.BasicBlockInterpreter, OIR.CompiledBasicBlock(bb))
cnst, slot, output = f(input)
@test slot == 11.0
@test output == 1.0 + 10.0 + 10.0
@test cnst == 1.0
end

let
f = OIR.compile(OIR.Native, bb)
cnst, slot, output = f(input)
@test slot == 11.0
@test output == 1.0 + 10.0 + 10.0
@test cnst == 1.0
end

let
f = OIR.compile(OIR.Native, OIR.CompiledBasicBlock(bb))
cnst, slot, output = f(input)
@test slot == 11.0
@test output == 1.0 + 10.0 + 10.0
@test cnst == 1.0
end
end

@testset "has_symbol" begin
Expand Down

0 comments on commit 1ec53d5

Please sign in to comment.