Commit Graph

318 Commits

Author SHA1 Message Date
zoecarver
fc1e4df039 [cxx-interop] Empty base classes have zero offset.
Make sure empty base classes don't affect the offset of stored properties.
2022-03-03 15:47:15 -08:00
zoecarver
cc08a3124b [cxx-interop] Make sure to use the cannonical base class type. 2022-03-03 15:34:31 -08:00
zoecarver
bb00e8dcfe [cxx-interop] Rudimentary support for importing base classes.
This commit adds very basic support for importing and calling base class methods, getting and setting base class fields, and using types inside of base classes.
2022-02-18 15:59:52 -08:00
Joe Groff
23557b86da Merge pull request #41111 from jckarter/infinite-type-lowering-bailout
SIL: Detect and bail out on infinite aggregates.
2022-02-01 09:19:07 -08:00
Joe Groff
fe19399585 SIL: Detect and bail out on infinite aggregates.
Sema diagnoses obvious cases of value types defined in terms of themselves,
but it can't catch cases that arise as a result of generic substitution,
such as if a generic struct has a field of associated type that becomes the
same as the struct type itself. SIL may end up handling these types, even
though they can't be instantiated (the runtime will complain and crash if
the metadata is requested), either during SILGen because they were written
in source code, or as a result of generic specialization during optimization.
SIL thus can't rely on diagnostics preventing such types from appearing, so
it needs to be able to continue gracefully when they show up. Add checks
in type lowering for when a struct or enum lowering ends up depending on
itself, and generate an infinite type lowering that's opaque and address-only.

Fixes rdar://80310017
2022-01-31 14:40:18 -08:00
zoecarver
323e2b16a7 [cxx-interop] Fix linker errors when using std-vector.
Adds tests for using std-vector and some other interesting types.

