Author: Nikita Popov
Date: 2025-12-16T09:48:28+01:00
New Revision: af3f2da253bb7a2edcddd67467023f3531700ae4

URL: 
https://github.com/llvm/llvm-project/commit/af3f2da253bb7a2edcddd67467023f3531700ae4
DIFF: 
https://github.com/llvm/llvm-project/commit/af3f2da253bb7a2edcddd67467023f3531700ae4.diff

LOG: [CGExprScalar] Fix inc/dec of vector larger than 64-bit (#172301)

Use getSigned() to create the 1 or -1 constant, so it gets properly sign
extended.

This miscompile was found while working on
https://github.com/llvm/llvm-project/pull/171456.

Added: 
    

Modified: 
    clang/lib/CodeGen/CGExprScalar.cpp
    clang/test/CodeGen/SystemZ/zvector.c

Removed: 
    


################################################################################
diff  --git a/clang/lib/CodeGen/CGExprScalar.cpp 
b/clang/lib/CodeGen/CGExprScalar.cpp
index 769bc37b0e131..9b6497fca829a 100644
--- a/clang/lib/CodeGen/CGExprScalar.cpp
+++ b/clang/lib/CodeGen/CGExprScalar.cpp
@@ -3335,7 +3335,7 @@ ScalarExprEmitter::EmitScalarPrePostIncDec(const 
UnaryOperator *E, LValue LV,
   // Vector increment/decrement.
   } else if (type->isVectorType()) {
     if (type->hasIntegerRepresentation()) {
-      llvm::Value *amt = llvm::ConstantInt::get(value->getType(), amount);
+      llvm::Value *amt = llvm::ConstantInt::getSigned(value->getType(), 
amount);
 
       value = Builder.CreateAdd(value, amt, isInc ? "inc" : "dec");
     } else {

diff  --git a/clang/test/CodeGen/SystemZ/zvector.c 
b/clang/test/CodeGen/SystemZ/zvector.c
index a0b654d9acc9a..35bee8d41df61 100644
--- a/clang/test/CodeGen/SystemZ/zvector.c
+++ b/clang/test/CodeGen/SystemZ/zvector.c
@@ -298,10 +298,10 @@ void test_postinc(void) {
 // CHECK-NEXT:    [[DEC7:%.*]] = add <2 x i64> [[TMP7]], splat (i64 -1)
 // CHECK-NEXT:    store volatile <2 x i64> [[DEC7]], ptr @ul2, align 8
 // CHECK-NEXT:    [[TMP8:%.*]] = load volatile <1 x i128>, ptr @slll2, align 8
-// CHECK-NEXT:    [[DEC8:%.*]] = add <1 x i128> [[TMP8]], splat (i128 
18446744073709551615)
+// CHECK-NEXT:    [[DEC8:%.*]] = add <1 x i128> [[TMP8]], splat (i128 -1)
 // CHECK-NEXT:    store volatile <1 x i128> [[DEC8]], ptr @slll2, align 8
 // CHECK-NEXT:    [[TMP9:%.*]] = load volatile <1 x i128>, ptr @ulll2, align 8
-// CHECK-NEXT:    [[DEC9:%.*]] = add <1 x i128> [[TMP9]], splat (i128 
18446744073709551615)
+// CHECK-NEXT:    [[DEC9:%.*]] = add <1 x i128> [[TMP9]], splat (i128 -1)
 // CHECK-NEXT:    store volatile <1 x i128> [[DEC9]], ptr @ulll2, align 8
 // CHECK-NEXT:    [[TMP10:%.*]] = load volatile <2 x double>, ptr @fd2, align 8
 // CHECK-NEXT:    [[DEC10:%.*]] = fadd <2 x double> [[TMP10]], splat (double 
-1.000000e+00)
@@ -356,10 +356,10 @@ void test_predec(void) {
 // CHECK-NEXT:    [[DEC7:%.*]] = add <2 x i64> [[TMP7]], splat (i64 -1)
 // CHECK-NEXT:    store volatile <2 x i64> [[DEC7]], ptr @ul2, align 8
 // CHECK-NEXT:    [[TMP8:%.*]] = load volatile <1 x i128>, ptr @slll2, align 8
-// CHECK-NEXT:    [[DEC8:%.*]] = add <1 x i128> [[TMP8]], splat (i128 
18446744073709551615)
+// CHECK-NEXT:    [[DEC8:%.*]] = add <1 x i128> [[TMP8]], splat (i128 -1)
 // CHECK-NEXT:    store volatile <1 x i128> [[DEC8]], ptr @slll2, align 8
 // CHECK-NEXT:    [[TMP9:%.*]] = load volatile <1 x i128>, ptr @ulll2, align 8
-// CHECK-NEXT:    [[DEC9:%.*]] = add <1 x i128> [[TMP9]], splat (i128 
18446744073709551615)
+// CHECK-NEXT:    [[DEC9:%.*]] = add <1 x i128> [[TMP9]], splat (i128 -1)
 // CHECK-NEXT:    store volatile <1 x i128> [[DEC9]], ptr @ulll2, align 8
 // CHECK-NEXT:    [[TMP10:%.*]] = load volatile <2 x double>, ptr @fd2, align 8
 // CHECK-NEXT:    [[DEC10:%.*]] = fadd <2 x double> [[TMP10]], splat (double 
-1.000000e+00)


        
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to