[llvm] [InstCombine] Increase coverage of shuffle-gep (PR #139725)

Ramkumar Ramachandra via llvm-commits llvm-commits at lists.llvm.org
Tue May 13 06:09:16 PDT 2025


https://github.com/artagnon created https://github.com/llvm/llvm-project/pull/139725

There is an uncovered codepath in InstCombineVectorOps, where a new GEP instruction is created via buildNew. Fix this coverage hole.

>From 5837194f7e31375c3fe8a99d12cd9542dd7ea6b6 Mon Sep 17 00:00:00 2001
From: Ramkumar Ramachandra <ramkumar.ramachandra at codasip.com>
Date: Tue, 13 May 2025 13:54:40 +0100
Subject: [PATCH 1/2] [InstCombine] Pre-commit change for coverage

---
 .../InstCombine/InstCombineVectorOps.cpp        |  1 +
 llvm/test/Transforms/InstCombine/vec_shuffle.ll | 17 ++++++++++++++++-
 2 files changed, 17 insertions(+), 1 deletion(-)

diff --git a/llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp b/llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp
index f6423cb40492e..54540c49edef3 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp
@@ -1978,6 +1978,7 @@ static Value *buildNew(Instruction *I, ArrayRef<Value*> NewOps,
                                 DestTy);
     }
     case Instruction::GetElementPtr: {
+      llvm_unreachable("Foo");
       Value *Ptr = NewOps[0];
       ArrayRef<Value*> Idx = NewOps.slice(1);
       return Builder.CreateGEP(cast<GEPOperator>(I)->getSourceElementType(),
diff --git a/llvm/test/Transforms/InstCombine/vec_shuffle.ll b/llvm/test/Transforms/InstCombine/vec_shuffle.ll
index dd9fab794917f..63b3052acd6bd 100644
--- a/llvm/test/Transforms/InstCombine/vec_shuffle.ll
+++ b/llvm/test/Transforms/InstCombine/vec_shuffle.ll
@@ -1,5 +1,6 @@
 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
-; RUN: opt < %s -passes=instcombine -S | FileCheck %s
+; REQURIES: asserts
+; RUN: not --crash opt %s -passes=instcombine -disable-output
 
 declare <2 x half> @llvm.fabs.v2f16(<2 x half>)
 declare <2 x float> @llvm.fabs.v2f32(<2 x float>)
@@ -248,6 +249,20 @@ define <2 x i8> @test13a(i8 %x1, i8 %x2) {
   ret <2 x i8> %D
 }
 
+define <1 x ptr> @shuffle_gep(ptr %x1, ptr %x2) {
+; CHECK-LABEL: @shuffle_gep(
+; CHECK-NEXT:    [[TMP1:%.*]] = insertelement <2 x i8> poison, i8 [[X2:%.*]], i64 0
+; CHECK-NEXT:    [[TMP2:%.*]] = insertelement <2 x i8> [[TMP1]], i8 [[X1:%.*]], i64 1
+; CHECK-NEXT:    [[D:%.*]] = add <2 x i8> [[TMP2]], <i8 7, i8 5>
+; CHECK-NEXT:    ret <2 x i8> [[D]]
+;
+  %ins.1 = insertelement <2 x ptr> poison, ptr %x1, i32 0
+  %ins.2 = insertelement <2 x ptr> %ins.1, ptr %x2, i32 1
+  %gep = getelementptr i8, <2 x ptr> %ins.2, i64 5
+  %ret = shufflevector <2 x ptr> %gep, <2 x ptr> poison, <1 x i32> <i32 1>
+  ret <1 x ptr> %ret
+}
+
 ; Increasing length of vector ops is not a good canonicalization.
 
 define <3 x i32> @add_wider(i32 %y, i32 %z) {

>From c1aa868908ed772f051e4e7e946921177583c5b0 Mon Sep 17 00:00:00 2001
From: Ramkumar Ramachandra <ramkumar.ramachandra at codasip.com>
Date: Tue, 13 May 2025 13:56:38 +0100
Subject: [PATCH 2/2] [InstCombine] Increase coverage of shuffle-gep

There is an uncovered codepath in InstCombineVectorOps, where a new GEP
instruction is created via buildNew. Fix this coverage hole.
---
 .../Transforms/InstCombine/InstCombineVectorOps.cpp    |  1 -
 llvm/test/Transforms/InstCombine/vec_shuffle.ll        | 10 ++++------
 2 files changed, 4 insertions(+), 7 deletions(-)

diff --git a/llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp b/llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp
index 54540c49edef3..f6423cb40492e 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp
@@ -1978,7 +1978,6 @@ static Value *buildNew(Instruction *I, ArrayRef<Value*> NewOps,
                                 DestTy);
     }
     case Instruction::GetElementPtr: {
-      llvm_unreachable("Foo");
       Value *Ptr = NewOps[0];
       ArrayRef<Value*> Idx = NewOps.slice(1);
       return Builder.CreateGEP(cast<GEPOperator>(I)->getSourceElementType(),
diff --git a/llvm/test/Transforms/InstCombine/vec_shuffle.ll b/llvm/test/Transforms/InstCombine/vec_shuffle.ll
index 63b3052acd6bd..fa34a42714c46 100644
--- a/llvm/test/Transforms/InstCombine/vec_shuffle.ll
+++ b/llvm/test/Transforms/InstCombine/vec_shuffle.ll
@@ -1,6 +1,5 @@
 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
-; REQURIES: asserts
-; RUN: not --crash opt %s -passes=instcombine -disable-output
+; RUN: opt < %s -passes=instcombine -S | FileCheck %s
 
 declare <2 x half> @llvm.fabs.v2f16(<2 x half>)
 declare <2 x float> @llvm.fabs.v2f32(<2 x float>)
@@ -251,10 +250,9 @@ define <2 x i8> @test13a(i8 %x1, i8 %x2) {
 
 define <1 x ptr> @shuffle_gep(ptr %x1, ptr %x2) {
 ; CHECK-LABEL: @shuffle_gep(
-; CHECK-NEXT:    [[TMP1:%.*]] = insertelement <2 x i8> poison, i8 [[X2:%.*]], i64 0
-; CHECK-NEXT:    [[TMP2:%.*]] = insertelement <2 x i8> [[TMP1]], i8 [[X1:%.*]], i64 1
-; CHECK-NEXT:    [[D:%.*]] = add <2 x i8> [[TMP2]], <i8 7, i8 5>
-; CHECK-NEXT:    ret <2 x i8> [[D]]
+; CHECK-NEXT:    [[TMP1:%.*]] = insertelement <1 x ptr> poison, ptr [[X2:%.*]], i64 0
+; CHECK-NEXT:    [[RET:%.*]] = getelementptr i8, <1 x ptr> [[TMP1]], i64 5
+; CHECK-NEXT:    ret <1 x ptr> [[RET]]
 ;
   %ins.1 = insertelement <2 x ptr> poison, ptr %x1, i32 0
   %ins.2 = insertelement <2 x ptr> %ins.1, ptr %x2, i32 1



More information about the llvm-commits mailing list