Selaa lähdekoodia

Fix build of when wasm is enabled, but wasm.simd is disabled.
Make sure we don't inadvertently allow Simd type to flow in the engine if it is disabled.

Michael Ferris 8 vuotta sitten
vanhempi
sitoutus
a7aa47c018

+ 9 - 7
lib/Backend/LowerMDSharedSimd128.cpp

@@ -5,13 +5,7 @@
 
 #include "Backend.h"
 
-static IR::Instr* removeInstr(IR::Instr* instr)
-{
-    IR::Instr* prevInstr;
-    prevInstr = instr->m_prev;
-    instr->Remove();
-    return prevInstr;
-}
+static IR::Instr* removeInstr(IR::Instr* instr);
 
 #ifdef ENABLE_SIMDJS
 // FromVar
@@ -448,6 +442,14 @@ IR::Instr* LowererMD::Simd128LowerSelect(IR::Instr *instr)
 
 #if defined(ENABLE_SIMDJS) || defined(ENABLE_WASM_SIMD)
 
+static IR::Instr* removeInstr(IR::Instr* instr)
+{
+    IR::Instr* prevInstr;
+    prevInstr = instr->m_prev;
+    instr->Remove();
+    return prevInstr;
+}
+
 #define GET_SIMDOPCODE(irOpcode) m_simd128OpCodesMap[(uint32)(irOpcode - Js::OpCode::Simd128_Start)]
 
 #define SET_SIMDOPCODE(irOpcode, mdOpcode) \

+ 3 - 0
lib/Runtime/Language/InterpreterHandlerAsmJs.inl

@@ -282,6 +282,8 @@ EXDEF2_WMS( D1toD1Mem, PrintF64, WAsmJs::Tracing::PrintF64 )
 #endif
   //unary ops
 
+#if defined(ENABLE_SIMD_JS) || defined(ENABLE_WASM_SIMD)
+
 EXDEF2_WMS( SIMD_F4_1toF4_1  , Simd128_Ld_F4        , (AsmJsSIMDValue)                                   )
   DEF2_WMS( SIMD_I4_1toI4_1  , Simd128_Ld_I4        , (AsmJsSIMDValue)                                   )
 EXDEF2_WMS( SIMD_B4_1toB4_1  , Simd128_Ld_B4        , (AsmJsSIMDValue)                                   )
@@ -752,5 +754,6 @@ EXDEF3_WMS(CUSTOM_ASMJS, Simd128_LdArr_D2, OP_SimdLdArrGeneric, AsmSimdTypedArr)
 EXDEF3_WMS(CUSTOM_ASMJS, Simd128_LdArrConst_D2, OP_SimdLdArrConstIndex, AsmSimdTypedArr)
 EXDEF3_WMS(CUSTOM_ASMJS, Simd128_StArrConst_D2, OP_SimdStArrConstIndex, AsmSimdTypedArr)
 #endif // 0
+#endif
 
 #endif

+ 3 - 3
lib/Runtime/Language/InterpreterStackFrame.cpp

@@ -2081,7 +2081,7 @@ namespace Js
 #if defined(ENABLE_WASM_SIMD) || defined(ENABLE_SIMDJS)
 
 #ifdef ENABLE_WASM_SIMD
-            if (CONFIG_FLAG(WasmSimd))
+            if (Wasm::Simd::IsEnabled())
 #elif ENABLE_SIMDJS
             if (function->GetScriptContext()->GetConfig()->IsSimdjsEnabled())
 #endif
@@ -3126,7 +3126,7 @@ namespace Js
 #endif
 
 #ifdef ENABLE_WASM_SIMD
-                if (CONFIG_FLAG(WasmSimd) && i == 2) // last argument ?
+                if (Wasm::Simd::IsEnabled() && i == 2) // last argument ?
 #endif
 
 #if defined(ENABLE_SIMDJS) || defined(ENABLE_WASM_SIMD)
@@ -3198,7 +3198,7 @@ namespace Js
 #endif
 
 #ifdef ENABLE_WASM_SIMD
-            else if (CONFIG_FLAG(WasmSimd) && info->GetArgType(i).isSIMD())
+            else if (Wasm::Simd::IsEnabled() && info->GetArgType(i).isSIMD())
 #endif
 
 #if defined(ENABLE_SIMDJS) || defined(ENABLE_WASM_SIMD)

+ 6 - 2
lib/Runtime/Library/WebAssemblyModule.cpp

