Commit Graph

758 Commits

Author SHA1 Message Date
Roman Levenstein
f9d0609d16 Fix a dynamic symbol lookup issue in interpreter mode.
This was a very subtle bug, which occurred only in interpreter mode under Linux.
The actual name of the symbol should be artificially prefixed by an underscore,
because this underscore is stripped during a symbol lookup in interpreter mode.
If this is not done, then a reference to variable "_x" is being resolved as a reference
to the function "x"!
2016-02-25 09:11:15 -08:00
Roman Levenstein
2ff5755dc3 Use the "rt_" prefix for all generated wrappers to distinguish them from the actual runtime functions. 2016-02-25 06:00:30 -08:00
Roman Levenstein
99fd8b6080 Rename some macros based on the PR review comments.
- use  the SWIFT prefix for all macros
- make names of some macros shorter
2016-02-25 05:31:00 -08:00
Roman Levenstein
de3b850ce8 Use more descriptive names for calling conventions.
Rename RuntimeCC into DefaultCC
Rename RuntimeCC1 into RegisterPreservingCC
Remove RuntimeCC0 because it was identical to DefaultCC.
2016-02-25 05:31:00 -08:00
Roman Levenstein
2956e5675f Update x-macro metaprogramming support in IRGenModule.
Teach IRGen how to handle runtime functions definitions in RuntimeFunctions.def
depending on their calling convention and on their need for global symbols referring
to their internal implementations.

IRGen would now generate wrappers for runtime functions invocations if at least one
of the following conditions is met:
- The runtime function is defined using FUNCTION_WITH_GLOBAL_SYMBOL_AND_IMPL,
  which explicitly states that it has a global symbol referring to its implementation. In this case,
  the generated wrapper will perform an indirect call using the specified global symbol.
  This results in some performance improvements, because doing so removes one level of
  indirection during a call of the runtime function.
  The invocation sequence before looked like:
    Swift code -> dynamic linker stub -> runtime_function -> indirect call of runtime_function's implementation through a global function pointer
  And using a wrapper it becomes:
    Swift code -> wrapper -> indirect call of runtime_function's implementation through a global function pointer

- The runtime function is defined using the usual FUNCTION x-macro but it uses a calling convention
   that requires that wrappers should be used instead of dynamic linker stubs to avoid the situations
   where the dynamic linker would clobber some of the callee-saved registers when it performs a lazy
   binding of the runtime function, which may lead to an undefined behaviour during the program execution.

   In this case, the behaviour is similar to the first case, except that the name of the global symbol for
   the  runtime function f is assumed to be _f, i.e. it has an underscore as a prefix. This symbol
   will be auto-generated and properly initialized by an x-macro based metaprogramming machinery
   in the runtime library.
2016-02-25 05:31:00 -08:00
Roman Levenstein
4941f51388 Add a new RuntimeCC1 calling convention in IRGen.
This calling convention can be now used in RuntimeFunctions.def.
2016-02-25 05:30:59 -08:00
Roman Levenstein
850dde984d Set the C_CC calling convention dynamically in IRGen.
It makes it look more uniform with other calling convention members of IRGen, e.g. with RuntimeCC.
2016-02-25 05:30:59 -08:00
Roman Levenstein
634579aae2 Move RuntimeFunctions.def into a shared location.
RuntimeFunctions.def was used only by IRGen so far. But it is going to be used by the runtime library as well.
2016-02-25 05:30:59 -08:00
Roman Levenstein
8f5e525d2d Add function to generate an llvm wrapper for performing a runtime function call.
The generated wrapper simply invokes a corresponding entry point by means of
an indirect call via a global the symbol, which is a function pointer referring
to the implementation of a runtime function.

Using such wrappers allows for invocations of runtime functions from dynamic
libraries without the usual indirections via dynamic linker stubs.

If the calling convention and the current target require a wrapper, it will be
generated. Each wrapper gets a hidden linkage and is marked as ODR, so that
a linker can merge all wrappers with the same name.
2016-02-25 05:30:58 -08:00
Roman Levenstein
420d6deda8 Make the functions generating LLVM IR declarations of runtime entry points available outside of IRGen.
This functionality could be re-used by e.g. LLVMPasses, which currently create LLVM IR declarations of runtime entry points on their own.

