Skip to content

Commit

Permalink
Fix more test errors
Browse files Browse the repository at this point in the history
Struct Inheriting from a interface doesn't violate the rules of C-Style
struct.

Add the visibility modifier to the synthesized ctor.
  • Loading branch information
kaizhangNV committed Oct 10, 2024
1 parent 4ed14e9 commit 1013d73
Show file tree
Hide file tree
Showing 13 changed files with 78 additions and 28 deletions.
22 changes: 17 additions & 5 deletions source/slang/slang-check-conversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,25 +237,37 @@ namespace Slang
if (isFromStdLib(decl))
return false;

StructDecl* structDecl = nullptr;
if (auto varDecl = as<VarDecl>(decl))
{
// check for basic scalar, vector or matrix type
auto type = varDecl->getType();
if( as<VectorExpressionType>(type) ||
as<MatrixExpressionType>(type) ||
as<BasicExpressionType>(type))
return true;
else

// check for user-defined struct type
structDecl = _getStructDecl(type);
if (!structDecl)
return false;
}

auto structDecl = as<StructDecl>(decl);
if (!structDecl)
return false;
structDecl = as<StructDecl>(decl);

// 2. It cannot have inheritance
if (structDecl->getMembersOfType<InheritanceDecl>().getCount() > 0)
if (!structDecl)
return false;

// 2. It cannot have inheritance, but inherit from interface is fine.
for (auto inheritanceDecl: structDecl->getMembersOfType<InheritanceDecl>())
{
if (!isDeclRefTypeOf<InterfaceDecl>(inheritanceDecl->base.type))
{
return false;
}
}

// 3. It cannot have explicit constructor
if (_hasExplicitConstructor(structDecl, true))
return false;
Expand Down
4 changes: 1 addition & 3 deletions source/slang/slang-check-decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8769,9 +8769,6 @@ namespace Slang
cachedDeclToCheckedVar.add({ member, checkedMemberVarExpr });
}

if (!checkedMemberVarExpr->type.isLeftValue)
return;

seqStmtChild->stmts.add(stmt);
}

Expand Down Expand Up @@ -11426,6 +11423,7 @@ namespace Slang
// 1. The constructor's name is always `$init`, we create one without parameters now.
ConstructorDecl* ctor = _createCtor(this, getASTBuilder(), structDecl);
ctor->addTag(ConstructorDecl::ConstructorTags::MemberInitCtor);
addVisibilityModifier(getASTBuilder(), ctor, ctorVisibility);
structDecl->m_synthesizedCtorMap.addIfNotExists((int)ConstructorDecl::ConstructorTags::MemberInitCtor, ctor);

ctor->members.reserve(resultMembers.getCount());
Expand Down
6 changes: 3 additions & 3 deletions tests/autodiff/differential-type-constructor.slang
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ RWStructuredBuffer<float> outputBuffer;
void computeMain(uint3 dispatchThreadID: SV_DispatchThreadID)
{
{
var dp = diffPair(MyStruct(), MyStruct.Differential());
var dp = diffPair(MyStruct(0.0, 0), MyStruct.Differential(0.0));

outputBuffer[0] = dp.d.a;

var dp2 = diffPair<MyStruct>(MyStruct(), MyStruct.Differential(1.f));
var dp2 = diffPair<MyStruct>(MyStruct(0.0, 0), MyStruct.Differential(1.f));

outputBuffer[1] = dp2.d.a;
}
}
}
10 changes: 10 additions & 0 deletions tests/autodiff/generic-constructor.slang
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ struct Impl : IFoo
{
x = v.x;
}

// We have to add this __init so that the following code can still work:
// Impl.Differential v0 = { (float)x };
// because when there is a explicit constructor defined, we will not fall back
// to legacy constructor. So this construction will fail.
[Differentiable]
__init(float v)
{
x = v;
}
}

[Differentiable]
Expand Down
26 changes: 24 additions & 2 deletions tests/autodiff/generic-impl-jvp.slang
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,16 @@ struct lineardvector : IDifferentiable
val.values[i] = a[i];
}
}

