Skip to content

Commit 26d9282

Browse files
authored
[mlir][flang] Add an interface of OpenACC compute regions for further getAllocaBlock support (llvm#100675)
This PR implements `ComputeRegionOpInterface` to define `getAllocaBlock` of OpenACC loop and 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 inside loops and kernels.
1 parent efd13eb commit 26d9282

File tree

9 files changed

+56
-8
lines changed

9 files changed

+56
-8
lines changed

flang/lib/Optimizer/Builder/FIRBuilder.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,11 @@ mlir::Value fir::FirOpBuilder::allocateLocal(
248248

249249
/// Get the block for adding Allocas.
250250
mlir::Block *fir::FirOpBuilder::getAllocaBlock() {
251+
if (auto accComputeRegionIface =
252+
getRegion().getParentOfType<mlir::acc::ComputeRegionOpInterface>()) {
253+
return accComputeRegionIface.getAllocaBlock();
254+
}
255+
251256
if (auto ompOutlineableIface =
252257
getRegion()
253258
.getParentOfType<mlir::omp::OutlineableOpenMPOpInterface>()) {

flang/test/Lower/OpenACC/acc-loop.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,10 +317,10 @@ subroutine sub1(i, j, k)
317317
end subroutine
318318

319319
! CHECK: func.func @_QPsub1
320+
! CHECK: acc.parallel
320321
! CHECK: %[[DC_K:.*]] = fir.alloca i32 {bindc_name = "k"}
321322
! CHECK: %[[DC_J:.*]] = fir.alloca i32 {bindc_name = "j"}
322323
! CHECK: %[[DC_I:.*]] = fir.alloca i32 {bindc_name = "i"}
323-
! CHECK: acc.parallel
324324
! CHECK: %[[P_I:.*]] = acc.private varPtr(%[[DC_I]] : !fir.ref<i32>) -> !fir.ref<i32> {implicit = true, name = ""}
325325
! CHECK: %[[P_J:.*]] = acc.private varPtr(%[[DC_J]] : !fir.ref<i32>) -> !fir.ref<i32> {implicit = true, name = ""}
326326
! CHECK: %[[P_K:.*]] = acc.private varPtr(%[[DC_K]] : !fir.ref<i32>) -> !fir.ref<i32> {implicit = true, name = ""}

mlir/include/mlir/Dialect/OpenACC/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ mlir_tablegen(OpenACCOpsAttributes.cpp.inc -gen-attrdef-defs -attrdefs-dialect=a
2121
add_public_tablegen_target(MLIROpenACCAttributesIncGen)
2222
add_dependencies(mlir-headers MLIROpenACCAttributesIncGen)
2323

24+
add_mlir_interface(OpenACCOpsInterfaces)
25+
2426
set(LLVM_TARGET_DEFINITIONS OpenACCTypeInterfaces.td)
2527
mlir_tablegen(OpenACCTypeInterfaces.h.inc -gen-type-interface-decls)
2628
mlir_tablegen(OpenACCTypeInterfaces.cpp.inc -gen-type-interface-defs)
2729
add_public_tablegen_target(MLIROpenACCTypeInterfacesIncGen)
2830
add_dependencies(mlir-headers MLIROpenACCTypeInterfacesIncGen)
29-

mlir/include/mlir/Dialect/OpenACC/OpenACC.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "mlir/Bytecode/BytecodeOpInterface.h"
2323
#include "mlir/Dialect/OpenACC/OpenACCOpsDialect.h.inc"
2424
#include "mlir/Dialect/OpenACC/OpenACCOpsEnums.h.inc"
25+
#include "mlir/Dialect/OpenACC/OpenACCOpsInterfaces.h.inc"
2526
#include "mlir/Dialect/OpenACC/OpenACCTypeInterfaces.h.inc"
2627
#include "mlir/Dialect/OpenACCMPCommon/Interfaces/AtomicInterfaces.h"
2728
#include "mlir/Interfaces/ControlFlowInterfaces.h"

mlir/include/mlir/Dialect/OpenACC/OpenACCOps.td

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ include "mlir/IR/OpBase.td"
2222
include "mlir/IR/SymbolInterfaces.td"
2323
include "mlir/Dialect/OpenACC/OpenACCBase.td"
2424
include "mlir/Dialect/OpenACC/OpenACCOpsTypes.td"
25+
include "mlir/Dialect/OpenACC/OpenACCOpsInterfaces.td"
2526
include "mlir/Dialect/OpenACC/OpenACCTypeInterfaces.td"
2627
include "mlir/Dialect/OpenACCMPCommon/Interfaces/AtomicInterfaces.td"
2728
include "mlir/Dialect/OpenACCMPCommon/Interfaces/OpenACCMPOpsInterfaces.td"
@@ -1067,7 +1068,9 @@ def OpenACC_ReductionRecipeOp : OpenACC_Op<"reduction.recipe",
10671068
//===----------------------------------------------------------------------===//
10681069

10691070
def OpenACC_ParallelOp : OpenACC_Op<"parallel",
1070-
[AttrSizedOperandSegments, RecursiveMemoryEffects,
1071+
[AttrSizedOperandSegments, AutomaticAllocationScope,
1072+
RecursiveMemoryEffects,
1073+
DeclareOpInterfaceMethods<ComputeRegionOpInterface>,
10711074
MemoryEffects<[MemWrite<OpenACC_ConstructResource>,
10721075
MemRead<OpenACC_CurrentDeviceIdResource>]>]> {
10731076
let summary = "parallel construct";
@@ -1231,7 +1234,9 @@ def OpenACC_ParallelOp : OpenACC_Op<"parallel",
12311234
//===----------------------------------------------------------------------===//
12321235

12331236
def OpenACC_SerialOp : OpenACC_Op<"serial",
1234-
[AttrSizedOperandSegments, RecursiveMemoryEffects,
1237+
[AttrSizedOperandSegments, AutomaticAllocationScope,
1238+
RecursiveMemoryEffects,
1239+
DeclareOpInterfaceMethods<ComputeRegionOpInterface>,
12351240
MemoryEffects<[MemWrite<OpenACC_ConstructResource>,
12361241
MemRead<OpenACC_CurrentDeviceIdResource>]>]> {
12371242
let summary = "serial construct";
@@ -1347,7 +1352,9 @@ def OpenACC_SerialOp : OpenACC_Op<"serial",
13471352
//===----------------------------------------------------------------------===//
13481353

13491354
def OpenACC_KernelsOp : OpenACC_Op<"kernels",
1350-
[AttrSizedOperandSegments, RecursiveMemoryEffects,
1355+
[AttrSizedOperandSegments, AutomaticAllocationScope,
1356+
RecursiveMemoryEffects,
1357+
DeclareOpInterfaceMethods<ComputeRegionOpInterface>,
13511358
MemoryEffects<[MemWrite<OpenACC_ConstructResource>,
13521359
MemRead<OpenACC_CurrentDeviceIdResource>]>]> {
13531360
let summary = "kernels construct";
@@ -1737,9 +1744,11 @@ def OpenACC_HostDataOp : OpenACC_Op<"host_data",
17371744
//===----------------------------------------------------------------------===//
17381745

17391746
def OpenACC_LoopOp : OpenACC_Op<"loop",
1740-
[AttrSizedOperandSegments, RecursiveMemoryEffects,
1741-
MemoryEffects<[MemWrite<OpenACC_ConstructResource>]>,
1742-
DeclareOpInterfaceMethods<LoopLikeOpInterface>]> {
1747+
[AttrSizedOperandSegments, AutomaticAllocationScope,
1748+
RecursiveMemoryEffects,
1749+
DeclareOpInterfaceMethods<ComputeRegionOpInterface>,
1750+
DeclareOpInterfaceMethods<LoopLikeOpInterface>,
1751+
MemoryEffects<[MemWrite<OpenACC_ConstructResource>]>]> {
17431752
let summary = "loop construct";
17441753

17451754
let description = [{
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//===-- OpenACCOpsInterfaces.td - OpenACC type interfaces ---*- tablegen -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef OPENACC_OPS_INTERFACES
10+
#define OPENACC_OPS_INTERFACES
11+
12+
include "mlir/IR/OpBase.td"
13+
14+
def ComputeRegionOpInterface : OpInterface<"ComputeRegionOpInterface"> {
15+
let cppNamespace = "::mlir::acc";
16+
17+
let description = [{
18+
An interface for compute and loop construct operations.
19+
}];
20+
21+
let methods = [
22+
InterfaceMethod<"Get alloca block", "::mlir::Block*", "getAllocaBlock",
23+
(ins), [{
24+
return &$_op.getRegion().front();
25+
}]>,
26+
];
27+
}
28+
29+
#endif // OPENACC_OPS_INTERFACES

mlir/lib/Dialect/OpenACC/IR/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ add_mlir_dialect_library(MLIROpenACCDialect
99
MLIROpenACCEnumsIncGen
1010
MLIROpenACCAttributesIncGen
1111
MLIROpenACCMPOpsInterfacesIncGen
12+
MLIROpenACCOpsInterfacesIncGen
1213
MLIROpenACCTypeInterfacesIncGen
1314

1415
LINK_LIBS PUBLIC

mlir/lib/Dialect/OpenACC/IR/OpenACC.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ using namespace acc;
2424

2525
#include "mlir/Dialect/OpenACC/OpenACCOpsDialect.cpp.inc"
2626
#include "mlir/Dialect/OpenACC/OpenACCOpsEnums.cpp.inc"
27+
#include "mlir/Dialect/OpenACC/OpenACCOpsInterfaces.cpp.inc"
2728
#include "mlir/Dialect/OpenACC/OpenACCTypeInterfaces.cpp.inc"
2829
#include "mlir/Dialect/OpenACCMPCommon/Interfaces/OpenACCMPOpsInterfaces.cpp.inc"
2930

mlir/lib/Dialect/OpenACC/Transforms/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ add_mlir_dialect_library(MLIROpenACCTransforms
1010
MLIROpenACCEnumsIncGen
1111
MLIROpenACCAttributesIncGen
1212
MLIROpenACCMPOpsInterfacesIncGen
13+
MLIROpenACCOpsInterfacesIncGen
1314
MLIROpenACCTypeInterfacesIncGen
1415

1516
LINK_LIBS PUBLIC

0 commit comments

Comments
 (0)