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