MLIR  21.0.0git
LLVMToLLVMIRTranslation.cpp
Go to the documentation of this file.
1 //===- LLVMToLLVMIRTranslation.cpp - Translate LLVM dialect to LLVM IR ----===//
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 implements a translation between the MLIR LLVM dialect and LLVM IR.
10 //
11 //===----------------------------------------------------------------------===//
12 
15 #include "mlir/IR/Operation.h"
16 #include "mlir/Support/LLVM.h"
18 
19 #include "llvm/ADT/TypeSwitch.h"
20 #include "llvm/IR/IRBuilder.h"
21 #include "llvm/IR/InlineAsm.h"
22 #include "llvm/IR/Instructions.h"
23 #include "llvm/IR/MDBuilder.h"
24 #include "llvm/IR/MatrixBuilder.h"
25 
26 using namespace mlir;
27 using namespace mlir::LLVM;
29 
30 #include "mlir/Dialect/LLVMIR/LLVMConversionEnumsToLLVM.inc"
31 
32 static llvm::FastMathFlags getFastmathFlags(FastmathFlagsInterface &op) {
33  using llvmFMF = llvm::FastMathFlags;
34  using FuncT = void (llvmFMF::*)(bool);
35  const std::pair<FastmathFlags, FuncT> handlers[] = {
36  // clang-format off
37  {FastmathFlags::nnan, &llvmFMF::setNoNaNs},
38  {FastmathFlags::ninf, &llvmFMF::setNoInfs},
39  {FastmathFlags::nsz, &llvmFMF::setNoSignedZeros},
40  {FastmathFlags::arcp, &llvmFMF::setAllowReciprocal},
41  {FastmathFlags::contract, &llvmFMF::setAllowContract},
42  {FastmathFlags::afn, &llvmFMF::setApproxFunc},
43  {FastmathFlags::reassoc, &llvmFMF::setAllowReassoc},
44  // clang-format on
45  };
46  llvm::FastMathFlags ret;
47  ::mlir::LLVM::FastmathFlags fmfMlir = op.getFastmathAttr().getValue();
48  for (auto it : handlers)
49  if (bitEnumContainsAll(fmfMlir, it.first))
50  (ret.*(it.second))(true);
51  return ret;
52 }
53 
54 /// Convert the value of a DenseI64ArrayAttr to a vector of unsigned indices.
56  SmallVector<unsigned> position;
57  llvm::append_range(position, indices);
58  return position;
59 }
60 
61 /// Convert an LLVM type to a string for printing in diagnostics.
62 static std::string diagStr(const llvm::Type *type) {
63  std::string str;
64  llvm::raw_string_ostream os(str);
65  type->print(os);
66  return str;
67 }
68 
69 /// Get the declaration of an overloaded llvm intrinsic. First we get the
70 /// overloaded argument types and/or result type from the CallIntrinsicOp, and
71 /// then use those to get the correct declaration of the overloaded intrinsic.
72 static FailureOr<llvm::Function *>
74  llvm::Module *module,
75  LLVM::ModuleTranslation &moduleTranslation) {
77  for (Type type : op->getOperandTypes())
78  allArgTys.push_back(moduleTranslation.convertType(type));
79 
80  llvm::Type *resTy;
81  if (op.getNumResults() == 0)
82  resTy = llvm::Type::getVoidTy(module->getContext());
83  else
84  resTy = moduleTranslation.convertType(op.getResult(0).getType());
85 
86  // ATM we do not support variadic intrinsics.
87  llvm::FunctionType *ft = llvm::FunctionType::get(resTy, allArgTys, false);
88 
90  getIntrinsicInfoTableEntries(id, table);
92 
93  SmallVector<llvm::Type *, 8> overloadedArgTys;
94  if (llvm::Intrinsic::matchIntrinsicSignature(ft, tableRef,
95  overloadedArgTys) !=
96  llvm::Intrinsic::MatchIntrinsicTypesResult::MatchIntrinsicTypes_Match) {
97  return mlir::emitError(op.getLoc(), "call intrinsic signature ")
98  << diagStr(ft) << " to overloaded intrinsic " << op.getIntrinAttr()
99  << " does not match any of the overloads";
100  }
101 
102  ArrayRef<llvm::Type *> overloadedArgTysRef = overloadedArgTys;
103  return llvm::Intrinsic::getOrInsertDeclaration(module, id,
104  overloadedArgTysRef);
105 }
106 
107 static llvm::OperandBundleDef
108 convertOperandBundle(OperandRange bundleOperands, StringRef bundleTag,
109  LLVM::ModuleTranslation &moduleTranslation) {
110  std::vector<llvm::Value *> operands;
111  operands.reserve(bundleOperands.size());
112  for (Value bundleArg : bundleOperands)
113  operands.push_back(moduleTranslation.lookupValue(bundleArg));
114  return llvm::OperandBundleDef(bundleTag.str(), std::move(operands));
115 }
116 
118 convertOperandBundles(OperandRangeRange bundleOperands, ArrayAttr bundleTags,
119  LLVM::ModuleTranslation &moduleTranslation) {
121  bundles.reserve(bundleOperands.size());
122 
123  for (auto [operands, tagAttr] : llvm::zip_equal(bundleOperands, bundleTags)) {
124  StringRef tag = cast<StringAttr>(tagAttr).getValue();
125  bundles.push_back(convertOperandBundle(operands, tag, moduleTranslation));
126  }
127  return bundles;
128 }
129 
132  std::optional<ArrayAttr> bundleTags,
133  LLVM::ModuleTranslation &moduleTranslation) {
134  if (!bundleTags)
135  return {};
136  return convertOperandBundles(bundleOperands, *bundleTags, moduleTranslation);
137 }
138 
139 static LogicalResult
140 convertParameterAndResultAttrs(mlir::Location loc, ArrayAttr argAttrsArray,
141  ArrayAttr resAttrsArray, llvm::CallBase *call,
142  LLVM::ModuleTranslation &moduleTranslation) {
143  if (argAttrsArray) {
144  for (auto [argIdx, argAttrsAttr] : llvm::enumerate(argAttrsArray)) {
145  if (auto argAttrs = cast<DictionaryAttr>(argAttrsAttr);
146  !argAttrs.empty()) {
147  FailureOr<llvm::AttrBuilder> attrBuilder =
148  moduleTranslation.convertParameterAttrs(loc, argAttrs);
149  if (failed(attrBuilder))
150  return failure();
151  call->addParamAttrs(argIdx, *attrBuilder);
152  }
153  }
154  }
155 
156  if (resAttrsArray && resAttrsArray.size() > 0) {
157  if (resAttrsArray.size() != 1)
158  return mlir::emitError(loc, "llvm.func cannot have multiple results");
159  if (auto resAttrs = cast<DictionaryAttr>(resAttrsArray[0]);
160  !resAttrs.empty()) {
161  FailureOr<llvm::AttrBuilder> attrBuilder =
162  moduleTranslation.convertParameterAttrs(loc, resAttrs);
163  if (failed(attrBuilder))
164  return failure();
165  call->addRetAttrs(*attrBuilder);
166  }
167  }
168  return success();
169 }
170 
171 static LogicalResult
172 convertParameterAndResultAttrs(CallOpInterface callOp, llvm::CallBase *call,
173  LLVM::ModuleTranslation &moduleTranslation) {
175  callOp.getLoc(), callOp.getArgAttrsAttr(), callOp.getResAttrsAttr(), call,
176  moduleTranslation);
177 }
178 
179 /// Builder for LLVM_CallIntrinsicOp
180 static LogicalResult
181 convertCallLLVMIntrinsicOp(CallIntrinsicOp op, llvm::IRBuilderBase &builder,
182  LLVM::ModuleTranslation &moduleTranslation) {
183  llvm::Module *module = builder.GetInsertBlock()->getModule();
185  llvm::Intrinsic::lookupIntrinsicID(op.getIntrinAttr());
186  if (!id)
187  return mlir::emitError(op.getLoc(), "could not find LLVM intrinsic: ")
188  << op.getIntrinAttr();
189 
190  llvm::Function *fn = nullptr;
191  if (llvm::Intrinsic::isOverloaded(id)) {
192  auto fnOrFailure =
193  getOverloadedDeclaration(op, id, module, moduleTranslation);
194  if (failed(fnOrFailure))
195  return failure();
196  fn = *fnOrFailure;
197  } else {
198  fn = llvm::Intrinsic::getOrInsertDeclaration(module, id, {});
199  }
200 
201  // Check the result type of the call.
202  const llvm::Type *intrinType =
203  op.getNumResults() == 0
204  ? llvm::Type::getVoidTy(module->getContext())
205  : moduleTranslation.convertType(op.getResultTypes().front());
206  if (intrinType != fn->getReturnType()) {
207  return mlir::emitError(op.getLoc(), "intrinsic call returns ")
208  << diagStr(intrinType) << " but " << op.getIntrinAttr()
209  << " actually returns " << diagStr(fn->getReturnType());
210  }
211 
212  // Check the argument types of the call. If the function is variadic, check
213  // the subrange of required arguments.
214  if (!fn->getFunctionType()->isVarArg() &&
215  op.getArgs().size() != fn->arg_size()) {
216  return mlir::emitError(op.getLoc(), "intrinsic call has ")
217  << op.getArgs().size() << " operands but " << op.getIntrinAttr()
218  << " expects " << fn->arg_size();
219  }
220  if (fn->getFunctionType()->isVarArg() &&
221  op.getArgs().size() < fn->arg_size()) {
222  return mlir::emitError(op.getLoc(), "intrinsic call has ")
223  << op.getArgs().size() << " operands but variadic "
224  << op.getIntrinAttr() << " expects at least " << fn->arg_size();
225  }
226  // Check the arguments up to the number the function requires.
227  for (unsigned i = 0, e = fn->arg_size(); i != e; ++i) {
228  const llvm::Type *expected = fn->getArg(i)->getType();
229  const llvm::Type *actual =
230  moduleTranslation.convertType(op.getOperandTypes()[i]);
231  if (actual != expected) {
232  return mlir::emitError(op.getLoc(), "intrinsic call operand #")
233  << i << " has type " << diagStr(actual) << " but "
234  << op.getIntrinAttr() << " expects " << diagStr(expected);
235  }
236  }
237 
238  FastmathFlagsInterface itf = op;
239  builder.setFastMathFlags(getFastmathFlags(itf));
240 
241  auto *inst = builder.CreateCall(
242  fn, moduleTranslation.lookupValues(op.getArgs()),
243  convertOperandBundles(op.getOpBundleOperands(), op.getOpBundleTags(),
244  moduleTranslation));
245 
246  if (failed(convertParameterAndResultAttrs(op.getLoc(), op.getArgAttrsAttr(),
247  op.getResAttrsAttr(), inst,
248  moduleTranslation)))
249  return failure();
250 
251  if (op.getNumResults() == 1)
252  moduleTranslation.mapValue(op->getResults().front()) = inst;
253  return success();
254 }
255 
256 static void convertLinkerOptionsOp(ArrayAttr options,
257  llvm::IRBuilderBase &builder,
258  LLVM::ModuleTranslation &moduleTranslation) {
259  llvm::Module *llvmModule = moduleTranslation.getLLVMModule();
260  llvm::LLVMContext &context = llvmModule->getContext();
261  llvm::NamedMDNode *linkerMDNode =
262  llvmModule->getOrInsertNamedMetadata("llvm.linker.options");
264  MDNodes.reserve(options.size());
265  for (auto s : options.getAsRange<StringAttr>()) {
266  auto *MDNode = llvm::MDString::get(context, s.getValue());
267  MDNodes.push_back(MDNode);
268  }
269 
270  auto *listMDNode = llvm::MDTuple::get(context, MDNodes);
271  linkerMDNode->addOperand(listMDNode);
272 }
273 
274 static llvm::Metadata *
275 convertModuleFlagValue(StringRef key, ArrayAttr arrayAttr,
276  llvm::IRBuilderBase &builder,
277  LLVM::ModuleTranslation &moduleTranslation) {
278  llvm::LLVMContext &context = builder.getContext();
279  llvm::MDBuilder mdb(context);
281 
282  if (key == LLVMDialect::getModuleFlagKeyCGProfileName()) {
283  for (auto entry : arrayAttr.getAsRange<ModuleFlagCGProfileEntryAttr>()) {
284  llvm::Metadata *fromMetadata =
285  entry.getFrom()
286  ? llvm::ValueAsMetadata::get(moduleTranslation.lookupFunction(
287  entry.getFrom().getValue()))
288  : nullptr;
289  llvm::Metadata *toMetadata =
290  entry.getTo()
292  moduleTranslation.lookupFunction(entry.getTo().getValue()))
293  : nullptr;
294 
295  llvm::Metadata *vals[] = {
296  fromMetadata, toMetadata,
297  mdb.createConstant(llvm::ConstantInt::get(
298  llvm::Type::getInt64Ty(context), entry.getCount()))};
299  nodes.push_back(llvm::MDNode::get(context, vals));
300  }
301  return llvm::MDTuple::getDistinct(context, nodes);
302  }
303  return nullptr;
304 }
305 
306 static llvm::Metadata *convertModuleFlagProfileSummaryAttr(
307  StringRef key, ModuleFlagProfileSummaryAttr summaryAttr,
308  llvm::IRBuilderBase &builder, LLVM::ModuleTranslation &moduleTranslation) {
309  llvm::LLVMContext &context = builder.getContext();
310  llvm::MDBuilder mdb(context);
311 
312  auto getIntTuple = [&](StringRef key, uint64_t val) -> llvm::MDTuple * {
314  mdb.createString(key), mdb.createConstant(llvm::ConstantInt::get(
315  llvm::Type::getInt64Ty(context), val))};
316  return llvm::MDTuple::get(context, tupleNodes);
317  };
318 
320  mdb.createString("ProfileFormat"),
321  mdb.createString(
322  stringifyProfileSummaryFormatKind(summaryAttr.getFormat()))};
323 
325  llvm::MDTuple::get(context, fmtNode),
326  getIntTuple("TotalCount", summaryAttr.getTotalCount()),
327  getIntTuple("MaxCount", summaryAttr.getMaxCount()),
328  getIntTuple("MaxInternalCount", summaryAttr.getMaxInternalCount()),
329  getIntTuple("MaxFunctionCount", summaryAttr.getMaxFunctionCount()),
330  getIntTuple("NumCounts", summaryAttr.getNumCounts()),
331  getIntTuple("NumFunctions", summaryAttr.getNumFunctions()),
332  };
333 
334  if (summaryAttr.getIsPartialProfile())
335  vals.push_back(
336  getIntTuple("IsPartialProfile", *summaryAttr.getIsPartialProfile()));
337 
338  if (summaryAttr.getPartialProfileRatio()) {
340  mdb.createString("PartialProfileRatio"),
341  mdb.createConstant(llvm::ConstantFP::get(
342  llvm::Type::getDoubleTy(context),
343  summaryAttr.getPartialProfileRatio().getValue()))};
344  vals.push_back(llvm::MDTuple::get(context, tupleNodes));
345  }
346 
347  SmallVector<llvm::Metadata *> detailedEntries;
348  llvm::Type *llvmInt64Type = llvm::Type::getInt64Ty(context);
349  for (ModuleFlagProfileSummaryDetailedAttr detailedEntry :
350  summaryAttr.getDetailedSummary()) {
352  mdb.createConstant(
353  llvm::ConstantInt::get(llvmInt64Type, detailedEntry.getCutOff())),
354  mdb.createConstant(
355  llvm::ConstantInt::get(llvmInt64Type, detailedEntry.getMinCount())),
356  mdb.createConstant(llvm::ConstantInt::get(
357  llvmInt64Type, detailedEntry.getNumCounts()))};
358  detailedEntries.push_back(llvm::MDTuple::get(context, tupleNodes));
359  }
360  SmallVector<llvm::Metadata *> detailedSummary{
361  mdb.createString("DetailedSummary"),
362  llvm::MDTuple::get(context, detailedEntries)};
363  vals.push_back(llvm::MDTuple::get(context, detailedSummary));
364 
365  return llvm::MDNode::get(context, vals);
366 }
367 
368 static void convertModuleFlagsOp(ArrayAttr flags, llvm::IRBuilderBase &builder,
369  LLVM::ModuleTranslation &moduleTranslation) {
370  llvm::Module *llvmModule = moduleTranslation.getLLVMModule();
371  for (auto flagAttr : flags.getAsRange<ModuleFlagAttr>()) {
372  llvm::Metadata *valueMetadata =
374  .Case<StringAttr>([&](auto strAttr) {
375  return llvm::MDString::get(builder.getContext(),
376  strAttr.getValue());
377  })
378  .Case<IntegerAttr>([&](auto intAttr) {
380  llvm::Type::getInt32Ty(builder.getContext()),
381  intAttr.getInt()));
382  })
383  .Case<ArrayAttr>([&](auto arrayAttr) {
384  return convertModuleFlagValue(flagAttr.getKey().getValue(),
385  arrayAttr, builder,
386  moduleTranslation);
387  })
388  .Case([&](ModuleFlagProfileSummaryAttr summaryAttr) {
390  flagAttr.getKey().getValue(), summaryAttr, builder,
391  moduleTranslation);
392  })
393  .Default([](auto) { return nullptr; });
394 
395  assert(valueMetadata && "expected valid metadata");
396  llvmModule->addModuleFlag(
397  convertModFlagBehaviorToLLVM(flagAttr.getBehavior()),
398  flagAttr.getKey().getValue(), valueMetadata);
399  }
400 }
401 
402 static LogicalResult
403 convertOperationImpl(Operation &opInst, llvm::IRBuilderBase &builder,
404  LLVM::ModuleTranslation &moduleTranslation) {
405 
406  llvm::IRBuilder<>::FastMathFlagGuard fmfGuard(builder);
407  if (auto fmf = dyn_cast<FastmathFlagsInterface>(opInst))
408  builder.setFastMathFlags(getFastmathFlags(fmf));
409 
410 #include "mlir/Dialect/LLVMIR/LLVMConversions.inc"
411 #include "mlir/Dialect/LLVMIR/LLVMIntrinsicConversions.inc"
412 
413  // Emit function calls. If the "callee" attribute is present, this is a
414  // direct function call and we also need to look up the remapped function
415  // itself. Otherwise, this is an indirect call and the callee is the first
416  // operand, look it up as a normal value.
417  if (auto callOp = dyn_cast<LLVM::CallOp>(opInst)) {
418  auto operands = moduleTranslation.lookupValues(callOp.getCalleeOperands());
420  convertOperandBundles(callOp.getOpBundleOperands(),
421  callOp.getOpBundleTags(), moduleTranslation);
422  ArrayRef<llvm::Value *> operandsRef(operands);
423  llvm::CallInst *call;
424  if (auto attr = callOp.getCalleeAttr()) {
425  call =
426  builder.CreateCall(moduleTranslation.lookupFunction(attr.getValue()),
427  operandsRef, opBundles);
428  } else {
429  llvm::FunctionType *calleeType = llvm::cast<llvm::FunctionType>(
430  moduleTranslation.convertType(callOp.getCalleeFunctionType()));
431  call = builder.CreateCall(calleeType, operandsRef.front(),
432  operandsRef.drop_front(), opBundles);
433  }
434  call->setCallingConv(convertCConvToLLVM(callOp.getCConv()));
435  call->setTailCallKind(convertTailCallKindToLLVM(callOp.getTailCallKind()));
436  if (callOp.getConvergentAttr())
437  call->addFnAttr(llvm::Attribute::Convergent);
438  if (callOp.getNoUnwindAttr())
439  call->addFnAttr(llvm::Attribute::NoUnwind);
440  if (callOp.getWillReturnAttr())
441  call->addFnAttr(llvm::Attribute::WillReturn);
442  if (callOp.getNoInlineAttr())
443  call->addFnAttr(llvm::Attribute::NoInline);
444  if (callOp.getAlwaysInlineAttr())
445  call->addFnAttr(llvm::Attribute::AlwaysInline);
446  if (callOp.getInlineHintAttr())
447  call->addFnAttr(llvm::Attribute::InlineHint);
448 
449  if (failed(convertParameterAndResultAttrs(callOp, call, moduleTranslation)))
450  return failure();
451 
452  if (MemoryEffectsAttr memAttr = callOp.getMemoryEffectsAttr()) {
453  llvm::MemoryEffects memEffects =
454  llvm::MemoryEffects(llvm::MemoryEffects::Location::ArgMem,
455  convertModRefInfoToLLVM(memAttr.getArgMem())) |
456  llvm::MemoryEffects(
457  llvm::MemoryEffects::Location::InaccessibleMem,
458  convertModRefInfoToLLVM(memAttr.getInaccessibleMem())) |
459  llvm::MemoryEffects(llvm::MemoryEffects::Location::Other,
460  convertModRefInfoToLLVM(memAttr.getOther()));
461  call->setMemoryEffects(memEffects);
462  }
463 
464  moduleTranslation.setAccessGroupsMetadata(callOp, call);
465  moduleTranslation.setAliasScopeMetadata(callOp, call);
466  moduleTranslation.setTBAAMetadata(callOp, call);
467  // If the called function has a result, remap the corresponding value. Note
468  // that LLVM IR dialect CallOp has either 0 or 1 result.
469  if (opInst.getNumResults() != 0)
470  moduleTranslation.mapValue(opInst.getResult(0), call);
471  // Check that LLVM call returns void for 0-result functions.
472  else if (!call->getType()->isVoidTy())
473  return failure();
474  moduleTranslation.mapCall(callOp, call);
475  return success();
476  }
477 
478  if (auto inlineAsmOp = dyn_cast<LLVM::InlineAsmOp>(opInst)) {
479  // TODO: refactor function type creation which usually occurs in std-LLVM
480  // conversion.
481  SmallVector<Type, 8> operandTypes;
482  llvm::append_range(operandTypes, inlineAsmOp.getOperands().getTypes());
483 
484  Type resultType;
485  if (inlineAsmOp.getNumResults() == 0) {
486  resultType = LLVM::LLVMVoidType::get(&moduleTranslation.getContext());
487  } else {
488  assert(inlineAsmOp.getNumResults() == 1);
489  resultType = inlineAsmOp.getResultTypes()[0];
490  }
491  auto ft = LLVM::LLVMFunctionType::get(resultType, operandTypes);
492  llvm::InlineAsm *inlineAsmInst =
493  inlineAsmOp.getAsmDialect()
495  static_cast<llvm::FunctionType *>(
496  moduleTranslation.convertType(ft)),
497  inlineAsmOp.getAsmString(), inlineAsmOp.getConstraints(),
498  inlineAsmOp.getHasSideEffects(),
499  inlineAsmOp.getIsAlignStack(),
500  convertAsmDialectToLLVM(*inlineAsmOp.getAsmDialect()))
501  : llvm::InlineAsm::get(static_cast<llvm::FunctionType *>(
502  moduleTranslation.convertType(ft)),
503  inlineAsmOp.getAsmString(),
504  inlineAsmOp.getConstraints(),
505  inlineAsmOp.getHasSideEffects(),
506  inlineAsmOp.getIsAlignStack());
507  llvm::CallInst *inst = builder.CreateCall(
508  inlineAsmInst,
509  moduleTranslation.lookupValues(inlineAsmOp.getOperands()));
510  inst->setTailCallKind(convertTailCallKindToLLVM(
511  inlineAsmOp.getTailCallKindAttr().getTailCallKind()));
512  if (auto maybeOperandAttrs = inlineAsmOp.getOperandAttrs()) {
513  llvm::AttributeList attrList;
514  for (const auto &it : llvm::enumerate(*maybeOperandAttrs)) {
515  Attribute attr = it.value();
516  if (!attr)
517  continue;
518  DictionaryAttr dAttr = cast<DictionaryAttr>(attr);
519  if (dAttr.empty())
520  continue;
521  TypeAttr tAttr =
522  cast<TypeAttr>(dAttr.get(InlineAsmOp::getElementTypeAttrName()));
523  llvm::AttrBuilder b(moduleTranslation.getLLVMContext());
524  llvm::Type *ty = moduleTranslation.convertType(tAttr.getValue());
525  b.addTypeAttr(llvm::Attribute::ElementType, ty);
526  // shift to account for the returned value (this is always 1 aggregate
527  // value in LLVM).
528  int shift = (opInst.getNumResults() > 0) ? 1 : 0;
529  attrList = attrList.addAttributesAtIndex(
530  moduleTranslation.getLLVMContext(), it.index() + shift, b);
531  }
532  inst->setAttributes(attrList);
533  }
534 
535  if (opInst.getNumResults() != 0)
536  moduleTranslation.mapValue(opInst.getResult(0), inst);
537  return success();
538  }
539 
540  if (auto invOp = dyn_cast<LLVM::InvokeOp>(opInst)) {
541  auto operands = moduleTranslation.lookupValues(invOp.getCalleeOperands());
543  convertOperandBundles(invOp.getOpBundleOperands(),
544  invOp.getOpBundleTags(), moduleTranslation);
545  ArrayRef<llvm::Value *> operandsRef(operands);
546  llvm::InvokeInst *result;
547  if (auto attr = opInst.getAttrOfType<FlatSymbolRefAttr>("callee")) {
548  result = builder.CreateInvoke(
549  moduleTranslation.lookupFunction(attr.getValue()),
550  moduleTranslation.lookupBlock(invOp.getSuccessor(0)),
551  moduleTranslation.lookupBlock(invOp.getSuccessor(1)), operandsRef,
552  opBundles);
553  } else {
554  llvm::FunctionType *calleeType = llvm::cast<llvm::FunctionType>(
555  moduleTranslation.convertType(invOp.getCalleeFunctionType()));
556  result = builder.CreateInvoke(
557  calleeType, operandsRef.front(),
558  moduleTranslation.lookupBlock(invOp.getSuccessor(0)),
559  moduleTranslation.lookupBlock(invOp.getSuccessor(1)),
560  operandsRef.drop_front(), opBundles);
561  }
562  result->setCallingConv(convertCConvToLLVM(invOp.getCConv()));
563  if (failed(
564  convertParameterAndResultAttrs(invOp, result, moduleTranslation)))
565  return failure();
566  moduleTranslation.mapBranch(invOp, result);
567  // InvokeOp can only have 0 or 1 result
568  if (invOp->getNumResults() != 0) {
569  moduleTranslation.mapValue(opInst.getResult(0), result);
570  return success();
571  }
572  return success(result->getType()->isVoidTy());
573  }
574 
575  if (auto lpOp = dyn_cast<LLVM::LandingpadOp>(opInst)) {
576  llvm::Type *ty = moduleTranslation.convertType(lpOp.getType());
577  llvm::LandingPadInst *lpi =
578  builder.CreateLandingPad(ty, lpOp.getNumOperands());
579  lpi->setCleanup(lpOp.getCleanup());
580 
581  // Add clauses
582  for (llvm::Value *operand :
583  moduleTranslation.lookupValues(lpOp.getOperands())) {
584  // All operands should be constant - checked by verifier
585  if (auto *constOperand = dyn_cast<llvm::Constant>(operand))
586  lpi->addClause(constOperand);
587  }
588  moduleTranslation.mapValue(lpOp.getResult(), lpi);
589  return success();
590  }
591 
592  // Emit branches. We need to look up the remapped blocks and ignore the
593  // block arguments that were transformed into PHI nodes.
594  if (auto brOp = dyn_cast<LLVM::BrOp>(opInst)) {
595  llvm::BranchInst *branch =
596  builder.CreateBr(moduleTranslation.lookupBlock(brOp.getSuccessor()));
597  moduleTranslation.mapBranch(&opInst, branch);
598  moduleTranslation.setLoopMetadata(&opInst, branch);
599  return success();
600  }
601  if (auto condbrOp = dyn_cast<LLVM::CondBrOp>(opInst)) {
602  llvm::BranchInst *branch = builder.CreateCondBr(
603  moduleTranslation.lookupValue(condbrOp.getOperand(0)),
604  moduleTranslation.lookupBlock(condbrOp.getSuccessor(0)),
605  moduleTranslation.lookupBlock(condbrOp.getSuccessor(1)));
606  moduleTranslation.mapBranch(&opInst, branch);
607  moduleTranslation.setLoopMetadata(&opInst, branch);
608  return success();
609  }
610  if (auto switchOp = dyn_cast<LLVM::SwitchOp>(opInst)) {
611  llvm::SwitchInst *switchInst = builder.CreateSwitch(
612  moduleTranslation.lookupValue(switchOp.getValue()),
613  moduleTranslation.lookupBlock(switchOp.getDefaultDestination()),
614  switchOp.getCaseDestinations().size());
615 
616  // Handle switch with zero cases.
617  if (!switchOp.getCaseValues())
618  return success();
619 
620  auto *ty = llvm::cast<llvm::IntegerType>(
621  moduleTranslation.convertType(switchOp.getValue().getType()));
622  for (auto i :
623  llvm::zip(llvm::cast<DenseIntElementsAttr>(*switchOp.getCaseValues()),
624  switchOp.getCaseDestinations()))
625  switchInst->addCase(
626  llvm::ConstantInt::get(ty, std::get<0>(i).getLimitedValue()),
627  moduleTranslation.lookupBlock(std::get<1>(i)));
628 
629  moduleTranslation.mapBranch(&opInst, switchInst);
630  return success();
631  }
632  if (auto indBrOp = dyn_cast<LLVM::IndirectBrOp>(opInst)) {
633  llvm::IndirectBrInst *indBr = builder.CreateIndirectBr(
634  moduleTranslation.lookupValue(indBrOp.getAddr()),
635  indBrOp->getNumSuccessors());
636  for (auto *succ : indBrOp.getSuccessors())
637  indBr->addDestination(moduleTranslation.lookupBlock(succ));
638  moduleTranslation.mapBranch(&opInst, indBr);
639  return success();
640  }
641 
642  // Emit addressof. We need to look up the global value referenced by the
643  // operation and store it in the MLIR-to-LLVM value mapping. This does not
644  // emit any LLVM instruction.
645  if (auto addressOfOp = dyn_cast<LLVM::AddressOfOp>(opInst)) {
646  LLVM::GlobalOp global =
647  addressOfOp.getGlobal(moduleTranslation.symbolTable());
648  LLVM::LLVMFuncOp function =
649  addressOfOp.getFunction(moduleTranslation.symbolTable());
650  LLVM::AliasOp alias = addressOfOp.getAlias(moduleTranslation.symbolTable());
651 
652  // The verifier should not have allowed this.
653  assert((global || function || alias) &&
654  "referencing an undefined global, function, or alias");
655 
656  llvm::Value *llvmValue = nullptr;
657  if (global)
658  llvmValue = moduleTranslation.lookupGlobal(global);
659  else if (alias)
660  llvmValue = moduleTranslation.lookupAlias(alias);
661  else
662  llvmValue = moduleTranslation.lookupFunction(function.getName());
663 
664  moduleTranslation.mapValue(addressOfOp.getResult(), llvmValue);
665  return success();
666  }
667 
668  // Emit dso_local_equivalent. We need to look up the global value referenced
669  // by the operation and store it in the MLIR-to-LLVM value mapping.
670  if (auto dsoLocalEquivalentOp =
671  dyn_cast<LLVM::DSOLocalEquivalentOp>(opInst)) {
672  LLVM::LLVMFuncOp function =
673  dsoLocalEquivalentOp.getFunction(moduleTranslation.symbolTable());
674  LLVM::AliasOp alias =
675  dsoLocalEquivalentOp.getAlias(moduleTranslation.symbolTable());
676 
677  // The verifier should not have allowed this.
678  assert((function || alias) &&
679  "referencing an undefined function, or alias");
680 
681  llvm::Value *llvmValue = nullptr;
682  if (alias)
683  llvmValue = moduleTranslation.lookupAlias(alias);
684  else
685  llvmValue = moduleTranslation.lookupFunction(function.getName());
686 
687  moduleTranslation.mapValue(
688  dsoLocalEquivalentOp.getResult(),
689  llvm::DSOLocalEquivalent::get(cast<llvm::GlobalValue>(llvmValue)));
690  return success();
691  }
692 
693  // Emit blockaddress. We first need to find the LLVM block referenced by this
694  // operation and then create a LLVM block address for it.
695  if (auto blockAddressOp = dyn_cast<LLVM::BlockAddressOp>(opInst)) {
696  BlockAddressAttr blockAddressAttr = blockAddressOp.getBlockAddr();
697  llvm::BasicBlock *llvmBlock =
698  moduleTranslation.lookupBlockAddress(blockAddressAttr);
699 
700  llvm::Value *llvmValue = nullptr;
701  StringRef fnName = blockAddressAttr.getFunction().getValue();
702  if (llvmBlock) {
703  llvm::Function *llvmFn = moduleTranslation.lookupFunction(fnName);
704  llvmValue = llvm::BlockAddress::get(llvmFn, llvmBlock);
705  } else {
706  // The matching LLVM block is not yet emitted, a placeholder is created
707  // in its place. When the LLVM block is emitted later in translation,
708  // the llvmValue is replaced with the actual llvm::BlockAddress.
709  // A GlobalVariable is chosen as placeholder because in general LLVM
710  // constants are uniqued and are not proper for RAUW, since that could
711  // harm unrelated uses of the constant.
712  llvmValue = new llvm::GlobalVariable(
713  *moduleTranslation.getLLVMModule(),
714  llvm::PointerType::getUnqual(moduleTranslation.getLLVMContext()),
715  /*isConstant=*/true, llvm::GlobalValue::LinkageTypes::ExternalLinkage,
716  /*Initializer=*/nullptr,
717  Twine("__mlir_block_address_")
718  .concat(Twine(fnName))
719  .concat(Twine((uint64_t)blockAddressOp.getOperation())));
720  moduleTranslation.mapUnresolvedBlockAddress(blockAddressOp, llvmValue);
721  }
722 
723  moduleTranslation.mapValue(blockAddressOp.getResult(), llvmValue);
724  return success();
725  }
726 
727  // Emit block label. If this label is seen before BlockAddressOp is
728  // translated, go ahead and already map it.
729  if (auto blockTagOp = dyn_cast<LLVM::BlockTagOp>(opInst)) {
730  auto funcOp = blockTagOp->getParentOfType<LLVMFuncOp>();
731  BlockAddressAttr blockAddressAttr = BlockAddressAttr::get(
732  &moduleTranslation.getContext(),
733  FlatSymbolRefAttr::get(&moduleTranslation.getContext(),
734  funcOp.getName()),
735  blockTagOp.getTag());
736  moduleTranslation.mapBlockAddress(blockAddressAttr,
737  builder.GetInsertBlock());
738  return success();
739  }
740 
741  return failure();
742 }
743 
744 namespace {
745 /// Implementation of the dialect interface that converts operations belonging
746 /// to the LLVM dialect to LLVM IR.
747 class LLVMDialectLLVMIRTranslationInterface
749 public:
751 
752  /// Translates the given operation to LLVM IR using the provided IR builder
753  /// and saving the state in `moduleTranslation`.
754  LogicalResult
755  convertOperation(Operation *op, llvm::IRBuilderBase &builder,
756  LLVM::ModuleTranslation &moduleTranslation) const final {
757  return convertOperationImpl(*op, builder, moduleTranslation);
758  }
759 };
760 } // namespace
761 
763  registry.insert<LLVM::LLVMDialect>();
764  registry.addExtension(+[](MLIRContext *ctx, LLVM::LLVMDialect *dialect) {
765  dialect->addInterfaces<LLVMDialectLLVMIRTranslationInterface>();
766  });
767 }
768 
770  DialectRegistry registry;
772  context.appendDialectRegistry(registry);
773 }
static SmallVector< unsigned > extractPosition(ArrayRef< int64_t > indices)
Convert the value of a DenseI64ArrayAttr to a vector of unsigned indices.
static std::string diagStr(const llvm::Type *type)
Convert an LLVM type to a string for printing in diagnostics.
static LogicalResult convertParameterAndResultAttrs(mlir::Location loc, ArrayAttr argAttrsArray, ArrayAttr resAttrsArray, llvm::CallBase *call, LLVM::ModuleTranslation &moduleTranslation)
static llvm::Metadata * convertModuleFlagProfileSummaryAttr(StringRef key, ModuleFlagProfileSummaryAttr summaryAttr, llvm::IRBuilderBase &builder, LLVM::ModuleTranslation &moduleTranslation)
static void convertModuleFlagsOp(ArrayAttr flags, llvm::IRBuilderBase &builder, LLVM::ModuleTranslation &moduleTranslation)
static FailureOr< llvm::Function * > getOverloadedDeclaration(CallIntrinsicOp op, llvm::Intrinsic::ID id, llvm::Module *module, LLVM::ModuleTranslation &moduleTranslation)
Get the declaration of an overloaded llvm intrinsic.
static LogicalResult convertOperationImpl(Operation &opInst, llvm::IRBuilderBase &builder, LLVM::ModuleTranslation &moduleTranslation)
static SmallVector< llvm::OperandBundleDef > convertOperandBundles(OperandRangeRange bundleOperands, ArrayAttr bundleTags, LLVM::ModuleTranslation &moduleTranslation)
static LogicalResult convertCallLLVMIntrinsicOp(CallIntrinsicOp op, llvm::IRBuilderBase &builder, LLVM::ModuleTranslation &moduleTranslation)
Builder for LLVM_CallIntrinsicOp.
static llvm::FastMathFlags getFastmathFlags(FastmathFlagsInterface &op)
static llvm::OperandBundleDef convertOperandBundle(OperandRange bundleOperands, StringRef bundleTag, LLVM::ModuleTranslation &moduleTranslation)
static llvm::Metadata * convertModuleFlagValue(StringRef key, ArrayAttr arrayAttr, llvm::IRBuilderBase &builder, LLVM::ModuleTranslation &moduleTranslation)
static void convertLinkerOptionsOp(ArrayAttr options, llvm::IRBuilderBase &builder, LLVM::ModuleTranslation &moduleTranslation)
static llvm::ManagedStatic< PassManagerOptions > options
static void contract(RootOrderingGraph &graph, ArrayRef< Value > cycle, const DenseMap< Value, unsigned > &parentDepths, DenseMap< Value, Value > &actualSource, DenseMap< Value, Value > &actualTarget)
Contracts the specified cycle in the given graph in-place.
const float * table
Attributes are known-constant values of operations.
Definition: Attributes.h:25
The DialectRegistry maps a dialect namespace to a constructor for the matching dialect.
bool addExtension(TypeID extensionID, std::unique_ptr< DialectExtensionBase > extension)
Add the given extension to the registry.
A symbol reference with a reference path containing a single element.
static FlatSymbolRefAttr get(StringAttr value)
Construct a symbol reference for the given value name.
Base class for dialect interfaces providing translation to LLVM IR.
Implementation class for module translation.
void mapUnresolvedBlockAddress(BlockAddressOp op, llvm::Value *cst)
Maps a blockaddress operation to its corresponding placeholder LLVM value.
llvm::Value * lookupValue(Value value) const
Finds an LLVM IR value corresponding to the given MLIR value.
void mapCall(Operation *mlir, llvm::CallInst *llvm)
Stores a mapping between an MLIR call operation and a corresponding LLVM call instruction.
FailureOr< llvm::AttrBuilder > convertParameterAttrs(mlir::Location loc, DictionaryAttr paramAttrs)
Translates parameter attributes of a call and adds them to the returned AttrBuilder.
void mapBranch(Operation *mlir, llvm::Instruction *llvm)
Stores the mapping between an MLIR operation with successors and a corresponding LLVM IR instruction.
SmallVector< llvm::Value * > lookupValues(ValueRange values)
Looks up remapped a list of remapped values.
llvm::BasicBlock * lookupBlock(Block *block) const
Finds an LLVM IR basic block that corresponds to the given MLIR block.
SymbolTableCollection & symbolTable()
llvm::Type * convertType(Type type)
Converts the type from MLIR LLVM dialect to LLVM.
llvm::GlobalValue * lookupAlias(Operation *op)
Finds an LLVM IR global value that corresponds to the given MLIR operation defining a global alias va...
void setTBAAMetadata(AliasAnalysisOpInterface op, llvm::Instruction *inst)
Sets LLVM TBAA metadata for memory operations that have TBAA attributes.
llvm::LLVMContext & getLLVMContext() const
Returns the LLVM context in which the IR is being constructed.
llvm::GlobalValue * lookupGlobal(Operation *op)
Finds an LLVM IR global value that corresponds to the given MLIR operation defining a global value.
llvm::Module * getLLVMModule()
Returns the LLVM module in which the IR is being constructed.
llvm::Function * lookupFunction(StringRef name) const
Finds an LLVM IR function by its name.
llvm::BasicBlock * lookupBlockAddress(BlockAddressAttr attr) const
Finds the LLVM basic block that corresponds to the given BlockAddressAttr.
void setAliasScopeMetadata(AliasAnalysisOpInterface op, llvm::Instruction *inst)
void setAccessGroupsMetadata(AccessGroupOpInterface op, llvm::Instruction *inst)
MLIRContext & getContext()
Returns the MLIR context of the module being translated.
void mapValue(Value mlir, llvm::Value *llvm)
Stores the mapping between an MLIR value and its LLVM IR counterpart.
void mapBlockAddress(BlockAddressAttr attr, llvm::BasicBlock *block)
Maps a BlockAddressAttr to its corresponding LLVM basic block.
void setLoopMetadata(Operation *op, llvm::Instruction *inst)
Sets LLVM loop metadata for branch operations that have a loop annotation attribute.
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition: Location.h:76
MLIRContext is the top-level object for a collection of MLIR operations.
Definition: MLIRContext.h:60
void appendDialectRegistry(const DialectRegistry &registry)
Append the contents of the given dialect registry to the registry associated with this context.
This class represents a contiguous range of operand ranges, e.g.
Definition: ValueRange.h:84
This class implements the operand iterators for the Operation class.
Definition: ValueRange.h:43
Operation is the basic unit of execution within MLIR.
Definition: Operation.h:88
AttrClass getAttrOfType(StringAttr name)
Definition: Operation.h:550
OpResult getResult(unsigned idx)
Get the 'idx'th result of this operation.
Definition: Operation.h:407
unsigned getNumResults()
Return the number of results held by this operation.
Definition: Operation.h:404
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition: Types.h:74
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition: Value.h:96
llvm::Constant * getLLVMConstant(llvm::Type *llvmType, Attribute attr, Location loc, const ModuleTranslation &moduleTranslation)
Create an LLVM IR constant of llvmType from the MLIR attribute attr.
constexpr void enumerate(std::tuple< Tys... > &tuple, CallbackT &&callback)
Definition: Matchers.h:344
SmallVector< AffineExpr, 4 > concat(ArrayRef< AffineExpr > a, ArrayRef< AffineExpr > b)
Return the vector that is the concatenation of a and b.
Definition: LinalgOps.cpp:2424
Include the generated interface declarations.
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.
void registerLLVMDialectTranslation(DialectRegistry &registry)
Register the LLVM dialect and the translation from it to the LLVM IR in the given registry;.
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...