1 /****************************************************************************
2 * Copyright (C) 2014-2018 Intel Corporation. All Rights Reserved.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
25 * @brief llvm pass to lower meta code to x86
29 ******************************************************************************/
31 #include "jit_pch.hpp"
33 #include "JitManager.h"
35 #include "common/simdlib.hpp"
37 #include <unordered_map>
39 extern "C" void ScatterPS_256(uint8_t*, SIMD256::Integer
, SIMD256::Float
, uint8_t, uint32_t);
43 // foward declare the initializer
44 void initializeLowerX86Pass(PassRegistry
&);
51 #if LLVM_VERSION_MAJOR > 10
52 typedef unsigned IntrinsicID
;
54 typedef Intrinsic::ID IntrinsicID
;
73 typedef std::function
<Instruction
*(LowerX86
*, TargetArch
, TargetWidth
, CallInst
*)> EmuFunc
;
77 IntrinsicID intrin
[NUM_WIDTHS
];
81 // Map of intrinsics that haven't been moved to the new mechanism yet. If used, these get the
82 // previous behavior of mapping directly to avx/avx2 intrinsics.
83 static std::map
<std::string
, IntrinsicID
> intrinsicMap
= {
84 {"meta.intrinsic.BEXTR_32", Intrinsic::x86_bmi_bextr_32
},
85 {"meta.intrinsic.VPSHUFB", Intrinsic::x86_avx2_pshuf_b
},
86 {"meta.intrinsic.VCVTPS2PH", Intrinsic::x86_vcvtps2ph_256
},
87 {"meta.intrinsic.VPTESTC", Intrinsic::x86_avx_ptestc_256
},
88 {"meta.intrinsic.VPTESTZ", Intrinsic::x86_avx_ptestz_256
},
89 {"meta.intrinsic.VPHADDD", Intrinsic::x86_avx2_phadd_d
},
90 {"meta.intrinsic.PDEP32", Intrinsic::x86_bmi_pdep_32
},
91 {"meta.intrinsic.RDTSC", Intrinsic::x86_rdtsc
},
95 Instruction
* NO_EMU(LowerX86
* pThis
, TargetArch arch
, TargetWidth width
, CallInst
* pCallInst
);
97 VPERM_EMU(LowerX86
* pThis
, TargetArch arch
, TargetWidth width
, CallInst
* pCallInst
);
99 VGATHER_EMU(LowerX86
* pThis
, TargetArch arch
, TargetWidth width
, CallInst
* pCallInst
);
101 VSCATTER_EMU(LowerX86
* pThis
, TargetArch arch
, TargetWidth width
, CallInst
* pCallInst
);
103 VROUND_EMU(LowerX86
* pThis
, TargetArch arch
, TargetWidth width
, CallInst
* pCallInst
);
105 VHSUB_EMU(LowerX86
* pThis
, TargetArch arch
, TargetWidth width
, CallInst
* pCallInst
);
107 VCONVERT_EMU(LowerX86
* pThis
, TargetArch arch
, TargetWidth width
, CallInst
* pCallInst
);
109 Instruction
* DOUBLE_EMU(LowerX86
* pThis
,
113 Intrinsic::ID intrin
);
115 static Intrinsic::ID DOUBLE
= (Intrinsic::ID
)-1;
118 static std::map
<std::string
, X86Intrinsic
> intrinsicMap2
[] = {
122 {"meta.intrinsic.VRCPPS", {{Intrinsic::x86_avx_rcp_ps_256
, DOUBLE
}, NO_EMU
}},
123 {"meta.intrinsic.VPERMPS", {{Intrinsic::not_intrinsic
, Intrinsic::not_intrinsic
}, VPERM_EMU
}},
124 {"meta.intrinsic.VPERMD", {{Intrinsic::not_intrinsic
, Intrinsic::not_intrinsic
}, VPERM_EMU
}},
125 {"meta.intrinsic.VGATHERPD", {{Intrinsic::not_intrinsic
, Intrinsic::not_intrinsic
}, VGATHER_EMU
}},
126 {"meta.intrinsic.VGATHERPS", {{Intrinsic::not_intrinsic
, Intrinsic::not_intrinsic
}, VGATHER_EMU
}},
127 {"meta.intrinsic.VGATHERDD", {{Intrinsic::not_intrinsic
, Intrinsic::not_intrinsic
}, VGATHER_EMU
}},
128 {"meta.intrinsic.VSCATTERPS", {{Intrinsic::not_intrinsic
, Intrinsic::not_intrinsic
}, VSCATTER_EMU
}},
129 {"meta.intrinsic.VCVTPD2PS", {{Intrinsic::x86_avx_cvt_pd2_ps_256
, Intrinsic::not_intrinsic
}, NO_EMU
}},
130 {"meta.intrinsic.VCVTPH2PS", {{Intrinsic::x86_vcvtph2ps_256
, Intrinsic::not_intrinsic
}, NO_EMU
}},
131 {"meta.intrinsic.VROUND", {{Intrinsic::x86_avx_round_ps_256
, DOUBLE
}, NO_EMU
}},
132 {"meta.intrinsic.VHSUBPS", {{Intrinsic::x86_avx_hsub_ps_256
, DOUBLE
}, NO_EMU
}},
136 {"meta.intrinsic.VRCPPS", {{Intrinsic::x86_avx_rcp_ps_256
, DOUBLE
}, NO_EMU
}},
137 {"meta.intrinsic.VPERMPS", {{Intrinsic::x86_avx2_permps
, Intrinsic::not_intrinsic
}, VPERM_EMU
}},
138 {"meta.intrinsic.VPERMD", {{Intrinsic::x86_avx2_permd
, Intrinsic::not_intrinsic
}, VPERM_EMU
}},
139 {"meta.intrinsic.VGATHERPD", {{Intrinsic::not_intrinsic
, Intrinsic::not_intrinsic
}, VGATHER_EMU
}},
140 {"meta.intrinsic.VGATHERPS", {{Intrinsic::not_intrinsic
, Intrinsic::not_intrinsic
}, VGATHER_EMU
}},
141 {"meta.intrinsic.VGATHERDD", {{Intrinsic::not_intrinsic
, Intrinsic::not_intrinsic
}, VGATHER_EMU
}},
142 {"meta.intrinsic.VSCATTERPS", {{Intrinsic::not_intrinsic
, Intrinsic::not_intrinsic
}, VSCATTER_EMU
}},
143 {"meta.intrinsic.VCVTPD2PS", {{Intrinsic::x86_avx_cvt_pd2_ps_256
, DOUBLE
}, NO_EMU
}},
144 {"meta.intrinsic.VCVTPH2PS", {{Intrinsic::x86_vcvtph2ps_256
, Intrinsic::not_intrinsic
}, NO_EMU
}},
145 {"meta.intrinsic.VROUND", {{Intrinsic::x86_avx_round_ps_256
, DOUBLE
}, NO_EMU
}},
146 {"meta.intrinsic.VHSUBPS", {{Intrinsic::x86_avx_hsub_ps_256
, DOUBLE
}, NO_EMU
}},
150 {"meta.intrinsic.VRCPPS", {{Intrinsic::x86_avx512_rcp14_ps_256
, Intrinsic::x86_avx512_rcp14_ps_512
}, NO_EMU
}},
151 #if LLVM_VERSION_MAJOR < 7
152 {"meta.intrinsic.VPERMPS", {{Intrinsic::x86_avx512_mask_permvar_sf_256
, Intrinsic::x86_avx512_mask_permvar_sf_512
}, NO_EMU
}},
153 {"meta.intrinsic.VPERMD", {{Intrinsic::x86_avx512_mask_permvar_si_256
, Intrinsic::x86_avx512_mask_permvar_si_512
}, NO_EMU
}},
155 {"meta.intrinsic.VPERMPS", {{Intrinsic::not_intrinsic
, Intrinsic::not_intrinsic
}, VPERM_EMU
}},
156 {"meta.intrinsic.VPERMD", {{Intrinsic::not_intrinsic
, Intrinsic::not_intrinsic
}, VPERM_EMU
}},
158 {"meta.intrinsic.VGATHERPD", {{Intrinsic::not_intrinsic
, Intrinsic::not_intrinsic
}, VGATHER_EMU
}},
159 {"meta.intrinsic.VGATHERPS", {{Intrinsic::not_intrinsic
, Intrinsic::not_intrinsic
}, VGATHER_EMU
}},
160 {"meta.intrinsic.VGATHERDD", {{Intrinsic::not_intrinsic
, Intrinsic::not_intrinsic
}, VGATHER_EMU
}},
161 {"meta.intrinsic.VSCATTERPS", {{Intrinsic::not_intrinsic
, Intrinsic::not_intrinsic
}, VSCATTER_EMU
}},
162 #if LLVM_VERSION_MAJOR < 7
163 {"meta.intrinsic.VCVTPD2PS", {{Intrinsic::x86_avx512_mask_cvtpd2ps_256
, Intrinsic::x86_avx512_mask_cvtpd2ps_512
}, NO_EMU
}},
165 {"meta.intrinsic.VCVTPD2PS", {{Intrinsic::not_intrinsic
, Intrinsic::not_intrinsic
}, VCONVERT_EMU
}},
167 {"meta.intrinsic.VCVTPH2PS", {{Intrinsic::x86_avx512_mask_vcvtph2ps_256
, Intrinsic::x86_avx512_mask_vcvtph2ps_512
}, NO_EMU
}},
168 {"meta.intrinsic.VROUND", {{Intrinsic::not_intrinsic
, Intrinsic::not_intrinsic
}, VROUND_EMU
}},
169 {"meta.intrinsic.VHSUBPS", {{Intrinsic::not_intrinsic
, Intrinsic::not_intrinsic
}, VHSUB_EMU
}},
173 struct LowerX86
: public FunctionPass
175 LowerX86(Builder
* b
= nullptr) : FunctionPass(ID
), B(b
)
177 initializeLowerX86Pass(*PassRegistry::getPassRegistry());
179 // Determine target arch
180 if (JM()->mArch
.AVX512F())
184 else if (JM()->mArch
.AVX2())
188 else if (JM()->mArch
.AVX())
194 SWR_ASSERT(false, "Unsupported AVX architecture.");
198 // Setup scatter function for 256 wide
199 uint32_t curWidth
= B
->mVWidth
;
200 B
->SetTargetWidth(8);
201 std::vector
<Type
*> args
= {
202 B
->mInt8PtrTy
, // pBase
203 B
->mSimdInt32Ty
, // vIndices
204 B
->mSimdFP32Ty
, // vSrc
209 FunctionType
* pfnScatterTy
= FunctionType::get(B
->mVoidTy
, args
, false);
210 mPfnScatter256
= cast
<Function
>(
211 #if LLVM_VERSION_MAJOR >= 9
212 B
->JM()->mpCurrentModule
->getOrInsertFunction("ScatterPS_256", pfnScatterTy
).getCallee());
214 B
->JM()->mpCurrentModule
->getOrInsertFunction("ScatterPS_256", pfnScatterTy
));
216 if (sys::DynamicLibrary::SearchForAddressOfSymbol("ScatterPS_256") == nullptr)
218 sys::DynamicLibrary::AddSymbol("ScatterPS_256", (void*)&ScatterPS_256
);
221 B
->SetTargetWidth(curWidth
);
224 // Try to decipher the vector type of the instruction. This does not work properly
225 // across all intrinsics, and will have to be rethought. Probably need something
226 // similar to llvm's getDeclaration() utility to map a set of inputs to a specific typed
228 void GetRequestedWidthAndType(CallInst
* pCallInst
,
229 const StringRef intrinName
,
234 Type
* pVecTy
= pCallInst
->getType();
236 // Check for intrinsic specific types
237 // VCVTPD2PS type comes from src, not dst
238 if (intrinName
.equals("meta.intrinsic.VCVTPD2PS"))
240 Value
* pOp
= pCallInst
->getOperand(0);
242 pVecTy
= pOp
->getType();
245 if (!pVecTy
->isVectorTy())
247 for (auto& op
: pCallInst
->arg_operands())
249 if (op
.get()->getType()->isVectorTy())
251 pVecTy
= op
.get()->getType();
256 SWR_ASSERT(pVecTy
->isVectorTy(), "Couldn't determine vector size");
258 uint32_t width
= cast
<VectorType
>(pVecTy
)->getBitWidth();
268 SWR_ASSERT(false, "Unhandled vector width %d", width
);
272 *pTy
= pVecTy
->getScalarType();
275 Value
* GetZeroVec(TargetWidth width
, Type
* pTy
)
277 uint32_t numElem
= 0;
287 SWR_ASSERT(false, "Unhandled vector width type %d\n", width
);
290 return ConstantVector::getNullValue(VectorType::get(pTy
, numElem
));
293 Value
* GetMask(TargetWidth width
)
299 mask
= B
->C((uint8_t)-1);
302 mask
= B
->C((uint16_t)-1);
305 SWR_ASSERT(false, "Unhandled vector width type %d\n", width
);
310 // Convert <N x i1> mask to <N x i32> x86 mask
311 Value
* VectorMask(Value
* vi1Mask
)
313 uint32_t numElem
= vi1Mask
->getType()->getVectorNumElements();
314 return B
->S_EXT(vi1Mask
, VectorType::get(B
->mInt32Ty
, numElem
));
317 Instruction
* ProcessIntrinsicAdvanced(CallInst
* pCallInst
)
319 Function
* pFunc
= pCallInst
->getCalledFunction();
322 auto& intrinsic
= intrinsicMap2
[mTarget
][pFunc
->getName().str()];
323 TargetWidth vecWidth
;
325 GetRequestedWidthAndType(pCallInst
, pFunc
->getName(), &vecWidth
, &pElemTy
);
327 // Check if there is a native intrinsic for this instruction
328 IntrinsicID id
= intrinsic
.intrin
[vecWidth
];
331 // Double pump the next smaller SIMD intrinsic
332 SWR_ASSERT(vecWidth
!= 0, "Cannot double pump smallest SIMD width.");
333 Intrinsic::ID id2
= intrinsic
.intrin
[vecWidth
- 1];
334 SWR_ASSERT(id2
!= Intrinsic::not_intrinsic
,
335 "Cannot find intrinsic to double pump.");
336 return DOUBLE_EMU(this, mTarget
, vecWidth
, pCallInst
, id2
);
338 else if (id
!= Intrinsic::not_intrinsic
)
340 Function
* pIntrin
= Intrinsic::getDeclaration(B
->JM()->mpCurrentModule
, id
);
341 SmallVector
<Value
*, 8> args
;
342 for (auto& arg
: pCallInst
->arg_operands())
344 args
.push_back(arg
.get());
347 // If AVX512, all instructions add a src operand and mask. We'll pass in 0 src and
348 // full mask for now Assuming the intrinsics are consistent and place the src
349 // operand and mask last in the argument list.
350 if (mTarget
== AVX512
)
352 if (pFunc
->getName().equals("meta.intrinsic.VCVTPD2PS"))
354 args
.push_back(GetZeroVec(W256
, pCallInst
->getType()->getScalarType()));
355 args
.push_back(GetMask(W256
));
356 // for AVX512 VCVTPD2PS, we also have to add rounding mode
357 args
.push_back(B
->C(_MM_FROUND_TO_NEAREST_INT
| _MM_FROUND_NO_EXC
));
361 args
.push_back(GetZeroVec(vecWidth
, pElemTy
));
362 args
.push_back(GetMask(vecWidth
));
366 return B
->CALLA(pIntrin
, args
);
370 // No native intrinsic, call emulation function
371 return intrinsic
.emuFunc(this, mTarget
, vecWidth
, pCallInst
);
378 Instruction
* ProcessIntrinsic(CallInst
* pCallInst
)
380 Function
* pFunc
= pCallInst
->getCalledFunction();
383 // Forward to the advanced support if found
384 if (intrinsicMap2
[mTarget
].find(pFunc
->getName().str()) != intrinsicMap2
[mTarget
].end())
386 return ProcessIntrinsicAdvanced(pCallInst
);
389 SWR_ASSERT(intrinsicMap
.find(pFunc
->getName().str()) != intrinsicMap
.end(),
390 "Unimplemented intrinsic %s.",
391 pFunc
->getName().str().c_str());
393 Intrinsic::ID x86Intrinsic
= intrinsicMap
[pFunc
->getName().str()];
394 Function
* pX86IntrinFunc
=
395 Intrinsic::getDeclaration(B
->JM()->mpCurrentModule
, x86Intrinsic
);
397 SmallVector
<Value
*, 8> args
;
398 for (auto& arg
: pCallInst
->arg_operands())
400 args
.push_back(arg
.get());
402 return B
->CALLA(pX86IntrinFunc
, args
);
405 //////////////////////////////////////////////////////////////////////////
406 /// @brief LLVM funtion pass run method.
407 /// @param f- The function we're working on with this pass.
408 virtual bool runOnFunction(Function
& F
)
410 std::vector
<Instruction
*> toRemove
;
411 std::vector
<BasicBlock
*> bbs
;
413 // Make temp copy of the basic blocks and instructions, as the intrinsic
414 // replacement code might invalidate the iterators
415 for (auto& b
: F
.getBasicBlockList())
422 std::vector
<Instruction
*> insts
;
423 for (auto& i
: BB
->getInstList())
428 for (auto* I
: insts
)
430 if (CallInst
* pCallInst
= dyn_cast
<CallInst
>(I
))
432 Function
* pFunc
= pCallInst
->getCalledFunction();
435 if (pFunc
->getName().startswith("meta.intrinsic"))
437 B
->IRB()->SetInsertPoint(I
);
438 Instruction
* pReplace
= ProcessIntrinsic(pCallInst
);
439 toRemove
.push_back(pCallInst
);
442 pCallInst
->replaceAllUsesWith(pReplace
);
450 for (auto* pInst
: toRemove
)
452 pInst
->eraseFromParent();
455 JitManager::DumpToFile(&F
, "lowerx86");
460 virtual void getAnalysisUsage(AnalysisUsage
& AU
) const {}
462 JitManager
* JM() { return B
->JM(); }
465 Function
* mPfnScatter256
;
467 static char ID
; ///< Needed by LLVM to generate ID for FunctionPass.
470 char LowerX86::ID
= 0; // LLVM uses address of ID as the actual ID.
472 FunctionPass
* createLowerX86Pass(Builder
* b
) { return new LowerX86(b
); }
474 Instruction
* NO_EMU(LowerX86
* pThis
, TargetArch arch
, TargetWidth width
, CallInst
* pCallInst
)
476 SWR_ASSERT(false, "Unimplemented intrinsic emulation.");
480 Instruction
* VPERM_EMU(LowerX86
* pThis
, TargetArch arch
, TargetWidth width
, CallInst
* pCallInst
)
482 // Only need vperm emulation for AVX
483 SWR_ASSERT(arch
== AVX
);
485 Builder
* B
= pThis
->B
;
486 auto v32A
= pCallInst
->getArgOperand(0);
487 auto vi32Index
= pCallInst
->getArgOperand(1);
490 if (isa
<Constant
>(vi32Index
))
492 // Can use llvm shuffle vector directly with constant shuffle indices
493 v32Result
= B
->VSHUFFLE(v32A
, v32A
, vi32Index
);
497 v32Result
= UndefValue::get(v32A
->getType());
498 for (uint32_t l
= 0; l
< v32A
->getType()->getVectorNumElements(); ++l
)
500 auto i32Index
= B
->VEXTRACT(vi32Index
, B
->C(l
));
501 auto val
= B
->VEXTRACT(v32A
, i32Index
);
502 v32Result
= B
->VINSERT(v32Result
, val
, B
->C(l
));
505 return cast
<Instruction
>(v32Result
);
509 VGATHER_EMU(LowerX86
* pThis
, TargetArch arch
, TargetWidth width
, CallInst
* pCallInst
)
511 Builder
* B
= pThis
->B
;
512 auto vSrc
= pCallInst
->getArgOperand(0);
513 auto pBase
= pCallInst
->getArgOperand(1);
514 auto vi32Indices
= pCallInst
->getArgOperand(2);
515 auto vi1Mask
= pCallInst
->getArgOperand(3);
516 auto i8Scale
= pCallInst
->getArgOperand(4);
518 pBase
= B
->POINTER_CAST(pBase
, PointerType::get(B
->mInt8Ty
, 0));
519 uint32_t numElem
= vSrc
->getType()->getVectorNumElements();
520 auto i32Scale
= B
->Z_EXT(i8Scale
, B
->mInt32Ty
);
521 auto srcTy
= vSrc
->getType()->getVectorElementType();
522 Value
* v32Gather
= nullptr;
525 // Full emulation for AVX
526 // Store source on stack to provide a valid address to load from inactive lanes
527 auto pStack
= B
->STACKSAVE();
528 auto pTmp
= B
->ALLOCA(vSrc
->getType());
529 B
->STORE(vSrc
, pTmp
);
531 v32Gather
= UndefValue::get(vSrc
->getType());
532 auto vi32Scale
= ConstantVector::getSplat(numElem
, cast
<ConstantInt
>(i32Scale
));
533 auto vi32Offsets
= B
->MUL(vi32Indices
, vi32Scale
);
535 for (uint32_t i
= 0; i
< numElem
; ++i
)
537 auto i32Offset
= B
->VEXTRACT(vi32Offsets
, B
->C(i
));
538 auto pLoadAddress
= B
->GEP(pBase
, i32Offset
);
539 pLoadAddress
= B
->BITCAST(pLoadAddress
, PointerType::get(srcTy
, 0));
540 auto pMaskedLoadAddress
= B
->GEP(pTmp
, {0, i
});
541 auto i1Mask
= B
->VEXTRACT(vi1Mask
, B
->C(i
));
542 auto pValidAddress
= B
->SELECT(i1Mask
, pLoadAddress
, pMaskedLoadAddress
);
543 auto val
= B
->LOAD(pValidAddress
);
544 v32Gather
= B
->VINSERT(v32Gather
, val
, B
->C(i
));
547 B
->STACKRESTORE(pStack
);
549 else if (arch
== AVX2
|| (arch
== AVX512
&& width
== W256
))
551 Function
* pX86IntrinFunc
= nullptr;
552 if (srcTy
== B
->mFP32Ty
)
554 pX86IntrinFunc
= Intrinsic::getDeclaration(B
->JM()->mpCurrentModule
,
555 Intrinsic::x86_avx2_gather_d_ps_256
);
557 else if (srcTy
== B
->mInt32Ty
)
559 pX86IntrinFunc
= Intrinsic::getDeclaration(B
->JM()->mpCurrentModule
,
560 Intrinsic::x86_avx2_gather_d_d_256
);
562 else if (srcTy
== B
->mDoubleTy
)
564 pX86IntrinFunc
= Intrinsic::getDeclaration(B
->JM()->mpCurrentModule
,
565 Intrinsic::x86_avx2_gather_d_q_256
);
569 SWR_ASSERT(false, "Unsupported vector element type for gather.");
574 auto v32Mask
= B
->BITCAST(pThis
->VectorMask(vi1Mask
), vSrc
->getType());
575 v32Gather
= B
->CALL(pX86IntrinFunc
, {vSrc
, pBase
, vi32Indices
, v32Mask
, i8Scale
});
577 else if (width
== W512
)
579 // Double pump 4-wide for 64bit elements
580 if (vSrc
->getType()->getVectorElementType() == B
->mDoubleTy
)
582 auto v64Mask
= pThis
->VectorMask(vi1Mask
);
585 VectorType::get(B
->mInt64Ty
, v64Mask
->getType()->getVectorNumElements()));
586 v64Mask
= B
->BITCAST(v64Mask
, vSrc
->getType());
588 Value
* src0
= B
->VSHUFFLE(vSrc
, vSrc
, B
->C({0, 1, 2, 3}));
589 Value
* src1
= B
->VSHUFFLE(vSrc
, vSrc
, B
->C({4, 5, 6, 7}));
591 Value
* indices0
= B
->VSHUFFLE(vi32Indices
, vi32Indices
, B
->C({0, 1, 2, 3}));
592 Value
* indices1
= B
->VSHUFFLE(vi32Indices
, vi32Indices
, B
->C({4, 5, 6, 7}));
594 Value
* mask0
= B
->VSHUFFLE(v64Mask
, v64Mask
, B
->C({0, 1, 2, 3}));
595 Value
* mask1
= B
->VSHUFFLE(v64Mask
, v64Mask
, B
->C({4, 5, 6, 7}));
599 VectorType::get(B
->mInt64Ty
, src0
->getType()->getVectorNumElements()));
602 VectorType::get(B
->mInt64Ty
, mask0
->getType()->getVectorNumElements()));
604 B
->CALL(pX86IntrinFunc
, {src0
, pBase
, indices0
, mask0
, i8Scale
});
607 VectorType::get(B
->mInt64Ty
, src1
->getType()->getVectorNumElements()));
610 VectorType::get(B
->mInt64Ty
, mask1
->getType()->getVectorNumElements()));
612 B
->CALL(pX86IntrinFunc
, {src1
, pBase
, indices1
, mask1
, i8Scale
});
614 v32Gather
= B
->VSHUFFLE(gather0
, gather1
, B
->C({0, 1, 2, 3, 4, 5, 6, 7}));
615 v32Gather
= B
->BITCAST(v32Gather
, vSrc
->getType());
619 // Double pump 8-wide for 32bit elements
620 auto v32Mask
= pThis
->VectorMask(vi1Mask
);
621 v32Mask
= B
->BITCAST(v32Mask
, vSrc
->getType());
622 Value
* src0
= B
->EXTRACT_16(vSrc
, 0);
623 Value
* src1
= B
->EXTRACT_16(vSrc
, 1);
625 Value
* indices0
= B
->EXTRACT_16(vi32Indices
, 0);
626 Value
* indices1
= B
->EXTRACT_16(vi32Indices
, 1);
628 Value
* mask0
= B
->EXTRACT_16(v32Mask
, 0);
629 Value
* mask1
= B
->EXTRACT_16(v32Mask
, 1);
632 B
->CALL(pX86IntrinFunc
, {src0
, pBase
, indices0
, mask0
, i8Scale
});
634 B
->CALL(pX86IntrinFunc
, {src1
, pBase
, indices1
, mask1
, i8Scale
});
636 v32Gather
= B
->JOIN_16(gather0
, gather1
);
640 else if (arch
== AVX512
)
642 Value
* iMask
= nullptr;
643 Function
* pX86IntrinFunc
= nullptr;
644 if (srcTy
== B
->mFP32Ty
)
646 pX86IntrinFunc
= Intrinsic::getDeclaration(B
->JM()->mpCurrentModule
,
647 Intrinsic::x86_avx512_gather_dps_512
);
648 iMask
= B
->BITCAST(vi1Mask
, B
->mInt16Ty
);
650 else if (srcTy
== B
->mInt32Ty
)
652 pX86IntrinFunc
= Intrinsic::getDeclaration(B
->JM()->mpCurrentModule
,
653 Intrinsic::x86_avx512_gather_dpi_512
);
654 iMask
= B
->BITCAST(vi1Mask
, B
->mInt16Ty
);
656 else if (srcTy
== B
->mDoubleTy
)
658 pX86IntrinFunc
= Intrinsic::getDeclaration(B
->JM()->mpCurrentModule
,
659 Intrinsic::x86_avx512_gather_dpd_512
);
660 iMask
= B
->BITCAST(vi1Mask
, B
->mInt8Ty
);
664 SWR_ASSERT(false, "Unsupported vector element type for gather.");
667 auto i32Scale
= B
->Z_EXT(i8Scale
, B
->mInt32Ty
);
668 v32Gather
= B
->CALL(pX86IntrinFunc
, {vSrc
, pBase
, vi32Indices
, iMask
, i32Scale
});
671 return cast
<Instruction
>(v32Gather
);
674 VSCATTER_EMU(LowerX86
* pThis
, TargetArch arch
, TargetWidth width
, CallInst
* pCallInst
)
676 Builder
* B
= pThis
->B
;
677 auto pBase
= pCallInst
->getArgOperand(0);
678 auto vi1Mask
= pCallInst
->getArgOperand(1);
679 auto vi32Indices
= pCallInst
->getArgOperand(2);
680 auto v32Src
= pCallInst
->getArgOperand(3);
681 auto i32Scale
= pCallInst
->getArgOperand(4);
685 // Call into C function to do the scatter. This has significantly better compile perf
686 // compared to jitting scatter loops for every scatter
689 auto mask
= B
->BITCAST(vi1Mask
, B
->mInt8Ty
);
690 B
->CALL(pThis
->mPfnScatter256
, {pBase
, vi32Indices
, v32Src
, mask
, i32Scale
});
694 // Need to break up 512 wide scatter to two 256 wide
695 auto maskLo
= B
->VSHUFFLE(vi1Mask
, vi1Mask
, B
->C({0, 1, 2, 3, 4, 5, 6, 7}));
697 B
->VSHUFFLE(vi32Indices
, vi32Indices
, B
->C({0, 1, 2, 3, 4, 5, 6, 7}));
698 auto srcLo
= B
->VSHUFFLE(v32Src
, v32Src
, B
->C({0, 1, 2, 3, 4, 5, 6, 7}));
700 auto mask
= B
->BITCAST(maskLo
, B
->mInt8Ty
);
701 B
->CALL(pThis
->mPfnScatter256
, {pBase
, indicesLo
, srcLo
, mask
, i32Scale
});
703 auto maskHi
= B
->VSHUFFLE(vi1Mask
, vi1Mask
, B
->C({8, 9, 10, 11, 12, 13, 14, 15}));
705 B
->VSHUFFLE(vi32Indices
, vi32Indices
, B
->C({8, 9, 10, 11, 12, 13, 14, 15}));
706 auto srcHi
= B
->VSHUFFLE(v32Src
, v32Src
, B
->C({8, 9, 10, 11, 12, 13, 14, 15}));
708 mask
= B
->BITCAST(maskHi
, B
->mInt8Ty
);
709 B
->CALL(pThis
->mPfnScatter256
, {pBase
, indicesHi
, srcHi
, mask
, i32Scale
});
715 Function
* pX86IntrinFunc
;
718 // No direct intrinsic supported in llvm to scatter 8 elem with 32bit indices, but we
719 // can use the scatter of 8 elements with 64bit indices
720 pX86IntrinFunc
= Intrinsic::getDeclaration(B
->JM()->mpCurrentModule
,
721 Intrinsic::x86_avx512_scatter_qps_512
);
723 auto vi32IndicesExt
= B
->Z_EXT(vi32Indices
, B
->mSimdInt64Ty
);
724 iMask
= B
->BITCAST(vi1Mask
, B
->mInt8Ty
);
725 B
->CALL(pX86IntrinFunc
, {pBase
, iMask
, vi32IndicesExt
, v32Src
, i32Scale
});
727 else if (width
== W512
)
729 pX86IntrinFunc
= Intrinsic::getDeclaration(B
->JM()->mpCurrentModule
,
730 Intrinsic::x86_avx512_scatter_dps_512
);
731 iMask
= B
->BITCAST(vi1Mask
, B
->mInt16Ty
);
732 B
->CALL(pX86IntrinFunc
, {pBase
, iMask
, vi32Indices
, v32Src
, i32Scale
});
737 // No support for vroundps in avx512 (it is available in kncni), so emulate with avx
740 VROUND_EMU(LowerX86
* pThis
, TargetArch arch
, TargetWidth width
, CallInst
* pCallInst
)
742 SWR_ASSERT(arch
== AVX512
);
745 auto vf32Src
= pCallInst
->getOperand(0);
747 auto i8Round
= pCallInst
->getOperand(1);
750 Intrinsic::getDeclaration(B
->JM()->mpCurrentModule
, Intrinsic::x86_avx_round_ps_256
);
754 return cast
<Instruction
>(B
->CALL2(pfnFunc
, vf32Src
, i8Round
));
756 else if (width
== W512
)
758 auto v8f32SrcLo
= B
->EXTRACT_16(vf32Src
, 0);
759 auto v8f32SrcHi
= B
->EXTRACT_16(vf32Src
, 1);
761 auto v8f32ResLo
= B
->CALL2(pfnFunc
, v8f32SrcLo
, i8Round
);
762 auto v8f32ResHi
= B
->CALL2(pfnFunc
, v8f32SrcHi
, i8Round
);
764 return cast
<Instruction
>(B
->JOIN_16(v8f32ResLo
, v8f32ResHi
));
768 SWR_ASSERT(false, "Unimplemented vector width.");
775 VCONVERT_EMU(LowerX86
* pThis
, TargetArch arch
, TargetWidth width
, CallInst
* pCallInst
)
777 SWR_ASSERT(arch
== AVX512
);
780 auto vf32Src
= pCallInst
->getOperand(0);
784 auto vf32SrcRound
= Intrinsic::getDeclaration(B
->JM()->mpCurrentModule
,
785 Intrinsic::x86_avx_round_ps_256
);
786 return cast
<Instruction
>(B
->FP_TRUNC(vf32SrcRound
, B
->mFP32Ty
));
788 else if (width
== W512
)
790 // 512 can use intrinsic
791 auto pfnFunc
= Intrinsic::getDeclaration(B
->JM()->mpCurrentModule
,
792 Intrinsic::x86_avx512_mask_cvtpd2ps_512
);
793 return cast
<Instruction
>(B
->CALL(pfnFunc
, vf32Src
));
797 SWR_ASSERT(false, "Unimplemented vector width.");
803 // No support for hsub in AVX512
804 Instruction
* VHSUB_EMU(LowerX86
* pThis
, TargetArch arch
, TargetWidth width
, CallInst
* pCallInst
)
806 SWR_ASSERT(arch
== AVX512
);
809 auto src0
= pCallInst
->getOperand(0);
810 auto src1
= pCallInst
->getOperand(1);
812 // 256b hsub can just use avx intrinsic
815 auto pX86IntrinFunc
=
816 Intrinsic::getDeclaration(B
->JM()->mpCurrentModule
, Intrinsic::x86_avx_hsub_ps_256
);
817 return cast
<Instruction
>(B
->CALL2(pX86IntrinFunc
, src0
, src1
));
819 else if (width
== W512
)
821 // 512b hsub can be accomplished with shuf/sub combo
822 auto minuend
= B
->VSHUFFLE(src0
, src1
, B
->C({0, 2, 8, 10, 4, 6, 12, 14}));
823 auto subtrahend
= B
->VSHUFFLE(src0
, src1
, B
->C({1, 3, 9, 11, 5, 7, 13, 15}));
824 return cast
<Instruction
>(B
->SUB(minuend
, subtrahend
));
828 SWR_ASSERT(false, "Unimplemented vector width.");
833 // Double pump input using Intrin template arg. This blindly extracts lower and upper 256 from
834 // each vector argument and calls the 256 wide intrinsic, then merges the results to 512 wide
835 Instruction
* DOUBLE_EMU(LowerX86
* pThis
,
839 Intrinsic::ID intrin
)
842 SWR_ASSERT(width
== W512
);
844 Function
* pX86IntrinFunc
= Intrinsic::getDeclaration(B
->JM()->mpCurrentModule
, intrin
);
845 for (uint32_t i
= 0; i
< 2; ++i
)
847 SmallVector
<Value
*, 8> args
;
848 for (auto& arg
: pCallInst
->arg_operands())
850 auto argType
= arg
.get()->getType();
851 if (argType
->isVectorTy())
853 uint32_t vecWidth
= argType
->getVectorNumElements();
854 Value
* lanes
= B
->CInc
<int>(i
* vecWidth
/ 2, vecWidth
/ 2);
855 Value
* argToPush
= B
->VSHUFFLE(
856 arg
.get(), B
->VUNDEF(argType
->getVectorElementType(), vecWidth
), lanes
);
857 args
.push_back(argToPush
);
861 args
.push_back(arg
.get());
864 result
[i
] = B
->CALLA(pX86IntrinFunc
, args
);
867 if (result
[0]->getType()->isVectorTy())
869 assert(result
[1]->getType()->isVectorTy());
870 vecWidth
= result
[0]->getType()->getVectorNumElements() +
871 result
[1]->getType()->getVectorNumElements();
877 Value
* lanes
= B
->CInc
<int>(0, vecWidth
);
878 return cast
<Instruction
>(B
->VSHUFFLE(result
[0], result
[1], lanes
));
881 } // namespace SwrJit
883 using namespace SwrJit
;
885 INITIALIZE_PASS_BEGIN(LowerX86
, "LowerX86", "LowerX86", false, false)
886 INITIALIZE_PASS_END(LowerX86
, "LowerX86", "LowerX86", false, false)