-
Notifications
You must be signed in to change notification settings - Fork 187
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
base: master
Are you sure you want to change the base?
Member-wise constructor generation with visibility control #5107
Conversation
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.
…lG-NV/slang into initializer-list-visibility
…lG-NV/slang into initializer-list-visibility
1. resolve generics which are associated to a struct instance during init list evaluation 2. fix autodiff test with incorrect init-list
…lG-NV/slang into initializer-list-visibility
…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)
Just try to run the CI now to see everything is fine after rebase. |
9d28326
to
1439309
Compare
1439309
to
2682128
Compare
9333fe1
to
5401494
Compare
203c628
to
39268d6
Compare
…zedMemberwiseCtor'
…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()".
|
||
if( coercedArg ) | ||
// 2. We have a `{}` (0 arguments), try to coerce |
There was a problem hiding this comment.
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:
- create a AST node that is
InvokeExpr(calleeExpr, coercedArgs)
, where:
calleeExpr
is aVarExpr(declRef = toStructDeclRef, type = TypeType(toType), checked = true)
. - 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));
- call
subVisitor.ResolveInvoke()
on the invokeExpr. If tempSink doesn't have any errors, then we succeeded in calling the ctor, and the result ofResolveInvoke
is the*outToExpr
we need to return, otherwise, we returnfalse
. 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.
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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)) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
source/slang/slang-check-decl.cpp
Outdated
seqStmt->stmts.insert(ctorInfo.m_insertOffset++, seqStmtChild); | ||
} | ||
|
||
// Compiler generated ctor may be destroyed |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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:
- Certain tests will need to be adjusted due to making incorrect assumptions on how code-gen works with uninitialized values.
- Code-gen will include
init()
function's that effectively do nothing.
There was a problem hiding this comment.
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` |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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()
):
- We need to treat
MyStruct()
as a 'default-initialize'. This is a requirement since lots of code relies on this behavior. - We cannot treat
MyStruct()
as a 'default-initialize' using a compiler generatedinit()
since this will force all uninitializedstruct
types into calling theirinit()
('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.
}
There was a problem hiding this comment.
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
There was a problem hiding this 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.
Rebased version of PR #4854.