Skip to content

Commit 298764a

Browse files
authored
[mlir][ToLLVM] Fix the index bitwidth handling for the dynamic case of convert-to-llvm (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"} } ```
1 parent 38b376f commit 298764a

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

mlir/lib/Conversion/ConvertToLLVM/ConvertToLLVMPass.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,9 @@ struct DynamicConvertToLLVM : public ConvertToLLVMPassInterface {
174174
target.addLegalDialect<LLVM::LLVMDialect>();
175175
// Get the data layout analysis.
176176
const auto &dlAnalysis = manager.getAnalysis<DataLayoutAnalysis>();
177-
LLVMTypeConverter typeConverter(context, &dlAnalysis);
177+
const DataLayout &dl = dlAnalysis.getAtOrAbove(op);
178+
LowerToLLVMOptions options(context, dl);
179+
LLVMTypeConverter typeConverter(context, options, &dlAnalysis);
178180

179181
// Configure the conversion with dialect level interfaces.
180182
for (ConvertToLLVMPatternInterface *iface : *interfaces)
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// RUN: mlir-opt --convert-to-llvm="filter-dialects=func dynamic=true" --split-input-file %s
2+
3+
4+
// CHECK-LABEL: llvm.func @test_default_index
5+
// CHECK-SAME: (%{{.*}}: i64) -> i64
6+
func.func private @test_default_index(%arg0: index) -> index
7+
8+
// -----
9+
10+
// CHECK-LABEL: module attributes {dlti.dl_spec = #dlti.dl_spec<
11+
// CHECK-SAME: #dlti.dl_entry<index, 32>
12+
// CHECK-SAME: >}
13+
module attributes {dlti.dl_spec = #dlti.dl_spec<#dlti.dl_entry<index, 32>>} {
14+
// CHECK-LABEL: llvm.func @test_32bit_index
15+
// CHECK-SAME: (%{{.*}}: i32) -> i32
16+
func.func private @test_32bit_index(%arg0: index) -> index
17+
}
18+
19+
// -----
20+
21+
// CHECK-LABEL: module attributes {dlti.dl_spec = #dlti.dl_spec<
22+
// CHECK-SAME: #dlti.dl_entry<index, 64>
23+
// CHECK-SAME: >}
24+
module attributes {dlti.dl_spec = #dlti.dl_spec<#dlti.dl_entry<index, 64>>} {
25+
// CHECK-LABEL: llvm.func @test_64bit_index
26+
// CHECK-SAME: (%{{.*}}: i64) -> i64
27+
func.func private @test_64bit_index(%arg0: index) -> index
28+
}
29+
30+
// -----
31+
32+
// CHECK-LABEL: module attributes {dlti.dl_spec = #dlti.dl_spec<
33+
// CHECK-SAME: #dlti.dl_entry<index, 16>
34+
// CHECK-SAME: >}
35+
module attributes {dlti.dl_spec = #dlti.dl_spec<#dlti.dl_entry<index, 16>>} {
36+
// CHECK-LABEL: llvm.func @test_16bit_index
37+
// CHECK-SAME: (%{{.*}}: i16) -> i16
38+
func.func private @test_16bit_index(%arg0: index) -> index
39+
}

0 commit comments

Comments
 (0)