-
Notifications
You must be signed in to change notification settings - Fork 13k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement the length
HLSL Function
#99134
Comments
For the Expectations for the implementation would be to see an extractelement per scalar in the vector an fmul per scalar to get https://hlsl.godbolt.org/z/5EfE6Yno8 define float @"fn"(<4 x float> %p1) #0 {
call void @llvm.dbg.value(metadata <4 x float> %p1, i64 0, metadata !35, metadata !36), !dbg !37
%1 = extractelement <4 x float> %p1, i64 0, !dbg !38
%2 = fmul fast float %1, %1, !dbg !38
%3 = extractelement <4 x float> %p1, i64 1, !dbg !38
%4 = fmul fast float %3, %3, !dbg !38
%5 = fadd fast float %2, %4, !dbg !38
%6 = extractelement <4 x float> %p1, i64 2, !dbg !38
%7 = fmul fast float %6, %6, !dbg !38
%8 = fadd fast float %5, %7, !dbg !38
%9 = extractelement <4 x float> %p1, i64 3, !dbg !38
%10 = fmul fast float %9, %9, !dbg !38
%11 = fadd fast float %8, %10, !dbg !38
%Sqrt = call float @dx.op.unary.f32(i32 24, float %11), !dbg !38
%12 = load i32, i32* getelementptr inbounds ([1 x i32], [1 x i32]* @dx.nothing.a, i32 0, i32 0), !dbg !39
ret float %Sqrt, !dbg !39
} if it is not a vector input the behavior is to emit a DXIL abs call define float @"fn"(float %p1) #0 {
call void @llvm.dbg.value(metadata float %p1, i64 0, metadata !23, metadata !24), !dbg !25
%FAbs = call float @dx.op.unary.f32(i32 6, float %p1), !dbg !26
%1 = load i32, i32* getelementptr inbounds ([1 x i32], [1 x i32]* @dx.nothing.a, i32 0, i32 0), !dbg !27
ret float %FAbs, !dbg !27
} |
) This PR adds the length intrinsic and an HLSL function that uses it. The SPIRV implementation is left for a future PR. This PR addresses #99134, though some SPIR-V changes still need to be made to complete the task. Below is how this PR addresses #99134. - "Implement `length` clang builtin" was done by defining `HLSLL ength` in Builtins.td - "Link `length` clang builtin with hlsl_intrinsics.h" was done by using the alias attribute to make `length` an alias of `__builtin_hlsl_elementwise_length` in hlsl_intrinsics.h - "Add sema checks for `length` to `CheckHLSLBuiltinFunctionCall` in `SemaChecking.cpp` " was done, but in this case not in SemaChecking.cpp, rather SemaHLSL.cpp. A case was added to the builtin to check for semantic failures, and set `TheCall` up to have the right return type. - "Add codegen for `length` to `EmitHLSLBuiltinExpr` in `CGBuiltin.cpp`" was done. For scalars, fabs is emitted, otherwise, length is emitted. - "Add codegen tests to `clang/test/CodeGenHLSL/builtins/length.hlsl` was done to test that `length` in HLSL emits the right intrinsic. - "Add sema tests to `clang/test/SemaHLSL/BuiltIns/length-errors.hlsl`" was done to test for diagnostics emitted in SemaHLSL.cpp - "Create the `int_dx_length` intrinsic in `IntrinsicsDirectX.td`" was done. Specifying return types and parameter types was difficult, but `idot` was used for reference, and `llvm\include\llvm\IR\Intrinsics.td` contains all the ways to express return / parameter types. - "Create an intrinsic expansion of `int_dx_length` in `llvm/lib/Target/DirectX/DXILIntrinsicExpansion.cpp`" was done, and was mostly derived by looking at `TranslateLength` in `HLOperationLower.cpp` in the DXC codebase. - "Create the `length.ll` and `length_errors.ll` tests in `llvm/test/CodeGen/DirectX/`" was done by taking the DXIL output of `clang/test/CodeGenHLSL/builtins/length.hlsl` and running `opt -S -dxil-intrinsic-expansion` and ` opt -S -dxil-op-lower` on it, checking for how the length intrinsic was either expanded or lowered. - "Create the `int_spv_length` intrinsic in `IntrinsicsSPIRV.td`" was done by copying `IntrinsicsDirectX.td`. --------- Co-authored-by: Justin Bogner <[email protected]>
length
clang builtin,length
clang builtin withhlsl_intrinsics.h
length
toCheckHLSLBuiltinFunctionCall
inSemaChecking.cpp
#101096length
toEmitHLSLBuiltinExpr
inCGBuiltin.cpp
clang/test/CodeGenHLSL/builtins/length.hlsl
clang/test/SemaHLSL/BuiltIns/length-errors.hlsl
int_dx_length
intrinsic inIntrinsicsDirectX.td
int_dx_length
inllvm/lib/Target/DirectX/DXILIntrinsicExpansion.cpp
length.ll
andlength_errors.ll
tests inllvm/test/CodeGen/DirectX/
int_spv_length
intrinsic inIntrinsicsSPIRV.td
length
lowering and map it toint_spv_length
inSPIRVInstructionSelector::selectIntrinsic
.llvm/test/CodeGen/SPIRV/hlsl-intrinsics/length.ll
DirectX
SPIR-V
Length:
Description:
Length
Result is the length of vector x, i.e., sqrt(x [0] 2 +
x [1] 2 + …).
The operand x must be a scalar or vector whose component type is
floating-point.
Result Type must be a scalar of the same type as the component type of
x.
66
<id>
x
Test Case(s)
Example 1
HLSL:
Returns the length of the specified floating-point vector.
Parameters
Return Value
A floating-point scalar that represents the length of the x parameter.
Type Description
Minimum Shader Model
This function is supported in the following shader models.
See also
Intrinsic Functions (DirectX HLSL)
The text was updated successfully, but these errors were encountered: