[WebAssembly] Support instruction selection for catching exceptions

Summary:
This lowers exception catching-related instructions:
1. Lowers `wasm.catch` intrinsic to `catch` instruction
2. Removes `catchpad` and `cleanuppad` instructions; they are not
necessary after isel phase. (`MachineBasicBlock::isEHFuncletEntry()` or
`MachineBasicBlock::isEHPad()` can be used instead.)
3. Lowers `catchret` and `cleanupret` instructions to pseudo `catchret`
and `cleanupret` instructions in isel, which will be replaced with other
instructions in `WebAssemblyExceptionPrepare` pass.
4. Adds 'WebAssemblyExceptionPrepare` pass, which is for running various
transformation for EH. Currently this pass only replaces `catchret` and
`cleanupret` instructions into appropriate wasm instructions to make
this patch successfully run until the end.

Currently this does not handle lowering of intrinsics related to LSDA
info generation (`wasm.landingpad.index` and `wasm.lsda`), because they
cannot be tested without implementing `EHStreamer`'s wasm-specific
handlers. They are marked as TODO, which is needed to make isel pass.
Also this does not generate `try` and `end_try` markers yet, which will
be handled in later patches.

This patch is based on the first wasm EH proposal.
(https://github.com/WebAssembly/exception-handling/blob/master/proposals/Exceptions.md)

Reviewers: dschuff, majnemer

Subscribers: jfb, sbc100, jgravelle-google, sunfish, llvm-commits

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

llvm-svn: 333705
This commit is contained in:
Heejin Ahn 2018-05-31 22:25:54 +00:00
parent cd81614e8b
commit 5ef4d5f9c1
13 changed files with 233 additions and 23 deletions

View File

@ -1380,14 +1380,17 @@ void SelectionDAGBuilder::visitCatchPad(const CatchPadInst &I) {
bool IsMSVCCXX = Pers == EHPersonality::MSVC_CXX;
bool IsCoreCLR = Pers == EHPersonality::CoreCLR;
bool IsSEH = isAsynchronousEHPersonality(Pers);
bool IsWasmCXX = Pers == EHPersonality::Wasm_CXX;
MachineBasicBlock *CatchPadMBB = FuncInfo.MBB;
if (!IsSEH)
CatchPadMBB->setIsEHScopeEntry();
// In MSVC C++ and CoreCLR, catchblocks are funclets and need prologues.
if (IsMSVCCXX || IsCoreCLR)
CatchPadMBB->setIsEHFuncletEntry();
DAG.setRoot(DAG.getNode(ISD::CATCHPAD, getCurSDLoc(), MVT::Other, getControlRoot()));
// Wasm does not need catchpads anymore
if (!IsWasmCXX)
DAG.setRoot(DAG.getNode(ISD::CATCHPAD, getCurSDLoc(), MVT::Other,
getControlRoot()));
}
void SelectionDAGBuilder::visitCatchRet(const CatchReturnInst &I) {
@ -6172,6 +6175,12 @@ SelectionDAGBuilder::visitIntrinsicCall(const CallInst &I, unsigned Intrinsic) {
HasTailCall = true;
return nullptr;
}
case Intrinsic::wasm_landingpad_index: {
// TODO store landing pad index in a map, which will be used when generating
// LSDA information
return nullptr;
}
}
}
@ -6321,7 +6330,10 @@ SelectionDAGBuilder::lowerInvokable(TargetLowering::CallLoweringInfo &CLI,
DAG.setRoot(DAG.getEHLabel(getCurSDLoc(), getRoot(), EndLabel));
// Inform MachineModuleInfo of range.
if (MF.hasEHFunclets()) {
auto Pers = classifyEHPersonality(FuncInfo.Fn->getPersonalityFn());
// There is a platform (e.g. wasm) that uses funclet style IR but does not
// actually use outlined funclets and their LSDA info style.
if (MF.hasEHFunclets() && isFuncletEHPersonality(Pers)) {
assert(CLI.CS);
WinEHFuncInfo *EHInfo = DAG.getMachineFunction().getWinEHFuncInfo();
EHInfo->addIPToStateRange(cast<InvokeInst>(CLI.CS.getInstruction()),

View File

@ -18,6 +18,7 @@ add_llvm_target(WebAssemblyCodeGen
WebAssemblyCallIndirectFixup.cpp
WebAssemblyCFGStackify.cpp
WebAssemblyCFGSort.cpp
WebAssemblyExceptionPrepare.cpp
WebAssemblyExplicitLocals.cpp
WebAssemblyFastISel.cpp
WebAssemblyFixIrreducibleControlFlow.cpp

View File

@ -46,6 +46,7 @@ FunctionPass *createWebAssemblyRegStackify();
FunctionPass *createWebAssemblyRegColoring();
FunctionPass *createWebAssemblyExplicitLocals();
FunctionPass *createWebAssemblyFixIrreducibleControlFlow();
FunctionPass *createWebAssemblyExceptionPrepare();
FunctionPass *createWebAssemblyCFGSort();
FunctionPass *createWebAssemblyCFGStackify();
FunctionPass *createWebAssemblyLowerBrUnless();
@ -68,6 +69,7 @@ void initializeWebAssemblyRegStackifyPass(PassRegistry &);
void initializeWebAssemblyRegColoringPass(PassRegistry &);
void initializeWebAssemblyExplicitLocalsPass(PassRegistry &);
void initializeWebAssemblyFixIrreducibleControlFlowPass(PassRegistry &);
void initializeWebAssemblyExceptionPreparePass(PassRegistry &);
void initializeWebAssemblyCFGSortPass(PassRegistry &);
void initializeWebAssemblyCFGStackifyPass(PassRegistry &);
void initializeWebAssemblyLowerBrUnlessPass(PassRegistry &);

View File

@ -145,9 +145,6 @@ static void PlaceBlockMarker(
std::prev(InsertPos)->getOpcode() != WebAssembly::END_LOOP)
--InsertPos;
}
// The header block in which a 'block' mark will be inserted should have a
// terminator because it is branching to a non-layout successor.
assert(InsertPos != Header->end());
// Add the BLOCK.
MachineInstr *Begin =

View File

@ -0,0 +1,88 @@
//=== WebAssemblyExceptionPrepare.cpp - WebAssembly Exception Preparation -===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
///
/// \file
/// \brief Does various transformations for exception handling.
///
//===----------------------------------------------------------------------===//
#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
#include "WebAssembly.h"
#include "WebAssemblySubtarget.h"
#include "llvm/CodeGen/MachineInstrBuilder.h"
using namespace llvm;
#define DEBUG_TYPE "wasm-exception-prepare"
namespace {
class WebAssemblyExceptionPrepare final : public MachineFunctionPass {
StringRef getPassName() const override {
return "WebAssembly Prepare Exception";
}
bool runOnMachineFunction(MachineFunction &MF) override;
bool replaceFuncletReturns(MachineFunction &MF);
public:
static char ID; // Pass identification, replacement for typeid
WebAssemblyExceptionPrepare() : MachineFunctionPass(ID) {}
};
} // end anonymous namespace
char WebAssemblyExceptionPrepare::ID = 0;
INITIALIZE_PASS(WebAssemblyExceptionPrepare, DEBUG_TYPE,
"WebAssembly Exception Preparation", false, false)
FunctionPass *llvm::createWebAssemblyExceptionPrepare() {
return new WebAssemblyExceptionPrepare();
}
bool WebAssemblyExceptionPrepare::runOnMachineFunction(MachineFunction &MF) {
bool Changed = false;
if (!MF.getFunction().hasPersonalityFn())
return false;
Changed |= replaceFuncletReturns(MF);
// TODO More transformations will be added
return Changed;
}
bool WebAssemblyExceptionPrepare::replaceFuncletReturns(MachineFunction &MF) {
bool Changed = false;
const auto &TII = *MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
for (auto &MBB : MF) {
auto Pos = MBB.getFirstTerminator();
if (Pos == MBB.end())
continue;
MachineInstr *TI = &*Pos;
switch (TI->getOpcode()) {
case WebAssembly::CATCHRET: {
// Replace a catchret with a branch
MachineBasicBlock *TBB = TI->getOperand(0).getMBB();
if (!MBB.isLayoutSuccessor(TBB))
BuildMI(MBB, TI, TI->getDebugLoc(), TII.get(WebAssembly::BR))
.addMBB(TBB);
TI->eraseFromParent();
Changed = true;
break;
}
case WebAssembly::CLEANUPRET: {
// Replace a cleanupret with a rethrow
BuildMI(MBB, TI, TI->getDebugLoc(), TII.get(WebAssembly::RETHROW))
.addImm(0);
TI->eraseFromParent();
Changed = true;
break;
}
}
}
return Changed;
}

View File

@ -151,6 +151,9 @@ WebAssemblyTargetLowering::WebAssemblyTargetLowering(
// Trap lowers to wasm unreachable
setOperationAction(ISD::TRAP, MVT::Other, Legal);
// Exception handling intrinsics
setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
setMaxAtomicSizeInBitsSupported(64);
}
@ -737,6 +740,8 @@ SDValue WebAssemblyTargetLowering::LowerOperation(SDValue Op,
return LowerFRAMEADDR(Op, DAG);
case ISD::CopyToReg:
return LowerCopyToReg(Op, DAG);
case ISD::INTRINSIC_WO_CHAIN:
return LowerINTRINSIC_WO_CHAIN(Op, DAG);
}
}
@ -869,6 +874,21 @@ SDValue WebAssemblyTargetLowering::LowerVASTART(SDValue Op,
MachinePointerInfo(SV), 0);
}
SDValue
WebAssemblyTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
SelectionDAG &DAG) const {
unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
SDLoc DL(Op);
switch (IntNo) {
default:
return {}; // Don't custom lower most intrinsics.
case Intrinsic::wasm_lsda:
// TODO For now, just return 0 not to crash
return DAG.getConstant(0, DL, Op.getValueType());
}
}
//===----------------------------------------------------------------------===//
// WebAssembly Optimization Hooks
//===----------------------------------------------------------------------===//

View File

@ -90,6 +90,7 @@ class WebAssemblyTargetLowering final : public TargetLowering {
SDValue LowerJumpTable(SDValue Op, SelectionDAG &DAG) const;
SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) const;
SDValue LowerCopyToReg(SDValue Op, SelectionDAG &DAG) const;
SDValue LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const;
};
namespace WebAssembly {

View File

@ -140,9 +140,25 @@ def TRY : I<(outs), (ins Signature:$sig), [], "try \t$sig", 0x06>;
def END_TRY : I<(outs), (ins), [], "end_try", 0x0b>;
} // Uses = [VALUE_STACK], Defs = [VALUE_STACK]
} // Defs = [ARGUMENTS]
// Catching an exception: catch / catch_all
let hasCtrlDep = 1 in {
def CATCH_I32 : I<(outs I32:$dst), (ins i32imm:$tag),
[(set I32:$dst, (int_wasm_catch imm:$tag))],
"i32.catch \t$dst, $tag", 0x07>;
def CATCH_I64 : I<(outs I64:$dst), (ins i32imm:$tag),
[(set I64:$dst, (int_wasm_catch imm:$tag))],
"i64.catch \t$dst, $tag", 0x07>;
def CATCH_ALL : I<(outs), (ins), [], "catch_all", 0x05>;
}
// rethrow takes a relative depth as an argument, for which currently only 0 is
// possible for C++. Once other languages need depths other than 0, depths will
// be computed in CFGStackify.
def : Pat<(int_wasm_rethrow), (RETHROW 0)>;
// Pseudo instructions: cleanupret / catchret
// They are not return instructions in wasm, but setting 'isReturn' to true as
// in X86 is necessary for computing funclet membership.
let isTerminator = 1, hasSideEffects = 1, isBarrier = 1, hasCtrlDep = 1,
isCodeGenOnly = 1, isReturn = 1 in {
def CLEANUPRET : I<(outs), (ins), [(cleanupret)], "", 0>;
def CATCHRET : I<(outs), (ins bb_op:$dst, bb_op:$from),
[(catchret bb:$dst, bb:$from)], "", 0>;
}
} // Defs = [ARGUMENTS]