@@ -37,7 +37,7 @@ WebAssemblyModule::WebAssemblyModule(Js::ScriptContext* scriptContext, const byt
     m_customSections(nullptr)
 {
     m_alloc = HeapNew(ArenaAllocator, _u("WebAssemblyModule"), scriptContext->GetThreadContext()->GetPageAllocator(), Js::Throw::OutOfMemory);
-    //the first elm is the number of Vars in front of I32; makes for a nicer offset computation
+    // the first elem is the number of Vars in front of I32; makes for a nicer offset computation
     memset(m_globalCounts, 0, sizeof(uint) * Wasm::WasmTypes::Limit);
     m_functionsInfo = RecyclerNew(scriptContext->GetRecycler(), WasmFunctionInfosList, scriptContext->GetRecycler());
     m_imports = Anew(m_alloc, WasmImportsList, m_alloc);
@@ -881,7 +881,7 @@ uint
 WebAssemblyModule::GetOffsetForGlobal(Wasm::WasmGlobal* global) const
 {
     Wasm::WasmTypes::WasmType type = global->GetType();
-    if (type >= Wasm::WasmTypes::Limit)
+    if (!Wasm::WasmTypes::IsLocalType(type))
     {
         throw Wasm::WasmCompilationException(_u("Invalid Global type"));
     }
@@ -934,6 +934,10 @@ WebAssemblyModule::GetGlobalsByteSize() const
     uint32 size = 0;
     for (Wasm::WasmTypes::WasmType type = (Wasm::WasmTypes::WasmType)(Wasm::WasmTypes::Void + 1); type < Wasm::WasmTypes::Limit; type = (Wasm::WasmTypes::WasmType)(type + 1))
     {
+        if (!Wasm::WasmTypes::IsLocalType(type))
+        {
+            continue;
+        }
         size = AddGlobalByteSizeToOffset(type, size);
     }
     return size;

+ 40 - 3
lib/WasmReader/WasmBinaryReader.cpp

@@ -14,12 +14,29 @@
 namespace Wasm
 {
 
+namespace Simd
+{
+void EnsureSimdIsEnabled()
+{
+    if (!Wasm::Simd::IsEnabled())
+    {
+        throw WasmCompilationException(_u("Wasm.Simd is not supported"));
+    }
+}
+}
+
 namespace WasmTypes
 {
 
 bool IsLocalType(WasmTypes::WasmType type)
 {
     // Check if type in range ]Void,Limit[
+#ifdef ENABLE_WASM_SIMD
+    if (type == WasmTypes::M128 && !Simd::IsEnabled())
+    {
+        return false;
+    }
+#endif
     return (uint32)(type - 1) < (WasmTypes::Limit - 1);
 }
 
@@ -32,9 +49,12 @@ uint32 GetTypeByteSize(WasmType type)
     case I64: return sizeof(int64);
     case F32: return sizeof(float);
     case F64: return sizeof(double);
+#ifdef ENABLE_WASM_SIMD
     case M128:
+        Simd::EnsureSimdIsEnabled();
         CompileAssert(sizeof(Simd::simdvec) == 16);
         return sizeof(Simd::simdvec);
+#endif
     case Ptr: return sizeof(void*);
     default:
         Js::Throw::InternalError();
@@ -49,7 +69,11 @@ const char16 * GetTypeName(WasmType type)
     case WasmTypes::WasmType::I64: return _u("i64");
     case WasmTypes::WasmType::F32: return _u("f32");
     case WasmTypes::WasmType::F64: return _u("f64");
-    case WasmTypes::WasmType::M128: return _u("m128");
+#ifdef ENABLE_WASM_SIMD
+    case WasmTypes::WasmType::M128: 
+        Simd::EnsureSimdIsEnabled();
+        return _u("m128");
+#endif
     case WasmTypes::WasmType::Any: return _u("any");
     default: Assert(UNREACHED); break;
     }
@@ -66,7 +90,11 @@ WasmTypes::WasmType LanguageTypes::ToWasmType(int8 binType)
     case LanguageTypes::i64: return WasmTypes::I64;
     case LanguageTypes::f32: return WasmTypes::F32;
     case LanguageTypes::f64: return WasmTypes::F64;
-    case LanguageTypes::m128: return WasmTypes::M128;
+#ifdef ENABLE_WASM_SIMD
+    case LanguageTypes::m128:
+        Simd::EnsureSimdIsEnabled();
+        return WasmTypes::M128;
+#endif
     default:
         throw WasmCompilationException(_u("Invalid binary type %d"), binType);
     }
@@ -403,9 +431,10 @@ WasmOp WasmBinaryReader::ReadOpCode()
     WasmOp op = m_currentNode.op = (WasmOp)*m_pc++;
     ++m_funcState.count;
 
+#ifdef ENABLE_WASM_SIMD
     if (op == wbSimdStart)
     {
-        if (!CONFIG_FLAG(WasmSimd))
+        if (!Wasm::Simd::IsEnabled())
         {
             ThrowDecodingError(_u("WebAssembly SIMD support is not enabled"));
         }
@@ -416,6 +445,7 @@ WasmOp WasmBinaryReader::ReadOpCode()
         op = (WasmOp)extOpCode;
         m_funcState.count += len;
     }
+#endif
     return op;
 }
 
@@ -470,9 +500,11 @@ WasmOp WasmBinaryReader::ReadExpr()
     case wbF64Const:
         ConstNode<WasmTypes::F64>();
         break;
+#ifdef ENABLE_WASM_SIMD
     case wbM128Const:
         ConstNode<WasmTypes::M128>();
         break;
+#endif
     case wbSetLocal:
     case wbGetLocal:
     case wbTeeLocal:
@@ -500,6 +532,7 @@ WasmOp WasmBinaryReader::ReadExpr()
         }
         break;
     }
