Skip to content

[flang][flang-driver][mlir][OpenMP] atomic control support #143441

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
22 changes: 11 additions & 11 deletions clang/include/clang/Driver/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -2303,21 +2303,21 @@ def fsymbol_partition_EQ : Joined<["-"], "fsymbol-partition=">, Group<f_Group>,

defm atomic_remote_memory : BoolFOption<"atomic-remote-memory",
LangOpts<"AtomicRemoteMemory">, DefaultFalse,
PosFlag<SetTrue, [], [ClangOption, CC1Option], "May have">,
NegFlag<SetFalse, [], [ClangOption], "Assume no">,
BothFlags<[], [ClangOption], " atomic operations on remote memory">>;
PosFlag<SetTrue, [], [ClangOption, CC1Option, FlangOption, FC1Option], "May have">,
NegFlag<SetFalse, [], [ClangOption, FlangOption], "Assume no">,
BothFlags<[], [ClangOption, FlangOption], " atomic operations on remote memory">>;

defm atomic_fine_grained_memory : BoolFOption<"atomic-fine-grained-memory",
LangOpts<"AtomicFineGrainedMemory">, DefaultFalse,
PosFlag<SetTrue, [], [ClangOption, CC1Option], "May have">,
NegFlag<SetFalse, [], [ClangOption], "Assume no">,
BothFlags<[], [ClangOption], " atomic operations on fine-grained memory">>;
PosFlag<SetTrue, [], [ClangOption, CC1Option, FlangOption, FC1Option], "May have">,
NegFlag<SetFalse, [], [ClangOption, FlangOption], "Assume no">,
BothFlags<[], [ClangOption, FlangOption], " atomic operations on fine-grained memory">>;

defm atomic_ignore_denormal_mode : BoolFOption<"atomic-ignore-denormal-mode",
LangOpts<"AtomicIgnoreDenormalMode">, DefaultFalse,
PosFlag<SetTrue, [], [ClangOption, CC1Option], "Allow">,
NegFlag<SetFalse, [], [ClangOption], "Disallow">,
BothFlags<[], [ClangOption], " atomic operations to ignore denormal mode">>;
PosFlag<SetTrue, [], [ClangOption, CC1Option, FlangOption, FC1Option], "Allow">,
NegFlag<SetFalse, [], [ClangOption, FlangOption], "Disallow">,
BothFlags<[], [ClangOption, FlangOption], " atomic operations to ignore denormal mode">>;

defm memory_profile : OptInCC1FFlag<"memory-profile", "Enable", "Disable", " heap memory profiling">;
def fmemory_profile_EQ : Joined<["-"], "fmemory-profile=">,
Expand Down Expand Up @@ -5305,9 +5305,9 @@ defm amdgpu_precise_memory_op
" precise memory mode (AMDGPU only)">;

def munsafe_fp_atomics : Flag<["-"], "munsafe-fp-atomics">,
Visibility<[ClangOption, CC1Option]>, Alias<fatomic_ignore_denormal_mode>;
Visibility<[ClangOption, CC1Option, FlangOption, FC1Option]>, Alias<fatomic_ignore_denormal_mode>;
def mno_unsafe_fp_atomics : Flag<["-"], "mno-unsafe-fp-atomics">,
Visibility<[ClangOption]>, Alias<fno_atomic_ignore_denormal_mode>;
Visibility<[ClangOption, FlangOption]>, Alias<fno_atomic_ignore_denormal_mode>;

