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

Member-wise constructor generation with visibility control #5107

Open
wants to merge 136 commits into
base: master
Choose a base branch
from

Conversation

kaizhangNV
Copy link
Contributor

Rebased version of PR #4854.

NOTE: This is a draft

Key Changes:
1. Initializer lists call into constructors (and no longer hack around per element)
2. we auto-generate member-wise constructors for varying visibility of members (public, public-internal, public-internal-private)
3. Implements visibility and member-wise constructor rules described in shader-slang#3406
4. Reorders and reformats how we auto-generate constructors along with adding support for member-wise constructors ('visitStruct' and 'visitAggTypeDecl').
    * This was changed since currently Slang (if no constructors are found) falls back to initializer list syntax for non initializer list constructor code. Since we add a non-default-ctor this fallback logic never happens causing failures now.
5. initialization-list logic for struct objects has been reordered and reformatted due to previous logical incompatibilities.
1. fix breaking tests which cannot be fixed by adding 'old style slang array init-list syntax' support, specifically for constructing a struct without an explicit '{}'
2. clean up tests
1. resolve generics which are associated to a struct instance during init list evaluation
2. fix autodiff test with incorrect init-list
…rrent limitations."

Revert because it will break code, instead just check more generally for 0, if we don't want the "everything does an 'init'" logic this can be changes later.
…iures

remove all null default-ctor's like before, instead though redesign the hacky overload resolution into ctor such that it works:
1. resolveInvoke properly culls useless overloads
2. This stops spirious generation of empty init's (side-effect if we generate a real ctor)
3. This stops lots of warnings since we don't have a ctor that init's nothing
1. allow a more formalized init-list logic for flattened init-lists's
2. fixing invalid tests
1. clean-up documentation tests 2. fix recursive type crash with uninitialized value checks 3. fully disallow synth object printing rather than partially for auto-documentation code.
…ness bug

Adding 2 things:
1. add no_diff to synth'ed ctor if param is no_diff
2. Fix bug where DifferentialType 'ownedScope' is not setup to track the associated contained (causes 'ThisExpr' to resolve incorrectly)
@kaizhangNV
Copy link
Contributor Author

Just try to run the CI now to see everything is fine after rebase.

…t' to '_tryOverloadWithDefaultInit'

This function just tried to resolve a problem that
struct S {int x};
S s = S();

We will try to overload `S()` with synthesized default initializer.
Because there is no such function with signature of "S S()".
@kaizhangNV kaizhangNV marked this pull request as ready for review September 20, 2024 20:46

if( coercedArg )
// 2. We have a `{}` (0 arguments), try to coerce
Copy link
Collaborator

@csyonghe csyonghe Sep 20, 2024

Choose a reason for hiding this comment

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

I think we should delete all these logic here from 535 till 671, and reuse the existing ResolveInvoke logic to resolve the constructor call.

Once we reach here, we are about to turn initializer list {...} into a ctor call that is StructType(...).
This can be done by:

  1. create a AST node that is InvokeExpr(calleeExpr, coercedArgs), where:
    calleeExpr is a VarExpr(declRef = toStructDeclRef, type = TypeType(toType), checked = true).
  2. Create a clone of the current semantic visitor to try to check the invoke Expr in a sub context so that any checking errors will not leak into the current visitor:
DiagnosticSink tempSink(getSourceManager(), nullptr);
ExprLocalScope localScope;
SemanticsVisitor subVisitor(withSink(&tempSink).withParentFunc(synFuncDecl).withExprLocalScope(&localScope));
  1. call subVisitor.ResolveInvoke() on the invokeExpr. If tempSink doesn't have any errors, then we succeeded in calling the ctor, and the result of ResolveInvoke is the *outToExpr we need to return, otherwise, we return false. We may want to copy out any error message here, and report them to the user if the legacy logic also failed at line 780.

This should handle all the generic case or the variadic case just fine using the same logic in ResolveInvoke.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Create a sub-task to address this issue: #5134.
Note, I won't submit a new PR for this. It just that I found out the original task took much longer than we estimated (estimation is just 1 day! And I already spent 3 days). And it could take longer, so split the task smaller so that I will have a clear goal what I'm going to resolve.

@@ -201,11 +221,24 @@ namespace Slang
return baseStructDeclRef;
}

GenericAppDeclRef* _getGenericAppDeclRefType(Type* argType)
{
auto argDeclRefType = as<DeclRefType>(argType);
Copy link
Collaborator

Choose a reason for hiding this comment

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

auto declRef = isDeclRefTypeOf<Decl>(argType);
if (!declRef)
    return nullptr;
return as<GenericAppDeclRef>(declRef.getDeclRefBase());

// is possible (a type conversion exists).
//
return canCoerce(toType, fromExpr->type, fromExpr);
// If 2 types are "broadly equal" we know the types can be coerced directly
Copy link
Collaborator

Choose a reason for hiding this comment

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

I want to understand why this change here, along with the change in line 151 is needed.

I think it should be fine if we revert all changes to this function and in 151.

If things break, I want to understand why.

// pre-calculate any requirements of a CudaHostAttribute
HashSet<VarDeclBase*> requiresCudaHostModifier;
for (auto member : membersOfStructDeclInstance)
if (containsTargetType<TorchTensorType>(m_astBuilder, member.getDecl()->type.type))
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is going to be bad for performance because it revisits the entire type hierarchy for all the member types.

Copy link
Collaborator

Choose a reason for hiding this comment

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

An alternative is to add the required CudaHost attribute in an IR pass to make things cleaner.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Create sub-task #5136 to address this.

}

