Skip to content

Commit

Permalink
[flang][OpenMP] Add flag to enable/disable GlobalFilteringPass
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ergawy committed Oct 15, 2024
1 parent 40a16e7 commit feee258
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 20 deletions.
1 change: 1 addition & 0 deletions clang/include/clang/Driver/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -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<SetTrue, [], [ClangOption], "Specifies PowerPC native vector element order (default)">,
NegFlag<SetFalse, [], [ClangOption], "Specifies PowerPC non-native vector element order">>;
Expand Down
4 changes: 3 additions & 1 deletion clang/lib/Driver/ToolChains/Flang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
1 change: 1 addition & 0 deletions flang/include/flang/Frontend/CodeGenOptions.def
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 8 additions & 3 deletions flang/include/flang/Optimizer/Passes/Pipelines.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand All @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions flang/lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
15 changes: 9 additions & 6 deletions flang/lib/Frontend/FrontendActions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -349,15 +353,15 @@ bool CodeGenAction::beginSourceFileAction() {
}

if (isOpenMPEnabled) {
bool isDevice = false;
opts.isTargetDevice = false;
if (auto offloadMod = llvm::dyn_cast<mlir::omp::OffloadModuleInterface>(
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);
Expand All @@ -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(),
Expand Down
2 changes: 2 additions & 0 deletions flang/lib/Optimizer/OpenMP/GlobalFiltering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<GlobalFilteringPass> {
public:
Expand Down
15 changes: 8 additions & 7 deletions flang/lib/Optimizer/Passes/Pipelines.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

Expand Down
7 changes: 4 additions & 3 deletions flang/tools/bbc/bbc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<DoConcurrentMappingKind>(
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";
Expand Down

0 comments on commit feee258

Please sign in to comment.