Skip to content

Commit 3b91657

Browse files
committed
[mlir][LLVMIR] Add support for translating from some simple LLVM instructions
Add support for translating from llvm::Select, llvm::FNeg, and llvm::Unreachable. This patch also cleans up (NFC) the opcode map for simple instructions and adds `// clang-format off/on` comments to prevent those lines from being churned by clang-format between commits. Differential Revision: https://reviews.llvm.org/D125817
1 parent 5528784 commit 3b91657

File tree

2 files changed

+74
-25
lines changed

2 files changed

+74
-25
lines changed

mlir/lib/Target/LLVMIR/ConvertFromLLVMIR.cpp

Lines changed: 55 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -581,46 +581,73 @@ static StringRef lookupOperationNameFromOpcode(unsigned opcode) {
581581
#define INST(llvm_n, mlir_n) \
582582
{ llvm::Instruction::llvm_n, LLVM::mlir_n##Op::getOperationName() }
583583
static const DenseMap<unsigned, StringRef> opcMap = {
584-
// Ret is handled specially.
584+
// clang-format off
585+
INST(Ret, Return),
585586
// Br is handled specially.
586587
// Switch is handled specially.
587588
// FIXME: indirectbr
588-
// FIXME: invoke
589+
// Invoke is handled specially.
589590
INST(Resume, Resume),
590-
// FIXME: unreachable
591+
INST(Unreachable, Unreachable),
591592
// FIXME: cleanupret
592593
// FIXME: catchret
593594
// FIXME: catchswitch
594595
// FIXME: callbr
595-
// FIXME: fneg
596-
INST(Add, Add), INST(FAdd, FAdd), INST(Sub, Sub), INST(FSub, FSub),
597-
INST(Mul, Mul), INST(FMul, FMul), INST(UDiv, UDiv), INST(SDiv, SDiv),
598-
INST(FDiv, FDiv), INST(URem, URem), INST(SRem, SRem), INST(FRem, FRem),
599-
INST(Shl, Shl), INST(LShr, LShr), INST(AShr, AShr), INST(And, And),
600-
INST(Or, Or), INST(Xor, XOr), INST(Alloca, Alloca), INST(Load, Load),
596+
INST(FNeg, FNeg),
597+
INST(Add, Add),
598+
INST(FAdd, FAdd),
599+
INST(Sub, Sub),
600+
INST(FSub, FSub),
601+
INST(Mul, Mul),
602+
INST(FMul, FMul),
603+
INST(UDiv, UDiv),
604+
INST(SDiv, SDiv),
605+
INST(FDiv, FDiv),
606+
INST(URem, URem),
607+
INST(SRem, SRem),
608+
INST(FRem, FRem),
609+
INST(Shl, Shl),
610+
INST(LShr, LShr),
611+
INST(AShr, AShr),
612+
INST(And, And),
613+
INST(Or, Or),
614+
INST(Xor, XOr),
615+
INST(ExtractElement, ExtractElement),
616+
INST(InsertElement, InsertElement),
617+
// ShuffleVector is handled specially.
618+
// ExtractValue is handled specially.
619+
// InsertValue is handled specially.
620+
INST(Alloca, Alloca),
621+
INST(Load, Load),
601622
INST(Store, Store),
602-
// Getelementptr is handled specially.
603-
INST(Ret, Return), INST(Fence, Fence),
623+
INST(Fence, Fence),
604624
// FIXME: atomiccmpxchg
605625
// FIXME: atomicrmw
606-
INST(Trunc, Trunc), INST(ZExt, ZExt), INST(SExt, SExt),
607-
INST(FPToUI, FPToUI), INST(FPToSI, FPToSI), INST(UIToFP, UIToFP),
608-
INST(SIToFP, SIToFP), INST(FPTrunc, FPTrunc), INST(FPExt, FPExt),
609-
INST(PtrToInt, PtrToInt), INST(IntToPtr, IntToPtr),
610-
INST(BitCast, Bitcast), INST(AddrSpaceCast, AddrSpaceCast),
611-
// FIXME: cleanuppad
612-
// FIXME: catchpad
626+
// Getelementptr is handled specially.
627+
INST(Trunc, Trunc),
628+
INST(ZExt, ZExt),
629+
INST(SExt, SExt),
630+
INST(FPToUI, FPToUI),
631+
INST(FPToSI, FPToSI),
632+
INST(UIToFP, UIToFP),
633+
INST(SIToFP, SIToFP),
634+
INST(FPTrunc, FPTrunc),
635+
INST(FPExt, FPExt),
636+
INST(PtrToInt, PtrToInt),
637+
INST(IntToPtr, IntToPtr),
638+
INST(BitCast, Bitcast),
639+
INST(AddrSpaceCast, AddrSpaceCast),
613640
// ICmp is handled specially.
614641
// FCmp is handled specially.
615642
// PHI is handled specially.
616-
INST(Freeze, Freeze), INST(Call, Call),
617-
// FIXME: select
643+
INST(Select, Select),
644+
INST(Freeze, Freeze),
645+
INST(Call, Call),
618646
// FIXME: vaarg
619-
INST(ExtractElement, ExtractElement), INST(InsertElement, InsertElement),
620-
// ShuffleVector is handled specially.
621-
// InsertValue is handled specially.
622-
// ExtractValue is handled specially.
623647
// FIXME: landingpad
648+
// FIXME: catchpad
649+
// FIXME: cleanuppad
650+
// clang-format on
624651
};
625652
#undef INST
626653

@@ -776,7 +803,10 @@ LogicalResult Importer::processInstruction(llvm::Instruction *inst) {
776803
case llvm::Instruction::Freeze:
777804
case llvm::Instruction::BitCast:
778805
case llvm::Instruction::ExtractElement:
779-
case llvm::Instruction::InsertElement: {
806+
case llvm::Instruction::InsertElement:
807+
case llvm::Instruction::Select:
808+
case llvm::Instruction::FNeg:
809+
case llvm::Instruction::Unreachable: {
780810
OperationState state(loc, lookupOperationNameFromOpcode(inst->getOpcode()));
781811
SmallVector<Value, 4> ops;
782812
ops.reserve(inst->getNumOperands());

mlir/test/Target/LLVMIR/Import/basic.ll

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,10 @@ define void @FPArithmetic(float %a, float %b, double %c, double %d) {
278278
%10 = frem float %a, %b
279279
; CHECK: %[[a13:[0-9]+]] = llvm.frem %arg2, %arg3 : f64
280280
%11 = frem double %c, %d
281+
; CHECK: %{{.+}} = llvm.fneg %{{.+}} : f32
282+
%12 = fneg float %a
283+
; CHECK: %{{.+}} = llvm.fneg %{{.+}} : f64
284+
%13 = fneg double %c
281285
ret void
282286
}
283287

@@ -605,3 +609,18 @@ define <4 x half> @insert_element(<4 x half>* %vec, half %v, i32 %idx) {
605609
; CHECK: llvm.return %[[V1]]
606610
ret <4 x half> %r
607611
}
612+
613+
; Select
614+
; CHECK-LABEL: llvm.func @select_inst
615+
define void @select_inst(i32 %arg0, i32 %arg1, i1 %pred) {
616+
; CHECK: %{{.+}} = llvm.select %{{.+}}, %{{.+}}, %{{.+}} : i1, i32
617+
%1 = select i1 %pred, i32 %arg0, i32 %arg1
618+
ret void
619+
}
620+
621+
; Unreachable
622+
; CHECK-LABEL: llvm.func @unreachable_inst
623+
define void @unreachable_inst() {
624+
; CHECK: llvm.unreachable
625+
unreachable
626+
}

0 commit comments

Comments
 (0)