Commit Graph

213 Commits

Author SHA1 Message Date
John McCall
656aa4e089 Make the ptrauth builtins no-ops when ptrauth is disabled. 2020-05-05 20:50:46 -04:00
Robert Widmann
40d9cd8d3f [NFC] Give IRGenModule Exclusive Ownership of an LLVMContext Object 2020-04-16 11:57:44 -07:00
swift-ci
270f734403 Merge pull request #30435 from zoecarver/fix/builtin-is-dead 2020-03-17 20:48:59 -07:00
zoecarver
8f1594685f Polymorphic builtins should have empy attributes 2020-03-17 15:17:10 -07:00
Kuba Mracek
84c4864911 [arm64e] Add Swift compiler support for arm64e pointer authentication 2020-02-27 16:10:31 -08:00
Dan Zheng
1779632a6f [IRGen] NFC: silence llvm::MaybeAlign warnings.
Use `llvm::MaybeAlign` instead of `unsigned` to silence slew of warnings.
2020-02-20 08:49:28 +00:00
Jonas Devlieghere
4db7f40686 Update for upstream intrinsics change (NFC)
The Intrinsic ID is now an opaque typedef to facilitate splitting up
the enum into target-specific enums.
2020-01-03 10:53:22 -08:00
Michael Gottesman
b9046c6d03 [polymorphic-builtin] Rather than asserting in IRGen if we see a builtin that was not specialized, turn the builtin into a trap.
I forgot about this part of the design when I was working on this. To ensure
that the whole design works as expected, I included a small end-to-end test
using an experimental design for simd that uses polymorphic builtins that test
this functionally.

NOTE: The experimental design is only intended to exercise the code functionally.

rdar://48248417
2019-09-23 13:51:01 -07:00
Michael Gottesman
20c5dff4b5 [builtin] Implement polymorphic builtins for all BUILTIN_BINARY_OPERATIONs.
TLDR: This patch introduces a new kind of builtin, "a polymorphic builtin". One
calls it like any other builtin, e.x.:

```
Builtin.generic_add(x, y)
```

but it has a contract: it must be specialized to a concrete builtin by the time
we hit Lowered SIL. In this commit, I add support for the following generic
operations:

Type           | Op
------------------------
FloatOrVector  |FAdd
FloatOrVector  |FDiv
FloatOrVector  |FMul
FloatOrVector  |FRem
FloatOrVector  |FSub
IntegerOrVector|AShr
IntegerOrVector|Add
IntegerOrVector|And
IntegerOrVector|ExactSDiv
IntegerOrVector|ExactUDiv
IntegerOrVector|LShr
IntegerOrVector|Mul
IntegerOrVector|Or
IntegerOrVector|SDiv
IntegerOrVector|SRem
IntegerOrVector|Shl
IntegerOrVector|Sub
IntegerOrVector|UDiv
IntegerOrVector|Xor
Integer        |URem

NOTE: I only implemented support for the builtins in SIL and in SILGen. I am
going to implement the optimizer parts of this in a separate series of commits.

DISCUSSION
----------

Today there are polymorphic like instructions in LLVM-IR. Yet, at the
swift and SIL level we represent these operations instead as Builtins whose
names are resolved by splatting the builtin into the name. For example, adding
two things in LLVM:

```
  %2 = add i64 %0, %1
  %2 = add <2 x i64> %0, %1
  %2 = add <4 x i64> %0, %1
  %2 = add <8 x i64> %0, %1
```

Each of the add operations are done by the same polymorphic instruction. In
constrast, we splat out these Builtins in swift today, i.e.:

```
let x, y: Builtin.Int32
Builtin.add_Int32(x, y)
let x, y: Builtin.Vec4xInt32
Builtin.add_Vec4xInt32(x, y)
...
```

In SIL, we translate these verbatim and then IRGen just lowers them to the
appropriate polymorphic instruction. Beyond being verbose, these prevent these
Builtins (which need static types) from being used in polymorphic contexts where
we can guarantee that eventually a static type will be provided.

