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/IR/IRBuilder.h"
20 #include "llvm/IR/InlineAsm.h"
21 #include "llvm/IR/MDBuilder.h"
22 #include "llvm/IR/MatrixBuilder.h"
23 #include "llvm/IR/Operator.h"
24 
25 using namespace mlir;
26 using namespace mlir::LLVM;
28 
29 #include "mlir/Dialect/LLVMIR/LLVMConversionEnumsToLLVM.inc"
30 
31 static llvm::FastMathFlags getFastmathFlags(FastmathFlagsInterface &op) {
32  using llvmFMF = llvm::FastMathFlags;
33  using FuncT = void (llvmFMF::*)(bool);
34  const std::pair<FastmathFlags, FuncT> handlers[] = {
35  // clang-format off
36  {FastmathFlags::nnan, &llvmFMF::setNoNaNs},
37  {FastmathFlags::ninf, &llvmFMF::setNoInfs},
38  {FastmathFlags::nsz, &llvmFMF::setNoSignedZeros},
39  {FastmathFlags::arcp, &llvmFMF::setAllowReciprocal},
40  {FastmathFlags::contract, &llvmFMF::setAllowContract},
41  {FastmathFlags::afn, &llvmFMF::setApproxFunc},
42  {FastmathFlags::reassoc, &llvmFMF::setAllowReassoc},
43  // clang-format on
44  };
45  llvm::FastMathFlags ret;
46  ::mlir::LLVM::FastmathFlags fmfMlir = op.getFastmathAttr().getValue();
47  for (auto it : handlers)
48  if (bitEnumContainsAll(fmfMlir, it.first))
49  (ret.*(it.second))(true);
50  return ret;
51 }
52 
53 /// Convert the value of a DenseI64ArrayAttr to a vector of unsigned indices.
55  SmallVector<unsigned> position;
56  llvm::append_range(position, indices);
57  return position;
58 }
59 
60 /// Convert an LLVM type to a string for printing in diagnostics.
61 static std::string diagStr(const llvm::Type *type) {
62  std::string str;
63  llvm::raw_string_ostream os(str);
64  type->print(os);
65  return str;
66 }
67 
68 /// Get the declaration of an overloaded llvm intrinsic. First we get the
69 /// overloaded argument types and/or result type from the CallIntrinsicOp, and
70 /// then use those to get the correct declaration of the overloaded intrinsic.
71 static FailureOr<llvm::Function *>
73  llvm::Module *module,
74  LLVM::ModuleTranslation &moduleTranslation) {
76  for (Type type : op->getOperandTypes())
77  allArgTys.push_back(moduleTranslation.convertType(type));
78 
79  llvm::Type *resTy;
80  if (op.getNumResults() == 0)
81  resTy = llvm::Type::getVoidTy(module->getContext());
82  else
83  resTy = moduleTranslation.convertType(op.getResult(0).getType());
84 
85  // ATM we do not support variadic intrinsics.
86  llvm::FunctionType *ft = llvm::FunctionType::get(resTy, allArgTys, false);
87 
89  getIntrinsicInfoTableEntries(id, table);
91 
92  SmallVector<llvm::Type *, 8> overloadedArgTys;
93  if (llvm::Intrinsic::matchIntrinsicSignature(ft, tableRef,
94  overloadedArgTys) !=
95  llvm::Intrinsic::MatchIntrinsicTypesResult::MatchIntrinsicTypes_Match) {
96  return mlir::emitError(op.getLoc(), "call intrinsic signature ")
97  << diagStr(ft) << " to overloaded intrinsic " << op.getIntrinAttr()
98  << " does not match any of the overloads";
99  }
100 
101  ArrayRef<llvm::Type *> overloadedArgTysRef = overloadedArgTys;
102  return llvm::Intrinsic::getOrInsertDeclaration(module, id,
103  overloadedArgTysRef);
104 }
105 
106 static llvm::OperandBundleDef
107 convertOperandBundle(OperandRange bundleOperands, StringRef bundleTag,
108  LLVM::ModuleTranslation &moduleTranslation) {
109  std::vector<llvm::Value *> operands;
110  operands.reserve(bundleOperands.size());
111  for (Value bundleArg : bundleOperands)
112  operands.push_back(moduleTranslation.lookupValue(bundleArg));
113  return llvm::OperandBundleDef(bundleTag.str(), std::move(operands));
114 }
115 
117 convertOperandBundles(OperandRangeRange bundleOperands, ArrayAttr bundleTags,
118  LLVM::ModuleTranslation &moduleTranslation) {
120  bundles.reserve(bundleOperands.size());
121 
122  for (auto [operands, tagAttr] : llvm::zip_equal(bundleOperands, bundleTags)) {
123  StringRef tag = cast<StringAttr>(tagAttr).getValue();
124  bundles.push_back(convertOperandBundle(operands, tag, moduleTranslation));
125  }
126  return bundles;
127 }
128 
131  std::optional<ArrayAttr> bundleTags,
132  LLVM::ModuleTranslation &moduleTranslation) {
133  if (!bundleTags)
134  return {};
135  return convertOperandBundles(bundleOperands, *bundleTags, moduleTranslation);
136 }
137 
138 static LogicalResult
139 convertParameterAndResultAttrs(mlir::Location loc, ArrayAttr argAttrsArray,
140  ArrayAttr resAttrsArray, llvm::CallBase *call,
141  LLVM::ModuleTranslation &moduleTranslation) {
142  if (argAttrsArray) {
143  for (auto [argIdx, argAttrsAttr] : llvm::enumerate(argAttrsArray)) {
144  if (auto argAttrs = cast<DictionaryAttr>(argAttrsAttr);
145  !argAttrs.empty()) {
146  FailureOr<llvm::AttrBuilder> attrBuilder =
147  moduleTranslation.convertParameterAttrs(loc, argAttrs);
148  if (failed(attrBuilder))
149  return failure();
150  call->addParamAttrs(argIdx, *attrBuilder);
151  }
152  }
153  }
154 
155  if (resAttrsArray && resAttrsArray.size() > 0) {
156  if (resAttrsArray.size() != 1)
157  return mlir::emitError(loc, "llvm.func cannot have multiple results");
158  if (auto resAttrs = cast<DictionaryAttr>(resAttrsArray[0]);
159  !resAttrs.empty()) {
160  FailureOr<llvm::AttrBuilder> attrBuilder =
161  moduleTranslation.convertParameterAttrs(loc, resAttrs);
162  if (failed(attrBuilder))
163  return failure();
164  call->addRetAttrs(*attrBuilder);
165  }
166  }
167  return success();
168 }
169 
170 static LogicalResult
171 convertParameterAndResultAttrs(CallOpInterface callOp, llvm::CallBase *call,
172  LLVM::ModuleTranslation &moduleTranslation) {
174  callOp.getLoc(), callOp.getArgAttrsAttr(), callOp.getResAttrsAttr(), call,
175  moduleTranslation);
176 }
177 
178 /// Builder for LLVM_CallIntrinsicOp
179 static LogicalResult
180 convertCallLLVMIntrinsicOp(CallIntrinsicOp op, llvm::IRBuilderBase &builder,
181  LLVM::ModuleTranslation &moduleTranslation) {
182  llvm::Module *module = builder.GetInsertBlock()->getModule();
184  llvm::Intrinsic::lookupIntrinsicID(op.getIntrinAttr());
185  if (!id)
186  return mlir::emitError(op.getLoc(), "could not find LLVM intrinsic: ")
187  << op.getIntrinAttr();
188 
189  llvm::Function *fn = nullptr;
190  if (llvm::Intrinsic::isOverloaded(id)) {
191  auto fnOrFailure =
192  getOverloadedDeclaration(op, id, module, moduleTranslation);
193  if (failed(fnOrFailure))
194  return failure();
195  fn = *fnOrFailure;
196  } else {
197  fn = llvm::Intrinsic::getOrInsertDeclaration(module, id, {});
198  }
199 
200  // Check the result type of the call.
201  const llvm::Type *intrinType =
202  op.getNumResults() == 0
203  ? llvm::Type::getVoidTy(module->getContext())
204  : moduleTranslation.convertType(op.getResultTypes().front());
205  if (intrinType != fn->getReturnType()) {
206  return mlir::emitError(op.getLoc(), "intrinsic call returns ")
207  << diagStr(intrinType) << " but " << op.getIntrinAttr()
208  << " actually returns " << diagStr(fn->getReturnType());
209  }
210 
211  // Check the argument types of the call. If the function is variadic, check
212  // the subrange of required arguments.
213  if (!fn->getFunctionType()->isVarArg() &&
214  op.getArgs().size() != fn->arg_size()) {
215  return mlir::emitError(op.getLoc(), "intrinsic call has ")
216  << op.getArgs().size() << " operands but " << op.getIntrinAttr()
217  << " expects " << fn->arg_size();
218  }
219  if (fn->getFunctionType()->isVarArg() &&
220  op.getArgs().size() < fn->arg_size()) {
221  return mlir::emitError(op.getLoc(), "intrinsic call has ")
222  << op.getArgs().size() << " operands but variadic "
223  << op.getIntrinAttr() << " expects at least " << fn->arg_size();
224  }
225  // Check the arguments up to the number the function requires.
226  for (unsigned i = 0, e = fn->arg_size(); i != e; ++i) {
227  const llvm::Type *expected = fn->getArg(i)->getType();
228  const llvm::Type *actual =
229  moduleTranslation.convertType(op.getOperandTypes()[i]);
230  if (actual != expected) {
231  return mlir::emitError(op.getLoc(), "intrinsic call operand #")
232  << i << " has type " << diagStr(actual) << " but "
233  << op.getIntrinAttr() << " expects " << diagStr(expected);
234  }
235  }
236 
237  FastmathFlagsInterface itf = op;
238  builder.setFastMathFlags(getFastmathFlags(itf));
239 
240  auto *inst = builder.CreateCall(
241  fn, moduleTranslation.lookupValues(op.getArgs()),
242  convertOperandBundles(op.getOpBundleOperands(), op.getOpBundleTags(),
243  moduleTranslation));
244 
245  if (failed(convertParameterAndResultAttrs(op.getLoc(), op.getArgAttrsAttr(),
246  op.getResAttrsAttr(), inst,
247  moduleTranslation)))
248  return failure();
249 
250  if (op.getNumResults() == 1)
251  moduleTranslation.mapValue(op->getResults().front()) = inst;
252  return success();
253 }
254 
255 static void convertLinkerOptionsOp(ArrayAttr options,
256  llvm::IRBuilderBase &builder,
257  LLVM::ModuleTranslation &moduleTranslation) {
258  llvm::Module *llvmModule = moduleTranslation.getLLVMModule();
259  llvm::LLVMContext &context = llvmModule->getContext();
260  llvm::NamedMDNode *linkerMDNode =
261  llvmModule->getOrInsertNamedMetadata("llvm.linker.options");
263  MDNodes.reserve(options.size());
264  for (auto s : options.getAsRange<StringAttr>()) {
265  auto *MDNode = llvm::MDString::get(context, s.getValue());
266  MDNodes.push_back(MDNode);
267  }
268 
269  auto *listMDNode = llvm::MDTuple::get(context, MDNodes);
270  linkerMDNode->addOperand(listMDNode);
271 }
272 
273 static void convertModuleFlagsOp(ArrayAttr flags, llvm::IRBuilderBase &builder,
274  LLVM::ModuleTranslation &moduleTranslation) {
275  llvm::Module *llvmModule = moduleTranslation.getLLVMModule();
276  for (auto flagAttr : flags.getAsRange<ModuleFlagAttr>())
277  llvmModule->addModuleFlag(
278  convertModFlagBehaviorToLLVM(flagAttr.getBehavior()),
279  flagAttr.getKey().getValue(), flagAttr.getValue());
280 }
281 
282 static LogicalResult
283 convertOperationImpl(Operation &opInst, llvm::IRBuilderBase &builder,
284  LLVM::ModuleTranslation &moduleTranslation) {
285 
286  llvm::IRBuilder<>::FastMathFlagGuard fmfGuard(builder);
287  if (auto fmf = dyn_cast<FastmathFlagsInterface>(opInst))
288  builder.setFastMathFlags(getFastmathFlags(fmf));
289 
290 #include "mlir/Dialect/LLVMIR/LLVMConversions.inc"
291 #include "mlir/Dialect/LLVMIR/LLVMIntrinsicConversions.inc"
292 
293  // Emit function calls. If the "callee" attribute is present, this is a
294  // direct function call and we also need to look up the remapped function
295  // itself. Otherwise, this is an indirect call and the callee is the first
296  // operand, look it up as a normal value.
297  if (auto callOp = dyn_cast<LLVM::CallOp>(opInst)) {
298  auto operands = moduleTranslation.lookupValues(callOp.getCalleeOperands());
300  convertOperandBundles(callOp.getOpBundleOperands(),
301  callOp.getOpBundleTags(), moduleTranslation);
302  ArrayRef<llvm::Value *> operandsRef(operands);
303  llvm::CallInst *call;
304  if (auto attr = callOp.getCalleeAttr()) {
305  call =
306  builder.CreateCall(moduleTranslation.lookupFunction(attr.getValue()),
307  operandsRef, opBundles);
308  } else {
309  llvm::FunctionType *calleeType = llvm::cast<llvm::FunctionType>(
310  moduleTranslation.convertType(callOp.getCalleeFunctionType()));
311  call = builder.CreateCall(calleeType, operandsRef.front(),
312  operandsRef.drop_front(), opBundles);
313  }
314  call->setCallingConv(convertCConvToLLVM(callOp.getCConv()));
315  call->setTailCallKind(convertTailCallKindToLLVM(callOp.getTailCallKind()));
316  if (callOp.getConvergentAttr())
317  call->addFnAttr(llvm::Attribute::Convergent);
318  if (callOp.getNoUnwindAttr())
319  call->addFnAttr(llvm::Attribute::NoUnwind);
320  if (callOp.getWillReturnAttr())
321  call->addFnAttr(llvm::Attribute::WillReturn);
322  if (callOp.getNoInlineAttr())
323  call->addFnAttr(llvm::Attribute::NoInline);
324  if (callOp.getAlwaysInlineAttr())
325  call->addFnAttr(llvm::Attribute::AlwaysInline);
326  if (callOp.getInlineHintAttr())
327  call->addFnAttr(llvm::Attribute::InlineHint);
328 
329  if (failed(convertParameterAndResultAttrs(callOp, call, moduleTranslation)))
330  return failure();
331 
332  if (MemoryEffectsAttr memAttr = callOp.getMemoryEffectsAttr()) {
333  llvm::MemoryEffects memEffects =
334  llvm::MemoryEffects(llvm::MemoryEffects::Location::ArgMem,
335  convertModRefInfoToLLVM(memAttr.getArgMem())) |
336  llvm::MemoryEffects(
337  llvm::MemoryEffects::Location::InaccessibleMem,
338  convertModRefInfoToLLVM(memAttr.getInaccessibleMem())) |
339  llvm::MemoryEffects(llvm::MemoryEffects::Location::Other,
340  convertModRefInfoToLLVM(memAttr.getOther()));
341  call->setMemoryEffects(memEffects);
342  }
343 
344  moduleTranslation.setAccessGroupsMetadata(callOp, call);
345  moduleTranslation.setAliasScopeMetadata(callOp, call);
346  moduleTranslation.setTBAAMetadata(callOp, call);
347  // If the called function has a result, remap the corresponding value. Note
348  // that LLVM IR dialect CallOp has either 0 or 1 result.
349  if (opInst.getNumResults() != 0)
350  moduleTranslation.mapValue(opInst.getResult(0), call);
351  // Check that LLVM call returns void for 0-result functions.
352  else if (!call->getType()->isVoidTy())
353  return failure();
354  moduleTranslation.mapCall(callOp, call);
355  return success();
356  }
357 
358  if (auto inlineAsmOp = dyn_cast<LLVM::InlineAsmOp>(opInst)) {
359  // TODO: refactor function type creation which usually occurs in std-LLVM
360  // conversion.
361  SmallVector<Type, 8> operandTypes;
362  llvm::append_range(operandTypes, inlineAsmOp.getOperands().getTypes());
363 
364  Type resultType;
365  if (inlineAsmOp.getNumResults() == 0) {
366  resultType = LLVM::LLVMVoidType::get(&moduleTranslation.getContext());
367  } else {
368  assert(inlineAsmOp.getNumResults() == 1);
369  resultType = inlineAsmOp.getResultTypes()[0];
370  }
371  auto ft = LLVM::LLVMFunctionType::get(resultType, operandTypes);
372  llvm::InlineAsm *inlineAsmInst =
373  inlineAsmOp.getAsmDialect()
375  static_cast<llvm::FunctionType *>(
376  moduleTranslation.convertType(ft)),
377  inlineAsmOp.getAsmString(), inlineAsmOp.getConstraints(),
378  inlineAsmOp.getHasSideEffects(),
379  inlineAsmOp.getIsAlignStack(),
380  convertAsmDialectToLLVM(*inlineAsmOp.getAsmDialect()))
381  : llvm::InlineAsm::get(static_cast<llvm::FunctionType *>(
382  moduleTranslation.convertType(ft)),
383  inlineAsmOp.getAsmString(),
384  inlineAsmOp.getConstraints(),
385  inlineAsmOp.getHasSideEffects(),
386  inlineAsmOp.getIsAlignStack());
387  llvm::CallInst *inst = builder.CreateCall(
388  inlineAsmInst,
389  moduleTranslation.lookupValues(inlineAsmOp.getOperands()));
390  if (auto maybeOperandAttrs = inlineAsmOp.getOperandAttrs()) {
391  llvm::AttributeList attrList;
392  for (const auto &it : llvm::enumerate(*maybeOperandAttrs)) {
393  Attribute attr = it.value();
394  if (!attr)
395  continue;
396  DictionaryAttr dAttr = cast<DictionaryAttr>(attr);
397  TypeAttr tAttr =
398  cast<TypeAttr>(dAttr.get(InlineAsmOp::getElementTypeAttrName()));
399  llvm::AttrBuilder b(moduleTranslation.getLLVMContext());
400  llvm::Type *ty = moduleTranslation.convertType(tAttr.getValue());
401  b.addTypeAttr(llvm::Attribute::ElementType, ty);
402  // shift to account for the returned value (this is always 1 aggregate
403  // value in LLVM).
404  int shift = (opInst.getNumResults() > 0) ? 1 : 0;
405  attrList = attrList.addAttributesAtIndex(
406  moduleTranslation.getLLVMContext(), it.index() + shift, b);
407  }
408  inst->setAttributes(attrList);
409  }
410 
411  if (opInst.getNumResults() != 0)
412  moduleTranslation.mapValue(opInst.getResult(0), inst);
413  return success();
414  }
415 
416  if (auto invOp = dyn_cast<LLVM::InvokeOp>(opInst)) {
417  auto operands = moduleTranslation.lookupValues(invOp.getCalleeOperands());
419  convertOperandBundles(invOp.getOpBundleOperands(),
420  invOp.getOpBundleTags(), moduleTranslation);
421  ArrayRef<llvm::Value *> operandsRef(operands);
422  llvm::InvokeInst *result;
423  if (auto attr = opInst.getAttrOfType<FlatSymbolRefAttr>("callee")) {
424  result = builder.CreateInvoke(
425  moduleTranslation.lookupFunction(attr.getValue()),
426  moduleTranslation.lookupBlock(invOp.getSuccessor(0)),
427  moduleTranslation.lookupBlock(invOp.getSuccessor(1)), operandsRef,
428  opBundles);
429  } else {
430  llvm::FunctionType *calleeType = llvm::cast<llvm::FunctionType>(
431  moduleTranslation.convertType(invOp.getCalleeFunctionType()));
432  result = builder.CreateInvoke(
433  calleeType, operandsRef.front(),
434  moduleTranslation.lookupBlock(invOp.getSuccessor(0)),
435  moduleTranslation.lookupBlock(invOp.getSuccessor(1)),
436  operandsRef.drop_front(), opBundles);
437  }
438  result->setCallingConv(convertCConvToLLVM(invOp.getCConv()));
439  if (failed(
440  convertParameterAndResultAttrs(invOp, result, moduleTranslation)))
441  return failure();
442  moduleTranslation.mapBranch(invOp, result);
443  // InvokeOp can only have 0 or 1 result
444  if (invOp->getNumResults() != 0) {
445  moduleTranslation.mapValue(opInst.getResult(0), result);
446  return success();
447  }
448  return success(result->getType()->isVoidTy());
449  }
450 
451  if (auto lpOp = dyn_cast<LLVM::LandingpadOp>(opInst)) {
452  llvm::Type *ty = moduleTranslation.convertType(lpOp.getType());
453  llvm::LandingPadInst *lpi =
454  builder.CreateLandingPad(ty, lpOp.getNumOperands());
455  lpi->setCleanup(lpOp.getCleanup());
456 
457  // Add clauses
458  for (llvm::Value *operand :
459  moduleTranslation.lookupValues(lpOp.getOperands())) {
460  // All operands should be constant - checked by verifier
461  if (auto *constOperand = dyn_cast<llvm::Constant>(operand))
462  lpi->addClause(constOperand);
463  }
464  moduleTranslation.mapValue(lpOp.getResult(), lpi);
465  return success();
466  }
467 
468  // Emit branches. We need to look up the remapped blocks and ignore the
469  // block arguments that were transformed into PHI nodes.
470  if (auto brOp = dyn_cast<LLVM::BrOp>(opInst)) {
471  llvm::BranchInst *branch =
472  builder.CreateBr(moduleTranslation.lookupBlock(brOp.getSuccessor()));
473  moduleTranslation.mapBranch(&opInst, branch);
474  moduleTranslation.setLoopMetadata(&opInst, branch);
475  return success();
476  }
477  if (auto condbrOp = dyn_cast<LLVM::CondBrOp>(opInst)) {
478  llvm::BranchInst *branch = builder.CreateCondBr(
479  moduleTranslation.lookupValue(condbrOp.getOperand(0)),
480  moduleTranslation.lookupBlock(condbrOp.getSuccessor(0)),
481  moduleTranslation.lookupBlock(condbrOp.getSuccessor(1)));
482  moduleTranslation.mapBranch(&opInst, branch);
483  moduleTranslation.setLoopMetadata(&opInst, branch);
484  return success();
485  }
486  if (auto switchOp = dyn_cast<LLVM::SwitchOp>(opInst)) {
487  llvm::SwitchInst *switchInst = builder.CreateSwitch(
488  moduleTranslation.lookupValue(switchOp.getValue()),
489  moduleTranslation.lookupBlock(switchOp.getDefaultDestination()),
490  switchOp.getCaseDestinations().size());
491 
492  // Handle switch with zero cases.
493  if (!switchOp.getCaseValues())
494  return success();
495 
496  auto *ty = llvm::cast<llvm::IntegerType>(
497  moduleTranslation.convertType(switchOp.getValue().getType()));
498  for (auto i :
499  llvm::zip(llvm::cast<DenseIntElementsAttr>(*switchOp.getCaseValues()),
500  switchOp.getCaseDestinations()))
501  switchInst->addCase(
502  llvm::ConstantInt::get(ty, std::get<0>(i).getLimitedValue()),
503  moduleTranslation.lookupBlock(std::get<1>(i)));
504 
505  moduleTranslation.mapBranch(&opInst, switchInst);
506  return success();
507  }
508 
509  // Emit addressof. We need to look up the global value referenced by the
510  // operation and store it in the MLIR-to-LLVM value mapping. This does not
511  // emit any LLVM instruction.
512  if (auto addressOfOp = dyn_cast<LLVM::AddressOfOp>(opInst)) {
513  LLVM::GlobalOp global =
514  addressOfOp.getGlobal(moduleTranslation.symbolTable());
515  LLVM::LLVMFuncOp function =
516  addressOfOp.getFunction(moduleTranslation.symbolTable());
517  LLVM::AliasOp alias = addressOfOp.getAlias(moduleTranslation.symbolTable());
518 
519  // The verifier should not have allowed this.
520  assert((global || function || alias) &&
521  "referencing an undefined global, function, or alias");
522 
523  llvm::Value *llvmValue = nullptr;
524  if (global)
525  llvmValue = moduleTranslation.lookupGlobal(global);
526  else if (alias)
527  llvmValue = moduleTranslation.lookupAlias(alias);
528  else
529  llvmValue = moduleTranslation.lookupFunction(function.getName());
530 
531  moduleTranslation.mapValue(addressOfOp.getResult(), llvmValue);
532  return success();
533  }
534 
535  // Emit dso_local_equivalent. We need to look up the global value referenced
536  // by the operation and store it in the MLIR-to-LLVM value mapping.
537  if (auto dsoLocalEquivalentOp =
538  dyn_cast<LLVM::DSOLocalEquivalentOp>(opInst)) {
539  LLVM::LLVMFuncOp function =
540  dsoLocalEquivalentOp.getFunction(moduleTranslation.symbolTable());
541  LLVM::AliasOp alias =
542  dsoLocalEquivalentOp.getAlias(moduleTranslation.symbolTable());
543 
544  // The verifier should not have allowed this.
545  assert((function || alias) &&
546  "referencing an undefined function, or alias");
547 
548  llvm::Value *llvmValue = nullptr;
549  if (alias)
550  llvmValue = moduleTranslation.lookupAlias(alias);
551  else
552  llvmValue = moduleTranslation.lookupFunction(function.getName());
553 
554  moduleTranslation.mapValue(
555  dsoLocalEquivalentOp.getResult(),
556  llvm::DSOLocalEquivalent::get(cast<llvm::GlobalValue>(llvmValue)));
557  return success();
558  }
559 
560  // Emit blockaddress. We first need to find the LLVM block referenced by this
561  // operation and then create a LLVM block address for it.
562  if (auto blockAddressOp = dyn_cast<LLVM::BlockAddressOp>(opInst)) {
563  // getBlockTagOp() walks a function to search for block labels. Check
564  // whether it's in cache first.
565  BlockAddressAttr blockAddressAttr = blockAddressOp.getBlockAddr();
566  BlockTagOp blockTagOp = moduleTranslation.lookupBlockTag(blockAddressAttr);
567  if (!blockTagOp) {
568  blockTagOp = blockAddressOp.getBlockTagOp();
569  moduleTranslation.mapBlockTag(blockAddressAttr, blockTagOp);
570  }
571 
572  llvm::Value *llvmValue = nullptr;
573  StringRef fnName = blockAddressAttr.getFunction().getValue();
574  if (llvm::BasicBlock *llvmBlock =
575  moduleTranslation.lookupBlock(blockTagOp->getBlock())) {
576  llvm::Function *llvmFn = moduleTranslation.lookupFunction(fnName);
577  llvmValue = llvm::BlockAddress::get(llvmFn, llvmBlock);
578  } else {
579  // The matching LLVM block is not yet emitted, a placeholder is created
580  // in its place. When the LLVM block is emitted later in translation,
581  // the llvmValue is replaced with the actual llvm::BlockAddress.
582  // A GlobalVariable is chosen as placeholder because in general LLVM
583  // constants are uniqued and are not proper for RAUW, since that could
584  // harm unrelated uses of the constant.
585  llvmValue = new llvm::GlobalVariable(
586  *moduleTranslation.getLLVMModule(),
587  llvm::PointerType::getUnqual(moduleTranslation.getLLVMContext()),
588  /*isConstant=*/true, llvm::GlobalValue::LinkageTypes::ExternalLinkage,
589  /*Initializer=*/nullptr,
590  Twine("__mlir_block_address_")
591  .concat(Twine(fnName))
592  .concat(Twine((uint64_t)blockAddressOp.getOperation())));
593  moduleTranslation.mapUnresolvedBlockAddress(blockAddressOp, llvmValue);
594  }
595 
596  moduleTranslation.mapValue(blockAddressOp.getResult(), llvmValue);
597  return success();
598  }
599 
600  // Emit block label. If this label is seen before BlockAddressOp is
601  // translated, go ahead and already map it.
602  if (auto blockTagOp = dyn_cast<LLVM::BlockTagOp>(opInst)) {
603  auto funcOp = blockTagOp->getParentOfType<LLVMFuncOp>();
604  BlockAddressAttr blockAddressAttr = BlockAddressAttr::get(
605  &moduleTranslation.getContext(),
606  FlatSymbolRefAttr::get(&moduleTranslation.getContext(),
607  funcOp.getName()),
608  blockTagOp.getTag());
609  moduleTranslation.mapBlockTag(blockAddressAttr, blockTagOp);
610  return success();
611  }
612 
613  return failure();
614 }
615 
616 namespace {
617 /// Implementation of the dialect interface that converts operations belonging
618 /// to the LLVM dialect to LLVM IR.
619 class LLVMDialectLLVMIRTranslationInterface
621 public:
623 
624  /// Translates the given operation to LLVM IR using the provided IR builder
625  /// and saving the state in `moduleTranslation`.
626  LogicalResult
627  convertOperation(Operation *op, llvm::IRBuilderBase &builder,
628  LLVM::ModuleTranslation &moduleTranslation) const final {
629  return convertOperationImpl(*op, builder, moduleTranslation);
630  }
631 };
632 } // namespace
633 
635  registry.insert<LLVM::LLVMDialect>();
636  registry.addExtension(+[](MLIRContext *ctx, LLVM::LLVMDialect *dialect) {
637  dialect->addInterfaces<LLVMDialectLLVMIRTranslationInterface>();
638  });
639 }
640 
642  DialectRegistry registry;
644  context.appendDialectRegistry(registry);
645 }
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 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 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.
void mapBlockTag(BlockAddressAttr attr, BlockTagOp blockTag)
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.
BlockTagOp lookupBlockTag(BlockAddressAttr attr) const
Finds an MLIR block that corresponds to the given MLIR call operation.
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.
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 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:66
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:2317
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...