@@ -106,24 +106,38 @@ static bool verifyTypeParamCount(mlir::Type inType, unsigned numParams) {
106
106
return false ;
107
107
}
108
108
109
- // / Parser shared by Alloca and Allocmem
110
- // /
109
+ // / Parser shared by Alloca, Allocmem and OmpTargetAllocmem
110
+ // / boolean flag isTargetOp is used to identify omp_target_allocmem
111
111
// / operation ::= %res = (`fir.alloca` | `fir.allocmem`) $in_type
112
112
// / ( `(` $typeparams `)` )? ( `,` $shape )?
113
113
// / attr-dict-without-keyword
114
+ // / operation ::= %res = (`fir.omp_target_alloca`) $device : devicetype,
115
+ // / $in_type ( `(` $typeparams `)` )? ( `,` $shape )?
116
+ // / attr-dict-without-keyword
114
117
template <typename FN>
115
- static mlir::ParseResult parseAllocatableOp (FN wrapResultType,
116
- mlir::OpAsmParser &parser,
117
- mlir::OperationState &result) {
118
+ static mlir::ParseResult
119
+ parseAllocatableOp (FN wrapResultType, mlir::OpAsmParser &parser,
120
+ mlir::OperationState &result, bool isTargetOp = false ) {
121
+ auto &builder = parser.getBuilder ();
122
+ bool hasOperands = false ;
123
+ std::int32_t typeparamsSize = 0 ;
124
+ // Parse device number as a new operand
125
+ if (isTargetOp) {
126
+ mlir::OpAsmParser::UnresolvedOperand deviceOperand;
127
+ mlir::Type deviceType;
128
+ if (parser.parseOperand (deviceOperand) || parser.parseColonType (deviceType))
129
+ return mlir::failure ();
130
+ if (parser.resolveOperand (deviceOperand, deviceType, result.operands ))
131
+ return mlir::failure ();
132
+ if (parser.parseComma ())
133
+ return mlir::failure ();
134
+ }
118
135
mlir::Type intype;
119
136
if (parser.parseType (intype))
120
137
return mlir::failure ();
121
- auto &builder = parser.getBuilder ();
122
138
result.addAttribute (" in_type" , mlir::TypeAttr::get (intype));
123
139
llvm::SmallVector<mlir::OpAsmParser::UnresolvedOperand> operands;
124
140
llvm::SmallVector<mlir::Type> typeVec;
125
- bool hasOperands = false ;
126
- std::int32_t typeparamsSize = 0 ;
127
141
if (!parser.parseOptionalLParen ()) {
128
142
// parse the LEN params of the derived type. (<params> : <types>)
129
143
if (parser.parseOperandList (operands, mlir::OpAsmParser::Delimiter::None) ||
@@ -147,13 +161,19 @@ static mlir::ParseResult parseAllocatableOp(FN wrapResultType,
147
161
parser.resolveOperands (operands, typeVec, parser.getNameLoc (),
148
162
result.operands ))
149
163
return mlir::failure ();
164
+
150
165
mlir::Type restype = wrapResultType (intype);
151
166
if (!restype) {
152
167
parser.emitError (parser.getNameLoc (), " invalid allocate type: " ) << intype;
153
168
return mlir::failure ();
154
169
}
155
- result.addAttribute (" operandSegmentSizes" , builder.getDenseI32ArrayAttr (
156
- {typeparamsSize, shapeSize}));
170
+ llvm::SmallVector<std::int32_t > segmentSizes;
171
+ if (isTargetOp)
172
+ segmentSizes.push_back (1 );
173
+ segmentSizes.push_back (typeparamsSize);
174
+ segmentSizes.push_back (shapeSize);
175
+ result.addAttribute (" operandSegmentSizes" ,
176
+ builder.getDenseI32ArrayAttr (segmentSizes));
157
177
if (parser.parseOptionalAttrDict (result.attributes ) ||
158
178
parser.addTypeToList (restype, result.types ))
159
179
return mlir::failure ();
@@ -385,6 +405,56 @@ llvm::LogicalResult fir::AllocMemOp::verify() {
385
405
return mlir::success ();
386
406
}
387
407
408
+ // ===----------------------------------------------------------------------===//
409
+ // OmpTargetAllocMemOp
410
+ // ===----------------------------------------------------------------------===//
411
+
412
+ mlir::Type fir::OmpTargetAllocMemOp::getAllocatedType () {
413
+ return mlir::cast<fir::HeapType>(getType ()).getEleTy ();
414
+ }
415
+
416
+ mlir::Type fir::OmpTargetAllocMemOp::getRefTy (mlir::Type ty) {
417
+ return fir::HeapType::get (ty);
418
+ }
419
+
420
+ mlir::ParseResult
421
+ fir::OmpTargetAllocMemOp::parse (mlir::OpAsmParser &parser,
422
+ mlir::OperationState &result) {
423
+ return parseAllocatableOp (wrapAllocMemResultType, parser, result, true );
424
+ }
425
+
426
+ void fir::OmpTargetAllocMemOp::print (mlir::OpAsmPrinter &p) {
427
+ p << " " ;
428
+ p.printOperand (getDevice ());
429
+ p << " : " ;
430
+ p << getDevice ().getType ();
431
+ p << " , " ;
432
+ p << getInType ();
433
+ if (!getTypeparams ().empty ()) {
434
+ p << ' (' << getTypeparams () << " : " << getTypeparams ().getTypes () << ' )' ;
435
+ }
436
+ for (auto sh : getShape ()) {
437
+ p << " , " ;
438
+ p.printOperand (sh);
439
+ }
440
+ p.printOptionalAttrDict ((*this )->getAttrs (),
441
+ {" in_type" , " operandSegmentSizes" });
442
+ }
443
+
444
+ llvm::LogicalResult fir::OmpTargetAllocMemOp::verify () {
445
+ llvm::SmallVector<llvm::StringRef> visited;
446
+ if (verifyInType (getInType (), visited, numShapeOperands ()))
447
+ return emitOpError (" invalid type for allocation" );
448
+ if (verifyTypeParamCount (getInType (), numLenParams ()))
449
+ return emitOpError (" LEN params do not correspond to type" );
450
+ mlir::Type outType = getType ();
451
+ if (!mlir::dyn_cast<fir::HeapType>(outType))
452
+ return emitOpError (" must be a !fir.heap type" );
453
+ if (fir::isa_unknown_size_box (fir::dyn_cast_ptrEleTy (outType)))
454
+ return emitOpError (" cannot allocate !fir.box of unknown rank or type" );
455
+ return mlir::success ();
456
+ }
457
+
388
458
// ===----------------------------------------------------------------------===//
389
459
// ArrayCoorOp
390
460
// ===----------------------------------------------------------------------===//
0 commit comments