Skip to content

Commit f31618d

Browse files
dulmarodmeta-codesync[bot]
authored andcommitted
[swift] Fix translation of move_phi
Summary: When we translate phi nodes, in llair this is a move_phi, and, whilst in normal move, we do not deref variables, here it's necessary. It was needed in an example for optional types. Now we see another example, when adding deref is a problem, because the translation adds it again later leading to a typechecking error. Now we keep track of that the deref has been already added in the move, and we don't add it later. With this approach both examples work correctly. Reviewed By: davidpichardie Differential Revision: D89551549 Privacy Context Container: L1208441 fbshipit-source-id: 7e0c80ef28f6d58c075467b28d89bbf99455fa5f
1 parent befc86c commit f31618d

File tree

4 files changed

+47
-14
lines changed

4 files changed

+47
-14
lines changed

infer/src/llvm/Llair2Textual.ml

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,14 @@ let add_deref ~proc_state ?from_call exp loc =
260260
| Textual.Exp.Field _ ->
261261
add_load_instr
262262
| Textual.Exp.Var id -> (
263-
let typ = IdentMap.find_opt id proc_state.ProcState.ids_move in
264-
match typ with Some {typ= Textual.Typ.Ptr _} -> add_load_instr | _ -> ([], exp) )
263+
let id_data = IdentMap.find_opt id proc_state.ProcState.ids_move in
264+
match id_data with
265+
| Some {no_deref_needed= true} ->
266+
([], exp)
267+
| Some {typ= {typ= Textual.Typ.Ptr _}} ->
268+
add_load_instr
269+
| _ ->
270+
([], exp) )
265271
| _ ->
266272
([], exp)
267273

@@ -662,17 +668,31 @@ let remove_store_zero_in_class typ_exp1 exp2 =
662668
false
663669

664670

665-
let translate_move ~move_phi ~proc_state loc textual_instrs reg_exps =
671+
let translate_move ~proc_state ~move_phi loc textual_instrs reg_exps =
666672
let reg_exps = StdUtils.iarray_to_list reg_exps in
667673
let loc = to_textual_loc_instr ~proc_state loc in
668674
let instrs =
669675
List.fold
670676
~f:(fun instrs (reg, exp) ->
671677
let id = Some (Var.reg_to_id ~proc_state reg |> fst) in
672678
let exp, exp_typ, exp_instrs = to_textual_exp loc ~proc_state exp in
679+
let no_deref_needed =
680+
match exp with
681+
| Textual.Exp.Var var ->
682+
Option.value_map
683+
~f:(fun id_data -> id_data.ProcState.no_deref_needed)
684+
(IdentMap.find_opt var proc_state.ProcState.ids_move)
685+
~default:false
686+
| _ when move_phi ->
687+
true
688+
| _ ->
689+
false
690+
in
673691
( match (id, exp_typ) with
674692
| Some id, Some exp_typ ->
675-
ProcState.update_ids_move ~proc_state id (Textual.Typ.mk_without_attributes exp_typ)
693+
ProcState.update_ids_move ~proc_state id
694+
(Textual.Typ.mk_without_attributes exp_typ)
695+
~no_deref_needed
676696
| _ ->
677697
() ) ;
678698
let deref_instrs, exp = if move_phi then add_deref ~proc_state exp loc else ([], exp) in
@@ -816,9 +836,9 @@ let cmnd_to_instrs ~(proc_state : ProcState.t) block =
816836
let call_textual_instrs = to_textual_builtin ~proc_state reg name args loc in
817837
List.append call_textual_instrs textual_instrs
818838
| Move {reg_exps: (Reg.t * Exp.t) NS.iarray; loc} ->
819-
translate_move ~move_phi:false ~proc_state loc textual_instrs reg_exps
839+
translate_move ~proc_state ~move_phi:false loc textual_instrs reg_exps
820840
| MovePhi {reg_exps: (Reg.t * Exp.t) NS.iarray; loc} ->
821-
translate_move ~move_phi:true ~proc_state loc textual_instrs reg_exps
841+
translate_move ~proc_state ~move_phi:true loc textual_instrs reg_exps
822842
| AtomicRMW {reg; ptr; exp; loc} ->
823843
let loc = to_textual_loc ~proc_state loc in
824844
let call_textual_instrs =

infer/src/llvm/Llair2TextualState.ml

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,14 +98,20 @@ module ModuleState = struct
9898
end
9999

100100
module ProcState = struct
101+
type id_data = {typ: Textual.Typ.annotated; no_deref_needed: bool}
102+
103+
let pp_data fmt {typ; no_deref_needed} =
104+
F.fprintf fmt "typ:%a, no_deref_needed: %b" Textual.Typ.pp_annotated typ no_deref_needed
105+
106+
101107
type t =
102108
{ qualified_name: Textual.QualifiedProcName.t
103109
; sourcefile: SourceFile.t
104110
; loc: Textual.Location.t
105111
; mutable locals: Textual.Typ.annotated VarMap.t
106112
; mutable formals: (Textual.Typ.annotated * Textual.VarName.t option) VarMap.t
107113
; mutable local_map: Textual.Typ.t Textual.VarName.Hashtbl.t
108-
; mutable ids_move: Textual.Typ.annotated IdentMap.t
114+
; mutable ids_move: id_data IdentMap.t
109115
; mutable ids_types: Textual.Typ.annotated IdentMap.t
110116
; mutable id_offset: (Textual.Ident.t * int) option
111117
; mutable get_element_ptr_offset: (Textual.VarName.t * int) option
@@ -159,6 +165,11 @@ module ProcState = struct
159165
(Pp.comma_seq (Pp.pair ~fst:Textual.Ident.pp ~snd:Textual.Typ.pp_annotated))
160166
(IdentMap.bindings current_ids)
161167
in
168+
let pp_ids_data fmt current_ids =
169+
F.fprintf fmt "%a"
170+
(Pp.comma_seq (Pp.pair ~fst:Textual.Ident.pp ~snd:pp_data))
171+
(IdentMap.bindings current_ids)
172+
in
162173
let pp_vars fmt vars =
163174
F.fprintf fmt "%a"
164175
(Pp.comma_seq (Pp.pair ~fst:Textual.VarName.pp ~snd:Textual.Typ.pp_annotated))
@@ -188,7 +199,7 @@ module ProcState = struct
188199
@[get_element_ptr_offset: %a@]@;\
189200
]@]"
190201
Textual.QualifiedProcName.pp proc_state.qualified_name Textual.Location.pp proc_state.loc
191-
pp_vars proc_state.locals pp_formals proc_state.formals pp_ids proc_state.ids_move pp_ids
202+
pp_vars proc_state.locals pp_formals proc_state.formals pp_ids_data proc_state.ids_move pp_ids
192203
proc_state.ids_types
193204
(Pp.option (Pp.pair ~fst:Textual.Ident.pp ~snd:Int.pp))
194205
proc_state.id_offset
@@ -201,8 +212,8 @@ module ProcState = struct
201212
proc_state.locals <- VarMap.add varname typ proc_state.locals
202213

