Skip to content

Commit 4e24d97

Browse files
author
Razvan Lupusoru
committed
[draft][acc][flang] Add recipes to data entry operations
Currently recipes are held as part of the constructs. But the recipes are needed specifically for materializing semantics of the data clauses. There is no reason to not have this on the data clauses themselves. TODO: Fix the IR in flang tests which do not expect new operand TODO: Deprecate the recipe list held on the constructs
1 parent 9e9fdd4 commit 4e24d97

File tree

4 files changed

+66
-3
lines changed

4 files changed

+66
-3
lines changed

flang/lib/Lower/OpenACC.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1340,6 +1340,8 @@ static void genPrivatizationRecipes(
13401340
builder, operandLocation, info.addr, asFortran, bounds, true,
13411341
/*implicit=*/false, mlir::acc::DataClause::acc_private, retTy, async,
13421342
asyncDeviceTypes, asyncOnlyDeviceTypes, /*unwrapBoxAddr=*/true);
1343+
op.setRecipeAttr(mlir::SymbolRefAttr::get(builder.getContext(),
1344+
recipe.getSymName().str()));
13431345
dataOperands.push_back(op.getAccVar());
13441346
} else {
13451347
std::string suffix =
@@ -1353,6 +1355,8 @@ static void genPrivatizationRecipes(
13531355
/*implicit=*/false, mlir::acc::DataClause::acc_firstprivate, retTy,
13541356
async, asyncDeviceTypes, asyncOnlyDeviceTypes,
13551357
/*unwrapBoxAddr=*/true);
1358+
op.setRecipeAttr(mlir::SymbolRefAttr::get(builder.getContext(),
1359+
recipe.getSymName().str()));
13561360
dataOperands.push_back(op.getAccVar());
13571361
}
13581362
privatizationRecipes.push_back(mlir::SymbolRefAttr::get(
@@ -1787,6 +1791,8 @@ genReductions(const Fortran::parser::AccObjectListWithReduction &objectList,
17871791
mlir::acc::ReductionRecipeOp recipe =
17881792
Fortran::lower::createOrGetReductionRecipe(
17891793
builder, recipeName, operandLocation, ty, mlirOp, bounds);
1794+
op.setRecipeAttr(mlir::SymbolRefAttr::get(builder.getContext(),
1795+
recipe.getSymName().str()));
17901796
reductionRecipes.push_back(mlir::SymbolRefAttr::get(
17911797
builder.getContext(), recipe.getSymName().str()));
17921798
reductionOperands.push_back(op.getAccVar());
@@ -2038,6 +2044,8 @@ privatizeIv(Fortran::lower::AbstractConverter &converter,
20382044
builder, loc, ivValue, asFortran, {}, true, /*implicit=*/true,
20392045
mlir::acc::DataClause::acc_private, ivValue.getType(),
20402046
/*async=*/{}, /*asyncDeviceTypes=*/{}, /*asyncOnlyDeviceTypes=*/{});
2047+
op.setRecipeAttr(mlir::SymbolRefAttr::get(builder.getContext(),
2048+
recipe.getSymName().str()));
20412049
privateOp = op.getOperation();
20422050

20432051
privateOperands.push_back(op.getAccVar());

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,9 @@ mlir::ValueRange getDataOperands(mlir::Operation *accOp);
151151
/// Used to get a mutable range iterating over the data operands.
152152
mlir::MutableOperandRange getMutableDataOperands(mlir::Operation *accOp);
153153

154+
/// Used to get the recipe attribute from a data clause operation.
155+
mlir::SymbolRefAttr getRecipe(mlir::Operation *accOp);
156+
154157
/// Used to obtain the enclosing compute construct operation that contains
155158
/// the provided `region`. Returns nullptr if no compute construct operation
156159
/// is found. The returns operation is one of types defined by

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

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,8 @@ class OpenACC_DataEntryOp<string mnemonic, string clause, string extraDescriptio
514514
DefaultValuedAttr<BoolAttr, "false">:$implicit,
515515
DefaultValuedAttr<OpenACC_DataClauseModifierAttr,
516516
"mlir::acc::DataClauseModifier::none">:$modifiers,
517-
OptionalAttr<StrAttr>:$name));
517+
OptionalAttr<StrAttr>:$name,
518+
OptionalAttr<SymbolRefAttr>:$recipe));
518519

