Skip to content

Commit 4bdc905

Browse files
author
Razvan Lupusoru
committed
[openacc] Add implicit flag to declare attribute
The declare attribute has been updated to allow implicit flag. This is useful for variables that can be declare'd implicitly - like global constants. The verifier has been updated to ensure that an implicit declare'd variable has an implicit data action. The builder doesn't require for this flag to be set so any code creating this attribute will continue to work as-is. Reviewed By: vzakhari Differential Revision: https://reviews.llvm.org/D159124
1 parent 21d7287 commit 4bdc905

File tree

4 files changed

+38
-5
lines changed

4 files changed

+38
-5
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ mlir::Value getVarPtr(mlir::Operation *accDataEntryOp);
7373
std::optional<mlir::acc::DataClause>
7474
getDataClause(mlir::Operation *accDataEntryOp);
7575

76+
/// Used to find out whether data operation is implicit.
77+
/// Returns false if not a data operation or if it is a data operation without
78+
/// implicit flag.
79+
bool getImplicitFlag(mlir::Operation *accDataEntryOp);
80+
7681
/// Used to obtain the attribute name for declare.
7782
static constexpr StringLiteral getDeclareAttrName() {
7883
return StringLiteral("acc.declare");

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,13 @@ class OpenACC_Attr<string name, string attrMnemonic,
131131
// easier to find out whether the variable is in a declare clause and what kind
132132
// of clause it is.
133133
def DeclareAttr : OpenACC_Attr<"Declare", "declare"> {
134-
let parameters = (ins "DataClauseAttr":$dataClause);
134+
let parameters = (ins "DataClauseAttr":$dataClause,
135+
DefaultValuedParameter<"bool", "false">:$implicit);
135136
let assemblyFormat = "`<` struct(params) `>`";
137+
let builders = [AttrBuilder<(ins "DataClauseAttr":$dataClause), [{
138+
return $_get($_ctxt, dataClause, /*implicit=*/false);
139+
}]>
140+
];
136141
}
137142

138143
// Attribute to attach functions that perform the pre/post allocation actions or

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

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,11 +1015,21 @@ static LogicalResult checkDeclareOperands(Op &op,
10151015
if (!declareAttribute)
10161016
return op.emitError(
10171017
"expect declare attribute on variable in declare operation");
1018-
if (mlir::cast<mlir::acc::DeclareAttr>(declareAttribute)
1019-
.getDataClause()
1020-
.getValue() != dataClauseOptional.value())
1018+
1019+
auto declAttr = mlir::cast<mlir::acc::DeclareAttr>(declareAttribute);
1020+
if (declAttr.getDataClause().getValue() != dataClauseOptional.value())
10211021
return op.emitError(
10221022
"expect matching declare attribute on variable in declare operation");
1023+
1024+
// If the variable is marked with implicit attribute, the matching declare
1025+
// data action must also be marked implicit. The reverse is not checked
1026+
// since implicit data action may be inserted to do actions like updating
1027+
// device copy, in which case the variable is not necessarily implicitly
1028+
// declare'd.
1029+
if (declAttr.getImplicit() &&
1030+
declAttr.getImplicit() != acc::getImplicitFlag(operand.getDefiningOp()))
1031+
return op.emitError(
1032+
"implicitness must match between declare op and flag on variable");
10231033
}
10241034

10251035
return success();
@@ -1234,3 +1244,11 @@ mlir::acc::getDataClause(mlir::Operation *accDataEntryOp) {
12341244
.Default([&](mlir::Operation *) { return std::nullopt; })};
12351245
return dataClause;
12361246
}
1247+
1248+
bool mlir::acc::getImplicitFlag(mlir::Operation *accDataEntryOp) {
1249+
auto implicit{llvm::TypeSwitch<mlir::Operation *, bool>(accDataEntryOp)
1250+
.Case<ACC_DATA_ENTRY_OPS>(
1251+
[&](auto entry) { return entry.getImplicit(); })
1252+
.Default([&](mlir::Operation *) { return false; })};
1253+
return implicit;
1254+
}

mlir/test/Dialect/OpenACC/ops.mlir

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1684,12 +1684,17 @@ func.func @compute3(%a: memref<10x10xf32>, %b: memref<10x10xf32>, %c: memref<10x
16841684
%numGangs = arith.constant 10 : i64
16851685
%numWorkers = arith.constant 10 : i64
16861686

1687+
%c20 = arith.constant 20 : i32
1688+
%alloc = llvm.alloca %c20 x i32 { acc.declare = #acc.declare<dataClause = acc_create, implicit = true> } : (i32) -> !llvm.ptr<i32>
1689+
%createlocal = acc.create varPtr(%alloc : !llvm.ptr<i32>) -> !llvm.ptr<i32> {implicit = true}
1690+
16871691
%pa = acc.present varPtr(%a : memref<10x10xf32>) -> memref<10x10xf32>
16881692
%pb = acc.present varPtr(%b : memref<10x10xf32>) -> memref<10x10xf32>
16891693
%pc = acc.present varPtr(%c : memref<10xf32>) -> memref<10xf32>
16901694
%pd = acc.present varPtr(%d : memref<10xf32>) -> memref<10xf32>
1691-
acc.declare dataOperands(%pa, %pb, %pc, %pd: memref<10x10xf32>, memref<10x10xf32>, memref<10xf32>, memref<10xf32>) {
1695+
acc.declare dataOperands(%pa, %pb, %pc, %pd, %createlocal: memref<10x10xf32>, memref<10x10xf32>, memref<10xf32>, memref<10xf32>, !llvm.ptr<i32>) {
16921696
}
1697+
16931698
return
16941699
}
16951700

0 commit comments

Comments
 (0)