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 normalize HLSL Function #99139

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

Implement the normalize HLSL Function #99139

farzonl opened this issue Jul 16, 2024 · 1 comment · Fixed by #102683
Assignees
Labels
backend:DirectX backend:SPIR-V bot:HLSL clang:codegen IR generation bugs: mangling, exceptions, etc. 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 normalize clang builtin,
  • Link normalize clang builtin with hlsl_intrinsics.h
  • Add sema checks for normalize to CheckHLSLBuiltinFunctionCall in SemaChecking.cpp
  • Add codegen for normalize to EmitHLSLBuiltinExpr in CGBuiltin.cpp
  • Add codegen tests to clang/test/CodeGenHLSL/builtins/normalize.hlsl
  • Add sema tests to clang/test/SemaHLSL/BuiltIns/normalize-errors.hlsl
  • Create the int_dx_normalize intrinsic in IntrinsicsDirectX.td
  • Create an intrinsic expansion int_dx_normalize in llvm/lib/Target/DirectX/DXILIntrinsicExpansion.cpp
  • Create the normalize.ll and normalize_errors.ll tests in llvm/test/CodeGen/DirectX/
  • Create the int_spv_normalize intrinsic in IntrinsicsSPIRV.td
  • In SPIRVInstructionSelector.cpp create the normalize lowering and map it to int_spv_normalize in SPIRVInstructionSelector::selectIntrinsic.
  • Create SPIR-V backend test case in llvm/test/CodeGen/SPIRV/hlsl-intrinsics/normalize.ll

DirectX

DXIL Opcode DXIL OpName Shader Model Shader Stages
56 Dot4 6.0 ()

SPIR-V

Normalize:

Description:

Normalize

Result is the vector in the same direction as x but with a length of
1.

The operand x must be a scalar or vector whose component type is
floating-point.

Result Type and the type of x must be the same type.

Number Operand 1 Operand 2 Operand 3 Operand 4

69

<id>
x

Test Case(s)

Example 1

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

export float4 fn(float4 p1) {
    return normalize(p1);
}

HLSL:

Normalizes the specified floating-point vector according to x / length(x).

ret normalize(x)

Parameters

Item Description
x
[in] The specified floating-point vector.

Return Value

The normalized x parameter. If the length of the x parameter is 0, the result is indefinite.

Remarks

The normalize HLSL intrinsic function uses the following formula: x / length(x).

Type Description

Name Template Type Component Type Size
x vector float any
ret same as input x float same dimension(s) as input x

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 Aug 8, 2024
@damyanp damyanp moved this to Active in HLSL Support Aug 12, 2024
bob80905 added a commit that referenced this issue Aug 13, 2024
…R-V backend (#102683)

This PR adds the normalize intrinsic and an HLSL function that uses it.
The SPIRV backend is also implemented.

Used #101256 as a reference,
along with #102243
Fixes #99139
@github-project-automation github-project-automation bot moved this from Active to Done in HLSL Support Aug 13, 2024
@EugeneZelenko EugeneZelenko added the clang:codegen IR generation bugs: mangling, exceptions, etc. label Aug 13, 2024
@llvmbot
Copy link
Member

llvmbot commented Aug 13, 2024

@llvm/issue-subscribers-clang-codegen

Author: Farzon Lotfi (farzonl)

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

DirectX

DXIL Opcode DXIL OpName Shader Model Shader Stages
56 Dot4 6.0 ()

SPIR-V

Normalize:

Description:

Normalize

Result is the vector in the same direction as x but with a length of
1.

The operand x must be a scalar or vector whose component type is
floating-point.

Result Type and the type of x must be the same type.

<table>
<colgroup>
<col style="width: 20%" />
<col style="width: 20%" />
<col style="width: 20%" />
<col style="width: 20%" />
<col style="width: 20%" />
</colgroup>
<thead>
<tr>
<th>Number</th>
<th>Operand 1</th>
<th>Operand 2</th>
<th>Operand 3</th>
<th>Operand 4</th>
</tr>
</thead>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><p>69</p></td>
<td
class="tableblock halign-left valign-top"><p><em>&lt;id&gt;</em><br />
<em>x</em></p></td>
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>

Test Case(s)

Example 1

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

export float4 fn(float4 p1) {
    return normalize(p1);
}

HLSL:

Normalizes the specified floating-point vector according to x / length(x).

ret normalize(x)

Parameters

Item Description
<span id="x"></span><span id="X"></span>x<br/> [in] The specified floating-point vector.<br/>

Return Value

The normalized x parameter. If the length of the x parameter is 0, the result is indefinite.

Remarks

The normalize HLSL intrinsic function uses the following formula: x / length(x).

Type Description

Name Template Type Component Type Size
x vector float any
ret same as input x float same dimension(s) as input x

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

<dl> <dt>

Intrinsic Functions (DirectX HLSL)
</dt> </dl>

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 clang:codegen IR generation bugs: mangling, exceptions, etc. HLSL HLSL Language Support metabug Issue to collect references to a group of similar or related issues.
Projects
Archived in project
4 participants