1 //===- ModuleTranslation.cpp - MLIR to LLVM conversion --------------------===//
2 //
3 // Copyright 2019 The MLIR Authors.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //   http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 // =============================================================================
17 //
18 // This file implements the translation between an MLIR LLVM dialect module and
19 // the corresponding LLVMIR module. It only handles core LLVM IR operations.
20 //
21 //===----------------------------------------------------------------------===//
22 
23 #include "mlir/Target/LLVMIR/ModuleTranslation.h"
24 
25 #include "mlir/Dialect/LLVMIR/LLVMDialect.h"
26 #include "mlir/IR/Attributes.h"
27 #include "mlir/IR/Module.h"
28 #include "mlir/Support/LLVM.h"
29 
30 #include "llvm/ADT/SetVector.h"
31 #include "llvm/IR/BasicBlock.h"
32 #include "llvm/IR/Constants.h"
33 #include "llvm/IR/DerivedTypes.h"
34 #include "llvm/IR/IRBuilder.h"
35 #include "llvm/IR/LLVMContext.h"
36 #include "llvm/IR/Module.h"
37 #include "llvm/Transforms/Utils/Cloning.h"
38 
39 namespace mlir {
40 namespace LLVM {
41 
42 // Create an LLVM IR constant of `llvmType` from the MLIR attribute `attr`.
43 // This currently supports integer, floating point, splat and dense element
44 // attributes and combinations thereof.  In case of error, report it to `loc`
45 // and return nullptr.
46 llvm::Constant *ModuleTranslation::getLLVMConstant(llvm::Type *llvmType,
47                                                    Attribute attr,
48                                                    Location loc) {
49   if (!attr)
50     return llvm::UndefValue::get(llvmType);
51   if (auto intAttr = attr.dyn_cast<IntegerAttr>())
52     return llvm::ConstantInt::get(llvmType, intAttr.getValue());
53   if (auto floatAttr = attr.dyn_cast<FloatAttr>())
54     return llvm::ConstantFP::get(llvmType, floatAttr.getValue());
55   if (auto funcAttr = attr.dyn_cast<FlatSymbolRefAttr>())
56     return functionMapping.lookup(funcAttr.getValue());
57   if (auto splatAttr = attr.dyn_cast<SplatElementsAttr>()) {
58     auto *sequentialType = cast<llvm::SequentialType>(llvmType);
59     auto elementType = sequentialType->getElementType();
60     uint64_t numElements = sequentialType->getNumElements();
61     auto *child = getLLVMConstant(elementType, splatAttr.getSplatValue(), loc);
62     if (llvmType->isVectorTy())
63       return llvm::ConstantVector::getSplat(numElements, child);
64     if (llvmType->isArrayTy()) {
65       auto arrayType = llvm::ArrayType::get(elementType, numElements);
66       SmallVector<llvm::Constant *, 8> constants(numElements, child);
67       return llvm::ConstantArray::get(arrayType, constants);
68     }
69   }
70   if (auto elementsAttr = attr.dyn_cast<ElementsAttr>()) {
71     auto *sequentialType = cast<llvm::SequentialType>(llvmType);
72     auto elementType = sequentialType->getElementType();
73     uint64_t numElements = sequentialType->getNumElements();
74     SmallVector<llvm::Constant *, 8> constants;
75     constants.reserve(numElements);
76     for (auto n : elementsAttr.getValues<Attribute>()) {
77       constants.push_back(getLLVMConstant(elementType, n, loc));
78       if (!constants.back())
79         return nullptr;
80     }
81     if (llvmType->isVectorTy())
82       return llvm::ConstantVector::get(constants);
83     if (llvmType->isArrayTy()) {
84       auto arrayType = llvm::ArrayType::get(elementType, numElements);
85       return llvm::ConstantArray::get(arrayType, constants);
86     }
87   }
88   if (auto stringAttr = attr.dyn_cast<StringAttr>()) {
89     return llvm::ConstantDataArray::get(
90         llvmModule->getContext(), ArrayRef<char>{stringAttr.getValue().data(),
91                                                  stringAttr.getValue().size()});
92   }
93   emitError(loc, "unsupported constant value");
94   return nullptr;
95 }
96 
97 // Convert MLIR integer comparison predicate to LLVM IR comparison predicate.
98 static llvm::CmpInst::Predicate getLLVMCmpPredicate(ICmpPredicate p) {
99   switch (p) {
100   case LLVM::ICmpPredicate::eq:
101     return llvm::CmpInst::Predicate::ICMP_EQ;
102   case LLVM::ICmpPredicate::ne:
103     return llvm::CmpInst::Predicate::ICMP_NE;
104   case LLVM::ICmpPredicate::slt:
105     return llvm::CmpInst::Predicate::ICMP_SLT;
106   case LLVM::ICmpPredicate::sle:
107     return llvm::CmpInst::Predicate::ICMP_SLE;
108   case LLVM::ICmpPredicate::sgt:
109     return llvm::CmpInst::Predicate::ICMP_SGT;
110   case LLVM::ICmpPredicate::sge:
111     return llvm::CmpInst::Predicate::ICMP_SGE;
112   case LLVM::ICmpPredicate::ult:
113     return llvm::CmpInst::Predicate::ICMP_ULT;
114   case LLVM::ICmpPredicate::ule:
115     return llvm::CmpInst::Predicate::ICMP_ULE;
116   case LLVM::ICmpPredicate::ugt:
117     return llvm::CmpInst::Predicate::ICMP_UGT;
118   case LLVM::ICmpPredicate::uge:
119     return llvm::CmpInst::Predicate::ICMP_UGE;
120   }
121   llvm_unreachable("incorrect comparison predicate");
122 }
123 
124 static llvm::CmpInst::Predicate getLLVMCmpPredicate(FCmpPredicate p) {
125   switch (p) {
126   case LLVM::FCmpPredicate::_false:
127     return llvm::CmpInst::Predicate::FCMP_FALSE;
128   case LLVM::FCmpPredicate::oeq:
129     return llvm::CmpInst::Predicate::FCMP_OEQ;
130   case LLVM::FCmpPredicate::ogt:
131     return llvm::CmpInst::Predicate::FCMP_OGT;
132   case LLVM::FCmpPredicate::oge:
133     return llvm::CmpInst::Predicate::FCMP_OGE;
134   case LLVM::FCmpPredicate::olt:
135     return llvm::CmpInst::Predicate::FCMP_OLT;
136   case LLVM::FCmpPredicate::ole:
137     return llvm::CmpInst::Predicate::FCMP_OLE;
138   case LLVM::FCmpPredicate::one:
139     return llvm::CmpInst::Predicate::FCMP_ONE;
140   case LLVM::FCmpPredicate::ord:
141     return llvm::CmpInst::Predicate::FCMP_ORD;
142   case LLVM::FCmpPredicate::ueq:
143     return llvm::CmpInst::Predicate::FCMP_UEQ;
144   case LLVM::FCmpPredicate::ugt:
145     return llvm::CmpInst::Predicate::FCMP_UGT;
146   case LLVM::FCmpPredicate::uge:
147     return llvm::CmpInst::Predicate::FCMP_UGE;
148   case LLVM::FCmpPredicate::ult:
149     return llvm::CmpInst::Predicate::FCMP_ULT;
150   case LLVM::FCmpPredicate::ule:
151     return llvm::CmpInst::Predicate::FCMP_ULE;
152   case LLVM::FCmpPredicate::une:
153     return llvm::CmpInst::Predicate::FCMP_UNE;
154   case LLVM::FCmpPredicate::uno:
155     return llvm::CmpInst::Predicate::FCMP_UNO;
156   case LLVM::FCmpPredicate::_true:
157     return llvm::CmpInst::Predicate::FCMP_TRUE;
158   }
159   llvm_unreachable("incorrect comparison predicate");
160 }
161 
162 // A helper to look up remapped operands in the value remapping table.
163 template <typename Range>
164 SmallVector<llvm::Value *, 8> ModuleTranslation::lookupValues(Range &&values) {
165   SmallVector<llvm::Value *, 8> remapped;
166   remapped.reserve(llvm::size(values));
167   for (Value *v : values) {
168     remapped.push_back(valueMapping.lookup(v));
169   }
170   return remapped;
171 }
172 
173 // Given a single MLIR operation, create the corresponding LLVM IR operation
174 // using the `builder`.  LLVM IR Builder does not have a generic interface so
175 // this has to be a long chain of `if`s calling different functions with a
176 // different number of arguments.
177 LogicalResult ModuleTranslation::convertOperation(Operation &opInst,
178                                                   llvm::IRBuilder<> &builder) {
179   auto extractPosition = [](ArrayAttr attr) {
180     SmallVector<unsigned, 4> position;
181     position.reserve(attr.size());
182     for (Attribute v : attr)
183       position.push_back(v.cast<IntegerAttr>().getValue().getZExtValue());
184     return position;
185   };
186 
187 #include "mlir/Dialect/LLVMIR/LLVMConversions.inc"
188 
189   // Emit function calls.  If the "callee" attribute is present, this is a
190   // direct function call and we also need to look up the remapped function
191   // itself.  Otherwise, this is an indirect call and the callee is the first
192   // operand, look it up as a normal value.  Return the llvm::Value representing
193   // the function result, which may be of llvm::VoidTy type.
194   auto convertCall = [this, &builder](Operation &op) -> llvm::Value * {
195     auto operands = lookupValues(op.getOperands());
196     ArrayRef<llvm::Value *> operandsRef(operands);
197     if (auto attr = op.getAttrOfType<FlatSymbolRefAttr>("callee")) {
198       return builder.CreateCall(functionMapping.lookup(attr.getValue()),
199                                 operandsRef);
200     } else {
201       return builder.CreateCall(operandsRef.front(), operandsRef.drop_front());
202     }
203   };
204 
205   // Emit calls.  If the called function has a result, remap the corresponding
206   // value.  Note that LLVM IR dialect CallOp has either 0 or 1 result.
207   if (isa<LLVM::CallOp>(opInst)) {
208     llvm::Value *result = convertCall(opInst);
209     if (opInst.getNumResults() != 0) {
210       valueMapping[opInst.getResult(0)] = result;
211       return success();
212     }
213     // Check that LLVM call returns void for 0-result functions.
214     return success(result->getType()->isVoidTy());
215   }
216 
217   // Emit branches.  We need to look up the remapped blocks and ignore the block
218   // arguments that were transformed into PHI nodes.
219   if (auto brOp = dyn_cast<LLVM::BrOp>(opInst)) {
220     builder.CreateBr(blockMapping[brOp.getSuccessor(0)]);
221     return success();
222   }
223   if (auto condbrOp = dyn_cast<LLVM::CondBrOp>(opInst)) {
224     builder.CreateCondBr(valueMapping.lookup(condbrOp.getOperand(0)),
225                          blockMapping[condbrOp.getSuccessor(0)],
226                          blockMapping[condbrOp.getSuccessor(1)]);
227     return success();
228   }
229 
230   // Emit addressof.  We need to look up the global value referenced by the
231   // operation and store it in the MLIR-to-LLVM value mapping.  This does not
232   // emit any LLVM instruction.
233   if (auto addressOfOp = dyn_cast<LLVM::AddressOfOp>(opInst)) {
234     LLVM::GlobalOp global = addressOfOp.getGlobal();
235     // The verifier should not have allowed this.
236     assert(global && "referencing an undefined global");
237 
238     valueMapping[addressOfOp.getResult()] = globalsMapping.lookup(global);
239     return success();
240   }
241 
242   return opInst.emitError("unsupported or non-LLVM operation: ")
243          << opInst.getName();
244 }
245 
246 // Convert block to LLVM IR.  Unless `ignoreArguments` is set, emit PHI nodes
247 // to define values corresponding to the MLIR block arguments.  These nodes
248 // are not connected to the source basic blocks, which may not exist yet.
249 LogicalResult ModuleTranslation::convertBlock(Block &bb, bool ignoreArguments) {
250   llvm::IRBuilder<> builder(blockMapping[&bb]);
251 
252   // Before traversing operations, make block arguments available through
253   // value remapping and PHI nodes, but do not add incoming edges for the PHI
254   // nodes just yet: those values may be defined by this or following blocks.
255   // This step is omitted if "ignoreArguments" is set.  The arguments of the
256   // first block have been already made available through the remapping of
257   // LLVM function arguments.
258   if (!ignoreArguments) {
259     auto predecessors = bb.getPredecessors();
260     unsigned numPredecessors =
261         std::distance(predecessors.begin(), predecessors.end());
262     for (auto *arg : bb.getArguments()) {
263       auto wrappedType = arg->getType().dyn_cast<LLVM::LLVMType>();
264       if (!wrappedType)
265         return emitError(bb.front().getLoc(),
266                          "block argument does not have an LLVM type");
267       llvm::Type *type = wrappedType.getUnderlyingType();
268       llvm::PHINode *phi = builder.CreatePHI(type, numPredecessors);
269       valueMapping[arg] = phi;
270     }
271   }
272 
273   // Traverse operations.
274   for (auto &op : bb) {
275     if (failed(convertOperation(op, builder)))
276       return failure();
277   }
278 
279   return success();
280 }
281 
282 // Convert the LLVM dialect linkage type to LLVM IR linkage type.
283 llvm::GlobalVariable::LinkageTypes convertLinkageType(LLVM::Linkage linkage) {
284   switch (linkage) {
285   case LLVM::Linkage::Private:
286     return llvm::GlobalValue::PrivateLinkage;
287   case LLVM::Linkage::Internal:
288     return llvm::GlobalValue::InternalLinkage;
289   case LLVM::Linkage::AvailableExternally:
290     return llvm::GlobalValue::AvailableExternallyLinkage;
291   case LLVM::Linkage::Linkonce:
292     return llvm::GlobalValue::LinkOnceAnyLinkage;
293   case LLVM::Linkage::Weak:
294     return llvm::GlobalValue::WeakAnyLinkage;
295   case LLVM::Linkage::Common:
296     return llvm::GlobalValue::CommonLinkage;
297   case LLVM::Linkage::Appending:
298     return llvm::GlobalValue::AppendingLinkage;
299   case LLVM::Linkage::ExternWeak:
300     return llvm::GlobalValue::ExternalWeakLinkage;
301   case LLVM::Linkage::LinkonceODR:
302     return llvm::GlobalValue::LinkOnceODRLinkage;
303   case LLVM::Linkage::WeakODR:
304     return llvm::GlobalValue::WeakODRLinkage;
305   case LLVM::Linkage::External:
306     return llvm::GlobalValue::ExternalLinkage;
307   }
308   llvm_unreachable("unknown linkage type");
309 }
310 
311 // Create named global variables that correspond to llvm.mlir.global
312 // definitions.
313 void ModuleTranslation::convertGlobals() {
314   for (auto op : mlirModule.getOps<LLVM::GlobalOp>()) {
315     llvm::Type *type = op.getType().getUnderlyingType();
316     llvm::Constant *cst = llvm::UndefValue::get(type);
317     if (op.getValueOrNull()) {
318       // String attributes are treated separately because they cannot appear as
319       // in-function constants and are thus not supported by getLLVMConstant.
320       if (auto strAttr = op.getValueOrNull().dyn_cast_or_null<StringAttr>()) {
321         cst = llvm::ConstantDataArray::getString(
322             llvmModule->getContext(), strAttr.getValue(), /*AddNull=*/false);
323         type = cst->getType();
324       } else {
325         cst = getLLVMConstant(type, op.getValueOrNull(), op.getLoc());
326       }
327     } else if (Block *initializer = op.getInitializerBlock()) {
328       llvm::IRBuilder<> builder(llvmModule->getContext());
329       for (auto &op : initializer->without_terminator()) {
330         if (failed(convertOperation(op, builder)) ||
331             !isa<llvm::Constant>(valueMapping.lookup(op.getResult(0)))) {
332           emitError(op.getLoc(), "unemittable constant value");
333           return;
334         }
335       }
336       ReturnOp ret = cast<ReturnOp>(initializer->getTerminator());
337       cst = cast<llvm::Constant>(valueMapping.lookup(ret.getOperand(0)));
338     }
339 
340     auto linkage = convertLinkageType(op.linkage());
341     bool anyExternalLinkage =
342         (linkage == llvm::GlobalVariable::ExternalLinkage ||
343          linkage == llvm::GlobalVariable::ExternalWeakLinkage);
344     auto addrSpace = op.addr_space().getLimitedValue();
345     auto *var = new llvm::GlobalVariable(
346         *llvmModule, type, op.constant(), linkage,
347         anyExternalLinkage ? nullptr : cst, op.sym_name(),
348         /*InsertBefore=*/nullptr, llvm::GlobalValue::NotThreadLocal, addrSpace);
349 
350     globalsMapping.try_emplace(op, var);
351   }
352 }
353 
354 // Get the SSA value passed to the current block from the terminator operation
355 // of its predecessor.
356 static Value *getPHISourceValue(Block *current, Block *pred,
357                                 unsigned numArguments, unsigned index) {
358   auto &terminator = *pred->getTerminator();
359   if (isa<LLVM::BrOp>(terminator)) {
360     return terminator.getOperand(index);
361   }
362 
363   // For conditional branches, we need to check if the current block is reached
364   // through the "true" or the "false" branch and take the relevant operands.
365   auto condBranchOp = dyn_cast<LLVM::CondBrOp>(terminator);
366   assert(condBranchOp &&
367          "only branch operations can be terminators of a block that "
368          "has successors");
369   assert((condBranchOp.getSuccessor(0) != condBranchOp.getSuccessor(1)) &&
370          "successors with arguments in LLVM conditional branches must be "
371          "different blocks");
372 
373   return condBranchOp.getSuccessor(0) == current
374              ? terminator.getSuccessorOperand(0, index)
375              : terminator.getSuccessorOperand(1, index);
376 }
377 
378 void ModuleTranslation::connectPHINodes(LLVMFuncOp func) {
379   // Skip the first block, it cannot be branched to and its arguments correspond
380   // to the arguments of the LLVM function.
381   for (auto it = std::next(func.begin()), eit = func.end(); it != eit; ++it) {
382     Block *bb = &*it;
383     llvm::BasicBlock *llvmBB = blockMapping.lookup(bb);
384     auto phis = llvmBB->phis();
385     auto numArguments = bb->getNumArguments();
386     assert(numArguments == std::distance(phis.begin(), phis.end()));
387     for (auto &numberedPhiNode : llvm::enumerate(phis)) {
388       auto &phiNode = numberedPhiNode.value();
389       unsigned index = numberedPhiNode.index();
390       for (auto *pred : bb->getPredecessors()) {
391         phiNode.addIncoming(valueMapping.lookup(getPHISourceValue(
392                                 bb, pred, numArguments, index)),
393                             blockMapping.lookup(pred));
394       }
395     }
396   }
397 }
398 
399 // TODO(mlir-team): implement an iterative version
400 static void topologicalSortImpl(llvm::SetVector<Block *> &blocks, Block *b) {
401   blocks.insert(b);
402   for (Block *bb : b->getSuccessors()) {
403     if (blocks.count(bb) == 0)
404       topologicalSortImpl(blocks, bb);
405   }
406 }
407 
408 // Sort function blocks topologically.
409 static llvm::SetVector<Block *> topologicalSort(LLVMFuncOp f) {
410   // For each blocks that has not been visited yet (i.e. that has no
411   // predecessors), add it to the list and traverse its successors in DFS
412   // preorder.
413   llvm::SetVector<Block *> blocks;
414   for (Block &b : f.getBlocks()) {
415     if (blocks.count(&b) == 0)
416       topologicalSortImpl(blocks, &b);
417   }
418   assert(blocks.size() == f.getBlocks().size() && "some blocks are not sorted");
419 
420   return blocks;
421 }
422 
423 LogicalResult ModuleTranslation::convertOneFunction(LLVMFuncOp func) {
424   // Clear the block and value mappings, they are only relevant within one
425   // function.
426   blockMapping.clear();
427   valueMapping.clear();
428   llvm::Function *llvmFunc = functionMapping.lookup(func.getName());
429   // Add function arguments to the value remapping table.
430   // If there was noalias info then we decorate each argument accordingly.
431   unsigned int argIdx = 0;
432   for (const auto &kvp : llvm::zip(func.getArguments(), llvmFunc->args())) {
433     llvm::Argument &llvmArg = std::get<1>(kvp);
434     BlockArgument *mlirArg = std::get<0>(kvp);
435 
436     if (auto attr = func.getArgAttrOfType<BoolAttr>(argIdx, "llvm.noalias")) {
437       // NB: Attribute already verified to be boolean, so check if we can indeed
438       // attach the attribute to this argument, based on its type.
439       auto argTy = mlirArg->getType().dyn_cast<LLVM::LLVMType>();
440       if (!argTy.getUnderlyingType()->isPointerTy())
441         return func.emitError(
442             "llvm.noalias attribute attached to LLVM non-pointer argument");
443       if (attr.getValue())
444         llvmArg.addAttr(llvm::Attribute::AttrKind::NoAlias);
445     }
446     valueMapping[mlirArg] = &llvmArg;
447     argIdx++;
448   }
449 
450   // First, create all blocks so we can jump to them.
451   llvm::LLVMContext &llvmContext = llvmFunc->getContext();
452   for (auto &bb : func) {
453     auto *llvmBB = llvm::BasicBlock::Create(llvmContext);
454     llvmBB->insertInto(llvmFunc);
455     blockMapping[&bb] = llvmBB;
456   }
457 
458   // Then, convert blocks one by one in topological order to ensure defs are
459   // converted before uses.
460   auto blocks = topologicalSort(func);
461   for (auto indexedBB : llvm::enumerate(blocks)) {
462     auto *bb = indexedBB.value();
463     if (failed(convertBlock(*bb, /*ignoreArguments=*/indexedBB.index() == 0)))
464       return failure();
465   }
466 
467   // Finally, after all blocks have been traversed and values mapped, connect
468   // the PHI nodes to the results of preceding blocks.
469   connectPHINodes(func);
470   return success();
471 }
472 
473 LogicalResult ModuleTranslation::checkSupportedModuleOps(ModuleOp m) {
474   for (Operation &o : m.getBody()->getOperations())
475     if (!isa<LLVM::LLVMFuncOp>(&o) && !isa<LLVM::GlobalOp>(&o) &&
476         !isa<ModuleTerminatorOp>(&o))
477       return o.emitOpError("unsupported module-level operation");
478   return success();
479 }
480 
481 LogicalResult ModuleTranslation::convertFunctions() {
482   // Declare all functions first because there may be function calls that form a
483   // call graph with cycles.
484   for (auto function : mlirModule.getOps<LLVMFuncOp>()) {
485     llvm::FunctionCallee llvmFuncCst = llvmModule->getOrInsertFunction(
486         function.getName(),
487         llvm::cast<llvm::FunctionType>(function.getType().getUnderlyingType()));
488     assert(isa<llvm::Function>(llvmFuncCst.getCallee()));
489     functionMapping[function.getName()] =
490         cast<llvm::Function>(llvmFuncCst.getCallee());
491   }
492 
493   // Convert functions.
494   for (auto function : mlirModule.getOps<LLVMFuncOp>()) {
495     // Ignore external functions.
496     if (function.isExternal())
497       continue;
498 
499     if (failed(convertOneFunction(function)))
500       return failure();
501   }
502 
503   return success();
504 }
505 
506 std::unique_ptr<llvm::Module> ModuleTranslation::prepareLLVMModule(ModuleOp m) {
507   auto *dialect = m.getContext()->getRegisteredDialect<LLVM::LLVMDialect>();
508   assert(dialect && "LLVM dialect must be registered");
509 
510   auto llvmModule = llvm::CloneModule(dialect->getLLVMModule());
511   if (!llvmModule)
512     return nullptr;
513 
514   llvm::LLVMContext &llvmContext = llvmModule->getContext();
515   llvm::IRBuilder<> builder(llvmContext);
516 
517   // Inject declarations for `malloc` and `free` functions that can be used in
518   // memref allocation/deallocation coming from standard ops lowering.
519   llvmModule->getOrInsertFunction("malloc", builder.getInt8PtrTy(),
520                                   builder.getInt64Ty());
521   llvmModule->getOrInsertFunction("free", builder.getVoidTy(),
522                                   builder.getInt8PtrTy());
523 
524   return llvmModule;
525 }
526 
527 } // namespace LLVM
528 } // namespace mlir
529