From 3ac1cef8665935140f10ac1db8fbc587b56dcfa8 Mon Sep 17 00:00:00 2001 From: Joshua Cranmer Date: Tue, 23 May 2023 15:00:19 -0400 Subject: [PATCH] [CodeGen] Fix crash in CodeGenPrepare::optimizeGatherScatterInst. Reviewed By: craig.topper Differential Revision: https://reviews.llvm.org/D151141 --- llvm/lib/CodeGen/CodeGenPrepare.cpp | 3 ++- .../CodeGenPrepare/X86/masked-gather-struct-gep.ll | 23 ++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 llvm/test/Transforms/CodeGenPrepare/X86/masked-gather-struct-gep.ll diff --git a/llvm/lib/CodeGen/CodeGenPrepare.cpp b/llvm/lib/CodeGen/CodeGenPrepare.cpp index 3e8d0a4..d806c7c 100644 --- a/llvm/lib/CodeGen/CodeGenPrepare.cpp +++ b/llvm/lib/CodeGen/CodeGenPrepare.cpp @@ -5708,7 +5708,8 @@ bool CodeGenPrepare::optimizeGatherScatterInst(Instruction *MemoryInst, // Create a scalar GEP if there are more than 2 operands. if (Ops.size() != 2) { // Replace the last index with 0. - Ops[FinalIndex] = Constant::getNullValue(ScalarIndexTy); + Ops[FinalIndex] = + Constant::getNullValue(Ops[FinalIndex]->getType()->getScalarType()); Base = Builder.CreateGEP(SourceTy, Base, ArrayRef(Ops).drop_front()); SourceTy = GetElementPtrInst::getIndexedType( SourceTy, ArrayRef(Ops).drop_front()); diff --git a/llvm/test/Transforms/CodeGenPrepare/X86/masked-gather-struct-gep.ll b/llvm/test/Transforms/CodeGenPrepare/X86/masked-gather-struct-gep.ll new file mode 100644 index 0000000..ea07a5f --- /dev/null +++ b/llvm/test/Transforms/CodeGenPrepare/X86/masked-gather-struct-gep.ll @@ -0,0 +1,23 @@ +; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 2 +; RUN: opt -S -codegenprepare < %s | FileCheck %s +; REQUIRES: x86-registered-target +target triple = "x86_64-pc-linux" + +%s = type <{ float, i32, i8, [3 x i8] }> + +declare <4 x float> @llvm.masked.gather.v4f32.v4p0(<4 x ptr>, i32 immarg, <4 x i1>, <4 x float>) + +define <4 x float> @foo(ptr %p) { +; CHECK-LABEL: define <4 x float> @foo +; CHECK-SAME: (ptr [[P:%.*]]) { +; CHECK-NEXT: [[TMP1:%.*]] = bitcast ptr [[P]] to ptr +; CHECK-NEXT: [[TMP2:%.*]] = getelementptr float, ptr [[TMP1]], <4 x i32> zeroinitializer +; CHECK-NEXT: [[GATHER:%.*]] = call <4 x float> @llvm.masked.gather.v4f32.v4p0(<4 x ptr> [[TMP2]], i32 0, <4 x i1> zeroinitializer, <4 x float> zeroinitializer) +; CHECK-NEXT: ret <4 x float> [[GATHER]] +; + %base.splatinsert = insertelement <4 x ptr> poison, ptr %p, i32 0 + %base = shufflevector <4 x ptr> %base.splatinsert, <4 x ptr> poison, <4 x i32> + %gep = getelementptr %s, <4 x ptr> %base, <4 x i64> zeroinitializer, <4 x i32> zeroinitializer + %gather = call <4 x float> @llvm.masked.gather.v4f32.v4p0(<4 x ptr> %gep, i32 0, <4 x i1> zeroinitializer, <4 x float> zeroinitializer) + ret <4 x float> %gather +} -- 2.7.4