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