Skip to content

[flang][OpenMP] Reintroduce TODO for FIR lowering of linear clause #144883

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 1 commit into
base: main
Choose a base branch
from

Conversation

NimishMishra
Copy link
Contributor

@NimishMishra NimishMishra commented Jun 19, 2025

Current design of the linear clause lowering and translation shifts all responsibility for handling the clause (like privatisation, linear stepping, finalisation, and emission of synchronisation barriers) to the IRBuilder. However in certain corner cases (like associated loops in or before OpenMP version 4.5), variables are are implicitly linear. This currently causes a problem with the existing linear clause implementation. Hence, re-introduce TODO on the linear clause until the linear clause lowering/translation are robust enough to handle such cases as well.

Fixes #142935

@llvmbot llvmbot added flang Flang issues not falling into any other category flang:fir-hlfir flang:openmp labels Jun 19, 2025
@llvmbot
Copy link
Member

llvmbot commented Jun 19, 2025

@llvm/pr-subscribers-flang-fir-hlfir

Author: None (NimishMishra)

Changes

Current design of the linear clause lowering and translation shifts all responsibility for handling the clause (like privatisation, linear stepping, finalisation, and emission of synchronisation barriers) to the IRBuilder. However in certain corner cases (like associated loops in or before OpenMP version 4.5), variables are are implicitly linear. This currently causes a problem with the existing linear clause implementation. Hence, re-introduce TODO on the linear clause until the linear clause lowering/translation are robust enough to handle such cases as well.

Fixes #142935


Full diff: https://github.com/llvm/llvm-project/pull/144883.diff

2 Files Affected:

  • (modified) flang/lib/Lower/OpenMP/OpenMP.cpp (+2-2)
  • (removed) flang/test/Lower/OpenMP/wsloop-linear.f90 (-57)
diff --git a/flang/lib/Lower/OpenMP/OpenMP.cpp b/flang/lib/Lower/OpenMP/OpenMP.cpp
index 3e865a1ee7185..b742179d45851 100644
--- a/flang/lib/Lower/OpenMP/OpenMP.cpp
+++ b/flang/lib/Lower/OpenMP/OpenMP.cpp
@@ -1979,13 +1979,13 @@ static void genWsloopClauses(
     llvm::SmallVectorImpl<const semantics::Symbol *> &reductionSyms) {
   ClauseProcessor cp(converter, semaCtx, clauses);
   cp.processNowait(clauseOps);
-  cp.processLinear(clauseOps);
   cp.processOrder(clauseOps);
   cp.processOrdered(clauseOps);
   cp.processReduction(loc, clauseOps, reductionSyms);
   cp.processSchedule(stmtCtx, clauseOps);
 
-  cp.processTODO<clause::Allocate>(loc, llvm::omp::Directive::OMPD_do);
+  cp.processTODO<clause::Allocate, clause::Linear>(
+      loc, llvm::omp::Directive::OMPD_do);
 }
 
 //===----------------------------------------------------------------------===//
