Skip to content

Commit 9758917

Browse files
committed
Fully outline all GlobalRefs
This is an alternative to #56714 that goes in the opposite direction - just outline all GlobalRefs during lowering. It is a lot simpler that #56714 at the cost of some size increase. Numbers: sys.so .ldata size: This PR: 159.8 MB Master: 158.9 MB I don't have numbers of #56714, because it's not fully complete. Additionally, it's possible that restricting GlobalRefs from arguments position would let us use a more efficient encoding in the future.
1 parent 5835c3b commit 9758917

File tree

6 files changed

+48
-73
lines changed

6 files changed

+48
-73
lines changed

Compiler/test/inference.jl

+4-5
Original file line numberDiff line numberDiff line change
@@ -3031,14 +3031,13 @@ end
30313031

30323032
# issue #28279
30333033
# ensure that lowering doesn't move these into statement position, which would require renumbering
3034-
using Base: +, -
3035-
function f28279(b::Bool)
3034+
@eval function f28279(b::Bool)
30363035
let i = 1
3037-
while i > b
3038-
i -= 1
3036+
while $(>)(i, b)
3037+
i = $(-)(i, 1)
30393038
end
30403039
if b end
3041-
return i + 1
3040+
return $(+)(i, 1)
30423041
end
30433042
end
30443043
code28279 = code_lowered(f28279, (Bool,))[1].code

src/ast.c

-37
Original file line numberDiff line numberDiff line change
@@ -181,42 +181,6 @@ static value_t fl_defined_julia_global(fl_context_t *fl_ctx, value_t *args, uint
181181
return (bpart != NULL && decode_restriction_kind(jl_atomic_load_relaxed(&bpart->restriction)) == BINDING_KIND_GLOBAL) ? fl_ctx->T : fl_ctx->F;
182182
}
183183

