From: Alex Zinenko Date: Fri, 27 Dec 2019 09:54:01 +0000 (+0100) Subject: [mlir] Floating constants for import-llvm X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=cda94d3e8ae3679eb75988afc85faa1e14068a83;p=platform%2Fupstream%2Fllvm.git [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 --- diff --git a/mlir/lib/Target/LLVMIR/ConvertFromLLVMIR.cpp b/mlir/lib/Target/LLVMIR/ConvertFromLLVMIR.cpp index 4466fb5..cc13360 100644 --- a/mlir/lib/Target/LLVMIR/ConvertFromLLVMIR.cpp +++ b/mlir/lib/Target/LLVMIR/ConvertFromLLVMIR.cpp @@ -203,6 +203,12 @@ Attribute Importer::getConstantAsAttr(llvm::Constant *value) { if (auto *c = dyn_cast(value)) if (c->isString()) return b.getStringAttr(c->getAsString()); + if (auto *c = dyn_cast(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(); } diff --git a/mlir/test/Target/import.ll b/mlir/test/Target/import.ll index a8e0b44..1ea7af1 100644 --- a/mlir/test/Target/import.ll +++ b/mlir/test/Target/import.ll @@ -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 +} \ No newline at end of file