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
&);
67 typedef std::function
<Instruction
*(LowerX86
*, TargetArch
, TargetWidth
, CallInst
*)> EmuFunc
;
71 Intrinsic::ID intrin
[NUM_WIDTHS
];
75 // Map of intrinsics that haven't been moved to the new mechanism yet. If used, these get the
76 // previous behavior of mapping directly to avx/avx2 intrinsics.
77 static std::map
<std::string
, Intrinsic::ID
> intrinsicMap
= {
78 {"meta.intrinsic.BEXTR_32", Intrinsic::x86_bmi_bextr_32
},
79 {"meta.intrinsic.VPSHUFB", Intrinsic::x86_avx2_pshuf_b
},
80 {"meta.intrinsic.VCVTPS2PH", Intrinsic::x86_vcvtps2ph_256
},
81 {"meta.intrinsic.VPTESTC", Intrinsic::x86_avx_ptestc_256
},
82 {"meta.intrinsic.VPTESTZ", Intrinsic::x86_avx_ptestz_256
},
83 {"meta.intrinsic.VPHADDD", Intrinsic::x86_avx2_phadd_d
},
84 {"meta.intrinsic.PDEP32", Intrinsic::x86_bmi_pdep_32
},
85 {"meta.intrinsic.RDTSC", Intrinsic::x86_rdtsc
},
89 Instruction
* NO_EMU(LowerX86
* pThis
, TargetArch arch
, TargetWidth width
, CallInst
* pCallInst
);
91 VPERM_EMU(LowerX86
* pThis
, TargetArch arch
, TargetWidth width
, CallInst
* pCallInst
);
93 VGATHER_EMU(LowerX86
* pThis
, TargetArch arch
, TargetWidth width
, CallInst
* pCallInst
);
95 VSCATTER_EMU(LowerX86
* pThis
, TargetArch arch
, TargetWidth width
, CallInst
* pCallInst
);
97 VROUND_EMU(LowerX86
* pThis
, TargetArch arch
, TargetWidth width
, CallInst
* pCallInst
);
99 VHSUB_EMU(LowerX86
* pThis
, TargetArch arch
, TargetWidth width
, CallInst
* pCallInst
);
101 VCONVERT_EMU(LowerX86
* pThis
, TargetArch arch
, TargetWidth width
, CallInst
* pCallInst
);
103 Instruction
* DOUBLE_EMU(LowerX86
* pThis
,
107 Intrinsic::ID intrin
);
109 static Intrinsic::ID DOUBLE
= (Intrinsic::ID
)-1;
112 static std::map
<std::string
, X86Intrinsic
> intrinsicMap2
[] = {
116 {"meta.intrinsic.VRCPPS", {{Intrinsic::x86_avx_rcp_ps_256
, DOUBLE
}, NO_EMU
}},
117 {"meta.intrinsic.VPERMPS", {{Intrinsic::not_intrinsic
, Intrinsic::not_intrinsic
}, VPERM_EMU
}},
118 {"meta.intrinsic.VPERMD", {{Intrinsic::not_intrinsic
, Intrinsic::not_intrinsic
}, VPERM_EMU
}},
119 {"meta.intrinsic.VGATHERPD", {{Intrinsic::not_intrinsic
, Intrinsic::not_intrinsic
}, VGATHER_EMU
}},
120 {"meta.intrinsic.VGATHERPS", {{Intrinsic::not_intrinsic
, Intrinsic::not_intrinsic
}, VGATHER_EMU
}},
121 {"meta.intrinsic.VGATHERDD", {{Intrinsic::not_intrinsic
, Intrinsic::not_intrinsic
}, VGATHER_EMU
}},
122 {"meta.intrinsic.VSCATTERPS", {{Intrinsic::not_intrinsic
, Intrinsic::not_intrinsic
}, VSCATTER_EMU
}},
123 {"meta.intrinsic.VCVTPD2PS", {{Intrinsic::x86_avx_cvt_pd2_ps_256
, Intrinsic::not_intrinsic
}, NO_EMU
}},
124 {"meta.intrinsic.VCVTPH2PS", {{Intrinsic::x86_vcvtph2ps_256
, Intrinsic::not_intrinsic
}, NO_EMU
}},
125 {"meta.intrinsic.VROUND", {{Intrinsic::x86_avx_round_ps_256
, DOUBLE
}, NO_EMU
}},
126 {"meta.intrinsic.VHSUBPS", {{Intrinsic::x86_avx_hsub_ps_256
, DOUBLE
}, NO_EMU
}},
130 {"meta.intrinsic.VRCPPS", {{Intrinsic::x86_avx_rcp_ps_256
, DOUBLE
}, NO_EMU
}},
131 {"meta.intrinsic.VPERMPS", {{Intrinsic::x86_avx2_permps
, Intrinsic::not_intrinsic
}, VPERM_EMU
}},
132 {"meta.intrinsic.VPERMD", {{Intrinsic::x86_avx2_permd
, Intrinsic::not_intrinsic
}, VPERM_EMU
}},
133 {"meta.intrinsic.VGATHERPD", {{Intrinsic::not_intrinsic
, Intrinsic::not_intrinsic
}, VGATHER_EMU
}},
134 {"meta.intrinsic.VGATHERPS", {{Intrinsic::not_intrinsic
, Intrinsic::not_intrinsic
}, VGATHER_EMU
}},
135 {"meta.intrinsic.VGATHERDD", {{Intrinsic::not_intrinsic
, Intrinsic::not_intrinsic
}, VGATHER_EMU
}},
136 {"meta.intrinsic.VSCATTERPS", {{Intrinsic::not_intrinsic
, Intrinsic::not_intrinsic
}, VSCATTER_EMU
}},
137 {"meta.intrinsic.VCVTPD2PS", {{Intrinsic::x86_avx_cvt_pd2_ps_256
, DOUBLE
}, NO_EMU
}},
138 {"meta.intrinsic.VCVTPH2PS", {{Intrinsic::x86_vcvtph2ps_256
, Intrinsic::not_intrinsic
}, NO_EMU
}},
139 {"meta.intrinsic.VROUND", {{Intrinsic::x86_avx_round_ps_256
, DOUBLE
}, NO_EMU
}},
140 {"meta.intrinsic.VHSUBPS", {{Intrinsic::x86_avx_hsub_ps_256
, DOUBLE
}, NO_EMU
}},
144 {"meta.intrinsic.VRCPPS", {{Intrinsic::x86_avx512_rcp14_ps_256
, Intrinsic::x86_avx512_rcp14_ps_512
}, NO_EMU
}},
145 #if LLVM_VERSION_MAJOR < 7
146 {"meta.intrinsic.VPERMPS", {{Intrinsic::x86_avx512_mask_permvar_sf_256
, Intrinsic::x86_avx512_mask_permvar_sf_512
}, NO_EMU
}},
147 {"meta.intrinsic.VPERMD", {{Intrinsic::x86_avx512_mask_permvar_si_256
, Intrinsic::x86_avx512_mask_permvar_si_512
}, NO_EMU
}},
149 {"meta.intrinsic.VPERMPS", {{Intrinsic::not_intrinsic
, Intrinsic::not_intrinsic
}, VPERM_EMU
}},
150 {"meta.intrinsic.VPERMD", {{Intrinsic::not_intrinsic
, Intrinsic::not_intrinsic
}, VPERM_EMU
}},
152 {"meta.intrinsic.VGATHERPD", {{Intrinsic::not_intrinsic
, Intrinsic::not_intrinsic
}, VGATHER_EMU
}},
153 {"meta.intrinsic.VGATHERPS", {{Intrinsic::not_intrinsic
, Intrinsic::not_intrinsic
}, VGATHER_EMU
}},
154 {"meta.intrinsic.VGATHERDD", {{Intrinsic::not_intrinsic
, Intrinsic::not_intrinsic
}, VGATHER_EMU
}},
155 {"meta.intrinsic.VSCATTERPS", {{Intrinsic::not_intrinsic
, Intrinsic::not_intrinsic
}, VSCATTER_EMU
}},
156 #if LLVM_VERSION_MAJOR < 7
157 {"meta.intrinsic.VCVTPD2PS", {{Intrinsic::x86_avx512_mask_cvtpd2ps_256
, Intrinsic::x86_avx512_mask_cvtpd2ps_512
}, NO_EMU
}},
159 {"meta.intrinsic.VCVTPD2PS", {{Intrinsic::not_intrinsic
, Intrinsic::not_intrinsic
}, VCONVERT_EMU
}},
161 {"meta.intrinsic.VCVTPH2PS", {{Intrinsic::x86_avx512_mask_vcvtph2ps_256
, Intrinsic::x86_avx512_mask_vcvtph2ps_512
}, NO_EMU
}},
162 {"meta.intrinsic.VROUND", {{Intrinsic::not_intrinsic
, Intrinsic::not_intrinsic
}, VROUND_EMU
}},
163 {"meta.intrinsic.VHSUBPS", {{Intrinsic::not_intrinsic
, Intrinsic::not_intrinsic
}, VHSUB_EMU
}},
167 struct LowerX86
: public FunctionPass
169 LowerX86(Builder
* b
= nullptr) : FunctionPass(ID
), B(b
)
171 initializeLowerX86Pass(*PassRegistry::getPassRegistry());
173 // Determine target arch
174 if (JM()->mArch
.AVX512F())
178 else if (JM()->mArch
.AVX2())
182 else if (JM()->mArch
.AVX())
188 SWR_ASSERT(false, "Unsupported AVX architecture.");
192 // Setup scatter function for 256 wide
193 uint32_t curWidth
= B
->mVWidth
;
194 B
->SetTargetWidth(8);
195 std::vector
<Type
*> args
= {
196 B
->mInt8PtrTy
, // pBase
197 B
->mSimdInt32Ty
, // vIndices
198 B
->mSimdFP32Ty
, // vSrc
203 FunctionType
* pfnScatterTy
= FunctionType::get(B
->mVoidTy
, args
, false);
204 mPfnScatter256
= cast
<Function
>(
205 B
->JM()->mpCurrentModule
->getOrInsertFunction("ScatterPS_256", pfnScatterTy
));
206 if (sys::DynamicLibrary::SearchForAddressOfSymbol("ScatterPS_256") == nullptr)
208 sys::DynamicLibrary::AddSymbol("ScatterPS_256", (void*)&ScatterPS_256
);
211 B
->SetTargetWidth(curWidth
);
214 // Try to decipher the vector type of the instruction. This does not work properly
215 // across all intrinsics, and will have to be rethought. Probably need something
216 // similar to llvm's getDeclaration() utility to map a set of inputs to a specific typed
218 void GetRequestedWidthAndType(CallInst
* pCallInst
,
219 const StringRef intrinName
,
223 Type
* pVecTy
= pCallInst
->getType();
225 // Check for intrinsic specific types
226 // VCVTPD2PS type comes from src, not dst
227 if (intrinName
.equals("meta.intrinsic.VCVTPD2PS"))
229 pVecTy
= pCallInst
->getOperand(0)->getType();
232 if (!pVecTy
->isVectorTy())
234 for (auto& op
: pCallInst
->arg_operands())
236 if (op
.get()->getType()->isVectorTy())
238 pVecTy
= op
.get()->getType();
243 SWR_ASSERT(pVecTy
->isVectorTy(), "Couldn't determine vector size");
245 uint32_t width
= cast
<VectorType
>(pVecTy
)->getBitWidth();
255 SWR_ASSERT(false, "Unhandled vector width %d", width
);
259 *pTy
= pVecTy
->getScalarType();
262 Value
* GetZeroVec(TargetWidth width
, Type
* pTy
)
264 uint32_t numElem
= 0;
274 SWR_ASSERT(false, "Unhandled vector width type %d\n", width
);
277 return ConstantVector::getNullValue(VectorType::get(pTy
, numElem
));
280 Value
* GetMask(TargetWidth width
)
286 mask
= B
->C((uint8_t)-1);
289 mask
= B
->C((uint16_t)-1);
292 SWR_ASSERT(false, "Unhandled vector width type %d\n", width
);
297 // Convert <N x i1> mask to <N x i32> x86 mask
298 Value
* VectorMask(Value
* vi1Mask
)
300 uint32_t numElem
= vi1Mask
->getType()->getVectorNumElements();
301 return B
->S_EXT(vi1Mask
, VectorType::get(B
->mInt32Ty
, numElem
));
304 Instruction
* ProcessIntrinsicAdvanced(CallInst
* pCallInst
)
306 Function
* pFunc
= pCallInst
->getCalledFunction();
307 auto& intrinsic
= intrinsicMap2
[mTarget
][pFunc
->getName()];
308 TargetWidth vecWidth
;
310 GetRequestedWidthAndType(pCallInst
, pFunc
->getName(), &vecWidth
, &pElemTy
);
312 // Check if there is a native intrinsic for this instruction
313 Intrinsic::ID id
= intrinsic
.intrin
[vecWidth
];
316 // Double pump the next smaller SIMD intrinsic
317 SWR_ASSERT(vecWidth
!= 0, "Cannot double pump smallest SIMD width.");
318 Intrinsic::ID id2
= intrinsic
.intrin
[vecWidth
- 1];
319 SWR_ASSERT(id2
!= Intrinsic::not_intrinsic
,
320 "Cannot find intrinsic to double pump.");
321 return DOUBLE_EMU(this, mTarget
, vecWidth
, pCallInst
, id2
);
323 else if (id
!= Intrinsic::not_intrinsic
)
325 Function
* pIntrin
= Intrinsic::getDeclaration(B
->JM()->mpCurrentModule
, id
);
326 SmallVector
<Value
*, 8> args
;
327 for (auto& arg
: pCallInst
->arg_operands())
329 args
.push_back(arg
.get());
332 // If AVX512, all instructions add a src operand and mask. We'll pass in 0 src and
333 // full mask for now Assuming the intrinsics are consistent and place the src
334 // operand and mask last in the argument list.
335 if (mTarget
== AVX512
)
337 if (pFunc
->getName().equals("meta.intrinsic.VCVTPD2PS"))
339 args
.push_back(GetZeroVec(W256
, pCallInst
->getType()->getScalarType()));
340 args
.push_back(GetMask(W256
));
341 // for AVX512 VCVTPD2PS, we also have to add rounding mode
342 args
.push_back(B
->C(_MM_FROUND_TO_NEAREST_INT
| _MM_FROUND_NO_EXC
));
346 args
.push_back(GetZeroVec(vecWidth
, pElemTy
));
347 args
.push_back(GetMask(vecWidth
));
351 return B
->CALLA(pIntrin
, args
);
355 // No native intrinsic, call emulation function
356 return intrinsic
.emuFunc(this, mTarget
, vecWidth
, pCallInst
);
363 Instruction
* ProcessIntrinsic(CallInst
* pCallInst
)
365 Function
* pFunc
= pCallInst
->getCalledFunction();
367 // Forward to the advanced support if found
368 if (intrinsicMap2
[mTarget
].find(pFunc
->getName()) != intrinsicMap2
[mTarget
].end())
370 return ProcessIntrinsicAdvanced(pCallInst
);
373 SWR_ASSERT(intrinsicMap
.find(pFunc
->getName()) != intrinsicMap
.end(),
374 "Unimplemented intrinsic %s.",
377 Intrinsic::ID x86Intrinsic
= intrinsicMap
[pFunc
->getName()];
378 Function
* pX86IntrinFunc
=
379 Intrinsic::getDeclaration(B
->JM()->mpCurrentModule
, x86Intrinsic
);
381 SmallVector
<Value
*, 8> args
;
382 for (auto& arg
: pCallInst
->arg_operands())
384 args
.push_back(arg
.get());
386 return B
->CALLA(pX86IntrinFunc
, args
);
389 //////////////////////////////////////////////////////////////////////////
390 /// @brief LLVM funtion pass run method.
391 /// @param f- The function we're working on with this pass.
392 virtual bool runOnFunction(Function
& F
)
394 std::vector
<Instruction
*> toRemove
;
395 std::vector
<BasicBlock
*> bbs
;
397 // Make temp copy of the basic blocks and instructions, as the intrinsic
398 // replacement code might invalidate the iterators
399 for (auto& b
: F
.getBasicBlockList())
406 std::vector
<Instruction
*> insts
;
407 for (auto& i
: BB
->getInstList())
412 for (auto* I
: insts
)
414 if (CallInst
* pCallInst
= dyn_cast
<CallInst
>(I
))
416 Function
* pFunc
= pCallInst
->getCalledFunction();
419 if (pFunc
->getName().startswith("meta.intrinsic"))
421 B
->IRB()->SetInsertPoint(I
);
422 Instruction
* pReplace
= ProcessIntrinsic(pCallInst
);
423 toRemove
.push_back(pCallInst
);
426 pCallInst
->replaceAllUsesWith(pReplace
);
434 for (auto* pInst
: toRemove
)
436 pInst
->eraseFromParent();
439 JitManager::DumpToFile(&F
, "lowerx86");
444 virtual void getAnalysisUsage(AnalysisUsage
& AU
) const {}
446 JitManager
* JM() { return B
->JM(); }
449 Function
* mPfnScatter256
;
451 static char ID
; ///< Needed by LLVM to generate ID for FunctionPass.
454 char LowerX86::ID
= 0; // LLVM uses address of ID as the actual ID.
456 FunctionPass
* createLowerX86Pass(Builder
* b
) { return new LowerX86(b
); }
458 Instruction
* NO_EMU(LowerX86
* pThis
, TargetArch arch
, TargetWidth width
, CallInst
* pCallInst
)
460 SWR_ASSERT(false, "Unimplemented intrinsic emulation.");
464 Instruction
* VPERM_EMU(LowerX86
* pThis
, TargetArch arch
, TargetWidth width
, CallInst
* pCallInst
)
466 // Only need vperm emulation for AVX
467 SWR_ASSERT(arch
== AVX
);
469 Builder
* B
= pThis
->B
;
470 auto v32A
= pCallInst
->getArgOperand(0);
471 auto vi32Index
= pCallInst
->getArgOperand(1);
474 if (isa
<Constant
>(vi32Index
))
476 // Can use llvm shuffle vector directly with constant shuffle indices
477 v32Result
= B
->VSHUFFLE(v32A
, v32A
, vi32Index
);
481 v32Result
= UndefValue::get(v32A
->getType());
482 for (uint32_t l
= 0; l
< v32A
->getType()->getVectorNumElements(); ++l
)
484 auto i32Index
= B
->VEXTRACT(vi32Index
, B
->C(l
));
485 auto val
= B
->VEXTRACT(v32A
, i32Index
);
486 v32Result
= B
->VINSERT(v32Result
, val
, B
->C(l
));
489 return cast
<Instruction
>(v32Result
);
493 VGATHER_EMU(LowerX86
* pThis
, TargetArch arch
, TargetWidth width
, CallInst
* pCallInst
)
495 Builder
* B
= pThis
->B
;
496 auto vSrc
= pCallInst
->getArgOperand(0);
497 auto pBase
= pCallInst
->getArgOperand(1);
498 auto vi32Indices
= pCallInst
->getArgOperand(2);
499 auto vi1Mask
= pCallInst
->getArgOperand(3);
500 auto i8Scale
= pCallInst
->getArgOperand(4);
502 pBase
= B
->POINTER_CAST(pBase
, PointerType::get(B
->mInt8Ty
, 0));
503 uint32_t numElem
= vSrc
->getType()->getVectorNumElements();
504 auto i32Scale
= B
->Z_EXT(i8Scale
, B
->mInt32Ty
);
505 auto srcTy
= vSrc
->getType()->getVectorElementType();
509 // Full emulation for AVX
510 // Store source on stack to provide a valid address to load from inactive lanes
511 auto pStack
= B
->STACKSAVE();
512 auto pTmp
= B
->ALLOCA(vSrc
->getType());
513 B
->STORE(vSrc
, pTmp
);
515 v32Gather
= UndefValue::get(vSrc
->getType());
516 auto vi32Scale
= ConstantVector::getSplat(numElem
, cast
<ConstantInt
>(i32Scale
));
517 auto vi32Offsets
= B
->MUL(vi32Indices
, vi32Scale
);
519 for (uint32_t i
= 0; i
< numElem
; ++i
)
521 auto i32Offset
= B
->VEXTRACT(vi32Offsets
, B
->C(i
));
522 auto pLoadAddress
= B
->GEP(pBase
, i32Offset
);
523 pLoadAddress
= B
->BITCAST(pLoadAddress
, PointerType::get(srcTy
, 0));
524 auto pMaskedLoadAddress
= B
->GEP(pTmp
, {0, i
});
525 auto i1Mask
= B
->VEXTRACT(vi1Mask
, B
->C(i
));
526 auto pValidAddress
= B
->SELECT(i1Mask
, pLoadAddress
, pMaskedLoadAddress
);
527 auto val
= B
->LOAD(pValidAddress
);
528 v32Gather
= B
->VINSERT(v32Gather
, val
, B
->C(i
));
531 B
->STACKRESTORE(pStack
);
533 else if (arch
== AVX2
|| (arch
== AVX512
&& width
== W256
))
535 Function
* pX86IntrinFunc
;
536 if (srcTy
== B
->mFP32Ty
)
538 pX86IntrinFunc
= Intrinsic::getDeclaration(B
->JM()->mpCurrentModule
,
539 Intrinsic::x86_avx2_gather_d_ps_256
);
541 else if (srcTy
== B
->mInt32Ty
)
543 pX86IntrinFunc
= Intrinsic::getDeclaration(B
->JM()->mpCurrentModule
,
544 Intrinsic::x86_avx2_gather_d_d_256
);
546 else if (srcTy
== B
->mDoubleTy
)
548 pX86IntrinFunc
= Intrinsic::getDeclaration(B
->JM()->mpCurrentModule
,
549 Intrinsic::x86_avx2_gather_d_q_256
);
553 SWR_ASSERT(false, "Unsupported vector element type for gather.");
558 auto v32Mask
= B
->BITCAST(pThis
->VectorMask(vi1Mask
), vSrc
->getType());
559 v32Gather
= B
->CALL(pX86IntrinFunc
, {vSrc
, pBase
, vi32Indices
, v32Mask
, i8Scale
});
561 else if (width
== W512
)
563 // Double pump 4-wide for 64bit elements
564 if (vSrc
->getType()->getVectorElementType() == B
->mDoubleTy
)
566 auto v64Mask
= pThis
->VectorMask(vi1Mask
);
569 VectorType::get(B
->mInt64Ty
, v64Mask
->getType()->getVectorNumElements()));
570 v64Mask
= B
->BITCAST(v64Mask
, vSrc
->getType());
572 Value
* src0
= B
->VSHUFFLE(vSrc
, vSrc
, B
->C({0, 1, 2, 3}));
573 Value
* src1
= B
->VSHUFFLE(vSrc
, vSrc
, B
->C({4, 5, 6, 7}));
575 Value
* indices0
= B
->VSHUFFLE(vi32Indices
, vi32Indices
, B
->C({0, 1, 2, 3}));
576 Value
* indices1
= B
->VSHUFFLE(vi32Indices
, vi32Indices
, B
->C({4, 5, 6, 7}));
578 Value
* mask0
= B
->VSHUFFLE(v64Mask
, v64Mask
, B
->C({0, 1, 2, 3}));
579 Value
* mask1
= B
->VSHUFFLE(v64Mask
, v64Mask
, B
->C({4, 5, 6, 7}));
583 VectorType::get(B
->mInt64Ty
, src0
->getType()->getVectorNumElements()));
586 VectorType::get(B
->mInt64Ty
, mask0
->getType()->getVectorNumElements()));
588 B
->CALL(pX86IntrinFunc
, {src0
, pBase
, indices0
, mask0
, i8Scale
});
591 VectorType::get(B
->mInt64Ty
, src1
->getType()->getVectorNumElements()));
594 VectorType::get(B
->mInt64Ty
, mask1
->getType()->getVectorNumElements()));
596 B
->CALL(pX86IntrinFunc
, {src1
, pBase
, indices1
, mask1
, i8Scale
});
598 v32Gather
= B
->VSHUFFLE(gather0
, gather1
, B
->C({0, 1, 2, 3, 4, 5, 6, 7}));
599 v32Gather
= B
->BITCAST(v32Gather
, vSrc
->getType());
603 // Double pump 8-wide for 32bit elements
604 auto v32Mask
= pThis
->VectorMask(vi1Mask
);
605 v32Mask
= B
->BITCAST(v32Mask
, vSrc
->getType());
606 Value
* src0
= B
->EXTRACT_16(vSrc
, 0);
607 Value
* src1
= B
->EXTRACT_16(vSrc
, 1);
609 Value
* indices0
= B
->EXTRACT_16(vi32Indices
, 0);
610 Value
* indices1
= B
->EXTRACT_16(vi32Indices
, 1);
612 Value
* mask0
= B
->EXTRACT_16(v32Mask
, 0);
613 Value
* mask1
= B
->EXTRACT_16(v32Mask
, 1);
616 B
->CALL(pX86IntrinFunc
, {src0
, pBase
, indices0
, mask0
, i8Scale
});
618 B
->CALL(pX86IntrinFunc
, {src1
, pBase
, indices1
, mask1
, i8Scale
});
620 v32Gather
= B
->JOIN_16(gather0
, gather1
);
624 else if (arch
== AVX512
)
627 Function
* pX86IntrinFunc
;
628 if (srcTy
== B
->mFP32Ty
)
630 pX86IntrinFunc
= Intrinsic::getDeclaration(B
->JM()->mpCurrentModule
,
631 Intrinsic::x86_avx512_gather_dps_512
);
632 iMask
= B
->BITCAST(vi1Mask
, B
->mInt16Ty
);
634 else if (srcTy
== B
->mInt32Ty
)
636 pX86IntrinFunc
= Intrinsic::getDeclaration(B
->JM()->mpCurrentModule
,
637 Intrinsic::x86_avx512_gather_dpi_512
);
638 iMask
= B
->BITCAST(vi1Mask
, B
->mInt16Ty
);
640 else if (srcTy
== B
->mDoubleTy
)
642 pX86IntrinFunc
= Intrinsic::getDeclaration(B
->JM()->mpCurrentModule
,
643 Intrinsic::x86_avx512_gather_dpd_512
);
644 iMask
= B
->BITCAST(vi1Mask
, B
->mInt8Ty
);
648 SWR_ASSERT(false, "Unsupported vector element type for gather.");
651 auto i32Scale
= B
->Z_EXT(i8Scale
, B
->mInt32Ty
);
652 v32Gather
= B
->CALL(pX86IntrinFunc
, {vSrc
, pBase
, vi32Indices
, iMask
, i32Scale
});
655 return cast
<Instruction
>(v32Gather
);
658 VSCATTER_EMU(LowerX86
* pThis
, TargetArch arch
, TargetWidth width
, CallInst
* pCallInst
)
660 Builder
* B
= pThis
->B
;
661 auto pBase
= pCallInst
->getArgOperand(0);
662 auto vi1Mask
= pCallInst
->getArgOperand(1);
663 auto vi32Indices
= pCallInst
->getArgOperand(2);
664 auto v32Src
= pCallInst
->getArgOperand(3);
665 auto i32Scale
= pCallInst
->getArgOperand(4);
669 // Call into C function to do the scatter. This has significantly better compile perf
670 // compared to jitting scatter loops for every scatter
673 auto mask
= B
->BITCAST(vi1Mask
, B
->mInt8Ty
);
674 B
->CALL(pThis
->mPfnScatter256
, {pBase
, vi32Indices
, v32Src
, mask
, i32Scale
});
678 // Need to break up 512 wide scatter to two 256 wide
679 auto maskLo
= B
->VSHUFFLE(vi1Mask
, vi1Mask
, B
->C({0, 1, 2, 3, 4, 5, 6, 7}));
681 B
->VSHUFFLE(vi32Indices
, vi32Indices
, B
->C({0, 1, 2, 3, 4, 5, 6, 7}));
682 auto srcLo
= B
->VSHUFFLE(v32Src
, v32Src
, B
->C({0, 1, 2, 3, 4, 5, 6, 7}));
684 auto mask
= B
->BITCAST(maskLo
, B
->mInt8Ty
);
685 B
->CALL(pThis
->mPfnScatter256
, {pBase
, indicesLo
, srcLo
, mask
, i32Scale
});
687 auto maskHi
= B
->VSHUFFLE(vi1Mask
, vi1Mask
, B
->C({8, 9, 10, 11, 12, 13, 14, 15}));
689 B
->VSHUFFLE(vi32Indices
, vi32Indices
, B
->C({8, 9, 10, 11, 12, 13, 14, 15}));
690 auto srcHi
= B
->VSHUFFLE(v32Src
, v32Src
, B
->C({8, 9, 10, 11, 12, 13, 14, 15}));
692 mask
= B
->BITCAST(maskHi
, B
->mInt8Ty
);
693 B
->CALL(pThis
->mPfnScatter256
, {pBase
, indicesHi
, srcHi
, mask
, i32Scale
});
699 Function
* pX86IntrinFunc
;
702 // No direct intrinsic supported in llvm to scatter 8 elem with 32bit indices, but we
703 // can use the scatter of 8 elements with 64bit indices
704 pX86IntrinFunc
= Intrinsic::getDeclaration(B
->JM()->mpCurrentModule
,
705 Intrinsic::x86_avx512_scatter_qps_512
);
707 auto vi32IndicesExt
= B
->Z_EXT(vi32Indices
, B
->mSimdInt64Ty
);
708 iMask
= B
->BITCAST(vi1Mask
, B
->mInt8Ty
);
709 B
->CALL(pX86IntrinFunc
, {pBase
, iMask
, vi32IndicesExt
, v32Src
, i32Scale
});
711 else if (width
== W512
)
713 pX86IntrinFunc
= Intrinsic::getDeclaration(B
->JM()->mpCurrentModule
,
714 Intrinsic::x86_avx512_scatter_dps_512
);
715 iMask
= B
->BITCAST(vi1Mask
, B
->mInt16Ty
);
716 B
->CALL(pX86IntrinFunc
, {pBase
, iMask
, vi32Indices
, v32Src
, i32Scale
});
721 // No support for vroundps in avx512 (it is available in kncni), so emulate with avx
724 VROUND_EMU(LowerX86
* pThis
, TargetArch arch
, TargetWidth width
, CallInst
* pCallInst
)
726 SWR_ASSERT(arch
== AVX512
);
729 auto vf32Src
= pCallInst
->getOperand(0);
730 auto i8Round
= pCallInst
->getOperand(1);
732 Intrinsic::getDeclaration(B
->JM()->mpCurrentModule
, Intrinsic::x86_avx_round_ps_256
);
736 return cast
<Instruction
>(B
->CALL2(pfnFunc
, vf32Src
, i8Round
));
738 else if (width
== W512
)
740 auto v8f32SrcLo
= B
->EXTRACT_16(vf32Src
, 0);
741 auto v8f32SrcHi
= B
->EXTRACT_16(vf32Src
, 1);
743 auto v8f32ResLo
= B
->CALL2(pfnFunc
, v8f32SrcLo
, i8Round
);
744 auto v8f32ResHi
= B
->CALL2(pfnFunc
, v8f32SrcHi
, i8Round
);
746 return cast
<Instruction
>(B
->JOIN_16(v8f32ResLo
, v8f32ResHi
));
750 SWR_ASSERT(false, "Unimplemented vector width.");
757 VCONVERT_EMU(LowerX86
* pThis
, TargetArch arch
, TargetWidth width
, CallInst
* pCallInst
)
759 SWR_ASSERT(arch
== AVX512
);
762 auto vf32Src
= pCallInst
->getOperand(0);
766 auto vf32SrcRound
= Intrinsic::getDeclaration(B
->JM()->mpCurrentModule
,
767 Intrinsic::x86_avx_round_ps_256
);
768 return cast
<Instruction
>(B
->FP_TRUNC(vf32SrcRound
, B
->mFP32Ty
));
770 else if (width
== W512
)
772 // 512 can use intrinsic
773 auto pfnFunc
= Intrinsic::getDeclaration(B
->JM()->mpCurrentModule
,
774 Intrinsic::x86_avx512_mask_cvtpd2ps_512
);
775 return cast
<Instruction
>(B
->CALL(pfnFunc
, vf32Src
));
779 SWR_ASSERT(false, "Unimplemented vector width.");
785 // No support for hsub in AVX512
786 Instruction
* VHSUB_EMU(LowerX86
* pThis
, TargetArch arch
, TargetWidth width
, CallInst
* pCallInst
)
788 SWR_ASSERT(arch
== AVX512
);
791 auto src0
= pCallInst
->getOperand(0);
792 auto src1
= pCallInst
->getOperand(1);
794 // 256b hsub can just use avx intrinsic
797 auto pX86IntrinFunc
=
798 Intrinsic::getDeclaration(B
->JM()->mpCurrentModule
, Intrinsic::x86_avx_hsub_ps_256
);
799 return cast
<Instruction
>(B
->CALL2(pX86IntrinFunc
, src0
, src1
));
801 else if (width
== W512
)
803 // 512b hsub can be accomplished with shuf/sub combo
804 auto minuend
= B
->VSHUFFLE(src0
, src1
, B
->C({0, 2, 8, 10, 4, 6, 12, 14}));
805 auto subtrahend
= B
->VSHUFFLE(src0
, src1
, B
->C({1, 3, 9, 11, 5, 7, 13, 15}));
806 return cast
<Instruction
>(B
->SUB(minuend
, subtrahend
));
810 SWR_ASSERT(false, "Unimplemented vector width.");
815 // Double pump input using Intrin template arg. This blindly extracts lower and upper 256 from
816 // each vector argument and calls the 256 wide intrinsic, then merges the results to 512 wide
817 Instruction
* DOUBLE_EMU(LowerX86
* pThis
,
821 Intrinsic::ID intrin
)
824 SWR_ASSERT(width
== W512
);
826 Function
* pX86IntrinFunc
= Intrinsic::getDeclaration(B
->JM()->mpCurrentModule
, intrin
);
827 for (uint32_t i
= 0; i
< 2; ++i
)
829 SmallVector
<Value
*, 8> args
;
830 for (auto& arg
: pCallInst
->arg_operands())
832 auto argType
= arg
.get()->getType();
833 if (argType
->isVectorTy())
835 uint32_t vecWidth
= argType
->getVectorNumElements();
836 Value
* lanes
= B
->CInc
<int>(i
* vecWidth
/ 2, vecWidth
/ 2);
837 Value
* argToPush
= B
->VSHUFFLE(
838 arg
.get(), B
->VUNDEF(argType
->getVectorElementType(), vecWidth
), lanes
);
839 args
.push_back(argToPush
);
843 args
.push_back(arg
.get());
846 result
[i
] = B
->CALLA(pX86IntrinFunc
, args
);
849 if (result
[0]->getType()->isVectorTy())
851 assert(result
[1]->getType()->isVectorTy());
852 vecWidth
= result
[0]->getType()->getVectorNumElements() +
853 result
[1]->getType()->getVectorNumElements();
859 Value
* lanes
= B
->CInc
<int>(0, vecWidth
);
860 return cast
<Instruction
>(B
->VSHUFFLE(result
[0], result
[1], lanes
));
863 } // namespace SwrJit
865 using namespace SwrJit
;
867 INITIALIZE_PASS_BEGIN(LowerX86
, "LowerX86", "LowerX86", false, false)
868 INITIALIZE_PASS_END(LowerX86
, "LowerX86", "LowerX86", false, false)