From feee25820d26f4293602028f2d5a28c7a8a5bdd4 Mon Sep 17 00:00:00 2001 From: ergawy Date: Tue, 15 Oct 2024 05:40:30 -0500 Subject: [PATCH] [flang][OpenMP] Add flag to enable/disable `GlobalFilteringPass` Since this pass is only needed/useful when using the old clang driver, we are adding a flang to enable/disable it with the goal of completely removing the pass. --- clang/include/clang/Driver/Options.td | 1 + clang/lib/Driver/ToolChains/Flang.cpp | 4 +++- flang/include/flang/Frontend/CodeGenOptions.def | 1 + flang/include/flang/Optimizer/Passes/Pipelines.h | 11 ++++++++--- flang/lib/Frontend/CompilerInvocation.cpp | 5 +++++ flang/lib/Frontend/FrontendActions.cpp | 15 +++++++++------ flang/lib/Optimizer/OpenMP/GlobalFiltering.cpp | 2 ++ flang/lib/Optimizer/Passes/Pipelines.cpp | 15 ++++++++------- flang/tools/bbc/bbc.cpp | 7 ++++--- 9 files changed, 41 insertions(+), 20 deletions(-) diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td index ea493d0e6aede7..90d30b62ce8332 100644 --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -6826,6 +6826,7 @@ defm xor_operator : OptInFC1FFlag<"xor-operator", "Enable .XOR. as a synonym of defm logical_abbreviations : OptInFC1FFlag<"logical-abbreviations", "Enable logical abbreviations">; defm implicit_none : OptInFC1FFlag<"implicit-none", "No implicit typing allowed unless overridden by IMPLICIT statements">; defm underscoring : OptInFC1FFlag<"underscoring", "Appends one trailing underscore to external names">; +defm offload_global_filtering : OptInFC1FFlag<"offload-global-filtering", "Enable/disable OpenMP global filtering pass">; defm ppc_native_vec_elem_order: BoolOptionWithoutMarshalling<"f", "ppc-native-vector-element-order", PosFlag, NegFlag>; diff --git a/clang/lib/Driver/ToolChains/Flang.cpp b/clang/lib/Driver/ToolChains/Flang.cpp index 3703832992bcb1..67de415e5af274 100644 --- a/clang/lib/Driver/ToolChains/Flang.cpp +++ b/clang/lib/Driver/ToolChains/Flang.cpp @@ -120,7 +120,9 @@ void Flang::addOtherOptions(const ArgList &Args, ArgStringList &CmdArgs) const { options::OPT_fintrinsic_modules_path, options::OPT_pedantic, options::OPT_std_EQ, options::OPT_W_Joined, options::OPT_fconvert_EQ, options::OPT_fpass_plugin_EQ, - options::OPT_funderscoring, options::OPT_fno_underscoring}); + options::OPT_funderscoring, options::OPT_fno_underscoring, + options::OPT_foffload_global_filtering, + options::OPT_fno_offload_global_filtering}); llvm::codegenoptions::DebugInfoKind DebugInfoKind; if (Args.hasArg(options::OPT_gN_Group)) { diff --git a/flang/include/flang/Frontend/CodeGenOptions.def b/flang/include/flang/Frontend/CodeGenOptions.def index a6128130baadc3..98226818f7f1c5 100644 --- a/flang/include/flang/Frontend/CodeGenOptions.def +++ b/flang/include/flang/Frontend/CodeGenOptions.def @@ -35,6 +35,7 @@ CODEGENOPT(LoopVersioning, 1, 0) ///< Enable loop versioning. CODEGENOPT(AliasAnalysis, 1, 0) ///< Enable alias analysis pass CODEGENOPT(Underscoring, 1, 1) +CODEGENOPT(OffloadGlobalFiltering, 1, 1) ENUM_CODEGENOPT(RelocationModel, llvm::Reloc::Model, 3, llvm::Reloc::PIC_) ///< Name of the relocation model to use. ENUM_CODEGENOPT(DebugInfo, llvm::codegenoptions::DebugInfoKind, 4, llvm::codegenoptions::NoDebugInfo) ///< Level of debug info to generate ENUM_CODEGENOPT(VecLib, llvm::driver::VectorLibrary, 3, llvm::driver::VectorLibrary::NoLibrary) ///< Vector functions library to use diff --git a/flang/include/flang/Optimizer/Passes/Pipelines.h b/flang/include/flang/Optimizer/Passes/Pipelines.h index ddedec0b5df620..a85ce40c7e81ea 100644 --- a/flang/include/flang/Optimizer/Passes/Pipelines.h +++ b/flang/include/flang/Optimizer/Passes/Pipelines.h @@ -128,6 +128,12 @@ void createHLFIRToFIRPassPipeline( using DoConcurrentMappingKind = Fortran::frontend::CodeGenOptions::DoConcurrentMappingKind; +struct OpenMPFIRPassPipelineOpts { + bool isTargetDevice; + bool enableOffloadGlobalFiltering; + DoConcurrentMappingKind doConcurrentMappingKind; +}; + /// Create a pass pipeline for handling certain OpenMP transformations needed /// prior to FIR lowering. /// @@ -137,9 +143,8 @@ using DoConcurrentMappingKind = /// \param pm - MLIR pass manager that will hold the pipeline definition. /// \param isTargetDevice - Whether code is being generated for a target device /// rather than the host device. -void createOpenMPFIRPassPipeline( - mlir::PassManager &pm, bool isTargetDevice, - DoConcurrentMappingKind doConcurrentMappingKind); +void createOpenMPFIRPassPipeline(mlir::PassManager &pm, + OpenMPFIRPassPipelineOpts opts); #if !defined(FLANG_EXCLUDE_CODEGEN) void createDebugPasses(mlir::PassManager &pm, diff --git a/flang/lib/Frontend/CompilerInvocation.cpp b/flang/lib/Frontend/CompilerInvocation.cpp index bdccc1a86d7f1e..3bbb35cb542c21 100644 --- a/flang/lib/Frontend/CompilerInvocation.cpp +++ b/flang/lib/Frontend/CompilerInvocation.cpp @@ -443,6 +443,11 @@ static void parseCodeGenArgs(Fortran::frontend::CodeGenOptions &opts, opts.Underscoring = 0; } + if (args.hasFlag(clang::driver::options::OPT_fno_offload_global_filtering, + clang::driver::options::OPT_foffload_global_filtering, false)) { + opts.OffloadGlobalFiltering = 0; + } + parseDoConcurrentMapping(opts, args, diags); } diff --git a/flang/lib/Frontend/FrontendActions.cpp b/flang/lib/Frontend/FrontendActions.cpp index b69af577e522f8..0713e7159a2e6f 100644 --- a/flang/lib/Frontend/FrontendActions.cpp +++ b/flang/lib/Frontend/FrontendActions.cpp @@ -334,12 +334,16 @@ bool CodeGenAction::beginSourceFileAction() { ci.getInvocation().getFrontendOpts().features.IsEnabled( Fortran::common::LanguageFeature::OpenMP); + fir::OpenMPFIRPassPipelineOpts opts; + using DoConcurrentMappingKind = Fortran::frontend::CodeGenOptions::DoConcurrentMappingKind; - DoConcurrentMappingKind doConcurrentMappingKind = + opts.doConcurrentMappingKind = ci.getInvocation().getCodeGenOpts().getDoConcurrentMapping(); + opts.enableOffloadGlobalFiltering = + ci.getInvocation().getCodeGenOpts().OffloadGlobalFiltering; - if (doConcurrentMappingKind != DoConcurrentMappingKind::DCMK_None && + if (opts.doConcurrentMappingKind != DoConcurrentMappingKind::DCMK_None && !isOpenMPEnabled) { unsigned diagID = ci.getDiagnostics().getCustomDiagID( clang::DiagnosticsEngine::Warning, @@ -349,15 +353,15 @@ bool CodeGenAction::beginSourceFileAction() { } if (isOpenMPEnabled) { - bool isDevice = false; + opts.isTargetDevice = false; if (auto offloadMod = llvm::dyn_cast( mlirModule->getOperation())) - isDevice = offloadMod.getIsTargetDevice(); + opts.isTargetDevice = offloadMod.getIsTargetDevice(); // WARNING: This pipeline must be run immediately after the lowering to // ensure that the FIR is correct with respect to OpenMP operations/ // attributes. - fir::createOpenMPFIRPassPipeline(pm, isDevice, doConcurrentMappingKind); + fir::createOpenMPFIRPassPipeline(pm, opts); } pm.enableVerifier(/*verifyPasses=*/true); @@ -371,7 +375,6 @@ bool CodeGenAction::beginSourceFileAction() { return false; } - // Print initial full MLIR module, before lowering or transformations, if // -save-temps has been specified. if (!saveMLIRTempFile(ci.getInvocation(), *mlirModule, getCurrentFile(), diff --git a/flang/lib/Optimizer/OpenMP/GlobalFiltering.cpp b/flang/lib/Optimizer/OpenMP/GlobalFiltering.cpp index 0ebaf452b9619b..1a38be6476ec00 100644 --- a/flang/lib/Optimizer/OpenMP/GlobalFiltering.cpp +++ b/flang/lib/Optimizer/OpenMP/GlobalFiltering.cpp @@ -29,6 +29,8 @@ namespace flangomp { using namespace mlir; namespace { +// TODO Remove this pass when AOMP moves to `clang-linker-wrapper` (instead of +// `clang-offload-packager`). class GlobalFilteringPass : public flangomp::impl::GlobalFilteringPassBase { public: diff --git a/flang/lib/Optimizer/Passes/Pipelines.cpp b/flang/lib/Optimizer/Passes/Pipelines.cpp index d45c5ceaf20bf0..a53a1150083c11 100644 --- a/flang/lib/Optimizer/Passes/Pipelines.cpp +++ b/flang/lib/Optimizer/Passes/Pipelines.cpp @@ -241,18 +241,19 @@ void createHLFIRToFIRPassPipeline(mlir::PassManager &pm, /// \param pm - MLIR pass manager that will hold the pipeline definition. /// \param isTargetDevice - Whether code is being generated for a target device /// rather than the host device. -void createOpenMPFIRPassPipeline( - mlir::PassManager &pm, bool isTargetDevice, - DoConcurrentMappingKind doConcurrentMappingKind) { - if (doConcurrentMappingKind != DoConcurrentMappingKind::DCMK_None) +void createOpenMPFIRPassPipeline(mlir::PassManager &pm, + OpenMPFIRPassPipelineOpts opts) { + if (opts.doConcurrentMappingKind != DoConcurrentMappingKind::DCMK_None) pm.addPass(flangomp::createDoConcurrentConversionPass( - doConcurrentMappingKind == DoConcurrentMappingKind::DCMK_Device)); + opts.doConcurrentMappingKind == DoConcurrentMappingKind::DCMK_Device)); pm.addPass(flangomp::createMapInfoFinalizationPass()); pm.addPass(flangomp::createMarkDeclareTargetPass()); - if (isTargetDevice) { + if (opts.isTargetDevice) { pm.addPass(flangomp::createFunctionFilteringPass()); - pm.addPass(flangomp::createGlobalFilteringPass()); + + if (opts.enableOffloadGlobalFiltering) + pm.addPass(flangomp::createGlobalFilteringPass()); } } diff --git a/flang/tools/bbc/bbc.cpp b/flang/tools/bbc/bbc.cpp index 50865ed97c899b..49cdc71f1e025c 100644 --- a/flang/tools/bbc/bbc.cpp +++ b/flang/tools/bbc/bbc.cpp @@ -295,15 +295,16 @@ static llvm::LogicalResult runOpenMPPasses(mlir::ModuleOp mlirModule) { using DoConcurrentMappingKind = Fortran::frontend::CodeGenOptions::DoConcurrentMappingKind; - auto doConcurrentMappingKind = + fir::OpenMPFIRPassPipelineOpts opts; + opts.isTargetDevice = enableOpenMPDevice; + opts.doConcurrentMappingKind = llvm::StringSwitch( enableDoConcurrentToOpenMPConversion) .Case("host", DoConcurrentMappingKind::DCMK_Host) .Case("device", DoConcurrentMappingKind::DCMK_Device) .Default(DoConcurrentMappingKind::DCMK_None); - fir::createOpenMPFIRPassPipeline(pm, enableOpenMPDevice, - doConcurrentMappingKind); + fir::createOpenMPFIRPassPipeline(pm, opts); (void)mlir::applyPassManagerCLOptions(pm); if (mlir::failed(pm.run(mlirModule))) { llvm::errs() << "FATAL: failed to correctly apply OpenMP pass pipeline";