Skip to content
This repository has been archived by the owner on Apr 23, 2021. It is now read-only.

Commit

Permalink
Standard-to-LLVM conversion: check that operands have LLVM types
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ftynse authored and jpienaar committed Oct 11, 2019
1 parent d0c127e commit 51a9eb0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
6 changes: 6 additions & 0 deletions lib/Conversion/StandardToLLVM/ConvertStandardToLLVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,12 @@ struct BinaryOpLLVMOpLowering : public LLVMLegalizationPattern<SourceOp> {
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>();

Expand Down
13 changes: 12 additions & 1 deletion test/Conversion/StandardToLLVM/standard-to-llvm.mlir
Original file line number Diff line number Diff line change
@@ -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] }*">)
Expand All @@ -17,3 +17,14 @@ func @strided_memref(%ind: index) {
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
}

0 comments on commit 51a9eb0

Please sign in to comment.