[flang-commits] [flang] 72b7645 - [flang][OpenMP] Add lowering for assume and assumes directives (#205615)

via flang-commits flang-commits at lists.llvm.org
Wed Jul 22 23:05:23 PDT 2026


Author: Ritanya-B-Bharadwaj
Date: 2026-07-23T11:35:19+05:30
New Revision: 72b764559fd2d5f564f2c6f42edd782c8e20616d

URL: https://github.com/llvm/llvm-project/commit/72b764559fd2d5f564f2c6f42edd782c8e20616d
DIFF: https://github.com/llvm/llvm-project/commit/72b764559fd2d5f564f2c6f42edd782c8e20616d.diff

LOG: [flang][OpenMP] Add lowering for assume and assumes directives (#205615)

Adds lowering for the assume and assumes directives. holds clauses are lowered to llvm.assume, the other assumption clauses are ignored, and assumes is a no-op. Hints are skipped under -fopenmp-simd.

Added: 
    flang/test/Lower/OpenMP/assumption.f90

Modified: 
    flang/lib/Lower/OpenMP/OpenMP.cpp
    flang/test/Lower/OpenMP/Todo/assume.f90
    flang/test/Lower/OpenMP/Todo/assumes.f90

Removed: 
    


################################################################################
diff  --git a/flang/lib/Lower/OpenMP/OpenMP.cpp b/flang/lib/Lower/OpenMP/OpenMP.cpp
index 27c40436e90d5..54c1a06edde8a 100644
--- a/flang/lib/Lower/OpenMP/OpenMP.cpp
+++ b/flang/lib/Lower/OpenMP/OpenMP.cpp
@@ -50,6 +50,7 @@
 #include "flang/Support/OpenMP-utils.h"
 #include "flang/Utils/OpenMP.h"
 #include "mlir/Dialect/ControlFlow/IR/ControlFlowOps.h"
+#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
 #include "mlir/Dialect/OpenMP/OpenMPDialect.h"
 #include "mlir/IR/IRMapping.h"
 #include "mlir/Support/StateStack.h"
@@ -5080,8 +5081,11 @@ static void genOMP(lower::AbstractConverter &converter, lower::SymMap &symTable,
                    semantics::SemanticsContext &semaCtx,
                    lower::pft::Evaluation &eval,
                    const parser::OmpAssumesDirective &assumesConstruct) {
+  // Assumption clauses are hints with no representation in the OpenMP dialect,
+  // so this declarative directive is a no-op.
   if (!semaCtx.langOptions().OpenMPSimd)
-    TODO(converter.getCurrentLocation(), "OpenMP ASSUMES declaration");
+    TODO(converter.getCurrentLocation(),
+         "assumption clauses on the assumes directive");
 }
 
 static void
@@ -6540,9 +6544,31 @@ static void genOMP(lower::AbstractConverter &converter, lower::SymMap &symTable,
                    semantics::SemanticsContext &semaCtx,
                    lower::pft::Evaluation &eval,
                    const parser::OmpAssumeDirective &assumeConstruct) {
-  mlir::Location clauseLocation = converter.genLocation(assumeConstruct.source);
-  if (!semaCtx.langOptions().OpenMPSimd)
-    TODO(clauseLocation, "OpenMP ASSUME construct");
+  if (!semaCtx.langOptions().OpenMPSimd) {
+    fir::FirOpBuilder &firOpBuilder = converter.getFirOpBuilder();
+    lower::StatementContext stmtCtx;
+
+    const parser::OmpDirectiveSpecification &beginSpec =
+        assumeConstruct.BeginDir();
+    for (const parser::OmpClause &clause : beginSpec.Clauses().v) {
+      mlir::Location clauseLoc = converter.genLocation(clause.source);
+      const auto *holds = std::get_if<parser::OmpClause::Holds>(&clause.u);
+      if (!holds) {
+        TODO(clauseLoc, "assumption clause is not implemented yet");
+      }
+      const parser::Expr &parserExpr = holds->v.v.value();
+      const semantics::SomeExpr *expr = semantics::GetExpr(semaCtx, parserExpr);
+      assert(expr && "Expecting analyzed expression for holds clause");
+
+      mlir::Value cond =
+          fir::getBase(converter.genExprValue(*expr, stmtCtx, &clauseLoc));
+      cond =
+          firOpBuilder.createConvert(clauseLoc, firOpBuilder.getI1Type(), cond);
+      mlir::LLVM::AssumeOp::create(firOpBuilder, clauseLoc, cond);
+    }
+    stmtCtx.finalizeAndPop();
+  }
+  genNestedEvaluations(converter, eval);
 }
 
 static void genOMP(lower::AbstractConverter &converter, lower::SymMap &symTable,

diff  --git a/flang/test/Lower/OpenMP/Todo/assume.f90 b/flang/test/Lower/OpenMP/Todo/assume.f90
index 1216888efabd1..58c900c9212b9 100644
--- a/flang/test/Lower/OpenMP/Todo/assume.f90
+++ b/flang/test/Lower/OpenMP/Todo/assume.f90
@@ -1,6 +1,6 @@
 ! RUN: %not_todo_cmd %flang_fc1 -emit-fir -fopenmp -fopenmp-version=51 -o - %s 2>&1 | FileCheck %s
 
-! CHECK: not yet implemented: OpenMP ASSUME construct
+! CHECK: not yet implemented: assumption clause is not implemented yet
 program p
   integer r
   r = 1

diff  --git a/flang/test/Lower/OpenMP/Todo/assumes.f90 b/flang/test/Lower/OpenMP/Todo/assumes.f90
index ac26ed14ded3c..8ac080cd376be 100644
--- a/flang/test/Lower/OpenMP/Todo/assumes.f90
+++ b/flang/test/Lower/OpenMP/Todo/assumes.f90
@@ -1,6 +1,6 @@
 ! RUN: %not_todo_cmd %flang_fc1 -emit-fir -fopenmp -fopenmp-version=51 -o - %s 2>&1 | FileCheck %s
 
-! CHECK: not yet implemented: OpenMP ASSUMES declaration
+! CHECK: not yet implemented: assumption clauses on the assumes directive
 program p
   !$omp assumes no_openmp
 end program p

diff  --git a/flang/test/Lower/OpenMP/assumption.f90 b/flang/test/Lower/OpenMP/assumption.f90
new file mode 100644
index 0000000000000..3cf51efd59dec
--- /dev/null
+++ b/flang/test/Lower/OpenMP/assumption.f90
@@ -0,0 +1,21 @@
+! RUN: %flang_fc1 -emit-hlfir -fopenmp -fopenmp-version=51 -o - %s 2>&1 | FileCheck %s
+
+! Lowering of the OpenMP ASSUME construct. The HOLDS clause asserts a scalar
+! logical expression, which is lowered to an llvm.assume intrinsic. The block is
+! always lowered.
+
+! CHECK-LABEL: func.func @_QPassume_block
+subroutine assume_block(x)
+  integer :: x
+  ! CHECK: %[[DECL:.*]]:2 = hlfir.declare
+  !$omp assume holds(x > 0)
+  block
+    ! CHECK: %[[LOAD:.*]] = fir.load %[[DECL]]#0
+    ! CHECK: %[[C0:.*]] = arith.constant 0 : i32
+    ! CHECK: %[[CMP:.*]] = arith.cmpi sgt, %[[LOAD]], %[[C0]] : i32
+    ! CHECK: llvm.intr.assume %[[CMP]] : i1
+    ! CHECK: %[[C2:.*]] = arith.constant 2 : i32
+    ! CHECK: hlfir.assign %[[C2]] to %[[DECL]]#0
+    x = 2
+  end block
+end subroutine assume_block


        


More information about the flang-commits mailing list