In contrast, the polymorphic builtins introduced in this commit can be passed
any type, with the proviso that the expert user using this feature can guarantee
that before we reach Lowered SIL, the generic_add has been eliminated. This is
enforced by IRGen asserting if passed such a builtin and by the SILVerifier
checking that the underlying builtin is never called once the module is in
Lowered SIL.

In forthcoming commits, I am going to add two optimizations that give the stdlib
tool writer the tools needed to use this builtin:

1. I am going to add an optimization to constant propagation that changes a
"generic_*" op to the type of its argument if the argument is a type that is
valid for the builtin (i.e. integer or vector).

2. I am going to teach the SILCloner how to specialize these as it inlines. This
ensures that when we transparent inline, we specialize the builtin automatically
and can then form SSA at -Onone using predictable memory access operations.

The main implication around these polymorphic builtins are that if an author is
not able to specialize the builtin, they need to ensure that after constant
propagation, the generic builtin has been DCEed. The general rules are that the
-Onone optimizer will constant fold branches with constant integer operands. So
if one can use a bool of some sort to trigger the operation, one can be
guaranteed that the code will not codegen. I am considering putting in some sort
of diagnostic to ensure that the stdlib writer has a good experience (e.x. get
an error instead of crashing the compiler).
2019-09-19 11:42:10 -07:00
Nikolai Vazquez
ba0612f2e9 Add Builtin.isConcrete<T>(T.Type) -> Int1
Returns `true` if `T.Type` is known to refer to a concrete type. The
implementation allows for the optimizer to specialize this at -O and
eliminate conditional code.

Includes `Swift._isConcrete<T>(T.Type) -> Bool` wrapper function.
2019-08-27 16:51:09 -07:00
Vedant Kumar
9606717bae [Coverage] Drop records for functions DCE'd after builtin lowering
A function may be eliminated as dead code after initial builtin lowering
occurs. When this happens, an entry in the profile symbol table for the
function is not guaranteed. Its coverage record should be dropped.

rdar://42564768
2019-08-23 18:55:31 -07:00
Joe Groff
f0e5e1911d IRGen: Access concrete type metadata by mangled name.
When we generate code that asks for complete metadata for a fully concrete specific type that
doesn't have trivial metadata access, like `(Int, String)` or `[String: [Any]]`,
generate a cache variable that points to a mangled name, and use a common accessor function
that turns that cache variable into a pointer to the instantiated metadata. This saves a bunch
of code size, and should have minimal runtime impact, since the demangling of any string only
has to happen once.

This mostly just works, though it exposed a couple of issues:

- Mangling a type ref including objc protocols didn't cause the objc protocol record to get
  instantiated. Fixed as part of this patch.
- The runtime type demangler doesn't correctly handle retroactive conformances. If there are
  multiple retroactive conformances in a process at runtime, then even though the mangled string
  refers to a specific conformance, the runtime still just picks one without listening to the
  mangler. This is left to fix later, rdar://problem/53828345.

There is some more follow-up work that we can do to further improve the gains:

- We could improve the runtime-provided entry points, adding versions that don't require size
  to be cached, and which can handle arbitrary metadata requests. This would allow for mangled
  names to also be used for incomplete metadata accesses and improve code size of some generic
  type accessors. However, we'd only be able to take advantage of the new entry points in
  OSes that ship a new runtime.
- We could choose to always symbolic reference all type references, which would generally reduce
  the size of mangled strings, as well as make runtime demangling more efficient, since it wouldn't
  need to hit the runtime caches. This would however require that we be able to handle symbolic
  references across files in the MetadataReader in order to avoid regressing remote mirror
  functionality.
2019-08-02 14:28:53 -07:00
Erik Eckstein
ffd7f75de3 IRGen: debug info generation for cond_fail messages.
To display a failure message in the debugger, create a function in the debug info which has the name of the failure message.
The debug location of the trap/cond_fail is then wrapped into this function and the function is declared as "inlined".
In case the debugger stops at the trap instruction, it displays the inline function, which looks like the failure message.
For example:

* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
    frame #0: 0x0000000100000cbf a.out`testit3(_:) [inlined] Unexpectedly found nil while unwrapping an Optional value at test.swift:14:11 [opt]
   11
   12   @inline(never)
   13   func testit(_ a: Int?) -> Int {
-> 14     return a!
   15   }
   16

This change is currently not enabled by default, but can be enabled with the option "-Xllvm -enable-trap-debug-info".
Enabling this feature needs some changes in lldb. When the lldb part is done, this option can be removed and the feature enabled by default.
2019-07-16 14:51:18 +02:00
Brent Royal-Gordon
d5a2912a26 Revert "Better runtime failure messages (not yet enabled by default)" 2019-07-15 13:42:40 -07:00
Erik Eckstein
e915300e96 IRGen: debug info generation for cond_fail messages.
To display a failure message in the debugger, create a function in the debug info which has the name of the failure message.
The debug location of the trap/cond_fail is then wrapped into this function and the function is declared as "inlined".
In case the debugger stops at the trap instruction, it displays the inline function, which looks like the failure message.
For example:

* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
    frame #0: 0x0000000100000cbf a.out`testit3(_:) [inlined] Unexpectedly found nil while unwrapping an Optional value at test.swift:14:11 [opt]
   11
   12   @inline(never)
   13   func testit(_ a: Int?) -> Int {
-> 14     return a!
   15   }
   16

