[llvm] [OCaml] Use DataLayout instead of string in (set_)data_layout (PR #228445)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 2 07:04:10 PDT 2026


https://github.com/nikic created https://github.com/llvm/llvm-project/pull/228445

Now that DataLayout is part of llvm rather than llvm_target, make use of it in the `data_layout` and `set_data_layout` APIs.

>From c99d6bd7f60cf18a3234fe87c7fd6cd61bf4dfb4 Mon Sep 17 00:00:00 2001
From: Nikita Popov <npopov at redhat.com>
Date: Fri, 2 Oct 2026 16:01:25 +0200
Subject: [PATCH] [OCaml] Use DataLayout instead of string in (set_)data_layout

Now that DataLayout is part of llvm rather than llvm_target, make
use of it in the `data_layout` and `set_data_layout` APIs.
---
 llvm/bindings/ocaml/llvm/llvm.ml      |  4 ++--
 llvm/bindings/ocaml/llvm/llvm.mli     | 15 +++++++--------
 llvm/bindings/ocaml/llvm/llvm_ocaml.c | 10 ++++------
 llvm/bindings/ocaml/llvm/llvm_ocaml.h |  1 +
 llvm/docs/ReleaseNotes.md             |  4 ++++
 llvm/test/Bindings/OCaml/core.ml      |  4 ++--
 llvm/test/Bindings/OCaml/debuginfo.ml |  5 ++---
 7 files changed, 22 insertions(+), 21 deletions(-)

diff --git a/llvm/bindings/ocaml/llvm/llvm.ml b/llvm/bindings/ocaml/llvm/llvm.ml
index ce57b4316e879f..48bd48b9e4ebaf 100644
--- a/llvm/bindings/ocaml/llvm/llvm.ml
+++ b/llvm/bindings/ocaml/llvm/llvm.ml
@@ -483,9 +483,9 @@ external target_triple: llmodule -> string
                       = "llvm_target_triple"
 external set_target_triple: string -> llmodule -> unit
                           = "llvm_set_target_triple"
-external data_layout: llmodule -> string
+external data_layout: llmodule -> DataLayout.t
                     = "llvm_data_layout"
-external set_data_layout: string -> llmodule -> unit
+external set_data_layout: DataLayout.t -> llmodule -> unit
                         = "llvm_set_data_layout"
 external dump_module : llmodule -> unit = "llvm_dump_module"
 external print_module : string -> llmodule -> unit = "llvm_print_module"
diff --git a/llvm/bindings/ocaml/llvm/llvm.mli b/llvm/bindings/ocaml/llvm/llvm.mli
index 93e0364227ebc1..294f88c70f8f10 100644
--- a/llvm/bindings/ocaml/llvm/llvm.mli
+++ b/llvm/bindings/ocaml/llvm/llvm.mli
@@ -604,14 +604,13 @@ val target_triple: llmodule -> string
     the string [triple]. See the method [llvm::Module::setTargetTriple]. *)
 val set_target_triple: string -> llmodule -> unit
 
-(** [data_layout m] is the data layout specifier for the module [m], something
-    like [e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-...-a0:0:64-f80:128:128]. See the
-    method [llvm::Module::getDataLayout]. *)
-val data_layout: llmodule -> string
-
-(** [set_data_layout s m] changes the data layout specifier for the module [m]
-    to the string [s]. See the method [llvm::Module::setDataLayout]. *)
-val set_data_layout: string -> llmodule -> unit
+(** [data_layout m] is the data layout for the module [m].
+    See the method [llvm::Module::getDataLayout]. *)
+val data_layout: llmodule -> DataLayout.t
+
+(** [set_data_layout dl m] changes the data layout specifier for the module [m]
+    to [dl]. See the method [llvm::Module::setDataLayout]. *)
+val set_data_layout: DataLayout.t -> llmodule -> unit
 
 (** [dump_module m] prints the .ll representation of the module [m] to standard
     error. See the method [llvm::Module::dump]. *)
diff --git a/llvm/bindings/ocaml/llvm/llvm_ocaml.c b/llvm/bindings/ocaml/llvm/llvm_ocaml.c
index 42a07c8dc79a04..e08797cf0e3d99 100644
--- a/llvm/bindings/ocaml/llvm/llvm_ocaml.c
+++ b/llvm/bindings/ocaml/llvm/llvm_ocaml.c
@@ -328,14 +328,14 @@ value llvm_set_target_triple(value Trip, value M) {
   return Val_unit;
 }
 
-/* llmodule -> string */
+/* llmodule -> DataLayout.t */
 value llvm_data_layout(value M) {
-  return caml_copy_string(LLVMGetDataLayout(Module_val(M)));
+  return llvm_alloc_data_layout(LLVMGetModuleDataLayout(Module_val(M)));
 }
 
