microsoft / DirectXShaderCompiler

This repo hosts the source for the DirectX Shader Compiler which is based on LLVM/Clang.
Other
3.05k stars 677 forks source link

`out` parameter diagnostics affect uninitialized variables at the _call site_ #5093

Closed MarijnS95 closed 1 year ago

MarijnS95 commented 1 year ago

A recent PR to prevent leaving out parameters uninitialized in function calls (https://github.com/microsoft/DirectXShaderCompiler/pull/5047) seems to have escaped its cage and is now also disallowing uninitialized arguments into functions with out paramameters. See for example this (IMO totally valid) Interlocked* invocation:

        uint original;
        buffer.InterlockedAdd(offset, value, original);
        return original;
bindless.hlsl:110:46: error: variable 'original' is uninitialized when used here [-Werror,-Wuninitialized]
        buffer.InterlockedAdd(offset, value, original);
                                             ^~~~~~~~
bindless.hlsl:109:22: note: initialize the variable 'original' to silence this warning
        uint original;
                     ^
                      = 0

(Compiler from https://github.com/microsoft/DirectXShaderCompiler/commit/667fb773cc2fb40bc31dba6d53e5230a196838ff)

I thought the whole point of out parameters and by extension this diagnostic is to know and force that the function will initialize original here, and it should thus not need to be pre-initialized by the caller.

Originally posted by @MarijnS95 in https://github.com/microsoft/DirectXShaderCompiler/issues/5047#issuecomment-1462949361, cc @llvm-beanz

llvm-beanz commented 1 year ago

Just going to drop a full-source example here:

RWByteAddressBuffer buffer;

void interlockWrapper(out uint original) {
  buffer.InterlockedAdd(16, 1, original);
}
llvm-beanz commented 1 year ago

Something to also be aware of here. -Wuninitialized in DXC does cause spurious warnings around struct types too. We also have issue #2494, which is caused by our bad AST hygiene.