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

llvmlistbot at llvm.org llvmlistbot at llvm.org
Wed Sep 3 05:07:22 PDT 2025


Author: Fabian Mora
Date: 2025-09-03T08:07:18-04:00
New Revision: 298764a250150752b556b3212af955e1e2a01877

URL: https://github.com/llvm/llvm-project/commit/298764a250150752b556b3212af955e1e2a01877
DIFF: https://github.com/llvm/llvm-project/commit/298764a250150752b556b3212af955e1e2a01877.diff

LOG: [mlir][ToLLVM] Fix the index bitwidth handling for the dynamic case of `convert-to-llvm` (#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"}
}
```

Added: 
    mlir/test/Conversion/FuncToLLVM/func-to-llvm-datalayout.mlir

Modified: 
    mlir/lib/Conversion/ConvertToLLVM/ConvertToLLVMPass.cpp

Removed: 
    


################################################################################
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