swr: Fix crashes on non-AVX hardware
[mesa.git] / src / gallium / drivers / swr / rasterizer / jitter / functionpasses / lower_x86.cpp
index 7605823c04d1c1addec0031042ff4044b1a8b326..216121bc51f454f53b314761ae6c94bcdb03e662 100644 (file)
 #include "passes.h"
 #include "JitManager.h"
 
+#include "common/simdlib.hpp"
+
 #include <unordered_map>
 
+extern "C" void ScatterPS_256(uint8_t*, SIMD256::Integer, SIMD256::Float, uint8_t, uint32_t);
+
 namespace llvm
 {
     // foward declare the initializer
@@ -64,23 +68,26 @@ namespace SwrJit
 
     struct X86Intrinsic
     {
-        Intrinsic::ID intrin[NUM_WIDTHS];
+        IntrinsicID intrin[NUM_WIDTHS];
         EmuFunc       emuFunc;
     };
 
     // Map of intrinsics that haven't been moved to the new mechanism yet. If used, these get the
     // previous behavior of mapping directly to avx/avx2 intrinsics.
-    static std::map<std::string, Intrinsic::ID> intrinsicMap = {
-        {"meta.intrinsic.BEXTR_32", Intrinsic::x86_bmi_bextr_32},
-        {"meta.intrinsic.VPSHUFB", Intrinsic::x86_avx2_pshuf_b},
-        {"meta.intrinsic.VCVTPS2PH", Intrinsic::x86_vcvtps2ph_256},
-        {"meta.intrinsic.VPTESTC", Intrinsic::x86_avx_ptestc_256},
-        {"meta.intrinsic.VPTESTZ", Intrinsic::x86_avx_ptestz_256},
-        {"meta.intrinsic.VFMADDPS", Intrinsic::x86_fma_vfmadd_ps_256},
-        {"meta.intrinsic.VPHADDD", Intrinsic::x86_avx2_phadd_d},
-        {"meta.intrinsic.PDEP32", Intrinsic::x86_bmi_pdep_32},
-        {"meta.intrinsic.RDTSC", Intrinsic::x86_rdtsc},
-    };
+    using intrinsicMap_t = std::map<std::string, IntrinsicID>;
+    static intrinsicMap_t& getIntrinsicMap() {
+        static std::map<std::string, IntrinsicID> intrinsicMap = {
+            {"meta.intrinsic.BEXTR_32", Intrinsic::x86_bmi_bextr_32},
+            {"meta.intrinsic.VPSHUFB", Intrinsic::x86_avx2_pshuf_b},
+            {"meta.intrinsic.VCVTPS2PH", Intrinsic::x86_vcvtps2ph_256},
+            {"meta.intrinsic.VPTESTC", Intrinsic::x86_avx_ptestc_256},
+            {"meta.intrinsic.VPTESTZ", Intrinsic::x86_avx_ptestz_256},
+            {"meta.intrinsic.VPHADDD", Intrinsic::x86_avx2_phadd_d},
+            {"meta.intrinsic.PDEP32", Intrinsic::x86_bmi_pdep_32},
+            {"meta.intrinsic.RDTSC", Intrinsic::x86_rdtsc}
+        };
+        return intrinsicMap;
+    }
 
     // Forward decls
     Instruction* NO_EMU(LowerX86* pThis, TargetArch arch, TargetWidth width, CallInst* pCallInst);
@@ -89,6 +96,8 @@ namespace SwrJit
     Instruction*
     VGATHER_EMU(LowerX86* pThis, TargetArch arch, TargetWidth width, CallInst* pCallInst);
     Instruction*
+    VSCATTER_EMU(LowerX86* pThis, TargetArch arch, TargetWidth width, CallInst* pCallInst);
+    Instruction*
     VROUND_EMU(LowerX86* pThis, TargetArch arch, TargetWidth width, CallInst* pCallInst);
     Instruction*
     VHSUB_EMU(LowerX86* pThis, TargetArch arch, TargetWidth width, CallInst* pCallInst);
@@ -103,88 +112,73 @@ namespace SwrJit
 
     static Intrinsic::ID DOUBLE = (Intrinsic::ID)-1;
 
-    static std::map<std::string, X86Intrinsic> intrinsicMap2[] = {
-        //                              256 wide                                    512 wide
-        {
-            // AVX
-            {"meta.intrinsic.VRCPPS", {{Intrinsic::x86_avx_rcp_ps_256, DOUBLE}, NO_EMU}},
-            {"meta.intrinsic.VPERMPS",
-             {{Intrinsic::not_intrinsic, Intrinsic::not_intrinsic}, VPERM_EMU}},
-            {"meta.intrinsic.VPERMD",
-             {{Intrinsic::not_intrinsic, Intrinsic::not_intrinsic}, VPERM_EMU}},
-            {"meta.intrinsic.VGATHERPD",
-             {{Intrinsic::not_intrinsic, Intrinsic::not_intrinsic}, VGATHER_EMU}},
-            {"meta.intrinsic.VGATHERPS",
-             {{Intrinsic::not_intrinsic, Intrinsic::not_intrinsic}, VGATHER_EMU}},
-            {"meta.intrinsic.VGATHERDD",
-             {{Intrinsic::not_intrinsic, Intrinsic::not_intrinsic}, VGATHER_EMU}},
-            {"meta.intrinsic.VCVTPD2PS",
-             {{Intrinsic::x86_avx_cvt_pd2_ps_256, Intrinsic::not_intrinsic}, NO_EMU}},
-            {"meta.intrinsic.VCVTPH2PS",
-             {{Intrinsic::x86_vcvtph2ps_256, Intrinsic::not_intrinsic}, NO_EMU}},
-            {"meta.intrinsic.VROUND", {{Intrinsic::x86_avx_round_ps_256, DOUBLE}, NO_EMU}},
-            {"meta.intrinsic.VHSUBPS", {{Intrinsic::x86_avx_hsub_ps_256, DOUBLE}, NO_EMU}},
-        },
-        {
-            // AVX2
-            {"meta.intrinsic.VRCPPS", {{Intrinsic::x86_avx_rcp_ps_256, DOUBLE}, NO_EMU}},
-            {"meta.intrinsic.VPERMPS",
-             {{Intrinsic::x86_avx2_permps, Intrinsic::not_intrinsic}, VPERM_EMU}},
-            {"meta.intrinsic.VPERMD",
-             {{Intrinsic::x86_avx2_permd, Intrinsic::not_intrinsic}, VPERM_EMU}},
-            {"meta.intrinsic.VGATHERPD",
-             {{Intrinsic::not_intrinsic, Intrinsic::not_intrinsic}, VGATHER_EMU}},
-            {"meta.intrinsic.VGATHERPS",
-             {{Intrinsic::not_intrinsic, Intrinsic::not_intrinsic}, VGATHER_EMU}},
-            {"meta.intrinsic.VGATHERDD",
-             {{Intrinsic::not_intrinsic, Intrinsic::not_intrinsic}, VGATHER_EMU}},
-            {"meta.intrinsic.VCVTPD2PS", {{Intrinsic::x86_avx_cvt_pd2_ps_256, DOUBLE}, NO_EMU}},
-            {"meta.intrinsic.VCVTPH2PS",
-             {{Intrinsic::x86_vcvtph2ps_256, Intrinsic::not_intrinsic}, NO_EMU}},
-            {"meta.intrinsic.VROUND", {{Intrinsic::x86_avx_round_ps_256, DOUBLE}, NO_EMU}},
-            {"meta.intrinsic.VHSUBPS", {{Intrinsic::x86_avx_hsub_ps_256, DOUBLE}, NO_EMU}},
-        },
-        {
-            // AVX512
-            {"meta.intrinsic.VRCPPS",
-             {{Intrinsic::x86_avx512_rcp14_ps_256, Intrinsic::x86_avx512_rcp14_ps_512}, NO_EMU}},
-#if LLVM_VERSION_MAJOR < 7
-            {"meta.intrinsic.VPERMPS",
-             {{Intrinsic::x86_avx512_mask_permvar_sf_256,
-               Intrinsic::x86_avx512_mask_permvar_sf_512},
-              NO_EMU}},
-            {"meta.intrinsic.VPERMD",
-             {{Intrinsic::x86_avx512_mask_permvar_si_256,
-               Intrinsic::x86_avx512_mask_permvar_si_512},
-              NO_EMU}},
-#else
-            {"meta.intrinsic.VPERMPS",
-             {{Intrinsic::not_intrinsic, Intrinsic::not_intrinsic}, VPERM_EMU}},
-            {"meta.intrinsic.VPERMD",
-             {{Intrinsic::not_intrinsic, Intrinsic::not_intrinsic}, VPERM_EMU}},
-#endif
-            {"meta.intrinsic.VGATHERPD",
-             {{Intrinsic::not_intrinsic, Intrinsic::not_intrinsic}, VGATHER_EMU}},
-            {"meta.intrinsic.VGATHERPS",
-             {{Intrinsic::not_intrinsic, Intrinsic::not_intrinsic}, VGATHER_EMU}},
-            {"meta.intrinsic.VGATHERDD",
-             {{Intrinsic::not_intrinsic, Intrinsic::not_intrinsic}, VGATHER_EMU}},
-#if LLVM_VERSION_MAJOR < 7
-            {"meta.intrinsic.VCVTPD2PS",
-             {{Intrinsic::x86_avx512_mask_cvtpd2ps_256, Intrinsic::x86_avx512_mask_cvtpd2ps_512},
-              NO_EMU}},
+    using intrinsicMapAdvanced_t = std::vector<std::map<std::string, X86Intrinsic>>;
+
+    static intrinsicMapAdvanced_t&  getIntrinsicMapAdvanced()
+    {
+        // clang-format off
+        static intrinsicMapAdvanced_t intrinsicMapAdvanced = {
+            //                               256 wide                               512 wide
+            {
+                // AVX
+                {"meta.intrinsic.VRCPPS",    {{Intrinsic::x86_avx_rcp_ps_256,       DOUBLE},                    NO_EMU}},
+                {"meta.intrinsic.VPERMPS",   {{Intrinsic::not_intrinsic,            Intrinsic::not_intrinsic},  VPERM_EMU}},
+                {"meta.intrinsic.VPERMD",    {{Intrinsic::not_intrinsic,            Intrinsic::not_intrinsic},  VPERM_EMU}},
+                {"meta.intrinsic.VGATHERPD", {{Intrinsic::not_intrinsic,            Intrinsic::not_intrinsic},  VGATHER_EMU}},
+                {"meta.intrinsic.VGATHERPS", {{Intrinsic::not_intrinsic,            Intrinsic::not_intrinsic},  VGATHER_EMU}},
+                {"meta.intrinsic.VGATHERDD", {{Intrinsic::not_intrinsic,            Intrinsic::not_intrinsic},  VGATHER_EMU}},
+                {"meta.intrinsic.VSCATTERPS", {{Intrinsic::not_intrinsic,           Intrinsic::not_intrinsic}, VSCATTER_EMU}},
+                {"meta.intrinsic.VCVTPD2PS", {{Intrinsic::x86_avx_cvt_pd2_ps_256,   Intrinsic::not_intrinsic},  NO_EMU}},
+                {"meta.intrinsic.VROUND",    {{Intrinsic::x86_avx_round_ps_256,     DOUBLE},                    NO_EMU}},
+                {"meta.intrinsic.VHSUBPS",   {{Intrinsic::x86_avx_hsub_ps_256,      DOUBLE},                    NO_EMU}},
+            },
+            {
+                // AVX2
+                {"meta.intrinsic.VRCPPS",       {{Intrinsic::x86_avx_rcp_ps_256,    DOUBLE},                    NO_EMU}},
+                {"meta.intrinsic.VPERMPS",      {{Intrinsic::x86_avx2_permps,       Intrinsic::not_intrinsic},  VPERM_EMU}},
+                {"meta.intrinsic.VPERMD",       {{Intrinsic::x86_avx2_permd,        Intrinsic::not_intrinsic},  VPERM_EMU}},
+                {"meta.intrinsic.VGATHERPD",    {{Intrinsic::not_intrinsic,         Intrinsic::not_intrinsic},  VGATHER_EMU}},
+                {"meta.intrinsic.VGATHERPS",    {{Intrinsic::not_intrinsic,         Intrinsic::not_intrinsic},  VGATHER_EMU}},
+                {"meta.intrinsic.VGATHERDD",    {{Intrinsic::not_intrinsic,         Intrinsic::not_intrinsic},  VGATHER_EMU}},
+                {"meta.intrinsic.VSCATTERPS", {{Intrinsic::not_intrinsic,           Intrinsic::not_intrinsic}, VSCATTER_EMU}},
+                {"meta.intrinsic.VCVTPD2PS",    {{Intrinsic::x86_avx_cvt_pd2_ps_256, DOUBLE},                   NO_EMU}},
+                {"meta.intrinsic.VROUND",       {{Intrinsic::x86_avx_round_ps_256,  DOUBLE},                    NO_EMU}},
+                {"meta.intrinsic.VHSUBPS",      {{Intrinsic::x86_avx_hsub_ps_256,   DOUBLE},                    NO_EMU}},
+            },
+            {
+                // AVX512
+                {"meta.intrinsic.VRCPPS", {{Intrinsic::x86_avx512_rcp14_ps_256,     Intrinsic::x86_avx512_rcp14_ps_512}, NO_EMU}},
+    #if LLVM_VERSION_MAJOR < 7
+                {"meta.intrinsic.VPERMPS", {{Intrinsic::x86_avx512_mask_permvar_sf_256, Intrinsic::x86_avx512_mask_permvar_sf_512}, NO_EMU}},
+                {"meta.intrinsic.VPERMD", {{Intrinsic::x86_avx512_mask_permvar_si_256, Intrinsic::x86_avx512_mask_permvar_si_512}, NO_EMU}},
+    #else
+                {"meta.intrinsic.VPERMPS", {{Intrinsic::not_intrinsic,              Intrinsic::not_intrinsic}, VPERM_EMU}},
+                {"meta.intrinsic.VPERMD", {{Intrinsic::not_intrinsic,               Intrinsic::not_intrinsic}, VPERM_EMU}},
+    #endif
+                {"meta.intrinsic.VGATHERPD", {{Intrinsic::not_intrinsic,            Intrinsic::not_intrinsic}, VGATHER_EMU}},
+                {"meta.intrinsic.VGATHERPS", {{Intrinsic::not_intrinsic,            Intrinsic::not_intrinsic}, VGATHER_EMU}},
+                {"meta.intrinsic.VGATHERDD", {{Intrinsic::not_intrinsic,            Intrinsic::not_intrinsic}, VGATHER_EMU}},
+                {"meta.intrinsic.VSCATTERPS", {{Intrinsic::not_intrinsic,           Intrinsic::not_intrinsic}, VSCATTER_EMU}},
+    #if LLVM_VERSION_MAJOR < 7
+                {"meta.intrinsic.VCVTPD2PS", {{Intrinsic::x86_avx512_mask_cvtpd2ps_256, Intrinsic::x86_avx512_mask_cvtpd2ps_512}, NO_EMU}},
+    #else
+                {"meta.intrinsic.VCVTPD2PS", {{Intrinsic::not_intrinsic,            Intrinsic::not_intrinsic}, VCONVERT_EMU}},
+    #endif
+                {"meta.intrinsic.VROUND", {{Intrinsic::not_intrinsic,               Intrinsic::not_intrinsic}, VROUND_EMU}},
+                {"meta.intrinsic.VHSUBPS", {{Intrinsic::not_intrinsic,              Intrinsic::not_intrinsic}, VHSUB_EMU}}
+            }};
+        // clang-format on
+        return intrinsicMapAdvanced;
+    }
+
+    static uint32_t getBitWidth(VectorType *pVTy)
+    {
+#if LLVM_VERSION_MAJOR >= 11
+        return pVTy->getNumElements() * pVTy->getElementType()->getPrimitiveSizeInBits();
 #else
-            {"meta.intrinsic.VCVTPD2PS",
-             {{Intrinsic::not_intrinsic, Intrinsic::not_intrinsic}, VCONVERT_EMU}},
+        return pVTy->getBitWidth();
 #endif
-            {"meta.intrinsic.VCVTPH2PS",
-             {{Intrinsic::x86_avx512_mask_vcvtph2ps_256, Intrinsic::x86_avx512_mask_vcvtph2ps_512},
-              NO_EMU}},
-            {"meta.intrinsic.VROUND",
-             {{Intrinsic::not_intrinsic, Intrinsic::not_intrinsic}, VROUND_EMU}},
-            {"meta.intrinsic.VHSUBPS",
-             {{Intrinsic::not_intrinsic, Intrinsic::not_intrinsic}, VHSUB_EMU}},
-        }};
+    }
 
     struct LowerX86 : public FunctionPass
     {
@@ -210,6 +204,31 @@ namespace SwrJit
                 SWR_ASSERT(false, "Unsupported AVX architecture.");
                 mTarget = AVX;
             }
+
+            // Setup scatter function for 256 wide
+            uint32_t curWidth = B->mVWidth;
+            B->SetTargetWidth(8);
+            std::vector<Type*> args = {
+                B->mInt8PtrTy,   // pBase
+                B->mSimdInt32Ty, // vIndices
+                B->mSimdFP32Ty,  // vSrc
+                B->mInt8Ty,      // mask
+                B->mInt32Ty      // scale
+            };
+
+            FunctionType* pfnScatterTy = FunctionType::get(B->mVoidTy, args, false);
+            mPfnScatter256             = cast<Function>(
+#if LLVM_VERSION_MAJOR >= 9
+                B->JM()->mpCurrentModule->getOrInsertFunction("ScatterPS_256", pfnScatterTy).getCallee());
+#else
+                B->JM()->mpCurrentModule->getOrInsertFunction("ScatterPS_256", pfnScatterTy));
+#endif
+            if (sys::DynamicLibrary::SearchForAddressOfSymbol("ScatterPS_256") == nullptr)
+            {
+                sys::DynamicLibrary::AddSymbol("ScatterPS_256", (void*)&ScatterPS_256);
+            }
+
+            B->SetTargetWidth(curWidth);
         }
 
         // Try to decipher the vector type of the instruction. This does not work properly
