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

Allow swizzle on n-dimension vectors #4277

Merged

Conversation

sriramm-nv
Copy link
Collaborator

Fixes bug #3180

Adds a test case to repro the failure
Fails when using either subscript operator or swizzle operator.

@sriramm-nv sriramm-nv self-assigned this Jun 6, 2024
@sriramm-nv sriramm-nv marked this pull request as draft June 6, 2024 03:45
@sriramm-nv sriramm-nv changed the title Draft: Allow swizzle on n-dimension vectors Allow swizzle on n-dimension vectors Jun 11, 2024
@sriramm-nv sriramm-nv marked this pull request as ready for review June 11, 2024 02:07
@sriramm-nv sriramm-nv added the pr: non-breaking PRs without breaking changes label Jun 11, 2024
@sriramm-nv sriramm-nv force-pushed the sriramm/bugs/unary-swizzle-subscript branch from d5ab695 to f18557b Compare June 11, 2024 02:12
@sriramm-nv
Copy link
Collaborator Author

After adding the check for vector<vector<...>...> in checkUnsupportedInst, I am able to get past the error that breaks the test, but then ran into an assertion error, in the outRes.standardOutput as

assert failure: basicType

I am not sure where this error gets generated. So, it would be great if you can suggest if this is the right place to fix this, or if it needs fixing early on during IR generation.

This is the generated IR before it goes into the checker:

