Skip to content

Commit c2634fc

Browse files
committed
[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
1 parent 3bad961 commit c2634fc

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1685,7 +1685,8 @@ static void computeKnownBitsFromOperator(const Operator *I,
16851685
Known.Zero.setBitsFrom(31);
16861686
break;
16871687
case Intrinsic::vscale: {
1688-
if (!II->getFunction()->hasFnAttribute(Attribute::VScaleRange))
1688+
if (!II->getParent() ||
1689+
!II->getFunction()->hasFnAttribute(Attribute::VScaleRange))
16891690
break;
16901691

16911692
auto VScaleRange = II->getFunction()

llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -963,7 +963,8 @@ Instruction *InstCombinerImpl::visitTrunc(TruncInst &Trunc) {
963963
}
964964

965965
if (match(Src, m_VScale(DL))) {
966-
if (Trunc.getFunction()->hasFnAttribute(Attribute::VScaleRange)) {
966+
if (Trunc.getFunction() &&
967+
Trunc.getFunction()->hasFnAttribute(Attribute::VScaleRange)) {
967968
unsigned MaxVScale = Trunc.getFunction()
968969
->getFnAttribute(Attribute::VScaleRange)
969970
.getVScaleRangeArgs()
@@ -1334,7 +1335,8 @@ Instruction *InstCombinerImpl::visitZExt(ZExtInst &CI) {
13341335
}
13351336

13361337
if (match(Src, m_VScale(DL))) {
1337-
if (CI.getFunction()->hasFnAttribute(Attribute::VScaleRange)) {
1338+
if (CI.getFunction() &&
1339+
CI.getFunction()->hasFnAttribute(Attribute::VScaleRange)) {
13381340
unsigned MaxVScale = CI.getFunction()
13391341
->getFnAttribute(Attribute::VScaleRange)
13401342
.getVScaleRangeArgs()
@@ -1604,7 +1606,8 @@ Instruction *InstCombinerImpl::visitSExt(SExtInst &CI) {
16041606
}
16051607

16061608
if (match(Src, m_VScale(DL))) {
1607-
if (CI.getFunction()->hasFnAttribute(Attribute::VScaleRange)) {
1609+
if (CI.getFunction() &&
1610+
CI.getFunction()->hasFnAttribute(Attribute::VScaleRange)) {
16081611
unsigned MaxVScale = CI.getFunction()
16091612
->getFnAttribute(Attribute::VScaleRange)
16101613
.getVScaleRangeArgs()

llvm/unittests/Analysis/ValueTrackingTest.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "llvm/IR/ConstantRange.h"
1313
#include "llvm/IR/Dominators.h"
1414
#include "llvm/IR/Function.h"
15+
#include "llvm/IR/IRBuilder.h"
1516
#include "llvm/IR/InstIterator.h"
1617
#include "llvm/IR/Instructions.h"
1718
#include "llvm/IR/LLVMContext.h"
@@ -1597,6 +1598,22 @@ TEST_F(ComputeKnownBitsTest, ComputeKnownBitsAddWithRange) {
15971598
EXPECT_EQ(Known.getMaxValue(), 131071);
15981599
}
15991600

1601+
TEST_F(ComputeKnownBitsTest, ComputeKnownBitsUnknownVScale) {
1602+
Module M("", Context);
1603+
IRBuilder<> Builder(Context);
1604+
Function *TheFn =
1605+
Intrinsic::getDeclaration(&M, Intrinsic::vscale, {Builder.getInt32Ty()});
1606+
CallInst *CI = Builder.CreateCall(TheFn, {}, {}, "");
1607+
1608+
KnownBits Known = computeKnownBits(CI, M.getDataLayout(), /* Depth */ 0);
1609+
delete CI;
1610+
1611+
// There is no parent function so we cannot look up the vscale_range
1612+
// attribute to determine the number of bits.
1613+
EXPECT_EQ(Known.One.getZExtValue(), 0u);
1614+
EXPECT_EQ(Known.Zero.getZExtValue(), 0u);
1615+
}
1616+
16001617
// 512 + [32, 64) doesn't produce overlapping bits.
16011618
// Make sure we get all the individual bits properly.
16021619
TEST_F(ComputeKnownBitsTest, ComputeKnownBitsAddWithRangeNoOverlap) {

0 commit comments

Comments
 (0)