[flang-commits] [flang] [Flang] [OpenMP] Run `MarkDeclareTarget` pass again before `HostOpFiltering` (PR #210049)

Soham Karandikar via flang-commits flang-commits at lists.llvm.org
Fri Jul 17 23:33:52 PDT 2026


https://github.com/skadewdl3 updated https://github.com/llvm/llvm-project/pull/210049

>From d72c000a48cf01c5ea9f4e837be6464d05d64ecd Mon Sep 17 00:00:00 2001
From: Soham Karandikar <sohamk10 at gmail.com>
Date: Thu, 16 Jul 2026 07:57:24 -0500
Subject: [PATCH 1/3] Run `MarkDeclareTarget` pass again after
 `ConvertMathToFuncs`

---
 flang/lib/Optimizer/Passes/Pipelines.cpp | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/flang/lib/Optimizer/Passes/Pipelines.cpp b/flang/lib/Optimizer/Passes/Pipelines.cpp
index 879a224824119..7fe8bccc14d54 100644
--- a/flang/lib/Optimizer/Passes/Pipelines.cpp
+++ b/flang/lib/Optimizer/Passes/Pipelines.cpp
@@ -464,6 +464,11 @@ void createDefaultFIRCodeGenPassPipeline(mlir::PassManager &pm,
   pm.addPass(fir::createEmitMIFGlobalCtors());
 
   if (config.EnableOpenMP && !config.EnableOpenMPSimd) {
+    // Since some math operations may be converted to function calls by the
+    // ConvertMathToFuncs pass, we need to mark them with the omp.declare_target
+    // attribute if they're called from a target region.
+    pm.addPass(mlir::omp::createMarkDeclareTargetPass());
+
     // Remove all non target-related operations from host functions still
     // remaining at this point, if compiling for an OpenMP target device. This
     // is required before translating 'omp' dialect operations to LLVM IR.

>From 3012174db0243a9500690e5e4c03ab4b2ace5cda Mon Sep 17 00:00:00 2001
From: Soham Karandikar <sohamk10 at gmail.com>
Date: Thu, 16 Jul 2026 10:58:17 -0500
Subject: [PATCH 2/3] Added a lit test for ensuring target functions get the
 correct attribute before host-op-filtering

---
 .../OpenMP/declare-target-funcs.f90           | 29 +++++++++++++++++++
 1 file changed, 29 insertions(+)
 create mode 100644 flang/test/Integration/OpenMP/declare-target-funcs.f90

diff --git a/flang/test/Integration/OpenMP/declare-target-funcs.f90 b/flang/test/Integration/OpenMP/declare-target-funcs.f90
new file mode 100644
index 0000000000000..ecfe380752690
--- /dev/null
+++ b/flang/test/Integration/OpenMP/declare-target-funcs.f90
@@ -0,0 +1,29 @@
+!===----------------------------------------------------------------------===!
+! This directory can be used to add Integration tests involving multiple
+! stages of the compiler (for eg. from Fortran to LLVM IR). It should not
+! contain executable tests. We should only add tests here sparingly and only
+! if there is no other way to test. Repeat this message in each test that is
+! added to this directory and sub-directories.
+!===----------------------------------------------------------------------===!
+
+!REQUIRES: amdgpu-registered-target
+!RUN: %flang_fc1 -triple amdgcn-amd-amdhsa -fopenmp -nogpulib -fopenmp-is-target-device -mmlir --mlir-print-ir-before-all -S %s -o /dev/null 2>&1 | FileCheck %s
+
+
+! This tests the fix for https://github.com/llvm/llvm-project/issues/209123
+! We are only interested in ensuring that functions used in target regions should have
+! the omp.declare_target attribute so the -omp-host-op-fitler pass doesn't crash.
+
+! CHECK-LABEL: IR Dump Before HostOpFilteringPass: omp-host-op-filter
+! CHECK: llvm.func{{.*}}@__mlir_math_ipowi_i32
+! CHECK-SAME: attributes{{.*}}omp.declare_target
+
+module m
+contains
+    subroutine s()
+      integer :: n1
+      real :: tmp1
+      !$omp declare target
+      tmp1 = 2**n1
+    end subroutine s
+end module

>From 625af7e8db6ef3a2cb4901f50d1e3bd48416e9c9 Mon Sep 17 00:00:00 2001
From: Soham Karandikar <sohamk10 at gmail.com>
Date: Sat, 18 Jul 2026 01:33:38 -0500
Subject: [PATCH 3/3] Added `MarkDeclareTargetPass` to the fir pipeline lit
 test

---
 flang/test/Fir/basic-program.fir | 1 +
 1 file changed, 1 insertion(+)

diff --git a/flang/test/Fir/basic-program.fir b/flang/test/Fir/basic-program.fir
index 1fb7ed81f547c..14dea7818f230 100644
--- a/flang/test/Fir/basic-program.fir
+++ b/flang/test/Fir/basic-program.fir
@@ -185,6 +185,7 @@ func.func @_QQmain() {
 // PASSES-NEXT: FIRToLLVMLowering
 // PASSES-NEXT: ReconcileUnrealizedCasts
 // PASSES-NEXT: EmitMIFGlobalCtors
+// PASSES-NEXT: MarkDeclareTargetPass
 // PASSES-NEXT:  HostOpFilteringPass
 // PASSES-NEXT: 'llvm.func' Pipeline
 // PASSES-NEXT:  StackToSharedPass



More information about the flang-commits mailing list