Skip to content

Commit a620697

Browse files
authored
[IR] Check callee param attributes as well in CallBase::getParamAttr() (#91394)
These methods aren't used yet, but may be in the future. This keeps them in line with other methods like getFnAttr().
1 parent 4298fc5 commit a620697

File tree

3 files changed

+72
-2
lines changed

3 files changed

+72
-2
lines changed

llvm/include/llvm/IR/InstrTypes.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1997,13 +1997,19 @@ class CallBase : public Instruction {
19971997
/// Get the attribute of a given kind from a given arg
19981998
Attribute getParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) const {
19991999
assert(ArgNo < arg_size() && "Out of bounds");
2000-
return getAttributes().getParamAttr(ArgNo, Kind);
2000+
Attribute A = getAttributes().getParamAttr(ArgNo, Kind);
2001+
if (A.isValid())
2002+
return A;
2003+
return getParamAttrOnCalledFunction(ArgNo, Kind);
20012004
}
20022005

20032006
/// Get the attribute of a given kind from a given arg
20042007
Attribute getParamAttr(unsigned ArgNo, StringRef Kind) const {
20052008
assert(ArgNo < arg_size() && "Out of bounds");
2006-
return getAttributes().getParamAttr(ArgNo, Kind);
2009+
Attribute A = getAttributes().getParamAttr(ArgNo, Kind);
2010+
if (A.isValid())
2011+
return A;
2012+
return getParamAttrOnCalledFunction(ArgNo, Kind);
20072013
}
20082014

20092015
/// Return true if the data operand at index \p i has the attribute \p
@@ -2652,6 +2658,8 @@ class CallBase : public Instruction {
26522658
return hasFnAttrOnCalledFunction(Kind);
26532659
}
26542660
template <typename AK> Attribute getFnAttrOnCalledFunction(AK Kind) const;
2661+
template <typename AK>
2662+
Attribute getParamAttrOnCalledFunction(unsigned ArgNo, AK Kind) const;
26552663

26562664
/// Determine whether the return value has the given attribute. Supports
26572665
/// Attribute::AttrKind and StringRef as \p AttrKind types.

llvm/lib/IR/Instructions.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,22 @@ template Attribute
500500
CallBase::getFnAttrOnCalledFunction(Attribute::AttrKind Kind) const;
501501
template Attribute CallBase::getFnAttrOnCalledFunction(StringRef Kind) const;
502502

503+
template <typename AK>
504+
Attribute CallBase::getParamAttrOnCalledFunction(unsigned ArgNo,
505+
AK Kind) const {
506+
Value *V = getCalledOperand();
507+
508+
if (auto *F = dyn_cast<Function>(V))
509+
return F->getAttributes().getParamAttr(ArgNo, Kind);
510+
511+
return Attribute();
512+
}
513+
template Attribute
514+
CallBase::getParamAttrOnCalledFunction(unsigned ArgNo,
515+
Attribute::AttrKind Kind) const;
516+
template Attribute CallBase::getParamAttrOnCalledFunction(unsigned ArgNo,
517+
StringRef Kind) const;
518+
503519
void CallBase::getOperandBundlesAsDefs(
504520
SmallVectorImpl<OperandBundleDef> &Defs) const {
505521
for (unsigned i = 0, e = getNumOperandBundles(); i != e; ++i)

llvm/unittests/IR/AttributesTest.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,4 +340,50 @@ TEST(Attributes, ConstantRangeAttributeCAPI) {
340340
}
341341
}
342342

343+
TEST(Attributes, CalleeAttributes) {
344+
const char *IRString = R"IR(
345+
declare void @f1(i32 %i)
346+
declare void @f2(i32 range(i32 1, 2) %i)
347+
348+
define void @g1(i32 %i) {
349+
call void @f1(i32 %i)
350+
ret void
351+
}
352+
define void @g2(i32 %i) {
353+
call void @f2(i32 %i)
354+
ret void
355+
}
356+
define void @g3(i32 %i) {
357+
call void @f1(i32 range(i32 3, 4) %i)
358+
ret void
359+
}
360+
define void @g4(i32 %i) {
361+
call void @f2(i32 range(i32 3, 4) %i)
362+
ret void
363+
}
364+
)IR";
365+
366+
SMDiagnostic Err;
367+
LLVMContext Context;
368+
std::unique_ptr<Module> M = parseAssemblyString(IRString, Err, Context);
369+
ASSERT_TRUE(M);
370+
371+
{
372+
auto *I = cast<CallBase>(&M->getFunction("g1")->getEntryBlock().front());
373+
ASSERT_FALSE(I->getParamAttr(0, Attribute::Range).isValid());
374+
}
375+
{
376+
auto *I = cast<CallBase>(&M->getFunction("g2")->getEntryBlock().front());
377+
ASSERT_TRUE(I->getParamAttr(0, Attribute::Range).isValid());
378+
}
379+
{
380+
auto *I = cast<CallBase>(&M->getFunction("g3")->getEntryBlock().front());
381+
ASSERT_TRUE(I->getParamAttr(0, Attribute::Range).isValid());
382+
}
383+
{
384+
auto *I = cast<CallBase>(&M->getFunction("g4")->getEntryBlock().front());
385+
ASSERT_TRUE(I->getParamAttr(0, Attribute::Range).isValid());
386+
}
387+
}
388+
343389
} // end anonymous namespace

0 commit comments

Comments
 (0)