Skip to content
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

Closed
12 tasks done
Tracked by #99235 ...
farzonl opened this issue Jul 16, 2024 · 1 comment · Fixed by #102243
Closed
12 tasks done
Tracked by #99235 ...

Implement the length HLSL Function #99134

farzonl opened this issue Jul 16, 2024 · 1 comment · Fixed by #102243
Assignees
Labels
backend:DirectX backend:SPIR-V bot:HLSL HLSL HLSL Language Support metabug Issue to collect references to a group of similar or related issues.

Comments

@farzonl
Copy link
Member

farzonl commented Jul 16, 2024

  • Implement length clang builtin,
  • Link length clang builtin with hlsl_intrinsics.h
  • Add sema checks for length to CheckHLSLBuiltinFunctionCall in SemaChecking.cpp #101096
  • Add codegen for length to EmitHLSLBuiltinExpr in CGBuiltin.cpp
  • Add codegen tests to clang/test/CodeGenHLSL/builtins/length.hlsl
  • Add sema tests to clang/test/SemaHLSL/BuiltIns/length-errors.hlsl
  • Create the int_dx_length intrinsic in IntrinsicsDirectX.td
  • Create an intrinsic expansion of int_dx_length in llvm/lib/Target/DirectX/DXILIntrinsicExpansion.cpp
  • Create the length.ll and length_errors.ll tests in llvm/test/CodeGen/DirectX/
  • Create the int_spv_length intrinsic in IntrinsicsSPIRV.td
  • In SPIRVInstructionSelector.cpp create the length lowering and map it to int_spv_length in SPIRVInstructionSelector::selectIntrinsic.
  • Create SPIR-V backend test case in llvm/test/CodeGen/SPIRV/hlsl-intrinsics/length.ll

DirectX

DXIL Opcode DXIL OpName Shader Model Shader Stages
24 Sqrt 6.0 ()

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.

Number Operand 1 Operand 2 Operand 3 Operand 4

66

<id>
x

Test Case(s)

Example 1

//dxc length_test.hlsl -T lib_6_8 -enable-16bit-types -O0

export float fn(float4 p1) {
    return length(p1);
}

HLSL:

Returns the length of the specified floating-point vector.

ret length(x)

Parameters

Item Description
x
The specified floating-point vector.

Return Value

A floating-point scalar that represents the length of the x parameter.

Type Description

Name Template Type Component Type Size
x vector float any
ret scalar float 1

Minimum Shader Model

This function is supported in the following shader models.

Shader Model Supported
Shader Model 2 (DirectX HLSL) and higher shader models yes
Shader Model 1 (DirectX HLSL) yes (vs_1_1 only)

See also

Intrinsic Functions (DirectX HLSL)

@farzonl farzonl added backend:DirectX backend:SPIR-V bot:HLSL HLSL HLSL Language Support metabug Issue to collect references to a group of similar or related issues. labels Jul 16, 2024
@bob80905 bob80905 self-assigned this Jul 25, 2024
@farzonl
Copy link
Member Author

farzonl commented Jul 29, 2024

For the DXILIntrinsicExpansion.cpp
see IntrinsicOp::IOP_length TranslateLength implementation:
https://github.com/microsoft/DirectXShaderCompiler/blob/b6f7ff81e9cea7b82a6412b6d2521dfdacc09517/lib/HLSL/HLOperationLower.cpp#L2154-L2180

Expectations for the implementation would be to see an extractelement per scalar in the vector an fmul per scalar to get V[i]^2 LEN(V)-1 fadd(s) for the sumation. and finally one dxil sqrt.

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
https://hlsl.godbolt.org/z/eM7Gs38fP

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
}

bob80905 added a commit that referenced this issue Aug 3, 2024
)

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 <mail@justinbogner.com>
banach-space pushed a commit to banach-space/llvm-project that referenced this issue Aug 7, 2024
…#101256)

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 llvm#99134, though some SPIR-V changes still need to be
made to complete the task. Below is how this PR addresses llvm#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 <mail@justinbogner.com>
bwendling pushed a commit to bwendling/llvm-project that referenced this issue Aug 15, 2024
This PR finishes llvm#99134 by lowering the length function to the SPIR-V
backend. A test was added to verify that the generated SPIR-V is
correct.
Fixes llvm#99134
kstoimenov pushed a commit to kstoimenov/llvm-project that referenced this issue Aug 15, 2024
…#101256)

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 llvm#99134, though some SPIR-V changes still need to be
made to complete the task. Below is how this PR addresses llvm#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 <mail@justinbogner.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend:DirectX backend:SPIR-V bot:HLSL HLSL HLSL Language Support metabug Issue to collect references to a group of similar or related issues.
Projects
Status: No status
Development

Successfully merging a pull request may close this issue.

4 participants