func %computeMain       : Func(Void, ConstantBuffer(%EntryPointParams), Vec(UInt, 3 : Int))
{
block %30(
                [nameHint("entryPointParams")]
                [layout(%16)]
                param %entryPointParams : ConstantBuffer(%EntryPointParams),
                [layout(%25)]
                [nameHint("dispatchThreadID")]
                [semantic("SV_DispatchThreadID", 0 : Int)]
                param %dispatchThreadID : Vec(UInt, 3 : Int)):
        [nameHint("dispatchThreadID")]
        let  %dispatchThreadID1 : Vec(UInt, 3 : Int)    = DebugVar(%29, 9 : UInt, 6 : UInt, 0 : UInt)
        DebugValue(%dispatchThreadID1, %dispatchThreadID)
        DebugLine(%29, 11 : UInt, 11 : UInt, 5 : UInt, 6 : UInt)
        DebugLine(%29, 11 : UInt, 11 : UInt, 5 : UInt, 6 : UInt)
        DebugLine(%29, 11 : UInt, 11 : UInt, 5 : UInt, 6 : UInt)
        [DebugLocation(%29, 11 : UInt, 31 : UInt)]
        [nameHint("v")]
        let  %v : Vec(Vec(Int, 2 : Int), 2 : Int)       = DebugVar(%29, 11 : UInt, 31 : UInt)
        [DebugLocation(%29, 11 : UInt, 31 : UInt)]
        [nameHint("v")]
        let  %v1        : Ptr(Vec(Vec(Int, 2 : Int), 2 : Int))  = var
        DebugLine(%29, 12 : UInt, 12 : UInt, 5 : UInt, 6 : UInt)
        let  %31        : Ptr(Vec(Int, 2 : Int))        = getElementPtr(%v1, 0 : Int)
        let  %32        : Ptr(Int)      = getElementPtr(%31, 0 : Int)
        store(%32, 1 : Int)
        DebugValue(%v, 1 : Int, 0 : Int, 0 : Int)
        DebugLine(%29, 13 : UInt, 13 : UInt, 5 : UInt, 6 : UInt)
        let  %33        : UInt  = swizzle(%dispatchThreadID, 0 : Int)
        let  %34        : Ptr(RWStructuredBuffer(Float, DefaultLayout, %14))    = get_field_addr(%globalParams, %outputBuffer)
        let  %35        : RWStructuredBuffer(Float, DefaultLayout, %14) = load(%34)
        let  %36        : Ptr(Float)    = rwstructuredBufferGetElementPtr(%35, %33)
        let  %37        : Vec(Vec(Int, 2 : Int), 2 : Int)       = load(%v1)
        let  %38        : Vec(Int, 2 : Int)     = swizzle(%37, 0 : Int)
        let  %39        : Int   = getElement(%38, 0 : Int)
        let  %40        : Float = castIntToFloat(%39)
        store(%36, %40)
        return_val(void_constant)

side note: how do I get rid of the Debug stuff so that the IR looks cleaner?

@sriramm-nv sriramm-nv force-pushed the sriramm/bugs/unary-swizzle-subscript branch from 7d8a29b to 1ff7505 Compare June 11, 2024 13:43
@ArielG-NV
Copy link
Contributor

ArielG-NV commented Jun 11, 2024

side note: how do I get rid of the Debug stuff so that the IR looks cleaner?

-g0 is no debug information
DebugInfoLevel has the meaning of each value

the whole syntax:
{ OptionKind::DebugInformation, "-g...", "-g, -g<debug-info-format>, -g<debug-level>"}

@@ -0,0 +1,14 @@
//TEST(smoke,compute):COMPARE_COMPUTE(filecheck-buffer=CHECK):-cpu -shaderobj
Copy link
Contributor

@ArielG-NV ArielG-NV Jun 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recommend adding a spirv and a glsl/hlsl test to catch errors that may be cpp specific.

@ArielG-NV
Copy link
Contributor

ArielG-NV commented Jun 11, 2024

I am not sure where this error gets generated. So, it would be great if you can suggest if this is the right place to fix this, or if it needs fixing early on during IR generation.

//slang-emit-cpp.cpp
const UnownedStringSlice* CPPSourceEmitter::getVectorElementNames(IRVectorType* vectorType)
{
    Index elemCount = Index(getIntVal(vectorType->getElementCount()));

    IRType* type = vectorType->getElementType()->getCanonicalType();
    IRBasicType* basicType = as<IRBasicType>(type);
    SLANG_ASSERT(basicType); // ASSERTS HERE
    return getVectorElementNames(basicType->getBaseType(), elemCount);
}

The code asserts because IRType* type has the value of a vector<int, 2>, which is not a IRBasicType.

The code works with glsl/hlsl.
The code fails with cpp & spirv, this is likely a bug in slang-emit-* of each respective target.

@jkwak-work
Copy link
Collaborator

jkwak-work commented Jun 12, 2024

assert failure: basicType

When you have an error message like that, it means an assertion is failing.
You should be able to immediately translate it to a following line when you see an error like that without running a debugger at all.

SLANG_ASSERT(basicType);

It will take a lot of time to figure out what it means if you try to manually narrow it down to where the message comes from.

@jkwak-work
Copy link
Collaborator

I don't think it is the same thing, but recently Yong submitted a similar fix.
https://github.com/shader-slang/slang/pull/4310/files
It may help you for this task. If not, it will still be a good thing to check out.

@csyonghe
Copy link
Collaborator

vector<vector<float, 2>, 3> isn't something we support today.

Systematically support general vector types will take a lot more effort. We should just diagnose an error when the element type of a vector is not a basic type.

@sriramm-nv sriramm-nv force-pushed the sriramm/bugs/unary-swizzle-subscript branch from 1ff7505 to 33bdd7b Compare June 16, 2024 09:33
@sriramm-nv
Copy link
Collaborator Author

side note: how do I get rid of the Debug stuff so that the IR looks cleaner?

-g0 is no debug information DebugInfoLevel has the meaning of each value

the whole syntax: { OptionKind::DebugInformation, "-g...", "-g, -g<debug-info-format>, -g<debug-level>"}

This works great in slangc. But to reproduce the assertion failure, I am using slang-test, where this option doesn't work. Will look into how this option is passed in slang-test

@sriramm-nv
Copy link
Collaborator Author

So, based on what I can infer from the advise, I will update the test to explicitly call out the error scenario, and not permit vector of non basic types.

Fixes bug shader-slang#3180
This test verifies the check for illegal swizzle on vector types, whose
element is not a basic Type (int, float).
The check captures the failure within swizzle access on a nested vector
subscript element.
vector<vector<int, 2>, 2> a
int b = a[0].x; // illegal
@sriramm-nv sriramm-nv force-pushed the sriramm/bugs/unary-swizzle-subscript branch from 33bdd7b to e873ade Compare June 16, 2024 10:24
@csyonghe csyonghe merged commit 0fe55d6 into shader-slang:master Jul 25, 2024
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr: non-breaking PRs without breaking changes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants