Skip to content

[mlir][llvm] Fix verifier for const float #74247

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2539,6 +2539,20 @@ LogicalResult LLVM::ConstantOp::verify() {
if (!llvm::isa<IntegerAttr, ArrayAttr, FloatAttr, ElementsAttr>(getValue()))
return emitOpError()
<< "only supports integer, float, string or elements attributes";
if (auto floatAttr = dyn_cast<FloatAttr>(getValue())) {
const llvm::fltSemantics &sem = floatAttr.getValue().getSemantics();
unsigned floatWidth = APFloat::getSizeInBits(sem);
if (auto floatTy = dyn_cast<FloatType>(getType())) {
if (floatTy.getWidth() != floatWidth) {
return emitOpError() << "expected float type of width " << floatWidth;
}
}
// See the comment for getLLVMConstant for more details about why 8-bit
// floats can be represented by integers.
if (getType().isa<IntegerType>() && !getType().isInteger(floatWidth)) {
return emitOpError() << "expected integer type of width " << floatWidth;
}
}
return success();
}

Expand Down
16 changes: 16 additions & 0 deletions mlir/test/Target/LLVMIR/llvmir-invalid.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,22 @@ llvm.func @struct_wrong_attribute_element_type() -> !llvm.struct<(f64, f64)> {

// -----

llvm.func @incompatible_float_attribute_type() -> f32 {
// expected-error @below{{expected float type of width 64}}
%cst = llvm.mlir.constant(1.0 : f64) : f32
llvm.return %cst : f32
}

// -----

llvm.func @incompatible_integer_type_for_float_attr() -> i32 {
// expected-error @below{{expected integer type of width 16}}
%cst = llvm.mlir.constant(1.0 : f16) : i32
llvm.return %cst : i32
}

// -----

// expected-error @below{{unsupported constant value}}
llvm.mlir.global internal constant @test([2.5, 7.4]) : !llvm.array<2 x f64>

Expand Down
2 changes: 1 addition & 1 deletion mlir/test/Target/LLVMIR/llvmir.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ llvm.mlir.global external @explicit_undef() : i32 {
// CHECK: @int_gep = internal constant ptr getelementptr (i32, ptr @i32_global, i32 2)
llvm.mlir.global internal constant @int_gep() : !llvm.ptr {
%addr = llvm.mlir.addressof @i32_global : !llvm.ptr
%_c0 = llvm.mlir.constant(2: i32) :i32
%_c0 = llvm.mlir.constant(2: i32) : i32
%gepinit = llvm.getelementptr %addr[%_c0] : (!llvm.ptr, i32) -> !llvm.ptr, i32
llvm.return %gepinit : !llvm.ptr
}
Expand Down