MLIR  22.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  if (llvm::Function *function =
426  moduleTranslation.lookupFunction(attr.getValue())) {
427  call = builder.CreateCall(function, operandsRef, opBundles);
428  } else {
429  Operation *moduleOp = parentLLVMModule(&opInst);
430  Operation *ifuncOp =
431  moduleTranslation.symbolTable().lookupSymbolIn(moduleOp, attr);
432  llvm::GlobalValue *ifunc = moduleTranslation.lookupIFunc(ifuncOp);
433  llvm::FunctionType *calleeType = llvm::cast<llvm::FunctionType>(
434  moduleTranslation.convertType(callOp.getCalleeFunctionType()));
435  call = builder.CreateCall(calleeType, ifunc, operandsRef, opBundles);
436  }
437  } else {
438  llvm::FunctionType *calleeType = llvm::cast<llvm::FunctionType>(
439  moduleTranslation.convertType(callOp.getCalleeFunctionType()));
440  call = builder.CreateCall(calleeType, operandsRef.front(),
441  operandsRef.drop_front(), opBundles);
442  }
443  call->setCallingConv(convertCConvToLLVM(callOp.getCConv()));
444  call->setTailCallKind(convertTailCallKindToLLVM(callOp.getTailCallKind()));
445  if (callOp.getConvergentAttr())
446  call->addFnAttr(llvm::Attribute::Convergent);
447  if (callOp.getNoUnwindAttr())
448  call->addFnAttr(llvm::Attribute::NoUnwind);
449  if (callOp.getWillReturnAttr())
450  call->addFnAttr(llvm::Attribute::WillReturn);
451  if (callOp.getNoInlineAttr())
452  call->addFnAttr(llvm::Attribute::NoInline);
453  if (callOp.getAlwaysInlineAttr())
454  call->addFnAttr(llvm::Attribute::AlwaysInline);
455  if (callOp.getInlineHintAttr())
456  call->addFnAttr(llvm::Attribute::InlineHint);
457 
458  if (failed(convertParameterAndResultAttrs(callOp, call, moduleTranslation)))
459  return failure();
460 
461  if (MemoryEffectsAttr memAttr = callOp.getMemoryEffectsAttr()) {
462  llvm::MemoryEffects memEffects =
463  llvm::MemoryEffects(llvm::MemoryEffects::Location::ArgMem,
464  convertModRefInfoToLLVM(memAttr.getArgMem())) |
465  llvm::MemoryEffects(
466  llvm::MemoryEffects::Location::InaccessibleMem,
467  convertModRefInfoToLLVM(memAttr.getInaccessibleMem())) |
468  llvm::MemoryEffects(llvm::MemoryEffects::Location::Other,
469  convertModRefInfoToLLVM(memAttr.getOther()));
470  call->setMemoryEffects(memEffects);
471  }
472 
473  moduleTranslation.setAccessGroupsMetadata(callOp, call);
474  moduleTranslation.setAliasScopeMetadata(callOp, call);
475  moduleTranslation.setTBAAMetadata(callOp, call);
476  // If the called function has a result, remap the corresponding value. Note
477  // that LLVM IR dialect CallOp has either 0 or 1 result.
478  if (opInst.getNumResults() != 0)
479  moduleTranslation.mapValue(opInst.getResult(0), call);
480  // Check that LLVM call returns void for 0-result functions.
481  else if (!call->getType()->isVoidTy())
482  return failure();
483  moduleTranslation.mapCall(callOp, call);
484  return success();
485  }
486 
487  if (auto inlineAsmOp = dyn_cast<LLVM::InlineAsmOp>(opInst)) {
488  // TODO: refactor function type creation which usually occurs in std-LLVM
489  // conversion.
490  SmallVector<Type, 8> operandTypes;
491  llvm::append_range(operandTypes, inlineAsmOp.getOperands().getTypes());
492 
493  Type resultType;
494  if (inlineAsmOp.getNumResults() == 0) {
495  resultType = LLVM::LLVMVoidType::get(&moduleTranslation.getContext());
496  } else {
497  assert(inlineAsmOp.getNumResults() == 1);
498  resultType = inlineAsmOp.getResultTypes()[0];
499  }
500  auto ft = LLVM::LLVMFunctionType::get(resultType, operandTypes);
501  llvm::InlineAsm *inlineAsmInst =
502  inlineAsmOp.getAsmDialect()
504  static_cast<llvm::FunctionType *>(
505  moduleTranslation.convertType(ft)),
506  inlineAsmOp.getAsmString(), inlineAsmOp.getConstraints(),
507  inlineAsmOp.getHasSideEffects(),
508  inlineAsmOp.getIsAlignStack(),
509  convertAsmDialectToLLVM(*inlineAsmOp.getAsmDialect()))
510  : llvm::InlineAsm::get(static_cast<llvm::FunctionType *>(
511  moduleTranslation.convertType(ft)),
512  inlineAsmOp.getAsmString(),
513  inlineAsmOp.getConstraints(),
514  inlineAsmOp.getHasSideEffects(),
515  inlineAsmOp.getIsAlignStack());
516  llvm::CallInst *inst = builder.CreateCall(
517  inlineAsmInst,
518  moduleTranslation.lookupValues(inlineAsmOp.getOperands()));
519  inst->setTailCallKind(convertTailCallKindToLLVM(
520  inlineAsmOp.getTailCallKindAttr().getTailCallKind()));
521  if (auto maybeOperandAttrs = inlineAsmOp.getOperandAttrs()) {
522  llvm::AttributeList attrList;
523  for (const auto &it : llvm::enumerate(*maybeOperandAttrs)) {
524  Attribute attr = it.value();
525  if (!attr)
526  continue;
527  DictionaryAttr dAttr = cast<DictionaryAttr>(attr);
528  if (dAttr.empty())
529  continue;
530  TypeAttr tAttr =
531  cast<TypeAttr>(dAttr.get(InlineAsmOp::getElementTypeAttrName()));
532  llvm::AttrBuilder b(moduleTranslation.getLLVMContext());
533  llvm::Type *ty = moduleTranslation.convertType(tAttr.getValue());
534  b.addTypeAttr(llvm::Attribute::ElementType, ty);
535  // shift to account for the returned value (this is always 1 aggregate
536  // value in LLVM).
537  int shift = (opInst.getNumResults() > 0) ? 1 : 0;
538  attrList = attrList.addAttributesAtIndex(
539  moduleTranslation.getLLVMContext(), it.index() + shift, b);
540  }
541  inst->setAttributes(attrList);
542  }
543 
544  if (opInst.getNumResults() != 0)
545  moduleTranslation.mapValue(opInst.getResult(0), inst);
546  return success();
547  }
548 
549  if (auto invOp = dyn_cast<LLVM::InvokeOp>(opInst)) {
550  auto operands = moduleTranslation.lookupValues(invOp.getCalleeOperands());
552  convertOperandBundles(invOp.getOpBundleOperands(),
553  invOp.getOpBundleTags(), moduleTranslation);
554  ArrayRef<llvm::Value *> operandsRef(operands);
555  llvm::InvokeInst *result;
556  if (auto attr = opInst.getAttrOfType<FlatSymbolRefAttr>("callee")) {
557  result = builder.CreateInvoke(
558  moduleTranslation.lookupFunction(attr.getValue()),
559  moduleTranslation.lookupBlock(invOp.getSuccessor(0)),
560  moduleTranslation.lookupBlock(invOp.getSuccessor(1)), operandsRef,
561  opBundles);
562  } else {
563  llvm::FunctionType *calleeType = llvm::cast<llvm::FunctionType>(
564  moduleTranslation.convertType(invOp.getCalleeFunctionType()));
565  result = builder.CreateInvoke(
566  calleeType, operandsRef.front(),
567  moduleTranslation.lookupBlock(invOp.getSuccessor(0)),
568  moduleTranslation.lookupBlock(invOp.getSuccessor(1)),
569  operandsRef.drop_front(), opBundles);
570  }
571  result->setCallingConv(convertCConvToLLVM(invOp.getCConv()));
572  if (failed(
573  convertParameterAndResultAttrs(invOp, result, moduleTranslation)))
574  return failure();
575  moduleTranslation.mapBranch(invOp, result);
576  // InvokeOp can only have 0 or 1 result
577  if (invOp->getNumResults() != 0) {
578  moduleTranslation.mapValue(opInst.getResult(0), result);
579  return success();
580  }
581  return success(result->getType()->isVoidTy());
582  }
583 
584  if (auto lpOp = dyn_cast<LLVM::LandingpadOp>(opInst)) {
585  llvm::Type *ty = moduleTranslation.convertType(lpOp.getType());
586  llvm::LandingPadInst *lpi =
587  builder.CreateLandingPad(ty, lpOp.getNumOperands());
588  lpi->setCleanup(lpOp.getCleanup());
589 
590  // Add clauses
591  for (llvm::Value *operand :
592  moduleTranslation.lookupValues(lpOp.getOperands())) {
593  // All operands should be constant - checked by verifier
594  if (auto *constOperand = dyn_cast<llvm::Constant>(operand))
595  lpi->addClause(constOperand);
596  }
597  moduleTranslation.mapValue(lpOp.getResult(), lpi);
598  return success();
599  }
600 
601  // Emit branches. We need to look up the remapped blocks and ignore the
602  // block arguments that were transformed into PHI nodes.
603  if (auto brOp = dyn_cast<LLVM::BrOp>(opInst)) {
604  llvm::BranchInst *branch =
605  builder.CreateBr(moduleTranslation.lookupBlock(brOp.getSuccessor()));
606  moduleTranslation.mapBranch(&opInst, branch);
607  moduleTranslation.setLoopMetadata(&opInst, branch);
608  return success();
609  }
610  if (auto condbrOp = dyn_cast<LLVM::CondBrOp>(opInst)) {
611  llvm::BranchInst *branch = builder.CreateCondBr(
612  moduleTranslation.lookupValue(condbrOp.getOperand(0)),
613  moduleTranslation.lookupBlock(condbrOp.getSuccessor(0)),
614  moduleTranslation.lookupBlock(condbrOp.getSuccessor(1)));
615  moduleTranslation.mapBranch(&opInst, branch);
616  moduleTranslation.setLoopMetadata(&opInst, branch);
617  return success();
618  }
619  if (auto switchOp = dyn_cast<LLVM::SwitchOp>(opInst)) {
620  llvm::SwitchInst *switchInst = builder.CreateSwitch(
621  moduleTranslation.lookupValue(switchOp.getValue()),
622  moduleTranslation.lookupBlock(switchOp.getDefaultDestination()),
623  switchOp.getCaseDestinations().size());
624 
625  // Handle switch with zero cases.
626  if (!switchOp.getCaseValues())
627  return success();
628 
629  auto *ty = llvm::cast<llvm::IntegerType>(
630  moduleTranslation.convertType(switchOp.getValue().getType()));
631  for (auto i :
632  llvm::zip(llvm::cast<DenseIntElementsAttr>(*switchOp.getCaseValues()),
633  switchOp.getCaseDestinations()))
634  switchInst->addCase(
635  llvm::ConstantInt::get(ty, std::get<0>(i).getLimitedValue()),
636  moduleTranslation.lookupBlock(std::get<1>(i)));
637 
638  moduleTranslation.mapBranch(&opInst, switchInst);
639  return success();
640  }
641  if (auto indBrOp = dyn_cast<LLVM::IndirectBrOp>(opInst)) {
642  llvm::IndirectBrInst *indBr = builder.CreateIndirectBr(
643  moduleTranslation.lookupValue(indBrOp.getAddr()),
644  indBrOp->getNumSuccessors());
645  for (auto *succ : indBrOp.getSuccessors())
646  indBr->addDestination(moduleTranslation.lookupBlock(succ));
647  moduleTranslation.mapBranch(&opInst, indBr);
648  return success();
649  }
650 
651  // Emit addressof. We need to look up the global value referenced by the
652  // operation and store it in the MLIR-to-LLVM value mapping. This does not
653  // emit any LLVM instruction.
654  if (auto addressOfOp = dyn_cast<LLVM::AddressOfOp>(opInst)) {
655  LLVM::GlobalOp global =
656  addressOfOp.getGlobal(moduleTranslation.symbolTable());
657  LLVM::LLVMFuncOp function =
658  addressOfOp.getFunction(moduleTranslation.symbolTable());
659  LLVM::AliasOp alias = addressOfOp.getAlias(moduleTranslation.symbolTable());
660  LLVM::IFuncOp ifunc = addressOfOp.getIFunc(moduleTranslation.symbolTable());
661 
662  // The verifier should not have allowed this.
663  assert((global || function || alias || ifunc) &&
664  "referencing an undefined global, function, alias, or ifunc");
665 
666  llvm::Value *llvmValue = nullptr;
667  if (global)
668  llvmValue = moduleTranslation.lookupGlobal(global);
669  else if (alias)
670  llvmValue = moduleTranslation.lookupAlias(alias);
671  else if (function)
672  llvmValue = moduleTranslation.lookupFunction(function.getName());
673  else
674  llvmValue = moduleTranslation.lookupIFunc(ifunc);
675 
676  moduleTranslation.mapValue(addressOfOp.getResult(), llvmValue);
677  return success();
678  }
679 
680  // Emit dso_local_equivalent. We need to look up the global value referenced
681  // by the operation and store it in the MLIR-to-LLVM value mapping.
682  if (auto dsoLocalEquivalentOp =
683  dyn_cast<LLVM::DSOLocalEquivalentOp>(opInst)) {
684  LLVM::LLVMFuncOp function =
685  dsoLocalEquivalentOp.getFunction(moduleTranslation.symbolTable());
686  LLVM::AliasOp alias =
687  dsoLocalEquivalentOp.getAlias(moduleTranslation.symbolTable());
688 
689  // The verifier should not have allowed this.
690  assert((function || alias) &&
691  "referencing an undefined function, or alias");
692 
693  llvm::Value *llvmValue = nullptr;
694  if (alias)
695  llvmValue = moduleTranslation.lookupAlias(alias);
696  else
697  llvmValue = moduleTranslation.lookupFunction(function.getName());
698 
699  moduleTranslation.mapValue(
700  dsoLocalEquivalentOp.getResult(),
701  llvm::DSOLocalEquivalent::get(cast<llvm::GlobalValue>(llvmValue)));
702  return success();
703  }
704 
705  // Emit blockaddress. We first need to find the LLVM block referenced by this
706  // operation and then create a LLVM block address for it.
707  if (auto blockAddressOp = dyn_cast<LLVM::BlockAddressOp>(opInst)) {
708  BlockAddressAttr blockAddressAttr = blockAddressOp.getBlockAddr();
709  llvm::BasicBlock *llvmBlock =
710  moduleTranslation.lookupBlockAddress(blockAddressAttr);
711 
712  llvm::Value *llvmValue = nullptr;
713  StringRef fnName = blockAddressAttr.getFunction().getValue();
714  if (llvmBlock) {
715  llvm::Function *llvmFn = moduleTranslation.lookupFunction(fnName);
716  llvmValue = llvm::BlockAddress::get(llvmFn, llvmBlock);
717  } else {
718  // The matching LLVM block is not yet emitted, a placeholder is created
719  // in its place. When the LLVM block is emitted later in translation,
720  // the llvmValue is replaced with the actual llvm::BlockAddress.
721  // A GlobalVariable is chosen as placeholder because in general LLVM
722  // constants are uniqued and are not proper for RAUW, since that could
723  // harm unrelated uses of the constant.
724  llvmValue = new llvm::GlobalVariable(
725  *moduleTranslation.getLLVMModule(),
726  llvm::PointerType::getUnqual(moduleTranslation.getLLVMContext()),
727  /*isConstant=*/true, llvm::GlobalValue::LinkageTypes::ExternalLinkage,
728  /*Initializer=*/nullptr,
729  Twine("__mlir_block_address_")
730  .concat(Twine(fnName))
731  .concat(Twine((uint64_t)blockAddressOp.getOperation())));
732  moduleTranslation.mapUnresolvedBlockAddress(blockAddressOp, llvmValue);
733  }
734 
735  moduleTranslation.mapValue(blockAddressOp.getResult(), llvmValue);
736  return success();
737  }
738 
739  // Emit block label. If this label is seen before BlockAddressOp is
740  // translated, go ahead and already map it.
741  if (auto blockTagOp = dyn_cast<LLVM::BlockTagOp>(opInst)) {
742  auto funcOp = blockTagOp->getParentOfType<LLVMFuncOp>();
743  BlockAddressAttr blockAddressAttr = BlockAddressAttr::get(
744  &moduleTranslation.getContext(),
745  FlatSymbolRefAttr::get(&moduleTranslation.getContext(),
746  funcOp.getName()),
747  blockTagOp.getTag());
748  moduleTranslation.mapBlockAddress(blockAddressAttr,
749  builder.GetInsertBlock());
750  return success();
751  }
752 
753  return failure();
754 }
755 
756 namespace {
757 /// Implementation of the dialect interface that converts operations belonging
758 /// to the LLVM dialect to LLVM IR.
759 class LLVMDialectLLVMIRTranslationInterface
761 public:
763 
764  /// Translates the given operation to LLVM IR using the provided IR builder
765  /// and saving the state in `moduleTranslation`.
766  LogicalResult
767  convertOperation(Operation *op, llvm::IRBuilderBase &builder,
768  LLVM::ModuleTranslation &moduleTranslation) const final {
769  return convertOperationImpl(*op, builder, moduleTranslation);
770  }
771 };
772 } // namespace
773 
775  registry.insert<LLVM::LLVMDialect>();
776  registry.addExtension(+[](MLIRContext *ctx, LLVM::LLVMDialect *dialect) {
777  dialect->addInterfaces<LLVMDialectLLVMIRTranslationInterface>();
778  });
779 }
780 
782  DialectRegistry registry;
784  context.appendDialectRegistry(registry);
785 }
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::GlobalValue * lookupIFunc(Operation *op)
Finds an LLVM IR global value that corresponds to the given MLIR operation defining an IFunc.
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
virtual Operation * lookupSymbolIn(Operation *symbolTableOp, StringAttr symbol)
Look up a symbol with the specified name within the specified symbol table operation,...
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.
Operation * parentLLVMModule(Operation *op)
Lookup parent Module satisfying LLVM conditions on the Module Operation.
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...