[Mlir-commits] [flang] [mlir] [mlir][flang] Add an interface of OpenACC compute regions for further getAllocaBlock support (PR #100675)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Thu Jul 25 17:41:33 PDT 2024


https://github.com/khaki3 created https://github.com/llvm/llvm-project/pull/100675

This PR implements `ComputeConstructOpInterface` to define `getAllocaBlock` of OpenACC compute constructs (parallel/kernels/serial). The primary objective here is to accommodate local variables in OpenACC compute regions. The change in `fir::FirOpBuilder::getAllocaBlock` allows local variable allocation in kernels.

>From 9d0ce80be504d12aeabe2c8e65f862f3d417873a Mon Sep 17 00:00:00 2001
From: Kazuaki Matsumura <kmatsumura at nvidia.com>
Date: Thu, 25 Jul 2024 16:09:23 -0700
Subject: [PATCH 1/3] [mlir][flang] Add getAllocaBlock to OpenACC compute
 constructs

---
 flang/lib/Optimizer/Builder/FIRBuilder.cpp    |  6 ++++
 flang/test/Lower/OpenACC/acc-loop.f90         |  2 +-
 .../mlir/Dialect/OpenACC/CMakeLists.txt       |  3 +-
 mlir/include/mlir/Dialect/OpenACC/OpenACC.h   |  1 +
 .../mlir/Dialect/OpenACC/OpenACCOps.td        |  4 +++
 .../Dialect/OpenACC/OpenACCOpsInterfaces.td   | 29 +++++++++++++++++++
 mlir/lib/Dialect/OpenACC/IR/CMakeLists.txt    |  1 +
 mlir/lib/Dialect/OpenACC/IR/OpenACC.cpp       |  1 +
 .../Dialect/OpenACC/Transforms/CMakeLists.txt |  1 +
 9 files changed, 46 insertions(+), 2 deletions(-)
 create mode 100644 mlir/include/mlir/Dialect/OpenACC/OpenACCOpsInterfaces.td

diff --git a/flang/lib/Optimizer/Builder/FIRBuilder.cpp b/flang/lib/Optimizer/Builder/FIRBuilder.cpp
index fbe79d0e45e5a..6de51f6e7b58e 100644
--- a/flang/lib/Optimizer/Builder/FIRBuilder.cpp
+++ b/flang/lib/Optimizer/Builder/FIRBuilder.cpp
@@ -248,6 +248,12 @@ mlir::Value fir::FirOpBuilder::allocateLocal(
 
 /// Get the block for adding Allocas.
 mlir::Block *fir::FirOpBuilder::getAllocaBlock() {
+  if (auto accComputeConstructIface =
+          getRegion()
+              .getParentOfType<mlir::acc::ComputeConstructOpInterface>()) {
+    return accComputeConstructIface.getAllocaBlock();
+  }
+
   if (auto ompOutlineableIface =
           getRegion()
               .getParentOfType<mlir::omp::OutlineableOpenMPOpInterface>()) {
diff --git a/flang/test/Lower/OpenACC/acc-loop.f90 b/flang/test/Lower/OpenACC/acc-loop.f90
index fa910e79bd766..0d2594c86a6eb 100644
--- a/flang/test/Lower/OpenACC/acc-loop.f90
+++ b/flang/test/Lower/OpenACC/acc-loop.f90
@@ -317,10 +317,10 @@ subroutine sub1(i, j, k)
 end subroutine
 
 ! CHECK: func.func @_QPsub1
+! CHECK: acc.parallel
 ! CHECK: %[[DC_K:.*]] = fir.alloca i32 {bindc_name = "k"}
 ! CHECK: %[[DC_J:.*]] = fir.alloca i32 {bindc_name = "j"}
 ! CHECK: %[[DC_I:.*]] = fir.alloca i32 {bindc_name = "i"}
-! CHECK: acc.parallel
 ! CHECK: %[[P_I:.*]] = acc.private varPtr(%[[DC_I]] : !fir.ref<i32>) -> !fir.ref<i32> {implicit = true, name = ""}
 ! CHECK: %[[P_J:.*]] = acc.private varPtr(%[[DC_J]] : !fir.ref<i32>) -> !fir.ref<i32> {implicit = true, name = ""}
 ! CHECK: %[[P_K:.*]] = acc.private varPtr(%[[DC_K]] : !fir.ref<i32>) -> !fir.ref<i32> {implicit = true, name = ""}
diff --git a/mlir/include/mlir/Dialect/OpenACC/CMakeLists.txt b/mlir/include/mlir/Dialect/OpenACC/CMakeLists.txt
index 2aa4b1828e0ad..66b1e89a9881d 100644
--- a/mlir/include/mlir/Dialect/OpenACC/CMakeLists.txt
+++ b/mlir/include/mlir/Dialect/OpenACC/CMakeLists.txt
@@ -21,9 +21,10 @@ mlir_tablegen(OpenACCOpsAttributes.cpp.inc -gen-attrdef-defs -attrdefs-dialect=a
 add_public_tablegen_target(MLIROpenACCAttributesIncGen)
 add_dependencies(mlir-headers MLIROpenACCAttributesIncGen)
 
+add_mlir_interface(OpenACCOpsInterfaces)
+
 set(LLVM_TARGET_DEFINITIONS OpenACCTypeInterfaces.td)
 mlir_tablegen(OpenACCTypeInterfaces.h.inc -gen-type-interface-decls)
 mlir_tablegen(OpenACCTypeInterfaces.cpp.inc -gen-type-interface-defs)
 add_public_tablegen_target(MLIROpenACCTypeInterfacesIncGen)
 add_dependencies(mlir-headers MLIROpenACCTypeInterfacesIncGen)
-
diff --git a/mlir/include/mlir/Dialect/OpenACC/OpenACC.h b/mlir/include/mlir/Dialect/OpenACC/OpenACC.h
index 8239367fdd3e7..ca96ce62ae404 100644
--- a/mlir/include/mlir/Dialect/OpenACC/OpenACC.h
+++ b/mlir/include/mlir/Dialect/OpenACC/OpenACC.h
@@ -22,6 +22,7 @@
 #include "mlir/Bytecode/BytecodeOpInterface.h"
 #include "mlir/Dialect/OpenACC/OpenACCOpsDialect.h.inc"
 #include "mlir/Dialect/OpenACC/OpenACCOpsEnums.h.inc"
+#include "mlir/Dialect/OpenACC/OpenACCOpsInterfaces.h.inc"
 #include "mlir/Dialect/OpenACC/OpenACCTypeInterfaces.h.inc"
 #include "mlir/Dialect/OpenACCMPCommon/Interfaces/AtomicInterfaces.h"
 #include "mlir/Interfaces/ControlFlowInterfaces.h"
diff --git a/mlir/include/mlir/Dialect/OpenACC/OpenACCOps.td b/mlir/include/mlir/Dialect/OpenACC/OpenACCOps.td
index 148bed62aa8f2..7525347961127 100644
--- a/mlir/include/mlir/Dialect/OpenACC/OpenACCOps.td
+++ b/mlir/include/mlir/Dialect/OpenACC/OpenACCOps.td
@@ -22,6 +22,7 @@ include "mlir/IR/OpBase.td"
 include "mlir/IR/SymbolInterfaces.td"
 include "mlir/Dialect/OpenACC/OpenACCBase.td"
 include "mlir/Dialect/OpenACC/OpenACCOpsTypes.td"
+include "mlir/Dialect/OpenACC/OpenACCOpsInterfaces.td"
 include "mlir/Dialect/OpenACC/OpenACCTypeInterfaces.td"
 include "mlir/Dialect/OpenACCMPCommon/Interfaces/AtomicInterfaces.td"
 include "mlir/Dialect/OpenACCMPCommon/Interfaces/OpenACCMPOpsInterfaces.td"
@@ -1068,6 +1069,7 @@ def OpenACC_ReductionRecipeOp : OpenACC_Op<"reduction.recipe",
 
 def OpenACC_ParallelOp : OpenACC_Op<"parallel",
     [AttrSizedOperandSegments, RecursiveMemoryEffects,
+     DeclareOpInterfaceMethods<ComputeConstructOpInterface>,
      MemoryEffects<[MemWrite<OpenACC_ConstructResource>,
                     MemRead<OpenACC_CurrentDeviceIdResource>]>]> {
   let summary = "parallel construct";
@@ -1232,6 +1234,7 @@ def OpenACC_ParallelOp : OpenACC_Op<"parallel",
 
 def OpenACC_SerialOp : OpenACC_Op<"serial",
     [AttrSizedOperandSegments, RecursiveMemoryEffects,
+     DeclareOpInterfaceMethods<ComputeConstructOpInterface>,
      MemoryEffects<[MemWrite<OpenACC_ConstructResource>,
                     MemRead<OpenACC_CurrentDeviceIdResource>]>]> {
   let summary = "serial construct";
@@ -1348,6 +1351,7 @@ def OpenACC_SerialOp : OpenACC_Op<"serial",
 
 def OpenACC_KernelsOp : OpenACC_Op<"kernels",
     [AttrSizedOperandSegments, RecursiveMemoryEffects,
+     DeclareOpInterfaceMethods<ComputeConstructOpInterface>,
      MemoryEffects<[MemWrite<OpenACC_ConstructResource>,
                     MemRead<OpenACC_CurrentDeviceIdResource>]>]> {
   let summary = "kernels construct";
diff --git a/mlir/include/mlir/Dialect/OpenACC/OpenACCOpsInterfaces.td b/mlir/include/mlir/Dialect/OpenACC/OpenACCOpsInterfaces.td
new file mode 100644
index 0000000000000..cffc9ce924882
--- /dev/null
+++ b/mlir/include/mlir/Dialect/OpenACC/OpenACCOpsInterfaces.td
@@ -0,0 +1,29 @@
+//===-- OpenACCOpsInterfaces.td - OpenACC type interfaces ---*- tablegen -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef OPENACC_OPS_INTERFACES
+#define OPENACC_OPS_INTERFACES
+
+include "mlir/IR/OpBase.td"
+
+def ComputeConstructOpInterface : OpInterface<"ComputeConstructOpInterface"> {
+  let cppNamespace = "::mlir::acc";
+
+  let description = [{
+    An interface for compute construct operations.
+  }];
+
+  let methods = [
+    InterfaceMethod<"Get alloca block", "::mlir::Block*", "getAllocaBlock",
+      (ins), [{
+        return &$_op.getRegion().front();
+      }]>,
+  ];
+}
+
+#endif // OPENACC_OPS_INTERFACES
diff --git a/mlir/lib/Dialect/OpenACC/IR/CMakeLists.txt b/mlir/lib/Dialect/OpenACC/IR/CMakeLists.txt
index 387a6903b9d8b..ed7425bd52525 100644
--- a/mlir/lib/Dialect/OpenACC/IR/CMakeLists.txt
+++ b/mlir/lib/Dialect/OpenACC/IR/CMakeLists.txt
@@ -9,6 +9,7 @@ add_mlir_dialect_library(MLIROpenACCDialect
   MLIROpenACCEnumsIncGen
   MLIROpenACCAttributesIncGen
   MLIROpenACCMPOpsInterfacesIncGen
+  MLIROpenACCOpsInterfacesIncGen
   MLIROpenACCTypeInterfacesIncGen
 
   LINK_LIBS PUBLIC
diff --git a/mlir/lib/Dialect/OpenACC/IR/OpenACC.cpp b/mlir/lib/Dialect/OpenACC/IR/OpenACC.cpp
index c3c6dffd5ae49..b4da50443c6cc 100644
--- a/mlir/lib/Dialect/OpenACC/IR/OpenACC.cpp
+++ b/mlir/lib/Dialect/OpenACC/IR/OpenACC.cpp
@@ -24,6 +24,7 @@ using namespace acc;
 
 #include "mlir/Dialect/OpenACC/OpenACCOpsDialect.cpp.inc"
 #include "mlir/Dialect/OpenACC/OpenACCOpsEnums.cpp.inc"
+#include "mlir/Dialect/OpenACC/OpenACCOpsInterfaces.cpp.inc"
 #include "mlir/Dialect/OpenACC/OpenACCTypeInterfaces.cpp.inc"
 #include "mlir/Dialect/OpenACCMPCommon/Interfaces/OpenACCMPOpsInterfaces.cpp.inc"
 
diff --git a/mlir/lib/Dialect/OpenACC/Transforms/CMakeLists.txt b/mlir/lib/Dialect/OpenACC/Transforms/CMakeLists.txt
index 2a29bd1c9cfa9..41ba7f8f53d36 100644
--- a/mlir/lib/Dialect/OpenACC/Transforms/CMakeLists.txt
+++ b/mlir/lib/Dialect/OpenACC/Transforms/CMakeLists.txt
@@ -10,6 +10,7 @@ add_mlir_dialect_library(MLIROpenACCTransforms
   MLIROpenACCEnumsIncGen
   MLIROpenACCAttributesIncGen
   MLIROpenACCMPOpsInterfacesIncGen
+  MLIROpenACCOpsInterfacesIncGen
   MLIROpenACCTypeInterfacesIncGen
 
   LINK_LIBS PUBLIC

>From 7ec2b6d4eff0c9811225309010ab52dce5ade377 Mon Sep 17 00:00:00 2001
From: Kazuaki Matsumura <kmatsumura at nvidia.com>
Date: Thu, 25 Jul 2024 17:28:30 -0700
Subject: [PATCH 2/3] [mlir][flang] Rename ComputeConstructOpInterface to
 ComputeRegionOpInterface to include the loop construct

---
 flang/lib/Optimizer/Builder/FIRBuilder.cpp                | 7 +++----
 mlir/include/mlir/Dialect/OpenACC/OpenACCOps.td           | 7 ++++---
 mlir/include/mlir/Dialect/OpenACC/OpenACCOpsInterfaces.td | 4 ++--
 3 files changed, 9 insertions(+), 9 deletions(-)

diff --git a/flang/lib/Optimizer/Builder/FIRBuilder.cpp b/flang/lib/Optimizer/Builder/FIRBuilder.cpp
index 6de51f6e7b58e..d54715d3fa3f5 100644
--- a/flang/lib/Optimizer/Builder/FIRBuilder.cpp
+++ b/flang/lib/Optimizer/Builder/FIRBuilder.cpp
@@ -248,10 +248,9 @@ mlir::Value fir::FirOpBuilder::allocateLocal(
 
 /// Get the block for adding Allocas.
 mlir::Block *fir::FirOpBuilder::getAllocaBlock() {
-  if (auto accComputeConstructIface =
-          getRegion()
-              .getParentOfType<mlir::acc::ComputeConstructOpInterface>()) {
-    return accComputeConstructIface.getAllocaBlock();
+  if (auto accComputeRegionIface =
+          getRegion().getParentOfType<mlir::acc::ComputeRegionOpInterface>()) {
+    return accComputeRegionIface.getAllocaBlock();
   }
 
   if (auto ompOutlineableIface =
diff --git a/mlir/include/mlir/Dialect/OpenACC/OpenACCOps.td b/mlir/include/mlir/Dialect/OpenACC/OpenACCOps.td
index 7525347961127..66cb81a9e0129 100644
--- a/mlir/include/mlir/Dialect/OpenACC/OpenACCOps.td
+++ b/mlir/include/mlir/Dialect/OpenACC/OpenACCOps.td
@@ -1069,7 +1069,7 @@ def OpenACC_ReductionRecipeOp : OpenACC_Op<"reduction.recipe",
 
 def OpenACC_ParallelOp : OpenACC_Op<"parallel",
     [AttrSizedOperandSegments, RecursiveMemoryEffects,
-     DeclareOpInterfaceMethods<ComputeConstructOpInterface>,
+     DeclareOpInterfaceMethods<ComputeRegionOpInterface>,
      MemoryEffects<[MemWrite<OpenACC_ConstructResource>,
                     MemRead<OpenACC_CurrentDeviceIdResource>]>]> {
   let summary = "parallel construct";
@@ -1234,7 +1234,7 @@ def OpenACC_ParallelOp : OpenACC_Op<"parallel",
 
 def OpenACC_SerialOp : OpenACC_Op<"serial",
     [AttrSizedOperandSegments, RecursiveMemoryEffects,
-     DeclareOpInterfaceMethods<ComputeConstructOpInterface>,
+     DeclareOpInterfaceMethods<ComputeRegionOpInterface>,
      MemoryEffects<[MemWrite<OpenACC_ConstructResource>,
                     MemRead<OpenACC_CurrentDeviceIdResource>]>]> {
   let summary = "serial construct";
@@ -1351,7 +1351,7 @@ def OpenACC_SerialOp : OpenACC_Op<"serial",
 
 def OpenACC_KernelsOp : OpenACC_Op<"kernels",
     [AttrSizedOperandSegments, RecursiveMemoryEffects,
-     DeclareOpInterfaceMethods<ComputeConstructOpInterface>,
+     DeclareOpInterfaceMethods<ComputeRegionOpInterface>,
      MemoryEffects<[MemWrite<OpenACC_ConstructResource>,
                     MemRead<OpenACC_CurrentDeviceIdResource>]>]> {
   let summary = "kernels construct";
@@ -1742,6 +1742,7 @@ def OpenACC_HostDataOp : OpenACC_Op<"host_data",
 
 def OpenACC_LoopOp : OpenACC_Op<"loop",
     [AttrSizedOperandSegments, RecursiveMemoryEffects,
+     DeclareOpInterfaceMethods<ComputeRegionOpInterface>,
      MemoryEffects<[MemWrite<OpenACC_ConstructResource>]>,
      DeclareOpInterfaceMethods<LoopLikeOpInterface>]> {
   let summary = "loop construct";
diff --git a/mlir/include/mlir/Dialect/OpenACC/OpenACCOpsInterfaces.td b/mlir/include/mlir/Dialect/OpenACC/OpenACCOpsInterfaces.td
index cffc9ce924882..6fb9a950489f8 100644
--- a/mlir/include/mlir/Dialect/OpenACC/OpenACCOpsInterfaces.td
+++ b/mlir/include/mlir/Dialect/OpenACC/OpenACCOpsInterfaces.td
@@ -11,11 +11,11 @@
 
 include "mlir/IR/OpBase.td"
 
-def ComputeConstructOpInterface : OpInterface<"ComputeConstructOpInterface"> {
+def ComputeRegionOpInterface : OpInterface<"ComputeRegionOpInterface"> {
   let cppNamespace = "::mlir::acc";
 
   let description = [{
-    An interface for compute construct operations.
+    An interface for compute and loop construct operations.
   }];
 
   let methods = [

>From aba1b1faeff8616d78e854a39fb72639b26151f3 Mon Sep 17 00:00:00 2001
From: Kazuaki Matsumura <kmatsumura at nvidia.com>
Date: Thu, 25 Jul 2024 17:36:32 -0700
Subject: [PATCH 3/3] Revert "[mlir][flang] Rename ComputeConstructOpInterface
 to ComputeRegionOpInterface to include the loop construct"

This reverts commit 7ec2b6d4eff0c9811225309010ab52dce5ade377.
---
 flang/lib/Optimizer/Builder/FIRBuilder.cpp                | 7 ++++---
 mlir/include/mlir/Dialect/OpenACC/OpenACCOps.td           | 7 +++----
 mlir/include/mlir/Dialect/OpenACC/OpenACCOpsInterfaces.td | 4 ++--
 3 files changed, 9 insertions(+), 9 deletions(-)

diff --git a/flang/lib/Optimizer/Builder/FIRBuilder.cpp b/flang/lib/Optimizer/Builder/FIRBuilder.cpp
index d54715d3fa3f5..6de51f6e7b58e 100644
--- a/flang/lib/Optimizer/Builder/FIRBuilder.cpp
+++ b/flang/lib/Optimizer/Builder/FIRBuilder.cpp
@@ -248,9 +248,10 @@ mlir::Value fir::FirOpBuilder::allocateLocal(
 
 /// Get the block for adding Allocas.
 mlir::Block *fir::FirOpBuilder::getAllocaBlock() {
-  if (auto accComputeRegionIface =
-          getRegion().getParentOfType<mlir::acc::ComputeRegionOpInterface>()) {
-    return accComputeRegionIface.getAllocaBlock();
+  if (auto accComputeConstructIface =
+          getRegion()
+              .getParentOfType<mlir::acc::ComputeConstructOpInterface>()) {
+    return accComputeConstructIface.getAllocaBlock();
   }
 
   if (auto ompOutlineableIface =
diff --git a/mlir/include/mlir/Dialect/OpenACC/OpenACCOps.td b/mlir/include/mlir/Dialect/OpenACC/OpenACCOps.td
index 66cb81a9e0129..7525347961127 100644
--- a/mlir/include/mlir/Dialect/OpenACC/OpenACCOps.td
+++ b/mlir/include/mlir/Dialect/OpenACC/OpenACCOps.td
@@ -1069,7 +1069,7 @@ def OpenACC_ReductionRecipeOp : OpenACC_Op<"reduction.recipe",
 
 def OpenACC_ParallelOp : OpenACC_Op<"parallel",
     [AttrSizedOperandSegments, RecursiveMemoryEffects,
-     DeclareOpInterfaceMethods<ComputeRegionOpInterface>,
+     DeclareOpInterfaceMethods<ComputeConstructOpInterface>,
      MemoryEffects<[MemWrite<OpenACC_ConstructResource>,
                     MemRead<OpenACC_CurrentDeviceIdResource>]>]> {
   let summary = "parallel construct";
@@ -1234,7 +1234,7 @@ def OpenACC_ParallelOp : OpenACC_Op<"parallel",
 
 def OpenACC_SerialOp : OpenACC_Op<"serial",
     [AttrSizedOperandSegments, RecursiveMemoryEffects,
-     DeclareOpInterfaceMethods<ComputeRegionOpInterface>,
+     DeclareOpInterfaceMethods<ComputeConstructOpInterface>,
      MemoryEffects<[MemWrite<OpenACC_ConstructResource>,
                     MemRead<OpenACC_CurrentDeviceIdResource>]>]> {
   let summary = "serial construct";
@@ -1351,7 +1351,7 @@ def OpenACC_SerialOp : OpenACC_Op<"serial",
 
 def OpenACC_KernelsOp : OpenACC_Op<"kernels",
     [AttrSizedOperandSegments, RecursiveMemoryEffects,
-     DeclareOpInterfaceMethods<ComputeRegionOpInterface>,
+     DeclareOpInterfaceMethods<ComputeConstructOpInterface>,
      MemoryEffects<[MemWrite<OpenACC_ConstructResource>,
                     MemRead<OpenACC_CurrentDeviceIdResource>]>]> {
   let summary = "kernels construct";
@@ -1742,7 +1742,6 @@ def OpenACC_HostDataOp : OpenACC_Op<"host_data",
 
 def OpenACC_LoopOp : OpenACC_Op<"loop",
     [AttrSizedOperandSegments, RecursiveMemoryEffects,
-     DeclareOpInterfaceMethods<ComputeRegionOpInterface>,
      MemoryEffects<[MemWrite<OpenACC_ConstructResource>]>,
      DeclareOpInterfaceMethods<LoopLikeOpInterface>]> {
   let summary = "loop construct";
diff --git a/mlir/include/mlir/Dialect/OpenACC/OpenACCOpsInterfaces.td b/mlir/include/mlir/Dialect/OpenACC/OpenACCOpsInterfaces.td
index 6fb9a950489f8..cffc9ce924882 100644
--- a/mlir/include/mlir/Dialect/OpenACC/OpenACCOpsInterfaces.td
+++ b/mlir/include/mlir/Dialect/OpenACC/OpenACCOpsInterfaces.td
@@ -11,11 +11,11 @@
 
 include "mlir/IR/OpBase.td"
 
-def ComputeRegionOpInterface : OpInterface<"ComputeRegionOpInterface"> {
+def ComputeConstructOpInterface : OpInterface<"ComputeConstructOpInterface"> {
   let cppNamespace = "::mlir::acc";
 
   let description = [{
-    An interface for compute and loop construct operations.
+    An interface for compute construct operations.
   }];
 
   let methods = [



More information about the Mlir-commits mailing list