-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathops.rs
245 lines (215 loc) · 7.02 KB
/
ops.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
//! The operation types for the HUGR.
pub mod constant;
pub mod controlflow;
pub mod custom;
pub mod dataflow;
pub mod handle;
pub mod leaf;
pub mod module;
pub mod tag;
pub mod validate;
use crate::types::{EdgeKind, FunctionType, Type};
use crate::PortIndex;
use crate::{Direction, Port};
use portgraph::NodeIndex;
use smol_str::SmolStr;
use enum_dispatch::enum_dispatch;
pub use constant::Const;
pub use controlflow::{BasicBlock, Case, Conditional, TailLoop, CFG};
pub use dataflow::{Call, CallIndirect, Input, LoadConstant, Output, DFG};
pub use leaf::LeafOp;
pub use module::{AliasDecl, AliasDefn, FuncDecl, FuncDefn, Module};
pub use tag::OpTag;
#[enum_dispatch(OpTrait, OpName, ValidateOp)]
#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
/// The concrete operation types for a node in the HUGR.
// TODO: Link the NodeHandles to the OpType.
#[non_exhaustive]
#[allow(missing_docs)]
#[serde(tag = "op")]
pub enum OpType {
Module,
FuncDefn,
FuncDecl,
AliasDecl,
AliasDefn,
Const,
Input,
Output,
Call,
CallIndirect,
LoadConstant,
DFG,
LeafOp,
BasicBlock,
TailLoop,
CFG,
Conditional,
Case,
}
/// The default OpType (as returned by [Default::default])
pub const DEFAULT_OPTYPE: OpType = OpType::Module(Module);
impl Default for OpType {
fn default() -> Self {
DEFAULT_OPTYPE
}
}
impl OpType {
/// The edge kind for the non-dataflow or constant-input ports of the
/// operation, not described by the signature.
///
/// If not None, a single extra multiport of that kind will be present on
/// the given direction.
pub fn other_port(&self, dir: Direction) -> Option<EdgeKind> {
match dir {
Direction::Incoming => self.other_input(),
Direction::Outgoing => self.other_output(),
}
}
/// Returns the edge kind for the given port.
pub fn port_kind(&self, port: impl Into<Port>) -> Option<EdgeKind> {
let signature = self.signature();
let port: Port = port.into();
let dir = port.direction();
let port_count = signature.port_count(dir);
if port.index() < port_count {
signature.get(port).cloned().map(EdgeKind::Value)
} else if port.index() == port_count
&& dir == Direction::Incoming
&& self.static_input().is_some()
{
self.static_input().map(EdgeKind::Static)
} else {
self.other_port(dir)
}
}
/// The non-dataflow port for the operation, not described by the signature.
/// See `[OpType::other_port]`.
///
/// Returns None if there is no such port, or if the operation defines multiple non-dataflow ports.
pub fn other_port_index(&self, dir: Direction) -> Option<Port> {
let non_df_count = self.validity_flags().non_df_port_count(dir).unwrap_or(1);
if self.other_port(dir).is_some() && non_df_count == 1 {
// if there is a static input it comes before the non_df_ports
let static_input =
(dir == Direction::Incoming && self.static_input().is_some()) as usize;
Some(Port::new(
dir,
self.signature().port_count(dir) + static_input,
))
} else {
None
}
}
/// Returns the number of ports for the given direction.
pub fn port_count(&self, dir: Direction) -> usize {
let signature = self.signature();
let has_other_ports = self.other_port(dir).is_some();
let non_df_count = self
.validity_flags()
.non_df_port_count(dir)
.unwrap_or(has_other_ports as usize);
// if there is a static input it comes before the non_df_ports
let static_input = (dir == Direction::Incoming && self.static_input().is_some()) as usize;
signature.port_count(dir) + non_df_count + static_input
}
/// Returns the number of inputs ports for the operation.
pub fn input_count(&self) -> usize {
self.port_count(Direction::Incoming)
}
/// Returns the number of outputs ports for the operation.
pub fn output_count(&self) -> usize {
self.port_count(Direction::Outgoing)
}
/// Checks whether the operation can contain children nodes.
pub fn is_container(&self) -> bool {
self.validity_flags().allowed_children != OpTag::None
}
}
/// Macro used by operations that want their
/// name to be the same as their type name
macro_rules! impl_op_name {
($i: ident) => {
impl $crate::ops::OpName for $i {
fn name(&self) -> smol_str::SmolStr {
stringify!($i).into()
}
}
};
}
use impl_op_name;
#[enum_dispatch]
/// Trait for setting name of OpType variants.
// Separate to OpTrait to allow simple definition via impl_op_name
pub trait OpName {
/// The name of the operation.
fn name(&self) -> SmolStr;
}
/// Trait statically querying the tag of an operation.
///
/// This is implemented by all OpType variants, and always contains the dynamic
/// tag returned by `OpType::tag(&self)`.
pub trait StaticTag {
/// The name of the operation.
const TAG: OpTag;
}
#[enum_dispatch]
/// Trait implemented by all OpType variants.
pub trait OpTrait {
/// A human-readable description of the operation.
fn description(&self) -> &str;
/// Tag identifying the operation.
fn tag(&self) -> OpTag;
/// The signature of the operation.
///
/// Only dataflow operations have a non-empty signature.
fn signature(&self) -> FunctionType {
Default::default()
}
/// Get the static input type of this operation if it has one (only Some for
/// [`LoadConstant`] and [`Call`])
#[inline]
fn static_input(&self) -> Option<Type> {
None
}
/// The edge kind for the non-dataflow or constant inputs of the operation,
/// not described by the signature.
///
/// If not None, a single extra output multiport of that kind will be
/// present.
fn other_input(&self) -> Option<EdgeKind> {
None
}
/// The edge kind for the non-dataflow outputs of the operation, not
/// described by the signature.
///
/// If not None, a single extra output multiport of that kind will be
/// present.
fn other_output(&self) -> Option<EdgeKind> {
None
}
}
#[enum_dispatch]
/// Methods for Ops to validate themselves and children
pub trait ValidateOp {
/// Returns a set of flags describing the validity predicates for this operation.
#[inline]
fn validity_flags(&self) -> validate::OpValidityFlags {
Default::default()
}
/// Validate the ordered list of children.
#[inline]
fn validate_op_children<'a>(
&self,
_children: impl DoubleEndedIterator<Item = (NodeIndex, &'a OpType)>,
) -> Result<(), validate::ChildrenValidationError> {
Ok(())
}
}
/// Macro used for default implementation of ValidateOp
macro_rules! impl_validate_op {
($i: ident) => {
impl $crate::ops::ValidateOp for $i {}
};
}
use impl_validate_op;