Standard-to-LLVM conversion: check that operands have LLVM types

In Standard to LLVM dialect conversion, the binary op conversion pattern
implicitly assumed some operands were of LLVM IR dialect type. This is not
necessarily true, for example if the Ops that produce those operands did not
match the existing convresion patterns. Check if all operands are of LLVM IR
dialect type and if not, fail to patch the binary op pattern.

Closes #168

PiperOrigin-RevId: 274063207
diff --git a/lib/Conversion/StandardToLLVM/ConvertStandardToLLVM.cpp b/lib/Conversion/StandardToLLVM/ConvertStandardToLLVM.cpp
index 31a9e13..76de499 100644
--- a/lib/Conversion/StandardToLLVM/ConvertStandardToLLVM.cpp
+++ b/lib/Conversion/StandardToLLVM/ConvertStandardToLLVM.cpp
@@ -466,6 +466,12 @@
                                   SourceOp>::value,
                   "expected single result op");
 
+    // Cannot convert ops if their operands are not of LLVM type.
+    for (Value *operand : operands) {
+      if (!operand || !operand->getType().isa<LLVM::LLVMType>())
+        return this->matchFailure();
+    }
+
     auto loc = op->getLoc();
     auto llvmArrayTy = operands[0]->getType().cast<LLVM::LLVMType>();
 
diff --git a/test/Conversion/StandardToLLVM/standard-to-llvm.mlir b/test/Conversion/StandardToLLVM/standard-to-llvm.mlir
index 0f40509..abbda44 100644
--- a/test/Conversion/StandardToLLVM/standard-to-llvm.mlir
+++ b/test/Conversion/StandardToLLVM/standard-to-llvm.mlir
@@ -1,4 +1,4 @@
-// RUN: mlir-opt %s -lower-to-llvm | FileCheck %s
+// RUN: mlir-opt %s -lower-to-llvm -split-input-file -verify-diagnostics | FileCheck %s
 
 // CHECK-LABEL: func @address_space(
 //       CHECK:   %{{.*}}: !llvm<"{ float addrspace(7)*, i64, [1 x i64], [1 x i64] }*">)
@@ -17,3 +17,14 @@
   std.return
 }
 
+// -----
+
+// This should not crash. The first operation cannot be converted, so the
+// secound should not match. This attempts to convert `return` to `llvm.return`
+// and complains about non-LLVM types.
+func @unknown_source() -> i32 {
+  %0 = "foo"() : () -> i32
+  %1 = addi %0, %0 : i32
+  // expected-error@+1 {{must be LLVM dialect type}}
+  return %1 : i32
+}