[clang] [llvm] Add length HLSL function to DirectX Backend (PR #101256)
Farzon Lotfi via llvm-commits
llvm-commits at lists.llvm.org
Tue Jul 30 15:37:25 PDT 2024
================
@@ -18460,6 +18460,22 @@ Value *CodeGenFunction::EmitHLSLBuiltinExpr(unsigned BuiltinID,
/*ReturnType=*/X->getType(), CGM.getHLSLRuntime().getLerpIntrinsic(),
ArrayRef<Value *>{X, Y, S}, nullptr, "hlsl.lerp");
}
+ case Builtin::BI__builtin_hlsl_elementwise_length: {
+ Value *X = EmitScalarExpr(E->getArg(0));
+
+ if (!E->getArg(0)->getType()->hasFloatingRepresentation())
+ llvm_unreachable("length operand must have a float representation");
+ // if the operand is a scalar, we can use the fabs llvm intrinsic directly
+ if (!E->getArg(0)->getType()->isVectorType()) {
+ llvm::Type *ResultType = ConvertType(E->getType());
+ Function *F = CGM.getIntrinsic(Intrinsic::fabs, ResultType);
+ return Builder.CreateCall(F, X);
+ }
+ return Builder.CreateIntrinsic(
+ /*ReturnType=*/X->getType()->getScalarType(),
+ CGM.getHLSLRuntime().getLengthIntrinsic(),
----------------
farzonl wrote:
This looks right, but did you notice any behavioral differences because the intrinsics were not defined the same? Just curious? Maybe the issue i'm worried about would become more clear when you start the SPIRV backend changes.
https://github.com/llvm/llvm-project/pull/101256
More information about the llvm-commits
mailing list