This patch fixes four mis conceptions that the compiler was previously making:

	1. Implicit destructors have no side effects. (Yes, this means we were not cleaning up some objects.)
	2. Implicit destructors have bodies. (Technically they do, but the body doesn't include CallExprs that they make when lowered to IR.)
	3. Functions other than methods can be uninstantiated templates.
	4. Uninstantiated templates may have executable code. (I.e., we can never take the fast path.)

And makes sure that we visit the destructor of any VarDecl (including parameters).
2022-01-17 11:56:15 -08:00
Arnold Schwaighofer
5c9de9e656 Revert "Merge pull request #40356 from gmittert/TypeLayoutFixes"
This reverts commit d27e6e1e46, reversing
changes made to f2e85a2b1f.

It causes an execution time failure in
`Interpreter/struct_extra_inhabitants.swift` with

```
ninja -C swift-macosx-x86_64 check-swift-optimize
```

rdar://86054209
2021-12-06 07:49:57 -08:00
Gwen Mittertreiner
13bb43b72e Revert "Revert "Merge pull request #37116 from gmittert/Descaling""
This reverts commit 7c7dd666c6.
2021-11-30 16:19:34 -08:00
Arnold Schwaighofer
7c7dd666c6 Revert "Merge pull request #37116 from gmittert/Descaling"
This reverts commit 5ebb1b2fc6, reversing
changes made to 76260c2235.

This commit causes compiler crashes when using protocol composition
types involving objc.

Repo:

```
import Foundation

public class SomeObject : NSObject {}

public protocol ProtoA{}

public protocol SomeProtoType { }
public typealias Composition = SomeObject & SomeProtoType

public struct Thing<T: ProtoA> {
    let a: Composition
    let b: T

    init(a: Composition,
         b: T
    ) {
        self.a = a
        self.b = b
    }
}

$ swiftc -c Repo.swift -O
```

While looking at this issue I noticed that it is not correct to use a
ScalarEntry of ObjCReference (or other ScalarKind::XXXReference) for
`AddressOnly##Name##ClassExistentialTypeInfo` types. These should be
calling the IGF.emit##Name##Destroy(addr, Refcounting); functions not
objc_release.
It is probably best to use the macro facilities in a similar fashion like
lib/IRGen/GenExistential.cpp does.

rdar://85269025
2021-11-11 11:44:09 -08:00
Arnold Schwaighofer
8f159cfb88 Fix some deprecated uses of IRBuilder API
rdar://84034998
2021-10-11 11:01:22 -07:00
Gwen Mittertreiner
a3ceda653f Teach TypeLayout the Different Types of References
Summary:
As part of SR-14273, the type layout infrastructure needs to be able to be able
to differentiate between types of scalars so it knows how to release/retain
appropriately. Right now, for example, to destroy a scalar, it blindly calls
into typeInfo's irgen functions which means it's not able to generate any of
the needed information for itself.

This patch adds a field to ScalarTypeLayout to allow them to know what kind of
reference they are and strings through the machinery to provide the information
to set it.

This also moves ScalarTypeLayout::destroy to use the new information.

Test Plan: ninja check-swift

Reviewers: mren, #pika_compiler

Reviewed By: mren

Subscribers: apl, phabricatorlinter

Differential Revision: https://phabricator.intern.facebook.com/D30983093

Tasks: T100580959

Tags: swift-adoption

Signature: 30983093:1632340205:3bdd3218ae86ad6b3d199cc1b504a625e3650ec0
2021-09-29 10:40:55 -07:00
Gwen Mittertreiner
12d78cf925 Use TypeLayout Structs instead of TypeInfo
When generating TypeLayouts, rather than using a Scalar Layout, use an
aligned struct so that we use the typelayout path rather than the
typeinfo path when doing irgen.
2021-09-21 22:21:15 -07:00
Gwen Mittertreiner
e94a871236 Improve TypeLayout IRGen for Fixed Structs
If the Aligned Group is fixed sized and alignment, rather than doing an
alignment computation at runtime, we compute the offset for each field
and emit a GEP.
2021-09-21 22:21:15 -07:00
Min-Yih Hsu
b363bbdd78 [IRGen][Patch 2/2] Add IRGen support for SIL DIExpression
This patch is primarily doing two things:
 - Teach IRGen for some of the instructions to generate debug info based
   on SILDebugVariable rather than AST node, which is not always
   available when reading from SIL.
 - Generate corresponding `llvm.dbg.*` intrinsics and `!DIExpression`
   from the newly added SIL DIExpression.
2021-07-21 09:26:51 -07:00
Zoe Carver
74b7341b2d Merge pull request #32378 from zoecarver/cxx/copy-const
[cxx-interop] Use user defined copy constructor to copy C++ objects.
2021-02-03 22:39:57 -08:00
zoecarver
b2b7f7b853 [cxx-interop] Use user defined copy constructor to copy C++ objects.
If a user-defined copy constructor exists, use that to copy imported C++
types.
2021-02-03 14:34:07 -08:00
swift-ci
404c2bf085 Merge remote-tracking branch 'origin/main' into rebranch 2021-01-28 13:32:22 -08:00
zoecarver
ffa0d1cf93 [cxx-interop] Add support for custom C++ destructors.
This patch adds support for custom C++ destructors. The most notable thing here, I think, is that this is the first place a struct type has a custom destructor. I suspect with more code we will expose a few places where optimization passes need to be fixed to account for this.

One of many patches to fix SR-12797.
2021-01-27 12:54:48 -08:00
Nathan Hawes
40b6798d35 Update usages of 'SwiftNewtypeAttr' for the new spelling 'SwiftNewTypeAttr' 2020-09-29 10:46:13 -07:00
Zoe Carver
63875e9e8f [cxx-interop] [IRGen] TypeInfo for address-only types. (#32973)
The current "ClangRecordTypeInfo" derives from "LoadableTypeInfo" and is
only meant for loadable types. While we have not yet run into problems,
this may cause issues in the future and as more logic is needed around
copying, moving, and destroying C++ objects, this needs to be fixed.
2020-08-10 10:41:21 -07:00
zoecarver
b2e6068d4f [cxx-interop] Rename ClangRecordTypeInfo -> LoadableClangRecordTypeInfo.
This is a more accurate and clear name. This change will help further differenciate between LoadableClangRecordTypeInfo and (the soon to be added) AddressOnlyClangRecordTypeInfo.
2020-07-23 12:31:15 -07:00
Joe Groff
950ee3b0de IRGen: Generalize the nullable optimization for single-payload enums.
Augment the `isSingleRetainablePointer` check that allows IRGen to avoid adding branching around retain/release operations on enums that use the null pointer extra inhabitant with a more general "can value witness extra inhabitants" method on TypeInfo, which says whether a type's retain/release operations are safe to invoke on some or all of its extra inhabitants. This lets us generalize the optimization to include things like `String?` or `ClassProtocol?` which are common types with a nullable pointer in them.
2020-03-03 15:00:27 -08:00
Arnold Schwaighofer
7b65768167 IRGen: Add code to compute type layouts
rdar://51988441
2020-02-19 07:12:55 -08:00
swift-ci
b068ea573f Merge remote-tracking branch 'origin/master' into master-rebranch 2020-01-22 14:24:40 -08:00
zoecarver
b3a305b3ab Remove newRet 2020-01-21 17:31:46 -08:00
zoecarver
f99b53d382 Remove debug helpers 2020-01-21 16:05:38 -08:00
zoecarver
bd3d7431c7 Load offset from metadata instead of returning offset directly 2020-01-21 16:04:21 -08:00
zoecarver
7f5f1c3f78 stash 2020-01-19 21:35:51 -08:00
zoecarver
7720a628fd Modify to catch issue 2020-01-17 13:16:02 -08:00
zoecarver
e19f40a25c Use getMetadataLayout offset instead of manual gep 2020-01-14 14:57:40 -08:00
Raphael Isemann
b9f9f23a9c Add missing include for Attr.h in GenStruct.cpp
ASTContext.h no longer includes Attr.h so we need to include
this ourselves to have the definition of SwiftNewTypeAttr.
2019-12-11 18:43:03 +01:00
Arnold Schwaighofer
4cba76309f IRGen: Add TypeExpansionContext to IRGen 2019-11-11 14:21:52 -08:00
Arnold Schwaighofer
7a54927b03 IRGen: Fix MemoryLayout::offset(of:) for tail allocated C arrays
There is no storage but an offset.

rdar://51194713
2019-10-15 14:04:31 -07:00
Slava Pestov
83c90b6b2a AST: Turn NominalTypeDecl::getStoredProperties() into a request
This improves on the previous situation:

- The request ensures that the backing storage for lazy properties
  and property wrappers gets synthesized first; previously it was
  only somewhat guaranteed by callers.

- Instead of returning a range this just returns an ArrayRef,
  which simplifies clients.

- Indexing into the ArrayRef is O(1), which addresses some FIXMEs
  in the SIL optimizer.
2019-07-16 16:38:38 -04:00
Michael Munday
192bcb2007 Basic: Use APInt to implement ClusteredBitVector
Simplify the implementation of ClusteredBitVector by using an APInt
to represent the raw bits. This simplification will make it easier
to incrementally move to a representation of bit vectors that works
on both big- and little-endian machines.

This commit also removes reserve and reserveExtra from the API
since they were only used in one place and no longer have any effect
because memory allocation is now handled by the APInt class.
2019-06-04 14:05:10 +01:00
Arnold Schwaighofer
da999f0fd7 Use noteUseOfOpaqueTypeDescriptor instead of introducing and using markOpaqueTypeDescriptorEmitted 2019-05-06 12:08:04 -07:00
Arnold Schwaighofer
fc8ff70be7 IRGen: Don't emit opaque type descriptors twice
rdar://50500620
2019-05-06 09:32:29 -07:00
Joe Groff
22793b4567 Fixes for opaque return types on local functions.
- In Sema, don't traverse nested declarations while deducing the opaque return type. This would
  cause returns inside nested functions to clobber the return type of the outer function.
- In IRGen, walk the list of opaque return types we keep in the SourceFile already for type
  reconstruction, instead of trying to visit them ad-hoc as part of walking the AST, since
  IRGen doesn't normally walk the bodies of function decls directly.

Fixes rdar://problem/50459091
2019-05-03 16:53:21 -07:00
Joe Groff
4eb064dc21 IRGen: Emit opaque type descriptors for runtime lookup.
If -enable-anonymous-context-mangled-names is enabled, meaning that we assign names to
anonymous context descriptors for discovery by RemoteAST, then include opaque type descriptors
in the type metadata record table so that they can also be found at runtime by RemoteAST for
debugger support.
2019-04-29 18:11:01 -07:00
Joe Groff
a99716bbb8 Visit var/subscript opaque return decls during IRGen/TBDGen. 2019-04-17 14:46:22 -07:00
Joe Groff
c58737fb91 merge fixup 2019-04-17 14:46:22 -07:00
Joe Groff
ca537d1e86 opaque descriptor wip 2019-04-17 14:44:40 -07:00
Slava Pestov
af83492a45 IRGen: Lazily emit reflection field descriptors
Previously even if a type's metadata was optimized away, we would still
emit a field descriptor, which in turn could reference nominal type
descriptors for other types via symbolic references, etc.
2019-04-12 01:46:23 -04:00
Slava Pestov
6d2fc0c08c IRGen: Change around how we decide to emit lazy type metadata
The old logic was confusing. The LazyTypeGlobals map would contain
entries for all referenced types, even those without lazy metadata.

And for a type with lazy metadata, the IsLazy field would begin
with a value of false -- unless it was imported.

When a non-imported type was finally visited in the AST, we would
try to "enable" lazyness for it, which meant queueing up any
metadata that had been requested prior, or immediately emitting
the metadata otherwise.

Instead, let's add a separate map that caches whether a type has
lazy metadata or not. The first time we ask for the metadata of a
type, consult this map. If the type has lazy metadata according to
the map, queue up metadata emission for the type. Otherwise, emit
metadata eagerly when the type is visited in the AST.
2019-04-12 01:46:23 -04:00
Slava Pestov
61f21a7195 IRGen: Emit field reflection descriptors for types with custom alignment
The code to decide if field descriptors were going to be emitted was
confusing, so I've refactored it a bit.
2019-04-12 01:46:23 -04:00
John McCall
7f55a4a4f0 Always give known-empty class properties a zero offset in the static layout.
Field offset vectors are always filled out with either zero or the static layout's offset, depending on the metadata initialization strategy.  This change means that the static layout's offset will only be non-zero for properties with a statically-known layout.  Existing runtimes doing dynamic class layout assign class properties a zero offset if the field offset vector entry is zero and the property is zero-sized.  So this effectively brings the compiler into accord with the runtime (for all newly-compiled Swift code, which will eventually be all Swift code because the current public releases of Swift 5 are not yet considered ABI-stable) and guarantees a zero value for the offset everywhere.

Since the runtime will agree with the compiler about the zero value of the offset, the compiler can continue to emit such offset variables as constant.  The exception to this rule is if the class has non-fragile ObjC ancestry, in which case the ObjC runtime (which is not aware of this special rule for empty fields) will attempt to slide it along with everything else.

Fixes rdar://48031465, in which the `FixedClassMetadataBuilder` for a class with a legacy-fixed layout was writing a non-zero offset for an empty field into the field offset vector, causing the runtime to not apply the special case and thus to compute a non-zero offset, which it then attempted to copy into the global field offset variable, which the compiler had emitted as a true-constant zero.
2019-02-20 00:53:11 -05:00
John McCall
724c192120 Propagate the XI count into the get/store XI tag callbacks.
This allows callers to avoid needing to reload these tags in common cases.
2018-12-11 22:18:44 -05:00
John McCall
2ba7090fe8 Remove the extra-inhabitant value witness functions.
This is essentially a long-belated follow-up to Arnold's #12606.
The key observation here is that the enum-tag-single-payload witnesses
are strictly more powerful than the XI witnesses: you can simulate
the XI witnesses by using an extra case count that's <= the XI count.
Of course the result is less efficient than the XI witnesses, but
that's less important than overall code size, and we can work on
fast-paths for that.

The extra inhabitant count is stored in a 32-bit field (always present)
following the ValueWitnessFlags, which now occupy a fixed 32 bits.
This inflates non-XI VWTs on 32-bit targets by a word, but the net effect
on XI VWTs is to shrink them by two words, which is likely to be the
more important change.  Also, being able to access the XI count directly
should be a nice win.
2018-12-11 22:18:44 -05:00
Slava Pestov
73aff74e8d IRGen: Type lowering cache doesn't need to store 'forward declarations' 2018-10-04 20:01:23 -04:00
Joe Groff
93d85997e8 Generalize extra inhabitants of tuples.
Like we did for structs, make it so that tuple types can also get extra inhabitants from whichever element with the most, not only the first. This lets us move all of the extra inhabitant handling functionality between structs and tuples in IRGen up to the common RecordTypeInfo CRTP base.
2018-09-20 15:39:44 -07:00