Commit Graph

92 Commits

Author SHA1 Message Date
Anna Zaks
b687c3ce9c Add runtime integer truncation checking to the conversion constructors
Add new builtins(by generalizing, renaming, and extending the builtins used for compile time integer literal checking). These new builtins truncate integers and check for overflow/truncation errors at runtime. Use these for FixedPoint conversion constructors.

Fix a routine in stdlib's String implementation and a test that relied on bitwise behavior of the constructors (and triggered overflows).

TODO:
- Teach CCP about these to get static checking.
- Add special builtins for same size signed <-> unsigned conversions.

Swift SVN r10432
2013-11-13 21:54:34 +00:00
Anna Zaks
084bdfd05f Change the signature of the trunc builtins to return a tuple containing teh overflow bit as well as the result.
This is a first step in generalizing the builtin to handle non-constant int to int truncations.

Swift SVN r10341
2013-11-11 21:20:04 +00:00
Greg Parker
49e1856c13 Add Builtin.[trunc|zext|sext]OrBitCast() instead of hijacking the true casts.
Swift SVN r9985
2013-11-06 03:15:04 +00:00
Chris Lattner
beab485db3 enhance the constant folding pass a bit to be smarter about folding
tuple-producing results (like those produced by folding overflow builtins).

Before the pass would just RAUW the apply_inst with a tuple_inst, but this
would leave around a bunch of tuple extracts.  Now we seek and destroy them.

This unblocks other transformations and allows the stdlib to shrink by another
1700 LOC.


Swift SVN r9900
2013-11-03 06:01:13 +00:00
Chris Lattner
7403194b56 minor simplifications, no functionality change.
Swift SVN r9862
2013-11-01 00:07:10 +00:00
Chris Lattner
c118613de3 Fix SILValue use_iterators to type the user as a SILInstruction, not just a ValueBase.
There are no values other than instructions that can use other values.  BBArguments are
defs, not uses.  This eliminates a bunch of casts in clients that use getUser().


Swift SVN r9701
2013-10-27 23:32:14 +00:00
Anna Zaks
b2034f7a46 [CCP] Fold integer binary predicates.
Swift SVN r9642
2013-10-24 18:23:04 +00:00
Anna Zaks
594e2f5781 [CCP] Rework CCP to use an inverted algorithm.
Instead of adding all the instructions into the WorkList to begin with,
add only the constants and try to fold their users. Each time a user can
be folded, add it to the WorkList. This keeps the WorkList smaller.

The inverted algorithm also allows to simplify the handling of struct_extract
and tuple_extract.

(as suggested by Chris in comments on r9545)

Swift SVN r9598
2013-10-22 21:10:40 +00:00
Chris Lattner
91c7cea310 Remove the SILModule& argument from a few methods in SILInstruction.h.
Instructions know which module they came from, so there is no need to
pass it in.



Swift SVN r9584
2013-10-22 11:06:50 +00:00
Chris Lattner
c538feb61d some more minor tidying up: use StringRef instead of std::string in
one place, and use types already available instead of reconstructing
them (to simplify code).



Swift SVN r9583
2013-10-22 10:49:28 +00:00
Chris Lattner
0dd231ffff indentation: don't indent cases under switch statements. Even thought
Xcode insists on doing this, it is "wrong".


Swift SVN r9548
2013-10-21 18:23:44 +00:00
Chris Lattner
511931859e Some belated code review for the constant prop pass. Overall
the algorithm looks great, I made a number of minor stylistic
and comment changes, used some simpler APIs, and simplified
some things.  The only functionality change is a corner case
correctness bug where it looks like the struct/tuple extraction
folding logic in constantFoldInstruction could return the wrong
result in some corner case.


Swift SVN r9547
2013-10-21 18:21:27 +00:00
Anna Zaks
65a4766c29 Demote the integer literal overflow error into warning in some cases.
As a temporary workaround for radar://15236662.

Swift SVN r9395
2013-10-16 00:56:26 +00:00
Anna Zaks
38745bffa7 Add division by zero and integer division overflow diagnostics.
Addresses radar://15225468.

Swift SVN r9387
2013-10-15 23:13:13 +00:00
Anna Zaks
cf2ff89af5 Add type info to the constant expression overflow message
(As per Jordan's and Dmitry's code review for r9033.)

Swift SVN r9370
2013-10-15 20:22:29 +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
Dmitri Hrybenko
158ed79f34 Adds overflow checking for floating point literals. The approach is the same
as for integer literals.


Swift SVN r9325
2013-10-14 19:47:38 +00:00
Dmitri Hrybenko
4a92814a67 Fix typo in comments
Swift SVN r9257
2013-10-12 11:26:16 +00:00
Dmitri Hrybenko
4204c02e71 Revert r9252 -- 'underflow' is not the correct term here
Swift SVN r9255
2013-10-12 05:33:00 +00:00
Dmitri Hrybenko
58e449445e Integer overflow diagnostics: specialize diagnostic message for underflow
Swift SVN r9254
2013-10-12 05:14:58 +00:00
Anna Zaks
1d646a8ba8 Implement overflow checking on integer literals (1 of 2)
- Added 2 new builtins strunc_with_overflow and utrunc_with_overflow
  that perform truncation and produce a compile time error when truncation
  overflows.

- Used these builtins instead of trunc to implement "_convertFromBuiltinIntegerLiteral".

- Currently, the builtins are converted to trunc in IRGen, but we should
  not be IRGenning code that uses them, since all uses of
  "_convertFromBuiltinIntegerLiteral" should be inlined and the arguments
  constant folded.