def faltivec : Flag<["-"], "faltivec">, Group<f_Group>;
def fno_altivec : Flag<["-"], "fno-altivec">, Group<f_Group>;
Expand Down
5 changes: 5 additions & 0 deletions flang/include/flang/Frontend/TargetOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ class TargetOptions {

/// Print verbose assembly
bool asmVerbose = false;

/// Atomic control options for AMD gpu
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// Atomic control options for AMD gpu
/// Atomic control options

bool ignoreDenormalMode = false;
bool remoteMemory = false;
bool fineGrainedMemory = false;
Comment on lines +58 to +60
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I think it makes sense to include "atomic" within variables names and also associated getters/setters and attributes, to avoid confusion as to what these flags apply to.

Suggested change
bool ignoreDenormalMode = false;
bool remoteMemory = false;
bool fineGrainedMemory = false;
bool atomicIgnoreDenormalMode = false;
bool atomicRemoteMemory = false;
bool atomicFineGrainedMemory = false;

};

} // end namespace Fortran::frontend
Expand Down
8 changes: 8 additions & 0 deletions flang/include/flang/Optimizer/Dialect/Support/FIRContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ void setTargetCPU(mlir::ModuleOp mod, llvm::StringRef cpu);
/// Get the target CPU string from the Module or return a null reference.
llvm::StringRef getTargetCPU(mlir::ModuleOp mod);

// Setters and getters for atomic control options.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: All other getters/setters here are documented individually using doxygen-formatted comments. I'd suggest doing the same, even if a chunk of the description is going to repeat across multiple functions.

void setIgnoreDenormalMode(mlir::ModuleOp mod);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Setters should allow us to specify a value. Their implementation, when set to false would have to remove the associated unit attribute if it's present.

Suggested change
void setIgnoreDenormalMode(mlir::ModuleOp mod);
void setIgnoreDenormalMode(mlir::ModuleOp mod, bool value);

bool getIgnoreDenormalMode(mlir::ModuleOp mod);
void setFineGrainedMemory(mlir::ModuleOp mod);
bool getFineGrainedMemory(mlir::ModuleOp mod);
void setRemoteMemory(mlir::ModuleOp mod);
bool getRemoteMemory(mlir::ModuleOp mod);

/// Set the tune CPU for the module. `cpu` must not be deallocated while
/// module `mod` is still live.
void setTuneCPU(mlir::ModuleOp mod, llvm::StringRef cpu);
Expand Down
10 changes: 10 additions & 0 deletions flang/lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,16 @@ static void parseTargetArgs(TargetOptions &opts, llvm::opt::ArgList &args) {
args.getLastArg(clang::driver::options::OPT_triple))
opts.triple = a->getValue();

opts.ignoreDenormalMode = args.hasFlag(
clang::driver::options::OPT_fatomic_ignore_denormal_mode,
clang::driver::options::OPT_fno_atomic_ignore_denormal_mode, false);
opts.fineGrainedMemory = args.hasFlag(
clang::driver::options::OPT_fatomic_fine_grained_memory,
clang::driver::options::OPT_fno_atomic_fine_grained_memory, false);
opts.remoteMemory =
args.hasFlag(clang::driver::options::OPT_fatomic_remote_memory,
clang::driver::options::OPT_fno_atomic_remote_memory, false);

if (const llvm::opt::Arg *a =
args.getLastArg(clang::driver::options::OPT_target_cpu))
opts.cpu = a->getValue();
Expand Down
6 changes: 6 additions & 0 deletions flang/lib/Lower/Bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6654,6 +6654,12 @@ Fortran::lower::LoweringBridge::LoweringBridge(
fir::setKindMapping(*module, kindMap);
fir::setTargetCPU(*module, targetMachine.getTargetCPU());
fir::setTuneCPU(*module, targetOpts.cpuToTuneFor);
if (targetOpts.ignoreDenormalMode)
fir::setIgnoreDenormalMode(*module);
Comment on lines +6657 to +6658
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (targetOpts.ignoreDenormalMode)
fir::setIgnoreDenormalMode(*module);
fir::setIgnoreDenormalMode(*module, targetOpts.ignoreDenormalMode);

if (targetOpts.fineGrainedMemory)
fir::setFineGrainedMemory(*module);
if (targetOpts.remoteMemory)
fir::setRemoteMemory(*module);
fir::setTargetFeatures(*module, targetMachine.getTargetFeatureString());
fir::support::setMLIRDataLayout(*module, targetMachine.createDataLayout());
fir::setIdent(*module, Fortran::common::getFlangFullVersion());
Expand Down
8 changes: 7 additions & 1 deletion flang/lib/Lower/OpenMP/OpenMP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2979,9 +2979,15 @@ genAtomicUpdate(lower::AbstractConverter &converter,
}
}

