Skip to content

Conversation

alexey-bataev
Copy link
Member

The translator should not generate zeroinitilizer on fp all zero values,
since it may affect signedness of zeroes

Fixes #160437

Created using spr 1.3.7
@llvmbot
Copy link
Member

llvmbot commented Sep 25, 2025

@llvm/pr-subscribers-mlir-llvm

Author: Alexey Bataev (alexey-bataev)

Changes

The translator should not generate zeroinitilizer on fp all zero values,
since it may affect signedness of zeroes

Fixes #160437


Full diff: https://github.com/llvm/llvm-project/pull/160713.diff

2 Files Affected:

  • (modified) mlir/lib/Target/LLVMIR/ModuleTranslation.cpp (+1-1)
  • (added) mlir/test/Target/LLVMIR/global_float_array.mlir (+4)
diff --git a/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp b/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
index adc5a74e2031f..5a3eb209f0a92 100644
--- a/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
+++ b/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
@@ -647,7 +647,7 @@ llvm::Constant *mlir::LLVM::detail::getLLVMConstant(
           llvm::ElementCount::get(numElements, /*Scalable=*/isScalable), child);
     if (llvmType->isArrayTy()) {
       auto *arrayType = llvm::ArrayType::get(elementType, numElements);
-      if (child->isZeroValue()) {
+      if (child->isZeroValue() && !elementType->isFPOrFPVectorTy()) {
         return llvm::ConstantAggregateZero::get(arrayType);
       } else {
         if (llvm::ConstantDataSequential::isElementTypeCompatible(
diff --git a/mlir/test/Target/LLVMIR/global_float_array.mlir b/mlir/test/Target/LLVMIR/global_float_array.mlir
new file mode 100644
index 0000000000000..eba7948d2c55e
--- /dev/null
+++ b/mlir/test/Target/LLVMIR/global_float_array.mlir
@@ -0,0 +1,4 @@
+// RUN: mlir-translate -mlir-to-llvmir %s | FileCheck %s
+
+// CHECK: @test = internal global [1 x float] [float -0.000000e+00]
+llvm.mlir.global internal @test(dense<-0.000000e+00> : tensor<1xf32>) {addr_space = 0 : i32} : !llvm.array<1 x f32>

@llvmbot
Copy link
Member

llvmbot commented Sep 25, 2025

@llvm/pr-subscribers-mlir

Author: Alexey Bataev (alexey-bataev)

Changes

The translator should not generate zeroinitilizer on fp all zero values,
since it may affect signedness of zeroes

Fixes #160437


Full diff: https://github.com/llvm/llvm-project/pull/160713.diff

2 Files Affected:

  • (modified) mlir/lib/Target/LLVMIR/ModuleTranslation.cpp (+1-1)
  • (added) mlir/test/Target/LLVMIR/global_float_array.mlir (+4)
diff --git a/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp b/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
index adc5a74e2031f..5a3eb209f0a92 100644
--- a/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
+++ b/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
@@ -647,7 +647,7 @@ llvm::Constant *mlir::LLVM::detail::getLLVMConstant(
           llvm::ElementCount::get(numElements, /*Scalable=*/isScalable), child);
     if (llvmType->isArrayTy()) {
       auto *arrayType = llvm::ArrayType::get(elementType, numElements);
-      if (child->isZeroValue()) {
+      if (child->isZeroValue() && !elementType->isFPOrFPVectorTy()) {
         return llvm::ConstantAggregateZero::get(arrayType);
       } else {
         if (llvm::ConstantDataSequential::isElementTypeCompatible(
diff --git a/mlir/test/Target/LLVMIR/global_float_array.mlir b/mlir/test/Target/LLVMIR/global_float_array.mlir
new file mode 100644
index 0000000000000..eba7948d2c55e
--- /dev/null
+++ b/mlir/test/Target/LLVMIR/global_float_array.mlir
@@ -0,0 +1,4 @@
+// RUN: mlir-translate -mlir-to-llvmir %s | FileCheck %s
+
+// CHECK: @test = internal global [1 x float] [float -0.000000e+00]
+llvm.mlir.global internal @test(dense<-0.000000e+00> : tensor<1xf32>) {addr_space = 0 : i32} : !llvm.array<1 x f32>

Copy link
Contributor

@vzakhari vzakhari left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thank you!

@alexey-bataev alexey-bataev merged commit cc0fecf into main Sep 26, 2025
12 checks passed
@alexey-bataev alexey-bataev deleted the users/alexey-bataev/spr/mlirmlirtollvm-fix-zero-fp-value-to-array-translation branch September 26, 2025 00:20
mahesh-attarde pushed a commit to mahesh-attarde/llvm-project that referenced this pull request Oct 3, 2025
The translator should not generate zeroinitilizer on fp all zero values,
since it may affect signedness of zeroes

Fixes llvm#160437
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[flang] Incorrect sign of array initexpr
3 participants