Skip to content

[MLIR][acc] Introduce varType to acc data clause operations #119007

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions flang/lib/Lower/OpenACC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ createDataEntryOp(fir::FirOpBuilder &builder, mlir::Location loc,
op.setStructured(structured);
op.setImplicit(implicit);
op.setDataClause(dataClause);
op.setVarType(mlir::cast<mlir::acc::PointerLikeType>(baseAddr.getType())
.getElementType());
op->setAttr(Op::getOperandSegmentSizeAttr(),
builder.getDenseI32ArrayAttr(operandSegments));
if (!asyncDeviceTypes.empty())
Expand Down Expand Up @@ -266,8 +268,8 @@ static void createDeclareDeallocFuncWithArg(
if constexpr (std::is_same_v<ExitOp, mlir::acc::CopyoutOp> ||
std::is_same_v<ExitOp, mlir::acc::UpdateHostOp>)
builder.create<ExitOp>(entryOp.getLoc(), entryOp.getAccPtr(),
entryOp.getVarPtr(), entryOp.getBounds(),
entryOp.getAsyncOperands(),
entryOp.getVarPtr(), entryOp.getVarType(),
entryOp.getBounds(), entryOp.getAsyncOperands(),
entryOp.getAsyncOperandsDeviceTypeAttr(),
entryOp.getAsyncOnlyAttr(), entryOp.getDataClause(),
/*structured=*/false, /*implicit=*/false,
Expand Down Expand Up @@ -450,7 +452,7 @@ static void genDataExitOperations(fir::FirOpBuilder &builder,
std::is_same_v<ExitOp, mlir::acc::UpdateHostOp>)
builder.create<ExitOp>(
entryOp.getLoc(), entryOp.getAccPtr(), entryOp.getVarPtr(),
entryOp.getBounds(), entryOp.getAsyncOperands(),
entryOp.getVarType(), entryOp.getBounds(), entryOp.getAsyncOperands(),
entryOp.getAsyncOperandsDeviceTypeAttr(), entryOp.getAsyncOnlyAttr(),
entryOp.getDataClause(), structured, entryOp.getImplicit(),
builder.getStringAttr(*entryOp.getName()));
Expand Down
131 changes: 65 additions & 66 deletions mlir/include/mlir/Dialect/OpenACC/OpenACCOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -381,17 +381,18 @@ class OpenACC_DataEntryOp<string mnemonic, string clause, string extraDescriptio
OpenACC_Op<mnemonic, !listconcat(traits,
[AttrSizedOperandSegments,
MemoryEffects<[MemRead<OpenACC_CurrentDeviceIdResource>]>])> {
let arguments = !con(additionalArgs,
(ins
Optional<OpenACC_PointerLikeTypeInterface>:$varPtrPtr,
Variadic<OpenACC_DataBoundsType>:$bounds, /* rank-0 to rank-{n-1} */
Variadic<IntOrIndex>:$asyncOperands,
OptionalAttr<DeviceTypeArrayAttr>:$asyncOperandsDeviceType,
OptionalAttr<DeviceTypeArrayAttr>:$asyncOnly,
DefaultValuedAttr<OpenACC_DataClauseAttr,clause>:$dataClause,
DefaultValuedAttr<BoolAttr, "true">:$structured,
DefaultValuedAttr<BoolAttr, "false">:$implicit,
OptionalAttr<StrAttr>:$name));
let arguments = !con(
additionalArgs,
(ins TypeAttr:$varType,
Optional<OpenACC_PointerLikeTypeInterface>:$varPtrPtr,
Variadic<OpenACC_DataBoundsType>:$bounds, /* rank-0 to rank-{n-1} */
Variadic<IntOrIndex>:$asyncOperands,
OptionalAttr<DeviceTypeArrayAttr>:$asyncOperandsDeviceType,
OptionalAttr<DeviceTypeArrayAttr>:$asyncOnly,
DefaultValuedAttr<OpenACC_DataClauseAttr, clause>:$dataClause,
DefaultValuedAttr<BoolAttr, "true">:$structured,
DefaultValuedAttr<BoolAttr, "false">:$implicit,
OptionalAttr<StrAttr>:$name));

let description = !strconcat(extraDescription, [{
Description of arguments:
Expand Down Expand Up @@ -458,7 +459,7 @@ class OpenACC_DataEntryOp<string mnemonic, string clause, string extraDescriptio
}];

let assemblyFormat = [{
`varPtr` `(` $varPtr `:` type($varPtr) `)`
`varPtr` `(` $varPtr `:` custom<VarPtrType>(type($varPtr), $varType)
oilist(
`varPtrPtr` `(` $varPtrPtr `:` type($varPtrPtr) `)`
| `bounds` `(` $bounds `)`
Expand All @@ -469,32 +470,35 @@ class OpenACC_DataEntryOp<string mnemonic, string clause, string extraDescriptio

let hasVerifier = 1;

let builders = [
OpBuilder<(ins "::mlir::Value":$varPtr,
"bool":$structured,
"bool":$implicit,
CArg<"::mlir::ValueRange", "{}">:$bounds), [{
build($_builder, $_state, varPtr.getType(), varPtr, /*varPtrPtr=*/{},
bounds, /*asyncOperands=*/{}, /*asyncOperandsDeviceType=*/nullptr,
let builders = [OpBuilder<(ins "::mlir::Value":$varPtr, "bool":$structured,
"bool":$implicit,
CArg<"::mlir::ValueRange", "{}">:$bounds),
[{
build($_builder, $_state, varPtr.getType(), varPtr,
/*varType=*/::mlir::TypeAttr::get(
::mlir::cast<::mlir::acc::PointerLikeType>(
varPtr.getType()).getElementType()),
/*varPtrPtr=*/{}, bounds, /*asyncOperands=*/{},
/*asyncOperandsDeviceType=*/nullptr,
/*asyncOnly=*/nullptr, /*dataClause=*/nullptr,
/*structured=*/$_builder.getBoolAttr(structured),
/*implicit=*/$_builder.getBoolAttr(implicit), /*name=*/nullptr);
}]
>,
OpBuilder<(ins "::mlir::Value":$varPtr,
"bool":$structured,
"bool":$implicit,
"const ::llvm::Twine &":$name,
CArg<"::mlir::ValueRange", "{}">:$bounds), [{
build($_builder, $_state, varPtr.getType(), varPtr, /*varPtrPtr=*/{},
bounds, /*asyncOperands=*/{}, /*asyncOperandsDeviceType=*/nullptr,
}]>,
OpBuilder<(ins "::mlir::Value":$varPtr, "bool":$structured,
"bool":$implicit, "const ::llvm::Twine &":$name,
CArg<"::mlir::ValueRange", "{}">:$bounds),
[{
build($_builder, $_state, varPtr.getType(), varPtr,
/*varType=*/::mlir::TypeAttr::get(
::mlir::cast<::mlir::acc::PointerLikeType>(
varPtr.getType()).getElementType()),
/*varPtrPtr=*/{}, bounds, /*asyncOperands=*/{},
/*asyncOperandsDeviceType=*/nullptr,
/*asyncOnly=*/nullptr, /*dataClause=*/nullptr,
/*structured=*/$_builder.getBoolAttr(structured),
/*implicit=*/$_builder.getBoolAttr(implicit),
/*name=*/$_builder.getStringAttr(name));
}]
>
];
}]>];
}

//===----------------------------------------------------------------------===//
Expand Down Expand Up @@ -794,63 +798,58 @@ class OpenACC_DataExitOp<string mnemonic, string clause, string extraDescription
}
}];

let assemblyFormat = [{
`accPtr` `(` $accPtr `:` type($accPtr) `)`
oilist(
`bounds` `(` $bounds `)`
| `to` `varPtr` `(` $varPtr `:` type($varPtr) `)`
| `async` `(` custom<DeviceTypeOperands>($asyncOperands,
type($asyncOperands), $asyncOperandsDeviceType) `)`
) attr-dict
}];

let hasVerifier = 1;
}

class OpenACC_DataExitOpWithVarPtr<string mnemonic, string clause> :
OpenACC_DataExitOp<mnemonic, clause,
"- `varPtr`: The address of variable to copy back to.",
[MemoryEffects<[MemRead<OpenACC_RuntimeCounters>,
MemWrite<OpenACC_RuntimeCounters>]>],
(ins Arg<OpenACC_PointerLikeTypeInterface,"Address of device variable",[MemRead]>:$accPtr,
Arg<OpenACC_PointerLikeTypeInterface,"Address of variable",[MemWrite]>:$varPtr)> {
class OpenACC_DataExitOpWithVarPtr<string mnemonic, string clause>
: OpenACC_DataExitOp<
mnemonic, clause,
"- `varPtr`: The address of variable to copy back to.",
[MemoryEffects<[MemRead<OpenACC_RuntimeCounters>,
MemWrite<OpenACC_RuntimeCounters>]>],
(ins Arg<OpenACC_PointerLikeTypeInterface,
"Address of device variable", [MemRead]>:$accPtr,
Arg<OpenACC_PointerLikeTypeInterface,
"Address of variable", [MemWrite]>:$varPtr,
TypeAttr:$varType)> {
let assemblyFormat = [{
`accPtr` `(` $accPtr `:` type($accPtr) `)`
(`bounds` `(` $bounds^ `)` )?
(`async` `(` custom<DeviceTypeOperands>($asyncOperands,
type($asyncOperands), $asyncOperandsDeviceType)^ `)`)?
`to` `varPtr` `(` $varPtr `:` type($varPtr) `)`
`to` `varPtr` `(` $varPtr `:` custom<VarPtrType>(type($varPtr), $varType)
attr-dict
}];

let builders = [
OpBuilder<(ins "::mlir::Value":$accPtr,
"::mlir::Value":$varPtr,
"bool":$structured,
"bool":$implicit,
CArg<"::mlir::ValueRange", "{}">:$bounds), [{
let builders = [OpBuilder<(ins "::mlir::Value":$accPtr,
"::mlir::Value":$varPtr, "bool":$structured,
"bool":$implicit,
CArg<"::mlir::ValueRange", "{}">:$bounds),
[{
build($_builder, $_state, accPtr, varPtr,
/*varType=*/::mlir::TypeAttr::get(
::mlir::cast<::mlir::acc::PointerLikeType>(
varPtr.getType()).getElementType()),
bounds, /*asyncOperands=*/{}, /*asyncOperandsDeviceType=*/nullptr,
/*asyncOnly=*/nullptr, /*dataClause=*/nullptr,
/*structured=*/$_builder.getBoolAttr(structured),
/*implicit=*/$_builder.getBoolAttr(implicit), /*name=*/nullptr);
}]
>,
OpBuilder<(ins "::mlir::Value":$accPtr,
"::mlir::Value":$varPtr,
"bool":$structured,
"bool":$implicit,
"const ::llvm::Twine &":$name,
CArg<"::mlir::ValueRange", "{}">:$bounds), [{
}]>,
OpBuilder<(ins "::mlir::Value":$accPtr,
"::mlir::Value":$varPtr, "bool":$structured,
"bool":$implicit, "const ::llvm::Twine &":$name,
CArg<"::mlir::ValueRange", "{}">:$bounds),
[{
build($_builder, $_state, accPtr, varPtr,
/*varType=*/::mlir::TypeAttr::get(
::mlir::cast<::mlir::acc::PointerLikeType>(
varPtr.getType()).getElementType()),
bounds, /*asyncOperands=*/{}, /*asyncOperandsDeviceType=*/nullptr,
/*asyncOnly=*/nullptr, /*dataClause=*/nullptr,
/*structured=*/$_builder.getBoolAttr(structured),
/*implicit=*/$_builder.getBoolAttr(implicit),
/*name=*/$_builder.getStringAttr(name));
}]
>
];
}]>];
}

class OpenACC_DataExitOpNoVarPtr<string mnemonic, string clause> :
Expand Down
44 changes: 44 additions & 0 deletions mlir/lib/Dialect/OpenACC/IR/OpenACC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@
#include "mlir/Dialect/LLVMIR/LLVMTypes.h"
#include "mlir/Dialect/MemRef/IR/MemRef.h"
#include "mlir/IR/Builders.h"
#include "mlir/IR/BuiltinAttributes.h"
#include "mlir/IR/BuiltinTypes.h"
#include "mlir/IR/DialectImplementation.h"
#include "mlir/IR/Matchers.h"
#include "mlir/IR/OpImplementation.h"
#include "mlir/Transforms/DialectConversion.h"
#include "llvm/ADT/SmallSet.h"
#include "llvm/ADT/TypeSwitch.h"
#include "llvm/Support/LogicalResult.h"

using namespace mlir;
using namespace acc;
Expand Down Expand Up @@ -190,6 +192,48 @@ static LogicalResult checkWaitAndAsyncConflict(Op op) {
return success();
}

static ParseResult parseVarPtrType(mlir::OpAsmParser &parser,
mlir::Type &varPtrType,
mlir::TypeAttr &varTypeAttr) {
if (failed(parser.parseType(varPtrType)))
return failure();
if (failed(parser.parseRParen()))
return failure();

if (succeeded(parser.parseOptionalKeyword("varType"))) {
if (failed(parser.parseLParen()))
return failure();
mlir::Type varType;
if (failed(parser.parseType(varType)))
return failure();
varTypeAttr = mlir::TypeAttr::get(varType);
if (failed(parser.parseRParen()))
return failure();
} else {
// Set `varType` from the element type of the type of `varPtr`.
varTypeAttr = mlir::TypeAttr::get(
mlir::cast<mlir::acc::PointerLikeType>(varPtrType).getElementType());
}

return success();
}

static void printVarPtrType(mlir::OpAsmPrinter &p, mlir::Operation *op,
mlir::Type varPtrType, mlir::TypeAttr varTypeAttr) {
p.printType(varPtrType);
p << ")";

// Print the `varType` only if it differs from the element type of
// `varPtr`'s type.
mlir::Type varType = varTypeAttr.getValue();
if (mlir::cast<mlir::acc::PointerLikeType>(varPtrType).getElementType() !=
varType) {
p << " varType(";
p.printType(varType);
p << ")";
}
}

//===----------------------------------------------------------------------===//
// DataBoundsOp
//===----------------------------------------------------------------------===//
Expand Down
Loading
Loading