Skip to content

[mlir][vector] Fix attaching write effects on transfer_write's base #142940

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions flang/test/HLFIR/assign-side-effects.fir
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
// RUN: fir-opt %s --test-side-effects --verify-diagnostics

func.func @test1(%x: !fir.ref<i32>, %i: i32) {
// expected-remark @below {{found an instance of 'write' on a op operand, on resource '<Default>'}}
// expected-remark @below {{found an instance of 'write' on op operand 1, on resource '<Default>'}}
hlfir.assign %i to %x : i32, !fir.ref<i32>
return
}

func.func @test2(%x: !fir.ref<i32>, %y: !fir.ref<i32>) {
// expected-remark @below {{found an instance of 'write' on a op operand, on resource '<Default>'}}
// expected-remark @below {{found an instance of 'read' on a op operand, on resource '<Default>'}}
// expected-remark @below {{found an instance of 'write' on op operand 1, on resource '<Default>'}}
// expected-remark @below {{found an instance of 'read' on op operand 0, on resource '<Default>'}}
hlfir.assign %y to %x : !fir.ref<i32>, !fir.ref<i32>
return
}
Expand All @@ -22,7 +22,8 @@ func.func @test3(%x: !fir.ref<!fir.type<t>>, %y: !fir.ref<!fir.type<t>>) {
}