@@ -221,13 +240,16 @@ namespace SwrJit
                                       TargetWidth*    pWidth,
                                       Type**          pTy)
         {
+            assert(pCallInst);
             Type* pVecTy = pCallInst->getType();
 
             // Check for intrinsic specific types
             // VCVTPD2PS type comes from src, not dst
             if (intrinName.equals("meta.intrinsic.VCVTPD2PS"))
             {
-                pVecTy = pCallInst->getOperand(0)->getType();
+                Value* pOp = pCallInst->getOperand(0);
+                assert(pOp);
+                pVecTy = pOp->getType();
             }
 
             if (!pVecTy->isVectorTy())
@@ -243,7 +265,7 @@ namespace SwrJit
             }
             SWR_ASSERT(pVecTy->isVectorTy(), "Couldn't determine vector size");
 
-            uint32_t width = cast<VectorType>(pVecTy)->getBitWidth();
+            uint32_t width = getBitWidth(cast<VectorType>(pVecTy));
             switch (width)
             {
             case 256:
@@ -275,7 +297,7 @@ namespace SwrJit
                 SWR_ASSERT(false, "Unhandled vector width type %d\n", width);
             }
 
-            return ConstantVector::getNullValue(VectorType::get(pTy, numElem));
+            return ConstantVector::getNullValue(getVectorType(pTy, numElem));
         }
 
         Value* GetMask(TargetWidth width)
