Class: Kumi::Core::Analyzer::Passes::LoadInputCSE

Inherits:
PassBase
  • Object
show all
Defined in:
lib/kumi/core/analyzer/passes/load_input_cse.rb

Overview

Load Input Common Subexpression Elimination Pass

Eliminates redundant load_input operations by reusing loads that were already stored by earlier declarations.

OPTIMIZATION STRATEGY:

  • Cross-declaration load reuse: If a load_input with the same (plan_id, scope, is_scalar, has_idx) was already stored by an earlier declaration, rewrite later identical loads to ref the stored value instead of re-loading.

  • Only reuses producers that appear earlier in module order (no reordering/hoisting).

  • Safe because interpreter’s outputs persist across declarations and ref operations resolve previously stored values.

REQUIREMENTS:

  • Must run after LowerToIR pass

  • IR module must be available in state

DEBUG:

  • Set DEBUG_LOAD_CSE=1 to see optimization decisions

Instance Method Summary collapse

Methods inherited from PassBase

#debug, #debug_enabled?, #initialize

Methods included from ErrorReporting

#inferred_location, #raise_localized_error, #raise_syntax_error, #raise_type_error, #report_enhanced_error, #report_error, #report_semantic_error, #report_syntax_error, #report_type_error

Constructor Details

This class inherits a constructor from Kumi::Core::Analyzer::Passes::PassBase

Instance Method Details

#run(_errors) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/kumi/core/analyzer/passes/load_input_cse.rb', line 29

def run(_errors)
  ir = get_state(:ir_module, required: true)
  return state unless ir&.decls

  debug = ENV.fetch("DEBUG_LOAD_CSE", nil)

  # Map: key -> { name:, decl_index: }
  producers = {}

  puts "LOAD_CSE: Analyzing #{ir.decls.length} declarations" if debug

  # First pass: find canonical producers (earliest decl that stores a given load)
  ir.decls.each_with_index do |decl, di|
    decl.ops.each_with_index do |op, oi|
      next unless op.tag == :load_input

      key = load_key(op)
      # Does this decl store that slot under a name?
      store_name = name_storing_slot(decl.ops, oi)
      next unless store_name

      # Keep earliest producer only
      unless producers.key?(key)
        producers[key] = { name: store_name, decl_index: di }
        puts "LOAD_CSE: Found producer #{store_name} in decl #{di} for key #{key.inspect}" if debug
      end
    end
  end

  puts "LOAD_CSE: Found #{producers.size} unique load patterns" if debug

  # Second pass: rewrite later identical loads to refs
  optimizations = 0
  new_decls = ir.decls.each_with_index.map do |decl, di|
    new_ops = decl.ops.each_with_index.map do |op, oi|
      next op unless op.tag == :load_input

      key = load_key(op)
      prod = producers[key]

      # Only rewrite if producer is in an earlier decl
      if prod && prod[:decl_index] < di
        optimizations += 1
        puts "LOAD_CSE: Replacing load_input in #{decl.name}[#{oi}] with ref to #{prod[:name]}" if debug
        Kumi::Core::IR::Ops.Ref(prod[:name])
      else
        op
      end
    end

    Kumi::Core::IR::Decl.new(
      name: decl.name,
      kind: decl.kind,
      shape: decl.shape,
      ops: new_ops
    )
  end

  puts "LOAD_CSE: Applied #{optimizations} optimizations" if debug

  new_ir = Kumi::Core::IR::Module.new(inputs: ir.inputs, decls: new_decls)
  state.with(:ir_module, new_ir)
end