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