microsoft / microsoft/DirectXShaderCompiler

Bitfield initialization unclear

Open
#5,587 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug hlsl2021
Dominant language
C++
Stars
3.7k
Forks
900
Avg merge
2d 11h
Merged PRs (30d)
44

Description

Description
It is unclear to me how to initialize a bitfield to all 0 in HLSL. It appears a simple syntax works in some cases, but not others.

Steps to Reproduce
test.hlsl file contains:

RWStructuredBuffer<uint> g_Buffer : register(u1);

enum SomeEnum
{
    SomeEnum_val0,
    SomeEnum_val1,
    SomeEnum_val2,
    SomeEnum_val3
};

struct SomeBitfield
{
    SomeEnum field1 : 2;
    uint32_t rest : 30;
};

[RootSignature("UAV(u1)")]
[numthreads(1, 1, 1)]
void main(uint3 DTid : SV_DispatchThreadID)
{
    SomeBitfield val = (SomeBitfield)0;
    g_Buffer[0] = (uint)val;
}

Compile with:

dxc -T cs_6_6 -HV 2021 -E main test.hlsl

Actual Behavior
You get this output:

test.hlsl:21:24: error: cannot convert from 'literal int' to 'SomeBitfield'
    SomeBitfield val = (SomeBitfield)0;

However, if I switch the order of the field1 and rest members in SomeBitfield, it seemingly works and the generated code looks correct. Is the SomeBitfield val = (SomeBitfield)0 syntax meant to be valid? If yes, it doesn't seem to work reliably. If no, how to I easily (and most efficiently) initialize all members of a bitfield to 0?

Environment

  • DXC version: dxcompiler_xs.dll!DxcCreateInstance: 1.7 - 2310.2307.12501.10025
  • Host Operating System: Windows 11 Version 22H2 (OS Build 22621.2134)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the test.hlsl reproduction and run the documented dxc command using the shown member order, then compare it with the reordered bitfield case and generated output. Done means determining whether the cast-based initialization is valid and whether its behavior is consistent, with the expected behavior captured for regression coverage.

Written by the indexing model from the issue text.

Assessment

Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.