diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp b/llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp --- a/llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp +++ b/llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp @@ -644,13 +644,31 @@ if (CLI.IsPatchPoint) fail(DL, DAG, "WebAssembly doesn't support patch point yet"); - // Fail if tail calls are required but not enabled - if (!Subtarget->hasTailCall()) { - if ((CallConv == CallingConv::Fast && CLI.IsTailCall && - MF.getTarget().Options.GuaranteedTailCallOpt) || - (CLI.CS && CLI.CS.isMustTailCall())) - fail(DL, DAG, "WebAssembly 'tail-call' feature not enabled"); - CLI.IsTailCall = false; + if (CLI.IsTailCall) { + bool MustTail = (CallConv == CallingConv::Fast && CLI.IsTailCall && + MF.getTarget().Options.GuaranteedTailCallOpt) || + (CLI.CS && CLI.CS.isMustTailCall()); + if (Subtarget->hasTailCall()) { + // Do not tail call unless caller and callee return types match + const Function &F = MF.getFunction(); + const TargetMachine &TM = getTargetMachine(); + Type *RetTy = F.getReturnType(); + SmallVector CallerRetTys; + SmallVector CalleeRetTys; + computeLegalValueVTs(F, TM, RetTy, CallerRetTys); + computeLegalValueVTs(F, TM, CLI.RetTy, CalleeRetTys); + bool TypesMatch = CallerRetTys.size() == CalleeRetTys.size() && + std::equal(CallerRetTys.begin(), CallerRetTys.end(), + CalleeRetTys.begin()); + if (!TypesMatch) { + assert(!MustTail); + CLI.IsTailCall = false; + } + } else { + CLI.IsTailCall = false; + if (MustTail) + fail(DL, DAG, "WebAssembly 'tail-call' feature not enabled"); + } } SmallVectorImpl &Ins = CLI.Ins; diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyMCInstLower.cpp b/llvm/lib/Target/WebAssembly/WebAssemblyMCInstLower.cpp --- a/llvm/lib/Target/WebAssembly/WebAssemblyMCInstLower.cpp +++ b/llvm/lib/Target/WebAssembly/WebAssemblyMCInstLower.cpp @@ -226,6 +226,17 @@ if (WebAssembly::isCallIndirect(MI->getOpcode())) Params.pop_back(); + // return_call_indirect instructions have the return type of the + // caller + if (MI->getOpcode() == WebAssembly::RET_CALL_INDIRECT) { + const Function &F = MI->getMF()->getFunction(); + const TargetMachine &TM = MI->getMF()->getTarget(); + Type *RetTy = F.getReturnType(); + SmallVector CallerRetTys; + computeLegalValueVTs(F, TM, RetTy, CallerRetTys); + valTypesFromMVTs(CallerRetTys, Returns); + } + auto *WasmSym = cast(Sym); auto Signature = make_unique(std::move(Returns), std::move(Params)); diff --git a/llvm/test/CodeGen/WebAssembly/tailcall.ll b/llvm/test/CodeGen/WebAssembly/tailcall.ll --- a/llvm/test/CodeGen/WebAssembly/tailcall.ll +++ b/llvm/test/CodeGen/WebAssembly/tailcall.ll @@ -1,7 +1,8 @@ ; RUN: llc < %s -asm-verbose=false -verify-machineinstrs -disable-wasm-fallthrough-return-opt -wasm-disable-explicit-locals -wasm-keep-registers -mattr=+tail-call | FileCheck --check-prefixes=CHECK,SLOW %s ; RUN: llc < %s -asm-verbose=false -verify-machineinstrs -disable-wasm-fallthrough-return-opt -wasm-disable-explicit-locals -wasm-keep-registers -fast-isel -mattr=+tail-call | FileCheck --check-prefixes=CHECK,FAST %s +; RUN: llc < %s --filetype=obj -mattr=+tail-call | obj2yaml | FileCheck --check-prefix=YAML %s -; Test that the tail-call attribute is accepted +; Test that the tail calls lower correctly target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128" target triple = "wasm32-unknown-unknown" @@ -124,6 +125,42 @@ ret i32 %v } +; CHECK-LABEL: mismatched_return_void: +; CHECK: i32.call $drop=, baz, $pop{{[0-9]+}}, $pop{{[0-9]+}}, $pop{{[0-9]+}}{{$}} +; CHECK: return{{$}} +define void @mismatched_return_void() { + %v = tail call i32 @baz(i32 0, i32 42, i32 6) + ret void +} + +; CHECK-LABEL: mismatched_return_f32: +; CHECK: i32.call $drop=, baz, $pop{{[0-9]+}}, $pop{{[0-9]+}}, $pop{{[0-9]+}}{{$}} +; CHECK: f32.const $push[[L:[0-9]+]]=, 0x1p0{{$}} +; CHECK: return $pop[[L]]{{$}} +define float @mismatched_return_f32() { + %v = tail call i32 @baz(i32 0, i32 42, i32 6) + ret float 1. +} + +; CHECK-LABEL: mismatched_indirect_void: +; CHECK: i32.call_indirect $drop=, $0, $1, $2, $0{{$}} +; CHECK: return{{$}} +define void @mismatched_indirect_void(%fn %f, i32 %x, i32 %y) { + %p = extractvalue %fn %f, 0 + %v = tail call i32 %p(%fn %f, i32 %x, i32 %y) + ret void +} + +; CHECK-LABEL: mismatched_indirect_f32: +; CHECK: i32.call_indirect $drop=, $0, $1, $2, $0{{$}} +; CHECK: f32.const $push[[L:[0-9]+]]=, 0x1p0{{$}} +; CHECK: return $pop[[L]]{{$}} +define float @mismatched_indirect_f32(%fn %f, i32 %x, i32 %y) { + %p = extractvalue %fn %f, 0 + %v = tail call i32 %p(%fn %f, i32 %x, i32 %y) + ret float 1. +} + ; CHECK-LABEL: mismatched_byval: ; CHECK: i32.store ; CHECK: return_call quux, $pop{{[0-9]+}}{{$}} @@ -142,6 +179,22 @@ ret i32 %v } +; Check that the signatures generated for external indirectly +; return-called functions include the proper return types + +; YAML-LABEL: - Index: 8 +; YAML-NEXT: ReturnType: I32 +; YAML-NEXT: ParamTypes: +; YAML-NEXT: - I32 +; YAML-NEXT: - F32 +; YAML-NEXT: - I64 +; YAML-NEXT: - F64 +define i32 @unique_caller(i32 (i32, float, i64, double)** %p) { + %f = load i32 (i32, float, i64, double)*, i32 (i32, float, i64, double)** %p + %v = tail call i32 %f(i32 0, float 0., i64 0, double 0.) + ret i32 %v +} + ; CHECK-LABEL: .section .custom_section.target_features ; CHECK-NEXT: .int8 1 ; CHECK-NEXT: .int8 43