Skip to content

Commit d041343

Browse files
authored
[flang][OpenMP] Handle omp.private in FirOpBuilder::getAllocaBlock() (#93927)
Fixes a crash uncovered by [pr89651](https://github.com/llvm/llvm-test-suite/blob/main/Fortran/gfortran/regression/gomp/pr89651.f90) in the test suite. Fixes a crash caused by missing handling of `omp.private` ops in `FirOpBuilder::getAllocaBlock()`.
1 parent 392ca64 commit d041343

File tree

15 files changed

+78
-48
lines changed

15 files changed

+78
-48
lines changed

flang/lib/Optimizer/Builder/FIRBuilder.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -252,11 +252,10 @@ mlir::Block *fir::FirOpBuilder::getAllocaBlock() {
252252
.getParentOfType<mlir::omp::OutlineableOpenMPOpInterface>()) {
253253
return ompOutlineableIface.getAllocaBlock();
254254
}
255-
if (getRegion().getParentOfType<mlir::omp::DeclareReductionOp>())
256-
return &getRegion().front();
257-
if (auto accRecipeIface =
258-
getRegion().getParentOfType<mlir::acc::RecipeInterface>()) {
259-
return accRecipeIface.getAllocaBlock(getRegion());
255+
256+
if (auto recipeIface =
257+
getRegion().getParentOfType<mlir::accomp::RecipeInterface>()) {
258+
return recipeIface.getAllocaBlock(getRegion());
260259
}
261260

262261
return getEntryBlock();

flang/test/Lower/OpenMP/delayed-privatization-allocatable-firstprivate.f90

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
! Test delayed privatization for allocatables: `firstprivate`.
22

3+
! RUN: split-file %s %t
4+
35
! RUN: %flang_fc1 -emit-hlfir -fopenmp -mmlir --openmp-enable-delayed-privatization \
4-
! RUN: -o - %s 2>&1 | FileCheck %s
5-
! RUN: bbc -emit-hlfir -fopenmp --openmp-enable-delayed-privatization -o - %s 2>&1 |\
6+
! RUN: -o - %t/test_ir.f90 2>&1 | FileCheck %s
7+
! RUN: bbc -emit-hlfir -fopenmp --openmp-enable-delayed-privatization -o - %t/test_ir.f90 2>&1 |\
68
! RUN: FileCheck %s
79

10+
!--- test_ir.f90
811
subroutine delayed_privatization_allocatable
912
implicit none
1013
integer, allocatable :: var1
@@ -34,3 +37,26 @@ subroutine delayed_privatization_allocatable
3437
! CHECK-NEXT: %[[ORIG_BASE_LD:.*]] = fir.load %[[ORIG_BASE_ADDR]]
3538
! CHECK-NEXT: hlfir.assign %[[ORIG_BASE_LD]] to %[[PRIV_BASE_BOX]] temporary_lhs
3639
! CHECK-NEXT: }
40+
41+
! RUN: %flang -c -emit-llvm -fopenmp -mmlir --openmp-enable-delayed-privatization \
42+
! RUN: -o - %t/test_compilation_to_obj.f90 | \
43+
! RUN: llvm-dis 2>&1 |\
44+
! RUN: FileCheck %s -check-prefix=LLVM
45+
46+
!--- test_compilation_to_obj.f90
47+
48+
program compilation_to_obj
49+
real, allocatable :: t(:)
50+
51+
!$omp parallel firstprivate(t)
52+
t(1) = 3.14
53+
!$omp end parallel
54+
55+
end program compilation_to_obj
56+
57+
! LLVM: @[[GLOB_VAR:[^[:space:]]+]]t = internal global
58+
59+
! LLVM: define internal void @_QQmain..omp_par
60+
! LLVM: %[[LOCAL_VAR:.*]] = alloca { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, align 8
61+
! LLVM-NEXT: %[[GLOB_VAL:.*]] = load { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, ptr @[[GLOB_VAR]]t, align 8
62+
! LLVM-NEXT: store { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] } %[[GLOB_VAL]], ptr %[[LOCAL_VAR]], align 8

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,3 @@ mlir_tablegen(OpenACCTypeInterfaces.cpp.inc -gen-type-interface-defs)
2727
add_public_tablegen_target(MLIROpenACCTypeInterfacesIncGen)
2828
add_dependencies(mlir-headers MLIROpenACCTypeInterfacesIncGen)
2929

30-
add_mlir_interface(OpenACCOpsInterfaces)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
#define GET_ATTRDEF_CLASSES
3535
#include "mlir/Dialect/OpenACC/OpenACCOpsAttributes.h.inc"
3636

37-
#include "mlir/Dialect/OpenACC/OpenACCInterfaces.h"
37+
#include "mlir/Dialect/OpenACCMPCommon/Interfaces/OpenACCMPOpsInterfaces.h"
3838

3939
#define GET_OP_CLASSES
4040
#include "mlir/Dialect/OpenACC/OpenACCOps.h.inc"

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

Lines changed: 0 additions & 20 deletions
This file was deleted.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ 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"
2625
include "mlir/Dialect/OpenACC/OpenACCTypeInterfaces.td"
2726
include "mlir/Dialect/OpenACCMPCommon/Interfaces/AtomicInterfaces.td"
27+
include "mlir/Dialect/OpenACCMPCommon/Interfaces/OpenACCMPOpsInterfaces.td"
2828

2929
// AccCommon requires definition of OpenACC_Dialect.
3030
include "mlir/Dialect/OpenACC/AccCommon.td"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
add_mlir_interface(AtomicInterfaces)
2+
3+
add_mlir_interface(OpenACCMPOpsInterfaces)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===- OpenACCMPOpsInterfaces.h - MLIR Interfaces for OpenACC/MP *- C++ -*-===//
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+
// This file declares OpenACC/OpenMP Interface implementations for the
10+
// OpenACC/OpenMP dialects.
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
#ifndef OPENACC_MP_COMMON_INTERFACES_OPSINTERFACES_H_
15+
#define OPENACC_MP_COMMON_INTERFACES_OPSINTERFACES_H_
16+
17+
#include "mlir/IR/OpDefinition.h"
18+
19+
#include "mlir/Dialect/OpenACCMPCommon/Interfaces/OpenACCMPOpsInterfaces.h.inc"
20+
21+
#endif // OPENACC_MP_COMMON_INTERFACES_OPSINTERFACES_H_

mlir/include/mlir/Dialect/OpenACC/OpenACCOpsInterfaces.td renamed to mlir/include/mlir/Dialect/OpenACCMPCommon/Interfaces/OpenACCMPOpsInterfaces.td

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
1-
//===-- OpenACCOpsInterfaces.td - OpenACC op interfaces ----*- tablegen -*-===//
1+
//===-- OpenACCMPOpsInterfaces.td - OpenACC/MP op interfaces - tablegen -*-===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
55
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66
//
77
//===----------------------------------------------------------------------===//
88
//
9-
// This is the OpenACC Dialect interfaces definition file.
9+
// This is the OpenACC/OpenMP Dialect interfaces definition file.
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
#ifndef OPENACC_OPS_INTERFACES
14-
#define OPENACC_OPS_INTERFACES
13+
#ifndef OPENACC_MP_COMMON_OPS_INTERFACES
14+
#define OPENACC_MP_COMMON_OPS_INTERFACES
1515

1616
include "mlir/IR/OpBase.td"
1717

1818
def RecipeInterface : OpInterface<"RecipeInterface"> {
1919
let description = [{
20-
OpenACC operations with one or more regions holding executable code.
20+
OpenACC/OpenMP operations with one or more regions holding executable code.
2121
}];
22-
let cppNamespace = "::mlir::acc";
22+
let cppNamespace = "::mlir::accomp";
2323
let methods = [
2424
InterfaceMethod<
2525
/*description=*/[{
@@ -38,4 +38,4 @@ def RecipeInterface : OpInterface<"RecipeInterface"> {
3838
];
3939
}
4040

41-
#endif // OPENACC_OPS_INTERFACES
41+
#endif // OPENACC_MP_COMMON_OPS_INTERFACES

mlir/include/mlir/Dialect/OpenMP/OpenMPDialect.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
1717
#include "mlir/Dialect/OpenACCMPCommon/Interfaces/AtomicInterfaces.h"
18+
#include "mlir/Dialect/OpenACCMPCommon/Interfaces/OpenACCMPOpsInterfaces.h"
1819
#include "mlir/IR/Dialect.h"
1920
#include "mlir/IR/OpDefinition.h"
2021
#include "mlir/IR/PatternMatch.h"

mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
include "mlir/Dialect/LLVMIR/LLVMOpBase.td"
1818
include "mlir/Dialect/OpenACCMPCommon/Interfaces/AtomicInterfaces.td"
19+
include "mlir/Dialect/OpenACCMPCommon/Interfaces/OpenACCMPOpsInterfaces.td"
1920
include "mlir/Dialect/OpenMP/OpenMPAttrDefs.td"
2021
include "mlir/Dialect/OpenMP/OpenMPOpBase.td"
2122
include "mlir/Interfaces/ControlFlowInterfaces.td"
@@ -28,7 +29,7 @@ include "mlir/IR/SymbolInterfaces.td"
2829
// 2.19.4 Data-Sharing Attribute Clauses
2930
//===----------------------------------------------------------------------===//
3031

31-
def PrivateClauseOp : OpenMP_Op<"private", [IsolatedFromAbove]> {
32+
def PrivateClauseOp : OpenMP_Op<"private", [IsolatedFromAbove, RecipeInterface]> {
3233
let summary = "Provides declaration of [first]private logic.";
3334
let description = [{
3435
This operation provides a declaration of how to implement the
@@ -2091,7 +2092,8 @@ def CancellationPointOp : OpenMP_Op<"cancellation_point"> {
20912092
//===----------------------------------------------------------------------===//
20922093

20932094
def DeclareReductionOp : OpenMP_Op<"declare_reduction", [Symbol,
2094-
IsolatedFromAbove]> {
2095+
IsolatedFromAbove,
2096+
RecipeInterface]> {
20952097
let summary = "declares a reduction kind";
20962098

20972099
let description = [{

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ add_mlir_dialect_library(MLIROpenACCDialect
88
MLIROpenACCOpsIncGen
99
MLIROpenACCEnumsIncGen
1010
MLIROpenACCAttributesIncGen
11-
MLIROpenACCOpsInterfacesIncGen
11+
MLIROpenACCMPOpsInterfacesIncGen
1212
MLIROpenACCTypeInterfacesIncGen
1313

1414
LINK_LIBS PUBLIC

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ 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"
2827
#include "mlir/Dialect/OpenACC/OpenACCTypeInterfaces.cpp.inc"
28+
#include "mlir/Dialect/OpenACCMPCommon/Interfaces/OpenACCMPOpsInterfaces.cpp.inc"
2929

3030
namespace {
3131
struct MemRefPointerLikeModel

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ add_mlir_dialect_library(MLIROpenACCTransforms
99
MLIROpenACCOpsIncGen
1010
MLIROpenACCEnumsIncGen
1111
MLIROpenACCAttributesIncGen
12-
MLIROpenACCOpsInterfacesIncGen
12+
MLIROpenACCMPOpsInterfacesIncGen
1313
MLIROpenACCTypeInterfacesIncGen
1414

1515
LINK_LIBS PUBLIC

utils/bazel/llvm-project-overlay/mlir/BUILD.bazel

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9937,10 +9937,10 @@ td_library(
99379937
"include/mlir/Dialect/OpenACC/AccCommon.td",
99389938
"include/mlir/Dialect/OpenACC/OpenACCBase.td",
99399939
"include/mlir/Dialect/OpenACC/OpenACCOps.td",
9940-
"include/mlir/Dialect/OpenACC/OpenACCOpsInterfaces.td",
99419940
"include/mlir/Dialect/OpenACC/OpenACCOpsTypes.td",
99429941
"include/mlir/Dialect/OpenACC/OpenACCTypeInterfaces.td",
99439942
"include/mlir/Dialect/OpenACCMPCommon/Interfaces/AtomicInterfaces.td",
9943+
"include/mlir/Dialect/OpenACCMPCommon/Interfaces/OpenACCMPOpsInterfaces.td",
99449944
],
99459945
includes = ["include"],
99469946
deps = [
@@ -9951,19 +9951,19 @@ td_library(
99519951
)
99529952

99539953
gentbl_cc_library(
9954-
name = "OpenACCOpsInterfacesIncGen",
9954+
name = "OpenACCMPOpsInterfacesIncGen",
99559955
tbl_outs = [
99569956
(
99579957
["-gen-op-interface-decls"],
9958-
"include/mlir/Dialect/OpenACC/OpenACCOpsInterfaces.h.inc",
9958+
"include/mlir/Dialect/OpenACCMPCommon/Interfaces/OpenACCMPOpsInterfaces.h.inc",
99599959
),
99609960
(
99619961
["-gen-op-interface-defs"],
9962-
"include/mlir/Dialect/OpenACC/OpenACCOpsInterfaces.cpp.inc",
9962+
"include/mlir/Dialect/OpenACCMPCommon/Interfaces/OpenACCMPOpsInterfaces.cpp.inc",
99639963
),
99649964
],
99659965
tblgen = ":mlir-tblgen",
9966-
td_file = "include/mlir/Dialect/OpenACC/OpenACCOpsInterfaces.td",
9966+
td_file = "include/mlir/Dialect/OpenACCMPCommon/Interfaces/OpenACCMPOpsInterfaces.td",
99679967
deps = [":OpenAccOpsTdFiles"],
99689968
)
99699969

@@ -10099,7 +10099,7 @@ cc_library(
1009910099
":LoopLikeInterface",
1010010100
":MemRefDialect",
1010110101
":OpenACCOpsIncGen",
10102-
":OpenACCOpsInterfacesIncGen",
10102+
":OpenACCMPOpsInterfacesIncGen",
1010310103
":OpenACCTypeInterfacesIncGen",
1010410104
":OpenACCTypesIncGen",
1010510105
":SideEffectInterfaces",

0 commit comments

Comments
 (0)