]> granicus.if.org Git - llvm/commitdiff
[WebAssembly] Implement __builtin_return_address for emscripten
authorThomas Lively <tlively@google.com>
Thu, 23 May 2019 01:24:01 +0000 (01:24 +0000)
committerThomas Lively <tlively@google.com>
Thu, 23 May 2019 01:24:01 +0000 (01:24 +0000)
Summary:
In this patch, `ISD::RETURNADDR` is lowered on the emscripten target
to the new Emscripten runtime function `emscripten_return_address`, which
implements the functionality.

Patch by Guanzhong Chen

Reviewers: tlively, aheejin

Reviewed By: tlively

Subscribers: dschuff, sbc100, jgravelle-google, hiraditya, sunfish, llvm-commits

Tags: #llvm

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

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@361454 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/IR/RuntimeLibcalls.def
lib/Target/WebAssembly/WebAssemblyISelLowering.cpp
lib/Target/WebAssembly/WebAssemblyISelLowering.h
lib/Target/WebAssembly/WebAssemblyRuntimeLibcallSignatures.cpp
test/CodeGen/WebAssembly/return-address-emscripten.ll [new file with mode: 0644]
test/CodeGen/WebAssembly/return-address-unknown.ll [new file with mode: 0644]

index 6e29d29f9deaed7b43406c3cfb578351a9085cac..c617ef9a8d3b2b806b8bad5219239c40b4d5dbad 100644 (file)
@@ -539,6 +539,9 @@ HANDLE_LIBCALL(STACKPROTECTOR_CHECK_FAIL, "__stack_chk_fail")
 // Deoptimization
 HANDLE_LIBCALL(DEOPTIMIZE, "__llvm_deoptimize")
 
+// Return address
+HANDLE_LIBCALL(RETURN_ADDRESS, nullptr)
+
 HANDLE_LIBCALL(UNKNOWN_LIBCALL, nullptr)
 
 #undef HANDLE_LIBCALL