To make the function re-usable, slightly change the API of the function:
- use llvm::Module instead of IRGenModule.
- use llvm::ArrayRef instead of std::initializer_list, which allows the clients of this API to dynamically form the lists of return types and arguments.
2016-02-25 05:30:58 -08:00
Han Sangjin
e06c7136cb Porting to Cygwin. rebased and squashed 2016-02-22 13:20:21 +09:00
Slava Pestov
e0e2d9bf24 IRGen: Emit default witness tables in protocol metadata
IRGen now uses a ConstantBuilder to build protocol metadata, which may
now have additional fields at the end for default witnesses.

For now, the default implementations in the test have to external
because IRGen cannot emit a witness_method body where Self is
abstract. I will fix this by passing in the witness table as part
of the witness_method calling convention.

On the IRGen side, other than the calling convention change, the only
remaining piece here is emitting GenericWitnessTables and accessor
functions for conformances where the conformance is defined in
a different module than the protocol, and the protocol is resilient.

Sema still needs to infer default witnesses and store them in the
ProtocolDecl, so that SILGen can emit default witness thunks for
them.
2016-02-08 00:48:55 -08:00
David Farler
a6a5ece206 IRGen: Emit type references for remote reflection
- Implement emission of type references for nominal type field
  reflection, using a small custom encoder resulting in packed
  structs, not strings. This will let us embed 7-bit encoded
  32-bit relative offsets directly in the structure (not yet
  hooked in).
- Use the AST Mangler for encoding type references
  Archetypes and internal references were complicating this before, so we
  can take the opportunity to reuse this machinery and avoid unique code
  and new ABI.

Next up: Tests for reading the reflection sections and converting the
demangle tree into a tree of type references.

Todo: For concrete types, serialize the types for associated types of
their conformances to bootstrap the typeref substitution process.

rdar://problem/15617914
2016-02-03 13:52:26 -08:00
Erik Eckstein
aaaf36e835 Incremental compilation for the llvm part of the compiler.
Only re-generate an object file if the llvm IR (after IRGen) changed.
The check is done based on a MD5 hash of the llvm IR which is stored in a special section in the object file.

This reduces compilation time for multi-threaded whole module compilation if only a small number of files are changed.
The incremental compilation also works for compilations with a single output file. In this case it's all-or-nothing.
2016-01-29 13:16:30 -08:00
Slava Pestov
d887d823ef Re-apply "Protocol conformances can now reference resilient value types"
This comes with a fix for a null pointer dereference in _typeByName()
that would pop with foreign classes that do not have a
NominalTypeDescriptor.

Also, I decided to back out part of the change for now, where the
NominalTypeDescriptor references an accessor function instead of a
pattern, since this broke LLDB, which reaches into the pattern to
get the generic cache.

Soon we will split off the generic cache from the pattern, and at
that time we can change the NominalTypeDescriptor to point at the
cache. But for now, let's avoid needless churn in LLDB by keeping
that part of the setup unchanged.
2016-01-29 00:49:00 -08:00
Slava Pestov
4fd1387b3a Revert "Protocol conformances can now reference resilient value types"
This apparently broke Foundation and LLDB tests. I need to investigate further.

This reverts commit 65dd0e7b93.
2016-01-28 01:03:39 -08:00
Slava Pestov
65dd0e7b93 Protocol conformances can now reference resilient value types
Change conformance records to reference NominalTypeDescriptors instead of
metadata patterns for resilient or generic types.

For a resilient type, we don't know if the metadata is constant or not,
so we can't directly reference either constant metadata or the metadata
template.

Also, whereas previously NominalTypeDescriptors would point to the
metadata pattern, they now point to the metadata accessor function.
This allows the recently-added logic for instantiating concrete types
by name to continue working.

In turn, swift_initClassMetadata_UniversalStrategy() would reach into
the NominalTypeDescriptor to get the pattern out, so that its bump
allocator could be used to allocate ivar tables. Since the pattern is
no longer available this way, we have to pass it in as a parameter.

