Skip to content

Commit fb4bdf3

Browse files
committed
[Flang][OpenMP] Run Flang-specific OpenMP MLIR passes in bbc
This patch moves the group of OpenMP MLIR passes using after lowering of Fortran to MLIR into a pipeline to be shared by `flang-new` and `bbc`. Currently, the `bbc` tool does not produce the expected FIR for offloading- enabled OpenMP codes due to not running these passes. Unit tests exercising these passes are updated to check `bbc` output as well.
1 parent bf7c490 commit fb4bdf3

File tree

9 files changed

+46
-13
lines changed

9 files changed

+46
-13
lines changed

flang/include/flang/Tools/CLOptions.inc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,24 @@ inline void createHLFIRToFIRPassPipeline(
254254
pm.addPass(hlfir::createConvertHLFIRtoFIRPass());
255255
}
256256

257+
/// Create a pass pipeline for handling certain OpenMP transformations needed
258+
/// prior to FIR lowering.
259+
///
260+
/// WARNING: These passes must be run immediately after the lowering to ensure
261+
/// that the FIR is correct with respect to OpenMP operations/attributes.
262+
///
263+
/// \param pm - MLIR pass manager that will hold the pipeline definition.
264+
/// \param isTargetDevice - Whether code is being generated for a target device
265+
/// rather than the host device.
266+
inline void createOpenMPFIRPassPipeline(
267+
mlir::PassManager &pm, bool isTargetDevice) {
268+
pm.addPass(fir::createOMPMarkDeclareTargetPass());
269+
if (isTargetDevice) {
270+
pm.addPass(fir::createOMPEarlyOutliningPass());
271+
pm.addPass(fir::createOMPFunctionFilteringPass());
272+
}
273+
}
274+
257275
#if !defined(FLANG_EXCLUDE_CODEGEN)
258276
inline void createDebugPasses(
259277
mlir::PassManager &pm, llvm::codegenoptions::DebugInfoKind debugLevel) {

flang/lib/Frontend/FrontendActions.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@
7070
#include <memory>
7171
#include <system_error>
7272

73+
#include "flang/Tools/CLOptions.inc"
74+
7375
using namespace Fortran::frontend;
7476

7577
// Declare plugin extension function declarations.
@@ -313,12 +315,10 @@ bool CodeGenAction::beginSourceFileAction() {
313315
if (auto offloadMod = llvm::dyn_cast<mlir::omp::OffloadModuleInterface>(
314316
mlirModule->getOperation()))
315317
isDevice = offloadMod.getIsTargetDevice();
316-
317-
pm.addPass(fir::createOMPMarkDeclareTargetPass());
318-
if (isDevice) {
319-
pm.addPass(fir::createOMPEarlyOutliningPass());
320-
pm.addPass(fir::createOMPFunctionFilteringPass());
321-
}
318+
// WARNING: This pipeline must be run immediately after the lowering to
319+
// ensure that the FIR is correct with respect to OpenMP operations/
320+
// attributes.
321+
fir::createOpenMPFIRPassPipeline(pm, isDevice);
322322
}
323323

324324
pm.enableVerifier(/*verifyPasses=*/true);
@@ -651,8 +651,6 @@ void GetSymbolsSourcesAction::executeAction() {
651651

652652
CodeGenAction::~CodeGenAction() = default;
653653

654-
#include "flang/Tools/CLOptions.inc"
655-
656654
static llvm::OptimizationLevel
657655
mapToLevel(const Fortran::frontend::CodeGenOptions &opts) {
658656
switch (opts.OptimizationLevel) {

flang/test/Driver/omp-cse-region-boundary.f90

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
!This test checks that when compiling an OpenMP program for the target device
22
!CSE is not done across target op region boundaries. It also checks that when
33
!compiling for the host CSE is done.
4-
!RUN: %flang_fc1 -fopenmp-is-device -emit-mlir -fopenmp %s -o - | fir-opt -cse | FileCheck %s -check-prefix=CHECK-DEVICE
4+
!RUN: %flang_fc1 -fopenmp-is-target-device -emit-mlir -fopenmp %s -o - | fir-opt -cse | FileCheck %s -check-prefix=CHECK-DEVICE
55
!RUN: %flang_fc1 -emit-mlir -fopenmp %s -o - | fir-opt -cse | FileCheck %s -check-prefix=CHECK-HOST
6+
!RUN: bbc -fopenmp-is-target-device -emit-fir -fopenmp %s -o - | fir-opt -cse | FileCheck %s -check-prefix=CHECK-DEVICE
7+
!RUN: bbc -emit-fir -fopenmp %s -o - | fir-opt -cse | FileCheck %s -check-prefix=CHECK-HOST
68

79
!Constant should be present inside target region.
810
!CHECK-DEVICE: omp.target
@@ -19,7 +21,7 @@ subroutine writeIndex(sum)
1921
integer :: myconst1
2022
integer :: myconst2
2123
myconst1 = 10
22-
!$omp target map(from:new_len)
24+
!$omp target map(from:myconst2)
2325
myconst2 = 10
2426
!$omp end target
2527
sum = myconst2 + myconst2

flang/test/Lower/OpenMP/declare-target-implicit-func-and-subr-cap.f90

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
!RUN: %flang_fc1 -emit-fir -fopenmp %s -o - | FileCheck %s
2-
!RUN: %flang_fc1 -emit-fir -fopenmp -fopenmp-is-device %s -o - | FileCheck %s --check-prefix=DEVICE
2+
!RUN: %flang_fc1 -emit-fir -fopenmp -fopenmp-is-target-device %s -o - | FileCheck %s --check-prefix=DEVICE
3+
!RUN: bbc -emit-fir -fopenmp %s -o - | FileCheck %s
4+
!RUN: bbc -emit-fir -fopenmp -fopenmp-is-target-device %s -o - | FileCheck %s --check-prefix=DEVICE
35

46
! CHECK-LABEL: func.func @_QPimplicitly_captured
57
! CHECK-SAME: {{.*}}attributes {omp.declare_target = #omp.declaretarget<device_type = (any), capture_clause = (to)>{{.*}}}

flang/test/Lower/OpenMP/declare-target-implicit-tarop-cap.f90

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
!RUN: %flang_fc1 -emit-fir -fopenmp %s -o - | FileCheck %s
22
!RUN: %flang_fc1 -emit-fir -fopenmp -fopenmp-is-device %s -o - | FileCheck %s --check-prefix=DEVICE
3+
!RUN: bbc -emit-fir -fopenmp %s -o - | FileCheck %s
4+
!RUN: bbc -emit-fir -fopenmp -fopenmp-is-target-device %s -o - | FileCheck %s --check-prefix=DEVICE
35

46
! DEVICE-LABEL: func.func @_QPimplicit_capture
57
! DEVICE-SAME: {{.*}}attributes {omp.declare_target = #omp.declaretarget<device_type = (nohost), capture_clause = (to)>{{.*}}}

flang/test/Lower/OpenMP/function-filtering-2.f90

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
! RUN: %flang_fc1 -fopenmp -emit-mlir %s -o - | FileCheck --check-prefix=MLIR %s
33
! RUN: %flang_fc1 -fopenmp -fopenmp-is-target-device -emit-llvm %s -o - | FileCheck --check-prefix=LLVM %s
44
! RUN: %flang_fc1 -fopenmp -fopenmp-is-target-device -emit-mlir %s -o - | FileCheck --check-prefix=MLIR %s
5+
! RUN: bbc -fopenmp -emit-fir %s -o - | FileCheck --check-prefixes=MLIR-HOST,MLIR-ALL %s
6+
! RUN: bbc -fopenmp -fopenmp-is-target-device -emit-fir %s -o - | FileCheck --check-prefixes=MLIR-DEVICE,MLIR-ALL %s
57

68
! MLIR: func.func @{{.*}}implicit_invocation() attributes {omp.declare_target = #omp.declaretarget<device_type = (nohost), capture_clause = (to)>}
79
! MLIR: return

flang/test/Lower/OpenMP/function-filtering.f90

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
! RUN: %flang_fc1 -fopenmp -emit-mlir %s -o - | FileCheck --check-prefixes=MLIR-HOST,MLIR-ALL %s
33
! RUN: %flang_fc1 -fopenmp -fopenmp-is-target-device -emit-llvm %s -o - | FileCheck --check-prefixes=LLVM-DEVICE,LLVM-ALL %s
44
! RUN: %flang_fc1 -fopenmp -fopenmp-is-target-device -emit-mlir %s -o - | FileCheck --check-prefixes=MLIR-DEVICE,MLIR-ALL %s
5+
! RUN: bbc -fopenmp -emit-fir %s -o - | FileCheck --check-prefixes=MLIR-HOST,MLIR-ALL %s
6+
! RUN: bbc -fopenmp -fopenmp-is-target-device -emit-fir %s -o - | FileCheck --check-prefixes=MLIR-DEVICE,MLIR-ALL %s
57

68
! Check that the correct LLVM IR functions are kept for the host and device
79
! after running the whole set of translation and transformation passes from

flang/test/Lower/OpenMP/omp-target-early-outlining.f90

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
!REQUIRES: amdgpu-registered-target
22

3-
!RUN: %flang_fc1 -triple amdgcn-amd-amdhsa -emit-fir -fopenmp -fopenmp-is-device %s -o - | FileCheck %s
4-
!RUN: %flang_fc1 -triple x86_64-unknown-linux-gnu -emit-fir -fopenmp -fopenmp-is-device %s -o - | FileCheck %s
3+
!RUN: %flang_fc1 -triple amdgcn-amd-amdhsa -emit-fir -fopenmp -fopenmp-is-target-device %s -o - | FileCheck %s
4+
!RUN: %flang_fc1 -triple x86_64-unknown-linux-gnu -emit-fir -fopenmp -fopenmp-is-target-device %s -o - | FileCheck %s
5+
!RUN: bbc -emit-fir -fopenmp -fopenmp-is-target-device %s -o - | FileCheck %s
6+
!RUN: bbc -emit-fir -fopenmp -fopenmp-is-gpu -fopenmp-is-target-device %s -o - | FileCheck %s
57

68
!CHECK: func.func @_QPtarget_function
79

flang/tools/bbc/bbc.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,11 @@ static mlir::LogicalResult convertFortranSourceToMLIR(
329329
// Otherwise run the default passes.
330330
mlir::PassManager pm(mlirModule->getName(),
331331
mlir::OpPassManager::Nesting::Implicit);
332+
if (enableOpenMP)
333+
// WARNING: This pipeline must be run immediately after the lowering to
334+
// ensure that the FIR is correct with respect to OpenMP operations/
335+
// attributes.
336+
fir::createOpenMPFIRPassPipeline(pm, enableOpenMPDevice);
332337
pm.enableVerifier(/*verifyPasses=*/true);
333338
(void)mlir::applyPassManagerCLOptions(pm);
334339
if (passPipeline.hasAnyOccurrences()) {

0 commit comments

Comments
 (0)