Class: Kumi::Core::LIR::Peephole

Inherits:
Object
  • Object
show all
Defined in:
lib/kumi/core/lir/peephole.rb

Overview

Peephole provides a small helper for implementing local LIR rewrites. It iterates over an instruction array and yields a Window object that exposes the current instruction alongside a handful of mutation helpers.

Example:

Peephole.run(ops) do |window|
  next unless window.match?(:Constant, :Constant, :KernelCall)

  last = window.instruction(2)
  const = Build.constant(
    value: 3,
    dtype: last.stamp.dtype,
    as: last.result_register,
    ids: ids
  )
  window.replace(3, with: const)
end

Defined Under Namespace

Classes: Window

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ops) ⇒ Peephole

Returns a new instance of Peephole.



30
31
32
# File 'lib/kumi/core/lir/peephole.rb', line 30

def initialize(ops)
  @ops = ops
end

Instance Attribute Details

#opsObject (readonly)

Returns the value of attribute ops.



24
25
26
# File 'lib/kumi/core/lir/peephole.rb', line 24

def ops
  @ops
end

Class Method Details

.run(ops) ⇒ Object



26
27
28
# File 'lib/kumi/core/lir/peephole.rb', line 26

def self.run(ops, &)
  new(ops).run(&)
end

Instance Method Details

#runObject



34
35
36
37
38
39
40
41
42
# File 'lib/kumi/core/lir/peephole.rb', line 34

def run
  index = 0
  while index < @ops.length
    window = Window.new(@ops, index)
    yield window
    index = window.next_index
  end
  @ops
end