[PATCH] D71912: [mlir] Floating constants for import-llvm
shraiysh via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Thu Dec 26 11:19:29 PST 2019
shraiysh created this revision.
Herald added subscribers: llvm-commits, burmako, jpienaar, rriddle, mehdi_amini.
Herald added a project: LLVM.
shraiysh added a reviewer: ftynse.
`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
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D71912
Files:
mlir/lib/Target/LLVMIR/ConvertFromLLVMIR.cpp
mlir/test/Target/import.ll
Index: mlir/test/Target/import.ll
===================================================================
--- mlir/test/Target/import.ll
+++ mlir/test/Target/import.ll
@@ -180,3 +180,32 @@
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
+ %10 = fdiv double %c, %d
+ ; CHECK: %[[a11:[0-9]+]] = llvm.frem %arg0, %arg1 : !llvm.float
+ %9 = frem float %a, %b
+ ; CHECK: %[[a13:[0-9]+]] = llvm.frem %arg2, %arg3 : !llvm.double
+ %11 = frem double %c, %d
+ ret void
+}
\ No newline at end of file
Index: mlir/lib/Target/LLVMIR/ConvertFromLLVMIR.cpp
===================================================================
--- mlir/lib/Target/LLVMIR/ConvertFromLLVMIR.cpp
+++ mlir/lib/Target/LLVMIR/ConvertFromLLVMIR.cpp
@@ -203,6 +203,12 @@
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();
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D71912.235366.patch
Type: text/x-patch
Size: 2381 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20191226/04814c5d/attachment.bin>
More information about the llvm-commits
mailing list