@@ -839,7 +839,8 @@ static LLValue *DtoCallableValue(DValue *fn) {
839839
840840// FIXME: this function is a mess !
841841DValue *DtoCallFunction (Loc loc, Type *resulttype, DValue *fnval,
842- Expressions *arguments, LLValue *sretPointer, bool directcall) {
842+ Expressions *arguments, LLValue *sretPointer,
843+ bool directcall, bool isMustTail) {
843844 IF_LOG Logger::println (" DtoCallFunction()" );
844845 LOG_SCOPE
845846
@@ -1060,6 +1061,18 @@ DValue *DtoCallFunction(Loc loc, Type *resulttype, DValue *fnval,
10601061 attrlist = attrlist.addFnAttributes (
10611062 gIR ->context (), llvm::AttrBuilder (gIR ->context (), attrbuildattribs));
10621063 call->setAttributes (attrlist);
1064+ if (isMustTail) {
1065+ if (auto ci = llvm::dyn_cast<llvm::CallInst>(call)) {
1066+ ci->setTailCallKind (llvm::CallInst::TCK_MustTail);
1067+ } else {
1068+ if (!tf->isNothrow ()) {
1069+ error (loc, " cannot perform tail-call - callee must be nothrow" );
1070+ } else {
1071+ error (loc, " cannot perform tail-call - no code like destructors or scope(exit) should run after the call" );
1072+ }
1073+ fatal ();
1074+ }
1075+ }
10631076
10641077 // Special case for struct constructor calls: For temporaries, using the
10651078 // this pointer value returned from the constructor instead of the alloca
0 commit comments