[clang] [llvm] Adding splitdouble HLSL function (PR #109331)
Chris B via llvm-commits
llvm-commits at lists.llvm.org
Tue Oct 8 07:38:30 PDT 2024
================
@@ -18901,6 +18901,150 @@ case Builtin::BI__builtin_hlsl_elementwise_isinf: {
CGM.getHLSLRuntime().getRadiansIntrinsic(), ArrayRef<Value *>{Op0},
nullptr, "hlsl.radians");
}
+ // This should only be called when targeting DXIL
+ case Builtin::BI__builtin_hlsl_splitdouble: {
+
+ assert((E->getArg(0)->getType()->hasFloatingRepresentation() &&
+ E->getArg(1)->getType()->hasUnsignedIntegerRepresentation() &&
+ E->getArg(2)->getType()->hasUnsignedIntegerRepresentation()) &&
+ "asuint operands types mismatch");
+ Value *Op0 = EmitScalarExpr(E->getArg(0));
+ const HLSLOutArgExpr *OutArg1 = dyn_cast<HLSLOutArgExpr>(E->getArg(1));
+ const HLSLOutArgExpr *OutArg2 = dyn_cast<HLSLOutArgExpr>(E->getArg(2));
+
+ CallArgList Args;
+ auto [Op1BaseLValue, Op1TmpLValue] =
+ EmitHLSLOutArgExpr(OutArg1, Args, OutArg1->getType());
+ auto [Op2BaseLValue, Op2TmpLValue] =
+ EmitHLSLOutArgExpr(OutArg2, Args, OutArg2->getType());
+
+ if (CGM.getTarget().getTriple().getArch() == llvm::Triple::dxil) {
+
+ llvm::StructType *retType = llvm::StructType::get(Int32Ty, Int32Ty);
+
+ if (Op0->getType()->isVectorTy()) {
+ auto *Op0VecTy = E->getArg(0)->getType()->getAs<VectorType>();
+
+ llvm::VectorType *i32VecTy = llvm::VectorType::get(
+ Int32Ty, ElementCount::getFixed(Op0VecTy->getNumElements()));
+ retType = llvm::StructType::get(i32VecTy, i32VecTy);
+ }
+
+ CallInst *CI =
+ Builder.CreateIntrinsic(retType, Intrinsic::dx_splitdouble, {Op0},
+ nullptr, "hlsl.splitdouble");
+
+ Value *arg0 = Builder.CreateExtractValue(CI, 0);
+ Value *arg1 = Builder.CreateExtractValue(CI, 1);
+
+ Builder.CreateStore(arg0, Op1TmpLValue.getAddress());
+ auto *s = Builder.CreateStore(arg1, Op2TmpLValue.getAddress());
+
+ EmitWritebacks(*this, Args);
+ return s;
+ }
+
+ if (!Op0->getType()->isVectorTy()) {
+ FixedVectorType *destTy = FixedVectorType::get(Int32Ty, 2);
+ Value *bitcast = Builder.CreateBitCast(Op0, destTy);
+
+ Value *arg0 = Builder.CreateExtractElement(bitcast, 0.0);
+ Value *arg1 = Builder.CreateExtractElement(bitcast, 1.0);
+
+ Builder.CreateStore(arg0, Op1TmpLValue.getAddress());
+ auto *s = Builder.CreateStore(arg1, Op2TmpLValue.getAddress());
+
+ EmitWritebacks(*this, Args);
+ return s;
+ }
+
+ auto emitVectorCode =
+ [](Value *Op, CGBuilderTy *Builder,
+ FixedVectorType *DestTy) -> std::pair<Value *, Value *> {
+ Value *bitcast = Builder->CreateBitCast(Op, DestTy);
+
+ SmallVector<int> lowbitsIndex;
+ SmallVector<int> highbitsIndex;
+
+ for (unsigned int idx = 0; idx < DestTy->getNumElements(); idx += 2) {
+ lowbitsIndex.push_back(idx);
+ highbitsIndex.push_back(idx + 1);
+ }
+
+ Value *arg0 = Builder->CreateShuffleVector(bitcast, lowbitsIndex);
+ Value *arg1 = Builder->CreateShuffleVector(bitcast, highbitsIndex);
+
+ return std::make_pair(arg0, arg1);
+ };
+
+ const VectorType *targTy = E->getArg(0)->getType()->getAs<VectorType>();
+
+ int numElements = targTy->getNumElements();
+
+ switch (numElements) {
+ case 2: {
+
+ FixedVectorType *destTy = FixedVectorType::get(Int32Ty, 4);
+
+ auto vec2res = emitVectorCode(Op0, &Builder, destTy);
----------------
llvm-beanz wrote:
This is a case where `auto` is discouraged because the type is not obvious.
https://github.com/llvm/llvm-project/pull/109331
More information about the llvm-commits
mailing list