1 //===- LLVMDialect.cpp - LLVM IR Ops and Dialect registration -------------===//
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 // This file defines the types and operation details for the LLVM IR dialect in
10 // MLIR, and the LLVM IR dialect.  It also registers the dialect.
11 //
12 //===----------------------------------------------------------------------===//
13 #include "mlir/Dialect/LLVMIR/LLVMDialect.h"
14 #include "TypeDetail.h"
15 #include "mlir/Dialect/LLVMIR/LLVMTypes.h"
16 #include "mlir/IR/Builders.h"
17 #include "mlir/IR/BuiltinOps.h"
18 #include "mlir/IR/BuiltinTypes.h"
19 #include "mlir/IR/DialectImplementation.h"
20 #include "mlir/IR/FunctionImplementation.h"
21 #include "mlir/IR/MLIRContext.h"
22 
23 #include "llvm/ADT/StringSwitch.h"
24 #include "llvm/ADT/TypeSwitch.h"
25 #include "llvm/AsmParser/Parser.h"
26 #include "llvm/Bitcode/BitcodeReader.h"
27 #include "llvm/Bitcode/BitcodeWriter.h"
28 #include "llvm/IR/Attributes.h"
29 #include "llvm/IR/Function.h"
30 #include "llvm/IR/Type.h"
31 #include "llvm/Support/Mutex.h"
32 #include "llvm/Support/SourceMgr.h"
33 
34 #include <iostream>
35 
36 using namespace mlir;
37 using namespace mlir::LLVM;
38 
39 static constexpr const char kVolatileAttrName[] = "volatile_";
40 static constexpr const char kNonTemporalAttrName[] = "nontemporal";
41 
42 #include "mlir/Dialect/LLVMIR/LLVMOpsEnums.cpp.inc"
43 #include "mlir/Dialect/LLVMIR/LLVMOpsInterfaces.cpp.inc"
44 #define GET_ATTRDEF_CLASSES
45 #include "mlir/Dialect/LLVMIR/LLVMOpsAttrDefs.cpp.inc"
46 
47 static auto processFMFAttr(ArrayRef<NamedAttribute> attrs) {
48   SmallVector<NamedAttribute, 8> filteredAttrs(
49       llvm::make_filter_range(attrs, [&](NamedAttribute attr) {
50         if (attr.first == "fastmathFlags") {
51           auto defAttr = FMFAttr::get(attr.second.getContext(), {});
52           return defAttr != attr.second;
53         }
54         return true;
55       }));
56   return filteredAttrs;
57 }
58 
59 static ParseResult parseLLVMOpAttrs(OpAsmParser &parser,
60                                     NamedAttrList &result) {
61   return parser.parseOptionalAttrDict(result);
62 }
63 
64 static void printLLVMOpAttrs(OpAsmPrinter &printer, Operation *op,
65                              DictionaryAttr attrs) {
66   printer.printOptionalAttrDict(processFMFAttr(attrs.getValue()));
67 }
68 
69 //===----------------------------------------------------------------------===//
70 // Printing/parsing for LLVM::CmpOp.
71 //===----------------------------------------------------------------------===//
72 static void printICmpOp(OpAsmPrinter &p, ICmpOp &op) {
73   p << op.getOperationName() << " \"" << stringifyICmpPredicate(op.predicate())
74     << "\" " << op.getOperand(0) << ", " << op.getOperand(1);
75   p.printOptionalAttrDict(op->getAttrs(), {"predicate"});
76   p << " : " << op.lhs().getType();
77 }
78 
79 static void printFCmpOp(OpAsmPrinter &p, FCmpOp &op) {
80   p << op.getOperationName() << " \"" << stringifyFCmpPredicate(op.predicate())
81     << "\" " << op.getOperand(0) << ", " << op.getOperand(1);
82   p.printOptionalAttrDict(processFMFAttr(op->getAttrs()), {"predicate"});
83   p << " : " << op.lhs().getType();
84 }
85 
86 // <operation> ::= `llvm.icmp` string-literal ssa-use `,` ssa-use
87 //                 attribute-dict? `:` type
88 // <operation> ::= `llvm.fcmp` string-literal ssa-use `,` ssa-use
89 //                 attribute-dict? `:` type
90 template <typename CmpPredicateType>
91 static ParseResult parseCmpOp(OpAsmParser &parser, OperationState &result) {
92   Builder &builder = parser.getBuilder();
93 
94   StringAttr predicateAttr;
95   OpAsmParser::OperandType lhs, rhs;
96   Type type;
97   llvm::SMLoc predicateLoc, trailingTypeLoc;
98   if (parser.getCurrentLocation(&predicateLoc) ||
99       parser.parseAttribute(predicateAttr, "predicate", result.attributes) ||
100       parser.parseOperand(lhs) || parser.parseComma() ||
101       parser.parseOperand(rhs) ||
102       parser.parseOptionalAttrDict(result.attributes) || parser.parseColon() ||
103       parser.getCurrentLocation(&trailingTypeLoc) || parser.parseType(type) ||
104       parser.resolveOperand(lhs, type, result.operands) ||
105       parser.resolveOperand(rhs, type, result.operands))
106     return failure();
107 
108   // Replace the string attribute `predicate` with an integer attribute.
109   int64_t predicateValue = 0;
110   if (std::is_same<CmpPredicateType, ICmpPredicate>()) {
111     Optional<ICmpPredicate> predicate =
112         symbolizeICmpPredicate(predicateAttr.getValue());
113     if (!predicate)
114       return parser.emitError(predicateLoc)
115              << "'" << predicateAttr.getValue()
116              << "' is an incorrect value of the 'predicate' attribute";
117     predicateValue = static_cast<int64_t>(predicate.getValue());
118   } else {
119     Optional<FCmpPredicate> predicate =
120         symbolizeFCmpPredicate(predicateAttr.getValue());
121     if (!predicate)
122       return parser.emitError(predicateLoc)
123              << "'" << predicateAttr.getValue()
124              << "' is an incorrect value of the 'predicate' attribute";
125     predicateValue = static_cast<int64_t>(predicate.getValue());
126   }
127 
128   result.attributes.set("predicate",
129                         parser.getBuilder().getI64IntegerAttr(predicateValue));
130 
131   // The result type is either i1 or a vector type <? x i1> if the inputs are
132   // vectors.
133   Type resultType = IntegerType::get(builder.getContext(), 1);
134   if (!isCompatibleType(type))
135     return parser.emitError(trailingTypeLoc,
136                             "expected LLVM dialect-compatible type");
137   if (LLVM::isCompatibleVectorType(type)) {
138     if (type.isa<LLVM::LLVMScalableVectorType>()) {
139       resultType = LLVM::LLVMScalableVectorType::get(
140           resultType, LLVM::getVectorNumElements(type).getKnownMinValue());
141     } else {
142       resultType = LLVM::getFixedVectorType(
143           resultType, LLVM::getVectorNumElements(type).getFixedValue());
144     }
145   }
146 
147   result.addTypes({resultType});
148   return success();
149 }
150 
151 //===----------------------------------------------------------------------===//
152 // Printing/parsing for LLVM::AllocaOp.
153 //===----------------------------------------------------------------------===//
154 
155 static void printAllocaOp(OpAsmPrinter &p, AllocaOp &op) {
156   auto elemTy = op.getType().cast<LLVM::LLVMPointerType>().getElementType();
157 
158   auto funcTy = FunctionType::get(op.getContext(), {op.arraySize().getType()},
159                                   {op.getType()});
160 
161   p << op.getOperationName() << ' ' << op.arraySize() << " x " << elemTy;
162   if (op.alignment().hasValue() && *op.alignment() != 0)
163     p.printOptionalAttrDict(op->getAttrs());
164   else
165     p.printOptionalAttrDict(op->getAttrs(), {"alignment"});
166   p << " : " << funcTy;
167 }
168 
169 // <operation> ::= `llvm.alloca` ssa-use `x` type attribute-dict?
170 //                 `:` type `,` type
171 static ParseResult parseAllocaOp(OpAsmParser &parser, OperationState &result) {
172   OpAsmParser::OperandType arraySize;
173   Type type, elemType;
174   llvm::SMLoc trailingTypeLoc;
175   if (parser.parseOperand(arraySize) || parser.parseKeyword("x") ||
176       parser.parseType(elemType) ||
177       parser.parseOptionalAttrDict(result.attributes) || parser.parseColon() ||
178       parser.getCurrentLocation(&trailingTypeLoc) || parser.parseType(type))
179     return failure();
180 
181   Optional<NamedAttribute> alignmentAttr =
182       result.attributes.getNamed("alignment");
183   if (alignmentAttr.hasValue()) {
184     auto alignmentInt = alignmentAttr.getValue().second.dyn_cast<IntegerAttr>();
185     if (!alignmentInt)
186       return parser.emitError(parser.getNameLoc(),
187                               "expected integer alignment");
188     if (alignmentInt.getValue().isNullValue())
189       result.attributes.erase("alignment");
190   }
191 
192   // Extract the result type from the trailing function type.
193   auto funcType = type.dyn_cast<FunctionType>();
194   if (!funcType || funcType.getNumInputs() != 1 ||
195       funcType.getNumResults() != 1)
196     return parser.emitError(
197         trailingTypeLoc,
198         "expected trailing function type with one argument and one result");
199 
200   if (parser.resolveOperand(arraySize, funcType.getInput(0), result.operands))
201     return failure();
202 
203   result.addTypes({funcType.getResult(0)});
204   return success();
205 }
206 
207 //===----------------------------------------------------------------------===//
208 // LLVM::BrOp
209 //===----------------------------------------------------------------------===//
210 
211 Optional<MutableOperandRange>
212 BrOp::getMutableSuccessorOperands(unsigned index) {
213   assert(index == 0 && "invalid successor index");
214   return destOperandsMutable();
215 }
216 
217 //===----------------------------------------------------------------------===//
218 // LLVM::CondBrOp
219 //===----------------------------------------------------------------------===//
220 
221 Optional<MutableOperandRange>
222 CondBrOp::getMutableSuccessorOperands(unsigned index) {
223   assert(index < getNumSuccessors() && "invalid successor index");
224   return index == 0 ? trueDestOperandsMutable() : falseDestOperandsMutable();
225 }
226 
227 //===----------------------------------------------------------------------===//
228 // LLVM::SwitchOp
229 //===----------------------------------------------------------------------===//
230 
231 void SwitchOp::build(OpBuilder &builder, OperationState &result, Value value,
232                      Block *defaultDestination, ValueRange defaultOperands,
233                      ArrayRef<int32_t> caseValues, BlockRange caseDestinations,
234                      ArrayRef<ValueRange> caseOperands,
235                      ArrayRef<int32_t> branchWeights) {
236   SmallVector<Value> flattenedCaseOperands;
237   SmallVector<int32_t> caseOperandOffsets;
238   int32_t offset = 0;
239   for (ValueRange operands : caseOperands) {
240     flattenedCaseOperands.append(operands.begin(), operands.end());
241     caseOperandOffsets.push_back(offset);
242     offset += operands.size();
243   }
244   ElementsAttr caseValuesAttr;
245   if (!caseValues.empty())
246     caseValuesAttr = builder.getI32VectorAttr(caseValues);
247   ElementsAttr caseOperandOffsetsAttr;
248   if (!caseOperandOffsets.empty())
249     caseOperandOffsetsAttr = builder.getI32VectorAttr(caseOperandOffsets);
250 
251   ElementsAttr weightsAttr;
252   if (!branchWeights.empty())
253     weightsAttr = builder.getI32VectorAttr(llvm::to_vector<4>(branchWeights));
254 
255   build(builder, result, value, defaultOperands, flattenedCaseOperands,
256         caseValuesAttr, caseOperandOffsetsAttr, weightsAttr, defaultDestination,
257         caseDestinations);
258 }
259 
260 /// <cases> ::= integer `:` bb-id (`(` ssa-use-and-type-list `)`)?
261 ///             ( `,` integer `:` bb-id (`(` ssa-use-and-type-list `)`)? )?
262 static ParseResult
263 parseSwitchOpCases(OpAsmParser &parser, ElementsAttr &caseValues,
264                    SmallVectorImpl<Block *> &caseDestinations,
265                    SmallVectorImpl<OpAsmParser::OperandType> &caseOperands,
266                    SmallVectorImpl<Type> &caseOperandTypes,
267                    ElementsAttr &caseOperandOffsets) {
268   SmallVector<int32_t> values;
269   SmallVector<int32_t> offsets;
270   int32_t value, offset = 0;
271   do {
272     OptionalParseResult integerParseResult = parser.parseOptionalInteger(value);
273     if (values.empty() && !integerParseResult.hasValue())
274       return success();
275 
276     if (!integerParseResult.hasValue() || integerParseResult.getValue())
277       return failure();
278     values.push_back(value);
279 
280     Block *destination;
281     SmallVector<OpAsmParser::OperandType> operands;
282     if (parser.parseColon() || parser.parseSuccessor(destination))
283       return failure();
284     if (!parser.parseOptionalLParen()) {
285       if (parser.parseRegionArgumentList(operands) ||
286           parser.parseColonTypeList(caseOperandTypes) || parser.parseRParen())
287         return failure();
288     }
289     caseDestinations.push_back(destination);
290     caseOperands.append(operands.begin(), operands.end());
291     offsets.push_back(offset);
292     offset += operands.size();
293   } while (!parser.parseOptionalComma());
294 
295   Builder &builder = parser.getBuilder();
296   caseValues = builder.getI32VectorAttr(values);
297   caseOperandOffsets = builder.getI32VectorAttr(offsets);
298 
299   return success();
300 }
301 
302 static void printSwitchOpCases(OpAsmPrinter &p, SwitchOp op,
303                                ElementsAttr caseValues,
304                                SuccessorRange caseDestinations,
305                                OperandRange caseOperands,
306                                TypeRange caseOperandTypes,
307                                ElementsAttr caseOperandOffsets) {
308   if (!caseValues)
309     return;
310 
311   size_t index = 0;
312   llvm::interleave(
313       llvm::zip(caseValues.cast<DenseIntElementsAttr>(), caseDestinations),
314       [&](auto i) {
315         p << "  ";
316         p << std::get<0>(i).getLimitedValue();
317         p << ": ";
318         p.printSuccessorAndUseList(std::get<1>(i), op.getCaseOperands(index++));
319       },
320       [&] {
321         p << ',';
322         p.printNewline();
323       });
324   p.printNewline();
325 }
326 
327 static LogicalResult verify(SwitchOp op) {
328   if ((!op.case_values() && !op.caseDestinations().empty()) ||
329       (op.case_values() &&
330        op.case_values()->size() !=
331            static_cast<int64_t>(op.caseDestinations().size())))
332     return op.emitOpError("expects number of case values to match number of "
333                           "case destinations");
334   if (op.branch_weights() &&
335       op.branch_weights()->size() != op.getNumSuccessors())
336     return op.emitError("expects number of branch weights to match number of "
337                         "successors: ")
338            << op.branch_weights()->size() << " vs " << op.getNumSuccessors();
339   return success();
340 }
341 
342 OperandRange SwitchOp::getCaseOperands(unsigned index) {
343   return getCaseOperandsMutable(index);
344 }
345 
346 MutableOperandRange SwitchOp::getCaseOperandsMutable(unsigned index) {
347   MutableOperandRange caseOperands = caseOperandsMutable();
348   if (!case_operand_offsets()) {
349     assert(caseOperands.size() == 0 &&
350            "non-empty case operands must have offsets");
351     return caseOperands;
352   }
353 
354   ElementsAttr offsets = case_operand_offsets().getValue();
355   assert(index < offsets.size() && "invalid case operand offset index");
356 
357   int64_t begin = offsets.getValue(index).cast<IntegerAttr>().getInt();
358   int64_t end = index + 1 == offsets.size()
359                     ? caseOperands.size()
360                     : offsets.getValue(index + 1).cast<IntegerAttr>().getInt();
361   return caseOperandsMutable().slice(begin, end - begin);
362 }
363 
364 Optional<MutableOperandRange>
365 SwitchOp::getMutableSuccessorOperands(unsigned index) {
366   assert(index < getNumSuccessors() && "invalid successor index");
367   return index == 0 ? defaultOperandsMutable()
368                     : getCaseOperandsMutable(index - 1);
369 }
370 
371 //===----------------------------------------------------------------------===//
372 // Builder, printer and parser for for LLVM::LoadOp.
373 //===----------------------------------------------------------------------===//
374 
375 static LogicalResult verifyAccessGroups(Operation *op) {
376   if (Attribute attribute =
377           op->getAttr(LLVMDialect::getAccessGroupsAttrName())) {
378     // The attribute is already verified to be a symbol ref array attribute via
379     // a constraint in the operation definition.
380     for (SymbolRefAttr accessGroupRef :
381          attribute.cast<ArrayAttr>().getAsRange<SymbolRefAttr>()) {
382       StringRef metadataName = accessGroupRef.getRootReference();
383       auto metadataOp = SymbolTable::lookupNearestSymbolFrom<LLVM::MetadataOp>(
384           op->getParentOp(), metadataName);
385       if (!metadataOp)
386         return op->emitOpError() << "expected '" << accessGroupRef
387                                  << "' to reference a metadata op";
388       StringRef accessGroupName = accessGroupRef.getLeafReference();
389       Operation *accessGroupOp =
390           SymbolTable::lookupNearestSymbolFrom(metadataOp, accessGroupName);
391       if (!accessGroupOp)
392         return op->emitOpError() << "expected '" << accessGroupRef
393                                  << "' to reference an access_group op";
394     }
395   }
396   return success();
397 }
398 
399 static LogicalResult verify(LoadOp op) {
400   return verifyAccessGroups(op.getOperation());
401 }
402 
403 void LoadOp::build(OpBuilder &builder, OperationState &result, Type t,
404                    Value addr, unsigned alignment, bool isVolatile,
405                    bool isNonTemporal) {
406   result.addOperands(addr);
407   result.addTypes(t);
408   if (isVolatile)
409     result.addAttribute(kVolatileAttrName, builder.getUnitAttr());
410   if (isNonTemporal)
411     result.addAttribute(kNonTemporalAttrName, builder.getUnitAttr());
412   if (alignment != 0)
413     result.addAttribute("alignment", builder.getI64IntegerAttr(alignment));
414 }
415 
416 static void printLoadOp(OpAsmPrinter &p, LoadOp &op) {
417   p << op.getOperationName() << ' ';
418   if (op.volatile_())
419     p << "volatile ";
420   p << op.addr();
421   p.printOptionalAttrDict(op->getAttrs(), {kVolatileAttrName});
422   p << " : " << op.addr().getType();
423 }
424 
425 // Extract the pointee type from the LLVM pointer type wrapped in MLIR.  Return
426 // the resulting type wrapped in MLIR, or nullptr on error.
427 static Type getLoadStoreElementType(OpAsmParser &parser, Type type,
428                                     llvm::SMLoc trailingTypeLoc) {
429   auto llvmTy = type.dyn_cast<LLVM::LLVMPointerType>();
430   if (!llvmTy)
431     return parser.emitError(trailingTypeLoc, "expected LLVM pointer type"),
432            nullptr;
433   return llvmTy.getElementType();
434 }
435 
436 // <operation> ::= `llvm.load` `volatile` ssa-use attribute-dict? `:` type
437 static ParseResult parseLoadOp(OpAsmParser &parser, OperationState &result) {
438   OpAsmParser::OperandType addr;
439   Type type;
440   llvm::SMLoc trailingTypeLoc;
441 
442   if (succeeded(parser.parseOptionalKeyword("volatile")))
443     result.addAttribute(kVolatileAttrName, parser.getBuilder().getUnitAttr());
444 
445   if (parser.parseOperand(addr) ||
446       parser.parseOptionalAttrDict(result.attributes) || parser.parseColon() ||
447       parser.getCurrentLocation(&trailingTypeLoc) || parser.parseType(type) ||
448       parser.resolveOperand(addr, type, result.operands))
449     return failure();
450 
451   Type elemTy = getLoadStoreElementType(parser, type, trailingTypeLoc);
452 
453   result.addTypes(elemTy);
454   return success();
455 }
456 
457 //===----------------------------------------------------------------------===//
458 // Builder, printer and parser for LLVM::StoreOp.
459 //===----------------------------------------------------------------------===//
460 
461 static LogicalResult verify(StoreOp op) {
462   return verifyAccessGroups(op.getOperation());
463 }
464 
465 void StoreOp::build(OpBuilder &builder, OperationState &result, Value value,
466                     Value addr, unsigned alignment, bool isVolatile,
467                     bool isNonTemporal) {
468   result.addOperands({value, addr});
469   result.addTypes({});
470   if (isVolatile)
471     result.addAttribute(kVolatileAttrName, builder.getUnitAttr());
472   if (isNonTemporal)
473     result.addAttribute(kNonTemporalAttrName, builder.getUnitAttr());
474   if (alignment != 0)
475     result.addAttribute("alignment", builder.getI64IntegerAttr(alignment));
476 }
477 
478 static void printStoreOp(OpAsmPrinter &p, StoreOp &op) {
479   p << op.getOperationName() << ' ';
480   if (op.volatile_())
481     p << "volatile ";
482   p << op.value() << ", " << op.addr();
483   p.printOptionalAttrDict(op->getAttrs(), {kVolatileAttrName});
484   p << " : " << op.addr().getType();
485 }
486 
487 // <operation> ::= `llvm.store` `volatile` ssa-use `,` ssa-use
488 //                 attribute-dict? `:` type
489 static ParseResult parseStoreOp(OpAsmParser &parser, OperationState &result) {
490   OpAsmParser::OperandType addr, value;
491   Type type;
492   llvm::SMLoc trailingTypeLoc;
493 
494   if (succeeded(parser.parseOptionalKeyword("volatile")))
495     result.addAttribute(kVolatileAttrName, parser.getBuilder().getUnitAttr());
496 
497   if (parser.parseOperand(value) || parser.parseComma() ||
498       parser.parseOperand(addr) ||
499       parser.parseOptionalAttrDict(result.attributes) || parser.parseColon() ||
500       parser.getCurrentLocation(&trailingTypeLoc) || parser.parseType(type))
501     return failure();
502 
503   Type elemTy = getLoadStoreElementType(parser, type, trailingTypeLoc);
504   if (!elemTy)
505     return failure();
506 
507   if (parser.resolveOperand(value, elemTy, result.operands) ||
508       parser.resolveOperand(addr, type, result.operands))
509     return failure();
510 
511   return success();
512 }
513 
514 ///===---------------------------------------------------------------------===//
515 /// LLVM::InvokeOp
516 ///===---------------------------------------------------------------------===//
517 
518 Optional<MutableOperandRange>
519 InvokeOp::getMutableSuccessorOperands(unsigned index) {
520   assert(index < getNumSuccessors() && "invalid successor index");
521   return index == 0 ? normalDestOperandsMutable() : unwindDestOperandsMutable();
522 }
523 
524 static LogicalResult verify(InvokeOp op) {
525   if (op.getNumResults() > 1)
526     return op.emitOpError("must have 0 or 1 result");
527 
528   Block *unwindDest = op.unwindDest();
529   if (unwindDest->empty())
530     return op.emitError(
531         "must have at least one operation in unwind destination");
532 
533   // In unwind destination, first operation must be LandingpadOp
534   if (!isa<LandingpadOp>(unwindDest->front()))
535     return op.emitError("first operation in unwind destination should be a "
536                         "llvm.landingpad operation");
537 
538   return success();
539 }
540 
541 static void printInvokeOp(OpAsmPrinter &p, InvokeOp op) {
542   auto callee = op.callee();
543   bool isDirect = callee.hasValue();
544 
545   p << op.getOperationName() << ' ';
546 
547   // Either function name or pointer
548   if (isDirect)
549     p.printSymbolName(callee.getValue());
550   else
551     p << op.getOperand(0);
552 
553   p << '(' << op.getOperands().drop_front(isDirect ? 0 : 1) << ')';
554   p << " to ";
555   p.printSuccessorAndUseList(op.normalDest(), op.normalDestOperands());
556   p << " unwind ";
557   p.printSuccessorAndUseList(op.unwindDest(), op.unwindDestOperands());
558 
559   p.printOptionalAttrDict(op->getAttrs(),
560                           {InvokeOp::getOperandSegmentSizeAttr(), "callee"});
561   p << " : ";
562   p.printFunctionalType(
563       llvm::drop_begin(op.getOperandTypes(), isDirect ? 0 : 1),
564       op.getResultTypes());
565 }
566 
567 /// <operation> ::= `llvm.invoke` (function-id | ssa-use) `(` ssa-use-list `)`
568 ///                  `to` bb-id (`[` ssa-use-and-type-list `]`)?
569 ///                  `unwind` bb-id (`[` ssa-use-and-type-list `]`)?
570 ///                  attribute-dict? `:` function-type
571 static ParseResult parseInvokeOp(OpAsmParser &parser, OperationState &result) {
572   SmallVector<OpAsmParser::OperandType, 8> operands;
573   FunctionType funcType;
574   SymbolRefAttr funcAttr;
575   llvm::SMLoc trailingTypeLoc;
576   Block *normalDest, *unwindDest;
577   SmallVector<Value, 4> normalOperands, unwindOperands;
578   Builder &builder = parser.getBuilder();
579 
580   // Parse an operand list that will, in practice, contain 0 or 1 operand.  In
581   // case of an indirect call, there will be 1 operand before `(`.  In case of a
582   // direct call, there will be no operands and the parser will stop at the
583   // function identifier without complaining.
584   if (parser.parseOperandList(operands))
585     return failure();
586   bool isDirect = operands.empty();
587 
588   // Optionally parse a function identifier.
589   if (isDirect && parser.parseAttribute(funcAttr, "callee", result.attributes))
590     return failure();
591 
592   if (parser.parseOperandList(operands, OpAsmParser::Delimiter::Paren) ||
593       parser.parseKeyword("to") ||
594       parser.parseSuccessorAndUseList(normalDest, normalOperands) ||
595       parser.parseKeyword("unwind") ||
596       parser.parseSuccessorAndUseList(unwindDest, unwindOperands) ||
597       parser.parseOptionalAttrDict(result.attributes) || parser.parseColon() ||
598       parser.getCurrentLocation(&trailingTypeLoc) || parser.parseType(funcType))
599     return failure();
600 
601   if (isDirect) {
602     // Make sure types match.
603     if (parser.resolveOperands(operands, funcType.getInputs(),
604                                parser.getNameLoc(), result.operands))
605       return failure();
606     result.addTypes(funcType.getResults());
607   } else {
608     // Construct the LLVM IR Dialect function type that the first operand
609     // should match.
610     if (funcType.getNumResults() > 1)
611       return parser.emitError(trailingTypeLoc,
612                               "expected function with 0 or 1 result");
613 
614     Type llvmResultType;
615     if (funcType.getNumResults() == 0) {
616       llvmResultType = LLVM::LLVMVoidType::get(builder.getContext());
617     } else {
618       llvmResultType = funcType.getResult(0);
619       if (!isCompatibleType(llvmResultType))
620         return parser.emitError(trailingTypeLoc,
621                                 "expected result to have LLVM type");
622     }
623 
624     SmallVector<Type, 8> argTypes;
625     argTypes.reserve(funcType.getNumInputs());
626     for (Type ty : funcType.getInputs()) {
627       if (isCompatibleType(ty))
628         argTypes.push_back(ty);
629       else
630         return parser.emitError(trailingTypeLoc,
631                                 "expected LLVM types as inputs");
632     }
633 
634     auto llvmFuncType = LLVM::LLVMFunctionType::get(llvmResultType, argTypes);
635     auto wrappedFuncType = LLVM::LLVMPointerType::get(llvmFuncType);
636 
637     auto funcArguments = llvm::makeArrayRef(operands).drop_front();
638 
639     // Make sure that the first operand (indirect callee) matches the wrapped
640     // LLVM IR function type, and that the types of the other call operands
641     // match the types of the function arguments.
642     if (parser.resolveOperand(operands[0], wrappedFuncType, result.operands) ||
643         parser.resolveOperands(funcArguments, funcType.getInputs(),
644                                parser.getNameLoc(), result.operands))
645       return failure();
646 
647     result.addTypes(llvmResultType);
648   }
649   result.addSuccessors({normalDest, unwindDest});
650   result.addOperands(normalOperands);
651   result.addOperands(unwindOperands);
652 
653   result.addAttribute(
654       InvokeOp::getOperandSegmentSizeAttr(),
655       builder.getI32VectorAttr({static_cast<int32_t>(operands.size()),
656                                 static_cast<int32_t>(normalOperands.size()),
657                                 static_cast<int32_t>(unwindOperands.size())}));
658   return success();
659 }
660 
661 ///===----------------------------------------------------------------------===//
662 /// Verifying/Printing/Parsing for LLVM::LandingpadOp.
663 ///===----------------------------------------------------------------------===//
664 
665 static LogicalResult verify(LandingpadOp op) {
666   Value value;
667   if (LLVMFuncOp func = op->getParentOfType<LLVMFuncOp>()) {
668     if (!func.personality().hasValue())
669       return op.emitError(
670           "llvm.landingpad needs to be in a function with a personality");
671   }
672 
673   if (!op.cleanup() && op.getOperands().empty())
674     return op.emitError("landingpad instruction expects at least one clause or "
675                         "cleanup attribute");
676 
677   for (unsigned idx = 0, ie = op.getNumOperands(); idx < ie; idx++) {
678     value = op.getOperand(idx);
679     bool isFilter = value.getType().isa<LLVMArrayType>();
680     if (isFilter) {
681       // FIXME: Verify filter clauses when arrays are appropriately handled
682     } else {
683       // catch - global addresses only.
684       // Bitcast ops should have global addresses as their args.
685       if (auto bcOp = value.getDefiningOp<BitcastOp>()) {
686         if (auto addrOp = bcOp.arg().getDefiningOp<AddressOfOp>())
687           continue;
688         return op.emitError("constant clauses expected")
689                    .attachNote(bcOp.getLoc())
690                << "global addresses expected as operand to "
691                   "bitcast used in clauses for landingpad";
692       }
693       // NullOp and AddressOfOp allowed
694       if (value.getDefiningOp<NullOp>())
695         continue;
696       if (value.getDefiningOp<AddressOfOp>())
697         continue;
698       return op.emitError("clause #")
699              << idx << " is not a known constant - null, addressof, bitcast";
700     }
701   }
702   return success();
703 }
704 
705 static void printLandingpadOp(OpAsmPrinter &p, LandingpadOp &op) {
706   p << op.getOperationName() << (op.cleanup() ? " cleanup " : " ");
707 
708   // Clauses
709   for (auto value : op.getOperands()) {
710     // Similar to llvm - if clause is an array type then it is filter
711     // clause else catch clause
712     bool isArrayTy = value.getType().isa<LLVMArrayType>();
713     p << '(' << (isArrayTy ? "filter " : "catch ") << value << " : "
714       << value.getType() << ") ";
715   }
716 
717   p.printOptionalAttrDict(op->getAttrs(), {"cleanup"});
718 
719   p << ": " << op.getType();
720 }
721 
722 /// <operation> ::= `llvm.landingpad` `cleanup`?
723 ///                 ((`catch` | `filter`) operand-type ssa-use)* attribute-dict?
724 static ParseResult parseLandingpadOp(OpAsmParser &parser,
725                                      OperationState &result) {
726   // Check for cleanup
727   if (succeeded(parser.parseOptionalKeyword("cleanup")))
728     result.addAttribute("cleanup", parser.getBuilder().getUnitAttr());
729 
730   // Parse clauses with types
731   while (succeeded(parser.parseOptionalLParen()) &&
732          (succeeded(parser.parseOptionalKeyword("filter")) ||
733           succeeded(parser.parseOptionalKeyword("catch")))) {
734     OpAsmParser::OperandType operand;
735     Type ty;
736     if (parser.parseOperand(operand) || parser.parseColon() ||
737         parser.parseType(ty) ||
738         parser.resolveOperand(operand, ty, result.operands) ||
739         parser.parseRParen())
740       return failure();
741   }
742 
743   Type type;
744   if (parser.parseColon() || parser.parseType(type))
745     return failure();
746 
747   result.addTypes(type);
748   return success();
749 }
750 
751 //===----------------------------------------------------------------------===//
752 // Verifying/Printing/parsing for LLVM::CallOp.
753 //===----------------------------------------------------------------------===//
754 
755 static LogicalResult verify(CallOp &op) {
756   if (op.getNumResults() > 1)
757     return op.emitOpError("must have 0 or 1 result");
758 
759   // Type for the callee, we'll get it differently depending if it is a direct
760   // or indirect call.
761   Type fnType;
762 
763   bool isIndirect = false;
764 
765   // If this is an indirect call, the callee attribute is missing.
766   Optional<StringRef> calleeName = op.callee();
767   if (!calleeName) {
768     isIndirect = true;
769     if (!op.getNumOperands())
770       return op.emitOpError(
771           "must have either a `callee` attribute or at least an operand");
772     auto ptrType = op.getOperand(0).getType().dyn_cast<LLVMPointerType>();
773     if (!ptrType)
774       return op.emitOpError("indirect call expects a pointer as callee: ")
775              << ptrType;
776     fnType = ptrType.getElementType();
777   } else {
778     Operation *callee = SymbolTable::lookupNearestSymbolFrom(op, *calleeName);
779     if (!callee)
780       return op.emitOpError()
781              << "'" << *calleeName
782              << "' does not reference a symbol in the current scope";
783     auto fn = dyn_cast<LLVMFuncOp>(callee);
784     if (!fn)
785       return op.emitOpError() << "'" << *calleeName
786                               << "' does not reference a valid LLVM function";
787 
788     fnType = fn.getType();
789   }
790 
791   LLVMFunctionType funcType = fnType.dyn_cast<LLVMFunctionType>();
792   if (!funcType)
793     return op.emitOpError("callee does not have a functional type: ") << fnType;
794 
795   // Verify that the operand and result types match the callee.
796 
797   if (!funcType.isVarArg() &&
798       funcType.getNumParams() != (op.getNumOperands() - isIndirect))
799     return op.emitOpError()
800            << "incorrect number of operands ("
801            << (op.getNumOperands() - isIndirect)
802            << ") for callee (expecting: " << funcType.getNumParams() << ")";
803 
804   if (funcType.getNumParams() > (op.getNumOperands() - isIndirect))
805     return op.emitOpError() << "incorrect number of operands ("
806                             << (op.getNumOperands() - isIndirect)
807                             << ") for varargs callee (expecting at least: "
808                             << funcType.getNumParams() << ")";
809 
810   for (unsigned i = 0, e = funcType.getNumParams(); i != e; ++i)
811     if (op.getOperand(i + isIndirect).getType() != funcType.getParamType(i))
812       return op.emitOpError() << "operand type mismatch for operand " << i
813                               << ": " << op.getOperand(i + isIndirect).getType()
814                               << " != " << funcType.getParamType(i);
815 
816   if (op.getNumResults() &&
817       op.getResult(0).getType() != funcType.getReturnType())
818     return op.emitOpError()
819            << "result type mismatch: " << op.getResult(0).getType()
820            << " != " << funcType.getReturnType();
821 
822   return success();
823 }
824 
825 static void printCallOp(OpAsmPrinter &p, CallOp &op) {
826   auto callee = op.callee();
827   bool isDirect = callee.hasValue();
828 
829   // Print the direct callee if present as a function attribute, or an indirect
830   // callee (first operand) otherwise.
831   p << op.getOperationName() << ' ';
832   if (isDirect)
833     p.printSymbolName(callee.getValue());
834   else
835     p << op.getOperand(0);
836 
837   auto args = op.getOperands().drop_front(isDirect ? 0 : 1);
838   p << '(' << args << ')';
839   p.printOptionalAttrDict(processFMFAttr(op->getAttrs()), {"callee"});
840 
841   // Reconstruct the function MLIR function type from operand and result types.
842   p << " : "
843     << FunctionType::get(op.getContext(), args.getTypes(), op.getResultTypes());
844 }
845 
846 // <operation> ::= `llvm.call` (function-id | ssa-use) `(` ssa-use-list `)`
847 //                 attribute-dict? `:` function-type
848 static ParseResult parseCallOp(OpAsmParser &parser, OperationState &result) {
849   SmallVector<OpAsmParser::OperandType, 8> operands;
850   Type type;
851   SymbolRefAttr funcAttr;
852   llvm::SMLoc trailingTypeLoc;
853 
854   // Parse an operand list that will, in practice, contain 0 or 1 operand.  In
855   // case of an indirect call, there will be 1 operand before `(`.  In case of a
856   // direct call, there will be no operands and the parser will stop at the
857   // function identifier without complaining.
858   if (parser.parseOperandList(operands))
859     return failure();
860   bool isDirect = operands.empty();
861 
862   // Optionally parse a function identifier.
863   if (isDirect)
864     if (parser.parseAttribute(funcAttr, "callee", result.attributes))
865       return failure();
866 
867   if (parser.parseOperandList(operands, OpAsmParser::Delimiter::Paren) ||
868       parser.parseOptionalAttrDict(result.attributes) || parser.parseColon() ||
869       parser.getCurrentLocation(&trailingTypeLoc) || parser.parseType(type))
870     return failure();
871 
872   auto funcType = type.dyn_cast<FunctionType>();
873   if (!funcType)
874     return parser.emitError(trailingTypeLoc, "expected function type");
875   if (isDirect) {
876     // Make sure types match.
877     if (parser.resolveOperands(operands, funcType.getInputs(),
878                                parser.getNameLoc(), result.operands))
879       return failure();
880     result.addTypes(funcType.getResults());
881   } else {
882     // Construct the LLVM IR Dialect function type that the first operand
883     // should match.
884     if (funcType.getNumResults() > 1)
885       return parser.emitError(trailingTypeLoc,
886                               "expected function with 0 or 1 result");
887 
888     Builder &builder = parser.getBuilder();
889     Type llvmResultType;
890     if (funcType.getNumResults() == 0) {
891       llvmResultType = LLVM::LLVMVoidType::get(builder.getContext());
892     } else {
893       llvmResultType = funcType.getResult(0);
894       if (!isCompatibleType(llvmResultType))
895         return parser.emitError(trailingTypeLoc,
896                                 "expected result to have LLVM type");
897     }
898 
899     SmallVector<Type, 8> argTypes;
900     argTypes.reserve(funcType.getNumInputs());
901     for (int i = 0, e = funcType.getNumInputs(); i < e; ++i) {
902       auto argType = funcType.getInput(i);
903       if (!isCompatibleType(argType))
904         return parser.emitError(trailingTypeLoc,
905                                 "expected LLVM types as inputs");
906       argTypes.push_back(argType);
907     }
908     auto llvmFuncType = LLVM::LLVMFunctionType::get(llvmResultType, argTypes);
909     auto wrappedFuncType = LLVM::LLVMPointerType::get(llvmFuncType);
910 
911     auto funcArguments =
912         ArrayRef<OpAsmParser::OperandType>(operands).drop_front();
913 
914     // Make sure that the first operand (indirect callee) matches the wrapped
915     // LLVM IR function type, and that the types of the other call operands
916     // match the types of the function arguments.
917     if (parser.resolveOperand(operands[0], wrappedFuncType, result.operands) ||
918         parser.resolveOperands(funcArguments, funcType.getInputs(),
919                                parser.getNameLoc(), result.operands))
920       return failure();
921 
922     result.addTypes(llvmResultType);
923   }
924 
925   return success();
926 }
927 
928 //===----------------------------------------------------------------------===//
929 // Printing/parsing for LLVM::ExtractElementOp.
930 //===----------------------------------------------------------------------===//
931 // Expects vector to be of wrapped LLVM vector type and position to be of
932 // wrapped LLVM i32 type.
933 void LLVM::ExtractElementOp::build(OpBuilder &b, OperationState &result,
934                                    Value vector, Value position,
935                                    ArrayRef<NamedAttribute> attrs) {
936   auto vectorType = vector.getType();
937   auto llvmType = LLVM::getVectorElementType(vectorType);
938   build(b, result, llvmType, vector, position);
939   result.addAttributes(attrs);
940 }
941 
942 static void printExtractElementOp(OpAsmPrinter &p, ExtractElementOp &op) {
943   p << op.getOperationName() << ' ' << op.vector() << "[" << op.position()
944     << " : " << op.position().getType() << "]";
945   p.printOptionalAttrDict(op->getAttrs());
946   p << " : " << op.vector().getType();
947 }
948 
949 // <operation> ::= `llvm.extractelement` ssa-use `, ` ssa-use
950 //                 attribute-dict? `:` type
951 static ParseResult parseExtractElementOp(OpAsmParser &parser,
952                                          OperationState &result) {
953   llvm::SMLoc loc;
954   OpAsmParser::OperandType vector, position;
955   Type type, positionType;
956   if (parser.getCurrentLocation(&loc) || parser.parseOperand(vector) ||
957       parser.parseLSquare() || parser.parseOperand(position) ||
958       parser.parseColonType(positionType) || parser.parseRSquare() ||
959       parser.parseOptionalAttrDict(result.attributes) ||
960       parser.parseColonType(type) ||
961       parser.resolveOperand(vector, type, result.operands) ||
962       parser.resolveOperand(position, positionType, result.operands))
963     return failure();
964   if (!LLVM::isCompatibleVectorType(type))
965     return parser.emitError(
966         loc, "expected LLVM dialect-compatible vector type for operand #1");
967   result.addTypes(LLVM::getVectorElementType(type));
968   return success();
969 }
970 
971 //===----------------------------------------------------------------------===//
972 // Printing/parsing for LLVM::ExtractValueOp.
973 //===----------------------------------------------------------------------===//
974 
975 static void printExtractValueOp(OpAsmPrinter &p, ExtractValueOp &op) {
976   p << op.getOperationName() << ' ' << op.container() << op.position();
977   p.printOptionalAttrDict(op->getAttrs(), {"position"});
978   p << " : " << op.container().getType();
979 }
980 
981 // Extract the type at `position` in the wrapped LLVM IR aggregate type
982 // `containerType`.  Position is an integer array attribute where each value
983 // is a zero-based position of the element in the aggregate type.  Return the
984 // resulting type wrapped in MLIR, or nullptr on error.
985 static Type getInsertExtractValueElementType(OpAsmParser &parser,
986                                              Type containerType,
987                                              ArrayAttr positionAttr,
988                                              llvm::SMLoc attributeLoc,
989                                              llvm::SMLoc typeLoc) {
990   Type llvmType = containerType;
991   if (!isCompatibleType(containerType))
992     return parser.emitError(typeLoc, "expected LLVM IR Dialect type"), nullptr;
993 
994   // Infer the element type from the structure type: iteratively step inside the
995   // type by taking the element type, indexed by the position attribute for
996   // structures.  Check the position index before accessing, it is supposed to
997   // be in bounds.
998   for (Attribute subAttr : positionAttr) {
999     auto positionElementAttr = subAttr.dyn_cast<IntegerAttr>();
1000     if (!positionElementAttr)
1001       return parser.emitError(attributeLoc,
1002                               "expected an array of integer literals"),
1003              nullptr;
1004     int position = positionElementAttr.getInt();
1005     if (auto arrayType = llvmType.dyn_cast<LLVMArrayType>()) {
1006       if (position < 0 ||
1007           static_cast<unsigned>(position) >= arrayType.getNumElements())
1008         return parser.emitError(attributeLoc, "position out of bounds"),
1009                nullptr;
1010       llvmType = arrayType.getElementType();
1011     } else if (auto structType = llvmType.dyn_cast<LLVMStructType>()) {
1012       if (position < 0 ||
1013           static_cast<unsigned>(position) >= structType.getBody().size())
1014         return parser.emitError(attributeLoc, "position out of bounds"),
1015                nullptr;
1016       llvmType = structType.getBody()[position];
1017     } else {
1018       return parser.emitError(typeLoc, "expected LLVM IR structure/array type"),
1019              nullptr;
1020     }
1021   }
1022   return llvmType;
1023 }
1024 
1025 // <operation> ::= `llvm.extractvalue` ssa-use
1026 //                 `[` integer-literal (`,` integer-literal)* `]`
1027 //                 attribute-dict? `:` type
1028 static ParseResult parseExtractValueOp(OpAsmParser &parser,
1029                                        OperationState &result) {
1030   OpAsmParser::OperandType container;
1031   Type containerType;
1032   ArrayAttr positionAttr;
1033   llvm::SMLoc attributeLoc, trailingTypeLoc;
1034 
1035   if (parser.parseOperand(container) ||
1036       parser.getCurrentLocation(&attributeLoc) ||
1037       parser.parseAttribute(positionAttr, "position", result.attributes) ||
1038       parser.parseOptionalAttrDict(result.attributes) || parser.parseColon() ||
1039       parser.getCurrentLocation(&trailingTypeLoc) ||
1040       parser.parseType(containerType) ||
1041       parser.resolveOperand(container, containerType, result.operands))
1042     return failure();
1043 
1044   auto elementType = getInsertExtractValueElementType(
1045       parser, containerType, positionAttr, attributeLoc, trailingTypeLoc);
1046   if (!elementType)
1047     return failure();
1048 
1049   result.addTypes(elementType);
1050   return success();
1051 }
1052 
1053 //===----------------------------------------------------------------------===//
1054 // Printing/parsing for LLVM::InsertElementOp.
1055 //===----------------------------------------------------------------------===//
1056 
1057 static void printInsertElementOp(OpAsmPrinter &p, InsertElementOp &op) {
1058   p << op.getOperationName() << ' ' << op.value() << ", " << op.vector() << "["
1059     << op.position() << " : " << op.position().getType() << "]";
1060   p.printOptionalAttrDict(op->getAttrs());
1061   p << " : " << op.vector().getType();
1062 }
1063 
1064 // <operation> ::= `llvm.insertelement` ssa-use `,` ssa-use `,` ssa-use
1065 //                 attribute-dict? `:` type
1066 static ParseResult parseInsertElementOp(OpAsmParser &parser,
1067                                         OperationState &result) {
1068   llvm::SMLoc loc;
1069   OpAsmParser::OperandType vector, value, position;
1070   Type vectorType, positionType;
1071   if (parser.getCurrentLocation(&loc) || parser.parseOperand(value) ||
1072       parser.parseComma() || parser.parseOperand(vector) ||
1073       parser.parseLSquare() || parser.parseOperand(position) ||
1074       parser.parseColonType(positionType) || parser.parseRSquare() ||
1075       parser.parseOptionalAttrDict(result.attributes) ||
1076       parser.parseColonType(vectorType))
1077     return failure();
1078 
1079   if (!LLVM::isCompatibleVectorType(vectorType))
1080     return parser.emitError(
1081         loc, "expected LLVM dialect-compatible vector type for operand #1");
1082   Type valueType = LLVM::getVectorElementType(vectorType);
1083   if (!valueType)
1084     return failure();
1085 
1086   if (parser.resolveOperand(vector, vectorType, result.operands) ||
1087       parser.resolveOperand(value, valueType, result.operands) ||
1088       parser.resolveOperand(position, positionType, result.operands))
1089     return failure();
1090 
1091   result.addTypes(vectorType);
1092   return success();
1093 }
1094 
1095 //===----------------------------------------------------------------------===//
1096 // Printing/parsing for LLVM::InsertValueOp.
1097 //===----------------------------------------------------------------------===//
1098 
1099 static void printInsertValueOp(OpAsmPrinter &p, InsertValueOp &op) {
1100   p << op.getOperationName() << ' ' << op.value() << ", " << op.container()
1101     << op.position();
1102   p.printOptionalAttrDict(op->getAttrs(), {"position"});
1103   p << " : " << op.container().getType();
1104 }
1105 
1106 // <operation> ::= `llvm.insertvaluevalue` ssa-use `,` ssa-use
1107 //                 `[` integer-literal (`,` integer-literal)* `]`
1108 //                 attribute-dict? `:` type
1109 static ParseResult parseInsertValueOp(OpAsmParser &parser,
1110                                       OperationState &result) {
1111   OpAsmParser::OperandType container, value;
1112   Type containerType;
1113   ArrayAttr positionAttr;
1114   llvm::SMLoc attributeLoc, trailingTypeLoc;
1115 
1116   if (parser.parseOperand(value) || parser.parseComma() ||
1117       parser.parseOperand(container) ||
1118       parser.getCurrentLocation(&attributeLoc) ||
1119       parser.parseAttribute(positionAttr, "position", result.attributes) ||
1120       parser.parseOptionalAttrDict(result.attributes) || parser.parseColon() ||
1121       parser.getCurrentLocation(&trailingTypeLoc) ||
1122       parser.parseType(containerType))
1123     return failure();
1124 
1125   auto valueType = getInsertExtractValueElementType(
1126       parser, containerType, positionAttr, attributeLoc, trailingTypeLoc);
1127   if (!valueType)
1128     return failure();
1129 
1130   if (parser.resolveOperand(container, containerType, result.operands) ||
1131       parser.resolveOperand(value, valueType, result.operands))
1132     return failure();
1133 
1134   result.addTypes(containerType);
1135   return success();
1136 }
1137 
1138 //===----------------------------------------------------------------------===//
1139 // Printing, parsing and verification for LLVM::ReturnOp.
1140 //===----------------------------------------------------------------------===//
1141 
1142 static void printReturnOp(OpAsmPrinter &p, ReturnOp op) {
1143   p << op.getOperationName();
1144   p.printOptionalAttrDict(op->getAttrs());
1145   assert(op.getNumOperands() <= 1);
1146 
1147   if (op.getNumOperands() == 0)
1148     return;
1149 
1150   p << ' ' << op.getOperand(0) << " : " << op.getOperand(0).getType();
1151 }
1152 
1153 // <operation> ::= `llvm.return` ssa-use-list attribute-dict? `:`
1154 //                 type-list-no-parens
1155 static ParseResult parseReturnOp(OpAsmParser &parser, OperationState &result) {
1156   SmallVector<OpAsmParser::OperandType, 1> operands;
1157   Type type;
1158 
1159   if (parser.parseOperandList(operands) ||
1160       parser.parseOptionalAttrDict(result.attributes))
1161     return failure();
1162   if (operands.empty())
1163     return success();
1164 
1165   if (parser.parseColonType(type) ||
1166       parser.resolveOperand(operands[0], type, result.operands))
1167     return failure();
1168   return success();
1169 }
1170 
1171 static LogicalResult verify(ReturnOp op) {
1172   if (op->getNumOperands() > 1)
1173     return op->emitOpError("expected at most 1 operand");
1174 
1175   if (auto parent = op->getParentOfType<LLVMFuncOp>()) {
1176     Type expectedType = parent.getType().getReturnType();
1177     if (expectedType.isa<LLVMVoidType>()) {
1178       if (op->getNumOperands() == 0)
1179         return success();
1180       InFlightDiagnostic diag = op->emitOpError("expected no operands");
1181       diag.attachNote(parent->getLoc()) << "when returning from function";
1182       return diag;
1183     }
1184     if (op->getNumOperands() == 0) {
1185       if (expectedType.isa<LLVMVoidType>())
1186         return success();
1187       InFlightDiagnostic diag = op->emitOpError("expected 1 operand");
1188       diag.attachNote(parent->getLoc()) << "when returning from function";
1189       return diag;
1190     }
1191     if (expectedType != op->getOperand(0).getType()) {
1192       InFlightDiagnostic diag = op->emitOpError("mismatching result types");
1193       diag.attachNote(parent->getLoc()) << "when returning from function";
1194       return diag;
1195     }
1196   }
1197   return success();
1198 }
1199 
1200 //===----------------------------------------------------------------------===//
1201 // Verifier for LLVM::AddressOfOp.
1202 //===----------------------------------------------------------------------===//
1203 
1204 template <typename OpTy>
1205 static OpTy lookupSymbolInModule(Operation *parent, StringRef name) {
1206   Operation *module = parent;
1207   while (module && !satisfiesLLVMModule(module))
1208     module = module->getParentOp();
1209   assert(module && "unexpected operation outside of a module");
1210   return dyn_cast_or_null<OpTy>(
1211       mlir::SymbolTable::lookupSymbolIn(module, name));
1212 }
1213 
1214 GlobalOp AddressOfOp::getGlobal() {
1215   return lookupSymbolInModule<LLVM::GlobalOp>((*this)->getParentOp(),
1216                                               global_name());
1217 }
1218 
1219 LLVMFuncOp AddressOfOp::getFunction() {
1220   return lookupSymbolInModule<LLVM::LLVMFuncOp>((*this)->getParentOp(),
1221                                                 global_name());
1222 }
1223 
1224 static LogicalResult verify(AddressOfOp op) {
1225   auto global = op.getGlobal();
1226   auto function = op.getFunction();
1227   if (!global && !function)
1228     return op.emitOpError(
1229         "must reference a global defined by 'llvm.mlir.global' or 'llvm.func'");
1230 
1231   if (global &&
1232       LLVM::LLVMPointerType::get(global.getType(), global.addr_space()) !=
1233           op.getResult().getType())
1234     return op.emitOpError(
1235         "the type must be a pointer to the type of the referenced global");
1236 
1237   if (function && LLVM::LLVMPointerType::get(function.getType()) !=
1238                       op.getResult().getType())
1239     return op.emitOpError(
1240         "the type must be a pointer to the type of the referenced function");
1241 
1242   return success();
1243 }
1244 
1245 //===----------------------------------------------------------------------===//
1246 // Builder, printer and verifier for LLVM::GlobalOp.
1247 //===----------------------------------------------------------------------===//
1248 
1249 /// Returns the name used for the linkage attribute. This *must* correspond to
1250 /// the name of the attribute in ODS.
1251 static StringRef getLinkageAttrName() { return "linkage"; }
1252 
1253 /// Returns the name used for the unnamed_addr attribute. This *must* correspond
1254 /// to the name of the attribute in ODS.
1255 static StringRef getUnnamedAddrAttrName() { return "unnamed_addr"; }
1256 
1257 void GlobalOp::build(OpBuilder &builder, OperationState &result, Type type,
1258                      bool isConstant, Linkage linkage, StringRef name,
1259                      Attribute value, uint64_t alignment, unsigned addrSpace,
1260                      ArrayRef<NamedAttribute> attrs) {
1261   result.addAttribute(SymbolTable::getSymbolAttrName(),
1262                       builder.getStringAttr(name));
1263   result.addAttribute("type", TypeAttr::get(type));
1264   if (isConstant)
1265     result.addAttribute("constant", builder.getUnitAttr());
1266   if (value)
1267     result.addAttribute("value", value);
1268 
1269   // Only add an alignment attribute if the "alignment" input
1270   // is different from 0. The value must also be a power of two, but
1271   // this is tested in GlobalOp::verify, not here.
1272   if (alignment != 0)
1273     result.addAttribute("alignment", builder.getI64IntegerAttr(alignment));
1274 
1275   result.addAttribute(getLinkageAttrName(),
1276                       builder.getI64IntegerAttr(static_cast<int64_t>(linkage)));
1277   if (addrSpace != 0)
1278     result.addAttribute("addr_space", builder.getI32IntegerAttr(addrSpace));
1279   result.attributes.append(attrs.begin(), attrs.end());
1280   result.addRegion();
1281 }
1282 
1283 static void printGlobalOp(OpAsmPrinter &p, GlobalOp op) {
1284   p << op.getOperationName() << ' ' << stringifyLinkage(op.linkage()) << ' ';
1285   if (op.unnamed_addr())
1286     p << stringifyUnnamedAddr(*op.unnamed_addr()) << ' ';
1287   if (op.constant())
1288     p << "constant ";
1289   p.printSymbolName(op.sym_name());
1290   p << '(';
1291   if (auto value = op.getValueOrNull())
1292     p.printAttribute(value);
1293   p << ')';
1294   // Note that the alignment attribute is printed using the
1295   // default syntax here, even though it is an inherent attribute
1296   // (as defined in https://mlir.llvm.org/docs/LangRef/#attributes)
1297   p.printOptionalAttrDict(op->getAttrs(),
1298                           {SymbolTable::getSymbolAttrName(), "type", "constant",
1299                            "value", getLinkageAttrName(),
1300                            getUnnamedAddrAttrName()});
1301 
1302   // Print the trailing type unless it's a string global.
1303   if (op.getValueOrNull().dyn_cast_or_null<StringAttr>())
1304     return;
1305   p << " : " << op.type();
1306 
1307   Region &initializer = op.getInitializerRegion();
1308   if (!initializer.empty())
1309     p.printRegion(initializer, /*printEntryBlockArgs=*/false);
1310 }
1311 
1312 //===----------------------------------------------------------------------===//
1313 // Verifier for LLVM::DialectCastOp.
1314 //===----------------------------------------------------------------------===//
1315 
1316 /// Checks if `llvmType` is dialect cast-compatible with `index` type. Does not
1317 /// report the error, the user is expected to produce an appropriate message.
1318 // TODO: make the size depend on data layout rather than on the conversion
1319 // pass option, and pull that information here.
1320 static LogicalResult verifyCastWithIndex(Type llvmType) {
1321   return success(llvmType.isa<IntegerType>());
1322 }
1323 
1324 /// Checks if `llvmType` is dialect cast-compatible with built-in `type` and
1325 /// reports errors to the location of `op`. `isElement` indicates whether the
1326 /// verification is performed for types that are element types inside a
1327 /// container; we don't want casts from X to X at the top level, but c1<X> to
1328 /// c2<X> may be fine.
1329 static LogicalResult verifyCast(DialectCastOp op, Type llvmType, Type type,
1330                                 bool isElement = false) {
1331   // Equal element types are directly compatible.
1332   if (isElement && llvmType == type)
1333     return success();
1334 
1335   // Index is compatible with any integer.
1336   if (type.isIndex()) {
1337     if (succeeded(verifyCastWithIndex(llvmType)))
1338       return success();
1339 
1340     return op.emitOpError("invalid cast between index and non-integer type");
1341   }
1342 
1343   if (type.isa<IntegerType>()) {
1344     auto llvmIntegerType = llvmType.dyn_cast<IntegerType>();
1345     if (!llvmIntegerType)
1346       return op->emitOpError("invalid cast between integer and non-integer");
1347     if (llvmIntegerType.getWidth() != type.getIntOrFloatBitWidth())
1348       return op.emitOpError("invalid cast changing integer width");
1349     return success();
1350   }
1351 
1352   // Vectors are compatible if they are 1D non-scalable, and their element types
1353   // are compatible. nD vectors are compatible with (n-1)D arrays containing 1D
1354   // vector.
1355   if (auto vectorType = type.dyn_cast<VectorType>()) {
1356     if (vectorType == llvmType && !isElement)
1357       return op.emitOpError("vector types should not be casted");
1358 
1359     if (vectorType.getRank() == 1) {
1360       auto llvmVectorType = llvmType.dyn_cast<VectorType>();
1361       if (!llvmVectorType || llvmVectorType.getRank() != 1)
1362         return op.emitOpError("invalid cast for vector types");
1363 
1364       return verifyCast(op, llvmVectorType.getElementType(),
1365                         vectorType.getElementType(), /*isElement=*/true);
1366     }
1367 
1368     auto arrayType = llvmType.dyn_cast<LLVM::LLVMArrayType>();
1369     if (!arrayType ||
1370         arrayType.getNumElements() != vectorType.getShape().front())
1371       return op.emitOpError("invalid cast for vector, expected array");
1372     return verifyCast(op, arrayType.getElementType(),
1373                       VectorType::get(vectorType.getShape().drop_front(),
1374                                       vectorType.getElementType()),
1375                       /*isElement=*/true);
1376   }
1377 
1378   if (auto memrefType = type.dyn_cast<MemRefType>()) {
1379     // Bare pointer convention: statically-shaped memref is compatible with an
1380     // LLVM pointer to the element type.
1381     if (auto ptrType = llvmType.dyn_cast<LLVMPointerType>()) {
1382       if (!memrefType.hasStaticShape())
1383         return op->emitOpError(
1384             "unexpected bare pointer for dynamically shaped memref");
1385       if (memrefType.getMemorySpaceAsInt() != ptrType.getAddressSpace())
1386         return op->emitError("invalid conversion between memref and pointer in "
1387                              "different memory spaces");
1388 
1389       return verifyCast(op, ptrType.getElementType(),
1390                         memrefType.getElementType(), /*isElement=*/true);
1391     }
1392 
1393     // Otherwise, memrefs are convertible to a descriptor, which is a structure
1394     // type.
1395     auto structType = llvmType.dyn_cast<LLVMStructType>();
1396     if (!structType)
1397       return op->emitOpError("invalid cast between a memref and a type other "
1398                              "than pointer or memref descriptor");
1399 
1400     unsigned expectedNumElements = memrefType.getRank() == 0 ? 3 : 5;
1401     if (structType.getBody().size() != expectedNumElements) {
1402       return op->emitOpError() << "expected memref descriptor with "
1403                                << expectedNumElements << " elements";
1404     }
1405 
1406     // The first two elements are pointers to the element type.
1407     auto allocatedPtr = structType.getBody()[0].dyn_cast<LLVMPointerType>();
1408     if (!allocatedPtr ||
1409         allocatedPtr.getAddressSpace() != memrefType.getMemorySpaceAsInt())
1410       return op->emitOpError("expected first element of a memref descriptor to "
1411                              "be a pointer in the address space of the memref");
1412     if (failed(verifyCast(op, allocatedPtr.getElementType(),
1413                           memrefType.getElementType(), /*isElement=*/true)))
1414       return failure();
1415 
1416     auto alignedPtr = structType.getBody()[1].dyn_cast<LLVMPointerType>();
1417     if (!alignedPtr ||
1418         alignedPtr.getAddressSpace() != memrefType.getMemorySpaceAsInt())
1419       return op->emitOpError(
1420           "expected second element of a memref descriptor to "
1421           "be a pointer in the address space of the memref");
1422     if (failed(verifyCast(op, alignedPtr.getElementType(),
1423                           memrefType.getElementType(), /*isElement=*/true)))
1424       return failure();
1425 
1426     // The second element (offset) is an equivalent of index.
1427     if (failed(verifyCastWithIndex(structType.getBody()[2])))
1428       return op->emitOpError("expected third element of a memref descriptor to "
1429                              "be index-compatible integers");
1430 
1431     // 0D memrefs don't have sizes/strides.
1432     if (memrefType.getRank() == 0)
1433       return success();
1434 
1435     // Sizes and strides are rank-sized arrays of `index` equivalents.
1436     auto sizes = structType.getBody()[3].dyn_cast<LLVMArrayType>();
1437     if (!sizes || failed(verifyCastWithIndex(sizes.getElementType())) ||
1438         sizes.getNumElements() != memrefType.getRank())
1439       return op->emitOpError(
1440           "expected fourth element of a memref descriptor "
1441           "to be an array of <rank> index-compatible integers");
1442 
1443     auto strides = structType.getBody()[4].dyn_cast<LLVMArrayType>();
1444     if (!strides || failed(verifyCastWithIndex(strides.getElementType())) ||
1445         strides.getNumElements() != memrefType.getRank())
1446       return op->emitOpError(
1447           "expected fifth element of a memref descriptor "
1448           "to be an array of <rank> index-compatible integers");
1449 
1450     return success();
1451   }
1452 
1453   // Unranked memrefs are compatible with their descriptors.
1454   if (auto unrankedMemrefType = type.dyn_cast<UnrankedMemRefType>()) {
1455     auto structType = llvmType.dyn_cast<LLVMStructType>();
1456     if (!structType || structType.getBody().size() != 2)
1457       return op->emitOpError(
1458           "expected descriptor to be a struct with two elements");
1459 
1460     if (failed(verifyCastWithIndex(structType.getBody()[0])))
1461       return op->emitOpError("expected first element of a memref descriptor to "
1462                              "be an index-compatible integer");
1463 
1464     auto ptrType = structType.getBody()[1].dyn_cast<LLVMPointerType>();
1465     auto ptrElementType =
1466         ptrType ? ptrType.getElementType().dyn_cast<IntegerType>() : nullptr;
1467     if (!ptrElementType || ptrElementType.getWidth() != 8)
1468       return op->emitOpError("expected second element of a memref descriptor "
1469                              "to be an !llvm.ptr<i8>");
1470 
1471     return success();
1472   }
1473 
1474   // Complex types are compatible with the two-element structs.
1475   if (auto complexType = type.dyn_cast<ComplexType>()) {
1476     auto structType = llvmType.dyn_cast<LLVMStructType>();
1477     if (!structType || structType.getBody().size() != 2 ||
1478         structType.getBody()[0] != structType.getBody()[1] ||
1479         structType.getBody()[0] != complexType.getElementType())
1480       return op->emitOpError("expected 'complex' to map to two-element struct "
1481                              "with identical element types");
1482     return success();
1483   }
1484 
1485   // Everything else is not supported.
1486   return op->emitError("unsupported cast");
1487 }
1488 
1489 static LogicalResult verify(DialectCastOp op) {
1490   if (isCompatibleType(op.getType()))
1491     return verifyCast(op, op.getType(), op.in().getType());
1492 
1493   if (!isCompatibleType(op.in().getType()))
1494     return op->emitOpError("expected one LLVM type and one built-in type");
1495 
1496   return verifyCast(op, op.in().getType(), op.getType());
1497 }
1498 
1499 // Parses one of the keywords provided in the list `keywords` and returns the
1500 // position of the parsed keyword in the list. If none of the keywords from the
1501 // list is parsed, returns -1.
1502 static int parseOptionalKeywordAlternative(OpAsmParser &parser,
1503                                            ArrayRef<StringRef> keywords) {
1504   for (auto en : llvm::enumerate(keywords)) {
1505     if (succeeded(parser.parseOptionalKeyword(en.value())))
1506       return en.index();
1507   }
1508   return -1;
1509 }
1510 
1511 namespace {
1512 template <typename Ty>
1513 struct EnumTraits {};
1514 
1515 #define REGISTER_ENUM_TYPE(Ty)                                                 \
1516   template <>                                                                  \
1517   struct EnumTraits<Ty> {                                                      \
1518     static StringRef stringify(Ty value) { return stringify##Ty(value); }      \
1519     static unsigned getMaxEnumVal() { return getMaxEnumValFor##Ty(); }         \
1520   }
1521 
1522 REGISTER_ENUM_TYPE(Linkage);
1523 REGISTER_ENUM_TYPE(UnnamedAddr);
1524 } // end namespace
1525 
1526 template <typename EnumTy>
1527 static ParseResult parseOptionalLLVMKeyword(OpAsmParser &parser,
1528                                             OperationState &result,
1529                                             StringRef name) {
1530   SmallVector<StringRef, 10> names;
1531   for (unsigned i = 0, e = getMaxEnumValForLinkage(); i <= e; ++i)
1532     names.push_back(EnumTraits<EnumTy>::stringify(static_cast<EnumTy>(i)));
1533 
1534   int index = parseOptionalKeywordAlternative(parser, names);
1535   if (index == -1)
1536     return failure();
1537   result.addAttribute(name, parser.getBuilder().getI64IntegerAttr(index));
1538   return success();
1539 }
1540 
1541 // operation ::= `llvm.mlir.global` linkage? `constant`? `@` identifier
1542 //               `(` attribute? `)` align? attribute-list? (`:` type)? region?
1543 // align     ::= `align` `=` UINT64
1544 //
1545 // The type can be omitted for string attributes, in which case it will be
1546 // inferred from the value of the string as [strlen(value) x i8].
1547 static ParseResult parseGlobalOp(OpAsmParser &parser, OperationState &result) {
1548   if (failed(parseOptionalLLVMKeyword<Linkage>(parser, result,
1549                                                getLinkageAttrName())))
1550     result.addAttribute(getLinkageAttrName(),
1551                         parser.getBuilder().getI64IntegerAttr(
1552                             static_cast<int64_t>(LLVM::Linkage::External)));
1553 
1554   if (failed(parseOptionalLLVMKeyword<UnnamedAddr>(parser, result,
1555                                                    getUnnamedAddrAttrName())))
1556     result.addAttribute(getUnnamedAddrAttrName(),
1557                         parser.getBuilder().getI64IntegerAttr(
1558                             static_cast<int64_t>(LLVM::UnnamedAddr::None)));
1559 
1560   if (succeeded(parser.parseOptionalKeyword("constant")))
1561     result.addAttribute("constant", parser.getBuilder().getUnitAttr());
1562 
1563   StringAttr name;
1564   if (parser.parseSymbolName(name, SymbolTable::getSymbolAttrName(),
1565                              result.attributes) ||
1566       parser.parseLParen())
1567     return failure();
1568 
1569   Attribute value;
1570   if (parser.parseOptionalRParen()) {
1571     if (parser.parseAttribute(value, "value", result.attributes) ||
1572         parser.parseRParen())
1573       return failure();
1574   }
1575 
1576   SmallVector<Type, 1> types;
1577   if (parser.parseOptionalAttrDict(result.attributes) ||
1578       parser.parseOptionalColonTypeList(types))
1579     return failure();
1580 
1581   if (types.size() > 1)
1582     return parser.emitError(parser.getNameLoc(), "expected zero or one type");
1583 
1584   Region &initRegion = *result.addRegion();
1585   if (types.empty()) {
1586     if (auto strAttr = value.dyn_cast_or_null<StringAttr>()) {
1587       MLIRContext *context = parser.getBuilder().getContext();
1588       auto arrayType = LLVM::LLVMArrayType::get(IntegerType::get(context, 8),
1589                                                 strAttr.getValue().size());
1590       types.push_back(arrayType);
1591     } else {
1592       return parser.emitError(parser.getNameLoc(),
1593                               "type can only be omitted for string globals");
1594     }
1595   } else {
1596     OptionalParseResult parseResult =
1597         parser.parseOptionalRegion(initRegion, /*arguments=*/{},
1598                                    /*argTypes=*/{});
1599     if (parseResult.hasValue() && failed(*parseResult))
1600       return failure();
1601   }
1602 
1603   result.addAttribute("type", TypeAttr::get(types[0]));
1604   return success();
1605 }
1606 
1607 static bool isZeroAttribute(Attribute value) {
1608   if (auto intValue = value.dyn_cast<IntegerAttr>())
1609     return intValue.getValue().isNullValue();
1610   if (auto fpValue = value.dyn_cast<FloatAttr>())
1611     return fpValue.getValue().isZero();
1612   if (auto splatValue = value.dyn_cast<SplatElementsAttr>())
1613     return isZeroAttribute(splatValue.getSplatValue());
1614   if (auto elementsValue = value.dyn_cast<ElementsAttr>())
1615     return llvm::all_of(elementsValue.getValues<Attribute>(), isZeroAttribute);
1616   if (auto arrayValue = value.dyn_cast<ArrayAttr>())
1617     return llvm::all_of(arrayValue.getValue(), isZeroAttribute);
1618   return false;
1619 }
1620 
1621 static LogicalResult verify(GlobalOp op) {
1622   if (!LLVMPointerType::isValidElementType(op.getType()))
1623     return op.emitOpError(
1624         "expects type to be a valid element type for an LLVM pointer");
1625   if (op->getParentOp() && !satisfiesLLVMModule(op->getParentOp()))
1626     return op.emitOpError("must appear at the module level");
1627 
1628   if (auto strAttr = op.getValueOrNull().dyn_cast_or_null<StringAttr>()) {
1629     auto type = op.getType().dyn_cast<LLVMArrayType>();
1630     IntegerType elementType =
1631         type ? type.getElementType().dyn_cast<IntegerType>() : nullptr;
1632     if (!elementType || elementType.getWidth() != 8 ||
1633         type.getNumElements() != strAttr.getValue().size())
1634       return op.emitOpError(
1635           "requires an i8 array type of the length equal to that of the string "
1636           "attribute");
1637   }
1638 
1639   if (Block *b = op.getInitializerBlock()) {
1640     ReturnOp ret = cast<ReturnOp>(b->getTerminator());
1641     if (ret.operand_type_begin() == ret.operand_type_end())
1642       return op.emitOpError("initializer region cannot return void");
1643     if (*ret.operand_type_begin() != op.getType())
1644       return op.emitOpError("initializer region type ")
1645              << *ret.operand_type_begin() << " does not match global type "
1646              << op.getType();
1647 
1648     if (op.getValueOrNull())
1649       return op.emitOpError("cannot have both initializer value and region");
1650   }
1651 
1652   if (op.linkage() == Linkage::Common) {
1653     if (Attribute value = op.getValueOrNull()) {
1654       if (!isZeroAttribute(value)) {
1655         return op.emitOpError()
1656                << "expected zero value for '"
1657                << stringifyLinkage(Linkage::Common) << "' linkage";
1658       }
1659     }
1660   }
1661 
1662   if (op.linkage() == Linkage::Appending) {
1663     if (!op.getType().isa<LLVMArrayType>()) {
1664       return op.emitOpError()
1665              << "expected array type for '"
1666              << stringifyLinkage(Linkage::Appending) << "' linkage";
1667     }
1668   }
1669 
1670   Optional<uint64_t> alignAttr = op.alignment();
1671   if (alignAttr.hasValue()) {
1672     uint64_t value = alignAttr.getValue();
1673     if (!llvm::isPowerOf2_64(value))
1674       return op->emitError() << "alignment attribute is not a power of 2";
1675   }
1676 
1677   return success();
1678 }
1679 
1680 //===----------------------------------------------------------------------===//
1681 // Printing/parsing for LLVM::ShuffleVectorOp.
1682 //===----------------------------------------------------------------------===//
1683 // Expects vector to be of wrapped LLVM vector type and position to be of
1684 // wrapped LLVM i32 type.
1685 void LLVM::ShuffleVectorOp::build(OpBuilder &b, OperationState &result,
1686                                   Value v1, Value v2, ArrayAttr mask,
1687                                   ArrayRef<NamedAttribute> attrs) {
1688   auto containerType = v1.getType();
1689   auto vType = LLVM::getFixedVectorType(
1690       LLVM::getVectorElementType(containerType), mask.size());
1691   build(b, result, vType, v1, v2, mask);
1692   result.addAttributes(attrs);
1693 }
1694 
1695 static void printShuffleVectorOp(OpAsmPrinter &p, ShuffleVectorOp &op) {
1696   p << op.getOperationName() << ' ' << op.v1() << ", " << op.v2() << " "
1697     << op.mask();
1698   p.printOptionalAttrDict(op->getAttrs(), {"mask"});
1699   p << " : " << op.v1().getType() << ", " << op.v2().getType();
1700 }
1701 
1702 // <operation> ::= `llvm.shufflevector` ssa-use `, ` ssa-use
1703 //                 `[` integer-literal (`,` integer-literal)* `]`
1704 //                 attribute-dict? `:` type
1705 static ParseResult parseShuffleVectorOp(OpAsmParser &parser,
1706                                         OperationState &result) {
1707   llvm::SMLoc loc;
1708   OpAsmParser::OperandType v1, v2;
1709   ArrayAttr maskAttr;
1710   Type typeV1, typeV2;
1711   if (parser.getCurrentLocation(&loc) || parser.parseOperand(v1) ||
1712       parser.parseComma() || parser.parseOperand(v2) ||
1713       parser.parseAttribute(maskAttr, "mask", result.attributes) ||
1714       parser.parseOptionalAttrDict(result.attributes) ||
1715       parser.parseColonType(typeV1) || parser.parseComma() ||
1716       parser.parseType(typeV2) ||
1717       parser.resolveOperand(v1, typeV1, result.operands) ||
1718       parser.resolveOperand(v2, typeV2, result.operands))
1719     return failure();
1720   if (!LLVM::isCompatibleVectorType(typeV1))
1721     return parser.emitError(
1722         loc, "expected LLVM IR dialect vector type for operand #1");
1723   auto vType = LLVM::getFixedVectorType(LLVM::getVectorElementType(typeV1),
1724                                         maskAttr.size());
1725   result.addTypes(vType);
1726   return success();
1727 }
1728 
1729 //===----------------------------------------------------------------------===//
1730 // Implementations for LLVM::LLVMFuncOp.
1731 //===----------------------------------------------------------------------===//
1732 
1733 // Add the entry block to the function.
1734 Block *LLVMFuncOp::addEntryBlock() {
1735   assert(empty() && "function already has an entry block");
1736   assert(!isVarArg() && "unimplemented: non-external variadic functions");
1737 
1738   auto *entry = new Block;
1739   push_back(entry);
1740 
1741   LLVMFunctionType type = getType();
1742   for (unsigned i = 0, e = type.getNumParams(); i < e; ++i)
1743     entry->addArgument(type.getParamType(i));
1744   return entry;
1745 }
1746 
1747 void LLVMFuncOp::build(OpBuilder &builder, OperationState &result,
1748                        StringRef name, Type type, LLVM::Linkage linkage,
1749                        ArrayRef<NamedAttribute> attrs,
1750                        ArrayRef<DictionaryAttr> argAttrs) {
1751   result.addRegion();
1752   result.addAttribute(SymbolTable::getSymbolAttrName(),
1753                       builder.getStringAttr(name));
1754   result.addAttribute("type", TypeAttr::get(type));
1755   result.addAttribute(getLinkageAttrName(),
1756                       builder.getI64IntegerAttr(static_cast<int64_t>(linkage)));
1757   result.attributes.append(attrs.begin(), attrs.end());
1758   if (argAttrs.empty())
1759     return;
1760 
1761   assert(type.cast<LLVMFunctionType>().getNumParams() == argAttrs.size() &&
1762          "expected as many argument attribute lists as arguments");
1763   function_like_impl::addArgAndResultAttrs(builder, result, argAttrs,
1764                                            /*resultAttrs=*/llvm::None);
1765 }
1766 
1767 // Builds an LLVM function type from the given lists of input and output types.
1768 // Returns a null type if any of the types provided are non-LLVM types, or if
1769 // there is more than one output type.
1770 static Type
1771 buildLLVMFunctionType(OpAsmParser &parser, llvm::SMLoc loc,
1772                       ArrayRef<Type> inputs, ArrayRef<Type> outputs,
1773                       function_like_impl::VariadicFlag variadicFlag) {
1774   Builder &b = parser.getBuilder();
1775   if (outputs.size() > 1) {
1776     parser.emitError(loc, "failed to construct function type: expected zero or "
1777                           "one function result");
1778     return {};
1779   }
1780 
1781   // Convert inputs to LLVM types, exit early on error.
1782   SmallVector<Type, 4> llvmInputs;
1783   for (auto t : inputs) {
1784     if (!isCompatibleType(t)) {
1785       parser.emitError(loc, "failed to construct function type: expected LLVM "
1786                             "type for function arguments");
1787       return {};
1788     }
1789     llvmInputs.push_back(t);
1790   }
1791 
1792   // No output is denoted as "void" in LLVM type system.
1793   Type llvmOutput =
1794       outputs.empty() ? LLVMVoidType::get(b.getContext()) : outputs.front();
1795   if (!isCompatibleType(llvmOutput)) {
1796     parser.emitError(loc, "failed to construct function type: expected LLVM "
1797                           "type for function results")
1798         << llvmOutput;
1799     return {};
1800   }
1801   return LLVMFunctionType::get(llvmOutput, llvmInputs,
1802                                variadicFlag.isVariadic());
1803 }
1804 
1805 // Parses an LLVM function.
1806 //
1807 // operation ::= `llvm.func` linkage? function-signature function-attributes?
1808 //               function-body
1809 //
1810 static ParseResult parseLLVMFuncOp(OpAsmParser &parser,
1811                                    OperationState &result) {
1812   // Default to external linkage if no keyword is provided.
1813   if (failed(parseOptionalLLVMKeyword<Linkage>(parser, result,
1814                                                getLinkageAttrName())))
1815     result.addAttribute(getLinkageAttrName(),
1816                         parser.getBuilder().getI64IntegerAttr(
1817                             static_cast<int64_t>(LLVM::Linkage::External)));
1818 
1819   StringAttr nameAttr;
1820   SmallVector<OpAsmParser::OperandType, 8> entryArgs;
1821   SmallVector<NamedAttrList, 1> argAttrs;
1822   SmallVector<NamedAttrList, 1> resultAttrs;
1823   SmallVector<Type, 8> argTypes;
1824   SmallVector<Type, 4> resultTypes;
1825   bool isVariadic;
1826 
1827   auto signatureLocation = parser.getCurrentLocation();
1828   if (parser.parseSymbolName(nameAttr, SymbolTable::getSymbolAttrName(),
1829                              result.attributes) ||
1830       function_like_impl::parseFunctionSignature(
1831           parser, /*allowVariadic=*/true, entryArgs, argTypes, argAttrs,
1832           isVariadic, resultTypes, resultAttrs))
1833     return failure();
1834 
1835   auto type =
1836       buildLLVMFunctionType(parser, signatureLocation, argTypes, resultTypes,
1837                             function_like_impl::VariadicFlag(isVariadic));
1838   if (!type)
1839     return failure();
1840   result.addAttribute(function_like_impl::getTypeAttrName(),
1841                       TypeAttr::get(type));
1842 
1843   if (failed(parser.parseOptionalAttrDictWithKeyword(result.attributes)))
1844     return failure();
1845   function_like_impl::addArgAndResultAttrs(parser.getBuilder(), result,
1846                                            argAttrs, resultAttrs);
1847 
1848   auto *body = result.addRegion();
1849   OptionalParseResult parseResult = parser.parseOptionalRegion(
1850       *body, entryArgs, entryArgs.empty() ? ArrayRef<Type>() : argTypes);
1851   return failure(parseResult.hasValue() && failed(*parseResult));
1852 }
1853 
1854 // Print the LLVMFuncOp. Collects argument and result types and passes them to
1855 // helper functions. Drops "void" result since it cannot be parsed back. Skips
1856 // the external linkage since it is the default value.
1857 static void printLLVMFuncOp(OpAsmPrinter &p, LLVMFuncOp op) {
1858   p << op.getOperationName() << ' ';
1859   if (op.linkage() != LLVM::Linkage::External)
1860     p << stringifyLinkage(op.linkage()) << ' ';
1861   p.printSymbolName(op.getName());
1862 
1863   LLVMFunctionType fnType = op.getType();
1864   SmallVector<Type, 8> argTypes;
1865   SmallVector<Type, 1> resTypes;
1866   argTypes.reserve(fnType.getNumParams());
1867   for (unsigned i = 0, e = fnType.getNumParams(); i < e; ++i)
1868     argTypes.push_back(fnType.getParamType(i));
1869 
1870   Type returnType = fnType.getReturnType();
1871   if (!returnType.isa<LLVMVoidType>())
1872     resTypes.push_back(returnType);
1873 
1874   function_like_impl::printFunctionSignature(p, op, argTypes, op.isVarArg(),
1875                                              resTypes);
1876   function_like_impl::printFunctionAttributes(
1877       p, op, argTypes.size(), resTypes.size(), {getLinkageAttrName()});
1878 
1879   // Print the body if this is not an external function.
1880   Region &body = op.body();
1881   if (!body.empty())
1882     p.printRegion(body, /*printEntryBlockArgs=*/false,
1883                   /*printBlockTerminators=*/true);
1884 }
1885 
1886 // Hook for OpTrait::FunctionLike, called after verifying that the 'type'
1887 // attribute is present.  This can check for preconditions of the
1888 // getNumArguments hook not failing.
1889 LogicalResult LLVMFuncOp::verifyType() {
1890   auto llvmType = getTypeAttr().getValue().dyn_cast_or_null<LLVMFunctionType>();
1891   if (!llvmType)
1892     return emitOpError("requires '" + getTypeAttrName() +
1893                        "' attribute of wrapped LLVM function type");
1894 
1895   return success();
1896 }
1897 
1898 // Hook for OpTrait::FunctionLike, returns the number of function arguments.
1899 // Depends on the type attribute being correct as checked by verifyType
1900 unsigned LLVMFuncOp::getNumFuncArguments() { return getType().getNumParams(); }
1901 
1902 // Hook for OpTrait::FunctionLike, returns the number of function results.
1903 // Depends on the type attribute being correct as checked by verifyType
1904 unsigned LLVMFuncOp::getNumFuncResults() {
1905   // We model LLVM functions that return void as having zero results,
1906   // and all others as having one result.
1907   // If we modeled a void return as one result, then it would be possible to
1908   // attach an MLIR result attribute to it, and it isn't clear what semantics we
1909   // would assign to that.
1910   if (getType().getReturnType().isa<LLVMVoidType>())
1911     return 0;
1912   return 1;
1913 }
1914 
1915 // Verifies LLVM- and implementation-specific properties of the LLVM func Op:
1916 // - functions don't have 'common' linkage
1917 // - external functions have 'external' or 'extern_weak' linkage;
1918 // - vararg is (currently) only supported for external functions;
1919 // - entry block arguments are of LLVM types and match the function signature.
1920 static LogicalResult verify(LLVMFuncOp op) {
1921   if (op.linkage() == LLVM::Linkage::Common)
1922     return op.emitOpError()
1923            << "functions cannot have '"
1924            << stringifyLinkage(LLVM::Linkage::Common) << "' linkage";
1925 
1926   if (op.isExternal()) {
1927     if (op.linkage() != LLVM::Linkage::External &&
1928         op.linkage() != LLVM::Linkage::ExternWeak)
1929       return op.emitOpError()
1930              << "external functions must have '"
1931              << stringifyLinkage(LLVM::Linkage::External) << "' or '"
1932              << stringifyLinkage(LLVM::Linkage::ExternWeak) << "' linkage";
1933     return success();
1934   }
1935 
1936   if (op.isVarArg())
1937     return op.emitOpError("only external functions can be variadic");
1938 
1939   unsigned numArguments = op.getType().getNumParams();
1940   Block &entryBlock = op.front();
1941   for (unsigned i = 0; i < numArguments; ++i) {
1942     Type argType = entryBlock.getArgument(i).getType();
1943     if (!isCompatibleType(argType))
1944       return op.emitOpError("entry block argument #")
1945              << i << " is not of LLVM type";
1946     if (op.getType().getParamType(i) != argType)
1947       return op.emitOpError("the type of entry block argument #")
1948              << i << " does not match the function signature";
1949   }
1950 
1951   return success();
1952 }
1953 
1954 //===----------------------------------------------------------------------===//
1955 // Verification for LLVM::ConstantOp.
1956 //===----------------------------------------------------------------------===//
1957 
1958 static LogicalResult verify(LLVM::ConstantOp op) {
1959   if (StringAttr sAttr = op.value().dyn_cast<StringAttr>()) {
1960     auto arrayType = op.getType().dyn_cast<LLVMArrayType>();
1961     if (!arrayType || arrayType.getNumElements() != sAttr.getValue().size() ||
1962         !arrayType.getElementType().isInteger(8)) {
1963       return op->emitOpError()
1964              << "expected array type of " << sAttr.getValue().size()
1965              << " i8 elements for the string constant";
1966     }
1967     return success();
1968   }
1969   if (auto structType = op.getType().dyn_cast<LLVMStructType>()) {
1970     if (structType.getBody().size() != 2 ||
1971         structType.getBody()[0] != structType.getBody()[1]) {
1972       return op.emitError() << "expected struct type with two elements of the "
1973                                "same type, the type of a complex constant";
1974     }
1975 
1976     auto arrayAttr = op.value().dyn_cast<ArrayAttr>();
1977     if (!arrayAttr || arrayAttr.size() != 2 ||
1978         arrayAttr[0].getType() != arrayAttr[1].getType()) {
1979       return op.emitOpError() << "expected array attribute with two elements, "
1980                                  "representing a complex constant";
1981     }
1982 
1983     Type elementType = structType.getBody()[0];
1984     if (!elementType
1985              .isa<IntegerType, Float16Type, Float32Type, Float64Type>()) {
1986       return op.emitError()
1987              << "expected struct element types to be floating point type or "
1988                 "integer type";
1989     }
1990     return success();
1991   }
1992   if (!op.value().isa<IntegerAttr, ArrayAttr, FloatAttr, ElementsAttr>())
1993     return op.emitOpError()
1994            << "only supports integer, float, string or elements attributes";
1995   return success();
1996 }
1997 
1998 //===----------------------------------------------------------------------===//
1999 // Utility functions for parsing atomic ops
2000 //===----------------------------------------------------------------------===//
2001 
2002 // Helper function to parse a keyword into the specified attribute named by
2003 // `attrName`. The keyword must match one of the string values defined by the
2004 // AtomicBinOp enum. The resulting I64 attribute is added to the `result`
2005 // state.
2006 static ParseResult parseAtomicBinOp(OpAsmParser &parser, OperationState &result,
2007                                     StringRef attrName) {
2008   llvm::SMLoc loc;
2009   StringRef keyword;
2010   if (parser.getCurrentLocation(&loc) || parser.parseKeyword(&keyword))
2011     return failure();
2012 
2013   // Replace the keyword `keyword` with an integer attribute.
2014   auto kind = symbolizeAtomicBinOp(keyword);
2015   if (!kind) {
2016     return parser.emitError(loc)
2017            << "'" << keyword << "' is an incorrect value of the '" << attrName
2018            << "' attribute";
2019   }
2020 
2021   auto value = static_cast<int64_t>(kind.getValue());
2022   auto attr = parser.getBuilder().getI64IntegerAttr(value);
2023   result.addAttribute(attrName, attr);
2024 
2025   return success();
2026 }
2027 
2028 // Helper function to parse a keyword into the specified attribute named by
2029 // `attrName`. The keyword must match one of the string values defined by the
2030 // AtomicOrdering enum. The resulting I64 attribute is added to the `result`
2031 // state.
2032 static ParseResult parseAtomicOrdering(OpAsmParser &parser,
2033                                        OperationState &result,
2034                                        StringRef attrName) {
2035   llvm::SMLoc loc;
2036   StringRef ordering;
2037   if (parser.getCurrentLocation(&loc) || parser.parseKeyword(&ordering))
2038     return failure();
2039 
2040   // Replace the keyword `ordering` with an integer attribute.
2041   auto kind = symbolizeAtomicOrdering(ordering);
2042   if (!kind) {
2043     return parser.emitError(loc)
2044            << "'" << ordering << "' is an incorrect value of the '" << attrName
2045            << "' attribute";
2046   }
2047 
2048   auto value = static_cast<int64_t>(kind.getValue());
2049   auto attr = parser.getBuilder().getI64IntegerAttr(value);
2050   result.addAttribute(attrName, attr);
2051 
2052   return success();
2053 }
2054 
2055 //===----------------------------------------------------------------------===//
2056 // Printer, parser and verifier for LLVM::AtomicRMWOp.
2057 //===----------------------------------------------------------------------===//
2058 
2059 static void printAtomicRMWOp(OpAsmPrinter &p, AtomicRMWOp &op) {
2060   p << op.getOperationName() << ' ' << stringifyAtomicBinOp(op.bin_op()) << ' '
2061     << op.ptr() << ", " << op.val() << ' '
2062     << stringifyAtomicOrdering(op.ordering()) << ' ';
2063   p.printOptionalAttrDict(op->getAttrs(), {"bin_op", "ordering"});
2064   p << " : " << op.res().getType();
2065 }
2066 
2067 // <operation> ::= `llvm.atomicrmw` keyword ssa-use `,` ssa-use keyword
2068 //                 attribute-dict? `:` type
2069 static ParseResult parseAtomicRMWOp(OpAsmParser &parser,
2070                                     OperationState &result) {
2071   Type type;
2072   OpAsmParser::OperandType ptr, val;
2073   if (parseAtomicBinOp(parser, result, "bin_op") || parser.parseOperand(ptr) ||
2074       parser.parseComma() || parser.parseOperand(val) ||
2075       parseAtomicOrdering(parser, result, "ordering") ||
2076       parser.parseOptionalAttrDict(result.attributes) ||
2077       parser.parseColonType(type) ||
2078       parser.resolveOperand(ptr, LLVM::LLVMPointerType::get(type),
2079                             result.operands) ||
2080       parser.resolveOperand(val, type, result.operands))
2081     return failure();
2082 
2083   result.addTypes(type);
2084   return success();
2085 }
2086 
2087 static LogicalResult verify(AtomicRMWOp op) {
2088   auto ptrType = op.ptr().getType().cast<LLVM::LLVMPointerType>();
2089   auto valType = op.val().getType();
2090   if (valType != ptrType.getElementType())
2091     return op.emitOpError("expected LLVM IR element type for operand #0 to "
2092                           "match type for operand #1");
2093   auto resType = op.res().getType();
2094   if (resType != valType)
2095     return op.emitOpError(
2096         "expected LLVM IR result type to match type for operand #1");
2097   if (op.bin_op() == AtomicBinOp::fadd || op.bin_op() == AtomicBinOp::fsub) {
2098     if (!mlir::LLVM::isCompatibleFloatingPointType(valType))
2099       return op.emitOpError("expected LLVM IR floating point type");
2100   } else if (op.bin_op() == AtomicBinOp::xchg) {
2101     auto intType = valType.dyn_cast<IntegerType>();
2102     unsigned intBitWidth = intType ? intType.getWidth() : 0;
2103     if (intBitWidth != 8 && intBitWidth != 16 && intBitWidth != 32 &&
2104         intBitWidth != 64 && !valType.isa<BFloat16Type>() &&
2105         !valType.isa<Float16Type>() && !valType.isa<Float32Type>() &&
2106         !valType.isa<Float64Type>())
2107       return op.emitOpError("unexpected LLVM IR type for 'xchg' bin_op");
2108   } else {
2109     auto intType = valType.dyn_cast<IntegerType>();
2110     unsigned intBitWidth = intType ? intType.getWidth() : 0;
2111     if (intBitWidth != 8 && intBitWidth != 16 && intBitWidth != 32 &&
2112         intBitWidth != 64)
2113       return op.emitOpError("expected LLVM IR integer type");
2114   }
2115 
2116   if (static_cast<unsigned>(op.ordering()) <
2117       static_cast<unsigned>(AtomicOrdering::monotonic))
2118     return op.emitOpError()
2119            << "expected at least '"
2120            << stringifyAtomicOrdering(AtomicOrdering::monotonic)
2121            << "' ordering";
2122 
2123   return success();
2124 }
2125 
2126 //===----------------------------------------------------------------------===//
2127 // Printer, parser and verifier for LLVM::AtomicCmpXchgOp.
2128 //===----------------------------------------------------------------------===//
2129 
2130 static void printAtomicCmpXchgOp(OpAsmPrinter &p, AtomicCmpXchgOp &op) {
2131   p << op.getOperationName() << ' ' << op.ptr() << ", " << op.cmp() << ", "
2132     << op.val() << ' ' << stringifyAtomicOrdering(op.success_ordering()) << ' '
2133     << stringifyAtomicOrdering(op.failure_ordering());
2134   p.printOptionalAttrDict(op->getAttrs(),
2135                           {"success_ordering", "failure_ordering"});
2136   p << " : " << op.val().getType();
2137 }
2138 
2139 // <operation> ::= `llvm.cmpxchg` ssa-use `,` ssa-use `,` ssa-use
2140 //                 keyword keyword attribute-dict? `:` type
2141 static ParseResult parseAtomicCmpXchgOp(OpAsmParser &parser,
2142                                         OperationState &result) {
2143   auto &builder = parser.getBuilder();
2144   Type type;
2145   OpAsmParser::OperandType ptr, cmp, val;
2146   if (parser.parseOperand(ptr) || parser.parseComma() ||
2147       parser.parseOperand(cmp) || parser.parseComma() ||
2148       parser.parseOperand(val) ||
2149       parseAtomicOrdering(parser, result, "success_ordering") ||
2150       parseAtomicOrdering(parser, result, "failure_ordering") ||
2151       parser.parseOptionalAttrDict(result.attributes) ||
2152       parser.parseColonType(type) ||
2153       parser.resolveOperand(ptr, LLVM::LLVMPointerType::get(type),
2154                             result.operands) ||
2155       parser.resolveOperand(cmp, type, result.operands) ||
2156       parser.resolveOperand(val, type, result.operands))
2157     return failure();
2158 
2159   auto boolType = IntegerType::get(builder.getContext(), 1);
2160   auto resultType =
2161       LLVMStructType::getLiteral(builder.getContext(), {type, boolType});
2162   result.addTypes(resultType);
2163 
2164   return success();
2165 }
2166 
2167 static LogicalResult verify(AtomicCmpXchgOp op) {
2168   auto ptrType = op.ptr().getType().cast<LLVM::LLVMPointerType>();
2169   if (!ptrType)
2170     return op.emitOpError("expected LLVM IR pointer type for operand #0");
2171   auto cmpType = op.cmp().getType();
2172   auto valType = op.val().getType();
2173   if (cmpType != ptrType.getElementType() || cmpType != valType)
2174     return op.emitOpError("expected LLVM IR element type for operand #0 to "
2175                           "match type for all other operands");
2176   auto intType = valType.dyn_cast<IntegerType>();
2177   unsigned intBitWidth = intType ? intType.getWidth() : 0;
2178   if (!valType.isa<LLVMPointerType>() && intBitWidth != 8 &&
2179       intBitWidth != 16 && intBitWidth != 32 && intBitWidth != 64 &&
2180       !valType.isa<BFloat16Type>() && !valType.isa<Float16Type>() &&
2181       !valType.isa<Float32Type>() && !valType.isa<Float64Type>())
2182     return op.emitOpError("unexpected LLVM IR type");
2183   if (op.success_ordering() < AtomicOrdering::monotonic ||
2184       op.failure_ordering() < AtomicOrdering::monotonic)
2185     return op.emitOpError("ordering must be at least 'monotonic'");
2186   if (op.failure_ordering() == AtomicOrdering::release ||
2187       op.failure_ordering() == AtomicOrdering::acq_rel)
2188     return op.emitOpError("failure ordering cannot be 'release' or 'acq_rel'");
2189   return success();
2190 }
2191 
2192 //===----------------------------------------------------------------------===//
2193 // Printer, parser and verifier for LLVM::FenceOp.
2194 //===----------------------------------------------------------------------===//
2195 
2196 // <operation> ::= `llvm.fence` (`syncscope(`strAttr`)`)? keyword
2197 // attribute-dict?
2198 static ParseResult parseFenceOp(OpAsmParser &parser, OperationState &result) {
2199   StringAttr sScope;
2200   StringRef syncscopeKeyword = "syncscope";
2201   if (!failed(parser.parseOptionalKeyword(syncscopeKeyword))) {
2202     if (parser.parseLParen() ||
2203         parser.parseAttribute(sScope, syncscopeKeyword, result.attributes) ||
2204         parser.parseRParen())
2205       return failure();
2206   } else {
2207     result.addAttribute(syncscopeKeyword,
2208                         parser.getBuilder().getStringAttr(""));
2209   }
2210   if (parseAtomicOrdering(parser, result, "ordering") ||
2211       parser.parseOptionalAttrDict(result.attributes))
2212     return failure();
2213   return success();
2214 }
2215 
2216 static void printFenceOp(OpAsmPrinter &p, FenceOp &op) {
2217   StringRef syncscopeKeyword = "syncscope";
2218   p << op.getOperationName() << ' ';
2219   if (!op->getAttr(syncscopeKeyword).cast<StringAttr>().getValue().empty())
2220     p << "syncscope(" << op->getAttr(syncscopeKeyword) << ") ";
2221   p << stringifyAtomicOrdering(op.ordering());
2222 }
2223 
2224 static LogicalResult verify(FenceOp &op) {
2225   if (op.ordering() == AtomicOrdering::not_atomic ||
2226       op.ordering() == AtomicOrdering::unordered ||
2227       op.ordering() == AtomicOrdering::monotonic)
2228     return op.emitOpError("can be given only acquire, release, acq_rel, "
2229                           "and seq_cst orderings");
2230   return success();
2231 }
2232 
2233 //===----------------------------------------------------------------------===//
2234 // LLVMDialect initialization, type parsing, and registration.
2235 //===----------------------------------------------------------------------===//
2236 
2237 void LLVMDialect::initialize() {
2238   addAttributes<FMFAttr, LoopOptionsAttr>();
2239 
2240   // clang-format off
2241   addTypes<LLVMVoidType,
2242            LLVMPPCFP128Type,
2243            LLVMX86MMXType,
2244            LLVMTokenType,
2245            LLVMLabelType,
2246            LLVMMetadataType,
2247            LLVMFunctionType,
2248            LLVMPointerType,
2249            LLVMFixedVectorType,
2250            LLVMScalableVectorType,
2251            LLVMArrayType,
2252            LLVMStructType>();
2253   // clang-format on
2254   addOperations<
2255 #define GET_OP_LIST
2256 #include "mlir/Dialect/LLVMIR/LLVMOps.cpp.inc"
2257       >();
2258 
2259   // Support unknown operations because not all LLVM operations are registered.
2260   allowUnknownOperations();
2261 }
2262 
2263 #define GET_OP_CLASSES
2264 #include "mlir/Dialect/LLVMIR/LLVMOps.cpp.inc"
2265 
2266 /// Parse a type registered to this dialect.
2267 Type LLVMDialect::parseType(DialectAsmParser &parser) const {
2268   return detail::parseType(parser);
2269 }
2270 
2271 /// Print a type registered to this dialect.
2272 void LLVMDialect::printType(Type type, DialectAsmPrinter &os) const {
2273   return detail::printType(type, os);
2274 }
2275 
2276 LogicalResult LLVMDialect::verifyDataLayoutString(
2277     StringRef descr, llvm::function_ref<void(const Twine &)> reportError) {
2278   llvm::Expected<llvm::DataLayout> maybeDataLayout =
2279       llvm::DataLayout::parse(descr);
2280   if (maybeDataLayout)
2281     return success();
2282 
2283   std::string message;
2284   llvm::raw_string_ostream messageStream(message);
2285   llvm::logAllUnhandledErrors(maybeDataLayout.takeError(), messageStream);
2286   reportError("invalid data layout descriptor: " + messageStream.str());
2287   return failure();
2288 }
2289 
2290 /// Verify LLVM dialect attributes.
2291 LogicalResult LLVMDialect::verifyOperationAttribute(Operation *op,
2292                                                     NamedAttribute attr) {
2293   // If the `llvm.loop` attribute is present, enforce the following structure,
2294   // which the module translation can assume.
2295   if (attr.first.strref() == LLVMDialect::getLoopAttrName()) {
2296     auto loopAttr = attr.second.dyn_cast<DictionaryAttr>();
2297     if (!loopAttr)
2298       return op->emitOpError() << "expected '" << LLVMDialect::getLoopAttrName()
2299                                << "' to be a dictionary attribute";
2300     Optional<NamedAttribute> parallelAccessGroup =
2301         loopAttr.getNamed(LLVMDialect::getParallelAccessAttrName());
2302     if (parallelAccessGroup.hasValue()) {
2303       auto accessGroups = parallelAccessGroup->second.dyn_cast<ArrayAttr>();
2304       if (!accessGroups)
2305         return op->emitOpError()
2306                << "expected '" << LLVMDialect::getParallelAccessAttrName()
2307                << "' to be an array attribute";
2308       for (Attribute attr : accessGroups) {
2309         auto accessGroupRef = attr.dyn_cast<SymbolRefAttr>();
2310         if (!accessGroupRef)
2311           return op->emitOpError()
2312                  << "expected '" << attr << "' to be a symbol reference";
2313         StringRef metadataName = accessGroupRef.getRootReference();
2314         auto metadataOp =
2315             SymbolTable::lookupNearestSymbolFrom<LLVM::MetadataOp>(
2316                 op->getParentOp(), metadataName);
2317         if (!metadataOp)
2318           return op->emitOpError()
2319                  << "expected '" << attr << "' to reference a metadata op";
2320         StringRef accessGroupName = accessGroupRef.getLeafReference();
2321         Operation *accessGroupOp =
2322             SymbolTable::lookupNearestSymbolFrom(metadataOp, accessGroupName);
2323         if (!accessGroupOp)
2324           return op->emitOpError()
2325                  << "expected '" << attr << "' to reference an access_group op";
2326       }
2327     }
2328 
2329     Optional<NamedAttribute> loopOptions =
2330         loopAttr.getNamed(LLVMDialect::getLoopOptionsAttrName());
2331     if (loopOptions.hasValue() && !loopOptions->second.isa<LoopOptionsAttr>())
2332       return op->emitOpError()
2333              << "expected '" << LLVMDialect::getLoopOptionsAttrName()
2334              << "' to be a `loopopts` attribute";
2335   }
2336 
2337   // If the data layout attribute is present, it must use the LLVM data layout
2338   // syntax. Try parsing it and report errors in case of failure. Users of this
2339   // attribute may assume it is well-formed and can pass it to the (asserting)
2340   // llvm::DataLayout constructor.
2341   if (attr.first.strref() != LLVM::LLVMDialect::getDataLayoutAttrName())
2342     return success();
2343   if (auto stringAttr = attr.second.dyn_cast<StringAttr>())
2344     return verifyDataLayoutString(
2345         stringAttr.getValue(),
2346         [op](const Twine &message) { op->emitOpError() << message.str(); });
2347 
2348   return op->emitOpError() << "expected '"
2349                            << LLVM::LLVMDialect::getDataLayoutAttrName()
2350                            << "' to be a string attribute";
2351 }
2352 
2353 /// Verify LLVMIR function argument attributes.
2354 LogicalResult LLVMDialect::verifyRegionArgAttribute(Operation *op,
2355                                                     unsigned regionIdx,
2356                                                     unsigned argIdx,
2357                                                     NamedAttribute argAttr) {
2358   // Check that llvm.noalias is a unit attribute.
2359   if (argAttr.first == LLVMDialect::getNoAliasAttrName() &&
2360       !argAttr.second.isa<UnitAttr>())
2361     return op->emitError()
2362            << "expected llvm.noalias argument attribute to be a unit attribute";
2363   // Check that llvm.align is an integer attribute.
2364   if (argAttr.first == LLVMDialect::getAlignAttrName() &&
2365       !argAttr.second.isa<IntegerAttr>())
2366     return op->emitError()
2367            << "llvm.align argument attribute of non integer type";
2368   return success();
2369 }
2370 
2371 //===----------------------------------------------------------------------===//
2372 // Utility functions.
2373 //===----------------------------------------------------------------------===//
2374 
2375 Value mlir::LLVM::createGlobalString(Location loc, OpBuilder &builder,
2376                                      StringRef name, StringRef value,
2377                                      LLVM::Linkage linkage) {
2378   assert(builder.getInsertionBlock() &&
2379          builder.getInsertionBlock()->getParentOp() &&
2380          "expected builder to point to a block constrained in an op");
2381   auto module =
2382       builder.getInsertionBlock()->getParentOp()->getParentOfType<ModuleOp>();
2383   assert(module && "builder points to an op outside of a module");
2384 
2385   // Create the global at the entry of the module.
2386   OpBuilder moduleBuilder(module.getBodyRegion(), builder.getListener());
2387   MLIRContext *ctx = builder.getContext();
2388   auto type = LLVM::LLVMArrayType::get(IntegerType::get(ctx, 8), value.size());
2389   auto global = moduleBuilder.create<LLVM::GlobalOp>(
2390       loc, type, /*isConstant=*/true, linkage, name,
2391       builder.getStringAttr(value), /*alignment=*/0);
2392 
2393   // Get the pointer to the first character in the global string.
2394   Value globalPtr = builder.create<LLVM::AddressOfOp>(loc, global);
2395   Value cst0 = builder.create<LLVM::ConstantOp>(
2396       loc, IntegerType::get(ctx, 64),
2397       builder.getIntegerAttr(builder.getIndexType(), 0));
2398   return builder.create<LLVM::GEPOp>(
2399       loc, LLVM::LLVMPointerType::get(IntegerType::get(ctx, 8)), globalPtr,
2400       ValueRange{cst0, cst0});
2401 }
2402 
2403 bool mlir::LLVM::satisfiesLLVMModule(Operation *op) {
2404   return op->hasTrait<OpTrait::SymbolTable>() &&
2405          op->hasTrait<OpTrait::IsIsolatedFromAbove>();
2406 }
2407 
2408 static constexpr const FastmathFlags FastmathFlagsList[] = {
2409     // clang-format off
2410     FastmathFlags::nnan,
2411     FastmathFlags::ninf,
2412     FastmathFlags::nsz,
2413     FastmathFlags::arcp,
2414     FastmathFlags::contract,
2415     FastmathFlags::afn,
2416     FastmathFlags::reassoc,
2417     FastmathFlags::fast,
2418     // clang-format on
2419 };
2420 
2421 void FMFAttr::print(DialectAsmPrinter &printer) const {
2422   printer << "fastmath<";
2423   auto flags = llvm::make_filter_range(FastmathFlagsList, [&](auto flag) {
2424     return bitEnumContains(this->getFlags(), flag);
2425   });
2426   llvm::interleaveComma(flags, printer,
2427                         [&](auto flag) { printer << stringifyEnum(flag); });
2428   printer << ">";
2429 }
2430 
2431 Attribute FMFAttr::parse(MLIRContext *context, DialectAsmParser &parser,
2432                          Type type) {
2433   if (failed(parser.parseLess()))
2434     return {};
2435 
2436   FastmathFlags flags = {};
2437   if (failed(parser.parseOptionalGreater())) {
2438     do {
2439       StringRef elemName;
2440       if (failed(parser.parseKeyword(&elemName)))
2441         return {};
2442 
2443       auto elem = symbolizeFastmathFlags(elemName);
2444       if (!elem) {
2445         parser.emitError(parser.getNameLoc(), "Unknown fastmath flag: ")
2446             << elemName;
2447         return {};
2448       }
2449 
2450       flags = flags | *elem;
2451     } while (succeeded(parser.parseOptionalComma()));
2452 
2453     if (failed(parser.parseGreater()))
2454       return {};
2455   }
2456 
2457   return FMFAttr::get(parser.getBuilder().getContext(), flags);
2458 }
2459 
2460 LoopOptionsAttrBuilder::LoopOptionsAttrBuilder(LoopOptionsAttr attr)
2461     : options(attr.getOptions().begin(), attr.getOptions().end()) {}
2462 
2463 template <typename T>
2464 LoopOptionsAttrBuilder &LoopOptionsAttrBuilder::setOption(LoopOptionCase tag,
2465                                                           Optional<T> value) {
2466   auto option = llvm::find_if(
2467       options, [tag](auto option) { return option.first == tag; });
2468   if (option != options.end()) {
2469     if (value.hasValue())
2470       option->second = *value;
2471     else
2472       options.erase(option);
2473   } else {
2474     options.push_back(LoopOptionsAttr::OptionValuePair(tag, *value));
2475   }
2476   return *this;
2477 }
2478 
2479 LoopOptionsAttrBuilder &
2480 LoopOptionsAttrBuilder::setDisableLICM(Optional<bool> value) {
2481   return setOption(LoopOptionCase::disable_licm, value);
2482 }
2483 
2484 /// Set the `interleave_count` option to the provided value. If no value
2485 /// is provided the option is deleted.
2486 LoopOptionsAttrBuilder &
2487 LoopOptionsAttrBuilder::setInterleaveCount(Optional<uint64_t> count) {
2488   return setOption(LoopOptionCase::interleave_count, count);
2489 }
2490 
2491 /// Set the `disable_unroll` option to the provided value. If no value
2492 /// is provided the option is deleted.
2493 LoopOptionsAttrBuilder &
2494 LoopOptionsAttrBuilder::setDisableUnroll(Optional<bool> value) {
2495   return setOption(LoopOptionCase::disable_unroll, value);
2496 }
2497 
2498 /// Set the `disable_pipeline` option to the provided value. If no value
2499 /// is provided the option is deleted.
2500 LoopOptionsAttrBuilder &
2501 LoopOptionsAttrBuilder::setDisablePipeline(Optional<bool> value) {
2502   return setOption(LoopOptionCase::disable_pipeline, value);
2503 }
2504 
2505 /// Set the `pipeline_initiation_interval` option to the provided value.
2506 /// If no value is provided the option is deleted.
2507 LoopOptionsAttrBuilder &LoopOptionsAttrBuilder::setPipelineInitiationInterval(
2508     Optional<uint64_t> count) {
2509   return setOption(LoopOptionCase::pipeline_initiation_interval, count);
2510 }
2511 
2512 template <typename T>
2513 static Optional<T>
2514 getOption(ArrayRef<std::pair<LoopOptionCase, int64_t>> options,
2515           LoopOptionCase option) {
2516   auto it =
2517       lower_bound(options, option, [](auto optionPair, LoopOptionCase option) {
2518         return optionPair.first < option;
2519       });
2520   if (it == options.end())
2521     return {};
2522   return static_cast<T>(it->second);
2523 }
2524 
2525 Optional<bool> LoopOptionsAttr::disableUnroll() {
2526   return getOption<bool>(getOptions(), LoopOptionCase::disable_unroll);
2527 }
2528 
2529 Optional<bool> LoopOptionsAttr::disableLICM() {
2530   return getOption<bool>(getOptions(), LoopOptionCase::disable_licm);
2531 }
2532 
2533 Optional<int64_t> LoopOptionsAttr::interleaveCount() {
2534   return getOption<int64_t>(getOptions(), LoopOptionCase::interleave_count);
2535 }
2536 
2537 /// Build the LoopOptions Attribute from a sorted array of individual options.
2538 LoopOptionsAttr LoopOptionsAttr::get(
2539     MLIRContext *context,
2540     ArrayRef<std::pair<LoopOptionCase, int64_t>> sortedOptions) {
2541   assert(llvm::is_sorted(sortedOptions, llvm::less_first()) &&
2542          "LoopOptionsAttr ctor expects a sorted options array");
2543   return Base::get(context, sortedOptions);
2544 }
2545 
2546 /// Build the LoopOptions Attribute from a sorted array of individual options.
2547 LoopOptionsAttr LoopOptionsAttr::get(MLIRContext *context,
2548                                      LoopOptionsAttrBuilder &optionBuilders) {
2549   llvm::sort(optionBuilders.options, llvm::less_first());
2550   return Base::get(context, optionBuilders.options);
2551 }
2552 
2553 void LoopOptionsAttr::print(DialectAsmPrinter &printer) const {
2554   printer << getMnemonic() << "<";
2555   llvm::interleaveComma(getOptions(), printer, [&](auto option) {
2556     printer << stringifyEnum(option.first) << " = ";
2557     switch (option.first) {
2558     case LoopOptionCase::disable_licm:
2559     case LoopOptionCase::disable_unroll:
2560     case LoopOptionCase::disable_pipeline:
2561       printer << (option.second ? "true" : "false");
2562       break;
2563     case LoopOptionCase::interleave_count:
2564     case LoopOptionCase::pipeline_initiation_interval:
2565       printer << option.second;
2566       break;
2567     }
2568   });
2569   printer << ">";
2570 }
2571 
2572 Attribute LoopOptionsAttr::parse(MLIRContext *context, DialectAsmParser &parser,
2573                                  Type type) {
2574   if (failed(parser.parseLess()))
2575     return {};
2576 
2577   SmallVector<std::pair<LoopOptionCase, int64_t>> options;
2578   llvm::SmallDenseSet<LoopOptionCase> seenOptions;
2579   do {
2580     StringRef optionName;
2581     if (parser.parseKeyword(&optionName))
2582       return {};
2583 
2584     auto option = symbolizeLoopOptionCase(optionName);
2585     if (!option) {
2586       parser.emitError(parser.getNameLoc(), "unknown loop option: ")
2587           << optionName;
2588       return {};
2589     }
2590     if (!seenOptions.insert(*option).second) {
2591       parser.emitError(parser.getNameLoc(), "loop option present twice");
2592       return {};
2593     }
2594     if (failed(parser.parseEqual()))
2595       return {};
2596 
2597     int64_t value;
2598     switch (*option) {
2599     case LoopOptionCase::disable_licm:
2600     case LoopOptionCase::disable_unroll:
2601     case LoopOptionCase::disable_pipeline:
2602       if (succeeded(parser.parseOptionalKeyword("true")))
2603         value = 1;
2604       else if (succeeded(parser.parseOptionalKeyword("false")))
2605         value = 0;
2606       else {
2607         parser.emitError(parser.getNameLoc(),
2608                          "expected boolean value 'true' or 'false'");
2609         return {};
2610       }
2611       break;
2612     case LoopOptionCase::interleave_count:
2613     case LoopOptionCase::pipeline_initiation_interval:
2614       if (failed(parser.parseInteger(value))) {
2615         parser.emitError(parser.getNameLoc(), "expected integer value");
2616         return {};
2617       }
2618       break;
2619     }
2620     options.push_back(std::make_pair(*option, value));
2621   } while (succeeded(parser.parseOptionalComma()));
2622   if (failed(parser.parseGreater()))
2623     return {};
2624 
2625   llvm::sort(options, llvm::less_first());
2626   return get(parser.getBuilder().getContext(), options);
2627 }
2628 
2629 Attribute LLVMDialect::parseAttribute(DialectAsmParser &parser,
2630                                       Type type) const {
2631   if (type) {
2632     parser.emitError(parser.getNameLoc(), "unexpected type");
2633     return {};
2634   }
2635   StringRef attrKind;
2636   if (parser.parseKeyword(&attrKind))
2637     return {};
2638   {
2639     Attribute attr;
2640     auto parseResult =
2641         generatedAttributeParser(getContext(), parser, attrKind, type, attr);
2642     if (parseResult.hasValue())
2643       return attr;
2644   }
2645   parser.emitError(parser.getNameLoc(), "unknown attribute type: ") << attrKind;
2646   return {};
2647 }
2648 
2649 void LLVMDialect::printAttribute(Attribute attr, DialectAsmPrinter &os) const {
2650   if (succeeded(generatedAttributePrinter(attr, os)))
2651     return;
2652   llvm_unreachable("Unknown attribute type");
2653 }
2654