diff --git a/flang/test/Lower/OpenMP/rtl-flags.f90 b/flang/test/Lower/OpenMP/rtl-flags.f90 new file mode 100644 --- /dev/null +++ b/flang/test/Lower/OpenMP/rtl-flags.f90 @@ -0,0 +1,59 @@ + +!RUN: %flang_fc1 -emit-llvm -fopenmp -fopenmp-is-device %s -o - | FileCheck %s --check-prefix=DEFAULT-DEVICE-LLVM +!RUN: %flang_fc1 -emit-llvm -fopenmp -fopenmp-target-debug -fopenmp-is-device %s -o - | FileCheck %s --check-prefix=DBG-DEVICE-LLVM +!RUN: %flang_fc1 -emit-llvm -fopenmp -fopenmp-target-debug=111 -fopenmp-is-device %s -o - | FileCheck %s --check-prefix=DBG-EQ-DEVICE-LLVM +!RUN: %flang_fc1 -emit-llvm -fopenmp -fopenmp-assume-teams-oversubscription -fopenmp-is-device %s -o - | FileCheck %s --check-prefix=TEAMS-OSUB-DEVICE-LLVM +!RUN: %flang_fc1 -emit-llvm -fopenmp -fopenmp-assume-threads-oversubscription -fopenmp-is-device %s -o - | FileCheck %s --check-prefix=THREAD-OSUB-DEVICE-LLVM +!RUN: %flang_fc1 -emit-llvm -fopenmp -fopenmp-assume-no-thread-state -fopenmp-is-device %s -o - | FileCheck %s --check-prefix=THREAD-STATE-DEVICE-LLVM +!RUN: %flang_fc1 -emit-llvm -fopenmp -fopenmp-assume-no-nested-parallelism -fopenmp-is-device %s -o - | FileCheck %s --check-prefix=NEST-PAR-DEVICE-LLVM +!RUN: %flang_fc1 -emit-llvm -fopenmp -fopenmp-target-debug -fopenmp-assume-teams-oversubscription -fopenmp-assume-no-nested-parallelism -fopenmp-assume-threads-oversubscription -fopenmp-assume-no-thread-state -fopenmp-is-device %s -o - | FileCheck %s --check-prefix=ALL-DEVICE-LLVM + +!DEFAULT-DEVICE-LLVM: @__omp_rtl_debug_kind = weak_odr hidden constant i32 0 +!DEFAULT-DEVICE-LLVM: @__omp_rtl_assume_teams_oversubscription = weak_odr hidden constant i32 0 +!DEFAULT-DEVICE-LLVM: @__omp_rtl_assume_threads_oversubscription = weak_odr hidden constant i32 0 +!DEFAULT-DEVICE-LLVM: @__omp_rtl_assume_no_thread_state = weak_odr hidden constant i32 0 +!DEFAULT-DEVICE-LLVM: @__omp_rtl_assume_no_nested_parallelism = weak_odr hidden constant i32 0 + +!DBG-DEVICE-LLVM: @__omp_rtl_debug_kind = weak_odr hidden constant i32 1 +!DBG-DEVICE-LLVM: @__omp_rtl_assume_teams_oversubscription = weak_odr hidden constant i32 0 +!DBG-DEVICE-LLVM: @__omp_rtl_assume_threads_oversubscription = weak_odr hidden constant i32 0 +!DBG-DEVICE-LLVM: @__omp_rtl_assume_no_thread_state = weak_odr hidden constant i32 0 +!DBG-DEVICE-LLVM: @__omp_rtl_assume_no_nested_parallelism = weak_odr hidden constant i32 0 + +!DBG-EQ-DEVICE-LLVM: @__omp_rtl_debug_kind = weak_odr hidden constant i32 111 +!DBG-EQ-DEVICE-LLVM: @__omp_rtl_assume_teams_oversubscription = weak_odr hidden constant i32 0 +!DBG-EQ-DEVICE-LLVM: @__omp_rtl_assume_threads_oversubscription = weak_odr hidden constant i32 0 +!DBG-EQ-DEVICE-LLVM: @__omp_rtl_assume_no_thread_state = weak_odr hidden constant i32 0 +!DBG-EQ-DEVICE-LLVM: @__omp_rtl_assume_no_nested_parallelism = weak_odr hidden constant i32 0 + +!TEAMS-OSUB-DEVICE-LLVM: @__omp_rtl_debug_kind = weak_odr hidden constant i32 0 +!TEAMS-OSUB-DEVICE-LLVM: @__omp_rtl_assume_teams_oversubscription = weak_odr hidden constant i32 1 +!TEAMS-OSUB-DEVICE-LLVM: @__omp_rtl_assume_threads_oversubscription = weak_odr hidden constant i32 0 +!TEAMS-OSUB-DEVICE-LLVM: @__omp_rtl_assume_no_thread_state = weak_odr hidden constant i32 0 +!TEAMS-OSUB-DEVICE-LLVM: @__omp_rtl_assume_no_nested_parallelism = weak_odr hidden constant i32 0 + +!THREAD-OSUB-DEVICE-LLVM: @__omp_rtl_debug_kind = weak_odr hidden constant i32 0 +!THREAD-OSUB-DEVICE-LLVM: @__omp_rtl_assume_teams_oversubscription = weak_odr hidden constant i32 0 +!THREAD-OSUB-DEVICE-LLVM: @__omp_rtl_assume_threads_oversubscription = weak_odr hidden constant i32 1 +!THREAD-OSUB-DEVICE-LLVM: @__omp_rtl_assume_no_thread_state = weak_odr hidden constant i32 0 +!THREAD-OSUB-DEVICE-LLVM: @__omp_rtl_assume_no_nested_parallelism = weak_odr hidden constant i32 0 + +!THREAD-STATE-DEVICE-LLVM: @__omp_rtl_debug_kind = weak_odr hidden constant i32 0 +!THREAD-STATE-DEVICE-LLVM: @__omp_rtl_assume_teams_oversubscription = weak_odr hidden constant i32 0 +!THREAD-STATE-DEVICE-LLVM: @__omp_rtl_assume_threads_oversubscription = weak_odr hidden constant i32 0 +!THREAD-STATE-DEVICE-LLVM: @__omp_rtl_assume_no_thread_state = weak_odr hidden constant i32 1 +!THREAD-STATE-DEVICE-LLVM: @__omp_rtl_assume_no_nested_parallelism = weak_odr hidden constant i32 0 + +!NEST-PAR-DEVICE-LLVM: @__omp_rtl_debug_kind = weak_odr hidden constant i32 0 +!NEST-PAR-DEVICE-LLVM: @__omp_rtl_assume_teams_oversubscription = weak_odr hidden constant i32 0 +!NEST-PAR-DEVICE-LLVM: @__omp_rtl_assume_threads_oversubscription = weak_odr hidden constant i32 0 +!NEST-PAR-DEVICE-LLVM: @__omp_rtl_assume_no_thread_state = weak_odr hidden constant i32 0 +!NEST-PAR-DEVICE-LLVM: @__omp_rtl_assume_no_nested_parallelism = weak_odr hidden constant i32 1 + +!ALL-DEVICE-LLVM: @__omp_rtl_debug_kind = weak_odr hidden constant i32 1 +!ALL-DEVICE-LLVM: @__omp_rtl_assume_teams_oversubscription = weak_odr hidden constant i32 1 +!ALL-DEVICE-LLVM: @__omp_rtl_assume_threads_oversubscription = weak_odr hidden constant i32 1 +!ALL-DEVICE-LLVM: @__omp_rtl_assume_no_thread_state = weak_odr hidden constant i32 1 +!ALL-DEVICE-LLVM: @__omp_rtl_assume_no_nested_parallelism = weak_odr hidden constant i32 1 +subroutine omp_subroutine() +end subroutine omp_subroutine diff --git a/mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp b/mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp --- a/mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp +++ b/mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp @@ -1542,6 +1542,38 @@ return bodyGenStatus; } +/// Lowers the FlagsAttr which is applied to the module on the device +/// pass when offloading, this attribute contains OpenMP RTL globals that can +/// be passed as flags to the frontend, otherwise they are set to default +LogicalResult convertFlagsAttr(Operation *op, mlir::omp::FlagsAttr attribute, + LLVM::ModuleTranslation &moduleTranslation) { + llvm::OpenMPIRBuilder *ompBuilder = moduleTranslation.getOpenMPBuilder(); + + ompBuilder->createGlobalFlag( + attribute.getDebugKind() /*LangOpts().OpenMPTargetDebug*/, + "__omp_rtl_debug_kind"); + ompBuilder->createGlobalFlag( + attribute + .getAssumeTeamsOversubscription() /*LangOpts().OpenMPTeamSubscription*/ + , + "__omp_rtl_assume_teams_oversubscription"); + ompBuilder->createGlobalFlag( + attribute + .getAssumeThreadsOversubscription() /*LangOpts().OpenMPThreadSubscription*/ + , + "__omp_rtl_assume_threads_oversubscription"); + ompBuilder->createGlobalFlag( + attribute.getAssumeNoThreadState() /*LangOpts().OpenMPNoThreadState*/, + "__omp_rtl_assume_no_thread_state"); + ompBuilder->createGlobalFlag( + attribute + .getAssumeNoNestedParallelism() /*LangOpts().OpenMPNoNestedParallelism*/ + , + "__omp_rtl_assume_no_nested_parallelism"); + + return success(); +} + namespace { /// Implementation of the dialect interface that converts operations belonging @@ -1556,10 +1588,34 @@ LogicalResult convertOperation(Operation *op, llvm::IRBuilderBase &builder, LLVM::ModuleTranslation &moduleTranslation) const final; + + LogicalResult + amendOperation(Operation *op, NamedAttribute attribute, + LLVM::ModuleTranslation &moduleTranslation) const final; }; } // namespace +/// Given an OpenMP MLIR attribute, create the corresponding LLVM-IR, runtime +/// calls, or operation amendments +LogicalResult OpenMPDialectLLVMIRTranslationInterface::amendOperation( + Operation *op, NamedAttribute attribute, + LLVM::ModuleTranslation &moduleTranslation) const { + + return llvm::TypeSwitch(attribute.getValue()) + .Case([&](mlir::omp::FlagsAttr rtlAttr) { + return convertFlagsAttr(op, rtlAttr, moduleTranslation); + }) + .Default([&](Attribute attr) { + // fall through for omp attributes that do not require lowering and/or + // have no concrete definition and thus no type to define a case on + // e.g. omp.is_device + return success(); + }); + + return failure(); +} + /// Given an OpenMP MLIR operation, create the corresponding LLVM IR /// (including OpenMP runtime calls). LogicalResult OpenMPDialectLLVMIRTranslationInterface::convertOperation( diff --git a/mlir/test/Target/LLVMIR/openmp-llvm.mlir b/mlir/test/Target/LLVMIR/openmp-llvm.mlir --- a/mlir/test/Target/LLVMIR/openmp-llvm.mlir +++ b/mlir/test/Target/LLVMIR/openmp-llvm.mlir @@ -2454,3 +2454,86 @@ } llvm.return } + +// ----- + +// CHECK-NOT: @__omp_rtl_debug_kind = weak_odr hidden constant i32 1 +// CHECK-NOT: @__omp_rtl_assume_teams_oversubscription = weak_odr hidden constant i32 1 +// CHECK-NOT: @__omp_rtl_assume_threads_oversubscription = weak_odr hidden constant i32 1 +// CHECK-NOT: @__omp_rtl_assume_no_thread_state = weak_odr hidden constant i32 1 +// CHECK-NOT: @__omp_rtl_assume_no_nested_parallelism = weak_odr hidden constant i32 1 +module attributes {omp.flags = #omp.flags} {} + +// ----- + +// CHECK: @__omp_rtl_debug_kind = weak_odr hidden constant i32 1 +// CHECK: @__omp_rtl_assume_teams_oversubscription = weak_odr hidden constant i32 1 +// CHECK: @__omp_rtl_assume_threads_oversubscription = weak_odr hidden constant i32 1 +// CHECK: @__omp_rtl_assume_no_thread_state = weak_odr hidden constant i32 1 +// CHECK: @__omp_rtl_assume_no_nested_parallelism = weak_odr hidden constant i32 1 +module attributes {omp.flags = #omp.flags, + omp.is_device = #omp.isdevice} {} + +// ----- + +// CHECK-NOT: @__omp_rtl_debug_kind = weak_odr hidden constant i32 0 +// CHECK-NOT: @__omp_rtl_assume_teams_oversubscription = weak_odr hidden constant i32 0 +// CHECK-NOT: @__omp_rtl_assume_threads_oversubscription = weak_odr hidden constant i32 0 +// CHECK-NOT: @__omp_rtl_assume_no_thread_state = weak_odr hidden constant i32 0 +// CHECK-NOT: @__omp_rtl_assume_no_nested_parallelism = weak_odr hidden constant i32 0 +module attributes {omp.flags = #omp.flags<>} {} + +// ----- + +// CHECK: @__omp_rtl_debug_kind = weak_odr hidden constant i32 0 +// CHECK: @__omp_rtl_assume_teams_oversubscription = weak_odr hidden constant i32 0 +// CHECK: @__omp_rtl_assume_threads_oversubscription = weak_odr hidden constant i32 0 +// CHECK: @__omp_rtl_assume_no_thread_state = weak_odr hidden constant i32 0 +// CHECK: @__omp_rtl_assume_no_nested_parallelism = weak_odr hidden constant i32 0 +module attributes {omp.flags = #omp.flags<>, omp.is_device = #omp.isdevice} {} + +// ----- + +// CHECK-NOT: @__omp_rtl_debug_kind = weak_odr hidden constant i32 0 +// CHECK-NOT: @__omp_rtl_assume_teams_oversubscription = weak_odr hidden constant i32 0 +// CHECK-NOT: @__omp_rtl_assume_threads_oversubscription = weak_odr hidden constant i32 0 +// CHECK-NOT: @__omp_rtl_assume_no_thread_state = weak_odr hidden constant i32 0 +// CHECK-NOT: @__omp_rtl_assume_no_nested_parallelism = weak_odr hidden constant i32 0 +module attributes {omp.flags = #omp.flags} {} + +// ----- + +// CHECK: @__omp_rtl_debug_kind = weak_odr hidden constant i32 0 +// CHECK: @__omp_rtl_assume_teams_oversubscription = weak_odr hidden constant i32 0 +// CHECK: @__omp_rtl_assume_threads_oversubscription = weak_odr hidden constant i32 0 +// CHECK: @__omp_rtl_assume_no_thread_state = weak_odr hidden constant i32 0 +// CHECK: @__omp_rtl_assume_no_nested_parallelism = weak_odr hidden constant i32 0 +module attributes {omp.flags = #omp.flags, + omp.is_device = #omp.isdevice} {} + +// ----- + +// CHECK-NOT: @__omp_rtl_debug_kind = weak_odr hidden constant i32 0 +// CHECK-NOT: @__omp_rtl_assume_teams_oversubscription = weak_odr hidden constant i32 1 +// CHECK-NOT: @__omp_rtl_assume_threads_oversubscription = weak_odr hidden constant i32 0 +// CHECK-NOT: @__omp_rtl_assume_no_thread_state = weak_odr hidden constant i32 1 +// CHECK-NOT: @__omp_rtl_assume_no_nested_parallelism = weak_odr hidden constant i32 0 +module attributes {omp.flags = #omp.flags} {} + +// ----- + +// CHECK: @__omp_rtl_debug_kind = weak_odr hidden constant i32 0 +// CHECK: @__omp_rtl_assume_teams_oversubscription = weak_odr hidden constant i32 1 +// CHECK: @__omp_rtl_assume_threads_oversubscription = weak_odr hidden constant i32 0 +// CHECK: @__omp_rtl_assume_no_thread_state = weak_odr hidden constant i32 1 +// CHECK: @__omp_rtl_assume_no_nested_parallelism = weak_odr hidden constant i32 0 +module attributes {omp.flags = #omp.flags, + omp.is_device = #omp.isdevice} {}