[mlir] Floating constants for import-llvm

Summary:
`mlir-translate -import-llvm test.ll`  was going into segmentation fault if `test.ll` had `float` or `double` constants.
For example,
```
%3 = fadd double 3.030000e+01, %0
```
Now, it is handled in `Importer::getConstantAsAttr` (similar behaviour as normal integers)
Added tests for FP arithmetic

Reviewers: ftynse, mehdi_amini

Reviewed By: ftynse, mehdi_amini

Subscribers: shauheen, mehdi_amini, rriddle, jpienaar, burmako, llvm-commits

Tags: #llvm

Differential Revision: https://reviews.llvm.org/D71912
This commit is contained in:
Alex Zinenko 2019-12-27 10:54:01 +01:00
parent e2d9f4e6a2
commit cda94d3e8a
2 changed files with 35 additions and 0 deletions

View File

@ -203,6 +203,12 @@ Attribute Importer::getConstantAsAttr(llvm::Constant *value) {
if (auto *c = dyn_cast<llvm::ConstantDataArray>(value))
if (c->isString())
return b.getStringAttr(c->getAsString());
if (auto *c = dyn_cast<llvm::ConstantFP>(value)) {
if (c->getType()->isDoubleTy())
return b.getFloatAttr(FloatType::getF64(context), c->getValueAPF());
else if (c->getType()->isFloatingPointTy())
return b.getFloatAttr(FloatType::getF32(context), c->getValueAPF());
}
return Attribute();
}

View File

@ -180,3 +180,32 @@ define void @f6(void (i16) *%fn) {
call void %fn(i16 0)
ret void
}
; CHECK-LABEL: llvm.func @FPArithmetic(%arg0: !llvm.float, %arg1: !llvm.float, %arg2: !llvm.double, %arg3: !llvm.double)
define void @FPArithmetic(float %a, float %b, double %c, double %d) {
; CHECK: %[[a1:[0-9]+]] = llvm.mlir.constant(3.030000e+01 : f64) : !llvm.double
; CHECK: %[[a2:[0-9]+]] = llvm.mlir.constant(3.030000e+01 : f32) : !llvm.float
; CHECK: %[[a3:[0-9]+]] = llvm.fadd %[[a2]], %arg0 : !llvm.float
%1 = fadd float 0x403E4CCCC0000000, %a
; CHECK: %[[a4:[0-9]+]] = llvm.fadd %arg0, %arg1 : !llvm.float
%2 = fadd float %a, %b
; CHECK: %[[a5:[0-9]+]] = llvm.fadd %[[a1]], %arg2 : !llvm.double
%3 = fadd double 3.030000e+01, %c
; CHECK: %[[a6:[0-9]+]] = llvm.fsub %arg0, %arg1 : !llvm.float
%4 = fsub float %a, %b
; CHECK: %[[a7:[0-9]+]] = llvm.fsub %arg2, %arg3 : !llvm.double
%5 = fsub double %c, %d
; CHECK: %[[a8:[0-9]+]] = llvm.fmul %arg0, %arg1 : !llvm.float
%6 = fmul float %a, %b
; CHECK: %[[a9:[0-9]+]] = llvm.fmul %arg2, %arg3 : !llvm.double
%7 = fmul double %c, %d
; CHECK: %[[a10:[0-9]+]] = llvm.fdiv %arg0, %arg1 : !llvm.float
%8 = fdiv float %a, %b
; CHECK: %[[a12:[0-9]+]] = llvm.fdiv %arg2, %arg3 : !llvm.double
%9 = fdiv double %c, %d
; CHECK: %[[a11:[0-9]+]] = llvm.frem %arg0, %arg1 : !llvm.float
%10 = frem float %a, %b
; CHECK: %[[a13:[0-9]+]] = llvm.frem %arg2, %arg3 : !llvm.double
%11 = frem double %c, %d
ret void
}