[Mlir-commits] [mlir] [mlir][ToLLVM] Fix the index bitwidth handling for the dynamic case of `convert-to-llvm` (PR #156557)

Fabian Mora llvmlistbot at llvm.org
Tue Sep 2 16:46:08 PDT 2025


https://github.com/fabianmcg created https://github.com/llvm/llvm-project/pull/156557

This patch changes the behavior of `convert-to-llvm{dynamic=true}` so that the nearest `DataLayout` is used to configure LowerToLLVMOptions and LLVMTypeConverter.

Example:

```mlir
module attributes {dlti.dl_spec = #dlti.dl_spec<#dlti.dl_entry<index, 16>>} {
  func.func private @test_16bit_index(%arg0: index) -> index
}
// mlir-opt --convert-to-llvm="dynamic=true"
module attributes {dlti.dl_spec = #dlti.dl_spec<index = 16 : i64>} {
  llvm.func @test_16bit_index(i16) -> i16 attributes {sym_visibility = "private"}
}
```

>From 52fa5024a9580b3b1b0049a21d66668e7ce3d4fb Mon Sep 17 00:00:00 2001
From: Fabian Mora <fabian.mora-cordero at amd.com>
Date: Tue, 2 Sep 2025 23:29:53 +0000
Subject: [PATCH] fix DL handling

---
 .../ConvertToLLVM/ConvertToLLVMPass.cpp       |  4 +-
 .../FuncToLLVM/func-to-llvm-datalayout.mlir   | 39 +++++++++++++++++++
 2 files changed, 42 insertions(+), 1 deletion(-)
 create mode 100644 mlir/test/Conversion/FuncToLLVM/func-to-llvm-datalayout.mlir

diff --git a/mlir/lib/Conversion/ConvertToLLVM/ConvertToLLVMPass.cpp b/mlir/lib/Conversion/ConvertToLLVM/ConvertToLLVMPass.cpp
index 764ad2e766c73..ab558d9a93be8 100644
--- a/mlir/lib/Conversion/ConvertToLLVM/ConvertToLLVMPass.cpp
+++ b/mlir/lib/Conversion/ConvertToLLVM/ConvertToLLVMPass.cpp
@@ -174,7 +174,9 @@ struct DynamicConvertToLLVM : public ConvertToLLVMPassInterface {
     target.addLegalDialect<LLVM::LLVMDialect>();
     // Get the data layout analysis.
     const auto &dlAnalysis = manager.getAnalysis<DataLayoutAnalysis>();
-    LLVMTypeConverter typeConverter(context, &dlAnalysis);
+    const DataLayout &dl = dlAnalysis.getAtOrAbove(op);
+    LowerToLLVMOptions options(context, dl);
+    LLVMTypeConverter typeConverter(context, options, &dlAnalysis);
 
     // Configure the conversion with dialect level interfaces.
     for (ConvertToLLVMPatternInterface *iface : *interfaces)
diff --git a/mlir/test/Conversion/FuncToLLVM/func-to-llvm-datalayout.mlir b/mlir/test/Conversion/FuncToLLVM/func-to-llvm-datalayout.mlir
new file mode 100644
index 0000000000000..36c3cf39d3822
--- /dev/null
+++ b/mlir/test/Conversion/FuncToLLVM/func-to-llvm-datalayout.mlir
@@ -0,0 +1,39 @@
+// RUN: mlir-opt --convert-to-llvm="filter-dialects=func dynamic=true" --split-input-file %s
+
+
+// CHECK-LABEL: llvm.func @test_default_index
+// CHECK-SAME: (%{{.*}}: i64) -> i64
+func.func private @test_default_index(%arg0: index) -> index
+
+// -----
+
+// CHECK-LABEL: module attributes {dlti.dl_spec = #dlti.dl_spec<
+// CHECK-SAME: #dlti.dl_entry<index, 32>
+// CHECK-SAME: >}
+module attributes {dlti.dl_spec = #dlti.dl_spec<#dlti.dl_entry<index, 32>>} {
+  // CHECK-LABEL: llvm.func @test_32bit_index
+  // CHECK-SAME: (%{{.*}}: i32) -> i32
+  func.func private @test_32bit_index(%arg0: index) -> index
+}
+
+// -----
+
+// CHECK-LABEL: module attributes {dlti.dl_spec = #dlti.dl_spec<
+// CHECK-SAME: #dlti.dl_entry<index, 64>
+// CHECK-SAME: >}
+module attributes {dlti.dl_spec = #dlti.dl_spec<#dlti.dl_entry<index, 64>>} {
+  // CHECK-LABEL: llvm.func @test_64bit_index
+  // CHECK-SAME: (%{{.*}}: i64) -> i64
+  func.func private @test_64bit_index(%arg0: index) -> index
+}
+
+// -----
+
+// CHECK-LABEL: module attributes {dlti.dl_spec = #dlti.dl_spec<
+// CHECK-SAME: #dlti.dl_entry<index, 16>
+// CHECK-SAME: >}
+module attributes {dlti.dl_spec = #dlti.dl_spec<#dlti.dl_entry<index, 16>>} {
+  // CHECK-LABEL: llvm.func @test_16bit_index
+  // CHECK-SAME: (%{{.*}}: i16) -> i16
+  func.func private @test_16bit_index(%arg0: index) -> index
+}



More information about the Mlir-commits mailing list