In the future, we will split off the read-write metadata cache entry
from the pattern; then swift_initClassMetadata_UniversalStrategy() can
just take a pointer to that, since it doesn't actually need anything
else from the pattern.

Since Clang doesn't guarantee alignment for function pointers, I had
to kill the cute trick that packed the NominalTypeKind into the low
bits of the relative pointer to the pattern; instead the kind is now
stored out of line. We could fix this by packing it with some other
field, or keep it this way in case we add new flags later.

Now that generic metadata is instantiated by calling accessor functions,
this change removes the last remaining place that metadata patterns were
referenced from outside the module they were defined in. Now, the layout
of the metadata pattern and the behavior of swift_getGenericMetadata()
is purely an implementation detail of generic metadata accessors.

This patch allows two previously-XFAIL'd tests to pass.
2016-01-28 00:33:10 -08:00
David Farler
ca1804f455 IRGen: Add helper methods to get field metadata and field name sections 2016-01-26 09:33:54 -08:00
David Farler
aa57b7402d Stub out FieldDescriptor LLVM type 2016-01-26 09:33:54 -08:00
David Farler
0b8a74800f Define the FieldRecord LLVM type
An individual field record for a nominal type consists of:

- 32-bit general purpose flags,
- 32-bit relative offset to the encoded type reference string, or
  32-bit relative offset to the mangled name of the type defined
  in another image, and
- 32-bit relative offset to the field name string.
2016-01-21 11:24:28 -08:00
Luke Howard
70c5755adb [SR-381]: runtime resolution of type metadata from a name
replace ProtocolConformanceTypeKind with TypeMetadataRecordKind

metadata reference does not need to be indirectable

more efficient check for protocol conformances

remove swift_getMangledTypeName(), not needed yet

kill off Remangle.cpp for non-ObjC builds

cleanup

cleanup

cleanup comments
2016-01-15 17:48:42 +11:00
John McCall
f1682cd9a8 Use real types instead of "Self" for the IR value names of local type data.
Since that's somewhat expensive, allow the generation of meaningful
IR value names to be efficiently controlled in IRGen.  By default,
enable meaningful value names only when generating .ll output.

I considered giving protocol witness tables the name T:Protocol
instead of T.Protocol, but decided that I didn't want to update that
many test cases.
2016-01-13 19:26:18 -08:00
Zach Panzarino
e3a4147ac9 Update copyright date 2015-12-31 23:28:40 +00:00
Jordan Rose
234d63e37c Remove unused and incorrect sort comparator. NFC. 2015-12-21 10:59:06 -08:00
John McCall
4d1b6e2eb6 Reform the runtime interface for unowned reference-counting.
This is a bit of a hodge-podge of related changes that I decided
weren't quite worth teasing apart:

First, rename the weak{Retain,Release} entrypoints to
unowned{Retain,Release} to better reflect their actual use
from generated code.

Second, standardize the names of the rest of the entrypoints around
unowned{operation}.

Third, standardize IRGen's internal naming scheme and API for
reference-counting so that (1) there are generic functions for
emitting operations using a given reference-counting style and
(2) all operations explicitly call out the kind and style of
reference counting.

Finally, implement a number of new entrypoints for unknown unowned
reference-counting.  These entrypoints use a completely different
and incompatible scheme for working with ObjC references.  The
primary difference is that the new scheme abandons the flawed idea
(which I take responsibility for) that we can simulate an unowned
reference count for ObjC references, and instead moves towards an
address-only scheme when the reference might store an ObjC reference.
(The current implementation is still trivially takable, but that is
not something we should be relying on.)  These will be tested in a
follow-up commit.  For now, we still rely on the bad assumption of
reference-countability.
2015-12-04 13:18:14 -08:00
Joe Groff
5f79cab79a IRGen: Purge iostreams. 2015-11-16 13:05:19 -08:00
Michael Gottesman
894f54842e Add in two missing includes.
Swift SVN r32799
2015-10-21 21:45:12 +00:00
Adrian Prantl
3356f7ac59 Pass the debug info settings through to Clang and ensure that compile units
created by Swift and Clang set the CU's compilation dir similarly.

rdar://problem/22692266

Swift SVN r32637
2015-10-12 22:20:58 +00:00
Joe Groff
42c71b7972 Don't mangle directness into type metadata symbols.
Anywhere we can't directly address type metadata in Swift, we've found we need a function call. Directness isn't useful here.

Swift SVN r32626
2015-10-12 17:22:40 +00:00
Michael Gottesman
b564b414cb Update for DataLayout changes
Swift SVN r31801
2015-09-09 04:37:18 +00:00
Joe Groff
773eadb9f2 IRGen/Runtime: Populate the runtime protocol conformance table with relative references.
By using relative references, either directly to symbols internal to the current TU, or to the GOT entry for external symbols, we avoid unnecessary runtime relocations, and we save space on 64-bit platforms, since a single image is still <2GB in size. For the 64-bit standard library, this trades 26KB of fake-const data in __DATA,__swift1_proto for 13KB of true-const data in __TEXT,__swift2_proto. Implements rdar://problem/22334380.

Swift SVN r31555
2015-08-28 18:07:22 +00:00
Joe Groff
b94a0e49b9 IRGen: Prepopulate class ObjC metadata with a null pointer instead of &_objc_empty_vtable.
The absolute symbol reference isn't needed on OS X >=10.9 or any iOS/watchOS, which are the only Darwin platforms Swift targets. Fixes rdar://problem/22339638.

Swift SVN r31367
2015-08-20 21:25:25 +00:00
Joe Groff
d7d3ff2b10 IRGen/Runtime: Keep the payload offset inside both compiler- and runtime-generated box metadata.
A missing link in our polymorphic box projection story, and likely a perf improvement for the generic case as well.

Swift SVN r29835
2015-07-01 04:51:56 +00:00
Dmitri Hrybenko
92897ea2c9 IRGen: stop using the DisableFPElim target option
Use function attributes instead.  This is required for a newer LLVM
since it does not have the target option at all.

rdar://21480622

Swift SVN r29676
2015-06-25 18:24:37 +00:00
Doug Gregor
c8e3438f32 Add frontend option -disable-autolink-framework <framework>
This frontend option allows one to turn off autolinking to the
specified framework. This general capability is motivated by
rdar://problem/21246363, where we need to turn off some autolinking in
our overlays due to internal vs. public SDK differences.

Swift SVN r29393
2015-06-15 23:43:33 +00:00
Joe Groff
2852a3951f IRGen: Mark metadata loads as invariant.
And mark loads of the value witness table as dereferenceable, so that LLVM is able to better optimize metadata accesses.

Swift SVN r29372
2015-06-13 20:44:05 +00:00
John McCall
73258f2b21 Generate method/property @encodings from the foreign
SILFunctionType of the method instead of its formal type.

Gives more accurate information to the @encoding, makes
foreign error conventions work implicitly, and allows
IRGen's Swift-to-Clang to avoid duplicating arbitrary
amounts of the bridging logic from SILGen.

Some finagling was required in order to avoid calling
getConstantFunctionType from within other kinds of
lowering, which might have re-entered a generic context.

Also required fixing a bug with the type lowering of
optional DynamicSelfTypes where we would end up with
a substituted type in the lowered type.

Also, for some reason, our @encoding for -dealloc
methods was pretending that there was a formal parameter.
There didn't seem to be any justification for this,
and it's not like Clang does that.  Fixed.

This commit reapplies r29266 with a conservative build fix
that disables ObjC property descriptors for @objc properties
that lack a getter.  That should only be possible in SIL
files, because @objc should force accessors to be synthesized.
Arguably, Sema shouldn't be marking things implicitly @objc
in SIL files, but I'll leave that decision open for now.

Swift SVN r29272
2015-06-03 04:59:54 +00:00
Dmitri Hrybenko
8d8a462ee1 Revert "Generate method/property @encodings from the foreign"
This reverts commit r29266.  It broke buildbots.