+#ifdef ENABLE_WASM_SIMD
     case wbV8X16Shuffle:
         ShuffleNode();
         break;
@@ -507,6 +540,7 @@ WasmOp WasmBinaryReader::ReadExpr()
     case wb##opname: \
         LaneNode(); \
         break;
+#endif
 #define WASM_MEM_OPCODE(opname, opcode, sig, nyi) \
     case wb##opname: \
         MemNode(); \
@@ -686,13 +720,16 @@ void WasmBinaryReader::ConstNode()
         CompileAssert(sizeof(int64) == sizeof(double));
         m_funcState.count += sizeof(double);
         break;
+#ifdef ENABLE_WASM_SIMD
     case WasmTypes::M128:
+        Simd::EnsureSimdIsEnabled();
         for (uint i = 0; i < Simd::VEC_WIDTH; i++) 
         {
             m_currentNode.cnst.v128[i] = ReadConst<uint>();
         }
         m_funcState.count += Simd::VEC_WIDTH;
         break;
+#endif
     }
 }
 

+ 42 - 5
lib/WasmReader/WasmByteCodeGenerator.cpp

@@ -193,7 +193,11 @@ Js::AsmJsRetType WasmToAsmJs::GetAsmJsReturnType(WasmTypes::WasmType wasmType)
     case WasmTypes::F32: return Js::AsmJsRetType::Float;
     case WasmTypes::F64: return Js::AsmJsRetType::Double;
     case WasmTypes::Void: return Js::AsmJsRetType::Void;
-    case WasmTypes::M128: return Js::AsmJsRetType::Float32x4;
+#ifdef ENABLE_WASM_SIMD
+    case WasmTypes::M128:
+        Simd::EnsureSimdIsEnabled();
+        return Js::AsmJsRetType::Float32x4;
+#endif
     default:
         throw WasmCompilationException(_u("Unknown return type %u"), wasmType);
     }
@@ -209,7 +213,11 @@ Js::AsmJsVarType WasmToAsmJs::GetAsmJsVarType(WasmTypes::WasmType wasmType)
     case WasmTypes::I64: return Js::AsmJsVarType::Int64;
     case WasmTypes::F32: return Js::AsmJsVarType::Float;
     case WasmTypes::F64: return Js::AsmJsVarType::Double;
-    case WasmTypes::M128:  return Js::AsmJsVarType::Float32x4;
+#ifdef ENABLE_WASM_SIMD
+    case WasmTypes::M128:
+        Simd::EnsureSimdIsEnabled();
+        return Js::AsmJsVarType::Float32x4;
+#endif
     default:
         throw WasmCompilationException(_u("Unknown var type %u"), wasmType);
     }