if (structDeclInfo.defaultCtor)
// Note: we assume only 1 inheritance decl currently.
Copy link
Collaborator

Choose a reason for hiding this comment

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

There is a lot of code added to this checkAggType method. We really need to split them into separate functions for readability.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Create sub-task #5135 to address comment.

seqStmt->stmts.insert(ctorInfo.m_insertOffset++, seqStmtChild);
}

// Compiler generated ctor may be destroyed
Copy link
Collaborator

Choose a reason for hiding this comment

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

What will go wrong if we just leave the empty ctor there without deleting it?

Copy link
Contributor Author

@kaizhangNV kaizhangNV Sep 23, 2024

Choose a reason for hiding this comment

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

I just figure out why Ariel wants to delete the empty default ctor.
Considering this case

struct S
{
    int a;
}

S s;

In this case, based on currently logic, we will synthesize a empty default ctor: S.$init(This), and for S s;, we will call this default ctor to construct the object. So this will cause an issue that slang will report a warning that a is not initialized.
While, if there is no default ctor (removing the default ctor), the default ctor won't be called to construct s, no warning will be reported.

I didn't participant in your previous discussion about what the expected behavior it is. I know that for C++, the default ctor will be called, and C++ won't report warning for that.

If we still want to insist this behavior, I think the warning reported here is actually helppful because it kind of reminds developers to initialize it or use s = {} to zero init it.

BTW, there is actually noting wrong that if we don't delete this ctor. But I need to fix bunch of the tests code.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@csyonghe to comment this reply first.

Copy link

@16-Bit-Dog 16-Bit-Dog Sep 23, 2024

Choose a reason for hiding this comment

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

It is worth noting that there was a second reason that makes this change (in isolation) slightly problematic.

By auto generating init() functions, we will be changing how code-gen is structured since certain previously uninitialized struct values will now have an assignment to a function (even though this init() effectively does nothing).

Example of an init() function that effectively does nothing if user writes RayDesc ray; (assuming we don't delete init() anymore)

#line 15702 "hlsl.meta.slang"
RayDesc RayDesc_x24init_0()
{

#line 15702
    RayDesc _S1;

#line 15702
    return _S1;
}

This means that the following changes can be observed:

  1. Certain tests will need to be adjusted due to making incorrect assumptions on how code-gen works with uninitialized values.
  2. Code-gen will include init() function's that effectively do nothing.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

create sub-task to remove this hack: Create a sub-task to remove this hack: #5138

auto defaultConstructExpr = createDefaultConstructExprForType(m_astBuilder, structDeclType, seqStmt->loc);

// Part 1
// Assign DefaultConstructExpr to `this`
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we only need to execute part 1 if "-zero-init" option is set.

/// init returning data. Instead we will call `$DefaultInit` through this logic below.
/// struct S { int x; }
/// S s = S(); // This will call S.$DefaultInit().
Expr* _tryOverloadWithDefaultInit(SemanticsVisitor* visitor, SemanticsVisitor::OverloadResolveContext& context, Expr* expr, OverloadCandidate* bestCandidate)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you explain this again? I still don't understand what it is trying to do. For the example shown in the comment here, haven't we already synthesized the default ctors for S, so that the overload resolution will resolve into a call to that defualt ctor already? Why do we need to run the coercion logic again here?

Copy link

@16-Bit-Dog 16-Bit-Dog Sep 23, 2024

Choose a reason for hiding this comment

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

This is code that mostly carries over from how TOT did things. The quick answer is that "we need this function to handle an edge case related to how Slang always assigns default-constructors to uninitialized values if present".

I will break down why we have this function into 2 parts below.


What _tryOverloadWithDefaultInit does:

If I type the following code, _tryOverloadWithDefaultInit will be used to resolve MyStruct():

struct MyStruct
{
    float a;
};

[numthreads(1,1,1)]
void computeMain(int3 tid : SV_DispatchThreadID)
{
    MyStruct val = MyStruct(); // effectively `MyStruct val = {};`
}

Why do we have this function:

[using the code above] We have 2 requirements for any call to MyStruct() or related (Ex: RayDesc()):

  1. We need to treat MyStruct() as a 'default-initialize'. This is a requirement since lots of code relies on this behavior.
  2. We cannot treat MyStruct() as a 'default-initialize' using a compiler generated init() since this will force all uninitialized struct types into calling their init() ('default-initialize'). This is undesired behavior.

An example where rule 2 is important:

struct MyStruct
{
    float a;
};

[numthreads(1,1,1)]
void computeMain(int3 tid : SV_DispatchThreadID)
{
    MyStruct val1 = MyStruct(); // effectively `MyStruct val = {};`. This variable is default initialized.
    MyStruct val2; // Effectively `MyStruct val2;`. This variable is uninitialized.
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Create a sub-task to remove this hack: #5138

Copy link
Collaborator

@csyonghe csyonghe left a comment

Choose a reason for hiding this comment

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

Looks good overall, but I think there are few important cleanups we need to do before checking this in.

This commit will be reverted when the initializer list re-work
is done.

This is just to unblock the test failure when we're in the middle
ground.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr: breaking change PRs with breaking changes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants