[llvm] [Inliner] Check number of operands in AddReturnAttributes (PR #87093)

Dmitrii Dolgov via llvm-commits llvm-commits at lists.llvm.org
Fri Mar 29 10:40:49 PDT 2024


https://github.com/erthalion created https://github.com/llvm/llvm-project/pull/87093

The commit [2da4960](https://github.com/llvm/llvm-project/commit/2da4960f20f7e5d88a68ce25636a895284dc66d8) enabled `noundef` attributes propagation. It looks like ret void is considered to be `noundef`, thus `ValidUB.hasAttributes` now returns true for this type of instructions and everything proceed further to work with operands. The issue is that such instruction doesn't have operands, which means when accessing `RI->getOperand(0)` inliner pass crashes with an assert:

    llvm/include/llvm/IR/Instructions.h:3420: llvm::Value* llvm::ReturnInst::getOperand(unsigned int) const:
    Assertion `i_nocapture < OperandTraits<ReturnInst>::operands(this) && "getOperand() out of range!"' failed.

Fix that by verifying if the `ReturnInst` in fact has some operands to process.

Fixes #86162 

>From 957ba82977ed19c443924f8b57aa6e06d0670075 Mon Sep 17 00:00:00 2001
From: Dmitrii Dolgov <9erthalion6 at gmail.com>
Date: Fri, 29 Mar 2024 18:18:58 +0100
Subject: [PATCH] [Inliner] Check number of operands in AddReturnAttributes

The commit 2da4960 enabled `noundef` attributes propagation. It looks
like ret void is considered to be `noundef` thus `ValidUB.hasAttributes`
now returns true for this type of instructions and everything proceed
further to work with operands. The issue is that such instruction
doesn't have operands, which means when accessing `RI->getOperand(0)`
inliner pass crashes with an assert:

    llvm/include/llvm/IR/Instructions.h:3420: llvm::Value* llvm::ReturnInst::getOperand(unsigned int) const:
    Assertion `i_nocapture < OperandTraits<ReturnInst>::operands(this) && "getOperand() out of range!"' failed.

Fix that by verifying if the ReturnInst in fact has some operands to
process.

Fixes #86163
---
 llvm/lib/Transforms/Utils/InlineFunction.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/lib/Transforms/Utils/InlineFunction.cpp b/llvm/lib/Transforms/Utils/InlineFunction.cpp
index 833dcbec228b88..d5cf6bd036e07c 100644
--- a/llvm/lib/Transforms/Utils/InlineFunction.cpp
+++ b/llvm/lib/Transforms/Utils/InlineFunction.cpp
@@ -1384,7 +1384,7 @@ static void AddReturnAttributes(CallBase &CB, ValueToValueMapTy &VMap) {
 
   for (auto &BB : *CalledFunction) {
     auto *RI = dyn_cast<ReturnInst>(BB.getTerminator());
-    if (!RI || !isa<CallBase>(RI->getOperand(0)))
+    if (!RI || RI->getNumOperands() == 0 || !isa<CallBase>(RI->getOperand(0)))
       continue;
     auto *RetVal = cast<CallBase>(RI->getOperand(0));
     // Check that the cloned RetVal exists and is a call, otherwise we cannot



More information about the llvm-commits mailing list