Commit Graph

264 Commits

Author SHA1 Message Date
Ivan Kylchik a56b462eb6 [IR] Reuse IR utils functions to find methods of Any in interpreter 2023-07-24 17:20:39 +00:00
Ivan Kylchik 1eca5d1a3b [IR] Rewrite how interpreter works with the Unit result
This way interpreter works in more correct way. Every expression
produces some result, and if we don't need it, the interpreter will
discard it.
2023-07-24 17:20:39 +00:00
Ivan Kylchik 6204119219 [IR] Simplify logic of constructing unsigned number in interpreter
This way it is a little less "hacky". We are looking only for "data"
property, and as long as this property is declared in constructor, we
can safely assume we will get the correct one even if it is
renamed.
2023-07-24 17:20:39 +00:00
Ivan Kylchik d795ce3582 [IR] Simplify logic of interpretCall
Drop excess call of `callStack.loadState(it)` when store state for
extension receiver. When we interpret lambda with extension receiver,
this receiver will be actually represented as a value parameter,
and it required some additional processing. Apparently, after all
interpreter's refactorings, this does not matter anymore and excess
call can be dropped.
2023-07-24 17:20:39 +00:00
Ivan Kylchik 30c00f7983 [IR] Specify explicitly that classes from Java can be interpreted
In early prototypes of interpreter, it was easier to assume that
all classes from Java can be interpreted and fix something if not.
Check for Java declaration was done by checking that the package name is
starting with "java". But this is actually wrong and can lead to errors
when some code is declared in "java" something package, but is not from
Java stdlib.

#KT-60467 Fixed
2023-07-20 09:40:42 +00:00
Ivan Kylchik ca8db7d0b8 [IR] Move toIrConst method into common IrUtils file
This method is used not only in IR interpreter, so it makes sense to
move it.
2023-07-19 15:12:54 +00:00
Ivan Kylchik 4f92e7c48c [IR] Rename IrTypeTransformerVoid to IrTypeTransformer
This naming is more consistent with already existing
`IrElementTransformerVoid` and `IrElementTransformer`
2023-07-18 15:42:11 +00:00
Alexander Udalov 69059362b8 Fir2Ir: generate 'finally' block always with Unit type
This is important for IR lowerings like PolymorphicSignatureLowering
which are very sensitive about the correct types of expressions and
placement of coercions to Unit (KT-59218).

A boolean parameter to `insertImplicitCasts` is not the best solution to
ensure that coercion to Unit is added. The best solution would be to fix
the TODO and generate coercion to the block's type for the last
statement. But that will affect many other places and will need to be
done separately => KT-59781.

Code in IrInterpreter is uncommented to fix the FIR test
`compiler/testData/ir/interpreter/exceptions/tryFinally.kt`; otherwise
evaluation of the function `returnTryFinally` there crashes with
"NoSuchElementException: ArrayDeque is empty". No idea why this test
didn't fail for K1 though, since the created IR is exactly the same.

For some unknown reason this breaks WASM backend with K2, but not with
K1 => KT-59800.
2023-07-18 11:37:41 +00:00
Ivan Kylchik a4b8ab8199 [Native] Forbid to inline NaN values during const evaluation 2023-07-10 13:19:52 +00:00
Ivan Kylchik 31e75cc603 [JS_IR] Forbid to inline constants during const lowering 2023-07-06 11:00:14 +00:00
Ivan Kylchik d3d28783c5 [IR] Support const optimizations for JS backend 2023-07-06 11:00:14 +00:00
Ivan Kylchik d557bcaba4 [IR] Forbid unconditional interpretation of IrGetObjectValue
Earlier we always allowed to interpret `IrGetObjectValue` because
this value is used in const val getter. But now we do a special
check for such getter avoiding visit of `IrGetObjectValue` node.

#KT-59775 Fixed
2023-07-05 13:33:41 +00:00
Sergej Jaskiewicz 7b77ec9930 [IR] Reorder parameters in IrFactory#createFunction
This is to prepare for IrFactory auto-generation (KT-59308).
2023-06-28 10:13:09 +00:00
Svyatoslav Kuzmich 3e564236f9 [Wasm] Fix unsigned types in IR interpreter
Wasm kotlin.Any class has properties,
 additional filtering is required when searching for
 value class property
