Index: mlir/lib/Target/LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.cpp =================================================================== --- mlir/lib/Target/LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.cpp +++ mlir/lib/Target/LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.cpp @@ -342,22 +342,29 @@ if (auto invOp = dyn_cast(opInst)) { auto operands = moduleTranslation.lookupValues(opInst.getOperands()); ArrayRef operandsRef(operands); + llvm::Value *result; if (auto attr = opInst.getAttrOfType("callee")) { - builder.CreateInvoke(moduleTranslation.lookupFunction(attr.getValue()), - moduleTranslation.lookupBlock(invOp.getSuccessor(0)), - moduleTranslation.lookupBlock(invOp.getSuccessor(1)), - operandsRef); + result = builder.CreateInvoke( + moduleTranslation.lookupFunction(attr.getValue()), + moduleTranslation.lookupBlock(invOp.getSuccessor(0)), + moduleTranslation.lookupBlock(invOp.getSuccessor(1)), operandsRef); } else { auto *calleePtrType = cast(operandsRef.front()->getType()); auto *calleeType = cast(calleePtrType->getElementType()); - builder.CreateInvoke(calleeType, operandsRef.front(), - moduleTranslation.lookupBlock(invOp.getSuccessor(0)), - moduleTranslation.lookupBlock(invOp.getSuccessor(1)), - operandsRef.drop_front()); + result = builder.CreateInvoke( + calleeType, operandsRef.front(), + moduleTranslation.lookupBlock(invOp.getSuccessor(0)), + moduleTranslation.lookupBlock(invOp.getSuccessor(1)), + operandsRef.drop_front()); } - return success(); + // InvokeOp can only have 0 or 1 result + if (invOp->getNumResults() != 0) { + moduleTranslation.mapValue(opInst.getResult(0), result); + return success(); + } + return success(result->getType()->isVoidTy()); } if (auto lpOp = dyn_cast(opInst)) { Index: mlir/test/Target/LLVMIR/llvmir.mlir =================================================================== --- mlir/test/Target/LLVMIR/llvmir.mlir +++ mlir/test/Target/LLVMIR/llvmir.mlir @@ -1296,6 +1296,38 @@ %8 = llvm.invoke @bar(%6) to ^bb2 unwind ^bb1 : (!llvm.ptr) -> !llvm.ptr } +// ----- + +llvm.mlir.global external constant @_ZTIi() : !llvm.ptr +llvm.func @foo() -> i8 +llvm.func @__gxx_personality_v0(...) -> i32 + +// CHECK-LABEL: @invoke_result +// CHECK-SAME: %[[a0:[0-9]+]] +llvm.func @invoke_result(%arg0 : !llvm.ptr) attributes { personality = @__gxx_personality_v0 } { + %0 = llvm.mlir.addressof @_ZTIi : !llvm.ptr> +// CHECK: %[[a1:[0-9]+]] = invoke i8 @foo() +// CHECK-NEXT: to label %[[normal:[0-9]+]] unwind label %[[unwind:[0-9]+]] + %1 = llvm.invoke @foo() to ^bb1 unwind ^bb2 : () -> i8 + +// CHECK: [[normal]]: +// CHECK-NEXT: store i8 %[[a1]], i8* %[[a0]] +// CHECK-NEXT: ret void +^bb1: + llvm.store %1, %arg0 : !llvm.ptr + llvm.return + +// CHECK: [[unwind]]: +// CHECK-NEXT: landingpad { i8*, i32 } +// CHECK-NEXT: catch i8** @_ZTIi +// CHECK-NEXT: ret void +^bb2: + %7 = llvm.landingpad (catch %0 : !llvm.ptr>) : !llvm.struct<(ptr, i32)> + llvm.return +} + +// ----- + // CHECK-LABEL: @callFreezeOp llvm.func @callFreezeOp(%x : i32) { // CHECK: freeze i32 %{{[0-9]+}}