This change is currently not enabled by default, but can be enabled with the option "-Xllvm -enable-trap-debug-info".
Enabling this feature needs some changes in lldb. When the lldb part is done, this option can be removed and the feature enabled by default.
2019-07-12 14:03:13 +02:00
Ravi Kandhadai
5430aa0b17 [Builtin][SILGen][IRGen] Create a new builtin "globalStringTablePointer":
String -> Builtin.RawPointer that given a string constructed from a
literal, returns the address of the string literal in the global string
table of the compiled binary as a pointer.
2019-07-03 16:47:34 -07:00
Carl Peto
db24809cc1 Remove apparently obsolete builtin functions (#20947)
* Remove apparently obsolete builtin functions.

- Remove s_to_u_checked_conversion and u_to_s_checked_conversion functions from builtin AST parsing, SIL/IR generation and from SIL optimisations.

* Remove apparently obsolete builtin functions - unit tests.

- Remove unit tests for SIL transformations relating to s_to_u_checked_conversion and u_to_s_checked_conversion builtin functions.
2018-12-03 23:44:27 -08:00
Stephen Canon
a5c38d099c Revert "Remove apparently obsolete builtin functions (#20947)" (#20975)
This reverts commit b914464712, which passed the public CI bots, but broke some tests on watchOS.
2018-12-03 17:48:24 -05:00
Carl Peto
b914464712 Remove apparently obsolete builtin functions (#20947)
* Remove apparently obsolete builtin functions.

- Remove s_to_u_checked_conversion and u_to_s_checked_conversion functions from builtin AST parsing, SIL/IR generation and from SIL optimisations.

* Remove apparently obsolete builtin functions - unit tests.

- Remove unit tests for SIL transformations relating to s_to_u_checked_conversion and u_to_s_checked_conversion builtin functions.
2018-12-03 07:07:34 -05:00
John McCall
abdba1d3f4 Change the integer-literal type from Int2048 to IntLiteral.
Part of SR-290.
2018-10-31 23:14:58 -04:00
John McCall
cf511445e2 Basic support for Builtin.IntegerLiteral. 2018-10-31 18:42:34 -04:00
Arnold Schwaighofer
0463ba3f88 Add the Swift equivalent of the llvm.assume intrinsic
rdar://44947459
2018-10-04 13:08:26 -07:00
Mike Ash
ccf265dbbb [IRGen] Zero out the error buffer after a swift_willThrow call. 2018-09-05 17:12:51 -04:00
Mike Ash
1abf0d83c4 Merge branch 'master' into willthrow-error-register 2018-08-28 10:57:25 -04:00
Johannes Weiss
bd2de10057 Builtin.isbitwisetakable 2018-08-24 16:03:38 +01:00
Mike Ash
3e4a5a2d79 [Runtime][ABI] Have swift_willThrow take the error value in the return register.
rdar://problem/37578477
2018-08-22 15:04:49 -04:00
Vedant Kumar
bdfd220968 [Coverage] Mark symtab entries as guaranteed after intrinsic lowering
SIL optimizations may rewrite profiling intrinsics in a way that IRGen
can't lower (r://39146527). Don't claim that a coverage mapping has a
guaranteed associated symbol table entry when this happens.

I have not added a test, as this is a defensive workaround until we can
land add a SIL verifier check that prevents profiling intrinsics from
being rewritten.

rdar://40133800
2018-05-10 18:51:40 -07:00
Doug Gregor
408aaa5332 [SIL] Use SubstitutionMap in BuiltinInst. 2018-05-03 08:48:55 -07:00
Doug Gregor
d2cf60c465 Revert "[SIL] Replace more SubstitutionLists with SubstitutionMap" 2018-05-03 08:35:20 -07:00
Doug Gregor
ed1983d9d0 [SIL] Use SubstitutionMap in BuiltinInst. 2018-05-03 00:05:21 -07:00
Vedant Kumar
5eacb57921 [GenBuiltin] Avoid lowering ill-formed profiling intrinsics
The SIL optimizer may rewrite the name argument to profiling intrinsics
in a way that makes them impossible to lower.

Avoid lowering ill-formed intrinsics until the issue is addressed
(r://39146527).

rdar://38926982
2018-04-03 12:26:11 -07:00
Vedant Kumar
ef0cdcaa22 [GenBuiltin] Clear an Explosion before destroying it
This corrects a prior change to ignore profiling intrinsics imported
from swift modules. The argument Explosion passed to visitBuiltinInst
neds to be cleared before it's destroyed.
2018-04-03 12:10:30 -07:00
Vedant Kumar
f64d88591e [IRGen] Prevent mergeable traps from being created
PR #14729 made more calls to llvm.trap() non-mergeable. This follow-up
adds asserts to IRBuilder which make it harder to accidentally introduce
mergeable calls to llvm.trap() in the future.

The newly-added assertions exposed an issue in GenBuiltin while
compiling parts of the stdlib. This PR fixes the issue.

Suggested by Adrian Prantl!

rdar://32772768
2018-02-26 13:44:52 -08:00
Joe Shajrawi
6538d543f7 Builtins: add StringObjectOr and use it in the standard library 2018-02-13 11:12:55 +02:00
Vedant Kumar
5670324d93 [IRGen] Don't lower profiling intrinsics when profiling is disabled
This avoids a compiler crash when importing profiling intrinsics from
the serialized SIL in a swiftmodule.

rdar://36911790, fixes SR-6846 and SR-6800.
2018-01-26 16:05:14 -08:00
Vedant Kumar
6debe84674 [Coverage] Record PGO function name in SILCoverageMap
This simplifies the lowering of instrprof_increment by getting rid of a
call to llvm::getPGOFuncName().
2018-01-05 17:20:20 -08:00
John McCall
dfe119f86e Compute the BuiltinInfo for a builtin call to IRGenSIL; NFC.
The intention here is to make it easier to do specialized lowering
for specific builtin arguments.
2017-12-15 00:21:21 -05:00
Arnold Schwaighofer
b6f39e3132 IRGen: Whe emitting a call to a builtin runtime function transfer the
calling convention and attributes
2017-10-27 10:42:59 -07:00
Arnold Schwaighofer
8a85a9efd5 Use array copy runtime implementation instead of the array value witnesses
And add builtins for the added runtime functions (assign-take, assign-copy).

rdar://27412867
SR-3376
2017-09-12 12:43:26 -07:00
Bob Wilson
0b84388947 master-next: update for LLVM r307722
Use the new SyncScope::SingleThread and SyncScope::System values.
2017-07-13 12:07:40 -07:00
Joe Groff
80c6671c39 Runtime: Only complain about a deprecated ObjC entry point once.
We only need to alert the user once. rdar://problem/32229417
2017-05-17 12:05:53 -07:00
Joe Groff
c8a7a442ae Runtime: Include source location information in log messages about deprecated implicit Objective-C entry points.
Make it easier for migration by pinpointing exactly where to insert @objc to keep the entry points in Swift 4 mode. rdar://problem/32230003
2017-05-16 19:02:47 -07:00
Roman Levenstein
d522618bac Add a new builtin called is_same_metatype for checking the equality between metatypes
Having such a builtin makes it easier for the optimizer to reason about what is actually happening.
I plan to add later some optimizations which can optimize pieces of code dominated by such a check.
2017-05-15 16:21:09 -07:00
practicalswift
492f5cd35a [gardening] Remove redundant repetition of type names (DRY): RepeatedTypeName foo = dyn_cast<RepeatedTypeName>(bar)
Replace `NameOfType foo = dyn_cast<NameOfType>(bar)` with DRY version `auto foo = dyn_cast<NameOfType>(bar)`.

The DRY auto version is by far the dominant form already used in the repo, so this PR merely brings the exceptional cases (redundant repetition form) in line with the dominant form (auto form).

See the [C++ Core Guidelines](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#es11-use-auto-to-avoid-redundant-repetition-of-type-names) for a general discussion on why to use `auto` to avoid redundant repetition of type names.
2017-05-05 09:45:53 +02:00
Joe Groff
d42f2049f7 KeyPaths: Implement in-place instantiation of invariant key paths.
For key paths without generic or subscript parameterization, we can turn the compiler-generated key path pattern into a global object in-place.
2017-04-05 08:46:45 -07:00
Doug Gregor
f4b72f0cfc Minor tweaks per Jordan's review (thanks!) 2017-03-31 21:22:16 -07:00
Doug Gregor
5b3fe49cd0 [SE-0160] Log uses of @objc thunks emitted due to deprecated @objc inference.
Introduce a new runtime entry point,
`swift_objc_swift3ImplicitObjCEntrypoint`, which is called from any
Objective-C method that was generated due to `@objc` inference rules
that were removed by SE-0160. Aside from being a central place where
users can set a breakpoint to catch when this occurs, this operation
provides logging capabilities that can be enabled by setting the
environment variable SWIFT_DEBUG_IMPLICIT_OBJC_ENTRYPOINT:

  SWIFT_DEBUG_IMPLICIT_OBJC_ENTRYPOINT=0 (default): do not log
  SWIFT_DEBUG_IMPLICIT_OBJC_ENTRYPOINT=1: log failed messages
  SWIFT_DEBUG_IMPLICIT_OBJC_ENTRYPOINT=2: log failed messages with
  backtrace
  SWIFT_DEBUG_IMPLICIT_OBJC_ENTRYPOINT=3: log failed messages with
  backtrace and abort the process.

The log messages look something like:

    ***Swift runtime: entrypoint -[t.MyClass foo] generated by
       implicit @objc inference is deprecated and will be removed in
       Swift 4
2017-03-31 21:22:16 -07:00
Devin Coughlin
6ac36df1e7 SIL: Add SIL builtin for Thread Sanitizer inout accesses
...and IRGen it into a call to __tsan_write1 in compiler-rt. This is
preparatory work for a later patch that will add an experimental
option to treat Swift inout accesses as TSan writes.
2017-03-06 19:13:50 -08:00
Vedant Kumar
757125ab10 [Coverage] Pass each prof name var to the lowering pass just once
While invoking the instrprof_increment intrinsic, we used to pass the
profile name variable by using a GEP instruction to get to the string
constant. That prevented llvm from uniquing the names. Use a constexpr
GEP to fix the issue.
2017-02-15 13:57:47 -08:00
Slava Pestov
3519e0cd25 AST: Introduce new SubstitutionList type to replace ArrayRef<Substitution>
SubstitutionList is going to be a more compact representation of
a SubstitutionMap, suitable for inline allocation inside another
object.

For now, it's just a typedef for ArrayRef<Substitution>.
2017-02-06 21:36:33 -08:00