1 //===- ModuleTranslation.cpp - MLIR to LLVM conversion --------------------===//
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 the translation between an MLIR LLVM dialect module and
10 // the corresponding LLVMIR module. It only handles core LLVM IR operations.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "mlir/Target/LLVMIR/ModuleTranslation.h"
15 
16 #include "DebugTranslation.h"
17 #include "mlir/Dialect/LLVMIR/LLVMDialect.h"
18 #include "mlir/Dialect/OpenMP/OpenMPDialect.h"
19 #include "mlir/IR/Attributes.h"
20 #include "mlir/IR/BuiltinOps.h"
21 #include "mlir/IR/BuiltinTypes.h"
22 #include "mlir/IR/RegionGraphTraits.h"
23 #include "mlir/Support/LLVM.h"
24 #include "mlir/Target/LLVMIR/TypeTranslation.h"
25 #include "llvm/ADT/TypeSwitch.h"
26 
27 #include "llvm/ADT/PostOrderIterator.h"
28 #include "llvm/ADT/SetVector.h"
29 #include "llvm/Frontend/OpenMP/OMPIRBuilder.h"
30 #include "llvm/IR/BasicBlock.h"
31 #include "llvm/IR/CFG.h"
32 #include "llvm/IR/Constants.h"
33 #include "llvm/IR/DerivedTypes.h"
34 #include "llvm/IR/IRBuilder.h"
35 #include "llvm/IR/InlineAsm.h"
36 #include "llvm/IR/LLVMContext.h"
37 #include "llvm/IR/MDBuilder.h"
38 #include "llvm/IR/Module.h"
39 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
40 #include "llvm/Transforms/Utils/Cloning.h"
41 
42 using namespace mlir;
43 using namespace mlir::LLVM;
44 using namespace mlir::LLVM::detail;
45 
46 #include "mlir/Dialect/LLVMIR/LLVMConversionEnumsToLLVM.inc"
47 
48 /// Builds a constant of a sequential LLVM type `type`, potentially containing
49 /// other sequential types recursively, from the individual constant values
50 /// provided in `constants`. `shape` contains the number of elements in nested
51 /// sequential types. Reports errors at `loc` and returns nullptr on error.
52 static llvm::Constant *
buildSequentialConstant(ArrayRef<llvm::Constant * > & constants,ArrayRef<int64_t> shape,llvm::Type * type,Location loc)53 buildSequentialConstant(ArrayRef<llvm::Constant *> &constants,
54                         ArrayRef<int64_t> shape, llvm::Type *type,
55                         Location loc) {
56   if (shape.empty()) {
57     llvm::Constant *result = constants.front();
58     constants = constants.drop_front();
59     return result;
60   }
61 
62   llvm::Type *elementType;
63   if (auto *arrayTy = dyn_cast<llvm::ArrayType>(type)) {
64     elementType = arrayTy->getElementType();
65   } else if (auto *vectorTy = dyn_cast<llvm::VectorType>(type)) {
66     elementType = vectorTy->getElementType();
67   } else {
68     emitError(loc) << "expected sequential LLVM types wrapping a scalar";
69     return nullptr;
70   }
71 
72   SmallVector<llvm::Constant *, 8> nested;
73   nested.reserve(shape.front());
74   for (int64_t i = 0; i < shape.front(); ++i) {
75     nested.push_back(buildSequentialConstant(constants, shape.drop_front(),
76                                              elementType, loc));
77     if (!nested.back())
78       return nullptr;
79   }
80 
81   if (shape.size() == 1 && type->isVectorTy())
82     return llvm::ConstantVector::get(nested);
83   return llvm::ConstantArray::get(
84       llvm::ArrayType::get(elementType, shape.front()), nested);
85 }
86 
87 /// Returns the first non-sequential type nested in sequential types.
getInnermostElementType(llvm::Type * type)88 static llvm::Type *getInnermostElementType(llvm::Type *type) {
89   do {
90     if (auto *arrayTy = dyn_cast<llvm::ArrayType>(type)) {
91       type = arrayTy->getElementType();
92     } else if (auto *vectorTy = dyn_cast<llvm::VectorType>(type)) {
93       type = vectorTy->getElementType();
94     } else {
95       return type;
96     }
97   } while (1);
98 }
99 
100 /// Create an LLVM IR constant of `llvmType` from the MLIR attribute `attr`.
101 /// This currently supports integer, floating point, splat and dense element
102 /// attributes and combinations thereof.  In case of error, report it to `loc`
103 /// and return nullptr.
getLLVMConstant(llvm::Type * llvmType,Attribute attr,Location loc)104 llvm::Constant *ModuleTranslation::getLLVMConstant(llvm::Type *llvmType,
105                                                    Attribute attr,
106                                                    Location loc) {
107   if (!attr)
108     return llvm::UndefValue::get(llvmType);
109   if (llvmType->isStructTy()) {
110     emitError(loc, "struct types are not supported in constants");
111     return nullptr;
112   }
113   // For integer types, we allow a mismatch in sizes as the index type in
114   // MLIR might have a different size than the index type in the LLVM module.
115   if (auto intAttr = attr.dyn_cast<IntegerAttr>())
116     return llvm::ConstantInt::get(
117         llvmType,
118         intAttr.getValue().sextOrTrunc(llvmType->getIntegerBitWidth()));
119   if (auto floatAttr = attr.dyn_cast<FloatAttr>())
120     return llvm::ConstantFP::get(llvmType, floatAttr.getValue());
121   if (auto funcAttr = attr.dyn_cast<FlatSymbolRefAttr>())
122     return llvm::ConstantExpr::getBitCast(
123         functionMapping.lookup(funcAttr.getValue()), llvmType);
124   if (auto splatAttr = attr.dyn_cast<SplatElementsAttr>()) {
125     llvm::Type *elementType;
126     uint64_t numElements;
127     if (auto *arrayTy = dyn_cast<llvm::ArrayType>(llvmType)) {
128       elementType = arrayTy->getElementType();
129       numElements = arrayTy->getNumElements();
130     } else {
131       auto *vectorTy = cast<llvm::FixedVectorType>(llvmType);
132       elementType = vectorTy->getElementType();
133       numElements = vectorTy->getNumElements();
134     }
135     // Splat value is a scalar. Extract it only if the element type is not
136     // another sequence type. The recursion terminates because each step removes
137     // one outer sequential type.
138     bool elementTypeSequential =
139         isa<llvm::ArrayType, llvm::VectorType>(elementType);
140     llvm::Constant *child = getLLVMConstant(
141         elementType,
142         elementTypeSequential ? splatAttr : splatAttr.getSplatValue(), loc);
143     if (!child)
144       return nullptr;
145     if (llvmType->isVectorTy())
146       return llvm::ConstantVector::getSplat(
147           llvm::ElementCount::get(numElements, /*Scalable=*/false), child);
148     if (llvmType->isArrayTy()) {
149       auto *arrayType = llvm::ArrayType::get(elementType, numElements);
150       SmallVector<llvm::Constant *, 8> constants(numElements, child);
151       return llvm::ConstantArray::get(arrayType, constants);
152     }
153   }
154 
155   if (auto elementsAttr = attr.dyn_cast<ElementsAttr>()) {
156     assert(elementsAttr.getType().hasStaticShape());
157     assert(elementsAttr.getNumElements() != 0 &&
158            "unexpected empty elements attribute");
159     assert(!elementsAttr.getType().getShape().empty() &&
160            "unexpected empty elements attribute shape");
161 
162     SmallVector<llvm::Constant *, 8> constants;
163     constants.reserve(elementsAttr.getNumElements());
164     llvm::Type *innermostType = getInnermostElementType(llvmType);
165     for (auto n : elementsAttr.getValues<Attribute>()) {
166       constants.push_back(getLLVMConstant(innermostType, n, loc));
167       if (!constants.back())
168         return nullptr;
169     }
170     ArrayRef<llvm::Constant *> constantsRef = constants;
171     llvm::Constant *result = buildSequentialConstant(
172         constantsRef, elementsAttr.getType().getShape(), llvmType, loc);
173     assert(constantsRef.empty() && "did not consume all elemental constants");
174     return result;
175   }
176 
177   if (auto stringAttr = attr.dyn_cast<StringAttr>()) {
178     return llvm::ConstantDataArray::get(
179         llvmModule->getContext(), ArrayRef<char>{stringAttr.getValue().data(),
180                                                  stringAttr.getValue().size()});
181   }
182   emitError(loc, "unsupported constant value");
183   return nullptr;
184 }
185 
186 /// Convert MLIR integer comparison predicate to LLVM IR comparison predicate.
getLLVMCmpPredicate(ICmpPredicate p)187 static llvm::CmpInst::Predicate getLLVMCmpPredicate(ICmpPredicate p) {
188   switch (p) {
189   case LLVM::ICmpPredicate::eq:
190     return llvm::CmpInst::Predicate::ICMP_EQ;
191   case LLVM::ICmpPredicate::ne:
192     return llvm::CmpInst::Predicate::ICMP_NE;
193   case LLVM::ICmpPredicate::slt:
194     return llvm::CmpInst::Predicate::ICMP_SLT;
195   case LLVM::ICmpPredicate::sle:
196     return llvm::CmpInst::Predicate::ICMP_SLE;
197   case LLVM::ICmpPredicate::sgt:
198     return llvm::CmpInst::Predicate::ICMP_SGT;
199   case LLVM::ICmpPredicate::sge:
200     return llvm::CmpInst::Predicate::ICMP_SGE;
201   case LLVM::ICmpPredicate::ult:
202     return llvm::CmpInst::Predicate::ICMP_ULT;
203   case LLVM::ICmpPredicate::ule:
204     return llvm::CmpInst::Predicate::ICMP_ULE;
205   case LLVM::ICmpPredicate::ugt:
206     return llvm::CmpInst::Predicate::ICMP_UGT;
207   case LLVM::ICmpPredicate::uge:
208     return llvm::CmpInst::Predicate::ICMP_UGE;
209   }
210   llvm_unreachable("incorrect comparison predicate");
211 }
212 
getLLVMCmpPredicate(FCmpPredicate p)213 static llvm::CmpInst::Predicate getLLVMCmpPredicate(FCmpPredicate p) {
214   switch (p) {
215   case LLVM::FCmpPredicate::_false:
216     return llvm::CmpInst::Predicate::FCMP_FALSE;
217   case LLVM::FCmpPredicate::oeq:
218     return llvm::CmpInst::Predicate::FCMP_OEQ;
219   case LLVM::FCmpPredicate::ogt:
220     return llvm::CmpInst::Predicate::FCMP_OGT;
221   case LLVM::FCmpPredicate::oge:
222     return llvm::CmpInst::Predicate::FCMP_OGE;
223   case LLVM::FCmpPredicate::olt:
224     return llvm::CmpInst::Predicate::FCMP_OLT;
225   case LLVM::FCmpPredicate::ole:
226     return llvm::CmpInst::Predicate::FCMP_OLE;
227   case LLVM::FCmpPredicate::one:
228     return llvm::CmpInst::Predicate::FCMP_ONE;
229   case LLVM::FCmpPredicate::ord:
230     return llvm::CmpInst::Predicate::FCMP_ORD;
231   case LLVM::FCmpPredicate::ueq:
232     return llvm::CmpInst::Predicate::FCMP_UEQ;
233   case LLVM::FCmpPredicate::ugt:
234     return llvm::CmpInst::Predicate::FCMP_UGT;
235   case LLVM::FCmpPredicate::uge:
236     return llvm::CmpInst::Predicate::FCMP_UGE;
237   case LLVM::FCmpPredicate::ult:
238     return llvm::CmpInst::Predicate::FCMP_ULT;
239   case LLVM::FCmpPredicate::ule:
240     return llvm::CmpInst::Predicate::FCMP_ULE;
241   case LLVM::FCmpPredicate::une:
242     return llvm::CmpInst::Predicate::FCMP_UNE;
243   case LLVM::FCmpPredicate::uno:
244     return llvm::CmpInst::Predicate::FCMP_UNO;
245   case LLVM::FCmpPredicate::_true:
246     return llvm::CmpInst::Predicate::FCMP_TRUE;
247   }
248   llvm_unreachable("incorrect comparison predicate");
249 }
250 
getLLVMAtomicBinOp(AtomicBinOp op)251 static llvm::AtomicRMWInst::BinOp getLLVMAtomicBinOp(AtomicBinOp op) {
252   switch (op) {
253   case LLVM::AtomicBinOp::xchg:
254     return llvm::AtomicRMWInst::BinOp::Xchg;
255   case LLVM::AtomicBinOp::add:
256     return llvm::AtomicRMWInst::BinOp::Add;
257   case LLVM::AtomicBinOp::sub:
258     return llvm::AtomicRMWInst::BinOp::Sub;
259   case LLVM::AtomicBinOp::_and:
260     return llvm::AtomicRMWInst::BinOp::And;
261   case LLVM::AtomicBinOp::nand:
262     return llvm::AtomicRMWInst::BinOp::Nand;
263   case LLVM::AtomicBinOp::_or:
264     return llvm::AtomicRMWInst::BinOp::Or;
265   case LLVM::AtomicBinOp::_xor:
266     return llvm::AtomicRMWInst::BinOp::Xor;
267   case LLVM::AtomicBinOp::max:
268     return llvm::AtomicRMWInst::BinOp::Max;
269   case LLVM::AtomicBinOp::min:
270     return llvm::AtomicRMWInst::BinOp::Min;
271   case LLVM::AtomicBinOp::umax:
272     return llvm::AtomicRMWInst::BinOp::UMax;
273   case LLVM::AtomicBinOp::umin:
274     return llvm::AtomicRMWInst::BinOp::UMin;
275   case LLVM::AtomicBinOp::fadd:
276     return llvm::AtomicRMWInst::BinOp::FAdd;
277   case LLVM::AtomicBinOp::fsub:
278     return llvm::AtomicRMWInst::BinOp::FSub;
279   }
280   llvm_unreachable("incorrect atomic binary operator");
281 }
282 
getLLVMAtomicOrdering(AtomicOrdering ordering)283 static llvm::AtomicOrdering getLLVMAtomicOrdering(AtomicOrdering ordering) {
284   switch (ordering) {
285   case LLVM::AtomicOrdering::not_atomic:
286     return llvm::AtomicOrdering::NotAtomic;
287   case LLVM::AtomicOrdering::unordered:
288     return llvm::AtomicOrdering::Unordered;
289   case LLVM::AtomicOrdering::monotonic:
290     return llvm::AtomicOrdering::Monotonic;
291   case LLVM::AtomicOrdering::acquire:
292     return llvm::AtomicOrdering::Acquire;
293   case LLVM::AtomicOrdering::release:
294     return llvm::AtomicOrdering::Release;
295   case LLVM::AtomicOrdering::acq_rel:
296     return llvm::AtomicOrdering::AcquireRelease;
297   case LLVM::AtomicOrdering::seq_cst:
298     return llvm::AtomicOrdering::SequentiallyConsistent;
299   }
300   llvm_unreachable("incorrect atomic ordering");
301 }
302 
ModuleTranslation(Operation * module,std::unique_ptr<llvm::Module> llvmModule)303 ModuleTranslation::ModuleTranslation(Operation *module,
304                                      std::unique_ptr<llvm::Module> llvmModule)
305     : mlirModule(module), llvmModule(std::move(llvmModule)),
306       debugTranslation(
307           std::make_unique<DebugTranslation>(module, *this->llvmModule)),
308       ompDialect(module->getContext()->getLoadedDialect("omp")),
309       typeTranslator(this->llvmModule->getContext()) {
310   assert(satisfiesLLVMModule(mlirModule) &&
311          "mlirModule should honor LLVM's module semantics.");
312 }
~ModuleTranslation()313 ModuleTranslation::~ModuleTranslation() {
314   if (ompBuilder)
315     ompBuilder->finalize();
316 }
317 
318 /// Get the SSA value passed to the current block from the terminator operation
319 /// of its predecessor.
getPHISourceValue(Block * current,Block * pred,unsigned numArguments,unsigned index)320 static Value getPHISourceValue(Block *current, Block *pred,
321                                unsigned numArguments, unsigned index) {
322   Operation &terminator = *pred->getTerminator();
323   if (isa<LLVM::BrOp>(terminator))
324     return terminator.getOperand(index);
325 
326   // For conditional branches, we need to check if the current block is reached
327   // through the "true" or the "false" branch and take the relevant operands.
328   auto condBranchOp = dyn_cast<LLVM::CondBrOp>(terminator);
329   assert(condBranchOp &&
330          "only branch operations can be terminators of a block that "
331          "has successors");
332   assert((condBranchOp.getSuccessor(0) != condBranchOp.getSuccessor(1)) &&
333          "successors with arguments in LLVM conditional branches must be "
334          "different blocks");
335 
336   return condBranchOp.getSuccessor(0) == current
337              ? condBranchOp.trueDestOperands()[index]
338              : condBranchOp.falseDestOperands()[index];
339 }
340 
341 /// Connect the PHI nodes to the results of preceding blocks.
342 template <typename T>
343 static void
connectPHINodes(T & func,const DenseMap<Value,llvm::Value * > & valueMapping,const DenseMap<Block *,llvm::BasicBlock * > & blockMapping)344 connectPHINodes(T &func, const DenseMap<Value, llvm::Value *> &valueMapping,
345                 const DenseMap<Block *, llvm::BasicBlock *> &blockMapping) {
346   // Skip the first block, it cannot be branched to and its arguments correspond
347   // to the arguments of the LLVM function.
348   for (auto it = std::next(func.begin()), eit = func.end(); it != eit; ++it) {
349     Block *bb = &*it;
350     llvm::BasicBlock *llvmBB = blockMapping.lookup(bb);
351     auto phis = llvmBB->phis();
352     auto numArguments = bb->getNumArguments();
353     assert(numArguments == std::distance(phis.begin(), phis.end()));
354     for (auto &numberedPhiNode : llvm::enumerate(phis)) {
355       auto &phiNode = numberedPhiNode.value();
356       unsigned index = numberedPhiNode.index();
357       for (auto *pred : bb->getPredecessors()) {
358         phiNode.addIncoming(valueMapping.lookup(getPHISourceValue(
359                                 bb, pred, numArguments, index)),
360                             blockMapping.lookup(pred));
361       }
362     }
363   }
364 }
365 
366 /// Sort function blocks topologically.
367 template <typename T>
topologicalSort(T & f)368 static llvm::SetVector<Block *> topologicalSort(T &f) {
369   // For each block that has not been visited yet (i.e. that has no
370   // predecessors), add it to the list as well as its successors.
371   llvm::SetVector<Block *> blocks;
372   for (Block &b : f) {
373     if (blocks.count(&b) == 0) {
374       llvm::ReversePostOrderTraversal<Block *> traversal(&b);
375       blocks.insert(traversal.begin(), traversal.end());
376     }
377   }
378   assert(blocks.size() == f.getBlocks().size() && "some blocks are not sorted");
379 
380   return blocks;
381 }
382 
383 /// Convert the OpenMP parallel Operation to LLVM IR.
384 LogicalResult
convertOmpParallel(Operation & opInst,llvm::IRBuilder<> & builder)385 ModuleTranslation::convertOmpParallel(Operation &opInst,
386                                       llvm::IRBuilder<> &builder) {
387   using InsertPointTy = llvm::OpenMPIRBuilder::InsertPointTy;
388   // TODO: support error propagation in OpenMPIRBuilder and use it instead of
389   // relying on captured variables.
390   LogicalResult bodyGenStatus = success();
391 
392   auto bodyGenCB = [&](InsertPointTy allocaIP, InsertPointTy codeGenIP,
393                        llvm::BasicBlock &continuationIP) {
394     llvm::LLVMContext &llvmContext = llvmModule->getContext();
395 
396     llvm::BasicBlock *codeGenIPBB = codeGenIP.getBlock();
397     llvm::Instruction *codeGenIPBBTI = codeGenIPBB->getTerminator();
398     ompContinuationIPStack.push_back(&continuationIP);
399 
400     // ParallelOp has only `1` region associated with it.
401     auto &region = cast<omp::ParallelOp>(opInst).getRegion();
402     for (auto &bb : region) {
403       auto *llvmBB = llvm::BasicBlock::Create(
404           llvmContext, "omp.par.region", codeGenIP.getBlock()->getParent());
405       blockMapping[&bb] = llvmBB;
406     }
407 
408     convertOmpOpRegions(region, valueMapping, blockMapping, codeGenIPBBTI,
409                         continuationIP, builder, bodyGenStatus);
410     ompContinuationIPStack.pop_back();
411 
412   };
413 
414   // TODO: Perform appropriate actions according to the data-sharing
415   // attribute (shared, private, firstprivate, ...) of variables.
416   // Currently defaults to shared.
417   auto privCB = [&](InsertPointTy allocaIP, InsertPointTy codeGenIP,
418                     llvm::Value &, llvm::Value &vPtr,
419                     llvm::Value *&replacementValue) -> InsertPointTy {
420     replacementValue = &vPtr;
421 
422     return codeGenIP;
423   };
424 
425   // TODO: Perform finalization actions for variables. This has to be
426   // called for variables which have destructors/finalizers.
427   auto finiCB = [&](InsertPointTy codeGenIP) {};
428 
429   llvm::Value *ifCond = nullptr;
430   if (auto ifExprVar = cast<omp::ParallelOp>(opInst).if_expr_var())
431     ifCond = valueMapping.lookup(ifExprVar);
432   llvm::Value *numThreads = nullptr;
433   if (auto numThreadsVar = cast<omp::ParallelOp>(opInst).num_threads_var())
434     numThreads = valueMapping.lookup(numThreadsVar);
435   llvm::omp::ProcBindKind pbKind = llvm::omp::OMP_PROC_BIND_default;
436   if (auto bind = cast<omp::ParallelOp>(opInst).proc_bind_val())
437     pbKind = llvm::omp::getProcBindKind(bind.getValue());
438   // TODO: Is the Parallel construct cancellable?
439   bool isCancellable = false;
440   // TODO: Determine the actual alloca insertion point, e.g., the function
441   // entry or the alloca insertion point as provided by the body callback
442   // above.
443   llvm::OpenMPIRBuilder::InsertPointTy allocaIP(builder.saveIP());
444   if (failed(bodyGenStatus))
445     return failure();
446   builder.restoreIP(
447       ompBuilder->createParallel(builder, allocaIP, bodyGenCB, privCB, finiCB,
448                                  ifCond, numThreads, pbKind, isCancellable));
449   return success();
450 }
451 
convertOmpOpRegions(Region & region,DenseMap<Value,llvm::Value * > & valueMapping,DenseMap<Block *,llvm::BasicBlock * > & blockMapping,llvm::Instruction * codeGenIPBBTI,llvm::BasicBlock & continuationIP,llvm::IRBuilder<> & builder,LogicalResult & bodyGenStatus)452 void ModuleTranslation::convertOmpOpRegions(
453     Region &region, DenseMap<Value, llvm::Value *> &valueMapping,
454     DenseMap<Block *, llvm::BasicBlock *> &blockMapping,
455     llvm::Instruction *codeGenIPBBTI, llvm::BasicBlock &continuationIP,
456     llvm::IRBuilder<> &builder, LogicalResult &bodyGenStatus) {
457   // Convert blocks one by one in topological order to ensure
458   // defs are converted before uses.
459   llvm::SetVector<Block *> blocks = topologicalSort(region);
460   for (auto indexedBB : llvm::enumerate(blocks)) {
461     Block *bb = indexedBB.value();
462     llvm::BasicBlock *curLLVMBB = blockMapping[bb];
463     if (bb->isEntryBlock()) {
464       assert(codeGenIPBBTI->getNumSuccessors() == 1 &&
465              "OpenMPIRBuilder provided entry block has multiple successors");
466       assert(codeGenIPBBTI->getSuccessor(0) == &continuationIP &&
467              "ContinuationIP is not the successor of OpenMPIRBuilder "
468              "provided entry block");
469       codeGenIPBBTI->setSuccessor(0, curLLVMBB);
470     }
471 
472     if (failed(convertBlock(*bb, /*ignoreArguments=*/indexedBB.index() == 0))) {
473       bodyGenStatus = failure();
474       return;
475     }
476   }
477   // Finally, after all blocks have been traversed and values mapped,
478   // connect the PHI nodes to the results of preceding blocks.
479   connectPHINodes(region, valueMapping, blockMapping);
480 }
481 
convertOmpMaster(Operation & opInst,llvm::IRBuilder<> & builder)482 LogicalResult ModuleTranslation::convertOmpMaster(Operation &opInst,
483                                                   llvm::IRBuilder<> &builder) {
484   using InsertPointTy = llvm::OpenMPIRBuilder::InsertPointTy;
485   // TODO: support error propagation in OpenMPIRBuilder and use it instead of
486   // relying on captured variables.
487   LogicalResult bodyGenStatus = success();
488 
489   auto bodyGenCB = [&](InsertPointTy allocaIP, InsertPointTy codeGenIP,
490                        llvm::BasicBlock &continuationIP) {
491     llvm::LLVMContext &llvmContext = llvmModule->getContext();
492 
493     llvm::BasicBlock *codeGenIPBB = codeGenIP.getBlock();
494     llvm::Instruction *codeGenIPBBTI = codeGenIPBB->getTerminator();
495     ompContinuationIPStack.push_back(&continuationIP);
496 
497     // MasterOp has only `1` region associated with it.
498     auto &region = cast<omp::MasterOp>(opInst).getRegion();
499     for (auto &bb : region) {
500       auto *llvmBB = llvm::BasicBlock::Create(
501           llvmContext, "omp.master.region", codeGenIP.getBlock()->getParent());
502       blockMapping[&bb] = llvmBB;
503     }
504     convertOmpOpRegions(region, valueMapping, blockMapping, codeGenIPBBTI,
505                         continuationIP, builder, bodyGenStatus);
506     ompContinuationIPStack.pop_back();
507   };
508 
509   // TODO: Perform finalization actions for variables. This has to be
510   // called for variables which have destructors/finalizers.
511   auto finiCB = [&](InsertPointTy codeGenIP) {};
512 
513   builder.restoreIP(ompBuilder->createMaster(builder, bodyGenCB, finiCB));
514   return success();
515 }
516 
517 /// Given an OpenMP MLIR operation, create the corresponding LLVM IR
518 /// (including OpenMP runtime calls).
519 LogicalResult
convertOmpOperation(Operation & opInst,llvm::IRBuilder<> & builder)520 ModuleTranslation::convertOmpOperation(Operation &opInst,
521                                        llvm::IRBuilder<> &builder) {
522   if (!ompBuilder) {
523     ompBuilder = std::make_unique<llvm::OpenMPIRBuilder>(*llvmModule);
524     ompBuilder->initialize();
525   }
526   return llvm::TypeSwitch<Operation *, LogicalResult>(&opInst)
527       .Case([&](omp::BarrierOp) {
528         ompBuilder->createBarrier(builder.saveIP(), llvm::omp::OMPD_barrier);
529         return success();
530       })
531       .Case([&](omp::TaskwaitOp) {
532         ompBuilder->createTaskwait(builder.saveIP());
533         return success();
534       })
535       .Case([&](omp::TaskyieldOp) {
536         ompBuilder->createTaskyield(builder.saveIP());
537         return success();
538       })
539       .Case([&](omp::FlushOp) {
540         // No support in Openmp runtime function (__kmpc_flush) to accept
541         // the argument list.
542         // OpenMP standard states the following:
543         //  "An implementation may implement a flush with a list by ignoring
544         //   the list, and treating it the same as a flush without a list."
545         //
546         // The argument list is discarded so that, flush with a list is treated
547         // same as a flush without a list.
548         ompBuilder->createFlush(builder.saveIP());
549         return success();
550       })
551       .Case([&](omp::TerminatorOp) {
552         builder.CreateBr(ompContinuationIPStack.back());
553         return success();
554       })
555       .Case(
556           [&](omp::ParallelOp) { return convertOmpParallel(opInst, builder); })
557       .Case([&](omp::MasterOp) { return convertOmpMaster(opInst, builder); })
558       .Default([&](Operation *inst) {
559         return inst->emitError("unsupported OpenMP operation: ")
560                << inst->getName();
561       });
562 }
563 
564 /// Given a single MLIR operation, create the corresponding LLVM IR operation
565 /// using the `builder`.  LLVM IR Builder does not have a generic interface so
566 /// this has to be a long chain of `if`s calling different functions with a
567 /// different number of arguments.
convertOperation(Operation & opInst,llvm::IRBuilder<> & builder)568 LogicalResult ModuleTranslation::convertOperation(Operation &opInst,
569                                                   llvm::IRBuilder<> &builder) {
570   auto extractPosition = [](ArrayAttr attr) {
571     SmallVector<unsigned, 4> position;
572     position.reserve(attr.size());
573     for (Attribute v : attr)
574       position.push_back(v.cast<IntegerAttr>().getValue().getZExtValue());
575     return position;
576   };
577 
578 #include "mlir/Dialect/LLVMIR/LLVMConversions.inc"
579 
580   // Emit function calls.  If the "callee" attribute is present, this is a
581   // direct function call and we also need to look up the remapped function
582   // itself.  Otherwise, this is an indirect call and the callee is the first
583   // operand, look it up as a normal value.  Return the llvm::Value representing
584   // the function result, which may be of llvm::VoidTy type.
585   auto convertCall = [this, &builder](Operation &op) -> llvm::Value * {
586     auto operands = lookupValues(op.getOperands());
587     ArrayRef<llvm::Value *> operandsRef(operands);
588     if (auto attr = op.getAttrOfType<FlatSymbolRefAttr>("callee")) {
589       return builder.CreateCall(functionMapping.lookup(attr.getValue()),
590                                 operandsRef);
591     } else {
592       auto *calleePtrType =
593           cast<llvm::PointerType>(operandsRef.front()->getType());
594       auto *calleeType =
595           cast<llvm::FunctionType>(calleePtrType->getElementType());
596       return builder.CreateCall(calleeType, operandsRef.front(),
597                                 operandsRef.drop_front());
598     }
599   };
600 
601   // Emit calls.  If the called function has a result, remap the corresponding
602   // value.  Note that LLVM IR dialect CallOp has either 0 or 1 result.
603   if (isa<LLVM::CallOp>(opInst)) {
604     llvm::Value *result = convertCall(opInst);
605     if (opInst.getNumResults() != 0) {
606       valueMapping[opInst.getResult(0)] = result;
607       return success();
608     }
609     // Check that LLVM call returns void for 0-result functions.
610     return success(result->getType()->isVoidTy());
611   }
612 
613   if (auto inlineAsmOp = dyn_cast<LLVM::InlineAsmOp>(opInst)) {
614     // TODO: refactor function type creation which usually occurs in std-LLVM
615     // conversion.
616     SmallVector<LLVM::LLVMType, 8> operandTypes;
617     operandTypes.reserve(inlineAsmOp.operands().size());
618     for (auto t : inlineAsmOp.operands().getTypes())
619       operandTypes.push_back(t.cast<LLVM::LLVMType>());
620 
621     LLVM::LLVMType resultType;
622     if (inlineAsmOp.getNumResults() == 0) {
623       resultType = LLVM::LLVMType::getVoidTy(mlirModule->getContext());
624     } else {
625       assert(inlineAsmOp.getNumResults() == 1);
626       resultType = inlineAsmOp.getResultTypes()[0].cast<LLVM::LLVMType>();
627     }
628     auto ft = LLVM::LLVMType::getFunctionTy(resultType, operandTypes,
629                                             /*isVarArg=*/false);
630     llvm::InlineAsm *inlineAsmInst =
631         inlineAsmOp.asm_dialect().hasValue()
632             ? llvm::InlineAsm::get(
633                   static_cast<llvm::FunctionType *>(convertType(ft)),
634                   inlineAsmOp.asm_string(), inlineAsmOp.constraints(),
635                   inlineAsmOp.has_side_effects(), inlineAsmOp.is_align_stack(),
636                   convertAsmDialectToLLVM(*inlineAsmOp.asm_dialect()))
637             : llvm::InlineAsm::get(
638                   static_cast<llvm::FunctionType *>(convertType(ft)),
639                   inlineAsmOp.asm_string(), inlineAsmOp.constraints(),
640                   inlineAsmOp.has_side_effects(), inlineAsmOp.is_align_stack());
641     llvm::Value *result =
642         builder.CreateCall(inlineAsmInst, lookupValues(inlineAsmOp.operands()));
643     if (opInst.getNumResults() != 0)
644       valueMapping[opInst.getResult(0)] = result;
645     return success();
646   }
647 
648   if (auto invOp = dyn_cast<LLVM::InvokeOp>(opInst)) {
649     auto operands = lookupValues(opInst.getOperands());
650     ArrayRef<llvm::Value *> operandsRef(operands);
651     if (auto attr = opInst.getAttrOfType<FlatSymbolRefAttr>("callee")) {
652       builder.CreateInvoke(functionMapping.lookup(attr.getValue()),
653                            blockMapping[invOp.getSuccessor(0)],
654                            blockMapping[invOp.getSuccessor(1)], operandsRef);
655     } else {
656       auto *calleePtrType =
657           cast<llvm::PointerType>(operandsRef.front()->getType());
658       auto *calleeType =
659           cast<llvm::FunctionType>(calleePtrType->getElementType());
660       builder.CreateInvoke(
661           calleeType, operandsRef.front(), blockMapping[invOp.getSuccessor(0)],
662           blockMapping[invOp.getSuccessor(1)], operandsRef.drop_front());
663     }
664     return success();
665   }
666 
667   if (auto lpOp = dyn_cast<LLVM::LandingpadOp>(opInst)) {
668     llvm::Type *ty = convertType(lpOp.getType().cast<LLVMType>());
669     llvm::LandingPadInst *lpi =
670         builder.CreateLandingPad(ty, lpOp.getNumOperands());
671 
672     // Add clauses
673     for (auto operand : lookupValues(lpOp.getOperands())) {
674       // All operands should be constant - checked by verifier
675       if (auto constOperand = dyn_cast<llvm::Constant>(operand))
676         lpi->addClause(constOperand);
677     }
678     valueMapping[lpOp.getResult()] = lpi;
679     return success();
680   }
681 
682   // Emit branches.  We need to look up the remapped blocks and ignore the block
683   // arguments that were transformed into PHI nodes.
684   if (auto brOp = dyn_cast<LLVM::BrOp>(opInst)) {
685     builder.CreateBr(blockMapping[brOp.getSuccessor()]);
686     return success();
687   }
688   if (auto condbrOp = dyn_cast<LLVM::CondBrOp>(opInst)) {
689     auto weights = condbrOp.branch_weights();
690     llvm::MDNode *branchWeights = nullptr;
691     if (weights) {
692       // Map weight attributes to LLVM metadata.
693       auto trueWeight =
694           weights.getValue().getValue(0).cast<IntegerAttr>().getInt();
695       auto falseWeight =
696           weights.getValue().getValue(1).cast<IntegerAttr>().getInt();
697       branchWeights =
698           llvm::MDBuilder(llvmModule->getContext())
699               .createBranchWeights(static_cast<uint32_t>(trueWeight),
700                                    static_cast<uint32_t>(falseWeight));
701     }
702     builder.CreateCondBr(valueMapping.lookup(condbrOp.getOperand(0)),
703                          blockMapping[condbrOp.getSuccessor(0)],
704                          blockMapping[condbrOp.getSuccessor(1)], branchWeights);
705     return success();
706   }
707 
708   // Emit addressof.  We need to look up the global value referenced by the
709   // operation and store it in the MLIR-to-LLVM value mapping.  This does not
710   // emit any LLVM instruction.
711   if (auto addressOfOp = dyn_cast<LLVM::AddressOfOp>(opInst)) {
712     LLVM::GlobalOp global = addressOfOp.getGlobal();
713     LLVM::LLVMFuncOp function = addressOfOp.getFunction();
714 
715     // The verifier should not have allowed this.
716     assert((global || function) &&
717            "referencing an undefined global or function");
718 
719     valueMapping[addressOfOp.getResult()] =
720         global ? globalsMapping.lookup(global)
721                : functionMapping.lookup(function.getName());
722     return success();
723   }
724 
725   if (ompDialect && opInst.getDialect() == ompDialect)
726     return convertOmpOperation(opInst, builder);
727 
728   return opInst.emitError("unsupported or non-LLVM operation: ")
729          << opInst.getName();
730 }
731 
732 /// Convert block to LLVM IR.  Unless `ignoreArguments` is set, emit PHI nodes
733 /// to define values corresponding to the MLIR block arguments.  These nodes
734 /// are not connected to the source basic blocks, which may not exist yet.
convertBlock(Block & bb,bool ignoreArguments)735 LogicalResult ModuleTranslation::convertBlock(Block &bb, bool ignoreArguments) {
736   llvm::IRBuilder<> builder(blockMapping[&bb]);
737   auto *subprogram = builder.GetInsertBlock()->getParent()->getSubprogram();
738 
739   // Before traversing operations, make block arguments available through
740   // value remapping and PHI nodes, but do not add incoming edges for the PHI
741   // nodes just yet: those values may be defined by this or following blocks.
742   // This step is omitted if "ignoreArguments" is set.  The arguments of the
743   // first block have been already made available through the remapping of
744   // LLVM function arguments.
745   if (!ignoreArguments) {
746     auto predecessors = bb.getPredecessors();
747     unsigned numPredecessors =
748         std::distance(predecessors.begin(), predecessors.end());
749     for (auto arg : bb.getArguments()) {
750       auto wrappedType = arg.getType().dyn_cast<LLVM::LLVMType>();
751       if (!wrappedType)
752         return emitError(bb.front().getLoc(),
753                          "block argument does not have an LLVM type");
754       llvm::Type *type = convertType(wrappedType);
755       llvm::PHINode *phi = builder.CreatePHI(type, numPredecessors);
756       valueMapping[arg] = phi;
757     }
758   }
759 
760   // Traverse operations.
761   for (auto &op : bb) {
762     // Set the current debug location within the builder.
763     builder.SetCurrentDebugLocation(
764         debugTranslation->translateLoc(op.getLoc(), subprogram));
765 
766     if (failed(convertOperation(op, builder)))
767       return failure();
768   }
769 
770   return success();
771 }
772 
773 /// Create named global variables that correspond to llvm.mlir.global
774 /// definitions.
convertGlobals()775 LogicalResult ModuleTranslation::convertGlobals() {
776   for (auto op : getModuleBody(mlirModule).getOps<LLVM::GlobalOp>()) {
777     llvm::Type *type = convertType(op.getType());
778     llvm::Constant *cst = llvm::UndefValue::get(type);
779     if (op.getValueOrNull()) {
780       // String attributes are treated separately because they cannot appear as
781       // in-function constants and are thus not supported by getLLVMConstant.
782       if (auto strAttr = op.getValueOrNull().dyn_cast_or_null<StringAttr>()) {
783         cst = llvm::ConstantDataArray::getString(
784             llvmModule->getContext(), strAttr.getValue(), /*AddNull=*/false);
785         type = cst->getType();
786       } else if (!(cst = getLLVMConstant(type, op.getValueOrNull(),
787                                          op.getLoc()))) {
788         return failure();
789       }
790     } else if (Block *initializer = op.getInitializerBlock()) {
791       llvm::IRBuilder<> builder(llvmModule->getContext());
792       for (auto &op : initializer->without_terminator()) {
793         if (failed(convertOperation(op, builder)) ||
794             !isa<llvm::Constant>(valueMapping.lookup(op.getResult(0))))
795           return emitError(op.getLoc(), "unemittable constant value");
796       }
797       ReturnOp ret = cast<ReturnOp>(initializer->getTerminator());
798       cst = cast<llvm::Constant>(valueMapping.lookup(ret.getOperand(0)));
799     }
800 
801     auto linkage = convertLinkageToLLVM(op.linkage());
802     bool anyExternalLinkage =
803         ((linkage == llvm::GlobalVariable::ExternalLinkage &&
804           isa<llvm::UndefValue>(cst)) ||
805          linkage == llvm::GlobalVariable::ExternalWeakLinkage);
806     auto addrSpace = op.addr_space();
807     auto *var = new llvm::GlobalVariable(
808         *llvmModule, type, op.constant(), linkage,
809         anyExternalLinkage ? nullptr : cst, op.sym_name(),
810         /*InsertBefore=*/nullptr, llvm::GlobalValue::NotThreadLocal, addrSpace);
811 
812     globalsMapping.try_emplace(op, var);
813   }
814 
815   return success();
816 }
817 
818 /// Attempts to add an attribute identified by `key`, optionally with the given
819 /// `value` to LLVM function `llvmFunc`. Reports errors at `loc` if any. If the
820 /// attribute has a kind known to LLVM IR, create the attribute of this kind,
821 /// otherwise keep it as a string attribute. Performs additional checks for
822 /// attributes known to have or not have a value in order to avoid assertions
823 /// inside LLVM upon construction.
checkedAddLLVMFnAttribute(Location loc,llvm::Function * llvmFunc,StringRef key,StringRef value=StringRef ())824 static LogicalResult checkedAddLLVMFnAttribute(Location loc,
825                                                llvm::Function *llvmFunc,
826                                                StringRef key,
827                                                StringRef value = StringRef()) {
828   auto kind = llvm::Attribute::getAttrKindFromName(key);
829   if (kind == llvm::Attribute::None) {
830     llvmFunc->addFnAttr(key, value);
831     return success();
832   }
833 
834   if (llvm::Attribute::doesAttrKindHaveArgument(kind)) {
835     if (value.empty())
836       return emitError(loc) << "LLVM attribute '" << key << "' expects a value";
837 
838     int result;
839     if (!value.getAsInteger(/*Radix=*/0, result))
840       llvmFunc->addFnAttr(
841           llvm::Attribute::get(llvmFunc->getContext(), kind, result));
842     else
843       llvmFunc->addFnAttr(key, value);
844     return success();
845   }
846 
847   if (!value.empty())
848     return emitError(loc) << "LLVM attribute '" << key
849                           << "' does not expect a value, found '" << value
850                           << "'";
851 
852   llvmFunc->addFnAttr(kind);
853   return success();
854 }
855 
856 /// Attaches the attributes listed in the given array attribute to `llvmFunc`.
857 /// Reports error to `loc` if any and returns immediately. Expects `attributes`
858 /// to be an array attribute containing either string attributes, treated as
859 /// value-less LLVM attributes, or array attributes containing two string
860 /// attributes, with the first string being the name of the corresponding LLVM
861 /// attribute and the second string beings its value. Note that even integer
862 /// attributes are expected to have their values expressed as strings.
863 static LogicalResult
forwardPassthroughAttributes(Location loc,Optional<ArrayAttr> attributes,llvm::Function * llvmFunc)864 forwardPassthroughAttributes(Location loc, Optional<ArrayAttr> attributes,
865                              llvm::Function *llvmFunc) {
866   if (!attributes)
867     return success();
868 
869   for (Attribute attr : *attributes) {
870     if (auto stringAttr = attr.dyn_cast<StringAttr>()) {
871       if (failed(
872               checkedAddLLVMFnAttribute(loc, llvmFunc, stringAttr.getValue())))
873         return failure();
874       continue;
875     }
876 
877     auto arrayAttr = attr.dyn_cast<ArrayAttr>();
878     if (!arrayAttr || arrayAttr.size() != 2)
879       return emitError(loc)
880              << "expected 'passthrough' to contain string or array attributes";
881 
882     auto keyAttr = arrayAttr[0].dyn_cast<StringAttr>();
883     auto valueAttr = arrayAttr[1].dyn_cast<StringAttr>();
884     if (!keyAttr || !valueAttr)
885       return emitError(loc)
886              << "expected arrays within 'passthrough' to contain two strings";
887 
888     if (failed(checkedAddLLVMFnAttribute(loc, llvmFunc, keyAttr.getValue(),
889                                          valueAttr.getValue())))
890       return failure();
891   }
892   return success();
893 }
894 
convertOneFunction(LLVMFuncOp func)895 LogicalResult ModuleTranslation::convertOneFunction(LLVMFuncOp func) {
896   // Clear the block and value mappings, they are only relevant within one
897   // function.
898   blockMapping.clear();
899   valueMapping.clear();
900   llvm::Function *llvmFunc = functionMapping.lookup(func.getName());
901 
902   // Translate the debug information for this function.
903   debugTranslation->translate(func, *llvmFunc);
904 
905   // Add function arguments to the value remapping table.
906   // If there was noalias info then we decorate each argument accordingly.
907   unsigned int argIdx = 0;
908   for (auto kvp : llvm::zip(func.getArguments(), llvmFunc->args())) {
909     llvm::Argument &llvmArg = std::get<1>(kvp);
910     BlockArgument mlirArg = std::get<0>(kvp);
911 
912     if (auto attr = func.getArgAttrOfType<BoolAttr>(
913             argIdx, LLVMDialect::getNoAliasAttrName())) {
914       // NB: Attribute already verified to be boolean, so check if we can indeed
915       // attach the attribute to this argument, based on its type.
916       auto argTy = mlirArg.getType().dyn_cast<LLVM::LLVMType>();
917       if (!argTy.isPointerTy())
918         return func.emitError(
919             "llvm.noalias attribute attached to LLVM non-pointer argument");
920       if (attr.getValue())
921         llvmArg.addAttr(llvm::Attribute::AttrKind::NoAlias);
922     }
923 
924     if (auto attr = func.getArgAttrOfType<IntegerAttr>(
925             argIdx, LLVMDialect::getAlignAttrName())) {
926       // NB: Attribute already verified to be int, so check if we can indeed
927       // attach the attribute to this argument, based on its type.
928       auto argTy = mlirArg.getType().dyn_cast<LLVM::LLVMType>();
929       if (!argTy.isPointerTy())
930         return func.emitError(
931             "llvm.align attribute attached to LLVM non-pointer argument");
932       llvmArg.addAttrs(
933           llvm::AttrBuilder().addAlignmentAttr(llvm::Align(attr.getInt())));
934     }
935 
936     valueMapping[mlirArg] = &llvmArg;
937     argIdx++;
938   }
939 
940   // Check the personality and set it.
941   if (func.personality().hasValue()) {
942     llvm::Type *ty = llvm::Type::getInt8PtrTy(llvmFunc->getContext());
943     if (llvm::Constant *pfunc =
944             getLLVMConstant(ty, func.personalityAttr(), func.getLoc()))
945       llvmFunc->setPersonalityFn(pfunc);
946   }
947 
948   // First, create all blocks so we can jump to them.
949   llvm::LLVMContext &llvmContext = llvmFunc->getContext();
950   for (auto &bb : func) {
951     auto *llvmBB = llvm::BasicBlock::Create(llvmContext);
952     llvmBB->insertInto(llvmFunc);
953     blockMapping[&bb] = llvmBB;
954   }
955 
956   // Then, convert blocks one by one in topological order to ensure defs are
957   // converted before uses.
958   auto blocks = topologicalSort(func);
959   for (auto indexedBB : llvm::enumerate(blocks)) {
960     auto *bb = indexedBB.value();
961     if (failed(convertBlock(*bb, /*ignoreArguments=*/indexedBB.index() == 0)))
962       return failure();
963   }
964 
965   // Finally, after all blocks have been traversed and values mapped, connect
966   // the PHI nodes to the results of preceding blocks.
967   connectPHINodes(func, valueMapping, blockMapping);
968   return success();
969 }
970 
checkSupportedModuleOps(Operation * m)971 LogicalResult ModuleTranslation::checkSupportedModuleOps(Operation *m) {
972   for (Operation &o : getModuleBody(m).getOperations())
973     if (!isa<LLVM::LLVMFuncOp, LLVM::GlobalOp>(&o) && !o.isKnownTerminator())
974       return o.emitOpError("unsupported module-level operation");
975   return success();
976 }
977 
convertFunctionSignatures()978 LogicalResult ModuleTranslation::convertFunctionSignatures() {
979   // Declare all functions first because there may be function calls that form a
980   // call graph with cycles, or global initializers that reference functions.
981   for (auto function : getModuleBody(mlirModule).getOps<LLVMFuncOp>()) {
982     llvm::FunctionCallee llvmFuncCst = llvmModule->getOrInsertFunction(
983         function.getName(),
984         cast<llvm::FunctionType>(convertType(function.getType())));
985     llvm::Function *llvmFunc = cast<llvm::Function>(llvmFuncCst.getCallee());
986     llvmFunc->setLinkage(convertLinkageToLLVM(function.linkage()));
987     functionMapping[function.getName()] = llvmFunc;
988 
989     // Forward the pass-through attributes to LLVM.
990     if (failed(forwardPassthroughAttributes(function.getLoc(),
991                                             function.passthrough(), llvmFunc)))
992       return failure();
993   }
994 
995   return success();
996 }
997 
convertFunctions()998 LogicalResult ModuleTranslation::convertFunctions() {
999   // Convert functions.
1000   for (auto function : getModuleBody(mlirModule).getOps<LLVMFuncOp>()) {
1001     // Ignore external functions.
1002     if (function.isExternal())
1003       continue;
1004 
1005     if (failed(convertOneFunction(function)))
1006       return failure();
1007   }
1008 
1009   return success();
1010 }
1011 
convertType(LLVMType type)1012 llvm::Type *ModuleTranslation::convertType(LLVMType type) {
1013   return typeTranslator.translateType(type);
1014 }
1015 
1016 /// A helper to look up remapped operands in the value remapping table.`
1017 SmallVector<llvm::Value *, 8>
lookupValues(ValueRange values)1018 ModuleTranslation::lookupValues(ValueRange values) {
1019   SmallVector<llvm::Value *, 8> remapped;
1020   remapped.reserve(values.size());
1021   for (Value v : values) {
1022     assert(valueMapping.count(v) && "referencing undefined value");
1023     remapped.push_back(valueMapping.lookup(v));
1024   }
1025   return remapped;
1026 }
1027 
prepareLLVMModule(Operation * m,llvm::LLVMContext & llvmContext,StringRef name)1028 std::unique_ptr<llvm::Module> ModuleTranslation::prepareLLVMModule(
1029     Operation *m, llvm::LLVMContext &llvmContext, StringRef name) {
1030   m->getContext()->getOrLoadDialect<LLVM::LLVMDialect>();
1031   auto llvmModule = std::make_unique<llvm::Module>(name, llvmContext);
1032   if (auto dataLayoutAttr =
1033           m->getAttr(LLVM::LLVMDialect::getDataLayoutAttrName()))
1034     llvmModule->setDataLayout(dataLayoutAttr.cast<StringAttr>().getValue());
1035   if (auto targetTripleAttr =
1036           m->getAttr(LLVM::LLVMDialect::getTargetTripleAttrName()))
1037     llvmModule->setTargetTriple(targetTripleAttr.cast<StringAttr>().getValue());
1038 
1039   // Inject declarations for `malloc` and `free` functions that can be used in
1040   // memref allocation/deallocation coming from standard ops lowering.
1041   llvm::IRBuilder<> builder(llvmContext);
1042   llvmModule->getOrInsertFunction("malloc", builder.getInt8PtrTy(),
1043                                   builder.getInt64Ty());
1044   llvmModule->getOrInsertFunction("free", builder.getVoidTy(),
1045                                   builder.getInt8PtrTy());
1046 
1047   return llvmModule;
1048 }
1049