[mlir][llvm] Use tablegen to import extract/insert ops from LLVM IR.
authorTobias Gysi <tobias.gysi@nextsilicon.com>
Fri, 14 Oct 2022 06:32:30 +0000 (09:32 +0300)
committerTobias Gysi <tobias.gysi@nextsilicon.com>
Fri, 14 Oct 2022 07:09:21 +0000 (10:09 +0300)
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

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

index 5f798bc..c87be7c 100644 (file)
@@ -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()));
+  }];
 }
 
 //===----------------------------------------------------------------------===//
index 523aebf..3f95afb 100644 (file)
@@ -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));
index 961c5e8..6751e41 100644 (file)
@@ -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) {
index d42840d..b127586 100644 (file)
@@ -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]+]]