index 41c82ded69c0425503f62aa858cfa9efc5388018..1f72c654ee8f06f82bae07ac35db920ce203d6ff 100644 (file)
@@ -273,6 +273,11 @@ WebAssemblyTargetLowering::WebAssemblyTargetLowering(
   setLibcallName(RTLIB::FPEXT_F16_F32, "__extendhfsf2");
   setLibcallName(RTLIB::FPROUND_F32_F16, "__truncsfhf2");
 
+  // Define the emscripten name for return address helper.
+  // TODO: when implementing other WASM backends, make this generic or only do
+  // this on emscripten depending on what they end up doing.
+  setLibcallName(RTLIB::RETURN_ADDRESS, "emscripten_return_address");
+
   // Always convert switches to br_tables unless there is only one case, which
   // is equivalent to a simple branch. This reduces code size for wasm, and we
   // defer possible jump table optimizations to the VM.
@@ -919,9 +924,8 @@ SDValue WebAssemblyTargetLowering::LowerOperation(SDValue Op,
   case ISD::BRIND:
     fail(DL, DAG, "WebAssembly hasn't implemented computed gotos");
     return SDValue();
-  case ISD::RETURNADDR: // Probably nothing meaningful can be returned here.
-    fail(DL, DAG, "WebAssembly hasn't implemented __builtin_return_address");
-    return SDValue();
+  case ISD::RETURNADDR:
+    return LowerRETURNADDR(Op, DAG);
   case ISD::FRAMEADDR:
     return LowerFRAMEADDR(Op, DAG);
   case ISD::CopyToReg:
@@ -978,6 +982,26 @@ SDValue WebAssemblyTargetLowering::LowerFrameIndex(SDValue Op,
   return DAG.getTargetFrameIndex(FI, Op.getValueType());
 }
 
+SDValue WebAssemblyTargetLowering::LowerRETURNADDR(SDValue Op,
+                                                   SelectionDAG &DAG) const {
+  SDLoc DL(Op);
+
+  if (!Subtarget->getTargetTriple().isOSEmscripten()) {
+    fail(DL, DAG,
+         "Non-Emscripten WebAssembly hasn't implemented "
+         "__builtin_return_address");
+    return SDValue();
+  }
+
+  if (verifyReturnAddressArgumentIsConstant(Op, DAG))
+    return SDValue();
+
+  unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
+  return makeLibCall(DAG, RTLIB::RETURN_ADDRESS, Op.getValueType(),
+                     {DAG.getConstant(Depth, DL, MVT::i32)}, false, DL)
+      .first;
+}
+
 SDValue WebAssemblyTargetLowering::LowerFRAMEADDR(SDValue Op,
                                                   SelectionDAG &DAG) const {
   // Non-zero depths are not supported by WebAssembly currently. Use the
index 81e85fc8a0c1741d4000789b7f0df90f50f9fb56..f899f0feee417f253b918f2a8cff9b610f5afcef 100644 (file)
@@ -88,6 +88,7 @@ private:
   // Custom lowering hooks.
   SDValue LowerOperation(SDValue Op, SelectionDAG &DAG) const override;
   SDValue LowerFrameIndex(SDValue Op, SelectionDAG &DAG) const;
+  SDValue LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const;
   SDValue LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const;
   SDValue LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const;
   SDValue LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const;
index 2bc602034ba98a0292cd40fe4a8bcd799cc4517a..5a86b27cac02a878453d7575c8f9ac8480d28c32 100644 (file)
@@ -325,6 +325,9 @@ struct RuntimeLibcallSignatureTable {
     // __stack_chk_fail
     Table[RTLIB::STACKPROTECTOR_CHECK_FAIL] = func;
 
+    // Return address handling
+    Table[RTLIB::RETURN_ADDRESS] = i32_func_i32;
+
     // Element-wise Atomic memory
     // TODO: Fix these when we implement atomic support
     Table[RTLIB::MEMCPY_ELEMENT_UNORDERED_ATOMIC_1] = unsupported;
@@ -499,6 +502,8 @@ struct StaticLibcallNameMap {
     // consistent with the f64 and f128 names.
     Map["__extendhfsf2"] = RTLIB::FPEXT_F16_F32;
     Map["__truncsfhf2"] = RTLIB::FPROUND_F32_F16;
+
+    Map["emscripten_return_address"] = RTLIB::RETURN_ADDRESS;
   }
 };
 
diff --git a/test/CodeGen/WebAssembly/return-address-emscripten.ll b/test/CodeGen/WebAssembly/return-address-emscripten.ll
new file mode 100644 (file)
index 0000000..72d0393
--- /dev/null
@@ -0,0 +1,19 @@
+; RUN: llc < %s -asm-verbose=false | FileCheck %s
+
+target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128"
+target triple = "wasm32-unknown-emscripten"
+
+; This tests the implementation of __builtin_return_address on emscripten
+
+; CHECK-LABEL: test_returnaddress:
+; CHECK-NEXT: .functype test_returnaddress () -> (i32){{$}}
+; CHECK-NEXT: {{^}} i32.const 0{{$}}
+; CHECK-NEXT: {{^}} i32.call emscripten_return_address{{$}}
+; CHECK-NEXT: {{^}} end_function{{$}}
+define i8* @test_returnaddress() {
+  %r = call i8* @llvm.returnaddress(i32 0)
+  ret i8* %r
+}
+
+; LLVM represents __builtin_return_address as call to this function in IR.
+declare i8* @llvm.returnaddress(i32 immarg)
diff --git a/test/CodeGen/WebAssembly/return-address-unknown.ll b/test/CodeGen/WebAssembly/return-address-unknown.ll
new file mode 100644 (file)
index 0000000..85f3e3e
--- /dev/null
@@ -0,0 +1,16 @@
+; RUN: not llc < %s -asm-verbose=false 2>&1 | FileCheck %s
+
+target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128"
+target triple = "wasm32-unknown-unknown"
+
+; This tests the implementation of __builtin_return_address on the unknown OS.
+; Since this is not implemented, it should fail.
+
+; CHECK: Non-Emscripten WebAssembly hasn't implemented __builtin_return_address
+define i8* @test_returnaddress() {
+  %r = call i8* @llvm.returnaddress(i32 0)
+  ret i8* %r
+}
+
+; LLVM represents __builtin_return_address as call to this function in IR.
+declare i8* @llvm.returnaddress(i32 immarg)