Skip to content

Commit

Permalink
[flang] Fix OMPEarlyOutlining erasing declare target functions
Browse files Browse the repository at this point in the history
The early outlining pass was erasing target functions that need to be
kept. It should only erase functions that contain target ops.
  • Loading branch information
jsjodin committed Jul 13, 2023
1 parent ca8ef82 commit 22a1677
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
9 changes: 6 additions & 3 deletions flang/lib/Optimizer/Transforms/OMPEarlyOutlining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -93,6 +94,7 @@ class OMPEarlyOutliningPass
newFuncs.push_back(outlinedFunc);
count++;
}
return count > 0;
}

void runOnOperation() override {
Expand All @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions flang/test/Lower/OpenMP/omp-target-early-outlining.f90
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

0 comments on commit 22a1677

Please sign in to comment.