From c2634fc6abe73acf0d9c4421071948e43d96d7eb Mon Sep 17 00:00:00 2001 From: David Sherwood Date: Tue, 21 Sep 2021 12:11:04 +0100 Subject: [PATCH] [Analysis] Fix issues when querying vscale attributes on functions There are several places in the code that are currently broken as they assume an Instruction always has a parent Function when attempting to get the vscale_range attribute. This patch adds checks that an Instruction has a parent. I've added a test for a parentless @llvm.vscale intrinsic call here: unittests/Analysis/ValueTrackingTest.cpp Differential Revision: https://reviews.llvm.org/D110158 --- llvm/lib/Analysis/ValueTracking.cpp | 3 ++- llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp | 9 ++++++--- llvm/unittests/Analysis/ValueTrackingTest.cpp | 17 +++++++++++++++++ 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp index 174ee97..ab5c388 100644 --- a/llvm/lib/Analysis/ValueTracking.cpp +++ b/llvm/lib/Analysis/ValueTracking.cpp @@ -1685,7 +1685,8 @@ static void computeKnownBitsFromOperator(const Operator *I, Known.Zero.setBitsFrom(31); break; case Intrinsic::vscale: { - if (!II->getFunction()->hasFnAttribute(Attribute::VScaleRange)) + if (!II->getParent() || + !II->getFunction()->hasFnAttribute(Attribute::VScaleRange)) break; auto VScaleRange = II->getFunction() diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp index cca5265..5d18967 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp @@ -963,7 +963,8 @@ Instruction *InstCombinerImpl::visitTrunc(TruncInst &Trunc) { } if (match(Src, m_VScale(DL))) { - if (Trunc.getFunction()->hasFnAttribute(Attribute::VScaleRange)) { + if (Trunc.getFunction() && + Trunc.getFunction()->hasFnAttribute(Attribute::VScaleRange)) { unsigned MaxVScale = Trunc.getFunction() ->getFnAttribute(Attribute::VScaleRange) .getVScaleRangeArgs() @@ -1334,7 +1335,8 @@ Instruction *InstCombinerImpl::visitZExt(ZExtInst &CI) { } if (match(Src, m_VScale(DL))) { - if (CI.getFunction()->hasFnAttribute(Attribute::VScaleRange)) { + if (CI.getFunction() && + CI.getFunction()->hasFnAttribute(Attribute::VScaleRange)) { unsigned MaxVScale = CI.getFunction() ->getFnAttribute(Attribute::VScaleRange) .getVScaleRangeArgs() @@ -1604,7 +1606,8 @@ Instruction *InstCombinerImpl::visitSExt(SExtInst &CI) { } if (match(Src, m_VScale(DL))) { - if (CI.getFunction()->hasFnAttribute(Attribute::VScaleRange)) { + if (CI.getFunction() && + CI.getFunction()->hasFnAttribute(Attribute::VScaleRange)) { unsigned MaxVScale = CI.getFunction() ->getFnAttribute(Attribute::VScaleRange) .getVScaleRangeArgs() diff --git a/llvm/unittests/Analysis/ValueTrackingTest.cpp b/llvm/unittests/Analysis/ValueTrackingTest.cpp index a1dedd8..8d9df97 100644 --- a/llvm/unittests/Analysis/ValueTrackingTest.cpp +++ b/llvm/unittests/Analysis/ValueTrackingTest.cpp @@ -12,6 +12,7 @@ #include "llvm/IR/ConstantRange.h" #include "llvm/IR/Dominators.h" #include "llvm/IR/Function.h" +#include "llvm/IR/IRBuilder.h" #include "llvm/IR/InstIterator.h" #include "llvm/IR/Instructions.h" #include "llvm/IR/LLVMContext.h" @@ -1597,6 +1598,22 @@ TEST_F(ComputeKnownBitsTest, ComputeKnownBitsAddWithRange) { EXPECT_EQ(Known.getMaxValue(), 131071); } +TEST_F(ComputeKnownBitsTest, ComputeKnownBitsUnknownVScale) { + Module M("", Context); + IRBuilder<> Builder(Context); + Function *TheFn = + Intrinsic::getDeclaration(&M, Intrinsic::vscale, {Builder.getInt32Ty()}); + CallInst *CI = Builder.CreateCall(TheFn, {}, {}, ""); + + KnownBits Known = computeKnownBits(CI, M.getDataLayout(), /* Depth */ 0); + delete CI; + + // There is no parent function so we cannot look up the vscale_range + // attribute to determine the number of bits. + EXPECT_EQ(Known.One.getZExtValue(), 0u); + EXPECT_EQ(Known.Zero.getZExtValue(), 0u); +} + // 512 + [32, 64) doesn't produce overlapping bits. // Make sure we get all the individual bits properly. TEST_F(ComputeKnownBitsTest, ComputeKnownBitsAddWithRangeNoOverlap) { -- 2.7.4