Commit Graph

102 Commits

Author SHA1 Message Date
Ravi Kandhadai
935686460c [SIL Optimization] Create a new utility InstructionDeleter to delete instructions
and eliminate dead code. This is meant to be a replacement for the utility:
recursivelyDeleteTriviallyDeadInstructions. The new utility performs more aggresive
dead-code elimination for ownership SIL.

This patch also migrates most non-force-delete uses of
recursivelyDeleteTriviallyDeadInstructions to the new utility.
and migrates one force-delete use of recursivelyDeleteTriviallyDeadInstructions
(in IRGenPrepare) to use the new utility.
2019-12-18 13:17:17 -08:00
Ravi Kandhadai
7744720df3 [Constant Evaluator] Fix a bug in the composition of substitution
maps in the handling of partial applies. In particular, when using
substMap.subst(otherSubstMap), ensure that otherSubstMap is not empty.

Also, store the partial-apply instruction in the symbolic closure,
which makes it easier to debug errors in the folding of partial applies.
2019-12-10 20:13:49 -08:00
zoecarver
12883d07d9 Merge branch 'master' into semantics-def 2019-11-19 13:02:47 -08:00
Ravi Kandhadai
957dc8e302 [Constant Evaluator] Make symbolic closures, which are representations
of closure literals in the constant evaluator, store the
substitution map that was in the constant evaluator state when
the closure creation site (partial_apply or thin_to_thick_function)
that created the symbolic closure was evaluated.

A substitution map is a mapping from generic type parameters to types.
This map is updated and maintained by the constant evaluator as it
evaluates function calls. At a partial-application site, the substitution
map captures the mapping from the types of the captured arguments (which
could be generic types) to the types of the symbolic value they represent
(which has to be concrete types). The substitution map would be necessary
to fold the symbolic closure into SIL code that constructs the closure
it represents.
2019-11-07 15:04:48 -08:00
Ravi Kandhadai
48bc63bb8a [Constant Evaluator] Add support for tracking types with aggregate
symbolic values, which are used to represent constant struct and tuple
instances. Associating those symbolic values with the types of the
aggregate they are representing will allow writing some sanity checks,
and will also make constant folding of the symbolic values easier and
more robust.
2019-11-06 12:47:42 -08:00
zoecarver
2ca448b23d Address review comments
* add namespace
* fix block comment style
* SEMA_ATTR -> SEMANTICS_ATTR
* error when SEMANTICS_ATTR isn't defined
2019-11-02 21:58:26 -07:00
zoecarver
9c1a614efb Fix based on review comments 2019-11-02 13:07:20 -07:00
zoecarver
d4129d8659 Add def file for semantics attributes and use constants instead of cstring literals 2019-11-02 11:36:13 -07:00
Robert Widmann
5f3b1da22a Merge pull request #27949 from CodaFi/protoplasmic-supine-jellies
[NFC] Fold The Tri-State In Optional<ProtocolConformanceRef>
2019-10-30 07:47:17 -07:00
Ravi Kandhadai
861c0aaca9 [OSLogOptimization][stdlib/private] Make string interpolation methods
of OSLogMessage constant evaluable and remove @_transparent annotation
from the methods. Also, improve diagnostics in the OSLogOptimization
pass as now it rely on seeing the appendInterpolation/Literal calls.
2019-10-29 19:35:37 -07:00
Robert Widmann
37e82a6133 [NFC] getWitnessMethodConformanceOrNone -> getWitnessMethodConformanceOrInvalid 2019-10-29 16:56:20 -07:00
Robert Widmann
3e1a61f425 [NFC] Fold The Tri-State In Optional<ProtocolConformanceRef>
ProtocolConformanceRef already has an invalid state.  Drop all of the
uses of Optional<ProtocolConformanceRef> and just use
ProtocolConformanceRef::forInvalid() to represent it.  Mechanically
translate all of the callers and callsites to use this new
representation.
2019-10-29 16:55:56 -07:00
Joe Groff
03c7919b4a SIL: Add fields to SILFunctionType for substituted function types.
https://forums.swift.org/t/improving-the-representation-of-polymorphic-interfaces-in-sil-with-substituted-function-types/29711

This prepares SIL to be able to more accurately preserve the calling convention of
polymorphic generic interfaces by letting the type system represent "substituted function types".
We add a couple of fields to SILFunctionType to support this:

- A substitution map, accessed by `getSubstitutions()`, which maps the generic signature
  of the function to its concrete implementation. This will allow, for instance, a protocol
  witness for a requirement of type `<Self: P> (Self, ...) -> ...` for a concrete conforming
  type `Foo` to express its type as `<Self: P> (Self, ...) -> ... for <Foo>`, preserving the relation
  to the protocol interface without relying on the pile of hacks that is the `witness_method`
  protocol.

- A bool for whether the generic signature of the function is "implied" by the substitutions.
  If true, the generic signature isn't really part of the calling convention of the function.
  This will allow closure types to distinguish a closure being passed to a generic function, like
  `<T, U> in (*T, *U) -> T for <Int, String>`, from the concrete type `(*Int, *String) -> Int`,
  which will make it easier for us to differentiate the representation of those as types, for
  instance by giving them different pointer authentication discriminators to harden arm64e
  code.

