[llvm] r361454 - [WebAssembly] Implement __builtin_return_address for emscripten
Thomas Lively via llvm-commits
llvm-commits at lists.llvm.org
Wed May 22 18:24:01 PDT 2019
Author: tlively
Date: Wed May 22 18:24:01 2019
New Revision: 361454
URL: http://llvm.org/viewvc/llvm-project?rev=361454&view=rev
Log:
[WebAssembly] Implement __builtin_return_address for emscripten
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
Added:
llvm/trunk/test/CodeGen/WebAssembly/return-address-emscripten.ll
llvm/trunk/test/CodeGen/WebAssembly/return-address-unknown.ll
Modified:
llvm/trunk/include/llvm/IR/RuntimeLibcalls.def
llvm/trunk/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp
llvm/trunk/lib/Target/WebAssembly/WebAssemblyISelLowering.h
llvm/trunk/lib/Target/WebAssembly/WebAssemblyRuntimeLibcallSignatures.cpp
Modified: llvm/trunk/include/llvm/IR/RuntimeLibcalls.def
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/IR/RuntimeLibcalls.def?rev=361454&r1=361453&r2=361454&view=diff
==============================================================================
--- llvm/trunk/include/llvm/IR/RuntimeLibcalls.def (original)
+++ llvm/trunk/include/llvm/IR/RuntimeLibcalls.def Wed May 22 18:24:01 2019
@@ -539,6 +539,9 @@ HANDLE_LIBCALL(STACKPROTECTOR_CHECK_FAIL
// Deoptimization
HANDLE_LIBCALL(DEOPTIMIZE, "__llvm_deoptimize")
+// Return address
+HANDLE_LIBCALL(RETURN_ADDRESS, nullptr)
+
HANDLE_LIBCALL(UNKNOWN_LIBCALL, nullptr)
#undef HANDLE_LIBCALL
Modified: llvm/trunk/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp?rev=361454&r1=361453&r2=361454&view=diff
==============================================================================
--- llvm/trunk/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp (original)
+++ llvm/trunk/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp Wed May 22 18:24:01 2019
@@ -273,6 +273,11 @@ WebAssemblyTargetLowering::WebAssemblyTa
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::Lower
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::Lower
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
Modified: llvm/trunk/lib/Target/WebAssembly/WebAssemblyISelLowering.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/WebAssembly/WebAssemblyISelLowering.h?rev=361454&r1=361453&r2=361454&view=diff
==============================================================================
--- llvm/trunk/lib/Target/WebAssembly/WebAssemblyISelLowering.h (original)
+++ llvm/trunk/lib/Target/WebAssembly/WebAssemblyISelLowering.h Wed May 22 18:24:01 2019
@@ -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;
Modified: llvm/trunk/lib/Target/WebAssembly/WebAssemblyRuntimeLibcallSignatures.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/WebAssembly/WebAssemblyRuntimeLibcallSignatures.cpp?rev=361454&r1=361453&r2=361454&view=diff
==============================================================================
--- llvm/trunk/lib/Target/WebAssembly/WebAssemblyRuntimeLibcallSignatures.cpp (original)
+++ llvm/trunk/lib/Target/WebAssembly/WebAssemblyRuntimeLibcallSignatures.cpp Wed May 22 18:24:01 2019
@@ -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;
}
};
Added: llvm/trunk/test/CodeGen/WebAssembly/return-address-emscripten.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/WebAssembly/return-address-emscripten.ll?rev=361454&view=auto
==============================================================================
--- llvm/trunk/test/CodeGen/WebAssembly/return-address-emscripten.ll (added)
+++ llvm/trunk/test/CodeGen/WebAssembly/return-address-emscripten.ll Wed May 22 18:24:01 2019
@@ -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)
Added: llvm/trunk/test/CodeGen/WebAssembly/return-address-unknown.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/WebAssembly/return-address-unknown.ll?rev=361454&view=auto
==============================================================================
--- llvm/trunk/test/CodeGen/WebAssembly/return-address-unknown.ll (added)
+++ llvm/trunk/test/CodeGen/WebAssembly/return-address-unknown.ll Wed May 22 18:24:01 2019
@@ -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)
More information about the llvm-commits
mailing list