Skip to content

Commit 181ede0

Browse files
alan-j-hunikic
authored andcommitted
[llvm][ocaml] Replace deprecated C functions in OCaml bindings
Follow-up to D135524, to replace two more deprecated C functions in the OCaml bindings. const_in_bounds_gep now accepts the source element type as argument, and const_element has been changed into aggregate_element, which works on a wider range of constants and returns an option. Differential Revision: https://reviews.llvm.org/D136914
1 parent d5e59e9 commit 181ede0

File tree

4 files changed

+22
-17
lines changed

4 files changed

+22
-17
lines changed

llvm/bindings/ocaml/llvm/llvm.ml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,8 @@ external const_packed_struct : llcontext -> llvalue array -> llvalue
629629
= "llvm_const_packed_struct"
630630
external const_vector : llvalue array -> llvalue = "llvm_const_vector"
631631
external string_of_const : llvalue -> string option = "llvm_string_of_const"
632-
external const_element : llvalue -> int -> llvalue = "llvm_const_element"
632+
external aggregate_element : llvalue -> int -> llvalue option
633+
= "llvm_aggregate_element"
633634

634635
(*--... Constant expressions ...............................................--*)
635636
external align_of : lltype -> llvalue = "LLVMAlignOf"
@@ -659,8 +660,8 @@ external const_lshr : llvalue -> llvalue -> llvalue = "LLVMConstLShr"
659660
external const_ashr : llvalue -> llvalue -> llvalue = "LLVMConstAShr"
660661
external const_gep : lltype -> llvalue -> llvalue array -> llvalue
661662
= "llvm_const_gep"
662-
external const_in_bounds_gep : llvalue -> llvalue array -> llvalue
663-
= "llvm_const_in_bounds_gep"
663+
external const_in_bounds_gep : lltype -> llvalue -> llvalue array -> llvalue
664+
= "llvm_const_in_bounds_gep"
664665
external const_trunc : llvalue -> lltype -> llvalue = "LLVMConstTrunc"
665666
external const_sext : llvalue -> lltype -> llvalue = "LLVMConstSExt"
666667
external const_zext : llvalue -> lltype -> llvalue = "LLVMConstZExt"

llvm/bindings/ocaml/llvm/llvm.mli

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,9 +1044,11 @@ val const_vector : llvalue array -> llvalue
10441044
or [None] if this is not a string constant. *)
10451045
val string_of_const : llvalue -> string option
10461046

1047-
(** [const_element c] returns a constant for a specified index's element.
1048-
See the method ConstantDataSequential::getElementAsConstant. *)
1049-
val const_element : llvalue -> int -> llvalue
1047+
(** [aggregate_element c idx] returns [Some elt] where [elt] is the element of
1048+
constant aggregate [c] at the specified index [idx], or [None] if [idx] is
1049+
out of range or it's not possible to determine the element.
1050+
See the method [llvm::Constant::getAggregateElement]. *)
1051+
val aggregate_element : llvalue -> int -> llvalue option
10501052

10511053

10521054
(** {7 Constant expressions} *)
@@ -1169,10 +1171,10 @@ val const_ashr : llvalue -> llvalue -> llvalue
11691171
See the method [llvm::ConstantExpr::getGetElementPtr]. *)
11701172
val const_gep : lltype -> llvalue -> llvalue array -> llvalue
11711173

1172-
(** [const_in_bounds_gep pc indices] returns the constant [getElementPtr] of [pc]
1173-
with the constant integers indices from the array [indices].
1174+
(** [const_in_bounds_gep ty pc indices] returns the constant [getElementPtr] of
1175+
[pc] with the constant integers indices from the array [indices].
11741176
See the method [llvm::ConstantExpr::getInBoundsGetElementPtr]. *)
1175-
val const_in_bounds_gep : llvalue -> llvalue array -> llvalue
1177+
val const_in_bounds_gep : lltype -> llvalue -> llvalue array -> llvalue
11761178

11771179
(** [const_trunc c ty] returns the constant truncation of integer constant [c]
11781180
to the smaller integer type [ty].

llvm/bindings/ocaml/llvm/llvm_ocaml.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -968,9 +968,9 @@ value llvm_string_of_const(LLVMValueRef Const) {
968968
return cstr_to_string_option(CStr, Len);
969969
}
970970

971-
/* llvalue -> int -> llvalue */
972-
LLVMValueRef llvm_const_element(LLVMValueRef Const, value N) {
973-
return LLVMGetElementAsConstant(Const, Int_val(N));
971+
/* llvalue -> int -> llvalue option */
972+
value llvm_aggregate_element(LLVMValueRef Const, value N) {
973+
return ptr_to_option(LLVMGetAggregateElement(Const, Int_val(N)));
974974
}
975975

976976
/*--... Constant expressions ...............................................--*/
@@ -995,9 +995,10 @@ LLVMValueRef llvm_const_gep(LLVMTypeRef Ty, LLVMValueRef ConstantVal,
995995
}
996996

997997
/* llvalue -> llvalue array -> llvalue */
998-
LLVMValueRef llvm_const_in_bounds_gep(LLVMValueRef ConstantVal, value Indices) {
999-
return LLVMConstInBoundsGEP(ConstantVal, (LLVMValueRef *)Op_val(Indices),
1000-
Wosize_val(Indices));
998+
LLVMValueRef llvm_const_in_bounds_gep(LLVMTypeRef Ty, LLVMValueRef ConstantVal,
999+
value Indices) {
1000+
return LLVMConstInBoundsGEP2(Ty, ConstantVal, (LLVMValueRef *)Op_val(Indices),
1001+
Wosize_val(Indices));
10011002
}
10021003

10031004
/* llvalue -> lltype -> is_signed:bool -> llvalue */

llvm/test/Bindings/OCaml/core.ml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,9 @@ let test_constants () =
185185
let c = const_array i32_type [| three; four |] in
186186
ignore (define_global "const_array" c m);
187187
insist ((array_type i32_type 2) = (type_of c));
188-
insist (three = (const_element c 0));
189-
insist (four = (const_element c 1));
188+
insist (Some three = (aggregate_element c 0));
189+
insist (Some four = (aggregate_element c 1));
190+
insist (None = (aggregate_element c 2));
190191

191192
(* CHECK: const_vector{{.*}}<i16 1, i16 2{{.*}}>
192193
*)

0 commit comments

Comments
 (0)