1cde4d5a6SJacques Pienaar //===- ModuleTranslation.cpp - MLIR to LLVM conversion --------------------===//
25d7231d8SStephan Herhut //
356222a06SMehdi Amini // Part of the MLIR Project, under the Apache License v2.0 with LLVM Exceptions.
456222a06SMehdi Amini // See https://llvm.org/LICENSE.txt for license information.
556222a06SMehdi Amini // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
65d7231d8SStephan Herhut //
756222a06SMehdi Amini //===----------------------------------------------------------------------===//
85d7231d8SStephan Herhut //
95d7231d8SStephan Herhut // This file implements the translation between an MLIR LLVM dialect module and
105d7231d8SStephan Herhut // the corresponding LLVMIR module. It only handles core LLVM IR operations.
115d7231d8SStephan Herhut //
125d7231d8SStephan Herhut //===----------------------------------------------------------------------===//
135d7231d8SStephan Herhut 
145d7231d8SStephan Herhut #include "mlir/Target/LLVMIR/ModuleTranslation.h"
155d7231d8SStephan Herhut 
16ba0fa925SRiver Riddle #include "mlir/Dialect/LLVMIR/LLVMDialect.h"
175d7231d8SStephan Herhut #include "mlir/IR/Attributes.h"
185d7231d8SStephan Herhut #include "mlir/IR/Module.h"
19a4a42160SAlex Zinenko #include "mlir/IR/StandardTypes.h"
205d7231d8SStephan Herhut #include "mlir/Support/LLVM.h"
215d7231d8SStephan Herhut 
225d7231d8SStephan Herhut #include "llvm/ADT/SetVector.h"
235d7231d8SStephan Herhut #include "llvm/IR/BasicBlock.h"
245d7231d8SStephan Herhut #include "llvm/IR/Constants.h"
255d7231d8SStephan Herhut #include "llvm/IR/DerivedTypes.h"
265d7231d8SStephan Herhut #include "llvm/IR/IRBuilder.h"
275d7231d8SStephan Herhut #include "llvm/IR/LLVMContext.h"
285d7231d8SStephan Herhut #include "llvm/IR/Module.h"
295d7231d8SStephan Herhut #include "llvm/Transforms/Utils/Cloning.h"
305d7231d8SStephan Herhut 
312666b973SRiver Riddle using namespace mlir;
322666b973SRiver Riddle using namespace mlir::LLVM;
335d7231d8SStephan Herhut 
34*a922e231SAlex Zinenko /// Builds a constant of a sequential LLVM type `type`, potentially containing
35*a922e231SAlex Zinenko /// other sequential types recursively, from the individual constant values
36*a922e231SAlex Zinenko /// provided in `constants`. `shape` contains the number of elements in nested
37*a922e231SAlex Zinenko /// sequential types. Reports errors at `loc` and returns nullptr on error.
38a4a42160SAlex Zinenko static llvm::Constant *
39a4a42160SAlex Zinenko buildSequentialConstant(ArrayRef<llvm::Constant *> &constants,
40a4a42160SAlex Zinenko                         ArrayRef<int64_t> shape, llvm::Type *type,
41a4a42160SAlex Zinenko                         Location loc) {
42a4a42160SAlex Zinenko   if (shape.empty()) {
43a4a42160SAlex Zinenko     llvm::Constant *result = constants.front();
44a4a42160SAlex Zinenko     constants = constants.drop_front();
45a4a42160SAlex Zinenko     return result;
46a4a42160SAlex Zinenko   }
47a4a42160SAlex Zinenko 
48a4a42160SAlex Zinenko   if (!isa<llvm::SequentialType>(type)) {
49a4a42160SAlex Zinenko     emitError(loc) << "expected sequential LLVM types wrapping a scalar";
50a4a42160SAlex Zinenko     return nullptr;
51a4a42160SAlex Zinenko   }
52a4a42160SAlex Zinenko 
53a4a42160SAlex Zinenko   llvm::Type *elementType = type->getSequentialElementType();
54a4a42160SAlex Zinenko   SmallVector<llvm::Constant *, 8> nested;
55a4a42160SAlex Zinenko   nested.reserve(shape.front());
56a4a42160SAlex Zinenko   for (int64_t i = 0; i < shape.front(); ++i) {
57a4a42160SAlex Zinenko     nested.push_back(buildSequentialConstant(constants, shape.drop_front(),
58a4a42160SAlex Zinenko                                              elementType, loc));
59a4a42160SAlex Zinenko     if (!nested.back())
60a4a42160SAlex Zinenko       return nullptr;
61a4a42160SAlex Zinenko   }
62a4a42160SAlex Zinenko 
63a4a42160SAlex Zinenko   if (shape.size() == 1 && type->isVectorTy())
64a4a42160SAlex Zinenko     return llvm::ConstantVector::get(nested);
65a4a42160SAlex Zinenko   return llvm::ConstantArray::get(
66a4a42160SAlex Zinenko       llvm::ArrayType::get(elementType, shape.front()), nested);
67a4a42160SAlex Zinenko }
68a4a42160SAlex Zinenko 
69*a922e231SAlex Zinenko /// Returns the first non-sequential type netsed in sequential types.
70a4a42160SAlex Zinenko static llvm::Type *getInnermostElementType(llvm::Type *type) {
71a4a42160SAlex Zinenko   while (isa<llvm::SequentialType>(type))
72a4a42160SAlex Zinenko     type = type->getSequentialElementType();
73a4a42160SAlex Zinenko   return type;
74a4a42160SAlex Zinenko }
75a4a42160SAlex Zinenko 
762666b973SRiver Riddle /// Create an LLVM IR constant of `llvmType` from the MLIR attribute `attr`.
772666b973SRiver Riddle /// This currently supports integer, floating point, splat and dense element
782666b973SRiver Riddle /// attributes and combinations thereof.  In case of error, report it to `loc`
792666b973SRiver Riddle /// and return nullptr.
805d7231d8SStephan Herhut llvm::Constant *ModuleTranslation::getLLVMConstant(llvm::Type *llvmType,
815d7231d8SStephan Herhut                                                    Attribute attr,
825d7231d8SStephan Herhut                                                    Location loc) {
8333a3a91bSChristian Sigg   if (!attr)
8433a3a91bSChristian Sigg     return llvm::UndefValue::get(llvmType);
85a4a42160SAlex Zinenko   if (llvmType->isStructTy()) {
86a4a42160SAlex Zinenko     emitError(loc, "struct types are not supported in constants");
87a4a42160SAlex Zinenko     return nullptr;
88a4a42160SAlex Zinenko   }
895d7231d8SStephan Herhut   if (auto intAttr = attr.dyn_cast<IntegerAttr>())
905d7231d8SStephan Herhut     return llvm::ConstantInt::get(llvmType, intAttr.getValue());
915d7231d8SStephan Herhut   if (auto floatAttr = attr.dyn_cast<FloatAttr>())
925d7231d8SStephan Herhut     return llvm::ConstantFP::get(llvmType, floatAttr.getValue());
939b9c647cSRiver Riddle   if (auto funcAttr = attr.dyn_cast<FlatSymbolRefAttr>())
945d7231d8SStephan Herhut     return functionMapping.lookup(funcAttr.getValue());
955d7231d8SStephan Herhut   if (auto splatAttr = attr.dyn_cast<SplatElementsAttr>()) {
962f13df13SMLIR Team     auto *sequentialType = cast<llvm::SequentialType>(llvmType);
972f13df13SMLIR Team     auto elementType = sequentialType->getElementType();
982f13df13SMLIR Team     uint64_t numElements = sequentialType->getNumElements();
99d6ea8ff0SAlex Zinenko     // Splat value is a scalar. Extract it only if the element type is not
100d6ea8ff0SAlex Zinenko     // another sequence type. The recursion terminates because each step removes
101d6ea8ff0SAlex Zinenko     // one outer sequential type.
102d6ea8ff0SAlex Zinenko     llvm::Constant *child = getLLVMConstant(
103d6ea8ff0SAlex Zinenko         elementType,
104d6ea8ff0SAlex Zinenko         isa<llvm::SequentialType>(elementType) ? splatAttr
105d6ea8ff0SAlex Zinenko                                                : splatAttr.getSplatValue(),
106d6ea8ff0SAlex Zinenko         loc);
107a4a42160SAlex Zinenko     if (!child)
108a4a42160SAlex Zinenko       return nullptr;
1092f13df13SMLIR Team     if (llvmType->isVectorTy())
1102f13df13SMLIR Team       return llvm::ConstantVector::getSplat(numElements, child);
1112f13df13SMLIR Team     if (llvmType->isArrayTy()) {
1122f13df13SMLIR Team       auto arrayType = llvm::ArrayType::get(elementType, numElements);
1132f13df13SMLIR Team       SmallVector<llvm::Constant *, 8> constants(numElements, child);
1142f13df13SMLIR Team       return llvm::ConstantArray::get(arrayType, constants);
1152f13df13SMLIR Team     }
1165d7231d8SStephan Herhut   }
117a4a42160SAlex Zinenko 
118d906f84bSRiver Riddle   if (auto elementsAttr = attr.dyn_cast<ElementsAttr>()) {
119a4a42160SAlex Zinenko     assert(elementsAttr.getType().hasStaticShape());
120a4a42160SAlex Zinenko     assert(elementsAttr.getNumElements() != 0 &&
121a4a42160SAlex Zinenko            "unexpected empty elements attribute");
122a4a42160SAlex Zinenko     assert(!elementsAttr.getType().getShape().empty() &&
123a4a42160SAlex Zinenko            "unexpected empty elements attribute shape");
124a4a42160SAlex Zinenko 
1255d7231d8SStephan Herhut     SmallVector<llvm::Constant *, 8> constants;
126a4a42160SAlex Zinenko     constants.reserve(elementsAttr.getNumElements());
127a4a42160SAlex Zinenko     llvm::Type *innermostType = getInnermostElementType(llvmType);
128d906f84bSRiver Riddle     for (auto n : elementsAttr.getValues<Attribute>()) {
129a4a42160SAlex Zinenko       constants.push_back(getLLVMConstant(innermostType, n, loc));
1305d7231d8SStephan Herhut       if (!constants.back())
1315d7231d8SStephan Herhut         return nullptr;
1325d7231d8SStephan Herhut     }
133a4a42160SAlex Zinenko     ArrayRef<llvm::Constant *> constantsRef = constants;
134a4a42160SAlex Zinenko     llvm::Constant *result = buildSequentialConstant(
135a4a42160SAlex Zinenko         constantsRef, elementsAttr.getType().getShape(), llvmType, loc);
136a4a42160SAlex Zinenko     assert(constantsRef.empty() && "did not consume all elemental constants");
137a4a42160SAlex Zinenko     return result;
1382f13df13SMLIR Team   }
139a4a42160SAlex Zinenko 
140cb348dffSStephan Herhut   if (auto stringAttr = attr.dyn_cast<StringAttr>()) {
141cb348dffSStephan Herhut     return llvm::ConstantDataArray::get(
142cb348dffSStephan Herhut         llvmModule->getContext(), ArrayRef<char>{stringAttr.getValue().data(),
143cb348dffSStephan Herhut                                                  stringAttr.getValue().size()});
144cb348dffSStephan Herhut   }
145a4c3a645SRiver Riddle   emitError(loc, "unsupported constant value");
1465d7231d8SStephan Herhut   return nullptr;
1475d7231d8SStephan Herhut }
1485d7231d8SStephan Herhut 
1492666b973SRiver Riddle /// Convert MLIR integer comparison predicate to LLVM IR comparison predicate.
150ec82e1c9SAlex Zinenko static llvm::CmpInst::Predicate getLLVMCmpPredicate(ICmpPredicate p) {
1515d7231d8SStephan Herhut   switch (p) {
152ec82e1c9SAlex Zinenko   case LLVM::ICmpPredicate::eq:
1535d7231d8SStephan Herhut     return llvm::CmpInst::Predicate::ICMP_EQ;
154ec82e1c9SAlex Zinenko   case LLVM::ICmpPredicate::ne:
1555d7231d8SStephan Herhut     return llvm::CmpInst::Predicate::ICMP_NE;
156ec82e1c9SAlex Zinenko   case LLVM::ICmpPredicate::slt:
1575d7231d8SStephan Herhut     return llvm::CmpInst::Predicate::ICMP_SLT;
158ec82e1c9SAlex Zinenko   case LLVM::ICmpPredicate::sle:
1595d7231d8SStephan Herhut     return llvm::CmpInst::Predicate::ICMP_SLE;
160ec82e1c9SAlex Zinenko   case LLVM::ICmpPredicate::sgt:
1615d7231d8SStephan Herhut     return llvm::CmpInst::Predicate::ICMP_SGT;
162ec82e1c9SAlex Zinenko   case LLVM::ICmpPredicate::sge:
1635d7231d8SStephan Herhut     return llvm::CmpInst::Predicate::ICMP_SGE;
164ec82e1c9SAlex Zinenko   case LLVM::ICmpPredicate::ult:
1655d7231d8SStephan Herhut     return llvm::CmpInst::Predicate::ICMP_ULT;
166ec82e1c9SAlex Zinenko   case LLVM::ICmpPredicate::ule:
1675d7231d8SStephan Herhut     return llvm::CmpInst::Predicate::ICMP_ULE;
168ec82e1c9SAlex Zinenko   case LLVM::ICmpPredicate::ugt:
1695d7231d8SStephan Herhut     return llvm::CmpInst::Predicate::ICMP_UGT;
170ec82e1c9SAlex Zinenko   case LLVM::ICmpPredicate::uge:
1715d7231d8SStephan Herhut     return llvm::CmpInst::Predicate::ICMP_UGE;
1725d7231d8SStephan Herhut   }
173e6365f3dSJacques Pienaar   llvm_unreachable("incorrect comparison predicate");
1745d7231d8SStephan Herhut }
1755d7231d8SStephan Herhut 
17648fdc8d7SNagy Mostafa static llvm::CmpInst::Predicate getLLVMCmpPredicate(FCmpPredicate p) {
17748fdc8d7SNagy Mostafa   switch (p) {
17848fdc8d7SNagy Mostafa   case LLVM::FCmpPredicate::_false:
17948fdc8d7SNagy Mostafa     return llvm::CmpInst::Predicate::FCMP_FALSE;
18048fdc8d7SNagy Mostafa   case LLVM::FCmpPredicate::oeq:
18148fdc8d7SNagy Mostafa     return llvm::CmpInst::Predicate::FCMP_OEQ;
18248fdc8d7SNagy Mostafa   case LLVM::FCmpPredicate::ogt:
18348fdc8d7SNagy Mostafa     return llvm::CmpInst::Predicate::FCMP_OGT;
18448fdc8d7SNagy Mostafa   case LLVM::FCmpPredicate::oge:
18548fdc8d7SNagy Mostafa     return llvm::CmpInst::Predicate::FCMP_OGE;
18648fdc8d7SNagy Mostafa   case LLVM::FCmpPredicate::olt:
18748fdc8d7SNagy Mostafa     return llvm::CmpInst::Predicate::FCMP_OLT;
18848fdc8d7SNagy Mostafa   case LLVM::FCmpPredicate::ole:
18948fdc8d7SNagy Mostafa     return llvm::CmpInst::Predicate::FCMP_OLE;
19048fdc8d7SNagy Mostafa   case LLVM::FCmpPredicate::one:
19148fdc8d7SNagy Mostafa     return llvm::CmpInst::Predicate::FCMP_ONE;
19248fdc8d7SNagy Mostafa   case LLVM::FCmpPredicate::ord:
19348fdc8d7SNagy Mostafa     return llvm::CmpInst::Predicate::FCMP_ORD;
19448fdc8d7SNagy Mostafa   case LLVM::FCmpPredicate::ueq:
19548fdc8d7SNagy Mostafa     return llvm::CmpInst::Predicate::FCMP_UEQ;
19648fdc8d7SNagy Mostafa   case LLVM::FCmpPredicate::ugt:
19748fdc8d7SNagy Mostafa     return llvm::CmpInst::Predicate::FCMP_UGT;
19848fdc8d7SNagy Mostafa   case LLVM::FCmpPredicate::uge:
19948fdc8d7SNagy Mostafa     return llvm::CmpInst::Predicate::FCMP_UGE;
20048fdc8d7SNagy Mostafa   case LLVM::FCmpPredicate::ult:
20148fdc8d7SNagy Mostafa     return llvm::CmpInst::Predicate::FCMP_ULT;
20248fdc8d7SNagy Mostafa   case LLVM::FCmpPredicate::ule:
20348fdc8d7SNagy Mostafa     return llvm::CmpInst::Predicate::FCMP_ULE;
20448fdc8d7SNagy Mostafa   case LLVM::FCmpPredicate::une:
20548fdc8d7SNagy Mostafa     return llvm::CmpInst::Predicate::FCMP_UNE;
20648fdc8d7SNagy Mostafa   case LLVM::FCmpPredicate::uno:
20748fdc8d7SNagy Mostafa     return llvm::CmpInst::Predicate::FCMP_UNO;
20848fdc8d7SNagy Mostafa   case LLVM::FCmpPredicate::_true:
20948fdc8d7SNagy Mostafa     return llvm::CmpInst::Predicate::FCMP_TRUE;
21048fdc8d7SNagy Mostafa   }
211e6365f3dSJacques Pienaar   llvm_unreachable("incorrect comparison predicate");
21248fdc8d7SNagy Mostafa }
21348fdc8d7SNagy Mostafa 
2142666b973SRiver Riddle /// Given a single MLIR operation, create the corresponding LLVM IR operation
2152666b973SRiver Riddle /// using the `builder`.  LLVM IR Builder does not have a generic interface so
2162666b973SRiver Riddle /// this has to be a long chain of `if`s calling different functions with a
2172666b973SRiver Riddle /// different number of arguments.
218baa1ec22SAlex Zinenko LogicalResult ModuleTranslation::convertOperation(Operation &opInst,
2195d7231d8SStephan Herhut                                                   llvm::IRBuilder<> &builder) {
2205d7231d8SStephan Herhut   auto extractPosition = [](ArrayAttr attr) {
2215d7231d8SStephan Herhut     SmallVector<unsigned, 4> position;
2225d7231d8SStephan Herhut     position.reserve(attr.size());
2235d7231d8SStephan Herhut     for (Attribute v : attr)
2245d7231d8SStephan Herhut       position.push_back(v.cast<IntegerAttr>().getValue().getZExtValue());
2255d7231d8SStephan Herhut     return position;
2265d7231d8SStephan Herhut   };
2275d7231d8SStephan Herhut 
228ba0fa925SRiver Riddle #include "mlir/Dialect/LLVMIR/LLVMConversions.inc"
2295d7231d8SStephan Herhut 
2305d7231d8SStephan Herhut   // Emit function calls.  If the "callee" attribute is present, this is a
2315d7231d8SStephan Herhut   // direct function call and we also need to look up the remapped function
2325d7231d8SStephan Herhut   // itself.  Otherwise, this is an indirect call and the callee is the first
2335d7231d8SStephan Herhut   // operand, look it up as a normal value.  Return the llvm::Value representing
2345d7231d8SStephan Herhut   // the function result, which may be of llvm::VoidTy type.
2355d7231d8SStephan Herhut   auto convertCall = [this, &builder](Operation &op) -> llvm::Value * {
2365d7231d8SStephan Herhut     auto operands = lookupValues(op.getOperands());
2375d7231d8SStephan Herhut     ArrayRef<llvm::Value *> operandsRef(operands);
2389b9c647cSRiver Riddle     if (auto attr = op.getAttrOfType<FlatSymbolRefAttr>("callee")) {
2395d7231d8SStephan Herhut       return builder.CreateCall(functionMapping.lookup(attr.getValue()),
2405d7231d8SStephan Herhut                                 operandsRef);
2415d7231d8SStephan Herhut     } else {
2425d7231d8SStephan Herhut       return builder.CreateCall(operandsRef.front(), operandsRef.drop_front());
2435d7231d8SStephan Herhut     }
2445d7231d8SStephan Herhut   };
2455d7231d8SStephan Herhut 
2465d7231d8SStephan Herhut   // Emit calls.  If the called function has a result, remap the corresponding
2475d7231d8SStephan Herhut   // value.  Note that LLVM IR dialect CallOp has either 0 or 1 result.
248d5b60ee8SRiver Riddle   if (isa<LLVM::CallOp>(opInst)) {
2495d7231d8SStephan Herhut     llvm::Value *result = convertCall(opInst);
2505d7231d8SStephan Herhut     if (opInst.getNumResults() != 0) {
2515d7231d8SStephan Herhut       valueMapping[opInst.getResult(0)] = result;
252baa1ec22SAlex Zinenko       return success();
2535d7231d8SStephan Herhut     }
2545d7231d8SStephan Herhut     // Check that LLVM call returns void for 0-result functions.
255baa1ec22SAlex Zinenko     return success(result->getType()->isVoidTy());
2565d7231d8SStephan Herhut   }
2575d7231d8SStephan Herhut 
2585d7231d8SStephan Herhut   // Emit branches.  We need to look up the remapped blocks and ignore the block
2595d7231d8SStephan Herhut   // arguments that were transformed into PHI nodes.
260c5ecf991SRiver Riddle   if (auto brOp = dyn_cast<LLVM::BrOp>(opInst)) {
2615d7231d8SStephan Herhut     builder.CreateBr(blockMapping[brOp.getSuccessor(0)]);
262baa1ec22SAlex Zinenko     return success();
2635d7231d8SStephan Herhut   }
264c5ecf991SRiver Riddle   if (auto condbrOp = dyn_cast<LLVM::CondBrOp>(opInst)) {
2655d7231d8SStephan Herhut     builder.CreateCondBr(valueMapping.lookup(condbrOp.getOperand(0)),
2665d7231d8SStephan Herhut                          blockMapping[condbrOp.getSuccessor(0)],
2675d7231d8SStephan Herhut                          blockMapping[condbrOp.getSuccessor(1)]);
268baa1ec22SAlex Zinenko     return success();
2695d7231d8SStephan Herhut   }
2705d7231d8SStephan Herhut 
2712dd38b09SAlex Zinenko   // Emit addressof.  We need to look up the global value referenced by the
2722dd38b09SAlex Zinenko   // operation and store it in the MLIR-to-LLVM value mapping.  This does not
2732dd38b09SAlex Zinenko   // emit any LLVM instruction.
2742dd38b09SAlex Zinenko   if (auto addressOfOp = dyn_cast<LLVM::AddressOfOp>(opInst)) {
2752dd38b09SAlex Zinenko     LLVM::GlobalOp global = addressOfOp.getGlobal();
2762dd38b09SAlex Zinenko     // The verifier should not have allowed this.
2772dd38b09SAlex Zinenko     assert(global && "referencing an undefined global");
2782dd38b09SAlex Zinenko 
2792dd38b09SAlex Zinenko     valueMapping[addressOfOp.getResult()] = globalsMapping.lookup(global);
2802dd38b09SAlex Zinenko     return success();
2812dd38b09SAlex Zinenko   }
2822dd38b09SAlex Zinenko 
283baa1ec22SAlex Zinenko   return opInst.emitError("unsupported or non-LLVM operation: ")
284baa1ec22SAlex Zinenko          << opInst.getName();
2855d7231d8SStephan Herhut }
2865d7231d8SStephan Herhut 
2872666b973SRiver Riddle /// Convert block to LLVM IR.  Unless `ignoreArguments` is set, emit PHI nodes
2882666b973SRiver Riddle /// to define values corresponding to the MLIR block arguments.  These nodes
2892666b973SRiver Riddle /// are not connected to the source basic blocks, which may not exist yet.
290baa1ec22SAlex Zinenko LogicalResult ModuleTranslation::convertBlock(Block &bb, bool ignoreArguments) {
2915d7231d8SStephan Herhut   llvm::IRBuilder<> builder(blockMapping[&bb]);
2925d7231d8SStephan Herhut 
2935d7231d8SStephan Herhut   // Before traversing operations, make block arguments available through
2945d7231d8SStephan Herhut   // value remapping and PHI nodes, but do not add incoming edges for the PHI
2955d7231d8SStephan Herhut   // nodes just yet: those values may be defined by this or following blocks.
2965d7231d8SStephan Herhut   // This step is omitted if "ignoreArguments" is set.  The arguments of the
2975d7231d8SStephan Herhut   // first block have been already made available through the remapping of
2985d7231d8SStephan Herhut   // LLVM function arguments.
2995d7231d8SStephan Herhut   if (!ignoreArguments) {
3005d7231d8SStephan Herhut     auto predecessors = bb.getPredecessors();
3015d7231d8SStephan Herhut     unsigned numPredecessors =
3025d7231d8SStephan Herhut         std::distance(predecessors.begin(), predecessors.end());
30335807bc4SRiver Riddle     for (auto arg : bb.getArguments()) {
3042bdf33ccSRiver Riddle       auto wrappedType = arg.getType().dyn_cast<LLVM::LLVMType>();
305baa1ec22SAlex Zinenko       if (!wrappedType)
306baa1ec22SAlex Zinenko         return emitError(bb.front().getLoc(),
307a4c3a645SRiver Riddle                          "block argument does not have an LLVM type");
3085d7231d8SStephan Herhut       llvm::Type *type = wrappedType.getUnderlyingType();
3095d7231d8SStephan Herhut       llvm::PHINode *phi = builder.CreatePHI(type, numPredecessors);
3105d7231d8SStephan Herhut       valueMapping[arg] = phi;
3115d7231d8SStephan Herhut     }
3125d7231d8SStephan Herhut   }
3135d7231d8SStephan Herhut 
3145d7231d8SStephan Herhut   // Traverse operations.
3155d7231d8SStephan Herhut   for (auto &op : bb) {
316baa1ec22SAlex Zinenko     if (failed(convertOperation(op, builder)))
317baa1ec22SAlex Zinenko       return failure();
3185d7231d8SStephan Herhut   }
3195d7231d8SStephan Herhut 
320baa1ec22SAlex Zinenko   return success();
3215d7231d8SStephan Herhut }
3225d7231d8SStephan Herhut 
3232666b973SRiver Riddle /// Convert the LLVM dialect linkage type to LLVM IR linkage type.
324d5e627f8SAlex Zinenko llvm::GlobalVariable::LinkageTypes convertLinkageType(LLVM::Linkage linkage) {
325d5e627f8SAlex Zinenko   switch (linkage) {
326d5e627f8SAlex Zinenko   case LLVM::Linkage::Private:
327d5e627f8SAlex Zinenko     return llvm::GlobalValue::PrivateLinkage;
328d5e627f8SAlex Zinenko   case LLVM::Linkage::Internal:
329d5e627f8SAlex Zinenko     return llvm::GlobalValue::InternalLinkage;
330d5e627f8SAlex Zinenko   case LLVM::Linkage::AvailableExternally:
331d5e627f8SAlex Zinenko     return llvm::GlobalValue::AvailableExternallyLinkage;
332d5e627f8SAlex Zinenko   case LLVM::Linkage::Linkonce:
333d5e627f8SAlex Zinenko     return llvm::GlobalValue::LinkOnceAnyLinkage;
334d5e627f8SAlex Zinenko   case LLVM::Linkage::Weak:
335d5e627f8SAlex Zinenko     return llvm::GlobalValue::WeakAnyLinkage;
336d5e627f8SAlex Zinenko   case LLVM::Linkage::Common:
337d5e627f8SAlex Zinenko     return llvm::GlobalValue::CommonLinkage;
338d5e627f8SAlex Zinenko   case LLVM::Linkage::Appending:
339d5e627f8SAlex Zinenko     return llvm::GlobalValue::AppendingLinkage;
340d5e627f8SAlex Zinenko   case LLVM::Linkage::ExternWeak:
341d5e627f8SAlex Zinenko     return llvm::GlobalValue::ExternalWeakLinkage;
342d5e627f8SAlex Zinenko   case LLVM::Linkage::LinkonceODR:
343d5e627f8SAlex Zinenko     return llvm::GlobalValue::LinkOnceODRLinkage;
344d5e627f8SAlex Zinenko   case LLVM::Linkage::WeakODR:
345d5e627f8SAlex Zinenko     return llvm::GlobalValue::WeakODRLinkage;
346d5e627f8SAlex Zinenko   case LLVM::Linkage::External:
347d5e627f8SAlex Zinenko     return llvm::GlobalValue::ExternalLinkage;
348d5e627f8SAlex Zinenko   }
349d5e627f8SAlex Zinenko   llvm_unreachable("unknown linkage type");
350d5e627f8SAlex Zinenko }
351d5e627f8SAlex Zinenko 
3522666b973SRiver Riddle /// Create named global variables that correspond to llvm.mlir.global
3532666b973SRiver Riddle /// definitions.
354b9ff2dd8SAlex Zinenko void ModuleTranslation::convertGlobals() {
35544fc7d72STres Popp   for (auto op : getModuleBody(mlirModule).getOps<LLVM::GlobalOp>()) {
356250a11aeSJames Molloy     llvm::Type *type = op.getType().getUnderlyingType();
357250a11aeSJames Molloy     llvm::Constant *cst = llvm::UndefValue::get(type);
358250a11aeSJames Molloy     if (op.getValueOrNull()) {
35968451df2SAlex Zinenko       // String attributes are treated separately because they cannot appear as
36068451df2SAlex Zinenko       // in-function constants and are thus not supported by getLLVMConstant.
36133a3a91bSChristian Sigg       if (auto strAttr = op.getValueOrNull().dyn_cast_or_null<StringAttr>()) {
3622dd38b09SAlex Zinenko         cst = llvm::ConstantDataArray::getString(
36368451df2SAlex Zinenko             llvmModule->getContext(), strAttr.getValue(), /*AddNull=*/false);
3642dd38b09SAlex Zinenko         type = cst->getType();
3652dd38b09SAlex Zinenko       } else {
36633a3a91bSChristian Sigg         cst = getLLVMConstant(type, op.getValueOrNull(), op.getLoc());
36768451df2SAlex Zinenko       }
368250a11aeSJames Molloy     } else if (Block *initializer = op.getInitializerBlock()) {
369250a11aeSJames Molloy       llvm::IRBuilder<> builder(llvmModule->getContext());
370250a11aeSJames Molloy       for (auto &op : initializer->without_terminator()) {
371250a11aeSJames Molloy         if (failed(convertOperation(op, builder)) ||
372250a11aeSJames Molloy             !isa<llvm::Constant>(valueMapping.lookup(op.getResult(0)))) {
373250a11aeSJames Molloy           emitError(op.getLoc(), "unemittable constant value");
374250a11aeSJames Molloy           return;
375250a11aeSJames Molloy         }
376250a11aeSJames Molloy       }
377250a11aeSJames Molloy       ReturnOp ret = cast<ReturnOp>(initializer->getTerminator());
378250a11aeSJames Molloy       cst = cast<llvm::Constant>(valueMapping.lookup(ret.getOperand(0)));
379250a11aeSJames Molloy     }
38068451df2SAlex Zinenko 
381d5e627f8SAlex Zinenko     auto linkage = convertLinkageType(op.linkage());
382d5e627f8SAlex Zinenko     bool anyExternalLinkage =
383d5e627f8SAlex Zinenko         (linkage == llvm::GlobalVariable::ExternalLinkage ||
384d5e627f8SAlex Zinenko          linkage == llvm::GlobalVariable::ExternalWeakLinkage);
385e79bfefbSMLIR Team     auto addrSpace = op.addr_space().getLimitedValue();
386e79bfefbSMLIR Team     auto *var = new llvm::GlobalVariable(
387d5e627f8SAlex Zinenko         *llvmModule, type, op.constant(), linkage,
388d5e627f8SAlex Zinenko         anyExternalLinkage ? nullptr : cst, op.sym_name(),
389d5e627f8SAlex Zinenko         /*InsertBefore=*/nullptr, llvm::GlobalValue::NotThreadLocal, addrSpace);
390e79bfefbSMLIR Team 
3912dd38b09SAlex Zinenko     globalsMapping.try_emplace(op, var);
392b9ff2dd8SAlex Zinenko   }
393b9ff2dd8SAlex Zinenko }
394b9ff2dd8SAlex Zinenko 
3952666b973SRiver Riddle /// Get the SSA value passed to the current block from the terminator operation
3962666b973SRiver Riddle /// of its predecessor.
397e62a6956SRiver Riddle static Value getPHISourceValue(Block *current, Block *pred,
3985d7231d8SStephan Herhut                                unsigned numArguments, unsigned index) {
3995d7231d8SStephan Herhut   auto &terminator = *pred->getTerminator();
400d5b60ee8SRiver Riddle   if (isa<LLVM::BrOp>(terminator)) {
4015d7231d8SStephan Herhut     return terminator.getOperand(index);
4025d7231d8SStephan Herhut   }
4035d7231d8SStephan Herhut 
4045d7231d8SStephan Herhut   // For conditional branches, we need to check if the current block is reached
4055d7231d8SStephan Herhut   // through the "true" or the "false" branch and take the relevant operands.
406c5ecf991SRiver Riddle   auto condBranchOp = dyn_cast<LLVM::CondBrOp>(terminator);
4075d7231d8SStephan Herhut   assert(condBranchOp &&
4085d7231d8SStephan Herhut          "only branch operations can be terminators of a block that "
4095d7231d8SStephan Herhut          "has successors");
4105d7231d8SStephan Herhut   assert((condBranchOp.getSuccessor(0) != condBranchOp.getSuccessor(1)) &&
4115d7231d8SStephan Herhut          "successors with arguments in LLVM conditional branches must be "
4125d7231d8SStephan Herhut          "different blocks");
4135d7231d8SStephan Herhut 
4145d7231d8SStephan Herhut   return condBranchOp.getSuccessor(0) == current
4155d7231d8SStephan Herhut              ? terminator.getSuccessorOperand(0, index)
4165d7231d8SStephan Herhut              : terminator.getSuccessorOperand(1, index);
4175d7231d8SStephan Herhut }
4185d7231d8SStephan Herhut 
4195e7959a3SAlex Zinenko void ModuleTranslation::connectPHINodes(LLVMFuncOp func) {
4205d7231d8SStephan Herhut   // Skip the first block, it cannot be branched to and its arguments correspond
4215d7231d8SStephan Herhut   // to the arguments of the LLVM function.
4225d7231d8SStephan Herhut   for (auto it = std::next(func.begin()), eit = func.end(); it != eit; ++it) {
4235d7231d8SStephan Herhut     Block *bb = &*it;
4245d7231d8SStephan Herhut     llvm::BasicBlock *llvmBB = blockMapping.lookup(bb);
4255d7231d8SStephan Herhut     auto phis = llvmBB->phis();
4265d7231d8SStephan Herhut     auto numArguments = bb->getNumArguments();
4275d7231d8SStephan Herhut     assert(numArguments == std::distance(phis.begin(), phis.end()));
4285d7231d8SStephan Herhut     for (auto &numberedPhiNode : llvm::enumerate(phis)) {
4295d7231d8SStephan Herhut       auto &phiNode = numberedPhiNode.value();
4305d7231d8SStephan Herhut       unsigned index = numberedPhiNode.index();
4315d7231d8SStephan Herhut       for (auto *pred : bb->getPredecessors()) {
4325d7231d8SStephan Herhut         phiNode.addIncoming(valueMapping.lookup(getPHISourceValue(
4335d7231d8SStephan Herhut                                 bb, pred, numArguments, index)),
4345d7231d8SStephan Herhut                             blockMapping.lookup(pred));
4355d7231d8SStephan Herhut       }
4365d7231d8SStephan Herhut     }
4375d7231d8SStephan Herhut   }
4385d7231d8SStephan Herhut }
4395d7231d8SStephan Herhut 
4405d7231d8SStephan Herhut // TODO(mlir-team): implement an iterative version
4415d7231d8SStephan Herhut static void topologicalSortImpl(llvm::SetVector<Block *> &blocks, Block *b) {
4425d7231d8SStephan Herhut   blocks.insert(b);
4435d7231d8SStephan Herhut   for (Block *bb : b->getSuccessors()) {
4445d7231d8SStephan Herhut     if (blocks.count(bb) == 0)
4455d7231d8SStephan Herhut       topologicalSortImpl(blocks, bb);
4465d7231d8SStephan Herhut   }
4475d7231d8SStephan Herhut }
4485d7231d8SStephan Herhut 
4492666b973SRiver Riddle /// Sort function blocks topologically.
4505e7959a3SAlex Zinenko static llvm::SetVector<Block *> topologicalSort(LLVMFuncOp f) {
4515d7231d8SStephan Herhut   // For each blocks that has not been visited yet (i.e. that has no
4525d7231d8SStephan Herhut   // predecessors), add it to the list and traverse its successors in DFS
4535d7231d8SStephan Herhut   // preorder.
4545d7231d8SStephan Herhut   llvm::SetVector<Block *> blocks;
4555d7231d8SStephan Herhut   for (Block &b : f.getBlocks()) {
4565d7231d8SStephan Herhut     if (blocks.count(&b) == 0)
4575d7231d8SStephan Herhut       topologicalSortImpl(blocks, &b);
4585d7231d8SStephan Herhut   }
4595d7231d8SStephan Herhut   assert(blocks.size() == f.getBlocks().size() && "some blocks are not sorted");
4605d7231d8SStephan Herhut 
4615d7231d8SStephan Herhut   return blocks;
4625d7231d8SStephan Herhut }
4635d7231d8SStephan Herhut 
4645e7959a3SAlex Zinenko LogicalResult ModuleTranslation::convertOneFunction(LLVMFuncOp func) {
4655d7231d8SStephan Herhut   // Clear the block and value mappings, they are only relevant within one
4665d7231d8SStephan Herhut   // function.
4675d7231d8SStephan Herhut   blockMapping.clear();
4685d7231d8SStephan Herhut   valueMapping.clear();
469c33862b0SRiver Riddle   llvm::Function *llvmFunc = functionMapping.lookup(func.getName());
4705d7231d8SStephan Herhut   // Add function arguments to the value remapping table.
4715d7231d8SStephan Herhut   // If there was noalias info then we decorate each argument accordingly.
4725d7231d8SStephan Herhut   unsigned int argIdx = 0;
473eeef50b1SFangrui Song   for (auto kvp : llvm::zip(func.getArguments(), llvmFunc->args())) {
4745d7231d8SStephan Herhut     llvm::Argument &llvmArg = std::get<1>(kvp);
475e62a6956SRiver Riddle     BlockArgument mlirArg = std::get<0>(kvp);
4765d7231d8SStephan Herhut 
4775d7231d8SStephan Herhut     if (auto attr = func.getArgAttrOfType<BoolAttr>(argIdx, "llvm.noalias")) {
4785d7231d8SStephan Herhut       // NB: Attribute already verified to be boolean, so check if we can indeed
4795d7231d8SStephan Herhut       // attach the attribute to this argument, based on its type.
4802bdf33ccSRiver Riddle       auto argTy = mlirArg.getType().dyn_cast<LLVM::LLVMType>();
481baa1ec22SAlex Zinenko       if (!argTy.getUnderlyingType()->isPointerTy())
482baa1ec22SAlex Zinenko         return func.emitError(
4835d7231d8SStephan Herhut             "llvm.noalias attribute attached to LLVM non-pointer argument");
4845d7231d8SStephan Herhut       if (attr.getValue())
4855d7231d8SStephan Herhut         llvmArg.addAttr(llvm::Attribute::AttrKind::NoAlias);
4865d7231d8SStephan Herhut     }
4875d7231d8SStephan Herhut     valueMapping[mlirArg] = &llvmArg;
4885d7231d8SStephan Herhut     argIdx++;
4895d7231d8SStephan Herhut   }
4905d7231d8SStephan Herhut 
4915d7231d8SStephan Herhut   // First, create all blocks so we can jump to them.
4925d7231d8SStephan Herhut   llvm::LLVMContext &llvmContext = llvmFunc->getContext();
4935d7231d8SStephan Herhut   for (auto &bb : func) {
4945d7231d8SStephan Herhut     auto *llvmBB = llvm::BasicBlock::Create(llvmContext);
4955d7231d8SStephan Herhut     llvmBB->insertInto(llvmFunc);
4965d7231d8SStephan Herhut     blockMapping[&bb] = llvmBB;
4975d7231d8SStephan Herhut   }
4985d7231d8SStephan Herhut 
4995d7231d8SStephan Herhut   // Then, convert blocks one by one in topological order to ensure defs are
5005d7231d8SStephan Herhut   // converted before uses.
5015d7231d8SStephan Herhut   auto blocks = topologicalSort(func);
5025d7231d8SStephan Herhut   for (auto indexedBB : llvm::enumerate(blocks)) {
5035d7231d8SStephan Herhut     auto *bb = indexedBB.value();
504baa1ec22SAlex Zinenko     if (failed(convertBlock(*bb, /*ignoreArguments=*/indexedBB.index() == 0)))
505baa1ec22SAlex Zinenko       return failure();
5065d7231d8SStephan Herhut   }
5075d7231d8SStephan Herhut 
5085d7231d8SStephan Herhut   // Finally, after all blocks have been traversed and values mapped, connect
5095d7231d8SStephan Herhut   // the PHI nodes to the results of preceding blocks.
5105d7231d8SStephan Herhut   connectPHINodes(func);
511baa1ec22SAlex Zinenko   return success();
5125d7231d8SStephan Herhut }
5135d7231d8SStephan Herhut 
51444fc7d72STres Popp LogicalResult ModuleTranslation::checkSupportedModuleOps(Operation *m) {
51544fc7d72STres Popp   for (Operation &o : getModuleBody(m).getOperations())
5164dde19f0SAlex Zinenko     if (!isa<LLVM::LLVMFuncOp>(&o) && !isa<LLVM::GlobalOp>(&o) &&
51744fc7d72STres Popp         !o.isKnownTerminator())
5184dde19f0SAlex Zinenko       return o.emitOpError("unsupported module-level operation");
5194dde19f0SAlex Zinenko   return success();
5204dde19f0SAlex Zinenko }
5214dde19f0SAlex Zinenko 
522baa1ec22SAlex Zinenko LogicalResult ModuleTranslation::convertFunctions() {
5235d7231d8SStephan Herhut   // Declare all functions first because there may be function calls that form a
5245d7231d8SStephan Herhut   // call graph with cycles.
52544fc7d72STres Popp   for (auto function : getModuleBody(mlirModule).getOps<LLVMFuncOp>()) {
5265e7959a3SAlex Zinenko     llvm::FunctionCallee llvmFuncCst = llvmModule->getOrInsertFunction(
5275e7959a3SAlex Zinenko         function.getName(),
5284562e389SRiver Riddle         cast<llvm::FunctionType>(function.getType().getUnderlyingType()));
5295d7231d8SStephan Herhut     assert(isa<llvm::Function>(llvmFuncCst.getCallee()));
530c33862b0SRiver Riddle     functionMapping[function.getName()] =
5315d7231d8SStephan Herhut         cast<llvm::Function>(llvmFuncCst.getCallee());
5325d7231d8SStephan Herhut   }
5335d7231d8SStephan Herhut 
5345d7231d8SStephan Herhut   // Convert functions.
53544fc7d72STres Popp   for (auto function : getModuleBody(mlirModule).getOps<LLVMFuncOp>()) {
5365d7231d8SStephan Herhut     // Ignore external functions.
5375d7231d8SStephan Herhut     if (function.isExternal())
5385d7231d8SStephan Herhut       continue;
5395d7231d8SStephan Herhut 
540baa1ec22SAlex Zinenko     if (failed(convertOneFunction(function)))
541baa1ec22SAlex Zinenko       return failure();
5425d7231d8SStephan Herhut   }
5435d7231d8SStephan Herhut 
544baa1ec22SAlex Zinenko   return success();
5455d7231d8SStephan Herhut }
5465d7231d8SStephan Herhut 
547efadb6b8SAlex Zinenko /// A helper to look up remapped operands in the value remapping table.`
548efadb6b8SAlex Zinenko SmallVector<llvm::Value *, 8>
549efadb6b8SAlex Zinenko ModuleTranslation::lookupValues(ValueRange values) {
550efadb6b8SAlex Zinenko   SmallVector<llvm::Value *, 8> remapped;
551efadb6b8SAlex Zinenko   remapped.reserve(values.size());
552e62a6956SRiver Riddle   for (Value v : values)
553efadb6b8SAlex Zinenko     remapped.push_back(valueMapping.lookup(v));
554efadb6b8SAlex Zinenko   return remapped;
555efadb6b8SAlex Zinenko }
556efadb6b8SAlex Zinenko 
55744fc7d72STres Popp std::unique_ptr<llvm::Module>
55844fc7d72STres Popp ModuleTranslation::prepareLLVMModule(Operation *m) {
55944fc7d72STres Popp   auto *dialect = m->getContext()->getRegisteredDialect<LLVM::LLVMDialect>();
5605d7231d8SStephan Herhut   assert(dialect && "LLVM dialect must be registered");
5615d7231d8SStephan Herhut 
562bc5c7378SRiver Riddle   auto llvmModule = llvm::CloneModule(dialect->getLLVMModule());
5635d7231d8SStephan Herhut   if (!llvmModule)
5645d7231d8SStephan Herhut     return nullptr;
5655d7231d8SStephan Herhut 
5665d7231d8SStephan Herhut   llvm::LLVMContext &llvmContext = llvmModule->getContext();
5675d7231d8SStephan Herhut   llvm::IRBuilder<> builder(llvmContext);
5685d7231d8SStephan Herhut 
5695d7231d8SStephan Herhut   // Inject declarations for `malloc` and `free` functions that can be used in
5705d7231d8SStephan Herhut   // memref allocation/deallocation coming from standard ops lowering.
5715d7231d8SStephan Herhut   llvmModule->getOrInsertFunction("malloc", builder.getInt8PtrTy(),
5725d7231d8SStephan Herhut                                   builder.getInt64Ty());
5735d7231d8SStephan Herhut   llvmModule->getOrInsertFunction("free", builder.getVoidTy(),
5745d7231d8SStephan Herhut                                   builder.getInt8PtrTy());
5755d7231d8SStephan Herhut 
5765d7231d8SStephan Herhut   return llvmModule;
5775d7231d8SStephan Herhut }
578