[flang-commits] [flang] bdbc942 - [MLIR][OpenMP][Flang] Set OpenMP target attributes in MLIR module

Dominik Adamski via flang-commits flang-commits at lists.llvm.org
Fri Mar 31 03:53:27 PDT 2023


Author: Dominik Adamski
Date: 2023-03-31T05:44:16-05:00
New Revision: bdbc9429fcc19b61da8f4c24584ea3927f251348

URL: https://github.com/llvm/llvm-project/commit/bdbc9429fcc19b61da8f4c24584ea3927f251348
DIFF: https://github.com/llvm/llvm-project/commit/bdbc9429fcc19b61da8f4c24584ea3927f251348.diff

LOG: [MLIR][OpenMP][Flang] Set OpenMP target attributes in MLIR module

Scope of changes:
  1) Add attribute to OpenMP MLIR dialect which stores target cpu and
     target features
  2) Store target information in MLIR module

Differential Revision: https://reviews.llvm.org/D146612

Reviewed By: kiranchandramohan

Co-authored-by: Kiran Chandramohan <kiran.chandramohan at arm.com>

Added: 
    flang/test/Lower/OpenMP/target_cpu_features.f90

Modified: 
    flang/include/flang/Tools/CrossToolHelpers.h
    flang/lib/Frontend/FrontendActions.cpp
    mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td
    mlir/include/mlir/Dialect/OpenMP/OpenMPOpsInterfaces.td

Removed: 
    


################################################################################
diff  --git a/flang/include/flang/Tools/CrossToolHelpers.h b/flang/include/flang/Tools/CrossToolHelpers.h
index 16fee1ae3c3f4..63b6c9955d9f6 100644
--- a/flang/include/flang/Tools/CrossToolHelpers.h
+++ b/flang/include/flang/Tools/CrossToolHelpers.h
@@ -27,4 +27,15 @@ void setOffloadModuleInterfaceAttributes(
   }
 }
 
+//  Shares assinging of the OpenMP OffloadModuleInterface and its TargetCPU
+//  attribute accross Flang tools (bbc/flang)
+void setOffloadModuleInterfaceTargetAttribute(mlir::ModuleOp &module,
+    llvm::StringRef targetCPU, llvm::StringRef targetFeatures) {
+  // Should be registered by the OpenMPDialect
+  if (auto offloadMod = llvm::dyn_cast<mlir::omp::OffloadModuleInterface>(
+          module.getOperation())) {
+    offloadMod.setTarget(targetCPU, targetFeatures);
+  }
+}
+
 #endif // FORTRAN_TOOLS_CROSS_TOOL_HELPERS_H

