Skip to content

Implement the InterlockedAnd HLSL Function #99125

Open
@farzonl

Description

@farzonl
  • Implement InterlockedAnd clang builtin,
  • Link InterlockedAnd clang builtin with hlsl_intrinsics.h
  • Add sema checks for InterlockedAnd to CheckHLSLBuiltinFunctionCall in SemaChecking.cpp
  • Add codegen for InterlockedAnd to EmitHLSLBuiltinExpr in CGBuiltin.cpp
  • Add codegen tests to clang/test/CodeGenHLSL/builtins/InterlockedAnd.hlsl
  • Add sema tests to clang/test/SemaHLSL/BuiltIns/InterlockedAnd-errors.hlsl
  • Create the int_dx_InterlockedAnd intrinsic in IntrinsicsDirectX.td
  • Create the DXILOpMapping of int_dx_InterlockedAnd to 216 in DXIL.td
  • Create the InterlockedAnd.ll and InterlockedAnd_errors.ll tests in llvm/test/CodeGen/DirectX/
  • Create the int_spv_InterlockedAnd intrinsic in IntrinsicsSPIRV.td
  • In SPIRVInstructionSelector.cpp create the InterlockedAnd lowering and map it to int_spv_InterlockedAnd in SPIRVInstructionSelector::selectIntrinsic.
  • Create SPIR-V backend test case in llvm/test/CodeGen/SPIRV/hlsl-intrinsics/InterlockedAnd.ll

DirectX

DXIL Opcode DXIL OpName Shader Model Shader Stages
216 AnnotateHandle 6.6 ()

SPIR-V

OpAtomicAnd:

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 by the bitwise AND of Original Value and Value,
    and
  3. store the New Value back through Pointer.

The instruction’s result is the Original Value.

Result Type must be an integer type scalar.

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.

Memory is a memory Scope.

Word Count Opcode Results Operands

7

240

<id>
Result Type

Result <id>

<id>
Pointer

Scope <id>
Memory

Memory Semantics <id>
Semantics

<id>
Value

Test Case(s)

Example 1

//dxc InterlockedAnd_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) {
int index = dispatchThreadID.x;
    return InterlockedAnd(buffer[index], p1);
}

Example 2

//dxc InterlockedAnd_1_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) {
int index = dispatchThreadID.x;
    return InterlockedAnd(buffer[index], p1, p2);
}

Example 3

//dxc InterlockedAnd_2_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) {
int index = dispatchThreadID.x;
    return InterlockedAnd(buffer[index], p1);
}

Example 4

//dxc InterlockedAnd_3_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) {
int index = dispatchThreadID.x;
    return InterlockedAnd(buffer[index], p1, p2);
}

HLSL:

Performs a guaranteed atomic and.

Syntax

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

Parameters

dest [in]

Type: R

The destination address.

value [in]

Type: T

The input value.

original_value [out]

Type: T

Optional. The original input value.

Return value

This function does not return a value.

Remarks

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 an atomic and of value to the shared memory register referenced by dest. The second scenario is when R is a resource variable type. In this scenario, the function performs an atomic and of value to the resource location referenced by dest. The overloaded function has an additional output variable which will be set to the original value of dest. This overloaded operation is only available when R is readable and writable.

Interlocked operations do not imply any memory fence/barrier.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions