[llvm-branch-commits] [clang] [flang] [llvm] [mlir] [MLIR][OpenMP] Add LLVM translation support for OpenMP UserDefinedMappers (PR #124746)
Akash Banerjee via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Mon Feb 10 12:05:25 PST 2025
https://github.com/TIFitis updated https://github.com/llvm/llvm-project/pull/124746
>From ccf0f40cdccd419e7c173c6bc5e1bc6a4a2d1fc6 Mon Sep 17 00:00:00 2001
From: Akash Banerjee <Akash.Banerjee at amd.com>
Date: Mon, 10 Feb 2025 19:51:06 +0000
Subject: [PATCH 01/23] Add getSymVal helper function.
---
mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td | 5 +++++
1 file changed, 5 insertions(+)
diff --git a/mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td b/mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td
index d9d8856f0b726c..e1eef30c0dd060 100644
--- a/mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td
+++ b/mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td
@@ -1776,6 +1776,11 @@ def DeclareMapperOp : OpenMP_Op<"declare_mapper", [
DeclareMapperInfoOp getDeclareMapperInfo(){
return cast<DeclareMapperInfoOp>(getRegion().getBlocks().front().getTerminator());
}
+
+ /// Get SymVal block argument
+ BlockArgument getSymVal(){
+ return getRegion().getArgument(0);
+ }
}];
let hasRegionVerifier = 1;
>From e6e6c5cf37f240350023a25067c5dbe8fa7948a6 Mon Sep 17 00:00:00 2001
From: Akash Banerjee <Akash.Banerjee at amd.com>
Date: Wed, 20 Nov 2024 20:51:02 +0000
Subject: [PATCH 02/23] [MLIR][OpenMP] Add Lowering support for OpenMP Declare
Mapper directive
This patch adds HLFIR/FIR lowering support for OpenMP Declare Mapper directive.
---
flang/lib/Lower/OpenMP/OpenMP.cpp | 35 ++++++++++++++++++-
.../Optimizer/OpenMP/MapInfoFinalization.cpp | 3 +-
.../Lower/OpenMP/Todo/omp-declare-mapper.f90 | 8 ++---
flang/test/Lower/OpenMP/declare-mapper.f90 | 31 ++++++++++++++++
4 files changed, 71 insertions(+), 6 deletions(-)
create mode 100644 flang/test/Lower/OpenMP/declare-mapper.f90
diff --git a/flang/lib/Lower/OpenMP/OpenMP.cpp b/flang/lib/Lower/OpenMP/OpenMP.cpp
index bd794033cdf116..a7347c67d552cd 100644
--- a/flang/lib/Lower/OpenMP/OpenMP.cpp
+++ b/flang/lib/Lower/OpenMP/OpenMP.cpp
@@ -40,6 +40,7 @@
#include "mlir/Transforms/RegionUtils.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/Frontend/OpenMP/OMPConstants.h"
+#include <string>
using namespace Fortran::lower::omp;
using namespace Fortran::common::openmp;
@@ -3119,7 +3120,39 @@ static void
genOMP(lower::AbstractConverter &converter, lower::SymMap &symTable,
semantics::SemanticsContext &semaCtx, lower::pft::Evaluation &eval,
const parser::OpenMPDeclareMapperConstruct &declareMapperConstruct) {
- TODO(converter.getCurrentLocation(), "OpenMPDeclareMapperConstruct");
+ fir::FirOpBuilder &firOpBuilder = converter.getFirOpBuilder();
+ lower::StatementContext stmtCtx;
+ const auto &spec =
+ std::get<parser::OmpDeclareMapperSpecifier>(declareMapperConstruct.t);
+ const auto &mapperName{std::get<std::optional<parser::Name>>(spec.t)};
+ const auto &varType{std::get<parser::TypeSpec>(spec.t)};
+ const auto &varName{std::get<parser::Name>(spec.t)};
+ std::stringstream mapperNameStr;
+ if (mapperName.has_value()) {
+ mapperNameStr << mapperName->ToString();
+ } else {
+ mapperNameStr << "default_"
+ << varType.declTypeSpec->derivedTypeSpec().name().ToString();
+ }
+
+ mlir::OpBuilder::InsertPoint insPt = firOpBuilder.saveInsertionPoint();
+ firOpBuilder.setInsertionPointToStart(converter.getModuleOp().getBody());
+ auto mlirType = converter.genType(varType.declTypeSpec->derivedTypeSpec());
+ auto varVal = firOpBuilder.createTemporaryAlloc(
+ converter.getCurrentLocation(), mlirType, varName.ToString());
+ symTable.addSymbol(*varName.symbol, varVal);
+
+ mlir::omp::DeclareMapperOperands clauseOps;
+ const auto *clauseList{
+ parser::Unwrap<parser::OmpClauseList>(declareMapperConstruct.t)};
+ List<Clause> clauses = makeClauses(*clauseList, semaCtx);
+ ClauseProcessor cp(converter, semaCtx, clauses);
+ cp.processMap(converter.getCurrentLocation(), stmtCtx, clauseOps);
+ auto declMapperOp = firOpBuilder.create<mlir::omp::DeclareMapperOp>(
+ converter.getCurrentLocation(), mapperNameStr.str(), varVal, mlirType,
+ clauseOps.mapVars);
+ converter.getMLIRSymbolTable()->insert(declMapperOp.getOperation());
+ firOpBuilder.restoreInsertionPoint(insPt);
}
static void
diff --git a/flang/lib/Optimizer/OpenMP/MapInfoFinalization.cpp b/flang/lib/Optimizer/OpenMP/MapInfoFinalization.cpp
index 98e325c307d977..fa2072687c1775 100644
--- a/flang/lib/Optimizer/OpenMP/MapInfoFinalization.cpp
+++ b/flang/lib/Optimizer/OpenMP/MapInfoFinalization.cpp
@@ -464,7 +464,8 @@ class MapInfoFinalizationPass
for (auto *user : mapOp->getUsers()) {
if (llvm::isa<mlir::omp::TargetOp, mlir::omp::TargetDataOp,
mlir::omp::TargetUpdateOp, mlir::omp::TargetExitDataOp,
- mlir::omp::TargetEnterDataOp>(user))
+ mlir::omp::TargetEnterDataOp, mlir::omp::DeclareMapperOp>(
+ user))
return user;
if (auto mapUser = llvm::dyn_cast<mlir::omp::MapInfoOp>(user))
diff --git a/flang/test/Lower/OpenMP/Todo/omp-declare-mapper.f90 b/flang/test/Lower/OpenMP/Todo/omp-declare-mapper.f90
index 5ae48ff7360482..13a4da5849f8c0 100644
--- a/flang/test/Lower/OpenMP/Todo/omp-declare-mapper.f90
+++ b/flang/test/Lower/OpenMP/Todo/omp-declare-mapper.f90
@@ -10,7 +10,7 @@ subroutine declare_mapper_1
type my_type
integer :: num_vals
integer, allocatable :: values(:)
- end type
+ end type
type my_type2
type (my_type) :: my_type_var
@@ -21,7 +21,7 @@ subroutine declare_mapper_1
type (my_type2) :: t
real :: x, y(nvals)
!$omp declare mapper (my_type :: var) map (var, var%values (1:var%num_vals))
-!CHECK: not yet implemented: OpenMPDeclareMapperConstruct
+!CHECK: not yet implemented: lowering symbol to HLFIR
end subroutine declare_mapper_1
@@ -31,7 +31,7 @@ subroutine declare_mapper_2
type my_type
integer :: num_vals
integer, allocatable :: values(:)
- end type
+ end type
type my_type2
type (my_type) :: my_type_var
@@ -43,5 +43,5 @@ subroutine declare_mapper_2
real :: x, y(nvals)
!$omp declare mapper (my_mapper : my_type2 :: v) map (v%arr, x, y(:)) &
!$omp& map (alloc : v%temp)
-!CHECK: not yet implemented: OpenMPDeclareMapperConstruct
+!CHECK: not yet implemented: lowering symbol to HLFIR
end subroutine declare_mapper_2
diff --git a/flang/test/Lower/OpenMP/declare-mapper.f90 b/flang/test/Lower/OpenMP/declare-mapper.f90
new file mode 100644
index 00000000000000..fd018b4fbb0e0f
--- /dev/null
+++ b/flang/test/Lower/OpenMP/declare-mapper.f90
@@ -0,0 +1,31 @@
+! This test checks lowering of OpenMP declare mapper Directive.
+
+! RUN: split-file %s %t
+! RUN: %flang_fc1 -emit-hlfir -fopenmp -fopenmp-version=50 %t/declare-mapper-1.f90 -o - | FileCheck %t/declare-mapper-1.f90
+! RUN %flang_fc1 -emit-hlfir -fopenmp -fopenmp-version=50 %t/declare-mapper-2.f90 -o - | FileCheck %t/declare-mapper-2.f90
+
+!--- declare-mapper-1.f90
+subroutine mapper
+ implicit none
+ type my_type
+ integer, pointer :: my_buffer
+ integer :: my_buffer_size
+ end type
+ !CHECK: %[[MY_VAR:.*]] = fir.alloca ![[VAR_TYPE:.*]] {bindc_name = "my_var"}
+ !CHECK: %[[MAP_INFO:.*]] = omp.map.info var_ptr(%[[MY_VAR]] : !fir.ref<![[VAR_TYPE]]>, ![[VAR_TYPE]]) map_clauses(tofrom) capture(ByRef) -> !fir.ref<![[VAR_TYPE]]> {name = "my_var"}
+ !CHECK: omp.declare_mapper @my_mapper : %[[MY_VAR]] : !fir.ref<![[VAR_TYPE]]> : ![[VAR_TYPE]] map_entries(%[[MAP_INFO]] : !fir.ref<![[VAR_TYPE]]>)
+ !$omp DECLARE MAPPER(my_mapper : my_type :: my_var) map(tofrom : my_var)
+end subroutine
+
+!--- declare-mapper-2.f90
+subroutine mapper_default
+ implicit none
+ type my_type
+ integer, pointer :: my_buffer
+ integer :: my_buffer_size
+ end type
+ !CHECK: %[[MY_VAR:.*]] = fir.alloca ![[VAR_TYPE:.*]] {bindc_name = "my_var"}
+ !CHECK: %[[MAP_INFO:.*]] = omp.map.info var_ptr(%[[MY_VAR]] : !fir.ref<![[VAR_TYPE]]>, ![[VAR_TYPE]]) map_clauses(tofrom) capture(ByRef) -> !fir.ref<![[VAR_TYPE]]> {name = "my_var"}
+ !CHECK: omp.declare_mapper @default_my_type : %[[MY_VAR]] : !fir.ref<![[VAR_TYPE]]> : ![[VAR_TYPE]] map_entries(%[[MAP_INFO]] : !fir.ref<![[VAR_TYPE]]>)
+ !$omp DECLARE MAPPER(my_type :: my_var) map(tofrom : my_var)
+end subroutine
\ No newline at end of file
>From 4898d26a0083adaf3ec37545a0a4f9c9db3f00da Mon Sep 17 00:00:00 2001
From: Akash Banerjee <Akash.Banerjee at amd.com>
Date: Wed, 20 Nov 2024 20:55:45 +0000
Subject: [PATCH 03/23] Add emptyline to test.
---
flang/test/Lower/OpenMP/declare-mapper.f90 | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/flang/test/Lower/OpenMP/declare-mapper.f90 b/flang/test/Lower/OpenMP/declare-mapper.f90
index fd018b4fbb0e0f..eed40d2e4cdce5 100644
--- a/flang/test/Lower/OpenMP/declare-mapper.f90
+++ b/flang/test/Lower/OpenMP/declare-mapper.f90
@@ -28,4 +28,4 @@ subroutine mapper_default
!CHECK: %[[MAP_INFO:.*]] = omp.map.info var_ptr(%[[MY_VAR]] : !fir.ref<![[VAR_TYPE]]>, ![[VAR_TYPE]]) map_clauses(tofrom) capture(ByRef) -> !fir.ref<![[VAR_TYPE]]> {name = "my_var"}
!CHECK: omp.declare_mapper @default_my_type : %[[MY_VAR]] : !fir.ref<![[VAR_TYPE]]> : ![[VAR_TYPE]] map_entries(%[[MAP_INFO]] : !fir.ref<![[VAR_TYPE]]>)
!$omp DECLARE MAPPER(my_type :: my_var) map(tofrom : my_var)
-end subroutine
\ No newline at end of file
+end subroutine
>From d549130610e8c132e681e19259cfa32e47a36b64 Mon Sep 17 00:00:00 2001
From: Akash Banerjee <Akash.Banerjee at amd.com>
Date: Fri, 22 Nov 2024 12:50:32 +0000
Subject: [PATCH 04/23] Addressed reviewer comments. Added assert to check for
derived type.
---
flang/lib/Lower/OpenMP/OpenMP.cpp | 20 +++++++++++---------
1 file changed, 11 insertions(+), 9 deletions(-)
diff --git a/flang/lib/Lower/OpenMP/OpenMP.cpp b/flang/lib/Lower/OpenMP/OpenMP.cpp
index a7347c67d552cd..db84758ae99d1a 100644
--- a/flang/lib/Lower/OpenMP/OpenMP.cpp
+++ b/flang/lib/Lower/OpenMP/OpenMP.cpp
@@ -40,7 +40,6 @@
#include "mlir/Transforms/RegionUtils.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/Frontend/OpenMP/OMPConstants.h"
-#include <string>
using namespace Fortran::lower::omp;
using namespace Fortran::common::openmp;
@@ -3127,13 +3126,16 @@ genOMP(lower::AbstractConverter &converter, lower::SymMap &symTable,
const auto &mapperName{std::get<std::optional<parser::Name>>(spec.t)};
const auto &varType{std::get<parser::TypeSpec>(spec.t)};
const auto &varName{std::get<parser::Name>(spec.t)};
- std::stringstream mapperNameStr;
- if (mapperName.has_value()) {
- mapperNameStr << mapperName->ToString();
- } else {
- mapperNameStr << "default_"
- << varType.declTypeSpec->derivedTypeSpec().name().ToString();
- }
+ assert(varType.declTypeSpec->category() ==
+ semantics::DeclTypeSpec::Category::TypeDerived &&
+ "Expected derived type");
+
+ std::string mapperNameStr;
+ if (mapperName.has_value())
+ mapperNameStr = mapperName->ToString();
+ else
+ mapperNameStr =
+ "default_" + varType.declTypeSpec->derivedTypeSpec().name().ToString();
mlir::OpBuilder::InsertPoint insPt = firOpBuilder.saveInsertionPoint();
firOpBuilder.setInsertionPointToStart(converter.getModuleOp().getBody());
@@ -3149,7 +3151,7 @@ genOMP(lower::AbstractConverter &converter, lower::SymMap &symTable,
ClauseProcessor cp(converter, semaCtx, clauses);
cp.processMap(converter.getCurrentLocation(), stmtCtx, clauseOps);
auto declMapperOp = firOpBuilder.create<mlir::omp::DeclareMapperOp>(
- converter.getCurrentLocation(), mapperNameStr.str(), varVal, mlirType,
+ converter.getCurrentLocation(), mapperNameStr, varVal, mlirType,
clauseOps.mapVars);
converter.getMLIRSymbolTable()->insert(declMapperOp.getOperation());
firOpBuilder.restoreInsertionPoint(insPt);
>From 7c46df20f37efdb5dcc5429d294e54bb9f76a040 Mon Sep 17 00:00:00 2001
From: Akash Banerjee <Akash.Banerjee at amd.com>
Date: Thu, 28 Nov 2024 21:17:40 +0000
Subject: [PATCH 05/23] Add lowering changes for declMapperOp's region.
---
flang/lib/Lower/OpenMP/OpenMP.cpp | 49 ++++++--
.../Optimizer/OpenMP/MapInfoFinalization.cpp | 4 +-
.../Lower/OpenMP/Todo/omp-declare-mapper.f90 | 47 --------
flang/test/Lower/OpenMP/declare-mapper.f90 | 105 ++++++++++++++----
4 files changed, 123 insertions(+), 82 deletions(-)
delete mode 100644 flang/test/Lower/OpenMP/Todo/omp-declare-mapper.f90
diff --git a/flang/lib/Lower/OpenMP/OpenMP.cpp b/flang/lib/Lower/OpenMP/OpenMP.cpp
index db84758ae99d1a..cdf8f4b77c0c4c 100644
--- a/flang/lib/Lower/OpenMP/OpenMP.cpp
+++ b/flang/lib/Lower/OpenMP/OpenMP.cpp
@@ -3119,6 +3119,7 @@ static void
genOMP(lower::AbstractConverter &converter, lower::SymMap &symTable,
semantics::SemanticsContext &semaCtx, lower::pft::Evaluation &eval,
const parser::OpenMPDeclareMapperConstruct &declareMapperConstruct) {
+ mlir::Location loc = converter.genLocation(declareMapperConstruct.source);
fir::FirOpBuilder &firOpBuilder = converter.getFirOpBuilder();
lower::StatementContext stmtCtx;
const auto &spec =
@@ -3136,24 +3137,50 @@ genOMP(lower::AbstractConverter &converter, lower::SymMap &symTable,
else
mapperNameStr =
"default_" + varType.declTypeSpec->derivedTypeSpec().name().ToString();
+ auto mlirType = converter.genType(varType.declTypeSpec->derivedTypeSpec());
+ auto declMapperOp = firOpBuilder.create<mlir::omp::DeclareMapperOp>(
+ loc, mapperNameStr, mlirType);
+ auto ®ion = declMapperOp.getRegion();
+ // Save insert point just after the DeclMapperOp.
mlir::OpBuilder::InsertPoint insPt = firOpBuilder.saveInsertionPoint();
- firOpBuilder.setInsertionPointToStart(converter.getModuleOp().getBody());
- auto mlirType = converter.genType(varType.declTypeSpec->derivedTypeSpec());
- auto varVal = firOpBuilder.createTemporaryAlloc(
- converter.getCurrentLocation(), mlirType, varName.ToString());
- symTable.addSymbol(*varName.symbol, varVal);
+ firOpBuilder.createBlock(®ion);
+ auto varVal =
+ firOpBuilder.createTemporaryAlloc(loc, mlirType, varName.ToString());
+ converter.bindSymbol(*varName.symbol, varVal);
- mlir::omp::DeclareMapperOperands clauseOps;
+ // Insert dummy instruction to remember the insertion position. The
+ // marker will be deleted by clean up passes since there are no uses.
+ // Remembering the position for further insertion is important since
+ // there are hlfir.declares inserted above while setting block arguments
+ // and new code from the body should be inserted after that.
+ mlir::Value undefMarker =
+ firOpBuilder.create<fir::UndefOp>(loc, firOpBuilder.getIndexType());
+
+ // Create blocks for unstructured regions. This has to be done since
+ // blocks are initially allocated with the function as the parent region.
+ if (eval.lowerAsUnstructured()) {
+ lower::createEmptyRegionBlocks<mlir::omp::TerminatorOp, mlir::omp::YieldOp>(
+ firOpBuilder, eval.getNestedEvaluations());
+ }
+
+ firOpBuilder.create<mlir::omp::TerminatorOp>(loc);
+
+ // Set the insertion point after the marker.
+ firOpBuilder.setInsertionPointAfter(undefMarker.getDefiningOp());
+ genNestedEvaluations(converter, eval);
+
+ // Populate the declareMapper region with the map information.
+ mlir::omp::DeclareMapperInfoOperands clauseOps;
const auto *clauseList{
parser::Unwrap<parser::OmpClauseList>(declareMapperConstruct.t)};
List<Clause> clauses = makeClauses(*clauseList, semaCtx);
ClauseProcessor cp(converter, semaCtx, clauses);
- cp.processMap(converter.getCurrentLocation(), stmtCtx, clauseOps);
- auto declMapperOp = firOpBuilder.create<mlir::omp::DeclareMapperOp>(
- converter.getCurrentLocation(), mapperNameStr, varVal, mlirType,
- clauseOps.mapVars);
- converter.getMLIRSymbolTable()->insert(declMapperOp.getOperation());
+ cp.processMap(loc, stmtCtx, clauseOps);
+
+ firOpBuilder.create<mlir::omp::DeclareMapperInfoOp>(loc, clauseOps.mapVars);
+
+ // Restore the insert point to just after the DeclareMapperOp.
firOpBuilder.restoreInsertionPoint(insPt);
}
diff --git a/flang/lib/Optimizer/OpenMP/MapInfoFinalization.cpp b/flang/lib/Optimizer/OpenMP/MapInfoFinalization.cpp
index fa2072687c1775..7bbb005ec46ab7 100644
--- a/flang/lib/Optimizer/OpenMP/MapInfoFinalization.cpp
+++ b/flang/lib/Optimizer/OpenMP/MapInfoFinalization.cpp
@@ -464,8 +464,8 @@ class MapInfoFinalizationPass
for (auto *user : mapOp->getUsers()) {
if (llvm::isa<mlir::omp::TargetOp, mlir::omp::TargetDataOp,
mlir::omp::TargetUpdateOp, mlir::omp::TargetExitDataOp,
- mlir::omp::TargetEnterDataOp, mlir::omp::DeclareMapperOp>(
- user))
+ mlir::omp::TargetEnterDataOp,
+ mlir::omp::DeclareMapperInfoOp>(user))
return user;
if (auto mapUser = llvm::dyn_cast<mlir::omp::MapInfoOp>(user))
diff --git a/flang/test/Lower/OpenMP/Todo/omp-declare-mapper.f90 b/flang/test/Lower/OpenMP/Todo/omp-declare-mapper.f90
deleted file mode 100644
index 13a4da5849f8c0..00000000000000
--- a/flang/test/Lower/OpenMP/Todo/omp-declare-mapper.f90
+++ /dev/null
@@ -1,47 +0,0 @@
-! This test checks lowering of OpenMP declare mapper Directive.
-
-! RUN: split-file %s %t
-! RUN: not %flang_fc1 -emit-fir -fopenmp -fopenmp-version=50 %t/omp-declare-mapper-1.f90 2>&1 | FileCheck %t/omp-declare-mapper-1.f90
-! RUN not %flang_fc1 -emit-fir -fopenmp -fopenmp-version=50 %t/omp-declare-mapper-2.f90 2>&1 | FileCheck %t/omp-declare-mapper-2.f90
-
-!--- omp-declare-mapper-1.f90
-subroutine declare_mapper_1
- integer,parameter :: nvals = 250
- type my_type
- integer :: num_vals
- integer, allocatable :: values(:)
- end type
-
- type my_type2
- type (my_type) :: my_type_var
- type (my_type) :: temp
- real,dimension(nvals) :: unmapped
- real,dimension(nvals) :: arr
- end type
- type (my_type2) :: t
- real :: x, y(nvals)
- !$omp declare mapper (my_type :: var) map (var, var%values (1:var%num_vals))
-!CHECK: not yet implemented: lowering symbol to HLFIR
-end subroutine declare_mapper_1
-
-
-!--- omp-declare-mapper-2.f90
-subroutine declare_mapper_2
- integer,parameter :: nvals = 250
- type my_type
- integer :: num_vals
- integer, allocatable :: values(:)
- end type
-
- type my_type2
- type (my_type) :: my_type_var
- type (my_type) :: temp
- real,dimension(nvals) :: unmapped
- real,dimension(nvals) :: arr
- end type
- type (my_type2) :: t
- real :: x, y(nvals)
- !$omp declare mapper (my_mapper : my_type2 :: v) map (v%arr, x, y(:)) &
- !$omp& map (alloc : v%temp)
-!CHECK: not yet implemented: lowering symbol to HLFIR
-end subroutine declare_mapper_2
diff --git a/flang/test/Lower/OpenMP/declare-mapper.f90 b/flang/test/Lower/OpenMP/declare-mapper.f90
index eed40d2e4cdce5..e29cb93cf924dc 100644
--- a/flang/test/Lower/OpenMP/declare-mapper.f90
+++ b/flang/test/Lower/OpenMP/declare-mapper.f90
@@ -1,31 +1,92 @@
! This test checks lowering of OpenMP declare mapper Directive.
! RUN: split-file %s %t
-! RUN: %flang_fc1 -emit-hlfir -fopenmp -fopenmp-version=50 %t/declare-mapper-1.f90 -o - | FileCheck %t/declare-mapper-1.f90
-! RUN %flang_fc1 -emit-hlfir -fopenmp -fopenmp-version=50 %t/declare-mapper-2.f90 -o - | FileCheck %t/declare-mapper-2.f90
+! RUN: %flang_fc1 -emit-hlfir -fopenmp -fopenmp-version=50 %t/omp-declare-mapper-1.f90 -o - | FileCheck %t/omp-declare-mapper-1.f90
+! RUN %flang_fc1 -emit-hlfir -fopenmp -fopenmp-version=50 %t/omp-declare-mapper-2.f90 -o - | FileCheck %t/omp-declare-mapper-2.f90
-!--- declare-mapper-1.f90
-subroutine mapper
- implicit none
+!--- omp-declare-mapper-1.f90
+subroutine declare_mapper_1
+ integer, parameter :: nvals = 250
type my_type
- integer, pointer :: my_buffer
- integer :: my_buffer_size
+ integer :: num_vals
+ integer, allocatable :: values(:)
end type
- !CHECK: %[[MY_VAR:.*]] = fir.alloca ![[VAR_TYPE:.*]] {bindc_name = "my_var"}
- !CHECK: %[[MAP_INFO:.*]] = omp.map.info var_ptr(%[[MY_VAR]] : !fir.ref<![[VAR_TYPE]]>, ![[VAR_TYPE]]) map_clauses(tofrom) capture(ByRef) -> !fir.ref<![[VAR_TYPE]]> {name = "my_var"}
- !CHECK: omp.declare_mapper @my_mapper : %[[MY_VAR]] : !fir.ref<![[VAR_TYPE]]> : ![[VAR_TYPE]] map_entries(%[[MAP_INFO]] : !fir.ref<![[VAR_TYPE]]>)
- !$omp DECLARE MAPPER(my_mapper : my_type :: my_var) map(tofrom : my_var)
-end subroutine
-!--- declare-mapper-2.f90
-subroutine mapper_default
- implicit none
+ type my_type2
+ type(my_type) :: my_type_var
+ type(my_type) :: temp
+ real, dimension(nvals) :: unmapped
+ real, dimension(nvals) :: arr
+ end type
+ type(my_type2) :: t
+ real :: x, y(nvals)
+ !CHECK: omp.declare_mapper @default_my_type : ![[VAR_TYPE:.*]] {
+ !CHECK: %[[VAL_5:.*]] = fir.alloca ![[VAR_TYPE]] {bindc_name = "var"}
+ !CHECK: %[[VAL_6:.*]]:2 = hlfir.declare %[[VAL_5]] {uniq_name = "_QFdeclare_mapper_1Evar"} : (!fir.ref<![[VAR_TYPE]]>) -> (!fir.ref<![[VAR_TYPE]]>, !fir.ref<![[VAR_TYPE]]>)
+ !CHECK: %[[VAL_7:.*]] = hlfir.designate %[[VAL_6]]#0{{.*}} : (!fir.ref<![[VAR_TYPE]]>) -> !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>
+ !CHECK: %[[VAL_8:.*]] = fir.load %[[VAL_7]] : !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>
+ !CHECK: %[[VAL_9:.*]] = arith.constant 0 : index
+ !CHECK: %[[VAL_10:.*]]:3 = fir.box_dims %[[VAL_8]], %[[VAL_9]] : (!fir.box<!fir.heap<!fir.array<?xi32>>>, index) -> (index, index, index)
+ !CHECK: %[[VAL_11:.*]] = arith.constant 1 : index
+ !CHECK: %[[VAL_12:.*]] = arith.constant 1 : index
+ !CHECK: %[[VAL_13:.*]] = arith.subi %[[VAL_12]], %[[VAL_10]]#0 : index
+ !CHECK: %[[VAL_14:.*]] = hlfir.designate %[[VAL_6]]#0{"num_vals"} : (!fir.ref<![[VAR_TYPE]]>) -> !fir.ref<i32>
+ !CHECK: %[[VAL_15:.*]] = fir.load %[[VAL_14]] : !fir.ref<i32>
+ !CHECK: %[[VAL_16:.*]] = fir.convert %[[VAL_15]] : (i32) -> i64
+ !CHECK: %[[VAL_17:.*]] = fir.convert %[[VAL_16]] : (i64) -> index
+ !CHECK: %[[VAL_18:.*]] = arith.subi %[[VAL_17]], %[[VAL_10]]#0 : index
+ !CHECK: %[[VAL_19:.*]] = omp.map.bounds lower_bound(%[[VAL_13]] : index) upper_bound(%[[VAL_18]] : index) extent(%[[VAL_10]]#1 : index) stride(%[[VAL_11]] : index) start_idx(%[[VAL_10]]#0 : index)
+ !CHECK: %[[VAL_20:.*]] = arith.constant 1 : index
+ !CHECK: %[[VAL_21:.*]] = fir.coordinate_of %[[VAL_6]]#0, %[[VAL_20]] : (!fir.ref<![[VAR_TYPE]]>, index) -> !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>
+ !CHECK: %[[VAL_22:.*]] = fir.box_offset %[[VAL_21]] base_addr : (!fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>) -> !fir.llvm_ptr<!fir.ref<!fir.array<?xi32>>>
+ !CHECK: %[[VAL_23:.*]] = omp.map.info var_ptr(%[[VAL_21]] : !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>, !fir.array<?xi32>) var_ptr_ptr(%[[VAL_22]] : !fir.llvm_ptr<!fir.ref<!fir.array<?xi32>>>) map_clauses(tofrom) capture(ByRef) bounds(%[[VAL_19]]) -> !fir.llvm_ptr<!fir.ref<!fir.array<?xi32>>> {name = ""}
+ !CHECK: %[[VAL_24:.*]] = omp.map.info var_ptr(%[[VAL_21]] : !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>, !fir.box<!fir.heap<!fir.array<?xi32>>>) map_clauses(to) capture(ByRef) -> !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>> {name = "var%[[VAL_25:.*]](1:var%[[VAL_26:.*]])"}
+ !CHECK: %[[VAL_27:.*]] = omp.map.info var_ptr(%[[VAL_6]]#1 : !fir.ref<![[VAR_TYPE]]>, ![[VAR_TYPE]]) map_clauses(tofrom) capture(ByRef) members(%[[VAL_24]], %[[VAL_23]] : [1], [1, 0] : !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>, !fir.llvm_ptr<!fir.ref<!fir.array<?xi32>>>) -> !fir.ref<![[VAR_TYPE]]> {name = "var"}
+ !CHECK: omp.declare_mapper_info map_entries(%[[VAL_27]], %[[VAL_24]], %[[VAL_23]] : !fir.ref<![[VAR_TYPE]]>, !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>, !fir.llvm_ptr<!fir.ref<!fir.array<?xi32>>>)
+ !CHECK: omp.terminator
+ !CHECK: }
+ !$omp declare mapper (my_type :: var) map (var, var%values (1:var%num_vals))
+end subroutine declare_mapper_1
+
+!--- omp-declare-mapper-2.f90
+subroutine declare_mapper_2
+ integer, parameter :: nvals = 250
type my_type
- integer, pointer :: my_buffer
- integer :: my_buffer_size
+ integer :: num_vals
+ integer, allocatable :: values(:)
+ end type
+
+ type my_type2
+ type(my_type) :: my_type_var
+ type(my_type) :: temp
+ real, dimension(nvals) :: unmapped
+ real, dimension(nvals) :: arr
end type
- !CHECK: %[[MY_VAR:.*]] = fir.alloca ![[VAR_TYPE:.*]] {bindc_name = "my_var"}
- !CHECK: %[[MAP_INFO:.*]] = omp.map.info var_ptr(%[[MY_VAR]] : !fir.ref<![[VAR_TYPE]]>, ![[VAR_TYPE]]) map_clauses(tofrom) capture(ByRef) -> !fir.ref<![[VAR_TYPE]]> {name = "my_var"}
- !CHECK: omp.declare_mapper @default_my_type : %[[MY_VAR]] : !fir.ref<![[VAR_TYPE]]> : ![[VAR_TYPE]] map_entries(%[[MAP_INFO]] : !fir.ref<![[VAR_TYPE]]>)
- !$omp DECLARE MAPPER(my_type :: my_var) map(tofrom : my_var)
-end subroutine
+ type(my_type2) :: t
+ real :: x, y(nvals)
+ !CHECK: omp.declare_mapper @my_mapper : ![[VAR_TYPE:.*]] {
+ !CHECK: %[[VAL_0:.*]] = fir.alloca ![[VAR_TYPE]] {bindc_name = "v"}
+ !CHECK: %[[VAL_1:.*]]:2 = hlfir.declare %[[VAL_0]] {uniq_name = "_QFdeclare_mapper_2Ev"} : (!fir.ref<![[VAR_TYPE]]>) -> (!fir.ref<![[VAR_TYPE]]>, !fir.ref<![[VAR_TYPE]]>)
+ !CHECK: %[[VAL_2:.*]] = arith.constant 250 : index
+ !CHECK: %[[VAL_3:.*]] = fir.shape %[[VAL_2]] : (index) -> !fir.shape<1>
+ !CHECK: %[[VAL_4:.*]] = hlfir.designate %[[VAL_1]]#0{"arr"} shape %[[VAL_3]] : (!fir.ref<![[VAR_TYPE]]>, !fir.shape<1>) -> !fir.ref<!fir.array<250xf32>>
+ !CHECK: %[[VAL_5:.*]] = arith.constant 1 : index
+ !CHECK: %[[VAL_6:.*]] = arith.constant 0 : index
+ !CHECK: %[[VAL_7:.*]] = arith.subi %[[VAL_2]], %[[VAL_5]] : index
+ !CHECK: %[[VAL_8:.*]] = omp.map.bounds lower_bound(%[[VAL_6]] : index) upper_bound(%[[VAL_7]] : index) extent(%[[VAL_2]] : index) stride(%[[VAL_5]] : index) start_idx(%[[VAL_5]] : index)
+ !CHECK: %[[VAL_9:.*]] = omp.map.info var_ptr(%[[VAL_4]] : !fir.ref<!fir.array<250xf32>>, !fir.array<250xf32>) map_clauses(tofrom) capture(ByRef) bounds(%[[VAL_8]]) -> !fir.ref<!fir.array<250xf32>> {name = "v%[[VAL_10:.*]]"}
+ !CHECK: %[[VAL_11:.*]] = omp.map.info var_ptr(%[[VAL_12:.*]]#1 : !fir.ref<f32>, f32) map_clauses(tofrom) capture(ByRef) -> !fir.ref<f32> {name = "x"}
+ !CHECK: %[[VAL_13:.*]] = arith.constant 0 : index
+ !CHECK: %[[VAL_14:.*]] = arith.constant 1 : index
+ !CHECK: %[[VAL_15:.*]] = arith.subi %[[VAL_16:.*]], %[[VAL_14]] : index
+ !CHECK: %[[VAL_17:.*]] = omp.map.bounds lower_bound(%[[VAL_13]] : index) upper_bound(%[[VAL_15]] : index) extent(%[[VAL_16]] : index) stride(%[[VAL_14]] : index) start_idx(%[[VAL_14]] : index)
+ !CHECK: %[[VAL_18:.*]] = omp.map.info var_ptr(%[[VAL_19:.*]]#1 : !fir.ref<!fir.array<250xf32>>, !fir.array<250xf32>) map_clauses(tofrom) capture(ByRef) bounds(%[[VAL_17]]) -> !fir.ref<!fir.array<250xf32>> {name = "y(:)"}
+ !CHECK: %[[VAL_20:.*]] = hlfir.designate %[[VAL_1]]#0{"temp"} : (!fir.ref<![[VAR_TYPE]]>) -> !fir.ref<!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>
+ !CHECK: %[[VAL_21:.*]] = omp.map.info var_ptr(%[[VAL_20]] : !fir.ref<!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>, !fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>) map_clauses(exit_release_or_enter_alloc) capture(ByRef) -> !fir.ref<!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>> {name = "v%[[VAL_22:.*]]"}
+ !CHECK: %[[VAL_23:.*]] = omp.map.info var_ptr(%[[VAL_1]]#1 : !fir.ref<![[VAR_TYPE]]>, ![[VAR_TYPE]]) map_clauses(tofrom) capture(ByRef) members(%[[VAL_9]], %[[VAL_21]] : [3], [1] : !fir.ref<!fir.array<250xf32>>, !fir.ref<!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>) -> !fir.ref<![[VAR_TYPE]]> {name = "v", partial_map = true}
+ !CHECK: omp.declare_mapper_info map_entries(%[[VAL_11]], %[[VAL_18]], %[[VAL_23]], %[[VAL_9]], %[[VAL_21]] : !fir.ref<f32>, !fir.ref<!fir.array<250xf32>>, !fir.ref<![[VAR_TYPE]]>, !fir.ref<!fir.array<250xf32>>, !fir.ref<!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>)
+ !CHECK: omp.terminator
+ !CHECK: }
+ !$omp declare mapper (my_mapper : my_type2 :: v) map (v%arr, x, y(:)) &
+ !$omp& map (alloc : v%temp)
+end subroutine declare_mapper_2
>From aba8787f5aa9a0280ba0bfc05678a90d8ad1410d Mon Sep 17 00:00:00 2001
From: Akash Banerjee <Akash.Banerjee at amd.com>
Date: Mon, 23 Dec 2024 18:43:49 +0000
Subject: [PATCH 06/23] Add required changes to hoist DeclareMapperOp to the
ModuleOp's region. Add a new name mangling method which accepts a user
supplied scope to mangle the name, in addition to the existing function which
always used the currentScope.
---
flang/include/flang/Lower/AbstractConverter.h | 2 +
flang/lib/Lower/Bridge.cpp | 5 +
flang/lib/Lower/OpenMP/OpenMP.cpp | 45 +++------
flang/test/Lower/OpenMP/declare-mapper.f90 | 95 +++++++++----------
4 files changed, 65 insertions(+), 82 deletions(-)
diff --git a/flang/include/flang/Lower/AbstractConverter.h b/flang/include/flang/Lower/AbstractConverter.h
index 3d2b805da6f477..1d1323642bf9c1 100644
--- a/flang/include/flang/Lower/AbstractConverter.h
+++ b/flang/include/flang/Lower/AbstractConverter.h
@@ -314,6 +314,8 @@ class AbstractConverter {
mangleName(const Fortran::semantics::DerivedTypeSpec &) = 0;
/// Unique a compiler generated name (add a containing scope specific prefix)
virtual std::string mangleName(std::string &) = 0;
+ /// Unique a compiler generated name (add a provided scope specific prefix)
+ virtual std::string mangleName(std::string &, const semantics::Scope &) = 0;
/// Return the field name for a derived type component inside a fir.record
/// type.
virtual std::string
diff --git a/flang/lib/Lower/Bridge.cpp b/flang/lib/Lower/Bridge.cpp
index a31629b17cf295..8b23f17cda5b14 100644
--- a/flang/lib/Lower/Bridge.cpp
+++ b/flang/lib/Lower/Bridge.cpp
@@ -1048,6 +1048,11 @@ class FirConverter : public Fortran::lower::AbstractConverter {
return Fortran::lower::mangle::mangleName(name, getCurrentScope(),
scopeBlockIdMap);
}
+ std::string
+ mangleName(std::string &name,
+ const Fortran::semantics::Scope &myScope) override final {
+ return Fortran::lower::mangle::mangleName(name, myScope, scopeBlockIdMap);
+ }
std::string getRecordTypeFieldName(
const Fortran::semantics::Symbol &component) override final {
return Fortran::lower::mangle::getRecordTypeFieldName(component,
diff --git a/flang/lib/Lower/OpenMP/OpenMP.cpp b/flang/lib/Lower/OpenMP/OpenMP.cpp
index cdf8f4b77c0c4c..67a429e78aed2f 100644
--- a/flang/lib/Lower/OpenMP/OpenMP.cpp
+++ b/flang/lib/Lower/OpenMP/OpenMP.cpp
@@ -3132,44 +3132,30 @@ genOMP(lower::AbstractConverter &converter, lower::SymMap &symTable,
"Expected derived type");
std::string mapperNameStr;
- if (mapperName.has_value())
+ if (mapperName.has_value()) {
mapperNameStr = mapperName->ToString();
- else
mapperNameStr =
- "default_" + varType.declTypeSpec->derivedTypeSpec().name().ToString();
+ converter.mangleName(mapperNameStr, mapperName->symbol->owner());
+ } else {
+ mapperNameStr =
+ varType.declTypeSpec->derivedTypeSpec().name().ToString() + ".default";
+ mapperNameStr = converter.mangleName(
+ mapperNameStr, *varType.declTypeSpec->derivedTypeSpec().GetScope());
+ }
+
+ // Save insert point just after the DeclMapperOp.
+ mlir::OpBuilder::InsertPoint insPt = firOpBuilder.saveInsertionPoint();
+
+ firOpBuilder.setInsertionPointToStart(converter.getModuleOp().getBody());
auto mlirType = converter.genType(varType.declTypeSpec->derivedTypeSpec());
auto declMapperOp = firOpBuilder.create<mlir::omp::DeclareMapperOp>(
loc, mapperNameStr, mlirType);
+ converter.getMLIRSymbolTable()->insert(declMapperOp);
auto ®ion = declMapperOp.getRegion();
-
- // Save insert point just after the DeclMapperOp.
- mlir::OpBuilder::InsertPoint insPt = firOpBuilder.saveInsertionPoint();
firOpBuilder.createBlock(®ion);
- auto varVal =
- firOpBuilder.createTemporaryAlloc(loc, mlirType, varName.ToString());
+ auto varVal = region.addArgument(firOpBuilder.getRefType(mlirType), loc);
converter.bindSymbol(*varName.symbol, varVal);
- // Insert dummy instruction to remember the insertion position. The
- // marker will be deleted by clean up passes since there are no uses.
- // Remembering the position for further insertion is important since
- // there are hlfir.declares inserted above while setting block arguments
- // and new code from the body should be inserted after that.
- mlir::Value undefMarker =
- firOpBuilder.create<fir::UndefOp>(loc, firOpBuilder.getIndexType());
-
- // Create blocks for unstructured regions. This has to be done since
- // blocks are initially allocated with the function as the parent region.
- if (eval.lowerAsUnstructured()) {
- lower::createEmptyRegionBlocks<mlir::omp::TerminatorOp, mlir::omp::YieldOp>(
- firOpBuilder, eval.getNestedEvaluations());
- }
-
- firOpBuilder.create<mlir::omp::TerminatorOp>(loc);
-
- // Set the insertion point after the marker.
- firOpBuilder.setInsertionPointAfter(undefMarker.getDefiningOp());
- genNestedEvaluations(converter, eval);
-
// Populate the declareMapper region with the map information.
mlir::omp::DeclareMapperInfoOperands clauseOps;
const auto *clauseList{
@@ -3177,7 +3163,6 @@ genOMP(lower::AbstractConverter &converter, lower::SymMap &symTable,
List<Clause> clauses = makeClauses(*clauseList, semaCtx);
ClauseProcessor cp(converter, semaCtx, clauses);
cp.processMap(loc, stmtCtx, clauseOps);
-
firOpBuilder.create<mlir::omp::DeclareMapperInfoOp>(loc, clauseOps.mapVars);
// Restore the insert point to just after the DeclareMapperOp.
diff --git a/flang/test/Lower/OpenMP/declare-mapper.f90 b/flang/test/Lower/OpenMP/declare-mapper.f90
index e29cb93cf924dc..4d9e201ea07dae 100644
--- a/flang/test/Lower/OpenMP/declare-mapper.f90
+++ b/flang/test/Lower/OpenMP/declare-mapper.f90
@@ -2,7 +2,7 @@
! RUN: split-file %s %t
! RUN: %flang_fc1 -emit-hlfir -fopenmp -fopenmp-version=50 %t/omp-declare-mapper-1.f90 -o - | FileCheck %t/omp-declare-mapper-1.f90
-! RUN %flang_fc1 -emit-hlfir -fopenmp -fopenmp-version=50 %t/omp-declare-mapper-2.f90 -o - | FileCheck %t/omp-declare-mapper-2.f90
+! RUN: %flang_fc1 -emit-hlfir -fopenmp -fopenmp-version=50 %t/omp-declare-mapper-2.f90 -o - | FileCheck %t/omp-declare-mapper-2.f90
!--- omp-declare-mapper-1.f90
subroutine declare_mapper_1
@@ -20,31 +20,30 @@ subroutine declare_mapper_1
end type
type(my_type2) :: t
real :: x, y(nvals)
- !CHECK: omp.declare_mapper @default_my_type : ![[VAR_TYPE:.*]] {
- !CHECK: %[[VAL_5:.*]] = fir.alloca ![[VAR_TYPE]] {bindc_name = "var"}
- !CHECK: %[[VAL_6:.*]]:2 = hlfir.declare %[[VAL_5]] {uniq_name = "_QFdeclare_mapper_1Evar"} : (!fir.ref<![[VAR_TYPE]]>) -> (!fir.ref<![[VAR_TYPE]]>, !fir.ref<![[VAR_TYPE]]>)
- !CHECK: %[[VAL_7:.*]] = hlfir.designate %[[VAL_6]]#0{{.*}} : (!fir.ref<![[VAR_TYPE]]>) -> !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>
- !CHECK: %[[VAL_8:.*]] = fir.load %[[VAL_7]] : !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>
- !CHECK: %[[VAL_9:.*]] = arith.constant 0 : index
- !CHECK: %[[VAL_10:.*]]:3 = fir.box_dims %[[VAL_8]], %[[VAL_9]] : (!fir.box<!fir.heap<!fir.array<?xi32>>>, index) -> (index, index, index)
- !CHECK: %[[VAL_11:.*]] = arith.constant 1 : index
- !CHECK: %[[VAL_12:.*]] = arith.constant 1 : index
- !CHECK: %[[VAL_13:.*]] = arith.subi %[[VAL_12]], %[[VAL_10]]#0 : index
- !CHECK: %[[VAL_14:.*]] = hlfir.designate %[[VAL_6]]#0{"num_vals"} : (!fir.ref<![[VAR_TYPE]]>) -> !fir.ref<i32>
- !CHECK: %[[VAL_15:.*]] = fir.load %[[VAL_14]] : !fir.ref<i32>
- !CHECK: %[[VAL_16:.*]] = fir.convert %[[VAL_15]] : (i32) -> i64
- !CHECK: %[[VAL_17:.*]] = fir.convert %[[VAL_16]] : (i64) -> index
- !CHECK: %[[VAL_18:.*]] = arith.subi %[[VAL_17]], %[[VAL_10]]#0 : index
- !CHECK: %[[VAL_19:.*]] = omp.map.bounds lower_bound(%[[VAL_13]] : index) upper_bound(%[[VAL_18]] : index) extent(%[[VAL_10]]#1 : index) stride(%[[VAL_11]] : index) start_idx(%[[VAL_10]]#0 : index)
- !CHECK: %[[VAL_20:.*]] = arith.constant 1 : index
- !CHECK: %[[VAL_21:.*]] = fir.coordinate_of %[[VAL_6]]#0, %[[VAL_20]] : (!fir.ref<![[VAR_TYPE]]>, index) -> !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>
- !CHECK: %[[VAL_22:.*]] = fir.box_offset %[[VAL_21]] base_addr : (!fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>) -> !fir.llvm_ptr<!fir.ref<!fir.array<?xi32>>>
- !CHECK: %[[VAL_23:.*]] = omp.map.info var_ptr(%[[VAL_21]] : !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>, !fir.array<?xi32>) var_ptr_ptr(%[[VAL_22]] : !fir.llvm_ptr<!fir.ref<!fir.array<?xi32>>>) map_clauses(tofrom) capture(ByRef) bounds(%[[VAL_19]]) -> !fir.llvm_ptr<!fir.ref<!fir.array<?xi32>>> {name = ""}
- !CHECK: %[[VAL_24:.*]] = omp.map.info var_ptr(%[[VAL_21]] : !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>, !fir.box<!fir.heap<!fir.array<?xi32>>>) map_clauses(to) capture(ByRef) -> !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>> {name = "var%[[VAL_25:.*]](1:var%[[VAL_26:.*]])"}
- !CHECK: %[[VAL_27:.*]] = omp.map.info var_ptr(%[[VAL_6]]#1 : !fir.ref<![[VAR_TYPE]]>, ![[VAR_TYPE]]) map_clauses(tofrom) capture(ByRef) members(%[[VAL_24]], %[[VAL_23]] : [1], [1, 0] : !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>, !fir.llvm_ptr<!fir.ref<!fir.array<?xi32>>>) -> !fir.ref<![[VAR_TYPE]]> {name = "var"}
- !CHECK: omp.declare_mapper_info map_entries(%[[VAL_27]], %[[VAL_24]], %[[VAL_23]] : !fir.ref<![[VAR_TYPE]]>, !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>, !fir.llvm_ptr<!fir.ref<!fir.array<?xi32>>>)
- !CHECK: omp.terminator
- !CHECK: }
+ !CHECK-LABEL: omp.declare_mapper @_QQFdeclare_mapper_1my_type.default : !fir.type<_QFdeclare_mapper_1Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}> {
+ !CHECK: ^bb0(%[[VAL_0:.*]]: !fir.ref<!fir.type<_QFdeclare_mapper_1Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>):
+ !CHECK: %[[VAL_1:.*]]:2 = hlfir.declare %[[VAL_0]] {uniq_name = "_QFdeclare_mapper_1Evar"} : (!fir.ref<!fir.type<_QFdeclare_mapper_1Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>) -> (!fir.ref<!fir.type<_QFdeclare_mapper_1Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>, !fir.ref<!fir.type<_QFdeclare_mapper_1Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>)
+ !CHECK: %[[VAL_2:.*]] = hlfir.designate %[[VAL_1]]#0{"values"} {fortran_attrs = #fir.var_attrs<allocatable>} : (!fir.ref<!fir.type<_QFdeclare_mapper_1Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>) -> !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>
+ !CHECK: %[[VAL_3:.*]] = fir.load %[[VAL_2]] : !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>
+ !CHECK: %[[VAL_4:.*]] = fir.box_addr %[[VAL_3]] : (!fir.box<!fir.heap<!fir.array<?xi32>>>) -> !fir.heap<!fir.array<?xi32>>
+ !CHECK: %[[VAL_5:.*]] = arith.constant 0 : index
+ !CHECK: %[[VAL_6:.*]]:3 = fir.box_dims %[[VAL_3]], %[[VAL_5]] : (!fir.box<!fir.heap<!fir.array<?xi32>>>, index) -> (index, index, index)
+ !CHECK: %[[VAL_7:.*]] = arith.constant 0 : index
+ !CHECK: %[[VAL_8:.*]] = arith.constant 1 : index
+ !CHECK: %[[VAL_9:.*]] = arith.constant 1 : index
+ !CHECK: %[[VAL_10:.*]] = arith.subi %[[VAL_9]], %[[VAL_6]]#0 : index
+ !CHECK: %[[VAL_11:.*]] = hlfir.designate %[[VAL_1]]#0{"num_vals"} : (!fir.ref<!fir.type<_QFdeclare_mapper_1Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>) -> !fir.ref<i32>
+ !CHECK: %[[VAL_12:.*]] = fir.load %[[VAL_11]] : !fir.ref<i32>
+ !CHECK: %[[VAL_13:.*]] = fir.convert %[[VAL_12]] : (i32) -> i64
+ !CHECK: %[[VAL_14:.*]] = fir.convert %[[VAL_13]] : (i64) -> index
+ !CHECK: %[[VAL_15:.*]] = arith.subi %[[VAL_14]], %[[VAL_6]]#0 : index
+ !CHECK: %[[VAL_16:.*]] = omp.map.bounds lower_bound(%[[VAL_10]] : index) upper_bound(%[[VAL_15]] : index) extent(%[[VAL_6]]#1 : index) stride(%[[VAL_8]] : index) start_idx(%[[VAL_6]]#0 : index)
+ !CHECK: %[[VAL_17:.*]] = arith.constant 1 : index
+ !CHECK: %[[VAL_18:.*]] = fir.coordinate_of %[[VAL_1]]#0, %[[VAL_17]] : (!fir.ref<!fir.type<_QFdeclare_mapper_1Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>, index) -> !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>
+ !CHECK: %[[VAL_19:.*]] = omp.map.info var_ptr(%[[VAL_18]] : !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>, !fir.box<!fir.heap<!fir.array<?xi32>>>) map_clauses(tofrom) capture(ByRef) bounds(%[[VAL_16]]) -> !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>> {name = "var%[[VAL_20:.*]](1:var%[[VAL_21:.*]])"}
+ !CHECK: %[[VAL_22:.*]] = omp.map.info var_ptr(%[[VAL_1]]#1 : !fir.ref<!fir.type<_QFdeclare_mapper_1Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>, !fir.type<_QFdeclare_mapper_1Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>) map_clauses(tofrom) capture(ByRef) members(%[[VAL_19]] : [1] : !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>) -> !fir.ref<!fir.type<_QFdeclare_mapper_1Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>> {name = "var"}
+ !CHECK: omp.declare_mapper_info map_entries(%[[VAL_22]] : !fir.ref<!fir.type<_QFdeclare_mapper_1Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>)
+ !CHECK: }
!$omp declare mapper (my_type :: var) map (var, var%values (1:var%num_vals))
end subroutine declare_mapper_1
@@ -63,30 +62,22 @@ subroutine declare_mapper_2
real, dimension(nvals) :: arr
end type
type(my_type2) :: t
- real :: x, y(nvals)
- !CHECK: omp.declare_mapper @my_mapper : ![[VAR_TYPE:.*]] {
- !CHECK: %[[VAL_0:.*]] = fir.alloca ![[VAR_TYPE]] {bindc_name = "v"}
- !CHECK: %[[VAL_1:.*]]:2 = hlfir.declare %[[VAL_0]] {uniq_name = "_QFdeclare_mapper_2Ev"} : (!fir.ref<![[VAR_TYPE]]>) -> (!fir.ref<![[VAR_TYPE]]>, !fir.ref<![[VAR_TYPE]]>)
- !CHECK: %[[VAL_2:.*]] = arith.constant 250 : index
- !CHECK: %[[VAL_3:.*]] = fir.shape %[[VAL_2]] : (index) -> !fir.shape<1>
- !CHECK: %[[VAL_4:.*]] = hlfir.designate %[[VAL_1]]#0{"arr"} shape %[[VAL_3]] : (!fir.ref<![[VAR_TYPE]]>, !fir.shape<1>) -> !fir.ref<!fir.array<250xf32>>
- !CHECK: %[[VAL_5:.*]] = arith.constant 1 : index
- !CHECK: %[[VAL_6:.*]] = arith.constant 0 : index
- !CHECK: %[[VAL_7:.*]] = arith.subi %[[VAL_2]], %[[VAL_5]] : index
- !CHECK: %[[VAL_8:.*]] = omp.map.bounds lower_bound(%[[VAL_6]] : index) upper_bound(%[[VAL_7]] : index) extent(%[[VAL_2]] : index) stride(%[[VAL_5]] : index) start_idx(%[[VAL_5]] : index)
- !CHECK: %[[VAL_9:.*]] = omp.map.info var_ptr(%[[VAL_4]] : !fir.ref<!fir.array<250xf32>>, !fir.array<250xf32>) map_clauses(tofrom) capture(ByRef) bounds(%[[VAL_8]]) -> !fir.ref<!fir.array<250xf32>> {name = "v%[[VAL_10:.*]]"}
- !CHECK: %[[VAL_11:.*]] = omp.map.info var_ptr(%[[VAL_12:.*]]#1 : !fir.ref<f32>, f32) map_clauses(tofrom) capture(ByRef) -> !fir.ref<f32> {name = "x"}
- !CHECK: %[[VAL_13:.*]] = arith.constant 0 : index
- !CHECK: %[[VAL_14:.*]] = arith.constant 1 : index
- !CHECK: %[[VAL_15:.*]] = arith.subi %[[VAL_16:.*]], %[[VAL_14]] : index
- !CHECK: %[[VAL_17:.*]] = omp.map.bounds lower_bound(%[[VAL_13]] : index) upper_bound(%[[VAL_15]] : index) extent(%[[VAL_16]] : index) stride(%[[VAL_14]] : index) start_idx(%[[VAL_14]] : index)
- !CHECK: %[[VAL_18:.*]] = omp.map.info var_ptr(%[[VAL_19:.*]]#1 : !fir.ref<!fir.array<250xf32>>, !fir.array<250xf32>) map_clauses(tofrom) capture(ByRef) bounds(%[[VAL_17]]) -> !fir.ref<!fir.array<250xf32>> {name = "y(:)"}
- !CHECK: %[[VAL_20:.*]] = hlfir.designate %[[VAL_1]]#0{"temp"} : (!fir.ref<![[VAR_TYPE]]>) -> !fir.ref<!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>
- !CHECK: %[[VAL_21:.*]] = omp.map.info var_ptr(%[[VAL_20]] : !fir.ref<!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>, !fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>) map_clauses(exit_release_or_enter_alloc) capture(ByRef) -> !fir.ref<!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>> {name = "v%[[VAL_22:.*]]"}
- !CHECK: %[[VAL_23:.*]] = omp.map.info var_ptr(%[[VAL_1]]#1 : !fir.ref<![[VAR_TYPE]]>, ![[VAR_TYPE]]) map_clauses(tofrom) capture(ByRef) members(%[[VAL_9]], %[[VAL_21]] : [3], [1] : !fir.ref<!fir.array<250xf32>>, !fir.ref<!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>) -> !fir.ref<![[VAR_TYPE]]> {name = "v", partial_map = true}
- !CHECK: omp.declare_mapper_info map_entries(%[[VAL_11]], %[[VAL_18]], %[[VAL_23]], %[[VAL_9]], %[[VAL_21]] : !fir.ref<f32>, !fir.ref<!fir.array<250xf32>>, !fir.ref<![[VAR_TYPE]]>, !fir.ref<!fir.array<250xf32>>, !fir.ref<!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>)
- !CHECK: omp.terminator
- !CHECK: }
- !$omp declare mapper (my_mapper : my_type2 :: v) map (v%arr, x, y(:)) &
- !$omp& map (alloc : v%temp)
+ real :: x, y(nvals)
+ !CHECK-LABEL: omp.declare_mapper @_QQFdeclare_mapper_2my_mapper : !fir.type<_QFdeclare_mapper_2Tmy_type2{my_type_var:!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>,temp:!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>,unmapped:!fir.array<250xf32>,arr:!fir.array<250xf32>}> {
+ !CHECK: ^bb0(%[[VAL_0:.*]]: !fir.ref<!fir.type<_QFdeclare_mapper_2Tmy_type2{my_type_var:!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>,temp:!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>,unmapped:!fir.array<250xf32>,arr:!fir.array<250xf32>}>>):
+ !CHECK: %[[VAL_1:.*]]:2 = hlfir.declare %[[VAL_0]] {uniq_name = "_QFdeclare_mapper_2Ev"} : (!fir.ref<!fir.type<_QFdeclare_mapper_2Tmy_type2{my_type_var:!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>,temp:!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>,unmapped:!fir.array<250xf32>,arr:!fir.array<250xf32>}>>) -> (!fir.ref<!fir.type<_QFdeclare_mapper_2Tmy_type2{my_type_var:!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>,temp:!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>,unmapped:!fir.array<250xf32>,arr:!fir.array<250xf32>}>>, !fir.ref<!fir.type<_QFdeclare_mapper_2Tmy_type2{my_type_var:!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>,temp:!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>,unmapped:!fir.array<250xf32>,arr:!fir.array<250xf32>}>>)
+ !CHECK: %[[VAL_2:.*]] = arith.constant 250 : index
+ !CHECK: %[[VAL_3:.*]] = fir.shape %[[VAL_2]] : (index) -> !fir.shape<1>
+ !CHECK: %[[VAL_4:.*]] = hlfir.designate %[[VAL_1]]#0{"arr"} shape %[[VAL_3]] : (!fir.ref<!fir.type<_QFdeclare_mapper_2Tmy_type2{my_type_var:!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>,temp:!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>,unmapped:!fir.array<250xf32>,arr:!fir.array<250xf32>}>>, !fir.shape<1>) -> !fir.ref<!fir.array<250xf32>>
+ !CHECK: %[[VAL_5:.*]] = arith.constant 1 : index
+ !CHECK: %[[VAL_6:.*]] = arith.constant 0 : index
+ !CHECK: %[[VAL_7:.*]] = arith.subi %[[VAL_2]], %[[VAL_5]] : index
+ !CHECK: %[[VAL_8:.*]] = omp.map.bounds lower_bound(%[[VAL_6]] : index) upper_bound(%[[VAL_7]] : index) extent(%[[VAL_2]] : index) stride(%[[VAL_5]] : index) start_idx(%[[VAL_5]] : index)
+ !CHECK: %[[VAL_9:.*]] = omp.map.info var_ptr(%[[VAL_4]] : !fir.ref<!fir.array<250xf32>>, !fir.array<250xf32>) map_clauses(tofrom) capture(ByRef) bounds(%[[VAL_8]]) -> !fir.ref<!fir.array<250xf32>> {name = "v%[[VAL_10:.*]]"}
+ !CHECK: %[[VAL_11:.*]] = hlfir.designate %[[VAL_1]]#0{"temp"} : (!fir.ref<!fir.type<_QFdeclare_mapper_2Tmy_type2{my_type_var:!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>,temp:!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>,unmapped:!fir.array<250xf32>,arr:!fir.array<250xf32>}>>) -> !fir.ref<!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>
+ !CHECK: %[[VAL_12:.*]] = omp.map.info var_ptr(%[[VAL_11]] : !fir.ref<!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>, !fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>) map_clauses(exit_release_or_enter_alloc) capture(ByRef) -> !fir.ref<!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>> {name = "v%[[VAL_13:.*]]"}
+ !CHECK: %[[VAL_14:.*]] = omp.map.info var_ptr(%[[VAL_1]]#1 : !fir.ref<!fir.type<_QFdeclare_mapper_2Tmy_type2{my_type_var:!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>,temp:!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>,unmapped:!fir.array<250xf32>,arr:!fir.array<250xf32>}>>, !fir.type<_QFdeclare_mapper_2Tmy_type2{my_type_var:!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>,temp:!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>,unmapped:!fir.array<250xf32>,arr:!fir.array<250xf32>}>) map_clauses(tofrom) capture(ByRef) members(%[[VAL_9]], %[[VAL_12]] : [3], [1] : !fir.ref<!fir.array<250xf32>>, !fir.ref<!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>) -> !fir.ref<!fir.type<_QFdeclare_mapper_2Tmy_type2{my_type_var:!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>,temp:!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>,unmapped:!fir.array<250xf32>,arr:!fir.array<250xf32>}>> {name = "v", partial_map = true}
+ !CHECK: omp.declare_mapper_info map_entries(%[[VAL_14]] : !fir.ref<!fir.type<_QFdeclare_mapper_2Tmy_type2{my_type_var:!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>,temp:!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>,unmapped:!fir.array<250xf32>,arr:!fir.array<250xf32>}>>)
+ !CHECK: }
+ !$omp declare mapper (my_mapper : my_type2 :: v) map (v%arr) map (alloc : v%temp)
end subroutine declare_mapper_2
>From e52a29aa4d60a611d06494f31cbe5d1ca8739a4a Mon Sep 17 00:00:00 2001
From: Akash Banerjee <Akash.Banerjee at amd.com>
Date: Mon, 23 Dec 2024 20:29:50 +0000
Subject: [PATCH 07/23] Update MapInfoFinalization pass to look for MapInfoOps
inside DeclareMapperOps as well as inside the Module.
---
.../Optimizer/OpenMP/MapInfoFinalization.cpp | 5 +-
flang/test/Lower/OpenMP/declare-mapper.f90 | 82 ++++++++++---------
2 files changed, 46 insertions(+), 41 deletions(-)
diff --git a/flang/lib/Optimizer/OpenMP/MapInfoFinalization.cpp b/flang/lib/Optimizer/OpenMP/MapInfoFinalization.cpp
index 7bbb005ec46ab7..c877b9188b2a89 100644
--- a/flang/lib/Optimizer/OpenMP/MapInfoFinalization.cpp
+++ b/flang/lib/Optimizer/OpenMP/MapInfoFinalization.cpp
@@ -498,7 +498,10 @@ class MapInfoFinalizationPass
// ourselves to the possibility of race conditions while this pass
// undergoes frequent re-iteration for the near future. So we loop
// over function in the module and then map.info inside of those.
- getOperation()->walk([&](mlir::func::FuncOp func) {
+ getOperation()->walk([&](mlir::Operation *func) {
+ if (!(mlir::isa<mlir::func::FuncOp>(func) ||
+ mlir::isa<mlir::omp::DeclareMapperOp>(func)))
+ return;
// clear all local allocations we made for any boxes in any prior
// iterations from previous function scopes.
localBoxAllocas.clear();
diff --git a/flang/test/Lower/OpenMP/declare-mapper.f90 b/flang/test/Lower/OpenMP/declare-mapper.f90
index 4d9e201ea07dae..b303efbca58539 100644
--- a/flang/test/Lower/OpenMP/declare-mapper.f90
+++ b/flang/test/Lower/OpenMP/declare-mapper.f90
@@ -20,30 +20,32 @@ subroutine declare_mapper_1
end type
type(my_type2) :: t
real :: x, y(nvals)
- !CHECK-LABEL: omp.declare_mapper @_QQFdeclare_mapper_1my_type.default : !fir.type<_QFdeclare_mapper_1Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}> {
- !CHECK: ^bb0(%[[VAL_0:.*]]: !fir.ref<!fir.type<_QFdeclare_mapper_1Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>):
- !CHECK: %[[VAL_1:.*]]:2 = hlfir.declare %[[VAL_0]] {uniq_name = "_QFdeclare_mapper_1Evar"} : (!fir.ref<!fir.type<_QFdeclare_mapper_1Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>) -> (!fir.ref<!fir.type<_QFdeclare_mapper_1Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>, !fir.ref<!fir.type<_QFdeclare_mapper_1Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>)
- !CHECK: %[[VAL_2:.*]] = hlfir.designate %[[VAL_1]]#0{"values"} {fortran_attrs = #fir.var_attrs<allocatable>} : (!fir.ref<!fir.type<_QFdeclare_mapper_1Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>) -> !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>
- !CHECK: %[[VAL_3:.*]] = fir.load %[[VAL_2]] : !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>
- !CHECK: %[[VAL_4:.*]] = fir.box_addr %[[VAL_3]] : (!fir.box<!fir.heap<!fir.array<?xi32>>>) -> !fir.heap<!fir.array<?xi32>>
- !CHECK: %[[VAL_5:.*]] = arith.constant 0 : index
- !CHECK: %[[VAL_6:.*]]:3 = fir.box_dims %[[VAL_3]], %[[VAL_5]] : (!fir.box<!fir.heap<!fir.array<?xi32>>>, index) -> (index, index, index)
- !CHECK: %[[VAL_7:.*]] = arith.constant 0 : index
- !CHECK: %[[VAL_8:.*]] = arith.constant 1 : index
- !CHECK: %[[VAL_9:.*]] = arith.constant 1 : index
- !CHECK: %[[VAL_10:.*]] = arith.subi %[[VAL_9]], %[[VAL_6]]#0 : index
- !CHECK: %[[VAL_11:.*]] = hlfir.designate %[[VAL_1]]#0{"num_vals"} : (!fir.ref<!fir.type<_QFdeclare_mapper_1Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>) -> !fir.ref<i32>
- !CHECK: %[[VAL_12:.*]] = fir.load %[[VAL_11]] : !fir.ref<i32>
- !CHECK: %[[VAL_13:.*]] = fir.convert %[[VAL_12]] : (i32) -> i64
- !CHECK: %[[VAL_14:.*]] = fir.convert %[[VAL_13]] : (i64) -> index
- !CHECK: %[[VAL_15:.*]] = arith.subi %[[VAL_14]], %[[VAL_6]]#0 : index
- !CHECK: %[[VAL_16:.*]] = omp.map.bounds lower_bound(%[[VAL_10]] : index) upper_bound(%[[VAL_15]] : index) extent(%[[VAL_6]]#1 : index) stride(%[[VAL_8]] : index) start_idx(%[[VAL_6]]#0 : index)
- !CHECK: %[[VAL_17:.*]] = arith.constant 1 : index
- !CHECK: %[[VAL_18:.*]] = fir.coordinate_of %[[VAL_1]]#0, %[[VAL_17]] : (!fir.ref<!fir.type<_QFdeclare_mapper_1Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>, index) -> !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>
- !CHECK: %[[VAL_19:.*]] = omp.map.info var_ptr(%[[VAL_18]] : !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>, !fir.box<!fir.heap<!fir.array<?xi32>>>) map_clauses(tofrom) capture(ByRef) bounds(%[[VAL_16]]) -> !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>> {name = "var%[[VAL_20:.*]](1:var%[[VAL_21:.*]])"}
- !CHECK: %[[VAL_22:.*]] = omp.map.info var_ptr(%[[VAL_1]]#1 : !fir.ref<!fir.type<_QFdeclare_mapper_1Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>, !fir.type<_QFdeclare_mapper_1Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>) map_clauses(tofrom) capture(ByRef) members(%[[VAL_19]] : [1] : !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>) -> !fir.ref<!fir.type<_QFdeclare_mapper_1Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>> {name = "var"}
- !CHECK: omp.declare_mapper_info map_entries(%[[VAL_22]] : !fir.ref<!fir.type<_QFdeclare_mapper_1Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>)
- !CHECK: }
+ !CHECK-LABEL:omp.declare_mapper @_QQFdeclare_mapper_1my_type.default : !fir.type<_QFdeclare_mapper_1Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}> {
+ !CHECK: ^bb0(%[[VAL_0:.*]]: !fir.ref<!fir.type<_QFdeclare_mapper_1Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>):
+ !CHECK: %[[VAL_1:.*]]:2 = hlfir.declare %[[VAL_0]] {uniq_name = "_QFdeclare_mapper_1Evar"} : (!fir.ref<!fir.type<_QFdeclare_mapper_1Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>) -> (!fir.ref<!fir.type<_QFdeclare_mapper_1Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>, !fir.ref<!fir.type<_QFdeclare_mapper_1Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>)
+ !CHECK: %[[VAL_2:.*]] = hlfir.designate %[[VAL_1]]#0{"values"} {fortran_attrs = #fir.var_attrs<allocatable>} : (!fir.ref<!fir.type<_QFdeclare_mapper_1Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>) -> !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>
+ !CHECK: %[[VAL_3:.*]] = fir.load %[[VAL_2]] : !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>
+ !CHECK: %[[VAL_4:.*]] = fir.box_addr %[[VAL_3]] : (!fir.box<!fir.heap<!fir.array<?xi32>>>) -> !fir.heap<!fir.array<?xi32>>
+ !CHECK: %[[VAL_5:.*]] = arith.constant 0 : index
+ !CHECK: %[[VAL_6:.*]]:3 = fir.box_dims %[[VAL_3]], %[[VAL_5]] : (!fir.box<!fir.heap<!fir.array<?xi32>>>, index) -> (index, index, index)
+ !CHECK: %[[VAL_7:.*]] = arith.constant 0 : index
+ !CHECK: %[[VAL_8:.*]] = arith.constant 1 : index
+ !CHECK: %[[VAL_9:.*]] = arith.constant 1 : index
+ !CHECK: %[[VAL_10:.*]] = arith.subi %[[VAL_9]], %[[VAL_6]]#0 : index
+ !CHECK: %[[VAL_11:.*]] = hlfir.designate %[[VAL_1]]#0{"num_vals"} : (!fir.ref<!fir.type<_QFdeclare_mapper_1Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>) -> !fir.ref<i32>
+ !CHECK: %[[VAL_12:.*]] = fir.load %[[VAL_11]] : !fir.ref<i32>
+ !CHECK: %[[VAL_13:.*]] = fir.convert %[[VAL_12]] : (i32) -> i64
+ !CHECK: %[[VAL_14:.*]] = fir.convert %[[VAL_13]] : (i64) -> index
+ !CHECK: %[[VAL_15:.*]] = arith.subi %[[VAL_14]], %[[VAL_6]]#0 : index
+ !CHECK: %[[VAL_16:.*]] = omp.map.bounds lower_bound(%[[VAL_10]] : index) upper_bound(%[[VAL_15]] : index) extent(%[[VAL_6]]#1 : index) stride(%[[VAL_8]] : index) start_idx(%[[VAL_6]]#0 : index)
+ !CHECK: %[[VAL_17:.*]] = arith.constant 1 : index
+ !CHECK: %[[VAL_18:.*]] = fir.coordinate_of %[[VAL_1]]#0, %[[VAL_17]] : (!fir.ref<!fir.type<_QFdeclare_mapper_1Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>, index) -> !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>
+ !CHECK: %[[VAL_19:.*]] = fir.box_offset %[[VAL_18]] base_addr : (!fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>) -> !fir.llvm_ptr<!fir.ref<!fir.array<?xi32>>>
+ !CHECK: %[[VAL_20:.*]] = omp.map.info var_ptr(%[[VAL_18]] : !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>, !fir.array<?xi32>) var_ptr_ptr(%[[VAL_19]] : !fir.llvm_ptr<!fir.ref<!fir.array<?xi32>>>) map_clauses(tofrom) capture(ByRef) bounds(%[[VAL_16]]) -> !fir.llvm_ptr<!fir.ref<!fir.array<?xi32>>> {name = ""}
+ !CHECK: %[[VAL_21:.*]] = omp.map.info var_ptr(%[[VAL_18]] : !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>, !fir.box<!fir.heap<!fir.array<?xi32>>>) map_clauses(to) capture(ByRef) -> !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>> {name = "var%[[VAL_22:.*]](1:var%[[VAL_23:.*]])"}
+ !CHECK: %[[VAL_24:.*]] = omp.map.info var_ptr(%[[VAL_1]]#1 : !fir.ref<!fir.type<_QFdeclare_mapper_1Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>, !fir.type<_QFdeclare_mapper_1Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>) map_clauses(tofrom) capture(ByRef) members(%[[VAL_21]], %[[VAL_20]] : [1], [1, 0] : !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>, !fir.llvm_ptr<!fir.ref<!fir.array<?xi32>>>) -> !fir.ref<!fir.type<_QFdeclare_mapper_1Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>> {name = "var"}
+ !CHECK: omp.declare_mapper_info map_entries(%[[VAL_24]], %[[VAL_21]], %[[VAL_20]] : !fir.ref<!fir.type<_QFdeclare_mapper_1Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>, !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>, !fir.llvm_ptr<!fir.ref<!fir.array<?xi32>>>)
+ !CHECK: }
!$omp declare mapper (my_type :: var) map (var, var%values (1:var%num_vals))
end subroutine declare_mapper_1
@@ -63,21 +65,21 @@ subroutine declare_mapper_2
end type
type(my_type2) :: t
real :: x, y(nvals)
- !CHECK-LABEL: omp.declare_mapper @_QQFdeclare_mapper_2my_mapper : !fir.type<_QFdeclare_mapper_2Tmy_type2{my_type_var:!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>,temp:!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>,unmapped:!fir.array<250xf32>,arr:!fir.array<250xf32>}> {
- !CHECK: ^bb0(%[[VAL_0:.*]]: !fir.ref<!fir.type<_QFdeclare_mapper_2Tmy_type2{my_type_var:!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>,temp:!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>,unmapped:!fir.array<250xf32>,arr:!fir.array<250xf32>}>>):
- !CHECK: %[[VAL_1:.*]]:2 = hlfir.declare %[[VAL_0]] {uniq_name = "_QFdeclare_mapper_2Ev"} : (!fir.ref<!fir.type<_QFdeclare_mapper_2Tmy_type2{my_type_var:!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>,temp:!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>,unmapped:!fir.array<250xf32>,arr:!fir.array<250xf32>}>>) -> (!fir.ref<!fir.type<_QFdeclare_mapper_2Tmy_type2{my_type_var:!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>,temp:!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>,unmapped:!fir.array<250xf32>,arr:!fir.array<250xf32>}>>, !fir.ref<!fir.type<_QFdeclare_mapper_2Tmy_type2{my_type_var:!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>,temp:!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>,unmapped:!fir.array<250xf32>,arr:!fir.array<250xf32>}>>)
- !CHECK: %[[VAL_2:.*]] = arith.constant 250 : index
- !CHECK: %[[VAL_3:.*]] = fir.shape %[[VAL_2]] : (index) -> !fir.shape<1>
- !CHECK: %[[VAL_4:.*]] = hlfir.designate %[[VAL_1]]#0{"arr"} shape %[[VAL_3]] : (!fir.ref<!fir.type<_QFdeclare_mapper_2Tmy_type2{my_type_var:!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>,temp:!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>,unmapped:!fir.array<250xf32>,arr:!fir.array<250xf32>}>>, !fir.shape<1>) -> !fir.ref<!fir.array<250xf32>>
- !CHECK: %[[VAL_5:.*]] = arith.constant 1 : index
- !CHECK: %[[VAL_6:.*]] = arith.constant 0 : index
- !CHECK: %[[VAL_7:.*]] = arith.subi %[[VAL_2]], %[[VAL_5]] : index
- !CHECK: %[[VAL_8:.*]] = omp.map.bounds lower_bound(%[[VAL_6]] : index) upper_bound(%[[VAL_7]] : index) extent(%[[VAL_2]] : index) stride(%[[VAL_5]] : index) start_idx(%[[VAL_5]] : index)
- !CHECK: %[[VAL_9:.*]] = omp.map.info var_ptr(%[[VAL_4]] : !fir.ref<!fir.array<250xf32>>, !fir.array<250xf32>) map_clauses(tofrom) capture(ByRef) bounds(%[[VAL_8]]) -> !fir.ref<!fir.array<250xf32>> {name = "v%[[VAL_10:.*]]"}
- !CHECK: %[[VAL_11:.*]] = hlfir.designate %[[VAL_1]]#0{"temp"} : (!fir.ref<!fir.type<_QFdeclare_mapper_2Tmy_type2{my_type_var:!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>,temp:!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>,unmapped:!fir.array<250xf32>,arr:!fir.array<250xf32>}>>) -> !fir.ref<!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>
- !CHECK: %[[VAL_12:.*]] = omp.map.info var_ptr(%[[VAL_11]] : !fir.ref<!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>, !fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>) map_clauses(exit_release_or_enter_alloc) capture(ByRef) -> !fir.ref<!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>> {name = "v%[[VAL_13:.*]]"}
- !CHECK: %[[VAL_14:.*]] = omp.map.info var_ptr(%[[VAL_1]]#1 : !fir.ref<!fir.type<_QFdeclare_mapper_2Tmy_type2{my_type_var:!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>,temp:!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>,unmapped:!fir.array<250xf32>,arr:!fir.array<250xf32>}>>, !fir.type<_QFdeclare_mapper_2Tmy_type2{my_type_var:!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>,temp:!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>,unmapped:!fir.array<250xf32>,arr:!fir.array<250xf32>}>) map_clauses(tofrom) capture(ByRef) members(%[[VAL_9]], %[[VAL_12]] : [3], [1] : !fir.ref<!fir.array<250xf32>>, !fir.ref<!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>) -> !fir.ref<!fir.type<_QFdeclare_mapper_2Tmy_type2{my_type_var:!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>,temp:!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>,unmapped:!fir.array<250xf32>,arr:!fir.array<250xf32>}>> {name = "v", partial_map = true}
- !CHECK: omp.declare_mapper_info map_entries(%[[VAL_14]] : !fir.ref<!fir.type<_QFdeclare_mapper_2Tmy_type2{my_type_var:!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>,temp:!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>,unmapped:!fir.array<250xf32>,arr:!fir.array<250xf32>}>>)
- !CHECK: }
+ !CHECK-LABEL:omp.declare_mapper @_QQFdeclare_mapper_2my_mapper : !fir.type<_QFdeclare_mapper_2Tmy_type2{my_type_var:!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>,temp:!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>,unmapped:!fir.array<250xf32>,arr:!fir.array<250xf32>}> {
+ !CHECK: ^bb0(%[[VAL_0:.*]]: !fir.ref<!fir.type<_QFdeclare_mapper_2Tmy_type2{my_type_var:!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>,temp:!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>,unmapped:!fir.array<250xf32>,arr:!fir.array<250xf32>}>>):
+ !CHECK: %[[VAL_1:.*]]:2 = hlfir.declare %[[VAL_0]] {uniq_name = "_QFdeclare_mapper_2Ev"} : (!fir.ref<!fir.type<_QFdeclare_mapper_2Tmy_type2{my_type_var:!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>,temp:!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>,unmapped:!fir.array<250xf32>,arr:!fir.array<250xf32>}>>) -> (!fir.ref<!fir.type<_QFdeclare_mapper_2Tmy_type2{my_type_var:!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>,temp:!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>,unmapped:!fir.array<250xf32>,arr:!fir.array<250xf32>}>>, !fir.ref<!fir.type<_QFdeclare_mapper_2Tmy_type2{my_type_var:!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>,temp:!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>,unmapped:!fir.array<250xf32>,arr:!fir.array<250xf32>}>>)
+ !CHECK: %[[VAL_2:.*]] = arith.constant 250 : index
+ !CHECK: %[[VAL_3:.*]] = fir.shape %[[VAL_2]] : (index) -> !fir.shape<1>
+ !CHECK: %[[VAL_4:.*]] = hlfir.designate %[[VAL_1]]#0{"arr"} shape %[[VAL_3]] : (!fir.ref<!fir.type<_QFdeclare_mapper_2Tmy_type2{my_type_var:!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>,temp:!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>,unmapped:!fir.array<250xf32>,arr:!fir.array<250xf32>}>>, !fir.shape<1>) -> !fir.ref<!fir.array<250xf32>>
+ !CHECK: %[[VAL_5:.*]] = arith.constant 1 : index
+ !CHECK: %[[VAL_6:.*]] = arith.constant 0 : index
+ !CHECK: %[[VAL_7:.*]] = arith.subi %[[VAL_2]], %[[VAL_5]] : index
+ !CHECK: %[[VAL_8:.*]] = omp.map.bounds lower_bound(%[[VAL_6]] : index) upper_bound(%[[VAL_7]] : index) extent(%[[VAL_2]] : index) stride(%[[VAL_5]] : index) start_idx(%[[VAL_5]] : index)
+ !CHECK: %[[VAL_9:.*]] = omp.map.info var_ptr(%[[VAL_4]] : !fir.ref<!fir.array<250xf32>>, !fir.array<250xf32>) map_clauses(tofrom) capture(ByRef) bounds(%[[VAL_8]]) -> !fir.ref<!fir.array<250xf32>> {name = "v%[[VAL_10:.*]]"}
+ !CHECK: %[[VAL_11:.*]] = hlfir.designate %[[VAL_1]]#0{"temp"} : (!fir.ref<!fir.type<_QFdeclare_mapper_2Tmy_type2{my_type_var:!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>,temp:!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>,unmapped:!fir.array<250xf32>,arr:!fir.array<250xf32>}>>) -> !fir.ref<!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>
+ !CHECK: %[[VAL_12:.*]] = omp.map.info var_ptr(%[[VAL_11]] : !fir.ref<!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>, !fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>) map_clauses(exit_release_or_enter_alloc) capture(ByRef) -> !fir.ref<!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>> {name = "v%[[VAL_13:.*]]"}
+ !CHECK: %[[VAL_14:.*]] = omp.map.info var_ptr(%[[VAL_1]]#1 : !fir.ref<!fir.type<_QFdeclare_mapper_2Tmy_type2{my_type_var:!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>,temp:!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>,unmapped:!fir.array<250xf32>,arr:!fir.array<250xf32>}>>, !fir.type<_QFdeclare_mapper_2Tmy_type2{my_type_var:!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>,temp:!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>,unmapped:!fir.array<250xf32>,arr:!fir.array<250xf32>}>) map_clauses(tofrom) capture(ByRef) members(%[[VAL_9]], %[[VAL_12]] : [3], [1] : !fir.ref<!fir.array<250xf32>>, !fir.ref<!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>) -> !fir.ref<!fir.type<_QFdeclare_mapper_2Tmy_type2{my_type_var:!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>,temp:!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>,unmapped:!fir.array<250xf32>,arr:!fir.array<250xf32>}>> {name = "v", partial_map = true}
+ !CHECK: omp.declare_mapper_info map_entries(%[[VAL_14]], %[[VAL_9]], %[[VAL_12]] : !fir.ref<!fir.type<_QFdeclare_mapper_2Tmy_type2{my_type_var:!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>,temp:!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>,unmapped:!fir.array<250xf32>,arr:!fir.array<250xf32>}>>, !fir.ref<!fir.array<250xf32>>, !fir.ref<!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>)
+ !CHECK: }
!$omp declare mapper (my_mapper : my_type2 :: v) map (v%arr) map (alloc : v%temp)
end subroutine declare_mapper_2
>From 88daa53ace349de07dd945bfb806140b6cf8d300 Mon Sep 17 00:00:00 2001
From: Akash Banerjee <Akash.Banerjee at amd.com>
Date: Mon, 3 Feb 2025 14:20:37 +0000
Subject: [PATCH 08/23] Address reviewer comments.
---
flang/lib/Lower/OpenMP/OpenMP.cpp | 8 +++-----
1 file changed, 3 insertions(+), 5 deletions(-)
diff --git a/flang/lib/Lower/OpenMP/OpenMP.cpp b/flang/lib/Lower/OpenMP/OpenMP.cpp
index 67a429e78aed2f..e7e126d2647132 100644
--- a/flang/lib/Lower/OpenMP/OpenMP.cpp
+++ b/flang/lib/Lower/OpenMP/OpenMP.cpp
@@ -3143,8 +3143,9 @@ genOMP(lower::AbstractConverter &converter, lower::SymMap &symTable,
mapperNameStr, *varType.declTypeSpec->derivedTypeSpec().GetScope());
}
- // Save insert point just after the DeclMapperOp.
- mlir::OpBuilder::InsertPoint insPt = firOpBuilder.saveInsertionPoint();
+ // Save current insertion point before moving to the module scope to create
+ // the DeclareMapperOp
+ mlir::OpBuilder::InsertionGuard guard(firOpBuilder);
firOpBuilder.setInsertionPointToStart(converter.getModuleOp().getBody());
auto mlirType = converter.genType(varType.declTypeSpec->derivedTypeSpec());
@@ -3164,9 +3165,6 @@ genOMP(lower::AbstractConverter &converter, lower::SymMap &symTable,
ClauseProcessor cp(converter, semaCtx, clauses);
cp.processMap(loc, stmtCtx, clauseOps);
firOpBuilder.create<mlir::omp::DeclareMapperInfoOp>(loc, clauseOps.mapVars);
-
- // Restore the insert point to just after the DeclareMapperOp.
- firOpBuilder.restoreInsertionPoint(insPt);
}
static void
>From ea50dad896be964c543ac2d00e5566f3476cfd21 Mon Sep 17 00:00:00 2001
From: Akash Banerjee <Akash.Banerjee at amd.com>
Date: Fri, 7 Feb 2025 17:25:42 +0000
Subject: [PATCH 09/23] Addressed reviewer comments.
---
flang/lib/Lower/OpenMP/OpenMP.cpp | 1 -
.../Optimizer/OpenMP/MapInfoFinalization.cpp | 3 +-
flang/test/Lower/OpenMP/declare-mapper.f90 | 30 +++++++++----------
3 files changed, 16 insertions(+), 18 deletions(-)
diff --git a/flang/lib/Lower/OpenMP/OpenMP.cpp b/flang/lib/Lower/OpenMP/OpenMP.cpp
index e7e126d2647132..7dcb692650825f 100644
--- a/flang/lib/Lower/OpenMP/OpenMP.cpp
+++ b/flang/lib/Lower/OpenMP/OpenMP.cpp
@@ -3151,7 +3151,6 @@ genOMP(lower::AbstractConverter &converter, lower::SymMap &symTable,
auto mlirType = converter.genType(varType.declTypeSpec->derivedTypeSpec());
auto declMapperOp = firOpBuilder.create<mlir::omp::DeclareMapperOp>(
loc, mapperNameStr, mlirType);
- converter.getMLIRSymbolTable()->insert(declMapperOp);
auto ®ion = declMapperOp.getRegion();
firOpBuilder.createBlock(®ion);
auto varVal = region.addArgument(firOpBuilder.getRefType(mlirType), loc);
diff --git a/flang/lib/Optimizer/OpenMP/MapInfoFinalization.cpp b/flang/lib/Optimizer/OpenMP/MapInfoFinalization.cpp
index c877b9188b2a89..e7c1d1d9d560f8 100644
--- a/flang/lib/Optimizer/OpenMP/MapInfoFinalization.cpp
+++ b/flang/lib/Optimizer/OpenMP/MapInfoFinalization.cpp
@@ -499,8 +499,7 @@ class MapInfoFinalizationPass
// undergoes frequent re-iteration for the near future. So we loop
// over function in the module and then map.info inside of those.
getOperation()->walk([&](mlir::Operation *func) {
- if (!(mlir::isa<mlir::func::FuncOp>(func) ||
- mlir::isa<mlir::omp::DeclareMapperOp>(func)))
+ if (!mlir::isa<mlir::func::FuncOp, mlir::omp::DeclareMapperOp>(func))
return;
// clear all local allocations we made for any boxes in any prior
// iterations from previous function scopes.
diff --git a/flang/test/Lower/OpenMP/declare-mapper.f90 b/flang/test/Lower/OpenMP/declare-mapper.f90
index b303efbca58539..3c78f86935be64 100644
--- a/flang/test/Lower/OpenMP/declare-mapper.f90
+++ b/flang/test/Lower/OpenMP/declare-mapper.f90
@@ -20,10 +20,10 @@ subroutine declare_mapper_1
end type
type(my_type2) :: t
real :: x, y(nvals)
- !CHECK-LABEL:omp.declare_mapper @_QQFdeclare_mapper_1my_type.default : !fir.type<_QFdeclare_mapper_1Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}> {
- !CHECK: ^bb0(%[[VAL_0:.*]]: !fir.ref<!fir.type<_QFdeclare_mapper_1Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>):
- !CHECK: %[[VAL_1:.*]]:2 = hlfir.declare %[[VAL_0]] {uniq_name = "_QFdeclare_mapper_1Evar"} : (!fir.ref<!fir.type<_QFdeclare_mapper_1Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>) -> (!fir.ref<!fir.type<_QFdeclare_mapper_1Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>, !fir.ref<!fir.type<_QFdeclare_mapper_1Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>)
- !CHECK: %[[VAL_2:.*]] = hlfir.designate %[[VAL_1]]#0{"values"} {fortran_attrs = #fir.var_attrs<allocatable>} : (!fir.ref<!fir.type<_QFdeclare_mapper_1Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>) -> !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>
+ !CHECK:omp.declare_mapper @[[MY_TYPE_MAPPER:_QQFdeclare_mapper_1my_type\.default]] : [[MY_TYPE:!fir\.type<_QFdeclare_mapper_1Tmy_type\{num_vals:i32,values:!fir\.box<!fir\.heap<!fir\.array<\?xi32>>>\}>]] {
+ !CHECK: ^bb0(%[[VAL_0:.*]]: !fir.ref<[[MY_TYPE]]>):
+ !CHECK: %[[VAL_1:.*]]:2 = hlfir.declare %[[VAL_0]] {uniq_name = "_QFdeclare_mapper_1Evar"} : (!fir.ref<[[MY_TYPE]]>) -> (!fir.ref<[[MY_TYPE]]>, !fir.ref<[[MY_TYPE]]>)
+ !CHECK: %[[VAL_2:.*]] = hlfir.designate %[[VAL_1]]#0{"values"} {fortran_attrs = #fir.var_attrs<allocatable>} : (!fir.ref<[[MY_TYPE]]>) -> !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>
!CHECK: %[[VAL_3:.*]] = fir.load %[[VAL_2]] : !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>
!CHECK: %[[VAL_4:.*]] = fir.box_addr %[[VAL_3]] : (!fir.box<!fir.heap<!fir.array<?xi32>>>) -> !fir.heap<!fir.array<?xi32>>
!CHECK: %[[VAL_5:.*]] = arith.constant 0 : index
@@ -32,19 +32,19 @@ subroutine declare_mapper_1
!CHECK: %[[VAL_8:.*]] = arith.constant 1 : index
!CHECK: %[[VAL_9:.*]] = arith.constant 1 : index
!CHECK: %[[VAL_10:.*]] = arith.subi %[[VAL_9]], %[[VAL_6]]#0 : index
- !CHECK: %[[VAL_11:.*]] = hlfir.designate %[[VAL_1]]#0{"num_vals"} : (!fir.ref<!fir.type<_QFdeclare_mapper_1Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>) -> !fir.ref<i32>
+ !CHECK: %[[VAL_11:.*]] = hlfir.designate %[[VAL_1]]#0{"num_vals"} : (!fir.ref<[[MY_TYPE]]>) -> !fir.ref<i32>
!CHECK: %[[VAL_12:.*]] = fir.load %[[VAL_11]] : !fir.ref<i32>
!CHECK: %[[VAL_13:.*]] = fir.convert %[[VAL_12]] : (i32) -> i64
!CHECK: %[[VAL_14:.*]] = fir.convert %[[VAL_13]] : (i64) -> index
!CHECK: %[[VAL_15:.*]] = arith.subi %[[VAL_14]], %[[VAL_6]]#0 : index
!CHECK: %[[VAL_16:.*]] = omp.map.bounds lower_bound(%[[VAL_10]] : index) upper_bound(%[[VAL_15]] : index) extent(%[[VAL_6]]#1 : index) stride(%[[VAL_8]] : index) start_idx(%[[VAL_6]]#0 : index)
!CHECK: %[[VAL_17:.*]] = arith.constant 1 : index
- !CHECK: %[[VAL_18:.*]] = fir.coordinate_of %[[VAL_1]]#0, %[[VAL_17]] : (!fir.ref<!fir.type<_QFdeclare_mapper_1Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>, index) -> !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>
+ !CHECK: %[[VAL_18:.*]] = fir.coordinate_of %[[VAL_1]]#0, %[[VAL_17]] : (!fir.ref<[[MY_TYPE]]>, index) -> !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>
!CHECK: %[[VAL_19:.*]] = fir.box_offset %[[VAL_18]] base_addr : (!fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>) -> !fir.llvm_ptr<!fir.ref<!fir.array<?xi32>>>
!CHECK: %[[VAL_20:.*]] = omp.map.info var_ptr(%[[VAL_18]] : !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>, !fir.array<?xi32>) var_ptr_ptr(%[[VAL_19]] : !fir.llvm_ptr<!fir.ref<!fir.array<?xi32>>>) map_clauses(tofrom) capture(ByRef) bounds(%[[VAL_16]]) -> !fir.llvm_ptr<!fir.ref<!fir.array<?xi32>>> {name = ""}
!CHECK: %[[VAL_21:.*]] = omp.map.info var_ptr(%[[VAL_18]] : !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>, !fir.box<!fir.heap<!fir.array<?xi32>>>) map_clauses(to) capture(ByRef) -> !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>> {name = "var%[[VAL_22:.*]](1:var%[[VAL_23:.*]])"}
- !CHECK: %[[VAL_24:.*]] = omp.map.info var_ptr(%[[VAL_1]]#1 : !fir.ref<!fir.type<_QFdeclare_mapper_1Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>, !fir.type<_QFdeclare_mapper_1Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>) map_clauses(tofrom) capture(ByRef) members(%[[VAL_21]], %[[VAL_20]] : [1], [1, 0] : !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>, !fir.llvm_ptr<!fir.ref<!fir.array<?xi32>>>) -> !fir.ref<!fir.type<_QFdeclare_mapper_1Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>> {name = "var"}
- !CHECK: omp.declare_mapper_info map_entries(%[[VAL_24]], %[[VAL_21]], %[[VAL_20]] : !fir.ref<!fir.type<_QFdeclare_mapper_1Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>, !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>, !fir.llvm_ptr<!fir.ref<!fir.array<?xi32>>>)
+ !CHECK: %[[VAL_24:.*]] = omp.map.info var_ptr(%[[VAL_1]]#1 : !fir.ref<[[MY_TYPE]]>, [[MY_TYPE]]) map_clauses(tofrom) capture(ByRef) members(%[[VAL_21]], %[[VAL_20]] : [1], [1, 0] : !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>, !fir.llvm_ptr<!fir.ref<!fir.array<?xi32>>>) -> !fir.ref<[[MY_TYPE]]> {name = "var"}
+ !CHECK: omp.declare_mapper.info map_entries(%[[VAL_24]], %[[VAL_21]], %[[VAL_20]] : !fir.ref<[[MY_TYPE]]>, !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>, !fir.llvm_ptr<!fir.ref<!fir.array<?xi32>>>)
!CHECK: }
!$omp declare mapper (my_type :: var) map (var, var%values (1:var%num_vals))
end subroutine declare_mapper_1
@@ -65,21 +65,21 @@ subroutine declare_mapper_2
end type
type(my_type2) :: t
real :: x, y(nvals)
- !CHECK-LABEL:omp.declare_mapper @_QQFdeclare_mapper_2my_mapper : !fir.type<_QFdeclare_mapper_2Tmy_type2{my_type_var:!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>,temp:!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>,unmapped:!fir.array<250xf32>,arr:!fir.array<250xf32>}> {
- !CHECK: ^bb0(%[[VAL_0:.*]]: !fir.ref<!fir.type<_QFdeclare_mapper_2Tmy_type2{my_type_var:!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>,temp:!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>,unmapped:!fir.array<250xf32>,arr:!fir.array<250xf32>}>>):
- !CHECK: %[[VAL_1:.*]]:2 = hlfir.declare %[[VAL_0]] {uniq_name = "_QFdeclare_mapper_2Ev"} : (!fir.ref<!fir.type<_QFdeclare_mapper_2Tmy_type2{my_type_var:!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>,temp:!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>,unmapped:!fir.array<250xf32>,arr:!fir.array<250xf32>}>>) -> (!fir.ref<!fir.type<_QFdeclare_mapper_2Tmy_type2{my_type_var:!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>,temp:!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>,unmapped:!fir.array<250xf32>,arr:!fir.array<250xf32>}>>, !fir.ref<!fir.type<_QFdeclare_mapper_2Tmy_type2{my_type_var:!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>,temp:!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>,unmapped:!fir.array<250xf32>,arr:!fir.array<250xf32>}>>)
+ !CHECK:omp.declare_mapper @[[MY_TYPE_MAPPER:_QQFdeclare_mapper_2my_mapper]] : [[MY_TYPE:!fir\.type<_QFdeclare_mapper_2Tmy_type2\{my_type_var:!fir\.type<_QFdeclare_mapper_2Tmy_type\{num_vals:i32,values:!fir\.box<!fir\.heap<!fir\.array<\?xi32>>>\}>,temp:!fir\.type<_QFdeclare_mapper_2Tmy_type\{num_vals:i32,values:!fir\.box<!fir\.heap<!fir\.array<\?xi32>>>\}>,unmapped:!fir\.array<250xf32>,arr:!fir\.array<250xf32>\}>]] {
+ !CHECK: ^bb0(%[[VAL_0:.*]]: !fir.ref<[[MY_TYPE]]>):
+ !CHECK: %[[VAL_1:.*]]:2 = hlfir.declare %[[VAL_0]] {uniq_name = "_QFdeclare_mapper_2Ev"} : (!fir.ref<[[MY_TYPE]]>) -> (!fir.ref<[[MY_TYPE]]>, !fir.ref<[[MY_TYPE]]>)
!CHECK: %[[VAL_2:.*]] = arith.constant 250 : index
!CHECK: %[[VAL_3:.*]] = fir.shape %[[VAL_2]] : (index) -> !fir.shape<1>
- !CHECK: %[[VAL_4:.*]] = hlfir.designate %[[VAL_1]]#0{"arr"} shape %[[VAL_3]] : (!fir.ref<!fir.type<_QFdeclare_mapper_2Tmy_type2{my_type_var:!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>,temp:!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>,unmapped:!fir.array<250xf32>,arr:!fir.array<250xf32>}>>, !fir.shape<1>) -> !fir.ref<!fir.array<250xf32>>
+ !CHECK: %[[VAL_4:.*]] = hlfir.designate %[[VAL_1]]#0{"arr"} shape %[[VAL_3]] : (!fir.ref<[[MY_TYPE]]>, !fir.shape<1>) -> !fir.ref<!fir.array<250xf32>>
!CHECK: %[[VAL_5:.*]] = arith.constant 1 : index
!CHECK: %[[VAL_6:.*]] = arith.constant 0 : index
!CHECK: %[[VAL_7:.*]] = arith.subi %[[VAL_2]], %[[VAL_5]] : index
!CHECK: %[[VAL_8:.*]] = omp.map.bounds lower_bound(%[[VAL_6]] : index) upper_bound(%[[VAL_7]] : index) extent(%[[VAL_2]] : index) stride(%[[VAL_5]] : index) start_idx(%[[VAL_5]] : index)
!CHECK: %[[VAL_9:.*]] = omp.map.info var_ptr(%[[VAL_4]] : !fir.ref<!fir.array<250xf32>>, !fir.array<250xf32>) map_clauses(tofrom) capture(ByRef) bounds(%[[VAL_8]]) -> !fir.ref<!fir.array<250xf32>> {name = "v%[[VAL_10:.*]]"}
- !CHECK: %[[VAL_11:.*]] = hlfir.designate %[[VAL_1]]#0{"temp"} : (!fir.ref<!fir.type<_QFdeclare_mapper_2Tmy_type2{my_type_var:!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>,temp:!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>,unmapped:!fir.array<250xf32>,arr:!fir.array<250xf32>}>>) -> !fir.ref<!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>
+ !CHECK: %[[VAL_11:.*]] = hlfir.designate %[[VAL_1]]#0{"temp"} : (!fir.ref<[[MY_TYPE]]>) -> !fir.ref<!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>
!CHECK: %[[VAL_12:.*]] = omp.map.info var_ptr(%[[VAL_11]] : !fir.ref<!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>, !fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>) map_clauses(exit_release_or_enter_alloc) capture(ByRef) -> !fir.ref<!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>> {name = "v%[[VAL_13:.*]]"}
- !CHECK: %[[VAL_14:.*]] = omp.map.info var_ptr(%[[VAL_1]]#1 : !fir.ref<!fir.type<_QFdeclare_mapper_2Tmy_type2{my_type_var:!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>,temp:!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>,unmapped:!fir.array<250xf32>,arr:!fir.array<250xf32>}>>, !fir.type<_QFdeclare_mapper_2Tmy_type2{my_type_var:!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>,temp:!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>,unmapped:!fir.array<250xf32>,arr:!fir.array<250xf32>}>) map_clauses(tofrom) capture(ByRef) members(%[[VAL_9]], %[[VAL_12]] : [3], [1] : !fir.ref<!fir.array<250xf32>>, !fir.ref<!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>) -> !fir.ref<!fir.type<_QFdeclare_mapper_2Tmy_type2{my_type_var:!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>,temp:!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>,unmapped:!fir.array<250xf32>,arr:!fir.array<250xf32>}>> {name = "v", partial_map = true}
- !CHECK: omp.declare_mapper_info map_entries(%[[VAL_14]], %[[VAL_9]], %[[VAL_12]] : !fir.ref<!fir.type<_QFdeclare_mapper_2Tmy_type2{my_type_var:!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>,temp:!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>,unmapped:!fir.array<250xf32>,arr:!fir.array<250xf32>}>>, !fir.ref<!fir.array<250xf32>>, !fir.ref<!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>)
+ !CHECK: %[[VAL_14:.*]] = omp.map.info var_ptr(%[[VAL_1]]#1 : !fir.ref<[[MY_TYPE]]>, [[MY_TYPE]]) map_clauses(tofrom) capture(ByRef) members(%[[VAL_9]], %[[VAL_12]] : [3], [1] : !fir.ref<!fir.array<250xf32>>, !fir.ref<!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>) -> !fir.ref<[[MY_TYPE]]> {name = "v", partial_map = true}
+ !CHECK: omp.declare_mapper.info map_entries(%[[VAL_14]], %[[VAL_9]], %[[VAL_12]] : !fir.ref<[[MY_TYPE]]>, !fir.ref<!fir.array<250xf32>>, !fir.ref<!fir.type<_QFdeclare_mapper_2Tmy_type{num_vals:i32,values:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>)
!CHECK: }
!$omp declare mapper (my_mapper : my_type2 :: v) map (v%arr) map (alloc : v%temp)
end subroutine declare_mapper_2
>From 5d7a40246557b2510115104916fb9b553d9b322a Mon Sep 17 00:00:00 2001
From: Akash Banerjee <Akash.Banerjee at amd.com>
Date: Mon, 10 Feb 2025 17:02:08 +0000
Subject: [PATCH 10/23] Fix build issue.
---
flang/lib/Lower/OpenMP/OpenMP.cpp | 2 +-
flang/test/Lower/OpenMP/declare-mapper.f90 | 2 +-
2 files changed, 2 insertions(+), 2 deletions(-)
diff --git a/flang/lib/Lower/OpenMP/OpenMP.cpp b/flang/lib/Lower/OpenMP/OpenMP.cpp
index 7dcb692650825f..e0d23fc53eeca9 100644
--- a/flang/lib/Lower/OpenMP/OpenMP.cpp
+++ b/flang/lib/Lower/OpenMP/OpenMP.cpp
@@ -3123,7 +3123,7 @@ genOMP(lower::AbstractConverter &converter, lower::SymMap &symTable,
fir::FirOpBuilder &firOpBuilder = converter.getFirOpBuilder();
lower::StatementContext stmtCtx;
const auto &spec =
- std::get<parser::OmpDeclareMapperSpecifier>(declareMapperConstruct.t);
+ std::get<parser::OmpMapperSpecifier>(declareMapperConstruct.t);
const auto &mapperName{std::get<std::optional<parser::Name>>(spec.t)};
const auto &varType{std::get<parser::TypeSpec>(spec.t)};
const auto &varName{std::get<parser::Name>(spec.t)};
diff --git a/flang/test/Lower/OpenMP/declare-mapper.f90 b/flang/test/Lower/OpenMP/declare-mapper.f90
index 3c78f86935be64..af6ed28b570257 100644
--- a/flang/test/Lower/OpenMP/declare-mapper.f90
+++ b/flang/test/Lower/OpenMP/declare-mapper.f90
@@ -41,7 +41,7 @@ subroutine declare_mapper_1
!CHECK: %[[VAL_17:.*]] = arith.constant 1 : index
!CHECK: %[[VAL_18:.*]] = fir.coordinate_of %[[VAL_1]]#0, %[[VAL_17]] : (!fir.ref<[[MY_TYPE]]>, index) -> !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>
!CHECK: %[[VAL_19:.*]] = fir.box_offset %[[VAL_18]] base_addr : (!fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>) -> !fir.llvm_ptr<!fir.ref<!fir.array<?xi32>>>
- !CHECK: %[[VAL_20:.*]] = omp.map.info var_ptr(%[[VAL_18]] : !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>, !fir.array<?xi32>) var_ptr_ptr(%[[VAL_19]] : !fir.llvm_ptr<!fir.ref<!fir.array<?xi32>>>) map_clauses(tofrom) capture(ByRef) bounds(%[[VAL_16]]) -> !fir.llvm_ptr<!fir.ref<!fir.array<?xi32>>> {name = ""}
+ !CHECK: %[[VAL_20:.*]] = omp.map.info var_ptr(%[[VAL_18]] : !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>, i32) var_ptr_ptr(%[[VAL_19]] : !fir.llvm_ptr<!fir.ref<!fir.array<?xi32>>>) map_clauses(tofrom) capture(ByRef) bounds(%[[VAL_16]]) -> !fir.llvm_ptr<!fir.ref<!fir.array<?xi32>>> {name = ""}
!CHECK: %[[VAL_21:.*]] = omp.map.info var_ptr(%[[VAL_18]] : !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>, !fir.box<!fir.heap<!fir.array<?xi32>>>) map_clauses(to) capture(ByRef) -> !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>> {name = "var%[[VAL_22:.*]](1:var%[[VAL_23:.*]])"}
!CHECK: %[[VAL_24:.*]] = omp.map.info var_ptr(%[[VAL_1]]#1 : !fir.ref<[[MY_TYPE]]>, [[MY_TYPE]]) map_clauses(tofrom) capture(ByRef) members(%[[VAL_21]], %[[VAL_20]] : [1], [1, 0] : !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>, !fir.llvm_ptr<!fir.ref<!fir.array<?xi32>>>) -> !fir.ref<[[MY_TYPE]]> {name = "var"}
!CHECK: omp.declare_mapper.info map_entries(%[[VAL_24]], %[[VAL_21]], %[[VAL_20]] : !fir.ref<[[MY_TYPE]]>, !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>, !fir.llvm_ptr<!fir.ref<!fir.array<?xi32>>>)
>From 95e8e2b18d9e5b35ec2ad13bd7ee58a1bd1b996d Mon Sep 17 00:00:00 2001
From: Akash Banerjee <Akash.Banerjee at amd.com>
Date: Mon, 23 Dec 2024 20:53:47 +0000
Subject: [PATCH 11/23] Add mapper field to mapInfoOp.
---
flang/lib/Lower/OpenMP/Utils.cpp | 3 ++-
flang/lib/Lower/OpenMP/Utils.h | 3 ++-
flang/lib/Optimizer/OpenMP/MapInfoFinalization.cpp | 5 ++++-
flang/lib/Optimizer/OpenMP/MapsForPrivatizedSymbols.cpp | 1 +
mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td | 2 ++
mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp | 2 +-
mlir/test/Dialect/OpenMP/ops.mlir | 4 ++--
7 files changed, 14 insertions(+), 6 deletions(-)
diff --git a/flang/lib/Lower/OpenMP/Utils.cpp b/flang/lib/Lower/OpenMP/Utils.cpp
index 35722fa7d1b120..fa1975dac789b1 100644
--- a/flang/lib/Lower/OpenMP/Utils.cpp
+++ b/flang/lib/Lower/OpenMP/Utils.cpp
@@ -125,7 +125,7 @@ createMapInfoOp(fir::FirOpBuilder &builder, mlir::Location loc,
llvm::ArrayRef<mlir::Value> members,
mlir::ArrayAttr membersIndex, uint64_t mapType,
mlir::omp::VariableCaptureKind mapCaptureType, mlir::Type retTy,
- bool partialMap) {
+ bool partialMap, mlir::FlatSymbolRefAttr mapperId) {
if (auto boxTy = llvm::dyn_cast<fir::BaseBoxType>(baseAddr.getType())) {
baseAddr = builder.create<fir::BoxAddrOp>(loc, baseAddr);
retTy = baseAddr.getType();
@@ -144,6 +144,7 @@ createMapInfoOp(fir::FirOpBuilder &builder, mlir::Location loc,
mlir::omp::MapInfoOp op = builder.create<mlir::omp::MapInfoOp>(
loc, retTy, baseAddr, varType, varPtrPtr, members, membersIndex, bounds,
builder.getIntegerAttr(builder.getIntegerType(64, false), mapType),
+ mapperId,
builder.getAttr<mlir::omp::VariableCaptureKindAttr>(mapCaptureType),
builder.getStringAttr(name), builder.getBoolAttr(partialMap));
return op;
diff --git a/flang/lib/Lower/OpenMP/Utils.h b/flang/lib/Lower/OpenMP/Utils.h
index f2e378443e5f29..3943eb633b04e3 100644
--- a/flang/lib/Lower/OpenMP/Utils.h
+++ b/flang/lib/Lower/OpenMP/Utils.h
@@ -116,7 +116,8 @@ createMapInfoOp(fir::FirOpBuilder &builder, mlir::Location loc,
llvm::ArrayRef<mlir::Value> members,
mlir::ArrayAttr membersIndex, uint64_t mapType,
mlir::omp::VariableCaptureKind mapCaptureType, mlir::Type retTy,
- bool partialMap = false);
+ bool partialMap = false,
+ mlir::FlatSymbolRefAttr mapperId = mlir::FlatSymbolRefAttr());
void insertChildMapInfoIntoParent(
Fortran::lower::AbstractConverter &converter,
diff --git a/flang/lib/Optimizer/OpenMP/MapInfoFinalization.cpp b/flang/lib/Optimizer/OpenMP/MapInfoFinalization.cpp
index e7c1d1d9d560f8..beea7543e54b32 100644
--- a/flang/lib/Optimizer/OpenMP/MapInfoFinalization.cpp
+++ b/flang/lib/Optimizer/OpenMP/MapInfoFinalization.cpp
@@ -184,6 +184,7 @@ class MapInfoFinalizationPass
/*members=*/mlir::SmallVector<mlir::Value>{},
/*membersIndex=*/mlir::ArrayAttr{}, bounds,
builder.getIntegerAttr(builder.getIntegerType(64, false), mapType),
+ /*mapperId*/ mlir::FlatSymbolRefAttr(),
builder.getAttr<mlir::omp::VariableCaptureKindAttr>(
mlir::omp::VariableCaptureKind::ByRef),
/*name=*/builder.getStringAttr(""),
@@ -329,7 +330,8 @@ class MapInfoFinalizationPass
builder.getIntegerAttr(
builder.getIntegerType(64, false),
getDescriptorMapType(op.getMapType().value_or(0), target)),
- op.getMapCaptureTypeAttr(), op.getNameAttr(),
+ /*mapperId*/ mlir::FlatSymbolRefAttr(), op.getMapCaptureTypeAttr(),
+ op.getNameAttr(),
/*partial_map=*/builder.getBoolAttr(false));
op.replaceAllUsesWith(newDescParentMapOp.getResult());
op->erase();
@@ -623,6 +625,7 @@ class MapInfoFinalizationPass
/*members=*/mlir::ValueRange{},
/*members_index=*/mlir::ArrayAttr{},
/*bounds=*/bounds, op.getMapTypeAttr(),
+ /*mapperId*/ mlir::FlatSymbolRefAttr(),
builder.getAttr<mlir::omp::VariableCaptureKindAttr>(
mlir::omp::VariableCaptureKind::ByRef),
builder.getStringAttr(op.getNameAttr().strref() + "." +
diff --git a/flang/lib/Optimizer/OpenMP/MapsForPrivatizedSymbols.cpp b/flang/lib/Optimizer/OpenMP/MapsForPrivatizedSymbols.cpp
index 963ae863c1fc5c..97ea463a3c495d 100644
--- a/flang/lib/Optimizer/OpenMP/MapsForPrivatizedSymbols.cpp
+++ b/flang/lib/Optimizer/OpenMP/MapsForPrivatizedSymbols.cpp
@@ -91,6 +91,7 @@ class MapsForPrivatizedSymbolsPass
/*bounds=*/ValueRange{},
builder.getIntegerAttr(builder.getIntegerType(64, /*isSigned=*/false),
mapTypeTo),
+ /*mapperId*/ mlir::FlatSymbolRefAttr(),
builder.getAttr<omp::VariableCaptureKindAttr>(
omp::VariableCaptureKind::ByRef),
StringAttr(), builder.getBoolAttr(false));
diff --git a/mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td b/mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td
index e1eef30c0dd060..10c4a5e37a4f6b 100644
--- a/mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td
+++ b/mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td
@@ -1023,6 +1023,7 @@ def MapInfoOp : OpenMP_Op<"map.info", [AttrSizedOperandSegments]> {
OptionalAttr<IndexListArrayAttr>:$members_index,
Variadic<OpenMP_MapBoundsType>:$bounds, /* rank-0 to rank-{n-1} */
OptionalAttr<UI64Attr>:$map_type,
+ OptionalAttr<FlatSymbolRefAttr>:$mapper_id,
OptionalAttr<VariableCaptureKindAttr>:$map_capture_type,
OptionalAttr<StrAttr>:$name,
DefaultValuedAttr<BoolAttr, "false">:$partial_map);
@@ -1087,6 +1088,7 @@ def MapInfoOp : OpenMP_Op<"map.info", [AttrSizedOperandSegments]> {
`var_ptr` `(` $var_ptr `:` type($var_ptr) `,` $var_type `)`
oilist(
`var_ptr_ptr` `(` $var_ptr_ptr `:` type($var_ptr_ptr) `)`
+ | `mapper` `(` $mapper_id `)`
| `map_clauses` `(` custom<MapClause>($map_type) `)`
| `capture` `(` custom<CaptureType>($map_capture_type) `)`
| `members` `(` $members `:` custom<MembersIndex>($members_index) `:` type($members) `)`
diff --git a/mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp b/mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp
index 283d9e269f8b4c..b02da78b1fbf81 100644
--- a/mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp
+++ b/mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp
@@ -1631,7 +1631,7 @@ static LogicalResult verifyMapClause(Operation *op, OperandRange mapVars) {
to ? updateToVars.insert(updateVar) : updateFromVars.insert(updateVar);
}
- } else {
+ } else if (!isa<DeclareMapperInfoOp>(op)) {
emitError(op->getLoc(), "map argument is not a map entry operation");
}
}
diff --git a/mlir/test/Dialect/OpenMP/ops.mlir b/mlir/test/Dialect/OpenMP/ops.mlir
index f00d3d34266319..e318afbebbf0c6 100644
--- a/mlir/test/Dialect/OpenMP/ops.mlir
+++ b/mlir/test/Dialect/OpenMP/ops.mlir
@@ -2546,13 +2546,13 @@ func.func @omp_targets_with_map_bounds(%arg0: !llvm.ptr, %arg1: !llvm.ptr) -> ()
// CHECK: %[[C_12:.*]] = llvm.mlir.constant(2 : index) : i64
// CHECK: %[[C_13:.*]] = llvm.mlir.constant(2 : index) : i64
// CHECK: %[[BOUNDS1:.*]] = omp.map.bounds lower_bound(%[[C_11]] : i64) upper_bound(%[[C_10]] : i64) stride(%[[C_12]] : i64) start_idx(%[[C_13]] : i64)
- // CHECK: %[[MAP1:.*]] = omp.map.info var_ptr(%[[ARG1]] : !llvm.ptr, !llvm.array<10 x i32>) map_clauses(exit_release_or_enter_alloc) capture(ByCopy) bounds(%[[BOUNDS1]]) -> !llvm.ptr {name = ""}
+ // CHECK: %[[MAP1:.*]] = omp.map.info var_ptr(%[[ARG1]] : !llvm.ptr, !llvm.array<10 x i32>) mapper(@my_mapper) map_clauses(exit_release_or_enter_alloc) capture(ByCopy) bounds(%[[BOUNDS1]]) -> !llvm.ptr {name = ""}
%6 = llvm.mlir.constant(9 : index) : i64
%7 = llvm.mlir.constant(1 : index) : i64
%8 = llvm.mlir.constant(2 : index) : i64
%9 = llvm.mlir.constant(2 : index) : i64
%10 = omp.map.bounds lower_bound(%7 : i64) upper_bound(%6 : i64) stride(%8 : i64) start_idx(%9 : i64)
- %mapv2 = omp.map.info var_ptr(%arg1 : !llvm.ptr, !llvm.array<10 x i32>) map_clauses(exit_release_or_enter_alloc) capture(ByCopy) bounds(%10) -> !llvm.ptr {name = ""}
+ %mapv2 = omp.map.info var_ptr(%arg1 : !llvm.ptr, !llvm.array<10 x i32>) mapper(@my_mapper) map_clauses(exit_release_or_enter_alloc) capture(ByCopy) bounds(%10) -> !llvm.ptr {name = ""}
// CHECK: omp.target map_entries(%[[MAP0]] -> {{.*}}, %[[MAP1]] -> {{.*}} : !llvm.ptr, !llvm.ptr)
omp.target map_entries(%mapv1 -> %arg2, %mapv2 -> %arg3 : !llvm.ptr, !llvm.ptr) {
>From e905951735750df8ad8ad711bdc77c08767171a1 Mon Sep 17 00:00:00 2001
From: Akash Banerjee <Akash.Banerjee at amd.com>
Date: Tue, 28 Jan 2025 15:45:55 +0000
Subject: [PATCH 12/23] Add description for mapper_id. Add verifier check for
valid mapper_id.
---
mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td | 2 ++
mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp | 6 ++++++
mlir/test/Dialect/OpenMP/invalid.mlir | 10 ++++++++++
3 files changed, 18 insertions(+)
diff --git a/mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td b/mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td
index 10c4a5e37a4f6b..2d8e022190f620 100644
--- a/mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td
+++ b/mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td
@@ -1077,6 +1077,8 @@ def MapInfoOp : OpenMP_Op<"map.info", [AttrSizedOperandSegments]> {
- 'map_type': OpenMP map type for this map capture, for example: from, to and
always. It's a bitfield composed of the OpenMP runtime flags stored in
OpenMPOffloadMappingFlags.
+ - 'mapper_id': OpenMP mapper map type modifier for this map capture. It's used to
+ specify a user defined mapper to be used for mapping.
- 'map_capture_type': Capture type for the variable e.g. this, byref, byvalue, byvla
this can affect how the variable is lowered.
- `name`: Holds the name of variable as specified in user clause (including bounds).
diff --git a/mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp b/mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp
index b02da78b1fbf81..6787ad10ec7d14 100644
--- a/mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp
+++ b/mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp
@@ -1631,6 +1631,12 @@ static LogicalResult verifyMapClause(Operation *op, OperandRange mapVars) {
to ? updateToVars.insert(updateVar) : updateFromVars.insert(updateVar);
}
+
+ if (mapInfoOp.getMapperId() &&
+ !SymbolTable::lookupNearestSymbolFrom<omp::DeclareMapperOp>(
+ mapInfoOp, mapInfoOp.getMapperIdAttr())) {
+ return emitError(op->getLoc(), "invalid mapper id");
+ }
} else if (!isa<DeclareMapperInfoOp>(op)) {
emitError(op->getLoc(), "map argument is not a map entry operation");
}
diff --git a/mlir/test/Dialect/OpenMP/invalid.mlir b/mlir/test/Dialect/OpenMP/invalid.mlir
index a2d34a88d2ebe8..d7f468bed3d3dd 100644
--- a/mlir/test/Dialect/OpenMP/invalid.mlir
+++ b/mlir/test/Dialect/OpenMP/invalid.mlir
@@ -2849,3 +2849,13 @@ func.func @missing_workshare(%idx : index) {
^bb0(%arg0: !llvm.ptr):
omp.terminator
}
+
+// -----
+llvm.func @invalid_mapper(%0 : !llvm.ptr) {
+ %1 = omp.map.info var_ptr(%0 : !llvm.ptr, !llvm.struct<"my_type", (i32)>) mapper(@my_mapper) map_clauses(to) capture(ByRef) -> !llvm.ptr {name = ""}
+ // expected-error @below {{invalid mapper id}}
+ omp.target_data map_entries(%1 : !llvm.ptr) {
+ omp.terminator
+ }
+ llvm.return
+}
>From d7bb2309591d828756b7e1274ebcf592b992eb16 Mon Sep 17 00:00:00 2001
From: Akash Banerjee <Akash.Banerjee at amd.com>
Date: Mon, 23 Dec 2024 21:13:42 +0000
Subject: [PATCH 13/23] Add flang lowering changes for mapper field in map
clause.
---
flang/lib/Lower/OpenMP/ClauseProcessor.cpp | 32 +++++++++++++++++----
flang/lib/Lower/OpenMP/ClauseProcessor.h | 3 +-
flang/test/Lower/OpenMP/Todo/map-mapper.f90 | 16 -----------
flang/test/Lower/OpenMP/map-mapper.f90 | 23 +++++++++++++++
4 files changed, 52 insertions(+), 22 deletions(-)
delete mode 100644 flang/test/Lower/OpenMP/Todo/map-mapper.f90
create mode 100644 flang/test/Lower/OpenMP/map-mapper.f90
diff --git a/flang/lib/Lower/OpenMP/ClauseProcessor.cpp b/flang/lib/Lower/OpenMP/ClauseProcessor.cpp
index febc6adcf9d6ff..467a0dcebf2b8a 100644
--- a/flang/lib/Lower/OpenMP/ClauseProcessor.cpp
+++ b/flang/lib/Lower/OpenMP/ClauseProcessor.cpp
@@ -969,8 +969,10 @@ void ClauseProcessor::processMapObjects(
llvm::omp::OpenMPOffloadMappingFlags mapTypeBits,
std::map<Object, OmpMapParentAndMemberData> &parentMemberIndices,
llvm::SmallVectorImpl<mlir::Value> &mapVars,
- llvm::SmallVectorImpl<const semantics::Symbol *> &mapSyms) const {
+ llvm::SmallVectorImpl<const semantics::Symbol *> &mapSyms,
+ std::string mapperIdName) const {
fir::FirOpBuilder &firOpBuilder = converter.getFirOpBuilder();
+ mlir::FlatSymbolRefAttr mapperId;
for (const omp::Object &object : objects) {
llvm::SmallVector<mlir::Value> bounds;
@@ -1003,6 +1005,20 @@ void ClauseProcessor::processMapObjects(
}
}
+ if (!mapperIdName.empty()) {
+ if (mapperIdName == "default") {
+ auto &typeSpec = object.sym()->owner().IsDerivedType()
+ ? *object.sym()->owner().derivedTypeSpec()
+ : object.sym()->GetType()->derivedTypeSpec();
+ mapperIdName = typeSpec.name().ToString() + ".default";
+ mapperIdName = converter.mangleName(mapperIdName, *typeSpec.GetScope());
+ }
+ assert(converter.getMLIRSymbolTable()->lookup(mapperIdName) &&
+ "mapper not found");
+ mapperId = mlir::FlatSymbolRefAttr::get(&converter.getMLIRContext(),
+ mapperIdName);
+ mapperIdName.clear();
+ }
// Explicit map captures are captured ByRef by default,
// optimisation passes may alter this to ByCopy or other capture
// types to optimise
@@ -1016,7 +1032,8 @@ void ClauseProcessor::processMapObjects(
static_cast<
std::underlying_type_t<llvm::omp::OpenMPOffloadMappingFlags>>(
mapTypeBits),
- mlir::omp::VariableCaptureKind::ByRef, baseOp.getType());
+ mlir::omp::VariableCaptureKind::ByRef, baseOp.getType(), false,
+ mapperId);
if (parentObj.has_value()) {
parentMemberIndices[parentObj.value()].addChildIndexAndMapToParent(
@@ -1047,6 +1064,7 @@ bool ClauseProcessor::processMap(
const auto &[mapType, typeMods, mappers, iterator, objects] = clause.t;
llvm::omp::OpenMPOffloadMappingFlags mapTypeBits =
llvm::omp::OpenMPOffloadMappingFlags::OMP_MAP_NONE;
+ std::string mapperIdName;
// If the map type is specified, then process it else Tofrom is the
// default.
Map::MapType type = mapType.value_or(Map::MapType::Tofrom);
@@ -1090,13 +1108,17 @@ bool ClauseProcessor::processMap(
"Support for iterator modifiers is not implemented yet");
}
if (mappers) {
- TODO(currentLocation,
- "Support for mapper modifiers is not implemented yet");
+ assert(mappers->size() == 1 && "more than one mapper");
+ mapperIdName = mappers->front().v.id().symbol->name().ToString();
+ if (mapperIdName != "default")
+ mapperIdName = converter.mangleName(
+ mapperIdName, mappers->front().v.id().symbol->owner());
}
processMapObjects(stmtCtx, clauseLocation,
std::get<omp::ObjectList>(clause.t), mapTypeBits,
- parentMemberIndices, result.mapVars, *ptrMapSyms);
+ parentMemberIndices, result.mapVars, *ptrMapSyms,
+ mapperIdName);
};
bool clauseFound = findRepeatableClause<omp::clause::Map>(process);
diff --git a/flang/lib/Lower/OpenMP/ClauseProcessor.h b/flang/lib/Lower/OpenMP/ClauseProcessor.h
index e05f66c7666844..2b319e890a5adb 100644
--- a/flang/lib/Lower/OpenMP/ClauseProcessor.h
+++ b/flang/lib/Lower/OpenMP/ClauseProcessor.h
@@ -175,7 +175,8 @@ class ClauseProcessor {
llvm::omp::OpenMPOffloadMappingFlags mapTypeBits,
std::map<Object, OmpMapParentAndMemberData> &parentMemberIndices,
llvm::SmallVectorImpl<mlir::Value> &mapVars,
- llvm::SmallVectorImpl<const semantics::Symbol *> &mapSyms) const;
+ llvm::SmallVectorImpl<const semantics::Symbol *> &mapSyms,
+ std::string mapperIdName = "") const;
lower::AbstractConverter &converter;
semantics::SemanticsContext &semaCtx;
diff --git a/flang/test/Lower/OpenMP/Todo/map-mapper.f90 b/flang/test/Lower/OpenMP/Todo/map-mapper.f90
deleted file mode 100644
index 9554ffd5fda7bd..00000000000000
--- a/flang/test/Lower/OpenMP/Todo/map-mapper.f90
+++ /dev/null
@@ -1,16 +0,0 @@
-! RUN: not %flang_fc1 -emit-fir -fopenmp -fopenmp-version=50 %s 2>&1 | FileCheck %s
-program p
- integer, parameter :: n = 256
- real(8) :: a(256)
- !! TODO: Add declare mapper, when it works to lower this construct
- !!type t1
- !! integer :: x
- !!end type t1
- !!!$omp declare mapper(xx : t1 :: nn) map(nn, nn%x)
- !$omp target map(mapper(xx), from:a)
-!CHECK: not yet implemented: Support for mapper modifiers is not implemented yet
- do i=1,n
- a(i) = 4.2
- end do
- !$omp end target
-end program p
diff --git a/flang/test/Lower/OpenMP/map-mapper.f90 b/flang/test/Lower/OpenMP/map-mapper.f90
new file mode 100644
index 00000000000000..856fff834643e4
--- /dev/null
+++ b/flang/test/Lower/OpenMP/map-mapper.f90
@@ -0,0 +1,23 @@
+! RUN: %flang_fc1 -emit-hlfir -fopenmp -fopenmp-version=50 %s -o - | FileCheck %s
+program p
+ integer, parameter :: n = 256
+ type t1
+ integer :: x(256)
+ end type t1
+
+ !$omp declare mapper(xx : t1 :: nn) map(to: nn, nn%x)
+ !$omp declare mapper(t1 :: nn) map(from: nn)
+
+ !CHECK-LABEL: omp.declare_mapper @_QQFt1.default : !fir.type<_QFTt1{x:!fir.array<256xi32>}>
+ !CHECK-LABEL: omp.declare_mapper @_QQFxx : !fir.type<_QFTt1{x:!fir.array<256xi32>}>
+
+ type(t1) :: a, b
+ !CHECK: %[[MAP_A:.*]] = omp.map.info var_ptr(%{{.*}} : {{.*}}, {{.*}}) mapper(@_QQFxx) map_clauses(tofrom) capture(ByRef) -> {{.*}} {name = "a"}
+ !CHECK: %[[MAP_B:.*]] = omp.map.info var_ptr(%{{.*}} : {{.*}}, {{.*}}) mapper(@_QQFt1.default) map_clauses(tofrom) capture(ByRef) -> {{.*}} {name = "b"}
+ !CHECK: omp.target map_entries(%[[MAP_A]] -> %{{.*}}, %[[MAP_B]] -> %{{.*}}, %{{.*}} -> %{{.*}}, %{{.*}} -> %{{.*}} : {{.*}}, {{.*}}, {{.*}}, {{.*}}) {
+ !$omp target map(mapper(xx) : a) map(mapper(default) : b)
+ do i = 1, n
+ b%x(i) = a%x(i)
+ end do
+ !$omp end target
+end program p
>From fcd44f553ee84eebc811cc1a461d4aef7fa425dc Mon Sep 17 00:00:00 2001
From: Akash Banerjee <Akash.Banerjee at amd.com>
Date: Tue, 28 Jan 2025 15:28:29 +0000
Subject: [PATCH 14/23] Split test into two separate directives.
---
flang/test/Lower/OpenMP/map-mapper.f90 | 13 ++++++++++---
1 file changed, 10 insertions(+), 3 deletions(-)
diff --git a/flang/test/Lower/OpenMP/map-mapper.f90 b/flang/test/Lower/OpenMP/map-mapper.f90
index 856fff834643e4..0d8fe7344bfab5 100644
--- a/flang/test/Lower/OpenMP/map-mapper.f90
+++ b/flang/test/Lower/OpenMP/map-mapper.f90
@@ -13,11 +13,18 @@ program p
type(t1) :: a, b
!CHECK: %[[MAP_A:.*]] = omp.map.info var_ptr(%{{.*}} : {{.*}}, {{.*}}) mapper(@_QQFxx) map_clauses(tofrom) capture(ByRef) -> {{.*}} {name = "a"}
+ !CHECK: omp.target map_entries(%[[MAP_A]] -> %{{.*}}, %{{.*}} -> %{{.*}} : {{.*}}, {{.*}}) {
+ !$omp target map(mapper(xx) : a)
+ do i = 1, n
+ a%x(i) = i
+ end do
+ !$omp end target
+
!CHECK: %[[MAP_B:.*]] = omp.map.info var_ptr(%{{.*}} : {{.*}}, {{.*}}) mapper(@_QQFt1.default) map_clauses(tofrom) capture(ByRef) -> {{.*}} {name = "b"}
- !CHECK: omp.target map_entries(%[[MAP_A]] -> %{{.*}}, %[[MAP_B]] -> %{{.*}}, %{{.*}} -> %{{.*}}, %{{.*}} -> %{{.*}} : {{.*}}, {{.*}}, {{.*}}, {{.*}}) {
- !$omp target map(mapper(xx) : a) map(mapper(default) : b)
+ !CHECK: omp.target map_entries(%[[MAP_B]] -> %{{.*}}, %{{.*}} -> %{{.*}} : {{.*}}, {{.*}}) {
+ !$omp target map(mapper(default) : b)
do i = 1, n
- b%x(i) = a%x(i)
+ b%x(i) = i
end do
!$omp end target
end program p
>From 003adfa6d527749a956fd483d424ca09edabddb0 Mon Sep 17 00:00:00 2001
From: Akash Banerjee <Akash.Banerjee at amd.com>
Date: Fri, 7 Feb 2025 17:35:43 +0000
Subject: [PATCH 15/23] Address reviewer comments.
---
flang/lib/Lower/OpenMP/ClauseProcessor.cpp | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/flang/lib/Lower/OpenMP/ClauseProcessor.cpp b/flang/lib/Lower/OpenMP/ClauseProcessor.cpp
index 467a0dcebf2b8a..359a7a8a039a2d 100644
--- a/flang/lib/Lower/OpenMP/ClauseProcessor.cpp
+++ b/flang/lib/Lower/OpenMP/ClauseProcessor.cpp
@@ -1013,7 +1013,7 @@ void ClauseProcessor::processMapObjects(
mapperIdName = typeSpec.name().ToString() + ".default";
mapperIdName = converter.mangleName(mapperIdName, *typeSpec.GetScope());
}
- assert(converter.getMLIRSymbolTable()->lookup(mapperIdName) &&
+ assert(converter.getModuleOp().lookupSymbol(mapperIdName) &&
"mapper not found");
mapperId = mlir::FlatSymbolRefAttr::get(&converter.getMLIRContext(),
mapperIdName);
@@ -1032,8 +1032,8 @@ void ClauseProcessor::processMapObjects(
static_cast<
std::underlying_type_t<llvm::omp::OpenMPOffloadMappingFlags>>(
mapTypeBits),
- mlir::omp::VariableCaptureKind::ByRef, baseOp.getType(), false,
- mapperId);
+ mlir::omp::VariableCaptureKind::ByRef, baseOp.getType(),
+ /*partialMap=*/false, mapperId);
if (parentObj.has_value()) {
parentMemberIndices[parentObj.value()].addChildIndexAndMapToParent(
>From c5b4f50e130853b20698b4c1b2f55b9c49eae2fd Mon Sep 17 00:00:00 2001
From: Akash Banerjee <Akash.Banerjee at amd.com>
Date: Mon, 10 Feb 2025 17:46:02 +0000
Subject: [PATCH 16/23] Replace std:string with llvm::StringRef
---
flang/lib/Lower/OpenMP/ClauseProcessor.cpp | 3 ++-
flang/lib/Lower/OpenMP/ClauseProcessor.h | 2 +-
2 files changed, 3 insertions(+), 2 deletions(-)
diff --git a/flang/lib/Lower/OpenMP/ClauseProcessor.cpp b/flang/lib/Lower/OpenMP/ClauseProcessor.cpp
index 359a7a8a039a2d..e21d299570b863 100644
--- a/flang/lib/Lower/OpenMP/ClauseProcessor.cpp
+++ b/flang/lib/Lower/OpenMP/ClauseProcessor.cpp
@@ -970,9 +970,10 @@ void ClauseProcessor::processMapObjects(
std::map<Object, OmpMapParentAndMemberData> &parentMemberIndices,
llvm::SmallVectorImpl<mlir::Value> &mapVars,
llvm::SmallVectorImpl<const semantics::Symbol *> &mapSyms,
- std::string mapperIdName) const {
+ llvm::StringRef mapperIdNameRef) const {
fir::FirOpBuilder &firOpBuilder = converter.getFirOpBuilder();
mlir::FlatSymbolRefAttr mapperId;
+ std::string mapperIdName = mapperIdNameRef.str();
for (const omp::Object &object : objects) {
llvm::SmallVector<mlir::Value> bounds;
diff --git a/flang/lib/Lower/OpenMP/ClauseProcessor.h b/flang/lib/Lower/OpenMP/ClauseProcessor.h
index 2b319e890a5adb..889a09a8f0cd85 100644
--- a/flang/lib/Lower/OpenMP/ClauseProcessor.h
+++ b/flang/lib/Lower/OpenMP/ClauseProcessor.h
@@ -176,7 +176,7 @@ class ClauseProcessor {
std::map<Object, OmpMapParentAndMemberData> &parentMemberIndices,
llvm::SmallVectorImpl<mlir::Value> &mapVars,
llvm::SmallVectorImpl<const semantics::Symbol *> &mapSyms,
- std::string mapperIdName = "") const;
+ llvm::StringRef mapperIdNameRef = "") const;
lower::AbstractConverter &converter;
semantics::SemanticsContext &semaCtx;
>From 14e666593f9966b24f11602104788c3501364574 Mon Sep 17 00:00:00 2001
From: Akash Banerjee <Akash.Banerjee at amd.com>
Date: Mon, 23 Dec 2024 21:50:03 +0000
Subject: [PATCH 17/23] Add OpenMP to LLVM dialect conversion support for
DeclareMapperOp.
---
.../Fir/convert-to-llvm-openmp-and-fir.fir | 27 +++++++++--
.../Conversion/OpenMPToLLVM/OpenMPToLLVM.cpp | 48 +++++++++++++++----
.../OpenMPToLLVM/convert-to-llvmir.mlir | 13 +++++
3 files changed, 74 insertions(+), 14 deletions(-)
diff --git a/flang/test/Fir/convert-to-llvm-openmp-and-fir.fir b/flang/test/Fir/convert-to-llvm-openmp-and-fir.fir
index 8e4e1fe824d9f5..82f2aea3ad983c 100644
--- a/flang/test/Fir/convert-to-llvm-openmp-and-fir.fir
+++ b/flang/test/Fir/convert-to-llvm-openmp-and-fir.fir
@@ -936,9 +936,9 @@ func.func @omp_map_info_descriptor_type_conversion(%arg0 : !fir.ref<!fir.box<!fi
%1 = omp.map.info var_ptr(%0 : !fir.llvm_ptr<!fir.ref<i32>>, i32) map_clauses(tofrom) capture(ByRef) -> !fir.llvm_ptr<!fir.ref<i32>> {name = ""}
// CHECK: %[[DESC_MAP:.*]] = omp.map.info var_ptr(%[[ARG_0]] : !llvm.ptr, !llvm.struct<(ptr, i64, i32, i8, i8, i8, i8)>) map_clauses(always, delete) capture(ByRef) members(%[[MEMBER_MAP]] : [0] : !llvm.ptr) -> !llvm.ptr {name = ""}
%2 = omp.map.info var_ptr(%arg0 : !fir.ref<!fir.box<!fir.heap<i32>>>, !fir.box<!fir.heap<i32>>) map_clauses(always, delete) capture(ByRef) members(%1 : [0] : !fir.llvm_ptr<!fir.ref<i32>>) -> !fir.ref<!fir.box<!fir.heap<i32>>> {name = ""}
- // CHECK: omp.target_exit_data map_entries(%[[DESC_MAP]] : !llvm.ptr)
+ // CHECK: omp.target_exit_data map_entries(%[[DESC_MAP]] : !llvm.ptr)
omp.target_exit_data map_entries(%2 : !fir.ref<!fir.box<!fir.heap<i32>>>)
- return
+ return
}
// -----
@@ -956,8 +956,8 @@ func.func @omp_map_info_derived_type_explicit_member_conversion(%arg0 : !fir.ref
%3 = fir.field_index real, !fir.type<_QFderived_type{real:f32,array:!fir.array<10xi32>,int:i32}>
%4 = fir.coordinate_of %arg0, %3 : (!fir.ref<!fir.type<_QFderived_type{real:f32,array:!fir.array<10xi32>,int:i32}>>, !fir.field) -> !fir.ref<f32>
// CHECK: %[[MAP_MEMBER_2:.*]] = omp.map.info var_ptr(%[[GEP_2]] : !llvm.ptr, f32) map_clauses(tofrom) capture(ByRef) -> !llvm.ptr {name = "dtype%real"}
- %5 = omp.map.info var_ptr(%4 : !fir.ref<f32>, f32) map_clauses(tofrom) capture(ByRef) -> !fir.ref<f32> {name = "dtype%real"}
- // CHECK: %[[MAP_PARENT:.*]] = omp.map.info var_ptr(%[[ARG_0]] : !llvm.ptr, !llvm.struct<"_QFderived_type", (f32, array<10 x i32>, i32)>) map_clauses(tofrom) capture(ByRef) members(%[[MAP_MEMBER_1]], %[[MAP_MEMBER_2]] : [2], [0] : !llvm.ptr, !llvm.ptr) -> !llvm.ptr {name = "dtype", partial_map = true}
+ %5 = omp.map.info var_ptr(%4 : !fir.ref<f32>, f32) map_clauses(tofrom) capture(ByRef) -> !fir.ref<f32> {name = "dtype%real"}
+ // CHECK: %[[MAP_PARENT:.*]] = omp.map.info var_ptr(%[[ARG_0]] : !llvm.ptr, !llvm.struct<"_QFderived_type", (f32, array<10 x i32>, i32)>) map_clauses(tofrom) capture(ByRef) members(%[[MAP_MEMBER_1]], %[[MAP_MEMBER_2]] : [2], [0] : !llvm.ptr, !llvm.ptr) -> !llvm.ptr {name = "dtype", partial_map = true}
%6 = omp.map.info var_ptr(%arg0 : !fir.ref<!fir.type<_QFderived_type{real:f32,array:!fir.array<10xi32>,int:i32}>>, !fir.type<_QFderived_type{real:f32,array:!fir.array<10xi32>,int:i32}>) map_clauses(tofrom) capture(ByRef) members(%2, %5 : [2], [0] : !fir.ref<i32>, !fir.ref<f32>) -> !fir.ref<!fir.type<_QFderived_type{real:f32,array:!fir.array<10xi32>,int:i32}>> {name = "dtype", partial_map = true}
// CHECK: omp.target map_entries(%[[MAP_MEMBER_1]] -> %[[ARG_1:.*]], %[[MAP_MEMBER_2]] -> %[[ARG_2:.*]], %[[MAP_PARENT]] -> %[[ARG_3:.*]] : !llvm.ptr, !llvm.ptr, !llvm.ptr) {
omp.target map_entries(%2 -> %arg1, %5 -> %arg2, %6 -> %arg3 : !fir.ref<i32>, !fir.ref<f32>, !fir.ref<!fir.type<_QFderived_type{real:f32,array:!fir.array<10xi32>,int:i32}>>) {
@@ -1275,3 +1275,22 @@ func.func @map_nested_dtype_alloca_mem2(%arg0 : !fir.ref<!fir.type<_QFRecTy{i:f3
}
return
}
+
+// -----
+
+// CHECK-LABEL: omp.declare_mapper @my_mapper : !llvm.struct<"_QFdeclare_mapperTmy_type", (i32)> {
+omp.declare_mapper @my_mapper : !fir.type<_QFdeclare_mapperTmy_type{data:i32}> {
+// CHECK: ^bb0(%[[VAL_0:.*]]: !llvm.ptr):
+^bb0(%0: !fir.ref<!fir.type<_QFdeclare_mapperTmy_type{data:i32}>>):
+// CHECK: %[[VAL_1:.*]] = llvm.mlir.constant(0 : i32) : i32
+ %1 = fir.field_index data, !fir.type<_QFdeclare_mapperTmy_type{data:i32}>
+// CHECK: %[[VAL_2:.*]] = llvm.getelementptr %[[VAL_0]][0, 0] : (!llvm.ptr) -> !llvm.ptr, !llvm.struct<"_QFdeclare_mapperTmy_type", (i32)>
+ %2 = fir.coordinate_of %0, %1 : (!fir.ref<!fir.type<_QFdeclare_mapperTmy_type{data:i32}>>, !fir.field) -> !fir.ref<i32>
+// CHECK: %[[VAL_3:.*]] = omp.map.info var_ptr(%[[VAL_2]] : !llvm.ptr, i32) map_clauses(tofrom) capture(ByRef) -> !llvm.ptr {name = "var%[[VAL_4:.*]]"}
+ %3 = omp.map.info var_ptr(%2 : !fir.ref<i32>, i32) map_clauses(tofrom) capture(ByRef) -> !fir.ref<i32> {name = "var%data"}
+// CHECK: %[[VAL_5:.*]] = omp.map.info var_ptr(%[[VAL_0]] : !llvm.ptr, !llvm.struct<"_QFdeclare_mapperTmy_type", (i32)>) map_clauses(tofrom) capture(ByRef) members(%[[VAL_3]] : [0] : !llvm.ptr) -> !llvm.ptr {name = "var", partial_map = true}
+ %4 = omp.map.info var_ptr(%0 : !fir.ref<!fir.type<_QFdeclare_mapperTmy_type{data:i32}>>, !fir.type<_QFdeclare_mapperTmy_type{data:i32}>) map_clauses(tofrom) capture(ByRef) members(%3 : [0] : !fir.ref<i32>) -> !fir.ref<!fir.type<_QFdeclare_mapperTmy_type{data:i32}>> {name = "var", partial_map = true}
+// CHECK: omp.declare_mapper_info map_entries(%[[VAL_5]], %[[VAL_3]] : !llvm.ptr, !llvm.ptr)
+ omp.declare_mapper_info map_entries(%4, %3 : !fir.ref<!fir.type<_QFdeclare_mapperTmy_type{data:i32}>>, !fir.ref<i32>)
+// CHECK: }
+}
diff --git a/mlir/lib/Conversion/OpenMPToLLVM/OpenMPToLLVM.cpp b/mlir/lib/Conversion/OpenMPToLLVM/OpenMPToLLVM.cpp
index 12e3c07669839d..cc01d8aacf3cf7 100644
--- a/mlir/lib/Conversion/OpenMPToLLVM/OpenMPToLLVM.cpp
+++ b/mlir/lib/Conversion/OpenMPToLLVM/OpenMPToLLVM.cpp
@@ -186,6 +186,32 @@ struct MapInfoOpConversion : public ConvertOpToLLVMPattern<omp::MapInfoOp> {
}
};
+struct DeclMapperOpConversion
+ : public ConvertOpToLLVMPattern<omp::DeclareMapperOp> {
+ using ConvertOpToLLVMPattern<omp::DeclareMapperOp>::ConvertOpToLLVMPattern;
+ LogicalResult
+ matchAndRewrite(omp::DeclareMapperOp curOp, OpAdaptor adaptor,
+ ConversionPatternRewriter &rewriter) const override {
+ const TypeConverter *converter = ConvertToLLVMPattern::getTypeConverter();
+ SmallVector<NamedAttribute> newAttrs;
+ newAttrs.emplace_back(curOp.getSymNameAttrName(), curOp.getSymNameAttr());
+ newAttrs.emplace_back(
+ curOp.getVarTypeAttrName(),
+ TypeAttr::get(converter->convertType(curOp.getVarType())));
+
+ auto newOp = rewriter.create<omp::DeclareMapperOp>(
+ curOp.getLoc(), TypeRange(), adaptor.getOperands(), newAttrs);
+ rewriter.inlineRegionBefore(curOp.getRegion(), newOp.getRegion(),
+ newOp.getRegion().end());
+ if (failed(rewriter.convertRegionTypes(&newOp.getRegion(),
+ *this->getTypeConverter())))
+ return failure();
+
+ rewriter.eraseOp(curOp);
+ return success();
+ }
+};
+
template <typename OpType>
struct MultiRegionOpConversion : public ConvertOpToLLVMPattern<OpType> {
using ConvertOpToLLVMPattern<OpType>::ConvertOpToLLVMPattern;
@@ -225,19 +251,20 @@ void mlir::configureOpenMPToLLVMConversionLegality(
ConversionTarget &target, const LLVMTypeConverter &typeConverter) {
target.addDynamicallyLegalOp<
omp::AtomicReadOp, omp::AtomicWriteOp, omp::CancellationPointOp,
- omp::CancelOp, omp::CriticalDeclareOp, omp::FlushOp, omp::MapBoundsOp,
- omp::MapInfoOp, omp::OrderedOp, omp::ScanOp, omp::TargetEnterDataOp,
- omp::TargetExitDataOp, omp::TargetUpdateOp, omp::ThreadprivateOp,
- omp::YieldOp>([&](Operation *op) {
+ omp::CancelOp, omp::CriticalDeclareOp, omp::DeclareMapperInfoOp,
+ omp::FlushOp, omp::MapBoundsOp, omp::MapInfoOp, omp::OrderedOp, omp::ScanOp,
+ omp::TargetEnterDataOp, omp::TargetExitDataOp, omp::TargetUpdateOp,
+ omp::ThreadprivateOp, omp::YieldOp>([&](Operation *op) {
return typeConverter.isLegal(op->getOperandTypes()) &&
typeConverter.isLegal(op->getResultTypes());
});
target.addDynamicallyLegalOp<
- omp::AtomicUpdateOp, omp::CriticalOp, omp::DeclareReductionOp,
- omp::DistributeOp, omp::LoopNestOp, omp::LoopOp, omp::MasterOp,
- omp::OrderedRegionOp, omp::ParallelOp, omp::SectionOp, omp::SectionsOp,
- omp::SimdOp, omp::SingleOp, omp::TargetDataOp, omp::TargetOp,
- omp::TaskgroupOp, omp::TaskloopOp, omp::TaskOp, omp::TeamsOp,
+ omp::AtomicUpdateOp, omp::CriticalOp, omp::DeclareMapperOp,
+ omp::DeclareReductionOp, omp::DistributeOp, omp::LoopNestOp, omp::LoopOp,
+ omp::MasterOp, omp::OrderedRegionOp, omp::ParallelOp,
+ omp::PrivateClauseOp, omp::SectionOp, omp::SectionsOp, omp::SimdOp,
+ omp::SingleOp, omp::TargetDataOp, omp::TargetOp, omp::TaskgroupOp,
+ omp::TaskloopOp, omp::TaskOp, omp::TeamsOp,
omp::WsloopOp>([&](Operation *op) {
return std::all_of(op->getRegions().begin(), op->getRegions().end(),
[&](Region ®ion) {
@@ -267,12 +294,13 @@ void mlir::populateOpenMPToLLVMConversionPatterns(LLVMTypeConverter &converter,
[&](omp::MapBoundsType type) -> Type { return type; });
patterns.add<
- AtomicReadOpConversion, MapInfoOpConversion,
+ AtomicReadOpConversion, DeclMapperOpConversion, MapInfoOpConversion,
MultiRegionOpConversion<omp::DeclareReductionOp>,
MultiRegionOpConversion<omp::PrivateClauseOp>,
RegionLessOpConversion<omp::CancellationPointOp>,
RegionLessOpConversion<omp::CancelOp>,
RegionLessOpConversion<omp::CriticalDeclareOp>,
+ RegionLessOpConversion<omp::DeclareMapperInfoOp>,
RegionLessOpConversion<omp::OrderedOp>,
RegionLessOpConversion<omp::ScanOp>,
RegionLessOpConversion<omp::TargetEnterDataOp>,
diff --git a/mlir/test/Conversion/OpenMPToLLVM/convert-to-llvmir.mlir b/mlir/test/Conversion/OpenMPToLLVM/convert-to-llvmir.mlir
index 6f1ed73e778b43..9f21fcb2cb7b87 100644
--- a/mlir/test/Conversion/OpenMPToLLVM/convert-to-llvmir.mlir
+++ b/mlir/test/Conversion/OpenMPToLLVM/convert-to-llvmir.mlir
@@ -601,3 +601,16 @@ func.func @omp_taskloop(%arg0: index, %arg1 : memref<i32>) {
}
return
}
+
+// -----
+
+// CHECK-LABEL: omp.declare_mapper @my_mapper : !llvm.struct<"_QFdeclare_mapperTmy_type", (i32)> {
+omp.declare_mapper @my_mapper : !llvm.struct<"_QFdeclare_mapperTmy_type", (i32)> {
+^bb0(%arg0: !llvm.ptr):
+ %0 = llvm.mlir.constant(0 : i32) : i32
+ %1 = llvm.getelementptr %arg0[0, 0] : (!llvm.ptr) -> !llvm.ptr, !llvm.struct<"_QFdeclare_mapperTmy_type", (i32)>
+ %2 = omp.map.info var_ptr(%1 : !llvm.ptr, i32) map_clauses(tofrom) capture(ByRef) -> !llvm.ptr {name = "var%data"}
+ %3 = omp.map.info var_ptr(%arg0 : !llvm.ptr, !llvm.struct<"_QFdeclare_mapperTmy_type", (i32)>) map_clauses(tofrom) capture(ByRef) members(%2 : [0] : !llvm.ptr) -> !llvm.ptr {name = "var", partial_map = true}
+ // CHECK: omp.declare_mapper_info map_entries(%{{.*}}, %{{.*}} : !llvm.ptr, !llvm.ptr)
+ omp.declare_mapper_info map_entries(%3, %2 : !llvm.ptr, !llvm.ptr)
+}
>From af3dcbfd2a577009cc68e18a246f9d25f6b951ad Mon Sep 17 00:00:00 2001
From: Akash Banerjee <Akash.Banerjee at amd.com>
Date: Fri, 7 Feb 2025 17:42:05 +0000
Subject: [PATCH 18/23] Address reviewer comments.
---
.../test/Fir/convert-to-llvm-openmp-and-fir.fir | 4 ++--
.../Conversion/OpenMPToLLVM/OpenMPToLLVM.cpp | 17 +++++++++--------
.../OpenMPToLLVM/convert-to-llvmir.mlir | 4 ++--
3 files changed, 13 insertions(+), 12 deletions(-)
diff --git a/flang/test/Fir/convert-to-llvm-openmp-and-fir.fir b/flang/test/Fir/convert-to-llvm-openmp-and-fir.fir
index 82f2aea3ad983c..7cdcd2a10e9751 100644
--- a/flang/test/Fir/convert-to-llvm-openmp-and-fir.fir
+++ b/flang/test/Fir/convert-to-llvm-openmp-and-fir.fir
@@ -1290,7 +1290,7 @@ omp.declare_mapper @my_mapper : !fir.type<_QFdeclare_mapperTmy_type{data:i32}> {
%3 = omp.map.info var_ptr(%2 : !fir.ref<i32>, i32) map_clauses(tofrom) capture(ByRef) -> !fir.ref<i32> {name = "var%data"}
// CHECK: %[[VAL_5:.*]] = omp.map.info var_ptr(%[[VAL_0]] : !llvm.ptr, !llvm.struct<"_QFdeclare_mapperTmy_type", (i32)>) map_clauses(tofrom) capture(ByRef) members(%[[VAL_3]] : [0] : !llvm.ptr) -> !llvm.ptr {name = "var", partial_map = true}
%4 = omp.map.info var_ptr(%0 : !fir.ref<!fir.type<_QFdeclare_mapperTmy_type{data:i32}>>, !fir.type<_QFdeclare_mapperTmy_type{data:i32}>) map_clauses(tofrom) capture(ByRef) members(%3 : [0] : !fir.ref<i32>) -> !fir.ref<!fir.type<_QFdeclare_mapperTmy_type{data:i32}>> {name = "var", partial_map = true}
-// CHECK: omp.declare_mapper_info map_entries(%[[VAL_5]], %[[VAL_3]] : !llvm.ptr, !llvm.ptr)
- omp.declare_mapper_info map_entries(%4, %3 : !fir.ref<!fir.type<_QFdeclare_mapperTmy_type{data:i32}>>, !fir.ref<i32>)
+// CHECK: omp.declare_mapper.info map_entries(%[[VAL_5]], %[[VAL_3]] : !llvm.ptr, !llvm.ptr)
+ omp.declare_mapper.info map_entries(%4, %3 : !fir.ref<!fir.type<_QFdeclare_mapperTmy_type{data:i32}>>, !fir.ref<i32>)
// CHECK: }
}
diff --git a/mlir/lib/Conversion/OpenMPToLLVM/OpenMPToLLVM.cpp b/mlir/lib/Conversion/OpenMPToLLVM/OpenMPToLLVM.cpp
index cc01d8aacf3cf7..7888745dc6920d 100644
--- a/mlir/lib/Conversion/OpenMPToLLVM/OpenMPToLLVM.cpp
+++ b/mlir/lib/Conversion/OpenMPToLLVM/OpenMPToLLVM.cpp
@@ -196,8 +196,8 @@ struct DeclMapperOpConversion
SmallVector<NamedAttribute> newAttrs;
newAttrs.emplace_back(curOp.getSymNameAttrName(), curOp.getSymNameAttr());
newAttrs.emplace_back(
- curOp.getVarTypeAttrName(),
- TypeAttr::get(converter->convertType(curOp.getVarType())));
+ curOp.getTypeAttrName(),
+ TypeAttr::get(converter->convertType(curOp.getType())));
auto newOp = rewriter.create<omp::DeclareMapperOp>(
curOp.getLoc(), TypeRange(), adaptor.getOperands(), newAttrs);
@@ -252,12 +252,13 @@ void mlir::configureOpenMPToLLVMConversionLegality(
target.addDynamicallyLegalOp<
omp::AtomicReadOp, omp::AtomicWriteOp, omp::CancellationPointOp,
omp::CancelOp, omp::CriticalDeclareOp, omp::DeclareMapperInfoOp,
- omp::FlushOp, omp::MapBoundsOp, omp::MapInfoOp, omp::OrderedOp, omp::ScanOp,
- omp::TargetEnterDataOp, omp::TargetExitDataOp, omp::TargetUpdateOp,
- omp::ThreadprivateOp, omp::YieldOp>([&](Operation *op) {
- return typeConverter.isLegal(op->getOperandTypes()) &&
- typeConverter.isLegal(op->getResultTypes());
- });
+ omp::FlushOp, omp::MapBoundsOp, omp::MapInfoOp, omp::OrderedOp,
+ omp::ScanOp, omp::TargetEnterDataOp, omp::TargetExitDataOp,
+ omp::TargetUpdateOp, omp::ThreadprivateOp, omp::YieldOp>(
+ [&](Operation *op) {
+ return typeConverter.isLegal(op->getOperandTypes()) &&
+ typeConverter.isLegal(op->getResultTypes());
+ });
target.addDynamicallyLegalOp<
omp::AtomicUpdateOp, omp::CriticalOp, omp::DeclareMapperOp,
omp::DeclareReductionOp, omp::DistributeOp, omp::LoopNestOp, omp::LoopOp,
diff --git a/mlir/test/Conversion/OpenMPToLLVM/convert-to-llvmir.mlir b/mlir/test/Conversion/OpenMPToLLVM/convert-to-llvmir.mlir
index 9f21fcb2cb7b87..d69de998346b53 100644
--- a/mlir/test/Conversion/OpenMPToLLVM/convert-to-llvmir.mlir
+++ b/mlir/test/Conversion/OpenMPToLLVM/convert-to-llvmir.mlir
@@ -611,6 +611,6 @@ omp.declare_mapper @my_mapper : !llvm.struct<"_QFdeclare_mapperTmy_type", (i32)>
%1 = llvm.getelementptr %arg0[0, 0] : (!llvm.ptr) -> !llvm.ptr, !llvm.struct<"_QFdeclare_mapperTmy_type", (i32)>
%2 = omp.map.info var_ptr(%1 : !llvm.ptr, i32) map_clauses(tofrom) capture(ByRef) -> !llvm.ptr {name = "var%data"}
%3 = omp.map.info var_ptr(%arg0 : !llvm.ptr, !llvm.struct<"_QFdeclare_mapperTmy_type", (i32)>) map_clauses(tofrom) capture(ByRef) members(%2 : [0] : !llvm.ptr) -> !llvm.ptr {name = "var", partial_map = true}
- // CHECK: omp.declare_mapper_info map_entries(%{{.*}}, %{{.*}} : !llvm.ptr, !llvm.ptr)
- omp.declare_mapper_info map_entries(%3, %2 : !llvm.ptr, !llvm.ptr)
+ // CHECK: omp.declare_mapper.info map_entries(%{{.*}}, %{{.*}} : !llvm.ptr, !llvm.ptr)
+ omp.declare_mapper.info map_entries(%3, %2 : !llvm.ptr, !llvm.ptr)
}
>From 66fe4f9aa58d284967668dacc781e34a4816cc82 Mon Sep 17 00:00:00 2001
From: Akash Banerjee <Akash.Banerjee at amd.com>
Date: Tue, 28 Jan 2025 13:38:13 +0000
Subject: [PATCH 19/23] [MLIR][OpenMP] Add LLVM translation support for OpenMP
UserDefinedMappers
This patch adds OpenMPToLLVMIRTranslation support for the OpenMP Declare Mapper directive.
Since both MLIR and Clang now support custom mappers, I've made the relative params required instead of optional as well.
Depends on #121005
---
clang/lib/CodeGen/CGOpenMPRuntime.cpp | 11 +-
.../llvm/Frontend/OpenMP/OMPIRBuilder.h | 31 +--
llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp | 70 +++---
.../Frontend/OpenMPIRBuilderTest.cpp | 46 ++--
.../OpenMP/OpenMPToLLVMIRTranslation.cpp | 215 +++++++++++++++---
mlir/test/Target/LLVMIR/omptarget-llvm.mlir | 117 ++++++++++
.../fortran/target-custom-mapper.f90 | 46 ++++
7 files changed, 437 insertions(+), 99 deletions(-)
create mode 100644 offload/test/offloading/fortran/target-custom-mapper.f90
diff --git a/clang/lib/CodeGen/CGOpenMPRuntime.cpp b/clang/lib/CodeGen/CGOpenMPRuntime.cpp
index cafaaa364cb763..b919c1f6ac6276 100644
--- a/clang/lib/CodeGen/CGOpenMPRuntime.cpp
+++ b/clang/lib/CodeGen/CGOpenMPRuntime.cpp
@@ -8889,8 +8889,8 @@ static void emitOffloadingArraysAndArgs(
return MFunc;
};
OMPBuilder.emitOffloadingArraysAndArgs(
- AllocaIP, CodeGenIP, Info, Info.RTArgs, CombinedInfo, IsNonContiguous,
- ForEndCall, DeviceAddrCB, CustomMapperCB);
+ AllocaIP, CodeGenIP, Info, Info.RTArgs, CombinedInfo, CustomMapperCB,
+ IsNonContiguous, ForEndCall, DeviceAddrCB);
}
/// Check for inner distribute directive.
@@ -9099,9 +9099,10 @@ void CGOpenMPRuntime::emitUserDefinedMapper(const OMPDeclareMapperDecl *D,
CGM.getCXXABI().getMangleContext().mangleCanonicalTypeName(Ty, Out);
std::string Name = getName({"omp_mapper", TyStr, D->getName()});
- auto *NewFn = OMPBuilder.emitUserDefinedMapper(PrivatizeAndGenMapInfoCB,
- ElemTy, Name, CustomMapperCB);
- UDMMap.try_emplace(D, NewFn);
+ llvm::Expected<llvm::Function *> NewFn = OMPBuilder.emitUserDefinedMapper(
+ PrivatizeAndGenMapInfoCB, ElemTy, Name, CustomMapperCB);
+ assert(NewFn && "Unexpected error in emitUserDefinedMapper");
+ UDMMap.try_emplace(D, *NewFn);
if (CGF)
FunctionUDMMap[CGF->CurFn].push_back(D);
}
diff --git a/llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h b/llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
index d25077cae63e42..151bd36aadaf0a 100644
--- a/llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
+++ b/llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
@@ -2399,6 +2399,7 @@ class OpenMPIRBuilder {
CurInfo.NonContigInfo.Strides.end());
}
};
+ using MapInfosOrErrorTy = Expected<MapInfosTy &>;
/// Callback function type for functions emitting the host fallback code that
/// is executed when the kernel launch fails. It takes an insertion point as
@@ -2475,9 +2476,9 @@ class OpenMPIRBuilder {
/// including base pointers, pointers, sizes, map types, user-defined mappers.
void emitOffloadingArrays(
InsertPointTy AllocaIP, InsertPointTy CodeGenIP, MapInfosTy &CombinedInfo,
- TargetDataInfo &Info, bool IsNonContiguous = false,
- function_ref<void(unsigned int, Value *)> DeviceAddrCB = nullptr,
- function_ref<Value *(unsigned int)> CustomMapperCB = nullptr);
+ TargetDataInfo &Info, function_ref<Value *(unsigned int)> CustomMapperCB,
+ bool IsNonContiguous = false,
+ function_ref<void(unsigned int, Value *)> DeviceAddrCB = nullptr);
/// Allocates memory for and populates the arrays required for offloading
/// (offload_{baseptrs|ptrs|mappers|sizes|maptypes|mapnames}). Then, it
@@ -2488,9 +2489,9 @@ class OpenMPIRBuilder {
void emitOffloadingArraysAndArgs(
InsertPointTy AllocaIP, InsertPointTy CodeGenIP, TargetDataInfo &Info,
TargetDataRTArgs &RTArgs, MapInfosTy &CombinedInfo,
+ function_ref<Value *(unsigned int)> CustomMapperCB,
bool IsNonContiguous = false, bool ForEndCall = false,
- function_ref<void(unsigned int, Value *)> DeviceAddrCB = nullptr,
- function_ref<Value *(unsigned int)> CustomMapperCB = nullptr);
+ function_ref<void(unsigned int, Value *)> DeviceAddrCB = nullptr);
/// Creates offloading entry for the provided entry ID \a ID, address \a
/// Addr, size \a Size, and flags \a Flags.
@@ -2950,12 +2951,12 @@ class OpenMPIRBuilder {
/// \param FuncName Optional param to specify mapper function name.
/// \param CustomMapperCB Optional callback to generate code related to
/// custom mappers.
- Function *emitUserDefinedMapper(
- function_ref<MapInfosTy &(InsertPointTy CodeGenIP, llvm::Value *PtrPHI,
- llvm::Value *BeginArg)>
+ Expected<Function *> emitUserDefinedMapper(
+ function_ref<MapInfosOrErrorTy(
+ InsertPointTy CodeGenIP, llvm::Value *PtrPHI, llvm::Value *BeginArg)>
PrivAndGenMapInfoCB,
llvm::Type *ElemTy, StringRef FuncName,
- function_ref<bool(unsigned int, Function **)> CustomMapperCB = nullptr);
+ function_ref<bool(unsigned int, Function **)> CustomMapperCB);
/// Generator for '#omp target data'
///
@@ -2969,21 +2970,21 @@ class OpenMPIRBuilder {
/// \param IfCond Value which corresponds to the if clause condition.
/// \param Info Stores all information realted to the Target Data directive.
/// \param GenMapInfoCB Callback that populates the MapInfos and returns.
+ /// \param CustomMapperCB Callback to generate code related to
+ /// custom mappers.
/// \param BodyGenCB Optional Callback to generate the region code.
/// \param DeviceAddrCB Optional callback to generate code related to
/// use_device_ptr and use_device_addr.
- /// \param CustomMapperCB Optional callback to generate code related to
- /// custom mappers.
InsertPointOrErrorTy createTargetData(
const LocationDescription &Loc, InsertPointTy AllocaIP,
InsertPointTy CodeGenIP, Value *DeviceID, Value *IfCond,
TargetDataInfo &Info, GenMapInfoCallbackTy GenMapInfoCB,
+ function_ref<Value *(unsigned int)> CustomMapperCB,
omp::RuntimeFunction *MapperFunc = nullptr,
function_ref<InsertPointOrErrorTy(InsertPointTy CodeGenIP,
BodyGenTy BodyGenType)>
BodyGenCB = nullptr,
function_ref<void(unsigned int, Value *)> DeviceAddrCB = nullptr,
- function_ref<Value *(unsigned int)> CustomMapperCB = nullptr,
Value *SrcLocInfo = nullptr);
using TargetBodyGenCallbackTy = function_ref<InsertPointOrErrorTy(
@@ -2999,6 +3000,7 @@ class OpenMPIRBuilder {
/// \param IsOffloadEntry whether it is an offload entry.
/// \param CodeGenIP The insertion point where the call to the outlined
/// function should be emitted.
+ /// \param Info Stores all information realted to the Target directive.
/// \param EntryInfo The entry information about the function.
/// \param DefaultAttrs Structure containing the default attributes, including
/// numbers of threads and teams to launch the kernel with.
@@ -3010,6 +3012,8 @@ class OpenMPIRBuilder {
/// \param BodyGenCB Callback that will generate the region code.
/// \param ArgAccessorFuncCB Callback that will generate accessors
/// instructions for passed in target arguments where neccessary
+ /// \param CustomMapperCB Callback to generate code related to
+ /// custom mappers.
/// \param Dependencies A vector of DependData objects that carry
/// dependency information as passed in the depend clause
/// \param HasNowait Whether the target construct has a `nowait` clause or
@@ -3017,13 +3021,14 @@ class OpenMPIRBuilder {
InsertPointOrErrorTy createTarget(
const LocationDescription &Loc, bool IsOffloadEntry,
OpenMPIRBuilder::InsertPointTy AllocaIP,
- OpenMPIRBuilder::InsertPointTy CodeGenIP,
+ OpenMPIRBuilder::InsertPointTy CodeGenIP, TargetDataInfo &Info,
TargetRegionEntryInfo &EntryInfo,
const TargetKernelDefaultAttrs &DefaultAttrs,
const TargetKernelRuntimeAttrs &RuntimeAttrs, Value *IfCond,
SmallVectorImpl<Value *> &Inputs, GenMapInfoCallbackTy GenMapInfoCB,
TargetBodyGenCallbackTy BodyGenCB,
TargetGenArgAccessorsCallbackTy ArgAccessorFuncCB,
+ function_ref<Value *(unsigned int)> CustomMapperCB,
SmallVector<DependData> Dependencies = {}, bool HasNowait = false);
/// Returns __kmpc_for_static_init_* runtime function for the specified
diff --git a/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp b/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
index 91fc16e54c88fc..9c008aa4f967a6 100644
--- a/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
+++ b/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
@@ -6549,12 +6549,12 @@ OpenMPIRBuilder::InsertPointOrErrorTy OpenMPIRBuilder::createTargetData(
const LocationDescription &Loc, InsertPointTy AllocaIP,
InsertPointTy CodeGenIP, Value *DeviceID, Value *IfCond,
TargetDataInfo &Info, GenMapInfoCallbackTy GenMapInfoCB,
+ function_ref<Value *(unsigned int)> CustomMapperCB,
omp::RuntimeFunction *MapperFunc,
function_ref<InsertPointOrErrorTy(InsertPointTy CodeGenIP,
BodyGenTy BodyGenType)>
BodyGenCB,
- function_ref<void(unsigned int, Value *)> DeviceAddrCB,
- function_ref<Value *(unsigned int)> CustomMapperCB, Value *SrcLocInfo) {
+ function_ref<void(unsigned int, Value *)> DeviceAddrCB, Value *SrcLocInfo) {
if (!updateToLocation(Loc))
return InsertPointTy();
@@ -6580,8 +6580,8 @@ OpenMPIRBuilder::InsertPointOrErrorTy OpenMPIRBuilder::createTargetData(
InsertPointTy CodeGenIP) -> Error {
MapInfo = &GenMapInfoCB(Builder.saveIP());
emitOffloadingArrays(AllocaIP, Builder.saveIP(), *MapInfo, Info,
- /*IsNonContiguous=*/true, DeviceAddrCB,
- CustomMapperCB);
+ CustomMapperCB,
+ /*IsNonContiguous=*/true, DeviceAddrCB);
TargetDataRTArgs RTArgs;
emitOffloadingArraysArgument(Builder, RTArgs, Info);
@@ -7394,24 +7394,26 @@ OpenMPIRBuilder::InsertPointOrErrorTy OpenMPIRBuilder::emitTargetTask(
void OpenMPIRBuilder::emitOffloadingArraysAndArgs(
InsertPointTy AllocaIP, InsertPointTy CodeGenIP, TargetDataInfo &Info,
- TargetDataRTArgs &RTArgs, MapInfosTy &CombinedInfo, bool IsNonContiguous,
- bool ForEndCall, function_ref<void(unsigned int, Value *)> DeviceAddrCB,
- function_ref<Value *(unsigned int)> CustomMapperCB) {
- emitOffloadingArrays(AllocaIP, CodeGenIP, CombinedInfo, Info, IsNonContiguous,
- DeviceAddrCB, CustomMapperCB);
+ TargetDataRTArgs &RTArgs, MapInfosTy &CombinedInfo,
+ function_ref<Value *(unsigned int)> CustomMapperCB, bool IsNonContiguous,
+ bool ForEndCall, function_ref<void(unsigned int, Value *)> DeviceAddrCB) {
+ emitOffloadingArrays(AllocaIP, CodeGenIP, CombinedInfo, Info, CustomMapperCB,
+ IsNonContiguous, DeviceAddrCB);
emitOffloadingArraysArgument(Builder, RTArgs, Info, ForEndCall);
}
static void
emitTargetCall(OpenMPIRBuilder &OMPBuilder, IRBuilderBase &Builder,
OpenMPIRBuilder::InsertPointTy AllocaIP,
+ OpenMPIRBuilder::TargetDataInfo &Info,
const OpenMPIRBuilder::TargetKernelDefaultAttrs &DefaultAttrs,
const OpenMPIRBuilder::TargetKernelRuntimeAttrs &RuntimeAttrs,
Value *IfCond, Function *OutlinedFn, Constant *OutlinedFnID,
SmallVectorImpl<Value *> &Args,
OpenMPIRBuilder::GenMapInfoCallbackTy GenMapInfoCB,
- SmallVector<llvm::OpenMPIRBuilder::DependData> Dependencies = {},
- bool HasNoWait = false) {
+ function_ref<Value *(unsigned int)> CustomMapperCB,
+ SmallVector<llvm::OpenMPIRBuilder::DependData> Dependencies,
+ bool HasNoWait) {
// Generate a function call to the host fallback implementation of the target
// region. This is called by the host when no offload entry was generated for
// the target region and when the offloading call fails at runtime.
@@ -7489,7 +7491,7 @@ emitTargetCall(OpenMPIRBuilder &OMPBuilder, IRBuilderBase &Builder,
OpenMPIRBuilder::MapInfosTy &MapInfo = GenMapInfoCB(Builder.saveIP());
OpenMPIRBuilder::TargetDataRTArgs RTArgs;
OMPBuilder.emitOffloadingArraysAndArgs(AllocaIP, Builder.saveIP(), Info,
- RTArgs, MapInfo,
+ RTArgs, MapInfo, CustomMapperCB,
/*IsNonContiguous=*/true,
/*ForEndCall=*/false);
@@ -7593,12 +7595,14 @@ emitTargetCall(OpenMPIRBuilder &OMPBuilder, IRBuilderBase &Builder,
OpenMPIRBuilder::InsertPointOrErrorTy OpenMPIRBuilder::createTarget(
const LocationDescription &Loc, bool IsOffloadEntry, InsertPointTy AllocaIP,
- InsertPointTy CodeGenIP, TargetRegionEntryInfo &EntryInfo,
+ InsertPointTy CodeGenIP, TargetDataInfo &Info,
+ TargetRegionEntryInfo &EntryInfo,
const TargetKernelDefaultAttrs &DefaultAttrs,
const TargetKernelRuntimeAttrs &RuntimeAttrs, Value *IfCond,
- SmallVectorImpl<Value *> &Args, GenMapInfoCallbackTy GenMapInfoCB,
+ SmallVectorImpl<Value *> &Inputs, GenMapInfoCallbackTy GenMapInfoCB,
OpenMPIRBuilder::TargetBodyGenCallbackTy CBFunc,
OpenMPIRBuilder::TargetGenArgAccessorsCallbackTy ArgAccessorFuncCB,
+ function_ref<Value *(unsigned int)> CustomMapperCB,
SmallVector<DependData> Dependencies, bool HasNowait) {
if (!updateToLocation(Loc))
@@ -7613,16 +7617,16 @@ OpenMPIRBuilder::InsertPointOrErrorTy OpenMPIRBuilder::createTarget(
// and ArgAccessorFuncCB
if (Error Err = emitTargetOutlinedFunction(
*this, Builder, IsOffloadEntry, EntryInfo, DefaultAttrs, OutlinedFn,
- OutlinedFnID, Args, CBFunc, ArgAccessorFuncCB))
+ OutlinedFnID, Inputs, CBFunc, ArgAccessorFuncCB))
return Err;
// If we are not on the target device, then we need to generate code
// to make a remote call (offload) to the previously outlined function
// that represents the target region. Do that now.
if (!Config.isTargetDevice())
- emitTargetCall(*this, Builder, AllocaIP, DefaultAttrs, RuntimeAttrs, IfCond,
- OutlinedFn, OutlinedFnID, Args, GenMapInfoCB, Dependencies,
- HasNowait);
+ emitTargetCall(*this, Builder, AllocaIP, Info, DefaultAttrs, RuntimeAttrs,
+ IfCond, OutlinedFn, OutlinedFnID, Inputs, GenMapInfoCB,
+ CustomMapperCB, Dependencies, HasNowait);
return Builder.saveIP();
}
@@ -7947,9 +7951,9 @@ void OpenMPIRBuilder::emitUDMapperArrayInitOrDel(
OffloadingArgs);
}
-Function *OpenMPIRBuilder::emitUserDefinedMapper(
- function_ref<MapInfosTy &(InsertPointTy CodeGenIP, llvm::Value *PtrPHI,
- llvm::Value *BeginArg)>
+Expected<Function *> OpenMPIRBuilder::emitUserDefinedMapper(
+ function_ref<MapInfosOrErrorTy(InsertPointTy CodeGenIP, llvm::Value *PtrPHI,
+ llvm::Value *BeginArg)>
GenMapInfoCB,
Type *ElemTy, StringRef FuncName,
function_ref<bool(unsigned int, Function **)> CustomMapperCB) {
@@ -8023,7 +8027,9 @@ Function *OpenMPIRBuilder::emitUserDefinedMapper(
PtrPHI->addIncoming(PtrBegin, HeadBB);
// Get map clause information. Fill up the arrays with all mapped variables.
- MapInfosTy &Info = GenMapInfoCB(Builder.saveIP(), PtrPHI, BeginIn);
+ MapInfosOrErrorTy Info = GenMapInfoCB(Builder.saveIP(), PtrPHI, BeginIn);
+ if (!Info)
+ return Info.takeError();
// Call the runtime API __tgt_mapper_num_components to get the number of
// pre-existing components.
@@ -8035,20 +8041,20 @@ Function *OpenMPIRBuilder::emitUserDefinedMapper(
Builder.CreateShl(PreviousSize, Builder.getInt64(getFlagMemberOffset()));
// Fill up the runtime mapper handle for all components.
- for (unsigned I = 0; I < Info.BasePointers.size(); ++I) {
+ for (unsigned I = 0; I < Info->BasePointers.size(); ++I) {
Value *CurBaseArg =
- Builder.CreateBitCast(Info.BasePointers[I], Builder.getPtrTy());
+ Builder.CreateBitCast(Info->BasePointers[I], Builder.getPtrTy());
Value *CurBeginArg =
- Builder.CreateBitCast(Info.Pointers[I], Builder.getPtrTy());
- Value *CurSizeArg = Info.Sizes[I];
- Value *CurNameArg = Info.Names.size()
- ? Info.Names[I]
+ Builder.CreateBitCast(Info->Pointers[I], Builder.getPtrTy());
+ Value *CurSizeArg = Info->Sizes[I];
+ Value *CurNameArg = Info->Names.size()
+ ? Info->Names[I]
: Constant::getNullValue(Builder.getPtrTy());
// Extract the MEMBER_OF field from the map type.
Value *OriMapType = Builder.getInt64(
static_cast<std::underlying_type_t<OpenMPOffloadMappingFlags>>(
- Info.Types[I]));
+ Info->Types[I]));
Value *MemberMapType =
Builder.CreateNUWAdd(OriMapType, ShiftedPreviousSize);
@@ -8169,9 +8175,9 @@ Function *OpenMPIRBuilder::emitUserDefinedMapper(
void OpenMPIRBuilder::emitOffloadingArrays(
InsertPointTy AllocaIP, InsertPointTy CodeGenIP, MapInfosTy &CombinedInfo,
- TargetDataInfo &Info, bool IsNonContiguous,
- function_ref<void(unsigned int, Value *)> DeviceAddrCB,
- function_ref<Value *(unsigned int)> CustomMapperCB) {
+ TargetDataInfo &Info, function_ref<Value *(unsigned int)> CustomMapperCB,
+ bool IsNonContiguous,
+ function_ref<void(unsigned int, Value *)> DeviceAddrCB) {
// Reset the array information.
Info.clearArrayInfo();
diff --git a/llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp b/llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
index 83c8f7e932b2b6..2b9ed38911a2ad 100644
--- a/llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
+++ b/llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
@@ -5928,6 +5928,7 @@ TEST_F(OpenMPIRBuilderTest, TargetEnterData) {
return CombinedInfo;
};
+ auto CustomMapperCB = [&](unsigned int I) { return nullptr; };
llvm::OpenMPIRBuilder::TargetDataInfo Info(
/*RequiresDevicePointerInfo=*/false,
/*SeparateBeginEndCalls=*/true);
@@ -5939,7 +5940,7 @@ TEST_F(OpenMPIRBuilderTest, TargetEnterData) {
OpenMPIRBuilder::InsertPointTy, AfterIP,
OMPBuilder.createTargetData(
Loc, AllocaIP, Builder.saveIP(), Builder.getInt64(DeviceID),
- /* IfCond= */ nullptr, Info, GenMapInfoCB, &RTLFunc));
+ /* IfCond= */ nullptr, Info, GenMapInfoCB, CustomMapperCB, &RTLFunc));
Builder.restoreIP(AfterIP);
CallInst *TargetDataCall = dyn_cast<CallInst>(&BB->back());
@@ -5990,6 +5991,7 @@ TEST_F(OpenMPIRBuilderTest, TargetExitData) {
return CombinedInfo;
};
+ auto CustomMapperCB = [&](unsigned int I) { return nullptr; };
llvm::OpenMPIRBuilder::TargetDataInfo Info(
/*RequiresDevicePointerInfo=*/false,
/*SeparateBeginEndCalls=*/true);
@@ -6001,7 +6003,7 @@ TEST_F(OpenMPIRBuilderTest, TargetExitData) {
OpenMPIRBuilder::InsertPointTy, AfterIP,
OMPBuilder.createTargetData(
Loc, AllocaIP, Builder.saveIP(), Builder.getInt64(DeviceID),
- /* IfCond= */ nullptr, Info, GenMapInfoCB, &RTLFunc));
+ /* IfCond= */ nullptr, Info, GenMapInfoCB, CustomMapperCB, &RTLFunc));
Builder.restoreIP(AfterIP);
CallInst *TargetDataCall = dyn_cast<CallInst>(&BB->back());
@@ -6074,6 +6076,7 @@ TEST_F(OpenMPIRBuilderTest, TargetDataRegion) {
return CombinedInfo;
};
+ auto CustomMapperCB = [&](unsigned int I) { return nullptr; };
llvm::OpenMPIRBuilder::TargetDataInfo Info(
/*RequiresDevicePointerInfo=*/true,
/*SeparateBeginEndCalls=*/true);
@@ -6110,9 +6113,10 @@ TEST_F(OpenMPIRBuilderTest, TargetDataRegion) {
ASSERT_EXPECTED_INIT(
OpenMPIRBuilder::InsertPointTy, TargetDataIP1,
- OMPBuilder.createTargetData(
- Loc, AllocaIP, Builder.saveIP(), Builder.getInt64(DeviceID),
- /* IfCond= */ nullptr, Info, GenMapInfoCB, nullptr, BodyCB));
+ OMPBuilder.createTargetData(Loc, AllocaIP, Builder.saveIP(),
+ Builder.getInt64(DeviceID),
+ /* IfCond= */ nullptr, Info, GenMapInfoCB,
+ CustomMapperCB, nullptr, BodyCB));
Builder.restoreIP(TargetDataIP1);
CallInst *TargetDataCall = dyn_cast<CallInst>(&BB->back());
@@ -6138,9 +6142,10 @@ TEST_F(OpenMPIRBuilderTest, TargetDataRegion) {
};
ASSERT_EXPECTED_INIT(
OpenMPIRBuilder::InsertPointTy, TargetDataIP2,
- OMPBuilder.createTargetData(
- Loc, AllocaIP, Builder.saveIP(), Builder.getInt64(DeviceID),
- /* IfCond= */ nullptr, Info, GenMapInfoCB, nullptr, BodyTargetCB));
+ OMPBuilder.createTargetData(Loc, AllocaIP, Builder.saveIP(),
+ Builder.getInt64(DeviceID),
+ /* IfCond= */ nullptr, Info, GenMapInfoCB,
+ CustomMapperCB, nullptr, BodyTargetCB));
Builder.restoreIP(TargetDataIP2);
EXPECT_TRUE(CheckDevicePassBodyGen);
@@ -6241,6 +6246,10 @@ TEST_F(OpenMPIRBuilderTest, TargetRegion) {
return CombinedInfos;
};
+ auto CustomMapperCB = [&](unsigned int I) { return nullptr; };
+ llvm::OpenMPIRBuilder::TargetDataInfo Info(/*RequiresDevicePointerInfo=*/true,
+ /*SeparateBeginEndCalls=*/true);
+
TargetRegionEntryInfo EntryInfo("func", 42, 4711, 17);
OpenMPIRBuilder::LocationDescription OmpLoc({Builder.saveIP(), DL});
OpenMPIRBuilder::TargetKernelRuntimeAttrs RuntimeAttrs;
@@ -6254,9 +6263,10 @@ TEST_F(OpenMPIRBuilderTest, TargetRegion) {
ASSERT_EXPECTED_INIT(
OpenMPIRBuilder::InsertPointTy, AfterIP,
OMPBuilder.createTarget(OmpLoc, /*IsOffloadEntry=*/true, Builder.saveIP(),
- Builder.saveIP(), EntryInfo, DefaultAttrs,
+ Builder.saveIP(), Info, EntryInfo, DefaultAttrs,
RuntimeAttrs, /*IfCond=*/nullptr, Inputs,
- GenMapInfoCB, BodyGenCB, SimpleArgAccessorCB));
+ GenMapInfoCB, BodyGenCB, SimpleArgAccessorCB,
+ CustomMapperCB, {}, false));
EXPECT_EQ(DL, Builder.getCurrentDebugLocation());
Builder.restoreIP(AfterIP);
@@ -6400,6 +6410,7 @@ TEST_F(OpenMPIRBuilderTest, TargetRegionDevice) {
return CombinedInfos;
};
+ auto CustomMapperCB = [&](unsigned int I) { return nullptr; };
auto BodyGenCB = [&](OpenMPIRBuilder::InsertPointTy AllocaIP,
OpenMPIRBuilder::InsertPointTy CodeGenIP)
-> OpenMPIRBuilder::InsertPointTy {
@@ -6419,13 +6430,16 @@ TEST_F(OpenMPIRBuilderTest, TargetRegionDevice) {
OpenMPIRBuilder::TargetKernelDefaultAttrs DefaultAttrs = {
/*ExecFlags=*/omp::OMPTgtExecModeFlags::OMP_TGT_EXEC_MODE_GENERIC,
/*MaxTeams=*/{-1}, /*MinTeams=*/0, /*MaxThreads=*/{0}, /*MinThreads=*/0};
+ llvm::OpenMPIRBuilder::TargetDataInfo Info(/*RequiresDevicePointerInfo=*/true,
+ /*SeparateBeginEndCalls=*/true);
ASSERT_EXPECTED_INIT(
OpenMPIRBuilder::InsertPointTy, AfterIP,
OMPBuilder.createTarget(Loc, /*IsOffloadEntry=*/true, EntryIP, EntryIP,
- EntryInfo, DefaultAttrs, RuntimeAttrs,
+ Info, EntryInfo, DefaultAttrs, RuntimeAttrs,
/*IfCond=*/nullptr, CapturedArgs, GenMapInfoCB,
- BodyGenCB, SimpleArgAccessorCB));
+ BodyGenCB, SimpleArgAccessorCB, CustomMapperCB,
+ {}, false));
EXPECT_EQ(DL, Builder.getCurrentDebugLocation());
Builder.restoreIP(AfterIP);
@@ -6777,6 +6791,7 @@ TEST_F(OpenMPIRBuilderTest, ConstantAllocaRaise) {
return CombinedInfos;
};
+ auto CustomMapperCB = [&](unsigned int I) { return nullptr; };
llvm::Value *RaiseAlloca = nullptr;
auto BodyGenCB = [&](OpenMPIRBuilder::InsertPointTy AllocaIP,
@@ -6799,13 +6814,16 @@ TEST_F(OpenMPIRBuilderTest, ConstantAllocaRaise) {
OpenMPIRBuilder::TargetKernelDefaultAttrs DefaultAttrs = {
/*ExecFlags=*/omp::OMPTgtExecModeFlags::OMP_TGT_EXEC_MODE_GENERIC,
/*MaxTeams=*/{-1}, /*MinTeams=*/0, /*MaxThreads=*/{0}, /*MinThreads=*/0};
+ llvm::OpenMPIRBuilder::TargetDataInfo Info(/*RequiresDevicePointerInfo=*/true,
+ /*SeparateBeginEndCalls=*/true);
ASSERT_EXPECTED_INIT(
OpenMPIRBuilder::InsertPointTy, AfterIP,
OMPBuilder.createTarget(Loc, /*IsOffloadEntry=*/true, EntryIP, EntryIP,
- EntryInfo, DefaultAttrs, RuntimeAttrs,
+ Info, EntryInfo, DefaultAttrs, RuntimeAttrs,
/*IfCond=*/nullptr, CapturedArgs, GenMapInfoCB,
- BodyGenCB, SimpleArgAccessorCB));
+ BodyGenCB, SimpleArgAccessorCB, CustomMapperCB,
+ {}, false));
EXPECT_EQ(DL, Builder.getCurrentDebugLocation());
Builder.restoreIP(AfterIP);
diff --git a/mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp b/mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp
index 51a3cbdbb5e7fd..be546d47c3879c 100644
--- a/mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp
+++ b/mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp
@@ -2809,13 +2809,23 @@ getRefPtrIfDeclareTarget(mlir::Value value,
}
namespace {
+// Append customMappers information to existing MapInfosTy
+struct MapInfosTy : llvm::OpenMPIRBuilder::MapInfosTy {
+ SmallVector<Operation *, 4> Mappers;
+
+ /// Append arrays in \a CurInfo.
+ void append(MapInfosTy &curInfo) {
+ Mappers.append(curInfo.Mappers.begin(), curInfo.Mappers.end());
+ llvm::OpenMPIRBuilder::MapInfosTy::append(curInfo);
+ }
+};
// A small helper structure to contain data gathered
// for map lowering and coalese it into one area and
// avoiding extra computations such as searches in the
// llvm module for lowered mapped variables or checking
// if something is declare target (and retrieving the
// value) more than neccessary.
-struct MapInfoData : llvm::OpenMPIRBuilder::MapInfosTy {
+struct MapInfoData : MapInfosTy {
llvm::SmallVector<bool, 4> IsDeclareTarget;
llvm::SmallVector<bool, 4> IsAMember;
// Identify if mapping was added by mapClause or use_device clauses.
@@ -2834,7 +2844,7 @@ struct MapInfoData : llvm::OpenMPIRBuilder::MapInfosTy {
OriginalValue.append(CurInfo.OriginalValue.begin(),
CurInfo.OriginalValue.end());
BaseType.append(CurInfo.BaseType.begin(), CurInfo.BaseType.end());
- llvm::OpenMPIRBuilder::MapInfosTy::append(CurInfo);
+ MapInfosTy::append(CurInfo);
}
};
} // namespace
@@ -2955,6 +2965,12 @@ static void collectMapDataFromMapOperands(
mapData.Names.push_back(LLVM::createMappingInformation(
mapOp.getLoc(), *moduleTranslation.getOpenMPBuilder()));
mapData.DevicePointers.push_back(llvm::OpenMPIRBuilder::DeviceInfoTy::None);
+ if (mapOp.getMapperId())
+ mapData.Mappers.push_back(
+ SymbolTable::lookupNearestSymbolFrom<omp::DeclareMapperOp>(
+ mapOp, mapOp.getMapperIdAttr()));
+ else
+ mapData.Mappers.push_back(nullptr);
mapData.IsAMapping.push_back(true);
mapData.IsAMember.push_back(checkIsAMember(mapVars, mapOp));
}
@@ -2999,6 +3015,7 @@ static void collectMapDataFromMapOperands(
mapData.Names.push_back(LLVM::createMappingInformation(
mapOp.getLoc(), *moduleTranslation.getOpenMPBuilder()));
mapData.DevicePointers.push_back(devInfoTy);
+ mapData.Mappers.push_back(nullptr);
mapData.IsAMapping.push_back(false);
mapData.IsAMember.push_back(checkIsAMember(useDevOperands, mapOp));
}
@@ -3164,9 +3181,8 @@ calculateBoundsOffset(LLVM::ModuleTranslation &moduleTranslation,
// inside of CGOpenMPRuntime.cpp
static llvm::omp::OpenMPOffloadMappingFlags mapParentWithMembers(
LLVM::ModuleTranslation &moduleTranslation, llvm::IRBuilderBase &builder,
- llvm::OpenMPIRBuilder &ompBuilder, DataLayout &dl,
- llvm::OpenMPIRBuilder::MapInfosTy &combinedInfo, MapInfoData &mapData,
- uint64_t mapDataIndex, bool isTargetParams) {
+ llvm::OpenMPIRBuilder &ompBuilder, DataLayout &dl, MapInfosTy &combinedInfo,
+ MapInfoData &mapData, uint64_t mapDataIndex, bool isTargetParams) {
// Map the first segment of our structure
combinedInfo.Types.emplace_back(
isTargetParams
@@ -3174,6 +3190,7 @@ static llvm::omp::OpenMPOffloadMappingFlags mapParentWithMembers(
: llvm::omp::OpenMPOffloadMappingFlags::OMP_MAP_NONE);
combinedInfo.DevicePointers.emplace_back(
mapData.DevicePointers[mapDataIndex]);
+ combinedInfo.Mappers.emplace_back(mapData.Mappers[mapDataIndex]);
combinedInfo.Names.emplace_back(LLVM::createMappingInformation(
mapData.MapClause[mapDataIndex]->getLoc(), ompBuilder));
combinedInfo.BasePointers.emplace_back(mapData.BasePointers[mapDataIndex]);
@@ -3237,6 +3254,7 @@ static llvm::omp::OpenMPOffloadMappingFlags mapParentWithMembers(
combinedInfo.Types.emplace_back(mapFlag);
combinedInfo.DevicePointers.emplace_back(
llvm::OpenMPIRBuilder::DeviceInfoTy::None);
+ combinedInfo.Mappers.emplace_back(nullptr);
combinedInfo.Names.emplace_back(LLVM::createMappingInformation(
mapData.MapClause[mapDataIndex]->getLoc(), ompBuilder));
combinedInfo.BasePointers.emplace_back(mapData.BasePointers[mapDataIndex]);
@@ -3270,9 +3288,9 @@ static bool checkIfPointerMap(omp::MapInfoOp mapOp) {
// This function is intended to add explicit mappings of members
static void processMapMembersWithParent(
LLVM::ModuleTranslation &moduleTranslation, llvm::IRBuilderBase &builder,
- llvm::OpenMPIRBuilder &ompBuilder, DataLayout &dl,
- llvm::OpenMPIRBuilder::MapInfosTy &combinedInfo, MapInfoData &mapData,
- uint64_t mapDataIndex, llvm::omp::OpenMPOffloadMappingFlags memberOfFlag) {
+ llvm::OpenMPIRBuilder &ompBuilder, DataLayout &dl, MapInfosTy &combinedInfo,
+ MapInfoData &mapData, uint64_t mapDataIndex,
+ llvm::omp::OpenMPOffloadMappingFlags memberOfFlag) {
auto parentClause =
llvm::cast<omp::MapInfoOp>(mapData.MapClause[mapDataIndex]);
@@ -3300,6 +3318,7 @@ static void processMapMembersWithParent(
combinedInfo.Types.emplace_back(mapFlag);
combinedInfo.DevicePointers.emplace_back(
llvm::OpenMPIRBuilder::DeviceInfoTy::None);
+ combinedInfo.Mappers.emplace_back(nullptr);
combinedInfo.Names.emplace_back(
LLVM::createMappingInformation(memberClause.getLoc(), ompBuilder));
combinedInfo.BasePointers.emplace_back(
@@ -3322,6 +3341,7 @@ static void processMapMembersWithParent(
combinedInfo.Types.emplace_back(mapFlag);
combinedInfo.DevicePointers.emplace_back(
mapData.DevicePointers[memberDataIdx]);
+ combinedInfo.Mappers.emplace_back(mapData.Mappers[memberDataIdx]);
combinedInfo.Names.emplace_back(
LLVM::createMappingInformation(memberClause.getLoc(), ompBuilder));
uint64_t basePointerIndex =
@@ -3341,10 +3361,9 @@ static void processMapMembersWithParent(
}
}
-static void
-processIndividualMap(MapInfoData &mapData, size_t mapDataIdx,
- llvm::OpenMPIRBuilder::MapInfosTy &combinedInfo,
- bool isTargetParams, int mapDataParentIdx = -1) {
+static void processIndividualMap(MapInfoData &mapData, size_t mapDataIdx,
+ MapInfosTy &combinedInfo, bool isTargetParams,
+ int mapDataParentIdx = -1) {
// Declare Target Mappings are excluded from being marked as
// OMP_MAP_TARGET_PARAM as they are not passed as parameters, they're
// marked with OMP_MAP_PTR_AND_OBJ instead.
@@ -3374,16 +3393,18 @@ processIndividualMap(MapInfoData &mapData, size_t mapDataIdx,
combinedInfo.Pointers.emplace_back(mapData.Pointers[mapDataIdx]);
combinedInfo.DevicePointers.emplace_back(mapData.DevicePointers[mapDataIdx]);
+ combinedInfo.Mappers.emplace_back(mapData.Mappers[mapDataIdx]);
combinedInfo.Names.emplace_back(mapData.Names[mapDataIdx]);
combinedInfo.Types.emplace_back(mapFlag);
combinedInfo.Sizes.emplace_back(mapData.Sizes[mapDataIdx]);
}
-static void processMapWithMembersOf(
- LLVM::ModuleTranslation &moduleTranslation, llvm::IRBuilderBase &builder,
- llvm::OpenMPIRBuilder &ompBuilder, DataLayout &dl,
- llvm::OpenMPIRBuilder::MapInfosTy &combinedInfo, MapInfoData &mapData,
- uint64_t mapDataIndex, bool isTargetParams) {
+static void processMapWithMembersOf(LLVM::ModuleTranslation &moduleTranslation,
+ llvm::IRBuilderBase &builder,
+ llvm::OpenMPIRBuilder &ompBuilder,
+ DataLayout &dl, MapInfosTy &combinedInfo,
+ MapInfoData &mapData, uint64_t mapDataIndex,
+ bool isTargetParams) {
auto parentClause =
llvm::cast<omp::MapInfoOp>(mapData.MapClause[mapDataIndex]);
@@ -3488,8 +3509,7 @@ createAlteredByCaptureMap(MapInfoData &mapData,
// Generate all map related information and fill the combinedInfo.
static void genMapInfos(llvm::IRBuilderBase &builder,
LLVM::ModuleTranslation &moduleTranslation,
- DataLayout &dl,
- llvm::OpenMPIRBuilder::MapInfosTy &combinedInfo,
+ DataLayout &dl, MapInfosTy &combinedInfo,
MapInfoData &mapData, bool isTargetParams = false) {
// We wish to modify some of the methods in which arguments are
// passed based on their capture type by the target region, this can
@@ -3529,6 +3549,85 @@ static void genMapInfos(llvm::IRBuilderBase &builder,
}
}
+static llvm::Expected<llvm::Function *>
+emitUserDefinedMapper(Operation *declMapperOp, llvm::IRBuilderBase &builder,
+ LLVM::ModuleTranslation &moduleTranslation);
+
+static llvm::Expected<llvm::Function *>
+getOrCreateUserDefinedMapperFunc(Operation *declMapperOp,
+ llvm::IRBuilderBase &builder,
+ LLVM::ModuleTranslation &moduleTranslation) {
+ llvm::DenseMap<const Operation *, llvm::Function *> userDefMapperMap;
+ auto iter = userDefMapperMap.find(declMapperOp);
+ if (iter != userDefMapperMap.end())
+ return iter->second;
+ llvm::Expected<llvm::Function *> mapperFunc =
+ emitUserDefinedMapper(declMapperOp, builder, moduleTranslation);
+ if (!mapperFunc)
+ return mapperFunc.takeError();
+ userDefMapperMap.try_emplace(declMapperOp, *mapperFunc);
+ return userDefMapperMap.lookup(declMapperOp);
+}
+
+static llvm::Expected<llvm::Function *>
+emitUserDefinedMapper(Operation *op, llvm::IRBuilderBase &builder,
+ LLVM::ModuleTranslation &moduleTranslation) {
+ auto declMapperOp = cast<omp::DeclareMapperOp>(op);
+ auto declMapperInfoOp =
+ *declMapperOp.getOps<omp::DeclareMapperInfoOp>().begin();
+ DataLayout dl = DataLayout(declMapperOp->getParentOfType<ModuleOp>());
+ llvm::OpenMPIRBuilder *ompBuilder = moduleTranslation.getOpenMPBuilder();
+ llvm::Type *varType =
+ moduleTranslation.convertType(declMapperOp.getVarType());
+ std::string mapperName = ompBuilder->createPlatformSpecificName(
+ {"omp_mapper", declMapperOp.getSymName()});
+ SmallVector<Value> mapVars = declMapperInfoOp.getMapVars();
+
+ using InsertPointTy = llvm::OpenMPIRBuilder::InsertPointTy;
+
+ // Fill up the arrays with all the mapped variables.
+ MapInfosTy combinedInfo;
+ auto genMapInfoCB =
+ [&](InsertPointTy codeGenIP, llvm::Value *ptrPHI,
+ llvm::Value *unused2) -> llvm::OpenMPIRBuilder::MapInfosOrErrorTy {
+ builder.restoreIP(codeGenIP);
+ moduleTranslation.mapValue(declMapperOp.getRegion().getArgument(0), ptrPHI);
+ moduleTranslation.mapBlock(&declMapperOp.getRegion().front(),
+ builder.GetInsertBlock());
+ if (failed(moduleTranslation.convertBlock(declMapperOp.getRegion().front(),
+ /*ignoreArguments=*/true,
+ builder)))
+ return llvm::make_error<PreviouslyReportedError>();
+ MapInfoData mapData;
+ collectMapDataFromMapOperands(mapData, mapVars, moduleTranslation, dl,
+ builder);
+ genMapInfos(builder, moduleTranslation, dl, combinedInfo, mapData);
+
+ // Drop the mapping that is no longer necessary so that the same region can
+ // be processed multiple times.
+ moduleTranslation.forgetMapping(declMapperOp.getRegion());
+ return combinedInfo;
+ };
+
+ auto customMapperCB = [&](unsigned i, llvm::Function **mapperFunc) {
+ if (combinedInfo.Mappers[i]) {
+ // Call the corresponding mapper function.
+ llvm::Expected<llvm::Function *> newFn = getOrCreateUserDefinedMapperFunc(
+ combinedInfo.Mappers[i], builder, moduleTranslation);
+ assert(newFn && "Expect a valid mapper function is available");
+ *mapperFunc = *newFn;
+ return true;
+ }
+ return false;
+ };
+
+ llvm::Expected<llvm::Function *> newFn = ompBuilder->emitUserDefinedMapper(
+ genMapInfoCB, varType, mapperName, customMapperCB);
+ if (!newFn)
+ return newFn.takeError();
+ return *newFn;
+}
+
static LogicalResult
convertOmpTargetData(Operation *op, llvm::IRBuilderBase &builder,
LLVM::ModuleTranslation &moduleTranslation) {
@@ -3640,9 +3739,8 @@ convertOmpTargetData(Operation *op, llvm::IRBuilderBase &builder,
builder, useDevicePtrVars, useDeviceAddrVars);
// Fill up the arrays with all the mapped variables.
- llvm::OpenMPIRBuilder::MapInfosTy combinedInfo;
- auto genMapInfoCB =
- [&](InsertPointTy codeGenIP) -> llvm::OpenMPIRBuilder::MapInfosTy & {
+ MapInfosTy combinedInfo;
+ auto genMapInfoCB = [&](InsertPointTy codeGenIP) -> MapInfosTy & {
builder.restoreIP(codeGenIP);
genMapInfos(builder, moduleTranslation, DL, combinedInfo, mapData);
return combinedInfo;
@@ -3685,6 +3783,7 @@ convertOmpTargetData(Operation *op, llvm::IRBuilderBase &builder,
using BodyGenTy = llvm::OpenMPIRBuilder::BodyGenTy;
auto bodyGenCB = [&](InsertPointTy codeGenIP, BodyGenTy bodyGenType)
-> llvm::OpenMPIRBuilder::InsertPointOrErrorTy {
+ builder.restoreIP(codeGenIP);
assert(isa<omp::TargetDataOp>(op) &&
"BodyGen requested for non TargetDataOp");
auto blockArgIface = cast<omp::BlockArgOpenMPOpInterface>(op);
@@ -3693,8 +3792,6 @@ convertOmpTargetData(Operation *op, llvm::IRBuilderBase &builder,
case BodyGenTy::Priv:
// Check if any device ptr/addr info is available
if (!info.DevicePtrInfoMap.empty()) {
- builder.restoreIP(codeGenIP);
-
mapUseDevice(llvm::OpenMPIRBuilder::DeviceInfoTy::Address,
blockArgIface.getUseDeviceAddrBlockArgs(),
useDeviceAddrVars, mapData,
@@ -3724,7 +3821,6 @@ convertOmpTargetData(Operation *op, llvm::IRBuilderBase &builder,
case BodyGenTy::NoPriv:
// If device info is available then region has already been generated
if (info.DevicePtrInfoMap.empty()) {
- builder.restoreIP(codeGenIP);
// For device pass, if use_device_ptr(addr) mappings were present,
// we need to link them here before codegen.
if (ompBuilder->Config.IsTargetDevice.value_or(false)) {
@@ -3745,6 +3841,18 @@ convertOmpTargetData(Operation *op, llvm::IRBuilderBase &builder,
return builder.saveIP();
};
+ auto customMapperCB = [&](unsigned int i) {
+ llvm::Function *mapperFunc = nullptr;
+ if (combinedInfo.Mappers[i]) {
+ info.HasMapper = true;
+ llvm::Expected<llvm::Function *> newFn = getOrCreateUserDefinedMapperFunc(
+ combinedInfo.Mappers[i], builder, moduleTranslation);
+ assert(newFn && "Expect a valid mapper function is available");
+ mapperFunc = *newFn;
+ }
+ return mapperFunc;
+ };
+
llvm::OpenMPIRBuilder::LocationDescription ompLoc(builder);
llvm::OpenMPIRBuilder::InsertPointTy allocaIP =
findAllocaInsertPoint(builder, moduleTranslation);
@@ -3752,10 +3860,11 @@ convertOmpTargetData(Operation *op, llvm::IRBuilderBase &builder,
if (isa<omp::TargetDataOp>(op))
return ompBuilder->createTargetData(
ompLoc, allocaIP, builder.saveIP(), builder.getInt64(deviceID),
- ifCond, info, genMapInfoCB, nullptr, bodyGenCB);
- return ompBuilder->createTargetData(ompLoc, allocaIP, builder.saveIP(),
- builder.getInt64(deviceID), ifCond,
- info, genMapInfoCB, &RTLFn);
+ ifCond, info, genMapInfoCB, customMapperCB, nullptr, bodyGenCB,
+ /*DeviceAddrCB=*/nullptr);
+ return ompBuilder->createTargetData(
+ ompLoc, allocaIP, builder.saveIP(), builder.getInt64(deviceID), ifCond,
+ info, genMapInfoCB, customMapperCB, &RTLFn);
}();
if (failed(handleError(afterIP, *op)))
@@ -4367,9 +4476,9 @@ convertOmpTarget(Operation &opInst, llvm::IRBuilderBase &builder,
collectMapDataFromMapOperands(mapData, mapVars, moduleTranslation, dl,
builder);
- llvm::OpenMPIRBuilder::MapInfosTy combinedInfos;
- auto genMapInfoCB = [&](llvm::OpenMPIRBuilder::InsertPointTy codeGenIP)
- -> llvm::OpenMPIRBuilder::MapInfosTy & {
+ MapInfosTy combinedInfos;
+ auto genMapInfoCB =
+ [&](llvm::OpenMPIRBuilder::InsertPointTy codeGenIP) -> MapInfosTy & {
builder.restoreIP(codeGenIP);
genMapInfos(builder, moduleTranslation, dl, combinedInfos, mapData, true);
return combinedInfos;
@@ -4438,14 +4547,49 @@ convertOmpTarget(Operation &opInst, llvm::IRBuilderBase &builder,
findAllocaInsertPoint(builder, moduleTranslation);
llvm::OpenMPIRBuilder::LocationDescription ompLoc(builder);
+ llvm::OpenMPIRBuilder::TargetDataInfo info(
+ /*RequiresDevicePointerInfo=*/false,
+ /*SeparateBeginEndCalls=*/true);
+ llvm::Value *ifCond = nullptr;
+ if (Value targetIfCond = targetOp.getIfExpr())
+ ifCond = moduleTranslation.lookupValue(targetIfCond);
+
+ auto customMapperCB = [&](unsigned int i) {
+ llvm::Value *mapperFunc = nullptr;
+ if (combinedInfos.Mappers[i]) {
+ info.HasMapper = true;
+ llvm::Expected<llvm::Function *> newFn = getOrCreateUserDefinedMapperFunc(
+ combinedInfos.Mappers[i], builder, moduleTranslation);
+ assert(newFn && "Expect a valid mapper function is available");
+ mapperFunc = *newFn;
+ }
+ return mapperFunc;
+ };
+
+ llvm::OpenMPIRBuilder::TargetDataInfo info(
+ /*RequiresDevicePointerInfo=*/false,
+ /*SeparateBeginEndCalls=*/true);
+
+ auto customMapperCB = [&](unsigned int i) {
+ llvm::Value *mapperFunc = nullptr;
+ if (combinedInfos.Mappers[i]) {
+ info.HasMapper = true;
+ llvm::Expected<llvm::Function *> newFn = getOrCreateUserDefinedMapperFunc(
+ combinedInfos.Mappers[i], builder, moduleTranslation);
+ assert(newFn && "Expect a valid mapper function is available");
+ mapperFunc = *newFn;
+ }
+ return mapperFunc;
+ };
+
llvm::Value *ifCond = nullptr;
if (Value targetIfCond = targetOp.getIfExpr())
ifCond = moduleTranslation.lookupValue(targetIfCond);
llvm::OpenMPIRBuilder::InsertPointOrErrorTy afterIP =
moduleTranslation.getOpenMPBuilder()->createTarget(
- ompLoc, isOffloadEntry, allocaIP, builder.saveIP(), entryInfo,
- defaultAttrs, runtimeAttrs, ifCond, kernelInput, genMapInfoCB, bodyCB,
+ ompLoc, isOffloadEntry, allocaIP, builder.saveIP(), info, entryInfo,
+ defaultValTeams, defaultValThreads, kernelInput, genMapInfoCB, bodyCB,
argAccessorCB, dds, targetOp.getNowait());
if (failed(handleError(afterIP, opInst)))
@@ -4673,7 +4817,8 @@ convertHostOrTargetOperation(Operation *op, llvm::IRBuilderBase &builder,
.Case([&](omp::TaskwaitOp op) {
return convertOmpTaskwaitOp(op, builder, moduleTranslation);
})
- .Case<omp::YieldOp, omp::TerminatorOp, omp::DeclareReductionOp,
+ .Case<omp::YieldOp, omp::TerminatorOp, omp::DeclareMapperOp,
+ omp::DeclareMapperInfoOp, omp::DeclareReductionOp,
omp::CriticalDeclareOp>([](auto op) {
// `yield` and `terminator` can be just omitted. The block structure
// was created in the region that handles their parent operation.
diff --git a/mlir/test/Target/LLVMIR/omptarget-llvm.mlir b/mlir/test/Target/LLVMIR/omptarget-llvm.mlir
index 7f21095763a397..fcbc57f67ae1b9 100644
--- a/mlir/test/Target/LLVMIR/omptarget-llvm.mlir
+++ b/mlir/test/Target/LLVMIR/omptarget-llvm.mlir
@@ -485,3 +485,120 @@ llvm.func @_QPopenmp_target_data_update() {
// CHECK: call void @__tgt_target_data_update_mapper(ptr @2, i64 -1, i32 1, ptr %[[BASEPTRS_VAL_2]], ptr %[[PTRS_VAL_2]], ptr @{{.*}}, ptr @{{.*}}, ptr @{{.*}}, ptr null)
// CHECK: ret void
+
+// -----
+
+omp.declare_mapper @_QQFmy_testmy_mapper : !llvm.struct<"_QFmy_testTmy_type", (i32)> {
+^bb0(%arg0: !llvm.ptr):
+ %0 = llvm.mlir.constant(0 : i32) : i32
+ %1 = llvm.getelementptr %arg0[0, 0] : (!llvm.ptr) -> !llvm.ptr, !llvm.struct<"_QFmy_testTmy_type", (i32)>
+ %2 = omp.map.info var_ptr(%1 : !llvm.ptr, i32) map_clauses(tofrom) capture(ByRef) -> !llvm.ptr {name = "var%data"}
+ %3 = omp.map.info var_ptr(%arg0 : !llvm.ptr, !llvm.struct<"_QFmy_testTmy_type", (i32)>) map_clauses(tofrom) capture(ByRef) members(%2 : [0] : !llvm.ptr) -> !llvm.ptr {name = "var", partial_map = true}
+ omp.declare_mapper_info map_entries(%3, %2 : !llvm.ptr, !llvm.ptr)
+}
+
+llvm.func @_QPopenmp_target_data_mapper() {
+ %0 = llvm.mlir.constant(1 : i64) : i64
+ %1 = llvm.alloca %0 x !llvm.struct<"_QFmy_testTmy_type", (i32)> {bindc_name = "a"} : (i64) -> !llvm.ptr
+ %2 = omp.map.info var_ptr(%1 : !llvm.ptr, !llvm.struct<"_QFmy_testTmy_type", (i32)>) mapper(@_QQFmy_testmy_mapper) map_clauses(tofrom) capture(ByRef) -> !llvm.ptr {name = "a"}
+ omp.target_data map_entries(%2 : !llvm.ptr) {
+ %3 = llvm.mlir.constant(10 : i32) : i32
+ %4 = llvm.getelementptr %1[0, 0] : (!llvm.ptr) -> !llvm.ptr, !llvm.struct<"_QFmy_testTmy_type", (i32)>
+ llvm.store %3, %4 : i32, !llvm.ptr
+ omp.terminator
+ }
+ llvm.return
+}
+
+// CHECK: @.offload_sizes = private unnamed_addr constant [1 x i64] [i64 4]
+// CHECK: @.offload_maptypes = private unnamed_addr constant [1 x i64] [i64 3]
+// CHECK-LABEL: define void @_QPopenmp_target_data_mapper
+// CHECK: %[[VAL_0:.*]] = alloca [1 x ptr], align 8
+// CHECK: %[[VAL_1:.*]] = alloca [1 x ptr], align 8
+// CHECK: %[[VAL_2:.*]] = alloca [1 x ptr], align 8
+// CHECK: %[[VAL_3:.*]] = alloca %[[VAL_4:.*]], i64 1, align 8
+// CHECK: br label %[[VAL_5:.*]]
+// CHECK: entry: ; preds = %[[VAL_6:.*]]
+// CHECK: %[[VAL_7:.*]] = getelementptr inbounds [1 x ptr], ptr %[[VAL_0]], i32 0, i32 0
+// CHECK: store ptr %[[VAL_3]], ptr %[[VAL_7]], align 8
+// CHECK: %[[VAL_8:.*]] = getelementptr inbounds [1 x ptr], ptr %[[VAL_1]], i32 0, i32 0
+// CHECK: store ptr %[[VAL_3]], ptr %[[VAL_8]], align 8
+// CHECK: %[[VAL_9:.*]] = getelementptr inbounds [1 x ptr], ptr %[[VAL_2]], i64 0, i64 0
+// CHECK: store ptr @.omp_mapper._QQFmy_testmy_mapper, ptr %[[VAL_9]], align 8
+// CHECK: %[[VAL_10:.*]] = getelementptr inbounds [1 x ptr], ptr %[[VAL_0]], i32 0, i32 0
+// CHECK: %[[VAL_11:.*]] = getelementptr inbounds [1 x ptr], ptr %[[VAL_1]], i32 0, i32 0
+// CHECK: call void @__tgt_target_data_begin_mapper(ptr @4, i64 -1, i32 1, ptr %[[VAL_10]], ptr %[[VAL_11]], ptr @.offload_sizes, ptr @.offload_maptypes, ptr @.offload_mapnames, ptr %[[VAL_2]])
+// CHECK: %[[VAL_12:.*]] = getelementptr %[[VAL_4]], ptr %[[VAL_3]], i32 0, i32 0
+// CHECK: store i32 10, ptr %[[VAL_12]], align 4
+// CHECK: %[[VAL_13:.*]] = getelementptr inbounds [1 x ptr], ptr %[[VAL_0]], i32 0, i32 0
+// CHECK: %[[VAL_14:.*]] = getelementptr inbounds [1 x ptr], ptr %[[VAL_1]], i32 0, i32 0
+// CHECK: call void @__tgt_target_data_end_mapper(ptr @4, i64 -1, i32 1, ptr %[[VAL_13]], ptr %[[VAL_14]], ptr @.offload_sizes, ptr @.offload_maptypes, ptr @.offload_mapnames, ptr %[[VAL_2]])
+// CHECK: ret void
+
+// CHECK-LABEL: define internal void @.omp_mapper._QQFmy_testmy_mapper
+// CHECK: entry:
+// CHECK: %[[VAL_15:.*]] = udiv exact i64 %[[VAL_16:.*]], 4
+// CHECK: %[[VAL_17:.*]] = getelementptr %[[VAL_18:.*]], ptr %[[VAL_19:.*]], i64 %[[VAL_15]]
+// CHECK: %[[VAL_20:.*]] = icmp sgt i64 %[[VAL_15]], 1
+// CHECK: %[[VAL_21:.*]] = and i64 %[[VAL_22:.*]], 8
+// CHECK: %[[VAL_23:.*]] = icmp ne ptr %[[VAL_24:.*]], %[[VAL_19]]
+// CHECK: %[[VAL_25:.*]] = and i64 %[[VAL_22]], 16
+// CHECK: %[[VAL_26:.*]] = icmp ne i64 %[[VAL_25]], 0
+// CHECK: %[[VAL_27:.*]] = and i1 %[[VAL_23]], %[[VAL_26]]
+// CHECK: %[[VAL_28:.*]] = or i1 %[[VAL_20]], %[[VAL_27]]
+// CHECK: %[[VAL_29:.*]] = icmp eq i64 %[[VAL_21]], 0
+// CHECK: %[[VAL_30:.*]] = and i1 %[[VAL_28]], %[[VAL_29]]
+// CHECK: br i1 %[[VAL_30]], label %[[VAL_31:.*]], label %[[VAL_32:.*]]
+// CHECK: .omp.array..init: ; preds = %[[VAL_33:.*]]
+// CHECK: %[[VAL_34:.*]] = mul nuw i64 %[[VAL_15]], 4
+// CHECK: %[[VAL_35:.*]] = and i64 %[[VAL_22]], -4
+// CHECK: %[[VAL_36:.*]] = or i64 %[[VAL_35]], 512
+// CHECK: call void @__tgt_push_mapper_component(ptr %[[VAL_37:.*]], ptr %[[VAL_24]], ptr %[[VAL_19]], i64 %[[VAL_34]], i64 %[[VAL_36]], ptr %[[VAL_38:.*]])
+// CHECK: br label %[[VAL_32]]
+// CHECK: omp.arraymap.head: ; preds = %[[VAL_31]], %[[VAL_33]]
+// CHECK: %[[VAL_39:.*]] = icmp eq ptr %[[VAL_19]], %[[VAL_17]]
+// CHECK: br i1 %[[VAL_39]], label %[[VAL_40:.*]], label %[[VAL_41:.*]]
+// CHECK: omp.arraymap.body: ; preds = %[[VAL_42:.*]], %[[VAL_32]]
+// CHECK: %[[VAL_43:.*]] = phi ptr [ %[[VAL_19]], %[[VAL_32]] ], [ %[[VAL_44:.*]], %[[VAL_42]] ]
+// CHECK: %[[VAL_45:.*]] = getelementptr %[[VAL_18]], ptr %[[VAL_43]], i32 0, i32 0
+// CHECK: %[[VAL_46:.*]] = call i64 @__tgt_mapper_num_components(ptr %[[VAL_37]])
+// CHECK: %[[VAL_47:.*]] = shl i64 %[[VAL_46]], 48
+// CHECK: %[[VAL_48:.*]] = add nuw i64 3, %[[VAL_47]]
+// CHECK: %[[VAL_49:.*]] = and i64 %[[VAL_22]], 3
+// CHECK: %[[VAL_50:.*]] = icmp eq i64 %[[VAL_49]], 0
+// CHECK: br i1 %[[VAL_50]], label %[[VAL_51:.*]], label %[[VAL_52:.*]]
+// CHECK: omp.type.alloc: ; preds = %[[VAL_41]]
+// CHECK: %[[VAL_53:.*]] = and i64 %[[VAL_48]], -4
+// CHECK: br label %[[VAL_42]]
+// CHECK: omp.type.alloc.else: ; preds = %[[VAL_41]]
+// CHECK: %[[VAL_54:.*]] = icmp eq i64 %[[VAL_49]], 1
+// CHECK: br i1 %[[VAL_54]], label %[[VAL_55:.*]], label %[[VAL_56:.*]]
+// CHECK: omp.type.to: ; preds = %[[VAL_52]]
+// CHECK: %[[VAL_57:.*]] = and i64 %[[VAL_48]], -3
+// CHECK: br label %[[VAL_42]]
+// CHECK: omp.type.to.else: ; preds = %[[VAL_52]]
+// CHECK: %[[VAL_58:.*]] = icmp eq i64 %[[VAL_49]], 2
+// CHECK: br i1 %[[VAL_58]], label %[[VAL_59:.*]], label %[[VAL_42]]
+// CHECK: omp.type.from: ; preds = %[[VAL_56]]
+// CHECK: %[[VAL_60:.*]] = and i64 %[[VAL_48]], -2
+// CHECK: br label %[[VAL_42]]
+// CHECK: omp.type.end: ; preds = %[[VAL_59]], %[[VAL_56]], %[[VAL_55]], %[[VAL_51]]
+// CHECK: %[[VAL_61:.*]] = phi i64 [ %[[VAL_53]], %[[VAL_51]] ], [ %[[VAL_57]], %[[VAL_55]] ], [ %[[VAL_60]], %[[VAL_59]] ], [ %[[VAL_48]], %[[VAL_56]] ]
+// CHECK: call void @__tgt_push_mapper_component(ptr %[[VAL_37]], ptr %[[VAL_43]], ptr %[[VAL_45]], i64 4, i64 %[[VAL_61]], ptr @2)
+// CHECK: %[[VAL_44]] = getelementptr %[[VAL_18]], ptr %[[VAL_43]], i32 1
+// CHECK: %[[VAL_62:.*]] = icmp eq ptr %[[VAL_44]], %[[VAL_17]]
+// CHECK: br i1 %[[VAL_62]], label %[[VAL_63:.*]], label %[[VAL_41]]
+// CHECK: omp.arraymap.exit: ; preds = %[[VAL_42]]
+// CHECK: %[[VAL_64:.*]] = icmp sgt i64 %[[VAL_15]], 1
+// CHECK: %[[VAL_65:.*]] = and i64 %[[VAL_22]], 8
+// CHECK: %[[VAL_66:.*]] = icmp ne i64 %[[VAL_65]], 0
+// CHECK: %[[VAL_67:.*]] = and i1 %[[VAL_64]], %[[VAL_66]]
+// CHECK: br i1 %[[VAL_67]], label %[[VAL_68:.*]], label %[[VAL_40]]
+// CHECK: .omp.array..del: ; preds = %[[VAL_63]]
+// CHECK: %[[VAL_69:.*]] = mul nuw i64 %[[VAL_15]], 4
+// CHECK: %[[VAL_70:.*]] = and i64 %[[VAL_22]], -4
+// CHECK: %[[VAL_71:.*]] = or i64 %[[VAL_70]], 512
+// CHECK: call void @__tgt_push_mapper_component(ptr %[[VAL_37]], ptr %[[VAL_24]], ptr %[[VAL_19]], i64 %[[VAL_69]], i64 %[[VAL_71]], ptr %[[VAL_38]])
+// CHECK: br label %[[VAL_40]]
+// CHECK: omp.done: ; preds = %[[VAL_68]], %[[VAL_63]], %[[VAL_32]]
+// CHECK: ret void
diff --git a/offload/test/offloading/fortran/target-custom-mapper.f90 b/offload/test/offloading/fortran/target-custom-mapper.f90
new file mode 100644
index 00000000000000..5699a0613d9abb
--- /dev/null
+++ b/offload/test/offloading/fortran/target-custom-mapper.f90
@@ -0,0 +1,46 @@
+! Offloading test checking lowering of arrays with dynamic extents.
+! REQUIRES: flang, amdgpu
+
+! RUN: %libomptarget-compile-fortran-run-and-check-generic
+
+program test_openmp_mapper
+ implicit none
+ integer, parameter :: n = 1024
+ type :: mytype
+ integer :: data(n)
+ end type mytype
+
+ ! Declare a custom mapper for the derived type `mytype` with the name `my_mapper`
+ !$omp declare mapper(my_mapper : mytype :: t) map(to: t%data)
+
+ type(mytype) :: obj
+ integer :: i, sum_host, sum_device
+
+ ! Initialize the host data
+ do i = 1, n
+ obj%data(i) = 1
+ end do
+
+ ! Compute the sum on the host for verification
+ sum_host = sum(obj%data)
+
+ ! Offload computation to the device using the named mapper `my_mapper`
+ sum_device = 0
+ !$omp target map(tofrom: sum_device) map(mapper(my_mapper) : obj)
+ do i = 1, n
+ sum_device = sum_device + obj%data(i)
+ end do
+ !$omp end target
+
+ ! Check results
+ print *, "Sum on host: ", sum_host
+ print *, "Sum on device: ", sum_device
+
+ if (sum_device == sum_host) then
+ print *, "Test passed!"
+ else
+ print *, "Test failed!"
+ end if
+end program test_openmp_mapper
+
+! CHECK: Test passed!
>From 4aa1c5dc00552e5d9f6d529affa4fe5241210e14 Mon Sep 17 00:00:00 2001
From: Akash Banerjee <Akash.Banerjee at amd.com>
Date: Tue, 28 Jan 2025 15:01:27 +0000
Subject: [PATCH 20/23] Fix IRBuilderTest failure.
---
llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp | 15 +++++++++------
1 file changed, 9 insertions(+), 6 deletions(-)
diff --git a/llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp b/llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
index 2b9ed38911a2ad..a364bd6aaba571 100644
--- a/llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
+++ b/llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
@@ -6247,8 +6247,9 @@ TEST_F(OpenMPIRBuilderTest, TargetRegion) {
};
auto CustomMapperCB = [&](unsigned int I) { return nullptr; };
- llvm::OpenMPIRBuilder::TargetDataInfo Info(/*RequiresDevicePointerInfo=*/true,
- /*SeparateBeginEndCalls=*/true);
+ llvm::OpenMPIRBuilder::TargetDataInfo Info(
+ /*RequiresDevicePointerInfo=*/false,
+ /*SeparateBeginEndCalls=*/true);
TargetRegionEntryInfo EntryInfo("func", 42, 4711, 17);
OpenMPIRBuilder::LocationDescription OmpLoc({Builder.saveIP(), DL});
@@ -6430,8 +6431,9 @@ TEST_F(OpenMPIRBuilderTest, TargetRegionDevice) {
OpenMPIRBuilder::TargetKernelDefaultAttrs DefaultAttrs = {
/*ExecFlags=*/omp::OMPTgtExecModeFlags::OMP_TGT_EXEC_MODE_GENERIC,
/*MaxTeams=*/{-1}, /*MinTeams=*/0, /*MaxThreads=*/{0}, /*MinThreads=*/0};
- llvm::OpenMPIRBuilder::TargetDataInfo Info(/*RequiresDevicePointerInfo=*/true,
- /*SeparateBeginEndCalls=*/true);
+ llvm::OpenMPIRBuilder::TargetDataInfo Info(
+ /*RequiresDevicePointerInfo=*/false,
+ /*SeparateBeginEndCalls=*/true);
ASSERT_EXPECTED_INIT(
OpenMPIRBuilder::InsertPointTy, AfterIP,
@@ -6814,8 +6816,9 @@ TEST_F(OpenMPIRBuilderTest, ConstantAllocaRaise) {
OpenMPIRBuilder::TargetKernelDefaultAttrs DefaultAttrs = {
/*ExecFlags=*/omp::OMPTgtExecModeFlags::OMP_TGT_EXEC_MODE_GENERIC,
/*MaxTeams=*/{-1}, /*MinTeams=*/0, /*MaxThreads=*/{0}, /*MinThreads=*/0};
- llvm::OpenMPIRBuilder::TargetDataInfo Info(/*RequiresDevicePointerInfo=*/true,
- /*SeparateBeginEndCalls=*/true);
+ llvm::OpenMPIRBuilder::TargetDataInfo Info(
+ /*RequiresDevicePointerInfo=*/false,
+ /*SeparateBeginEndCalls=*/true);
ASSERT_EXPECTED_INIT(
OpenMPIRBuilder::InsertPointTy, AfterIP,
>From 06fc073841e060a4d4e9fb512b02075bfbc48305 Mon Sep 17 00:00:00 2001
From: Akash Banerjee <Akash.Banerjee at amd.com>
Date: Fri, 31 Jan 2025 10:34:04 +0000
Subject: [PATCH 21/23] Address reviewer comments.
---
.../OpenMP/OpenMPToLLVMIRTranslation.cpp | 4 +-
.../fortran/target-custom-mapper.f90 | 77 ++++++++++---------
2 files changed, 41 insertions(+), 40 deletions(-)
diff --git a/mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp b/mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp
index be546d47c3879c..d2a2b166b86c16 100644
--- a/mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp
+++ b/mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp
@@ -3557,7 +3557,7 @@ static llvm::Expected<llvm::Function *>
getOrCreateUserDefinedMapperFunc(Operation *declMapperOp,
llvm::IRBuilderBase &builder,
LLVM::ModuleTranslation &moduleTranslation) {
- llvm::DenseMap<const Operation *, llvm::Function *> userDefMapperMap;
+ static llvm::DenseMap<const Operation *, llvm::Function *> userDefMapperMap;
auto iter = userDefMapperMap.find(declMapperOp);
if (iter != userDefMapperMap.end())
return iter->second;
@@ -3566,7 +3566,7 @@ getOrCreateUserDefinedMapperFunc(Operation *declMapperOp,
if (!mapperFunc)
return mapperFunc.takeError();
userDefMapperMap.try_emplace(declMapperOp, *mapperFunc);
- return userDefMapperMap.lookup(declMapperOp);
+ return mapperFunc;
}
static llvm::Expected<llvm::Function *>
diff --git a/offload/test/offloading/fortran/target-custom-mapper.f90 b/offload/test/offloading/fortran/target-custom-mapper.f90
index 5699a0613d9abb..f81ec538f565d8 100644
--- a/offload/test/offloading/fortran/target-custom-mapper.f90
+++ b/offload/test/offloading/fortran/target-custom-mapper.f90
@@ -4,43 +4,44 @@
! RUN: %libomptarget-compile-fortran-run-and-check-generic
program test_openmp_mapper
- implicit none
- integer, parameter :: n = 1024
- type :: mytype
- integer :: data(n)
- end type mytype
-
- ! Declare a custom mapper for the derived type `mytype` with the name `my_mapper`
- !$omp declare mapper(my_mapper : mytype :: t) map(to: t%data)
-
- type(mytype) :: obj
- integer :: i, sum_host, sum_device
-
- ! Initialize the host data
- do i = 1, n
- obj%data(i) = 1
- end do
-
- ! Compute the sum on the host for verification
- sum_host = sum(obj%data)
-
- ! Offload computation to the device using the named mapper `my_mapper`
- sum_device = 0
- !$omp target map(tofrom: sum_device) map(mapper(my_mapper) : obj)
- do i = 1, n
- sum_device = sum_device + obj%data(i)
- end do
- !$omp end target
-
- ! Check results
- print *, "Sum on host: ", sum_host
- print *, "Sum on device: ", sum_device
-
- if (sum_device == sum_host) then
- print *, "Test passed!"
- else
- print *, "Test failed!"
- end if
-end program test_openmp_mapper
+ implicit none
+ integer, parameter :: n = 1024
+ type :: mytype
+ integer :: data(n)
+ end type mytype
+
+ ! Declare custom mappers for the derived type `mytype`
+ !$omp declare mapper(my_mapper1 : mytype :: t) map(to: t%data)
+ !$omp declare mapper(my_mapper2 : mytype :: t) map(mapper(my_mapper1): t%data)
+
+ type(mytype) :: obj
+ integer :: i, sum_host, sum_device
+
+ ! Initialize the host data
+ do i = 1, n
+ obj%data(i) = 1
+ end do
+
+ ! Compute the sum on the host for verification
+ sum_host = sum(obj%data)
+
+ ! Offload computation to the device using the named mapper `my_mapper2`
+ sum_device = 0
+ !$omp target map(tofrom: sum_device) map(mapper(my_mapper2) : obj)
+ do i = 1, n
+ sum_device = sum_device + obj%data(i)
+ end do
+ !$omp end target
+
+ ! Check results
+ print *, "Sum on host: ", sum_host
+ print *, "Sum on device: ", sum_device
+
+ if (sum_device == sum_host) then
+ print *, "Test passed!"
+ else
+ print *, "Test failed!"
+ end if
+ end program test_openmp_mapper
! CHECK: Test passed!
>From c476644ad330a469f74c8326afc2cf92b166c270 Mon Sep 17 00:00:00 2001
From: Akash Banerjee <Akash.Banerjee at amd.com>
Date: Fri, 7 Feb 2025 18:06:55 +0000
Subject: [PATCH 22/23] Address reviewer comments.
---
.../Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp | 3 +--
mlir/test/Target/LLVMIR/omptarget-llvm.mlir | 2 +-
2 files changed, 2 insertions(+), 3 deletions(-)
diff --git a/mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp b/mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp
index d2a2b166b86c16..cb0e4f042cb171 100644
--- a/mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp
+++ b/mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp
@@ -3577,8 +3577,7 @@ emitUserDefinedMapper(Operation *op, llvm::IRBuilderBase &builder,
*declMapperOp.getOps<omp::DeclareMapperInfoOp>().begin();
DataLayout dl = DataLayout(declMapperOp->getParentOfType<ModuleOp>());
llvm::OpenMPIRBuilder *ompBuilder = moduleTranslation.getOpenMPBuilder();
- llvm::Type *varType =
- moduleTranslation.convertType(declMapperOp.getVarType());
+ llvm::Type *varType = moduleTranslation.convertType(declMapperOp.getType());
std::string mapperName = ompBuilder->createPlatformSpecificName(
{"omp_mapper", declMapperOp.getSymName()});
SmallVector<Value> mapVars = declMapperInfoOp.getMapVars();
diff --git a/mlir/test/Target/LLVMIR/omptarget-llvm.mlir b/mlir/test/Target/LLVMIR/omptarget-llvm.mlir
index fcbc57f67ae1b9..02b84ff66a0d33 100644
--- a/mlir/test/Target/LLVMIR/omptarget-llvm.mlir
+++ b/mlir/test/Target/LLVMIR/omptarget-llvm.mlir
@@ -494,7 +494,7 @@ omp.declare_mapper @_QQFmy_testmy_mapper : !llvm.struct<"_QFmy_testTmy_type", (i
%1 = llvm.getelementptr %arg0[0, 0] : (!llvm.ptr) -> !llvm.ptr, !llvm.struct<"_QFmy_testTmy_type", (i32)>
%2 = omp.map.info var_ptr(%1 : !llvm.ptr, i32) map_clauses(tofrom) capture(ByRef) -> !llvm.ptr {name = "var%data"}
%3 = omp.map.info var_ptr(%arg0 : !llvm.ptr, !llvm.struct<"_QFmy_testTmy_type", (i32)>) map_clauses(tofrom) capture(ByRef) members(%2 : [0] : !llvm.ptr) -> !llvm.ptr {name = "var", partial_map = true}
- omp.declare_mapper_info map_entries(%3, %2 : !llvm.ptr, !llvm.ptr)
+ omp.declare_mapper.info map_entries(%3, %2 : !llvm.ptr, !llvm.ptr)
}
llvm.func @_QPopenmp_target_data_mapper() {
>From 8a0192d46d167e3d29a88a0f683bab3b9a55ced9 Mon Sep 17 00:00:00 2001
From: Akash Banerjee <Akash.Banerjee at amd.com>
Date: Mon, 10 Feb 2025 19:50:25 +0000
Subject: [PATCH 23/23] Address reviewer comments.
---
clang/lib/CodeGen/CGOpenMPRuntime.cpp | 3 +-
llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp | 4 --
.../Frontend/OpenMPIRBuilderTest.cpp | 19 +++++--
.../OpenMP/OpenMPToLLVMIRTranslation.cpp | 52 ++++++-------------
4 files changed, 33 insertions(+), 45 deletions(-)
diff --git a/clang/lib/CodeGen/CGOpenMPRuntime.cpp b/clang/lib/CodeGen/CGOpenMPRuntime.cpp
index b919c1f6ac6276..2f98e93cfa6ad7 100644
--- a/clang/lib/CodeGen/CGOpenMPRuntime.cpp
+++ b/clang/lib/CodeGen/CGOpenMPRuntime.cpp
@@ -10095,7 +10095,8 @@ void CGOpenMPRuntime::emitTargetDataCalls(
llvm::OpenMPIRBuilder::InsertPointTy AfterIP =
cantFail(OMPBuilder.createTargetData(
OmpLoc, AllocaIP, CodeGenIP, DeviceID, IfCondVal, Info, GenMapInfoCB,
- /*MapperFunc=*/nullptr, BodyCB, DeviceAddrCB, CustomMapperCB, RTLoc));
+ CustomMapperCB,
+ /*MapperFunc=*/nullptr, BodyCB, DeviceAddrCB, RTLoc));
CGF.Builder.restoreIP(AfterIP);
}
diff --git a/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp b/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
index 9c008aa4f967a6..80c739ef6fdfda 100644
--- a/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
+++ b/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
@@ -7484,10 +7484,6 @@ emitTargetCall(OpenMPIRBuilder &OMPBuilder, IRBuilderBase &Builder,
auto &&EmitTargetCallThen =
[&](OpenMPIRBuilder::InsertPointTy AllocaIP,
OpenMPIRBuilder::InsertPointTy CodeGenIP) -> Error {
- OpenMPIRBuilder::TargetDataInfo Info(
- /*RequiresDevicePointerInfo=*/false,
- /*SeparateBeginEndCalls=*/true);
-
OpenMPIRBuilder::MapInfosTy &MapInfo = GenMapInfoCB(Builder.saveIP());
OpenMPIRBuilder::TargetDataRTArgs RTArgs;
OMPBuilder.emitOffloadingArraysAndArgs(AllocaIP, Builder.saveIP(), Info,
diff --git a/llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp b/llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
index a364bd6aaba571..a1ea7849d7c0c5 100644
--- a/llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
+++ b/llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
@@ -6565,6 +6565,7 @@ TEST_F(OpenMPIRBuilderTest, TargetRegionSPMD) {
F->setName("func");
IRBuilder<> Builder(BB);
+ auto CustomMapperCB = [&](unsigned int I) { return nullptr; };
auto BodyGenCB = [&](InsertPointTy,
InsertPointTy CodeGenIP) -> InsertPointTy {
Builder.restoreIP(CodeGenIP);
@@ -6592,13 +6593,17 @@ TEST_F(OpenMPIRBuilderTest, TargetRegionSPMD) {
/*ExecFlags=*/omp::OMPTgtExecModeFlags::OMP_TGT_EXEC_MODE_SPMD,
/*MaxTeams=*/{-1}, /*MinTeams=*/0, /*MaxThreads=*/{0}, /*MinThreads=*/0};
RuntimeAttrs.LoopTripCount = Builder.getInt64(1000);
+ llvm::OpenMPIRBuilder::TargetDataInfo Info(
+ /*RequiresDevicePointerInfo=*/false,
+ /*SeparateBeginEndCalls=*/true);
ASSERT_EXPECTED_INIT(
OpenMPIRBuilder::InsertPointTy, AfterIP,
OMPBuilder.createTarget(OmpLoc, /*IsOffloadEntry=*/true, Builder.saveIP(),
- Builder.saveIP(), EntryInfo, DefaultAttrs,
+ Builder.saveIP(), Info, EntryInfo, DefaultAttrs,
RuntimeAttrs, /*IfCond=*/nullptr, Inputs,
- GenMapInfoCB, BodyGenCB, SimpleArgAccessorCB));
+ GenMapInfoCB, BodyGenCB, SimpleArgAccessorCB,
+ CustomMapperCB));
Builder.restoreIP(AfterIP);
OMPBuilder.finalize();
@@ -6679,6 +6684,7 @@ TEST_F(OpenMPIRBuilderTest, TargetRegionDeviceSPMD) {
return CombinedInfos;
};
+ auto CustomMapperCB = [&](unsigned int I) { return nullptr; };
auto BodyGenCB = [&](OpenMPIRBuilder::InsertPointTy,
OpenMPIRBuilder::InsertPointTy CodeGenIP)
-> OpenMPIRBuilder::InsertPointTy {
@@ -6695,13 +6701,16 @@ TEST_F(OpenMPIRBuilderTest, TargetRegionDeviceSPMD) {
OpenMPIRBuilder::TargetKernelDefaultAttrs DefaultAttrs = {
/*ExecFlags=*/omp::OMPTgtExecModeFlags::OMP_TGT_EXEC_MODE_SPMD,
/*MaxTeams=*/{-1}, /*MinTeams=*/0, /*MaxThreads=*/{0}, /*MinThreads=*/0};
+ llvm::OpenMPIRBuilder::TargetDataInfo Info(
+ /*RequiresDevicePointerInfo=*/false,
+ /*SeparateBeginEndCalls=*/true);
ASSERT_EXPECTED_INIT(
OpenMPIRBuilder::InsertPointTy, AfterIP,
OMPBuilder.createTarget(Loc, /*IsOffloadEntry=*/true, EntryIP, EntryIP,
- EntryInfo, DefaultAttrs, RuntimeAttrs,
+ Info, EntryInfo, DefaultAttrs, RuntimeAttrs,
/*IfCond=*/nullptr, CapturedArgs, GenMapInfoCB,
- BodyGenCB, SimpleArgAccessorCB));
+ BodyGenCB, SimpleArgAccessorCB, CustomMapperCB));
Builder.restoreIP(AfterIP);
Builder.CreateRetVoid();
@@ -6793,9 +6802,9 @@ TEST_F(OpenMPIRBuilderTest, ConstantAllocaRaise) {
return CombinedInfos;
};
- auto CustomMapperCB = [&](unsigned int I) { return nullptr; };
llvm::Value *RaiseAlloca = nullptr;
+ auto CustomMapperCB = [&](unsigned int I) { return nullptr; };
auto BodyGenCB = [&](OpenMPIRBuilder::InsertPointTy AllocaIP,
OpenMPIRBuilder::InsertPointTy CodeGenIP)
-> OpenMPIRBuilder::InsertPointTy {
diff --git a/mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp b/mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp
index cb0e4f042cb171..a16c7e93973926 100644
--- a/mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp
+++ b/mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp
@@ -3554,18 +3554,19 @@ emitUserDefinedMapper(Operation *declMapperOp, llvm::IRBuilderBase &builder,
LLVM::ModuleTranslation &moduleTranslation);
static llvm::Expected<llvm::Function *>
-getOrCreateUserDefinedMapperFunc(Operation *declMapperOp,
- llvm::IRBuilderBase &builder,
+getOrCreateUserDefinedMapperFunc(Operation *op, llvm::IRBuilderBase &builder,
LLVM::ModuleTranslation &moduleTranslation) {
- static llvm::DenseMap<const Operation *, llvm::Function *> userDefMapperMap;
- auto iter = userDefMapperMap.find(declMapperOp);
- if (iter != userDefMapperMap.end())
- return iter->second;
+ auto declMapperOp = cast<omp::DeclareMapperOp>(op);
+ std::string mapperFuncName =
+ moduleTranslation.getOpenMPBuilder()->createPlatformSpecificName(
+ {"omp_mapper", declMapperOp.getSymName()});
+ if (auto *lookupFunc = moduleTranslation.lookupFunction(mapperFuncName))
+ return lookupFunc;
+
llvm::Expected<llvm::Function *> mapperFunc =
emitUserDefinedMapper(declMapperOp, builder, moduleTranslation);
if (!mapperFunc)
return mapperFunc.takeError();
- userDefMapperMap.try_emplace(declMapperOp, *mapperFunc);
return mapperFunc;
}
@@ -3573,8 +3574,7 @@ static llvm::Expected<llvm::Function *>
emitUserDefinedMapper(Operation *op, llvm::IRBuilderBase &builder,
LLVM::ModuleTranslation &moduleTranslation) {
auto declMapperOp = cast<omp::DeclareMapperOp>(op);
- auto declMapperInfoOp =
- *declMapperOp.getOps<omp::DeclareMapperInfoOp>().begin();
+ auto declMapperInfoOp = declMapperOp.getDeclareMapperInfo();
DataLayout dl = DataLayout(declMapperOp->getParentOfType<ModuleOp>());
llvm::OpenMPIRBuilder *ompBuilder = moduleTranslation.getOpenMPBuilder();
llvm::Type *varType = moduleTranslation.convertType(declMapperOp.getType());
@@ -3590,7 +3590,7 @@ emitUserDefinedMapper(Operation *op, llvm::IRBuilderBase &builder,
[&](InsertPointTy codeGenIP, llvm::Value *ptrPHI,
llvm::Value *unused2) -> llvm::OpenMPIRBuilder::MapInfosOrErrorTy {
builder.restoreIP(codeGenIP);
- moduleTranslation.mapValue(declMapperOp.getRegion().getArgument(0), ptrPHI);
+ moduleTranslation.mapValue(declMapperOp.getSymVal(), ptrPHI);
moduleTranslation.mapBlock(&declMapperOp.getRegion().front(),
builder.GetInsertBlock());
if (failed(moduleTranslation.convertBlock(declMapperOp.getRegion().front(),
@@ -3857,10 +3857,11 @@ convertOmpTargetData(Operation *op, llvm::IRBuilderBase &builder,
findAllocaInsertPoint(builder, moduleTranslation);
llvm::OpenMPIRBuilder::InsertPointOrErrorTy afterIP = [&]() {
if (isa<omp::TargetDataOp>(op))
- return ompBuilder->createTargetData(
- ompLoc, allocaIP, builder.saveIP(), builder.getInt64(deviceID),
- ifCond, info, genMapInfoCB, customMapperCB, nullptr, bodyGenCB,
- /*DeviceAddrCB=*/nullptr);
+ return ompBuilder->createTargetData(ompLoc, allocaIP, builder.saveIP(),
+ builder.getInt64(deviceID), ifCond,
+ info, genMapInfoCB, customMapperCB,
+ /*MapperFunc=*/nullptr, bodyGenCB,
+ /*DeviceAddrCB=*/nullptr);
return ompBuilder->createTargetData(
ompLoc, allocaIP, builder.saveIP(), builder.getInt64(deviceID), ifCond,
info, genMapInfoCB, customMapperCB, &RTLFn);
@@ -4546,25 +4547,6 @@ convertOmpTarget(Operation &opInst, llvm::IRBuilderBase &builder,
findAllocaInsertPoint(builder, moduleTranslation);
llvm::OpenMPIRBuilder::LocationDescription ompLoc(builder);
- llvm::OpenMPIRBuilder::TargetDataInfo info(
- /*RequiresDevicePointerInfo=*/false,
- /*SeparateBeginEndCalls=*/true);
- llvm::Value *ifCond = nullptr;
- if (Value targetIfCond = targetOp.getIfExpr())
- ifCond = moduleTranslation.lookupValue(targetIfCond);
-
- auto customMapperCB = [&](unsigned int i) {
- llvm::Value *mapperFunc = nullptr;
- if (combinedInfos.Mappers[i]) {
- info.HasMapper = true;
- llvm::Expected<llvm::Function *> newFn = getOrCreateUserDefinedMapperFunc(
- combinedInfos.Mappers[i], builder, moduleTranslation);
- assert(newFn && "Expect a valid mapper function is available");
- mapperFunc = *newFn;
- }
- return mapperFunc;
- };
-
llvm::OpenMPIRBuilder::TargetDataInfo info(
/*RequiresDevicePointerInfo=*/false,
/*SeparateBeginEndCalls=*/true);
@@ -4588,8 +4570,8 @@ convertOmpTarget(Operation &opInst, llvm::IRBuilderBase &builder,
llvm::OpenMPIRBuilder::InsertPointOrErrorTy afterIP =
moduleTranslation.getOpenMPBuilder()->createTarget(
ompLoc, isOffloadEntry, allocaIP, builder.saveIP(), info, entryInfo,
- defaultValTeams, defaultValThreads, kernelInput, genMapInfoCB, bodyCB,
- argAccessorCB, dds, targetOp.getNowait());
+ defaultAttrs, runtimeAttrs, ifCond, kernelInput, genMapInfoCB, bodyCB,
+ argAccessorCB, customMapperCB, dds, targetOp.getNowait());
if (failed(handleError(afterIP, opInst)))
return failure();
More information about the llvm-branch-commits
mailing list