diff  --git a/flang/lib/Frontend/FrontendActions.cpp b/flang/lib/Frontend/FrontendActions.cpp
index 3e8bebd10d3ec..e09968cdf6b50 100644
--- a/flang/lib/Frontend/FrontendActions.cpp
+++ b/flang/lib/Frontend/FrontendActions.cpp
@@ -276,14 +276,17 @@ bool CodeGenAction::beginSourceFileAction() {
   // Fetch module from lb, so we can set
   mlirModule = std::make_unique<mlir::ModuleOp>(lb.getModule());
 
+  if (!setUpTargetMachine())
+    return false;
+
   if (ci.getInvocation().getFrontendOpts().features.IsEnabled(
           Fortran::common::LanguageFeature::OpenMP)) {
     setOffloadModuleInterfaceAttributes(
         *mlirModule, ci.getInvocation().getLangOpts().OpenMPIsDevice);
+    setOffloadModuleInterfaceTargetAttribute(*mlirModule, tm->getTargetCPU(),
+                                             tm->getTargetFeatureString());
   }
 
-  if (!setUpTargetMachine())
-    return false;
   const llvm::DataLayout &dl = tm->createDataLayout();
   setMLIRDataLayout(*mlirModule, dl);
 

diff  --git a/flang/test/Lower/OpenMP/target_cpu_features.f90 b/flang/test/Lower/OpenMP/target_cpu_features.f90
new file mode 100644
index 0000000000000..b37761ca3bf9c
--- /dev/null
+++ b/flang/test/Lower/OpenMP/target_cpu_features.f90
@@ -0,0 +1,16 @@
+!REQUIRES: amdgpu-registered-target
+!RUN: %flang_fc1 -emit-fir -triple amdgcn-amd-amdhsa -target-cpu gfx908 -fopenmp %s -o - | FileCheck %s
+
+!===============================================================================
+! Target_Enter Simple
+!===============================================================================
+
+!CHECK: omp.target = #omp.target<target_cpu = "gfx908",
+!CHECK-SAME: target_features = "+dot3-insts,+dot4-insts,+s-memtime-inst,
+!CHECK-SAME: +16-bit-insts,+s-memrealtime,+dot6-insts,+dl-insts,+wavefrontsize64,
+!CHECK-SAME: +gfx9-insts,+gfx8-insts,+ci-insts,+dot10-insts,+dot7-insts,
+!CHECK-SAME: +dot1-insts,+dot5-insts,+mai-insts,+dpp,+dot2-insts">
+!CHECK-LABEL: func.func @_QPomp_target_simple() {
+subroutine omp_target_simple
+end subroutine omp_target_simple
+

diff  --git a/mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td b/mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td
index 8a50d404bdd4e..e20d467f0df63 100644
--- a/mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td
+++ b/mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td
@@ -68,6 +68,16 @@ def FlagsAttr : OpenMP_Attr<"Flags", "flags"> {
   let assemblyFormat = "`<` struct(params) `>`";
 }
 
+def TargetAttr : OpenMP_Attr<"Target", "target"> {
+  let parameters = (ins
+    StringRefParameter<>:$target_cpu,
+    StringRefParameter<>:$target_features
+  );
+
+  let assemblyFormat = "`<` struct(params) `>`";
+}
+
+
 class OpenMP_Op<string mnemonic, list<Trait> traits = []> :
       Op<OpenMP_Dialect, mnemonic, traits>;
 

diff  --git a/mlir/include/mlir/Dialect/OpenMP/OpenMPOpsInterfaces.td b/mlir/include/mlir/Dialect/OpenMP/OpenMPOpsInterfaces.td
index 934698ae5d6fa..0cfdc3707de49 100644
--- a/mlir/include/mlir/Dialect/OpenMP/OpenMPOpsInterfaces.td
+++ b/mlir/include/mlir/Dialect/OpenMP/OpenMPOpsInterfaces.td
@@ -59,10 +59,10 @@ def OffloadModuleInterface : OpInterface<"OffloadModuleInterface"> {
     InterfaceMethod<
       /*description=*/[{
         Set the attribute IsDeviceAttr on the current module with the 
-        specified boolean argument. 
+        specified boolean argument.
       }],
       /*retTy=*/"void",
-      /*methodName=*/"setIsDevice", 
+      /*methodName=*/"setIsDevice",
       (ins "bool":$isDevice), [{}], [{
         $_op->setAttr(
           mlir::StringAttr::get($_op->getContext(), llvm::Twine{"omp.is_device"}),
@@ -74,7 +74,7 @@ def OffloadModuleInterface : OpInterface<"OffloadModuleInterface"> {
         its value, if it doesn't exit it returns false by default.
       }],
       /*retTy=*/"bool",
-      /*methodName=*/"getIsDevice", 
+      /*methodName=*/"getIsDevice",
       (ins), [{}], [{
         if (Attribute isDevice = $_op->getAttr("omp.is_device"))
           if (isDevice.isa<mlir::omp::IsDeviceAttr>())
@@ -110,6 +110,34 @@ def OffloadModuleInterface : OpInterface<"OffloadModuleInterface"> {
                       assumeTeamsOversubscription, assumeThreadsOversubscription, 
                       assumeNoThreadState, assumeNoNestedParallelism));
       }]>,
+    InterfaceMethod<
+      /*description=*/[{
+        Get the Target attribute on the current module if it exists
+        and return the attribute, if it doesn't exist it returns a nullptr.
+      }],
+      /*retTy=*/"mlir::omp::TargetAttr",
+      /*methodName=*/"getTarget",
+      (ins), [{}], [{
+        if (Attribute flags = $_op->getAttr("omp.target"))
+          return flags.dyn_cast_or_null<mlir::omp::TargetAttr>();
+        return nullptr;
+      }]>,
+    InterfaceMethod<
+      /*description=*/[{
+        Set the attribute target on the current module with the
+        specified string arguments - name of cpu and corresponding features.
+      }],
+      /*retTy=*/"void",
+      /*methodName=*/"setTarget",
+      (ins "llvm::StringRef":$targetCPU,
+           "llvm::StringRef":$targetFeatures), [{}], [{
+        if (targetCPU.empty())
+          return;
+        $_op->setAttr(("omp." + mlir::omp::TargetAttr::getMnemonic()).str(),
+                  mlir::omp::TargetAttr::get($_op->getContext(),
+                                             targetCPU.str(),
+                                             targetFeatures.str()));
+      }]>,
   ];
 }
 


        


More information about the flang-commits mailing list