This patch is currently NFC, it just introduces the new APIs and takes a first pass at updating
code to use them. Much more work will need to be done once we start exercising these new
fields.

This does bifurcate some existing APIs:

- SILFunctionType now has two accessors to get its generic signature.
  `getSubstGenericSignature` gets the generic signature that is used to apply its
  substitution map, if any. `getInvocationGenericSignature` gets the generic signature
  used to invoke the function at apply sites. These differ if the generic signature is
  implied.
- SILParameterInfo and SILResultInfo values carry the unsubstituted types of the parameters
  and results of the function. They now have two APIs to get that type. `getInterfaceType`
  returns the unsubstituted type of the generic interface, and
  `getArgumentType`/`getReturnValueType` produce the substituted type that is used at
  apply sites.
2019-10-25 13:38:51 -07:00
Ravi Kandhadai
981dca5a65 [Constant Evaluator] Add support for thin_to_thick_function SIL instruction,
and fix bugs in the evaluator in the implementation of array.append, and in
loading/linking external function.
2019-10-15 19:09:14 -07:00
Ravi Kandhadai
940119c5ff [Constant Evaluator] Add support for interpreting SIL code with
partial applies (i.e., closure creations).

A new SymbolicValue: SymbolicClosure represents a closure. It
tracks the SILFunction corresponding to the target of the closure
and the SIL and Symbolic Values of the captured arguments. The
representation does not impose that all captured values must have an
associated symbolic value. This allows the evaluator to track
creations of closures whose captured arguments or bodies are not
constant evaluable.

This commit does not add support for closure applications. It only
adds suppport for closure creations, which correspond to partial_apply
instructions in SIL.
2019-10-11 18:54:19 -07:00
Ravi Kandhadai
28e4cd02d5 [Constant Evaluator] Add a flag for denoting top-level evaluation
mode and separate out the code for top-level evaluation from the normal
step-by-step evaluation. This ensures that step-by-step evaluation
does not accidentally rely on any logic meant for top-level evaluation.

This change should not affect the observable behavior of the evaluator.
2019-10-10 19:32:26 -07:00
Marc Rasi
874bb1df25 [Constant Evaluator] Support Array constants in the constant
evaluator. Add support for evaluating array initializations
and array append.
2019-09-24 15:18:14 -07:00
Ravi Kandhadai
22861ec0fd [Constant Evaluator] Pass assert configuartion option to the
evaluator to precisely evaluate Builtin.assert_configuration.

Unify UnknownReason::Trap and UnknownReason::AssertionFailure error
values in the constant evaluator, now that we have 'condfail_message'
SIL instruction, which provides an error message for the traps.
2019-09-18 17:35:46 -07:00
Ravi Kandhadai
61fd4b1431 [OSLog][Test] Update the new oslog overlay implementation to use
@_semantics("constant_evaluable") annotation to denote constant
evaluable functions.

Add a test suite that uses the sil-opt pass ConstantEvaluableSubsetChecker.cpp
to check the constant evaluability of function in the OSLog
overlay.
2019-09-18 10:44:48 -07:00
Ravi Kandhadai
032442da93 [Constant Evaluator] Add a new sil-opt pass to check constant evaluability
of Swift code snippets. Add a new test: constant_evaluable_subset_test.swift
that tests Swift code snippets that are expected to be constant evaluable and
those that are not.
2019-09-16 18:49:19 -07:00
Ravi Kandhadai
b59cc7ad6a [Constant Evaluator] Add support to the constant evaluator for:
1. builtin "int_expect", which makes the evaluator work on more
integer operations such as left/right shift (with traps) and
integer conversions.

2. builtin "_assertConfiguration", which enables the evaluator
to work with debug stdlib.

3. builtin "ptrtoint", which enables the evaluator to track
StaticString

4. _assertionFailure API, which enables the evaluator to report
stdlib assertion failures encountered during constant evaluation.

Also, enable attaching auxiliary data with the enum "UnknownReason"
and use it to improve diagnostics for UnknownSymbolicValues,
which represent failures during constant evaluation.
2019-09-16 15:12:22 -07:00
Ravi Kandhadai
11efa58a6c [Const Evaluator] Make compile-time constant evaluator correctly handle
s_to_s_checked_trunc_IntLiteral_IntNN where the bit width of the source
symbolic value (an APInt) could be smaller than the destination bits.
2019-06-21 19:35:06 -07:00
Ravi Kandhadai
2c97b72651 [Constant evaluator] Add support for destructure_tuple and
destructure_struct instructions to the constant evaluator.
2019-05-29 18:24:27 -07:00
Arnold Schwaighofer
c187c8ac13 SIL: Replace uses of getReferencedFunction() by getReferencedFunctionOrNull() and getInitialReferencedFunction()
With the advent of dynamic_function_ref the actual callee of such a ref
my vary. Optimizations should not assume to know the content of a
function referenced by dynamic_function_ref. Introduce
getReferencedFunctionOrNull which will return null for such function
refs. And getInitialReferencedFunction to return the referenced
function.
Use as appropriate.