2023-06-25 10:18:51 +02:00
Ivan Kylchik bde6df8ca1 [IR] Correctly transform KCallable.name call to optimize it later
We need to transform call from `KFunction(N).name` to
`KFunction(N+1).name`. This way we keep IR correct and if something
goes wrong during interpretation, we still will have compilable code.

This commit suppose to fix three failing tests on Native aggregate
build:
1. `FirNativePartialLinkageTestGenerated.
testReferencingUnusableDeclarations`
2. `FirNativeCodegenBoxTestGenerated$Box$CallableReference$Bound.
testKCallableNameIntrinsic`
3. `FirNativeCodegenBoxTestNoPLGenerated$Box$CallableReference$Bound
.testKCallableNameIntrinsic`
2023-06-23 14:47:26 +00:00
Sergej Jaskiewicz f46761a241 [IR] Reorder parameters in IrFactory#createClass
This is to prepare for IrFactory auto-generation (KT-59308).
2023-06-23 13:25:34 +00:00
Ivan Kylchik 2115d55e1c [IR] Forbid to interpret expressions that involves unsigned numbers
We want to forbid expressions of type `Unsigned == Unsigned` or
similar. Working with unsigned numbers we can only inline them on the
call site or use in `String.plus` expression.

This commit fixes problem in test `unsignedTypesInAnnotations`. Here
interpreter thinks that expression `ONE_UINT != 1u` can be
evaluated, but fails to do it. This fail prevent us from inlining
`ONE_UINT` and on runtime we get error about missing getter.
Earlier this property would have been inlined by `ConstLowering`,
but we got rid of it.
2023-06-22 18:49:50 +00:00
Ivan Kylchik d1a54a522d [JVM IR] Drop ConstLowering
It basically can be replaced with IR interpreter. The only half-hack
was required in `PropertyReferenceLowering`. Const interpreter is
running before it, so we can't optimize some calls on const
properties that appear after this lowering. Solution is to inline
constants manually during property reference lowering.
2023-06-22 18:49:50 +00:00
Ivan Kylchik 3ce61f988f [IR] Replace mutable property in const transformer with data class 2023-06-14 19:02:39 +00:00
Ivan Kylchik 0e2b348fa0 [IR] Try to tun IrInterpreterKCallableNamePreprocessor only once
#KT-58923 Fixed
2023-06-14 19:02:39 +00:00
Ivan Kylchik a6a389933d [IR] Slightly simplify string concatenation folding
#KT-58923
2023-06-14 19:02:39 +00:00
Ivan Kylchik dd264cff50 [IR] Split const folding into necessary one and for optimizations only
In this commit we have a lot of change in test data. This was caused
by the way where we evaluate constants. We split constant evaluation
into two distinct parts: only necessary evaluations for `fir2ir`
(like const val and annotations) and optimizations for lowering.
Now we don't do all constant evaluation on `fir2ir`, but IR
dump is executed after this phase, so test data changed.

#KT-58923
2023-06-14 19:02:39 +00:00
Ivan Kylchik 8067df3c94 [IR] Combine IrInterpreterNameCheck with common one
This way we achieve faster compilation time. We want to traverse
IR tree as little as possible. If we add new checker than the time
to evaluate constants basically doubles.

#KT-58923
2023-06-14 19:02:39 +00:00
Ivan Kylchik e5ed1714d1 [IR] Unconditionally allow to interpret IrConst
We can interpret all const elements. It wasn't true on early
prototype phase because of some problems with unsigned numbers,
but right now it is correct to assume that all const are valid.