@@ -469,7 +477,7 @@ WasmBytecodeGenerator::WasmBytecodeGenerator(Js::ScriptContext* scriptContext, W
     m_scriptContext(scriptContext),
     m_alloc(_u("WasmBytecodeGen"), scriptContext->GetThreadContext()->GetPageAllocator(), Js::Throw::OutOfMemory),
     m_evalStack(&m_alloc),
-    mTypedRegisterAllocator(&m_alloc, AllocateRegisterSpace, CONFIG_FLAG(WasmSimd) ? 0 : 1 << WAsmJs::SIMD),
+    mTypedRegisterAllocator(&m_alloc, AllocateRegisterSpace, Simd::IsEnabled() ? 0 : 1 << WAsmJs::SIMD),
     m_blockInfos(&m_alloc),
     currentProfileId(0),
     isUnreachable(false)
@@ -600,11 +608,14 @@ void WasmBytecodeGenerator::EnregisterLocals()
             case WasmTypes::I64:
                 m_writer->AsmLong1Const1(Js::OpCodeAsmJs::Ld_LongConst, m_locals[i].location, 0);
                 break;
+#ifdef ENABLE_WASM_SIMD
             case WasmTypes::M128:
             {
+                Simd::EnsureSimdIsEnabled();
                 m_writer->WasmSimdConst(Js::OpCodeAsmJs::Simd128_LdC, m_locals[i].location, 0, 0, 0, 0);
                 break;
             }
+#endif
             default:
                 Assume(UNREACHED);
             }
@@ -696,9 +707,12 @@ void WasmBytecodeGenerator::EmitExpr(WasmOp op)
     case wbI64Const:
         info = EmitConst(WasmTypes::I64, GetReader()->m_currentNode.cnst);
         break;
+#ifdef ENABLE_WASM_SIMD
     case wbM128Const:
+        Simd::EnsureSimdIsEnabled();
         info = EmitConst(WasmTypes::M128, GetReader()->m_currentNode.cnst);
         break;
+#endif
     case wbBlock:
         info = EmitBlock();
         break;
@@ -755,20 +769,26 @@ void WasmBytecodeGenerator::EmitExpr(WasmOp op)
         SetUnreachableState(true);
         info.type = WasmTypes::Any;
         break;
+#ifdef ENABLE_WASM_SIMD
     case wbM128Bitselect:
+        Simd::EnsureSimdIsEnabled();
         info = EmitM128BitSelect();
         break;
     case wbV8X16Shuffle:
+        Simd::EnsureSimdIsEnabled();
         info = EmitV8X16Shuffle();
         break;
 #define WASM_EXTRACTLANE_OPCODE(opname, opcode, sig, asmjsop, nyi) \
     case wb##opname: \