203214

204-
let update_ids_move ~proc_state id typ =
205-
proc_state.ids_move <- IdentMap.add id typ proc_state.ids_move
215+
let update_ids_move ~proc_state id typ ~no_deref_needed =
216+
proc_state.ids_move <- IdentMap.add id {typ; no_deref_needed} proc_state.ids_move
206217

207218

208219
(* debug_name = var1,

infer/src/llvm/Llair2TextualState.mli

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,16 @@ module ModuleState : sig
8181
end
8282

8383
module ProcState : sig
84+
type id_data = {typ: Textual.Typ.annotated; no_deref_needed: bool}
85+
8486
type t = private
8587
{ qualified_name: Textual.QualifiedProcName.t
8688
; sourcefile: SourceFile.t
8789
; loc: Textual.Location.t
8890
; mutable locals: Textual.Typ.annotated VarMap.t
8991
; mutable formals: (Textual.Typ.annotated * Textual.VarName.t option) VarMap.t
9092
; mutable local_map: Textual.Typ.t Textual.VarName.Hashtbl.t
91-
; mutable ids_move: Textual.Typ.annotated IdentMap.t
93+
; mutable ids_move: id_data IdentMap.t
9294
; mutable ids_types: Textual.Typ.annotated IdentMap.t
9395
; mutable id_offset: (Textual.Ident.t * int) option
9496
; mutable get_element_ptr_offset: (Textual.VarName.t * int) option
@@ -111,7 +113,8 @@ module ProcState : sig
111113

112114
val update_locals : proc_state:t -> VarMap.key -> Textual.Typ.annotated -> unit
113115

114-
val update_ids_move : proc_state:t -> IdentMap.key -> Textual.Typ.annotated -> unit
116+
val update_ids_move :
117+
proc_state:t -> IdentMap.key -> Textual.Typ.annotated -> no_deref_needed:bool -> unit
115118

116119
val update_ids_types : proc_state:t -> IdentMap.key -> Textual.Typ.annotated -> unit
117120

infer/tests/codetoanalyze/swift/frontend/retain_cycle.swift.sil

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -493,8 +493,7 @@ define .plain_name = "rawValue.get" $s5Hello9TransportC15ConnectionStateO8rawVal
493493
#13: @[246:0]
494494
n14 = $builtins.llvm_init_tuple() @[246:0]
495495
store n14.__infer_tuple_class<int,*__infer_swift_type<ptr_elt>>.__infer_tuple_field_0 <- n8 @[247:0]
496-
n16 = load n11 @[247:0]
497-
store n14.__infer_tuple_class<int,*__infer_swift_type<ptr_elt>>.__infer_tuple_field_1 <- n16 @[247:0]
496+
store n14.__infer_tuple_class<int,*__infer_swift_type<ptr_elt>>.__infer_tuple_field_1 <- n11 @[247:0]
498497
ret n14 @[247:0]
499498

500499
#5: @[242:0]

0 commit comments

Comments
 (0)