Skip to content

Generic metadata prespecialization, part 1 #28610

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

Merged

Conversation

nate-chandler
Copy link
Contributor

@nate-chandler nate-chandler commented Dec 6, 2019

Initial support for compile-time specialization of generic metadata. The support here is very limited: static specializations are generated only for usages of generic structs which are defined in the same module as the use none of whose generic arguments are themselves generic. In these cases, a direct reference to the emitted static specialization is emitted into the using function. The metadata accessor is modified in order to make those static specializations canonical. The runtime has to be modified in order to avoid checking the cache for the specialization since the static specializations never make their way into the cache. Compatibility with old-runtimes, which will need a patched version of getTypeByMangledNode in order to avoid looking in the cache for statically specialized metadata, is not present yet. The feature is gated behind the -prespecialize-generic-metadata flag.

@nate-chandler
Copy link
Contributor Author

@swift-ci please test

@swift-ci
Copy link
Contributor

swift-ci commented Dec 6, 2019

Build failed
Swift Test OS X Platform
Git Sha - e5ebb6675a263adefcd2bc853a7908c9c28eb82a

@nate-chandler nate-chandler force-pushed the generic-metadata-prespecialization branch from e5ebb66 to 952248a Compare December 6, 2019 17:09
@nate-chandler
Copy link
Contributor Author

@swift-ci please test

@swift-ci
Copy link
Contributor

swift-ci commented Dec 6, 2019

Build failed
Swift Test Linux Platform
Git Sha - e5ebb6675a263adefcd2bc853a7908c9c28eb82a

@swift-ci
Copy link
Contributor

swift-ci commented Dec 6, 2019

Build failed
Swift Test OS X Platform
Git Sha - e5ebb6675a263adefcd2bc853a7908c9c28eb82a

@nate-chandler nate-chandler force-pushed the generic-metadata-prespecialization branch from 952248a to 1c3e71c Compare December 6, 2019 17:36
@nate-chandler
Copy link
Contributor Author

@swift-ci please test

@swift-ci
Copy link
Contributor

swift-ci commented Dec 6, 2019

Build failed
Swift Test OS X Platform
Git Sha - 952248a9ba5d52638007f0a902788853cbcb99a7

@swift-ci
Copy link
Contributor

swift-ci commented Dec 6, 2019

Build failed
Swift Test Linux Platform
Git Sha - 952248a9ba5d52638007f0a902788853cbcb99a7

@nate-chandler
Copy link
Contributor Author

@swift-ci please test

@swift-ci
Copy link
Contributor

swift-ci commented Dec 6, 2019

Build failed
Swift Test Linux Platform
Git Sha - 1c3e71cc7a6f7e7edf4e3df9070b65d5e84b224a

@nate-chandler nate-chandler force-pushed the generic-metadata-prespecialization branch 2 times, most recently from 7d00476 to 8a0ab08 Compare December 6, 2019 21:58
@nate-chandler
Copy link
Contributor Author

@swift-ci please test

@swift-ci
Copy link
Contributor

swift-ci commented Dec 6, 2019

Build failed
Swift Test OS X Platform
Git Sha - 1c3e71cc7a6f7e7edf4e3df9070b65d5e84b224a

@swift-ci
Copy link
Contributor

swift-ci commented Dec 6, 2019

Build failed
Swift Test Linux Platform
Git Sha - 1c3e71cc7a6f7e7edf4e3df9070b65d5e84b224a

@nate-chandler nate-chandler force-pushed the generic-metadata-prespecialization branch from 8a0ab08 to 76124ba Compare December 7, 2019 00:45
@nate-chandler
Copy link
Contributor Author

@swift-ci please test os x platform

@swift-ci
Copy link
Contributor

swift-ci commented Dec 7, 2019

Build failed
Swift Test OS X Platform
Git Sha - 8a0ab08137d8ac152fa559beef4d859c7a80d7bd

@nate-chandler
Copy link
Contributor Author

@swift-ci please smoke test os x platform

@nate-chandler nate-chandler force-pushed the generic-metadata-prespecialization branch 4 times, most recently from b5496df to c188697 Compare December 9, 2019 21:07
@nate-chandler nate-chandler changed the title [WIP] Generic metadata prespecialization, part 1 Generic metadata prespecialization, part 1 Dec 9, 2019
@nate-chandler
Copy link
Contributor Author

@swift-ci please test

@swift-ci
Copy link
Contributor

swift-ci commented Dec 9, 2019

Build failed
Swift Test Linux Platform
Git Sha - 8a0ab08137d8ac152fa559beef4d859c7a80d7bd

@swift-ci
Copy link
Contributor

