Skip to content

Commit dd2c0b1

Browse files
[mlir][acc] Simplify data entry/exit operation builders (#114880)
Add new builders to DataBoundsOp, data entry ops, and data exit ops to simplify their construction since many of their inputs are optional. Additionally, small refactoring was needed for data exit ops to reduce duplication. Unit tests were added to exercise the new builders.
1 parent 1a68459 commit dd2c0b1

File tree

2 files changed

+308
-60
lines changed

2 files changed

+308
-60
lines changed

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

Lines changed: 138 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,24 @@ def OpenACC_DataBoundsOp : OpenACC_Op<"bounds",
347347
}];
348348

349349
let hasVerifier = 1;
350+
351+
let builders = [
352+
OpBuilder<(ins "::mlir::Value":$extent), [{
353+
build($_builder, $_state,
354+
::mlir::acc::DataBoundsType::get($_builder.getContext()),
355+
/*lowerbound=*/{}, /*upperbound=*/{}, extent,
356+
/*stride=*/{}, /*strideInBytes=*/nullptr, /*startIdx=*/{});
357+
}]
358+
>,
359+
OpBuilder<(ins "::mlir::Value":$lowerbound,
360+
"::mlir::Value":$upperbound), [{
361+
build($_builder, $_state,
362+
::mlir::acc::DataBoundsType::get($_builder.getContext()),
363+
lowerbound, upperbound, /*extent=*/{},
364+
/*stride=*/{}, /*strideInBytes=*/nullptr, /*startIdx=*/{});
365+
}]
366+
>
367+
];
350368
}
351369

352370
// Data entry operation does not refer to OpenACC spec terminology, but to
@@ -450,6 +468,33 @@ class OpenACC_DataEntryOp<string mnemonic, string clause, string extraDescriptio
450468
}];
451469

452470
let hasVerifier = 1;
471+
472+
let builders = [
473+
OpBuilder<(ins "::mlir::Value":$varPtr,
474+
"bool":$structured,
475+
"bool":$implicit,
476+
CArg<"::mlir::ValueRange", "{}">:$bounds), [{
477+
build($_builder, $_state, varPtr.getType(), varPtr, /*varPtrPtr=*/{},
478+
bounds, /*asyncOperands=*/{}, /*asyncOperandsDeviceType=*/nullptr,
479+
/*asyncOnly=*/nullptr, /*dataClause=*/nullptr,
480+
/*structured=*/$_builder.getBoolAttr(structured),
481+
/*implicit=*/$_builder.getBoolAttr(implicit), /*name=*/nullptr);
482+
}]
483+
>,
484+
OpBuilder<(ins "::mlir::Value":$varPtr,
485+
"bool":$structured,
486+
"bool":$implicit,
487+
"const ::llvm::Twine &":$name,
488+
CArg<"::mlir::ValueRange", "{}">:$bounds), [{
489+
build($_builder, $_state, varPtr.getType(), varPtr, /*varPtrPtr=*/{},
490+
bounds, /*asyncOperands=*/{}, /*asyncOperandsDeviceType=*/nullptr,
491+
/*asyncOnly=*/nullptr, /*dataClause=*/nullptr,
492+
/*structured=*/$_builder.getBoolAttr(structured),
493+
/*implicit=*/$_builder.getBoolAttr(implicit),
494+
/*name=*/$_builder.getStringAttr(name));
495+
}]
496+
>
497+
];
453498
}
454499

455500
//===----------------------------------------------------------------------===//
@@ -762,23 +807,13 @@ class OpenACC_DataExitOp<string mnemonic, string clause, string extraDescription
762807
let hasVerifier = 1;
763808
}
764809

765-
//===----------------------------------------------------------------------===//
766-
// 2.7.8 copyout clause
767-
//===----------------------------------------------------------------------===//
768-
def OpenACC_CopyoutOp : OpenACC_DataExitOp<"copyout",
769-
"mlir::acc::DataClause::acc_copyout",
770-
"- `varPtr`: The address of variable to copy back to.",
771-
[MemoryEffects<[MemRead<OpenACC_RuntimeCounters>,
772-
MemWrite<OpenACC_RuntimeCounters>]>],
773-
(ins Arg<OpenACC_PointerLikeTypeInterface,"Address of device variable",[MemRead]>:$accPtr,
774-
Arg<OpenACC_PointerLikeTypeInterface,"Address of variable",[MemWrite]>:$varPtr)> {
775-
let summary = "Represents acc copyout semantics - reverse of copyin.";
776-
777-
let extraClassDeclaration = extraClassDeclarationBase # [{
778-
/// Check if this is a copyout with zero modifier.
779-
bool isCopyoutZero();
780-
}];
781-
810+
class OpenACC_DataExitOpWithVarPtr<string mnemonic, string clause> :
811+
OpenACC_DataExitOp<mnemonic, clause,
812+
"- `varPtr`: The address of variable to copy back to.",
813+
[MemoryEffects<[MemRead<OpenACC_RuntimeCounters>,
814+
MemWrite<OpenACC_RuntimeCounters>]>],
815+
(ins Arg<OpenACC_PointerLikeTypeInterface,"Address of device variable",[MemRead]>:$accPtr,
816+
Arg<OpenACC_PointerLikeTypeInterface,"Address of variable",[MemWrite]>:$varPtr)> {
782817
let assemblyFormat = [{
783818
`accPtr` `(` $accPtr `:` type($accPtr) `)`
784819
(`bounds` `(` $bounds^ `)` )?
@@ -787,76 +822,121 @@ def OpenACC_CopyoutOp : OpenACC_DataExitOp<"copyout",
787822
`to` `varPtr` `(` $varPtr `:` type($varPtr) `)`
788823
attr-dict
789824
}];
825+
826+
let builders = [
827+
OpBuilder<(ins "::mlir::Value":$accPtr,
828+
"::mlir::Value":$varPtr,
829+
"bool":$structured,
830+
"bool":$implicit,
831+
CArg<"::mlir::ValueRange", "{}">:$bounds), [{
832+
build($_builder, $_state, accPtr, varPtr,
833+
bounds, /*asyncOperands=*/{}, /*asyncOperandsDeviceType=*/nullptr,
834+
/*asyncOnly=*/nullptr, /*dataClause=*/nullptr,
835+
/*structured=*/$_builder.getBoolAttr(structured),
836+
/*implicit=*/$_builder.getBoolAttr(implicit), /*name=*/nullptr);
837+
}]
838+
>,
839+
OpBuilder<(ins "::mlir::Value":$accPtr,
840+
"::mlir::Value":$varPtr,
841+
"bool":$structured,
842+
"bool":$implicit,
843+
"const ::llvm::Twine &":$name,
844+
CArg<"::mlir::ValueRange", "{}">:$bounds), [{
845+
build($_builder, $_state, accPtr, varPtr,
846+
bounds, /*asyncOperands=*/{}, /*asyncOperandsDeviceType=*/nullptr,
847+
/*asyncOnly=*/nullptr, /*dataClause=*/nullptr,
848+
/*structured=*/$_builder.getBoolAttr(structured),
849+
/*implicit=*/$_builder.getBoolAttr(implicit),
850+
/*name=*/$_builder.getStringAttr(name));
851+
}]
852+
>
853+
];
790854
}
791855

792-
//===----------------------------------------------------------------------===//
793-
// 2.7.11 delete clause
794-
//===----------------------------------------------------------------------===//
795-
def OpenACC_DeleteOp : OpenACC_DataExitOp<"delete",
796-
"mlir::acc::DataClause::acc_delete", "",
797-
[MemoryEffects<[MemRead<OpenACC_RuntimeCounters>,
856+
class OpenACC_DataExitOpNoVarPtr<string mnemonic, string clause> :
857+
OpenACC_DataExitOp<mnemonic, clause, "",
858+
[MemoryEffects<[MemRead<OpenACC_RuntimeCounters>,
798859
MemWrite<OpenACC_RuntimeCounters>]>],
799-
(ins Arg<OpenACC_PointerLikeTypeInterface,"Address of device variable",[MemRead]>:$accPtr)> {
800-
let summary = "Represents acc delete semantics - reverse of create.";
801-
802-
let extraClassDeclaration = extraClassDeclarationBase;
803-
860+
(ins Arg<OpenACC_PointerLikeTypeInterface,"Address of device variable",[MemRead]>:$accPtr)> {
804861
let assemblyFormat = [{
805862
`accPtr` `(` $accPtr `:` type($accPtr) `)`
806863
(`bounds` `(` $bounds^ `)` )?
807864
(`async` `(` custom<DeviceTypeOperands>($asyncOperands,
808865
type($asyncOperands), $asyncOperandsDeviceType)^ `)`)?
809866
attr-dict
810867
}];
868+
869+
let builders = [
870+
OpBuilder<(ins "::mlir::Value":$accPtr,
871+
"bool":$structured,
872+
"bool":$implicit,
873+
CArg<"::mlir::ValueRange", "{}">:$bounds), [{
874+
build($_builder, $_state, accPtr,
875+
bounds, /*asyncOperands=*/{}, /*asyncOperandsDeviceType=*/nullptr,
876+
/*asyncOnly=*/nullptr, /*dataClause=*/nullptr,
877+
/*structured=*/$_builder.getBoolAttr(structured),
878+
/*implicit=*/$_builder.getBoolAttr(implicit), /*name=*/nullptr);
879+
}]
880+
>,
881+
OpBuilder<(ins "::mlir::Value":$accPtr,
882+
"bool":$structured,
883+
"bool":$implicit,
884+
"const ::llvm::Twine &":$name,
885+
CArg<"::mlir::ValueRange", "{}">:$bounds), [{
886+
build($_builder, $_state, accPtr,
887+
bounds, /*asyncOperands=*/{}, /*asyncOperandsDeviceType=*/nullptr,
888+
/*asyncOnly=*/nullptr, /*dataClause=*/nullptr,
889+
/*structured=*/$_builder.getBoolAttr(structured),
890+
/*implicit=*/$_builder.getBoolAttr(implicit),
891+
/*name=*/$_builder.getStringAttr(name));
892+
}]
893+
>
894+
];
811895
}
812896

813897
//===----------------------------------------------------------------------===//
814-
// 2.7.13 detach clause
898+
// 2.7.8 copyout clause
815899
//===----------------------------------------------------------------------===//
816-
def OpenACC_DetachOp : OpenACC_DataExitOp<"detach",
817-
"mlir::acc::DataClause::acc_detach", "",
818-
[MemoryEffects<[MemRead<OpenACC_RuntimeCounters>,
819-
MemWrite<OpenACC_RuntimeCounters>]>],
820-
(ins Arg<OpenACC_PointerLikeTypeInterface,"Address of device variable",[MemRead]>:$accPtr)> {
821-
let summary = "Represents acc detach semantics - reverse of attach.";
900+
def OpenACC_CopyoutOp : OpenACC_DataExitOpWithVarPtr<"copyout",
901+
"mlir::acc::DataClause::acc_copyout"> {
902+
let summary = "Represents acc copyout semantics - reverse of copyin.";
822903

904+
let extraClassDeclaration = extraClassDeclarationBase # [{
905+
/// Check if this is a copyout with zero modifier.
906+
bool isCopyoutZero();
907+
}];
908+
}
909+
910+
//===----------------------------------------------------------------------===//
911+
// 2.7.11 delete clause
912+
//===----------------------------------------------------------------------===//
913+
def OpenACC_DeleteOp : OpenACC_DataExitOpNoVarPtr<"delete",
914+
"mlir::acc::DataClause::acc_delete"> {
915+
let summary = "Represents acc delete semantics - reverse of create.";
823916
let extraClassDeclaration = extraClassDeclarationBase;
917+
}
824918

825-
let assemblyFormat = [{
826-
`accPtr` `(` $accPtr `:` type($accPtr) `)`
827-
(`bounds` `(` $bounds^ `)` )?
828-
(`async` `(` custom<DeviceTypeOperands>($asyncOperands,
829-
type($asyncOperands), $asyncOperandsDeviceType)^ `)`)?
830-
attr-dict
831-
}];
919+
//===----------------------------------------------------------------------===//
920+
// 2.7.13 detach clause
921+
//===----------------------------------------------------------------------===//
922+
def OpenACC_DetachOp : OpenACC_DataExitOpNoVarPtr<"detach",
923+
"mlir::acc::DataClause::acc_detach"> {
924+
let summary = "Represents acc detach semantics - reverse of attach.";
925+
let extraClassDeclaration = extraClassDeclarationBase;
832926
}
833927

834928
//===----------------------------------------------------------------------===//
835929
// 2.14.4 host clause
836930
//===----------------------------------------------------------------------===//
837-
def OpenACC_UpdateHostOp : OpenACC_DataExitOp<"update_host",
838-
"mlir::acc::DataClause::acc_update_host",
839-
"- `varPtr`: The address of variable to copy back to.",
840-
[MemoryEffects<[MemRead<OpenACC_RuntimeCounters>,
841-
MemWrite<OpenACC_RuntimeCounters>]>],
842-
(ins Arg<OpenACC_PointerLikeTypeInterface,"Address of device variable",[MemRead]>:$accPtr,
843-
Arg<OpenACC_PointerLikeTypeInterface,"Address of variable",[MemWrite]>:$varPtr)> {
931+
def OpenACC_UpdateHostOp : OpenACC_DataExitOpWithVarPtr<"update_host",
932+
"mlir::acc::DataClause::acc_update_host"> {
844933
let summary = "Represents acc update host semantics.";
845934
let extraClassDeclaration = extraClassDeclarationBase # [{
846935
/// Check if this is an acc update self.
847936
bool isSelf() {
848937
return getDataClause() == acc::DataClause::acc_update_self;
849938
}
850939
}];
851-
852-
let assemblyFormat = [{
853-
`accPtr` `(` $accPtr `:` type($accPtr) `)`
854-
(`bounds` `(` $bounds^ `)` )?
855-
(`async` `(` custom<DeviceTypeOperands>($asyncOperands,
856-
type($asyncOperands), $asyncOperandsDeviceType)^ `)`)?
857-
`to` `varPtr` `(` $varPtr `:` type($varPtr) `)`
858-
attr-dict
859-
}];
860940
}
861941

862942
//===----------------------------------------------------------------------===//

0 commit comments

Comments
 (0)