Skip to content

[Flang][OpenMP] Fix to resolve the crash with SIMD aligned clause. #150612

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 0 additions & 2 deletions flang/lib/Lower/OpenMP/ClauseProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -649,8 +649,6 @@ addAlignedClause(lower::AbstractConverter &converter,
// Do not generate alignment assumption if alignment is less than or equal to
// 0.
if (alignment > 0) {
// alignment value must be power of 2
assert((alignment & (alignment - 1)) == 0 && "alignment is not power of 2");
auto &objects = std::get<omp::ObjectList>(clause.t);
if (!objects.empty())
genObjectList(objects, converter, alignedVars);
Expand Down
17 changes: 17 additions & 0 deletions flang/test/Lower/OpenMP/simd.f90
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,23 @@ subroutine simdloop_aligned_allocatable()
end do
end subroutine

subroutine aligned_non_power_of_two()
integer :: i
integer, allocatable :: A(:)
allocate(A(10))
!CHECK: %[[A_PTR:.*]] = fir.alloca !fir.box<!fir.heap<!fir.array<?xi32>>> {bindc_name = "a",
!CHECK-SAME: uniq_name = "_QFaligned_non_power_of_twoEa"}
!CHECK: %[[A_DECL:.*]]:2 = hlfir.declare %[[A_PTR]] {fortran_attrs = #fir.var_attrs<allocatable>,
!CHECK-SAME: uniq_name = "_QFaligned_non_power_of_twoEa"} :
!CHECK-SAME: (!fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>) ->
!CHECK-SAME: (!fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>, !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>)
!CHECK: omp.simd aligned(%[[A_DECL]]#0 : !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>> -> 257 : i64)
!$OMP SIMD ALIGNED(A:257)
do i = 1, 10
A(i) = i
end do
end subroutine

!CHECK-LABEL: func @_QPsimd_with_nontemporal_clause
subroutine simd_with_nontemporal_clause(n)
!CHECK: %[[A_DECL:.*]]:2 = hlfir.declare %{{.*}} {uniq_name = "_QFsimd_with_nontemporal_clauseEa"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2893,6 +2893,15 @@ convertOmpSimd(Operation &opInst, llvm::IRBuilderBase &builder,
alignment = builder.getInt64(intAttr.getInt());
assert(ty->isPointerTy() && "Invalid type for aligned variable");
assert(alignment && "Invalid alignment value");
// Check if the alignment value is not a power of 2. If so, skip emitting
// alignment.
if (!intAttr.getValue().isPowerOf2()) {
emitWarning(simdOp->getLoc())
<< "The specified alignment value, " << intAttr.getInt()
<< " is not a power of two and will be ignored";
Comment on lines +2899 to +2901
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you add a test for this warning as well? There are similar tests in mlir/test/Target/LLVMIR/openmp-todo.mlir.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thankyou for pointing out the file. I have added a testcase for this scenario.

continue;
}

auto curInsert = builder.saveIP();
builder.SetInsertPoint(sourceBlock);
llvmVal = builder.CreateLoad(ty, llvmVal);
Expand Down
22 changes: 22 additions & 0 deletions mlir/test/Target/LLVMIR/openmp-simd-aligned.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,25 @@ llvm.func @_QPsimd_aligned_allocatable() {
}
llvm.return
}

//CHECK-LABEL: define void @_QPsimd_aligned_non_power_of_two() {
//CHECK: %[[A_ADDR:.*]] = alloca { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, i64 1, align 8
//CHECK: %[[B_ADDR:.*]] = alloca { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, i64 1, align 8
//CHECK: %[[LOAD_B:.*]] = load ptr, ptr %[[B_ADDR]], align 8
//CHECK: call void @llvm.assume(i1 true) [ "align"(ptr %[[LOAD_B]], i64 64) ]
//CHECK-NOT: call void @llvm.assume(i1 true) [ "align"(ptr %{{.*}}, i64 257) ]
llvm.func @_QPsimd_aligned_non_power_of_two() {
%0 = llvm.mlir.constant(1 : i64) : i64
%1 = llvm.alloca %0 x !llvm.struct<(ptr, i64, i32, i8, i8, i8, i8, array<1 x array<3 x i64>>)> {bindc_name = "a"} : (i64) -> !llvm.ptr
%2 = llvm.alloca %0 x !llvm.struct<(ptr, i64, i32, i8, i8, i8, i8, array<1 x array<3 x i64>>)> {bindc_name = "b"} : (i64) -> !llvm.ptr
%3 = llvm.mlir.constant(1 : i32) : i32
%4 = llvm.mlir.constant(10 : i32) : i32
%5 = llvm.mlir.constant(1 : i32) : i32
omp.simd aligned(%1 : !llvm.ptr -> 257 : i64, %2 : !llvm.ptr -> 64 : i64) {
omp.loop_nest (%arg0) : i32 = (%3) to (%4) inclusive step (%5) {
omp.yield
}
}
llvm.return
}

11 changes: 11 additions & 0 deletions mlir/test/Target/LLVMIR/openmp-todo.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,17 @@ llvm.func @simd_linear(%lb : i32, %ub : i32, %step : i32, %x : !llvm.ptr) {

// -----

llvm.func @omp_simd_non_power_of_two_alignment(%lb : i32, %ub : i32, %step : i32, %x : !llvm.ptr) {
// expected-warning@below {{The specified alignment value, 257 is not a power of two and will be ignored}}
omp.simd aligned(%x : !llvm.ptr -> 257 : i64) {
omp.loop_nest (%iv) : i32 = (%lb) to (%ub) step (%step) {
omp.yield
}
}
llvm.return
}
// -----

omp.declare_reduction @add_f32 : f32
init {
^bb0(%arg: f32):
Expand Down