From 7114185f4e51c07c9628f66131a8b18f6d74ca19 Mon Sep 17 00:00:00 2001 From: Tobias Gysi Date: Fri, 14 Oct 2022 09:32:30 +0300 Subject: [PATCH] [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 --- mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td | 12 +++++++++ mlir/lib/Target/LLVMIR/ConvertFromLLVMIR.cpp | 28 ++++++--------------- mlir/test/Target/LLVMIR/Import/basic.ll | 30 ---------------------- mlir/test/Target/LLVMIR/Import/instructions.ll | 35 ++++++++++++++++++++++++++ 4 files changed, 54 insertions(+), 51 deletions(-) diff --git a/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td b/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td index 5f798bc..c87be7c 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(inst); + $res = $_builder.create($_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(inst); + $res = $_builder.create($_location, + $container, $value, getPositionFromIndices(ivInst->getIndices())); + }]; } //===----------------------------------------------------------------------===// diff --git a/mlir/lib/Target/LLVMIR/ConvertFromLLVMIR.cpp b/mlir/lib/Target/LLVMIR/ConvertFromLLVMIR.cpp index 523aebf..3f95afb 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 getPositionFromIndices(ArrayRef indices) { + SmallVector 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(inst); - Value inserted = processValue(ivInst->getInsertedValueOperand()); - Value aggOperand = processValue(ivInst->getAggregateOperand()); - - SmallVector indices; - llvm::append_range(indices, ivInst->getIndices()); - Value res = b.create(loc, aggOperand, inserted, indices); - mapValue(inst, res); - return success(); - } - if (inst->getOpcode() == llvm::Instruction::ExtractValue) { - auto *evInst = cast(inst); - Value aggOperand = processValue(evInst->getAggregateOperand()); - - SmallVector indices; - llvm::append_range(indices, evInst->getIndices()); - Value res = b.create(loc, aggOperand, indices); - mapValue(inst, res); - return success(); - } if (inst->getOpcode() == llvm::Instruction::ShuffleVector) { auto *svInst = cast(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 961c5e8..6751e41 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 d42840d..b127586 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]+]] -- 2.7.4