rdar://50959798
2019-05-26 08:58:14 -07:00
Michael Gottesman
86a7ce67b9 Teach ConstExpr how to look through begin_borrow, copy_value and to ignore destroy_value, end_borrow.
This is needed now that const expr handles string operations/etc. There aren't
tests for this now since I would need to stub out the stdlib. But these are
pretty simple changes overall, I do not expect any problems, and I plan on
enabling this relatively soon.
2019-05-19 17:33:42 -07:00
Ravi Kandhadai
9be4fef53a [SIL Optimization] Add a mandatory optimization pass for optimizing
the new os log APIs based on string interpolation.
2019-05-14 18:08:59 -07:00
swift-ci
7ef557e7c8 Merge pull request #24780 from ravikandhadai/constexpr-refactoring 2019-05-14 16:34:48 -07:00
ravikandhadai
f563212f03 Revert "[SIL Optimization] Add a mandatory pass for optimizing the new os log APIs based on string interpolation." 2019-05-14 15:11:05 -07:00
Ravi Kandhadai
8b217afcaf [Const Evaluator] Fix a minor bug in the copy constructor deletion of
`ConstExprStepEvaluator`.
2019-05-14 12:09:57 -07:00
Ravi Kandhadai
b7b46622aa [SIL Optimization] Add a mandatory optimization pass for optimizing
the new os log APIs based on string interpolation.
2019-05-13 19:40:39 -07:00
ravikandhadai
d912e3312e Merge pull request #24113 from ravikandhadai/constexpr-skip
[Const evaluator] Add support to "skip" instructions in step-wise evaluation
2019-05-02 15:52:05 -07:00
ravikandhadai
26dc0921ed Merge pull request #24410 from ravikandhadai/consteval_inject_enum_addr
[Const Evaluator] Add support for inject_enum_addr, select_enum and select_enum_addr
2019-05-01 16:53:11 -07:00
Ravi Kandhadai
cb9cb31f7a [Const Evaluator] Add support for inject_enum_addr, select_enum
and select_enum_addr instructions to the constant evaluator.
2019-05-01 11:06:00 -07:00
Ravi Kandhadai
b0e56f70f9 [Const evaluator] Enable stepwise constant evaluator to skip
instructions without evaluating them while conservatively accounting
for the effects of the skipped instructions on the interpreter state.
2019-04-30 15:36:29 -07:00
Ravi Kandhadai
a7b7db7854 [Constant Evaluator] Move "string.append" semantics attribute from
`String.+=` function to `String.append` function, and use a new
semantics attribute for String.+=.

Teach the constant evaluator about `String.Append` instead of `String.+=`.
2019-04-30 14:26:06 -07:00
Ravi Kandhadai
d9a3f9c1b8 [const evaluator] Improving diagnostics generation for
unknown symbolic values by renaming some diagnostics and
creating new unknown reasons for each type of failure that
can happen during constant evaluation.
2019-04-18 13:17:17 -07:00
Ravi Kandhadai
55e82004a2 [const evaluator] Expose the step-wise constant evaluation mode
(also referred to as flow-sensitive mode) so that the evaluator
can be used by clients to constant evaluate instructions in a
SILFunction body one by one following the flow of control.
2019-04-17 18:12:17 -07:00
Ravi Kandhadai
6d373750ef [const evaluator] Refactoring: exposing functions to look up the
symbolic value bound to a SIL value from the state, and to
flow-sensitively evaluate a single instruction regardless of
whether or not it is a branch.
2019-04-12 15:39:02 -07:00
Ravi Kandhadai
bb7363da2e [const evaluator] Parameterize allocation of symbolic values in the
constant interpreter. Based on this, change to a short-lived bump
allocator for storing symbolic values in the pass that checks #assert.
2019-04-09 13:56:21 -07:00
Michael Gottesman
c0988d1ccb [const-expr] Teach ConstExpr how to handle switch_enum/unchecked_enum_data in ossa. 2019-02-20 12:39:52 -08:00
Michael Gottesman
c4cc6e42d9 [const-expr] Teach const-expr evaluator about destructures and translate its main test into an ownership test. 2019-02-18 19:40:05 -08:00
Ravi Kandhadai
ee222c3fdf [Constant Evaluator] Add support for string appends and equals to
the constant evaluator.
2019-01-25 18:51:33 -08:00
Marc Rasi
c5c2d2eb8c const evaluator: enums 2019-01-14 20:45:22 -08:00
Marc Rasi
2b6b16183a factor WellKnownFunction handler out into a function 2019-01-11 16:07:57 -08:00
Marc Rasi
0eff06e4f9 const evaluator: string values and init operations 2019-01-08 11:57:27 -08:00
Marc Rasi
867d96d6aa const interpreter generics 2019-01-03 19:46:27 -08:00
Marc Rasi
8dcf89cfd6 constant interpreter: addresses and memory
Adds memory objects and addresses to the constant interpreter, and
teaches the constant interpreter to interpret various instructions that
deal with memory and addresses.
2018-12-14 20:21:35 -08: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