Skip to content

Commit 7ee3ba9

Browse files
authored
Move EffectsOverride to expr.jl (#56187)
It makes sense that we originally added this to the compiler, but these annotations are really a runtime feature that the compiler simply reads to allow it to make additional assumptions. The runtime should not semantically depend on the compiler for this, so move these definitions to expr.jl. The practical effect of this right now is that Base gains a second copy of this code. Post #56128, the compiler will use the Base copy of this. Split out from #56128.
1 parent 222cde9 commit 7ee3ba9

File tree

6 files changed

+79
-78
lines changed

6 files changed

+79
-78
lines changed

base/compiler/compiler.jl

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -38,47 +38,6 @@ convert(::Type{T}, x::T) where {T} = x
3838
# Note that `@assume_effects` is available only after loading namedtuple.jl.
3939
abstract type MethodTableView end
4040
abstract type AbstractInterpreter end
41-
struct EffectsOverride
42-
consistent::Bool
43-
effect_free::Bool
44-
nothrow::Bool
45-
terminates_globally::Bool
46-
terminates_locally::Bool
47-
notaskstate::Bool
48-
inaccessiblememonly::Bool
49-
noub::Bool
50-
noub_if_noinbounds::Bool
51-
consistent_overlay::Bool
52-
nortcall::Bool
53-
end
54-
function EffectsOverride(
55-
override::EffectsOverride =
56-
EffectsOverride(false, false, false, false, false, false, false, false, false, false, false);
57-
consistent::Bool = override.consistent,
58-
effect_free::Bool = override.effect_free,
59-
nothrow::Bool = override.nothrow,
60-
terminates_globally::Bool = override.terminates_globally,
61-
terminates_locally::Bool = override.terminates_locally,
62-
notaskstate::Bool = override.notaskstate,
63-
inaccessiblememonly::Bool = override.inaccessiblememonly,
64-
noub::Bool = override.noub,
65-
noub_if_noinbounds::Bool = override.noub_if_noinbounds,
66-
consistent_overlay::Bool = override.consistent_overlay,
67-
nortcall::Bool = override.nortcall)
68-
return EffectsOverride(
69-
consistent,
70-
effect_free,
71-
nothrow,
72-
terminates_globally,
73-
terminates_locally,
74-
notaskstate,
75-
inaccessiblememonly,
76-
noub,
77-
noub_if_noinbounds,
78-
consistent_overlay,
79-
nortcall)
80-
end
81-
const NUM_EFFECTS_OVERRIDES = 11 # sync with julia.h
8241

8342
# essential files and libraries
8443
include("essentials.jl")

base/compiler/effects.jl

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -355,36 +355,5 @@ function decode_effects(e::UInt32)
355355
_Bool((e >> 14) & 0x01))
356356
end
357357

358-
function encode_effects_override(eo::EffectsOverride)
359-
e = 0x0000
360-
eo.consistent && (e |= (0x0001 << 0))
361-
eo.effect_free && (e |= (0x0001 << 1))
362-
eo.nothrow && (e |= (0x0001 << 2))
363-
eo.terminates_globally && (e |= (0x0001 << 3))
364-
eo.terminates_locally && (e |= (0x0001 << 4))
365-
eo.notaskstate && (e |= (0x0001 << 5))
366-
eo.inaccessiblememonly && (e |= (0x0001 << 6))
367-
eo.noub && (e |= (0x0001 << 7))
368-
eo.noub_if_noinbounds && (e |= (0x0001 << 8))
369-
eo.consistent_overlay && (e |= (0x0001 << 9))
370-
eo.nortcall && (e |= (0x0001 << 10))
371-
return e
372-
end
373-
374-
function decode_effects_override(e::UInt16)
375-
return EffectsOverride(
376-
!iszero(e & (0x0001 << 0)),
377-
!iszero(e & (0x0001 << 1)),
378-
!iszero(e & (0x0001 << 2)),
379-
!iszero(e & (0x0001 << 3)),
380-
!iszero(e & (0x0001 << 4)),
381-
!iszero(e & (0x0001 << 5)),
382-
!iszero(e & (0x0001 << 6)),
383-
!iszero(e & (0x0001 << 7)),
384-
!iszero(e & (0x0001 << 8)),
385-
!iszero(e & (0x0001 << 9)),
386-
!iszero(e & (0x0001 << 10)))
387-
end
388-
389358
decode_statement_effects_override(ssaflag::UInt32) =
390359
decode_effects_override(UInt16((ssaflag >> NUM_IR_FLAGS) & (1 << NUM_EFFECTS_OVERRIDES - 1)))

base/experimental.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ macro consistent_overlay(mt, def)
420420
inner = Base.unwrap_macrocalls(def)
421421
is_function_def(inner) || error("@consistent_overlay requires a function definition")
422422
overlay_def!(mt, inner)
423-
override = Core.Compiler.EffectsOverride(; consistent_overlay=true)
423+
override = Base.EffectsOverride(; consistent_overlay=true)
424424
Base.pushmeta!(def::Expr, Base.form_purity_expr(override))
425425
return esc(def)
426426
end

base/expr.jl