// Add a new constructor for dadd() function.
__init(Real a[N])
{
[ForceUnroll]
for (int i = 0; i < N; i++)
{
val.values[i] = a[i];
}
}
};

__generic<let N : int>
Expand Down Expand Up @@ -204,12 +214,24 @@ struct linearvector : MyLinearArithmeticType, IDifferentiable

static Differential dadd(Differential a, Differential b)
{
return { myvector<Real, N>.dadd(a.val, b.val) };
// return { myvector<Real, N>.dadd(a.val, b.val) };
//
// Above code will not work because
// myvector<Real, N>.dadd will return dvector<T.Differential, N> type
// while Differential == lineardvector<N> type
// and the constructor of lineardvector<N> requires a vector<Real.Differential, N> type
// and dvector<T.Differential, N> != vector<Real.Differential, N>, though they have the
// same members.
//
// In our new design, generic will not be C-Style struct anymore.
dvector<Real.Differential, N> d = myvector<Real, N>.dadd(a.val, b.val);
return {d.values};
}

static Differential dmul<T: __BuiltinRealType>(T a, Differential b)
{
return { myvector<Real, N>.dmul<T>(a, b.val) };
dvector<Real.Differential, N> d = myvector<Real, N>.dmul<T>(a, b.val);
return {d.values};
}

[ForwardDifferentiable]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ struct Ray<let N: int> : IDifferentiable {
[numthreads(1, 1, 1)]
void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID)
{
Ray<4> ray = Ray<4>();
Ray<4> ray = Ray<4>(0.0, float4(0.0), float4(0.0));
Ray<4>.Differential ray2;

ray.a = 1.f;
Expand Down
2 changes: 1 addition & 1 deletion tests/autodiff/self-differential-type-synthesis.slang
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ struct Ray : IDifferentiable {
[numthreads(1, 1, 1)]
void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID)
{
Ray ray = Ray();
Ray ray = Ray(0, float3(0, 0, 0), float3(0, 0, 0));
Ray.Differential ray2;

ray.a = 1.f;
Expand Down
3 changes: 2 additions & 1 deletion tests/bugs/addr-scope-fix.slang
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//TEST(compute):COMPARE_COMPUTE_EX:-slang -compute -shaderobj -output-using-type
//TEST(compute):COMPARE_COMPUTE_EX:-vk -slang -compute -shaderobj -output-using-type

//TEST_INPUT:ubuffer(data=[0 0 0], stride=4):out,name=outputBuffer
RWStructuredBuffer<float> outputBuffer;
Expand Down Expand Up @@ -73,4 +74,4 @@ void func(int x)
void computeMain(uint3 dispatchThreadID: SV_DispatchThreadID)
{
func(4);
}
}
9 changes: 7 additions & 2 deletions tests/bugs/generic-default-value.slang
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//TEST(compute):COMPARE_COMPUTE_EX:-slang -compute -shaderobj
//TEST(compute):COMPARE_COMPUTE_EX:-vk -slang -compute -shaderobj

//TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):out,name outputBuffer
RWStructuredBuffer<int> outputBuffer;
Expand All @@ -8,6 +9,10 @@ works with a generic */

struct Check<T>
{
// T is not default initialize type, because it's a generic type parameter.
// Therefore, when we synthesize the contructor, we won't create a default value
// for it.
// __init(T v);
T v;
};

Expand All @@ -16,8 +21,8 @@ void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID)
{
int index = int(dispatchThreadID.x);

Check<float> v = {};
Check<float> v = {0}; // this has to be translated to `Check<float>.__init(0);` because it's not a C-Style struct

outputBuffer[index] = index + int(v.v);
}