mlir::ModuleOp module = builder.getModule();
mlir::omp::AtomicControlAttr atomicControlAttr =
mlir::omp::AtomicControlAttr::get(
builder.getContext(), fir::getIgnoreDenormalMode(module),
fir::getFineGrainedMemory(module), fir::getRemoteMemory(module));
builder.restoreInsertionPoint(atomicAt);
auto updateOp = builder.create<mlir::omp::AtomicUpdateOp>(
loc, atomAddr, hint, makeMemOrderAttr(converter, memOrder));
loc, atomAddr, atomicControlAttr, hint,
makeMemOrderAttr(converter, memOrder));

mlir::Region &region = updateOp->getRegion(0);
mlir::Block *block = builder.createBlock(&region, {}, {atomType}, {loc});
Expand Down
30 changes: 30 additions & 0 deletions flang/lib/Optimizer/Dialect/Support/FIRContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,36 @@ void fir::setTuneCPU(mlir::ModuleOp mod, llvm::StringRef cpu) {
mod->setAttr(tuneCpuName, mlir::StringAttr::get(ctx, cpu));
}

static constexpr const char *ignoreDenormalModeName =
"fir.ignore.denormal.mode";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Generally, dots are used to separate "scopes" in some hierarchy. I'd suggest following the <dialect> <dot> <option name> convention here.

Suggested change
"fir.ignore.denormal.mode";
"fir.atomic_ignore_denormal_mode";

void fir::setIgnoreDenormalMode(mlir::ModuleOp mod) {
auto *ctx = mod.getContext();
mod->setAttr(ignoreDenormalModeName, mlir::UnitAttr::get(ctx));
}

bool fir::getIgnoreDenormalMode(mlir::ModuleOp mod) {
return mod->hasAttrOfType<mlir::UnitAttr>(ignoreDenormalModeName);
}

static constexpr const char *fineGrainedMemoryName = "fir.fine.grained.memory";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
static constexpr const char *fineGrainedMemoryName = "fir.fine.grained.memory";
static constexpr const char *fineGrainedMemoryName = "fir.atomic_fine_grained_memory";

void fir::setFineGrainedMemory(mlir::ModuleOp mod) {
auto *ctx = mod.getContext();
mod->setAttr(fineGrainedMemoryName, mlir::UnitAttr::get(ctx));
}

bool fir::getFineGrainedMemory(mlir::ModuleOp mod) {
return mod->hasAttrOfType<mlir::UnitAttr>(fineGrainedMemoryName);
}
static constexpr const char *remoteMemoryName = "fir.remote.memory";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
static constexpr const char *remoteMemoryName = "fir.remote.memory";
static constexpr const char *remoteMemoryName = "fir.atomic_remote_memory";

void fir::setRemoteMemory(mlir::ModuleOp mod) {
auto *ctx = mod.getContext();
mod->setAttr(remoteMemoryName, mlir::UnitAttr::get(ctx));
}

bool fir::getRemoteMemory(mlir::ModuleOp mod) {
return mod->hasAttrOfType<mlir::UnitAttr>(remoteMemoryName);
}

llvm::StringRef fir::getTuneCPU(mlir::ModuleOp mod) {
if (auto attr = mod->getAttrOfType<mlir::StringAttr>(tuneCpuName))
return attr.getValue();
Expand Down
4 changes: 2 additions & 2 deletions flang/test/Fir/convert-to-llvm-openmp-and-fir.fir
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@ func.func @_QPs() {
%c1_i32 = arith.constant 1 : i32
%1 = arith.addi %arg0, %c1_i32 : i32
omp.yield(%1 : i32)
}
} {atomic_control = #omp.atomic_control<>}
return
}
fir.global internal @_QFsEc : i32 {
Expand All @@ -634,7 +634,7 @@ fir.global internal @_QFsEc : i32 {
// CHECK: %[[CONST_1:.*]] = llvm.mlir.constant(1 : i32) : i32
// CHECK: %[[OUT_VAL:.*]] = llvm.add %[[IN_VAL]], %[[CONST_1]] : i32
// CHECK: omp.yield(%[[OUT_VAL]] : i32)
// CHECK: }
// CHECK: } {atomic_control = #omp.atomic_control<>}
// CHECK: llvm.return
// CHECK: }
// CHECK: llvm.mlir.global internal @[[GLOBAL]]() {{.*}} : i32 {
Expand Down
37 changes: 37 additions & 0 deletions flang/test/Lower/OpenMP/atomic-control-options.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
! RUN: %flang_fc1 -emit-hlfir -triple amdgcn-amd-amdhsa -fopenmp -fopenmp-is-device -munsafe-fp-atomics %s -o - | FileCheck -check-prefix=UNSAFE-FP-ATOMICS %s
! RUN: %flang_fc1 -emit-hlfir -triple amdgcn-amd-amdhsa -fopenmp -fopenmp-is-device -fatomic-ignore-denormal-mode %s -o - | FileCheck -check-prefix=IGNORE-DENORMAL %s
! RUN: %flang_fc1 -emit-hlfir -triple amdgcn-amd-amdhsa -fopenmp -fopenmp-is-device -fatomic-fine-grained-memory %s -o - | FileCheck -check-prefix=FINE-GRAINED-MEMORY %s
! RUN: %flang_fc1 -emit-hlfir -triple amdgcn-amd-amdhsa -fopenmp -fopenmp-is-device -fatomic-remote-memory %s -o - | FileCheck -check-prefix=REMOTE-MEMORY %s
program test
implicit none
integer :: A, B, threads
threads = 128
A = 0
B = 0
!UNSAFE-FP-ATOMICS: omp.atomic.update %{{.*}} : !fir.ref<i32> {
!UNSAFE-FP-ATOMICS: } {atomic_control = #omp.atomic_control<ignore_denormal_mode = true>}
!IGNORE-DENORMAL: omp.atomic.update %{{.*}} : !fir.ref<i32> {
!IGNORE-DENORMAL: } {atomic_control = #omp.atomic_control<ignore_denormal_mode = true>}
!FINE-GRAINED-MEMORY: omp.atomic.update %{{.*}} : !fir.ref<i32> {
!FINE-GRAINED-MEMORY: } {atomic_control = #omp.atomic_control<fine_grained_memory = true>}
!REMOTE-MEMORY: omp.atomic.update %{{.*}} : !fir.ref<i32> {
!REMOTE-MEMORY: } {atomic_control = #omp.atomic_control<remote_memory = true>}
!$omp target parallel num_threads(threads)
!$omp atomic
A = A + 1
!$omp end target parallel
!UNSAFE-FP-ATOMICS: omp.atomic.update %{{.*}} : !fir.ref<i32> {
!UNSAFE-FP-ATOMICS: } {atomic_control = #omp.atomic_control<ignore_denormal_mode = true>}
!IGNORE-DENORMAL: omp.atomic.update %{{.*}} : !fir.ref<i32> {
!IGNORE-DENORMAL: } {atomic_control = #omp.atomic_control<ignore_denormal_mode = true>}
!FINE-GRAINED-MEMORY: omp.atomic.update %{{.*}} : !fir.ref<i32> {
!FINE-GRAINED-MEMORY: } {atomic_control = #omp.atomic_control<fine_grained_memory = true>}
!REMOTE-MEMORY: omp.atomic.update %{{.*}} : !fir.ref<i32> {
!REMOTE-MEMORY: } {atomic_control = #omp.atomic_control<remote_memory = true>}
!$omp target parallel num_threads(threads)
!$omp atomic capture
A = A + B
B = A
!$omp end atomic
!$omp end target parallel
end program test
15 changes: 15 additions & 0 deletions mlir/include/mlir/Dialect/OpenMP/OpenMPAttrDefs.td
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,21 @@ def FlagsAttr : OpenMP_Attr<"Flags", "flags"> {
let assemblyFormat = "`<` struct(params) `>`";
}

//===----------------------------------------------------------------------===//
// AtomicControlAttr
//===----------------------------------------------------------------------===//

// Atomic control attributes hold information about architectural
// characteristics which are required for lowering atomic operations.
def AtomicControlAttr : OpenMP_Attr<"AtomicControl", "atomic_control"> {
let parameters =
(ins DefaultValuedParameter<"bool", "false">:$ignore_denormal_mode,
DefaultValuedParameter<"bool", "false">:$fine_grained_memory,
DefaultValuedParameter<"bool", "false">:$remote_memory);

let assemblyFormat = "`<` struct(params) `>`";
}

Comment on lines +57 to +71
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: This definition should go before DeclareTargetAttr, since this file is alphabetically sorted.

//===----------------------------------------------------------------------===//
// TaskDependArrayAttr
//===----------------------------------------------------------------------===//
Expand Down
8 changes: 5 additions & 3 deletions mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -1543,9 +1543,11 @@ def AtomicUpdateOp : OpenMP_Op<"atomic.update", traits = [
operations.
}] # clausesDescription;

let arguments = !con((ins Arg<OpenMP_PointerLikeType,
"Address of variable to be updated",
[MemRead, MemWrite]>:$x), clausesArgs);
let arguments = !con(
(ins Arg<OpenMP_PointerLikeType,
"Address of variable to be updated", [MemRead, MemWrite]>:$x,
AtomicControlAttr:$atomic_control),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why have you made this attribute required? There's a clear default for it (all flags set to false) and, even in the unit tests you had to update because of this choice, the attribute is specified but empty.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this new attribute only actually apply to omp.atomic.update or should we be adding it to the other atomic operations (omp.atomic.read, omp.atomic.write and omp.atomic.capture)?

clausesArgs);

// Override region definition.
let regions = (region SizedRegion<1>:$region);
Expand Down
2 changes: 1 addition & 1 deletion mlir/test/Conversion/OpenMPToLLVM/convert-to-llvmir.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func.func @atomic_update() {
%1 = arith.constant 1 : i32
%2 = arith.addi %arg0, %1 : i32
omp.yield(%2 : i32)
}
} {atomic_control = #omp.atomic_control<>}
return
}
llvm.mlir.global internal @_QFsEc() : i32 {
Expand Down
10 changes: 5 additions & 5 deletions mlir/test/Dialect/OpenMP/canonicalize.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ func.func @update_no_op(%x : memref<i32>) {
omp.atomic.update %x : memref<i32> {
^bb0(%xval : i32):
omp.yield(%xval : i32)
}
} {atomic_control = #omp.atomic_control<>}
return
}

Expand All @@ -17,7 +17,7 @@ func.func @update_write_op(%x : memref<i32>, %value: i32) {
omp.atomic.update %x : memref<i32> {
^bb0(%xval : i32):
omp.yield(%value : i32)
}
} {atomic_control = #omp.atomic_control<>}
return
}

Expand All @@ -33,7 +33,7 @@ func.func @update_normal(%x : memref<i32>, %value: i32) {
^bb0(%xval : i32):
%newval = arith.addi %xval, %value : i32
omp.yield(%newval : i32)
}
} {atomic_control = #omp.atomic_control<>}
return
}

Expand All @@ -50,7 +50,7 @@ func.func @update_unnecessary_computations(%x: memref<i32>) {
^bb0(%xval: i32):
%newval = arith.addi %xval, %c0 : i32
omp.yield(%newval: i32)
}
} {atomic_control = #omp.atomic_control<>}
return
}

Expand All @@ -65,7 +65,7 @@ func.func @update_unnecessary_computations(%x: memref<i32>) {
^bb0(%xval: i32):
%newval = arith.muli %xval, %c0 : i32
omp.yield(%newval: i32)
}
} {atomic_control = #omp.atomic_control<>}
return
}

Expand Down
Loading