Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[flang][OpenMP] Add initial pass to map do concurrent to OMP #48

Merged
merged 2 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions clang/include/clang/Driver/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -6473,6 +6473,10 @@ defm stack_arrays : BoolOptionWithoutMarshalling<"f", "stack-arrays",
defm loop_versioning : BoolOptionWithoutMarshalling<"f", "version-loops-for-stride",
PosFlag<SetTrue, [], [ClangOption], "Create unit-strided versions of loops">,
NegFlag<SetFalse, [], [ClangOption], "Do not create unit-strided loops (default)">>;

def do_concurrent_parallel_EQ : Joined<["-"], "fdo-concurrent-parallel=">,
HelpText<"Try to map `do concurrent` loops to OpenMP (on host or device)">,
Values<"none,host,device">;
} // let Visibility = [FC1Option, FlangOption]

def J : JoinedOrSeparate<["-"], "J">,
Expand Down
3 changes: 2 additions & 1 deletion clang/lib/Driver/ToolChains/Flang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,8 @@ void Flang::addCodegenOptions(const ArgList &Args,
options::OPT_flang_deprecated_no_hlfir,
options::OPT_flang_experimental_polymorphism,
options::OPT_fno_ppc_native_vec_elem_order,
options::OPT_fppc_native_vec_elem_order});
options::OPT_fppc_native_vec_elem_order,
options::OPT_do_concurrent_parallel_EQ});
}

