Skip to content

Commit f8b460d

Browse files
committed
Add support for tail calls and function attributes on calls
1 parent 0337931 commit f8b460d

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

mlir/lib/Target/LLVMIR/ModuleTranslation.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,12 +303,29 @@ LogicalResult ModuleTranslation::convertOperation(Operation &opInst,
303303
auto convertCall = [this, &builder](Operation &op) -> llvm::Value * {
304304
auto operands = lookupValues(op.getOperands());
305305
ArrayRef<llvm::Value *> operandsRef(operands);
306+
llvm::Value *callV;
306307
if (auto attr = op.getAttrOfType<FlatSymbolRefAttr>("callee")) {
307-
return builder.CreateCall(functionMapping.lookup(attr.getValue()),
308+
callV = builder.CreateCall(functionMapping.lookup(attr.getValue()),
308309
operandsRef);
309310
} else {
310-
return builder.CreateCall(operandsRef.front(), operandsRef.drop_front());
311+
callV = builder.CreateCall(operandsRef.front(), operandsRef.drop_front());
311312
}
313+
auto callInst = static_cast<llvm::CallInst *>(callV);
314+
if (op.getAttrOfType<UnitAttr>("tail")) {
315+
callInst->setTailCall(true);
316+
} else if (op.getAttrOfType<UnitAttr>("musttail")) {
317+
callInst->setTailCallKind(llvm::CallInst::TailCallKind::TCK_MustTail);
318+
}
319+
for (auto namedAttr : op.getAttrs()) {
320+
if (auto fnAttr = namedAttr.second.dyn_cast_or_null<UnitAttr>()) {
321+
StringRef fnAttrKindName = namedAttr.first.strref();
322+
auto fnAttrKind = llvm::Attribute::getAttrKindFromName(fnAttrKindName);
323+
if (fnAttrKind != llvm::Attribute::None) {
324+
callInst->addAttribute(llvm::AttributeList::FunctionIndex, fnAttrKind);
325+
}
326+
}
327+
}
328+
return callInst;
312329
};
313330

314331
// Emit calls. If the called function has a result, remap the corresponding

0 commit comments

Comments
 (0)