[llvm] c92d459 - [WebAssembly] Fix typechecking for else MCInst

Paulo Matos via llvm-commits llvm-commits at lists.llvm.org
Thu Dec 16 02:24:42 PST 2021


Author: Paulo Matos
Date: 2021-12-16T11:18:01+01:00
New Revision: c92d45913c43f0c03633ed5b2da0d568b3dbc5b1

URL: https://github.com/llvm/llvm-project/commit/c92d45913c43f0c03633ed5b2da0d568b3dbc5b1
DIFF: https://github.com/llvm/llvm-project/commit/c92d45913c43f0c03633ed5b2da0d568b3dbc5b1.diff

LOG: [WebAssembly] Fix typechecking for else MCInst

When hitting an else clause the type Stack should be reset to as it was at the start of the if, without taking into account the Type inserted into the Stack during the then branch of the if.

Reviewed By: aardappel

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

Added: 
    llvm/test/MC/WebAssembly/typechecking-else.s

Modified: 
    llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmTypeCheck.cpp
    llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmTypeCheck.h

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmTypeCheck.cpp b/llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmTypeCheck.cpp
index a6b5d4252f2f9..128ce5c4fec0a 100644
--- a/llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmTypeCheck.cpp
+++ b/llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmTypeCheck.cpp
@@ -112,9 +112,18 @@ bool WebAssemblyAsmTypeCheck::getLocal(SMLoc ErrorLoc, const MCInst &Inst,
   return false;
 }
 
-bool WebAssemblyAsmTypeCheck::checkEnd(SMLoc ErrorLoc) {
+bool WebAssemblyAsmTypeCheck::checkEnd(SMLoc ErrorLoc, bool PopVals) {
   if (LastSig.Returns.size() > Stack.size())
     return typeError(ErrorLoc, "end: insufficient values on the type stack");
+  
+  if (PopVals) {
+    for (auto VT : llvm::reverse(LastSig.Returns)) {
+      if (popType(ErrorLoc, VT)) 
+        return true;
+    }
+    return false;
+  }
+  
   for (size_t i = 0; i < LastSig.Returns.size(); i++) {
     auto EVT = LastSig.Returns[i];
     auto PVT = Stack[Stack.size() - LastSig.Returns.size() + i];
@@ -221,7 +230,7 @@ bool WebAssemblyAsmTypeCheck::typeCheck(SMLoc ErrorLoc, const MCInst &Inst) {
       return true;
   } else if (Name == "end_block" || Name == "end_loop" || Name == "end_if" ||
              Name == "else" || Name == "end_try") {
-    if (checkEnd(ErrorLoc))
+    if (checkEnd(ErrorLoc, Name == "else"))
       return true;
     if (Name == "end_block")
       Unreachable = false;

diff  --git a/llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmTypeCheck.h b/llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmTypeCheck.h
index aa35213ccca3b..2b07faf67a182 100644
--- a/llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmTypeCheck.h
+++ b/llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmTypeCheck.h
@@ -39,7 +39,7 @@ class WebAssemblyAsmTypeCheck final {
   bool typeError(SMLoc ErrorLoc, const Twine &Msg);
   bool popType(SMLoc ErrorLoc, Optional<wasm::ValType> EVT);
   bool getLocal(SMLoc ErrorLoc, const MCInst &Inst, wasm::ValType &Type);
-  bool checkEnd(SMLoc ErrorLoc);
+  bool checkEnd(SMLoc ErrorLoc, bool PopVals = false);
   bool checkSig(SMLoc ErrorLoc, const wasm::WasmSignature &Sig);
   bool getSymRef(SMLoc ErrorLoc, const MCInst &Inst,
                  const MCSymbolRefExpr *&SymRef);

diff  --git a/llvm/test/MC/WebAssembly/typechecking-else.s b/llvm/test/MC/WebAssembly/typechecking-else.s
new file mode 100644
index 0000000000000..184090171f7cf
--- /dev/null
+++ b/llvm/test/MC/WebAssembly/typechecking-else.s
@@ -0,0 +1,21 @@
+# Check that it converts to .o without errors
+# RUN: llvm-mc -triple=wasm32-unknown-unknown -filetype=obj -mattr=+reference-types < %s
+
+objects:
+    .tabletype objects,externref
+
+handle_value:
+    .hidden handle_value
+    .globl handle_value
+    .type handle_value, at function
+    .functype       handle_value (i32) -> (externref)
+    local.get 0
+    i32.const -1
+    i32.eq
+    if externref
+    ref.null_extern
+    else
+    local.get 0
+    table.get objects
+    end_if
+    end_function


        


More information about the llvm-commits mailing list