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 OpFoldResult LLVM::ExtractValueOp::fold(ArrayRef<Attribute> operands) {
1054   auto insertValueOp = container().getDefiningOp<InsertValueOp>();
1055   while (insertValueOp) {
1056     if (position() == insertValueOp.position())
1057       return insertValueOp.value();
1058     insertValueOp = insertValueOp.container().getDefiningOp<InsertValueOp>();
1059   }
1060   return {};
1061 }
1062 
1063 //===----------------------------------------------------------------------===//
1064 // Printing/parsing for LLVM::InsertElementOp.
1065 //===----------------------------------------------------------------------===//
1066 
1067 static void printInsertElementOp(OpAsmPrinter &p, InsertElementOp &op) {
1068   p << op.getOperationName() << ' ' << op.value() << ", " << op.vector() << "["
1069     << op.position() << " : " << op.position().getType() << "]";
1070   p.printOptionalAttrDict(op->getAttrs());
1071   p << " : " << op.vector().getType();
1072 }
1073 
1074 // <operation> ::= `llvm.insertelement` ssa-use `,` ssa-use `,` ssa-use
1075 //                 attribute-dict? `:` type
1076 static ParseResult parseInsertElementOp(OpAsmParser &parser,
1077                                         OperationState &result) {
1078   llvm::SMLoc loc;
1079   OpAsmParser::OperandType vector, value, position;
1080   Type vectorType, positionType;
1081   if (parser.getCurrentLocation(&loc) || parser.parseOperand(value) ||
1082       parser.parseComma() || parser.parseOperand(vector) ||
1083       parser.parseLSquare() || parser.parseOperand(position) ||
1084       parser.parseColonType(positionType) || parser.parseRSquare() ||
1085       parser.parseOptionalAttrDict(result.attributes) ||
1086       parser.parseColonType(vectorType))
1087     return failure();
1088 
1089   if (!LLVM::isCompatibleVectorType(vectorType))
1090     return parser.emitError(
1091         loc, "expected LLVM dialect-compatible vector type for operand #1");
1092   Type valueType = LLVM::getVectorElementType(vectorType);
1093   if (!valueType)
1094     return failure();
1095 
1096   if (parser.resolveOperand(vector, vectorType, result.operands) ||
1097       parser.resolveOperand(value, valueType, result.operands) ||
1098       parser.resolveOperand(position, positionType, result.operands))
1099     return failure();
1100 
1101   result.addTypes(vectorType);
1102   return success();
1103 }
1104 
1105 //===----------------------------------------------------------------------===//
1106 // Printing/parsing for LLVM::InsertValueOp.
1107 //===----------------------------------------------------------------------===//
1108 
1109 static void printInsertValueOp(OpAsmPrinter &p, InsertValueOp &op) {
1110   p << op.getOperationName() << ' ' << op.value() << ", " << op.container()
1111     << op.position();
1112   p.printOptionalAttrDict(op->getAttrs(), {"position"});
1113   p << " : " << op.container().getType();
1114 }
1115 
1116 // <operation> ::= `llvm.insertvaluevalue` ssa-use `,` ssa-use
1117 //                 `[` integer-literal (`,` integer-literal)* `]`
1118 //                 attribute-dict? `:` type
1119 static ParseResult parseInsertValueOp(OpAsmParser &parser,
1120                                       OperationState &result) {
1121   OpAsmParser::OperandType container, value;
1122   Type containerType;
1123   ArrayAttr positionAttr;
1124   llvm::SMLoc attributeLoc, trailingTypeLoc;
1125 
1126   if (parser.parseOperand(value) || parser.parseComma() ||
1127       parser.parseOperand(container) ||
1128       parser.getCurrentLocation(&attributeLoc) ||
1129       parser.parseAttribute(positionAttr, "position", result.attributes) ||
1130       parser.parseOptionalAttrDict(result.attributes) || parser.parseColon() ||
1131       parser.getCurrentLocation(&trailingTypeLoc) ||
1132       parser.parseType(containerType))
1133     return failure();
1134 
1135   auto valueType = getInsertExtractValueElementType(
1136       parser, containerType, positionAttr, attributeLoc, trailingTypeLoc);
1137   if (!valueType)
1138     return failure();
1139 
1140   if (parser.resolveOperand(container, containerType, result.operands) ||
1141       parser.resolveOperand(value, valueType, result.operands))
1142     return failure();
1143 
1144   result.addTypes(containerType);
1145   return success();
1146 }
1147 
1148 //===----------------------------------------------------------------------===//
1149 // Printing, parsing and verification for LLVM::ReturnOp.
1150 //===----------------------------------------------------------------------===//
1151 
1152 static void printReturnOp(OpAsmPrinter &p, ReturnOp op) {
1153   p << op.getOperationName();
1154   p.printOptionalAttrDict(op->getAttrs());
1155   assert(op.getNumOperands() <= 1);
1156 
1157   if (op.getNumOperands() == 0)
1158     return;
1159 
1160   p << ' ' << op.getOperand(0) << " : " << op.getOperand(0).getType();
1161 }
1162 
1163 // <operation> ::= `llvm.return` ssa-use-list attribute-dict? `:`
1164 //                 type-list-no-parens
1165 static ParseResult parseReturnOp(OpAsmParser &parser, OperationState &result) {
1166   SmallVector<OpAsmParser::OperandType, 1> operands;
1167   Type type;
1168 
1169   if (parser.parseOperandList(operands) ||
1170       parser.parseOptionalAttrDict(result.attributes))
1171     return failure();
1172   if (operands.empty())
1173     return success();
1174 
1175   if (parser.parseColonType(type) ||
1176       parser.resolveOperand(operands[0], type, result.operands))
1177     return failure();
1178   return success();
1179 }
1180 
1181 static LogicalResult verify(ReturnOp op) {
1182   if (op->getNumOperands() > 1)
1183     return op->emitOpError("expected at most 1 operand");
1184 
1185   if (auto parent = op->getParentOfType<LLVMFuncOp>()) {
1186     Type expectedType = parent.getType().getReturnType();
1187     if (expectedType.isa<LLVMVoidType>()) {
1188       if (op->getNumOperands() == 0)
1189         return success();
1190       InFlightDiagnostic diag = op->emitOpError("expected no operands");
1191       diag.attachNote(parent->getLoc()) << "when returning from function";
1192       return diag;
1193     }
1194     if (op->getNumOperands() == 0) {
1195       if (expectedType.isa<LLVMVoidType>())
1196         return success();
1197       InFlightDiagnostic diag = op->emitOpError("expected 1 operand");
1198       diag.attachNote(parent->getLoc()) << "when returning from function";
1199       return diag;
1200     }
1201     if (expectedType != op->getOperand(0).getType()) {
1202       InFlightDiagnostic diag = op->emitOpError("mismatching result types");
1203       diag.attachNote(parent->getLoc()) << "when returning from function";
1204       return diag;
1205     }
1206   }
1207   return success();
1208 }
1209 
1210 //===----------------------------------------------------------------------===//
1211 // Verifier for LLVM::AddressOfOp.
1212 //===----------------------------------------------------------------------===//
1213 
1214 template <typename OpTy>
1215 static OpTy lookupSymbolInModule(Operation *parent, StringRef name) {
1216   Operation *module = parent;
1217   while (module && !satisfiesLLVMModule(module))
1218     module = module->getParentOp();
1219   assert(module && "unexpected operation outside of a module");
1220   return dyn_cast_or_null<OpTy>(
1221       mlir::SymbolTable::lookupSymbolIn(module, name));
1222 }
1223 
1224 GlobalOp AddressOfOp::getGlobal() {
1225   return lookupSymbolInModule<LLVM::GlobalOp>((*this)->getParentOp(),
1226                                               global_name());
1227 }
1228 
1229 LLVMFuncOp AddressOfOp::getFunction() {
1230   return lookupSymbolInModule<LLVM::LLVMFuncOp>((*this)->getParentOp(),
1231                                                 global_name());
1232 }
1233 
1234 static LogicalResult verify(AddressOfOp op) {
1235   auto global = op.getGlobal();
1236   auto function = op.getFunction();
1237   if (!global && !function)
1238     return op.emitOpError(
1239         "must reference a global defined by 'llvm.mlir.global' or 'llvm.func'");
1240 
1241   if (global &&
1242       LLVM::LLVMPointerType::get(global.getType(), global.addr_space()) !=
1243           op.getResult().getType())
1244     return op.emitOpError(
1245         "the type must be a pointer to the type of the referenced global");
1246 
1247   if (function && LLVM::LLVMPointerType::get(function.getType()) !=
1248                       op.getResult().getType())
1249     return op.emitOpError(
1250         "the type must be a pointer to the type of the referenced function");
1251 
1252   return success();
1253 }
1254 
1255 //===----------------------------------------------------------------------===//
1256 // Builder, printer and verifier for LLVM::GlobalOp.
1257 //===----------------------------------------------------------------------===//
1258 
1259 /// Returns the name used for the linkage attribute. This *must* correspond to
1260 /// the name of the attribute in ODS.
1261 static StringRef getLinkageAttrName() { return "linkage"; }
1262 
1263 /// Returns the name used for the unnamed_addr attribute. This *must* correspond
1264 /// to the name of the attribute in ODS.
1265 static StringRef getUnnamedAddrAttrName() { return "unnamed_addr"; }
1266 
1267 void GlobalOp::build(OpBuilder &builder, OperationState &result, Type type,
1268                      bool isConstant, Linkage linkage, StringRef name,
1269                      Attribute value, uint64_t alignment, unsigned addrSpace,
1270                      ArrayRef<NamedAttribute> attrs) {
1271   result.addAttribute(SymbolTable::getSymbolAttrName(),
1272                       builder.getStringAttr(name));
1273   result.addAttribute("type", TypeAttr::get(type));
1274   if (isConstant)
1275     result.addAttribute("constant", builder.getUnitAttr());
1276   if (value)
1277     result.addAttribute("value", value);
1278 
1279   // Only add an alignment attribute if the "alignment" input
1280   // is different from 0. The value must also be a power of two, but
1281   // this is tested in GlobalOp::verify, not here.
1282   if (alignment != 0)
1283     result.addAttribute("alignment", builder.getI64IntegerAttr(alignment));
1284 
1285   result.addAttribute(getLinkageAttrName(),
1286                       builder.getI64IntegerAttr(static_cast<int64_t>(linkage)));
1287   if (addrSpace != 0)
1288     result.addAttribute("addr_space", builder.getI32IntegerAttr(addrSpace));
1289   result.attributes.append(attrs.begin(), attrs.end());
1290   result.addRegion();
1291 }
1292 
1293 static void printGlobalOp(OpAsmPrinter &p, GlobalOp op) {
1294   p << op.getOperationName() << ' ' << stringifyLinkage(op.linkage()) << ' ';
1295   if (op.unnamed_addr())
1296     p << stringifyUnnamedAddr(*op.unnamed_addr()) << ' ';
1297   if (op.constant())
1298     p << "constant ";
1299   p.printSymbolName(op.sym_name());
1300   p << '(';
1301   if (auto value = op.getValueOrNull())
1302     p.printAttribute(value);
1303   p << ')';
1304   // Note that the alignment attribute is printed using the
1305   // default syntax here, even though it is an inherent attribute
1306   // (as defined in https://mlir.llvm.org/docs/LangRef/#attributes)
1307   p.printOptionalAttrDict(op->getAttrs(),
1308                           {SymbolTable::getSymbolAttrName(), "type", "constant",
1309                            "value", getLinkageAttrName(),
1310                            getUnnamedAddrAttrName()});
1311 
1312   // Print the trailing type unless it's a string global.
1313   if (op.getValueOrNull().dyn_cast_or_null<StringAttr>())
1314     return;
1315   p << " : " << op.type();
1316 
1317   Region &initializer = op.getInitializerRegion();
1318   if (!initializer.empty())
1319     p.printRegion(initializer, /*printEntryBlockArgs=*/false);
1320 }
1321 
1322 //===----------------------------------------------------------------------===//
1323 // Verifier for LLVM::DialectCastOp.
1324 //===----------------------------------------------------------------------===//
1325 
1326 /// Checks if `llvmType` is dialect cast-compatible with `index` type. Does not
1327 /// report the error, the user is expected to produce an appropriate message.
1328 // TODO: make the size depend on data layout rather than on the conversion
1329 // pass option, and pull that information here.
1330 static LogicalResult verifyCastWithIndex(Type llvmType) {
1331   return success(llvmType.isa<IntegerType>());
1332 }
1333 
1334 /// Checks if `llvmType` is dialect cast-compatible with built-in `type` and
1335 /// reports errors to the location of `op`. `isElement` indicates whether the
1336 /// verification is performed for types that are element types inside a
1337 /// container; we don't want casts from X to X at the top level, but c1<X> to
1338 /// c2<X> may be fine.
1339 static LogicalResult verifyCast(DialectCastOp op, Type llvmType, Type type,
1340                                 bool isElement = false) {
1341   // Equal element types are directly compatible.
1342   if (isElement && llvmType == type)
1343     return success();
1344 
1345   // Index is compatible with any integer.
1346   if (type.isIndex()) {
1347     if (succeeded(verifyCastWithIndex(llvmType)))
1348       return success();
1349 
1350     return op.emitOpError("invalid cast between index and non-integer type");
1351   }
1352 
1353   if (type.isa<IntegerType>()) {
1354     auto llvmIntegerType = llvmType.dyn_cast<IntegerType>();
1355     if (!llvmIntegerType)
1356       return op->emitOpError("invalid cast between integer and non-integer");
1357     if (llvmIntegerType.getWidth() != type.getIntOrFloatBitWidth())
1358       return op.emitOpError("invalid cast changing integer width");
1359     return success();
1360   }
1361 
1362   // Vectors are compatible if they are 1D non-scalable, and their element types
1363   // are compatible. nD vectors are compatible with (n-1)D arrays containing 1D
1364   // vector.
1365   if (auto vectorType = type.dyn_cast<VectorType>()) {
1366     if (vectorType == llvmType && !isElement)
1367       return op.emitOpError("vector types should not be casted");
1368 
1369     if (vectorType.getRank() == 1) {
1370       auto llvmVectorType = llvmType.dyn_cast<VectorType>();
1371       if (!llvmVectorType || llvmVectorType.getRank() != 1)
1372         return op.emitOpError("invalid cast for vector types");
1373 
1374       return verifyCast(op, llvmVectorType.getElementType(),
1375                         vectorType.getElementType(), /*isElement=*/true);
1376     }
1377 
1378     auto arrayType = llvmType.dyn_cast<LLVM::LLVMArrayType>();
1379     if (!arrayType ||
1380         arrayType.getNumElements() != vectorType.getShape().front())
1381       return op.emitOpError("invalid cast for vector, expected array");
1382     return verifyCast(op, arrayType.getElementType(),
1383                       VectorType::get(vectorType.getShape().drop_front(),
1384                                       vectorType.getElementType()),
1385                       /*isElement=*/true);
1386   }
1387 
1388   if (auto memrefType = type.dyn_cast<MemRefType>()) {
1389     // Bare pointer convention: statically-shaped memref is compatible with an
1390     // LLVM pointer to the element type.
1391     if (auto ptrType = llvmType.dyn_cast<LLVMPointerType>()) {
1392       if (!memrefType.hasStaticShape())
1393         return op->emitOpError(
1394             "unexpected bare pointer for dynamically shaped memref");
1395       if (memrefType.getMemorySpaceAsInt() != ptrType.getAddressSpace())
1396         return op->emitError("invalid conversion between memref and pointer in "
1397                              "different memory spaces");
1398 
1399       return verifyCast(op, ptrType.getElementType(),
1400                         memrefType.getElementType(), /*isElement=*/true);
1401     }
1402 
1403     // Otherwise, memrefs are convertible to a descriptor, which is a structure
1404     // type.
1405     auto structType = llvmType.dyn_cast<LLVMStructType>();
1406     if (!structType)
1407       return op->emitOpError("invalid cast between a memref and a type other "
1408                              "than pointer or memref descriptor");
1409 
1410     unsigned expectedNumElements = memrefType.getRank() == 0 ? 3 : 5;
1411     if (structType.getBody().size() != expectedNumElements) {
1412       return op->emitOpError() << "expected memref descriptor with "
1413                                << expectedNumElements << " elements";
1414     }
1415 
1416     // The first two elements are pointers to the element type.
1417     auto allocatedPtr = structType.getBody()[0].dyn_cast<LLVMPointerType>();
1418     if (!allocatedPtr ||
1419         allocatedPtr.getAddressSpace() != memrefType.getMemorySpaceAsInt())
1420       return op->emitOpError("expected first element of a memref descriptor to "
1421                              "be a pointer in the address space of the memref");
1422     if (failed(verifyCast(op, allocatedPtr.getElementType(),
1423                           memrefType.getElementType(), /*isElement=*/true)))
1424       return failure();
1425 
1426     auto alignedPtr = structType.getBody()[1].dyn_cast<LLVMPointerType>();
1427     if (!alignedPtr ||
1428         alignedPtr.getAddressSpace() != memrefType.getMemorySpaceAsInt())
1429       return op->emitOpError(
1430           "expected second element of a memref descriptor to "
1431           "be a pointer in the address space of the memref");
1432     if (failed(verifyCast(op, alignedPtr.getElementType(),
1433                           memrefType.getElementType(), /*isElement=*/true)))
1434       return failure();
1435 
1436     // The second element (offset) is an equivalent of index.
1437     if (failed(verifyCastWithIndex(structType.getBody()[2])))
1438       return op->emitOpError("expected third element of a memref descriptor to "
1439                              "be index-compatible integers");
1440 
1441     // 0D memrefs don't have sizes/strides.
1442     if (memrefType.getRank() == 0)
1443       return success();
1444 
1445     // Sizes and strides are rank-sized arrays of `index` equivalents.
1446     auto sizes = structType.getBody()[3].dyn_cast<LLVMArrayType>();
1447     if (!sizes || failed(verifyCastWithIndex(sizes.getElementType())) ||
1448         sizes.getNumElements() != memrefType.getRank())
1449       return op->emitOpError(
1450           "expected fourth element of a memref descriptor "
1451           "to be an array of <rank> index-compatible integers");
1452 
1453     auto strides = structType.getBody()[4].dyn_cast<LLVMArrayType>();
1454     if (!strides || failed(verifyCastWithIndex(strides.getElementType())) ||
1455         strides.getNumElements() != memrefType.getRank())
1456       return op->emitOpError(
1457           "expected fifth element of a memref descriptor "
1458           "to be an array of <rank> index-compatible integers");
1459 
1460     return success();
1461   }
1462 
1463   // Unranked memrefs are compatible with their descriptors.
1464   if (auto unrankedMemrefType = type.dyn_cast<UnrankedMemRefType>()) {
1465     auto structType = llvmType.dyn_cast<LLVMStructType>();
1466     if (!structType || structType.getBody().size() != 2)
1467       return op->emitOpError(
1468           "expected descriptor to be a struct with two elements");
1469 
1470     if (failed(verifyCastWithIndex(structType.getBody()[0])))
1471       return op->emitOpError("expected first element of a memref descriptor to "
1472                              "be an index-compatible integer");
1473 
1474     auto ptrType = structType.getBody()[1].dyn_cast<LLVMPointerType>();
1475     auto ptrElementType =
1476         ptrType ? ptrType.getElementType().dyn_cast<IntegerType>() : nullptr;
1477     if (!ptrElementType || ptrElementType.getWidth() != 8)
1478       return op->emitOpError("expected second element of a memref descriptor "
1479                              "to be an !llvm.ptr<i8>");
1480 
1481     return success();
1482   }
1483 
1484   // Complex types are compatible with the two-element structs.
1485   if (auto complexType = type.dyn_cast<ComplexType>()) {
1486     auto structType = llvmType.dyn_cast<LLVMStructType>();
1487     if (!structType || structType.getBody().size() != 2 ||
1488         structType.getBody()[0] != structType.getBody()[1] ||
1489         structType.getBody()[0] != complexType.getElementType())
1490       return op->emitOpError("expected 'complex' to map to two-element struct "
1491                              "with identical element types");
1492     return success();
1493   }
1494 
1495   // Everything else is not supported.
1496   return op->emitError("unsupported cast");
1497 }
1498 
1499 static LogicalResult verify(DialectCastOp op) {
1500   if (isCompatibleType(op.getType()))
1501     return verifyCast(op, op.getType(), op.in().getType());
1502 
1503   if (!isCompatibleType(op.in().getType()))
1504     return op->emitOpError("expected one LLVM type and one built-in type");
1505 
1506   return verifyCast(op, op.in().getType(), op.getType());
1507 }
1508 
1509 // Parses one of the keywords provided in the list `keywords` and returns the
1510 // position of the parsed keyword in the list. If none of the keywords from the
1511 // list is parsed, returns -1.
1512 static int parseOptionalKeywordAlternative(OpAsmParser &parser,
1513                                            ArrayRef<StringRef> keywords) {
1514   for (auto en : llvm::enumerate(keywords)) {
1515     if (succeeded(parser.parseOptionalKeyword(en.value())))
1516       return en.index();
1517   }
1518   return -1;
1519 }
1520 
1521 namespace {
1522 template <typename Ty>
1523 struct EnumTraits {};
1524 
1525 #define REGISTER_ENUM_TYPE(Ty)                                                 \
1526   template <>                                                                  \
1527   struct EnumTraits<Ty> {                                                      \
1528     static StringRef stringify(Ty value) { return stringify##Ty(value); }      \
1529     static unsigned getMaxEnumVal() { return getMaxEnumValFor##Ty(); }         \
1530   }
1531 
1532 REGISTER_ENUM_TYPE(Linkage);
1533 REGISTER_ENUM_TYPE(UnnamedAddr);
1534 } // end namespace
1535 
1536 template <typename EnumTy>
1537 static ParseResult parseOptionalLLVMKeyword(OpAsmParser &parser,
1538                                             OperationState &result,
1539                                             StringRef name) {
1540   SmallVector<StringRef, 10> names;
1541   for (unsigned i = 0, e = getMaxEnumValForLinkage(); i <= e; ++i)
1542     names.push_back(EnumTraits<EnumTy>::stringify(static_cast<EnumTy>(i)));
1543 
1544   int index = parseOptionalKeywordAlternative(parser, names);
1545   if (index == -1)
1546     return failure();
1547   result.addAttribute(name, parser.getBuilder().getI64IntegerAttr(index));
1548   return success();
1549 }
1550 
1551 // operation ::= `llvm.mlir.global` linkage? `constant`? `@` identifier
1552 //               `(` attribute? `)` align? attribute-list? (`:` type)? region?
1553 // align     ::= `align` `=` UINT64
1554 //
1555 // The type can be omitted for string attributes, in which case it will be
1556 // inferred from the value of the string as [strlen(value) x i8].
1557 static ParseResult parseGlobalOp(OpAsmParser &parser, OperationState &result) {
1558   if (failed(parseOptionalLLVMKeyword<Linkage>(parser, result,
1559                                                getLinkageAttrName())))
1560     result.addAttribute(getLinkageAttrName(),
1561                         parser.getBuilder().getI64IntegerAttr(
1562                             static_cast<int64_t>(LLVM::Linkage::External)));
1563 
1564   if (failed(parseOptionalLLVMKeyword<UnnamedAddr>(parser, result,
1565                                                    getUnnamedAddrAttrName())))
1566     result.addAttribute(getUnnamedAddrAttrName(),
1567                         parser.getBuilder().getI64IntegerAttr(
1568                             static_cast<int64_t>(LLVM::UnnamedAddr::None)));
1569 
1570   if (succeeded(parser.parseOptionalKeyword("constant")))
1571     result.addAttribute("constant", parser.getBuilder().getUnitAttr());
1572 
1573   StringAttr name;
1574   if (parser.parseSymbolName(name, SymbolTable::getSymbolAttrName(),
1575                              result.attributes) ||
1576       parser.parseLParen())
1577     return failure();
1578 
1579   Attribute value;
1580   if (parser.parseOptionalRParen()) {
1581     if (parser.parseAttribute(value, "value", result.attributes) ||
1582         parser.parseRParen())
1583       return failure();
1584   }
1585 
1586   SmallVector<Type, 1> types;
1587   if (parser.parseOptionalAttrDict(result.attributes) ||
1588       parser.parseOptionalColonTypeList(types))
1589     return failure();
1590 
1591   if (types.size() > 1)
1592     return parser.emitError(parser.getNameLoc(), "expected zero or one type");
1593 
1594   Region &initRegion = *result.addRegion();
1595   if (types.empty()) {
1596     if (auto strAttr = value.dyn_cast_or_null<StringAttr>()) {
1597       MLIRContext *context = parser.getBuilder().getContext();
1598       auto arrayType = LLVM::LLVMArrayType::get(IntegerType::get(context, 8),
1599                                                 strAttr.getValue().size());
1600       types.push_back(arrayType);
1601     } else {
1602       return parser.emitError(parser.getNameLoc(),
1603                               "type can only be omitted for string globals");
1604     }
1605   } else {
1606     OptionalParseResult parseResult =
1607         parser.parseOptionalRegion(initRegion, /*arguments=*/{},
1608                                    /*argTypes=*/{});
1609     if (parseResult.hasValue() && failed(*parseResult))
1610       return failure();
1611   }
1612 
1613   result.addAttribute("type", TypeAttr::get(types[0]));
1614   return success();
1615 }
1616 
1617 static bool isZeroAttribute(Attribute value) {
1618   if (auto intValue = value.dyn_cast<IntegerAttr>())
1619     return intValue.getValue().isNullValue();
1620   if (auto fpValue = value.dyn_cast<FloatAttr>())
1621     return fpValue.getValue().isZero();
1622   if (auto splatValue = value.dyn_cast<SplatElementsAttr>())
1623     return isZeroAttribute(splatValue.getSplatValue());
1624   if (auto elementsValue = value.dyn_cast<ElementsAttr>())
1625     return llvm::all_of(elementsValue.getValues<Attribute>(), isZeroAttribute);
1626   if (auto arrayValue = value.dyn_cast<ArrayAttr>())
1627     return llvm::all_of(arrayValue.getValue(), isZeroAttribute);
1628   return false;
1629 }
1630 
1631 static LogicalResult verify(GlobalOp op) {
1632   if (!LLVMPointerType::isValidElementType(op.getType()))
1633     return op.emitOpError(
1634         "expects type to be a valid element type for an LLVM pointer");
1635   if (op->getParentOp() && !satisfiesLLVMModule(op->getParentOp()))
1636     return op.emitOpError("must appear at the module level");
1637 
1638   if (auto strAttr = op.getValueOrNull().dyn_cast_or_null<StringAttr>()) {
1639     auto type = op.getType().dyn_cast<LLVMArrayType>();
1640     IntegerType elementType =
1641         type ? type.getElementType().dyn_cast<IntegerType>() : nullptr;
1642     if (!elementType || elementType.getWidth() != 8 ||
1643         type.getNumElements() != strAttr.getValue().size())
1644       return op.emitOpError(
1645           "requires an i8 array type of the length equal to that of the string "
1646           "attribute");
1647   }
1648 
1649   if (Block *b = op.getInitializerBlock()) {
1650     ReturnOp ret = cast<ReturnOp>(b->getTerminator());
1651     if (ret.operand_type_begin() == ret.operand_type_end())
1652       return op.emitOpError("initializer region cannot return void");
1653     if (*ret.operand_type_begin() != op.getType())
1654       return op.emitOpError("initializer region type ")
1655              << *ret.operand_type_begin() << " does not match global type "
1656              << op.getType();
1657 
1658     if (op.getValueOrNull())
1659       return op.emitOpError("cannot have both initializer value and region");
1660   }
1661 
1662   if (op.linkage() == Linkage::Common) {
1663     if (Attribute value = op.getValueOrNull()) {
1664       if (!isZeroAttribute(value)) {
1665         return op.emitOpError()
1666                << "expected zero value for '"
1667                << stringifyLinkage(Linkage::Common) << "' linkage";
1668       }
1669     }
1670   }
1671 
1672   if (op.linkage() == Linkage::Appending) {
1673     if (!op.getType().isa<LLVMArrayType>()) {
1674       return op.emitOpError()
1675              << "expected array type for '"
1676              << stringifyLinkage(Linkage::Appending) << "' linkage";
1677     }
1678   }
1679 
1680   Optional<uint64_t> alignAttr = op.alignment();
1681   if (alignAttr.hasValue()) {
1682     uint64_t value = alignAttr.getValue();
1683     if (!llvm::isPowerOf2_64(value))
1684       return op->emitError() << "alignment attribute is not a power of 2";
1685   }
1686 
1687   return success();
1688 }
1689 
1690 //===----------------------------------------------------------------------===//
1691 // Printing/parsing for LLVM::ShuffleVectorOp.
1692 //===----------------------------------------------------------------------===//
1693 // Expects vector to be of wrapped LLVM vector type and position to be of
1694 // wrapped LLVM i32 type.
1695 void LLVM::ShuffleVectorOp::build(OpBuilder &b, OperationState &result,
1696                                   Value v1, Value v2, ArrayAttr mask,
1697                                   ArrayRef<NamedAttribute> attrs) {
1698   auto containerType = v1.getType();
1699   auto vType = LLVM::getFixedVectorType(
1700       LLVM::getVectorElementType(containerType), mask.size());
1701   build(b, result, vType, v1, v2, mask);
1702   result.addAttributes(attrs);
1703 }
1704 
1705 static void printShuffleVectorOp(OpAsmPrinter &p, ShuffleVectorOp &op) {
1706   p << op.getOperationName() << ' ' << op.v1() << ", " << op.v2() << " "
1707     << op.mask();
1708   p.printOptionalAttrDict(op->getAttrs(), {"mask"});
1709   p << " : " << op.v1().getType() << ", " << op.v2().getType();
1710 }
1711 
1712 // <operation> ::= `llvm.shufflevector` ssa-use `, ` ssa-use
1713 //                 `[` integer-literal (`,` integer-literal)* `]`
1714 //                 attribute-dict? `:` type
1715 static ParseResult parseShuffleVectorOp(OpAsmParser &parser,
1716                                         OperationState &result) {
1717   llvm::SMLoc loc;
1718   OpAsmParser::OperandType v1, v2;
1719   ArrayAttr maskAttr;
1720   Type typeV1, typeV2;
1721   if (parser.getCurrentLocation(&loc) || parser.parseOperand(v1) ||
1722       parser.parseComma() || parser.parseOperand(v2) ||
1723       parser.parseAttribute(maskAttr, "mask", result.attributes) ||
1724       parser.parseOptionalAttrDict(result.attributes) ||
1725       parser.parseColonType(typeV1) || parser.parseComma() ||
1726       parser.parseType(typeV2) ||
1727       parser.resolveOperand(v1, typeV1, result.operands) ||
1728       parser.resolveOperand(v2, typeV2, result.operands))
1729     return failure();
1730   if (!LLVM::isCompatibleVectorType(typeV1))
1731     return parser.emitError(
1732         loc, "expected LLVM IR dialect vector type for operand #1");
1733   auto vType = LLVM::getFixedVectorType(LLVM::getVectorElementType(typeV1),
1734                                         maskAttr.size());
1735   result.addTypes(vType);
1736   return success();
1737 }
1738 
1739 //===----------------------------------------------------------------------===//
1740 // Implementations for LLVM::LLVMFuncOp.
1741 //===----------------------------------------------------------------------===//
1742 
1743 // Add the entry block to the function.
1744 Block *LLVMFuncOp::addEntryBlock() {
1745   assert(empty() && "function already has an entry block");
1746   assert(!isVarArg() && "unimplemented: non-external variadic functions");
1747 
1748   auto *entry = new Block;
1749   push_back(entry);
1750 
1751   LLVMFunctionType type = getType();
1752   for (unsigned i = 0, e = type.getNumParams(); i < e; ++i)
1753     entry->addArgument(type.getParamType(i));
1754   return entry;
1755 }
1756 
1757 void LLVMFuncOp::build(OpBuilder &builder, OperationState &result,
1758                        StringRef name, Type type, LLVM::Linkage linkage,
1759                        ArrayRef<NamedAttribute> attrs,
1760                        ArrayRef<DictionaryAttr> argAttrs) {
1761   result.addRegion();
1762   result.addAttribute(SymbolTable::getSymbolAttrName(),
1763                       builder.getStringAttr(name));
1764   result.addAttribute("type", TypeAttr::get(type));
1765   result.addAttribute(getLinkageAttrName(),
1766                       builder.getI64IntegerAttr(static_cast<int64_t>(linkage)));
1767   result.attributes.append(attrs.begin(), attrs.end());
1768   if (argAttrs.empty())
1769     return;
1770 
1771   assert(type.cast<LLVMFunctionType>().getNumParams() == argAttrs.size() &&
1772          "expected as many argument attribute lists as arguments");
1773   function_like_impl::addArgAndResultAttrs(builder, result, argAttrs,
1774                                            /*resultAttrs=*/llvm::None);
1775 }
1776 
1777 // Builds an LLVM function type from the given lists of input and output types.
1778 // Returns a null type if any of the types provided are non-LLVM types, or if
1779 // there is more than one output type.
1780 static Type
1781 buildLLVMFunctionType(OpAsmParser &parser, llvm::SMLoc loc,
1782                       ArrayRef<Type> inputs, ArrayRef<Type> outputs,
1783                       function_like_impl::VariadicFlag variadicFlag) {
1784   Builder &b = parser.getBuilder();
1785   if (outputs.size() > 1) {
1786     parser.emitError(loc, "failed to construct function type: expected zero or "
1787                           "one function result");
1788     return {};
1789   }
1790 
1791   // Convert inputs to LLVM types, exit early on error.
1792   SmallVector<Type, 4> llvmInputs;
1793   for (auto t : inputs) {
1794     if (!isCompatibleType(t)) {
1795       parser.emitError(loc, "failed to construct function type: expected LLVM "
1796                             "type for function arguments");
1797       return {};
1798     }
1799     llvmInputs.push_back(t);
1800   }
1801 
1802   // No output is denoted as "void" in LLVM type system.
1803   Type llvmOutput =
1804       outputs.empty() ? LLVMVoidType::get(b.getContext()) : outputs.front();
1805   if (!isCompatibleType(llvmOutput)) {
1806     parser.emitError(loc, "failed to construct function type: expected LLVM "
1807                           "type for function results")
1808         << llvmOutput;
1809     return {};
1810   }
1811   return LLVMFunctionType::get(llvmOutput, llvmInputs,
1812                                variadicFlag.isVariadic());
1813 }
1814 
1815 // Parses an LLVM function.
1816 //
1817 // operation ::= `llvm.func` linkage? function-signature function-attributes?
1818 //               function-body
1819 //
1820 static ParseResult parseLLVMFuncOp(OpAsmParser &parser,
1821                                    OperationState &result) {
1822   // Default to external linkage if no keyword is provided.
1823   if (failed(parseOptionalLLVMKeyword<Linkage>(parser, result,
1824                                                getLinkageAttrName())))
1825     result.addAttribute(getLinkageAttrName(),
1826                         parser.getBuilder().getI64IntegerAttr(
1827                             static_cast<int64_t>(LLVM::Linkage::External)));
1828 
1829   StringAttr nameAttr;
1830   SmallVector<OpAsmParser::OperandType, 8> entryArgs;
1831   SmallVector<NamedAttrList, 1> argAttrs;
1832   SmallVector<NamedAttrList, 1> resultAttrs;
1833   SmallVector<Type, 8> argTypes;
1834   SmallVector<Type, 4> resultTypes;
1835   bool isVariadic;
1836 
1837   auto signatureLocation = parser.getCurrentLocation();
1838   if (parser.parseSymbolName(nameAttr, SymbolTable::getSymbolAttrName(),
1839                              result.attributes) ||
1840       function_like_impl::parseFunctionSignature(
1841           parser, /*allowVariadic=*/true, entryArgs, argTypes, argAttrs,
1842           isVariadic, resultTypes, resultAttrs))
1843     return failure();
1844 
1845   auto type =
1846       buildLLVMFunctionType(parser, signatureLocation, argTypes, resultTypes,
1847                             function_like_impl::VariadicFlag(isVariadic));
1848   if (!type)
1849     return failure();
1850   result.addAttribute(function_like_impl::getTypeAttrName(),
1851                       TypeAttr::get(type));
1852 
1853   if (failed(parser.parseOptionalAttrDictWithKeyword(result.attributes)))
1854     return failure();
1855   function_like_impl::addArgAndResultAttrs(parser.getBuilder(), result,
1856                                            argAttrs, resultAttrs);
1857 
1858   auto *body = result.addRegion();
1859   OptionalParseResult parseResult = parser.parseOptionalRegion(
1860       *body, entryArgs, entryArgs.empty() ? ArrayRef<Type>() : argTypes);
1861   return failure(parseResult.hasValue() && failed(*parseResult));
1862 }
1863 
1864 // Print the LLVMFuncOp. Collects argument and result types and passes them to
1865 // helper functions. Drops "void" result since it cannot be parsed back. Skips
1866 // the external linkage since it is the default value.
1867 static void printLLVMFuncOp(OpAsmPrinter &p, LLVMFuncOp op) {
1868   p << op.getOperationName() << ' ';
1869   if (op.linkage() != LLVM::Linkage::External)
1870     p << stringifyLinkage(op.linkage()) << ' ';
1871   p.printSymbolName(op.getName());
1872 
1873   LLVMFunctionType fnType = op.getType();
1874   SmallVector<Type, 8> argTypes;
1875   SmallVector<Type, 1> resTypes;
1876   argTypes.reserve(fnType.getNumParams());
1877   for (unsigned i = 0, e = fnType.getNumParams(); i < e; ++i)
1878     argTypes.push_back(fnType.getParamType(i));
1879 
1880   Type returnType = fnType.getReturnType();
1881   if (!returnType.isa<LLVMVoidType>())
1882     resTypes.push_back(returnType);
1883 
1884   function_like_impl::printFunctionSignature(p, op, argTypes, op.isVarArg(),
1885                                              resTypes);
1886   function_like_impl::printFunctionAttributes(
1887       p, op, argTypes.size(), resTypes.size(), {getLinkageAttrName()});
1888 
1889   // Print the body if this is not an external function.
1890   Region &body = op.body();
1891   if (!body.empty())
1892     p.printRegion(body, /*printEntryBlockArgs=*/false,
1893                   /*printBlockTerminators=*/true);
1894 }
1895 
1896 // Hook for OpTrait::FunctionLike, called after verifying that the 'type'
1897 // attribute is present.  This can check for preconditions of the
1898 // getNumArguments hook not failing.
1899 LogicalResult LLVMFuncOp::verifyType() {
1900   auto llvmType = getTypeAttr().getValue().dyn_cast_or_null<LLVMFunctionType>();
1901   if (!llvmType)
1902     return emitOpError("requires '" + getTypeAttrName() +
1903                        "' attribute of wrapped LLVM function type");
1904 
1905   return success();
1906 }
1907 
1908 // Hook for OpTrait::FunctionLike, returns the number of function arguments.
1909 // Depends on the type attribute being correct as checked by verifyType
1910 unsigned LLVMFuncOp::getNumFuncArguments() { return getType().getNumParams(); }
1911 
1912 // Hook for OpTrait::FunctionLike, returns the number of function results.
1913 // Depends on the type attribute being correct as checked by verifyType
1914 unsigned LLVMFuncOp::getNumFuncResults() {
1915   // We model LLVM functions that return void as having zero results,
1916   // and all others as having one result.
1917   // If we modeled a void return as one result, then it would be possible to
1918   // attach an MLIR result attribute to it, and it isn't clear what semantics we
1919   // would assign to that.
1920   if (getType().getReturnType().isa<LLVMVoidType>())
1921     return 0;
1922   return 1;
1923 }
1924 
1925 // Verifies LLVM- and implementation-specific properties of the LLVM func Op:
1926 // - functions don't have 'common' linkage
1927 // - external functions have 'external' or 'extern_weak' linkage;
1928 // - vararg is (currently) only supported for external functions;
1929 // - entry block arguments are of LLVM types and match the function signature.
1930 static LogicalResult verify(LLVMFuncOp op) {
1931   if (op.linkage() == LLVM::Linkage::Common)
1932     return op.emitOpError()
1933            << "functions cannot have '"
1934            << stringifyLinkage(LLVM::Linkage::Common) << "' linkage";
1935 
1936   if (op.isExternal()) {
1937     if (op.linkage() != LLVM::Linkage::External &&
1938         op.linkage() != LLVM::Linkage::ExternWeak)
1939       return op.emitOpError()
1940              << "external functions must have '"
1941              << stringifyLinkage(LLVM::Linkage::External) << "' or '"
1942              << stringifyLinkage(LLVM::Linkage::ExternWeak) << "' linkage";
1943     return success();
1944   }
1945 
1946   if (op.isVarArg())
1947     return op.emitOpError("only external functions can be variadic");
1948 
1949   unsigned numArguments = op.getType().getNumParams();
1950   Block &entryBlock = op.front();
1951   for (unsigned i = 0; i < numArguments; ++i) {
1952     Type argType = entryBlock.getArgument(i).getType();
1953     if (!isCompatibleType(argType))
1954       return op.emitOpError("entry block argument #")
1955              << i << " is not of LLVM type";
1956     if (op.getType().getParamType(i) != argType)
1957       return op.emitOpError("the type of entry block argument #")
1958              << i << " does not match the function signature";
1959   }
1960 
1961   return success();
1962 }
1963 
1964 //===----------------------------------------------------------------------===//
1965 // Verification for LLVM::ConstantOp.
1966 //===----------------------------------------------------------------------===//
1967 
1968 static LogicalResult verify(LLVM::ConstantOp op) {
1969   if (StringAttr sAttr = op.value().dyn_cast<StringAttr>()) {
1970     auto arrayType = op.getType().dyn_cast<LLVMArrayType>();
1971     if (!arrayType || arrayType.getNumElements() != sAttr.getValue().size() ||
1972         !arrayType.getElementType().isInteger(8)) {
1973       return op->emitOpError()
1974              << "expected array type of " << sAttr.getValue().size()
1975              << " i8 elements for the string constant";
1976     }
1977     return success();
1978   }
1979   if (auto structType = op.getType().dyn_cast<LLVMStructType>()) {
1980     if (structType.getBody().size() != 2 ||
1981         structType.getBody()[0] != structType.getBody()[1]) {
1982       return op.emitError() << "expected struct type with two elements of the "
1983                                "same type, the type of a complex constant";
1984     }
1985 
1986     auto arrayAttr = op.value().dyn_cast<ArrayAttr>();
1987     if (!arrayAttr || arrayAttr.size() != 2 ||
1988         arrayAttr[0].getType() != arrayAttr[1].getType()) {
1989       return op.emitOpError() << "expected array attribute with two elements, "
1990                                  "representing a complex constant";
1991     }
1992 
1993     Type elementType = structType.getBody()[0];
1994     if (!elementType
1995              .isa<IntegerType, Float16Type, Float32Type, Float64Type>()) {
1996       return op.emitError()
1997              << "expected struct element types to be floating point type or "
1998                 "integer type";
1999     }
2000     return success();
2001   }
2002   if (!op.value().isa<IntegerAttr, ArrayAttr, FloatAttr, ElementsAttr>())
2003     return op.emitOpError()
2004            << "only supports integer, float, string or elements attributes";
2005   return success();
2006 }
2007 
2008 //===----------------------------------------------------------------------===//
2009 // Utility functions for parsing atomic ops
2010 //===----------------------------------------------------------------------===//
2011 
2012 // Helper function to parse a keyword into the specified attribute named by
2013 // `attrName`. The keyword must match one of the string values defined by the
2014 // AtomicBinOp enum. The resulting I64 attribute is added to the `result`
2015 // state.
2016 static ParseResult parseAtomicBinOp(OpAsmParser &parser, OperationState &result,
2017                                     StringRef attrName) {
2018   llvm::SMLoc loc;
2019   StringRef keyword;
2020   if (parser.getCurrentLocation(&loc) || parser.parseKeyword(&keyword))
2021     return failure();
2022 
2023   // Replace the keyword `keyword` with an integer attribute.
2024   auto kind = symbolizeAtomicBinOp(keyword);
2025   if (!kind) {
2026     return parser.emitError(loc)
2027            << "'" << keyword << "' is an incorrect value of the '" << attrName
2028            << "' attribute";
2029   }
2030 
2031   auto value = static_cast<int64_t>(kind.getValue());
2032   auto attr = parser.getBuilder().getI64IntegerAttr(value);
2033   result.addAttribute(attrName, attr);
2034 
2035   return success();
2036 }
2037 
2038 // Helper function to parse a keyword into the specified attribute named by
2039 // `attrName`. The keyword must match one of the string values defined by the
2040 // AtomicOrdering enum. The resulting I64 attribute is added to the `result`
2041 // state.
2042 static ParseResult parseAtomicOrdering(OpAsmParser &parser,
2043                                        OperationState &result,
2044                                        StringRef attrName) {
2045   llvm::SMLoc loc;
2046   StringRef ordering;
2047   if (parser.getCurrentLocation(&loc) || parser.parseKeyword(&ordering))
2048     return failure();
2049 
2050   // Replace the keyword `ordering` with an integer attribute.
2051   auto kind = symbolizeAtomicOrdering(ordering);
2052   if (!kind) {
2053     return parser.emitError(loc)
2054            << "'" << ordering << "' is an incorrect value of the '" << attrName
2055            << "' attribute";
2056   }
2057 
2058   auto value = static_cast<int64_t>(kind.getValue());
2059   auto attr = parser.getBuilder().getI64IntegerAttr(value);
2060   result.addAttribute(attrName, attr);
2061 
2062   return success();
2063 }
2064 
2065 //===----------------------------------------------------------------------===//
2066 // Printer, parser and verifier for LLVM::AtomicRMWOp.
2067 //===----------------------------------------------------------------------===//
2068 
2069 static void printAtomicRMWOp(OpAsmPrinter &p, AtomicRMWOp &op) {
2070   p << op.getOperationName() << ' ' << stringifyAtomicBinOp(op.bin_op()) << ' '
2071     << op.ptr() << ", " << op.val() << ' '
2072     << stringifyAtomicOrdering(op.ordering()) << ' ';
2073   p.printOptionalAttrDict(op->getAttrs(), {"bin_op", "ordering"});
2074   p << " : " << op.res().getType();
2075 }
2076 
2077 // <operation> ::= `llvm.atomicrmw` keyword ssa-use `,` ssa-use keyword
2078 //                 attribute-dict? `:` type
2079 static ParseResult parseAtomicRMWOp(OpAsmParser &parser,
2080                                     OperationState &result) {
2081   Type type;
2082   OpAsmParser::OperandType ptr, val;
2083   if (parseAtomicBinOp(parser, result, "bin_op") || parser.parseOperand(ptr) ||
2084       parser.parseComma() || parser.parseOperand(val) ||
2085       parseAtomicOrdering(parser, result, "ordering") ||
2086       parser.parseOptionalAttrDict(result.attributes) ||
2087       parser.parseColonType(type) ||
2088       parser.resolveOperand(ptr, LLVM::LLVMPointerType::get(type),
2089                             result.operands) ||
2090       parser.resolveOperand(val, type, result.operands))
2091     return failure();
2092 
2093   result.addTypes(type);
2094   return success();
2095 }
2096 
2097 static LogicalResult verify(AtomicRMWOp op) {
2098   auto ptrType = op.ptr().getType().cast<LLVM::LLVMPointerType>();
2099   auto valType = op.val().getType();
2100   if (valType != ptrType.getElementType())
2101     return op.emitOpError("expected LLVM IR element type for operand #0 to "
2102                           "match type for operand #1");
2103   auto resType = op.res().getType();
2104   if (resType != valType)
2105     return op.emitOpError(
2106         "expected LLVM IR result type to match type for operand #1");
2107   if (op.bin_op() == AtomicBinOp::fadd || op.bin_op() == AtomicBinOp::fsub) {
2108     if (!mlir::LLVM::isCompatibleFloatingPointType(valType))
2109       return op.emitOpError("expected LLVM IR floating point type");
2110   } else if (op.bin_op() == AtomicBinOp::xchg) {
2111     auto intType = valType.dyn_cast<IntegerType>();
2112     unsigned intBitWidth = intType ? intType.getWidth() : 0;
2113     if (intBitWidth != 8 && intBitWidth != 16 && intBitWidth != 32 &&
2114         intBitWidth != 64 && !valType.isa<BFloat16Type>() &&
2115         !valType.isa<Float16Type>() && !valType.isa<Float32Type>() &&
2116         !valType.isa<Float64Type>())
2117       return op.emitOpError("unexpected LLVM IR type for 'xchg' bin_op");
2118   } else {
2119     auto intType = valType.dyn_cast<IntegerType>();
2120     unsigned intBitWidth = intType ? intType.getWidth() : 0;
2121     if (intBitWidth != 8 && intBitWidth != 16 && intBitWidth != 32 &&
2122         intBitWidth != 64)
2123       return op.emitOpError("expected LLVM IR integer type");
2124   }
2125 
2126   if (static_cast<unsigned>(op.ordering()) <
2127       static_cast<unsigned>(AtomicOrdering::monotonic))
2128     return op.emitOpError()
2129            << "expected at least '"
2130            << stringifyAtomicOrdering(AtomicOrdering::monotonic)
2131            << "' ordering";
2132 
2133   return success();
2134 }
2135 
2136 //===----------------------------------------------------------------------===//
2137 // Printer, parser and verifier for LLVM::AtomicCmpXchgOp.
2138 //===----------------------------------------------------------------------===//
2139 
2140 static void printAtomicCmpXchgOp(OpAsmPrinter &p, AtomicCmpXchgOp &op) {
2141   p << op.getOperationName() << ' ' << op.ptr() << ", " << op.cmp() << ", "
2142     << op.val() << ' ' << stringifyAtomicOrdering(op.success_ordering()) << ' '
2143     << stringifyAtomicOrdering(op.failure_ordering());
2144   p.printOptionalAttrDict(op->getAttrs(),
2145                           {"success_ordering", "failure_ordering"});
2146   p << " : " << op.val().getType();
2147 }
2148 
2149 // <operation> ::= `llvm.cmpxchg` ssa-use `,` ssa-use `,` ssa-use
2150 //                 keyword keyword attribute-dict? `:` type
2151 static ParseResult parseAtomicCmpXchgOp(OpAsmParser &parser,
2152                                         OperationState &result) {
2153   auto &builder = parser.getBuilder();
2154   Type type;
2155   OpAsmParser::OperandType ptr, cmp, val;
2156   if (parser.parseOperand(ptr) || parser.parseComma() ||
2157       parser.parseOperand(cmp) || parser.parseComma() ||
2158       parser.parseOperand(val) ||
2159       parseAtomicOrdering(parser, result, "success_ordering") ||
2160       parseAtomicOrdering(parser, result, "failure_ordering") ||
2161       parser.parseOptionalAttrDict(result.attributes) ||
2162       parser.parseColonType(type) ||
2163       parser.resolveOperand(ptr, LLVM::LLVMPointerType::get(type),
2164                             result.operands) ||
2165       parser.resolveOperand(cmp, type, result.operands) ||
2166       parser.resolveOperand(val, type, result.operands))
2167     return failure();
2168 
2169   auto boolType = IntegerType::get(builder.getContext(), 1);
2170   auto resultType =
2171       LLVMStructType::getLiteral(builder.getContext(), {type, boolType});
2172   result.addTypes(resultType);
2173 
2174   return success();
2175 }
2176 
2177 static LogicalResult verify(AtomicCmpXchgOp op) {
2178   auto ptrType = op.ptr().getType().cast<LLVM::LLVMPointerType>();
2179   if (!ptrType)
2180     return op.emitOpError("expected LLVM IR pointer type for operand #0");
2181   auto cmpType = op.cmp().getType();
2182   auto valType = op.val().getType();
2183   if (cmpType != ptrType.getElementType() || cmpType != valType)
2184     return op.emitOpError("expected LLVM IR element type for operand #0 to "
2185                           "match type for all other operands");
2186   auto intType = valType.dyn_cast<IntegerType>();
2187   unsigned intBitWidth = intType ? intType.getWidth() : 0;
2188   if (!valType.isa<LLVMPointerType>() && intBitWidth != 8 &&
2189       intBitWidth != 16 && intBitWidth != 32 && intBitWidth != 64 &&
2190       !valType.isa<BFloat16Type>() && !valType.isa<Float16Type>() &&
2191       !valType.isa<Float32Type>() && !valType.isa<Float64Type>())
2192     return op.emitOpError("unexpected LLVM IR type");
2193   if (op.success_ordering() < AtomicOrdering::monotonic ||
2194       op.failure_ordering() < AtomicOrdering::monotonic)
2195     return op.emitOpError("ordering must be at least 'monotonic'");
2196   if (op.failure_ordering() == AtomicOrdering::release ||
2197       op.failure_ordering() == AtomicOrdering::acq_rel)
2198     return op.emitOpError("failure ordering cannot be 'release' or 'acq_rel'");
2199   return success();
2200 }
2201 
2202 //===----------------------------------------------------------------------===//
2203 // Printer, parser and verifier for LLVM::FenceOp.
2204 //===----------------------------------------------------------------------===//
2205 
2206 // <operation> ::= `llvm.fence` (`syncscope(`strAttr`)`)? keyword
2207 // attribute-dict?
2208 static ParseResult parseFenceOp(OpAsmParser &parser, OperationState &result) {
2209   StringAttr sScope;
2210   StringRef syncscopeKeyword = "syncscope";
2211   if (!failed(parser.parseOptionalKeyword(syncscopeKeyword))) {
2212     if (parser.parseLParen() ||
2213         parser.parseAttribute(sScope, syncscopeKeyword, result.attributes) ||
2214         parser.parseRParen())
2215       return failure();
2216   } else {
2217     result.addAttribute(syncscopeKeyword,
2218                         parser.getBuilder().getStringAttr(""));
2219   }
2220   if (parseAtomicOrdering(parser, result, "ordering") ||
2221       parser.parseOptionalAttrDict(result.attributes))
2222     return failure();
2223   return success();
2224 }
2225 
2226 static void printFenceOp(OpAsmPrinter &p, FenceOp &op) {
2227   StringRef syncscopeKeyword = "syncscope";
2228   p << op.getOperationName() << ' ';
2229   if (!op->getAttr(syncscopeKeyword).cast<StringAttr>().getValue().empty())
2230     p << "syncscope(" << op->getAttr(syncscopeKeyword) << ") ";
2231   p << stringifyAtomicOrdering(op.ordering());
2232 }
2233 
2234 static LogicalResult verify(FenceOp &op) {
2235   if (op.ordering() == AtomicOrdering::not_atomic ||
2236       op.ordering() == AtomicOrdering::unordered ||
2237       op.ordering() == AtomicOrdering::monotonic)
2238     return op.emitOpError("can be given only acquire, release, acq_rel, "
2239                           "and seq_cst orderings");
2240   return success();
2241 }
2242 
2243 //===----------------------------------------------------------------------===//
2244 // LLVMDialect initialization, type parsing, and registration.
2245 //===----------------------------------------------------------------------===//
2246 
2247 void LLVMDialect::initialize() {
2248   addAttributes<FMFAttr, LoopOptionsAttr>();
2249 
2250   // clang-format off
2251   addTypes<LLVMVoidType,
2252            LLVMPPCFP128Type,
2253            LLVMX86MMXType,
2254            LLVMTokenType,
2255            LLVMLabelType,
2256            LLVMMetadataType,
2257            LLVMFunctionType,
2258            LLVMPointerType,
2259            LLVMFixedVectorType,
2260            LLVMScalableVectorType,
2261            LLVMArrayType,
2262            LLVMStructType>();
2263   // clang-format on
2264   addOperations<
2265 #define GET_OP_LIST
2266 #include "mlir/Dialect/LLVMIR/LLVMOps.cpp.inc"
2267       >();
2268 
2269   // Support unknown operations because not all LLVM operations are registered.
2270   allowUnknownOperations();
2271 }
2272 
2273 #define GET_OP_CLASSES
2274 #include "mlir/Dialect/LLVMIR/LLVMOps.cpp.inc"
2275 
2276 /// Parse a type registered to this dialect.
2277 Type LLVMDialect::parseType(DialectAsmParser &parser) const {
2278   return detail::parseType(parser);
2279 }
2280 
2281 /// Print a type registered to this dialect.
2282 void LLVMDialect::printType(Type type, DialectAsmPrinter &os) const {
2283   return detail::printType(type, os);
2284 }
2285 
2286 LogicalResult LLVMDialect::verifyDataLayoutString(
2287     StringRef descr, llvm::function_ref<void(const Twine &)> reportError) {
2288   llvm::Expected<llvm::DataLayout> maybeDataLayout =
2289       llvm::DataLayout::parse(descr);
2290   if (maybeDataLayout)
2291     return success();
2292 
2293   std::string message;
2294   llvm::raw_string_ostream messageStream(message);
2295   llvm::logAllUnhandledErrors(maybeDataLayout.takeError(), messageStream);
2296   reportError("invalid data layout descriptor: " + messageStream.str());
2297   return failure();
2298 }
2299 
2300 /// Verify LLVM dialect attributes.
2301 LogicalResult LLVMDialect::verifyOperationAttribute(Operation *op,
2302                                                     NamedAttribute attr) {
2303   // If the `llvm.loop` attribute is present, enforce the following structure,
2304   // which the module translation can assume.
2305   if (attr.first.strref() == LLVMDialect::getLoopAttrName()) {
2306     auto loopAttr = attr.second.dyn_cast<DictionaryAttr>();
2307     if (!loopAttr)
2308       return op->emitOpError() << "expected '" << LLVMDialect::getLoopAttrName()
2309                                << "' to be a dictionary attribute";
2310     Optional<NamedAttribute> parallelAccessGroup =
2311         loopAttr.getNamed(LLVMDialect::getParallelAccessAttrName());
2312     if (parallelAccessGroup.hasValue()) {
2313       auto accessGroups = parallelAccessGroup->second.dyn_cast<ArrayAttr>();
2314       if (!accessGroups)
2315         return op->emitOpError()
2316                << "expected '" << LLVMDialect::getParallelAccessAttrName()
2317                << "' to be an array attribute";
2318       for (Attribute attr : accessGroups) {
2319         auto accessGroupRef = attr.dyn_cast<SymbolRefAttr>();
2320         if (!accessGroupRef)
2321           return op->emitOpError()
2322                  << "expected '" << attr << "' to be a symbol reference";
2323         StringRef metadataName = accessGroupRef.getRootReference();
2324         auto metadataOp =
2325             SymbolTable::lookupNearestSymbolFrom<LLVM::MetadataOp>(
2326                 op->getParentOp(), metadataName);
2327         if (!metadataOp)
2328           return op->emitOpError()
2329                  << "expected '" << attr << "' to reference a metadata op";
2330         StringRef accessGroupName = accessGroupRef.getLeafReference();
2331         Operation *accessGroupOp =
2332             SymbolTable::lookupNearestSymbolFrom(metadataOp, accessGroupName);
2333         if (!accessGroupOp)
2334           return op->emitOpError()
2335                  << "expected '" << attr << "' to reference an access_group op";
2336       }
2337     }
2338 
2339     Optional<NamedAttribute> loopOptions =
2340         loopAttr.getNamed(LLVMDialect::getLoopOptionsAttrName());
2341     if (loopOptions.hasValue() && !loopOptions->second.isa<LoopOptionsAttr>())
2342       return op->emitOpError()
2343              << "expected '" << LLVMDialect::getLoopOptionsAttrName()
2344              << "' to be a `loopopts` attribute";
2345   }
2346 
2347   // If the data layout attribute is present, it must use the LLVM data layout
2348   // syntax. Try parsing it and report errors in case of failure. Users of this
2349   // attribute may assume it is well-formed and can pass it to the (asserting)
2350   // llvm::DataLayout constructor.
2351   if (attr.first.strref() != LLVM::LLVMDialect::getDataLayoutAttrName())
2352     return success();
2353   if (auto stringAttr = attr.second.dyn_cast<StringAttr>())
2354     return verifyDataLayoutString(
2355         stringAttr.getValue(),
2356         [op](const Twine &message) { op->emitOpError() << message.str(); });
2357 
2358   return op->emitOpError() << "expected '"
2359                            << LLVM::LLVMDialect::getDataLayoutAttrName()
2360                            << "' to be a string attribute";
2361 }
2362 
2363 /// Verify LLVMIR function argument attributes.
2364 LogicalResult LLVMDialect::verifyRegionArgAttribute(Operation *op,
2365                                                     unsigned regionIdx,
2366                                                     unsigned argIdx,
2367                                                     NamedAttribute argAttr) {
2368   // Check that llvm.noalias is a unit attribute.
2369   if (argAttr.first == LLVMDialect::getNoAliasAttrName() &&
2370       !argAttr.second.isa<UnitAttr>())
2371     return op->emitError()
2372            << "expected llvm.noalias argument attribute to be a unit attribute";
2373   // Check that llvm.align is an integer attribute.
2374   if (argAttr.first == LLVMDialect::getAlignAttrName() &&
2375       !argAttr.second.isa<IntegerAttr>())
2376     return op->emitError()
2377            << "llvm.align argument attribute of non integer type";
2378   return success();
2379 }
2380 
2381 //===----------------------------------------------------------------------===//
2382 // Utility functions.
2383 //===----------------------------------------------------------------------===//
2384 
2385 Value mlir::LLVM::createGlobalString(Location loc, OpBuilder &builder,
2386                                      StringRef name, StringRef value,
2387                                      LLVM::Linkage linkage) {
2388   assert(builder.getInsertionBlock() &&
2389          builder.getInsertionBlock()->getParentOp() &&
2390          "expected builder to point to a block constrained in an op");
2391   auto module =
2392       builder.getInsertionBlock()->getParentOp()->getParentOfType<ModuleOp>();
2393   assert(module && "builder points to an op outside of a module");
2394 
2395   // Create the global at the entry of the module.
2396   OpBuilder moduleBuilder(module.getBodyRegion(), builder.getListener());
2397   MLIRContext *ctx = builder.getContext();
2398   auto type = LLVM::LLVMArrayType::get(IntegerType::get(ctx, 8), value.size());
2399   auto global = moduleBuilder.create<LLVM::GlobalOp>(
2400       loc, type, /*isConstant=*/true, linkage, name,
2401       builder.getStringAttr(value), /*alignment=*/0);
2402 
2403   // Get the pointer to the first character in the global string.
2404   Value globalPtr = builder.create<LLVM::AddressOfOp>(loc, global);
2405   Value cst0 = builder.create<LLVM::ConstantOp>(
2406       loc, IntegerType::get(ctx, 64),
2407       builder.getIntegerAttr(builder.getIndexType(), 0));
2408   return builder.create<LLVM::GEPOp>(
2409       loc, LLVM::LLVMPointerType::get(IntegerType::get(ctx, 8)), globalPtr,
2410       ValueRange{cst0, cst0});
2411 }
2412 
2413 bool mlir::LLVM::satisfiesLLVMModule(Operation *op) {
2414   return op->hasTrait<OpTrait::SymbolTable>() &&
2415          op->hasTrait<OpTrait::IsIsolatedFromAbove>();
2416 }
2417 
2418 static constexpr const FastmathFlags FastmathFlagsList[] = {
2419     // clang-format off
2420     FastmathFlags::nnan,
2421     FastmathFlags::ninf,
2422     FastmathFlags::nsz,
2423     FastmathFlags::arcp,
2424     FastmathFlags::contract,
2425     FastmathFlags::afn,
2426     FastmathFlags::reassoc,
2427     FastmathFlags::fast,
2428     // clang-format on
2429 };
2430 
2431 void FMFAttr::print(DialectAsmPrinter &printer) const {
2432   printer << "fastmath<";
2433   auto flags = llvm::make_filter_range(FastmathFlagsList, [&](auto flag) {
2434     return bitEnumContains(this->getFlags(), flag);
2435   });
2436   llvm::interleaveComma(flags, printer,
2437                         [&](auto flag) { printer << stringifyEnum(flag); });
2438   printer << ">";
2439 }
2440 
2441 Attribute FMFAttr::parse(MLIRContext *context, DialectAsmParser &parser,
2442                          Type type) {
2443   if (failed(parser.parseLess()))
2444     return {};
2445 
2446   FastmathFlags flags = {};
2447   if (failed(parser.parseOptionalGreater())) {
2448     do {
2449       StringRef elemName;
2450       if (failed(parser.parseKeyword(&elemName)))
2451         return {};
2452 
2453       auto elem = symbolizeFastmathFlags(elemName);
2454       if (!elem) {
2455         parser.emitError(parser.getNameLoc(), "Unknown fastmath flag: ")
2456             << elemName;
2457         return {};
2458       }
2459 
2460       flags = flags | *elem;
2461     } while (succeeded(parser.parseOptionalComma()));
2462 
2463     if (failed(parser.parseGreater()))
2464       return {};
2465   }
2466 
2467   return FMFAttr::get(parser.getBuilder().getContext(), flags);
2468 }
2469 
2470 LoopOptionsAttrBuilder::LoopOptionsAttrBuilder(LoopOptionsAttr attr)
2471     : options(attr.getOptions().begin(), attr.getOptions().end()) {}
2472 
2473 template <typename T>
2474 LoopOptionsAttrBuilder &LoopOptionsAttrBuilder::setOption(LoopOptionCase tag,
2475                                                           Optional<T> value) {
2476   auto option = llvm::find_if(
2477       options, [tag](auto option) { return option.first == tag; });
2478   if (option != options.end()) {
2479     if (value.hasValue())
2480       option->second = *value;
2481     else
2482       options.erase(option);
2483   } else {
2484     options.push_back(LoopOptionsAttr::OptionValuePair(tag, *value));
2485   }
2486   return *this;
2487 }
2488 
2489 LoopOptionsAttrBuilder &
2490 LoopOptionsAttrBuilder::setDisableLICM(Optional<bool> value) {
2491   return setOption(LoopOptionCase::disable_licm, value);
2492 }
2493 
2494 /// Set the `interleave_count` option to the provided value. If no value
2495 /// is provided the option is deleted.
2496 LoopOptionsAttrBuilder &
2497 LoopOptionsAttrBuilder::setInterleaveCount(Optional<uint64_t> count) {
2498   return setOption(LoopOptionCase::interleave_count, count);
2499 }
2500 
2501 /// Set the `disable_unroll` option to the provided value. If no value
2502 /// is provided the option is deleted.
2503 LoopOptionsAttrBuilder &
2504 LoopOptionsAttrBuilder::setDisableUnroll(Optional<bool> value) {
2505   return setOption(LoopOptionCase::disable_unroll, value);
2506 }
2507 
2508 /// Set the `disable_pipeline` option to the provided value. If no value
2509 /// is provided the option is deleted.
2510 LoopOptionsAttrBuilder &
2511 LoopOptionsAttrBuilder::setDisablePipeline(Optional<bool> value) {
2512   return setOption(LoopOptionCase::disable_pipeline, value);
2513 }
2514 
2515 /// Set the `pipeline_initiation_interval` option to the provided value.
2516 /// If no value is provided the option is deleted.
2517 LoopOptionsAttrBuilder &LoopOptionsAttrBuilder::setPipelineInitiationInterval(
2518     Optional<uint64_t> count) {
2519   return setOption(LoopOptionCase::pipeline_initiation_interval, count);
2520 }
2521 
2522 template <typename T>
2523 static Optional<T>
2524 getOption(ArrayRef<std::pair<LoopOptionCase, int64_t>> options,
2525           LoopOptionCase option) {
2526   auto it =
2527       lower_bound(options, option, [](auto optionPair, LoopOptionCase option) {
2528         return optionPair.first < option;
2529       });
2530   if (it == options.end())
2531     return {};
2532   return static_cast<T>(it->second);
2533 }
2534 
2535 Optional<bool> LoopOptionsAttr::disableUnroll() {
2536   return getOption<bool>(getOptions(), LoopOptionCase::disable_unroll);
2537 }
2538 
2539 Optional<bool> LoopOptionsAttr::disableLICM() {
2540   return getOption<bool>(getOptions(), LoopOptionCase::disable_licm);
2541 }
2542 
2543 Optional<int64_t> LoopOptionsAttr::interleaveCount() {
2544   return getOption<int64_t>(getOptions(), LoopOptionCase::interleave_count);
2545 }
2546 
2547 /// Build the LoopOptions Attribute from a sorted array of individual options.
2548 LoopOptionsAttr LoopOptionsAttr::get(
2549     MLIRContext *context,
2550     ArrayRef<std::pair<LoopOptionCase, int64_t>> sortedOptions) {
2551   assert(llvm::is_sorted(sortedOptions, llvm::less_first()) &&
2552          "LoopOptionsAttr ctor expects a sorted options array");
2553   return Base::get(context, sortedOptions);
2554 }
2555 
2556 /// Build the LoopOptions Attribute from a sorted array of individual options.
2557 LoopOptionsAttr LoopOptionsAttr::get(MLIRContext *context,
2558                                      LoopOptionsAttrBuilder &optionBuilders) {
2559   llvm::sort(optionBuilders.options, llvm::less_first());
2560   return Base::get(context, optionBuilders.options);
2561 }
2562 
2563 void LoopOptionsAttr::print(DialectAsmPrinter &printer) const {
2564   printer << getMnemonic() << "<";
2565   llvm::interleaveComma(getOptions(), printer, [&](auto option) {
2566     printer << stringifyEnum(option.first) << " = ";
2567     switch (option.first) {
2568     case LoopOptionCase::disable_licm:
2569     case LoopOptionCase::disable_unroll:
2570     case LoopOptionCase::disable_pipeline:
2571       printer << (option.second ? "true" : "false");
2572       break;
2573     case LoopOptionCase::interleave_count:
2574     case LoopOptionCase::pipeline_initiation_interval:
2575       printer << option.second;
2576       break;
2577     }
2578   });
2579   printer << ">";
2580 }
2581 
2582 Attribute LoopOptionsAttr::parse(MLIRContext *context, DialectAsmParser &parser,
2583                                  Type type) {
2584   if (failed(parser.parseLess()))
2585     return {};
2586 
2587   SmallVector<std::pair<LoopOptionCase, int64_t>> options;
2588   llvm::SmallDenseSet<LoopOptionCase> seenOptions;
2589   do {
2590     StringRef optionName;
2591     if (parser.parseKeyword(&optionName))
2592       return {};
2593 
2594     auto option = symbolizeLoopOptionCase(optionName);
2595     if (!option) {
2596       parser.emitError(parser.getNameLoc(), "unknown loop option: ")
2597           << optionName;
2598       return {};
2599     }
2600     if (!seenOptions.insert(*option).second) {
2601       parser.emitError(parser.getNameLoc(), "loop option present twice");
2602       return {};
2603     }
2604     if (failed(parser.parseEqual()))
2605       return {};
2606 
2607     int64_t value;
2608     switch (*option) {
2609     case LoopOptionCase::disable_licm:
2610     case LoopOptionCase::disable_unroll:
2611     case LoopOptionCase::disable_pipeline:
2612       if (succeeded(parser.parseOptionalKeyword("true")))
2613         value = 1;
2614       else if (succeeded(parser.parseOptionalKeyword("false")))
2615         value = 0;
2616       else {
2617         parser.emitError(parser.getNameLoc(),
2618                          "expected boolean value 'true' or 'false'");
2619         return {};
2620       }
2621       break;
2622     case LoopOptionCase::interleave_count:
2623     case LoopOptionCase::pipeline_initiation_interval:
2624       if (failed(parser.parseInteger(value))) {
2625         parser.emitError(parser.getNameLoc(), "expected integer value");
2626         return {};
2627       }
2628       break;
2629     }
2630     options.push_back(std::make_pair(*option, value));
2631   } while (succeeded(parser.parseOptionalComma()));
2632   if (failed(parser.parseGreater()))
2633     return {};
2634 
2635   llvm::sort(options, llvm::less_first());
2636   return get(parser.getBuilder().getContext(), options);
2637 }
2638 
2639 Attribute LLVMDialect::parseAttribute(DialectAsmParser &parser,
2640                                       Type type) const {
2641   if (type) {
2642     parser.emitError(parser.getNameLoc(), "unexpected type");
2643     return {};
2644   }
2645   StringRef attrKind;
2646   if (parser.parseKeyword(&attrKind))
2647     return {};
2648   {
2649     Attribute attr;
2650     auto parseResult =
2651         generatedAttributeParser(getContext(), parser, attrKind, type, attr);
2652     if (parseResult.hasValue())
2653       return attr;
2654   }
2655   parser.emitError(parser.getNameLoc(), "unknown attribute type: ") << attrKind;
2656   return {};
2657 }
2658 
2659 void LLVMDialect::printAttribute(Attribute attr, DialectAsmPrinter &os) const {
2660   if (succeeded(generatedAttributePrinter(attr, os)))
2661     return;
2662   llvm_unreachable("Unknown attribute type");
2663 }
2664