Skip to content

Commit

Permalink
widenconst before type check in SROA (#49642)
Browse files Browse the repository at this point in the history
* `widenconst` before type check in SROA

These days, mutable structs can be `PartialStruct` if one of the
fields is annotated as `const`, so we need to widenconst before
this check.

* Update test/compiler/irpasses.jl

Co-authored-by: Shuhei Kadowaki <[email protected]>

---------

Co-authored-by: Shuhei Kadowaki <[email protected]>
  • Loading branch information
Keno and aviatesk authored May 5, 2023
1 parent 9201414 commit f4d9416
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
1 change: 1 addition & 0 deletions base/compiler/ssair/passes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1265,6 +1265,7 @@ function sroa_mutables!(ir::IRCode, defuses::IdDict{Int, Tuple{SPCSet, SSADefUse
typ = unwrap_unionall(ir.stmts[newidx][:type])
# Could still end up here if we tried to setfield! on an immutable, which would
# error at runtime, but is not illegal to have in the IR.
typ = widenconst(typ)
ismutabletype(typ) || continue
typ = typ::DataType
# First check for any finalizer calls
Expand Down
15 changes: 15 additions & 0 deletions test/compiler/irpasses.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1230,3 +1230,18 @@ let src = code_typed1(named_tuple_elim, Tuple{Symbol, Tuple})
count(iscall((src, Core._svec_ref)), src.code) == 0 &&
count(iscall(x->!isa(argextype(x, src).val, Core.Builtin)), src.code) == 0
end

# Test that sroa works if the struct type is a PartialStruct
mutable struct OneConstField
const a::Int
b::Int
end

@eval function one_const_field_partial()
# Use explicit :new here to avoid inlining messing with the type
strct = $(Expr(:new, OneConstField, 1, 2))
strct.b = 4
strct.b = 5
return strct.b
end
@test fully_eliminated(one_const_field_partial; retval=5)

0 comments on commit f4d9416

Please sign in to comment.