void Flang::addPicOptions(const ArgList &Args, ArgStringList &CmdArgs) const {
Expand Down
2 changes: 2 additions & 0 deletions flang/include/flang/Frontend/CodeGenOptions.def
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,7 @@ ENUM_CODEGENOPT(DebugInfo, llvm::codegenoptions::DebugInfoKind, 4, llvm::codeg
ENUM_CODEGENOPT(VecLib, llvm::driver::VectorLibrary, 3, llvm::driver::VectorLibrary::NoLibrary) ///< Vector functions library to use
ENUM_CODEGENOPT(FramePointer, llvm::FramePointerKind, 2, llvm::FramePointerKind::None) ///< Enable the usage of frame pointers

ENUM_CODEGENOPT(DoConcurrentMapping, DoConcurrentMappingKind, 2, DoConcurrentMappingKind::DCMK_None) ///< Map `do concurrent` to OpenMP

#undef CODEGENOPT
#undef ENUM_CODEGENOPT
8 changes: 8 additions & 0 deletions flang/include/flang/Frontend/CodeGenOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,14 @@ class CodeGenOptions : public CodeGenOptionsBase {
/// transformation.
OptRemark OptimizationRemarkAnalysis;

/// Optionally map `do concurrent` loops to OpenMP. This is only valid of
/// OpenMP is enabled.
enum class DoConcurrentMappingKind {
DCMK_None, // Do not lower `do concurrent` to OpenMP.
DCMK_Host, // Lower to run in parallel on the CPU.
DCMK_Device // Lower to run in parallel on the GPU.
};

// Define accessors/mutators for code generation options of enumeration type.
#define CODEGENOPT(Name, Bits, Default)
#define ENUM_CODEGENOPT(Name, Type, Bits, Default) \
Expand Down
2 changes: 2 additions & 0 deletions flang/include/flang/Optimizer/Transforms/Passes.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ createFunctionAttrPass(FunctionAttrTypes &functionAttr, bool noInfsFPMath,
bool noNaNsFPMath, bool approxFuncFPMath,
bool noSignedZerosFPMath, bool unsafeFPMath);

std::unique_ptr<mlir::Pass> createDoConcurrentConversionPass();

// declarative passes
#define GEN_PASS_REGISTRATION
#include "flang/Optimizer/Transforms/Passes.h.inc"
Expand Down
20 changes: 20 additions & 0 deletions flang/include/flang/Optimizer/Transforms/Passes.td
Original file line number Diff line number Diff line change
Expand Up @@ -397,4 +397,24 @@ def FunctionAttr : Pass<"function-attr", "mlir::func::FuncOp"> {
let constructor = "::fir::createFunctionAttrPass()";
}

def DoConcurrentConversionPass : Pass<"fopenmp-do-concurrent-conversion", "mlir::func::FuncOp"> {
let summary = "Map `DO CONCURRENT` loops to OpenMP worksharing loops.";

let description = [{ This is an experimental pass to map `DO CONCURRENT` loops
to their correspnding equivalent OpenMP worksharing constructs.

For now the following is supported:
- Mapping simple loops to `parallel do`.

Still to TODO:
- More extensive testing.
- Mapping to `target teams distribute parallel do`.
- Allowing the user to control mapping behavior: either to the host or
target.
}];

let constructor = "::fir::createDoConcurrentConversionPass()";
let dependentDialects = ["mlir::omp::OpenMPDialect"];
}

#endif // FLANG_OPTIMIZER_TRANSFORMS_PASSES
28 changes: 28 additions & 0 deletions flang/lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,32 @@ static bool parseDebugArgs(Fortran::frontend::CodeGenOptions &opts,
return true;
}

static bool parseDoConcurrentMapping(Fortran::frontend::CodeGenOptions &opts,
llvm::opt::ArgList &args,
clang::DiagnosticsEngine &diags) {
llvm::opt::Arg *arg =
args.getLastArg(clang::driver::options::OPT_do_concurrent_parallel_EQ);
if (!arg)
return true;

using DoConcurrentMappingKind = Fortran::frontend::CodeGenOptions::DoConcurrentMappingKind;
std::optional<DoConcurrentMappingKind> val =
llvm::StringSwitch<std::optional<DoConcurrentMappingKind>>(
arg->getValue())
.Case("none", DoConcurrentMappingKind::DCMK_None)
.Case("host", DoConcurrentMappingKind::DCMK_Host)
.Case("device", DoConcurrentMappingKind::DCMK_Device)
.Default(std::nullopt);

if (!val.has_value()) {
diags.Report(clang::diag::err_drv_invalid_value)
<< arg->getAsString(args) << arg->getValue();
return false;
}
opts.setDoConcurrentMapping(val.value());
return true;
}

static bool parseVectorLibArg(Fortran::frontend::CodeGenOptions &opts,
llvm::opt::ArgList &args,
clang::DiagnosticsEngine &diags) {
Expand Down Expand Up @@ -385,6 +411,8 @@ static void parseCodeGenArgs(Fortran::frontend::CodeGenOptions &opts,
clang::driver::options::OPT_funderscoring, false)) {
opts.Underscoring = 0;
}

parseDoConcurrentMapping(opts, args, diags);
}

/// Parses all target input arguments and populates the target
Expand Down
29 changes: 27 additions & 2 deletions flang/lib/Frontend/FrontendActions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -320,8 +320,9 @@ bool CodeGenAction::beginSourceFileAction() {
// Add OpenMP-related passes
// WARNING: These passes must be run immediately after the lowering to ensure
// that the FIR is correct with respect to OpenMP operations/attributes.
if (ci.getInvocation().getFrontendOpts().features.IsEnabled(
Fortran::common::LanguageFeature::OpenMP)) {
bool isOpenMPEnabled = ci.getInvocation().getFrontendOpts().features.IsEnabled(
Fortran::common::LanguageFeature::OpenMP);
if (isOpenMPEnabled) {
bool isDevice = false;
if (auto offloadMod = llvm::dyn_cast<mlir::omp::OffloadModuleInterface>(
mlirModule->getOperation()))
Expand All @@ -332,6 +333,30 @@ bool CodeGenAction::beginSourceFileAction() {
fir::createOpenMPFIRPassPipeline(pm, isDevice);
}

using DoConcurrentMappingKind =
Fortran::frontend::CodeGenOptions::DoConcurrentMappingKind;
DoConcurrentMappingKind selectedKind = ci.getInvocation().getCodeGenOpts().getDoConcurrentMapping();
if (selectedKind != DoConcurrentMappingKind::DCMK_None) {
if (!isOpenMPEnabled) {
unsigned diagID = ci.getDiagnostics().getCustomDiagID(
clang::DiagnosticsEngine::Warning,
"lowering `do concurrent` loops to OpenMP is only supported if "
"OpenMP is enabled");
ci.getDiagnostics().Report(diagID);
} else {
bool mapToDevice = selectedKind == DoConcurrentMappingKind::DCMK_Device;

if (mapToDevice) {
unsigned diagID = ci.getDiagnostics().getCustomDiagID(
clang::DiagnosticsEngine::Warning,
"TODO: lowering `do concurrent` loops to OpenMP device is not "
"supported yet");
ci.getDiagnostics().Report(diagID);
} else
pm.addPass(fir::createDoConcurrentConversionPass());
}
}

pm.enableVerifier(/*verifyPasses=*/true);
pm.addPass(std::make_unique<Fortran::lower::VerifierPass>());

Expand Down
1 change: 1 addition & 0 deletions flang/lib/Optimizer/Transforms/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ add_flang_library(FIRTransforms
OMPMarkDeclareTarget.cpp
VScaleAttr.cpp
FunctionAttr.cpp
DoConcurrentConversion.cpp

DEPENDS
FIRDialect
Expand Down
Loading