slang icon indicating copy to clipboard operation
slang copied to clipboard

`and` intrinsic does not support vector arguments

Open chaoticbob opened this issue 1 year ago • 3 comments

Currently, Slang does not support vector types for the and intrinsic:

shader.hlsl(14): error 30019: expected an expression of type 'bool', got 'vector<bool,2>'
    n = and(m, l);
            ^
shader.hlsl(14): error 30019: expected an expression of type 'bool', got 'vector<bool,2>'
    n = and(m, l);
               ^
shader.hlsl(15): error 30019: expected an expression of type 'bool', got 'vector<bool,3>'
    q = and(o, p);
            ^
shader.hlsl(15): error 30019: expected an expression of type 'bool', got 'vector<bool,3>'
    q = and(o, p);
               ^
shader.hlsl(16): error 30019: expected an expression of type 'bool', got 'vector<bool,4>'
    t = and(r, s);
            ^
shader.hlsl(16): error 30019: expected an expression of type 'bool', got 'vector<bool,4>'
    t = and(r, s);
               ^
shader.hlsl(19): error 30019: expected an expression of type 'bool', got 'vector<bool,2>'
    n = and(n, true);
            ^
shader.hlsl20): error 30019: expected an expression of type 'bool', got 'vector<bool,3>'
    q = and(q, true);
            ^
shader.hlsl(21): error 30019: expected an expression of type 'bool', got 'vector<bool,4>'

To ease porting, it would be nice to have support for a vectorized and. It's reasonable to assume that the and intrinsic gets a decent amount of usage in cases where clarity is desired. Note that there is a special case for when the arguments are a vector and a scalar bool. The scalar bool is implicitly expanded to a vector. However, there is not any special cases for arguments of vectors of two different dimensions, so no truncation.

Shader*

float4 main() : SV_Target
{
    bool a, b, c;
    c = and(a, b);

    bool1 i, j, k;
    bool2 l, m, n;
    bool3 o, p, q;
    bool4 r, s, t;
    k = and(i, j);
    n = and(m, l);
    q = and(o, p);
    t = and(r, s);
        
    k = and(k, true);
    n = and(n, true);
    q = and(q, true);
    t = and(t, true);

    bool2 tv = bool2(and(a, b), and(k.x, and(n.x, and(q.x, t.x))));   
    return tv.x ? float4(0, 0, 0, 0) : float4(1, 1, 1, 1);
}

chaoticbob avatar Jun 19 '24 18:06 chaoticbob

https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/and--sm4---asm- (reference for task)

ArielG-NV avatar Jun 19 '24 18:06 ArielG-NV

https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/and--sm4---asm- (reference for task)

I don't think this is a good reference link. The document shows the assemble code of how the operation should work not HLSL.

jkwak-work avatar Jul 02 '24 21:07 jkwak-work

The document shows the assemble code of how the operation should work not HLSL.

There is no actual doc page for and/or. I agree the provided link is not very good. HLSL2021 touch upon the topic, I suppose the following description is fine for implementation purposes although not complete(?): https://github.com/microsoft/DirectXShaderCompiler/wiki/HLSL-2021

ArielG-NV avatar Jul 02 '24 22:07 ArielG-NV

Closed by https://github.com/shader-slang/slang/pull/4529

expipiplus1 avatar Jul 09 '24 13:07 expipiplus1