[clang] [HLSL] Rewrite inline HLSL intrinsics into TableGen (PR #188362)

Deric C. via cfe-commits cfe-commits at lists.llvm.org
Thu Mar 26 09:49:46 PDT 2026


================
@@ -313,8 +313,424 @@ class HLSLOneArgInlineBuiltin<string name> : HLSLBuiltin<name> {
 // Intrinsic definitions (sorted alphabetically by function name)
 //===----------------------------------------------------------------------===//
 
-// TODO: Convert hand-written overloads from hlsl_intrinsics.h and
-//       hlsl_alias_intrinsics.h into TableGen below.
-//       Include "hlsl_alias_intrinsics_gen.inc" in hlsl_alias_intrinsics.h
-//       Include "hlsl_inline_intrinsics_gen.inc" in hlsl_intrinsics.h
+// Unsigned abs is a constexpr identity — unsigned values are already non-negative.
+def hlsl_abs_unsigned : HLSLOneArgInlineBuiltin<"abs"> {
+  let Doc = [{
+\fn T abs(T Val)
+\brief Returns the absolute value of the input value, \a Val.
+\param Val The input value.
+
+Unsigned overload — unsigned values are already non-negative, so this
+function returns its input unchanged.
+}];
+  let ParamNames = ["V"];
+  let Body = "return V;";
+  let IsConstexpr = 1;
+  let VaryingTypes = UnsignedIntTypes;
+  let VaryingMatDims = [];
+}
+
+// Checks whether the value is fully mapped.
+def hlsl_check_access_fully_mapped : HLSLBuiltin<"CheckAccessFullyMapped"> {
+  let Doc = [{
+\fn bool CheckAccessFullyMapped(uint Status)
+\brief Checks whether the value is fully mapped.
+\param Status The status value to check.
+}];
+  let ParamNames = ["Status"];
+  let Body = "return static_cast<bool>(Status);";
+  let Args = [UIntTy];
+  let ReturnType = BoolTy;
+}
+
+// Converts a floating-point, 4D vector set by a D3DCOLOR to a UBYTE4.
+def hlsl_d3d_color_to_ubyte4 : HLSLBuiltin<"D3DCOLORtoUBYTE4"> {
+  let Doc = [{
+\fn int4 D3DCOLORtoUBYTE4(float4 x)
+\brief Converts a floating-point, 4D vector set by a D3DCOLOR to a UBYTE4.
+\param x [in] The floating-point vector4 to convert.
+
+The return value is the UBYTE4 representation of the \a x parameter.
+
+This function swizzles and scales components of the \a x parameter. Use this
+function to compensate for the lack of UBYTE4 support in some hardware.
+}];
+  // Use the same scaling factor used by FXC, and DXC for DXIL
+  // (i.e., 255.001953)
+  // https://github.com/microsoft/DirectXShaderCompiler/blob/070d0d5a2beacef9eeb51037a9b04665716fd6f3/lib/HLSL/HLOperationLower.cpp#L666C1-L697C2
+  // The DXC implementation refers to a comment on the following stackoverflow
+  // discussion to justify the scaling factor: "Built-in rounding, necessary
+  // because of truncation. 0.001953 * 256 = 0.5"
+  // https://stackoverflow.com/questions/52103720/why-does-d3dcolortoubyte4-multiplies-components-by-255-001953f
+  let Body = "return V.zyxw * 255.001953f;";
+  let ParamNames = ["V"];
+  let Args = [VectorType<FloatTy, 4>];
+  let ReturnType = VectorType<IntTy, 4>;
+  let IsConstexpr = 1;
+}
+
+// Computes the partial derivative with regard to the x screen space coordinate.
+def hlsl_ddx : HLSLBuiltin<"ddx"> {
+  let Doc = [{
+\fn T ddx(T x)
+\brief Computes the partial derivative of the specified value with regard to
+the screen-space x-coordinate.
+\param x [in] The floating-point scalar or vector to process.
+}];
+  let DetailFunc = "ddx_impl";
+  let ParamNames = ["input"];
+  let Args = [Varying];
+  let ReturnType = Varying;
+  let VaryingTypes = [HalfTy, FloatTy];
+  let VaryingScalar = 1;
+  let VaryingVecSizes = [2, 3, 4];
+  let VaryingMatDims = [];
+}
+
+// Computes the partial derivative with regard to the y screen space coordinate.
+def hlsl_ddy : HLSLBuiltin<"ddy"> {
+  let Doc = [{
+\fn T ddy(T x)
+\brief Computes the partial derivative of the specified value with regard to
+the screen-space y-coordinate.
+\param x [in] The floating-point scalar or vector to process.
+}];
+  let DetailFunc = "ddy_impl";
+  let ParamNames = ["input"];
+  let Args = [Varying];
+  let ReturnType = Varying;
+  let VaryingTypes = [HalfTy, FloatTy];
+  let VaryingScalar = 1;
+  let VaryingVecSizes = [2, 3, 4];
+  let VaryingMatDims = [];
+}
+
+// Returns a distance scalar between X and Y (scalar overloads).
+// Scalar distance is equivalent to abs(X - Y).
----------------
Icohedron wrote:

"Distance is length(X - Y) which is equivalent to abs(X - Y)" is a false statement though.

https://github.com/llvm/llvm-project/pull/188362


More information about the cfe-commits mailing list