@@ -298,20 +320,26 @@ namespace SwrJit
         // Convert <N x i1> mask to <N x i32> x86 mask
         Value* VectorMask(Value* vi1Mask)
         {
+#if LLVM_VERSION_MAJOR >= 11
+            uint32_t numElem = cast<VectorType>(vi1Mask->getType())->getNumElements();
+#else
             uint32_t numElem = vi1Mask->getType()->getVectorNumElements();
-            return B->S_EXT(vi1Mask, VectorType::get(B->mInt32Ty, numElem));
+#endif
+            return B->S_EXT(vi1Mask, getVectorType(B->mInt32Ty, numElem));
         }
 
         Instruction* ProcessIntrinsicAdvanced(CallInst* pCallInst)
         {
-            Function*   pFunc     = pCallInst->getCalledFunction();
-            auto&       intrinsic = intrinsicMap2[mTarget][pFunc->getName()];
+            Function*   pFunc = pCallInst->getCalledFunction();
+            assert(pFunc);
+
+            auto&       intrinsic = getIntrinsicMapAdvanced()[mTarget][pFunc->getName().str()];
             TargetWidth vecWidth;
             Type*       pElemTy;
             GetRequestedWidthAndType(pCallInst, pFunc->getName(), &vecWidth, &pElemTy);
 
             // Check if there is a native intrinsic for this instruction
-            Intrinsic::ID id = intrinsic.intrin[vecWidth];
+            IntrinsicID id = intrinsic.intrin[vecWidth];
             if (id == DOUBLE)
             {
                 // Double pump the next smaller SIMD intrinsic
@@ -364,18 +392,19 @@ namespace SwrJit
         Instruction* ProcessIntrinsic(CallInst* pCallInst)
         {
             Function* pFunc = pCallInst->getCalledFunction();
+            assert(pFunc);
 
             // Forward to the advanced support if found
-            if (intrinsicMap2[mTarget].find(pFunc->getName()) != intrinsicMap2[mTarget].end())
+            if (getIntrinsicMapAdvanced()[mTarget].find(pFunc->getName().str()) != getIntrinsicMapAdvanced()[mTarget].end())
             {
                 return ProcessIntrinsicAdvanced(pCallInst);
             }
 
-            SWR_ASSERT(intrinsicMap.find(pFunc->getName()) != intrinsicMap.end(),
+            SWR_ASSERT(getIntrinsicMap().find(pFunc->getName().str()) != getIntrinsicMap().end(),
                        "Unimplemented intrinsic %s.",
-                       pFunc->getName());
+                       pFunc->getName().str().c_str());
 
-            Intrinsic::ID x86Intrinsic = intrinsicMap[pFunc->getName()];
+            Intrinsic::ID x86Intrinsic = getIntrinsicMap()[pFunc->getName().str()];
             Function*     pX86IntrinFunc =
                 Intrinsic::getDeclaration(B->JM()->mpCurrentModule, x86Intrinsic);
 
@@ -393,23 +422,39 @@ namespace SwrJit
         virtual bool runOnFunction(Function& F)
         {
             std::vector<Instruction*> toRemove;
+            std::vector<BasicBlock*>  bbs;
 
-            for (auto& BB : F.getBasicBlockList())
+            // Make temp copy of the basic blocks and instructions, as the intrinsic
+            // replacement code might invalidate the iterators
+            for (auto& b : F.getBasicBlockList())
             {
-                for (auto& I : BB.getInstList())
+                bbs.push_back(&b);
+            }
+
+            for (auto* BB : bbs)
+            {
+                std::vector<Instruction*> insts;
+                for (auto& i : BB->getInstList())
                 {
-                    if (CallInst* pCallInst = dyn_cast<CallInst>(&I))
+                    insts.push_back(&i);
+                }
+
+                for (auto* I : insts)
+                {
+                    if (CallInst* pCallInst = dyn_cast<CallInst>(I))
                     {
                         Function* pFunc = pCallInst->getCalledFunction();
                         if (pFunc)
                         {
                             if (pFunc->getName().startswith("meta.intrinsic"))
                             {
-                                B->IRB()->SetInsertPoint(&I);
+                                B->IRB()->SetInsertPoint(I);
                                 Instruction* pReplace = ProcessIntrinsic(pCallInst);
-                                SWR_ASSERT(pReplace);
                                 toRemove.push_back(pCallInst);
-                                pCallInst->replaceAllUsesWith(pReplace);
+                                if (pReplace)
+                                {
+                                    pCallInst->replaceAllUsesWith(pReplace);
+                                }
                             }
                         }
                     }
@@ -429,10 +474,9 @@ namespace SwrJit
         virtual void getAnalysisUsage(AnalysisUsage& AU) const {}
 
         JitManager* JM() { return B->JM(); }
-
-        Builder* B;
-
-        TargetArch mTarget;
+        Builder*    B;
+        TargetArch  mTarget;
+        Function*   mPfnScatter256;
 
         static char ID; ///< Needed by LLVM to generate ID for FunctionPass.
     };
@@ -465,7 +509,12 @@ namespace SwrJit
         else
         {
             v32Result = UndefValue::get(v32A->getType());
-            for (uint32_t l = 0; l < v32A->getType()->getVectorNumElements(); ++l)
+#if LLVM_VERSION_MAJOR >= 11
+            uint32_t numElem = cast<VectorType>(v32A->getType())->getNumElements();
+#else
+            uint32_t numElem = v32A->getType()->getVectorNumElements();
+#endif
+            for (uint32_t l = 0; l < numElem; ++l)
             {
                 auto i32Index = B->VEXTRACT(vi32Index, B->C(l));
                 auto val      = B->VEXTRACT(v32A, i32Index);
@@ -485,11 +534,18 @@ namespace SwrJit
         auto     vi1Mask     = pCallInst->getArgOperand(3);
         auto     i8Scale     = pCallInst->getArgOperand(4);
 
-        pBase             = B->POINTER_CAST(pBase, PointerType::get(B->mInt8Ty, 0));
-        uint32_t numElem  = vSrc->getType()->getVectorNumElements();
-        auto     i32Scale = B->Z_EXT(i8Scale, B->mInt32Ty);
-        auto     srcTy    = vSrc->getType()->getVectorElementType();
-        Value*   v32Gather;
+        pBase              = B->POINTER_CAST(pBase, PointerType::get(B->mInt8Ty, 0));
+#if LLVM_VERSION_MAJOR >= 11
+        VectorType* pVectorType = cast<VectorType>(vSrc->getType());
+        uint32_t    numElem     = pVectorType->getNumElements();
+        auto        srcTy       = pVectorType->getElementType();
+#else
+        uint32_t numElem   = vSrc->getType()->getVectorNumElements();
+        auto     srcTy     = vSrc->getType()->getVectorElementType();
+#endif
+        auto     i32Scale  = B->Z_EXT(i8Scale, B->mInt32Ty);
+
+        Value*   v32Gather = nullptr;
         if (arch == AVX)
         {
             // Full emulation for AVX
@@ -499,7 +555,11 @@ namespace SwrJit
             B->STORE(vSrc, pTmp);
 
             v32Gather        = UndefValue::get(vSrc->getType());
+#if LLVM_VERSION_MAJOR > 10
+            auto vi32Scale   = ConstantVector::getSplat(ElementCount::get(numElem, false), cast<ConstantInt>(i32Scale));
+#else
             auto vi32Scale   = ConstantVector::getSplat(numElem, cast<ConstantInt>(i32Scale));
+#endif
             auto vi32Offsets = B->MUL(vi32Indices, vi32Scale);
 
             for (uint32_t i = 0; i < numElem; ++i)
@@ -518,7 +578,7 @@ namespace SwrJit
         }
         else if (arch == AVX2 || (arch == AVX512 && width == W256))
         {
-            Function* pX86IntrinFunc;
+            Function* pX86IntrinFunc = nullptr;
             if (srcTy == B->mFP32Ty)
             {
                 pX86IntrinFunc = Intrinsic::getDeclaration(B->JM()->mpCurrentModule,
@@ -547,12 +607,19 @@ namespace SwrJit
             else if (width == W512)
             {
                 // Double pump 4-wide for 64bit elements
+#if LLVM_VERSION_MAJOR >= 11
+                if (cast<VectorType>(vSrc->getType())->getElementType() == B->mDoubleTy)
+#else
                 if (vSrc->getType()->getVectorElementType() == B->mDoubleTy)
+#endif
                 {
                     auto v64Mask = pThis->VectorMask(vi1Mask);
-                    v64Mask      = B->S_EXT(
-                        v64Mask,
-                        VectorType::get(B->mInt64Ty, v64Mask->getType()->getVectorNumElements()));
+#if LLVM_VERSION_MAJOR >= 11
+                    uint32_t numElem = cast<VectorType>(v64Mask->getType())->getNumElements();
+#else
+                    uint32_t numElem = v64Mask->getType()->getVectorNumElements();
+#endif
+                    v64Mask = B->S_EXT(v64Mask, getVectorType(B->mInt64Ty, numElem));
                     v64Mask = B->BITCAST(v64Mask, vSrc->getType());
 
                     Value* src0 = B->VSHUFFLE(vSrc, vSrc, B->C({0, 1, 2, 3}));
@@ -564,23 +631,25 @@ namespace SwrJit
                     Value* mask0 = B->VSHUFFLE(v64Mask, v64Mask, B->C({0, 1, 2, 3}));
                     Value* mask1 = B->VSHUFFLE(v64Mask, v64Mask, B->C({4, 5, 6, 7}));
 
-                    src0 = B->BITCAST(
-                        src0,
-                        VectorType::get(B->mInt64Ty, src0->getType()->getVectorNumElements()));
-                    mask0 = B->BITCAST(
-                        mask0,
-                        VectorType::get(B->mInt64Ty, mask0->getType()->getVectorNumElements()));
+#if LLVM_VERSION_MAJOR >= 11
+                    uint32_t numElemSrc0  = cast<VectorType>(src0->getType())->getNumElements();
+                    uint32_t numElemMask0 = cast<VectorType>(mask0->getType())->getNumElements();
+                    uint32_t numElemSrc1  = cast<VectorType>(src1->getType())->getNumElements();
+                    uint32_t numElemMask1 = cast<VectorType>(mask1->getType())->getNumElements();
+#else
+                    uint32_t numElemSrc0  = src0->getType()->getVectorNumElements();
+                    uint32_t numElemMask0 = mask0->getType()->getVectorNumElements();
+                    uint32_t numElemSrc1  = src1->getType()->getVectorNumElements();
+                    uint32_t numElemMask1 = mask1->getType()->getVectorNumElements();
+#endif
+                    src0 = B->BITCAST(src0, getVectorType(B->mInt64Ty, numElemSrc0));
+                    mask0 = B->BITCAST(mask0, getVectorType(B->mInt64Ty, numElemMask0));
                     Value* gather0 =
                         B->CALL(pX86IntrinFunc, {src0, pBase, indices0, mask0, i8Scale});
-                    src1 = B->BITCAST(
-                        src1,
-                        VectorType::get(B->mInt64Ty, src1->getType()->getVectorNumElements()));
-                    mask1 = B->BITCAST(
-                        mask1,
-                        VectorType::get(B->mInt64Ty, mask1->getType()->getVectorNumElements()));
+                    src1 = B->BITCAST(src1, getVectorType(B->mInt64Ty, numElemSrc1));
+                    mask1 = B->BITCAST(mask1, getVectorType(B->mInt64Ty, numElemMask1));
                     Value* gather1 =
                         B->CALL(pX86IntrinFunc, {src1, pBase, indices1, mask1, i8Scale});
-
                     v32Gather = B->VSHUFFLE(gather0, gather1, B->C({0, 1, 2, 3, 4, 5, 6, 7}));
                     v32Gather = B->BITCAST(v32Gather, vSrc->getType());
                 }
@@ -609,8 +678,8 @@ namespace SwrJit
         }
         else if (arch == AVX512)
         {
-            Value*    iMask;
-            Function* pX86IntrinFunc;
+            Value*    iMask = nullptr;
+            Function* pX86IntrinFunc = nullptr;
             if (srcTy == B->mFP32Ty)
             {
                 pX86IntrinFunc = Intrinsic::getDeclaration(B->JM()->mpCurrentModule,
@@ -640,6 +709,69 @@ namespace SwrJit
 
         return cast<Instruction>(v32Gather);
     }
+    Instruction*
+    VSCATTER_EMU(LowerX86* pThis, TargetArch arch, TargetWidth width, CallInst* pCallInst)
+    {
+        Builder* B           = pThis->B;
+        auto     pBase       = pCallInst->getArgOperand(0);
+        auto     vi1Mask     = pCallInst->getArgOperand(1);
+        auto     vi32Indices = pCallInst->getArgOperand(2);
+        auto     v32Src      = pCallInst->getArgOperand(3);
+        auto     i32Scale    = pCallInst->getArgOperand(4);
+
+        if (arch != AVX512)
+        {
+            // Call into C function to do the scatter. This has significantly better compile perf
+            // compared to jitting scatter loops for every scatter
+            if (width == W256)
+            {
+                auto mask = B->BITCAST(vi1Mask, B->mInt8Ty);
+                B->CALL(pThis->mPfnScatter256, {pBase, vi32Indices, v32Src, mask, i32Scale});
+            }
+            else
+            {
+                // Need to break up 512 wide scatter to two 256 wide
+                auto maskLo = B->VSHUFFLE(vi1Mask, vi1Mask, B->C({0, 1, 2, 3, 4, 5, 6, 7}));
+                auto indicesLo =
+                    B->VSHUFFLE(vi32Indices, vi32Indices, B->C({0, 1, 2, 3, 4, 5, 6, 7}));
+                auto srcLo = B->VSHUFFLE(v32Src, v32Src, B->C({0, 1, 2, 3, 4, 5, 6, 7}));
+
+                auto mask = B->BITCAST(maskLo, B->mInt8Ty);
+                B->CALL(pThis->mPfnScatter256, {pBase, indicesLo, srcLo, mask, i32Scale});
+
+                auto maskHi = B->VSHUFFLE(vi1Mask, vi1Mask, B->C({8, 9, 10, 11, 12, 13, 14, 15}));
+                auto indicesHi =
+                    B->VSHUFFLE(vi32Indices, vi32Indices, B->C({8, 9, 10, 11, 12, 13, 14, 15}));
+                auto srcHi = B->VSHUFFLE(v32Src, v32Src, B->C({8, 9, 10, 11, 12, 13, 14, 15}));
+
+                mask = B->BITCAST(maskHi, B->mInt8Ty);
+                B->CALL(pThis->mPfnScatter256, {pBase, indicesHi, srcHi, mask, i32Scale});
+            }
+            return nullptr;
+        }
+
+        Value*    iMask;
+        Function* pX86IntrinFunc;
+        if (width == W256)
+        {
+            // No direct intrinsic supported in llvm to scatter 8 elem with 32bit indices, but we
+            // can use the scatter of 8 elements with 64bit indices
+            pX86IntrinFunc = Intrinsic::getDeclaration(B->JM()->mpCurrentModule,
+                                                       Intrinsic::x86_avx512_scatter_qps_512);
+
+            auto vi32IndicesExt = B->Z_EXT(vi32Indices, B->mSimdInt64Ty);
+            iMask               = B->BITCAST(vi1Mask, B->mInt8Ty);
+            B->CALL(pX86IntrinFunc, {pBase, iMask, vi32IndicesExt, v32Src, i32Scale});
+        }
+        else if (width == W512)
+        {
+            pX86IntrinFunc = Intrinsic::getDeclaration(B->JM()->mpCurrentModule,
+                                                       Intrinsic::x86_avx512_scatter_dps_512);
+            iMask          = B->BITCAST(vi1Mask, B->mInt16Ty);
+            B->CALL(pX86IntrinFunc, {pBase, iMask, vi32Indices, v32Src, i32Scale});
+        }
+        return nullptr;
+    }
 
     // No support for vroundps in avx512 (it is available in kncni), so emulate with avx
     // instructions
@@ -650,7 +782,9 @@ namespace SwrJit
 
         auto B       = pThis->B;
         auto vf32Src = pCallInst->getOperand(0);
+        assert(vf32Src);
         auto i8Round = pCallInst->getOperand(1);
+        assert(i8Round);
         auto pfnFunc =
             Intrinsic::getDeclaration(B->JM()->mpCurrentModule, Intrinsic::x86_avx_round_ps_256);
 
@@ -755,10 +889,15 @@ namespace SwrJit
                 auto argType = arg.get()->getType();
                 if (argType->isVectorTy())
                 {
+#if LLVM_VERSION_MAJOR >= 11
+                    uint32_t vecWidth  = cast<VectorType>(argType)->getNumElements();
+                    auto     elemTy    = cast<VectorType>(argType)->getElementType();
+#else
                     uint32_t vecWidth  = argType->getVectorNumElements();
+                    auto     elemTy    = argType->getVectorElementType();
+#endif
                     Value*   lanes     = B->CInc<int>(i * vecWidth / 2, vecWidth / 2);
-                    Value*   argToPush = B->VSHUFFLE(
-                        arg.get(), B->VUNDEF(argType->getVectorElementType(), vecWidth), lanes);
+                    Value*   argToPush = B->VSHUFFLE(arg.get(), B->VUNDEF(elemTy, vecWidth), lanes);
                     args.push_back(argToPush);
                 }
                 else
@@ -772,8 +911,13 @@ namespace SwrJit
         if (result[0]->getType()->isVectorTy())
         {
             assert(result[1]->getType()->isVectorTy());
+#if LLVM_VERSION_MAJOR >= 11
+            vecWidth = cast<VectorType>(result[0]->getType())->getNumElements() +
+                       cast<VectorType>(result[1]->getType())->getNumElements();
+#else
             vecWidth = result[0]->getType()->getVectorNumElements() +
                        result[1]->getType()->getVectorNumElements();
+#endif
         }
         else
         {