[flang-commits] [flang] 22a1677 - [flang] Fix OMPEarlyOutlining erasing declare target functions

Jan Sjodin via flang-commits flang-commits at lists.llvm.org
Thu Jul 13 10:06:36 PDT 2023


Author: Jan Sjodin
Date: 2023-07-13T13:00:23-04:00
New Revision: 22a167779aca7b3384d3e0fd132ccdf3740f6911

URL: https://github.com/llvm/llvm-project/commit/22a167779aca7b3384d3e0fd132ccdf3740f6911
DIFF: https://github.com/llvm/llvm-project/commit/22a167779aca7b3384d3e0fd132ccdf3740f6911.diff

LOG: [flang] Fix OMPEarlyOutlining erasing declare target functions

The early outlining pass was erasing target functions that need to be
kept. It should only erase functions that contain target ops.

Added: 
    

Modified: 
    flang/lib/Optimizer/Transforms/OMPEarlyOutlining.cpp
    flang/test/Lower/OpenMP/omp-target-early-outlining.f90

Removed: 
    


################################################################################
diff  --git a/flang/lib/Optimizer/Transforms/OMPEarlyOutlining.cpp b/flang/lib/Optimizer/Transforms/OMPEarlyOutlining.cpp
index 1efd07697aad61..6bbcbf84ac0b3f 100644
--- a/flang/lib/Optimizer/Transforms/OMPEarlyOutlining.cpp
+++ b/flang/lib/Optimizer/Transforms/OMPEarlyOutlining.cpp
@@ -82,7 +82,8 @@ class OMPEarlyOutliningPass
     return newFunc;
   }
 
-  void outlineTargetOps(mlir::OpBuilder &builder,
+  // Returns true if a target region was found int the function.
+  bool outlineTargetOps(mlir::OpBuilder &builder,
                         mlir::func::FuncOp &functionOp,
                         mlir::ModuleOp &moduleOp,
                         llvm::SmallVectorImpl<mlir::func::FuncOp> &newFuncs) {
@@ -93,6 +94,7 @@ class OMPEarlyOutliningPass
       newFuncs.push_back(outlinedFunc);
       count++;
     }
+    return count > 0;
   }
 
   void runOnOperation() override {
@@ -103,8 +105,9 @@ class OMPEarlyOutliningPass
 
     for (auto functionOp :
          llvm::make_early_inc_range(moduleOp.getOps<mlir::func::FuncOp>())) {
-      outlineTargetOps(builder, functionOp, moduleOp, newFuncs);
-      functionOp.erase();
+      bool outlined = outlineTargetOps(builder, functionOp, moduleOp, newFuncs);
+      if (outlined)
+        functionOp.erase();
     }
 
     for (auto newFunc : newFuncs)

diff  --git a/flang/test/Lower/OpenMP/omp-target-early-outlining.f90 b/flang/test/Lower/OpenMP/omp-target-early-outlining.f90
index eb057f74bed693..724f363c013cc6 100644
--- a/flang/test/Lower/OpenMP/omp-target-early-outlining.f90
+++ b/flang/test/Lower/OpenMP/omp-target-early-outlining.f90
@@ -3,6 +3,8 @@
 !RUN: %flang_fc1 -triple amdgcn-amd-amdhsa -emit-fir -fopenmp -fopenmp-is-device %s -o - | FileCheck %s
 !RUN: %flang_fc1 -triple x86_64-unknown-linux-gnu -emit-fir -fopenmp -fopenmp-is-device %s -o - | FileCheck %s
 
+!CHECK: func.func @_QPtarget_function
+
 !CHECK:  func.func @_QPwrite_index_omp_outline_0(%[[ARG0:.*]]: !fir.ref<i32>) attributes {omp.declare_target = #omp.declaretarget<device_type = (host), capture_clause = (to)>, omp.outline_parent_name = "_QPwrite_index"} {
 !CHECK-NEXT: omp.target  {{.*}} {
 !CHECK: %[[CONSTANT_VALUE_10:.*]] = arith.constant 10 : i32
@@ -33,3 +35,7 @@ SUBROUTINE WRITE_INDEX(INT_ARRAY)
                 INT_ARRAY(INDEX_) = INDEX_
         end do
 end subroutine WRITE_INDEX
+
+SUBROUTINE TARGET_FUNCTION()
+!$omp declare target
+END


        


More information about the flang-commits mailing list