func.func @test4(%x: !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>, %y: !fir.box<!fir.array<?xi32>>) {
// expected-remark @below {{found an instance of 'read' on a op operand, on resource '<Default>'}}
// expected-remark @below {{found an instance of 'read' on op operand 0, on resource '<Default>'}}
// expected-remark @below {{found an instance of 'read' on op operand 1, on resource '<Default>'}}
// expected-remark @below {{found an instance of 'write' on resource '<Default>'}}
// expected-remark @below {{found an instance of 'free' on resource '<Default>'}}
// expected-remark @below {{found an instance of 'allocate' on resource '<Default>'}}
Expand Down
93 changes: 47 additions & 46 deletions flang/test/HLFIR/memory-effects.fir
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
func.func @concat(%arg0: !fir.ref<!fir.char<1,10>>, %arg1: !fir.ref<!fir.char<1, 20>>) {
// expected-remark@+1 {{operation has no memory effects}}
%c30 = arith.constant 30 : index
// expected-remark@+2 {{found an instance of 'allocate' on a op result, on resource '<Default>'}}
// expected-remark@+1 {{found an instance of 'read' on a op operand, on resource '<Default>'}}
// expected-remark@+3 {{found an instance of 'allocate' on op result 0, on resource '<Default>'}}
// expected-remark@+2 {{found an instance of 'read' on op operand 0, on resource '<Default>'}}
// expected-remark@+1 {{found an instance of 'read' on op operand 1, on resource '<Default>'}}
%0 = hlfir.concat %arg0, %arg1 len %c30 : (!fir.ref<!fir.char<1,10>>, !fir.ref<!fir.char<1,20>>, index) -> (!hlfir.expr<!fir.char<1,30>>)
return
}
Expand All @@ -16,8 +17,8 @@ func.func @all_no_effects(%arg0: !hlfir.expr<2x!fir.logical<4>>) {
}

func.func @all_effects(%arg0: !fir.ref<!fir.array<2x10x!fir.logical<4>>>, %arg1: i32) {
// expected-remark@+2 {{found an instance of 'allocate' on a op result, on resource '<Default>'}}
// expected-remark@+1 {{found an instance of 'read' on a op operand, on resource '<Default>'}}
// expected-remark@+2 {{found an instance of 'allocate' on op result 0, on resource '<Default>'}}
// expected-remark@+1 {{found an instance of 'read' on op operand 0, on resource '<Default>'}}
%all = hlfir.all %arg0 dim %arg1 : (!fir.ref<!fir.array<2x10x!fir.logical<4>>>, i32) -> !hlfir.expr<?x!fir.logical<4>>
return
}
Expand All @@ -29,8 +30,8 @@ func.func @any_no_effects(%arg0: !hlfir.expr<2x!fir.logical<4>>) {
}

func.func @any_effects(%arg0: !fir.ref<!fir.array<2x10x!fir.logical<4>>>, %arg1: i32) {
// expected-remark@+2 {{found an instance of 'allocate' on a op result, on resource '<Default>'}}
// expected-remark@+1 {{found an instance of 'read' on a op operand, on resource '<Default>'}}
// expected-remark@+2 {{found an instance of 'allocate' on op result 0, on resource '<Default>'}}
// expected-remark@+1 {{found an instance of 'read' on op operand 0, on resource '<Default>'}}
%all = hlfir.any %arg0 dim %arg1 : (!fir.ref<!fir.array<2x10x!fir.logical<4>>>, i32) -> !hlfir.expr<?x!fir.logical<4>>
return
}
Expand All @@ -42,7 +43,7 @@ func.func @count_no_effects(%arg0: !hlfir.expr<2x!fir.logical<4>>) {
}

func.func @count_effects(%arg0: !fir.ref<!fir.array<2x10x!fir.logical<4>>>, %arg1: i32) {
// expected-remark@+1 {{found an instance of 'read' on a op operand, on resource '<Default>'}}
// expected-remark@+1 {{found an instance of 'read' on op operand 0, on resource '<Default>'}}
%all = hlfir.count %arg0 dim %arg1 : (!fir.ref<!fir.array<2x10x!fir.logical<4>>>, i32) -> i32
return
}
Expand All @@ -54,15 +55,15 @@ func.func @product_no_effects(%arg0: !hlfir.expr<?xf32>) {
}

func.func @product_effects(%arg0: !fir.ref<!fir.array<2x2xf32>>, %arg1: i32) {
// expected-remark@+2 {{found an instance of 'allocate' on a op result, on resource '<Default>'}}
// expected-remark@+1 {{found an instance of 'read' on a op operand, on resource '<Default>'}}
// expected-remark@+2 {{found an instance of 'allocate' on op result 0, on resource '<Default>'}}
// expected-remark@+1 {{found an instance of 'read' on op operand 0, on resource '<Default>'}}
%product = hlfir.product %arg0 dim %arg1 : (!fir.ref<!fir.array<2x2xf32>>, i32) -> !hlfir.expr<2xf32>
return
}

func.func @set_length_read(%arg0: !fir.ref<!fir.char<1,10>>, %arg1: index) {
// expected-remark@+2 {{found an instance of 'allocate' on a op result, on resource '<Default>'}}
// expected-remark@+1 {{found an instance of 'read' on a op operand, on resource '<Default>'}}
// expected-remark@+2 {{found an instance of 'allocate' on op result 0, on resource '<Default>'}}
// expected-remark@+1 {{found an instance of 'read' on op operand 0, on resource '<Default>'}}
%0 = hlfir.set_length %arg0 len %arg1 : (!fir.ref<!fir.char<1,10>>, index) -> !hlfir.expr<!fir.char<1,?>>
return
}
Expand All @@ -74,8 +75,8 @@ func.func @sum_no_effects(%arg0: !hlfir.expr<?xf32>) {
}

func.func @sum_effects(%arg0: !fir.ref<!fir.array<2x2xf32>>, %arg1: i32) {
// expected-remark@+2 {{found an instance of 'allocate' on a op result, on resource '<Default>'}}
// expected-remark@+1 {{found an instance of 'read' on a op operand, on resource '<Default>'}}
// expected-remark@+2 {{found an instance of 'allocate' on op result 0, on resource '<Default>'}}
// expected-remark@+1 {{found an instance of 'read' on op operand 0, on resource '<Default>'}}
%sum = hlfir.sum %arg0 dim %arg1 : (!fir.ref<!fir.array<2x2xf32>>, i32) -> !hlfir.expr<2xf32>
return
}
Expand All @@ -87,8 +88,8 @@ func.func @maxval_no_effects(%arg0: !hlfir.expr<?xf32>) {
}

func.func @maxval_effects(%arg0: !fir.ref<!fir.array<2x2xf32>>, %arg1: i32) {
// expected-remark@+2 {{found an instance of 'allocate' on a op result, on resource '<Default>'}}
// expected-remark@+1 {{found an instance of 'read' on a op operand, on resource '<Default>'}}
// expected-remark@+2 {{found an instance of 'allocate' on op result 0, on resource '<Default>'}}
// expected-remark@+1 {{found an instance of 'read' on op operand 0, on resource '<Default>'}}
%maxval = hlfir.maxval %arg0 dim %arg1 : (!fir.ref<!fir.array<2x2xf32>>, i32) -> !hlfir.expr<2xf32>
return
}
Expand All @@ -100,34 +101,34 @@ func.func @minval_no_effects(%arg0: !hlfir.expr<?xf32>) {
}

func.func @minval_effects(%arg0: !fir.ref<!fir.array<2x2xf32>>, %arg1: i32) {
// expected-remark@+2 {{found an instance of 'allocate' on a op result, on resource '<Default>'}}
// expected-remark@+1 {{found an instance of 'read' on a op operand, on resource '<Default>'}}
// expected-remark@+2 {{found an instance of 'allocate' on op result 0, on resource '<Default>'}}
// expected-remark@+1 {{found an instance of 'read' on op operand 0, on resource '<Default>'}}
%minval = hlfir.minval %arg0 dim %arg1 : (!fir.ref<!fir.array<2x2xf32>>, i32) -> !hlfir.expr<2xf32>
return
}

func.func @minloc_effects_simple(%arg0: !hlfir.expr<?xf32>) {
// expected-remark@+1 {{found an instance of 'allocate' on a op result, on resource '<Default>'}}
// expected-remark@+1 {{found an instance of 'allocate' on op result 0, on resource '<Default>'}}
%minloc = hlfir.minloc %arg0 : (!hlfir.expr<?xf32>) -> !hlfir.expr<?xi32>
return
}

func.func @minloc_effects(%arg0: !fir.ref<!fir.array<2x2xf32>>, %arg1: i32) {
// expected-remark@+2 {{found an instance of 'allocate' on a op result, on resource '<Default>'}}
// expected-remark@+1 {{found an instance of 'read' on a op operand, on resource '<Default>'}}
// expected-remark@+2 {{found an instance of 'allocate' on op result 0, on resource '<Default>'}}
// expected-remark@+1 {{found an instance of 'read' on op operand 0, on resource '<Default>'}}
%minloc = hlfir.minloc %arg0 dim %arg1 : (!fir.ref<!fir.array<2x2xf32>>, i32) -> !hlfir.expr<2xi32>
return
}

func.func @maxloc_effects_simple(%arg0: !hlfir.expr<?xf32>) {
// expected-remark@+1 {{found an instance of 'allocate' on a op result, on resource '<Default>'}}
// expected-remark@+1 {{found an instance of 'allocate' on op result 0, on resource '<Default>'}}
%maxloc = hlfir.maxloc %arg0 : (!hlfir.expr<?xf32>) -> !hlfir.expr<?xi32>
return
}

func.func @maxloc_effects(%arg0: !fir.ref<!fir.array<2x2xf32>>, %arg1: i32) {
// expected-remark@+2 {{found an instance of 'allocate' on a op result, on resource '<Default>'}}
// expected-remark@+1 {{found an instance of 'read' on a op operand, on resource '<Default>'}}
// expected-remark@+2 {{found an instance of 'allocate' on op result 0, on resource '<Default>'}}
// expected-remark@+1 {{found an instance of 'read' on op operand 0, on resource '<Default>'}}
%maxloc = hlfir.maxloc %arg0 dim %arg1 : (!fir.ref<!fir.array<2x2xf32>>, i32) -> !hlfir.expr<2xi32>
return
}
Expand All @@ -139,49 +140,49 @@ func.func @dot_product_no_effects(%arg0: !hlfir.expr<?xf32>, %arg1: !hlfir.expr<
}

func.func @dot_product_effects(%arg0: !fir.ref<!fir.array<10xf32>>, %arg1: !fir.ref<!fir.array<10xf32>>) {
// there are read effects on both arguments - the diagnostic verification just doesn't register duplicate identical diagnostics
// expected-remark@+1 {{found an instance of 'read' on a op operand, on resource '<Default>'}}
// expected-remark@+2 {{found an instance of 'read' on op operand 0, on resource '<Default>'}}
// expected-remark@+1 {{found an instance of 'read' on op operand 1, on resource '<Default>'}}
%0 = hlfir.dot_product %arg0 %arg1 : (!fir.ref<!fir.array<10xf32>>, !fir.ref<!fir.array<10xf32>>) -> f32
return
}

func.func @matmul_no_reads(%arg0: !hlfir.expr<?x?xf32>, %arg1: !hlfir.expr<?x?xf32>) {
// expected-remark@+1 {{found an instance of 'allocate' on a op result, on resource '<Default>'}}
// expected-remark@+1 {{found an instance of 'allocate' on op result 0, on resource '<Default>'}}
%0 = hlfir.matmul %arg0 %arg1 : (!hlfir.expr<?x?xf32>, !hlfir.expr<?x?xf32>) -> !hlfir.expr<?x?xf32>
return
}

func.func @matmul_reads(%arg0: !fir.ref<!fir.array<10x5xf32>>, %arg1: !fir.ref<!fir.array<5x10xf32>>) {
// expected-remark@+3 {{found an instance of 'allocate' on a op result, on resource '<Default>'}}
// there are read effects on both arguments - the diagnostic verification just doesn't register duplicate identical diagnostics
// expected-remark@+1 {{found an instance of 'read' on a op operand, on resource '<Default>'}}
// expected-remark@+3 {{found an instance of 'allocate' on op result 0, on resource '<Default>'}}
// expected-remark@+2 {{found an instance of 'read' on op operand 0, on resource '<Default>'}}
// expected-remark@+1 {{found an instance of 'read' on op operand 1, on resource '<Default>'}}
%0 = hlfir.matmul %arg0 %arg1 : (!fir.ref<!fir.array<10x5xf32>>, !fir.ref<!fir.array<5x10xf32>>) -> !hlfir.expr<10x10xf32>
return
}

func.func @transpose_no_reads(%arg0: !hlfir.expr<?x?xf32>) {
// expected-remark@+1 {{found an instance of 'allocate' on a op result, on resource '<Default>'}}
// expected-remark@+1 {{found an instance of 'allocate' on op result 0, on resource '<Default>'}}
%0 = hlfir.transpose %arg0 : (!hlfir.expr<?x?xf32>) -> !hlfir.expr<?x?xf32>
return
}

func.func @transpose_read(%arg0: !fir.ref<!fir.array<10x5xf32>>) {
// expected-remark@+2 {{found an instance of 'allocate' on a op result, on resource '<Default>'}}
// expected-remark@+1 {{found an instance of 'read' on a op operand, on resource '<Default>'}}
// expected-remark@+2 {{found an instance of 'allocate' on op result 0, on resource '<Default>'}}
// expected-remark@+1 {{found an instance of 'read' on op operand 0, on resource '<Default>'}}
%0 = hlfir.transpose %arg0 : (!fir.ref<!fir.array<10x5xf32>>) -> !hlfir.expr<5x10xf32>
return
}

func.func @matmul_transpose_no_reads(%arg0: !hlfir.expr<?x?xf32>, %arg1: !hlfir.expr<?x?xf32>) {
// expected-remark@+1 {{found an instance of 'allocate' on a op result, on resource '<Default>'}}
// expected-remark@+1 {{found an instance of 'allocate' on op result 0, on resource '<Default>'}}
%0 = hlfir.matmul_transpose %arg0 %arg1 : (!hlfir.expr<?x?xf32>, !hlfir.expr<?x?xf32>) -> !hlfir.expr<?x?xf32>
return
}

func.func @matmul_transpose_reads(%arg0: !fir.ref<!fir.array<5x10xf32>>, %arg1: !fir.ref<!fir.array<5x10xf32>>) {
// expected-remark@+3 {{found an instance of 'allocate' on a op result, on resource '<Default>'}}
// there are read effects on both arguments - the diagnostic verification just doesn't register duplicate identical diagnostics
// expected-remark@+1 {{found an instance of 'read' on a op operand, on resource '<Default>'}}
// expected-remark@+3 {{found an instance of 'allocate' on op result 0, on resource '<Default>'}}
// expected-remark@+2 {{found an instance of 'read' on op operand 0, on resource '<Default>'}}
// expected-remark@+1 {{found an instance of 'read' on op operand 1, on resource '<Default>'}}
%0 = hlfir.matmul_transpose %arg0 %arg1 : (!fir.ref<!fir.array<5x10xf32>>, !fir.ref<!fir.array<5x10xf32>>) -> !hlfir.expr<10x10xf32>
return
}
Expand All @@ -195,37 +196,37 @@ func.func @associate(%arg0: i32) {
}

func.func @as_expr_read(%arg0: !fir.ref<!fir.array<2xi32>>) {
// expected-remark@+2 {{found an instance of 'allocate' on a op result, on resource '<Default>'}}
// expected-remark@+1 {{found an instance of 'read' on a op operand, on resource '<Default>'}}
// expected-remark@+2 {{found an instance of 'allocate' on op result 0, on resource '<Default>'}}
// expected-remark@+1 {{found an instance of 'read' on op operand 0, on resource '<Default>'}}
%0 = hlfir.as_expr %arg0 : (!fir.ref<!fir.array<2xi32>>) -> !hlfir.expr<?xi32>
// expected-remark@+1 {{found an instance of 'free' on resource '<Default>'}}
hlfir.destroy %0 : !hlfir.expr<?xi32>
return
}

func.func @char_extremum(%arg0: !fir.ref<!fir.char<1,10>>, %arg1: !fir.ref<!fir.char<1,20>>) {
// expected-remark@+3 {{found an instance of 'allocate' on a op result, on resource '<Default>'}}
// there are read effects on both arguments - the diagnostic verification just doesn't register duplicate identical diagnostics
// expected-remark@+1 {{found an instance of 'read' on a op operand, on resource '<Default>'}}
// expected-remark@+3 {{found an instance of 'allocate' on op result 0, on resource '<Default>'}}
// expected-remark@+2 {{found an instance of 'read' on op operand 0, on resource '<Default>'}}
// expected-remark@+1 {{found an instance of 'read' on op operand 1, on resource '<Default>'}}
%0 = hlfir.char_extremum min, %arg0, %arg1 : (!fir.ref<!fir.char<1, 10>>, !fir.ref<!fir.char<1,20>>) -> !hlfir.expr<!fir.char<1,10>>
return
}

func.func @copy_in(%box: !fir.box<!fir.array<?xf64>>, %temp: !fir.ref<!fir.box<!fir.heap<!fir.array<?xf64>>>>, %is_present: i1) {
// expected-remark@+3 {{found an instance of 'allocate' on resource '<Default>'}}
// expected-remark@+2 {{found an instance of 'read' on a op operand, on resource '<Default>'}}
// expected-remark@+1 {{found an instance of 'write' on a op operand, on resource '<Default>'}}
// expected-remark@+2 {{found an instance of 'read' on op operand 0, on resource '<Default>'}}
// expected-remark@+1 {{found an instance of 'write' on op operand 1, on resource '<Default>'}}
%0:2 = hlfir.copy_in %box to %temp : (!fir.box<!fir.array<?xf64>>, !fir.ref<!fir.box<!fir.heap<!fir.array<?xf64>>>>) -> (!fir.box<!fir.array<?xf64>>, i1)
return
}

func.func @copy_out(%box: !fir.box<!fir.array<?xf64>>, %temp: !fir.ref<!fir.box<!fir.heap<!fir.array<?xf64>>>>, %was_copied: i1) {
// expected-remark@+2 {{found an instance of 'free' on resource '<Default>'}}
// expected-remark@+1 {{found an instance of 'read' on a op operand, on resource '<Default>'}}
// expected-remark@+1 {{found an instance of 'read' on op operand 0, on resource '<Default>'}}
hlfir.copy_out %temp, %was_copied : (!fir.ref<!fir.box<!fir.heap<!fir.array<?xf64>>>>, i1) -> ()
// expected-remark@+3 {{found an instance of 'free' on resource '<Default>'}}
// expected-remark@+2 {{found an instance of 'read' on a op operand, on resource '<Default>'}}
// expected-remark@+1 {{found an instance of 'write' on a op operand, on resource '<Default>'}}
// expected-remark@+2 {{found an instance of 'read' on op operand 0, on resource '<Default>'}}
// expected-remark@+1 {{found an instance of 'write' on op operand 2, on resource '<Default>'}}
hlfir.copy_out %temp, %was_copied to %box : (!fir.ref<!fir.box<!fir.heap<!fir.array<?xf64>>>>, i1, !fir.box<!fir.array<?xf64>>) -> ()
return
}
2 changes: 1 addition & 1 deletion mlir/lib/Dialect/Vector/IR/VectorOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5034,7 +5034,7 @@ void TransferWriteOp::getEffects(
SmallVectorImpl<SideEffects::EffectInstance<MemoryEffects::Effect>>
&effects) {
if (llvm::isa<MemRefType>(getShapedType()))
effects.emplace_back(MemoryEffects::Write::get(), &getValueToStoreMutable(),
effects.emplace_back(MemoryEffects::Write::get(), &getBaseMutable(),
SideEffects::DefaultResource::get());
}

Expand Down
6 changes: 3 additions & 3 deletions mlir/test/Dialect/Bufferization/side-effects.mlir
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// RUN: mlir-opt %s --test-side-effects --verify-diagnostics

func.func @test_side_effects(%arg0: memref<2xi32>) -> memref<2xi32> {
// expected-remark @below {{found an instance of 'read' on a op operand, on resource '<Default>'}}
// expected-remark @below {{found an instance of 'write' on a op result, on resource '<Default>'}}
// expected-remark @below {{found an instance of 'allocate' on a op result, on resource '<Default>'}}
// expected-remark @below {{found an instance of 'read' on op operand 0, on resource '<Default>'}}
// expected-remark @below {{found an instance of 'write' on op result 0, on resource '<Default>'}}
// expected-remark @below {{found an instance of 'allocate' on op result 0, on resource '<Default>'}}
%0 = bufferization.clone %arg0 : memref<2xi32> to memref<2xi32>
return %0 : memref<2xi32>
}
15 changes: 15 additions & 0 deletions mlir/test/Dialect/Vector/side-effects.mlir
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// RUN: mlir-opt %s --test-side-effects --verify-diagnostics

func.func @test_side_effects(%arg0: memref<8xf32>) {
// expected-remark @below {{operation has no memory effects}}
%c0 = arith.constant 0 : index
// expected-remark @below {{operation has no memory effects}}
%c4 = arith.constant 4 : index
// expected-remark @below {{operation has no memory effects}}
%cst = arith.constant 0.0 : f32
// expected-remark @below {{found an instance of 'read' on op operand 0, on resource '<Default>'}}
%0 = vector.transfer_read %arg0[%c0], %cst : memref<8xf32>, vector<4xf32>
// expected-remark @below {{found an instance of 'write' on op operand 1, on resource '<Default>'}}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without the changes to the TransferWriteOp::getEffects(...) function the write effect would be attached to op operand 0 rather than on 1.

vector.transfer_write %0, %arg0[%c4] : vector<4xf32>, memref<8xf32>
return
}
Loading
Loading