519520
let description = !strconcat(extraDescription, [{
520521
Description of arguments:
@@ -623,7 +624,7 @@ class OpenACC_DataEntryOp<string mnemonic, string clause, string extraDescriptio
623624
/*asyncOnly=*/nullptr, /*dataClause=*/nullptr,
624625
/*structured=*/$_builder.getBoolAttr(structured),
625626
/*implicit=*/$_builder.getBoolAttr(implicit), /*modifiers=*/nullptr,
626-
/*name=*/nullptr);
627+
/*name=*/nullptr, /*recipe=*/nullptr);
627628
}]>,
628629
OpBuilder<(ins "::mlir::Value":$var,
629630
"bool":$structured, "bool":$implicit,
@@ -641,7 +642,7 @@ class OpenACC_DataEntryOp<string mnemonic, string clause, string extraDescriptio
641642
/*asyncOnly=*/nullptr, /*dataClause=*/nullptr,
642643
/*structured=*/$_builder.getBoolAttr(structured),
643644
/*implicit=*/$_builder.getBoolAttr(implicit), /*modifiers=*/nullptr,
644-
/*name=*/$_builder.getStringAttr(name));
645+
/*name=*/$_builder.getStringAttr(name), /*recipe=*/nullptr);
645646
}]>,
646647
OpBuilder<(ins "::mlir::Type":$accVarType, "::mlir::Value":$var,
647648
"::mlir::Type":$varType, "::mlir::Value":$varPtrPtr,
@@ -652,10 +653,27 @@ class OpenACC_DataEntryOp<string mnemonic, string clause, string extraDescriptio
652653
"::mlir::acc::DataClause":$dataClause, "bool":$structured,
653654
"bool":$implicit, "::mlir::StringAttr":$name),
654655
[{
656+
// Builder provided to ease transition for new data clause modifiers operand.
655657
build($_builder, $_state, accVarType, var, varType, varPtrPtr, bounds,
656658
asyncOperands, asyncOperandsDeviceType, asyncOnly, dataClause,
657659
structured, implicit, ::mlir::acc::DataClauseModifier::none, name);
658660
}]>,
661+
OpBuilder<(ins "::mlir::Type":$accVarType, "::mlir::Value":$var,
662+
"::mlir::Type":$varType, "::mlir::Value":$varPtrPtr,
663+
"::mlir::ValueRange":$bounds,
664+
"::mlir::ValueRange":$asyncOperands,
665+
"::mlir::ArrayAttr":$asyncOperandsDeviceType,
666+
"::mlir::ArrayAttr":$asyncOnly,
667+
"::mlir::acc::DataClause":$dataClause, "bool":$structured,
668+
"bool":$implicit,
669+
"::mlir::acc::DataClauseModifier":$modifiers,
670+
"::mlir::StringAttr":$name),
671+
[{
672+
// Builder provided to simplify building after recipe operand was added.
673+
build($_builder, $_state, accVarType, var, varType, varPtrPtr, bounds,
674+
asyncOperands, asyncOperandsDeviceType, asyncOnly, dataClause,
675+
structured, implicit, modifiers, name, /*recipe=*/nullptr);
676+
}]>,
659677
];
660678
}
661679

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,13 @@ checkValidModifier(Op op, acc::DataClauseModifier validModifiers) {
332332
return success();
333333
}
334334

335+
template <typename Op>
336+
static LogicalResult checkNoRecipe(Op op) {
337+
if (op.getRecipe().has_value())
338+
return op.emitError("no recipes are allowed");
339+
return success();
340+
}
341+
335342
static ParseResult parseVar(mlir::OpAsmParser &parser,
336343
OpAsmParser::UnresolvedOperand &var) {
337344
// Either `var` or `varPtr` keyword is required.
@@ -1063,6 +1070,24 @@ checkSymOperandList(Operation *op, std::optional<mlir::ArrayAttr> attributes,
10631070
return op->emitOpError()
10641071
<< "expected as many " << symbolName << " symbol reference as "
10651072
<< operandName << " operands";
1073+
if (attributes) {
1074+
for (auto operandAndAttribute : llvm::zip(operands, *attributes)) {
1075+
mlir::Value operand = std::get<0>(operandAndAttribute);
1076+
if (auto *definingOp = operand.getDefiningOp()) {
1077+
mlir::SymbolRefAttr operandRecipe = getRecipe(definingOp);
1078+
// If the operand operation has a recipe - check that it is consistent
1079+
// with the one recorded in the construct.
1080+
if (operandRecipe) {
1081+
if (operandRecipe.getLeafReference().compare(
1082+
llvm::cast<mlir::SymbolRefAttr>(
1083+
std::get<1>(operandAndAttribute))
1084+
.getLeafReference()) != 0)
1085+
return op->emitOpError() << "expected consistent recipe for "
1086+
<< operandName << " operand";
1087+
}
1088+
}
1089+
}
1090+
}
10661091
} else {
10671092
if (attributes)
10681093
return op->emitOpError()
@@ -4075,6 +4100,15 @@ mlir::acc::getMutableDataOperands(mlir::Operation *accOp) {
40754100
return dataOperands;
40764101
}
40774102

4103+
mlir::SymbolRefAttr mlir::acc::getRecipe(mlir::Operation *accOp) {
4104+
auto recipe{
4105+
llvm::TypeSwitch<mlir::Operation *, mlir::SymbolRefAttr>(accOp)
4106+
.Case<ACC_DATA_ENTRY_OPS>(
4107+
[&](auto entry) { return entry.getRecipeAttr(); })
4108+
.Default([&](mlir::Operation *) { return mlir::SymbolRefAttr{}; })};
4109+
return recipe;
4110+
}
4111+
40784112
mlir::Operation *mlir::acc::getEnclosingComputeOp(mlir::Region &region) {
40794113
mlir::Operation *parentOp = region.getParentOp();
40804114
while (parentOp) {

0 commit comments

Comments
 (0)