From 34e6552dcbb4f6647746588bb35591a97a7992a3 Mon Sep 17 00:00:00 2001 From: Huihui Zhang Date: Thu, 30 Jan 2020 10:44:20 -0800 Subject: [PATCH] [ConstantFold][SVE] Fix constant folding for scalable vector unary operations. Summary: Similar to issue D71445. Scalable vector should not be evaluated element by element. Add support to handle scalable vector UndefValue. Reviewers: sdesmalen, efriedma, apazos, huntergr, willlovett Reviewed By: efriedma Subscribers: tschuett, hiraditya, rkruppe, psnobl, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D73678 --- llvm/lib/IR/ConstantFold.cpp | 17 +++++++++++++---- llvm/test/Analysis/ConstantFolding/vscale.ll | 11 +++++++++++ 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/llvm/lib/IR/ConstantFold.cpp b/llvm/lib/IR/ConstantFold.cpp index 3d8ad88..9fd7dbd 100644 --- a/llvm/lib/IR/ConstantFold.cpp +++ b/llvm/lib/IR/ConstantFold.cpp @@ -953,10 +953,14 @@ Constant *llvm::ConstantFoldInsertValueInstruction(Constant *Agg, Constant *llvm::ConstantFoldUnaryInstruction(unsigned Opcode, Constant *C) { assert(Instruction::isUnaryOp(Opcode) && "Non-unary instruction detected"); - // Handle scalar UndefValue. Vectors are always evaluated per element. - bool HasScalarUndef = !C->getType()->isVectorTy() && isa(C); + // Handle scalar UndefValue and scalable vector UndefValue. Fixed-length + // vectors are always evaluated per element. + bool IsScalableVector = + C->getType()->isVectorTy() && C->getType()->getVectorIsScalable(); + bool HasScalarUndefOrScalableVectorUndef = + (!C->getType()->isVectorTy() || IsScalableVector) && isa(C); - if (HasScalarUndef) { + if (HasScalarUndefOrScalableVectorUndef) { switch (static_cast(Opcode)) { case Instruction::FNeg: return C; // -undef -> undef @@ -966,7 +970,7 @@ Constant *llvm::ConstantFoldUnaryInstruction(unsigned Opcode, Constant *C) { } // Constant should not be UndefValue, unless these are vector constants. - assert(!HasScalarUndef && "Unexpected UndefValue"); + assert(!HasScalarUndefOrScalableVectorUndef && "Unexpected UndefValue"); // We only have FP UnaryOps right now. assert(!isa(C) && "Unexpected Integer UnaryOp"); @@ -979,6 +983,11 @@ Constant *llvm::ConstantFoldUnaryInstruction(unsigned Opcode, Constant *C) { return ConstantFP::get(C->getContext(), neg(CV)); } } else if (VectorType *VTy = dyn_cast(C->getType())) { + // Do not iterate on scalable vector. The number of elements is unknown at + // compile-time. + if (IsScalableVector) + return nullptr; + // Fold each element and create a vector constant from those constants. SmallVector Result; Type *Ty = IntegerType::get(VTy->getContext(), 32); diff --git a/llvm/test/Analysis/ConstantFolding/vscale.ll b/llvm/test/Analysis/ConstantFolding/vscale.ll index 2c6e8f1..cd5bc44 100644 --- a/llvm/test/Analysis/ConstantFolding/vscale.ll +++ b/llvm/test/Analysis/ConstantFolding/vscale.ll @@ -1,6 +1,17 @@ ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py ; RUN: opt < %s -constprop -S | FileCheck %s +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; Unary Operations +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +define @fneg( %val) { +; CHECK-LABEL: @fneg( +; CHECK-NEXT: ret undef +; + %r = fneg undef + ret %r +} ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Binary Operations -- 2.7.4