From b5d9d20f6aaa92e2832cec914ce7c1df7f5ce1e7 Mon Sep 17 00:00:00 2001
From: Heejin Ahn <aheejin@gmail.com>
Date: Thu, 23 Jan 2020 16:39:32 -0800
Subject: [PATCH] [WebAssembly] Fix resume-only case in Emscripten EH

Summary:
D72308 incorrectly assumed `resume` cannot exist without a `landingpad`,
which is not true. This sets `Changed` to true whenever we make changes
to a function, including creating a call to `__resumeException` within a
function without a landing pad.

Reviewers: tlively

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

Tags: #llvm

Differential Revision: https://reviews.llvm.org/D73308
---
 .../WebAssemblyLowerEmscriptenEHSjLj.cpp      |  5 +++-
 .../lower-em-exceptions-lpad-only.ll          | 25 -------------------
 .../lower-em-exceptions-resume-only.ll        | 17 +++++++++++++
 3 files changed, 21 insertions(+), 26 deletions(-)
 delete mode 100644 llvm/test/CodeGen/WebAssembly/lower-em-exceptions-lpad-only.ll
 create mode 100644 llvm/test/CodeGen/WebAssembly/lower-em-exceptions-resume-only.ll

diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyLowerEmscriptenEHSjLj.cpp b/llvm/lib/Target/WebAssembly/WebAssemblyLowerEmscriptenEHSjLj.cpp
index b93ae9a692c2a..aa6a95d0abbff 100644
--- a/llvm/lib/Target/WebAssembly/WebAssemblyLowerEmscriptenEHSjLj.cpp
+++ b/llvm/lib/Target/WebAssembly/WebAssemblyLowerEmscriptenEHSjLj.cpp
@@ -755,6 +755,7 @@ bool WebAssemblyLowerEmscriptenEHSjLj::runEHOnFunction(Function &F) {
     auto *II = dyn_cast<InvokeInst>(BB.getTerminator());
     if (!II)
       continue;
+    Changed = true;
     LandingPads.insert(II->getLandingPadInst());
     IRB.SetInsertPoint(II);
 
@@ -795,6 +796,7 @@ bool WebAssemblyLowerEmscriptenEHSjLj::runEHOnFunction(Function &F) {
       auto *RI = dyn_cast<ResumeInst>(&I);
       if (!RI)
         continue;
+      Changed = true;
 
       // Split the input into legal values
       Value *Input = RI->getValue();
@@ -819,6 +821,7 @@ bool WebAssemblyLowerEmscriptenEHSjLj::runEHOnFunction(Function &F) {
         continue;
       if (Callee->getIntrinsicID() != Intrinsic::eh_typeid_for)
         continue;
+      Changed = true;
 
       IRB.SetInsertPoint(CI);
       CallInst *NewCI =
@@ -834,7 +837,7 @@ bool WebAssemblyLowerEmscriptenEHSjLj::runEHOnFunction(Function &F) {
     if (auto *LPI = dyn_cast<LandingPadInst>(I))
       LandingPads.insert(LPI);
   }
-  Changed = !LandingPads.empty();
+  Changed |= !LandingPads.empty();
 
   // Handle all the landingpad for this function together, as multiple invokes
   // may share a single lp
diff --git a/llvm/test/CodeGen/WebAssembly/lower-em-exceptions-lpad-only.ll b/llvm/test/CodeGen/WebAssembly/lower-em-exceptions-lpad-only.ll
deleted file mode 100644
index 6d5f2e8365427..0000000000000
--- a/llvm/test/CodeGen/WebAssembly/lower-em-exceptions-lpad-only.ll
+++ /dev/null
@@ -1,25 +0,0 @@
-; RUN: opt < %s -wasm-lower-em-ehsjlj -S | FileCheck %s
-
-target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128"
-target triple = "wasm32-unknown-unknown"
-
-@_ZTIi = external constant i8*
-
-; Checks if a module that only contains a landingpad (and resume) but not an
-; invoke works correctly and does not crash.
-; CHECK-LABEL: @landingpad_only
-define void @landingpad_only() personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) {
-entry:
-  br label %cont
-
-lpad:                                             ; preds = %entry
-  %0 = landingpad { i8*, i32 }
-          catch i8* bitcast (i8** @_ZTIi to i8*)
-          catch i8* null
-  resume { i8*, i32 } %0
-
-cont:
-  ret void
-}
-
-declare i32 @__gxx_personality_v0(...)
diff --git a/llvm/test/CodeGen/WebAssembly/lower-em-exceptions-resume-only.ll b/llvm/test/CodeGen/WebAssembly/lower-em-exceptions-resume-only.ll
new file mode 100644
index 0000000000000..4360d02195ee8
--- /dev/null
+++ b/llvm/test/CodeGen/WebAssembly/lower-em-exceptions-resume-only.ll
@@ -0,0 +1,17 @@
+; RUN: opt < %s -wasm-lower-em-ehsjlj -S | FileCheck %s
+
+target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128"
+target triple = "wasm32-unknown-unknown"
+
+; Checks if a module that only contains a resume but not an invoke works
+; correctly and does not crash.
+; CHECK-LABEL: @resume_only
+; CHECK: call void @__resumeException
+define void @resume_only() personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) {
+entry:
+  %val0 = insertvalue { i8*, i32 } undef, i8* null, 0
+  %val1 = insertvalue { i8*, i32} %val0, i32 0, 1
+  resume { i8*, i32 } %val1
+}
+
+declare i32 @__gxx_personality_v0(...)