1cde4d5a6SJacques Pienaar //===- ModuleTranslation.cpp - MLIR to LLVM conversion --------------------===// 25d7231d8SStephan Herhut // 330857107SMehdi Amini // Part of the LLVM 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 16c33d6970SRiver Riddle #include "DebugTranslation.h" 17ba0fa925SRiver Riddle #include "mlir/Dialect/LLVMIR/LLVMDialect.h" 1892a295ebSKiran Chandramohan #include "mlir/Dialect/OpenMP/OpenMPDialect.h" 195d7231d8SStephan Herhut #include "mlir/IR/Attributes.h" 2065fcddffSRiver Riddle #include "mlir/IR/BuiltinOps.h" 2109f7a55fSRiver Riddle #include "mlir/IR/BuiltinTypes.h" 22d4568ed7SGeorge Mitenkov #include "mlir/IR/RegionGraphTraits.h" 235d7231d8SStephan Herhut #include "mlir/Support/LLVM.h" 24b77bac05SAlex Zinenko #include "mlir/Target/LLVMIR/LLVMTranslationInterface.h" 25ec1f4e7cSAlex Zinenko #include "mlir/Target/LLVMIR/TypeTranslation.h" 26ebf190fcSRiver Riddle #include "llvm/ADT/TypeSwitch.h" 275d7231d8SStephan Herhut 28d4568ed7SGeorge Mitenkov #include "llvm/ADT/PostOrderIterator.h" 295d7231d8SStephan Herhut #include "llvm/ADT/SetVector.h" 3092a295ebSKiran Chandramohan #include "llvm/Frontend/OpenMP/OMPIRBuilder.h" 315d7231d8SStephan Herhut #include "llvm/IR/BasicBlock.h" 32d9067dcaSKiran Chandramohan #include "llvm/IR/CFG.h" 335d7231d8SStephan Herhut #include "llvm/IR/Constants.h" 345d7231d8SStephan Herhut #include "llvm/IR/DerivedTypes.h" 355d7231d8SStephan Herhut #include "llvm/IR/IRBuilder.h" 36047400edSNicolas Vasilache #include "llvm/IR/InlineAsm.h" 375d7231d8SStephan Herhut #include "llvm/IR/LLVMContext.h" 3899d03f03SGeorge Mitenkov #include "llvm/IR/MDBuilder.h" 395d7231d8SStephan Herhut #include "llvm/IR/Module.h" 40d9067dcaSKiran Chandramohan #include "llvm/Transforms/Utils/BasicBlockUtils.h" 415d7231d8SStephan Herhut #include "llvm/Transforms/Utils/Cloning.h" 425d7231d8SStephan Herhut 432666b973SRiver Riddle using namespace mlir; 442666b973SRiver Riddle using namespace mlir::LLVM; 45c33d6970SRiver Riddle using namespace mlir::LLVM::detail; 465d7231d8SStephan Herhut 47eb67bd78SAlex Zinenko #include "mlir/Dialect/LLVMIR/LLVMConversionEnumsToLLVM.inc" 48eb67bd78SAlex Zinenko 49a922e231SAlex Zinenko /// Builds a constant of a sequential LLVM type `type`, potentially containing 50a922e231SAlex Zinenko /// other sequential types recursively, from the individual constant values 51a922e231SAlex Zinenko /// provided in `constants`. `shape` contains the number of elements in nested 52a922e231SAlex Zinenko /// sequential types. Reports errors at `loc` and returns nullptr on error. 53a4a42160SAlex Zinenko static llvm::Constant * 54a4a42160SAlex Zinenko buildSequentialConstant(ArrayRef<llvm::Constant *> &constants, 55a4a42160SAlex Zinenko ArrayRef<int64_t> shape, llvm::Type *type, 56a4a42160SAlex Zinenko Location loc) { 57a4a42160SAlex Zinenko if (shape.empty()) { 58a4a42160SAlex Zinenko llvm::Constant *result = constants.front(); 59a4a42160SAlex Zinenko constants = constants.drop_front(); 60a4a42160SAlex Zinenko return result; 61a4a42160SAlex Zinenko } 62a4a42160SAlex Zinenko 6368b03aeeSEli Friedman llvm::Type *elementType; 6468b03aeeSEli Friedman if (auto *arrayTy = dyn_cast<llvm::ArrayType>(type)) { 6568b03aeeSEli Friedman elementType = arrayTy->getElementType(); 6668b03aeeSEli Friedman } else if (auto *vectorTy = dyn_cast<llvm::VectorType>(type)) { 6768b03aeeSEli Friedman elementType = vectorTy->getElementType(); 6868b03aeeSEli Friedman } else { 69a4a42160SAlex Zinenko emitError(loc) << "expected sequential LLVM types wrapping a scalar"; 70a4a42160SAlex Zinenko return nullptr; 71a4a42160SAlex Zinenko } 72a4a42160SAlex Zinenko 73a4a42160SAlex Zinenko SmallVector<llvm::Constant *, 8> nested; 74a4a42160SAlex Zinenko nested.reserve(shape.front()); 75a4a42160SAlex Zinenko for (int64_t i = 0; i < shape.front(); ++i) { 76a4a42160SAlex Zinenko nested.push_back(buildSequentialConstant(constants, shape.drop_front(), 77a4a42160SAlex Zinenko elementType, loc)); 78a4a42160SAlex Zinenko if (!nested.back()) 79a4a42160SAlex Zinenko return nullptr; 80a4a42160SAlex Zinenko } 81a4a42160SAlex Zinenko 82a4a42160SAlex Zinenko if (shape.size() == 1 && type->isVectorTy()) 83a4a42160SAlex Zinenko return llvm::ConstantVector::get(nested); 84a4a42160SAlex Zinenko return llvm::ConstantArray::get( 85a4a42160SAlex Zinenko llvm::ArrayType::get(elementType, shape.front()), nested); 86a4a42160SAlex Zinenko } 87a4a42160SAlex Zinenko 88fc817b09SKazuaki Ishizaki /// Returns the first non-sequential type nested in sequential types. 89a4a42160SAlex Zinenko static llvm::Type *getInnermostElementType(llvm::Type *type) { 9068b03aeeSEli Friedman do { 9168b03aeeSEli Friedman if (auto *arrayTy = dyn_cast<llvm::ArrayType>(type)) { 9268b03aeeSEli Friedman type = arrayTy->getElementType(); 9368b03aeeSEli Friedman } else if (auto *vectorTy = dyn_cast<llvm::VectorType>(type)) { 9468b03aeeSEli Friedman type = vectorTy->getElementType(); 9568b03aeeSEli Friedman } else { 96a4a42160SAlex Zinenko return type; 97a4a42160SAlex Zinenko } 980881a4f1SAlex Zinenko } while (true); 9968b03aeeSEli Friedman } 100a4a42160SAlex Zinenko 1012666b973SRiver Riddle /// Create an LLVM IR constant of `llvmType` from the MLIR attribute `attr`. 1022666b973SRiver Riddle /// This currently supports integer, floating point, splat and dense element 1032666b973SRiver Riddle /// attributes and combinations thereof. In case of error, report it to `loc` 1042666b973SRiver Riddle /// and return nullptr. 1055d7231d8SStephan Herhut llvm::Constant *ModuleTranslation::getLLVMConstant(llvm::Type *llvmType, 1065d7231d8SStephan Herhut Attribute attr, 1075d7231d8SStephan Herhut Location loc) { 10833a3a91bSChristian Sigg if (!attr) 10933a3a91bSChristian Sigg return llvm::UndefValue::get(llvmType); 110a4a42160SAlex Zinenko if (llvmType->isStructTy()) { 111a4a42160SAlex Zinenko emitError(loc, "struct types are not supported in constants"); 112a4a42160SAlex Zinenko return nullptr; 113a4a42160SAlex Zinenko } 114ac9d742bSStephan Herhut // For integer types, we allow a mismatch in sizes as the index type in 115ac9d742bSStephan Herhut // MLIR might have a different size than the index type in the LLVM module. 1165d7231d8SStephan Herhut if (auto intAttr = attr.dyn_cast<IntegerAttr>()) 117ac9d742bSStephan Herhut return llvm::ConstantInt::get( 118ac9d742bSStephan Herhut llvmType, 119ac9d742bSStephan Herhut intAttr.getValue().sextOrTrunc(llvmType->getIntegerBitWidth())); 1205d7231d8SStephan Herhut if (auto floatAttr = attr.dyn_cast<FloatAttr>()) 1215d7231d8SStephan Herhut return llvm::ConstantFP::get(llvmType, floatAttr.getValue()); 1229b9c647cSRiver Riddle if (auto funcAttr = attr.dyn_cast<FlatSymbolRefAttr>()) 1230881a4f1SAlex Zinenko return llvm::ConstantExpr::getBitCast(lookupFunction(funcAttr.getValue()), 1240881a4f1SAlex Zinenko llvmType); 1255d7231d8SStephan Herhut if (auto splatAttr = attr.dyn_cast<SplatElementsAttr>()) { 12668b03aeeSEli Friedman llvm::Type *elementType; 12768b03aeeSEli Friedman uint64_t numElements; 12868b03aeeSEli Friedman if (auto *arrayTy = dyn_cast<llvm::ArrayType>(llvmType)) { 12968b03aeeSEli Friedman elementType = arrayTy->getElementType(); 13068b03aeeSEli Friedman numElements = arrayTy->getNumElements(); 13168b03aeeSEli Friedman } else { 1325cba1c63SChristopher Tetreault auto *vectorTy = cast<llvm::FixedVectorType>(llvmType); 13368b03aeeSEli Friedman elementType = vectorTy->getElementType(); 13468b03aeeSEli Friedman numElements = vectorTy->getNumElements(); 13568b03aeeSEli Friedman } 136d6ea8ff0SAlex Zinenko // Splat value is a scalar. Extract it only if the element type is not 137d6ea8ff0SAlex Zinenko // another sequence type. The recursion terminates because each step removes 138d6ea8ff0SAlex Zinenko // one outer sequential type. 13968b03aeeSEli Friedman bool elementTypeSequential = 140d891d738SRahul Joshi isa<llvm::ArrayType, llvm::VectorType>(elementType); 141d6ea8ff0SAlex Zinenko llvm::Constant *child = getLLVMConstant( 142d6ea8ff0SAlex Zinenko elementType, 14368b03aeeSEli Friedman elementTypeSequential ? splatAttr : splatAttr.getSplatValue(), loc); 144a4a42160SAlex Zinenko if (!child) 145a4a42160SAlex Zinenko return nullptr; 1462f13df13SMLIR Team if (llvmType->isVectorTy()) 147396a42d9SRiver Riddle return llvm::ConstantVector::getSplat( 1480f95e731SAlex Zinenko llvm::ElementCount::get(numElements, /*Scalable=*/false), child); 1492f13df13SMLIR Team if (llvmType->isArrayTy()) { 150ac9d742bSStephan Herhut auto *arrayType = llvm::ArrayType::get(elementType, numElements); 1512f13df13SMLIR Team SmallVector<llvm::Constant *, 8> constants(numElements, child); 1522f13df13SMLIR Team return llvm::ConstantArray::get(arrayType, constants); 1532f13df13SMLIR Team } 1545d7231d8SStephan Herhut } 155a4a42160SAlex Zinenko 156d906f84bSRiver Riddle if (auto elementsAttr = attr.dyn_cast<ElementsAttr>()) { 157a4a42160SAlex Zinenko assert(elementsAttr.getType().hasStaticShape()); 158a4a42160SAlex Zinenko assert(elementsAttr.getNumElements() != 0 && 159a4a42160SAlex Zinenko "unexpected empty elements attribute"); 160a4a42160SAlex Zinenko assert(!elementsAttr.getType().getShape().empty() && 161a4a42160SAlex Zinenko "unexpected empty elements attribute shape"); 162a4a42160SAlex Zinenko 1635d7231d8SStephan Herhut SmallVector<llvm::Constant *, 8> constants; 164a4a42160SAlex Zinenko constants.reserve(elementsAttr.getNumElements()); 165a4a42160SAlex Zinenko llvm::Type *innermostType = getInnermostElementType(llvmType); 166d906f84bSRiver Riddle for (auto n : elementsAttr.getValues<Attribute>()) { 167a4a42160SAlex Zinenko constants.push_back(getLLVMConstant(innermostType, n, loc)); 1685d7231d8SStephan Herhut if (!constants.back()) 1695d7231d8SStephan Herhut return nullptr; 1705d7231d8SStephan Herhut } 171a4a42160SAlex Zinenko ArrayRef<llvm::Constant *> constantsRef = constants; 172a4a42160SAlex Zinenko llvm::Constant *result = buildSequentialConstant( 173a4a42160SAlex Zinenko constantsRef, elementsAttr.getType().getShape(), llvmType, loc); 174a4a42160SAlex Zinenko assert(constantsRef.empty() && "did not consume all elemental constants"); 175a4a42160SAlex Zinenko return result; 1762f13df13SMLIR Team } 177a4a42160SAlex Zinenko 178cb348dffSStephan Herhut if (auto stringAttr = attr.dyn_cast<StringAttr>()) { 179cb348dffSStephan Herhut return llvm::ConstantDataArray::get( 180cb348dffSStephan Herhut llvmModule->getContext(), ArrayRef<char>{stringAttr.getValue().data(), 181cb348dffSStephan Herhut stringAttr.getValue().size()}); 182cb348dffSStephan Herhut } 183a4c3a645SRiver Riddle emitError(loc, "unsupported constant value"); 1845d7231d8SStephan Herhut return nullptr; 1855d7231d8SStephan Herhut } 1865d7231d8SStephan Herhut 187c33d6970SRiver Riddle ModuleTranslation::ModuleTranslation(Operation *module, 188c33d6970SRiver Riddle std::unique_ptr<llvm::Module> llvmModule) 189c33d6970SRiver Riddle : mlirModule(module), llvmModule(std::move(llvmModule)), 190c33d6970SRiver Riddle debugTranslation( 19192a295ebSKiran Chandramohan std::make_unique<DebugTranslation>(module, *this->llvmModule)), 1929f9f89d4SMehdi Amini ompDialect(module->getContext()->getLoadedDialect("omp")), 193b77bac05SAlex Zinenko typeTranslator(this->llvmModule->getContext()), 194b77bac05SAlex Zinenko iface(module->getContext()) { 195c33d6970SRiver Riddle assert(satisfiesLLVMModule(mlirModule) && 196c33d6970SRiver Riddle "mlirModule should honor LLVM's module semantics."); 197c33d6970SRiver Riddle } 198d9067dcaSKiran Chandramohan ModuleTranslation::~ModuleTranslation() { 199d9067dcaSKiran Chandramohan if (ompBuilder) 200d9067dcaSKiran Chandramohan ompBuilder->finalize(); 201d9067dcaSKiran Chandramohan } 202d9067dcaSKiran Chandramohan 203d9067dcaSKiran Chandramohan /// Get the SSA value passed to the current block from the terminator operation 204d9067dcaSKiran Chandramohan /// of its predecessor. 205d9067dcaSKiran Chandramohan static Value getPHISourceValue(Block *current, Block *pred, 206d9067dcaSKiran Chandramohan unsigned numArguments, unsigned index) { 207d9067dcaSKiran Chandramohan Operation &terminator = *pred->getTerminator(); 208d9067dcaSKiran Chandramohan if (isa<LLVM::BrOp>(terminator)) 209d9067dcaSKiran Chandramohan return terminator.getOperand(index); 210d9067dcaSKiran Chandramohan 21114f24155SBrian Gesiak SuccessorRange successors = terminator.getSuccessors(); 21214f24155SBrian Gesiak assert(std::adjacent_find(successors.begin(), successors.end()) == 21314f24155SBrian Gesiak successors.end() && 21414f24155SBrian Gesiak "successors with arguments in LLVM branches must be different blocks"); 21558f2b765SChristian Sigg (void)successors; 216d9067dcaSKiran Chandramohan 21714f24155SBrian Gesiak // For instructions that branch based on a condition value, we need to take 21814f24155SBrian Gesiak // the operands for the branch that was taken. 21914f24155SBrian Gesiak if (auto condBranchOp = dyn_cast<LLVM::CondBrOp>(terminator)) { 22014f24155SBrian Gesiak // For conditional branches, we take the operands from either the "true" or 22114f24155SBrian Gesiak // the "false" branch. 222d9067dcaSKiran Chandramohan return condBranchOp.getSuccessor(0) == current 223d9067dcaSKiran Chandramohan ? condBranchOp.trueDestOperands()[index] 224d9067dcaSKiran Chandramohan : condBranchOp.falseDestOperands()[index]; 2250881a4f1SAlex Zinenko } 2260881a4f1SAlex Zinenko 2270881a4f1SAlex Zinenko if (auto switchOp = dyn_cast<LLVM::SwitchOp>(terminator)) { 22814f24155SBrian Gesiak // For switches, we take the operands from either the default case, or from 22914f24155SBrian Gesiak // the case branch that was taken. 23014f24155SBrian Gesiak if (switchOp.defaultDestination() == current) 23114f24155SBrian Gesiak return switchOp.defaultOperands()[index]; 23214f24155SBrian Gesiak for (auto i : llvm::enumerate(switchOp.caseDestinations())) 23314f24155SBrian Gesiak if (i.value() == current) 23414f24155SBrian Gesiak return switchOp.getCaseOperands(i.index())[index]; 23514f24155SBrian Gesiak } 23614f24155SBrian Gesiak 23714f24155SBrian Gesiak llvm_unreachable("only branch or switch operations can be terminators of a " 23814f24155SBrian Gesiak "block that has successors"); 239d9067dcaSKiran Chandramohan } 240d9067dcaSKiran Chandramohan 241d9067dcaSKiran Chandramohan /// Connect the PHI nodes to the results of preceding blocks. 242*66900b3eSAlex Zinenko void mlir::LLVM::detail::connectPHINodes(Region ®ion, 243*66900b3eSAlex Zinenko const ModuleTranslation &state) { 244d9067dcaSKiran Chandramohan // Skip the first block, it cannot be branched to and its arguments correspond 245d9067dcaSKiran Chandramohan // to the arguments of the LLVM function. 246*66900b3eSAlex Zinenko for (auto it = std::next(region.begin()), eit = region.end(); it != eit; 247*66900b3eSAlex Zinenko ++it) { 248d9067dcaSKiran Chandramohan Block *bb = &*it; 2490881a4f1SAlex Zinenko llvm::BasicBlock *llvmBB = state.lookupBlock(bb); 250d9067dcaSKiran Chandramohan auto phis = llvmBB->phis(); 251d9067dcaSKiran Chandramohan auto numArguments = bb->getNumArguments(); 252d9067dcaSKiran Chandramohan assert(numArguments == std::distance(phis.begin(), phis.end())); 253d9067dcaSKiran Chandramohan for (auto &numberedPhiNode : llvm::enumerate(phis)) { 254d9067dcaSKiran Chandramohan auto &phiNode = numberedPhiNode.value(); 255d9067dcaSKiran Chandramohan unsigned index = numberedPhiNode.index(); 256d9067dcaSKiran Chandramohan for (auto *pred : bb->getPredecessors()) { 257db884dafSAlex Zinenko // Find the LLVM IR block that contains the converted terminator 258db884dafSAlex Zinenko // instruction and use it in the PHI node. Note that this block is not 2590881a4f1SAlex Zinenko // necessarily the same as state.lookupBlock(pred), some operations 260db884dafSAlex Zinenko // (in particular, OpenMP operations using OpenMPIRBuilder) may have 261db884dafSAlex Zinenko // split the blocks. 262db884dafSAlex Zinenko llvm::Instruction *terminator = 2630881a4f1SAlex Zinenko state.lookupBranch(pred->getTerminator()); 264db884dafSAlex Zinenko assert(terminator && "missing the mapping for a terminator"); 2650881a4f1SAlex Zinenko phiNode.addIncoming( 2660881a4f1SAlex Zinenko state.lookupValue(getPHISourceValue(bb, pred, numArguments, index)), 267db884dafSAlex Zinenko terminator->getParent()); 268d9067dcaSKiran Chandramohan } 269d9067dcaSKiran Chandramohan } 270d9067dcaSKiran Chandramohan } 271d9067dcaSKiran Chandramohan } 272d9067dcaSKiran Chandramohan 273d9067dcaSKiran Chandramohan /// Sort function blocks topologically. 274*66900b3eSAlex Zinenko llvm::SetVector<Block *> 275*66900b3eSAlex Zinenko mlir::LLVM::detail::getTopologicallySortedBlocks(Region ®ion) { 276d4568ed7SGeorge Mitenkov // For each block that has not been visited yet (i.e. that has no 277d4568ed7SGeorge Mitenkov // predecessors), add it to the list as well as its successors. 278d9067dcaSKiran Chandramohan llvm::SetVector<Block *> blocks; 279*66900b3eSAlex Zinenko for (Block &b : region) { 280d4568ed7SGeorge Mitenkov if (blocks.count(&b) == 0) { 281d4568ed7SGeorge Mitenkov llvm::ReversePostOrderTraversal<Block *> traversal(&b); 282d4568ed7SGeorge Mitenkov blocks.insert(traversal.begin(), traversal.end()); 283d4568ed7SGeorge Mitenkov } 284d9067dcaSKiran Chandramohan } 285*66900b3eSAlex Zinenko assert(blocks.size() == region.getBlocks().size() && 286*66900b3eSAlex Zinenko "some blocks are not sorted"); 287d9067dcaSKiran Chandramohan 288d9067dcaSKiran Chandramohan return blocks; 289d9067dcaSKiran Chandramohan } 290d9067dcaSKiran Chandramohan 2912666b973SRiver Riddle /// Given a single MLIR operation, create the corresponding LLVM IR operation 2922666b973SRiver Riddle /// using the `builder`. LLVM IR Builder does not have a generic interface so 2932666b973SRiver Riddle /// this has to be a long chain of `if`s calling different functions with a 2942666b973SRiver Riddle /// different number of arguments. 295baa1ec22SAlex Zinenko LogicalResult ModuleTranslation::convertOperation(Operation &opInst, 2965d7231d8SStephan Herhut llvm::IRBuilder<> &builder) { 297b77bac05SAlex Zinenko if (succeeded(iface.convertOperation(&opInst, builder, *this))) 298baa1ec22SAlex Zinenko return success(); 2992dd38b09SAlex Zinenko 300baa1ec22SAlex Zinenko return opInst.emitError("unsupported or non-LLVM operation: ") 301baa1ec22SAlex Zinenko << opInst.getName(); 3025d7231d8SStephan Herhut } 3035d7231d8SStephan Herhut 3042666b973SRiver Riddle /// Convert block to LLVM IR. Unless `ignoreArguments` is set, emit PHI nodes 3052666b973SRiver Riddle /// to define values corresponding to the MLIR block arguments. These nodes 30610164a2eSAlex Zinenko /// are not connected to the source basic blocks, which may not exist yet. Uses 30710164a2eSAlex Zinenko /// `builder` to construct the LLVM IR. Expects the LLVM IR basic block to have 30810164a2eSAlex Zinenko /// been created for `bb` and included in the block mapping. Inserts new 30910164a2eSAlex Zinenko /// instructions at the end of the block and leaves `builder` in a state 31010164a2eSAlex Zinenko /// suitable for further insertion into the end of the block. 31110164a2eSAlex Zinenko LogicalResult ModuleTranslation::convertBlock(Block &bb, bool ignoreArguments, 31210164a2eSAlex Zinenko llvm::IRBuilder<> &builder) { 3130881a4f1SAlex Zinenko builder.SetInsertPoint(lookupBlock(&bb)); 314c33d6970SRiver Riddle auto *subprogram = builder.GetInsertBlock()->getParent()->getSubprogram(); 3155d7231d8SStephan Herhut 3165d7231d8SStephan Herhut // Before traversing operations, make block arguments available through 3175d7231d8SStephan Herhut // value remapping and PHI nodes, but do not add incoming edges for the PHI 3185d7231d8SStephan Herhut // nodes just yet: those values may be defined by this or following blocks. 3195d7231d8SStephan Herhut // This step is omitted if "ignoreArguments" is set. The arguments of the 3205d7231d8SStephan Herhut // first block have been already made available through the remapping of 3215d7231d8SStephan Herhut // LLVM function arguments. 3225d7231d8SStephan Herhut if (!ignoreArguments) { 3235d7231d8SStephan Herhut auto predecessors = bb.getPredecessors(); 3245d7231d8SStephan Herhut unsigned numPredecessors = 3255d7231d8SStephan Herhut std::distance(predecessors.begin(), predecessors.end()); 32635807bc4SRiver Riddle for (auto arg : bb.getArguments()) { 327c69c9e0fSAlex Zinenko auto wrappedType = arg.getType(); 328c69c9e0fSAlex Zinenko if (!isCompatibleType(wrappedType)) 329baa1ec22SAlex Zinenko return emitError(bb.front().getLoc(), 330a4c3a645SRiver Riddle "block argument does not have an LLVM type"); 331aec38c61SAlex Zinenko llvm::Type *type = convertType(wrappedType); 3325d7231d8SStephan Herhut llvm::PHINode *phi = builder.CreatePHI(type, numPredecessors); 3330881a4f1SAlex Zinenko mapValue(arg, phi); 3345d7231d8SStephan Herhut } 3355d7231d8SStephan Herhut } 3365d7231d8SStephan Herhut 3375d7231d8SStephan Herhut // Traverse operations. 3385d7231d8SStephan Herhut for (auto &op : bb) { 339c33d6970SRiver Riddle // Set the current debug location within the builder. 340c33d6970SRiver Riddle builder.SetCurrentDebugLocation( 341c33d6970SRiver Riddle debugTranslation->translateLoc(op.getLoc(), subprogram)); 342c33d6970SRiver Riddle 343baa1ec22SAlex Zinenko if (failed(convertOperation(op, builder))) 344baa1ec22SAlex Zinenko return failure(); 3455d7231d8SStephan Herhut } 3465d7231d8SStephan Herhut 347baa1ec22SAlex Zinenko return success(); 3485d7231d8SStephan Herhut } 3495d7231d8SStephan Herhut 3502666b973SRiver Riddle /// Create named global variables that correspond to llvm.mlir.global 3512666b973SRiver Riddle /// definitions. 352efa2d533SAlex Zinenko LogicalResult ModuleTranslation::convertGlobals() { 35344fc7d72STres Popp for (auto op : getModuleBody(mlirModule).getOps<LLVM::GlobalOp>()) { 354aec38c61SAlex Zinenko llvm::Type *type = convertType(op.getType()); 355250a11aeSJames Molloy llvm::Constant *cst = llvm::UndefValue::get(type); 356250a11aeSJames Molloy if (op.getValueOrNull()) { 35768451df2SAlex Zinenko // String attributes are treated separately because they cannot appear as 35868451df2SAlex Zinenko // in-function constants and are thus not supported by getLLVMConstant. 35933a3a91bSChristian Sigg if (auto strAttr = op.getValueOrNull().dyn_cast_or_null<StringAttr>()) { 3602dd38b09SAlex Zinenko cst = llvm::ConstantDataArray::getString( 36168451df2SAlex Zinenko llvmModule->getContext(), strAttr.getValue(), /*AddNull=*/false); 3622dd38b09SAlex Zinenko type = cst->getType(); 363efa2d533SAlex Zinenko } else if (!(cst = getLLVMConstant(type, op.getValueOrNull(), 364efa2d533SAlex Zinenko op.getLoc()))) { 365efa2d533SAlex Zinenko return failure(); 36668451df2SAlex Zinenko } 367250a11aeSJames Molloy } else if (Block *initializer = op.getInitializerBlock()) { 368250a11aeSJames Molloy llvm::IRBuilder<> builder(llvmModule->getContext()); 369250a11aeSJames Molloy for (auto &op : initializer->without_terminator()) { 370250a11aeSJames Molloy if (failed(convertOperation(op, builder)) || 3710881a4f1SAlex Zinenko !isa<llvm::Constant>(lookupValue(op.getResult(0)))) 372efa2d533SAlex Zinenko return emitError(op.getLoc(), "unemittable constant value"); 373250a11aeSJames Molloy } 374250a11aeSJames Molloy ReturnOp ret = cast<ReturnOp>(initializer->getTerminator()); 3750881a4f1SAlex Zinenko cst = cast<llvm::Constant>(lookupValue(ret.getOperand(0))); 376250a11aeSJames Molloy } 37768451df2SAlex Zinenko 378eb67bd78SAlex Zinenko auto linkage = convertLinkageToLLVM(op.linkage()); 379d5e627f8SAlex Zinenko bool anyExternalLinkage = 3807b5d4669SEric Schweitz ((linkage == llvm::GlobalVariable::ExternalLinkage && 3817b5d4669SEric Schweitz isa<llvm::UndefValue>(cst)) || 382d5e627f8SAlex Zinenko linkage == llvm::GlobalVariable::ExternalWeakLinkage); 383431bb8b3SRiver Riddle auto addrSpace = op.addr_space(); 384e79bfefbSMLIR Team auto *var = new llvm::GlobalVariable( 385d5e627f8SAlex Zinenko *llvmModule, type, op.constant(), linkage, 386d5e627f8SAlex Zinenko anyExternalLinkage ? nullptr : cst, op.sym_name(), 387d5e627f8SAlex Zinenko /*InsertBefore=*/nullptr, llvm::GlobalValue::NotThreadLocal, addrSpace); 388e79bfefbSMLIR Team 3892dd38b09SAlex Zinenko globalsMapping.try_emplace(op, var); 390b9ff2dd8SAlex Zinenko } 391efa2d533SAlex Zinenko 392efa2d533SAlex Zinenko return success(); 393b9ff2dd8SAlex Zinenko } 394b9ff2dd8SAlex Zinenko 3950a2131b7SAlex Zinenko /// Attempts to add an attribute identified by `key`, optionally with the given 3960a2131b7SAlex Zinenko /// `value` to LLVM function `llvmFunc`. Reports errors at `loc` if any. If the 3970a2131b7SAlex Zinenko /// attribute has a kind known to LLVM IR, create the attribute of this kind, 3980a2131b7SAlex Zinenko /// otherwise keep it as a string attribute. Performs additional checks for 3990a2131b7SAlex Zinenko /// attributes known to have or not have a value in order to avoid assertions 4000a2131b7SAlex Zinenko /// inside LLVM upon construction. 4010a2131b7SAlex Zinenko static LogicalResult checkedAddLLVMFnAttribute(Location loc, 4020a2131b7SAlex Zinenko llvm::Function *llvmFunc, 4030a2131b7SAlex Zinenko StringRef key, 4040a2131b7SAlex Zinenko StringRef value = StringRef()) { 4050a2131b7SAlex Zinenko auto kind = llvm::Attribute::getAttrKindFromName(key); 4060a2131b7SAlex Zinenko if (kind == llvm::Attribute::None) { 4070a2131b7SAlex Zinenko llvmFunc->addFnAttr(key, value); 4080a2131b7SAlex Zinenko return success(); 4090a2131b7SAlex Zinenko } 4100a2131b7SAlex Zinenko 4110a2131b7SAlex Zinenko if (llvm::Attribute::doesAttrKindHaveArgument(kind)) { 4120a2131b7SAlex Zinenko if (value.empty()) 4130a2131b7SAlex Zinenko return emitError(loc) << "LLVM attribute '" << key << "' expects a value"; 4140a2131b7SAlex Zinenko 4150a2131b7SAlex Zinenko int result; 4160a2131b7SAlex Zinenko if (!value.getAsInteger(/*Radix=*/0, result)) 4170a2131b7SAlex Zinenko llvmFunc->addFnAttr( 4180a2131b7SAlex Zinenko llvm::Attribute::get(llvmFunc->getContext(), kind, result)); 4190a2131b7SAlex Zinenko else 4200a2131b7SAlex Zinenko llvmFunc->addFnAttr(key, value); 4210a2131b7SAlex Zinenko return success(); 4220a2131b7SAlex Zinenko } 4230a2131b7SAlex Zinenko 4240a2131b7SAlex Zinenko if (!value.empty()) 4250a2131b7SAlex Zinenko return emitError(loc) << "LLVM attribute '" << key 4260a2131b7SAlex Zinenko << "' does not expect a value, found '" << value 4270a2131b7SAlex Zinenko << "'"; 4280a2131b7SAlex Zinenko 4290a2131b7SAlex Zinenko llvmFunc->addFnAttr(kind); 4300a2131b7SAlex Zinenko return success(); 4310a2131b7SAlex Zinenko } 4320a2131b7SAlex Zinenko 4330a2131b7SAlex Zinenko /// Attaches the attributes listed in the given array attribute to `llvmFunc`. 4340a2131b7SAlex Zinenko /// Reports error to `loc` if any and returns immediately. Expects `attributes` 4350a2131b7SAlex Zinenko /// to be an array attribute containing either string attributes, treated as 4360a2131b7SAlex Zinenko /// value-less LLVM attributes, or array attributes containing two string 4370a2131b7SAlex Zinenko /// attributes, with the first string being the name of the corresponding LLVM 4380a2131b7SAlex Zinenko /// attribute and the second string beings its value. Note that even integer 4390a2131b7SAlex Zinenko /// attributes are expected to have their values expressed as strings. 4400a2131b7SAlex Zinenko static LogicalResult 4410a2131b7SAlex Zinenko forwardPassthroughAttributes(Location loc, Optional<ArrayAttr> attributes, 4420a2131b7SAlex Zinenko llvm::Function *llvmFunc) { 4430a2131b7SAlex Zinenko if (!attributes) 4440a2131b7SAlex Zinenko return success(); 4450a2131b7SAlex Zinenko 4460a2131b7SAlex Zinenko for (Attribute attr : *attributes) { 4470a2131b7SAlex Zinenko if (auto stringAttr = attr.dyn_cast<StringAttr>()) { 4480a2131b7SAlex Zinenko if (failed( 4490a2131b7SAlex Zinenko checkedAddLLVMFnAttribute(loc, llvmFunc, stringAttr.getValue()))) 4500a2131b7SAlex Zinenko return failure(); 4510a2131b7SAlex Zinenko continue; 4520a2131b7SAlex Zinenko } 4530a2131b7SAlex Zinenko 4540a2131b7SAlex Zinenko auto arrayAttr = attr.dyn_cast<ArrayAttr>(); 4550a2131b7SAlex Zinenko if (!arrayAttr || arrayAttr.size() != 2) 4560a2131b7SAlex Zinenko return emitError(loc) 4570a2131b7SAlex Zinenko << "expected 'passthrough' to contain string or array attributes"; 4580a2131b7SAlex Zinenko 4590a2131b7SAlex Zinenko auto keyAttr = arrayAttr[0].dyn_cast<StringAttr>(); 4600a2131b7SAlex Zinenko auto valueAttr = arrayAttr[1].dyn_cast<StringAttr>(); 4610a2131b7SAlex Zinenko if (!keyAttr || !valueAttr) 4620a2131b7SAlex Zinenko return emitError(loc) 4630a2131b7SAlex Zinenko << "expected arrays within 'passthrough' to contain two strings"; 4640a2131b7SAlex Zinenko 4650a2131b7SAlex Zinenko if (failed(checkedAddLLVMFnAttribute(loc, llvmFunc, keyAttr.getValue(), 4660a2131b7SAlex Zinenko valueAttr.getValue()))) 4670a2131b7SAlex Zinenko return failure(); 4680a2131b7SAlex Zinenko } 4690a2131b7SAlex Zinenko return success(); 4700a2131b7SAlex Zinenko } 4710a2131b7SAlex Zinenko 4725e7959a3SAlex Zinenko LogicalResult ModuleTranslation::convertOneFunction(LLVMFuncOp func) { 473db884dafSAlex Zinenko // Clear the block, branch value mappings, they are only relevant within one 4745d7231d8SStephan Herhut // function. 4755d7231d8SStephan Herhut blockMapping.clear(); 4765d7231d8SStephan Herhut valueMapping.clear(); 477db884dafSAlex Zinenko branchMapping.clear(); 4780881a4f1SAlex Zinenko llvm::Function *llvmFunc = lookupFunction(func.getName()); 479c33d6970SRiver Riddle 480c33d6970SRiver Riddle // Translate the debug information for this function. 481c33d6970SRiver Riddle debugTranslation->translate(func, *llvmFunc); 482c33d6970SRiver Riddle 4835d7231d8SStephan Herhut // Add function arguments to the value remapping table. 4845d7231d8SStephan Herhut // If there was noalias info then we decorate each argument accordingly. 4855d7231d8SStephan Herhut unsigned int argIdx = 0; 486eeef50b1SFangrui Song for (auto kvp : llvm::zip(func.getArguments(), llvmFunc->args())) { 4875d7231d8SStephan Herhut llvm::Argument &llvmArg = std::get<1>(kvp); 488e62a6956SRiver Riddle BlockArgument mlirArg = std::get<0>(kvp); 4895d7231d8SStephan Herhut 49067cc5cecSStephan Herhut if (auto attr = func.getArgAttrOfType<BoolAttr>( 49167cc5cecSStephan Herhut argIdx, LLVMDialect::getNoAliasAttrName())) { 4925d7231d8SStephan Herhut // NB: Attribute already verified to be boolean, so check if we can indeed 4935d7231d8SStephan Herhut // attach the attribute to this argument, based on its type. 494c69c9e0fSAlex Zinenko auto argTy = mlirArg.getType(); 4958de43b92SAlex Zinenko if (!argTy.isa<LLVM::LLVMPointerType>()) 496baa1ec22SAlex Zinenko return func.emitError( 4975d7231d8SStephan Herhut "llvm.noalias attribute attached to LLVM non-pointer argument"); 4985d7231d8SStephan Herhut if (attr.getValue()) 4995d7231d8SStephan Herhut llvmArg.addAttr(llvm::Attribute::AttrKind::NoAlias); 5005d7231d8SStephan Herhut } 5012416e28cSStephan Herhut 50267cc5cecSStephan Herhut if (auto attr = func.getArgAttrOfType<IntegerAttr>( 50367cc5cecSStephan Herhut argIdx, LLVMDialect::getAlignAttrName())) { 5042416e28cSStephan Herhut // NB: Attribute already verified to be int, so check if we can indeed 5052416e28cSStephan Herhut // attach the attribute to this argument, based on its type. 506c69c9e0fSAlex Zinenko auto argTy = mlirArg.getType(); 5078de43b92SAlex Zinenko if (!argTy.isa<LLVM::LLVMPointerType>()) 5082416e28cSStephan Herhut return func.emitError( 5092416e28cSStephan Herhut "llvm.align attribute attached to LLVM non-pointer argument"); 5102416e28cSStephan Herhut llvmArg.addAttrs( 5112416e28cSStephan Herhut llvm::AttrBuilder().addAlignmentAttr(llvm::Align(attr.getInt()))); 5122416e28cSStephan Herhut } 5132416e28cSStephan Herhut 51470b841acSEric Schweitz if (auto attr = func.getArgAttrOfType<UnitAttr>(argIdx, "llvm.sret")) { 51570b841acSEric Schweitz auto argTy = mlirArg.getType(); 51670b841acSEric Schweitz if (!argTy.isa<LLVM::LLVMPointerType>()) 51770b841acSEric Schweitz return func.emitError( 51870b841acSEric Schweitz "llvm.sret attribute attached to LLVM non-pointer argument"); 5191d6df1fcSEric Schweitz llvmArg.addAttrs(llvm::AttrBuilder().addStructRetAttr( 5201d6df1fcSEric Schweitz llvmArg.getType()->getPointerElementType())); 52170b841acSEric Schweitz } 52270b841acSEric Schweitz 52370b841acSEric Schweitz if (auto attr = func.getArgAttrOfType<UnitAttr>(argIdx, "llvm.byval")) { 52470b841acSEric Schweitz auto argTy = mlirArg.getType(); 52570b841acSEric Schweitz if (!argTy.isa<LLVM::LLVMPointerType>()) 52670b841acSEric Schweitz return func.emitError( 52770b841acSEric Schweitz "llvm.byval attribute attached to LLVM non-pointer argument"); 5281d6df1fcSEric Schweitz llvmArg.addAttrs(llvm::AttrBuilder().addByValAttr( 5291d6df1fcSEric Schweitz llvmArg.getType()->getPointerElementType())); 53070b841acSEric Schweitz } 53170b841acSEric Schweitz 5320881a4f1SAlex Zinenko mapValue(mlirArg, &llvmArg); 5335d7231d8SStephan Herhut argIdx++; 5345d7231d8SStephan Herhut } 5355d7231d8SStephan Herhut 536ff77397fSShraiysh Vaishay // Check the personality and set it. 537ff77397fSShraiysh Vaishay if (func.personality().hasValue()) { 538ff77397fSShraiysh Vaishay llvm::Type *ty = llvm::Type::getInt8PtrTy(llvmFunc->getContext()); 539ff77397fSShraiysh Vaishay if (llvm::Constant *pfunc = 540ff77397fSShraiysh Vaishay getLLVMConstant(ty, func.personalityAttr(), func.getLoc())) 541ff77397fSShraiysh Vaishay llvmFunc->setPersonalityFn(pfunc); 542ff77397fSShraiysh Vaishay } 543ff77397fSShraiysh Vaishay 5445d7231d8SStephan Herhut // First, create all blocks so we can jump to them. 5455d7231d8SStephan Herhut llvm::LLVMContext &llvmContext = llvmFunc->getContext(); 5465d7231d8SStephan Herhut for (auto &bb : func) { 5475d7231d8SStephan Herhut auto *llvmBB = llvm::BasicBlock::Create(llvmContext); 5485d7231d8SStephan Herhut llvmBB->insertInto(llvmFunc); 5490881a4f1SAlex Zinenko mapBlock(&bb, llvmBB); 5505d7231d8SStephan Herhut } 5515d7231d8SStephan Herhut 5525d7231d8SStephan Herhut // Then, convert blocks one by one in topological order to ensure defs are 5535d7231d8SStephan Herhut // converted before uses. 554*66900b3eSAlex Zinenko auto blocks = detail::getTopologicallySortedBlocks(func.getBody()); 55510164a2eSAlex Zinenko for (Block *bb : blocks) { 55610164a2eSAlex Zinenko llvm::IRBuilder<> builder(llvmContext); 55710164a2eSAlex Zinenko if (failed(convertBlock(*bb, bb->isEntryBlock(), builder))) 558baa1ec22SAlex Zinenko return failure(); 5595d7231d8SStephan Herhut } 5605d7231d8SStephan Herhut 5615d7231d8SStephan Herhut // Finally, after all blocks have been traversed and values mapped, connect 5625d7231d8SStephan Herhut // the PHI nodes to the results of preceding blocks. 563*66900b3eSAlex Zinenko detail::connectPHINodes(func.getBody(), *this); 564baa1ec22SAlex Zinenko return success(); 5655d7231d8SStephan Herhut } 5665d7231d8SStephan Herhut 56744fc7d72STres Popp LogicalResult ModuleTranslation::checkSupportedModuleOps(Operation *m) { 56844fc7d72STres Popp for (Operation &o : getModuleBody(m).getOperations()) 569fe7c0d90SRiver Riddle if (!isa<LLVM::LLVMFuncOp, LLVM::GlobalOp>(&o) && 570fe7c0d90SRiver Riddle !o.hasTrait<OpTrait::IsTerminator>()) 5714dde19f0SAlex Zinenko return o.emitOpError("unsupported module-level operation"); 5724dde19f0SAlex Zinenko return success(); 5734dde19f0SAlex Zinenko } 5744dde19f0SAlex Zinenko 575a084b94fSSean Silva LogicalResult ModuleTranslation::convertFunctionSignatures() { 5765d7231d8SStephan Herhut // Declare all functions first because there may be function calls that form a 577a084b94fSSean Silva // call graph with cycles, or global initializers that reference functions. 57844fc7d72STres Popp for (auto function : getModuleBody(mlirModule).getOps<LLVMFuncOp>()) { 5795e7959a3SAlex Zinenko llvm::FunctionCallee llvmFuncCst = llvmModule->getOrInsertFunction( 5805e7959a3SAlex Zinenko function.getName(), 581aec38c61SAlex Zinenko cast<llvm::FunctionType>(convertType(function.getType()))); 5820a2131b7SAlex Zinenko llvm::Function *llvmFunc = cast<llvm::Function>(llvmFuncCst.getCallee()); 583ebbdecddSAlex Zinenko llvmFunc->setLinkage(convertLinkageToLLVM(function.linkage())); 5840881a4f1SAlex Zinenko mapFunction(function.getName(), llvmFunc); 5850a2131b7SAlex Zinenko 5860a2131b7SAlex Zinenko // Forward the pass-through attributes to LLVM. 5870a2131b7SAlex Zinenko if (failed(forwardPassthroughAttributes(function.getLoc(), 5880a2131b7SAlex Zinenko function.passthrough(), llvmFunc))) 5890a2131b7SAlex Zinenko return failure(); 5905d7231d8SStephan Herhut } 5915d7231d8SStephan Herhut 592a084b94fSSean Silva return success(); 593a084b94fSSean Silva } 594a084b94fSSean Silva 595a084b94fSSean Silva LogicalResult ModuleTranslation::convertFunctions() { 5965d7231d8SStephan Herhut // Convert functions. 59744fc7d72STres Popp for (auto function : getModuleBody(mlirModule).getOps<LLVMFuncOp>()) { 5985d7231d8SStephan Herhut // Ignore external functions. 5995d7231d8SStephan Herhut if (function.isExternal()) 6005d7231d8SStephan Herhut continue; 6015d7231d8SStephan Herhut 602baa1ec22SAlex Zinenko if (failed(convertOneFunction(function))) 603baa1ec22SAlex Zinenko return failure(); 6045d7231d8SStephan Herhut } 6055d7231d8SStephan Herhut 606baa1ec22SAlex Zinenko return success(); 6075d7231d8SStephan Herhut } 6085d7231d8SStephan Herhut 609c69c9e0fSAlex Zinenko llvm::Type *ModuleTranslation::convertType(Type type) { 610b2ab375dSAlex Zinenko return typeTranslator.translateType(type); 611aec38c61SAlex Zinenko } 612aec38c61SAlex Zinenko 613efadb6b8SAlex Zinenko /// A helper to look up remapped operands in the value remapping table.` 614efadb6b8SAlex Zinenko SmallVector<llvm::Value *, 8> 615efadb6b8SAlex Zinenko ModuleTranslation::lookupValues(ValueRange values) { 616efadb6b8SAlex Zinenko SmallVector<llvm::Value *, 8> remapped; 617efadb6b8SAlex Zinenko remapped.reserve(values.size()); 6180881a4f1SAlex Zinenko for (Value v : values) 6190881a4f1SAlex Zinenko remapped.push_back(lookupValue(v)); 620efadb6b8SAlex Zinenko return remapped; 621efadb6b8SAlex Zinenko } 622efadb6b8SAlex Zinenko 623*66900b3eSAlex Zinenko const llvm::DILocation * 624*66900b3eSAlex Zinenko ModuleTranslation::translateLoc(Location loc, llvm::DILocalScope *scope) { 625*66900b3eSAlex Zinenko return debugTranslation->translateLoc(loc, scope); 626*66900b3eSAlex Zinenko } 627*66900b3eSAlex Zinenko 628db1c197bSAlex Zinenko std::unique_ptr<llvm::Module> ModuleTranslation::prepareLLVMModule( 629db1c197bSAlex Zinenko Operation *m, llvm::LLVMContext &llvmContext, StringRef name) { 630f9dc2b70SMehdi Amini m->getContext()->getOrLoadDialect<LLVM::LLVMDialect>(); 631db1c197bSAlex Zinenko auto llvmModule = std::make_unique<llvm::Module>(name, llvmContext); 632168213f9SAlex Zinenko if (auto dataLayoutAttr = 633168213f9SAlex Zinenko m->getAttr(LLVM::LLVMDialect::getDataLayoutAttrName())) 634168213f9SAlex Zinenko llvmModule->setDataLayout(dataLayoutAttr.cast<StringAttr>().getValue()); 6355dd5a083SNicolas Vasilache if (auto targetTripleAttr = 6365dd5a083SNicolas Vasilache m->getAttr(LLVM::LLVMDialect::getTargetTripleAttrName())) 6375dd5a083SNicolas Vasilache llvmModule->setTargetTriple(targetTripleAttr.cast<StringAttr>().getValue()); 6385d7231d8SStephan Herhut 6395d7231d8SStephan Herhut // Inject declarations for `malloc` and `free` functions that can be used in 6405d7231d8SStephan Herhut // memref allocation/deallocation coming from standard ops lowering. 641db1c197bSAlex Zinenko llvm::IRBuilder<> builder(llvmContext); 6425d7231d8SStephan Herhut llvmModule->getOrInsertFunction("malloc", builder.getInt8PtrTy(), 6435d7231d8SStephan Herhut builder.getInt64Ty()); 6445d7231d8SStephan Herhut llvmModule->getOrInsertFunction("free", builder.getVoidTy(), 6455d7231d8SStephan Herhut builder.getInt8PtrTy()); 6465d7231d8SStephan Herhut 6475d7231d8SStephan Herhut return llvmModule; 6485d7231d8SStephan Herhut } 649