91 parameter_xdsl_types = []
92 for argument
in function.arguments:
93 if isinstance(argument.type, TStruct):
94 parameter_xdsl_types.append(llvm.LLVMPointerType())
95 self.
struct_map[argument.type.name] = argument.type
96 elif isinstance(argument.type, TFunction):
97 parameter_xdsl_types.append(argument.type.print(self))
98 xdsl_function = func.FuncOp(self.
internal_name, (tuple(parameter_xdsl_types), ()))
99 self.
builders.append(Builder(insertion_point=InsertPoint.at_start(xdsl_function.body.block)))
101 for i, argument
in enumerate(function.arguments):
102 if isinstance(argument.type, TStruct)
or isinstance(argument.type, TFunction):
103 self.
symbol_map[argument.name] = xdsl_function.args[i]
105 for statement
in function.body:
106 statement.print(self)
108 return_op = func.ReturnOp()
111 external_functions = []
112 assert(function.function_map
is not None)
113 for name, external_function
in function.function_map.items():
114 external_parameter_types = []
115 for argument_type
in external_function.argument_types.values():
116 if isinstance(argument_type, TArray):
117 element_type = argument_type.element_type.print(self)
118 assert(isinstance(element_type, Attribute))
119 external_parameter_types.append(
122 [builtin.DYNAMIC_INDEX],
123 builtin.StridedLayoutAttr([1],
None),
126 elif isinstance(argument_type, TVector):
127 element_type = argument_type.base_type.print(self)
128 assert(isinstance(element_type, Attribute))
129 external_parameter_types.append(
132 [builtin.DYNAMIC_INDEX],
133 builtin.StridedLayoutAttr([1],
None),
137 external_parameter_types.append(argument_type.print(self))
139 external_functions.append(
141 name=name.id +
"Stateless",
143 visibility=builtin.StringAttr(
"private"),
144 function_type=(tuple(external_parameter_types), ()),
148 module_op = builtin.ModuleOp(external_functions + [xdsl_function])
191 if array_allocation.array.array_size == 1:
192 return array_allocation.allocation.print(self, indent_level)
193 elif array_allocation.array.flatten:
194 outer_size_print = array_allocation.array.array_size[0].print(self)
195 inner_size_print = array_allocation.array.array_size[1].print(self)
196 if outer_size_print.type == builtin.IntegerType(64):
197 cast_op = arith.IndexCastOp(outer_size_print, builtin.IndexType())
199 outer_size_print = cast_op.result
200 if inner_size_print.type == builtin.IntegerType(64):
201 cast_op = arith.IndexCastOp(inner_size_print, builtin.IndexType())
203 inner_size_print = cast_op.result
205 memref_type = array_allocation.array.get_type().print(self)
206 allocation_op = memref.AllocOp(dynamic_sizes=[outer_size_print, inner_size_print], symbol_operands=[], result_type=memref_type)
207 self.
builders[-1].insert(allocation_op)
208 return allocation_op.memref
210 outer_allocation = array_allocation.outer_allocation.print(self, indent_level)
211 self.
symbol_map[array_allocation.variable.name] = outer_allocation
212 array_allocation.inner_allocation.print(self, indent_level)
213 return outer_allocation
276 def print_for(self, for_loop: For, indent_level: int = 0):
277 lower_bound_ssa_value = for_loop.iteration_start.print(self)
278 upper_bound_ssa_value = for_loop.iteration_end.print(self)
279 assert(isinstance(lower_bound_ssa_value, SSAValue))
280 assert(isinstance(upper_bound_ssa_value, SSAValue))
282 if lower_bound_ssa_value.type == builtin.IntegerType(64):
283 cast_op = arith.IndexCastOp(lower_bound_ssa_value, builtin.IndexType())
284 lower_bound_ssa_value = cast_op.result
286 if upper_bound_ssa_value.type == builtin.IntegerType(64):
287 cast_op = arith.IndexCastOp(upper_bound_ssa_value, builtin.IndexType())
288 upper_bound_ssa_value = cast_op.result
291 body_block = Block(arg_types=[builtin.IndexType()])
292 body_block.add_op(affine.YieldOp.get())
293 map_identity = AffineMap(1, 0, (AffineDimExpr(0),))
294 lower_bounds_map = builtin.AffineMapAttr(map_identity)
295 upper_bounds_map = builtin.AffineMapAttr(map_identity)
296 lower_bounds_groups = builtin.DenseIntElementsAttr.from_list(builtin.VectorType(builtin.IntegerType(32), [1]), [1])
297 upper_bounds_groups = builtin.DenseIntElementsAttr.from_list(builtin.VectorType(builtin.IntegerType(32), [1]), [1])
298 steps = builtin.ArrayAttr([builtin.IntegerAttr(1, builtin.IntegerType(64))])
300 parallel_op = affine.ParallelOp.build(
301 operands=[[lower_bound_ssa_value, upper_bound_ssa_value]],
303 regions=[Region(body_block)],
305 "reductions": builtin.ArrayAttr([]),
306 "lowerBoundsMap": lower_bounds_map,
307 "lowerBoundsGroups": lower_bounds_groups,
308 "upperBoundsMap": upper_bounds_map,
309 "upperBoundsGroups": upper_bounds_groups,
313 self.
builders.append(Builder(insertion_point=InsertPoint.at_start(body_block)))
315 self.
symbol_map[for_loop.iteration_variable.name] = body_block.args[0]
319 for statement
in for_loop.statements:
320 statement.print(self)
321 del self.
symbol_map[for_loop.iteration_variable.name]
325 self.
builders[-1].insert(parallel_op)
352 rhs_ssa_value = assignment.rhs.print(self)
353 assert(isinstance(rhs_ssa_value, SSAValue))
357 assert(isinstance(assignment.lhs, Subscript))
358 memref_ssa_value = assignment.lhs.base.print(self)
359 assert(isinstance(memref_ssa_value, SSAValue))
360 assert(isinstance(memref_ssa_value.type, builtin.MemRefType))
368 if assignment.lhs.flatten:
370 store_op = affine.StoreOp(rhs_ssa_value, memref_ssa_value, indices=dim_values + symbol_values, map=builtin.AffineMapAttr(map))
375 load_op = affine.LoadOp(
377 indices=dim_values + symbol_values,
378 map=builtin.AffineMapAttr(outer_map),
381 store_op = affine.StoreOp(
384 indices=dim_values + symbol_values,
385 map=builtin.AffineMapAttr(inner_map),
393 assert(isinstance(data_block_construction_from_external.rhs.value, External))
394 if isinstance(data_block_construction_from_external.rhs.value, ExternalStructField):
395 struct_variable_ssa_value = self.
symbol_map[data_block_construction_from_external.rhs.value.struct_name]
396 field_name = data_block_construction_from_external.rhs.value.field_name
397 struct_type = data_block_construction_from_external.rhs.value.struct_type
398 fields = [field
for field
in struct_type.fields.keys()]
399 field_index = fields.index(field_name)
401 field_type = struct_type.fields[field_name]
402 assert(isinstance(field_type, TArray))
406 if field_type.flatten
or field_type.number_of_dimensions == 1:
407 memref_shape = [builtin.DYNAMIC_INDEX
for _
in range(field_type.number_of_dimensions)]
408 if isinstance(field_type.element_type, TVector):
409 memref_shape.append(builtin.DYNAMIC_INDEX)
411 xdsl_struct_field_types = [
get_xdsl_type(field_type)
for field_type
in struct_type.fields.values()]
412 xdsl_struct_type = llvm.LLVMStructType(builtin.StringAttr(data_block_construction_from_external.rhs.value.struct_name.id), builtin.ArrayAttr(xdsl_struct_field_types))
414 gep_op = llvm.GEPOp(ptr=struct_variable_ssa_value,
415 result_type=llvm.LLVMPointerType(),
416 indices=[0, field_index],
417 pointee_type=xdsl_struct_type)
419 descriptor_load_op = llvm.LoadOp(ptr=gep_op.result, result_type=
get_xdsl_type(field_type))
420 memref_cast_op = builtin.UnrealizedConversionCastOp.get(inputs=descriptor_load_op.results, result_type=[builtin.MemRefType(builtin.Float64Type(), memref_shape)])
421 self.
symbol_map[data_block_construction_from_external.lhs.name] = memref_cast_op.results[0]
425 self.
builders[-1].insert(descriptor_load_op)
426 self.
builders[-1].insert(memref_cast_op)
428 nested_memref_type = field_type.print(self)
430 xdsl_struct_field_types = [
432 for field_type
in struct_type.fields.values()
434 xdsl_struct_type = llvm.LLVMStructType(
435 builtin.StringAttr(data_block_construction_from_external.rhs.value.struct_name.id),
436 builtin.ArrayAttr(xdsl_struct_field_types),
440 ptr=struct_variable_ssa_value,
441 result_type=llvm.LLVMPointerType(),
442 indices=[0, field_index],
443 pointee_type=xdsl_struct_type,
445 descriptor_load_op = llvm.LoadOp(
446 ptr=gep_op.result, result_type=xdsl_memref_descriptor
448 memref_cast_op = builtin.UnrealizedConversionCastOp.get(
449 inputs=descriptor_load_op.results,
450 result_type=[nested_memref_type],
452 self.
symbol_map[data_block_construction_from_external.lhs.name] = (
453 memref_cast_op.results[0]
458 self.
builders[-1].insert(descriptor_load_op)
459 self.
builders[-1].insert(memref_cast_op)
531 if len(subscript.indices) == 1:
532 memref_ssa_value = subscript.base.print(self)
533 assert(isinstance(memref_ssa_value, SSAValue))
534 assert(isinstance(memref_ssa_value.type, builtin.MemRefType))
542 if len(memref_ssa_value.type.get_shape()) == 2:
543 index_apply_op = affine.ApplyOp(
544 map_operands=dim_values + symbol_values,
545 affine_map=builtin.AffineMapAttr(map),
547 inner_dimension_op = arith.ConstantOp(
548 builtin.IntegerAttr(1, builtin.IndexType())
550 inner_size_op = memref.DimOp(
551 memref_ssa_value, inner_dimension_op.result
553 subview_op = memref.SubviewOp.get(
554 source=memref_ssa_value,
555 offsets=[index_apply_op.result, 0],
556 sizes=[1, inner_size_op.result],
558 result_type=builtin.MemRefType(
559 memref_ssa_value.type.element_type,
560 [builtin.DYNAMIC_INDEX],
561 builtin.StridedLayoutAttr([1],
None),
564 self.
builders[-1].insert(index_apply_op)
565 self.
builders[-1].insert(inner_dimension_op)
566 self.
builders[-1].insert(inner_size_op)
567 self.
builders[-1].insert(subview_op)
568 return subview_op.result
570 if subscript.get_address:
571 index_apply_op = affine.ApplyOp(
572 map_operands=dim_values + symbol_values,
573 affine_map=builtin.AffineMapAttr(map),
575 dimension_index_op = arith.ConstantOp(
576 builtin.IntegerAttr(0, builtin.IndexType())
578 dimension_size_op = memref.DimOp(
579 memref_ssa_value, dimension_index_op.result
581 remaining_size_op = arith.SubiOp(
582 dimension_size_op.result, index_apply_op.result
584 subview_op = memref.SubviewOp.get(
585 source=memref_ssa_value,
586 offsets=[index_apply_op.result],
587 sizes=[remaining_size_op.result],
589 result_type=builtin.MemRefType(
590 memref_ssa_value.type.element_type,
591 [builtin.DYNAMIC_INDEX],
592 builtin.StridedLayoutAttr([1],
None),
595 self.
builders[-1].insert(index_apply_op)
596 self.
builders[-1].insert(dimension_index_op)
597 self.
builders[-1].insert(dimension_size_op)
598 self.
builders[-1].insert(remaining_size_op)
599 self.
builders[-1].insert(subview_op)
600 return subview_op.result
602 load_op = affine.LoadOp(memref_ssa_value, indices=dim_values + symbol_values, map = builtin.AffineMapAttr(map))
604 return load_op.result
606 if len(subscript.indices) == 2
and subscript.get_address:
607 if isinstance(subscript.base, Variable):
608 base = subscript.base.get_value()
610 base = subscript.base
611 assert(isinstance(base, Array))
612 memref_ssa_value = subscript.base.print(self)
613 assert(isinstance(memref_ssa_value, SSAValue))
614 assert(isinstance(memref_ssa_value.type, builtin.MemRefType))
621 map_operands = dim_values + symbol_values
622 outer_map = AffineMap(
625 results=(outer_index_map,),
627 inner_map = AffineMap(
630 results=(inner_index_map,),
632 outer_apply_op = affine.ApplyOp(
633 map_operands=map_operands,
634 affine_map=builtin.AffineMapAttr(outer_map),
636 inner_apply_op = affine.ApplyOp(
637 map_operands=map_operands,
638 affine_map=builtin.AffineMapAttr(inner_map),
640 self.
builders[-1].insert(outer_apply_op)
641 self.
builders[-1].insert(inner_apply_op)
644 assert(len(memref_ssa_value.type.get_shape()) == 2)
645 dimension_index_op = arith.ConstantOp(
646 builtin.IntegerAttr(1, builtin.IndexType())
648 dimension_size_op = memref.DimOp(
649 memref_ssa_value, dimension_index_op.result
651 remaining_size_op = arith.SubiOp(
652 dimension_size_op.result, inner_apply_op.result
654 subview_op = memref.SubviewOp.get(
655 source=memref_ssa_value,
656 offsets=[outer_apply_op.result, inner_apply_op.result],
657 sizes=[1, remaining_size_op.result],
659 result_type=builtin.MemRefType(
660 memref_ssa_value.type.element_type,
661 [builtin.DYNAMIC_INDEX],
662 builtin.StridedLayoutAttr([1],
None),
665 self.
builders[-1].insert(dimension_index_op)
666 self.
builders[-1].insert(dimension_size_op)
667 self.
builders[-1].insert(remaining_size_op)
668 self.
builders[-1].insert(subview_op)
669 return subview_op.result
672 load_op = affine.LoadOp(
674 indices=map_operands,
675 map=builtin.AffineMapAttr(outer_map),
678 inner_memref_ssa_value = load_op.result
679 assert(isinstance(inner_memref_ssa_value.type, builtin.MemRefType))
680 dimension_index_op = arith.ConstantOp(
681 builtin.IntegerAttr(0, builtin.IndexType())
683 dimension_size_op = memref.DimOp(
684 inner_memref_ssa_value, dimension_index_op.result
686 remaining_size_op = arith.SubiOp(
687 dimension_size_op.result, inner_apply_op.result
689 subview_op = memref.SubviewOp.get(
690 source=inner_memref_ssa_value,
691 offsets=[inner_apply_op.result],
692 sizes=[remaining_size_op.result],
694 result_type=builtin.MemRefType(
695 inner_memref_ssa_value.type.element_type,
696 [builtin.DYNAMIC_INDEX],
697 builtin.StridedLayoutAttr([1],
None),
700 self.
builders[-1].insert(dimension_index_op)
701 self.
builders[-1].insert(dimension_size_op)
702 self.
builders[-1].insert(remaining_size_op)
703 self.
builders[-1].insert(subview_op)
704 return subview_op.result
706 if len(subscript.indices) == 2
and not subscript.get_address:
707 memref_ssa_value = subscript.base.print(self)
708 assert(isinstance(memref_ssa_value, SSAValue))
709 assert(isinstance(memref_ssa_value.type, builtin.MemRefType))
716 if subscript.flatten:
718 load_op = affine.LoadOp(memref_ssa_value, indices=dim_values + symbol_values, map = builtin.AffineMapAttr(map))
720 return load_op.result
724 outer_load_op = affine.LoadOp(
726 indices=dim_values + symbol_values,
727 map=builtin.AffineMapAttr(outer_map),
729 self.
builders[-1].insert(outer_load_op)
730 inner_load_op = affine.LoadOp(
731 outer_load_op.result,
732 indices=dim_values + symbol_values,
733 map=builtin.AffineMapAttr(inner_map),
735 self.
builders[-1].insert(inner_load_op)
736 return inner_load_op.result
740 lhs = element_binary_operation.lhs.print(self)
741 rhs = element_binary_operation.rhs.print(self)
743 assert(isinstance(lhs, SSAValue))
744 assert(isinstance(rhs, SSAValue))
745 if lhs.type == builtin.IntegerType(64):
746 cast_op = arith.SIToFPOp(lhs, builtin.Float64Type())
749 if lhs.type == builtin.IndexType():
750 cast_op = arith.UIToFPOp(lhs, builtin.Float64Type())
753 if rhs.type == builtin.IntegerType(64):
754 cast_op = arith.SIToFPOp(rhs, builtin.Float64Type())
757 if rhs.type == builtin.IndexType():
758 cast_op = arith.UIToFPOp(rhs, builtin.Float64Type())
763 match element_binary_operation.operator:
764 case Operator.ADDITION:
765 arith_op = arith.AddfOp(lhs, rhs)
766 case Operator.SUBTRACTION:
767 arith_op = arith.SubfOp(lhs, rhs)
768 case Operator.MULTIPLICATION:
769 arith_op = arith.MulfOp(lhs, rhs)
770 case Operator.DIVISION:
771 arith_op = arith.DivfOp(lhs, rhs)
773 arith_op = arith.MaximumfOp(lhs, rhs)
776 return arith_op.result