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 "mlir/Dialect/CommonFolders.h"
18 #include "mlir/Dialect/StandardOps/IR/Ops.h"
19 #include "mlir/IR/BuiltinOps.h"
20 #include "mlir/IR/Diagnostics.h"
21 #include "mlir/IR/Matchers.h"
22 #include "mlir/IR/PatternMatch.h"
23 #include "llvm/ADT/StringSwitch.h"
24 #include "llvm/ADT/TypeSwitch.h"
25 
26 using namespace fir;
27 
28 /// Return true if a sequence type is of some incomplete size or a record type
29 /// is malformed or contains an incomplete sequence type. An incomplete sequence
30 /// type is one with more unknown extents in the type than have been provided
31 /// via `dynamicExtents`. Sequence types with an unknown rank are incomplete by
32 /// definition.
33 static bool verifyInType(mlir::Type inType,
34                          llvm::SmallVectorImpl<llvm::StringRef> &visited,
35                          unsigned dynamicExtents = 0) {
36   if (auto st = inType.dyn_cast<fir::SequenceType>()) {
37     auto shape = st.getShape();
38     if (shape.size() == 0)
39       return true;
40     for (std::size_t i = 0, end{shape.size()}; i < end; ++i) {
41       if (shape[i] != fir::SequenceType::getUnknownExtent())
42         continue;
43       if (dynamicExtents-- == 0)
44         return true;
45     }
46   } else if (auto rt = inType.dyn_cast<fir::RecordType>()) {
47     // don't recurse if we're already visiting this one
48     if (llvm::is_contained(visited, rt.getName()))
49       return false;
50     // keep track of record types currently being visited
51     visited.push_back(rt.getName());
52     for (auto &field : rt.getTypeList())
53       if (verifyInType(field.second, visited))
54         return true;
55     visited.pop_back();
56   } else if (auto rt = inType.dyn_cast<fir::PointerType>()) {
57     return verifyInType(rt.getEleTy(), visited);
58   }
59   return false;
60 }
61 
62 static bool verifyRecordLenParams(mlir::Type inType, unsigned numLenParams) {
63   if (numLenParams > 0) {
64     if (auto rt = inType.dyn_cast<fir::RecordType>())
65       return numLenParams != rt.getNumLenParams();
66     return true;
67   }
68   return false;
69 }
70 
71 //===----------------------------------------------------------------------===//
72 // AllocaOp
73 //===----------------------------------------------------------------------===//
74 
75 mlir::Type fir::AllocaOp::getAllocatedType() {
76   return getType().cast<ReferenceType>().getEleTy();
77 }
78 
79 /// Create a legal memory reference as return type
80 mlir::Type fir::AllocaOp::wrapResultType(mlir::Type intype) {
81   // FIR semantics: memory references to memory references are disallowed
82   if (intype.isa<ReferenceType>())
83     return {};
84   return ReferenceType::get(intype);
85 }
86 
87 mlir::Type fir::AllocaOp::getRefTy(mlir::Type ty) {
88   return ReferenceType::get(ty);
89 }
90 
91 //===----------------------------------------------------------------------===//
92 // AllocMemOp
93 //===----------------------------------------------------------------------===//
94 
95 mlir::Type fir::AllocMemOp::getAllocatedType() {
96   return getType().cast<HeapType>().getEleTy();
97 }
98 
99 mlir::Type fir::AllocMemOp::getRefTy(mlir::Type ty) {
100   return HeapType::get(ty);
101 }
102 
103 /// Create a legal heap reference as return type
104 mlir::Type fir::AllocMemOp::wrapResultType(mlir::Type intype) {
105   // Fortran semantics: C852 an entity cannot be both ALLOCATABLE and POINTER
106   // 8.5.3 note 1 prohibits ALLOCATABLE procedures as well
107   // FIR semantics: one may not allocate a memory reference value
108   if (intype.isa<ReferenceType>() || intype.isa<HeapType>() ||
109       intype.isa<PointerType>() || intype.isa<FunctionType>())
110     return {};
111   return HeapType::get(intype);
112 }
113 
114 //===----------------------------------------------------------------------===//
115 // ArrayCoorOp
116 //===----------------------------------------------------------------------===//
117 
118 static mlir::LogicalResult verify(fir::ArrayCoorOp op) {
119   auto eleTy = fir::dyn_cast_ptrOrBoxEleTy(op.memref().getType());
120   auto arrTy = eleTy.dyn_cast<fir::SequenceType>();
121   if (!arrTy)
122     return op.emitOpError("must be a reference to an array");
123   auto arrDim = arrTy.getDimension();
124 
125   if (auto shapeOp = op.shape()) {
126     auto shapeTy = shapeOp.getType();
127     unsigned shapeTyRank = 0;
128     if (auto s = shapeTy.dyn_cast<fir::ShapeType>()) {
129       shapeTyRank = s.getRank();
130     } else if (auto ss = shapeTy.dyn_cast<fir::ShapeShiftType>()) {
131       shapeTyRank = ss.getRank();
132     } else {
133       auto s = shapeTy.cast<fir::ShiftType>();
134       shapeTyRank = s.getRank();
135       if (!op.memref().getType().isa<fir::BoxType>())
136         return op.emitOpError("shift can only be provided with fir.box memref");
137     }
138     if (arrDim && arrDim != shapeTyRank)
139       return op.emitOpError("rank of dimension mismatched");
140     if (shapeTyRank != op.indices().size())
141       return op.emitOpError("number of indices do not match dim rank");
142   }
143 
144   if (auto sliceOp = op.slice())
145     if (auto sliceTy = sliceOp.getType().dyn_cast<fir::SliceType>())
146       if (sliceTy.getRank() != arrDim)
147         return op.emitOpError("rank of dimension in slice mismatched");
148 
149   return mlir::success();
150 }
151 
152 //===----------------------------------------------------------------------===//
153 // ArrayLoadOp
154 //===----------------------------------------------------------------------===//
155 
156 std::vector<mlir::Value> fir::ArrayLoadOp::getExtents() {
157   if (auto sh = shape())
158     if (auto *op = sh.getDefiningOp()) {
159       if (auto shOp = dyn_cast<fir::ShapeOp>(op))
160         return shOp.getExtents();
161       return cast<fir::ShapeShiftOp>(op).getExtents();
162     }
163   return {};
164 }
165 
166 static mlir::LogicalResult verify(fir::ArrayLoadOp op) {
167   auto eleTy = fir::dyn_cast_ptrOrBoxEleTy(op.memref().getType());
168   auto arrTy = eleTy.dyn_cast<fir::SequenceType>();
169   if (!arrTy)
170     return op.emitOpError("must be a reference to an array");
171   auto arrDim = arrTy.getDimension();
172 
173   if (auto shapeOp = op.shape()) {
174     auto shapeTy = shapeOp.getType();
175     unsigned shapeTyRank = 0;
176     if (auto s = shapeTy.dyn_cast<fir::ShapeType>()) {
177       shapeTyRank = s.getRank();
178     } else if (auto ss = shapeTy.dyn_cast<fir::ShapeShiftType>()) {
179       shapeTyRank = ss.getRank();
180     } else {
181       auto s = shapeTy.cast<fir::ShiftType>();
182       shapeTyRank = s.getRank();
183       if (!op.memref().getType().isa<fir::BoxType>())
184         return op.emitOpError("shift can only be provided with fir.box memref");
185     }
186     if (arrDim && arrDim != shapeTyRank)
187       return op.emitOpError("rank of dimension mismatched");
188   }
189 
190   if (auto sliceOp = op.slice())
191     if (auto sliceTy = sliceOp.getType().dyn_cast<fir::SliceType>())
192       if (sliceTy.getRank() != arrDim)
193         return op.emitOpError("rank of dimension in slice mismatched");
194 
195   return mlir::success();
196 }
197 
198 //===----------------------------------------------------------------------===//
199 // BoxAddrOp
200 //===----------------------------------------------------------------------===//
201 
202 mlir::OpFoldResult fir::BoxAddrOp::fold(llvm::ArrayRef<mlir::Attribute> opnds) {
203   if (auto v = val().getDefiningOp()) {
204     if (auto box = dyn_cast<fir::EmboxOp>(v))
205       return box.memref();
206     if (auto box = dyn_cast<fir::EmboxCharOp>(v))
207       return box.memref();
208   }
209   return {};
210 }
211 
212 //===----------------------------------------------------------------------===//
213 // BoxCharLenOp
214 //===----------------------------------------------------------------------===//
215 
216 mlir::OpFoldResult
217 fir::BoxCharLenOp::fold(llvm::ArrayRef<mlir::Attribute> opnds) {
218   if (auto v = val().getDefiningOp()) {
219     if (auto box = dyn_cast<fir::EmboxCharOp>(v))
220       return box.len();
221   }
222   return {};
223 }
224 
225 //===----------------------------------------------------------------------===//
226 // BoxDimsOp
227 //===----------------------------------------------------------------------===//
228 
229 /// Get the result types packed in a tuple tuple
230 mlir::Type fir::BoxDimsOp::getTupleType() {
231   // note: triple, but 4 is nearest power of 2
232   llvm::SmallVector<mlir::Type, 4> triple{
233       getResult(0).getType(), getResult(1).getType(), getResult(2).getType()};
234   return mlir::TupleType::get(getContext(), triple);
235 }
236 
237 //===----------------------------------------------------------------------===//
238 // CallOp
239 //===----------------------------------------------------------------------===//
240 
241 mlir::FunctionType fir::CallOp::getFunctionType() {
242   return mlir::FunctionType::get(getContext(), getOperandTypes(),
243                                  getResultTypes());
244 }
245 
246 static void printCallOp(mlir::OpAsmPrinter &p, fir::CallOp &op) {
247   auto callee = op.callee();
248   bool isDirect = callee.hasValue();
249   p << ' ';
250   if (isDirect)
251     p << callee.getValue();
252   else
253     p << op.getOperand(0);
254   p << '(' << op->getOperands().drop_front(isDirect ? 0 : 1) << ')';
255   p.printOptionalAttrDict(op->getAttrs(), {"callee"});
256   auto resultTypes{op.getResultTypes()};
257   llvm::SmallVector<Type, 8> argTypes(
258       llvm::drop_begin(op.getOperandTypes(), isDirect ? 0 : 1));
259   p << " : " << FunctionType::get(op.getContext(), argTypes, resultTypes);
260 }
261 
262 static mlir::ParseResult parseCallOp(mlir::OpAsmParser &parser,
263                                      mlir::OperationState &result) {
264   llvm::SmallVector<mlir::OpAsmParser::OperandType, 8> operands;
265   if (parser.parseOperandList(operands))
266     return mlir::failure();
267 
268   mlir::NamedAttrList attrs;
269   mlir::SymbolRefAttr funcAttr;
270   bool isDirect = operands.empty();
271   if (isDirect)
272     if (parser.parseAttribute(funcAttr, "callee", attrs))
273       return mlir::failure();
274 
275   Type type;
276   if (parser.parseOperandList(operands, mlir::OpAsmParser::Delimiter::Paren) ||
277       parser.parseOptionalAttrDict(attrs) || parser.parseColon() ||
278       parser.parseType(type))
279     return mlir::failure();
280 
281   auto funcType = type.dyn_cast<mlir::FunctionType>();
282   if (!funcType)
283     return parser.emitError(parser.getNameLoc(), "expected function type");
284   if (isDirect) {
285     if (parser.resolveOperands(operands, funcType.getInputs(),
286                                parser.getNameLoc(), result.operands))
287       return mlir::failure();
288   } else {
289     auto funcArgs =
290         llvm::ArrayRef<mlir::OpAsmParser::OperandType>(operands).drop_front();
291     if (parser.resolveOperand(operands[0], funcType, result.operands) ||
292         parser.resolveOperands(funcArgs, funcType.getInputs(),
293                                parser.getNameLoc(), result.operands))
294       return mlir::failure();
295   }
296   result.addTypes(funcType.getResults());
297   result.attributes = attrs;
298   return mlir::success();
299 }
300 
301 //===----------------------------------------------------------------------===//
302 // CmpOp
303 //===----------------------------------------------------------------------===//
304 
305 template <typename OPTY>
306 static void printCmpOp(OpAsmPrinter &p, OPTY op) {
307   p << ' ';
308   auto predSym = mlir::symbolizeCmpFPredicate(
309       op->template getAttrOfType<mlir::IntegerAttr>(
310             OPTY::getPredicateAttrName())
311           .getInt());
312   assert(predSym.hasValue() && "invalid symbol value for predicate");
313   p << '"' << mlir::stringifyCmpFPredicate(predSym.getValue()) << '"' << ", ";
314   p.printOperand(op.lhs());
315   p << ", ";
316   p.printOperand(op.rhs());
317   p.printOptionalAttrDict(op->getAttrs(),
318                           /*elidedAttrs=*/{OPTY::getPredicateAttrName()});
319   p << " : " << op.lhs().getType();
320 }
321 
322 template <typename OPTY>
323 static mlir::ParseResult parseCmpOp(mlir::OpAsmParser &parser,
324                                     mlir::OperationState &result) {
325   llvm::SmallVector<mlir::OpAsmParser::OperandType, 2> ops;
326   mlir::NamedAttrList attrs;
327   mlir::Attribute predicateNameAttr;
328   mlir::Type type;
329   if (parser.parseAttribute(predicateNameAttr, OPTY::getPredicateAttrName(),
330                             attrs) ||
331       parser.parseComma() || parser.parseOperandList(ops, 2) ||
332       parser.parseOptionalAttrDict(attrs) || parser.parseColonType(type) ||
333       parser.resolveOperands(ops, type, result.operands))
334     return failure();
335 
336   if (!predicateNameAttr.isa<mlir::StringAttr>())
337     return parser.emitError(parser.getNameLoc(),
338                             "expected string comparison predicate attribute");
339 
340   // Rewrite string attribute to an enum value.
341   llvm::StringRef predicateName =
342       predicateNameAttr.cast<mlir::StringAttr>().getValue();
343   auto predicate = fir::CmpcOp::getPredicateByName(predicateName);
344   auto builder = parser.getBuilder();
345   mlir::Type i1Type = builder.getI1Type();
346   attrs.set(OPTY::getPredicateAttrName(),
347             builder.getI64IntegerAttr(static_cast<int64_t>(predicate)));
348   result.attributes = attrs;
349   result.addTypes({i1Type});
350   return success();
351 }
352 
353 //===----------------------------------------------------------------------===//
354 // CmpcOp
355 //===----------------------------------------------------------------------===//
356 
357 void fir::buildCmpCOp(OpBuilder &builder, OperationState &result,
358                       CmpFPredicate predicate, Value lhs, Value rhs) {
359   result.addOperands({lhs, rhs});
360   result.types.push_back(builder.getI1Type());
361   result.addAttribute(
362       fir::CmpcOp::getPredicateAttrName(),
363       builder.getI64IntegerAttr(static_cast<int64_t>(predicate)));
364 }
365 
366 mlir::CmpFPredicate fir::CmpcOp::getPredicateByName(llvm::StringRef name) {
367   auto pred = mlir::symbolizeCmpFPredicate(name);
368   assert(pred.hasValue() && "invalid predicate name");
369   return pred.getValue();
370 }
371 
372 static void printCmpcOp(OpAsmPrinter &p, fir::CmpcOp op) { printCmpOp(p, op); }
373 
374 mlir::ParseResult fir::parseCmpcOp(mlir::OpAsmParser &parser,
375                                    mlir::OperationState &result) {
376   return parseCmpOp<fir::CmpcOp>(parser, result);
377 }
378 
379 //===----------------------------------------------------------------------===//
380 // ConvertOp
381 //===----------------------------------------------------------------------===//
382 
383 void fir::ConvertOp::getCanonicalizationPatterns(
384     OwningRewritePatternList &results, MLIRContext *context) {}
385 
386 mlir::OpFoldResult fir::ConvertOp::fold(llvm::ArrayRef<mlir::Attribute> opnds) {
387   if (value().getType() == getType())
388     return value();
389   if (matchPattern(value(), m_Op<fir::ConvertOp>())) {
390     auto inner = cast<fir::ConvertOp>(value().getDefiningOp());
391     // (convert (convert 'a : logical -> i1) : i1 -> logical) ==> forward 'a
392     if (auto toTy = getType().dyn_cast<fir::LogicalType>())
393       if (auto fromTy = inner.value().getType().dyn_cast<fir::LogicalType>())
394         if (inner.getType().isa<mlir::IntegerType>() && (toTy == fromTy))
395           return inner.value();
396     // (convert (convert 'a : i1 -> logical) : logical -> i1) ==> forward 'a
397     if (auto toTy = getType().dyn_cast<mlir::IntegerType>())
398       if (auto fromTy = inner.value().getType().dyn_cast<mlir::IntegerType>())
399         if (inner.getType().isa<fir::LogicalType>() && (toTy == fromTy) &&
400             (fromTy.getWidth() == 1))
401           return inner.value();
402   }
403   return {};
404 }
405 
406 bool fir::ConvertOp::isIntegerCompatible(mlir::Type ty) {
407   return ty.isa<mlir::IntegerType>() || ty.isa<mlir::IndexType>() ||
408          ty.isa<fir::IntegerType>() || ty.isa<fir::LogicalType>();
409 }
410 
411 bool fir::ConvertOp::isFloatCompatible(mlir::Type ty) {
412   return ty.isa<mlir::FloatType>() || ty.isa<fir::RealType>();
413 }
414 
415 bool fir::ConvertOp::isPointerCompatible(mlir::Type ty) {
416   return ty.isa<fir::ReferenceType>() || ty.isa<fir::PointerType>() ||
417          ty.isa<fir::HeapType>() || ty.isa<mlir::MemRefType>() ||
418          ty.isa<mlir::FunctionType>() || ty.isa<fir::TypeDescType>();
419 }
420 
421 //===----------------------------------------------------------------------===//
422 // CoordinateOp
423 //===----------------------------------------------------------------------===//
424 
425 static void print(mlir::OpAsmPrinter &p, fir::CoordinateOp op) {
426   p << ' ' << op.ref() << ", " << op.coor();
427   p.printOptionalAttrDict(op->getAttrs(), /*elideAttrs=*/{"baseType"});
428   p << " : ";
429   p.printFunctionalType(op.getOperandTypes(), op->getResultTypes());
430 }
431 
432 static mlir::ParseResult parseCoordinateCustom(mlir::OpAsmParser &parser,
433                                                mlir::OperationState &result) {
434   mlir::OpAsmParser::OperandType memref;
435   if (parser.parseOperand(memref) || parser.parseComma())
436     return mlir::failure();
437   llvm::SmallVector<mlir::OpAsmParser::OperandType, 8> coorOperands;
438   if (parser.parseOperandList(coorOperands))
439     return mlir::failure();
440   llvm::SmallVector<mlir::OpAsmParser::OperandType, 16> allOperands;
441   allOperands.push_back(memref);
442   allOperands.append(coorOperands.begin(), coorOperands.end());
443   mlir::FunctionType funcTy;
444   auto loc = parser.getCurrentLocation();
445   if (parser.parseOptionalAttrDict(result.attributes) ||
446       parser.parseColonType(funcTy) ||
447       parser.resolveOperands(allOperands, funcTy.getInputs(), loc,
448                              result.operands))
449     return failure();
450   parser.addTypesToList(funcTy.getResults(), result.types);
451   result.addAttribute("baseType", mlir::TypeAttr::get(funcTy.getInput(0)));
452   return mlir::success();
453 }
454 
455 static mlir::LogicalResult verify(fir::CoordinateOp op) {
456   auto refTy = op.ref().getType();
457   if (fir::isa_ref_type(refTy)) {
458     auto eleTy = fir::dyn_cast_ptrEleTy(refTy);
459     if (auto arrTy = eleTy.dyn_cast<fir::SequenceType>()) {
460       if (arrTy.hasUnknownShape())
461         return op.emitOpError("cannot find coordinate in unknown shape");
462       if (arrTy.getConstantRows() < arrTy.getDimension() - 1)
463         return op.emitOpError("cannot find coordinate with unknown extents");
464     }
465     if (!(fir::isa_aggregate(eleTy) || fir::isa_complex(eleTy) ||
466           fir::isa_char_string(eleTy)))
467       return op.emitOpError("cannot apply coordinate_of to this type");
468   }
469   // Recovering a LEN type parameter only makes sense from a boxed value. For a
470   // bare reference, the LEN type parameters must be passed as additional
471   // arguments to `op`.
472   for (auto co : op.coor())
473     if (dyn_cast_or_null<fir::LenParamIndexOp>(co.getDefiningOp())) {
474       if (op.getNumOperands() != 2)
475         return op.emitOpError("len_param_index must be last argument");
476       if (!op.ref().getType().isa<BoxType>())
477         return op.emitOpError("len_param_index must be used on box type");
478     }
479   return mlir::success();
480 }
481 
482 //===----------------------------------------------------------------------===//
483 // DispatchOp
484 //===----------------------------------------------------------------------===//
485 
486 mlir::FunctionType fir::DispatchOp::getFunctionType() {
487   return mlir::FunctionType::get(getContext(), getOperandTypes(),
488                                  getResultTypes());
489 }
490 
491 //===----------------------------------------------------------------------===//
492 // DispatchTableOp
493 //===----------------------------------------------------------------------===//
494 
495 void fir::DispatchTableOp::appendTableEntry(mlir::Operation *op) {
496   assert(mlir::isa<fir::DTEntryOp>(*op) && "operation must be a DTEntryOp");
497   auto &block = getBlock();
498   block.getOperations().insert(block.end(), op);
499 }
500 
501 //===----------------------------------------------------------------------===//
502 // EmboxOp
503 //===----------------------------------------------------------------------===//
504 
505 static mlir::LogicalResult verify(fir::EmboxOp op) {
506   auto eleTy = fir::dyn_cast_ptrEleTy(op.memref().getType());
507   bool isArray = false;
508   if (auto seqTy = eleTy.dyn_cast<fir::SequenceType>()) {
509     eleTy = seqTy.getEleTy();
510     isArray = true;
511   }
512   if (op.hasLenParams()) {
513     auto lenPs = op.numLenParams();
514     if (auto rt = eleTy.dyn_cast<fir::RecordType>()) {
515       if (lenPs != rt.getNumLenParams())
516         return op.emitOpError("number of LEN params does not correspond"
517                               " to the !fir.type type");
518     } else if (auto strTy = eleTy.dyn_cast<fir::CharacterType>()) {
519       if (strTy.getLen() != fir::CharacterType::unknownLen())
520         return op.emitOpError("CHARACTER already has static LEN");
521     } else {
522       return op.emitOpError("LEN parameters require CHARACTER or derived type");
523     }
524     for (auto lp : op.typeparams())
525       if (!fir::isa_integer(lp.getType()))
526         return op.emitOpError("LEN parameters must be integral type");
527   }
528   if (op.getShape() && !isArray)
529     return op.emitOpError("shape must not be provided for a scalar");
530   if (op.getSlice() && !isArray)
531     return op.emitOpError("slice must not be provided for a scalar");
532   return mlir::success();
533 }
534 
535 //===----------------------------------------------------------------------===//
536 // GenTypeDescOp
537 //===----------------------------------------------------------------------===//
538 
539 void fir::GenTypeDescOp::build(OpBuilder &, OperationState &result,
540                                mlir::TypeAttr inty) {
541   result.addAttribute("in_type", inty);
542   result.addTypes(TypeDescType::get(inty.getValue()));
543 }
544 
545 //===----------------------------------------------------------------------===//
546 // GlobalOp
547 //===----------------------------------------------------------------------===//
548 
549 static ParseResult parseGlobalOp(OpAsmParser &parser, OperationState &result) {
550   // Parse the optional linkage
551   llvm::StringRef linkage;
552   auto &builder = parser.getBuilder();
553   if (mlir::succeeded(parser.parseOptionalKeyword(&linkage))) {
554     if (fir::GlobalOp::verifyValidLinkage(linkage))
555       return mlir::failure();
556     mlir::StringAttr linkAttr = builder.getStringAttr(linkage);
557     result.addAttribute(fir::GlobalOp::linkageAttrName(), linkAttr);
558   }
559 
560   // Parse the name as a symbol reference attribute.
561   mlir::SymbolRefAttr nameAttr;
562   if (parser.parseAttribute(nameAttr, fir::GlobalOp::symbolAttrName(),
563                             result.attributes))
564     return mlir::failure();
565   result.addAttribute(mlir::SymbolTable::getSymbolAttrName(),
566                       nameAttr.getRootReference());
567 
568   bool simpleInitializer = false;
569   if (mlir::succeeded(parser.parseOptionalLParen())) {
570     Attribute attr;
571     if (parser.parseAttribute(attr, "initVal", result.attributes) ||
572         parser.parseRParen())
573       return mlir::failure();
574     simpleInitializer = true;
575   }
576 
577   if (succeeded(parser.parseOptionalKeyword("constant"))) {
578     // if "constant" keyword then mark this as a constant, not a variable
579     result.addAttribute("constant", builder.getUnitAttr());
580   }
581 
582   mlir::Type globalType;
583   if (parser.parseColonType(globalType))
584     return mlir::failure();
585 
586   result.addAttribute(fir::GlobalOp::typeAttrName(result.name),
587                       mlir::TypeAttr::get(globalType));
588 
589   if (simpleInitializer) {
590     result.addRegion();
591   } else {
592     // Parse the optional initializer body.
593     auto parseResult = parser.parseOptionalRegion(
594         *result.addRegion(), /*arguments=*/llvm::None, /*argTypes=*/llvm::None);
595     if (parseResult.hasValue() && mlir::failed(*parseResult))
596       return mlir::failure();
597   }
598 
599   return mlir::success();
600 }
601 
602 void fir::GlobalOp::appendInitialValue(mlir::Operation *op) {
603   getBlock().getOperations().push_back(op);
604 }
605 
606 void fir::GlobalOp::build(mlir::OpBuilder &builder, OperationState &result,
607                           StringRef name, bool isConstant, Type type,
608                           Attribute initialVal, StringAttr linkage,
609                           ArrayRef<NamedAttribute> attrs) {
610   result.addRegion();
611   result.addAttribute(typeAttrName(result.name), mlir::TypeAttr::get(type));
612   result.addAttribute(mlir::SymbolTable::getSymbolAttrName(),
613                       builder.getStringAttr(name));
614   result.addAttribute(symbolAttrName(),
615                       SymbolRefAttr::get(builder.getContext(), name));
616   if (isConstant)
617     result.addAttribute(constantAttrName(result.name), builder.getUnitAttr());
618   if (initialVal)
619     result.addAttribute(initValAttrName(result.name), initialVal);
620   if (linkage)
621     result.addAttribute(linkageAttrName(), linkage);
622   result.attributes.append(attrs.begin(), attrs.end());
623 }
624 
625 void fir::GlobalOp::build(mlir::OpBuilder &builder, OperationState &result,
626                           StringRef name, Type type, Attribute initialVal,
627                           StringAttr linkage, ArrayRef<NamedAttribute> attrs) {
628   build(builder, result, name, /*isConstant=*/false, type, {}, linkage, attrs);
629 }
630 
631 void fir::GlobalOp::build(mlir::OpBuilder &builder, OperationState &result,
632                           StringRef name, bool isConstant, Type type,
633                           StringAttr linkage, ArrayRef<NamedAttribute> attrs) {
634   build(builder, result, name, isConstant, type, {}, linkage, attrs);
635 }
636 
637 void fir::GlobalOp::build(mlir::OpBuilder &builder, OperationState &result,
638                           StringRef name, Type type, StringAttr linkage,
639                           ArrayRef<NamedAttribute> attrs) {
640   build(builder, result, name, /*isConstant=*/false, type, {}, linkage, attrs);
641 }
642 
643 void fir::GlobalOp::build(mlir::OpBuilder &builder, OperationState &result,
644                           StringRef name, bool isConstant, Type type,
645                           ArrayRef<NamedAttribute> attrs) {
646   build(builder, result, name, isConstant, type, StringAttr{}, attrs);
647 }
648 
649 void fir::GlobalOp::build(mlir::OpBuilder &builder, OperationState &result,
650                           StringRef name, Type type,
651                           ArrayRef<NamedAttribute> attrs) {
652   build(builder, result, name, /*isConstant=*/false, type, attrs);
653 }
654 
655 mlir::ParseResult fir::GlobalOp::verifyValidLinkage(StringRef linkage) {
656   // Supporting only a subset of the LLVM linkage types for now
657   static const char *validNames[] = {"common", "internal", "linkonce", "weak"};
658   return mlir::success(llvm::is_contained(validNames, linkage));
659 }
660 
661 template <bool AllowFields>
662 static void appendAsAttribute(llvm::SmallVectorImpl<mlir::Attribute> &attrs,
663                               mlir::Value val) {
664   if (auto *op = val.getDefiningOp()) {
665     if (auto cop = mlir::dyn_cast<mlir::ConstantOp>(op)) {
666       // append the integer constant value
667       if (auto iattr = cop.getValue().dyn_cast<mlir::IntegerAttr>()) {
668         attrs.push_back(iattr);
669         return;
670       }
671     } else if (auto fld = mlir::dyn_cast<fir::FieldIndexOp>(op)) {
672       if constexpr (AllowFields) {
673         // append the field name and the record type
674         attrs.push_back(fld.field_idAttr());
675         attrs.push_back(fld.on_typeAttr());
676         return;
677       }
678     }
679   }
680   llvm::report_fatal_error("cannot build Op with these arguments");
681 }
682 
683 template <bool AllowFields = true>
684 static mlir::ArrayAttr collectAsAttributes(mlir::MLIRContext *ctxt,
685                                            OperationState &result,
686                                            llvm::ArrayRef<mlir::Value> inds) {
687   llvm::SmallVector<mlir::Attribute> attrs;
688   for (auto v : inds)
689     appendAsAttribute<AllowFields>(attrs, v);
690   assert(!attrs.empty());
691   return mlir::ArrayAttr::get(ctxt, attrs);
692 }
693 
694 //===----------------------------------------------------------------------===//
695 // InsertOnRangeOp
696 //===----------------------------------------------------------------------===//
697 
698 void fir::InsertOnRangeOp::build(mlir::OpBuilder &builder,
699                                  OperationState &result, mlir::Type resTy,
700                                  mlir::Value aggVal, mlir::Value eleVal,
701                                  llvm::ArrayRef<mlir::Value> inds) {
702   auto aa = collectAsAttributes<false>(builder.getContext(), result, inds);
703   build(builder, result, resTy, aggVal, eleVal, aa);
704 }
705 
706 /// Range bounds must be nonnegative, and the range must not be empty.
707 static mlir::LogicalResult verify(fir::InsertOnRangeOp op) {
708   if (op.coor().size() < 2 || op.coor().size() % 2 != 0)
709     return op.emitOpError("has uneven number of values in ranges");
710   bool rangeIsKnownToBeNonempty = false;
711   for (auto i = op.coor().end(), b = op.coor().begin(); i != b;) {
712     int64_t ub = (*--i).cast<IntegerAttr>().getInt();
713     int64_t lb = (*--i).cast<IntegerAttr>().getInt();
714     if (lb < 0 || ub < 0)
715       return op.emitOpError("negative range bound");
716     if (rangeIsKnownToBeNonempty)
717       continue;
718     if (lb > ub)
719       return op.emitOpError("empty range");
720     rangeIsKnownToBeNonempty = lb < ub;
721   }
722   return mlir::success();
723 }
724 
725 //===----------------------------------------------------------------------===//
726 // InsertValueOp
727 //===----------------------------------------------------------------------===//
728 
729 static bool checkIsIntegerConstant(mlir::Value v, int64_t conVal) {
730   if (auto c = dyn_cast_or_null<mlir::ConstantOp>(v.getDefiningOp())) {
731     auto attr = c.getValue();
732     if (auto iattr = attr.dyn_cast<mlir::IntegerAttr>())
733       return iattr.getInt() == conVal;
734   }
735   return false;
736 }
737 static bool isZero(mlir::Value v) { return checkIsIntegerConstant(v, 0); }
738 static bool isOne(mlir::Value v) { return checkIsIntegerConstant(v, 1); }
739 
740 // Undo some complex patterns created in the front-end and turn them back into
741 // complex ops.
742 template <typename FltOp, typename CpxOp>
743 struct UndoComplexPattern : public mlir::RewritePattern {
744   UndoComplexPattern(mlir::MLIRContext *ctx)
745       : mlir::RewritePattern("fir.insert_value", 2, ctx) {}
746 
747   mlir::LogicalResult
748   matchAndRewrite(mlir::Operation *op,
749                   mlir::PatternRewriter &rewriter) const override {
750     auto insval = dyn_cast_or_null<fir::InsertValueOp>(op);
751     if (!insval || !insval.getType().isa<fir::ComplexType>())
752       return mlir::failure();
753     auto insval2 =
754         dyn_cast_or_null<fir::InsertValueOp>(insval.adt().getDefiningOp());
755     if (!insval2 || !isa<fir::UndefOp>(insval2.adt().getDefiningOp()))
756       return mlir::failure();
757     auto binf = dyn_cast_or_null<FltOp>(insval.val().getDefiningOp());
758     auto binf2 = dyn_cast_or_null<FltOp>(insval2.val().getDefiningOp());
759     if (!binf || !binf2 || insval.coor().size() != 1 ||
760         !isOne(insval.coor()[0]) || insval2.coor().size() != 1 ||
761         !isZero(insval2.coor()[0]))
762       return mlir::failure();
763     auto eai =
764         dyn_cast_or_null<fir::ExtractValueOp>(binf.lhs().getDefiningOp());
765     auto ebi =
766         dyn_cast_or_null<fir::ExtractValueOp>(binf.rhs().getDefiningOp());
767     auto ear =
768         dyn_cast_or_null<fir::ExtractValueOp>(binf2.lhs().getDefiningOp());
769     auto ebr =
770         dyn_cast_or_null<fir::ExtractValueOp>(binf2.rhs().getDefiningOp());
771     if (!eai || !ebi || !ear || !ebr || ear.adt() != eai.adt() ||
772         ebr.adt() != ebi.adt() || eai.coor().size() != 1 ||
773         !isOne(eai.coor()[0]) || ebi.coor().size() != 1 ||
774         !isOne(ebi.coor()[0]) || ear.coor().size() != 1 ||
775         !isZero(ear.coor()[0]) || ebr.coor().size() != 1 ||
776         !isZero(ebr.coor()[0]))
777       return mlir::failure();
778     rewriter.replaceOpWithNewOp<CpxOp>(op, ear.adt(), ebr.adt());
779     return mlir::success();
780   }
781 };
782 
783 void fir::InsertValueOp::getCanonicalizationPatterns(
784     mlir::OwningRewritePatternList &results, mlir::MLIRContext *context) {
785   results.insert<UndoComplexPattern<mlir::AddFOp, fir::AddcOp>,
786                  UndoComplexPattern<mlir::SubFOp, fir::SubcOp>>(context);
787 }
788 
789 //===----------------------------------------------------------------------===//
790 // IterWhileOp
791 //===----------------------------------------------------------------------===//
792 
793 void fir::IterWhileOp::build(mlir::OpBuilder &builder,
794                              mlir::OperationState &result, mlir::Value lb,
795                              mlir::Value ub, mlir::Value step,
796                              mlir::Value iterate, bool finalCountValue,
797                              mlir::ValueRange iterArgs,
798                              llvm::ArrayRef<mlir::NamedAttribute> attributes) {
799   result.addOperands({lb, ub, step, iterate});
800   if (finalCountValue) {
801     result.addTypes(builder.getIndexType());
802     result.addAttribute(finalValueAttrName(result.name), builder.getUnitAttr());
803   }
804   result.addTypes(iterate.getType());
805   result.addOperands(iterArgs);
806   for (auto v : iterArgs)
807     result.addTypes(v.getType());
808   mlir::Region *bodyRegion = result.addRegion();
809   bodyRegion->push_back(new Block{});
810   bodyRegion->front().addArgument(builder.getIndexType());
811   bodyRegion->front().addArgument(iterate.getType());
812   bodyRegion->front().addArguments(iterArgs.getTypes());
813   result.addAttributes(attributes);
814 }
815 
816 static mlir::ParseResult parseIterWhileOp(mlir::OpAsmParser &parser,
817                                           mlir::OperationState &result) {
818   auto &builder = parser.getBuilder();
819   mlir::OpAsmParser::OperandType inductionVariable, lb, ub, step;
820   if (parser.parseLParen() || parser.parseRegionArgument(inductionVariable) ||
821       parser.parseEqual())
822     return mlir::failure();
823 
824   // Parse loop bounds.
825   auto indexType = builder.getIndexType();
826   auto i1Type = builder.getIntegerType(1);
827   if (parser.parseOperand(lb) ||
828       parser.resolveOperand(lb, indexType, result.operands) ||
829       parser.parseKeyword("to") || parser.parseOperand(ub) ||
830       parser.resolveOperand(ub, indexType, result.operands) ||
831       parser.parseKeyword("step") || parser.parseOperand(step) ||
832       parser.parseRParen() ||
833       parser.resolveOperand(step, indexType, result.operands))
834     return mlir::failure();
835 
836   mlir::OpAsmParser::OperandType iterateVar, iterateInput;
837   if (parser.parseKeyword("and") || parser.parseLParen() ||
838       parser.parseRegionArgument(iterateVar) || parser.parseEqual() ||
839       parser.parseOperand(iterateInput) || parser.parseRParen() ||
840       parser.resolveOperand(iterateInput, i1Type, result.operands))
841     return mlir::failure();
842 
843   // Parse the initial iteration arguments.
844   llvm::SmallVector<mlir::OpAsmParser::OperandType, 4> regionArgs;
845   auto prependCount = false;
846 
847   // Induction variable.
848   regionArgs.push_back(inductionVariable);
849   regionArgs.push_back(iterateVar);
850 
851   if (succeeded(parser.parseOptionalKeyword("iter_args"))) {
852     llvm::SmallVector<mlir::OpAsmParser::OperandType, 4> operands;
853     llvm::SmallVector<mlir::Type, 4> regionTypes;
854     // Parse assignment list and results type list.
855     if (parser.parseAssignmentList(regionArgs, operands) ||
856         parser.parseArrowTypeList(regionTypes))
857       return failure();
858     if (regionTypes.size() == operands.size() + 2)
859       prependCount = true;
860     llvm::ArrayRef<mlir::Type> resTypes = regionTypes;
861     resTypes = prependCount ? resTypes.drop_front(2) : resTypes;
862     // Resolve input operands.
863     for (auto operand_type : llvm::zip(operands, resTypes))
864       if (parser.resolveOperand(std::get<0>(operand_type),
865                                 std::get<1>(operand_type), result.operands))
866         return failure();
867     if (prependCount) {
868       result.addTypes(regionTypes);
869     } else {
870       result.addTypes(i1Type);
871       result.addTypes(resTypes);
872     }
873   } else if (succeeded(parser.parseOptionalArrow())) {
874     llvm::SmallVector<mlir::Type, 4> typeList;
875     if (parser.parseLParen() || parser.parseTypeList(typeList) ||
876         parser.parseRParen())
877       return failure();
878     // Type list must be "(index, i1)".
879     if (typeList.size() != 2 || !typeList[0].isa<mlir::IndexType>() ||
880         !typeList[1].isSignlessInteger(1))
881       return failure();
882     result.addTypes(typeList);
883     prependCount = true;
884   } else {
885     result.addTypes(i1Type);
886   }
887 
888   if (parser.parseOptionalAttrDictWithKeyword(result.attributes))
889     return mlir::failure();
890 
891   llvm::SmallVector<mlir::Type, 4> argTypes;
892   // Induction variable (hidden)
893   if (prependCount)
894     result.addAttribute(IterWhileOp::finalValueAttrName(result.name),
895                         builder.getUnitAttr());
896   else
897     argTypes.push_back(indexType);
898   // Loop carried variables (including iterate)
899   argTypes.append(result.types.begin(), result.types.end());
900   // Parse the body region.
901   auto *body = result.addRegion();
902   if (regionArgs.size() != argTypes.size())
903     return parser.emitError(
904         parser.getNameLoc(),
905         "mismatch in number of loop-carried values and defined values");
906 
907   if (parser.parseRegion(*body, regionArgs, argTypes))
908     return failure();
909 
910   fir::IterWhileOp::ensureTerminator(*body, builder, result.location);
911 
912   return mlir::success();
913 }
914 
915 static mlir::LogicalResult verify(fir::IterWhileOp op) {
916   // Check that the body defines as single block argument for the induction
917   // variable.
918   auto *body = op.getBody();
919   if (!body->getArgument(1).getType().isInteger(1))
920     return op.emitOpError(
921         "expected body second argument to be an index argument for "
922         "the induction variable");
923   if (!body->getArgument(0).getType().isIndex())
924     return op.emitOpError(
925         "expected body first argument to be an index argument for "
926         "the induction variable");
927 
928   auto opNumResults = op.getNumResults();
929   if (op.finalValue()) {
930     // Result type must be "(index, i1, ...)".
931     if (!op.getResult(0).getType().isa<mlir::IndexType>())
932       return op.emitOpError("result #0 expected to be index");
933     if (!op.getResult(1).getType().isSignlessInteger(1))
934       return op.emitOpError("result #1 expected to be i1");
935     opNumResults--;
936   } else {
937     // iterate_while always returns the early exit induction value.
938     // Result type must be "(i1, ...)"
939     if (!op.getResult(0).getType().isSignlessInteger(1))
940       return op.emitOpError("result #0 expected to be i1");
941   }
942   if (opNumResults == 0)
943     return mlir::failure();
944   if (op.getNumIterOperands() != opNumResults)
945     return op.emitOpError(
946         "mismatch in number of loop-carried values and defined values");
947   if (op.getNumRegionIterArgs() != opNumResults)
948     return op.emitOpError(
949         "mismatch in number of basic block args and defined values");
950   auto iterOperands = op.getIterOperands();
951   auto iterArgs = op.getRegionIterArgs();
952   auto opResults =
953       op.finalValue() ? op.getResults().drop_front() : op.getResults();
954   unsigned i = 0;
955   for (auto e : llvm::zip(iterOperands, iterArgs, opResults)) {
956     if (std::get<0>(e).getType() != std::get<2>(e).getType())
957       return op.emitOpError() << "types mismatch between " << i
958                               << "th iter operand and defined value";
959     if (std::get<1>(e).getType() != std::get<2>(e).getType())
960       return op.emitOpError() << "types mismatch between " << i
961                               << "th iter region arg and defined value";
962 
963     i++;
964   }
965   return mlir::success();
966 }
967 
968 static void print(mlir::OpAsmPrinter &p, fir::IterWhileOp op) {
969   p << " (" << op.getInductionVar() << " = " << op.lowerBound() << " to "
970     << op.upperBound() << " step " << op.step() << ") and (";
971   assert(op.hasIterOperands());
972   auto regionArgs = op.getRegionIterArgs();
973   auto operands = op.getIterOperands();
974   p << regionArgs.front() << " = " << *operands.begin() << ")";
975   if (regionArgs.size() > 1) {
976     p << " iter_args(";
977     llvm::interleaveComma(
978         llvm::zip(regionArgs.drop_front(), operands.drop_front()), p,
979         [&](auto it) { p << std::get<0>(it) << " = " << std::get<1>(it); });
980     p << ") -> (";
981     llvm::interleaveComma(
982         llvm::drop_begin(op.getResultTypes(), op.finalValue() ? 0 : 1), p);
983     p << ")";
984   } else if (op.finalValue()) {
985     p << " -> (" << op.getResultTypes() << ')';
986   }
987   p.printOptionalAttrDictWithKeyword(op->getAttrs(), {"finalValue"});
988   p.printRegion(op.region(), /*printEntryBlockArgs=*/false,
989                 /*printBlockTerminators=*/true);
990 }
991 
992 mlir::Region &fir::IterWhileOp::getLoopBody() { return region(); }
993 
994 bool fir::IterWhileOp::isDefinedOutsideOfLoop(mlir::Value value) {
995   return !region().isAncestor(value.getParentRegion());
996 }
997 
998 mlir::LogicalResult
999 fir::IterWhileOp::moveOutOfLoop(llvm::ArrayRef<mlir::Operation *> ops) {
1000   for (auto op : ops)
1001     op->moveBefore(*this);
1002   return success();
1003 }
1004 
1005 mlir::BlockArgument fir::IterWhileOp::iterArgToBlockArg(mlir::Value iterArg) {
1006   for (auto i : llvm::enumerate(initArgs()))
1007     if (iterArg == i.value())
1008       return region().front().getArgument(i.index() + 1);
1009   return {};
1010 }
1011 
1012 void fir::IterWhileOp::resultToSourceOps(
1013     llvm::SmallVectorImpl<mlir::Value> &results, unsigned resultNum) {
1014   auto oper = finalValue() ? resultNum + 1 : resultNum;
1015   auto *term = region().front().getTerminator();
1016   if (oper < term->getNumOperands())
1017     results.push_back(term->getOperand(oper));
1018 }
1019 
1020 mlir::Value fir::IterWhileOp::blockArgToSourceOp(unsigned blockArgNum) {
1021   if (blockArgNum > 0 && blockArgNum <= initArgs().size())
1022     return initArgs()[blockArgNum - 1];
1023   return {};
1024 }
1025 
1026 //===----------------------------------------------------------------------===//
1027 // LoadOp
1028 //===----------------------------------------------------------------------===//
1029 
1030 /// Get the element type of a reference like type; otherwise null
1031 static mlir::Type elementTypeOf(mlir::Type ref) {
1032   return llvm::TypeSwitch<mlir::Type, mlir::Type>(ref)
1033       .Case<ReferenceType, PointerType, HeapType>(
1034           [](auto type) { return type.getEleTy(); })
1035       .Default([](mlir::Type) { return mlir::Type{}; });
1036 }
1037 
1038 mlir::ParseResult fir::LoadOp::getElementOf(mlir::Type &ele, mlir::Type ref) {
1039   if ((ele = elementTypeOf(ref)))
1040     return mlir::success();
1041   return mlir::failure();
1042 }
1043 
1044 //===----------------------------------------------------------------------===//
1045 // DoLoopOp
1046 //===----------------------------------------------------------------------===//
1047 
1048 void fir::DoLoopOp::build(mlir::OpBuilder &builder,
1049                           mlir::OperationState &result, mlir::Value lb,
1050                           mlir::Value ub, mlir::Value step, bool unordered,
1051                           bool finalCountValue, mlir::ValueRange iterArgs,
1052                           llvm::ArrayRef<mlir::NamedAttribute> attributes) {
1053   result.addOperands({lb, ub, step});
1054   result.addOperands(iterArgs);
1055   if (finalCountValue) {
1056     result.addTypes(builder.getIndexType());
1057     result.addAttribute(finalValueAttrName(result.name), builder.getUnitAttr());
1058   }
1059   for (auto v : iterArgs)
1060     result.addTypes(v.getType());
1061   mlir::Region *bodyRegion = result.addRegion();
1062   bodyRegion->push_back(new Block{});
1063   if (iterArgs.empty() && !finalCountValue)
1064     DoLoopOp::ensureTerminator(*bodyRegion, builder, result.location);
1065   bodyRegion->front().addArgument(builder.getIndexType());
1066   bodyRegion->front().addArguments(iterArgs.getTypes());
1067   if (unordered)
1068     result.addAttribute(unorderedAttrName(result.name), builder.getUnitAttr());
1069   result.addAttributes(attributes);
1070 }
1071 
1072 static mlir::ParseResult parseDoLoopOp(mlir::OpAsmParser &parser,
1073                                        mlir::OperationState &result) {
1074   auto &builder = parser.getBuilder();
1075   mlir::OpAsmParser::OperandType inductionVariable, lb, ub, step;
1076   // Parse the induction variable followed by '='.
1077   if (parser.parseRegionArgument(inductionVariable) || parser.parseEqual())
1078     return mlir::failure();
1079 
1080   // Parse loop bounds.
1081   auto indexType = builder.getIndexType();
1082   if (parser.parseOperand(lb) ||
1083       parser.resolveOperand(lb, indexType, result.operands) ||
1084       parser.parseKeyword("to") || parser.parseOperand(ub) ||
1085       parser.resolveOperand(ub, indexType, result.operands) ||
1086       parser.parseKeyword("step") || parser.parseOperand(step) ||
1087       parser.resolveOperand(step, indexType, result.operands))
1088     return failure();
1089 
1090   if (mlir::succeeded(parser.parseOptionalKeyword("unordered")))
1091     result.addAttribute("unordered", builder.getUnitAttr());
1092 
1093   // Parse the optional initial iteration arguments.
1094   llvm::SmallVector<mlir::OpAsmParser::OperandType, 4> regionArgs, operands;
1095   llvm::SmallVector<mlir::Type, 4> argTypes;
1096   auto prependCount = false;
1097   regionArgs.push_back(inductionVariable);
1098 
1099   if (succeeded(parser.parseOptionalKeyword("iter_args"))) {
1100     // Parse assignment list and results type list.
1101     if (parser.parseAssignmentList(regionArgs, operands) ||
1102         parser.parseArrowTypeList(result.types))
1103       return failure();
1104     if (result.types.size() == operands.size() + 1)
1105       prependCount = true;
1106     // Resolve input operands.
1107     llvm::ArrayRef<mlir::Type> resTypes = result.types;
1108     for (auto operand_type :
1109          llvm::zip(operands, prependCount ? resTypes.drop_front() : resTypes))
1110       if (parser.resolveOperand(std::get<0>(operand_type),
1111                                 std::get<1>(operand_type), result.operands))
1112         return failure();
1113   } else if (succeeded(parser.parseOptionalArrow())) {
1114     if (parser.parseKeyword("index"))
1115       return failure();
1116     result.types.push_back(indexType);
1117     prependCount = true;
1118   }
1119 
1120   if (parser.parseOptionalAttrDictWithKeyword(result.attributes))
1121     return mlir::failure();
1122 
1123   // Induction variable.
1124   if (prependCount)
1125     result.addAttribute(DoLoopOp::finalValueAttrName(result.name),
1126                         builder.getUnitAttr());
1127   else
1128     argTypes.push_back(indexType);
1129   // Loop carried variables
1130   argTypes.append(result.types.begin(), result.types.end());
1131   // Parse the body region.
1132   auto *body = result.addRegion();
1133   if (regionArgs.size() != argTypes.size())
1134     return parser.emitError(
1135         parser.getNameLoc(),
1136         "mismatch in number of loop-carried values and defined values");
1137 
1138   if (parser.parseRegion(*body, regionArgs, argTypes))
1139     return failure();
1140 
1141   DoLoopOp::ensureTerminator(*body, builder, result.location);
1142 
1143   return mlir::success();
1144 }
1145 
1146 fir::DoLoopOp fir::getForInductionVarOwner(mlir::Value val) {
1147   auto ivArg = val.dyn_cast<mlir::BlockArgument>();
1148   if (!ivArg)
1149     return {};
1150   assert(ivArg.getOwner() && "unlinked block argument");
1151   auto *containingInst = ivArg.getOwner()->getParentOp();
1152   return dyn_cast_or_null<fir::DoLoopOp>(containingInst);
1153 }
1154 
1155 // Lifted from loop.loop
1156 static mlir::LogicalResult verify(fir::DoLoopOp op) {
1157   // Check that the body defines as single block argument for the induction
1158   // variable.
1159   auto *body = op.getBody();
1160   if (!body->getArgument(0).getType().isIndex())
1161     return op.emitOpError(
1162         "expected body first argument to be an index argument for "
1163         "the induction variable");
1164 
1165   auto opNumResults = op.getNumResults();
1166   if (opNumResults == 0)
1167     return success();
1168 
1169   if (op.finalValue()) {
1170     if (op.unordered())
1171       return op.emitOpError("unordered loop has no final value");
1172     opNumResults--;
1173   }
1174   if (op.getNumIterOperands() != opNumResults)
1175     return op.emitOpError(
1176         "mismatch in number of loop-carried values and defined values");
1177   if (op.getNumRegionIterArgs() != opNumResults)
1178     return op.emitOpError(
1179         "mismatch in number of basic block args and defined values");
1180   auto iterOperands = op.getIterOperands();
1181   auto iterArgs = op.getRegionIterArgs();
1182   auto opResults =
1183       op.finalValue() ? op.getResults().drop_front() : op.getResults();
1184   unsigned i = 0;
1185   for (auto e : llvm::zip(iterOperands, iterArgs, opResults)) {
1186     if (std::get<0>(e).getType() != std::get<2>(e).getType())
1187       return op.emitOpError() << "types mismatch between " << i
1188                               << "th iter operand and defined value";
1189     if (std::get<1>(e).getType() != std::get<2>(e).getType())
1190       return op.emitOpError() << "types mismatch between " << i
1191                               << "th iter region arg and defined value";
1192 
1193     i++;
1194   }
1195   return success();
1196 }
1197 
1198 static void print(mlir::OpAsmPrinter &p, fir::DoLoopOp op) {
1199   bool printBlockTerminators = false;
1200   p << ' ' << op.getInductionVar() << " = " << op.lowerBound() << " to "
1201     << op.upperBound() << " step " << op.step();
1202   if (op.unordered())
1203     p << " unordered";
1204   if (op.hasIterOperands()) {
1205     p << " iter_args(";
1206     auto regionArgs = op.getRegionIterArgs();
1207     auto operands = op.getIterOperands();
1208     llvm::interleaveComma(llvm::zip(regionArgs, operands), p, [&](auto it) {
1209       p << std::get<0>(it) << " = " << std::get<1>(it);
1210     });
1211     p << ") -> (" << op.getResultTypes() << ')';
1212     printBlockTerminators = true;
1213   } else if (op.finalValue()) {
1214     p << " -> " << op.getResultTypes();
1215     printBlockTerminators = true;
1216   }
1217   p.printOptionalAttrDictWithKeyword(op->getAttrs(),
1218                                      {"unordered", "finalValue"});
1219   p.printRegion(op.region(), /*printEntryBlockArgs=*/false,
1220                 printBlockTerminators);
1221 }
1222 
1223 mlir::Region &fir::DoLoopOp::getLoopBody() { return region(); }
1224 
1225 bool fir::DoLoopOp::isDefinedOutsideOfLoop(mlir::Value value) {
1226   return !region().isAncestor(value.getParentRegion());
1227 }
1228 
1229 mlir::LogicalResult
1230 fir::DoLoopOp::moveOutOfLoop(llvm::ArrayRef<mlir::Operation *> ops) {
1231   for (auto op : ops)
1232     op->moveBefore(*this);
1233   return success();
1234 }
1235 
1236 /// Translate a value passed as an iter_arg to the corresponding block
1237 /// argument in the body of the loop.
1238 mlir::BlockArgument fir::DoLoopOp::iterArgToBlockArg(mlir::Value iterArg) {
1239   for (auto i : llvm::enumerate(initArgs()))
1240     if (iterArg == i.value())
1241       return region().front().getArgument(i.index() + 1);
1242   return {};
1243 }
1244 
1245 /// Translate the result vector (by index number) to the corresponding value
1246 /// to the `fir.result` Op.
1247 void fir::DoLoopOp::resultToSourceOps(
1248     llvm::SmallVectorImpl<mlir::Value> &results, unsigned resultNum) {
1249   auto oper = finalValue() ? resultNum + 1 : resultNum;
1250   auto *term = region().front().getTerminator();
1251   if (oper < term->getNumOperands())
1252     results.push_back(term->getOperand(oper));
1253 }
1254 
1255 /// Translate the block argument (by index number) to the corresponding value
1256 /// passed as an iter_arg to the parent DoLoopOp.
1257 mlir::Value fir::DoLoopOp::blockArgToSourceOp(unsigned blockArgNum) {
1258   if (blockArgNum > 0 && blockArgNum <= initArgs().size())
1259     return initArgs()[blockArgNum - 1];
1260   return {};
1261 }
1262 
1263 //===----------------------------------------------------------------------===//
1264 // ReboxOp
1265 //===----------------------------------------------------------------------===//
1266 
1267 /// Get the scalar type related to a fir.box type.
1268 /// Example: return f32 for !fir.box<!fir.heap<!fir.array<?x?xf32>>.
1269 static mlir::Type getBoxScalarEleTy(mlir::Type boxTy) {
1270   auto eleTy = fir::dyn_cast_ptrOrBoxEleTy(boxTy);
1271   if (auto seqTy = eleTy.dyn_cast<fir::SequenceType>())
1272     return seqTy.getEleTy();
1273   return eleTy;
1274 }
1275 
1276 /// Get the rank from a !fir.box type
1277 static unsigned getBoxRank(mlir::Type boxTy) {
1278   auto eleTy = fir::dyn_cast_ptrOrBoxEleTy(boxTy);
1279   if (auto seqTy = eleTy.dyn_cast<fir::SequenceType>())
1280     return seqTy.getDimension();
1281   return 0;
1282 }
1283 
1284 static mlir::LogicalResult verify(fir::ReboxOp op) {
1285   auto inputBoxTy = op.box().getType();
1286   if (fir::isa_unknown_size_box(inputBoxTy))
1287     return op.emitOpError("box operand must not have unknown rank or type");
1288   auto outBoxTy = op.getType();
1289   if (fir::isa_unknown_size_box(outBoxTy))
1290     return op.emitOpError("result type must not have unknown rank or type");
1291   auto inputRank = getBoxRank(inputBoxTy);
1292   auto inputEleTy = getBoxScalarEleTy(inputBoxTy);
1293   auto outRank = getBoxRank(outBoxTy);
1294   auto outEleTy = getBoxScalarEleTy(outBoxTy);
1295 
1296   if (auto slice = op.slice()) {
1297     // Slicing case
1298     if (slice.getType().cast<fir::SliceType>().getRank() != inputRank)
1299       return op.emitOpError("slice operand rank must match box operand rank");
1300     if (auto shape = op.shape()) {
1301       if (auto shiftTy = shape.getType().dyn_cast<fir::ShiftType>()) {
1302         if (shiftTy.getRank() != inputRank)
1303           return op.emitOpError("shape operand and input box ranks must match "
1304                                 "when there is a slice");
1305       } else {
1306         return op.emitOpError("shape operand must absent or be a fir.shift "
1307                               "when there is a slice");
1308       }
1309     }
1310     if (auto sliceOp = slice.getDefiningOp()) {
1311       auto slicedRank = mlir::cast<fir::SliceOp>(sliceOp).getOutRank();
1312       if (slicedRank != outRank)
1313         return op.emitOpError("result type rank and rank after applying slice "
1314                               "operand must match");
1315     }
1316   } else {
1317     // Reshaping case
1318     unsigned shapeRank = inputRank;
1319     if (auto shape = op.shape()) {
1320       auto ty = shape.getType();
1321       if (auto shapeTy = ty.dyn_cast<fir::ShapeType>()) {
1322         shapeRank = shapeTy.getRank();
1323       } else if (auto shapeShiftTy = ty.dyn_cast<fir::ShapeShiftType>()) {
1324         shapeRank = shapeShiftTy.getRank();
1325       } else {
1326         auto shiftTy = ty.cast<fir::ShiftType>();
1327         shapeRank = shiftTy.getRank();
1328         if (shapeRank != inputRank)
1329           return op.emitOpError("shape operand and input box ranks must match "
1330                                 "when the shape is a fir.shift");
1331       }
1332     }
1333     if (shapeRank != outRank)
1334       return op.emitOpError("result type and shape operand ranks must match");
1335   }
1336 
1337   if (inputEleTy != outEleTy)
1338     // TODO: check that outBoxTy is a parent type of inputBoxTy for derived
1339     // types.
1340     if (!inputEleTy.isa<fir::RecordType>())
1341       return op.emitOpError(
1342           "op input and output element types must match for intrinsic types");
1343   return mlir::success();
1344 }
1345 
1346 //===----------------------------------------------------------------------===//
1347 // ResultOp
1348 //===----------------------------------------------------------------------===//
1349 
1350 static mlir::LogicalResult verify(fir::ResultOp op) {
1351   auto *parentOp = op->getParentOp();
1352   auto results = parentOp->getResults();
1353   auto operands = op->getOperands();
1354 
1355   if (parentOp->getNumResults() != op.getNumOperands())
1356     return op.emitOpError() << "parent of result must have same arity";
1357   for (auto e : llvm::zip(results, operands))
1358     if (std::get<0>(e).getType() != std::get<1>(e).getType())
1359       return op.emitOpError()
1360              << "types mismatch between result op and its parent";
1361   return success();
1362 }
1363 
1364 //===----------------------------------------------------------------------===//
1365 // SaveResultOp
1366 //===----------------------------------------------------------------------===//
1367 
1368 static mlir::LogicalResult verify(fir::SaveResultOp op) {
1369   auto resultType = op.value().getType();
1370   if (resultType != fir::dyn_cast_ptrEleTy(op.memref().getType()))
1371     return op.emitOpError("value type must match memory reference type");
1372   if (fir::isa_unknown_size_box(resultType))
1373     return op.emitOpError("cannot save !fir.box of unknown rank or type");
1374 
1375   if (resultType.isa<fir::BoxType>()) {
1376     if (op.shape() || !op.typeparams().empty())
1377       return op.emitOpError(
1378           "must not have shape or length operands if the value is a fir.box");
1379     return mlir::success();
1380   }
1381 
1382   // fir.record or fir.array case.
1383   unsigned shapeTyRank = 0;
1384   if (auto shapeOp = op.shape()) {
1385     auto shapeTy = shapeOp.getType();
1386     if (auto s = shapeTy.dyn_cast<fir::ShapeType>())
1387       shapeTyRank = s.getRank();
1388     else
1389       shapeTyRank = shapeTy.cast<fir::ShapeShiftType>().getRank();
1390   }
1391 
1392   auto eleTy = resultType;
1393   if (auto seqTy = resultType.dyn_cast<fir::SequenceType>()) {
1394     if (seqTy.getDimension() != shapeTyRank)
1395       op.emitOpError("shape operand must be provided and have the value rank "
1396                      "when the value is a fir.array");
1397     eleTy = seqTy.getEleTy();
1398   } else {
1399     if (shapeTyRank != 0)
1400       op.emitOpError(
1401           "shape operand should only be provided if the value is a fir.array");
1402   }
1403 
1404   if (auto recTy = eleTy.dyn_cast<fir::RecordType>()) {
1405     if (recTy.getNumLenParams() != op.typeparams().size())
1406       op.emitOpError("length parameters number must match with the value type "
1407                      "length parameters");
1408   } else if (auto charTy = eleTy.dyn_cast<fir::CharacterType>()) {
1409     if (op.typeparams().size() > 1)
1410       op.emitOpError("no more than one length parameter must be provided for "
1411                      "character value");
1412   } else {
1413     if (!op.typeparams().empty())
1414       op.emitOpError(
1415           "length parameters must not be provided for this value type");
1416   }
1417 
1418   return mlir::success();
1419 }
1420 
1421 //===----------------------------------------------------------------------===//
1422 // SelectOp
1423 //===----------------------------------------------------------------------===//
1424 
1425 static constexpr llvm::StringRef getCompareOffsetAttr() {
1426   return "compare_operand_offsets";
1427 }
1428 
1429 static constexpr llvm::StringRef getTargetOffsetAttr() {
1430   return "target_operand_offsets";
1431 }
1432 
1433 template <typename A, typename... AdditionalArgs>
1434 static A getSubOperands(unsigned pos, A allArgs,
1435                         mlir::DenseIntElementsAttr ranges,
1436                         AdditionalArgs &&...additionalArgs) {
1437   unsigned start = 0;
1438   for (unsigned i = 0; i < pos; ++i)
1439     start += (*(ranges.begin() + i)).getZExtValue();
1440   return allArgs.slice(start, (*(ranges.begin() + pos)).getZExtValue(),
1441                        std::forward<AdditionalArgs>(additionalArgs)...);
1442 }
1443 
1444 static mlir::MutableOperandRange
1445 getMutableSuccessorOperands(unsigned pos, mlir::MutableOperandRange operands,
1446                             StringRef offsetAttr) {
1447   Operation *owner = operands.getOwner();
1448   NamedAttribute targetOffsetAttr =
1449       *owner->getAttrDictionary().getNamed(offsetAttr);
1450   return getSubOperands(
1451       pos, operands, targetOffsetAttr.second.cast<DenseIntElementsAttr>(),
1452       mlir::MutableOperandRange::OperandSegment(pos, targetOffsetAttr));
1453 }
1454 
1455 static unsigned denseElementsSize(mlir::DenseIntElementsAttr attr) {
1456   return attr.getNumElements();
1457 }
1458 
1459 llvm::Optional<mlir::OperandRange> fir::SelectOp::getCompareOperands(unsigned) {
1460   return {};
1461 }
1462 
1463 llvm::Optional<llvm::ArrayRef<mlir::Value>>
1464 fir::SelectOp::getCompareOperands(llvm::ArrayRef<mlir::Value>, unsigned) {
1465   return {};
1466 }
1467 
1468 llvm::Optional<mlir::MutableOperandRange>
1469 fir::SelectOp::getMutableSuccessorOperands(unsigned oper) {
1470   return ::getMutableSuccessorOperands(oper, targetArgsMutable(),
1471                                        getTargetOffsetAttr());
1472 }
1473 
1474 llvm::Optional<llvm::ArrayRef<mlir::Value>>
1475 fir::SelectOp::getSuccessorOperands(llvm::ArrayRef<mlir::Value> operands,
1476                                     unsigned oper) {
1477   auto a =
1478       (*this)->getAttrOfType<mlir::DenseIntElementsAttr>(getTargetOffsetAttr());
1479   auto segments = (*this)->getAttrOfType<mlir::DenseIntElementsAttr>(
1480       getOperandSegmentSizeAttr());
1481   return {getSubOperands(oper, getSubOperands(2, operands, segments), a)};
1482 }
1483 
1484 unsigned fir::SelectOp::targetOffsetSize() {
1485   return denseElementsSize((*this)->getAttrOfType<mlir::DenseIntElementsAttr>(
1486       getTargetOffsetAttr()));
1487 }
1488 
1489 //===----------------------------------------------------------------------===//
1490 // SelectCaseOp
1491 //===----------------------------------------------------------------------===//
1492 
1493 llvm::Optional<mlir::OperandRange>
1494 fir::SelectCaseOp::getCompareOperands(unsigned cond) {
1495   auto a = (*this)->getAttrOfType<mlir::DenseIntElementsAttr>(
1496       getCompareOffsetAttr());
1497   return {getSubOperands(cond, compareArgs(), a)};
1498 }
1499 
1500 llvm::Optional<llvm::ArrayRef<mlir::Value>>
1501 fir::SelectCaseOp::getCompareOperands(llvm::ArrayRef<mlir::Value> operands,
1502                                       unsigned cond) {
1503   auto a = (*this)->getAttrOfType<mlir::DenseIntElementsAttr>(
1504       getCompareOffsetAttr());
1505   auto segments = (*this)->getAttrOfType<mlir::DenseIntElementsAttr>(
1506       getOperandSegmentSizeAttr());
1507   return {getSubOperands(cond, getSubOperands(1, operands, segments), a)};
1508 }
1509 
1510 llvm::Optional<mlir::MutableOperandRange>
1511 fir::SelectCaseOp::getMutableSuccessorOperands(unsigned oper) {
1512   return ::getMutableSuccessorOperands(oper, targetArgsMutable(),
1513                                        getTargetOffsetAttr());
1514 }
1515 
1516 llvm::Optional<llvm::ArrayRef<mlir::Value>>
1517 fir::SelectCaseOp::getSuccessorOperands(llvm::ArrayRef<mlir::Value> operands,
1518                                         unsigned oper) {
1519   auto a =
1520       (*this)->getAttrOfType<mlir::DenseIntElementsAttr>(getTargetOffsetAttr());
1521   auto segments = (*this)->getAttrOfType<mlir::DenseIntElementsAttr>(
1522       getOperandSegmentSizeAttr());
1523   return {getSubOperands(oper, getSubOperands(2, operands, segments), a)};
1524 }
1525 
1526 // parser for fir.select_case Op
1527 static mlir::ParseResult parseSelectCase(mlir::OpAsmParser &parser,
1528                                          mlir::OperationState &result) {
1529   mlir::OpAsmParser::OperandType selector;
1530   mlir::Type type;
1531   if (parseSelector(parser, result, selector, type))
1532     return mlir::failure();
1533 
1534   llvm::SmallVector<mlir::Attribute, 8> attrs;
1535   llvm::SmallVector<mlir::OpAsmParser::OperandType, 8> opers;
1536   llvm::SmallVector<mlir::Block *, 8> dests;
1537   llvm::SmallVector<llvm::SmallVector<mlir::Value, 8>, 8> destArgs;
1538   llvm::SmallVector<int32_t, 8> argOffs;
1539   int32_t offSize = 0;
1540   while (true) {
1541     mlir::Attribute attr;
1542     mlir::Block *dest;
1543     llvm::SmallVector<mlir::Value, 8> destArg;
1544     mlir::NamedAttrList temp;
1545     if (parser.parseAttribute(attr, "a", temp) || isValidCaseAttr(attr) ||
1546         parser.parseComma())
1547       return mlir::failure();
1548     attrs.push_back(attr);
1549     if (attr.dyn_cast_or_null<mlir::UnitAttr>()) {
1550       argOffs.push_back(0);
1551     } else if (attr.dyn_cast_or_null<fir::ClosedIntervalAttr>()) {
1552       mlir::OpAsmParser::OperandType oper1;
1553       mlir::OpAsmParser::OperandType oper2;
1554       if (parser.parseOperand(oper1) || parser.parseComma() ||
1555           parser.parseOperand(oper2) || parser.parseComma())
1556         return mlir::failure();
1557       opers.push_back(oper1);
1558       opers.push_back(oper2);
1559       argOffs.push_back(2);
1560       offSize += 2;
1561     } else {
1562       mlir::OpAsmParser::OperandType oper;
1563       if (parser.parseOperand(oper) || parser.parseComma())
1564         return mlir::failure();
1565       opers.push_back(oper);
1566       argOffs.push_back(1);
1567       ++offSize;
1568     }
1569     if (parser.parseSuccessorAndUseList(dest, destArg))
1570       return mlir::failure();
1571     dests.push_back(dest);
1572     destArgs.push_back(destArg);
1573     if (mlir::succeeded(parser.parseOptionalRSquare()))
1574       break;
1575     if (parser.parseComma())
1576       return mlir::failure();
1577   }
1578   result.addAttribute(fir::SelectCaseOp::getCasesAttr(),
1579                       parser.getBuilder().getArrayAttr(attrs));
1580   if (parser.resolveOperands(opers, type, result.operands))
1581     return mlir::failure();
1582   llvm::SmallVector<int32_t, 8> targOffs;
1583   int32_t toffSize = 0;
1584   const auto count = dests.size();
1585   for (std::remove_const_t<decltype(count)> i = 0; i != count; ++i) {
1586     result.addSuccessors(dests[i]);
1587     result.addOperands(destArgs[i]);
1588     auto argSize = destArgs[i].size();
1589     targOffs.push_back(argSize);
1590     toffSize += argSize;
1591   }
1592   auto &bld = parser.getBuilder();
1593   result.addAttribute(fir::SelectCaseOp::getOperandSegmentSizeAttr(),
1594                       bld.getI32VectorAttr({1, offSize, toffSize}));
1595   result.addAttribute(getCompareOffsetAttr(), bld.getI32VectorAttr(argOffs));
1596   result.addAttribute(getTargetOffsetAttr(), bld.getI32VectorAttr(targOffs));
1597   return mlir::success();
1598 }
1599 
1600 unsigned fir::SelectCaseOp::compareOffsetSize() {
1601   return denseElementsSize((*this)->getAttrOfType<mlir::DenseIntElementsAttr>(
1602       getCompareOffsetAttr()));
1603 }
1604 
1605 unsigned fir::SelectCaseOp::targetOffsetSize() {
1606   return denseElementsSize((*this)->getAttrOfType<mlir::DenseIntElementsAttr>(
1607       getTargetOffsetAttr()));
1608 }
1609 
1610 void fir::SelectCaseOp::build(mlir::OpBuilder &builder,
1611                               mlir::OperationState &result,
1612                               mlir::Value selector,
1613                               llvm::ArrayRef<mlir::Attribute> compareAttrs,
1614                               llvm::ArrayRef<mlir::ValueRange> cmpOperands,
1615                               llvm::ArrayRef<mlir::Block *> destinations,
1616                               llvm::ArrayRef<mlir::ValueRange> destOperands,
1617                               llvm::ArrayRef<mlir::NamedAttribute> attributes) {
1618   result.addOperands(selector);
1619   result.addAttribute(getCasesAttr(), builder.getArrayAttr(compareAttrs));
1620   llvm::SmallVector<int32_t, 8> operOffs;
1621   int32_t operSize = 0;
1622   for (auto attr : compareAttrs) {
1623     if (attr.isa<fir::ClosedIntervalAttr>()) {
1624       operOffs.push_back(2);
1625       operSize += 2;
1626     } else if (attr.isa<mlir::UnitAttr>()) {
1627       operOffs.push_back(0);
1628     } else {
1629       operOffs.push_back(1);
1630       ++operSize;
1631     }
1632   }
1633   for (auto ops : cmpOperands)
1634     result.addOperands(ops);
1635   result.addAttribute(getCompareOffsetAttr(),
1636                       builder.getI32VectorAttr(operOffs));
1637   const auto count = destinations.size();
1638   for (auto d : destinations)
1639     result.addSuccessors(d);
1640   const auto opCount = destOperands.size();
1641   llvm::SmallVector<int32_t, 8> argOffs;
1642   int32_t sumArgs = 0;
1643   for (std::remove_const_t<decltype(count)> i = 0; i != count; ++i) {
1644     if (i < opCount) {
1645       result.addOperands(destOperands[i]);
1646       const auto argSz = destOperands[i].size();
1647       argOffs.push_back(argSz);
1648       sumArgs += argSz;
1649     } else {
1650       argOffs.push_back(0);
1651     }
1652   }
1653   result.addAttribute(getOperandSegmentSizeAttr(),
1654                       builder.getI32VectorAttr({1, operSize, sumArgs}));
1655   result.addAttribute(getTargetOffsetAttr(), builder.getI32VectorAttr(argOffs));
1656   result.addAttributes(attributes);
1657 }
1658 
1659 /// This builder has a slightly simplified interface in that the list of
1660 /// operands need not be partitioned by the builder. Instead the operands are
1661 /// partitioned here, before being passed to the default builder. This
1662 /// partitioning is unchecked, so can go awry on bad input.
1663 void fir::SelectCaseOp::build(mlir::OpBuilder &builder,
1664                               mlir::OperationState &result,
1665                               mlir::Value selector,
1666                               llvm::ArrayRef<mlir::Attribute> compareAttrs,
1667                               llvm::ArrayRef<mlir::Value> cmpOpList,
1668                               llvm::ArrayRef<mlir::Block *> destinations,
1669                               llvm::ArrayRef<mlir::ValueRange> destOperands,
1670                               llvm::ArrayRef<mlir::NamedAttribute> attributes) {
1671   llvm::SmallVector<mlir::ValueRange, 16> cmpOpers;
1672   auto iter = cmpOpList.begin();
1673   for (auto &attr : compareAttrs) {
1674     if (attr.isa<fir::ClosedIntervalAttr>()) {
1675       cmpOpers.push_back(mlir::ValueRange({iter, iter + 2}));
1676       iter += 2;
1677     } else if (attr.isa<UnitAttr>()) {
1678       cmpOpers.push_back(mlir::ValueRange{});
1679     } else {
1680       cmpOpers.push_back(mlir::ValueRange({iter, iter + 1}));
1681       ++iter;
1682     }
1683   }
1684   build(builder, result, selector, compareAttrs, cmpOpers, destinations,
1685         destOperands, attributes);
1686 }
1687 
1688 //===----------------------------------------------------------------------===//
1689 // SelectRankOp
1690 //===----------------------------------------------------------------------===//
1691 
1692 llvm::Optional<mlir::OperandRange>
1693 fir::SelectRankOp::getCompareOperands(unsigned) {
1694   return {};
1695 }
1696 
1697 llvm::Optional<llvm::ArrayRef<mlir::Value>>
1698 fir::SelectRankOp::getCompareOperands(llvm::ArrayRef<mlir::Value>, unsigned) {
1699   return {};
1700 }
1701 
1702 llvm::Optional<mlir::MutableOperandRange>
1703 fir::SelectRankOp::getMutableSuccessorOperands(unsigned oper) {
1704   return ::getMutableSuccessorOperands(oper, targetArgsMutable(),
1705                                        getTargetOffsetAttr());
1706 }
1707 
1708 llvm::Optional<llvm::ArrayRef<mlir::Value>>
1709 fir::SelectRankOp::getSuccessorOperands(llvm::ArrayRef<mlir::Value> operands,
1710                                         unsigned oper) {
1711   auto a =
1712       (*this)->getAttrOfType<mlir::DenseIntElementsAttr>(getTargetOffsetAttr());
1713   auto segments = (*this)->getAttrOfType<mlir::DenseIntElementsAttr>(
1714       getOperandSegmentSizeAttr());
1715   return {getSubOperands(oper, getSubOperands(2, operands, segments), a)};
1716 }
1717 
1718 unsigned fir::SelectRankOp::targetOffsetSize() {
1719   return denseElementsSize((*this)->getAttrOfType<mlir::DenseIntElementsAttr>(
1720       getTargetOffsetAttr()));
1721 }
1722 
1723 //===----------------------------------------------------------------------===//
1724 // SelectTypeOp
1725 //===----------------------------------------------------------------------===//
1726 
1727 llvm::Optional<mlir::OperandRange>
1728 fir::SelectTypeOp::getCompareOperands(unsigned) {
1729   return {};
1730 }
1731 
1732 llvm::Optional<llvm::ArrayRef<mlir::Value>>
1733 fir::SelectTypeOp::getCompareOperands(llvm::ArrayRef<mlir::Value>, unsigned) {
1734   return {};
1735 }
1736 
1737 llvm::Optional<mlir::MutableOperandRange>
1738 fir::SelectTypeOp::getMutableSuccessorOperands(unsigned oper) {
1739   return ::getMutableSuccessorOperands(oper, targetArgsMutable(),
1740                                        getTargetOffsetAttr());
1741 }
1742 
1743 llvm::Optional<llvm::ArrayRef<mlir::Value>>
1744 fir::SelectTypeOp::getSuccessorOperands(llvm::ArrayRef<mlir::Value> operands,
1745                                         unsigned oper) {
1746   auto a =
1747       (*this)->getAttrOfType<mlir::DenseIntElementsAttr>(getTargetOffsetAttr());
1748   auto segments = (*this)->getAttrOfType<mlir::DenseIntElementsAttr>(
1749       getOperandSegmentSizeAttr());
1750   return {getSubOperands(oper, getSubOperands(2, operands, segments), a)};
1751 }
1752 
1753 static ParseResult parseSelectType(OpAsmParser &parser,
1754                                    OperationState &result) {
1755   mlir::OpAsmParser::OperandType selector;
1756   mlir::Type type;
1757   if (parseSelector(parser, result, selector, type))
1758     return mlir::failure();
1759 
1760   llvm::SmallVector<mlir::Attribute, 8> attrs;
1761   llvm::SmallVector<mlir::Block *, 8> dests;
1762   llvm::SmallVector<llvm::SmallVector<mlir::Value, 8>, 8> destArgs;
1763   while (true) {
1764     mlir::Attribute attr;
1765     mlir::Block *dest;
1766     llvm::SmallVector<mlir::Value, 8> destArg;
1767     mlir::NamedAttrList temp;
1768     if (parser.parseAttribute(attr, "a", temp) || parser.parseComma() ||
1769         parser.parseSuccessorAndUseList(dest, destArg))
1770       return mlir::failure();
1771     attrs.push_back(attr);
1772     dests.push_back(dest);
1773     destArgs.push_back(destArg);
1774     if (mlir::succeeded(parser.parseOptionalRSquare()))
1775       break;
1776     if (parser.parseComma())
1777       return mlir::failure();
1778   }
1779   auto &bld = parser.getBuilder();
1780   result.addAttribute(fir::SelectTypeOp::getCasesAttr(),
1781                       bld.getArrayAttr(attrs));
1782   llvm::SmallVector<int32_t, 8> argOffs;
1783   int32_t offSize = 0;
1784   const auto count = dests.size();
1785   for (std::remove_const_t<decltype(count)> i = 0; i != count; ++i) {
1786     result.addSuccessors(dests[i]);
1787     result.addOperands(destArgs[i]);
1788     auto argSize = destArgs[i].size();
1789     argOffs.push_back(argSize);
1790     offSize += argSize;
1791   }
1792   result.addAttribute(fir::SelectTypeOp::getOperandSegmentSizeAttr(),
1793                       bld.getI32VectorAttr({1, 0, offSize}));
1794   result.addAttribute(getTargetOffsetAttr(), bld.getI32VectorAttr(argOffs));
1795   return mlir::success();
1796 }
1797 
1798 unsigned fir::SelectTypeOp::targetOffsetSize() {
1799   return denseElementsSize((*this)->getAttrOfType<mlir::DenseIntElementsAttr>(
1800       getTargetOffsetAttr()));
1801 }
1802 
1803 //===----------------------------------------------------------------------===//
1804 // SliceOp
1805 //===----------------------------------------------------------------------===//
1806 
1807 /// Return the output rank of a slice op. The output rank must be between 1 and
1808 /// the rank of the array being sliced (inclusive).
1809 unsigned fir::SliceOp::getOutputRank(mlir::ValueRange triples) {
1810   unsigned rank = 0;
1811   if (!triples.empty()) {
1812     for (unsigned i = 1, end = triples.size(); i < end; i += 3) {
1813       auto op = triples[i].getDefiningOp();
1814       if (!mlir::isa_and_nonnull<fir::UndefOp>(op))
1815         ++rank;
1816     }
1817     assert(rank > 0);
1818   }
1819   return rank;
1820 }
1821 
1822 //===----------------------------------------------------------------------===//
1823 // StoreOp
1824 //===----------------------------------------------------------------------===//
1825 
1826 mlir::Type fir::StoreOp::elementType(mlir::Type refType) {
1827   if (auto ref = refType.dyn_cast<ReferenceType>())
1828     return ref.getEleTy();
1829   if (auto ref = refType.dyn_cast<PointerType>())
1830     return ref.getEleTy();
1831   if (auto ref = refType.dyn_cast<HeapType>())
1832     return ref.getEleTy();
1833   return {};
1834 }
1835 
1836 //===----------------------------------------------------------------------===//
1837 // StringLitOp
1838 //===----------------------------------------------------------------------===//
1839 
1840 bool fir::StringLitOp::isWideValue() {
1841   auto eleTy = getType().cast<fir::SequenceType>().getEleTy();
1842   return eleTy.cast<fir::CharacterType>().getFKind() != 1;
1843 }
1844 
1845 //===----------------------------------------------------------------------===//
1846 // IfOp
1847 //===----------------------------------------------------------------------===//
1848 
1849 void fir::IfOp::build(mlir::OpBuilder &builder, OperationState &result,
1850                       mlir::Value cond, bool withElseRegion) {
1851   build(builder, result, llvm::None, cond, withElseRegion);
1852 }
1853 
1854 void fir::IfOp::build(mlir::OpBuilder &builder, OperationState &result,
1855                       mlir::TypeRange resultTypes, mlir::Value cond,
1856                       bool withElseRegion) {
1857   result.addOperands(cond);
1858   result.addTypes(resultTypes);
1859 
1860   mlir::Region *thenRegion = result.addRegion();
1861   thenRegion->push_back(new mlir::Block());
1862   if (resultTypes.empty())
1863     IfOp::ensureTerminator(*thenRegion, builder, result.location);
1864 
1865   mlir::Region *elseRegion = result.addRegion();
1866   if (withElseRegion) {
1867     elseRegion->push_back(new mlir::Block());
1868     if (resultTypes.empty())
1869       IfOp::ensureTerminator(*elseRegion, builder, result.location);
1870   }
1871 }
1872 
1873 static mlir::ParseResult parseIfOp(OpAsmParser &parser,
1874                                    OperationState &result) {
1875   result.regions.reserve(2);
1876   mlir::Region *thenRegion = result.addRegion();
1877   mlir::Region *elseRegion = result.addRegion();
1878 
1879   auto &builder = parser.getBuilder();
1880   OpAsmParser::OperandType cond;
1881   mlir::Type i1Type = builder.getIntegerType(1);
1882   if (parser.parseOperand(cond) ||
1883       parser.resolveOperand(cond, i1Type, result.operands))
1884     return mlir::failure();
1885 
1886   if (parser.parseOptionalArrowTypeList(result.types))
1887     return mlir::failure();
1888 
1889   if (parser.parseRegion(*thenRegion, {}, {}))
1890     return mlir::failure();
1891   IfOp::ensureTerminator(*thenRegion, parser.getBuilder(), result.location);
1892 
1893   if (mlir::succeeded(parser.parseOptionalKeyword("else"))) {
1894     if (parser.parseRegion(*elseRegion, {}, {}))
1895       return mlir::failure();
1896     IfOp::ensureTerminator(*elseRegion, parser.getBuilder(), result.location);
1897   }
1898 
1899   // Parse the optional attribute list.
1900   if (parser.parseOptionalAttrDict(result.attributes))
1901     return mlir::failure();
1902   return mlir::success();
1903 }
1904 
1905 static LogicalResult verify(fir::IfOp op) {
1906   if (op.getNumResults() != 0 && op.elseRegion().empty())
1907     return op.emitOpError("must have an else block if defining values");
1908 
1909   return mlir::success();
1910 }
1911 
1912 static void print(mlir::OpAsmPrinter &p, fir::IfOp op) {
1913   bool printBlockTerminators = false;
1914   p << ' ' << op.condition();
1915   if (!op.results().empty()) {
1916     p << " -> (" << op.getResultTypes() << ')';
1917     printBlockTerminators = true;
1918   }
1919   p.printRegion(op.thenRegion(), /*printEntryBlockArgs=*/false,
1920                 printBlockTerminators);
1921 
1922   // Print the 'else' regions if it exists and has a block.
1923   auto &otherReg = op.elseRegion();
1924   if (!otherReg.empty()) {
1925     p << " else";
1926     p.printRegion(otherReg, /*printEntryBlockArgs=*/false,
1927                   printBlockTerminators);
1928   }
1929   p.printOptionalAttrDict(op->getAttrs());
1930 }
1931 
1932 void fir::IfOp::resultToSourceOps(llvm::SmallVectorImpl<mlir::Value> &results,
1933                                   unsigned resultNum) {
1934   auto *term = thenRegion().front().getTerminator();
1935   if (resultNum < term->getNumOperands())
1936     results.push_back(term->getOperand(resultNum));
1937   term = elseRegion().front().getTerminator();
1938   if (resultNum < term->getNumOperands())
1939     results.push_back(term->getOperand(resultNum));
1940 }
1941 
1942 //===----------------------------------------------------------------------===//
1943 
1944 mlir::ParseResult fir::isValidCaseAttr(mlir::Attribute attr) {
1945   if (attr.dyn_cast_or_null<mlir::UnitAttr>() ||
1946       attr.dyn_cast_or_null<ClosedIntervalAttr>() ||
1947       attr.dyn_cast_or_null<PointIntervalAttr>() ||
1948       attr.dyn_cast_or_null<LowerBoundAttr>() ||
1949       attr.dyn_cast_or_null<UpperBoundAttr>())
1950     return mlir::success();
1951   return mlir::failure();
1952 }
1953 
1954 unsigned fir::getCaseArgumentOffset(llvm::ArrayRef<mlir::Attribute> cases,
1955                                     unsigned dest) {
1956   unsigned o = 0;
1957   for (unsigned i = 0; i < dest; ++i) {
1958     auto &attr = cases[i];
1959     if (!attr.dyn_cast_or_null<mlir::UnitAttr>()) {
1960       ++o;
1961       if (attr.dyn_cast_or_null<ClosedIntervalAttr>())
1962         ++o;
1963     }
1964   }
1965   return o;
1966 }
1967 
1968 mlir::ParseResult fir::parseSelector(mlir::OpAsmParser &parser,
1969                                      mlir::OperationState &result,
1970                                      mlir::OpAsmParser::OperandType &selector,
1971                                      mlir::Type &type) {
1972   if (parser.parseOperand(selector) || parser.parseColonType(type) ||
1973       parser.resolveOperand(selector, type, result.operands) ||
1974       parser.parseLSquare())
1975     return mlir::failure();
1976   return mlir::success();
1977 }
1978 
1979 /// Generic pretty-printer of a binary operation
1980 static void printBinaryOp(Operation *op, OpAsmPrinter &p) {
1981   assert(op->getNumOperands() == 2 && "binary op must have two operands");
1982   assert(op->getNumResults() == 1 && "binary op must have one result");
1983 
1984   p << ' ' << op->getOperand(0) << ", " << op->getOperand(1);
1985   p.printOptionalAttrDict(op->getAttrs());
1986   p << " : " << op->getResult(0).getType();
1987 }
1988 
1989 /// Generic pretty-printer of an unary operation
1990 static void printUnaryOp(Operation *op, OpAsmPrinter &p) {
1991   assert(op->getNumOperands() == 1 && "unary op must have one operand");
1992   assert(op->getNumResults() == 1 && "unary op must have one result");
1993 
1994   p << ' ' << op->getOperand(0);
1995   p.printOptionalAttrDict(op->getAttrs());
1996   p << " : " << op->getResult(0).getType();
1997 }
1998 
1999 bool fir::isReferenceLike(mlir::Type type) {
2000   return type.isa<fir::ReferenceType>() || type.isa<fir::HeapType>() ||
2001          type.isa<fir::PointerType>();
2002 }
2003 
2004 mlir::FuncOp fir::createFuncOp(mlir::Location loc, mlir::ModuleOp module,
2005                                StringRef name, mlir::FunctionType type,
2006                                llvm::ArrayRef<mlir::NamedAttribute> attrs) {
2007   if (auto f = module.lookupSymbol<mlir::FuncOp>(name))
2008     return f;
2009   mlir::OpBuilder modBuilder(module.getBodyRegion());
2010   modBuilder.setInsertionPoint(module.getBody()->getTerminator());
2011   auto result = modBuilder.create<mlir::FuncOp>(loc, name, type, attrs);
2012   result.setVisibility(mlir::SymbolTable::Visibility::Private);
2013   return result;
2014 }
2015 
2016 fir::GlobalOp fir::createGlobalOp(mlir::Location loc, mlir::ModuleOp module,
2017                                   StringRef name, mlir::Type type,
2018                                   llvm::ArrayRef<mlir::NamedAttribute> attrs) {
2019   if (auto g = module.lookupSymbol<fir::GlobalOp>(name))
2020     return g;
2021   mlir::OpBuilder modBuilder(module.getBodyRegion());
2022   auto result = modBuilder.create<fir::GlobalOp>(loc, name, type, attrs);
2023   result.setVisibility(mlir::SymbolTable::Visibility::Private);
2024   return result;
2025 }
2026 
2027 bool fir::valueHasFirAttribute(mlir::Value value,
2028                                llvm::StringRef attributeName) {
2029   // If this is a fir.box that was loaded, the fir attributes will be on the
2030   // related fir.ref<fir.box> creation.
2031   if (value.getType().isa<fir::BoxType>())
2032     if (auto definingOp = value.getDefiningOp())
2033       if (auto loadOp = mlir::dyn_cast<fir::LoadOp>(definingOp))
2034         value = loadOp.memref();
2035   // If this is a function argument, look in the argument attributes.
2036   if (auto blockArg = value.dyn_cast<mlir::BlockArgument>()) {
2037     if (blockArg.getOwner() && blockArg.getOwner()->isEntryBlock())
2038       if (auto funcOp =
2039               mlir::dyn_cast<mlir::FuncOp>(blockArg.getOwner()->getParentOp()))
2040         if (funcOp.getArgAttr(blockArg.getArgNumber(), attributeName))
2041           return true;
2042     return false;
2043   }
2044 
2045   if (auto definingOp = value.getDefiningOp()) {
2046     // If this is an allocated value, look at the allocation attributes.
2047     if (mlir::isa<fir::AllocMemOp>(definingOp) ||
2048         mlir::isa<AllocaOp>(definingOp))
2049       return definingOp->hasAttr(attributeName);
2050     // If this is an imported global, look at AddrOfOp and GlobalOp attributes.
2051     // Both operations are looked at because use/host associated variable (the
2052     // AddrOfOp) can have ASYNCHRONOUS/VOLATILE attributes even if the ultimate
2053     // entity (the globalOp) does not have them.
2054     if (auto addressOfOp = mlir::dyn_cast<fir::AddrOfOp>(definingOp)) {
2055       if (addressOfOp->hasAttr(attributeName))
2056         return true;
2057       if (auto module = definingOp->getParentOfType<mlir::ModuleOp>())
2058         if (auto globalOp =
2059                 module.lookupSymbol<fir::GlobalOp>(addressOfOp.symbol()))
2060           return globalOp->hasAttr(attributeName);
2061     }
2062   }
2063   // TODO: Construct associated entities attributes. Decide where the fir
2064   // attributes must be placed/looked for in this case.
2065   return false;
2066 }
2067 
2068 // Tablegen operators
2069 
2070 #define GET_OP_CLASSES
2071 #include "flang/Optimizer/Dialect/FIROps.cpp.inc"
2072