[Mlir-commits] [mlir] 7114185 - [mlir][llvm] Use tablegen to import extract/insert ops from LLVM IR.
Tobias Gysi
llvmlistbot at llvm.org
Fri Oct 14 00:10:03 PDT 2022
Author: Tobias Gysi
Date: 2022-10-14T10:09:21+03:00
New Revision: 7114185f4e51c07c9628f66131a8b18f6d74ca19
URL: https://github.com/llvm/llvm-project/commit/7114185f4e51c07c9628f66131a8b18f6d74ca19
DIFF: https://github.com/llvm/llvm-project/commit/7114185f4e51c07c9628f66131a8b18f6d74ca19.diff
LOG: [mlir][llvm] Use tablegen to import extract/insert ops from LLVM IR.
The revision imports the extract and insert value operations using
tablegen generated builders. Additionally, it moves the tests to
the instructions.ll test file.
Reviewed By: ftynse, dcaballe
Differential Revision: https://reviews.llvm.org/D135874
Added:
Modified:
mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td
mlir/lib/Target/LLVMIR/ConvertFromLLVMIR.cpp
mlir/test/Target/LLVMIR/Import/basic.ll
mlir/test/Target/LLVMIR/Import/instructions.ll
Removed:
################################################################################
diff --git a/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td b/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td
index 5f798bc123a8d..c87be7c5863e9 100644
--- a/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td
+++ b/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td
@@ -815,9 +815,15 @@ def LLVM_ExtractValueOp : LLVM_Op<"extractvalue", [Pure]> {
let hasFolder = 1;
let hasVerifier = 1;
+ string llvmInstName = "ExtractValue";
string llvmBuilder = [{
$res = builder.CreateExtractValue($container, extractPosition($position));
}];
+ string mlirBuilder = [{
+ auto *evInst = cast<llvm::ExtractValueInst>(inst);
+ $res = $_builder.create<LLVM::ExtractValueOp>($_location,
+ $container, getPositionFromIndices(evInst->getIndices()));
+ }];
}
//===----------------------------------------------------------------------===//
@@ -871,10 +877,16 @@ def LLVM_InsertValueOp : LLVM_Op<
let hasVerifier = 1;
+ string llvmInstName = "InsertValue";
string llvmBuilder = [{
$res = builder.CreateInsertValue($container, $value,
extractPosition($position));
}];
+ string mlirBuilder = [{
+ auto *ivInst = cast<llvm::InsertValueInst>(inst);
+ $res = $_builder.create<LLVM::InsertValueOp>($_location,
+ $container, $value, getPositionFromIndices(ivInst->getIndices()));
+ }];
}
//===----------------------------------------------------------------------===//
diff --git a/mlir/lib/Target/LLVMIR/ConvertFromLLVMIR.cpp b/mlir/lib/Target/LLVMIR/ConvertFromLLVMIR.cpp
index 523aebf43baac..3f95afb19353b 100644
--- a/mlir/lib/Target/LLVMIR/ConvertFromLLVMIR.cpp
+++ b/mlir/lib/Target/LLVMIR/ConvertFromLLVMIR.cpp
@@ -236,6 +236,13 @@ static StringRef getLLVMSyncScope(llvm::FenceInst *fenceInst) {
llvm_unreachable("incorrect sync scope identifier");
}
+/// Converts an array of unsigned indices to a signed integer position array.
+static SmallVector<int64_t> getPositionFromIndices(ArrayRef<unsigned> indices) {
+ SmallVector<int64_t> position;
+ llvm::append_range(position, indices);
+ return position;
+}
+
DataLayoutSpecInterface
mlir::translateDataLayout(const llvm::DataLayout &dataLayout,
MLIRContext *context) {
@@ -986,27 +993,6 @@ LogicalResult Importer::processInstruction(llvm::Instruction *inst) {
mapValue(inst, res);
return success();
}
- if (inst->getOpcode() == llvm::Instruction::InsertValue) {
- auto *ivInst = cast<llvm::InsertValueInst>(inst);
- Value inserted = processValue(ivInst->getInsertedValueOperand());
- Value aggOperand = processValue(ivInst->getAggregateOperand());
-
- SmallVector<int64_t> indices;
- llvm::append_range(indices, ivInst->getIndices());
- Value res = b.create<InsertValueOp>(loc, aggOperand, inserted, indices);
- mapValue(inst, res);
- return success();
- }
- if (inst->getOpcode() == llvm::Instruction::ExtractValue) {
- auto *evInst = cast<llvm::ExtractValueInst>(inst);
- Value aggOperand = processValue(evInst->getAggregateOperand());
-
- SmallVector<int64_t> indices;
- llvm::append_range(indices, evInst->getIndices());
- Value res = b.create<ExtractValueOp>(loc, aggOperand, indices);
- mapValue(inst, res);
- return success();
- }
if (inst->getOpcode() == llvm::Instruction::ShuffleVector) {
auto *svInst = cast<llvm::ShuffleVectorInst>(inst);
Value vec1 = processValue(svInst->getOperand(0));
diff --git a/mlir/test/Target/LLVMIR/Import/basic.ll b/mlir/test/Target/LLVMIR/Import/basic.ll
index 961c5e876f3f8..6751e4132800e 100644
--- a/mlir/test/Target/LLVMIR/Import/basic.ll
+++ b/mlir/test/Target/LLVMIR/Import/basic.ll
@@ -448,36 +448,6 @@ def: ; pred: bb3, bbs
ret void
}
-; Insert/ExtractValue
-; CHECK-LABEL: llvm.func @insert_extract_value_struct
-define float @insert_extract_value_struct({{i32},{float, double}}* %p) {
- ; CHECK: %[[C0:.+]] = llvm.mlir.constant(2.000000e+00 : f64)
- ; CHECK: %[[VT:.+]] = llvm.load %{{.+}}
- %t = load {{i32},{float, double}}, {{i32},{float, double}}* %p
- ; CHECK: %[[EV:.+]] = llvm.extractvalue %[[VT]][1, 0] :
- ; CHECK-SAME: !llvm.struct<(struct<(i32)>, struct<(f32, f64)>)>
- %s = extractvalue {{i32},{float, double}} %t, 1, 0
- ; CHECK: %[[IV:.+]] = llvm.insertvalue %[[C0]], %[[VT]][1, 1] :
- ; CHECK-SAME: !llvm.struct<(struct<(i32)>, struct<(f32, f64)>)>
- %r = insertvalue {{i32},{float, double}} %t, double 2.0, 1, 1
- ; CHECK: llvm.store %[[IV]], %{{.+}}
- store {{i32},{float, double}} %r, {{i32},{float, double}}* %p
- ; CHECK: llvm.return %[[EV]]
- ret float %s
-}
-
-; CHECK-LABEL: llvm.func @insert_extract_value_array
-define void @insert_extract_value_array([4 x [4 x i8]] %x1) {
- ; CHECK: %[[C0:.+]] = llvm.mlir.constant(0 : i8)
- ; CHECK: llvm.insertvalue %[[C0]], %{{.+}}[0, 0] : !llvm.array<4 x array<4 x i8>>
- %res1 = insertvalue [4 x [4 x i8 ]] %x1, i8 0, 0, 0
- ; CHECK: llvm.extractvalue %{{.+}}[1] : !llvm.array<4 x array<4 x i8>>
- %res2 = extractvalue [4 x [4 x i8 ]] %x1, 1
- ; CHECK: llvm.extractvalue %{{.+}}[0, 1] : !llvm.array<4 x array<4 x i8>>
- %res3 = extractvalue [4 x [4 x i8 ]] %x1, 0, 1
- ret void
-}
-
; Shufflevector
; CHECK-LABEL: llvm.func @shuffle_vec
define <4 x half> @shuffle_vec(<4 x half>* %arg0, <4 x half>* %arg1) {
diff --git a/mlir/test/Target/LLVMIR/Import/instructions.ll b/mlir/test/Target/LLVMIR/Import/instructions.ll
index d42840dda521a..b127586ddfe5c 100644
--- a/mlir/test/Target/LLVMIR/Import/instructions.ll
+++ b/mlir/test/Target/LLVMIR/Import/instructions.ll
@@ -276,6 +276,41 @@ define <4 x half> @insert_element(<4 x half>* %vec, half %val, i32 %idx) {
; // -----
+; CHECK-LABEL: @insert_extract_value_struct
+; CHECK-SAME: %[[PTR:[a-zA-Z0-9]+]]
+define float @insert_extract_value_struct({{i32},{float, double}}* %ptr) {
+ ; CHECK: %[[C0:.+]] = llvm.mlir.constant(2.000000e+00 : f64)
+ ; CHECK: %[[VT:.+]] = llvm.load %[[PTR]]
+ %1 = load {{i32},{float, double}}, {{i32},{float, double}}* %ptr
+ ; CHECK: %[[EV:.+]] = llvm.extractvalue %[[VT]][1, 0] :
+ ; CHECK-SAME: !llvm.struct<(struct<(i32)>, struct<(f32, f64)>)>
+ %2 = extractvalue {{i32},{float, double}} %1, 1, 0
+ ; CHECK: %[[IV:.+]] = llvm.insertvalue %[[C0]], %[[VT]][1, 1] :
+ ; CHECK-SAME: !llvm.struct<(struct<(i32)>, struct<(f32, f64)>)>
+ %3 = insertvalue {{i32},{float, double}} %1, double 2.0, 1, 1
+ ; CHECK: llvm.store %[[IV]], %[[PTR]]
+ store {{i32},{float, double}} %3, {{i32},{float, double}}* %ptr
+ ; CHECK: llvm.return %[[EV]]
+ ret float %2
+}
+
+; // -----
+
+; CHECK-LABEL: @insert_extract_value_array
+; CHECK-SAME: %[[ARG1:[a-zA-Z0-9]+]]
+define void @insert_extract_value_array([4 x [4 x i8]] %arg1) {
+ ; CHECK: %[[C0:.+]] = llvm.mlir.constant(0 : i8)
+ ; CHECK: llvm.insertvalue %[[C0]], %[[ARG1]][0, 0] : !llvm.array<4 x array<4 x i8>>
+ %1 = insertvalue [4 x [4 x i8 ]] %arg1, i8 0, 0, 0
+ ; CHECK: llvm.extractvalue %[[ARG1]][1] : !llvm.array<4 x array<4 x i8>>
+ %2 = extractvalue [4 x [4 x i8 ]] %arg1, 1
+ ; CHECK: llvm.extractvalue %[[ARG1]][0, 1] : !llvm.array<4 x array<4 x i8>>
+ %3 = extractvalue [4 x [4 x i8 ]] %arg1, 0, 1
+ ret void
+}
+
+; // -----
+
; CHECK-LABEL: @select
; CHECK-SAME: %[[ARG1:[a-zA-Z0-9]+]]
; CHECK-SAME: %[[ARG2:[a-zA-Z0-9]+]]
More information about the Mlir-commits
mailing list