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