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