View File

@ -30,7 +30,8 @@ using namespace llvm;
WebAssemblyInstrInfo::WebAssemblyInstrInfo(const WebAssemblySubtarget &STI)
: WebAssemblyGenInstrInfo(WebAssembly::ADJCALLSTACKDOWN,
WebAssembly::ADJCALLSTACKUP),
WebAssembly::ADJCALLSTACKUP,
WebAssembly::CATCHRET),
RI(STI.getTargetTriple()) {}
bool WebAssemblyInstrInfo::isReallyTriviallyReMaterializable(

View File

@ -160,10 +160,9 @@ static void QueryCallee(const MachineInstr &MI, unsigned CalleeOpNo, bool &Read,
// and/or uses the stack pointer value.
static void Query(const MachineInstr &MI, AliasAnalysis &AA, bool &Read,
bool &Write, bool &Effects, bool &StackPointer) {
assert(!MI.isPosition());
assert(!MI.isTerminator());
if (MI.isDebugInstr())
if (MI.isDebugInstr() || MI.isPosition())
return;
// Check for loads.

View File

@ -45,6 +45,8 @@ public:
const TargetRegisterClass *
getPointerRegClass(const MachineFunction &MF,
unsigned Kind = 0) const override;
// This does not apply to wasm.
const uint32_t *getNoPreservedMask() const override { return nullptr; }
};
} // end namespace llvm

View File

@ -65,6 +65,7 @@ extern "C" void LLVMInitializeWebAssemblyTarget() {
initializeWebAssemblyRegColoringPass(PR);
initializeWebAssemblyExplicitLocalsPass(PR);
initializeWebAssemblyFixIrreducibleControlFlowPass(PR);
initializeWebAssemblyExceptionPreparePass(PR);
initializeWebAssemblyCFGSortPass(PR);
initializeWebAssemblyCFGStackifyPass(PR);
initializeWebAssemblyLowerBrUnlessPass(PR);
@ -320,6 +321,9 @@ void WebAssemblyPassConfig::addPreEmitPass() {
// Insert explicit get_local and set_local operators.
addPass(createWebAssemblyExplicitLocals());
// Do various transformations for exception handling
addPass(createWebAssemblyExceptionPrepare());
// Sort the blocks of the CFG into topological order, a prerequisite for
// BLOCK and LOOP markers.
addPass(createWebAssemblyCFGSort());

View File

@ -1,22 +1,89 @@
; RUN: llc < %s -asm-verbose=false -disable-wasm-fallthrough-return-opt -disable-wasm-explicit-locals | FileCheck %s
; RUN: llc < %s -asm-verbose=false -disable-wasm-fallthrough-return-opt -exception-model=wasm | FileCheck %s
target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128"
target triple = "wasm32-unknown-unknown"
declare void @llvm.wasm.throw(i32, i8*)
declare void @llvm.wasm.rethrow()
%struct.Cleanup = type { i8 }
; CHECK-LABEL: throw:
@_ZTIi = external constant i8*
declare void @llvm.wasm.throw(i32, i8*)
; CHECK-LABEL: test_throw:
; CHECK-NEXT: i32.const $push0=, 0
; CHECK-NEXT: throw 0, $pop0
define void @throw() {
define void @test_throw() {
call void @llvm.wasm.throw(i32 0, i8* null)
ret void
}
; CHECK-LABEL: rethrow:
; CHECK-NEXT: rethrow 0
define void @rethrow() {
call void @llvm.wasm.rethrow()
; CHECK-LABEL: test_catch:
; CHECK: call foo@FUNCTION
; CHECK: i32.catch $push{{.+}}=, 0
; CHECK-DAG: i32.store __wasm_lpad_context
; CHECK-DAG: i32.store __wasm_lpad_context+4
; CHECK: i32.call $push{{.+}}=, _Unwind_CallPersonality@FUNCTION
; CHECK: i32.call $push{{.+}}=, __cxa_begin_catch@FUNCTION
; CHECK: call __cxa_end_catch@FUNCTION
; CHECK: call __cxa_rethrow@FUNCTION
define void @test_catch() personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) {
entry:
invoke void @foo()
to label %try.cont unwind label %catch.dispatch
catch.dispatch: ; preds = %entry
%0 = catchswitch within none [label %catch.start] unwind to caller
catch.start: ; preds = %catch.dispatch
%1 = catchpad within %0 [i8* bitcast (i8** @_ZTIi to i8*)]
%2 = call i8* @llvm.wasm.get.exception(token %1)
%3 = call i32 @llvm.wasm.get.ehselector(token %1)
%4 = call i32 @llvm.eh.typeid.for(i8* bitcast (i8** @_ZTIi to i8*))
%matches = icmp eq i32 %3, %4
br i1 %matches, label %catch, label %rethrow
catch: ; preds = %catch.start
%5 = call i8* @__cxa_begin_catch(i8* %2) [ "funclet"(token %1) ]
call void @__cxa_end_catch() [ "funclet"(token %1) ]
catchret from %1 to label %try.cont
rethrow: ; preds = %catch.start
call void @__cxa_rethrow() [ "funclet"(token %1) ]
unreachable
try.cont: ; preds = %entry, %catch
ret void
}
; CHECK-LABEL: test_cleanup:
; CHECK: call foo@FUNCTION
; CHECK: return
; CHECK: i32.call $push20=, _ZN7CleanupD1Ev@FUNCTION
; CHECK: rethrow 0
define void @test_cleanup() personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) {
entry:
%c = alloca %struct.Cleanup, align 1
invoke void @foo()
to label %invoke.cont unwind label %ehcleanup
invoke.cont: ; preds = %entry
%call = call %struct.Cleanup* @_ZN7CleanupD1Ev(%struct.Cleanup* %c)
ret void
ehcleanup: ; preds = %entry
%0 = cleanuppad within none []
%call1 = call %struct.Cleanup* @_ZN7CleanupD1Ev(%struct.Cleanup* %c) [ "funclet"(token %0) ]
cleanupret from %0 unwind to caller
}
declare void @foo()
declare void @func(i32)
declare i32 @__gxx_wasm_personality_v0(...)
declare i8* @llvm.wasm.get.exception(token)
declare i32 @llvm.wasm.get.ehselector(token)
declare i32 @llvm.eh.typeid.for(i8*)
declare i8* @__cxa_begin_catch(i8*)
declare void @__cxa_end_catch()
declare void @__cxa_rethrow()
declare void @__clang_call_terminate(i8*)
declare %struct.Cleanup* @_ZN7CleanupD1Ev(%struct.Cleanup* returned)