Swift SVN r29271
2015-06-03 03:49:32 +00:00
John McCall
ed68d261e7 Generate method/property @encodings from the foreign
SILFunctionType of the method instead of its formal type.

Gives more accurate information to the @encoding, makes
foreign error conventions work implicitly, and allows
IRGen's Swift-to-Clang to avoid duplicating arbitrary
amounts of the bridging logic from SILGen.

Some finagling was required in order to avoid calling
getConstantFunctionType from within other kinds of
lowering, which might have re-entered a generic context.

Also required fixing a bug with the type lowering of
optional DynamicSelfTypes where we would end up with
a substituted type in the lowered type.

Also, for some reason, our @encoding for -dealloc
methods was pretending that there was a formal parameter.
There didn't seem to be any justification for this,
and it's not like Clang does that.  Fixed.

Swift SVN r29266
2015-06-03 02:33:38 +00:00
Arnold Schwaighofer
15ff698409 IRGen: Give ClangCodeGen a chance to emit its translation unit's global state
We need this because that global state includes tables like llvm[.compiler].used
which would otherwise be sorely missed.

This fixes an issue of the clang importer that would cause us to fail whenever
we imported a function (say it is marked as static inline) that performs an
objective-c method call and we optimize the code. The optimizer would not see
the objective-c selector global variable (which is marked private) as being
"used by unkown i.e the objc runtime" and would rightly assume it could
propagate the value of the global variable's initializer value as a constant to
loads of the global variable.

Now we call the ClangCodeGenerators translation unit finalization code which
will emit these tables and other module flags. We need to take care that we
merge those datastrutures with datastructures that we emit from swift's IRGen.

rdar://21115194

Swift SVN r29176
2015-05-31 00:01:29 +00:00
Jordan Rose
57b341acd3 [IRGen] Eliminate ObjCBoolTypeSize.
We can get this information from Clang, and should.

Swift SVN r29123
2015-05-29 00:05:09 +00:00
Erik Eckstein
daaea3705f Fix a debug-info bug in multi-threaded compilation mode.
The compilation-unit's filename was not set correctly. This led to warnings printed by dsymutil.
There is no radar for this problem.



Swift SVN r28424
2015-05-11 18:12:10 +00:00
Andrew Trick
bf60957a5c Added ZExt attribute for isUniquelyReferenced runtime calls.
Swift SVN r28272
2015-05-07 16:39:29 +00:00
Andrew Trick
9c4d1a560f Move some definitions so I can add a helper without forward declaring it.
Swift SVN r28271
2015-05-07 16:39:29 +00:00
Manman Ren
e94aae06da [Function Attribute] add target-cpu and target-features sets if they're non-null.
All llvm::Functions created during IRGen will have target-cpu and target-features
attributes if they are non-null.

Update testing cases to expect the attribute in function definition.
Add testing case function-target-features.swift to verify target-cpu and
target-features.

rdar://20772331


Swift SVN r28186
2015-05-05 23:19:48 +00:00
Erik Eckstein
3940797583 IRGen: Do a better distribution of functions to multi-threaded compiled modules.
Instead of putting a function without an associated source-file into the primary module,
it is now put into the module which first references the function.



Swift SVN r28116
2015-05-04 15:30:41 +00:00
John McCall
5c171fd448 Parsing, type-checking, SILGen, and IRGen for try!.
Swift SVN r28085
2015-05-02 08:03:15 +00:00
Erik Eckstein
f362570fb9 Change IRGenModuleDispatcher to a reference in IRGenModule.
According to Sean's feedback.



Swift SVN r27692
2015-04-24 09:17:29 +00:00
Erik Eckstein
8bd9712ed3 Fix crash when compiling a module with a main-file with -g and -wmo -num-threads.
rdar://problem/20655307



Swift SVN r27631
2015-04-23 08:32:47 +00:00
Joe Groff
1c18a71ab7 IRGen: Implement boxed existential instructions.
Provide a special single-ObjC-refcounted type info for error existentials, and lower the existential box instructions to their corresponding runtime calls.

Swift SVN r26469
2015-03-24 01:10:31 +00:00