[WebAssembly] Select call_indirect for alloca calls
authorHeejin Ahn <aheejin@gmail.com>
Tue, 28 Mar 2023 08:01:21 +0000 (01:01 -0700)
committerHeejin Ahn <aheejin@gmail.com>
Wed, 29 Mar 2023 19:46:58 +0000 (12:46 -0700)
Currently calling stack locations is selected using `CALL` in ISel,
resulting in an invalid code and crashing in AsmPrinter. FastISel
correctly selects it will `CALL_INDIRECT`.

Fixes the problem reported in D146781.

Reviewed By: tlively, HerrCai0907

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

llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp
llvm/test/CodeGen/WebAssembly/call-indirect.ll

index 32d0b01..4fd753b 100644 (file)
@@ -534,11 +534,12 @@ LowerCallResults(MachineInstr &CallResults, DebugLoc DL, MachineBasicBlock *BB,
   assert(CallResults.getOpcode() == WebAssembly::CALL_RESULTS ||
          CallResults.getOpcode() == WebAssembly::RET_CALL_RESULTS);
 
-  bool IsIndirect = CallParams.getOperand(0).isReg();
+  bool IsIndirect =
+      CallParams.getOperand(0).isReg() || CallParams.getOperand(0).isFI();
   bool IsRetCall = CallResults.getOpcode() == WebAssembly::RET_CALL_RESULTS;
 
   bool IsFuncrefCall = false;
-  if (IsIndirect) {
+  if (IsIndirect && CallParams.getOperand(0).isReg()) {
     Register Reg = CallParams.getOperand(0).getReg();
     const MachineFunction *MF = BB->getParent();
     const MachineRegisterInfo &MRI = MF->getRegInfo();
index b88d096..8d79d93 100644 (file)
@@ -18,6 +18,22 @@ define void @call_indirect_void(ptr %callee) {
   ret void
 }
 
+; CHECK-LABEL: call_indirect_alloca:
+; CHECK-NEXT: .functype call_indirect_alloca () -> ()
+; CHECK:      local.tee  0
+; CHECK-NEXT: global.set  __stack_pointer
+; CHECK-NEXT: local.get  0
+; CHECK-NEXT: i32.const  12
+; CHECK-NEXT: i32.add
+; REF:        call_indirect __indirect_function_table, () -> ()
+; NOREF:      call_indirect () -> ()
+define void @call_indirect_alloca() {
+entry:
+  %ptr = alloca i32, align 4
+  call void %ptr()
+  ret void
+}
+
 ; OBJ:    Imports:
 ; OBJ-NEXT:      - Module:          env
 ; OBJ-NEXT:        Field:           __linear_memory
@@ -25,5 +41,10 @@ define void @call_indirect_void(ptr %callee) {
 ; OBJ-NEXT:        Memory:
 ; OBJ-NEXT:          Minimum:         0x0
 ; OBJ-NEXT:      - Module:          env
+; OBJ-NEXT:        Field:           __stack_pointer
+; OBJ-NEXT:        Kind:            GLOBAL
+; OBJ-NEXT:        GlobalType:      I32
+; OBJ-NEXT:        GlobalMutable:   true
+; OBJ-NEXT:      - Module:          env
 ; OBJ-NEXT:        Field:           __indirect_function_table
 ; OBJ-NEXT:        Kind:            TABLE