Skip to content

Commit 758ddba

Browse files
committed
[MLIR] Use Datalayout defaults when importing LLVM
LLVM defines several default datalayouts for integer and floating point types that are not being considered when importing into MLIR. This patch remedies this. Reviewed By: ftynse Differential Revision: https://reviews.llvm.org/D120832
1 parent ab7a7cc commit 758ddba

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

mlir/lib/Target/LLVMIR/ConvertFromLLVMIR.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "mlir/Target/LLVMIR/TypeFromLLVM.h"
2323
#include "mlir/Translation.h"
2424

25+
#include "llvm/ADT/StringSet.h"
2526
#include "llvm/ADT/TypeSwitch.h"
2627
#include "llvm/IR/Attributes.h"
2728
#include "llvm/IR/Constants.h"
@@ -93,8 +94,23 @@ DataLayoutSpecInterface
9394
mlir::translateDataLayout(const llvm::DataLayout &dataLayout,
9495
MLIRContext *context) {
9596
assert(context && "expected MLIR context");
96-
StringRef layout = dataLayout.getStringRepresentation();
97+
std::string layoutstr = dataLayout.getStringRepresentation();
98+
99+
// Remaining unhandled default layout defaults
100+
// e (little endian if not set)
101+
// p[n]:64:64:64 (non zero address spaces have 64-bit properties)
102+
std::string append =
103+
"p:64:64:64-S0-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f16:16:16-f64:"
104+
"64:64-f128:128:128-v64:64:64-v128:128:128-a:0:64";
105+
if (layoutstr.empty())
106+
layoutstr = append;
107+
else
108+
layoutstr = layoutstr + "-" + append;
109+
110+
StringRef layout(layoutstr);
111+
97112
SmallVector<DataLayoutEntryInterface> entries;
113+
StringSet<> seen;
98114
while (!layout.empty()) {
99115
// Split at '-'.
100116
std::pair<StringRef, StringRef> split = layout.split('-');
@@ -104,6 +120,9 @@ mlir::translateDataLayout(const llvm::DataLayout &dataLayout,
104120
// Split at ':'.
105121
StringRef kind, spec;
106122
std::tie(kind, spec) = current.split(':');
123+
if (seen.contains(kind))
124+
continue;
125+
seen.insert(kind);
107126

108127
char symbol = kind.front();
109128
StringRef parameter = kind.substr(1);

mlir/test/Target/LLVMIR/data-layout.ll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
; CHECK: #dlti.dl_entry<"dlti.endianness", "little">
66
; CHECK: #dlti.dl_entry<i64, dense<64> : vector<2xi32>>
77
; CHECK: #dlti.dl_entry<f80, dense<128> : vector<2xi32>>
8+
; CHECK: #dlti.dl_entry<i8, dense<8> : vector<2xi32>>
89
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
910

1011
declare void @foo()

0 commit comments

Comments
 (0)