Class: Kumi::IR::Base::Instruction
- Inherits:
-
Object
- Object
- Kumi::IR::Base::Instruction
show all
- Defined in:
- lib/kumi/ir/base/instruction.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(opcode:, result: nil, inputs: [], attributes: {}, metadata: {}, effects: Effects::NONE) ⇒ Instruction
Returns a new instance of Instruction.
21
22
23
24
25
26
27
28
29
30
31
|
# File 'lib/kumi/ir/base/instruction.rb', line 21
def initialize(opcode:, result: nil, inputs: [], attributes: {}, metadata: {}, effects: Effects::NONE)
@opcode = opcode.to_sym
@result = result
@inputs = Array(inputs).freeze
@attributes = attributes.freeze
@metadata = metadata.freeze
@effects = normalize_effects(effects)
@axes = metadata[:axes] || []
@dtype = metadata[:dtype]
validate!
end
|
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
19
20
21
|
# File 'lib/kumi/ir/base/instruction.rb', line 19
def attributes
@attributes
end
|
#axes ⇒ Object
Returns the value of attribute axes.
19
20
21
|
# File 'lib/kumi/ir/base/instruction.rb', line 19
def axes
@axes
end
|
#dtype ⇒ Object
Returns the value of attribute dtype.
19
20
21
|
# File 'lib/kumi/ir/base/instruction.rb', line 19
def dtype
@dtype
end
|
#effects ⇒ Object
Returns the value of attribute effects.
19
20
21
|
# File 'lib/kumi/ir/base/instruction.rb', line 19
def effects
@effects
end
|
Returns the value of attribute inputs.
19
20
21
|
# File 'lib/kumi/ir/base/instruction.rb', line 19
def inputs
@inputs
end
|
Returns the value of attribute metadata.
19
20
21
|
# File 'lib/kumi/ir/base/instruction.rb', line 19
def metadata
@metadata
end
|
#opcode ⇒ Object
Returns the value of attribute opcode.
19
20
21
|
# File 'lib/kumi/ir/base/instruction.rb', line 19
def opcode
@opcode
end
|
#result ⇒ Object
Returns the value of attribute result.
19
20
21
|
# File 'lib/kumi/ir/base/instruction.rb', line 19
def result
@result
end
|
Instance Method Details
#control_effect? ⇒ Boolean
39
|
# File 'lib/kumi/ir/base/instruction.rb', line 39
def control_effect? = @effects.include?(Effects::CONTROL)
|
#defs ⇒ Object
48
49
50
|
# File 'lib/kumi/ir/base/instruction.rb', line 48
def defs
@result ? [@result] : []
end
|
#effectful? ⇒ Boolean
35
36
37
|
# File 'lib/kumi/ir/base/instruction.rb', line 35
def effectful?
!@effects.empty?
end
|
#io_effect? ⇒ Boolean
42
|
# File 'lib/kumi/ir/base/instruction.rb', line 42
def io_effect? = @effects.include?(Effects::IO)
|
#memory_effect? ⇒ Boolean
41
|
# File 'lib/kumi/ir/base/instruction.rb', line 41
def memory_effect? = @effects.include?(Effects::MEMORY)
|
#normalized_attributes ⇒ Object
56
57
58
59
60
|
# File 'lib/kumi/ir/base/instruction.rb', line 56
def normalized_attributes
return attributes unless attributes.is_a?(Hash)
attributes.sort_by { |k, _| k.to_s }
end
|
#printer_attributes ⇒ Object
137
138
139
|
# File 'lib/kumi/ir/base/instruction.rb', line 137
def printer_attributes
attributes
end
|
#printer_axes ⇒ Object
141
|
# File 'lib/kumi/ir/base/instruction.rb', line 141
def printer_axes = axes
|
#printer_dtype ⇒ Object
142
|
# File 'lib/kumi/ir/base/instruction.rb', line 142
def printer_dtype = dtype
|
#produces? ⇒ Boolean
33
|
# File 'lib/kumi/ir/base/instruction.rb', line 33
def produces? = !@result.nil?
|
#stamp ⇒ Object
52
53
54
|
# File 'lib/kumi/ir/base/instruction.rb', line 52
def stamp
{ axes:, dtype: }
end
|
#state_effect? ⇒ Boolean
40
|
# File 'lib/kumi/ir/base/instruction.rb', line 40
def state_effect? = @effects.include?(Effects::STATE)
|
#to_h ⇒ Object
116
117
118
119
120
121
122
123
124
125
|
# File 'lib/kumi/ir/base/instruction.rb', line 116
def to_h
{
opcode:,
result:,
inputs:,
attributes:,
metadata:,
effects: @effects.to_a
}
end
|
#to_print_string(_printer = nil) ⇒ Object
127
128
129
130
131
132
133
134
135
|
# File 'lib/kumi/ir/base/instruction.rb', line 127
def to_print_string(_printer = nil)
parts = []
parts << "%#{result} =" if result
parts << opcode.to_s
parts << format_inputs(inputs)
parts << format_attributes(printer_attributes)
parts << format_axes_dtype
parts.compact.join(" ")
end
|
#uses ⇒ Object
44
45
46
|
# File 'lib/kumi/ir/base/instruction.rb', line 44
def uses
@inputs.select { |input| input.is_a?(Symbol) }
end
|
#validate! ⇒ Object
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
# File 'lib/kumi/ir/base/instruction.rb', line 72
def validate!
raise ArgumentError, "invalid #{self.class}: opcode must be a Symbol" unless @opcode.is_a?(Symbol)
raise ArgumentError, "invalid #{self.class}: result must be a Symbol or nil" if @result && !@result.is_a?(Symbol)
raise ArgumentError, "invalid #{self.class}: inputs must be an Array" unless @inputs.is_a?(Array)
bad_inputs = @inputs.reject { |input| input.is_a?(Symbol) }
raise ArgumentError, "invalid #{self.class}: inputs must be Symbols (bad: #{bad_inputs.inspect})" if bad_inputs.any?
raise ArgumentError, "invalid #{self.class}: attributes must be a Hash" unless @attributes.is_a?(Hash)
unless @attributes.keys.all? { |key| key.is_a?(Symbol) }
raise ArgumentError, "invalid #{self.class}: attributes keys must be Symbols"
end
raise ArgumentError, "invalid #{self.class}: metadata must be a Hash" unless @metadata.is_a?(Hash)
axes = @metadata[:axes]
if axes && !(axes.is_a?(Array) && axes.all? { |axis| axis.is_a?(Symbol) })
raise ArgumentError, "invalid #{self.class}: axes must be an Array of Symbols"
end
dtype = @metadata[:dtype]
if dtype
type_class = defined?(Kumi::Core::Types::Type) ? Kumi::Core::Types::Type : nil
valid_dtype = dtype.is_a?(Symbol) || (type_class && dtype.is_a?(type_class))
raise ArgumentError, "invalid #{self.class}: dtype must be a Symbol or Types::Type" unless valid_dtype
end
self
end
|
#value_signature(inputs: @inputs, include_axes: false, include_dtype: false, allow_effectful: false) ⇒ Object
62
63
64
65
66
67
68
69
70
|
# File 'lib/kumi/ir/base/instruction.rb', line 62
def value_signature(inputs: @inputs, include_axes: false, include_dtype: false, allow_effectful: false)
return nil unless @result
return nil if effectful? && !allow_effectful
signature = [opcode, inputs, normalized_attributes]
signature << axes if include_axes
signature << dtype if include_dtype
signature
end
|
105
106
107
108
109
110
111
112
113
114
|
# File 'lib/kumi/ir/base/instruction.rb', line 105
def with_metadata()
self.class.new(
opcode: @opcode,
result: @result,
inputs: @inputs,
attributes: @attributes,
metadata: @metadata.merge(),
effects: @effects
)
end
|