#KT-58923
2023-06-14 19:02:39 +00:00
Ivan Kylchik 6f4f06757a [IR] Add KType to IrBuiltins 2023-06-14 19:02:39 +00:00
Ivan Kylchik 0021a5f655 [K2] Save inlined java field into special InlineConstTracker 2023-06-14 19:02:39 +00:00
Ivan Kylchik 39242e517e [IR] Minor refactoring 2023-06-14 19:02:39 +00:00
Ivan Kylchik 2129938ef2 [IR] Properly handle interpretation errors if inside annotation class
#KT-58923
2023-06-14 19:02:38 +00:00
Ivan Kylchik ec4cf40f6f [IR] Allow to fold IrGetField expressions
#KT-58923
2023-06-14 19:02:38 +00:00
Ivan Kylchik b9856320cf [IR] Rename fqName to packageFqName for IrPackageFragment 2023-06-02 16:38:04 +00:00
Ivan Kylchik fe538a6174 [IR] Rewrite generator for interpreter map
1. Reuse `DisabledIdSignatureDescriptor` to create `IrBuiltIns`
2. Provide FQ names for types to ensure method find correctness
2023-06-01 13:01:24 +00:00
Ivan Kylchik 6501d0b743 [IR] Add CHECK_NOT_NULL const val field in BuiltInOperatorNames 2023-06-01 13:01:24 +00:00
Ivan Kylchik 40aea531e1 [IR] Drop treatFloatInSpecialWay interpreter's parameter
Replaced it with more common `platform` parameter.
2023-06-01 13:01:24 +00:00
Ivan Kylchik 19b0ff8fb4 [IR] Simplify calculateBuiltIns method in interpreter 2023-06-01 13:01:24 +00:00
Ivan Kylchik e5c45a4e51 [IR] Move extension val property into Utils for interpreter 2023-05-18 18:16:54 +00:00
Ivan Kylchik 70560fc3eb [IR] Handle each interpreter checker one by one
This wy we can interpret all expressions like `A::a.name` at first and
after that evaluate all complex expressions like
`A::a.name + A::b.name`.
2023-05-18 18:16:54 +00:00
Ivan Kylchik e58e20fc7b [IR] Add special preprocessors for ir interpreter
These preprocessors allow us to modify IR at first and only after that
try to evaluate. With this we can drop `KCallableNamePropertyLowering`.
2023-05-18 18:16:54 +00:00
Ivan Kylchik c8371a339f [IR] Create common interface for all interpreter checkers 2023-05-18 18:16:54 +00:00
Ivan Kylchik 82a111250b [IR] Drop excess containingDeclaration parameter from IR checker 2023-05-18 18:16:54 +00:00
Ivan Kylchik 1fd8ef801e [IR] Create special checker that will analyze name methods
We can insert all this logic into `IrCompileTimeChecker` but it is
a little bit specific and looks like it is nicer to just extract it.
2023-05-18 18:16:54 +00:00
Ivan Kylchik 104ac4bd69 [IR] Move IR interpreter transformers into separate package 2023-05-18 18:16:53 +00:00
Ivan Kylchik b779dc5d27 [IR] Drop all explicit usages of EvaluationMode from interpreter checker 2023-05-18 18:16:53 +00:00
Ivan Kylchik e3fa342d5c [IR] Drop WITH_ANNOTATIONS evaluation mode in interpreter
This is old mode, that was used mainly in prototype phase
2023-05-18 18:16:53 +00:00
Ivan Kylchik 535806d0ab [K2] Force interpretation of default argument for annotation's property
We miss these expression because they might be represented as
`IrGetFiled` values, but they still must be turned into `IrConst`.

#KT-58007 Fixed
2023-05-18 18:16:53 +00:00
Ivan Kylchik 3d4f3d2f57 [IR] Visit file annotations in IR interpreter
#KT-55866 Fixed
2023-05-15 08:32:37 +00:00
Ivan Kylchik d26e3871ba [K2] Support interpretation of values in type annotations
#KT-57812
2023-05-11 08:22:13 +00:00
Ivan Kylchik 55cac9caa6 [IR] Separate IrConstTransformer into two distinct classes
The first one `IrConstExpressionTransformer` is responsible for
transforming ordinary expressions and the second one
`IrConstAnnotationTransformer` is responsible for annotations.

#KT-57812
2023-05-11 08:22:13 +00:00
Ivan Kylchik 568b593da7 [IR] Switch forbidden list in ONLY_BUILTINS mode onto allowed one 2023-05-03 12:47:35 +00:00
Ivan Kylchik 7747643945 [IR] Filter chars and codePoints from allowed to interpret function
These two functions apparently are represented in Kotlin as methods
of `kotlin.String`. Because of that we accidentally treated them as
builtins.

To also minimize such cases, added filtration by return type. We are
allowing to interpret only these functions that have primitive or
unsigned return type.

#KT-57028 Fixed
2023-05-03 12:47:35 +00:00
Ivan Kylchik 81fe411ef6 [IR] Extract some common code in EvaluationMode into extension val 2023-05-03 12:47:34 +00:00