-/* string -> llmodule -> unit */
+/* DataLayout.t -> llmodule -> unit */
 value llvm_set_data_layout(value Layout, value M) {
-  LLVMSetDataLayout(Module_val(M), String_val(Layout));
+  LLVMSetModuleDataLayout(Module_val(M), DataLayout_val(Layout));
   return Val_unit;
 }
 
@@ -2780,8 +2780,6 @@ value llvm_memorybuffer_dispose(value MemBuf) {
 
 /*===---- Data Layout -----------------------------------------------------===*/
 
-#define DataLayout_val(v) (*(LLVMTargetDataRef *)(Data_custom_val(v)))
-
 static void llvm_finalize_data_layout(value DataLayout) {
   LLVMDisposeTargetData(DataLayout_val(DataLayout));
 }
diff --git a/llvm/bindings/ocaml/llvm/llvm_ocaml.h b/llvm/bindings/ocaml/llvm/llvm_ocaml.h
index 79cadba6275ee9..6b4082a84baadc 100644
--- a/llvm/bindings/ocaml/llvm/llvm_ocaml.h
+++ b/llvm/bindings/ocaml/llvm/llvm_ocaml.h
@@ -58,6 +58,7 @@ void *from_val_array(value Elements);
 #define Use_val(v) ((LLVMUseRef)from_val(v))
 #define BasicBlock_val(v) ((LLVMBasicBlockRef)from_val(v))
 #define MemoryBuffer_val(v) ((LLVMMemoryBufferRef)from_val(v))
+#define DataLayout_val(v) (*(LLVMTargetDataRef *)(Data_custom_val(v)))
 
 /* Convert a C pointer to an OCaml option */
 value ptr_to_option(void *Ptr);
diff --git a/llvm/docs/ReleaseNotes.md b/llvm/docs/ReleaseNotes.md
index d14973f5fbd5b7..e96a1ec3938840 100644
--- a/llvm/docs/ReleaseNotes.md
+++ b/llvm/docs/ReleaseNotes.md
@@ -310,6 +310,10 @@ Makes programs 10x faster by doing Special New Thing.
 
 * `DataLayout` has been moved from `Llvm_target` to `Llvm`.
 
+* `data_layout` now returns a `DataLayout` instead of a `string`. Similarly
+  `set_data_layout` now accepts a `DataLayout` instead of a `string`. You can
+  use `DataLayout.of_string` and `DataLayout.as_string` to convert between them.
+
 ### Changes to the Python bindings
 
 ### Changes to the C API
diff --git a/llvm/test/Bindings/OCaml/core.ml b/llvm/test/Bindings/OCaml/core.ml
index 797f8d27f73df6..b44058b2ad25a4 100644
--- a/llvm/test/Bindings/OCaml/core.ml
+++ b/llvm/test/Bindings/OCaml/core.ml
@@ -83,8 +83,8 @@ let test_target () =
 
   begin group "layout";
     let layout = "e-m:o-p:32:32-p270:32:32-p271:32:32-p272:64:64-i128:128-f64:32:64-f80:128-n8:16:32-S128" in
-    set_data_layout layout m;
-    insist (layout = data_layout m)
+    set_data_layout (DataLayout.of_string layout) m;
+    insist (layout = DataLayout.as_string (data_layout m))
   end
   (* CHECK: target datalayout = "e-m:o-p:32:32-p270:32:32-p271:32:32-p272:64:64-i128:128-f64:32:64-f80:128-n8:16:32-S128"
    * CHECK: target triple = "i686-apple-darwin8"
diff --git a/llvm/test/Bindings/OCaml/debuginfo.ml b/llvm/test/Bindings/OCaml/debuginfo.ml
index 1f1d5c2c4c3cef..9135f183743b07 100644
--- a/llvm/test/Bindings/OCaml/debuginfo.ml
+++ b/llvm/test/Bindings/OCaml/debuginfo.ml
@@ -28,11 +28,10 @@ let prepare_target llmod =
   let triple = Llvm_target.Target.default_triple () in
   let lltarget = Llvm_target.Target.by_triple triple in
   let llmachine = Llvm_target.TargetMachine.create ~triple lltarget in
-  let lldly = Llvm.DataLayout.as_string
-      (Llvm_target.TargetMachine.data_layout llmachine)
+  let dl = Llvm_target.TargetMachine.data_layout llmachine
   in
   let _ = Llvm.set_target_triple triple llmod in
-  let _ = Llvm.set_data_layout lldly llmod in
+  let _ = Llvm.set_data_layout dl llmod in
   ()
 
 let new_module () =



More information about the llvm-commits mailing list