-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[DNM] compute the field index with a request instead of caching it in the instruction #37831
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4788,8 +4788,12 @@ enum class PropertyWrapperSynthesizedPropertyKind { | |
/// VarDecl - 'var' and 'let' declarations. | ||
class VarDecl : public AbstractStorageDecl { | ||
friend class NamingPatternRequest; | ||
friend class GetFieldIndexRequest; | ||
friend class ComputeFieldIndicesRequest; | ||
NamedPattern *NamingPattern = nullptr; | ||
|
||
int fieldIndex = -1; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's unfortunate that we'd still need to cache index in the decl but it would be expensive to compute indexes with request for an individual property since there could be quite a few stored properties... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This used to be cached as an It might also be worth moving into the inline bitfield for |
||
|
||
public: | ||
enum class Introducer : uint8_t { | ||
Let = 0, | ||
|
@@ -4821,6 +4825,8 @@ class VarDecl : public AbstractStorageDecl { | |
return hasName() ? getBaseIdentifier().str() : "_"; | ||
} | ||
|
||
int getFieldIndex() const; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I feel like this should probably have a slightly scarier name to convey it might entail a non-trivial amount of work (such as synthesising backing storage vars) – something like |
||
|
||
/// Get the type of the variable within its context. If the context is generic, | ||
/// this will use archetypes. | ||
Type getType() const; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -87,6 +87,20 @@ class LoweredSILRequest | |
ASTLoweringDescriptor desc) const; | ||
}; | ||
|
||
class ComputeFieldIndicesRequest : | ||
public SimpleRequest<ComputeFieldIndicesRequest, | ||
std::tuple<>(NominalTypeDecl *), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this has to be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IIRC the evaluator doesn't currently support There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah interesting, since the definition accepted void I got confused... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah I believe the signature written in the |
||
RequestFlags::Uncached> { | ||
public: | ||
using SimpleRequest::SimpleRequest; | ||
|
||
private: | ||
friend SimpleRequest; | ||
|
||
// Evaluation. | ||
std::tuple<> evaluate(Evaluator &evaluator, NominalTypeDecl *decl) const; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here as well? |
||
}; | ||
|
||
/// Report that a request of the given kind is being evaluated, so it | ||
/// can be recorded by the stats reporter. | ||
template <typename Request> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,3 +20,6 @@ SWIFT_REQUEST(SILOptimizer, ExecuteSILPipelineRequest, | |
SWIFT_REQUEST(SILOptimizer, LoweredSILRequest, | ||
std::unique_ptr<SILModule>(ASTLoweringDescriptor), | ||
Uncached, NoLocationInfo) | ||
SWIFT_REQUEST(SILOptimizer, ComputeFieldIndicesRequest, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, ideally we'd have a SIL request evaluator zone for this. I think for now at least it might be better off placed in the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok. I can move it to the type checker |
||
void(NominalTypeDecl *), | ||
Cached, NoLocationInfo) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you can make it |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,6 +34,7 @@ | |
#include "swift/AST/Module.h" | ||
#include "swift/AST/NameLookup.h" | ||
#include "swift/AST/NameLookupRequests.h" | ||
#include "swift/AST/SILOptimizerRequests.h" | ||
#include "swift/AST/ParameterList.h" | ||
#include "swift/AST/ParseRequests.h" | ||
#include "swift/AST/Pattern.h" | ||
|
@@ -5518,6 +5519,16 @@ VarDecl::VarDecl(DeclKind kind, bool isStatic, VarDecl::Introducer introducer, | |
Bits.VarDecl.IsTopLevelGlobal = false; | ||
} | ||
|
||
int VarDecl::getFieldIndex() const { | ||
if (fieldIndex < 0) { | ||
NominalTypeDecl *parent = getDeclContext()->getSelfNominalTypeDecl(); | ||
evaluateOrDefault(getASTContext().evaluator, | ||
ComputeFieldIndicesRequest{parent}, {}); | ||
assert(fieldIndex >= 0); | ||
} | ||
Comment on lines
+5523
to
+5528
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We generally want to try and avoid having side-effecting requests like this. Ideally You could also fill in the other field indices for the other properties as you iterate by calling There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hm, would a better approach here be to remove There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If it's going into a DenseMap then that would basically be more or less equivalent to just using the evaluator's internal cache, which we could possibly do here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, I could also add a DenseMap for this in SILModule without using the evaluator machinery. But the field indices are something which are used in a critical path in the optimizer (for computing projections). So the purpose of this exercise is to avoid the overhead of a hash lookup. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ideally, the index of all stored properties should be computed and cached for a type during a single request. This should result in a unique index for each property in an object. This is something that we can't currently rely on but would greatly simplify analysis because the index would have meaning independent of the address type. |
||
return fieldIndex; | ||
} | ||
|
||
Type VarDecl::getType() const { | ||
return getDeclContext()->mapTypeIntoContext(getInterfaceType()); | ||
} | ||
|
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.
GetFieldIndexRequest
doesn't appear to be definedThere 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.
good catch. I'll remove it