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 InterlockedCompareExchange HLSL Function #99130

Open
12 tasks
Tracked by #99235
farzonl opened this issue Jul 16, 2024 · 0 comments
Open
12 tasks
Tracked by #99235

Implement the InterlockedCompareExchange HLSL Function #99130

farzonl opened this issue Jul 16, 2024 · 0 comments
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 InterlockedCompareExchange clang builtin,
  • Link InterlockedCompareExchange clang builtin with hlsl_intrinsics.h
  • Add sema checks for InterlockedCompareExchange to CheckHLSLBuiltinFunctionCall in SemaChecking.cpp
  • Add codegen for InterlockedCompareExchange to EmitHLSLBuiltinExpr in CGBuiltin.cpp
  • Add codegen tests to clang/test/CodeGenHLSL/builtins/InterlockedCompareExchange.hlsl
  • Add sema tests to clang/test/SemaHLSL/BuiltIns/InterlockedCompareExchange-errors.hlsl
  • Create the int_dx_InterlockedCompareExchange intrinsic in IntrinsicsDirectX.td
  • Create the DXILOpMapping of int_dx_InterlockedCompareExchange to 160 in DXIL.td
  • Create the InterlockedCompareExchange.ll and InterlockedCompareExchange_errors.ll tests in llvm/test/CodeGen/DirectX/
  • Create the int_spv_InterlockedCompareExchange intrinsic in IntrinsicsSPIRV.td
  • In SPIRVInstructionSelector.cpp create the InterlockedCompareExchange lowering and map it to int_spv_InterlockedCompareExchange in SPIRVInstructionSelector::selectIntrinsic.
  • Create SPIR-V backend test case in llvm/test/CodeGen/SPIRV/hlsl-intrinsics/InterlockedCompareExchange.ll

DirectX

DXIL Opcode DXIL OpName Shader Model Shader Stages
160 CreateHandleForLib 6.3 ()

SPIR-V

OpAtomicCompareExchange:

Description:

Perform the following steps atomically with respect to any other atomic
accesses within Scope to the same location:

  1. load through Pointer to get an Original Value,
  2. get a New Value from Value only if Original Value equals
    Comparator, and
  3. store the New Value back through Pointer only if Original Value
    equaled Comparator.

The instruction’s result is the Original Value.

Result Type must be an integer type scalar.

Use Equal for the memory semantics of this instruction when Value
and Original Value compare equal.

Use Unequal for the memory semantics of this instruction when Value
and Original Value compare unequal. Unequal must not be set to
Release or Acquire and Release. In addition, Unequal cannot be
set to a stronger memory-order then Equal.

The type of Value must be the same as Result Type. The type of the
value pointed to by Pointer must be the same as Result Type. This
type must also match the type of Comparator.

Memory is a memory Scope.

Word Count Opcode Results Operands

9

230

<id>
Result Type

Result <id>

<id>
Pointer

Scope <id>
Memory

Memory Semantics <id>
Equal

Memory Semantics <id>
Unequal

<id>
Value

<id>
Comparator

Test Case(s)

Example 1

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

RWStructuredBuffer<int64_t> buffer : register(u0);
[numthreads(1, 1, 1)]
export void fn(uint3 dispatchThreadID : SV_DispatchThreadID, int64_t p1, uint64_t p2, uint64_t p3) {
int index = dispatchThreadID.x;
    return InterlockedCompareExchange(buffer[index], p1, p2, p3);
}

Example 2

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

RWStructuredBuffer<int> buffer : register(u0);
[numthreads(1, 1, 1)]
export void fn(uint3 dispatchThreadID : SV_DispatchThreadID, int p1, uint p2, uint p3) {
int index = dispatchThreadID.x;
    return InterlockedCompareExchange(buffer[index], p1, p2, p3);
}

HLSL:

Atomically compares the destination with the comparison value. If they are identical, the destination is overwritten with the input value. The original value is set to the destination's original value.

Syntax

void InterlockedCompareExchange(
  in  R dest,
  in  T compare_value,
  in  T value,
  out T original_value
);

Parameters

dest [in]

Type: R

The destination address.

compare_value [in]

Type: T

The comparison value.

value [in]

Type: T

The input value.

original_value [out]

Type: T

The original value.

Return value

This function does not return a value.

Remarks

Atomically compares the value referenced by dest with compare_value, stores value in the location referenced by dest if the values match, returns the original value of dest in original_value. This operation can only be performed on int or uint typed resources and shared memory variables. There are two possible uses for this function. The first is when R is a shared memory variable type. In this case, the function performs the operation on the shared memory register referenced by dest. The second scenario is when R is a resource variable type. In this scenario, the function performs the operation on the resource location referenced by dest. This operation is only available when R is readable and writable.

Interlocked operations do not imply any memory fence/barrier.

Note

If you call InterlockedCompareExchange in a for or while compute shader loop, to properly compile, you must use the [allow_uav_condition] attribute on that loop.

 

Minimum Shader Model

This function is supported in the following shader models.

Shader Model Supported
Shader Model 5 and higher shader models yes

 

This function is supported in the following types of shaders:

Vertex Hull Domain Geometry Pixel Compute
x x x x x x

 

See also

Intrinsic Functions

Shader Model 5

@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
@damyanp damyanp moved this to Ready in HLSL Support Oct 30, 2024
@damyanp damyanp moved this from Ready to Planning in HLSL Support Oct 30, 2024
@farzonl farzonl moved this from Planning to Designing in HLSL Support Nov 19, 2024
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: Designing
Development

No branches or pull requests

1 participant