-
Notifications
You must be signed in to change notification settings - Fork 30
/
virtualprocess.jl
1540 lines (1364 loc) · 61.9 KB
/
virtualprocess.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
ToplevelErrorReport
An interface type of error reports that JET collects while top-level concrete interpration.
All `ToplevelErrorReport` should have the following fields:
- `file::String`: the path to the file containing the interpretation context
- `line::Int`: the line number in the file containing the interpretation context
See also: [`virtual_process`](@ref), [`ConcreteInterpreter`](@ref)
"""
abstract type ToplevelErrorReport end
# `ToplevelErrorReport` interface
function Base.getproperty(er::ToplevelErrorReport, sym::Symbol)
return if sym === :file
getfield(er, sym)::String
elseif sym === :line
getfield(er, sym)::Int
else
getfield(er, sym) # fallback
end
end
struct SyntaxErrorReport <: ToplevelErrorReport
err::Exception
file::String
line::Int
function SyntaxErrorReport(@nospecialize(err), file, line)
isa(err, Exception) || (err = ErrorException(err))
return new(err, file, line)
end
end
# don't show stacktrace for syntax errors
print_report(io::IO, report::SyntaxErrorReport) = showerror(io, report.err)
# wraps general errors from actual execution
struct ActualErrorWrapped <: ToplevelErrorReport
err
st::Base.StackTraces.StackTrace
file::String
line::Int
function ActualErrorWrapped(@nospecialize(err), st, file, line)
if isa(err, ErrorException) && startswith(err.msg, "syntax: ")
# forward syntax error
return SyntaxErrorReport(err.msg, file, line)
end
return new(err, st, file, line)
end
end
# TODO: add context information, i.e. during macroexpansion, defining something
print_report(io::IO, report::ActualErrorWrapped) = showerror(io, report.err, report.st)
struct DependencyError <: ToplevelErrorReport
pkg::String
dep::String
file::String
line::Int
end
function print_report(io::IO, report::DependencyError)
(; pkg, dep) = report
# NOTE this message should sync with `Base.require`
print(io, """
Package $pkg does not have $dep in its dependencies:
- You may have a partially installed environment. Try `Pkg.instantiate()`
to ensure all packages in the environment are installed.
- Or, if you have $pkg checked out for development and have
added $dep as a dependency but haven't updated your primary
environment's manifest file, try `Pkg.resolve()`.
- Otherwise you may need to report an issue with $pkg""")
end
# wraps an error that might happen because of inappropriate top-level code abstraction
struct MissingConcretization <: ToplevelErrorReport
err
st::Base.StackTraces.StackTrace
file::String
line::Int
end
function print_report(io::IO, report::MissingConcretization)
printstyled(io, "HINT: "; bold = true, color = HINT_COLOR)
printlnstyled(io, """
the following error happened mostly because of the missing concretization of global variables,
and this could be fixed with the `concretization_patterns` configuration.
Check https://aviatesk.github.io/JET.jl/dev/config/#JET.ToplevelConfig for the details.
---"""; color = HINT_COLOR)
showerror(io, report.err, report.st)
end
struct RecursiveIncludeErrorReport <: ToplevelErrorReport
duplicated_file::String
files::Vector{String}
file::String
line::Int
end
function print_report(io::IO, report::RecursiveIncludeErrorReport)
printstyled(io, "ERROR: "; bold = true, color = ERROR_COLOR)
println(io, "recursive `include` call detected:")
println(io, " ⚈ duplicated file: ", report.duplicated_file)
println(io, " ⚈ included files: ", join(report.files, ' '))
end
"""
Configurations for top-level analysis.
These configurations will be active for all the top-level entries explained in the
[top-level analysis entry points](@ref jetanalysis-toplevel-entry) section.
---
- `context::Module = Main` \\
The module context in which the top-level execution will be simulated.
This configuration can be useful when you just want to analyze a submodule, without
starting entire analysis from the root module.
For example, we can analyze `Base.Math` like below:
```julia-repl
julia> report_file(JET.fullbasepath("math.jl");
context = Base, # `Base.Math`'s root module
analyze_from_definitions = true, # there're only definitions in `Base`
)
```
Note that this module context will be virtualized by default so that JET can repeat analysis
in the same session without having "invalid redefinition of constant ..." error etc.
In other word, JET virtualize the module context of `context` and make sure the original
module context isn't polluted by JET.
---
- `target_defined_modules::Bool = false` \\
If `true`, automatically set the [`target_modules`](@ref result-config) configuration so that
JET filters out errors that are reported within modules that JET doesn't analyze directly.
---
- `analyze_from_definitions::Bool = false` \\
If `true`, JET will start analysis using signatures of top-level definitions (e.g. method signatures),
after the top-level interpretation has been done (unless no serious top-level error has
happened, like errors involved within a macro expansion).
This can be handy when you want to analyze a package, which usually contains only definitions
but not their usages (i.e. top-level callsites).
With this option, JET can enter analysis just with method or type definitions, and we don't
need to pass a file that uses the target package.
!!! warning
This feature is very experimental at this point, and you may face lots of false positive
errors, especially when trying to analyze a big package with lots of dependencies.
If a file that contains top-level callsites (e.g. `test/runtests.jl`) is available,
JET analysis using the file is generally preferred, since analysis entered from
concrete call sites will produce more accurate results than analysis entered from
(maybe not concrete-typed) method signatures.
Also see: [`report_file`](@ref), [`watch_file`](@ref)
---
- `concretization_patterns::Vector{Any} = Any[]` \\
Specifies a customized top-level code concretization strategy.
When analyzing a top-level code, JET first splits the entire code into appropriate units
of code (i.e. "code blocks"), and then iterate a virtual top-level code execution process
on each code block in order to simulate Julia's top-level code execution.
In the virtual code execution, JET will selectively interpret "top-level definitions"
(like a function definition), while it tries to avoid executing any other parts of code
including function calls that typically do a main computational task, leaving them to be
analyzed by the succeeding abstract interpretation based analysis.
However, currently, JET doesn't track "inter-block" level code dependencies, and therefore
the selective interpretation of top-level definitions may fail when it needs to use global
bindings defined in the other code blocks that have not been selected and actually
interpreted (i.e. "concretized") but left for abstract interpretation (i.e. "abstracted").
For example, the issue would happen if the expansion of a macro uses a global variable, e.g.:
> test/fixtures/concretization_patterns.jl
$(let
text = read(normpath(@__DIR__, "..", "..", "test", "fixtures", "concretization_patterns.jl"), String)
lines = split(text, '\n')
pushfirst!(lines, "```julia"); push!(lines, "```")
join(lines, "\n ")
end)
To circumvent this issue, JET offers this `concretization_patterns::Vector{<:Any}` configuration,
which allows us to customize JET's top-level code concretization strategy.
`concretization_patterns` specifies the _patterns of code_ that should be concretized.
To put in other word, when JET sees a code that matches any of code patterns specified by
this configuration, JET will try to interpret and concretize the code, regardless of
whether or not JET's default code selection logic decides to concretize it.
JET uses [MacroTools.jl's expression pattern match](https://fluxml.ai/MacroTools.jl/stable/pattern-matching/),
and we can specify whatever code pattern expected by `MacroTools.@capture` macro.
For example, in order to solve the issue explained above, we can have:
```julia
concretization_patterns = [:(const GLOBAL_CODE_STORE = Dict())]
```
Then `GLOBAL_CODE_STORE` will just be concretized and so any top-level error won't happen
at the macro expansion.
Since configuring `concretization_patterns` properly can be tricky, JET offers a logging
system that allows us to debug 's top-level code concretization plan. With the
`toplevel_logger` configuration with specifying the logging level to be above than
`$JET_LOGGER_LEVEL_DEBUG` ("debug") level, we can see:
- which code is matched with `concretization_patterns` and forcibly concretized
- which code is selected to be concretized by JET's default code selection logic:
where `t`-annotated statements are concretized while `f`-annotated statements are abstracted
```julia-repl
julia> report_file("test/fixtures/concretization_patterns.jl";
concretization_patterns = [:(const GLOBAL_CODE_STORE = Dict())],
toplevel_logger = IOContext(stdout, :JET_LOGGER_LEVEL => 1))
```
```
[toplevel-debug] virtualized the context of Main (took 0.003 sec)
[toplevel-debug] entered into test/fixtures/concretization_patterns.jl
[toplevel-debug] concretization pattern `const GLOBAL_CODE_STORE = Dict()` matched `const GLOBAL_CODE_STORE = Dict()` at test/fixtures/concretization_patterns.jl:2
[toplevel-debug] concretization plan at test/fixtures/concretization_patterns.jl:4:
1 f 1 ─ \$(Expr(:thunk, CodeInfo(
@ none within `top-level scope`
1 ─ return \$(Expr(:method, Symbol("@with_code_record")))
)))
2 t │ \$(Expr(:method, Symbol("@with_code_record")))
3 t │ %3 = Core.Typeof(var"@with_code_record")
4 t │ %4 = Core.svec(%3, Core.LineNumberNode, Core.Module, Core.Any)
5 t │ %5 = Core.svec()
6 t │ %6 = Core.svec(%4, %5, \$(QuoteNode(:(#= test/fixtures/concretization_patterns.jl:4 =#))))
7 t │ \$(Expr(:method, Symbol("@with_code_record"), :(%6), CodeInfo(
@ test/fixtures/concretization_patterns.jl:5 within `none`
1 ─ \$(Expr(:meta, :nospecialize, :(a)))
│ Base.setindex!(GLOBAL_CODE_STORE, a, __source__)
│ @ test/fixtures/concretization_patterns.jl:6 within `none`
│ %3 = esc(a)
└── return %3
)))
8 f └── return var"@with_code_record"
[toplevel-debug] concretization plan at test/fixtures/concretization_patterns.jl:11:
1 f 1 ─ \$(Expr(:thunk, CodeInfo(
@ none within `top-level scope`
1 ─ return \$(Expr(:method, :foo))
)))
2 t │ \$(Expr(:method, :foo))
3 t │ %3 = Core.Typeof(foo)
4 t │ %4 = Core.svec(%3, Core.Any)
5 t │ %5 = Core.svec()
6 t │ %6 = Core.svec(%4, %5, \$(QuoteNode(:(#= test/fixtures/concretization_patterns.jl:11 =#))))
7 t │ \$(Expr(:method, :foo, :(%6), CodeInfo(
@ test/fixtures/concretization_patterns.jl:11 within `none`
1 ─ %1 = identity(a)
└── return %1
)))
8 f └── return foo
[toplevel-debug] concretization plan at test/fixtures/concretization_patterns.jl:13:
1 f 1 ─ %1 = foo(10)
2 f └── return %1
[toplevel-debug] exited from test/fixtures/concretization_patterns.jl (took 0.032 sec)
```
Also see: the `toplevel_logger` section below, [`virtual_process`](@ref).
!!! note
[`report_package`](@ref) automatically sets this configuration as
```julia
concretization_patterns = [:(x_)]
```
meaning that it will concretize all top-level code included in a package being analyzed.
---
- `toplevel_logger::Union{Nothing,IO} = nothing` \\
If `IO` object is given, it will track JET's toplevel analysis.
Logging level can be specified with `$(repr(JET_LOGGER_LEVEL))` `IO` property.
Currently supported logging levels are either of $(JET_LOGGER_LEVELS_DESC).
Examples:
* logs into `stdout`
```julia-repl
julia> report_file(filename; toplevel_logger = stdout)
```
* logs into `io::IOBuffer` with "debug" logger level
```julia-repl
julia> report_file(filename; toplevel_logger = IOContext(io, $(repr(JET_LOGGER_LEVEL)) => $JET_LOGGER_LEVEL_DEBUG));
```
---
- `virtualize::Bool = true` \\
When `true`, JET will virtualize the given root module context.
This configuration is supposed to be used only for testing or debugging.
See [`virtualize_module_context`](@ref) for the internal.
---
"""
struct ToplevelConfig
pkgid::Union{Nothing,Base.PkgId}
context::Module
analyze_from_definitions::Bool
concretization_patterns::Vector{Any}
virtualize::Bool
toplevel_logger # ::Union{Nothing,IO}
function ToplevelConfig(
pkgid::Union{Nothing,Base.PkgId} = nothing;
context::Module = Main,
analyze_from_definitions::Bool = false,
concretization_patterns = Any[],
virtualize::Bool = true,
toplevel_logger::Union{Nothing,IO} = nothing,
__jetconfigs...)
concretization_patterns = Any[striplines(normalise(x)) for x in concretization_patterns]
for pat in default_concretization_patterns()
push!(concretization_patterns, striplines(normalise(pat)))
end
if isa(toplevel_logger, IO)
@assert jet_logger_level(toplevel_logger) in keys(JET_LOGGER_LEVELS) "toplevel_logger's $JET_LOGGER_LEVEL should be either of $JET_LOGGER_LEVELS_DESC"
end
return new(
pkgid,
context,
analyze_from_definitions,
concretization_patterns,
virtualize,
toplevel_logger)
end
end
default_concretization_patterns() = (
# `@enum` macro is fairly complex and especially the `let insts = (Any[ $(esc(typename))(v) for v in $values ]...,)`
# (adapted from https://github.com/JuliaLang/julia/blob/e5d7ef01b06f44cb75c871e54c81eb92eceae738/base/Enums.jl#L198)
# part requires the statement selection logic to choose statements that only
# pushes elements (`v`) into a slot representing an array (`insts`),
# which is very hard to be generalized;
# here we add them as pre-defined concretization patterns and make sure
# false positive top-level errors won't happen by the macro expansion
:(@enum(args__)), :(Base.@enum(args__)),
# concretize type aliases
# https://github.com/aviatesk/JET.jl/issues/237
:(const T_ = U_{P__}), :(T_ = U_{P__}),
)
@nospecialize
with_toplevel_logger(f, config::ToplevelConfig; kwargs...) =
with_toplevel_logger(f, config.toplevel_logger; kwargs...)
function with_toplevel_logger(f, io; filter=≥(DEFAULT_LOGGER_LEVEL), pre=identity)
isa(io, IO) || return false
level = jet_logger_level(io)
filter(level) || return
pre(io)
print(io, "[toplevel-$(JET_LOGGER_LEVELS[level])] ")
f(io)
end
@specialize
const Actual2Virtual = Pair{Module,Module}
"""
res::VirtualProcessResult
- `res.included_files::Set{String}`: files that have been analyzed
- `res.defined_modules::Set{Module}`: module contexts created while this top-level analysis
- `res.toplevel_error_reports::Vector{ToplevelErrorReport}`: toplevel errors found during the
text parsing or partial (actual) interpretation; these reports are "critical" and should
have precedence over `inference_error_reports`
- `res.inference_error_reports::Vector{InferenceErrorReport}`: possible error reports found
by `AbstractAnalyzer`
- `res.toplevel_signatures`: signatures of methods defined within the analyzed files
- `res.actual2virtual::$Actual2Virtual`: keeps actual and virtual module
"""
struct VirtualProcessResult
included_files::Set{String}
files_stack::Vector{String}
defined_modules::Set{Module}
toplevel_error_reports::Vector{ToplevelErrorReport}
inference_error_reports::Vector{InferenceErrorReport}
toplevel_signatures::Vector{Type}
actual2virtual::Union{Actual2Virtual,Nothing}
end
function VirtualProcessResult(actual2virtual, context)
return VirtualProcessResult(Set{String}(),
Vector{String}(),
Set{Module}((context,)),
ToplevelErrorReport[],
InferenceErrorReport[],
Type[],
actual2virtual,
)
end
"""
virtual_process(s::AbstractString,
filename::AbstractString,
analyzer::AbstractAnalyzer,
config::ToplevelConfig) -> res::VirtualProcessResult
Simulates Julia's toplevel execution and collects error points, and finally returns $(@doc VirtualProcessResult)
This function first parses `s::AbstractString` into `toplevelex::Expr` and then iterate the
following steps on each code block (`blk`) of `toplevelex`:
1. if `blk` is a `:module` expression, recursively enters analysis into an newly defined
virtual module
2. `lower`s `blk` into `:thunk` expression `lwr` (macros are also expanded in this step)
3. if the context module is virtualized, replaces self-references of the original context
module with virtualized one: see `fix_self_references`
4. `ConcreteInterpreter` partially interprets some statements in `lwr` that should not be
abstracted away (e.g. a `:method` definition); see also [`partially_interpret!`](@ref)
5. finally, `AbstractAnalyzer` analyzes the remaining statements by abstract interpretation
!!! warning
In order to process the toplevel code sequentially as Julia runtime does, `virtual_process`
splits the entire code, and then iterate a simulation process on each code block.
With this approach, we can't track the inter-code-block level dependencies, and so a
partial interpretation of toplevle definitions will fail if it needs an access to global
variables defined in other code blocks that are not interpreted but just abstracted.
We can circumvent this issue using JET's `concretization_patterns` configuration, which
allows us to customize JET's concretization strategy.
See [`ToplevelConfig`](@ref) for more details.
"""
function virtual_process(x::Union{AbstractString,Expr},
filename::AbstractString,
analyzer::AbstractAnalyzer,
config::ToplevelConfig)
if config.virtualize
actual = config.context
start = time()
virtual = virtualize_module_context(actual)
with_toplevel_logger(config) do @nospecialize(io)
sec = round(time() - start; digits = 3)
println(io, "virtualized the context of $actual (took $sec sec)")
end
actual2virtual = Actual2Virtual(actual, virtual)
context = virtual
else
actual2virtual = nothing
context = config.context
end
# Override the configurations for the virtualized module with that for the original package
# to prevent Preferences.jl from throwing errors. Without this, we will encounter
# the issues reported at https://github.com/aviatesk/JET.jl/issues/497:
# ```
# │ ArgumentError: Module XXX does not correspond to a loaded package!
# │ Stacktrace:
# │ [1] get_uuid(m::Module)
# │ @ Preferences ~/.julia/packages/Preferences/VmJXL/src/utils.jl:8
# │ [2] var"@load_preference"(__source__::LineNumberNode, __module__::Module, key::Any, default::Any)
# │ @ Preferences ~/.julia/packages/Preferences/VmJXL/src/Preferences.jl:45
# ```
# Note that we can't use the CassetteOverlay-like mechanism here for a cleaner
# implementation, since Preferences.jl might be called within `macroexpand` or `lower`
# of the main `_virtual_process!` loop, where we don't have control over execution.
pkgid = config.pkgid
old_main_uuid = Preferences.main_uuid[]
if pkgid !== nothing && pkgid.uuid !== nothing
Preferences.main_uuid[] = pkgid.uuid
end
res = VirtualProcessResult(actual2virtual, context)
try
_virtual_process!(res, x, filename, analyzer, config, context, #=pkg_mod_depth=#0)
finally
Preferences.main_uuid[] = old_main_uuid
end
# analyze collected signatures unless critical error happened
if config.analyze_from_definitions && isempty(res.toplevel_error_reports)
analyze_from_definitions!(analyzer, res, config)
end
# TODO move this aggregation to `analyze_from_definitions!`?
unique!(aggregation_policy(analyzer), res.inference_error_reports)
return res
end
"""
virtualize_module_context(actual::Module)
HACK to return a module where the context of `actual` is virtualized.
The virtualization will be done by 2 steps below:
1. loads the module context of `actual` into a sandbox module, and export the whole context from there
2. then uses names exported from the sandbox
This way, JET's runtime simulation in the virtual module context will be able to define
a name that is already defined in `actual` without causing
"cannot assign a value to variable ... from module ..." error, etc.
It allows JET to virtualize the context of already-existing module other than `Main`.
!!! warning "TODO"
Currently this function relies on `Base.names`, and thus it can't restore the `using`ed
names.
"""
function virtualize_module_context(actual::Module)
modpath = split_module_path(actual)
if length(modpath) ≥ 2
if modpath[1] === :Main && modpath[2] === :anonymous
error("can't virtualize an anonymous module")
end
end
virtual = gen_virtual_module(actual)
sandbox = gen_virtual_module(actual; name = :JETSandboxModule)
uex = Expr(:(:), Expr(:., :., :., modpath...))
usage = Expr(:using, uex)
exprt = Expr(:export)
unames = uex.args
enames = exprt.args
for n in names(actual; all = true, imported = true)
isdefined(sandbox, n) && continue
isdefined(actual, n) && push!(unames, Expr(:., n)) # an exported name can be undefined, and `using` of it will throw otherwise
push!(enames, n)
end
Core.eval(sandbox, usage)
Core.eval(sandbox, exprt)
usage = Expr(:using, Expr(:., :., :., split_module_path(sandbox)...))
Core.eval(virtual, usage)
return virtual
end
const VIRTUAL_MODULE_NAME = :JETVirtualModule
gen_virtual_module(root = Main; name = VIRTUAL_MODULE_NAME) =
Core.eval(root, :(module $(gensym(name)) end))::Module
# NOTE when `@generated` function has been defined, signatures of both its entry and
# generator should have been collected, and we will just analyze them separately
# if code generation has failed given the entry method signature, the overload of
# `InferenceState(..., ::AbstractAnalyzer)` will collect `GeneratorErrorReport`
function analyze_from_definitions!(analyzer::AbstractAnalyzer, res::VirtualProcessResult, config::ToplevelConfig)
succeeded = Ref(0)
start = time()
n = length(res.toplevel_signatures)
state = AnalyzerState(analyzer)
oldworld = state.world
new_world = get_world_counter()
state.world = new_world
if analyzer isa JETAnalyzer && analyzer.report_pass === BasicPass()
analyzer = JETAnalyzer(state, DefinitionAnalysisPass(), JETAnalyzerConfig(analyzer))
else
analyzer = AbstractAnalyzer(analyzer, state)
end
for (i, tt) in enumerate(res.toplevel_signatures)
match = Base._which(tt;
# NOTE use the latest world counter with `method_table(analyzer)` unwrapped,
# otherwise it may use a world counter when this method isn't defined yet
method_table=unwrap_method_table(method_table(analyzer)),
world=new_world,
raise=false)
if match !== nothing
succeeded[] += 1
with_toplevel_logger(config; pre=clearline) do @nospecialize(io)
(i == n ? println : print)(io, "analyzing from top-level definitions ($(succeeded[])/$n)")
end
analyzer, result = analyze_method_signature!(analyzer,
match.method, match.spec_types, match.sparams)
reports = get_reports(analyzer, result)
append!(res.inference_error_reports, reports)
continue
end
# something went wrong
with_toplevel_logger(config; filter=≥(JET_LOGGER_LEVEL_DEBUG), pre=clearline) do @nospecialize(io)
println(io, "couldn't find a single method matching the signature `", tt, "`")
end
end
state.world = oldworld
with_toplevel_logger(config) do @nospecialize(io)
sec = round(time() - start; digits = 3)
println(io, "analyzed $(succeeded[]) top-level definitions (took $sec sec)")
end
return nothing
end
clearline(io) = print(io, '\r')
function _virtual_process!(res::VirtualProcessResult,
s::AbstractString,
filename::AbstractString,
analyzer::AbstractAnalyzer,
config::ToplevelConfig,
context::Module,
pkg_mod_depth::Int)
start = time()
with_toplevel_logger(config) do @nospecialize(io)
println(io, "entered into $filename")
end
push!(res.included_files, filename)
push!(res.files_stack, filename)
s = String(s)::String
toplevelex = Base.parse_input_line(s; filename)
if isexpr(toplevelex, (:error, :incomplete))
# if there's any syntax error, try to identify all the syntax error location
report_syntax_errors!(res, s, filename)
elseif isnothing(toplevelex)
# just return if there is nothing to analyze
else
@assert isexpr(toplevelex, :toplevel)
_virtual_process!(res, toplevelex, filename, analyzer, config, context, pkg_mod_depth)
end
pop!(res.files_stack)
with_toplevel_logger(config) do @nospecialize(io)
sec = round(time() - start; digits = 3)
println(io, " exited from $filename (took $sec sec)")
end
return res
end
function _virtual_process!(res::VirtualProcessResult,
toplevelex::Expr,
filename::AbstractString,
analyzer::AbstractAnalyzer,
config::ToplevelConfig,
context::Module,
pkg_mod_depth::Int,
force_concretize::Bool = false)
local lnnref = Ref(LineNumberNode(0, filename))
function err_handler(@nospecialize(err), st)
local report = is_missing_concretization(err) ?
MissingConcretization(err, st, filename, lnnref[].line) :
ActualErrorWrapped(err, st, filename, lnnref[].line)
push!(res.toplevel_error_reports, report)
return nothing
end
function macroexpand_with_err_handling(mod::Module, x::Expr)
# `scrub_offset = 4` corresponds to `with_err_handling` -> `f` -> `macroexpand` -> kwfunc (`macroexpand`)
return with_err_handling(err_handler, #=scrub_offset=#4) do
# XXX we want to non-recursive, sequential partial macro expansion here, which allows
# us to collect more fine-grained error reports within macro expansions
# but it can lead to invalid macro hygiene escaping because of https://github.com/JuliaLang/julia/issues/20241
return macroexpand(mod, x; recursive = true #= but want to use `false` here =#)
end
end
function eval_with_err_handling(mod::Module, x::Expr)
# `scrub_offset = 3` corresponds to `with_err_handling` -> `f` -> `Core.eval`
return with_err_handling(err_handler, #=scrub_offset=#3) do
return Core.eval(mod, x)
end
end
function lower_with_err_handling(mod::Module, x::Expr)
# `scrub_offset = 3` corresponds to `with_err_handling` -> `f` -> `lower`
return with_err_handling(err_handler, #=scrub_offset=#3) do
lwr = lower(mod, x)
# here we should capture syntax errors found during lowering
if isexpr(lwr, :error)
msg = first(lwr.args)
push!(res.toplevel_error_reports, SyntaxErrorReport(lazy"syntax: $msg", filename, lnnref[].line))
return nothing
end
return lwr
end
end
local dependencies = Set{Symbol}()
function usemodule_with_err_handling(mod::Module, ex::Expr)
# TODO recursive analysis on dependencies?
pkgid = config.pkgid
if pkgid !== nothing && !isexpr(ex, :export)
module_usage = pattern_match_module_usage(ex)
(; modpath) = module_usage
dep = first(modpath)::Symbol
if !(dep === :. || # relative module doesn't need to be fixed
dep === :Base || dep === :Core) # modules available by default
if dep === Symbol(pkgid.name)
# it's somehow allowed to use the package itself without the relative module path,
# so we need to special case it and fix it to use the relative module path
for _ = 1:pkg_mod_depth
pushfirst!(modpath, :.)
end
else
if dep ∉ dependencies
depstr = String(dep)
depid = Base.identify_package(pkgid, depstr)
if depid === nothing
# IDEA better message in a case of `any(m::Module->dep===nameof(m), res.defined_modules))`?
local report = DependencyError(pkgid.name, depstr, filename, lnnref[].line)
push!(res.toplevel_error_reports, report)
return nothing
end
require_ex = :(const $dep = $require_pkg($depid))
# TODO better handling of loading errors that may happen here
require_res = with_err_handling(err_handler, #=scrub_offset=#3) do
return Core.eval(mod, require_ex)
end
isnothing(require_res) && return nothing
push!(dependencies, dep)
end
pushfirst!(modpath, :.)
end
fixed_module_usage = ModuleUsage(module_usage; modpath)
ex = form_module_usage(fixed_module_usage)
end
end
# `scrub_offset = 3` corresponds to `with_err_handling` -> `f` -> `Core.eval`
return with_err_handling(err_handler, #=scrub_offset=#3) do
return Core.eval(mod, ex)
end
end
# transform, and then analyze sequentially
# IDEA the following code has some of duplicated work with `JuliaInterpreter.ExprSpliter` and we may want to factor them out
exs = push_vex_stack!(VExpr[], toplevelex, force_concretize)
while !isempty(exs)
(; x, force_concretize) = pop!(exs)
# with_toplevel_logger(config; filter=≥(JET_LOGGER_LEVEL_DEBUG)) do @nospecialize(io)
# println(io, "analyzing ", x)
# end
# update line info
if isa(x, LineNumberNode)
lnnref[] = x
continue
end
# apply user-specified concretization strategy, which is configured as expression
# pattern match on surface level AST code representation; if any of the specified
# patterns matches `x`, JET just concretizes everything involved with it
# since patterns are expected to work on surface level AST, we should configure it
# here before macro expansion and lowering
if !force_concretize
for pat in config.concretization_patterns
if @capture(x, $pat)
with_toplevel_logger(config; filter=≥(JET_LOGGER_LEVEL_DEBUG)) do @nospecialize(io)
line, file = lnnref[].line, lnnref[].file
x′ = striplines(normalise(x))
println(io, "concretization pattern `$pat` matched `$x′` at $file:$line")
end
force_concretize = true
break
end
end
end
# although we will lower `x` after special-handling `:toplevel` and `:module` expressions,
# expand `macrocall`s here because macro can arbitrarily generate those expressions
if isexpr(x, :macrocall)
newx = macroexpand_with_err_handling(context, x)
# if any error happened during macro expansion, bail out now and continue
isnothing(newx) && continue
# special case and flatten the resulting expression expanded from `@doc` macro
# the macro expands to a block expression and so it makes it difficult to specify
# concretization pattern correctly since `@doc` macro is attached implicitly
if first(x.args) === GlobalRef(Core, Symbol("@doc"))
# `@doc` macro usually produces :block expression, but may also produce :toplevel
# one when attached to a module expression
@assert isexpr(newx, :block) || isexpr(newx, :toplevel)
push_vex_stack!(exs, newx::Expr, force_concretize)
else
push!(exs, VExpr(newx, force_concretize))
end
continue
end
# flatten container expression
if isexpr(x, :toplevel)
push_vex_stack!(exs, x, force_concretize)
continue
end
# handle `:module` definition and module usage;
# should happen here because modules need to be loaded sequentially while
# "toplevel definitions" inside of the loaded modules shouldn't be evaluated in a
# context of `context` module
if isexpr(x, :module)
newblk = x.args[3]
@assert isexpr(newblk, :block)
newtoplevelex = Expr(:toplevel, newblk.args...)
x.args[3] = Expr(:block) # empty module's code body
newcontext = eval_with_err_handling(context, x)
isnothing(newcontext) && continue # error happened, e.g. duplicated naming
newcontext = newcontext::Module
push!(res.defined_modules, newcontext)
_virtual_process!(res, newtoplevelex, filename, analyzer, config, newcontext,
pkg_mod_depth+1, force_concretize)
continue
end
# can't wrap `:global` declaration into a block
if isexpr(x, :global)
eval_with_err_handling(context, x)
continue
end
blk = Expr(:block, lnnref[], x) # attach current line number info
lwr = lower_with_err_handling(context, blk)
isnothing(lwr) && continue # error happened during lowering
isexpr(lwr, :thunk) || continue # literal
src = first((lwr::Expr).args)::CodeInfo
fix_self_references!(res.actual2virtual, src)
interp = ConcreteInterpreter(filename, lnnref[], usemodule_with_err_handling,
context, analyzer, config, res, pkg_mod_depth)
if force_concretize
JuliaInterpreter.finish!(interp, Frame(context, src), true)
continue
end
concretized = partially_interpret!(interp, context, src)
# bail out if nothing to analyze (just a performance optimization)
all(concretized) && continue
analyzer = AbstractAnalyzer(analyzer, concretized, context)
_, result = analyze_toplevel!(analyzer, src)
append!(res.inference_error_reports, get_reports(analyzer, result)) # collect error reports
end
return res
end
function require_pkg(pkg::Base.PkgId)
@lock Base.require_lock begin
return Base._require_prelocked(pkg)
end
end
struct VExpr
x
force_concretize::Bool
VExpr(@nospecialize(x), force_concretize::Bool) = new(x, force_concretize)
end
function push_vex_stack!(exs::Vector{VExpr}, newex::Expr, force_concretize::Bool)
nargs = length(newex.args)
for i in 0:(nargs-1)
push!(exs, VExpr(newex.args[nargs-i], force_concretize))
end
return exs
end
function split_module_path(m::Module)
ret = Symbol[]
split_module_path!(m, ret)
return ret
end
function split_module_path!(m::Module, ret)
mp = parentmodule(m)
if m === Main || m === Base || m === Core || mp === m
push!(ret, nameof(m))
return
end
split_module_path!(mp, ret)
push!(ret, nameof(m))
end
struct ModuleUsage
head::Symbol
modpath::Vector{Any}
namepath::Union{Vector{Any},Nothing}
alias::Union{Symbol,Nothing}
end
function ModuleUsage(m::ModuleUsage;
head::Symbol = m.head,
modpath::Vector{Any} = m.modpath,
namepath::Union{Vector{Any},Nothing} = m.namepath,
alias::Union{Symbol,Nothing} = m.alias)
return ModuleUsage(head, modpath, namepath, alias)
end
function pattern_match_module_usage(usage::Expr)
modpath = namepath = alias = nothing
if @capture(usage, import modpath__)
head = :import
elseif @capture(usage, using modpath__)
head = :using
elseif @capture(usage, import modpath__: namepath__)
head = :import
elseif @capture(usage, using modpath__: namepath__)
head = :using
elseif @capture(usage, import modpath__ as alias_)
head = :import
elseif @capture(usage, import modpath__: namepath__ as alias_)
head = :import
elseif @capture(usage, using modpath__: namepath__ as alias_)
head = :using
else
error(lazy"unexpected module usage found: $usage")
end
return ModuleUsage(head, modpath::Vector{Any}, namepath::Union{Nothing,Vector{Any}}, alias::Union{Nothing,Symbol})
end
function form_module_usage(moduleusage::ModuleUsage)
(; head, modpath, namepath, alias) = moduleusage
if isa(alias, Symbol)
if !isnothing(namepath)
return form_module_usage_alias(head, modpath, namepath, alias)
else
return form_module_import_alias(modpath, alias)
end
elseif !isnothing(namepath)
return form_module_usage_specific(head, modpath, namepath)
end
return form_module_usage(head, modpath)
end
# using A.B.C
form_module_usage(head::Symbol, modpath::Vector{Any}) =
Expr(head, Expr(:., modpath...))
# using A.B.C: abc
form_module_usage_specific(head::Symbol, modpath::Vector{Any}, namepath::Vector{Any}) =
Expr(head, Expr(:(:), Expr(:., modpath...), Expr(:., namepath...)))
# using A.B.C: abc as abc′
form_module_usage_alias(head::Symbol, modpath::Vector{Any}, namepath::Vector{Any}, alias::Symbol) =
Expr(head, Expr(:(:), Expr(:., modpath...), Expr(:as, Expr(:., namepath...), alias)))
# import A.B.C as abc
form_module_import_alias(modpath::Vector{Any}, alias::Symbol) =
Expr(:import, Expr(:as, Expr(:., modpath...), alias))
# if virtualized, replace self references of `actualmod` with `virtualmod` (as is)
fix_self_references!(::Nothing, @nospecialize(x)) = x
function fix_self_references!((actualmod, virtualmod)::Actual2Virtual, @nospecialize(x))
actualmodsym = Symbol(actualmod)
virtualmodsyms = split_module_path(virtualmod)
function any_self(modpath::Vector{Any})
for x in modpath
if x === virtualmod # postwalk, so we compare to `virtualmod`
return true
end
end
return false
end
# we can't use `Module` object in module usage expression, so we need to replace it with
# its symbolic representation
function fix_self(modpath::Vector{Any})
ret = Any[]
for x in modpath
if x === virtualmod # postwalk, so we compare to `virtualmod`
push!(ret, virtualmodsyms...)
else
push!(ret, x)
end
end
return ret
end
function fix_self_reference_simple(usage::Expr)
if isexpr(usage, :export)
return usage
end
module_usage = pattern_match_module_usage(usage)
any_self(module_usage.modpath) || return usage
fixed_module_usage = ModuleUsage(module_usage; modpath = fix_self(module_usage.modpath))
return form_module_usage(fixed_module_usage)
end
function fix_self_reference(usage::Expr)
ret = Expr(usage.head)
for simple in to_simple_module_usages(usage)
fixed = fix_self_reference_simple(simple)
@assert usage.head === fixed.head === simple.head && length(fixed.args) == 1
push!(ret.args, first(fixed.args))
end
return ret
end
return postwalk_and_transform!(x) do @nospecialize(xx), scope::Vector{Symbol}
if ismoduleusage(xx)
return fix_self_reference(xx)
elseif xx === actualmodsym
return virtualmod
end
return xx
end
end
function postwalk_and_transform!(f, x, scope = Symbol[])
inner(@nospecialize(x)) = postwalk_and_transform!(f, x, scope)
return walk_and_transform!(x, inner, f, scope)
end
function prewalk_and_transform!(f, x, scope::Vector{Symbol} = Symbol[])
inner(@nospecialize(x)) = prewalk_and_transform!(f, x, scope)
outer(@nospecialize(x), scope::Vector{Symbol}) = x
return walk_and_transform!(f(x, scope), inner, outer, scope)
end
function walk_and_transform!(@nospecialize(x), inner, outer, scope::Vector{Symbol})
if isa(x, GotoIfNot)
push!(scope, :gotoifnot)
cond = inner(walk_and_transform!(x.cond, inner, outer, scope))
dest = inner(walk_and_transform!(x.dest, inner, outer, scope))
x = GotoIfNot(cond, dest)
pop!(scope)
elseif isa(x, Expr)
push!(scope, x.head)
for i = 1:length(x.args)
x.args[i] = inner(walk_and_transform!(x.args[i], inner, outer, scope))
end
pop!(scope)
elseif isa(x, CodeInfo)
for i = 1:length(x.code)