[WebAssembly] Fix epilogue insertion for indirect tail calls
authorThomas Lively <tlively@google.com>
Wed, 22 Mar 2023 16:28:47 +0000 (09:28 -0700)
committerThomas Lively <tlively@google.com>
Wed, 22 Mar 2023 16:28:48 +0000 (09:28 -0700)
Previously epilogues were incorrectly inserted after indirect tail calls because
they did not have the `isTerminator` property. Add that property and test that
they get correct epilogues. To be safe, also add other properties that were
defined for direct tail calls.

Differential Revision: https://reviews.llvm.org/D146569

llvm/lib/Target/WebAssembly/WebAssemblyInstrCall.td
llvm/test/CodeGen/WebAssembly/tailcall.ll

index 6a123f8..ca9a5ef 100644 (file)
@@ -73,7 +73,7 @@ defm RET_CALL :
     "return_call    \t$callee", "return_call\t$callee", 0x12>,
   Requires<[HasTailCall]>;
 
-let isReturn = 1 in
+let isReturn = 1, isTerminator = 1, hasCtrlDep = 1, isBarrier = 1 in
 defm RET_CALL_INDIRECT :
   I<(outs), (ins TypeIndex:$type, table32_op:$table, variable_ops),
     (outs), (ins TypeIndex:$type, table32_op:$table), [],
index 34dd0a9..84bd142 100644 (file)
@@ -507,6 +507,43 @@ define i32 @stack_arg_cast(i32 %x) {
   ret i32 %v
 }
 
+; Checks that epilogues are inserted after return calls.
+define i32 @direct_epilogue() {
+; CHECK-LABEL: direct_epilogue:
+; CHECK:         .functype direct_epilogue () -> (i32)
+; CHECK-NEXT:  # %bb.0:
+; CHECK-NEXT:    global.get $push0=, __stack_pointer
+; CHECK-NEXT:    i32.const $push1=, 256
+; CHECK-NEXT:    i32.sub $push5=, $pop0, $pop1
+; CHECK-NEXT:    local.tee $push4=, $0=, $pop5
+; CHECK-NEXT:    global.set __stack_pointer, $pop4
+; CHECK-NEXT:    i32.const $push2=, 256
+; CHECK-NEXT:    i32.add $push3=, $0, $pop2
+; CHECK-NEXT:    global.set __stack_pointer, $pop3
+; CHECK-NEXT:    return_call direct_epilogue
+  %a = alloca [64 x i32]
+  %v = musttail call i32 @direct_epilogue()
+  ret i32 %v
+}
+
+define i32 @indirect_epilogue(ptr %p) {
+; CHECK-LABEL: indirect_epilogue:
+; CHECK:         .functype indirect_epilogue (i32) -> (i32)
+; CHECK-NEXT:  # %bb.0:
+; CHECK-NEXT:    global.get $push0=, __stack_pointer
+; CHECK-NEXT:    i32.const $push1=, 256
+; CHECK-NEXT:    i32.sub $push5=, $pop0, $pop1
+; CHECK-NEXT:    local.tee $push4=, $1=, $pop5
+; CHECK-NEXT:    global.set __stack_pointer, $pop4
+; CHECK-NEXT:    i32.const $push2=, 256
+; CHECK-NEXT:    i32.add $push3=, $1, $pop2
+; CHECK-NEXT:    global.set __stack_pointer, $pop3
+; CHECK-NEXT:    return_call_indirect , $0, $0
+  %a = alloca [64 x i32]
+  %v = musttail call i32 %p(ptr %p)
+  ret i32 %v
+}
+
 ; Check that the signatures generated for external indirectly
 ; return-called functions include the proper return types