@@ -32,46 +32,50 @@ def apply(
3232 state : transform .TransformState ,
3333 ) -> DiagnosedSilenceableFailure :
3434 # Get the target operations to transform
35- target_op = state .get_payload_ops (op .target )[ 0 ]
35+ target_ops = state .get_payload_ops (op .target )
3636 # Get the address space value from the attribute
3737 address_space_value = ir .IntegerAttr (op .address_space ).value
3838 new_ops = []
39+ for target_op in target_ops :
40+ # Verify this is a memref.alloca operation
41+ if target_op .OPERATION_NAME != "memref.alloca" :
42+ return DiagnosedSilenceableFailure .emit_silenceable_error (
43+ f"Expected memref.alloca operation, got { target_op .OPERATION_NAME } "
44+ )
3945
40- # Verify this is a memref.alloca operation
41- if target_op .OPERATION_NAME != "memref.alloca" :
42- return DiagnosedSilenceableFailure .emit_silenceable_error (
43- f"Expected memref.alloca operation, got { target_op .OPERATION_NAME } "
46+ # Get the current result type (should be a MemRefType)
47+ old_result_type = target_op .results [0 ].type
48+ memref_type = ir .MemRefType (old_result_type )
49+ # Create a new memref type with the specified address space
50+ new_memref_type = ir .MemRefType .get (
51+ memref_type .shape ,
52+ memref_type .element_type ,
53+ layout = memref_type .layout ,
54+ memory_space = ir .Attribute .parse (str (address_space_value )),
4455 )
4556
46- # Get the current result type (should be a MemRefType)
47- old_result_type = target_op .results [0 ].type
48- memref_type = ir .MemRefType (old_result_type )
49- # Create a new memref type with the specified address space
50- new_memref_type = ir .MemRefType .get (
51- memref_type .shape ,
52- memref_type .element_type ,
53- layout = memref_type .layout ,
54- memory_space = ir .Attribute .parse (f"{ address_space_value } " ),
55- )
56-
57- # Replace the operation with a new one that has the updated type
58- with ir .InsertionPoint (target_op ):
59- # Get the operands from the original alloca (dynamic sizes and symbols)
60- dynamic_sizes = list (
61- target_op .operands [: target_op .attributes ["operandSegmentSizes" ][0 ]]
62- )
63- symbol_operands = list (
64- target_op .operands [target_op .attributes ["operandSegmentSizes" ][0 ] :]
65- )
66- # Create a new alloca with the updated type
67- new_alloca = memref .alloca (
68- new_memref_type , dynamic_sizes , symbol_operands
69- )
70- # Replace all uses of the old operation with the new one
71- # FIXME: This won't handle operations that consume the memref type and
72- # return a new memref (such as subview).
73- rewriter .replace_op (target_op , [new_alloca ])
74- new_ops .append (new_alloca .owner )
57+ # Replace the operation with a new one that has the updated type
58+ with ir .InsertionPoint (target_op ):
59+ # Get the operands from the original alloca (dynamic sizes and symbols)
60+ dynamic_sizes = list (
61+ target_op .operands [
62+ : target_op .attributes ["operandSegmentSizes" ][0 ]
63+ ]
64+ )
65+ symbol_operands = list (
66+ target_op .operands [
67+ target_op .attributes ["operandSegmentSizes" ][0 ] :
68+ ]
69+ )
70+ # Create a new alloca with the updated type
71+ new_alloca = memref .alloca (
72+ new_memref_type , dynamic_sizes , symbol_operands
73+ )
74+ # Replace all uses of the old operation with the new one
75+ # FIXME: This won't handle operations that consume the memref type and
76+ # return a new memref (such as subview).
77+ rewriter .replace_op (target_op , [new_alloca ])
78+ new_ops .append (new_alloca .owner )
7579
7680 # Set the results to the new operations
7781 results .set_ops (op .updated_op , new_ops )
0 commit comments