Lines changed: 76 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -757,7 +757,7 @@ macro assume_effects(args...)
757757
return esc(pushmeta!(lastex::Expr, form_purity_expr(override)))
758758
elseif isexpr(lastex, :macrocall) && lastex.args[1] === Symbol("@ccall")
759759
lastex.args[1] = GlobalRef(Base, Symbol("@ccall_effects"))
760-
insert!(lastex.args, 3, Core.Compiler.encode_effects_override(override))
760+
insert!(lastex.args, 3, encode_effects_override(override))
761761
return esc(lastex)
762762
end
763763
override′ = compute_assumed_setting(override, lastex)
@@ -784,7 +784,49 @@ function compute_assumed_settings(settings)
784784
return override
785785
end
786786

787-
using Core.Compiler: EffectsOverride
787+
struct EffectsOverride
788+
consistent::Bool
789+
effect_free::Bool
790+
nothrow::Bool
791+
terminates_globally::Bool
792+
terminates_locally::Bool
793+
notaskstate::Bool
794+
inaccessiblememonly::Bool
795+
noub::Bool
796+
noub_if_noinbounds::Bool
797+
consistent_overlay::Bool
798+
nortcall::Bool
799+
end
800+
801+
function EffectsOverride(
802+
override::EffectsOverride =
803+
EffectsOverride(false, false, false, false, false, false, false, false, false, false, false);
804+
consistent::Bool = override.consistent,
805+
effect_free::Bool = override.effect_free,
806+
nothrow::Bool = override.nothrow,
807+
terminates_globally::Bool = override.terminates_globally,
808+
terminates_locally::Bool = override.terminates_locally,
809+
notaskstate::Bool = override.notaskstate,
810+
inaccessiblememonly::Bool = override.inaccessiblememonly,
811+
noub::Bool = override.noub,
812+
noub_if_noinbounds::Bool = override.noub_if_noinbounds,
813+
consistent_overlay::Bool = override.consistent_overlay,
814+
nortcall::Bool = override.nortcall)
815+
return EffectsOverride(
816+
consistent,
817+
effect_free,
818+
nothrow,
819+
terminates_globally,
820+
terminates_locally,
821+
notaskstate,
822+
inaccessiblememonly,
823+
noub,
824+
noub_if_noinbounds,
825+
consistent_overlay,
826+
nortcall)
827+
end
828+
829+
const NUM_EFFECTS_OVERRIDES = 11 # sync with julia.h
788830

789831
function compute_assumed_setting(override::EffectsOverride, @nospecialize(setting), val::Bool=true)
790832
if isexpr(setting, :call) && setting.args[1] === :(!)
@@ -826,9 +868,40 @@ function compute_assumed_setting(override::EffectsOverride, @nospecialize(settin
826868
return nothing
827869
end
828870

871+
function encode_effects_override(eo::EffectsOverride)
872+
e = 0x0000
873+
eo.consistent && (e |= (0x0001 << 0))
874+
eo.effect_free && (e |= (0x0001 << 1))
875+
eo.nothrow && (e |= (0x0001 << 2))
876+
eo.terminates_globally && (e |= (0x0001 << 3))
877+
eo.terminates_locally && (e |= (0x0001 << 4))
878+
eo.notaskstate && (e |= (0x0001 << 5))
879+
eo.inaccessiblememonly && (e |= (0x0001 << 6))
880+
eo.noub && (e |= (0x0001 << 7))
881+
eo.noub_if_noinbounds && (e |= (0x0001 << 8))
882+
eo.consistent_overlay && (e |= (0x0001 << 9))
883+
eo.nortcall && (e |= (0x0001 << 10))
884+
return e
885+
end
886+
887+
function decode_effects_override(e::UInt16)
888+
return EffectsOverride(
889+
!iszero(e & (0x0001 << 0)),
890+
!iszero(e & (0x0001 << 1)),
891+
!iszero(e & (0x0001 << 2)),
892+
!iszero(e & (0x0001 << 3)),
893+
!iszero(e & (0x0001 << 4)),
894+
!iszero(e & (0x0001 << 5)),
895+
!iszero(e & (0x0001 << 6)),
896+
!iszero(e & (0x0001 << 7)),
897+
!iszero(e & (0x0001 << 8)),
898+
!iszero(e & (0x0001 << 9)),
899+
!iszero(e & (0x0001 << 10)))
900+
end
901+
829902
function form_purity_expr(override::EffectsOverride)
830903
ex = Expr(:purity)
831-
for i = 1:Core.Compiler.NUM_EFFECTS_OVERRIDES
904+
for i = 1:NUM_EFFECTS_OVERRIDES
832905
push!(ex.args, getfield(override, i))
833906
end
834907
return ex

src/julia.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ typedef struct _jl_debuginfo_t {
256256
jl_value_t *codelocs; // String // Memory{UInt8} // compressed info
257257
} jl_debuginfo_t;
258258

259-
// the following mirrors `struct EffectsOverride` in `base/compiler/effects.jl`
259+
// the following mirrors `struct EffectsOverride` in `base/expr.jl`
260260
typedef union __jl_purity_overrides_t {
261261
struct {
262262
uint16_t ipo_consistent : 1;

test/strings/basic.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1236,7 +1236,7 @@ end
12361236
end
12371237
@test_throws ArgumentError Symbol("a\0a")
12381238

1239-
@test Base._string_n_override == Core.Compiler.encode_effects_override(Base.compute_assumed_settings((:total, :(!:consistent))))
1239+
@test Base._string_n_override == Base.encode_effects_override(Base.compute_assumed_settings((:total, :(!:consistent))))
12401240
end
12411241

12421242
@testset "Ensure UTF-8 DFA can never leave invalid state" begin

0 commit comments

Comments
 (0)