- I had to change a test and the implementation of operator '~' in the standard library
  because they assumed that '0xFF' is a valid signed Int8. It is questionable if we should
  allow this and if we should treat signed and unsigned integers differently depending on
  how they are spelled (decimal or hexadecimal).

* This patch will be further improved (Ex: will start finding overflows on Int64, better
  deal with '-128' after the negative integer literal patch is committed.)

Swift SVN r9226
2013-10-11 22:19:14 +00:00
John McCall
b880e60100 Remove SILFunctionTypeInfo in favor of SILFunctionType.
We still don't actually use this as a type, however.

Swift SVN r9091
2013-10-09 20:55:55 +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
c4abbec798 [SIL] Print the constant expression as part of the arithmetic overflow error message
Swift SVN r9035
2013-10-08 23:07:58 +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
Anna Zaks
a19d7569cc Introduce Builtin.staticReport, which allows compiler diagnostic reporting.
- Introduces the Builtin
- If the first parameter evaluates to '1', the dataflow diagnostics pass produces a diagnostic.
- The Builtin gets cleaned up before IRGen, but not before SIL serialization.

This patch also removes the current, overflow warning and XFAILs one of the tests. The other test is switched to use Builtin.staticReport.

TODO:
 - Utilize the other parameters to the builtin - the Message and IsError flag.
 - Use this Builtin within the stdlib.

Swift SVN r8939
2013-10-05 00:12:23 +00:00
Stephen Lin
a6108dbd48 Rename FunctionRefInst::getFunction() and BuiltinFunctionRefInst::getFunction() to FunctionRefInst::getReferencedFunction() and BuiltinFunctionRefInst::getReferencedFunction() to avoid shadowing SILInstruction::getFunction().
Swift SVN r8929
2013-10-04 20:26:41 +00:00
Anna Zaks
9bc9d87034 [SIL] More aggressive folding of struct_extract and tuple_extract
Swift SVN r8923
2013-10-04 17:56:04 +00:00
Anna Zaks
a194b88741 [SIL] Using SetVector (instead of set) might make the pass faster
Since we will be processing instructions in roughly the same order as they appear in the file.

Swift SVN r8922
2013-10-04 17:56:03 +00:00
Anna Zaks
e136245676 [SIL] Add integer overflow warnings on simple arithmetic to swift.
- Adds transparent attribute to add/mult/sub on integer types.
 - Fix a bug in ConstantPropagation that ensures that the propagation is triggered on StructExtract, when struct is simplified.
 - Slightly improve the error message.

Swift SVN r8825
2013-10-01 23:02:35 +00:00
Jordan Rose
e05c03d5bc Standardize terminology for "computed", "stored", "variable", and "property".
These are the terms sent out in the proposal last week and described in
StoredAndComputedVariables.rst.

variable
  anything declared with 'var'
member variable
  a variable inside a nominal type (may be an instance variable or not)
property
  another term for "member variable"
computed variable
  a variable with a custom getter or setter
stored variable
  a variable with backing storage; any non-computed variable

These terms pre-exist in SIL and IRGen, so I only attempted to solidify
their definitions. Other than the use of "field" for "tuple element",
none of these should be exposed to users.

field
  a tuple element, or
  the underlying storage for a stored variable in a struct or class
physical
  describes an entity whose value can be accessed directly
logical
  describes an entity whose value must be accessed through some accessor

Swift SVN r8698
2013-09-26 18:50:44 +00:00
Anna Zaks
7a1233be74 [SIL: CCP] Fix a bug that was preventing chaining constant propagation.
Swift SVN r8471
2013-09-19 23:41:02 +00:00
Anna Zaks
2f3664d9f7 [SIL CCP] fix typo
Swift SVN r7481
2013-08-22 22:10:37 +00:00
Chris Lattner
611b52f5d5 enhance constant folding to work with BoundGenericStruct's, fixing a crash
building the stdlib with -enable-definite-init.


Swift SVN r7450
2013-08-22 16:40:18 +00:00
Anna Zaks
b6caa9b716 [SIL CCP] Constant fold struct_extract.
Swift SVN r7446
2013-08-22 01:09:44 +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
Anna Zaks
ae7bf3dec0 [SIL CCP] Handle more arithmetic intrinsics with overflow and tuple_extract.
Test that we now can constant fold: 2 * (3 + 2) - 3.

Swift SVN r7347
2013-08-19 23:57:01 +00:00
Anna Zaks
ff3143a370 [analyzer] Eagerly DCE in ConstPropagation; teach about side-effect-free intrinsics.
It would be good if LLVM provided mechanism to find out which intrinsics do not have side effects.
Currently, just ensure that everything we might constant fold is in that category.

Swift SVN r7312
2013-08-17 00:41:37 +00:00
Anna Zaks
3ac84f3878 [SIL] Add getIntrinsicID(FuncDecl*), which lazyly looks up the llvm::IntrinsicID for it.
The cache is stored in the SILModule.
Add getIntrinsicID() as a member of BuiltinFunctionRefInst.
Test by using the new method in the CCP pass.

Swift SVN r7311
2013-08-17 00:41:30 +00:00
Stephen Lin
8d90466523 Reorganize SIL source tree: move lib/SIL/SILGen -> lib/SILGen, move lib/SIL/Passes -> lib/SILPasses, add lib/SILPasses/Utils
Swift SVN r7246
2013-08-14 23:47:29 +00:00