Commit Graph

41 Commits

Author SHA1 Message Date
Joe Groff
d9798c0868 Concurrency: Redo non-_f variants of swift_task_create to accept closures as is.
In their previous form, the non-`_f` variants of these entry points were unused, and IRGen
lowered the `createAsyncTask` builtins to use the `_f` variants with a large amount of caller-side
codegen to manually unpack closure values. Amid all this, it also failed to make anyone responsible
for releasing the closure context after the task completed, causing every task creation to leak.
Redo the `swift_task_create_*` entry points to accept the two words of an async closure value
directly, and unpack the closure to get its invocation entry point and initial context size
inside the runtime. (Also get rid of the non-future `swift_task_create` variant, since it's unused
and it's subtly different in a lot of hairy ways from the future forms. Better to add it later
when it's needed than to have a broken unexercised version now.)
2021-03-08 16:54:19 -08:00
Doug Gregor
4c2c2f32e9 [Concurrency] Implement a builtin createAsyncTask() to create a new task.
`Builtin.createAsyncTask` takes flags, an optional parent task, and an
async/throwing function to execute, and passes it along to the
`swift_task_create_f` entry point to create a new (potentially child)
task, returning the new task and its initial context.
2020-11-07 23:05:04 -08:00
David Zarzycki
0fe540f836 Track upstream llvm change to enum definition
See LLVM commit: 3587c9c4275b96e7a7ddc4eeb6a001b7d03b55bb
2020-07-03 07:19:19 -04:00
Robert Widmann
e2cab420f3 Excise the Global LLVM Context
Add a private scratch context to the ASTContext and allow IntrinsicInfo sole access to it so it can allocate attributes into it. This removes the final dependency on the global context.
2020-04-17 17:48:31 -07:00
Michael Gottesman
e90a68fa17 [polymorphic-builtins] Teach dataflow diagnostics how to emit an error if it sees an unspecialized polymorphic builtin.
This will ensure that if an expert user is using this feature and makes a
mistake as a result of tweaking their code, they get an error. This will ensure
they do not ship and look into why this is happening.

This is not intended to be used by anyone except for expert stdlib users.
2019-09-20 17:25:56 -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
Michael Gottesman
918a224d48 [ast] Define BuiltinTypeKind a derived enum from TypeKind that allows for exhaustive switching over builtin types.
I am going to use this to refactor some code in ASTPrinter onto BuiltinType
wherein I use an exhaustive switch to print things as appropriate. Exhaustive
switching over all builtin types just seems like a useful thing to have as well.
2019-07-15 20:21:54 -07:00
Adrian Prantl
ff63eaea6f Remove \brief commands from doxygen comments.
We've been running doxygen with the autobrief option for a couple of
years now. This makes the \brief markers into our comments
redundant. Since they are a visual distraction and we don't want to
encourage more \brief markers in new code either, this patch removes
them all.

Patch produced by

      for i in $(git grep -l '\\brief'); do perl -pi -e 's/\\brief //g' $i & done
2018-12-04 15:45:04 -08:00
Jordan Rose
e786431a3b Use proper type for getLLVMIntrinsicID
At one point we were probably trying to avoid including
llvm/IR/Intrinsics.h in Builtins.h but that ship has sailed.

No functionality change.
2018-12-04 11:22:38 -08:00
Jordan Rose
805ae332e1 Cache LLVM attributes for intrinsics
Looking these up is slow because the attribute lists get rebuilt and
reuniqued in the LLVM context each time. Saving them is a single
pointer per IntrinsicInfo and IntrinsicInfos are already cached in
each SILModule.

This shaves about 4s off optimizing the standard library (72s out of a
3:00+ build), and probably has much less effect anywhere else. That
is, it's not /really/ important after all, but it was an easy bit of
the profile to hammer down.
2018-12-04 11:22:37 -08:00
Sho Ikeda
422136e1a2 [gardening][enum class] Replace unsigned char with uint8_t for consistency
Before the changes:

- `git grep -E "enum class .+ : uint8_t \{" | wc -l`: 90
- `git grep -E "enum class .+ : unsigned char \{" | wc -l`: 26
2018-03-12 13:57:36 +09: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
Saleem Abdulrasool
3b9608338a Adjust for SVN r290708
The introduction of `llvm.memcpy.element.atomic` would cause an
ambiguity when we did the lookup with the trailing `.` for the type
parameters.  The intrinsic itself is not necessarily suffixed with the
type in the identifier.  Look up the identifier by explicit name.
2017-01-08 17:18:52 -08:00
practicalswift
6d1ae2a39c [gardening] 2016 → 2017 2017-01-06 16:41:22 +01:00
practicalswift
797b80765f [gardening] Use the correct base URL (https://swift.org) in references to the Swift website
Remove all references to the old non-TLS enabled base URL (http://swift.org)
2016-11-20 17:36:03 +01:00
Doug Gregor
c16e4ffa88 More llvm::AtomicOrdering fixes. 2016-04-14 10:13:01 -07:00
Zach Panzarino
e3a4147ac9 Update copyright date 2015-12-31 23:28:40 +00:00
practicalswift
fa0b339a21 Fix typos. 2015-12-26 17:51:59 +01:00
Joe Groff
a0874ad8d0 Add a Builtin.canBeObjCClass "type trait" builtin.
This builtin returns true for types that might be ObjC class types. We want to use this builtin to optimize away NSArray handling for non-object Array types, so it needs to persist in SIL long enough for specialization to do its thing, but we never actually want to pay a runtime cost for this check, so always lower it to a constant value at IRGen time. Handle this by having canBeObjCClass return a tri-state "yes/maybe/no" result. In SILGen, we only fold away obviously "yes" or "no" cases, and in IRGen, we fold away "maybe" cases as "yes".

The optimizer will need to learn about this builtin too, but that part isn't done yet.

Swift SVN r13980
2014-02-17 07:25:52 +00:00
Chris Lattner
ecfba9fb53 Rename getBuiltinValue -> getBuiltinValueDecl since it doesn't
produce a value, it produces a decl.



Swift SVN r10697
2013-12-01 02:13:45 +00:00
Anna Zaks
740fc0a9d9 Revert "Revert "Reimplement integer arithmetic overflow checking to use special, error on overflow builtins""
(This only fails under -DSWIFT_OPTIMIZED=NO; most likely due to an llvm bug.)

We've decided that it's best to specialize each arithmetic builtin that could overflow, instead of calling a separate generic "staticReport" builtin and passing it enough info to produce the message. The main advantage of this approach is that it would be possible for the compiler to customize the message and better link it to the builtin that overflows. For example, the constants that participated in the computation could be printed. In addition, less code will be generated and the compiler could, in the future, automatically emit the overflow diagnostics/trap at runtime.

This patch introduces new versions of op_with_overflow swift builtins. Which are lowered to llvm.op_with_overflow builtins in IRGen after the static diagnostics. If the last argument to the builtins evaluates to true, the overflow is unintentional. CCP uses the builtins to diagnose the overflow detectable at compile time. FixedPoint is changed to rely on these in implementation of primitive arithmetic operations.

Swift SVN r9328
2013-10-14 21:42:33 +00:00
John McCall
a79cee2c54 Revert "Reimplement integer arithmetic overflow checking to use special, error on overflow builtins"
This was causing massive failures at run-time.

This reverts commit 80081db973ccb7100741fea19ce8e8c116fc410f.

Conflicts:
	lib/SILPasses/ConstantPropagation.cpp
	test/SILPasses/constant_propagation.swift
	test/SILPasses/constant_propagation2.sil

Swift SVN r9050
2013-10-09 01:20:39 +00:00
Anna Zaks
ccc1dae7fd Reimplement integer arithmetic overflow checking to use special, error on overflow builtins
After talking to John, Joe, and Dave Z, we've decided that it's best to
specialize each arithmetic builtin that could overflow, instead of calling
a separate generic "staticReport" builtin and passing it enough info to
produce the message. The main advantage of this approach is that it
would be possible for the compiler to customize the message and better
link it to the builtin that overflows. For example, the constants that
participated in the computation could be printed. In addition, less code
will be generated and the compiler could, in the future, automatically
emit the overflow diagnostics/trap at runtime.

This patch introduces new versions of op_with_overflow swift builtins.
Which are lowered to llvm.op_with_overflow builtins in IRGen after the
static diagnostics. If the last argument to the builtins evaluates to true,
the overflow is unintentional. CCP uses the builtins to diagnose the overflow
detectable at compile time. FixedPoint is changed to rely on these in
implementation of primitive arithmetic operations.

Swift SVN r9034
2013-10-08 23:07:56 +00:00
Joe Groff
450ad66a8a Comment typo
Swift SVN r8113
2013-09-11 22:00:45 +00:00
Anna Zaks
fe2d8d594d [SIL] Get access to LLVM intrinsic attributes.
And use them to decide if an llvm intrinsic apply instruction can be considered dead.

(This is a hack because it uses LLVM Global context. However, we already use
this approach elsewhere.)

Swift SVN r7404
2013-08-21 17:44:51 +00:00
Anna Zaks
21ce68188d [SIL] Add attributes to swift builtins, specifically, the readnone attribute.
Use the attribute when deciding if a call to a builtin can be eliminated as dead.

Swift SVN r7391
2013-08-21 00:02:25 +00:00
Anna Zaks
2f05d5c4df [SIL] Move GenFunc to use getBuiltinInfo and getIntrinsicInfo.
Make the functions support a wider range of builtins and store types to make
it possible.

This is an optimization - the cached ID will be used for builtin identification,
instead of retrieval of the string name and using it as the key.

Swift SVN r7390
2013-08-21 00:02:22 +00:00
Anna Zaks
2870c53a54 [SIL] Cache Builtin Kind and type lookup in SILModule and speed up CCP
Swift SVN r7354
2013-08-20 01:31:48 +00:00
Anna Zaks
fae4359d72 [SIL CCP] Fold trunc, sext, and zext builtins.
Swift SVN r7348
2013-08-19 23:57:03 +00:00
Doug Gregor
9a5c96a8c1 Introduce basic support for LLVM vectors as builtins.
This adds builtin types Builtin.VecNxT, where N is a natural number
and T is a builtin type, which map down to the LLVM type <N x T>. 

Update varous builtins to support vector arguments, e.g., binary
operations, comparisons, negation. Add InsertElement and
ExtractElement builtins for vectors.

On top of these builtins, add Vec4f and Vec4b structs to the standard
library, which provide 4xFloat and 4xBool vectors, respectively, with
basic support for arithmetic. These are mostly straw men, to be burned
down at our leisure.

Some issues as yet unresolved:
  - Comparisons of Vec4f'ss are producing bogus Vec4b's, which I
  haven't tracked down yet.
  - We still don't support the shuffle builtin, although it should be
  easy
  - More testing!



Swift SVN r5820
2013-06-26 21:16:36 +00:00
Ted Kremenek
219b7bd430 Move LLVM.h to libBasic.
Swift SVN r2550
2012-08-03 23:54:29 +00:00
Chris Lattner
b2fb91fba6 rip out my previous hack for Builtin.trap() and put in general code that
allows access to any LLVM IR intrinsic that has types that can be mapped
to swift types.  Notably, this excludes vector stuff, but there is a lot
of other goodness that can now be poked at.



Swift SVN r1890
2012-05-17 20:31:17 +00:00
Chris Lattner
f3c3ad086f Remove OverloadedBuiltinKind::Arithmetic, replacing it with OverloadedBuiltinKind::Special
and simplifying Builtins.def.


Swift SVN r1854
2012-05-15 04:29:47 +00:00
Chris Lattner
18530ed142 change BuiltinValueKind and isBuiltinValue to be private to Builtins.cpp.
Swift SVN r1853
2012-05-15 04:17:38 +00:00
Chris Lattner
c168af44e8 split the Builtin name parsing logic out to its own function.
Swift SVN r1832
2012-05-14 17:23:18 +00:00
Doug Gregor
86cf79a746 Add a range subscript operation for SliceInt64 that produces a slice, e.g.,
a[5..9]

will return a 4-element slice of the array a. Addresses
<rdar://problem/11329415>.


Swift SVN r1665
2012-04-27 00:07:52 +00:00
Chris Lattner
4f4c8b1455 add initial support for Builtin cast instructions, patch #1/2.
Swift SVN r1433
2012-04-14 00:56:35 +00:00
Chris Lattner
d525ba5fda simplify builtin type processing by eliminating BuiltinTypeKind.
Swift SVN r967
2011-12-22 07:38:17 +00:00
Chris Lattner
e248bc875f switch to llvm.h
Swift SVN r700
2011-09-09 04:59:19 +00:00
Chris Lattner
8e369b34da make builtins.cpp include its header first (exposing that it isn't actually self-contained),
and switch to AST.h


Swift SVN r699
2011-09-09 04:58:39 +00:00
John McCall
37b07c8691 Add builtin bindings for a bunch of primitive LLVM instructions.
Swift SVN r698
2011-09-08 00:21:11 +00:00