184-
static value_t fl_nothrow_julia_global(fl_context_t *fl_ctx, value_t *args, uint32_t nargs)
185-
{
186-
// tells whether a var is defined, in the sense that accessing it is nothrow
187-
// can take either a symbol or a module and a symbol
188-
jl_ast_context_t *ctx = jl_ast_ctx(fl_ctx);
189-
jl_module_t *mod = ctx->module;
190-
jl_sym_t *var = NULL;
191-
if (nargs == 1) {
192-
(void)tosymbol(fl_ctx, args[0], "nothrow-julia-global");
193-
var = scmsym_to_julia(fl_ctx, args[0]);
194-
}
195-
else {
196-
argcount(fl_ctx, "nothrow-julia-global", nargs, 2);
197-
value_t argmod = args[0];
198-
if (iscvalue(argmod) && cv_class((cvalue_t*)ptr(argmod)) == jl_ast_ctx(fl_ctx)->jvtype) {
199-
mod = *(jl_module_t**)cv_data((cvalue_t*)ptr(argmod));
200-
JL_GC_PROMISE_ROOTED(mod);
201-
} else {
202-
if (!iscons(argmod) || !issymbol(car_(argmod)) || scmsym_to_julia(fl_ctx, car_(argmod)) != jl_thismodule_sym) {
203-
lerrorf(fl_ctx, fl_ctx->ArgError, "nothrow-julia-global: Unknown globalref module kind");
204-
}
205-
}
206-
(void)tosymbol(fl_ctx, args[1], "nothrow-julia-global");
207-
var = scmsym_to_julia(fl_ctx, args[1]);
208-
}
209-
jl_binding_t *b = jl_get_module_binding(mod, var, 0);
210-
jl_binding_partition_t *bpart = jl_get_binding_partition(b, jl_current_task->world_age);
211-
jl_ptr_kind_union_t pku = jl_walk_binding_inplace(&b, &bpart, jl_current_task->world_age);
212-
if (!bpart)
213-
return fl_ctx->F;
214-
if (jl_bkind_is_some_guard(decode_restriction_kind(pku)))
215-
return fl_ctx->F;
216-
return (jl_bkind_is_some_constant(decode_restriction_kind(pku)) ?
217-
decode_restriction_value(pku) : jl_atomic_load_relaxed(&b->value)) != NULL ? fl_ctx->T : fl_ctx->F;
218-
}
219-
220184
// Used to generate a unique suffix for a given symbol (e.g. variable or type name)
221185
// first argument contains a stack of method definitions seen so far by `closure-convert` in flisp.
222186
// if the top of the stack is non-NIL, we use it to augment the suffix so that it becomes
@@ -288,7 +252,6 @@ static jl_value_t *scm_to_julia_(fl_context_t *fl_ctx, value_t e, jl_module_t *m
288252

289253
static const builtinspec_t julia_flisp_ast_ext[] = {
290254
{ "defined-julia-global", fl_defined_julia_global }, // TODO: can we kill this safepoint
291-
{ "nothrow-julia-global", fl_nothrow_julia_global },
292255
{ "current-julia-module-counter", fl_module_unique_name },
293256
{ "julia-scalar?", fl_julia_scalar },
294257
{ NULL, NULL }

src/jlfrontend.scm

-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131

3232
;; this is overwritten when we run in actual julia
3333
(define (defined-julia-global v) #f)
34-
(define (nothrow-julia-global v) #f)
3534

3635
;; parser entry points
3736

src/julia-syntax.scm

-1
Original file line numberDiff line numberDiff line change
@@ -4356,7 +4356,6 @@ f(x) = yt(x)
43564356

43574357
(define (valid-ir-argument? e)
43584358
(or (simple-atom? e)
4359-
(and (globalref? e) (nothrow-julia-global (cadr e) (caddr e)))
43604359
(and (pair? e)
43614360
(memq (car e) '(quote inert top core
43624361
slot static_parameter)))))

test/core.jl

+38-23
Original file line numberDiff line numberDiff line change
@@ -7434,6 +7434,7 @@ end
74347434
@test isa(Core.eval(@__MODULE__, :(Bar31062(()))), Bar31062)
74357435
@test precompile(identity, (Foo31062,))
74367436

7437+
using Core: SSAValue
74377438
ftype_eval = Ref(0)
74387439
FieldTypeA = String
74397440
FieldTypeE = UInt32
@@ -7457,27 +7458,41 @@ let fc = FieldConvert(1.0, [2.0], 0x3, 0x4, 0x5)
74577458
end
74587459
@test ftype_eval[] == 1
74597460
let code = code_lowered(FieldConvert)[1].code
7460-
local fc_global_ssa, sp1_ssa, apply_type_ssa, field_type_ssa,
7461-
field_type2_ssa, field_type4_ssa, field_type5_ssa,
7462-
slot_read_1, slot_read_2, slot_read_3, slot_read_4,
7463-
new_ssa
7464-
@test code[(fc_global_ssa = 1;)] == GlobalRef(@__MODULE__, :FieldConvert)
7465-
@test code[(sp1_ssa = 2;)] == Expr(:static_parameter, 1)
7466-
@test code[(apply_type_ssa = 3;)] == Expr(:call, GlobalRef(Core, :apply_type), Core.SSAValue(fc_global_ssa), GlobalRef(@__MODULE__, :FieldTypeA), Core.SSAValue(sp1_ssa))
7467-
@test code[(field_type_ssa = 4;)] == Expr(:call, GlobalRef(Core, :fieldtype), Core.SSAValue(apply_type_ssa), 1)
7468-
@test code[10] == Expr(:(=), Core.SlotNumber(10), Expr(:call, GlobalRef(Base, :convert), Core.SSAValue(field_type_ssa), Core.SlotNumber(10)))
7469-
@test code[(slot_read_1 = 11;)] == Core.SlotNumber(10)
7470-
@test code[(field_type2_ssa = 12;)] == Expr(:call, GlobalRef(Core, :fieldtype), Core.SSAValue(apply_type_ssa), 2)
7471-
@test code[18] == Expr(:(=), Core.SlotNumber(9), Expr(:call, GlobalRef(Base, :convert), Core.SSAValue(field_type2_ssa), Core.SlotNumber(9)))
7472-
@test code[(slot_read_2 = 19;)] == Core.SlotNumber(9)
7473-
@test code[(field_type4_ssa = 20;)] == Expr(:call, GlobalRef(Core, :fieldtype), Core.SSAValue(apply_type_ssa), 4)
7474-
@test code[26] == Expr(:(=), Core.SlotNumber(8), Expr(:call, GlobalRef(Base, :convert), Core.SSAValue(field_type4_ssa), Core.SlotNumber(8)))
7475-
@test code[(slot_read_3 = 27;)] == Core.SlotNumber(8)
7476-
@test code[(field_type5_ssa = 28;)] == Expr(:call, GlobalRef(Core, :fieldtype), Core.SSAValue(apply_type_ssa), 5)
7477-
@test code[34] == Expr(:(=), Core.SlotNumber(7), Expr(:call, GlobalRef(Base, :convert), Core.SSAValue(field_type5_ssa), Core.SlotNumber(7)))
7478-
@test code[(slot_read_4 = 35;)] == Core.SlotNumber(7)
7479-
@test code[(new_ssa = 36;)] == Expr(:new, Core.SSAValue(apply_type_ssa), Core.SSAValue(slot_read_1), Core.SSAValue(slot_read_2), Core.SlotNumber(4), Core.SSAValue(slot_read_3), Core.SSAValue(slot_read_4))
7480-
@test code[37] == Core.ReturnNode(Core.SSAValue(new_ssa))
7461+
calls = Vector{Pair{SSAValue, Expr}}(undef, 0)
7462+
for i = 1:length(code)
7463+
expr = code[i]
7464+
if Meta.isexpr(expr, :call) || (Meta.isexpr(expr, :(=)) && Meta.isexpr(expr.args[2], :call))
7465+
push!(calls, SSAValue(i)=>expr)
7466+
end
7467+
end
7468+
7469+
function is_globalref(arg, gr)
7470+
while isa(arg, SSAValue)
7471+
arg = code[arg.id]
7472+
end
7473+
arg == gr
7474+
end
7475+
7476+
# calls[1]
7477+
@test all(is_globalref.(calls[1][2].args[1:3], (GlobalRef(Core, :apply_type), GlobalRef(@__MODULE__, :FieldConvert), GlobalRef(@__MODULE__, :FieldTypeA))))
7478+
7479+
# calls[2]
7480+
@test all(is_globalref.(calls[2][2].args[1:1], (GlobalRef(Core, :fieldtype),)))
7481+
@test all(calls[2][2].args[2:3] .== (calls[1][1], 1))
7482+
7483+
# calls[3] - isa
7484+
7485+
# calls[4]
7486+
let calle = calls[4][2]
7487+
@test Meta.isexpr(calle, :(=))
7488+
call = calle.args[2]
7489+
@test is_globalref(call.args[1], GlobalRef(Base, :convert))
7490+
@test call.args[2] == calls[2][1]
7491+
end
7492+
7493+
# calls[5]
7494+
@test all(is_globalref.(calls[5][2].args[1:1], (GlobalRef(Core, :fieldtype),)))
7495+
@test all(calls[5][2].args[2:3] .== (calls[1][1], 2))
74817496
end
74827497

74837498
# Issue #32820
@@ -8155,7 +8170,7 @@ end
81558170
@test Core.Compiler.is_foldable(Base.infer_effects(length, (Core.SimpleVector,)))
81568171
@test Core.Compiler.is_foldable(Base.infer_effects(getindex, (Core.SimpleVector,Int)))
81578172

8158-
# Test that a nothrow-globalref doesn't get outlined during lowering
8173+
# Test that a the lowering of nothrow globalref
81598174
module WellKnownGlobal
81608175
global well_known = 1
81618176
end
@@ -8164,7 +8179,7 @@ macro insert_global()
81648179
end
81658180
check_globalref_lowering() = @insert_global
81668181
let src = code_lowered(check_globalref_lowering)[1]
8167-
@test length(src.code) == 2
8182+
@test length(src.code) == 4
81688183
end
81698184

81708185
# Test correctness of widen_diagonal

test/staged.jl

+6-6
Original file line numberDiff line numberDiff line change
@@ -270,12 +270,12 @@ end
270270

271271
# PR #23168
272272

273-
function f23168(a, x)
273+
@eval function f23168(a, x)
274274
push!(a, 1)
275275
if @generated
276-
:(y = x + x)
276+
:(y = $(+)(x, x))
277277
else
278-
y = 2x
278+
y = $(*)(2, x)
279279
end
280280
push!(a, y)
281281
if @generated
@@ -290,9 +290,9 @@ end
290290
let a = Any[]
291291
@test f23168(a, 3) == (6, Int)
292292
@test a == [1, 6, 3]
293-
@test occursin(" + ", string(code_lowered(f23168, (Vector{Any},Int))))
294-
@test occursin("2 * ", string(Base.uncompressed_ir(first(methods(f23168)))))
295-
@test occursin("2 * ", string(code_lowered(f23168, (Vector{Any},Int), generated=false)))
293+
@test occursin("(+)(", string(code_lowered(f23168, (Vector{Any},Int))))
294+
@test occursin("(*)(2", string(Base.uncompressed_ir(first(methods(f23168)))))
295+
@test occursin("(*)(2", string(code_lowered(f23168, (Vector{Any},Int), generated=false)))
296296
@test occursin("Base.add_int", string(code_typed(f23168, (Vector{Any},Int))))
297297
end
298298

0 commit comments

Comments
 (0)