13 changes: 6 additions & 7 deletions tests/diagnostics/mismatching-types.slang.expected
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,20 @@ tests/diagnostics/mismatching-types.slang(61): error 30019: expected an expressi
tests/diagnostics/mismatching-types.slang(63): error 30019: expected an expression of type 'GenericOuter<int>.NonGenericInner', got 'GenericOuter<float>.NonGenericInner'
a.ng = b.ng;
^~
tests/diagnostics/mismatching-types.slang(65): error 30019: expected an expression of type 'GenericInner<int>', got 'int'
tests/diagnostics/mismatching-types.slang(66): error 30019: expected an expression of type 'GenericInner<int>', got 'int'
c.i = 0;
^
tests/diagnostics/mismatching-types.slang(65): note: explicit conversion from 'int' to 'GenericInner<int>' is possible
tests/diagnostics/mismatching-types.slang(67): error 30019: expected an expression of type 'GenericInner<int>', got 'GenericInner<float>'
tests/diagnostics/mismatching-types.slang(68): error 30019: expected an expression of type 'GenericInner<int>', got 'GenericInner<float>'
c.i = c.f;
^
tests/diagnostics/mismatching-types.slang(71): error 30019: expected an expression of type 'GenericInner<int>.ReallyNested', got 'int'
tests/diagnostics/mismatching-types.slang(72): error 30019: expected an expression of type 'GenericInner<int>.ReallyNested', got 'int'
c.i.n = 0;
^
tests/diagnostics/mismatching-types.slang(71): note: explicit conversion from 'int' to 'GenericInner<int>.ReallyNested' is possible
tests/diagnostics/mismatching-types.slang(80): error 30019: expected an expression of type 'Texture1D<int>', got 'Texture1D<float>'
tests/diagnostics/mismatching-types.slang(72): note: explicit conversion from 'int' to 'GenericInner<int>.ReallyNested' is possible
tests/diagnostics/mismatching-types.slang(81): error 30019: expected an expression of type 'Texture1D<int>', got 'Texture1D<float>'
Texture1D<int> t1 = tex;
^~~
tests/diagnostics/mismatching-types.slang(82): error 30019: expected an expression of type 'Texture2D<float>', got 'Texture1D<float>'
tests/diagnostics/mismatching-types.slang(83): error 30019: expected an expression of type 'Texture2D<float>', got 'Texture1D<float>'
Texture2D<float> t2 = tex;
^~~
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
// when trying out the feature.

//TEST(compute):COMPARE_COMPUTE: -shaderobj
//TEST(compute):COMPARE_COMPUTE: -vk -shaderobj

import struct_generic_value_param_import;

Expand Down
4 changes: 3 additions & 1 deletion tests/language-feature/interfaces/zero-init-interface.slang
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Test that we can zero-init a struct with interface typed member.

//TEST(compute):COMPARE_COMPUTE(filecheck-buffer=BUFFER): -shaderobj
//TEST(compute):COMPARE_COMPUTE(filecheck-buffer=BUFFER): -vk -shaderobj

//TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):out,name=outputBuffer
RWStructuredBuffer<int> outputBuffer;
Expand All @@ -26,7 +27,8 @@ struct MyType
[numthreads(1, 1, 1)]
void computeMain(uint3 dispatchThreadID: SV_DispatchThreadID)
{
MyType t = {};
// Note 'MyType' is not a C-Style struct
MyType t = {{}};
// BUFFER: 1
outputBuffer[0] = t.foo.method();
}
4 changes: 2 additions & 2 deletions tests/language-feature/properties/property-in-interface.slang
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// property-in-interface.slang

//TEST(compute):COMPARE_COMPUTE: -shaderobj
//TEST(compute):COMPARE_COMPUTE: -shaderobj -vk

// Test that interfaces can include property declarations.

Expand Down Expand Up @@ -46,7 +46,7 @@ int test(int value)
}

//TEST_INPUT:ubuffer(data=[0 1 2 3], stride=4):out,name=outputBuffer
RWStructuredBuffer<int> outputBuffer : register(u0);
RWStructuredBuffer<int> outputBuffer;

[numthreads(4, 1, 1)]
void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID)
Expand Down

0 comments on commit 1013d73

Please sign in to comment.