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