[WebAssembly] Add exception handling option and feature
authorHeejin Ahn <aheejin@gmail.com>
Sat, 24 Feb 2018 00:40:50 +0000 (00:40 +0000)
committerHeejin Ahn <aheejin@gmail.com>
Sat, 24 Feb 2018 00:40:50 +0000 (00:40 +0000)
Summary:
Add a llc command line option and WebAssembly architecture feature for
exception handling.

Reviewers: dschuff

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

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

llvm-svn: 326004

llvm/include/llvm/CodeGen/CommandFlags.def
llvm/include/llvm/MC/MCTargetOptions.h
llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
llvm/lib/CodeGen/TargetPassConfig.cpp
llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyMCAsmInfo.cpp
llvm/lib/Target/WebAssembly/WebAssembly.td
llvm/lib/Target/WebAssembly/WebAssemblyInstrInfo.td
llvm/lib/Target/WebAssembly/WebAssemblySubtarget.cpp
llvm/lib/Target/WebAssembly/WebAssemblySubtarget.h
llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.cpp

index d7a5c94..84cfe9d 100644 (file)
@@ -98,7 +98,9 @@ static cl::opt<llvm::ExceptionHandling> ExceptionModel(
         clEnumValN(ExceptionHandling::SjLj, "sjlj", "SjLj exception handling"),
         clEnumValN(ExceptionHandling::ARM, "arm", "ARM EHABI exceptions"),
         clEnumValN(ExceptionHandling::WinEH, "wineh",
-                   "Windows exception model")));
+                   "Windows exception model"),
+        clEnumValN(ExceptionHandling::Wasm, "wasm",
+                   "WebAssembly exception handling")));
 
 static cl::opt<TargetMachine::CodeGenFileType> FileType(
     "filetype", cl::init(TargetMachine::CGFT_AssemblyFile),
index 5509bb3..f5d330f 100644 (file)
@@ -21,6 +21,7 @@ enum class ExceptionHandling {
   SjLj,     /// setjmp/longjmp based exceptions
   ARM,      /// ARM EHABI
   WinEH,    /// Windows Exception Handling
+  Wasm,     /// WebAssembly Exception Handling
 };
 
 enum class DebugCompressionType {
index 992eb40..979a03c 100644 (file)
@@ -353,6 +353,9 @@ bool AsmPrinter::doInitialization(Module &M) {
       break;
     }
     break;
+  case ExceptionHandling::Wasm:
+    // TODO to prevent warning
+    break;
   }
   if (ES)
     Handlers.push_back(HandlerInfo(ES, EHTimerName, EHTimerDescription,
index 9628363..bfe8f15 100644 (file)
@@ -653,6 +653,9 @@ void TargetPassConfig::addPassesToHandleExceptions() {
     addPass(createWinEHPass());
     addPass(createDwarfEHPass());
     break;
+  case ExceptionHandling::Wasm:
+    // TODO to prevent warning
+    break;
   case ExceptionHandling::None:
     addPass(createLowerInvokePass());
 
index 5f8c78e..c00dc19 100644 (file)
@@ -43,9 +43,6 @@ WebAssemblyMCAsmInfoELF::WebAssemblyMCAsmInfoELF(const Triple &T) {
 
   SupportsDebugInformation = true;
 
-  // For now, WebAssembly does not support exceptions.
-  ExceptionsType = ExceptionHandling::None;
-
   // TODO: UseIntegratedAssembler?
 
   // WebAssembly's stack is never executable.
@@ -76,8 +73,5 @@ WebAssemblyMCAsmInfo::WebAssemblyMCAsmInfo(const Triple &T) {
 
   SupportsDebugInformation = true;
 
-  // For now, WebAssembly does not support exceptions.
-  ExceptionsType = ExceptionHandling::None;
-
   // TODO: UseIntegratedAssembler?
 }
index 76b3ddb..9d626bd 100644 (file)
@@ -37,6 +37,10 @@ def FeatureSignExt :
                        "HasSignExt", "true",
                        "Enable sign extension operators">;
 
+def FeatureExceptionHandling :
+      SubtargetFeature<"exception-handling", "HasExceptionHandling", "true",
+                       "Enable Wasm exception handling">;
+
 //===----------------------------------------------------------------------===//
 // Architectures.
 //===----------------------------------------------------------------------===//
index 245d5ab..f5a1be1 100644 (file)
@@ -39,6 +39,16 @@ def NotHasSignExt :
               AssemblerPredicate<"!FeatureSignExt",
                                  "sign-ext">;
 
+def HasExceptionHandling :
+    Predicate<"Subtarget->hasExceptionHandling()">,
+              AssemblerPredicate<"FeatureExceptionHandling",
+                                 "exception-handling">;
+
+def NotHasExceptionHandling :
+    Predicate<"!Subtarget->hasExceptionHandling()">,
+              AssemblerPredicate<"!FeatureExceptionHandling",
+                                 "exception-handling">;
+
 //===----------------------------------------------------------------------===//
 // WebAssembly-specific DAG Node Types.
 //===----------------------------------------------------------------------===//
index 78602a3..6addaa3 100644 (file)
@@ -42,8 +42,8 @@ WebAssemblySubtarget::WebAssemblySubtarget(const Triple &TT,
                                            const TargetMachine &TM)
     : WebAssemblyGenSubtargetInfo(TT, CPU, FS), HasSIMD128(false),
       HasAtomics(false), HasNontrappingFPToInt(false), HasSignExt(false),
-      CPUString(CPU), TargetTriple(TT), FrameLowering(),
-      InstrInfo(initializeSubtargetDependencies(FS)), TSInfo(),
+      HasExceptionHandling(false), CPUString(CPU), TargetTriple(TT),
+      FrameLowering(), InstrInfo(initializeSubtargetDependencies(FS)), TSInfo(),
       TLInfo(TM, *this) {}
 
 bool WebAssemblySubtarget::enableMachineScheduler() const {
index c999f50..c2ced23 100644 (file)
@@ -33,6 +33,7 @@ class WebAssemblySubtarget final : public WebAssemblyGenSubtargetInfo {
   bool HasAtomics;
   bool HasNontrappingFPToInt;
   bool HasSignExt;
+  bool HasExceptionHandling;
 
   /// String name of used CPU.
   std::string CPUString;
@@ -80,6 +81,7 @@ public:
   bool hasAtomics() const { return HasAtomics; }
   bool hasNontrappingFPToInt() const { return HasNontrappingFPToInt; }
   bool hasSignExt() const { return HasSignExt; }
+  bool hasExceptionHandling() const { return HasExceptionHandling; }
 
   /// Parses features string setting specified subtarget options. Definition of
   /// function is auto generated by tblgen.
index d38cde7..a686e77 100644 (file)
@@ -190,7 +190,8 @@ void WebAssemblyPassConfig::addIRPasses() {
   // blocks. Lowering invokes when there is no EH support is done in
   // TargetPassConfig::addPassesToHandleExceptions, but this runs after this
   // function and SjLj handling expects all invokes to be lowered before.
-  if (!EnableEmException) {
+  if (!EnableEmException &&
+      TM->Options.ExceptionModel == ExceptionHandling::None) {
     addPass(createLowerInvokePass());
     // The lower invoke pass may create unreachable code. Remove it in order not
     // to process dead blocks in setjmp/longjmp handling.