diff --git a/flang/test/Lower/OpenMP/wsloop-linear.f90 b/flang/test/Lower/OpenMP/wsloop-linear.f90
deleted file mode 100644
index b99677108be2f..0000000000000
--- a/flang/test/Lower/OpenMP/wsloop-linear.f90
+++ /dev/null
@@ -1,57 +0,0 @@
-! This test checks lowering of OpenMP DO Directive (Worksharing)
-! with linear clause
-
-! RUN: %flang_fc1 -fopenmp -emit-hlfir %s -o - 2>&1 | FileCheck %s
-
-!CHECK: %[[X_alloca:.*]] = fir.alloca i32 {bindc_name = "x", uniq_name = "_QFsimple_linearEx"}
-!CHECK: %[[X:.*]]:2 = hlfir.declare %[[X_alloca]] {uniq_name = "_QFsimple_linearEx"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
-!CHECK: %[[const:.*]] = arith.constant 1 : i32
-subroutine simple_linear
-    implicit none
-    integer :: x, y, i
-    !CHECK: omp.wsloop linear(%[[X]]#0 = %[[const]] : !fir.ref<i32>) {{.*}}
-    !$omp do linear(x)
-    !CHECK: %[[LOAD:.*]] = fir.load %[[X]]#0 : !fir.ref<i32>
-    !CHECK: %[[const:.*]] = arith.constant 2 : i32
-    !CHECK: %[[RESULT:.*]] = arith.addi %[[LOAD]], %[[const]] : i32
-    do i = 1, 10
-        y = x + 2
-    end do
-    !$omp end do
-end subroutine
-
-
-!CHECK: %[[X_alloca:.*]] = fir.alloca i32 {bindc_name = "x", uniq_name = "_QFlinear_stepEx"}
-!CHECK: %[[X:.*]]:2 = hlfir.declare %[[X_alloca]] {uniq_name = "_QFlinear_stepEx"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
-subroutine linear_step
-    implicit none
-    integer :: x, y, i
-    !CHECK: %[[const:.*]] = arith.constant 4 : i32
-    !CHECK: omp.wsloop linear(%[[X]]#0 = %[[const]] : !fir.ref<i32>) {{.*}}
-    !$omp do linear(x:4)
-    !CHECK: %[[LOAD:.*]] = fir.load %[[X]]#0 : !fir.ref<i32>
-    !CHECK: %[[const:.*]] = arith.constant 2 : i32
-    !CHECK: %[[RESULT:.*]] = arith.addi %[[LOAD]], %[[const]] : i32   
-    do i = 1, 10
-        y = x + 2
-    end do
-    !$omp end do
-end subroutine
-
-!CHECK: %[[A_alloca:.*]] = fir.alloca i32 {bindc_name = "a", uniq_name = "_QFlinear_exprEa"}
-!CHECK: %[[A:.*]]:2 = hlfir.declare %[[A_alloca]] {uniq_name = "_QFlinear_exprEa"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
-!CHECK: %[[X_alloca:.*]] = fir.alloca i32 {bindc_name = "x", uniq_name = "_QFlinear_exprEx"}
-!CHECK: %[[X:.*]]:2 = hlfir.declare %[[X_alloca]] {uniq_name = "_QFlinear_exprEx"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
-subroutine linear_expr
-    implicit none
-    integer :: x, y, i, a
-    !CHECK: %[[LOAD_A:.*]] = fir.load %[[A]]#0 : !fir.ref<i32>
-    !CHECK: %[[const:.*]] = arith.constant 4 : i32
-    !CHECK: %[[LINEAR_EXPR:.*]] = arith.addi %[[LOAD_A]], %[[const]] : i32
-    !CHECK: omp.wsloop linear(%[[X]]#0 = %[[LINEAR_EXPR]] : !fir.ref<i32>) {{.*}}
-    !$omp do linear(x:a+4)
-    do i = 1, 10
-        y = x + 2
-    end do
-    !$omp end do
-end subroutine

@llvmbot
Copy link
Member

llvmbot commented Jun 19, 2025

@llvm/pr-subscribers-flang-openmp

Author: None (NimishMishra)

Changes

Current design of the linear clause lowering and translation shifts all responsibility for handling the clause (like privatisation, linear stepping, finalisation, and emission of synchronisation barriers) to the IRBuilder. However in certain corner cases (like associated loops in or before OpenMP version 4.5), variables are are implicitly linear. This currently causes a problem with the existing linear clause implementation. Hence, re-introduce TODO on the linear clause until the linear clause lowering/translation are robust enough to handle such cases as well.

Fixes #142935


Full diff: https://github.com/llvm/llvm-project/pull/144883.diff

2 Files Affected:

  • (modified) flang/lib/Lower/OpenMP/OpenMP.cpp (+2-2)
  • (removed) flang/test/Lower/OpenMP/wsloop-linear.f90 (-57)
diff --git a/flang/lib/Lower/OpenMP/OpenMP.cpp b/flang/lib/Lower/OpenMP/OpenMP.cpp
index 3e865a1ee7185..b742179d45851 100644
--- a/flang/lib/Lower/OpenMP/OpenMP.cpp
+++ b/flang/lib/Lower/OpenMP/OpenMP.cpp
@@ -1979,13 +1979,13 @@ static void genWsloopClauses(
     llvm::SmallVectorImpl<const semantics::Symbol *> &reductionSyms) {
   ClauseProcessor cp(converter, semaCtx, clauses);
   cp.processNowait(clauseOps);
-  cp.processLinear(clauseOps);
   cp.processOrder(clauseOps);
   cp.processOrdered(clauseOps);
   cp.processReduction(loc, clauseOps, reductionSyms);
   cp.processSchedule(stmtCtx, clauseOps);
 
-  cp.processTODO<clause::Allocate>(loc, llvm::omp::Directive::OMPD_do);
+  cp.processTODO<clause::Allocate, clause::Linear>(
+      loc, llvm::omp::Directive::OMPD_do);
 }
 
 //===----------------------------------------------------------------------===//
diff --git a/flang/test/Lower/OpenMP/wsloop-linear.f90 b/flang/test/Lower/OpenMP/wsloop-linear.f90
deleted file mode 100644
index b99677108be2f..0000000000000
--- a/flang/test/Lower/OpenMP/wsloop-linear.f90
+++ /dev/null
@@ -1,57 +0,0 @@
-! This test checks lowering of OpenMP DO Directive (Worksharing)
-! with linear clause
-
-! RUN: %flang_fc1 -fopenmp -emit-hlfir %s -o - 2>&1 | FileCheck %s
-
-!CHECK: %[[X_alloca:.*]] = fir.alloca i32 {bindc_name = "x", uniq_name = "_QFsimple_linearEx"}
-!CHECK: %[[X:.*]]:2 = hlfir.declare %[[X_alloca]] {uniq_name = "_QFsimple_linearEx"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
-!CHECK: %[[const:.*]] = arith.constant 1 : i32
-subroutine simple_linear
-    implicit none
-    integer :: x, y, i
-    !CHECK: omp.wsloop linear(%[[X]]#0 = %[[const]] : !fir.ref<i32>) {{.*}}
-    !$omp do linear(x)
-    !CHECK: %[[LOAD:.*]] = fir.load %[[X]]#0 : !fir.ref<i32>
-    !CHECK: %[[const:.*]] = arith.constant 2 : i32
-    !CHECK: %[[RESULT:.*]] = arith.addi %[[LOAD]], %[[const]] : i32
-    do i = 1, 10
-        y = x + 2
-    end do
-    !$omp end do
-end subroutine
-
-
-!CHECK: %[[X_alloca:.*]] = fir.alloca i32 {bindc_name = "x", uniq_name = "_QFlinear_stepEx"}
-!CHECK: %[[X:.*]]:2 = hlfir.declare %[[X_alloca]] {uniq_name = "_QFlinear_stepEx"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
-subroutine linear_step
-    implicit none
-    integer :: x, y, i
-    !CHECK: %[[const:.*]] = arith.constant 4 : i32
-    !CHECK: omp.wsloop linear(%[[X]]#0 = %[[const]] : !fir.ref<i32>) {{.*}}
-    !$omp do linear(x:4)
-    !CHECK: %[[LOAD:.*]] = fir.load %[[X]]#0 : !fir.ref<i32>
-    !CHECK: %[[const:.*]] = arith.constant 2 : i32
-    !CHECK: %[[RESULT:.*]] = arith.addi %[[LOAD]], %[[const]] : i32   
-    do i = 1, 10
-        y = x + 2
-    end do
-    !$omp end do
-end subroutine
-
-!CHECK: %[[A_alloca:.*]] = fir.alloca i32 {bindc_name = "a", uniq_name = "_QFlinear_exprEa"}
-!CHECK: %[[A:.*]]:2 = hlfir.declare %[[A_alloca]] {uniq_name = "_QFlinear_exprEa"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
-!CHECK: %[[X_alloca:.*]] = fir.alloca i32 {bindc_name = "x", uniq_name = "_QFlinear_exprEx"}
-!CHECK: %[[X:.*]]:2 = hlfir.declare %[[X_alloca]] {uniq_name = "_QFlinear_exprEx"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
-subroutine linear_expr
-    implicit none
-    integer :: x, y, i, a
-    !CHECK: %[[LOAD_A:.*]] = fir.load %[[A]]#0 : !fir.ref<i32>
-    !CHECK: %[[const:.*]] = arith.constant 4 : i32
-    !CHECK: %[[LINEAR_EXPR:.*]] = arith.addi %[[LOAD_A]], %[[const]] : i32
-    !CHECK: omp.wsloop linear(%[[X]]#0 = %[[LINEAR_EXPR]] : !fir.ref<i32>) {{.*}}
-    !$omp do linear(x:a+4)
-    do i = 1, 10
-        y = x + 2
-    end do
-    !$omp end do
-end subroutine

Copy link
Contributor

@tblah tblah left a comment

Choose a reason for hiding this comment

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

Thank you very much Nimish

Copy link
Contributor

@mrkajetanp mrkajetanp left a comment

Choose a reason for hiding this comment

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

Could you remove the check for OmpLinear that actually causes the crash?
As-is this does not actually resolve #142935 unless we're also merging #144315 at the same time.

@NimishMishra
Copy link
Contributor Author

NimishMishra commented Jun 19, 2025

Could you remove the check for OmpLinear that actually causes the crash? As-is this does not actually resolve #142935 unless we're also merging #144315 at the same time.

Hi @mrkajetanp, yes both PRs can be merged if it is okay with you and @tblah. I can merge the two into this too if you would prefer that. Essentially, we are disabling the lowering of linear clause until the design of linear clause is changed to include the implicit linearization of loop variable.

With these two PRs, linear clause lowering and translation is essentially disabled / unreachable, until it is further fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:fir-hlfir flang:openmp flang Flang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[flang][openmp] compiler crash when private clause is used with SIMD
4 participants