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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317 | @irdl_op_definition
class FuncOp(IRDLOperation):
name = "func.func"
body = region_def()
sym_name = prop_def(SymbolNameConstraint())
function_type = prop_def(FunctionType)
sym_visibility = opt_prop_def(StringAttr)
arg_attrs = opt_prop_def(ArrayAttr[DictionaryAttr])
res_attrs = opt_prop_def(ArrayAttr[DictionaryAttr])
traits = traits_def(
IsolatedFromAbove(), SymbolOpInterface(), FuncOpCallableInterface()
)
def __init__(
self,
name: str,
function_type: FunctionType | tuple[Sequence[Attribute], Sequence[Attribute]],
region: Region | type[Region.DEFAULT] = Region.DEFAULT,
visibility: StringAttr | str | None = None,
*,
arg_attrs: ArrayAttr[DictionaryAttr] | None = None,
res_attrs: ArrayAttr[DictionaryAttr] | None = None,
):
if isinstance(visibility, str):
visibility = StringAttr(visibility)
if isinstance(function_type, tuple):
inputs, outputs = function_type
function_type = FunctionType.from_lists(inputs, outputs)
if not isinstance(region, Region):
region = Region(Block(arg_types=function_type.inputs))
properties: dict[str, Attribute | None] = {
"sym_name": StringAttr(name),
"function_type": function_type,
"sym_visibility": visibility,
"arg_attrs": arg_attrs,
"res_attrs": res_attrs,
}
super().__init__(properties=properties, regions=[region])
def verify_(self) -> None:
# If this is an empty region (external function), then return
if len(self.body.blocks) == 0:
return
entry_block = self.body.blocks.first
assert entry_block is not None
block_arg_types = entry_block.arg_types
if self.function_type.inputs.data != tuple(block_arg_types):
raise VerifyException(
"Expected entry block arguments to have the same types as the function "
"input types"
)
@classmethod
def parse(cls, parser: Parser) -> FuncOp:
visibility = parser.parse_optional_visibility_keyword()
(
name,
input_types,
return_types,
region,
extra_attrs,
arg_attrs,
res_attrs,
) = parse_func_op_like(
parser, reserved_attr_names=("sym_name", "function_type", "sym_visibility")
)
func = FuncOp(
name=name,
function_type=(input_types, return_types),
region=region,
visibility=visibility,
arg_attrs=arg_attrs,
res_attrs=res_attrs,
)
if extra_attrs is not None:
func.attributes |= extra_attrs.data
return func
def print(self, printer: Printer):
if self.sym_visibility:
visibility = self.sym_visibility.data
printer.print_string(" ")
printer.print_string(visibility)
print_func_op_like(
printer,
self.sym_name,
self.function_type,
self.body,
self.attributes,
arg_attrs=self.arg_attrs,
res_attrs=self.res_attrs,
reserved_attr_names=(
"sym_name",
"function_type",
"sym_visibility",
"arg_attrs",
),
)
@staticmethod
def external(
name: str, input_types: Sequence[Attribute], return_types: Sequence[Attribute]
) -> FuncOp:
return FuncOp(
name=name,
function_type=(input_types, return_types),
region=Region(),
visibility="private",
)
@staticmethod
def from_region(
name: str,
input_types: Sequence[Attribute],
return_types: Sequence[Attribute],
region: Region | type[Region.DEFAULT] = Region.DEFAULT,
visibility: StringAttr | str | None = None,
) -> FuncOp:
return FuncOp(
name=name,
function_type=(input_types, return_types),
region=region,
visibility=visibility,
)
def replace_argument_type(
self,
arg: int | BlockArgument,
new_type: Attribute,
rewriter: Rewriter | PatternRewriter,
):
"""
Replaces the type of the argument specified by arg (either the index of the arg,
or the BlockArgument object itself) with new_type. This also takes care of updating
the function_type attribute.
"""
if isinstance(arg, int):
block = self.body.blocks.first
assert block is not None
try:
arg = block.args[arg]
except IndexError:
raise IndexError(f"Block {block} does not have argument #{arg}")
if arg not in self.args:
raise ValueError(f"Arg {arg} does not belong to this function")
rewriter.replace_value_with_new_type(arg, new_type)
self.update_function_type()
def update_function_type(self):
"""
Update the function_type attribute to reflect changes in the
block argument types or return statement arguments.
"""
# Refuse to work with external function definitions, as they don't have block args
assert not self.is_declaration, (
"update_function_type does not work with function declarations!"
)
return_op = self.get_return_op()
return_type = self.function_type.outputs.data
if return_op is not None:
return_type = return_op.operand_types
self.properties["function_type"] = FunctionType.from_lists(
[arg.type for arg in self.args],
return_type,
)
def get_return_op(self) -> ReturnOp | None:
"""
Helper for easily retrieving the return operation of a given
function. Returns None if it couldn't find a return op.
"""
if self.is_declaration:
return None
if (last_block := self.body.blocks.last) is None:
return None
ret_op = last_block.last_op
if not isinstance(ret_op, ReturnOp):
return None
return ret_op
@property
def args(self) -> tuple[BlockArgument, ...]:
"""
A helper to quickly get access to the block arguments of the function
"""
assert not self.is_declaration, (
"Function declarations don't have BlockArguments!"
)
block = self.body.blocks.first
assert block is not None
return block.args
@property
def is_declaration(self) -> bool:
"""
A helper to identify functions that are external declarations (have an empty
function body)
"""
return not self.body.blocks
|