+        Simd::EnsureSimdIsEnabled();\
         info = EmitExtractLaneExpr(Js::OpCodeAsmJs::##asmjsop, WasmOpCodeSignatures::sig); \
         break;
 #define WASM_REPLACELANE_OPCODE(opname, opcode, sig, asmjsop, nyi) \
     case wb##opname: \
+        Simd::EnsureSimdIsEnabled();\
         info = EmitReplaceLaneExpr(Js::OpCodeAsmJs::##asmjsop, WasmOpCodeSignatures::sig); \
         break;
+#endif
 #define WASM_MEMREAD_OPCODE(opname, opcode, sig, nyi, viewtype) \
     case wb##opname: \
         Assert(WasmOpCodeSignatures::n##sig > 0);\
@@ -949,11 +969,14 @@ void WasmBytecodeGenerator::EmitLoadConst(EmitInfo dst, WasmConstLitNode cnst)
     case WasmTypes::I64:
         m_writer->AsmLong1Const1(Js::OpCodeAsmJs::Ld_LongConst, dst.location, cnst.i64);
         break;
+#ifdef ENABLE_WASM_SIMD
     case WasmTypes::M128:
     {
+        Simd::EnsureSimdIsEnabled();
         m_writer->WasmSimdConst(Js::OpCodeAsmJs::Simd128_LdC, dst.location, cnst.v128[0], cnst.v128[1], cnst.v128[2], cnst.v128[3]);
         break;
     }
+#endif
     default:
         throw WasmCompilationException(_u("Unknown type %u"), dst.type);
     }
@@ -1161,9 +1184,12 @@ EmitInfo WasmBytecodeGenerator::EmitCall()
         case WasmTypes::I64:
             argOp = isImportCall ? Js::OpCodeAsmJs::ArgOut_Long : Js::OpCodeAsmJs::I_ArgOut_Long;
             break;
+#ifdef ENABLE_WASM_SIMD
         case WasmTypes::M128:
+            Simd::EnsureSimdIsEnabled();
             argOp = isImportCall ? Js::OpCodeAsmJs::Simd128_ArgOut_F4 : Js::OpCodeAsmJs::Simd128_I_ArgOut_F4;
             break;
+#endif
         case WasmTypes::Any:
             // In unreachable mode allow any type as argument since we won't actually emit the call
             Assert(IsUnreachable());
@@ -1438,6 +1464,7 @@ EmitInfo WasmBytecodeGenerator::EmitUnaryExpr(Js::OpCodeAsmJs op, const WasmType
     return EmitInfo(resultReg, resultType);
 }
 
+#ifdef ENABLE_WASM_SIMD
 void WasmBytecodeGenerator::CheckLaneIndex(Js::OpCodeAsmJs op, const uint index)
 {
     uint numLanes;
@@ -1596,6 +1623,7 @@ EmitInfo WasmBytecodeGenerator::EmitSimdMemAccess(Js::OpCodeAsmJs op, const Wasm
 
     return yieldInfo;
 }
+#endif
 
 EmitInfo WasmBytecodeGenerator::EmitMemAccess(WasmOp wasmOp, const WasmTypes::WasmType* signature, Js::ArrayBufferView::ViewType viewType, bool isStore)
 {
@@ -1755,8 +1783,11 @@ Js::OpCodeAsmJs WasmBytecodeGenerator::GetLoadOp(WasmTypes::WasmType wasmType)
         return Js::OpCodeAsmJs::Ld_Int;
     case WasmTypes::I64:
         return Js::OpCodeAsmJs::Ld_Long;
+#ifdef ENABLE_WASM_SIMD
     case WasmTypes::M128:
+        Simd::EnsureSimdIsEnabled();
         return Js::OpCodeAsmJs::Simd128_Ld_F4;
+#endif
     case WasmTypes::Any:
         // In unreachable mode load the any type like an int since we won't actually emit the load
         Assert(IsUnreachable());
@@ -1786,9 +1817,12 @@ Js::OpCodeAsmJs WasmBytecodeGenerator::GetReturnOp(WasmTypes::WasmType type)
     case WasmTypes::I64:
         retOp = Js::OpCodeAsmJs::Return_Long;
         break;
+#ifdef ENABLE_WASM_SIMD
     case WasmTypes::M128:
+        Simd::EnsureSimdIsEnabled();
         retOp = Js::OpCodeAsmJs::Simd128_Return_F4;
         break;
+#endif
     case WasmTypes::Any:
         // In unreachable mode load the any type like an int since we won't actually emit the load
         Assert(IsUnreachable());
@@ -1904,8 +1938,11 @@ WasmRegisterSpace* WasmBytecodeGenerator::GetRegisterSpace(WasmTypes::WasmType t
     case WasmTypes::I64:    return mTypedRegisterAllocator.GetRegisterSpace(WAsmJs::INT64);
     case WasmTypes::F32:    return mTypedRegisterAllocator.GetRegisterSpace(WAsmJs::FLOAT32);
     case WasmTypes::F64:    return mTypedRegisterAllocator.GetRegisterSpace(WAsmJs::FLOAT64);
-    case WasmTypes::M128:   return mTypedRegisterAllocator.GetRegisterSpace(WAsmJs::SIMD);
-
+#ifdef ENABLE_WASM_SIMD
+    case WasmTypes::M128:
+        Simd::EnsureSimdIsEnabled();
+        return mTypedRegisterAllocator.GetRegisterSpace(WAsmJs::SIMD);
+#endif
     default:
         return nullptr;
     }

+ 10 - 0
lib/WasmReader/WasmParseTree.h

@@ -12,6 +12,14 @@ namespace Wasm
         const size_t VEC_WIDTH = 4;
         typedef uint32 simdvec [VEC_WIDTH]; //TODO: maybe we should pull in SIMDValue?
         const size_t MAX_LANES = 16;
+        void EnsureSimdIsEnabled();
+        constexpr bool IsEnabled() {
+#ifdef ENABLE_WASM_SIMD
+            return CONFIG_FLAG(WasmSimd);
+#else
+            return false;
+#endif
+        }
     }
 
     namespace WasmTypes
@@ -24,7 +32,9 @@ namespace Wasm
             I64 = 2,
             F32 = 3,
             F64 = 4,
+#ifdef ENABLE_WASM_SIMD
             M128 = 5,
+#endif
             Limit,
             Ptr,
             Any

+ 3 - 0
lib/WasmReader/WasmSignature.cpp

@@ -115,10 +115,13 @@ Js::ArgSlot WasmSignature::GetParamSize(Js::ArgSlot index) const
         CompileAssert(sizeof(double) == sizeof(int64));
         return sizeof(int64);
         break;
+#ifdef ENABLE_WASM_SIMD
     case WasmTypes::M128:
+        Wasm::Simd::EnsureSimdIsEnabled();
         CompileAssert(sizeof(Simd::simdvec) == 16);
         return sizeof(Simd::simdvec);
         break;
+#endif
     default:
         throw WasmCompilationException(_u("Invalid param type"));
     }