swift-ci commented Dec 9, 2019

Build failed
Swift Test OS X Platform
Git Sha - 76124baa95f1386adf8821b154248a90f3bc44f4

Added a new flag to the GenericMetadataPatternFlags flagset for whether
the metadata has a set of flags at its tail.  When that flag is set,
there will be an extra uint64_t flagset at the end of the metadata.  For
struct metadata, the type of that flagset will be
StructMetadataTrailingFlags.  The first flag in that trailing flagset
indicates whether the metadata was statically specialized.  The second
flag in that trailing flagset indicates whether the metadata is
statically canonical.

When verifying the metadata cache, a check is done for whether the
metadata was statically specialized and whether it was known to be
canonical statically.  If so, verification is skipped.  Skipping it is
necessary because the known-canonical statically specialized metadata
will not be in the cache.  In that case, the canonical statically
specialized metadata will be returned from the metadata accessor and
never be cached.
Previously, the various generic builders implemented the methods
addGenericArgument and addGenericWitnessTable without being provided
what argument or witness table they were to add (because it was not
previously needed).  Now, the GenericRequirement is passed along to both
methods.
The new frontend flag -prespecialize-generic-metadata must be passed in
order for generic metadata to be specialized statically.

rdar://problem/56984885
Compatibility with earlier swift runtimes would require modifying the
runtime compatibility libraries to adjust the behavior of
checkMetadataState by way of typeForMangledNode or even
typeForMangledName.  For now, simply require that a version of swift
whose runtime knows about prespecialized metadata is being targeted.
When emitting a reference to the metadata for a generic type, prepare
to, rather than always inserting calls to the type metadata access
function, emit direct references to static specializations when possible
and emit calls to the forthcoming swift_getCanonicalSpecializedMetadata
when not possible.

For now, the metadata access function is always called.
Added worklist of prespecializations awaiting lazy emission to
IRGenModule.  Added map from type decl to list of bound types for which
prespecializations will be emitted.

For now, no specializations are emitted.
For every prespecialization of generic metadata that exists in the
module where the generic type is defined, the metadata accessor gains
code with the following effect

  switch arguments {
  case prespecialization1.genericArguments:
    return prespecialization1
  case prespecialization2.genericArguments:
    return prespecialization2
  ...
  default:
    return swift_getGenericMetadata(...)
  }

rdar://problem/56961700
Prespecialized records contain direct references to the generic
arguments and protocol witnesses with which it is specialized for now.

Both prespecialized records and the records that are specialized at
runtime gain a trailing pointer-sized flagset.  For now, the flags in it
include whether the record was prespecialized and whether it was known
to be canonical at compile time (which is true for prespecialized
records within the module which defines the type whose metadata is
specialized since in those cases the metadata accessor can be modified).

rdar://problem/56960307
When possible, directly reference metadata prespecializations.  Doing so
is possible when the type is defined in the same module, because in
those cases the metadata accessor can be modified to ensure that the
prespecialized metadata is canonical.

rdar://problem/56994171
Metadata accessors are dependent on prespecializations of the metadata
of generic, in-module types.  Those prespecializations are themselves
dependent on usages of the types in functions.  Consequently, the
accessors must be emitted after all the functions are emitted.
The usages of a type that happen to be made in a module are
implementation details of that module and should not be exported.
Prespecialized metadata records must refer to value witness tables that
have correct values for size and stride.  In order to do that, a
prespecialized value witness table must emitted and referred to.  Here,
a fully specialized value witness table is emitted.

For the moment, the value witness table is fully specialized.  Having it
be specialized, though, will likely cause serious code size problems,
since it results in specialized value witness functions being generated.

rdar://problem/58088270
@nate-chandler nate-chandler force-pushed the generic-metadata-prespecialization branch from ab7acac to d78dc03 Compare January 10, 2020 01:27
@nate-chandler
Copy link
Contributor Author

@swift-ci please test

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - ab7acacf4e24047d1d3e39a47083870ad55942c4

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - ab7acacf4e24047d1d3e39a47083870ad55942c4

@nate-chandler
Copy link
Contributor Author

@swift-ci please test source compatibility

@nate-chandler
Copy link
Contributor Author

@swift-ci please test linux platform

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - d78dc03

@nate-chandler
Copy link
Contributor Author

@swift-ci please clean test linux platform

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - d78dc03

@nate-chandler
Copy link
Contributor Author

@swift-ci please clean test linux platform

@nate-chandler nate-chandler merged commit 1430c04 into swiftlang:master Jan 10, 2020
@nate-chandler nate-chandler deleted the generic-metadata-prespecialization branch January 10, 2020 23:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants