1 //===-- FIROps.cpp --------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Coding style: https://mlir.llvm.org/getting_started/DeveloperGuide/
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "flang/Optimizer/Dialect/FIROps.h"
14 #include "flang/Optimizer/Dialect/FIRAttr.h"
15 #include "flang/Optimizer/Dialect/FIROpsSupport.h"
16 #include "flang/Optimizer/Dialect/FIRType.h"
17 #include "flang/Optimizer/Support/Utils.h"
18 #include "mlir/Dialect/CommonFolders.h"
19 #include "mlir/Dialect/StandardOps/IR/Ops.h"
20 #include "mlir/IR/BuiltinAttributes.h"
21 #include "mlir/IR/BuiltinOps.h"
22 #include "mlir/IR/Diagnostics.h"
23 #include "mlir/IR/Matchers.h"
24 #include "mlir/IR/OpDefinition.h"
25 #include "mlir/IR/PatternMatch.h"
26 #include "llvm/ADT/STLExtras.h"
27 #include "llvm/ADT/SmallVector.h"
28 #include "llvm/ADT/StringSwitch.h"
29 #include "llvm/ADT/TypeSwitch.h"
30 
31 namespace {
32 #include "flang/Optimizer/Dialect/CanonicalizationPatterns.inc"
33 } // namespace
34 using namespace fir;
35 
36 /// Return true if a sequence type is of some incomplete size or a record type
37 /// is malformed or contains an incomplete sequence type. An incomplete sequence
38 /// type is one with more unknown extents in the type than have been provided
39 /// via `dynamicExtents`. Sequence types with an unknown rank are incomplete by
40 /// definition.
41 static bool verifyInType(mlir::Type inType,
42                          llvm::SmallVectorImpl<llvm::StringRef> &visited,
43                          unsigned dynamicExtents = 0) {
44   if (auto st = inType.dyn_cast<fir::SequenceType>()) {
45     auto shape = st.getShape();
46     if (shape.size() == 0)
47       return true;
48     for (std::size_t i = 0, end{shape.size()}; i < end; ++i) {
49       if (shape[i] != fir::SequenceType::getUnknownExtent())
50         continue;
51       if (dynamicExtents-- == 0)
52         return true;
53     }
54   } else if (auto rt = inType.dyn_cast<fir::RecordType>()) {
55     // don't recurse if we're already visiting this one
56     if (llvm::is_contained(visited, rt.getName()))
57       return false;
58     // keep track of record types currently being visited
59     visited.push_back(rt.getName());
60     for (auto &field : rt.getTypeList())
61       if (verifyInType(field.second, visited))
62         return true;
63     visited.pop_back();
64   }
65   return false;
66 }
67 
68 static bool verifyTypeParamCount(mlir::Type inType, unsigned numParams) {
69   auto ty = fir::unwrapSequenceType(inType);
70   if (numParams > 0) {
71     if (auto recTy = ty.dyn_cast<fir::RecordType>())
72       return numParams != recTy.getNumLenParams();
73     if (auto chrTy = ty.dyn_cast<fir::CharacterType>())
74       return !(numParams == 1 && chrTy.hasDynamicLen());
75     return true;
76   }
77   if (auto chrTy = ty.dyn_cast<fir::CharacterType>())
78     return !chrTy.hasConstantLen();
79   return false;
80 }
81 
82 /// Parser shared by Alloca and Allocmem
83 ///
84 /// operation ::= %res = (`fir.alloca` | `fir.allocmem`) $in_type
85 ///                      ( `(` $typeparams `)` )? ( `,` $shape )?
86 ///                      attr-dict-without-keyword
87 template <typename FN>
88 static mlir::ParseResult parseAllocatableOp(FN wrapResultType,
89                                             mlir::OpAsmParser &parser,
90                                             mlir::OperationState &result) {
91   mlir::Type intype;
92   if (parser.parseType(intype))
93     return mlir::failure();
94   auto &builder = parser.getBuilder();
95   result.addAttribute("in_type", mlir::TypeAttr::get(intype));
96   llvm::SmallVector<mlir::OpAsmParser::OperandType> operands;
97   llvm::SmallVector<mlir::Type> typeVec;
98   bool hasOperands = false;
99   std::int32_t typeparamsSize = 0;
100   if (!parser.parseOptionalLParen()) {
101     // parse the LEN params of the derived type. (<params> : <types>)
102     if (parser.parseOperandList(operands, mlir::OpAsmParser::Delimiter::None) ||
103         parser.parseColonTypeList(typeVec) || parser.parseRParen())
104       return mlir::failure();
105     typeparamsSize = operands.size();
106     hasOperands = true;
107   }
108   std::int32_t shapeSize = 0;
109   if (!parser.parseOptionalComma()) {
110     // parse size to scale by, vector of n dimensions of type index
111     if (parser.parseOperandList(operands, mlir::OpAsmParser::Delimiter::None))
112       return mlir::failure();
113     shapeSize = operands.size() - typeparamsSize;
114     auto idxTy = builder.getIndexType();
115     for (std::int32_t i = typeparamsSize, end = operands.size(); i != end; ++i)
116       typeVec.push_back(idxTy);
117     hasOperands = true;
118   }
119   if (hasOperands &&
120       parser.resolveOperands(operands, typeVec, parser.getNameLoc(),
121                              result.operands))
122     return mlir::failure();
123   mlir::Type restype = wrapResultType(intype);
124   if (!restype) {
125     parser.emitError(parser.getNameLoc(), "invalid allocate type: ") << intype;
126     return mlir::failure();
127   }
128   result.addAttribute("operand_segment_sizes",
129                       builder.getI32VectorAttr({typeparamsSize, shapeSize}));
130   if (parser.parseOptionalAttrDict(result.attributes) ||
131       parser.addTypeToList(restype, result.types))
132     return mlir::failure();
133   return mlir::success();
134 }
135 
136 template <typename OP>
137 static void printAllocatableOp(mlir::OpAsmPrinter &p, OP &op) {
138   p << ' ' << op.in_type();
139   if (!op.typeparams().empty()) {
140     p << '(' << op.typeparams() << " : " << op.typeparams().getTypes() << ')';
141   }
142   // print the shape of the allocation (if any); all must be index type
143   for (auto sh : op.shape()) {
144     p << ", ";
145     p.printOperand(sh);
146   }
147   p.printOptionalAttrDict(op->getAttrs(), {"in_type", "operand_segment_sizes"});
148 }
149 
150 //===----------------------------------------------------------------------===//
151 // AllocaOp
152 //===----------------------------------------------------------------------===//
153 
154 /// Create a legal memory reference as return type
155 static mlir::Type wrapAllocaResultType(mlir::Type intype) {
156   // FIR semantics: memory references to memory references are disallowed
157   if (intype.isa<ReferenceType>())
158     return {};
159   return ReferenceType::get(intype);
160 }
161 
162 mlir::Type fir::AllocaOp::getAllocatedType() {
163   return getType().cast<ReferenceType>().getEleTy();
164 }
165 
166 mlir::Type fir::AllocaOp::getRefTy(mlir::Type ty) {
167   return ReferenceType::get(ty);
168 }
169 
170 void fir::AllocaOp::build(mlir::OpBuilder &builder,
171                           mlir::OperationState &result, mlir::Type inType,
172                           llvm::StringRef uniqName, mlir::ValueRange typeparams,
173                           mlir::ValueRange shape,
174                           llvm::ArrayRef<mlir::NamedAttribute> attributes) {
175   auto nameAttr = builder.getStringAttr(uniqName);
176   build(builder, result, wrapAllocaResultType(inType), inType, nameAttr, {},
177         /*pinned=*/false, typeparams, shape);
178   result.addAttributes(attributes);
179 }
180 
181 void fir::AllocaOp::build(mlir::OpBuilder &builder,
182                           mlir::OperationState &result, mlir::Type inType,
183                           llvm::StringRef uniqName, bool pinned,
184                           mlir::ValueRange typeparams, mlir::ValueRange shape,
185                           llvm::ArrayRef<mlir::NamedAttribute> attributes) {
186   auto nameAttr = builder.getStringAttr(uniqName);
187   build(builder, result, wrapAllocaResultType(inType), inType, nameAttr, {},
188         pinned, typeparams, shape);
189   result.addAttributes(attributes);
190 }
191 
192 void fir::AllocaOp::build(mlir::OpBuilder &builder,
193                           mlir::OperationState &result, mlir::Type inType,
194                           llvm::StringRef uniqName, llvm::StringRef bindcName,
195                           mlir::ValueRange typeparams, mlir::ValueRange shape,
196                           llvm::ArrayRef<mlir::NamedAttribute> attributes) {
197   auto nameAttr =
198       uniqName.empty() ? mlir::StringAttr{} : builder.getStringAttr(uniqName);
199   auto bindcAttr =
200       bindcName.empty() ? mlir::StringAttr{} : builder.getStringAttr(bindcName);
201   build(builder, result, wrapAllocaResultType(inType), inType, nameAttr,
202         bindcAttr, /*pinned=*/false, typeparams, shape);
203   result.addAttributes(attributes);
204 }
205 
206 void fir::AllocaOp::build(mlir::OpBuilder &builder,
207                           mlir::OperationState &result, mlir::Type inType,
208                           llvm::StringRef uniqName, llvm::StringRef bindcName,
209                           bool pinned, mlir::ValueRange typeparams,
210                           mlir::ValueRange shape,
211                           llvm::ArrayRef<mlir::NamedAttribute> attributes) {
212   auto nameAttr =
213       uniqName.empty() ? mlir::StringAttr{} : builder.getStringAttr(uniqName);
214   auto bindcAttr =
215       bindcName.empty() ? mlir::StringAttr{} : builder.getStringAttr(bindcName);
216   build(builder, result, wrapAllocaResultType(inType), inType, nameAttr,
217         bindcAttr, pinned, typeparams, shape);
218   result.addAttributes(attributes);
219 }
220 
221 void fir::AllocaOp::build(mlir::OpBuilder &builder,
222                           mlir::OperationState &result, mlir::Type inType,
223                           mlir::ValueRange typeparams, mlir::ValueRange shape,
224                           llvm::ArrayRef<mlir::NamedAttribute> attributes) {
225   build(builder, result, wrapAllocaResultType(inType), inType, {}, {},
226         /*pinned=*/false, typeparams, shape);
227   result.addAttributes(attributes);
228 }
229 
230 void fir::AllocaOp::build(mlir::OpBuilder &builder,
231                           mlir::OperationState &result, mlir::Type inType,
232                           bool pinned, mlir::ValueRange typeparams,
233                           mlir::ValueRange shape,
234                           llvm::ArrayRef<mlir::NamedAttribute> attributes) {
235   build(builder, result, wrapAllocaResultType(inType), inType, {}, {}, pinned,
236         typeparams, shape);
237   result.addAttributes(attributes);
238 }
239 
240 static mlir::LogicalResult verify(fir::AllocaOp &op) {
241   llvm::SmallVector<llvm::StringRef> visited;
242   if (verifyInType(op.getInType(), visited, op.numShapeOperands()))
243     return op.emitOpError("invalid type for allocation");
244   if (verifyTypeParamCount(op.getInType(), op.numLenParams()))
245     return op.emitOpError("LEN params do not correspond to type");
246   mlir::Type outType = op.getType();
247   if (!outType.isa<fir::ReferenceType>())
248     return op.emitOpError("must be a !fir.ref type");
249   if (fir::isa_unknown_size_box(fir::dyn_cast_ptrEleTy(outType)))
250     return op.emitOpError("cannot allocate !fir.box of unknown rank or type");
251   return mlir::success();
252 }
253 
254 //===----------------------------------------------------------------------===//
255 // AllocMemOp
256 //===----------------------------------------------------------------------===//
257 
258 /// Create a legal heap reference as return type
259 static mlir::Type wrapAllocMemResultType(mlir::Type intype) {
260   // Fortran semantics: C852 an entity cannot be both ALLOCATABLE and POINTER
261   // 8.5.3 note 1 prohibits ALLOCATABLE procedures as well
262   // FIR semantics: one may not allocate a memory reference value
263   if (intype.isa<ReferenceType>() || intype.isa<HeapType>() ||
264       intype.isa<PointerType>() || intype.isa<FunctionType>())
265     return {};
266   return HeapType::get(intype);
267 }
268 
269 mlir::Type fir::AllocMemOp::getAllocatedType() {
270   return getType().cast<HeapType>().getEleTy();
271 }
272 
273 mlir::Type fir::AllocMemOp::getRefTy(mlir::Type ty) {
274   return HeapType::get(ty);
275 }
276 
277 void fir::AllocMemOp::build(mlir::OpBuilder &builder,
278                             mlir::OperationState &result, mlir::Type inType,
279                             llvm::StringRef uniqName,
280                             mlir::ValueRange typeparams, mlir::ValueRange shape,
281                             llvm::ArrayRef<mlir::NamedAttribute> attributes) {
282   auto nameAttr = builder.getStringAttr(uniqName);
283   build(builder, result, wrapAllocMemResultType(inType), inType, nameAttr, {},
284         typeparams, shape);
285   result.addAttributes(attributes);
286 }
287 
288 void fir::AllocMemOp::build(mlir::OpBuilder &builder,
289                             mlir::OperationState &result, mlir::Type inType,
290                             llvm::StringRef uniqName, llvm::StringRef bindcName,
291                             mlir::ValueRange typeparams, mlir::ValueRange shape,
292                             llvm::ArrayRef<mlir::NamedAttribute> attributes) {
293   auto nameAttr = builder.getStringAttr(uniqName);
294   auto bindcAttr = builder.getStringAttr(bindcName);
295   build(builder, result, wrapAllocMemResultType(inType), inType, nameAttr,
296         bindcAttr, typeparams, shape);
297   result.addAttributes(attributes);
298 }
299 
300 void fir::AllocMemOp::build(mlir::OpBuilder &builder,
301                             mlir::OperationState &result, mlir::Type inType,
302                             mlir::ValueRange typeparams, mlir::ValueRange shape,
303                             llvm::ArrayRef<mlir::NamedAttribute> attributes) {
304   build(builder, result, wrapAllocMemResultType(inType), inType, {}, {},
305         typeparams, shape);
306   result.addAttributes(attributes);
307 }
308 
309 static mlir::LogicalResult verify(fir::AllocMemOp op) {
310   llvm::SmallVector<llvm::StringRef> visited;
311   if (verifyInType(op.getInType(), visited, op.numShapeOperands()))
312     return op.emitOpError("invalid type for allocation");
313   if (verifyTypeParamCount(op.getInType(), op.numLenParams()))
314     return op.emitOpError("LEN params do not correspond to type");
315   mlir::Type outType = op.getType();
316   if (!outType.dyn_cast<fir::HeapType>())
317     return op.emitOpError("must be a !fir.heap type");
318   if (fir::isa_unknown_size_box(fir::dyn_cast_ptrEleTy(outType)))
319     return op.emitOpError("cannot allocate !fir.box of unknown rank or type");
320   return mlir::success();
321 }
322 
323 //===----------------------------------------------------------------------===//
324 // ArrayCoorOp
325 //===----------------------------------------------------------------------===//
326 
327 static mlir::LogicalResult verify(fir::ArrayCoorOp op) {
328   auto eleTy = fir::dyn_cast_ptrOrBoxEleTy(op.memref().getType());
329   auto arrTy = eleTy.dyn_cast<fir::SequenceType>();
330   if (!arrTy)
331     return op.emitOpError("must be a reference to an array");
332   auto arrDim = arrTy.getDimension();
333 
334   if (auto shapeOp = op.shape()) {
335     auto shapeTy = shapeOp.getType();
336     unsigned shapeTyRank = 0;
337     if (auto s = shapeTy.dyn_cast<fir::ShapeType>()) {
338       shapeTyRank = s.getRank();
339     } else if (auto ss = shapeTy.dyn_cast<fir::ShapeShiftType>()) {
340       shapeTyRank = ss.getRank();
341     } else {
342       auto s = shapeTy.cast<fir::ShiftType>();
343       shapeTyRank = s.getRank();
344       if (!op.memref().getType().isa<fir::BoxType>())
345         return op.emitOpError("shift can only be provided with fir.box memref");
346     }
347     if (arrDim && arrDim != shapeTyRank)
348       return op.emitOpError("rank of dimension mismatched");
349     if (shapeTyRank != op.indices().size())
350       return op.emitOpError("number of indices do not match dim rank");
351   }
352 
353   if (auto sliceOp = op.slice()) {
354     if (auto sl = mlir::dyn_cast_or_null<fir::SliceOp>(sliceOp.getDefiningOp()))
355       if (!sl.substr().empty())
356         return op.emitOpError("array_coor cannot take a slice with substring");
357     if (auto sliceTy = sliceOp.getType().dyn_cast<fir::SliceType>())
358       if (sliceTy.getRank() != arrDim)
359         return op.emitOpError("rank of dimension in slice mismatched");
360   }
361 
362   return mlir::success();
363 }
364 
365 //===----------------------------------------------------------------------===//
366 // ArrayLoadOp
367 //===----------------------------------------------------------------------===//
368 
369 static mlir::Type adjustedElementType(mlir::Type t) {
370   if (auto ty = t.dyn_cast<fir::ReferenceType>()) {
371     auto eleTy = ty.getEleTy();
372     if (fir::isa_char(eleTy))
373       return eleTy;
374     if (fir::isa_derived(eleTy))
375       return eleTy;
376     if (eleTy.isa<fir::SequenceType>())
377       return eleTy;
378   }
379   return t;
380 }
381 
382 std::vector<mlir::Value> fir::ArrayLoadOp::getExtents() {
383   if (auto sh = shape())
384     if (auto *op = sh.getDefiningOp()) {
385       if (auto shOp = dyn_cast<fir::ShapeOp>(op)) {
386         auto extents = shOp.getExtents();
387         return {extents.begin(), extents.end()};
388       }
389       return cast<fir::ShapeShiftOp>(op).getExtents();
390     }
391   return {};
392 }
393 
394 static mlir::LogicalResult verify(fir::ArrayLoadOp op) {
395   auto eleTy = fir::dyn_cast_ptrOrBoxEleTy(op.memref().getType());
396   auto arrTy = eleTy.dyn_cast<fir::SequenceType>();
397   if (!arrTy)
398     return op.emitOpError("must be a reference to an array");
399   auto arrDim = arrTy.getDimension();
400 
401   if (auto shapeOp = op.shape()) {
402     auto shapeTy = shapeOp.getType();
403     unsigned shapeTyRank = 0;
404     if (auto s = shapeTy.dyn_cast<fir::ShapeType>()) {
405       shapeTyRank = s.getRank();
406     } else if (auto ss = shapeTy.dyn_cast<fir::ShapeShiftType>()) {
407       shapeTyRank = ss.getRank();
408     } else {
409       auto s = shapeTy.cast<fir::ShiftType>();
410       shapeTyRank = s.getRank();
411       if (!op.memref().getType().isa<fir::BoxType>())
412         return op.emitOpError("shift can only be provided with fir.box memref");
413     }
414     if (arrDim && arrDim != shapeTyRank)
415       return op.emitOpError("rank of dimension mismatched");
416   }
417 
418   if (auto sliceOp = op.slice()) {
419     if (auto sl = mlir::dyn_cast_or_null<fir::SliceOp>(sliceOp.getDefiningOp()))
420       if (!sl.substr().empty())
421         return op.emitOpError("array_load cannot take a slice with substring");
422     if (auto sliceTy = sliceOp.getType().dyn_cast<fir::SliceType>())
423       if (sliceTy.getRank() != arrDim)
424         return op.emitOpError("rank of dimension in slice mismatched");
425   }
426 
427   return mlir::success();
428 }
429 
430 //===----------------------------------------------------------------------===//
431 // ArrayMergeStoreOp
432 //===----------------------------------------------------------------------===//
433 
434 static mlir::LogicalResult verify(fir::ArrayMergeStoreOp op) {
435   if (!isa<ArrayLoadOp>(op.original().getDefiningOp()))
436     return op.emitOpError("operand #0 must be result of a fir.array_load op");
437   if (auto sl = op.slice()) {
438     if (auto sliceOp =
439             mlir::dyn_cast_or_null<fir::SliceOp>(sl.getDefiningOp())) {
440       if (!sliceOp.substr().empty())
441         return op.emitOpError(
442             "array_merge_store cannot take a slice with substring");
443       if (!sliceOp.fields().empty()) {
444         // This is an intra-object merge, where the slice is projecting the
445         // subfields that are to be overwritten by the merge operation.
446         auto eleTy = fir::dyn_cast_ptrOrBoxEleTy(op.memref().getType());
447         if (auto seqTy = eleTy.dyn_cast<fir::SequenceType>()) {
448           auto projTy =
449               fir::applyPathToType(seqTy.getEleTy(), sliceOp.fields());
450           if (fir::unwrapSequenceType(op.original().getType()) != projTy)
451             return op.emitOpError(
452                 "type of origin does not match sliced memref type");
453           if (fir::unwrapSequenceType(op.sequence().getType()) != projTy)
454             return op.emitOpError(
455                 "type of sequence does not match sliced memref type");
456           return mlir::success();
457         }
458         return op.emitOpError("referenced type is not an array");
459       }
460     }
461     return mlir::success();
462   }
463   auto eleTy = fir::dyn_cast_ptrOrBoxEleTy(op.memref().getType());
464   if (op.original().getType() != eleTy)
465     return op.emitOpError("type of origin does not match memref element type");
466   if (op.sequence().getType() != eleTy)
467     return op.emitOpError(
468         "type of sequence does not match memref element type");
469   return mlir::success();
470 }
471 
472 //===----------------------------------------------------------------------===//
473 // ArrayFetchOp
474 //===----------------------------------------------------------------------===//
475 
476 // Template function used for both array_fetch and array_update verification.
477 template <typename A>
478 mlir::Type validArraySubobject(A op) {
479   auto ty = op.sequence().getType();
480   return fir::applyPathToType(ty, op.indices());
481 }
482 
483 static mlir::LogicalResult verify(fir::ArrayFetchOp op) {
484   auto arrTy = op.sequence().getType().cast<fir::SequenceType>();
485   auto indSize = op.indices().size();
486   if (indSize < arrTy.getDimension())
487     return op.emitOpError("number of indices != dimension of array");
488   if (indSize == arrTy.getDimension() &&
489       ::adjustedElementType(op.element().getType()) != arrTy.getEleTy())
490     return op.emitOpError("return type does not match array");
491   auto ty = validArraySubobject(op);
492   if (!ty || ty != ::adjustedElementType(op.getType()))
493     return op.emitOpError("return type and/or indices do not type check");
494   if (!isa<fir::ArrayLoadOp>(op.sequence().getDefiningOp()))
495     return op.emitOpError("argument #0 must be result of fir.array_load");
496   return mlir::success();
497 }
498 
499 //===----------------------------------------------------------------------===//
500 // ArrayAccessOp
501 //===----------------------------------------------------------------------===//
502 
503 static mlir::LogicalResult verify(fir::ArrayAccessOp op) {
504   auto arrTy = op.sequence().getType().cast<fir::SequenceType>();
505   std::size_t indSize = op.indices().size();
506   if (indSize < arrTy.getDimension())
507     return op.emitOpError("number of indices != dimension of array");
508   if (indSize == arrTy.getDimension() &&
509       op.element().getType() != fir::ReferenceType::get(arrTy.getEleTy()))
510     return op.emitOpError("return type does not match array");
511   mlir::Type ty = validArraySubobject(op);
512   if (!ty || fir::ReferenceType::get(ty) != op.getType())
513     return op.emitOpError("return type and/or indices do not type check");
514   return mlir::success();
515 }
516 
517 //===----------------------------------------------------------------------===//
518 // ArrayUpdateOp
519 //===----------------------------------------------------------------------===//
520 
521 static mlir::LogicalResult verify(fir::ArrayUpdateOp op) {
522   if (fir::isa_ref_type(op.merge().getType()))
523     return op.emitOpError("does not support reference type for merge");
524   auto arrTy = op.sequence().getType().cast<fir::SequenceType>();
525   auto indSize = op.indices().size();
526   if (indSize < arrTy.getDimension())
527     return op.emitOpError("number of indices != dimension of array");
528   if (indSize == arrTy.getDimension() &&
529       ::adjustedElementType(op.merge().getType()) != arrTy.getEleTy())
530     return op.emitOpError("merged value does not have element type");
531   auto ty = validArraySubobject(op);
532   if (!ty || ty != ::adjustedElementType(op.merge().getType()))
533     return op.emitOpError("merged value and/or indices do not type check");
534   return mlir::success();
535 }
536 
537 //===----------------------------------------------------------------------===//
538 // ArrayModifyOp
539 //===----------------------------------------------------------------------===//
540 
541 static mlir::LogicalResult verify(fir::ArrayModifyOp op) {
542   auto arrTy = op.sequence().getType().cast<fir::SequenceType>();
543   auto indSize = op.indices().size();
544   if (indSize < arrTy.getDimension())
545     return op.emitOpError("number of indices must match array dimension");
546   return mlir::success();
547 }
548 
549 //===----------------------------------------------------------------------===//
550 // BoxAddrOp
551 //===----------------------------------------------------------------------===//
552 
553 mlir::OpFoldResult fir::BoxAddrOp::fold(llvm::ArrayRef<mlir::Attribute> opnds) {
554   if (auto v = val().getDefiningOp()) {
555     if (auto box = dyn_cast<fir::EmboxOp>(v))
556       return box.memref();
557     if (auto box = dyn_cast<fir::EmboxCharOp>(v))
558       return box.memref();
559   }
560   return {};
561 }
562 
563 //===----------------------------------------------------------------------===//
564 // BoxCharLenOp
565 //===----------------------------------------------------------------------===//
566 
567 mlir::OpFoldResult
568 fir::BoxCharLenOp::fold(llvm::ArrayRef<mlir::Attribute> opnds) {
569   if (auto v = val().getDefiningOp()) {
570     if (auto box = dyn_cast<fir::EmboxCharOp>(v))
571       return box.len();
572   }
573   return {};
574 }
575 
576 //===----------------------------------------------------------------------===//
577 // BoxDimsOp
578 //===----------------------------------------------------------------------===//
579 
580 /// Get the result types packed in a tuple tuple
581 mlir::Type fir::BoxDimsOp::getTupleType() {
582   // note: triple, but 4 is nearest power of 2
583   llvm::SmallVector<mlir::Type> triple{
584       getResult(0).getType(), getResult(1).getType(), getResult(2).getType()};
585   return mlir::TupleType::get(getContext(), triple);
586 }
587 
588 //===----------------------------------------------------------------------===//
589 // CallOp
590 //===----------------------------------------------------------------------===//
591 
592 mlir::FunctionType fir::CallOp::getFunctionType() {
593   return mlir::FunctionType::get(getContext(), getOperandTypes(),
594                                  getResultTypes());
595 }
596 
597 static void printCallOp(mlir::OpAsmPrinter &p, fir::CallOp &op) {
598   auto callee = op.callee();
599   bool isDirect = callee.hasValue();
600   p << ' ';
601   if (isDirect)
602     p << callee.getValue();
603   else
604     p << op.getOperand(0);
605   p << '(' << op->getOperands().drop_front(isDirect ? 0 : 1) << ')';
606   p.printOptionalAttrDict(op->getAttrs(),
607                           {fir::CallOp::getCalleeAttrNameStr()});
608   auto resultTypes{op.getResultTypes()};
609   llvm::SmallVector<Type> argTypes(
610       llvm::drop_begin(op.getOperandTypes(), isDirect ? 0 : 1));
611   p << " : " << FunctionType::get(op.getContext(), argTypes, resultTypes);
612 }
613 
614 static mlir::ParseResult parseCallOp(mlir::OpAsmParser &parser,
615                                      mlir::OperationState &result) {
616   llvm::SmallVector<mlir::OpAsmParser::OperandType> operands;
617   if (parser.parseOperandList(operands))
618     return mlir::failure();
619 
620   mlir::NamedAttrList attrs;
621   mlir::SymbolRefAttr funcAttr;
622   bool isDirect = operands.empty();
623   if (isDirect)
624     if (parser.parseAttribute(funcAttr, fir::CallOp::getCalleeAttrNameStr(),
625                               attrs))
626       return mlir::failure();
627 
628   Type type;
629   if (parser.parseOperandList(operands, mlir::OpAsmParser::Delimiter::Paren) ||
630       parser.parseOptionalAttrDict(attrs) || parser.parseColon() ||
631       parser.parseType(type))
632     return mlir::failure();
633 
634   auto funcType = type.dyn_cast<mlir::FunctionType>();
635   if (!funcType)
636     return parser.emitError(parser.getNameLoc(), "expected function type");
637   if (isDirect) {
638     if (parser.resolveOperands(operands, funcType.getInputs(),
639                                parser.getNameLoc(), result.operands))
640       return mlir::failure();
641   } else {
642     auto funcArgs =
643         llvm::ArrayRef<mlir::OpAsmParser::OperandType>(operands).drop_front();
644     if (parser.resolveOperand(operands[0], funcType, result.operands) ||
645         parser.resolveOperands(funcArgs, funcType.getInputs(),
646                                parser.getNameLoc(), result.operands))
647       return mlir::failure();
648   }
649   result.addTypes(funcType.getResults());
650   result.attributes = attrs;
651   return mlir::success();
652 }
653 
654 void fir::CallOp::build(mlir::OpBuilder &builder, mlir::OperationState &result,
655                         mlir::FuncOp callee, mlir::ValueRange operands) {
656   result.addOperands(operands);
657   result.addAttribute(getCalleeAttrNameStr(), SymbolRefAttr::get(callee));
658   result.addTypes(callee.getType().getResults());
659 }
660 
661 void fir::CallOp::build(mlir::OpBuilder &builder, mlir::OperationState &result,
662                         mlir::SymbolRefAttr callee,
663                         llvm::ArrayRef<mlir::Type> results,
664                         mlir::ValueRange operands) {
665   result.addOperands(operands);
666   if (callee)
667     result.addAttribute(getCalleeAttrNameStr(), callee);
668   result.addTypes(results);
669 }
670 
671 //===----------------------------------------------------------------------===//
672 // CmpOp
673 //===----------------------------------------------------------------------===//
674 
675 template <typename OPTY>
676 static void printCmpOp(OpAsmPrinter &p, OPTY op) {
677   p << ' ';
678   auto predSym = mlir::arith::symbolizeCmpFPredicate(
679       op->template getAttrOfType<mlir::IntegerAttr>(
680             OPTY::getPredicateAttrName())
681           .getInt());
682   assert(predSym.hasValue() && "invalid symbol value for predicate");
683   p << '"' << mlir::arith::stringifyCmpFPredicate(predSym.getValue()) << '"'
684     << ", ";
685   p.printOperand(op.lhs());
686   p << ", ";
687   p.printOperand(op.rhs());
688   p.printOptionalAttrDict(op->getAttrs(),
689                           /*elidedAttrs=*/{OPTY::getPredicateAttrName()});
690   p << " : " << op.lhs().getType();
691 }
692 
693 template <typename OPTY>
694 static mlir::ParseResult parseCmpOp(mlir::OpAsmParser &parser,
695                                     mlir::OperationState &result) {
696   llvm::SmallVector<mlir::OpAsmParser::OperandType> ops;
697   mlir::NamedAttrList attrs;
698   mlir::Attribute predicateNameAttr;
699   mlir::Type type;
700   if (parser.parseAttribute(predicateNameAttr, OPTY::getPredicateAttrName(),
701                             attrs) ||
702       parser.parseComma() || parser.parseOperandList(ops, 2) ||
703       parser.parseOptionalAttrDict(attrs) || parser.parseColonType(type) ||
704       parser.resolveOperands(ops, type, result.operands))
705     return failure();
706 
707   if (!predicateNameAttr.isa<mlir::StringAttr>())
708     return parser.emitError(parser.getNameLoc(),
709                             "expected string comparison predicate attribute");
710 
711   // Rewrite string attribute to an enum value.
712   llvm::StringRef predicateName =
713       predicateNameAttr.cast<mlir::StringAttr>().getValue();
714   auto predicate = fir::CmpcOp::getPredicateByName(predicateName);
715   auto builder = parser.getBuilder();
716   mlir::Type i1Type = builder.getI1Type();
717   attrs.set(OPTY::getPredicateAttrName(),
718             builder.getI64IntegerAttr(static_cast<int64_t>(predicate)));
719   result.attributes = attrs;
720   result.addTypes({i1Type});
721   return success();
722 }
723 
724 //===----------------------------------------------------------------------===//
725 // CharConvertOp
726 //===----------------------------------------------------------------------===//
727 
728 static mlir::LogicalResult verify(fir::CharConvertOp op) {
729   auto unwrap = [&](mlir::Type t) {
730     t = fir::unwrapSequenceType(fir::dyn_cast_ptrEleTy(t));
731     return t.dyn_cast<fir::CharacterType>();
732   };
733   auto inTy = unwrap(op.from().getType());
734   auto outTy = unwrap(op.to().getType());
735   if (!(inTy && outTy))
736     return op.emitOpError("not a reference to a character");
737   if (inTy.getFKind() == outTy.getFKind())
738     return op.emitOpError("buffers must have different KIND values");
739   return mlir::success();
740 }
741 
742 //===----------------------------------------------------------------------===//
743 // CmpcOp
744 //===----------------------------------------------------------------------===//
745 
746 void fir::buildCmpCOp(OpBuilder &builder, OperationState &result,
747                       arith::CmpFPredicate predicate, Value lhs, Value rhs) {
748   result.addOperands({lhs, rhs});
749   result.types.push_back(builder.getI1Type());
750   result.addAttribute(
751       fir::CmpcOp::getPredicateAttrName(),
752       builder.getI64IntegerAttr(static_cast<int64_t>(predicate)));
753 }
754 
755 mlir::arith::CmpFPredicate
756 fir::CmpcOp::getPredicateByName(llvm::StringRef name) {
757   auto pred = mlir::arith::symbolizeCmpFPredicate(name);
758   assert(pred.hasValue() && "invalid predicate name");
759   return pred.getValue();
760 }
761 
762 static void printCmpcOp(OpAsmPrinter &p, fir::CmpcOp op) { printCmpOp(p, op); }
763 
764 mlir::ParseResult fir::parseCmpcOp(mlir::OpAsmParser &parser,
765                                    mlir::OperationState &result) {
766   return parseCmpOp<fir::CmpcOp>(parser, result);
767 }
768 
769 //===----------------------------------------------------------------------===//
770 // ConstcOp
771 //===----------------------------------------------------------------------===//
772 
773 static mlir::ParseResult parseConstcOp(mlir::OpAsmParser &parser,
774                                        mlir::OperationState &result) {
775   fir::RealAttr realp;
776   fir::RealAttr imagp;
777   mlir::Type type;
778   if (parser.parseLParen() ||
779       parser.parseAttribute(realp, fir::ConstcOp::realAttrName(),
780                             result.attributes) ||
781       parser.parseComma() ||
782       parser.parseAttribute(imagp, fir::ConstcOp::imagAttrName(),
783                             result.attributes) ||
784       parser.parseRParen() || parser.parseColonType(type) ||
785       parser.addTypesToList(type, result.types))
786     return mlir::failure();
787   return mlir::success();
788 }
789 
790 static void print(mlir::OpAsmPrinter &p, fir::ConstcOp &op) {
791   p << '(';
792   p << op.getOperation()->getAttr(fir::ConstcOp::realAttrName()) << ", ";
793   p << op.getOperation()->getAttr(fir::ConstcOp::imagAttrName()) << ") : ";
794   p.printType(op.getType());
795 }
796 
797 static mlir::LogicalResult verify(fir::ConstcOp &op) {
798   if (!op.getType().isa<fir::ComplexType>())
799     return op.emitOpError("must be a !fir.complex type");
800   return mlir::success();
801 }
802 
803 //===----------------------------------------------------------------------===//
804 // ConvertOp
805 //===----------------------------------------------------------------------===//
806 
807 void fir::ConvertOp::getCanonicalizationPatterns(RewritePatternSet &results,
808                                                  MLIRContext *context) {
809   results.insert<ConvertConvertOptPattern, RedundantConvertOptPattern,
810                  CombineConvertOptPattern, ForwardConstantConvertPattern>(
811       context);
812 }
813 
814 mlir::OpFoldResult fir::ConvertOp::fold(llvm::ArrayRef<mlir::Attribute> opnds) {
815   if (value().getType() == getType())
816     return value();
817   if (matchPattern(value(), m_Op<fir::ConvertOp>())) {
818     auto inner = cast<fir::ConvertOp>(value().getDefiningOp());
819     // (convert (convert 'a : logical -> i1) : i1 -> logical) ==> forward 'a
820     if (auto toTy = getType().dyn_cast<fir::LogicalType>())
821       if (auto fromTy = inner.value().getType().dyn_cast<fir::LogicalType>())
822         if (inner.getType().isa<mlir::IntegerType>() && (toTy == fromTy))
823           return inner.value();
824     // (convert (convert 'a : i1 -> logical) : logical -> i1) ==> forward 'a
825     if (auto toTy = getType().dyn_cast<mlir::IntegerType>())
826       if (auto fromTy = inner.value().getType().dyn_cast<mlir::IntegerType>())
827         if (inner.getType().isa<fir::LogicalType>() && (toTy == fromTy) &&
828             (fromTy.getWidth() == 1))
829           return inner.value();
830   }
831   return {};
832 }
833 
834 bool fir::ConvertOp::isIntegerCompatible(mlir::Type ty) {
835   return ty.isa<mlir::IntegerType>() || ty.isa<mlir::IndexType>() ||
836          ty.isa<fir::IntegerType>() || ty.isa<fir::LogicalType>();
837 }
838 
839 bool fir::ConvertOp::isFloatCompatible(mlir::Type ty) {
840   return ty.isa<mlir::FloatType>() || ty.isa<fir::RealType>();
841 }
842 
843 bool fir::ConvertOp::isPointerCompatible(mlir::Type ty) {
844   return ty.isa<fir::ReferenceType>() || ty.isa<fir::PointerType>() ||
845          ty.isa<fir::HeapType>() || ty.isa<fir::LLVMPointerType>() ||
846          ty.isa<mlir::MemRefType>() || ty.isa<mlir::FunctionType>() ||
847          ty.isa<fir::TypeDescType>();
848 }
849 
850 static mlir::LogicalResult verify(fir::ConvertOp &op) {
851   auto inType = op.value().getType();
852   auto outType = op.getType();
853   if (inType == outType)
854     return mlir::success();
855   if ((op.isPointerCompatible(inType) && op.isPointerCompatible(outType)) ||
856       (op.isIntegerCompatible(inType) && op.isIntegerCompatible(outType)) ||
857       (op.isIntegerCompatible(inType) && op.isFloatCompatible(outType)) ||
858       (op.isFloatCompatible(inType) && op.isIntegerCompatible(outType)) ||
859       (op.isFloatCompatible(inType) && op.isFloatCompatible(outType)) ||
860       (op.isIntegerCompatible(inType) && op.isPointerCompatible(outType)) ||
861       (op.isPointerCompatible(inType) && op.isIntegerCompatible(outType)) ||
862       (inType.isa<fir::BoxType>() && outType.isa<fir::BoxType>()) ||
863       (fir::isa_complex(inType) && fir::isa_complex(outType)))
864     return mlir::success();
865   return op.emitOpError("invalid type conversion");
866 }
867 
868 //===----------------------------------------------------------------------===//
869 // CoordinateOp
870 //===----------------------------------------------------------------------===//
871 
872 static void print(mlir::OpAsmPrinter &p, fir::CoordinateOp op) {
873   p << ' ' << op.ref() << ", " << op.coor();
874   p.printOptionalAttrDict(op->getAttrs(), /*elideAttrs=*/{"baseType"});
875   p << " : ";
876   p.printFunctionalType(op.getOperandTypes(), op->getResultTypes());
877 }
878 
879 static mlir::ParseResult parseCoordinateCustom(mlir::OpAsmParser &parser,
880                                                mlir::OperationState &result) {
881   mlir::OpAsmParser::OperandType memref;
882   if (parser.parseOperand(memref) || parser.parseComma())
883     return mlir::failure();
884   llvm::SmallVector<mlir::OpAsmParser::OperandType> coorOperands;
885   if (parser.parseOperandList(coorOperands))
886     return mlir::failure();
887   llvm::SmallVector<mlir::OpAsmParser::OperandType> allOperands;
888   allOperands.push_back(memref);
889   allOperands.append(coorOperands.begin(), coorOperands.end());
890   mlir::FunctionType funcTy;
891   auto loc = parser.getCurrentLocation();
892   if (parser.parseOptionalAttrDict(result.attributes) ||
893       parser.parseColonType(funcTy) ||
894       parser.resolveOperands(allOperands, funcTy.getInputs(), loc,
895                              result.operands))
896     return failure();
897   parser.addTypesToList(funcTy.getResults(), result.types);
898   result.addAttribute("baseType", mlir::TypeAttr::get(funcTy.getInput(0)));
899   return mlir::success();
900 }
901 
902 static mlir::LogicalResult verify(fir::CoordinateOp op) {
903   auto refTy = op.ref().getType();
904   if (fir::isa_ref_type(refTy)) {
905     auto eleTy = fir::dyn_cast_ptrEleTy(refTy);
906     if (auto arrTy = eleTy.dyn_cast<fir::SequenceType>()) {
907       if (arrTy.hasUnknownShape())
908         return op.emitOpError("cannot find coordinate in unknown shape");
909       if (arrTy.getConstantRows() < arrTy.getDimension() - 1)
910         return op.emitOpError("cannot find coordinate with unknown extents");
911     }
912     if (!(fir::isa_aggregate(eleTy) || fir::isa_complex(eleTy) ||
913           fir::isa_char_string(eleTy)))
914       return op.emitOpError("cannot apply coordinate_of to this type");
915   }
916   // Recovering a LEN type parameter only makes sense from a boxed value. For a
917   // bare reference, the LEN type parameters must be passed as additional
918   // arguments to `op`.
919   for (auto co : op.coor())
920     if (dyn_cast_or_null<fir::LenParamIndexOp>(co.getDefiningOp())) {
921       if (op.getNumOperands() != 2)
922         return op.emitOpError("len_param_index must be last argument");
923       if (!op.ref().getType().isa<BoxType>())
924         return op.emitOpError("len_param_index must be used on box type");
925     }
926   return mlir::success();
927 }
928 
929 //===----------------------------------------------------------------------===//
930 // DispatchOp
931 //===----------------------------------------------------------------------===//
932 
933 mlir::FunctionType fir::DispatchOp::getFunctionType() {
934   return mlir::FunctionType::get(getContext(), getOperandTypes(),
935                                  getResultTypes());
936 }
937 
938 static mlir::ParseResult parseDispatchOp(mlir::OpAsmParser &parser,
939                                          mlir::OperationState &result) {
940   mlir::FunctionType calleeType;
941   llvm::SmallVector<mlir::OpAsmParser::OperandType> operands;
942   auto calleeLoc = parser.getNameLoc();
943   llvm::StringRef calleeName;
944   if (failed(parser.parseOptionalKeyword(&calleeName))) {
945     mlir::StringAttr calleeAttr;
946     if (parser.parseAttribute(calleeAttr,
947                               fir::DispatchOp::getMethodAttrNameStr(),
948                               result.attributes))
949       return mlir::failure();
950   } else {
951     result.addAttribute(fir::DispatchOp::getMethodAttrNameStr(),
952                         parser.getBuilder().getStringAttr(calleeName));
953   }
954   if (parser.parseOperandList(operands, mlir::OpAsmParser::Delimiter::Paren) ||
955       parser.parseOptionalAttrDict(result.attributes) ||
956       parser.parseColonType(calleeType) ||
957       parser.addTypesToList(calleeType.getResults(), result.types) ||
958       parser.resolveOperands(operands, calleeType.getInputs(), calleeLoc,
959                              result.operands))
960     return mlir::failure();
961   return mlir::success();
962 }
963 
964 static void print(mlir::OpAsmPrinter &p, fir::DispatchOp &op) {
965   p << ' ' << op.getMethodAttr() << '(';
966   p.printOperand(op.object());
967   if (!op.args().empty()) {
968     p << ", ";
969     p.printOperands(op.args());
970   }
971   p << ") : ";
972   p.printFunctionalType(op.getOperation()->getOperandTypes(),
973                         op.getOperation()->getResultTypes());
974 }
975 
976 //===----------------------------------------------------------------------===//
977 // DispatchTableOp
978 //===----------------------------------------------------------------------===//
979 
980 void fir::DispatchTableOp::appendTableEntry(mlir::Operation *op) {
981   assert(mlir::isa<fir::DTEntryOp>(*op) && "operation must be a DTEntryOp");
982   auto &block = getBlock();
983   block.getOperations().insert(block.end(), op);
984 }
985 
986 static mlir::ParseResult parseDispatchTableOp(mlir::OpAsmParser &parser,
987                                               mlir::OperationState &result) {
988   // Parse the name as a symbol reference attribute.
989   SymbolRefAttr nameAttr;
990   if (parser.parseAttribute(nameAttr, mlir::SymbolTable::getSymbolAttrName(),
991                             result.attributes))
992     return failure();
993 
994   // Convert the parsed name attr into a string attr.
995   result.attributes.set(mlir::SymbolTable::getSymbolAttrName(),
996                         nameAttr.getRootReference());
997 
998   // Parse the optional table body.
999   mlir::Region *body = result.addRegion();
1000   OptionalParseResult parseResult = parser.parseOptionalRegion(*body);
1001   if (parseResult.hasValue() && failed(*parseResult))
1002     return mlir::failure();
1003 
1004   fir::DispatchTableOp::ensureTerminator(*body, parser.getBuilder(),
1005                                          result.location);
1006   return mlir::success();
1007 }
1008 
1009 static void print(mlir::OpAsmPrinter &p, fir::DispatchTableOp &op) {
1010   auto tableName =
1011       op.getOperation()
1012           ->getAttrOfType<StringAttr>(mlir::SymbolTable::getSymbolAttrName())
1013           .getValue();
1014   p << " @" << tableName;
1015 
1016   Region &body = op.getOperation()->getRegion(0);
1017   if (!body.empty()) {
1018     p << ' ';
1019     p.printRegion(body, /*printEntryBlockArgs=*/false,
1020                   /*printBlockTerminators=*/false);
1021   }
1022 }
1023 
1024 static mlir::LogicalResult verify(fir::DispatchTableOp &op) {
1025   for (auto &op : op.getBlock())
1026     if (!(isa<fir::DTEntryOp>(op) || isa<fir::FirEndOp>(op)))
1027       return op.emitOpError("dispatch table must contain dt_entry");
1028   return mlir::success();
1029 }
1030 
1031 //===----------------------------------------------------------------------===//
1032 // EmboxOp
1033 //===----------------------------------------------------------------------===//
1034 
1035 static mlir::LogicalResult verify(fir::EmboxOp op) {
1036   auto eleTy = fir::dyn_cast_ptrEleTy(op.memref().getType());
1037   bool isArray = false;
1038   if (auto seqTy = eleTy.dyn_cast<fir::SequenceType>()) {
1039     eleTy = seqTy.getEleTy();
1040     isArray = true;
1041   }
1042   if (op.hasLenParams()) {
1043     auto lenPs = op.numLenParams();
1044     if (auto rt = eleTy.dyn_cast<fir::RecordType>()) {
1045       if (lenPs != rt.getNumLenParams())
1046         return op.emitOpError("number of LEN params does not correspond"
1047                               " to the !fir.type type");
1048     } else if (auto strTy = eleTy.dyn_cast<fir::CharacterType>()) {
1049       if (strTy.getLen() != fir::CharacterType::unknownLen())
1050         return op.emitOpError("CHARACTER already has static LEN");
1051     } else {
1052       return op.emitOpError("LEN parameters require CHARACTER or derived type");
1053     }
1054     for (auto lp : op.typeparams())
1055       if (!fir::isa_integer(lp.getType()))
1056         return op.emitOpError("LEN parameters must be integral type");
1057   }
1058   if (op.getShape() && !isArray)
1059     return op.emitOpError("shape must not be provided for a scalar");
1060   if (op.getSlice() && !isArray)
1061     return op.emitOpError("slice must not be provided for a scalar");
1062   return mlir::success();
1063 }
1064 
1065 //===----------------------------------------------------------------------===//
1066 // EmboxCharOp
1067 //===----------------------------------------------------------------------===//
1068 
1069 static mlir::LogicalResult verify(fir::EmboxCharOp &op) {
1070   auto eleTy = fir::dyn_cast_ptrEleTy(op.memref().getType());
1071   if (!eleTy.dyn_cast_or_null<CharacterType>())
1072     return mlir::failure();
1073   return mlir::success();
1074 }
1075 
1076 //===----------------------------------------------------------------------===//
1077 // EmboxProcOp
1078 //===----------------------------------------------------------------------===//
1079 
1080 static mlir::ParseResult parseEmboxProcOp(mlir::OpAsmParser &parser,
1081                                           mlir::OperationState &result) {
1082   mlir::SymbolRefAttr procRef;
1083   if (parser.parseAttribute(procRef, "funcname", result.attributes))
1084     return mlir::failure();
1085   bool hasTuple = false;
1086   mlir::OpAsmParser::OperandType tupleRef;
1087   if (!parser.parseOptionalComma()) {
1088     if (parser.parseOperand(tupleRef))
1089       return mlir::failure();
1090     hasTuple = true;
1091   }
1092   mlir::FunctionType type;
1093   if (parser.parseColon() || parser.parseLParen() || parser.parseType(type))
1094     return mlir::failure();
1095   result.addAttribute("functype", mlir::TypeAttr::get(type));
1096   if (hasTuple) {
1097     mlir::Type tupleType;
1098     if (parser.parseComma() || parser.parseType(tupleType) ||
1099         parser.resolveOperand(tupleRef, tupleType, result.operands))
1100       return mlir::failure();
1101   }
1102   mlir::Type boxType;
1103   if (parser.parseRParen() || parser.parseArrow() ||
1104       parser.parseType(boxType) || parser.addTypesToList(boxType, result.types))
1105     return mlir::failure();
1106   return mlir::success();
1107 }
1108 
1109 static void print(mlir::OpAsmPrinter &p, fir::EmboxProcOp &op) {
1110   p << ' ' << op.getOperation()->getAttr("funcname");
1111   auto h = op.host();
1112   if (h) {
1113     p << ", ";
1114     p.printOperand(h);
1115   }
1116   p << " : (" << op.getOperation()->getAttr("functype");
1117   if (h)
1118     p << ", " << h.getType();
1119   p << ") -> " << op.getType();
1120 }
1121 
1122 static mlir::LogicalResult verify(fir::EmboxProcOp &op) {
1123   // host bindings (optional) must be a reference to a tuple
1124   if (auto h = op.host()) {
1125     if (auto r = h.getType().dyn_cast<ReferenceType>()) {
1126       if (!r.getEleTy().dyn_cast<mlir::TupleType>())
1127         return mlir::failure();
1128     } else {
1129       return mlir::failure();
1130     }
1131   }
1132   return mlir::success();
1133 }
1134 
1135 //===----------------------------------------------------------------------===//
1136 // GenTypeDescOp
1137 //===----------------------------------------------------------------------===//
1138 
1139 void fir::GenTypeDescOp::build(OpBuilder &, OperationState &result,
1140                                mlir::TypeAttr inty) {
1141   result.addAttribute("in_type", inty);
1142   result.addTypes(TypeDescType::get(inty.getValue()));
1143 }
1144 
1145 static mlir::ParseResult parseGenTypeDescOp(mlir::OpAsmParser &parser,
1146                                             mlir::OperationState &result) {
1147   mlir::Type intype;
1148   if (parser.parseType(intype))
1149     return mlir::failure();
1150   result.addAttribute("in_type", mlir::TypeAttr::get(intype));
1151   mlir::Type restype = TypeDescType::get(intype);
1152   if (parser.addTypeToList(restype, result.types))
1153     return mlir::failure();
1154   return mlir::success();
1155 }
1156 
1157 static void print(mlir::OpAsmPrinter &p, fir::GenTypeDescOp &op) {
1158   p << ' ' << op.getOperation()->getAttr("in_type");
1159   p.printOptionalAttrDict(op.getOperation()->getAttrs(), {"in_type"});
1160 }
1161 
1162 static mlir::LogicalResult verify(fir::GenTypeDescOp &op) {
1163   mlir::Type resultTy = op.getType();
1164   if (auto tdesc = resultTy.dyn_cast<TypeDescType>()) {
1165     if (tdesc.getOfTy() != op.getInType())
1166       return op.emitOpError("wrapped type mismatched");
1167   } else {
1168     return op.emitOpError("must be !fir.tdesc type");
1169   }
1170   return mlir::success();
1171 }
1172 
1173 //===----------------------------------------------------------------------===//
1174 // GlobalOp
1175 //===----------------------------------------------------------------------===//
1176 
1177 mlir::Type fir::GlobalOp::resultType() {
1178   return wrapAllocaResultType(getType());
1179 }
1180 
1181 static ParseResult parseGlobalOp(OpAsmParser &parser, OperationState &result) {
1182   // Parse the optional linkage
1183   llvm::StringRef linkage;
1184   auto &builder = parser.getBuilder();
1185   if (mlir::succeeded(parser.parseOptionalKeyword(&linkage))) {
1186     if (fir::GlobalOp::verifyValidLinkage(linkage))
1187       return mlir::failure();
1188     mlir::StringAttr linkAttr = builder.getStringAttr(linkage);
1189     result.addAttribute(fir::GlobalOp::linkageAttrName(), linkAttr);
1190   }
1191 
1192   // Parse the name as a symbol reference attribute.
1193   mlir::SymbolRefAttr nameAttr;
1194   if (parser.parseAttribute(nameAttr, fir::GlobalOp::symbolAttrNameStr(),
1195                             result.attributes))
1196     return mlir::failure();
1197   result.addAttribute(mlir::SymbolTable::getSymbolAttrName(),
1198                       nameAttr.getRootReference());
1199 
1200   bool simpleInitializer = false;
1201   if (mlir::succeeded(parser.parseOptionalLParen())) {
1202     Attribute attr;
1203     if (parser.parseAttribute(attr, "initVal", result.attributes) ||
1204         parser.parseRParen())
1205       return mlir::failure();
1206     simpleInitializer = true;
1207   }
1208 
1209   if (succeeded(parser.parseOptionalKeyword("constant"))) {
1210     // if "constant" keyword then mark this as a constant, not a variable
1211     result.addAttribute("constant", builder.getUnitAttr());
1212   }
1213 
1214   mlir::Type globalType;
1215   if (parser.parseColonType(globalType))
1216     return mlir::failure();
1217 
1218   result.addAttribute(fir::GlobalOp::typeAttrName(result.name),
1219                       mlir::TypeAttr::get(globalType));
1220 
1221   if (simpleInitializer) {
1222     result.addRegion();
1223   } else {
1224     // Parse the optional initializer body.
1225     auto parseResult = parser.parseOptionalRegion(
1226         *result.addRegion(), /*arguments=*/llvm::None, /*argTypes=*/llvm::None);
1227     if (parseResult.hasValue() && mlir::failed(*parseResult))
1228       return mlir::failure();
1229   }
1230 
1231   return mlir::success();
1232 }
1233 
1234 static void print(mlir::OpAsmPrinter &p, fir::GlobalOp &op) {
1235   if (op.linkName().hasValue())
1236     p << ' ' << op.linkName().getValue();
1237   p << ' ';
1238   p.printAttributeWithoutType(op.getSymrefAttr());
1239   if (auto val = op.getValueOrNull())
1240     p << '(' << val << ')';
1241   if (op.getOperation()->getAttr(fir::GlobalOp::getConstantAttrNameStr()))
1242     p << " constant";
1243   p << " : ";
1244   p.printType(op.getType());
1245   if (op.hasInitializationBody()) {
1246     p << ' ';
1247     p.printRegion(op.getOperation()->getRegion(0),
1248                   /*printEntryBlockArgs=*/false,
1249                   /*printBlockTerminators=*/true);
1250   }
1251 }
1252 
1253 void fir::GlobalOp::appendInitialValue(mlir::Operation *op) {
1254   getBlock().getOperations().push_back(op);
1255 }
1256 
1257 void fir::GlobalOp::build(mlir::OpBuilder &builder, OperationState &result,
1258                           StringRef name, bool isConstant, Type type,
1259                           Attribute initialVal, StringAttr linkage,
1260                           ArrayRef<NamedAttribute> attrs) {
1261   result.addRegion();
1262   result.addAttribute(typeAttrName(result.name), mlir::TypeAttr::get(type));
1263   result.addAttribute(mlir::SymbolTable::getSymbolAttrName(),
1264                       builder.getStringAttr(name));
1265   result.addAttribute(symbolAttrNameStr(),
1266                       SymbolRefAttr::get(builder.getContext(), name));
1267   if (isConstant)
1268     result.addAttribute(constantAttrName(result.name), builder.getUnitAttr());
1269   if (initialVal)
1270     result.addAttribute(initValAttrName(result.name), initialVal);
1271   if (linkage)
1272     result.addAttribute(linkageAttrName(), linkage);
1273   result.attributes.append(attrs.begin(), attrs.end());
1274 }
1275 
1276 void fir::GlobalOp::build(mlir::OpBuilder &builder, OperationState &result,
1277                           StringRef name, Type type, Attribute initialVal,
1278                           StringAttr linkage, ArrayRef<NamedAttribute> attrs) {
1279   build(builder, result, name, /*isConstant=*/false, type, {}, linkage, attrs);
1280 }
1281 
1282 void fir::GlobalOp::build(mlir::OpBuilder &builder, OperationState &result,
1283                           StringRef name, bool isConstant, Type type,
1284                           StringAttr linkage, ArrayRef<NamedAttribute> attrs) {
1285   build(builder, result, name, isConstant, type, {}, linkage, attrs);
1286 }
1287 
1288 void fir::GlobalOp::build(mlir::OpBuilder &builder, OperationState &result,
1289                           StringRef name, Type type, StringAttr linkage,
1290                           ArrayRef<NamedAttribute> attrs) {
1291   build(builder, result, name, /*isConstant=*/false, type, {}, linkage, attrs);
1292 }
1293 
1294 void fir::GlobalOp::build(mlir::OpBuilder &builder, OperationState &result,
1295                           StringRef name, bool isConstant, Type type,
1296                           ArrayRef<NamedAttribute> attrs) {
1297   build(builder, result, name, isConstant, type, StringAttr{}, attrs);
1298 }
1299 
1300 void fir::GlobalOp::build(mlir::OpBuilder &builder, OperationState &result,
1301                           StringRef name, Type type,
1302                           ArrayRef<NamedAttribute> attrs) {
1303   build(builder, result, name, /*isConstant=*/false, type, attrs);
1304 }
1305 
1306 mlir::ParseResult fir::GlobalOp::verifyValidLinkage(StringRef linkage) {
1307   // Supporting only a subset of the LLVM linkage types for now
1308   static const char *validNames[] = {"common", "internal", "linkonce", "weak"};
1309   return mlir::success(llvm::is_contained(validNames, linkage));
1310 }
1311 
1312 //===----------------------------------------------------------------------===//
1313 // GlobalLenOp
1314 //===----------------------------------------------------------------------===//
1315 
1316 static mlir::ParseResult parseGlobalLenOp(mlir::OpAsmParser &parser,
1317                                           mlir::OperationState &result) {
1318   llvm::StringRef fieldName;
1319   if (failed(parser.parseOptionalKeyword(&fieldName))) {
1320     mlir::StringAttr fieldAttr;
1321     if (parser.parseAttribute(fieldAttr, fir::GlobalLenOp::lenParamAttrName(),
1322                               result.attributes))
1323       return mlir::failure();
1324   } else {
1325     result.addAttribute(fir::GlobalLenOp::lenParamAttrName(),
1326                         parser.getBuilder().getStringAttr(fieldName));
1327   }
1328   mlir::IntegerAttr constant;
1329   if (parser.parseComma() ||
1330       parser.parseAttribute(constant, fir::GlobalLenOp::intAttrName(),
1331                             result.attributes))
1332     return mlir::failure();
1333   return mlir::success();
1334 }
1335 
1336 static void print(mlir::OpAsmPrinter &p, fir::GlobalLenOp &op) {
1337   p << ' ' << op.getOperation()->getAttr(fir::GlobalLenOp::lenParamAttrName())
1338     << ", " << op.getOperation()->getAttr(fir::GlobalLenOp::intAttrName());
1339 }
1340 
1341 //===----------------------------------------------------------------------===//
1342 // FieldIndexOp
1343 //===----------------------------------------------------------------------===//
1344 
1345 static mlir::ParseResult parseFieldIndexOp(mlir::OpAsmParser &parser,
1346                                            mlir::OperationState &result) {
1347   llvm::StringRef fieldName;
1348   auto &builder = parser.getBuilder();
1349   mlir::Type recty;
1350   if (parser.parseOptionalKeyword(&fieldName) || parser.parseComma() ||
1351       parser.parseType(recty))
1352     return mlir::failure();
1353   result.addAttribute(fir::FieldIndexOp::fieldAttrName(),
1354                       builder.getStringAttr(fieldName));
1355   if (!recty.dyn_cast<RecordType>())
1356     return mlir::failure();
1357   result.addAttribute(fir::FieldIndexOp::typeAttrName(),
1358                       mlir::TypeAttr::get(recty));
1359   if (!parser.parseOptionalLParen()) {
1360     llvm::SmallVector<mlir::OpAsmParser::OperandType> operands;
1361     llvm::SmallVector<mlir::Type> types;
1362     auto loc = parser.getNameLoc();
1363     if (parser.parseOperandList(operands, mlir::OpAsmParser::Delimiter::None) ||
1364         parser.parseColonTypeList(types) || parser.parseRParen() ||
1365         parser.resolveOperands(operands, types, loc, result.operands))
1366       return mlir::failure();
1367   }
1368   mlir::Type fieldType = fir::FieldType::get(builder.getContext());
1369   if (parser.addTypeToList(fieldType, result.types))
1370     return mlir::failure();
1371   return mlir::success();
1372 }
1373 
1374 static void print(mlir::OpAsmPrinter &p, fir::FieldIndexOp &op) {
1375   p << ' '
1376     << op.getOperation()
1377            ->getAttrOfType<mlir::StringAttr>(fir::FieldIndexOp::fieldAttrName())
1378            .getValue()
1379     << ", " << op.getOperation()->getAttr(fir::FieldIndexOp::typeAttrName());
1380   if (op.getNumOperands()) {
1381     p << '(';
1382     p.printOperands(op.typeparams());
1383     const auto *sep = ") : ";
1384     for (auto op : op.typeparams()) {
1385       p << sep;
1386       if (op)
1387         p.printType(op.getType());
1388       else
1389         p << "()";
1390       sep = ", ";
1391     }
1392   }
1393 }
1394 
1395 void fir::FieldIndexOp::build(mlir::OpBuilder &builder,
1396                               mlir::OperationState &result,
1397                               llvm::StringRef fieldName, mlir::Type recTy,
1398                               mlir::ValueRange operands) {
1399   result.addAttribute(fieldAttrName(), builder.getStringAttr(fieldName));
1400   result.addAttribute(typeAttrName(), TypeAttr::get(recTy));
1401   result.addOperands(operands);
1402 }
1403 
1404 //===----------------------------------------------------------------------===//
1405 // InsertOnRangeOp
1406 //===----------------------------------------------------------------------===//
1407 
1408 static ParseResult
1409 parseCustomRangeSubscript(mlir::OpAsmParser &parser,
1410                           mlir::DenseIntElementsAttr &coord) {
1411   llvm::SmallVector<int64_t> lbounds;
1412   llvm::SmallVector<int64_t> ubounds;
1413   if (parser.parseKeyword("from") ||
1414       parser.parseCommaSeparatedList(
1415           AsmParser::Delimiter::Paren,
1416           [&] { return parser.parseInteger(lbounds.emplace_back(0)); }) ||
1417       parser.parseKeyword("to") ||
1418       parser.parseCommaSeparatedList(AsmParser::Delimiter::Paren, [&] {
1419         return parser.parseInteger(ubounds.emplace_back(0));
1420       }))
1421     return failure();
1422   llvm::SmallVector<int64_t> zippedBounds;
1423   for (auto zip : llvm::zip(lbounds, ubounds)) {
1424     zippedBounds.push_back(std::get<0>(zip));
1425     zippedBounds.push_back(std::get<1>(zip));
1426   }
1427   coord = mlir::Builder(parser.getContext()).getIndexTensorAttr(zippedBounds);
1428   return success();
1429 }
1430 
1431 void printCustomRangeSubscript(mlir::OpAsmPrinter &printer, InsertOnRangeOp op,
1432                                mlir::DenseIntElementsAttr coord) {
1433   printer << "from (";
1434   auto enumerate = llvm::enumerate(coord.getValues<int64_t>());
1435   // Even entries are the lower bounds.
1436   llvm::interleaveComma(
1437       make_filter_range(
1438           enumerate,
1439           [](auto indexed_value) { return indexed_value.index() % 2 == 0; }),
1440       printer, [&](auto indexed_value) { printer << indexed_value.value(); });
1441   printer << ") to (";
1442   // Odd entries are the upper bounds.
1443   llvm::interleaveComma(
1444       make_filter_range(
1445           enumerate,
1446           [](auto indexed_value) { return indexed_value.index() % 2 != 0; }),
1447       printer, [&](auto indexed_value) { printer << indexed_value.value(); });
1448   printer << ")";
1449 }
1450 
1451 /// Range bounds must be nonnegative, and the range must not be empty.
1452 static mlir::LogicalResult verify(fir::InsertOnRangeOp op) {
1453   if (fir::hasDynamicSize(op.seq().getType()))
1454     return op.emitOpError("must have constant shape and size");
1455   mlir::DenseIntElementsAttr coor = op.coor();
1456   if (coor.size() < 2 || coor.size() % 2 != 0)
1457     return op.emitOpError("has uneven number of values in ranges");
1458   bool rangeIsKnownToBeNonempty = false;
1459   for (auto i = coor.getValues<int64_t>().end(),
1460             b = coor.getValues<int64_t>().begin();
1461        i != b;) {
1462     int64_t ub = (*--i);
1463     int64_t lb = (*--i);
1464     if (lb < 0 || ub < 0)
1465       return op.emitOpError("negative range bound");
1466     if (rangeIsKnownToBeNonempty)
1467       continue;
1468     if (lb > ub)
1469       return op.emitOpError("empty range");
1470     rangeIsKnownToBeNonempty = lb < ub;
1471   }
1472   return mlir::success();
1473 }
1474 
1475 //===----------------------------------------------------------------------===//
1476 // InsertValueOp
1477 //===----------------------------------------------------------------------===//
1478 
1479 static bool checkIsIntegerConstant(mlir::Attribute attr, int64_t conVal) {
1480   if (auto iattr = attr.dyn_cast<mlir::IntegerAttr>())
1481     return iattr.getInt() == conVal;
1482   return false;
1483 }
1484 static bool isZero(mlir::Attribute a) { return checkIsIntegerConstant(a, 0); }
1485 static bool isOne(mlir::Attribute a) { return checkIsIntegerConstant(a, 1); }
1486 
1487 // Undo some complex patterns created in the front-end and turn them back into
1488 // complex ops.
1489 template <typename FltOp, typename CpxOp>
1490 struct UndoComplexPattern : public mlir::RewritePattern {
1491   UndoComplexPattern(mlir::MLIRContext *ctx)
1492       : mlir::RewritePattern("fir.insert_value", 2, ctx) {}
1493 
1494   mlir::LogicalResult
1495   matchAndRewrite(mlir::Operation *op,
1496                   mlir::PatternRewriter &rewriter) const override {
1497     auto insval = dyn_cast_or_null<fir::InsertValueOp>(op);
1498     if (!insval || !insval.getType().isa<fir::ComplexType>())
1499       return mlir::failure();
1500     auto insval2 =
1501         dyn_cast_or_null<fir::InsertValueOp>(insval.adt().getDefiningOp());
1502     if (!insval2 || !isa<fir::UndefOp>(insval2.adt().getDefiningOp()))
1503       return mlir::failure();
1504     auto binf = dyn_cast_or_null<FltOp>(insval.val().getDefiningOp());
1505     auto binf2 = dyn_cast_or_null<FltOp>(insval2.val().getDefiningOp());
1506     if (!binf || !binf2 || insval.coor().size() != 1 ||
1507         !isOne(insval.coor()[0]) || insval2.coor().size() != 1 ||
1508         !isZero(insval2.coor()[0]))
1509       return mlir::failure();
1510     auto eai =
1511         dyn_cast_or_null<fir::ExtractValueOp>(binf.getLhs().getDefiningOp());
1512     auto ebi =
1513         dyn_cast_or_null<fir::ExtractValueOp>(binf.getRhs().getDefiningOp());
1514     auto ear =
1515         dyn_cast_or_null<fir::ExtractValueOp>(binf2.getLhs().getDefiningOp());
1516     auto ebr =
1517         dyn_cast_or_null<fir::ExtractValueOp>(binf2.getRhs().getDefiningOp());
1518     if (!eai || !ebi || !ear || !ebr || ear.adt() != eai.adt() ||
1519         ebr.adt() != ebi.adt() || eai.coor().size() != 1 ||
1520         !isOne(eai.coor()[0]) || ebi.coor().size() != 1 ||
1521         !isOne(ebi.coor()[0]) || ear.coor().size() != 1 ||
1522         !isZero(ear.coor()[0]) || ebr.coor().size() != 1 ||
1523         !isZero(ebr.coor()[0]))
1524       return mlir::failure();
1525     rewriter.replaceOpWithNewOp<CpxOp>(op, ear.adt(), ebr.adt());
1526     return mlir::success();
1527   }
1528 };
1529 
1530 void fir::InsertValueOp::getCanonicalizationPatterns(
1531     mlir::RewritePatternSet &results, mlir::MLIRContext *context) {
1532   results.insert<UndoComplexPattern<mlir::arith::AddFOp, fir::AddcOp>,
1533                  UndoComplexPattern<mlir::arith::SubFOp, fir::SubcOp>>(context);
1534 }
1535 
1536 //===----------------------------------------------------------------------===//
1537 // IterWhileOp
1538 //===----------------------------------------------------------------------===//
1539 
1540 void fir::IterWhileOp::build(mlir::OpBuilder &builder,
1541                              mlir::OperationState &result, mlir::Value lb,
1542                              mlir::Value ub, mlir::Value step,
1543                              mlir::Value iterate, bool finalCountValue,
1544                              mlir::ValueRange iterArgs,
1545                              llvm::ArrayRef<mlir::NamedAttribute> attributes) {
1546   result.addOperands({lb, ub, step, iterate});
1547   if (finalCountValue) {
1548     result.addTypes(builder.getIndexType());
1549     result.addAttribute(getFinalValueAttrNameStr(), builder.getUnitAttr());
1550   }
1551   result.addTypes(iterate.getType());
1552   result.addOperands(iterArgs);
1553   for (auto v : iterArgs)
1554     result.addTypes(v.getType());
1555   mlir::Region *bodyRegion = result.addRegion();
1556   bodyRegion->push_back(new Block{});
1557   bodyRegion->front().addArgument(builder.getIndexType(), result.location);
1558   bodyRegion->front().addArgument(iterate.getType(), result.location);
1559   bodyRegion->front().addArguments(
1560       iterArgs.getTypes(),
1561       SmallVector<Location>(iterArgs.size(), result.location));
1562   result.addAttributes(attributes);
1563 }
1564 
1565 static mlir::ParseResult parseIterWhileOp(mlir::OpAsmParser &parser,
1566                                           mlir::OperationState &result) {
1567   auto &builder = parser.getBuilder();
1568   mlir::OpAsmParser::OperandType inductionVariable, lb, ub, step;
1569   if (parser.parseLParen() || parser.parseRegionArgument(inductionVariable) ||
1570       parser.parseEqual())
1571     return mlir::failure();
1572 
1573   // Parse loop bounds.
1574   auto indexType = builder.getIndexType();
1575   auto i1Type = builder.getIntegerType(1);
1576   if (parser.parseOperand(lb) ||
1577       parser.resolveOperand(lb, indexType, result.operands) ||
1578       parser.parseKeyword("to") || parser.parseOperand(ub) ||
1579       parser.resolveOperand(ub, indexType, result.operands) ||
1580       parser.parseKeyword("step") || parser.parseOperand(step) ||
1581       parser.parseRParen() ||
1582       parser.resolveOperand(step, indexType, result.operands))
1583     return mlir::failure();
1584 
1585   mlir::OpAsmParser::OperandType iterateVar, iterateInput;
1586   if (parser.parseKeyword("and") || parser.parseLParen() ||
1587       parser.parseRegionArgument(iterateVar) || parser.parseEqual() ||
1588       parser.parseOperand(iterateInput) || parser.parseRParen() ||
1589       parser.resolveOperand(iterateInput, i1Type, result.operands))
1590     return mlir::failure();
1591 
1592   // Parse the initial iteration arguments.
1593   llvm::SmallVector<mlir::OpAsmParser::OperandType> regionArgs;
1594   auto prependCount = false;
1595 
1596   // Induction variable.
1597   regionArgs.push_back(inductionVariable);
1598   regionArgs.push_back(iterateVar);
1599 
1600   if (succeeded(parser.parseOptionalKeyword("iter_args"))) {
1601     llvm::SmallVector<mlir::OpAsmParser::OperandType> operands;
1602     llvm::SmallVector<mlir::Type> regionTypes;
1603     // Parse assignment list and results type list.
1604     if (parser.parseAssignmentList(regionArgs, operands) ||
1605         parser.parseArrowTypeList(regionTypes))
1606       return failure();
1607     if (regionTypes.size() == operands.size() + 2)
1608       prependCount = true;
1609     llvm::ArrayRef<mlir::Type> resTypes = regionTypes;
1610     resTypes = prependCount ? resTypes.drop_front(2) : resTypes;
1611     // Resolve input operands.
1612     for (auto operandType : llvm::zip(operands, resTypes))
1613       if (parser.resolveOperand(std::get<0>(operandType),
1614                                 std::get<1>(operandType), result.operands))
1615         return failure();
1616     if (prependCount) {
1617       result.addTypes(regionTypes);
1618     } else {
1619       result.addTypes(i1Type);
1620       result.addTypes(resTypes);
1621     }
1622   } else if (succeeded(parser.parseOptionalArrow())) {
1623     llvm::SmallVector<mlir::Type> typeList;
1624     if (parser.parseLParen() || parser.parseTypeList(typeList) ||
1625         parser.parseRParen())
1626       return failure();
1627     // Type list must be "(index, i1)".
1628     if (typeList.size() != 2 || !typeList[0].isa<mlir::IndexType>() ||
1629         !typeList[1].isSignlessInteger(1))
1630       return failure();
1631     result.addTypes(typeList);
1632     prependCount = true;
1633   } else {
1634     result.addTypes(i1Type);
1635   }
1636 
1637   if (parser.parseOptionalAttrDictWithKeyword(result.attributes))
1638     return mlir::failure();
1639 
1640   llvm::SmallVector<mlir::Type> argTypes;
1641   // Induction variable (hidden)
1642   if (prependCount)
1643     result.addAttribute(IterWhileOp::getFinalValueAttrNameStr(),
1644                         builder.getUnitAttr());
1645   else
1646     argTypes.push_back(indexType);
1647   // Loop carried variables (including iterate)
1648   argTypes.append(result.types.begin(), result.types.end());
1649   // Parse the body region.
1650   auto *body = result.addRegion();
1651   if (regionArgs.size() != argTypes.size())
1652     return parser.emitError(
1653         parser.getNameLoc(),
1654         "mismatch in number of loop-carried values and defined values");
1655 
1656   if (parser.parseRegion(*body, regionArgs, argTypes))
1657     return failure();
1658 
1659   fir::IterWhileOp::ensureTerminator(*body, builder, result.location);
1660 
1661   return mlir::success();
1662 }
1663 
1664 static mlir::LogicalResult verify(fir::IterWhileOp op) {
1665   // Check that the body defines as single block argument for the induction
1666   // variable.
1667   auto *body = op.getBody();
1668   if (!body->getArgument(1).getType().isInteger(1))
1669     return op.emitOpError(
1670         "expected body second argument to be an index argument for "
1671         "the induction variable");
1672   if (!body->getArgument(0).getType().isIndex())
1673     return op.emitOpError(
1674         "expected body first argument to be an index argument for "
1675         "the induction variable");
1676 
1677   auto opNumResults = op.getNumResults();
1678   if (op.finalValue()) {
1679     // Result type must be "(index, i1, ...)".
1680     if (!op.getResult(0).getType().isa<mlir::IndexType>())
1681       return op.emitOpError("result #0 expected to be index");
1682     if (!op.getResult(1).getType().isSignlessInteger(1))
1683       return op.emitOpError("result #1 expected to be i1");
1684     opNumResults--;
1685   } else {
1686     // iterate_while always returns the early exit induction value.
1687     // Result type must be "(i1, ...)"
1688     if (!op.getResult(0).getType().isSignlessInteger(1))
1689       return op.emitOpError("result #0 expected to be i1");
1690   }
1691   if (opNumResults == 0)
1692     return mlir::failure();
1693   if (op.getNumIterOperands() != opNumResults)
1694     return op.emitOpError(
1695         "mismatch in number of loop-carried values and defined values");
1696   if (op.getNumRegionIterArgs() != opNumResults)
1697     return op.emitOpError(
1698         "mismatch in number of basic block args and defined values");
1699   auto iterOperands = op.getIterOperands();
1700   auto iterArgs = op.getRegionIterArgs();
1701   auto opResults =
1702       op.finalValue() ? op.getResults().drop_front() : op.getResults();
1703   unsigned i = 0;
1704   for (auto e : llvm::zip(iterOperands, iterArgs, opResults)) {
1705     if (std::get<0>(e).getType() != std::get<2>(e).getType())
1706       return op.emitOpError() << "types mismatch between " << i
1707                               << "th iter operand and defined value";
1708     if (std::get<1>(e).getType() != std::get<2>(e).getType())
1709       return op.emitOpError() << "types mismatch between " << i
1710                               << "th iter region arg and defined value";
1711 
1712     i++;
1713   }
1714   return mlir::success();
1715 }
1716 
1717 static void print(mlir::OpAsmPrinter &p, fir::IterWhileOp op) {
1718   p << " (" << op.getInductionVar() << " = " << op.lowerBound() << " to "
1719     << op.upperBound() << " step " << op.step() << ") and (";
1720   assert(op.hasIterOperands());
1721   auto regionArgs = op.getRegionIterArgs();
1722   auto operands = op.getIterOperands();
1723   p << regionArgs.front() << " = " << *operands.begin() << ")";
1724   if (regionArgs.size() > 1) {
1725     p << " iter_args(";
1726     llvm::interleaveComma(
1727         llvm::zip(regionArgs.drop_front(), operands.drop_front()), p,
1728         [&](auto it) { p << std::get<0>(it) << " = " << std::get<1>(it); });
1729     p << ") -> (";
1730     llvm::interleaveComma(
1731         llvm::drop_begin(op.getResultTypes(), op.finalValue() ? 0 : 1), p);
1732     p << ")";
1733   } else if (op.finalValue()) {
1734     p << " -> (" << op.getResultTypes() << ')';
1735   }
1736   p.printOptionalAttrDictWithKeyword(op->getAttrs(),
1737                                      {op.getFinalValueAttrNameStr()});
1738   p << ' ';
1739   p.printRegion(op.region(), /*printEntryBlockArgs=*/false,
1740                 /*printBlockTerminators=*/true);
1741 }
1742 
1743 mlir::Region &fir::IterWhileOp::getLoopBody() { return region(); }
1744 
1745 bool fir::IterWhileOp::isDefinedOutsideOfLoop(mlir::Value value) {
1746   return !region().isAncestor(value.getParentRegion());
1747 }
1748 
1749 mlir::LogicalResult
1750 fir::IterWhileOp::moveOutOfLoop(llvm::ArrayRef<mlir::Operation *> ops) {
1751   for (auto *op : ops)
1752     op->moveBefore(*this);
1753   return success();
1754 }
1755 
1756 mlir::BlockArgument fir::IterWhileOp::iterArgToBlockArg(mlir::Value iterArg) {
1757   for (auto i : llvm::enumerate(initArgs()))
1758     if (iterArg == i.value())
1759       return region().front().getArgument(i.index() + 1);
1760   return {};
1761 }
1762 
1763 void fir::IterWhileOp::resultToSourceOps(
1764     llvm::SmallVectorImpl<mlir::Value> &results, unsigned resultNum) {
1765   auto oper = finalValue() ? resultNum + 1 : resultNum;
1766   auto *term = region().front().getTerminator();
1767   if (oper < term->getNumOperands())
1768     results.push_back(term->getOperand(oper));
1769 }
1770 
1771 mlir::Value fir::IterWhileOp::blockArgToSourceOp(unsigned blockArgNum) {
1772   if (blockArgNum > 0 && blockArgNum <= initArgs().size())
1773     return initArgs()[blockArgNum - 1];
1774   return {};
1775 }
1776 
1777 //===----------------------------------------------------------------------===//
1778 // LenParamIndexOp
1779 //===----------------------------------------------------------------------===//
1780 
1781 static mlir::ParseResult parseLenParamIndexOp(mlir::OpAsmParser &parser,
1782                                               mlir::OperationState &result) {
1783   llvm::StringRef fieldName;
1784   auto &builder = parser.getBuilder();
1785   mlir::Type recty;
1786   if (parser.parseOptionalKeyword(&fieldName) || parser.parseComma() ||
1787       parser.parseType(recty))
1788     return mlir::failure();
1789   result.addAttribute(fir::LenParamIndexOp::fieldAttrName(),
1790                       builder.getStringAttr(fieldName));
1791   if (!recty.dyn_cast<RecordType>())
1792     return mlir::failure();
1793   result.addAttribute(fir::LenParamIndexOp::typeAttrName(),
1794                       mlir::TypeAttr::get(recty));
1795   mlir::Type lenType = fir::LenType::get(builder.getContext());
1796   if (parser.addTypeToList(lenType, result.types))
1797     return mlir::failure();
1798   return mlir::success();
1799 }
1800 
1801 static void print(mlir::OpAsmPrinter &p, fir::LenParamIndexOp &op) {
1802   p << ' '
1803     << op.getOperation()
1804            ->getAttrOfType<mlir::StringAttr>(
1805                fir::LenParamIndexOp::fieldAttrName())
1806            .getValue()
1807     << ", " << op.getOperation()->getAttr(fir::LenParamIndexOp::typeAttrName());
1808 }
1809 
1810 //===----------------------------------------------------------------------===//
1811 // LoadOp
1812 //===----------------------------------------------------------------------===//
1813 
1814 void fir::LoadOp::build(mlir::OpBuilder &builder, mlir::OperationState &result,
1815                         mlir::Value refVal) {
1816   if (!refVal) {
1817     mlir::emitError(result.location, "LoadOp has null argument");
1818     return;
1819   }
1820   auto eleTy = fir::dyn_cast_ptrEleTy(refVal.getType());
1821   if (!eleTy) {
1822     mlir::emitError(result.location, "not a memory reference type");
1823     return;
1824   }
1825   result.addOperands(refVal);
1826   result.addTypes(eleTy);
1827 }
1828 
1829 mlir::ParseResult fir::LoadOp::getElementOf(mlir::Type &ele, mlir::Type ref) {
1830   if ((ele = fir::dyn_cast_ptrEleTy(ref)))
1831     return mlir::success();
1832   return mlir::failure();
1833 }
1834 
1835 static mlir::ParseResult parseLoadOp(mlir::OpAsmParser &parser,
1836                                      mlir::OperationState &result) {
1837   mlir::Type type;
1838   mlir::OpAsmParser::OperandType oper;
1839   if (parser.parseOperand(oper) ||
1840       parser.parseOptionalAttrDict(result.attributes) ||
1841       parser.parseColonType(type) ||
1842       parser.resolveOperand(oper, type, result.operands))
1843     return mlir::failure();
1844   mlir::Type eleTy;
1845   if (fir::LoadOp::getElementOf(eleTy, type) ||
1846       parser.addTypeToList(eleTy, result.types))
1847     return mlir::failure();
1848   return mlir::success();
1849 }
1850 
1851 static void print(mlir::OpAsmPrinter &p, fir::LoadOp &op) {
1852   p << ' ';
1853   p.printOperand(op.memref());
1854   p.printOptionalAttrDict(op.getOperation()->getAttrs(), {});
1855   p << " : " << op.memref().getType();
1856 }
1857 
1858 //===----------------------------------------------------------------------===//
1859 // DoLoopOp
1860 //===----------------------------------------------------------------------===//
1861 
1862 void fir::DoLoopOp::build(mlir::OpBuilder &builder,
1863                           mlir::OperationState &result, mlir::Value lb,
1864                           mlir::Value ub, mlir::Value step, bool unordered,
1865                           bool finalCountValue, mlir::ValueRange iterArgs,
1866                           llvm::ArrayRef<mlir::NamedAttribute> attributes) {
1867   result.addOperands({lb, ub, step});
1868   result.addOperands(iterArgs);
1869   if (finalCountValue) {
1870     result.addTypes(builder.getIndexType());
1871     result.addAttribute(finalValueAttrName(result.name), builder.getUnitAttr());
1872   }
1873   for (auto v : iterArgs)
1874     result.addTypes(v.getType());
1875   mlir::Region *bodyRegion = result.addRegion();
1876   bodyRegion->push_back(new Block{});
1877   if (iterArgs.empty() && !finalCountValue)
1878     DoLoopOp::ensureTerminator(*bodyRegion, builder, result.location);
1879   bodyRegion->front().addArgument(builder.getIndexType(), result.location);
1880   bodyRegion->front().addArguments(
1881       iterArgs.getTypes(),
1882       SmallVector<Location>(iterArgs.size(), result.location));
1883   if (unordered)
1884     result.addAttribute(unorderedAttrName(result.name), builder.getUnitAttr());
1885   result.addAttributes(attributes);
1886 }
1887 
1888 static mlir::ParseResult parseDoLoopOp(mlir::OpAsmParser &parser,
1889                                        mlir::OperationState &result) {
1890   auto &builder = parser.getBuilder();
1891   mlir::OpAsmParser::OperandType inductionVariable, lb, ub, step;
1892   // Parse the induction variable followed by '='.
1893   if (parser.parseRegionArgument(inductionVariable) || parser.parseEqual())
1894     return mlir::failure();
1895 
1896   // Parse loop bounds.
1897   auto indexType = builder.getIndexType();
1898   if (parser.parseOperand(lb) ||
1899       parser.resolveOperand(lb, indexType, result.operands) ||
1900       parser.parseKeyword("to") || parser.parseOperand(ub) ||
1901       parser.resolveOperand(ub, indexType, result.operands) ||
1902       parser.parseKeyword("step") || parser.parseOperand(step) ||
1903       parser.resolveOperand(step, indexType, result.operands))
1904     return failure();
1905 
1906   if (mlir::succeeded(parser.parseOptionalKeyword("unordered")))
1907     result.addAttribute("unordered", builder.getUnitAttr());
1908 
1909   // Parse the optional initial iteration arguments.
1910   llvm::SmallVector<mlir::OpAsmParser::OperandType> regionArgs, operands;
1911   llvm::SmallVector<mlir::Type> argTypes;
1912   auto prependCount = false;
1913   regionArgs.push_back(inductionVariable);
1914 
1915   if (succeeded(parser.parseOptionalKeyword("iter_args"))) {
1916     // Parse assignment list and results type list.
1917     if (parser.parseAssignmentList(regionArgs, operands) ||
1918         parser.parseArrowTypeList(result.types))
1919       return failure();
1920     if (result.types.size() == operands.size() + 1)
1921       prependCount = true;
1922     // Resolve input operands.
1923     llvm::ArrayRef<mlir::Type> resTypes = result.types;
1924     for (auto operand_type :
1925          llvm::zip(operands, prependCount ? resTypes.drop_front() : resTypes))
1926       if (parser.resolveOperand(std::get<0>(operand_type),
1927                                 std::get<1>(operand_type), result.operands))
1928         return failure();
1929   } else if (succeeded(parser.parseOptionalArrow())) {
1930     if (parser.parseKeyword("index"))
1931       return failure();
1932     result.types.push_back(indexType);
1933     prependCount = true;
1934   }
1935 
1936   if (parser.parseOptionalAttrDictWithKeyword(result.attributes))
1937     return mlir::failure();
1938 
1939   // Induction variable.
1940   if (prependCount)
1941     result.addAttribute(DoLoopOp::finalValueAttrName(result.name),
1942                         builder.getUnitAttr());
1943   else
1944     argTypes.push_back(indexType);
1945   // Loop carried variables
1946   argTypes.append(result.types.begin(), result.types.end());
1947   // Parse the body region.
1948   auto *body = result.addRegion();
1949   if (regionArgs.size() != argTypes.size())
1950     return parser.emitError(
1951         parser.getNameLoc(),
1952         "mismatch in number of loop-carried values and defined values");
1953 
1954   if (parser.parseRegion(*body, regionArgs, argTypes))
1955     return failure();
1956 
1957   DoLoopOp::ensureTerminator(*body, builder, result.location);
1958 
1959   return mlir::success();
1960 }
1961 
1962 fir::DoLoopOp fir::getForInductionVarOwner(mlir::Value val) {
1963   auto ivArg = val.dyn_cast<mlir::BlockArgument>();
1964   if (!ivArg)
1965     return {};
1966   assert(ivArg.getOwner() && "unlinked block argument");
1967   auto *containingInst = ivArg.getOwner()->getParentOp();
1968   return dyn_cast_or_null<fir::DoLoopOp>(containingInst);
1969 }
1970 
1971 // Lifted from loop.loop
1972 static mlir::LogicalResult verify(fir::DoLoopOp op) {
1973   // Check that the body defines as single block argument for the induction
1974   // variable.
1975   auto *body = op.getBody();
1976   if (!body->getArgument(0).getType().isIndex())
1977     return op.emitOpError(
1978         "expected body first argument to be an index argument for "
1979         "the induction variable");
1980 
1981   auto opNumResults = op.getNumResults();
1982   if (opNumResults == 0)
1983     return success();
1984 
1985   if (op.finalValue()) {
1986     if (op.unordered())
1987       return op.emitOpError("unordered loop has no final value");
1988     opNumResults--;
1989   }
1990   if (op.getNumIterOperands() != opNumResults)
1991     return op.emitOpError(
1992         "mismatch in number of loop-carried values and defined values");
1993   if (op.getNumRegionIterArgs() != opNumResults)
1994     return op.emitOpError(
1995         "mismatch in number of basic block args and defined values");
1996   auto iterOperands = op.getIterOperands();
1997   auto iterArgs = op.getRegionIterArgs();
1998   auto opResults =
1999       op.finalValue() ? op.getResults().drop_front() : op.getResults();
2000   unsigned i = 0;
2001   for (auto e : llvm::zip(iterOperands, iterArgs, opResults)) {
2002     if (std::get<0>(e).getType() != std::get<2>(e).getType())
2003       return op.emitOpError() << "types mismatch between " << i
2004                               << "th iter operand and defined value";
2005     if (std::get<1>(e).getType() != std::get<2>(e).getType())
2006       return op.emitOpError() << "types mismatch between " << i
2007                               << "th iter region arg and defined value";
2008 
2009     i++;
2010   }
2011   return success();
2012 }
2013 
2014 static void print(mlir::OpAsmPrinter &p, fir::DoLoopOp op) {
2015   bool printBlockTerminators = false;
2016   p << ' ' << op.getInductionVar() << " = " << op.lowerBound() << " to "
2017     << op.upperBound() << " step " << op.step();
2018   if (op.unordered())
2019     p << " unordered";
2020   if (op.hasIterOperands()) {
2021     p << " iter_args(";
2022     auto regionArgs = op.getRegionIterArgs();
2023     auto operands = op.getIterOperands();
2024     llvm::interleaveComma(llvm::zip(regionArgs, operands), p, [&](auto it) {
2025       p << std::get<0>(it) << " = " << std::get<1>(it);
2026     });
2027     p << ") -> (" << op.getResultTypes() << ')';
2028     printBlockTerminators = true;
2029   } else if (op.finalValue()) {
2030     p << " -> " << op.getResultTypes();
2031     printBlockTerminators = true;
2032   }
2033   p.printOptionalAttrDictWithKeyword(op->getAttrs(),
2034                                      {"unordered", "finalValue"});
2035   p << ' ';
2036   p.printRegion(op.region(), /*printEntryBlockArgs=*/false,
2037                 printBlockTerminators);
2038 }
2039 
2040 mlir::Region &fir::DoLoopOp::getLoopBody() { return region(); }
2041 
2042 bool fir::DoLoopOp::isDefinedOutsideOfLoop(mlir::Value value) {
2043   return !region().isAncestor(value.getParentRegion());
2044 }
2045 
2046 mlir::LogicalResult
2047 fir::DoLoopOp::moveOutOfLoop(llvm::ArrayRef<mlir::Operation *> ops) {
2048   for (auto op : ops)
2049     op->moveBefore(*this);
2050   return success();
2051 }
2052 
2053 /// Translate a value passed as an iter_arg to the corresponding block
2054 /// argument in the body of the loop.
2055 mlir::BlockArgument fir::DoLoopOp::iterArgToBlockArg(mlir::Value iterArg) {
2056   for (auto i : llvm::enumerate(initArgs()))
2057     if (iterArg == i.value())
2058       return region().front().getArgument(i.index() + 1);
2059   return {};
2060 }
2061 
2062 /// Translate the result vector (by index number) to the corresponding value
2063 /// to the `fir.result` Op.
2064 void fir::DoLoopOp::resultToSourceOps(
2065     llvm::SmallVectorImpl<mlir::Value> &results, unsigned resultNum) {
2066   auto oper = finalValue() ? resultNum + 1 : resultNum;
2067   auto *term = region().front().getTerminator();
2068   if (oper < term->getNumOperands())
2069     results.push_back(term->getOperand(oper));
2070 }
2071 
2072 /// Translate the block argument (by index number) to the corresponding value
2073 /// passed as an iter_arg to the parent DoLoopOp.
2074 mlir::Value fir::DoLoopOp::blockArgToSourceOp(unsigned blockArgNum) {
2075   if (blockArgNum > 0 && blockArgNum <= initArgs().size())
2076     return initArgs()[blockArgNum - 1];
2077   return {};
2078 }
2079 
2080 //===----------------------------------------------------------------------===//
2081 // DTEntryOp
2082 //===----------------------------------------------------------------------===//
2083 
2084 static mlir::ParseResult parseDTEntryOp(mlir::OpAsmParser &parser,
2085                                         mlir::OperationState &result) {
2086   llvm::StringRef methodName;
2087   // allow `methodName` or `"methodName"`
2088   if (failed(parser.parseOptionalKeyword(&methodName))) {
2089     mlir::StringAttr methodAttr;
2090     if (parser.parseAttribute(methodAttr,
2091                               fir::DTEntryOp::getMethodAttrNameStr(),
2092                               result.attributes))
2093       return mlir::failure();
2094   } else {
2095     result.addAttribute(fir::DTEntryOp::getMethodAttrNameStr(),
2096                         parser.getBuilder().getStringAttr(methodName));
2097   }
2098   mlir::SymbolRefAttr calleeAttr;
2099   if (parser.parseComma() ||
2100       parser.parseAttribute(calleeAttr, fir::DTEntryOp::getProcAttrNameStr(),
2101                             result.attributes))
2102     return mlir::failure();
2103   return mlir::success();
2104 }
2105 
2106 static void print(mlir::OpAsmPrinter &p, fir::DTEntryOp &op) {
2107   p << ' ' << op.getMethodAttr() << ", " << op.getProcAttr();
2108 }
2109 
2110 //===----------------------------------------------------------------------===//
2111 // ReboxOp
2112 //===----------------------------------------------------------------------===//
2113 
2114 /// Get the scalar type related to a fir.box type.
2115 /// Example: return f32 for !fir.box<!fir.heap<!fir.array<?x?xf32>>.
2116 static mlir::Type getBoxScalarEleTy(mlir::Type boxTy) {
2117   auto eleTy = fir::dyn_cast_ptrOrBoxEleTy(boxTy);
2118   if (auto seqTy = eleTy.dyn_cast<fir::SequenceType>())
2119     return seqTy.getEleTy();
2120   return eleTy;
2121 }
2122 
2123 /// Get the rank from a !fir.box type
2124 static unsigned getBoxRank(mlir::Type boxTy) {
2125   auto eleTy = fir::dyn_cast_ptrOrBoxEleTy(boxTy);
2126   if (auto seqTy = eleTy.dyn_cast<fir::SequenceType>())
2127     return seqTy.getDimension();
2128   return 0;
2129 }
2130 
2131 static mlir::LogicalResult verify(fir::ReboxOp op) {
2132   auto inputBoxTy = op.box().getType();
2133   if (fir::isa_unknown_size_box(inputBoxTy))
2134     return op.emitOpError("box operand must not have unknown rank or type");
2135   auto outBoxTy = op.getType();
2136   if (fir::isa_unknown_size_box(outBoxTy))
2137     return op.emitOpError("result type must not have unknown rank or type");
2138   auto inputRank = getBoxRank(inputBoxTy);
2139   auto inputEleTy = getBoxScalarEleTy(inputBoxTy);
2140   auto outRank = getBoxRank(outBoxTy);
2141   auto outEleTy = getBoxScalarEleTy(outBoxTy);
2142 
2143   if (auto slice = op.slice()) {
2144     // Slicing case
2145     if (slice.getType().cast<fir::SliceType>().getRank() != inputRank)
2146       return op.emitOpError("slice operand rank must match box operand rank");
2147     if (auto shape = op.shape()) {
2148       if (auto shiftTy = shape.getType().dyn_cast<fir::ShiftType>()) {
2149         if (shiftTy.getRank() != inputRank)
2150           return op.emitOpError("shape operand and input box ranks must match "
2151                                 "when there is a slice");
2152       } else {
2153         return op.emitOpError("shape operand must absent or be a fir.shift "
2154                               "when there is a slice");
2155       }
2156     }
2157     if (auto sliceOp = slice.getDefiningOp()) {
2158       auto slicedRank = mlir::cast<fir::SliceOp>(sliceOp).getOutRank();
2159       if (slicedRank != outRank)
2160         return op.emitOpError("result type rank and rank after applying slice "
2161                               "operand must match");
2162     }
2163   } else {
2164     // Reshaping case
2165     unsigned shapeRank = inputRank;
2166     if (auto shape = op.shape()) {
2167       auto ty = shape.getType();
2168       if (auto shapeTy = ty.dyn_cast<fir::ShapeType>()) {
2169         shapeRank = shapeTy.getRank();
2170       } else if (auto shapeShiftTy = ty.dyn_cast<fir::ShapeShiftType>()) {
2171         shapeRank = shapeShiftTy.getRank();
2172       } else {
2173         auto shiftTy = ty.cast<fir::ShiftType>();
2174         shapeRank = shiftTy.getRank();
2175         if (shapeRank != inputRank)
2176           return op.emitOpError("shape operand and input box ranks must match "
2177                                 "when the shape is a fir.shift");
2178       }
2179     }
2180     if (shapeRank != outRank)
2181       return op.emitOpError("result type and shape operand ranks must match");
2182   }
2183 
2184   if (inputEleTy != outEleTy)
2185     // TODO: check that outBoxTy is a parent type of inputBoxTy for derived
2186     // types.
2187     if (!inputEleTy.isa<fir::RecordType>())
2188       return op.emitOpError(
2189           "op input and output element types must match for intrinsic types");
2190   return mlir::success();
2191 }
2192 
2193 //===----------------------------------------------------------------------===//
2194 // ResultOp
2195 //===----------------------------------------------------------------------===//
2196 
2197 static mlir::LogicalResult verify(fir::ResultOp op) {
2198   auto *parentOp = op->getParentOp();
2199   auto results = parentOp->getResults();
2200   auto operands = op->getOperands();
2201 
2202   if (parentOp->getNumResults() != op.getNumOperands())
2203     return op.emitOpError() << "parent of result must have same arity";
2204   for (auto e : llvm::zip(results, operands))
2205     if (std::get<0>(e).getType() != std::get<1>(e).getType())
2206       return op.emitOpError()
2207              << "types mismatch between result op and its parent";
2208   return success();
2209 }
2210 
2211 //===----------------------------------------------------------------------===//
2212 // SaveResultOp
2213 //===----------------------------------------------------------------------===//
2214 
2215 static mlir::LogicalResult verify(fir::SaveResultOp op) {
2216   auto resultType = op.value().getType();
2217   if (resultType != fir::dyn_cast_ptrEleTy(op.memref().getType()))
2218     return op.emitOpError("value type must match memory reference type");
2219   if (fir::isa_unknown_size_box(resultType))
2220     return op.emitOpError("cannot save !fir.box of unknown rank or type");
2221 
2222   if (resultType.isa<fir::BoxType>()) {
2223     if (op.shape() || !op.typeparams().empty())
2224       return op.emitOpError(
2225           "must not have shape or length operands if the value is a fir.box");
2226     return mlir::success();
2227   }
2228 
2229   // fir.record or fir.array case.
2230   unsigned shapeTyRank = 0;
2231   if (auto shapeOp = op.shape()) {
2232     auto shapeTy = shapeOp.getType();
2233     if (auto s = shapeTy.dyn_cast<fir::ShapeType>())
2234       shapeTyRank = s.getRank();
2235     else
2236       shapeTyRank = shapeTy.cast<fir::ShapeShiftType>().getRank();
2237   }
2238 
2239   auto eleTy = resultType;
2240   if (auto seqTy = resultType.dyn_cast<fir::SequenceType>()) {
2241     if (seqTy.getDimension() != shapeTyRank)
2242       op.emitOpError("shape operand must be provided and have the value rank "
2243                      "when the value is a fir.array");
2244     eleTy = seqTy.getEleTy();
2245   } else {
2246     if (shapeTyRank != 0)
2247       op.emitOpError(
2248           "shape operand should only be provided if the value is a fir.array");
2249   }
2250 
2251   if (auto recTy = eleTy.dyn_cast<fir::RecordType>()) {
2252     if (recTy.getNumLenParams() != op.typeparams().size())
2253       op.emitOpError("length parameters number must match with the value type "
2254                      "length parameters");
2255   } else if (auto charTy = eleTy.dyn_cast<fir::CharacterType>()) {
2256     if (op.typeparams().size() > 1)
2257       op.emitOpError("no more than one length parameter must be provided for "
2258                      "character value");
2259   } else {
2260     if (!op.typeparams().empty())
2261       op.emitOpError(
2262           "length parameters must not be provided for this value type");
2263   }
2264 
2265   return mlir::success();
2266 }
2267 
2268 //===----------------------------------------------------------------------===//
2269 // SelectOp
2270 //===----------------------------------------------------------------------===//
2271 
2272 static constexpr llvm::StringRef getCompareOffsetAttr() {
2273   return "compare_operand_offsets";
2274 }
2275 
2276 static constexpr llvm::StringRef getTargetOffsetAttr() {
2277   return "target_operand_offsets";
2278 }
2279 
2280 template <typename A, typename... AdditionalArgs>
2281 static A getSubOperands(unsigned pos, A allArgs,
2282                         mlir::DenseIntElementsAttr ranges,
2283                         AdditionalArgs &&...additionalArgs) {
2284   unsigned start = 0;
2285   for (unsigned i = 0; i < pos; ++i)
2286     start += (*(ranges.begin() + i)).getZExtValue();
2287   return allArgs.slice(start, (*(ranges.begin() + pos)).getZExtValue(),
2288                        std::forward<AdditionalArgs>(additionalArgs)...);
2289 }
2290 
2291 static mlir::MutableOperandRange
2292 getMutableSuccessorOperands(unsigned pos, mlir::MutableOperandRange operands,
2293                             StringRef offsetAttr) {
2294   Operation *owner = operands.getOwner();
2295   NamedAttribute targetOffsetAttr =
2296       *owner->getAttrDictionary().getNamed(offsetAttr);
2297   return getSubOperands(
2298       pos, operands, targetOffsetAttr.getValue().cast<DenseIntElementsAttr>(),
2299       mlir::MutableOperandRange::OperandSegment(pos, targetOffsetAttr));
2300 }
2301 
2302 static unsigned denseElementsSize(mlir::DenseIntElementsAttr attr) {
2303   return attr.getNumElements();
2304 }
2305 
2306 llvm::Optional<mlir::OperandRange> fir::SelectOp::getCompareOperands(unsigned) {
2307   return {};
2308 }
2309 
2310 llvm::Optional<llvm::ArrayRef<mlir::Value>>
2311 fir::SelectOp::getCompareOperands(llvm::ArrayRef<mlir::Value>, unsigned) {
2312   return {};
2313 }
2314 
2315 llvm::Optional<mlir::MutableOperandRange>
2316 fir::SelectOp::getMutableSuccessorOperands(unsigned oper) {
2317   return ::getMutableSuccessorOperands(oper, targetArgsMutable(),
2318                                        getTargetOffsetAttr());
2319 }
2320 
2321 llvm::Optional<llvm::ArrayRef<mlir::Value>>
2322 fir::SelectOp::getSuccessorOperands(llvm::ArrayRef<mlir::Value> operands,
2323                                     unsigned oper) {
2324   auto a =
2325       (*this)->getAttrOfType<mlir::DenseIntElementsAttr>(getTargetOffsetAttr());
2326   auto segments = (*this)->getAttrOfType<mlir::DenseIntElementsAttr>(
2327       getOperandSegmentSizeAttr());
2328   return {getSubOperands(oper, getSubOperands(2, operands, segments), a)};
2329 }
2330 
2331 llvm::Optional<mlir::ValueRange>
2332 fir::SelectOp::getSuccessorOperands(mlir::ValueRange operands, unsigned oper) {
2333   auto a =
2334       (*this)->getAttrOfType<mlir::DenseIntElementsAttr>(getTargetOffsetAttr());
2335   auto segments = (*this)->getAttrOfType<mlir::DenseIntElementsAttr>(
2336       getOperandSegmentSizeAttr());
2337   return {getSubOperands(oper, getSubOperands(2, operands, segments), a)};
2338 }
2339 
2340 unsigned fir::SelectOp::targetOffsetSize() {
2341   return denseElementsSize((*this)->getAttrOfType<mlir::DenseIntElementsAttr>(
2342       getTargetOffsetAttr()));
2343 }
2344 
2345 //===----------------------------------------------------------------------===//
2346 // SelectCaseOp
2347 //===----------------------------------------------------------------------===//
2348 
2349 llvm::Optional<mlir::OperandRange>
2350 fir::SelectCaseOp::getCompareOperands(unsigned cond) {
2351   auto a = (*this)->getAttrOfType<mlir::DenseIntElementsAttr>(
2352       getCompareOffsetAttr());
2353   return {getSubOperands(cond, compareArgs(), a)};
2354 }
2355 
2356 llvm::Optional<llvm::ArrayRef<mlir::Value>>
2357 fir::SelectCaseOp::getCompareOperands(llvm::ArrayRef<mlir::Value> operands,
2358                                       unsigned cond) {
2359   auto a = (*this)->getAttrOfType<mlir::DenseIntElementsAttr>(
2360       getCompareOffsetAttr());
2361   auto segments = (*this)->getAttrOfType<mlir::DenseIntElementsAttr>(
2362       getOperandSegmentSizeAttr());
2363   return {getSubOperands(cond, getSubOperands(1, operands, segments), a)};
2364 }
2365 
2366 llvm::Optional<mlir::ValueRange>
2367 fir::SelectCaseOp::getCompareOperands(mlir::ValueRange operands,
2368                                       unsigned cond) {
2369   auto a = (*this)->getAttrOfType<mlir::DenseIntElementsAttr>(
2370       getCompareOffsetAttr());
2371   auto segments = (*this)->getAttrOfType<mlir::DenseIntElementsAttr>(
2372       getOperandSegmentSizeAttr());
2373   return {getSubOperands(cond, getSubOperands(1, operands, segments), a)};
2374 }
2375 
2376 llvm::Optional<mlir::MutableOperandRange>
2377 fir::SelectCaseOp::getMutableSuccessorOperands(unsigned oper) {
2378   return ::getMutableSuccessorOperands(oper, targetArgsMutable(),
2379                                        getTargetOffsetAttr());
2380 }
2381 
2382 llvm::Optional<llvm::ArrayRef<mlir::Value>>
2383 fir::SelectCaseOp::getSuccessorOperands(llvm::ArrayRef<mlir::Value> operands,
2384                                         unsigned oper) {
2385   auto a =
2386       (*this)->getAttrOfType<mlir::DenseIntElementsAttr>(getTargetOffsetAttr());
2387   auto segments = (*this)->getAttrOfType<mlir::DenseIntElementsAttr>(
2388       getOperandSegmentSizeAttr());
2389   return {getSubOperands(oper, getSubOperands(2, operands, segments), a)};
2390 }
2391 
2392 llvm::Optional<mlir::ValueRange>
2393 fir::SelectCaseOp::getSuccessorOperands(mlir::ValueRange operands,
2394                                         unsigned oper) {
2395   auto a =
2396       (*this)->getAttrOfType<mlir::DenseIntElementsAttr>(getTargetOffsetAttr());
2397   auto segments = (*this)->getAttrOfType<mlir::DenseIntElementsAttr>(
2398       getOperandSegmentSizeAttr());
2399   return {getSubOperands(oper, getSubOperands(2, operands, segments), a)};
2400 }
2401 
2402 // parser for fir.select_case Op
2403 static mlir::ParseResult parseSelectCase(mlir::OpAsmParser &parser,
2404                                          mlir::OperationState &result) {
2405   mlir::OpAsmParser::OperandType selector;
2406   mlir::Type type;
2407   if (parseSelector(parser, result, selector, type))
2408     return mlir::failure();
2409 
2410   llvm::SmallVector<mlir::Attribute> attrs;
2411   llvm::SmallVector<mlir::OpAsmParser::OperandType> opers;
2412   llvm::SmallVector<mlir::Block *> dests;
2413   llvm::SmallVector<llvm::SmallVector<mlir::Value>> destArgs;
2414   llvm::SmallVector<int32_t> argOffs;
2415   int32_t offSize = 0;
2416   while (true) {
2417     mlir::Attribute attr;
2418     mlir::Block *dest;
2419     llvm::SmallVector<mlir::Value> destArg;
2420     mlir::NamedAttrList temp;
2421     if (parser.parseAttribute(attr, "a", temp) || isValidCaseAttr(attr) ||
2422         parser.parseComma())
2423       return mlir::failure();
2424     attrs.push_back(attr);
2425     if (attr.dyn_cast_or_null<mlir::UnitAttr>()) {
2426       argOffs.push_back(0);
2427     } else if (attr.dyn_cast_or_null<fir::ClosedIntervalAttr>()) {
2428       mlir::OpAsmParser::OperandType oper1;
2429       mlir::OpAsmParser::OperandType oper2;
2430       if (parser.parseOperand(oper1) || parser.parseComma() ||
2431           parser.parseOperand(oper2) || parser.parseComma())
2432         return mlir::failure();
2433       opers.push_back(oper1);
2434       opers.push_back(oper2);
2435       argOffs.push_back(2);
2436       offSize += 2;
2437     } else {
2438       mlir::OpAsmParser::OperandType oper;
2439       if (parser.parseOperand(oper) || parser.parseComma())
2440         return mlir::failure();
2441       opers.push_back(oper);
2442       argOffs.push_back(1);
2443       ++offSize;
2444     }
2445     if (parser.parseSuccessorAndUseList(dest, destArg))
2446       return mlir::failure();
2447     dests.push_back(dest);
2448     destArgs.push_back(destArg);
2449     if (mlir::succeeded(parser.parseOptionalRSquare()))
2450       break;
2451     if (parser.parseComma())
2452       return mlir::failure();
2453   }
2454   result.addAttribute(fir::SelectCaseOp::getCasesAttr(),
2455                       parser.getBuilder().getArrayAttr(attrs));
2456   if (parser.resolveOperands(opers, type, result.operands))
2457     return mlir::failure();
2458   llvm::SmallVector<int32_t> targOffs;
2459   int32_t toffSize = 0;
2460   const auto count = dests.size();
2461   for (std::remove_const_t<decltype(count)> i = 0; i != count; ++i) {
2462     result.addSuccessors(dests[i]);
2463     result.addOperands(destArgs[i]);
2464     auto argSize = destArgs[i].size();
2465     targOffs.push_back(argSize);
2466     toffSize += argSize;
2467   }
2468   auto &bld = parser.getBuilder();
2469   result.addAttribute(fir::SelectCaseOp::getOperandSegmentSizeAttr(),
2470                       bld.getI32VectorAttr({1, offSize, toffSize}));
2471   result.addAttribute(getCompareOffsetAttr(), bld.getI32VectorAttr(argOffs));
2472   result.addAttribute(getTargetOffsetAttr(), bld.getI32VectorAttr(targOffs));
2473   return mlir::success();
2474 }
2475 
2476 static void print(mlir::OpAsmPrinter &p, fir::SelectCaseOp &op) {
2477   p << ' ';
2478   p.printOperand(op.getSelector());
2479   p << " : " << op.getSelector().getType() << " [";
2480   auto cases = op.getOperation()
2481                    ->getAttrOfType<mlir::ArrayAttr>(op.getCasesAttr())
2482                    .getValue();
2483   auto count = op.getNumConditions();
2484   for (decltype(count) i = 0; i != count; ++i) {
2485     if (i)
2486       p << ", ";
2487     p << cases[i] << ", ";
2488     if (!cases[i].isa<mlir::UnitAttr>()) {
2489       auto caseArgs = *op.getCompareOperands(i);
2490       p.printOperand(*caseArgs.begin());
2491       p << ", ";
2492       if (cases[i].isa<fir::ClosedIntervalAttr>()) {
2493         p.printOperand(*(++caseArgs.begin()));
2494         p << ", ";
2495       }
2496     }
2497     op.printSuccessorAtIndex(p, i);
2498   }
2499   p << ']';
2500   p.printOptionalAttrDict(op.getOperation()->getAttrs(),
2501                           {op.getCasesAttr(), getCompareOffsetAttr(),
2502                            getTargetOffsetAttr(),
2503                            op.getOperandSegmentSizeAttr()});
2504 }
2505 
2506 unsigned fir::SelectCaseOp::compareOffsetSize() {
2507   return denseElementsSize((*this)->getAttrOfType<mlir::DenseIntElementsAttr>(
2508       getCompareOffsetAttr()));
2509 }
2510 
2511 unsigned fir::SelectCaseOp::targetOffsetSize() {
2512   return denseElementsSize((*this)->getAttrOfType<mlir::DenseIntElementsAttr>(
2513       getTargetOffsetAttr()));
2514 }
2515 
2516 void fir::SelectCaseOp::build(mlir::OpBuilder &builder,
2517                               mlir::OperationState &result,
2518                               mlir::Value selector,
2519                               llvm::ArrayRef<mlir::Attribute> compareAttrs,
2520                               llvm::ArrayRef<mlir::ValueRange> cmpOperands,
2521                               llvm::ArrayRef<mlir::Block *> destinations,
2522                               llvm::ArrayRef<mlir::ValueRange> destOperands,
2523                               llvm::ArrayRef<mlir::NamedAttribute> attributes) {
2524   result.addOperands(selector);
2525   result.addAttribute(getCasesAttr(), builder.getArrayAttr(compareAttrs));
2526   llvm::SmallVector<int32_t> operOffs;
2527   int32_t operSize = 0;
2528   for (auto attr : compareAttrs) {
2529     if (attr.isa<fir::ClosedIntervalAttr>()) {
2530       operOffs.push_back(2);
2531       operSize += 2;
2532     } else if (attr.isa<mlir::UnitAttr>()) {
2533       operOffs.push_back(0);
2534     } else {
2535       operOffs.push_back(1);
2536       ++operSize;
2537     }
2538   }
2539   for (auto ops : cmpOperands)
2540     result.addOperands(ops);
2541   result.addAttribute(getCompareOffsetAttr(),
2542                       builder.getI32VectorAttr(operOffs));
2543   const auto count = destinations.size();
2544   for (auto d : destinations)
2545     result.addSuccessors(d);
2546   const auto opCount = destOperands.size();
2547   llvm::SmallVector<int32_t> argOffs;
2548   int32_t sumArgs = 0;
2549   for (std::remove_const_t<decltype(count)> i = 0; i != count; ++i) {
2550     if (i < opCount) {
2551       result.addOperands(destOperands[i]);
2552       const auto argSz = destOperands[i].size();
2553       argOffs.push_back(argSz);
2554       sumArgs += argSz;
2555     } else {
2556       argOffs.push_back(0);
2557     }
2558   }
2559   result.addAttribute(getOperandSegmentSizeAttr(),
2560                       builder.getI32VectorAttr({1, operSize, sumArgs}));
2561   result.addAttribute(getTargetOffsetAttr(), builder.getI32VectorAttr(argOffs));
2562   result.addAttributes(attributes);
2563 }
2564 
2565 /// This builder has a slightly simplified interface in that the list of
2566 /// operands need not be partitioned by the builder. Instead the operands are
2567 /// partitioned here, before being passed to the default builder. This
2568 /// partitioning is unchecked, so can go awry on bad input.
2569 void fir::SelectCaseOp::build(mlir::OpBuilder &builder,
2570                               mlir::OperationState &result,
2571                               mlir::Value selector,
2572                               llvm::ArrayRef<mlir::Attribute> compareAttrs,
2573                               llvm::ArrayRef<mlir::Value> cmpOpList,
2574                               llvm::ArrayRef<mlir::Block *> destinations,
2575                               llvm::ArrayRef<mlir::ValueRange> destOperands,
2576                               llvm::ArrayRef<mlir::NamedAttribute> attributes) {
2577   llvm::SmallVector<mlir::ValueRange> cmpOpers;
2578   auto iter = cmpOpList.begin();
2579   for (auto &attr : compareAttrs) {
2580     if (attr.isa<fir::ClosedIntervalAttr>()) {
2581       cmpOpers.push_back(mlir::ValueRange({iter, iter + 2}));
2582       iter += 2;
2583     } else if (attr.isa<UnitAttr>()) {
2584       cmpOpers.push_back(mlir::ValueRange{});
2585     } else {
2586       cmpOpers.push_back(mlir::ValueRange({iter, iter + 1}));
2587       ++iter;
2588     }
2589   }
2590   build(builder, result, selector, compareAttrs, cmpOpers, destinations,
2591         destOperands, attributes);
2592 }
2593 
2594 static mlir::LogicalResult verify(fir::SelectCaseOp &op) {
2595   if (!(op.getSelector().getType().isa<mlir::IntegerType>() ||
2596         op.getSelector().getType().isa<mlir::IndexType>() ||
2597         op.getSelector().getType().isa<fir::IntegerType>() ||
2598         op.getSelector().getType().isa<fir::LogicalType>() ||
2599         op.getSelector().getType().isa<fir::CharacterType>()))
2600     return op.emitOpError("must be an integer, character, or logical");
2601   auto cases = op.getOperation()
2602                    ->getAttrOfType<mlir::ArrayAttr>(op.getCasesAttr())
2603                    .getValue();
2604   auto count = op.getNumDest();
2605   if (count == 0)
2606     return op.emitOpError("must have at least one successor");
2607   if (op.getNumConditions() != count)
2608     return op.emitOpError("number of conditions and successors don't match");
2609   if (op.compareOffsetSize() != count)
2610     return op.emitOpError("incorrect number of compare operand groups");
2611   if (op.targetOffsetSize() != count)
2612     return op.emitOpError("incorrect number of successor operand groups");
2613   for (decltype(count) i = 0; i != count; ++i) {
2614     auto &attr = cases[i];
2615     if (!(attr.isa<fir::PointIntervalAttr>() ||
2616           attr.isa<fir::LowerBoundAttr>() || attr.isa<fir::UpperBoundAttr>() ||
2617           attr.isa<fir::ClosedIntervalAttr>() || attr.isa<mlir::UnitAttr>()))
2618       return op.emitOpError("incorrect select case attribute type");
2619   }
2620   return mlir::success();
2621 }
2622 
2623 //===----------------------------------------------------------------------===//
2624 // SelectRankOp
2625 //===----------------------------------------------------------------------===//
2626 
2627 llvm::Optional<mlir::OperandRange>
2628 fir::SelectRankOp::getCompareOperands(unsigned) {
2629   return {};
2630 }
2631 
2632 llvm::Optional<llvm::ArrayRef<mlir::Value>>
2633 fir::SelectRankOp::getCompareOperands(llvm::ArrayRef<mlir::Value>, unsigned) {
2634   return {};
2635 }
2636 
2637 llvm::Optional<mlir::MutableOperandRange>
2638 fir::SelectRankOp::getMutableSuccessorOperands(unsigned oper) {
2639   return ::getMutableSuccessorOperands(oper, targetArgsMutable(),
2640                                        getTargetOffsetAttr());
2641 }
2642 
2643 llvm::Optional<llvm::ArrayRef<mlir::Value>>
2644 fir::SelectRankOp::getSuccessorOperands(llvm::ArrayRef<mlir::Value> operands,
2645                                         unsigned oper) {
2646   auto a =
2647       (*this)->getAttrOfType<mlir::DenseIntElementsAttr>(getTargetOffsetAttr());
2648   auto segments = (*this)->getAttrOfType<mlir::DenseIntElementsAttr>(
2649       getOperandSegmentSizeAttr());
2650   return {getSubOperands(oper, getSubOperands(2, operands, segments), a)};
2651 }
2652 
2653 llvm::Optional<mlir::ValueRange>
2654 fir::SelectRankOp::getSuccessorOperands(mlir::ValueRange operands,
2655                                         unsigned oper) {
2656   auto a =
2657       (*this)->getAttrOfType<mlir::DenseIntElementsAttr>(getTargetOffsetAttr());
2658   auto segments = (*this)->getAttrOfType<mlir::DenseIntElementsAttr>(
2659       getOperandSegmentSizeAttr());
2660   return {getSubOperands(oper, getSubOperands(2, operands, segments), a)};
2661 }
2662 
2663 unsigned fir::SelectRankOp::targetOffsetSize() {
2664   return denseElementsSize((*this)->getAttrOfType<mlir::DenseIntElementsAttr>(
2665       getTargetOffsetAttr()));
2666 }
2667 
2668 //===----------------------------------------------------------------------===//
2669 // SelectTypeOp
2670 //===----------------------------------------------------------------------===//
2671 
2672 llvm::Optional<mlir::OperandRange>
2673 fir::SelectTypeOp::getCompareOperands(unsigned) {
2674   return {};
2675 }
2676 
2677 llvm::Optional<llvm::ArrayRef<mlir::Value>>
2678 fir::SelectTypeOp::getCompareOperands(llvm::ArrayRef<mlir::Value>, unsigned) {
2679   return {};
2680 }
2681 
2682 llvm::Optional<mlir::MutableOperandRange>
2683 fir::SelectTypeOp::getMutableSuccessorOperands(unsigned oper) {
2684   return ::getMutableSuccessorOperands(oper, targetArgsMutable(),
2685                                        getTargetOffsetAttr());
2686 }
2687 
2688 llvm::Optional<llvm::ArrayRef<mlir::Value>>
2689 fir::SelectTypeOp::getSuccessorOperands(llvm::ArrayRef<mlir::Value> operands,
2690                                         unsigned oper) {
2691   auto a =
2692       (*this)->getAttrOfType<mlir::DenseIntElementsAttr>(getTargetOffsetAttr());
2693   auto segments = (*this)->getAttrOfType<mlir::DenseIntElementsAttr>(
2694       getOperandSegmentSizeAttr());
2695   return {getSubOperands(oper, getSubOperands(2, operands, segments), a)};
2696 }
2697 
2698 static ParseResult parseSelectType(OpAsmParser &parser,
2699                                    OperationState &result) {
2700   mlir::OpAsmParser::OperandType selector;
2701   mlir::Type type;
2702   if (parseSelector(parser, result, selector, type))
2703     return mlir::failure();
2704 
2705   llvm::SmallVector<mlir::Attribute> attrs;
2706   llvm::SmallVector<mlir::Block *> dests;
2707   llvm::SmallVector<llvm::SmallVector<mlir::Value>> destArgs;
2708   while (true) {
2709     mlir::Attribute attr;
2710     mlir::Block *dest;
2711     llvm::SmallVector<mlir::Value> destArg;
2712     mlir::NamedAttrList temp;
2713     if (parser.parseAttribute(attr, "a", temp) || parser.parseComma() ||
2714         parser.parseSuccessorAndUseList(dest, destArg))
2715       return mlir::failure();
2716     attrs.push_back(attr);
2717     dests.push_back(dest);
2718     destArgs.push_back(destArg);
2719     if (mlir::succeeded(parser.parseOptionalRSquare()))
2720       break;
2721     if (parser.parseComma())
2722       return mlir::failure();
2723   }
2724   auto &bld = parser.getBuilder();
2725   result.addAttribute(fir::SelectTypeOp::getCasesAttr(),
2726                       bld.getArrayAttr(attrs));
2727   llvm::SmallVector<int32_t> argOffs;
2728   int32_t offSize = 0;
2729   const auto count = dests.size();
2730   for (std::remove_const_t<decltype(count)> i = 0; i != count; ++i) {
2731     result.addSuccessors(dests[i]);
2732     result.addOperands(destArgs[i]);
2733     auto argSize = destArgs[i].size();
2734     argOffs.push_back(argSize);
2735     offSize += argSize;
2736   }
2737   result.addAttribute(fir::SelectTypeOp::getOperandSegmentSizeAttr(),
2738                       bld.getI32VectorAttr({1, 0, offSize}));
2739   result.addAttribute(getTargetOffsetAttr(), bld.getI32VectorAttr(argOffs));
2740   return mlir::success();
2741 }
2742 
2743 unsigned fir::SelectTypeOp::targetOffsetSize() {
2744   return denseElementsSize((*this)->getAttrOfType<mlir::DenseIntElementsAttr>(
2745       getTargetOffsetAttr()));
2746 }
2747 
2748 static void print(mlir::OpAsmPrinter &p, fir::SelectTypeOp &op) {
2749   p << ' ';
2750   p.printOperand(op.getSelector());
2751   p << " : " << op.getSelector().getType() << " [";
2752   auto cases = op.getOperation()
2753                    ->getAttrOfType<mlir::ArrayAttr>(op.getCasesAttr())
2754                    .getValue();
2755   auto count = op.getNumConditions();
2756   for (decltype(count) i = 0; i != count; ++i) {
2757     if (i)
2758       p << ", ";
2759     p << cases[i] << ", ";
2760     op.printSuccessorAtIndex(p, i);
2761   }
2762   p << ']';
2763   p.printOptionalAttrDict(op.getOperation()->getAttrs(),
2764                           {op.getCasesAttr(), getCompareOffsetAttr(),
2765                            getTargetOffsetAttr(),
2766                            fir::SelectTypeOp::getOperandSegmentSizeAttr()});
2767 }
2768 
2769 static mlir::LogicalResult verify(fir::SelectTypeOp &op) {
2770   if (!(op.getSelector().getType().isa<fir::BoxType>()))
2771     return op.emitOpError("must be a boxed type");
2772   auto cases = op.getOperation()
2773                    ->getAttrOfType<mlir::ArrayAttr>(op.getCasesAttr())
2774                    .getValue();
2775   auto count = op.getNumDest();
2776   if (count == 0)
2777     return op.emitOpError("must have at least one successor");
2778   if (op.getNumConditions() != count)
2779     return op.emitOpError("number of conditions and successors don't match");
2780   if (op.targetOffsetSize() != count)
2781     return op.emitOpError("incorrect number of successor operand groups");
2782   for (decltype(count) i = 0; i != count; ++i) {
2783     auto &attr = cases[i];
2784     if (!(attr.isa<fir::ExactTypeAttr>() || attr.isa<fir::SubclassAttr>() ||
2785           attr.isa<mlir::UnitAttr>()))
2786       return op.emitOpError("invalid type-case alternative");
2787   }
2788   return mlir::success();
2789 }
2790 
2791 void fir::SelectTypeOp::build(mlir::OpBuilder &builder,
2792                               mlir::OperationState &result,
2793                               mlir::Value selector,
2794                               llvm::ArrayRef<mlir::Attribute> typeOperands,
2795                               llvm::ArrayRef<mlir::Block *> destinations,
2796                               llvm::ArrayRef<mlir::ValueRange> destOperands,
2797                               llvm::ArrayRef<mlir::NamedAttribute> attributes) {
2798   result.addOperands(selector);
2799   result.addAttribute(getCasesAttr(), builder.getArrayAttr(typeOperands));
2800   const auto count = destinations.size();
2801   for (mlir::Block *dest : destinations)
2802     result.addSuccessors(dest);
2803   const auto opCount = destOperands.size();
2804   llvm::SmallVector<int32_t> argOffs;
2805   int32_t sumArgs = 0;
2806   for (std::remove_const_t<decltype(count)> i = 0; i != count; ++i) {
2807     if (i < opCount) {
2808       result.addOperands(destOperands[i]);
2809       const auto argSz = destOperands[i].size();
2810       argOffs.push_back(argSz);
2811       sumArgs += argSz;
2812     } else {
2813       argOffs.push_back(0);
2814     }
2815   }
2816   result.addAttribute(getOperandSegmentSizeAttr(),
2817                       builder.getI32VectorAttr({1, 0, sumArgs}));
2818   result.addAttribute(getTargetOffsetAttr(), builder.getI32VectorAttr(argOffs));
2819   result.addAttributes(attributes);
2820 }
2821 
2822 //===----------------------------------------------------------------------===//
2823 // ShapeOp
2824 //===----------------------------------------------------------------------===//
2825 
2826 static mlir::LogicalResult verify(fir::ShapeOp &op) {
2827   auto size = op.extents().size();
2828   auto shapeTy = op.getType().dyn_cast<fir::ShapeType>();
2829   assert(shapeTy && "must be a shape type");
2830   if (shapeTy.getRank() != size)
2831     return op.emitOpError("shape type rank mismatch");
2832   return mlir::success();
2833 }
2834 
2835 //===----------------------------------------------------------------------===//
2836 // ShapeShiftOp
2837 //===----------------------------------------------------------------------===//
2838 
2839 static mlir::LogicalResult verify(fir::ShapeShiftOp &op) {
2840   auto size = op.pairs().size();
2841   if (size < 2 || size > 16 * 2)
2842     return op.emitOpError("incorrect number of args");
2843   if (size % 2 != 0)
2844     return op.emitOpError("requires a multiple of 2 args");
2845   auto shapeTy = op.getType().dyn_cast<fir::ShapeShiftType>();
2846   assert(shapeTy && "must be a shape shift type");
2847   if (shapeTy.getRank() * 2 != size)
2848     return op.emitOpError("shape type rank mismatch");
2849   return mlir::success();
2850 }
2851 
2852 //===----------------------------------------------------------------------===//
2853 // ShiftOp
2854 //===----------------------------------------------------------------------===//
2855 
2856 static mlir::LogicalResult verify(fir::ShiftOp &op) {
2857   auto size = op.origins().size();
2858   auto shiftTy = op.getType().dyn_cast<fir::ShiftType>();
2859   assert(shiftTy && "must be a shift type");
2860   if (shiftTy.getRank() != size)
2861     return op.emitOpError("shift type rank mismatch");
2862   return mlir::success();
2863 }
2864 
2865 //===----------------------------------------------------------------------===//
2866 // SliceOp
2867 //===----------------------------------------------------------------------===//
2868 
2869 void fir::SliceOp::build(mlir::OpBuilder &builder, mlir::OperationState &result,
2870                          mlir::ValueRange trips, mlir::ValueRange path,
2871                          mlir::ValueRange substr) {
2872   const auto rank = trips.size() / 3;
2873   auto sliceTy = fir::SliceType::get(builder.getContext(), rank);
2874   build(builder, result, sliceTy, trips, path, substr);
2875 }
2876 
2877 /// Return the output rank of a slice op. The output rank must be between 1 and
2878 /// the rank of the array being sliced (inclusive).
2879 unsigned fir::SliceOp::getOutputRank(mlir::ValueRange triples) {
2880   unsigned rank = 0;
2881   if (!triples.empty()) {
2882     for (unsigned i = 1, end = triples.size(); i < end; i += 3) {
2883       auto *op = triples[i].getDefiningOp();
2884       if (!mlir::isa_and_nonnull<fir::UndefOp>(op))
2885         ++rank;
2886     }
2887     assert(rank > 0);
2888   }
2889   return rank;
2890 }
2891 
2892 static mlir::LogicalResult verify(fir::SliceOp &op) {
2893   auto size = op.triples().size();
2894   if (size < 3 || size > 16 * 3)
2895     return op.emitOpError("incorrect number of args for triple");
2896   if (size % 3 != 0)
2897     return op.emitOpError("requires a multiple of 3 args");
2898   auto sliceTy = op.getType().dyn_cast<fir::SliceType>();
2899   assert(sliceTy && "must be a slice type");
2900   if (sliceTy.getRank() * 3 != size)
2901     return op.emitOpError("slice type rank mismatch");
2902   return mlir::success();
2903 }
2904 
2905 //===----------------------------------------------------------------------===//
2906 // StoreOp
2907 //===----------------------------------------------------------------------===//
2908 
2909 mlir::Type fir::StoreOp::elementType(mlir::Type refType) {
2910   return fir::dyn_cast_ptrEleTy(refType);
2911 }
2912 
2913 static mlir::ParseResult parseStoreOp(mlir::OpAsmParser &parser,
2914                                       mlir::OperationState &result) {
2915   mlir::Type type;
2916   mlir::OpAsmParser::OperandType oper;
2917   mlir::OpAsmParser::OperandType store;
2918   if (parser.parseOperand(oper) || parser.parseKeyword("to") ||
2919       parser.parseOperand(store) ||
2920       parser.parseOptionalAttrDict(result.attributes) ||
2921       parser.parseColonType(type) ||
2922       parser.resolveOperand(oper, fir::StoreOp::elementType(type),
2923                             result.operands) ||
2924       parser.resolveOperand(store, type, result.operands))
2925     return mlir::failure();
2926   return mlir::success();
2927 }
2928 
2929 static void print(mlir::OpAsmPrinter &p, fir::StoreOp &op) {
2930   p << ' ';
2931   p.printOperand(op.value());
2932   p << " to ";
2933   p.printOperand(op.memref());
2934   p.printOptionalAttrDict(op.getOperation()->getAttrs(), {});
2935   p << " : " << op.memref().getType();
2936 }
2937 
2938 static mlir::LogicalResult verify(fir::StoreOp &op) {
2939   if (op.value().getType() != fir::dyn_cast_ptrEleTy(op.memref().getType()))
2940     return op.emitOpError("store value type must match memory reference type");
2941   if (fir::isa_unknown_size_box(op.value().getType()))
2942     return op.emitOpError("cannot store !fir.box of unknown rank or type");
2943   return mlir::success();
2944 }
2945 
2946 //===----------------------------------------------------------------------===//
2947 // StringLitOp
2948 //===----------------------------------------------------------------------===//
2949 
2950 bool fir::StringLitOp::isWideValue() {
2951   auto eleTy = getType().cast<fir::SequenceType>().getEleTy();
2952   return eleTy.cast<fir::CharacterType>().getFKind() != 1;
2953 }
2954 
2955 static mlir::NamedAttribute
2956 mkNamedIntegerAttr(mlir::OpBuilder &builder, llvm::StringRef name, int64_t v) {
2957   assert(v > 0);
2958   return builder.getNamedAttr(
2959       name, builder.getIntegerAttr(builder.getIntegerType(64), v));
2960 }
2961 
2962 void fir::StringLitOp::build(mlir::OpBuilder &builder, OperationState &result,
2963                              fir::CharacterType inType, llvm::StringRef val,
2964                              llvm::Optional<int64_t> len) {
2965   auto valAttr = builder.getNamedAttr(value(), builder.getStringAttr(val));
2966   int64_t length = len.hasValue() ? len.getValue() : inType.getLen();
2967   auto lenAttr = mkNamedIntegerAttr(builder, size(), length);
2968   result.addAttributes({valAttr, lenAttr});
2969   result.addTypes(inType);
2970 }
2971 
2972 template <typename C>
2973 static mlir::ArrayAttr convertToArrayAttr(mlir::OpBuilder &builder,
2974                                           llvm::ArrayRef<C> xlist) {
2975   llvm::SmallVector<mlir::Attribute> attrs;
2976   auto ty = builder.getIntegerType(8 * sizeof(C));
2977   for (auto ch : xlist)
2978     attrs.push_back(builder.getIntegerAttr(ty, ch));
2979   return builder.getArrayAttr(attrs);
2980 }
2981 
2982 void fir::StringLitOp::build(mlir::OpBuilder &builder, OperationState &result,
2983                              fir::CharacterType inType,
2984                              llvm::ArrayRef<char> vlist,
2985                              llvm::Optional<int64_t> len) {
2986   auto valAttr =
2987       builder.getNamedAttr(xlist(), convertToArrayAttr(builder, vlist));
2988   std::int64_t length = len.hasValue() ? len.getValue() : inType.getLen();
2989   auto lenAttr = mkNamedIntegerAttr(builder, size(), length);
2990   result.addAttributes({valAttr, lenAttr});
2991   result.addTypes(inType);
2992 }
2993 
2994 void fir::StringLitOp::build(mlir::OpBuilder &builder, OperationState &result,
2995                              fir::CharacterType inType,
2996                              llvm::ArrayRef<char16_t> vlist,
2997                              llvm::Optional<int64_t> len) {
2998   auto valAttr =
2999       builder.getNamedAttr(xlist(), convertToArrayAttr(builder, vlist));
3000   std::int64_t length = len.hasValue() ? len.getValue() : inType.getLen();
3001   auto lenAttr = mkNamedIntegerAttr(builder, size(), length);
3002   result.addAttributes({valAttr, lenAttr});
3003   result.addTypes(inType);
3004 }
3005 
3006 void fir::StringLitOp::build(mlir::OpBuilder &builder, OperationState &result,
3007                              fir::CharacterType inType,
3008                              llvm::ArrayRef<char32_t> vlist,
3009                              llvm::Optional<int64_t> len) {
3010   auto valAttr =
3011       builder.getNamedAttr(xlist(), convertToArrayAttr(builder, vlist));
3012   std::int64_t length = len.hasValue() ? len.getValue() : inType.getLen();
3013   auto lenAttr = mkNamedIntegerAttr(builder, size(), length);
3014   result.addAttributes({valAttr, lenAttr});
3015   result.addTypes(inType);
3016 }
3017 
3018 static mlir::ParseResult parseStringLitOp(mlir::OpAsmParser &parser,
3019                                           mlir::OperationState &result) {
3020   auto &builder = parser.getBuilder();
3021   mlir::Attribute val;
3022   mlir::NamedAttrList attrs;
3023   llvm::SMLoc trailingTypeLoc;
3024   if (parser.parseAttribute(val, "fake", attrs))
3025     return mlir::failure();
3026   if (auto v = val.dyn_cast<mlir::StringAttr>())
3027     result.attributes.push_back(
3028         builder.getNamedAttr(fir::StringLitOp::value(), v));
3029   else if (auto v = val.dyn_cast<mlir::ArrayAttr>())
3030     result.attributes.push_back(
3031         builder.getNamedAttr(fir::StringLitOp::xlist(), v));
3032   else
3033     return parser.emitError(parser.getCurrentLocation(),
3034                             "found an invalid constant");
3035   mlir::IntegerAttr sz;
3036   mlir::Type type;
3037   if (parser.parseLParen() ||
3038       parser.parseAttribute(sz, fir::StringLitOp::size(), result.attributes) ||
3039       parser.parseRParen() || parser.getCurrentLocation(&trailingTypeLoc) ||
3040       parser.parseColonType(type))
3041     return mlir::failure();
3042   auto charTy = type.dyn_cast<fir::CharacterType>();
3043   if (!charTy)
3044     return parser.emitError(trailingTypeLoc, "must have character type");
3045   type = fir::CharacterType::get(builder.getContext(), charTy.getFKind(),
3046                                  sz.getInt());
3047   if (!type || parser.addTypesToList(type, result.types))
3048     return mlir::failure();
3049   return mlir::success();
3050 }
3051 
3052 static void print(mlir::OpAsmPrinter &p, fir::StringLitOp &op) {
3053   p << ' ' << op.getValue() << '(';
3054   p << op.getSize().cast<mlir::IntegerAttr>().getValue() << ") : ";
3055   p.printType(op.getType());
3056 }
3057 
3058 static mlir::LogicalResult verify(fir::StringLitOp &op) {
3059   if (op.getSize().cast<mlir::IntegerAttr>().getValue().isNegative())
3060     return op.emitOpError("size must be non-negative");
3061   if (auto xl = op.getOperation()->getAttr(fir::StringLitOp::xlist())) {
3062     auto xList = xl.cast<mlir::ArrayAttr>();
3063     for (auto a : xList)
3064       if (!a.isa<mlir::IntegerAttr>())
3065         return op.emitOpError("values in list must be integers");
3066   }
3067   return mlir::success();
3068 }
3069 
3070 //===----------------------------------------------------------------------===//
3071 // UnboxProcOp
3072 //===----------------------------------------------------------------------===//
3073 
3074 static mlir::LogicalResult verify(fir::UnboxProcOp &op) {
3075   if (auto eleTy = fir::dyn_cast_ptrEleTy(op.refTuple().getType()))
3076     if (eleTy.isa<mlir::TupleType>())
3077       return mlir::success();
3078   return op.emitOpError("second output argument has bad type");
3079 }
3080 
3081 //===----------------------------------------------------------------------===//
3082 // IfOp
3083 //===----------------------------------------------------------------------===//
3084 
3085 void fir::IfOp::build(mlir::OpBuilder &builder, OperationState &result,
3086                       mlir::Value cond, bool withElseRegion) {
3087   build(builder, result, llvm::None, cond, withElseRegion);
3088 }
3089 
3090 void fir::IfOp::build(mlir::OpBuilder &builder, OperationState &result,
3091                       mlir::TypeRange resultTypes, mlir::Value cond,
3092                       bool withElseRegion) {
3093   result.addOperands(cond);
3094   result.addTypes(resultTypes);
3095 
3096   mlir::Region *thenRegion = result.addRegion();
3097   thenRegion->push_back(new mlir::Block());
3098   if (resultTypes.empty())
3099     IfOp::ensureTerminator(*thenRegion, builder, result.location);
3100 
3101   mlir::Region *elseRegion = result.addRegion();
3102   if (withElseRegion) {
3103     elseRegion->push_back(new mlir::Block());
3104     if (resultTypes.empty())
3105       IfOp::ensureTerminator(*elseRegion, builder, result.location);
3106   }
3107 }
3108 
3109 static mlir::ParseResult parseIfOp(OpAsmParser &parser,
3110                                    OperationState &result) {
3111   result.regions.reserve(2);
3112   mlir::Region *thenRegion = result.addRegion();
3113   mlir::Region *elseRegion = result.addRegion();
3114 
3115   auto &builder = parser.getBuilder();
3116   OpAsmParser::OperandType cond;
3117   mlir::Type i1Type = builder.getIntegerType(1);
3118   if (parser.parseOperand(cond) ||
3119       parser.resolveOperand(cond, i1Type, result.operands))
3120     return mlir::failure();
3121 
3122   if (parser.parseOptionalArrowTypeList(result.types))
3123     return mlir::failure();
3124 
3125   if (parser.parseRegion(*thenRegion, {}, {}))
3126     return mlir::failure();
3127   IfOp::ensureTerminator(*thenRegion, parser.getBuilder(), result.location);
3128 
3129   if (mlir::succeeded(parser.parseOptionalKeyword("else"))) {
3130     if (parser.parseRegion(*elseRegion, {}, {}))
3131       return mlir::failure();
3132     IfOp::ensureTerminator(*elseRegion, parser.getBuilder(), result.location);
3133   }
3134 
3135   // Parse the optional attribute list.
3136   if (parser.parseOptionalAttrDict(result.attributes))
3137     return mlir::failure();
3138   return mlir::success();
3139 }
3140 
3141 static LogicalResult verify(fir::IfOp op) {
3142   if (op.getNumResults() != 0 && op.elseRegion().empty())
3143     return op.emitOpError("must have an else block if defining values");
3144 
3145   return mlir::success();
3146 }
3147 
3148 static void print(mlir::OpAsmPrinter &p, fir::IfOp op) {
3149   bool printBlockTerminators = false;
3150   p << ' ' << op.condition();
3151   if (!op.results().empty()) {
3152     p << " -> (" << op.getResultTypes() << ')';
3153     printBlockTerminators = true;
3154   }
3155   p << ' ';
3156   p.printRegion(op.thenRegion(), /*printEntryBlockArgs=*/false,
3157                 printBlockTerminators);
3158 
3159   // Print the 'else' regions if it exists and has a block.
3160   auto &otherReg = op.elseRegion();
3161   if (!otherReg.empty()) {
3162     p << " else ";
3163     p.printRegion(otherReg, /*printEntryBlockArgs=*/false,
3164                   printBlockTerminators);
3165   }
3166   p.printOptionalAttrDict(op->getAttrs());
3167 }
3168 
3169 void fir::IfOp::resultToSourceOps(llvm::SmallVectorImpl<mlir::Value> &results,
3170                                   unsigned resultNum) {
3171   auto *term = thenRegion().front().getTerminator();
3172   if (resultNum < term->getNumOperands())
3173     results.push_back(term->getOperand(resultNum));
3174   term = elseRegion().front().getTerminator();
3175   if (resultNum < term->getNumOperands())
3176     results.push_back(term->getOperand(resultNum));
3177 }
3178 
3179 //===----------------------------------------------------------------------===//
3180 
3181 mlir::ParseResult fir::isValidCaseAttr(mlir::Attribute attr) {
3182   if (attr.dyn_cast_or_null<mlir::UnitAttr>() ||
3183       attr.dyn_cast_or_null<ClosedIntervalAttr>() ||
3184       attr.dyn_cast_or_null<PointIntervalAttr>() ||
3185       attr.dyn_cast_or_null<LowerBoundAttr>() ||
3186       attr.dyn_cast_or_null<UpperBoundAttr>())
3187     return mlir::success();
3188   return mlir::failure();
3189 }
3190 
3191 unsigned fir::getCaseArgumentOffset(llvm::ArrayRef<mlir::Attribute> cases,
3192                                     unsigned dest) {
3193   unsigned o = 0;
3194   for (unsigned i = 0; i < dest; ++i) {
3195     auto &attr = cases[i];
3196     if (!attr.dyn_cast_or_null<mlir::UnitAttr>()) {
3197       ++o;
3198       if (attr.dyn_cast_or_null<ClosedIntervalAttr>())
3199         ++o;
3200     }
3201   }
3202   return o;
3203 }
3204 
3205 mlir::ParseResult fir::parseSelector(mlir::OpAsmParser &parser,
3206                                      mlir::OperationState &result,
3207                                      mlir::OpAsmParser::OperandType &selector,
3208                                      mlir::Type &type) {
3209   if (parser.parseOperand(selector) || parser.parseColonType(type) ||
3210       parser.resolveOperand(selector, type, result.operands) ||
3211       parser.parseLSquare())
3212     return mlir::failure();
3213   return mlir::success();
3214 }
3215 
3216 bool fir::isReferenceLike(mlir::Type type) {
3217   return type.isa<fir::ReferenceType>() || type.isa<fir::HeapType>() ||
3218          type.isa<fir::PointerType>();
3219 }
3220 
3221 mlir::FuncOp fir::createFuncOp(mlir::Location loc, mlir::ModuleOp module,
3222                                StringRef name, mlir::FunctionType type,
3223                                llvm::ArrayRef<mlir::NamedAttribute> attrs) {
3224   if (auto f = module.lookupSymbol<mlir::FuncOp>(name))
3225     return f;
3226   mlir::OpBuilder modBuilder(module.getBodyRegion());
3227   modBuilder.setInsertionPointToEnd(module.getBody());
3228   auto result = modBuilder.create<mlir::FuncOp>(loc, name, type, attrs);
3229   result.setVisibility(mlir::SymbolTable::Visibility::Private);
3230   return result;
3231 }
3232 
3233 fir::GlobalOp fir::createGlobalOp(mlir::Location loc, mlir::ModuleOp module,
3234                                   StringRef name, mlir::Type type,
3235                                   llvm::ArrayRef<mlir::NamedAttribute> attrs) {
3236   if (auto g = module.lookupSymbol<fir::GlobalOp>(name))
3237     return g;
3238   mlir::OpBuilder modBuilder(module.getBodyRegion());
3239   auto result = modBuilder.create<fir::GlobalOp>(loc, name, type, attrs);
3240   result.setVisibility(mlir::SymbolTable::Visibility::Private);
3241   return result;
3242 }
3243 
3244 bool fir::valueHasFirAttribute(mlir::Value value,
3245                                llvm::StringRef attributeName) {
3246   // If this is a fir.box that was loaded, the fir attributes will be on the
3247   // related fir.ref<fir.box> creation.
3248   if (value.getType().isa<fir::BoxType>())
3249     if (auto definingOp = value.getDefiningOp())
3250       if (auto loadOp = mlir::dyn_cast<fir::LoadOp>(definingOp))
3251         value = loadOp.memref();
3252   // If this is a function argument, look in the argument attributes.
3253   if (auto blockArg = value.dyn_cast<mlir::BlockArgument>()) {
3254     if (blockArg.getOwner() && blockArg.getOwner()->isEntryBlock())
3255       if (auto funcOp =
3256               mlir::dyn_cast<mlir::FuncOp>(blockArg.getOwner()->getParentOp()))
3257         if (funcOp.getArgAttr(blockArg.getArgNumber(), attributeName))
3258           return true;
3259     return false;
3260   }
3261 
3262   if (auto definingOp = value.getDefiningOp()) {
3263     // If this is an allocated value, look at the allocation attributes.
3264     if (mlir::isa<fir::AllocMemOp>(definingOp) ||
3265         mlir::isa<AllocaOp>(definingOp))
3266       return definingOp->hasAttr(attributeName);
3267     // If this is an imported global, look at AddrOfOp and GlobalOp attributes.
3268     // Both operations are looked at because use/host associated variable (the
3269     // AddrOfOp) can have ASYNCHRONOUS/VOLATILE attributes even if the ultimate
3270     // entity (the globalOp) does not have them.
3271     if (auto addressOfOp = mlir::dyn_cast<fir::AddrOfOp>(definingOp)) {
3272       if (addressOfOp->hasAttr(attributeName))
3273         return true;
3274       if (auto module = definingOp->getParentOfType<mlir::ModuleOp>())
3275         if (auto globalOp =
3276                 module.lookupSymbol<fir::GlobalOp>(addressOfOp.symbol()))
3277           return globalOp->hasAttr(attributeName);
3278     }
3279   }
3280   // TODO: Construct associated entities attributes. Decide where the fir
3281   // attributes must be placed/looked for in this case.
3282   return false;
3283 }
3284 
3285 mlir::Type fir::applyPathToType(mlir::Type eleTy, mlir::ValueRange path) {
3286   for (auto i = path.begin(), end = path.end(); eleTy && i < end;) {
3287     eleTy = llvm::TypeSwitch<mlir::Type, mlir::Type>(eleTy)
3288                 .Case<fir::RecordType>([&](fir::RecordType ty) {
3289                   if (auto *op = (*i++).getDefiningOp()) {
3290                     if (auto off = mlir::dyn_cast<fir::FieldIndexOp>(op))
3291                       return ty.getType(off.getFieldName());
3292                     if (auto off = mlir::dyn_cast<mlir::arith::ConstantOp>(op))
3293                       return ty.getType(fir::toInt(off));
3294                   }
3295                   return mlir::Type{};
3296                 })
3297                 .Case<fir::SequenceType>([&](fir::SequenceType ty) {
3298                   bool valid = true;
3299                   const auto rank = ty.getDimension();
3300                   for (std::remove_const_t<decltype(rank)> ii = 0;
3301                        valid && ii < rank; ++ii)
3302                     valid = i < end && fir::isa_integer((*i++).getType());
3303                   return valid ? ty.getEleTy() : mlir::Type{};
3304                 })
3305                 .Case<mlir::TupleType>([&](mlir::TupleType ty) {
3306                   if (auto *op = (*i++).getDefiningOp())
3307                     if (auto off = mlir::dyn_cast<mlir::arith::ConstantOp>(op))
3308                       return ty.getType(fir::toInt(off));
3309                   return mlir::Type{};
3310                 })
3311                 .Case<fir::ComplexType>([&](fir::ComplexType ty) {
3312                   if (fir::isa_integer((*i++).getType()))
3313                     return ty.getElementType();
3314                   return mlir::Type{};
3315                 })
3316                 .Case<mlir::ComplexType>([&](mlir::ComplexType ty) {
3317                   if (fir::isa_integer((*i++).getType()))
3318                     return ty.getElementType();
3319                   return mlir::Type{};
3320                 })
3321                 .Default([&](const auto &) { return mlir::Type{}; });
3322   }
3323   return eleTy;
3324 }
3325 
3326 // Tablegen operators
3327 
3328 #define GET_OP_CLASSES
3329 #include "flang/Optimizer/Dialect/FIROps.cpp.inc"
3330