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