[llvm] 84a4cae - [InstSimplify] Don't assume parent function when simplifying llvm.vscale.

Sander de Smalen via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 29 12:08:43 PDT 2021


Author: Sander de Smalen
Date: 2021-07-29T20:08:08+01:00
New Revision: 84a4caeb84d31550790d6922903714b6563469c9

URL: https://github.com/llvm/llvm-project/commit/84a4caeb84d31550790d6922903714b6563469c9
DIFF: https://github.com/llvm/llvm-project/commit/84a4caeb84d31550790d6922903714b6563469c9.diff

LOG: [InstSimplify] Don't assume parent function when simplifying llvm.vscale.

D106850 introduced a simplification for llvm.vscale by looking at the
surrounding function's vscale_range attributes. The call that's being
simplified may not yet have been inserted into the IR. This happens for
example during function cloning.

This patch fixes the issue by checking if the instruction is in a
parent basic block.

Added: 
    

Modified: 
    llvm/lib/Analysis/InstructionSimplify.cpp
    llvm/test/Transforms/InstSimplify/fold-vscale.ll
    llvm/unittests/Transforms/Utils/LocalTest.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp
index dc2f1c29b9a43..4cb3fdbffaa09 100644
--- a/llvm/lib/Analysis/InstructionSimplify.cpp
+++ b/llvm/lib/Analysis/InstructionSimplify.cpp
@@ -5781,6 +5781,9 @@ static Value *simplifyIntrinsic(CallBase *Call, const SimplifyQuery &Q) {
   if (!NumOperands) {
     switch (IID) {
     case Intrinsic::vscale: {
+      // Call may not be inserted into the IR yet at point of calling simplify.
+      if (!Call->getParent() || !Call->getParent()->getParent())
+        return nullptr;
       auto Attr = Call->getFunction()->getFnAttribute(Attribute::VScaleRange);
       if (!Attr.isValid())
         return nullptr;

diff  --git a/llvm/test/Transforms/InstSimplify/fold-vscale.ll b/llvm/test/Transforms/InstSimplify/fold-vscale.ll
index f9643691e04c9..d5259b8ff435f 100644
--- a/llvm/test/Transforms/InstSimplify/fold-vscale.ll
+++ b/llvm/test/Transforms/InstSimplify/fold-vscale.ll
@@ -1,6 +1,15 @@
 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
 ; RUN: opt < %s -instsimplify -S | FileCheck %s
 
+define i64 @vscale_i64_range_none() #0 {
+; CHECK-LABEL: @vscale_i64_range_none(
+; CHECK-NEXT:    [[OUT:%.*]] = call i64 @llvm.vscale.i64()
+; CHECK-NEXT:    ret i64 [[OUT]]
+;
+  %out = call i64 @llvm.vscale.i64()
+  ret i64 %out
+}
+
 define i64 @vscale_i64_range_1_1() #1 {
 ; CHECK-LABEL: @vscale_i64_range_1_1(
 ; CHECK-NEXT:    ret i64 1

diff  --git a/llvm/unittests/Transforms/Utils/LocalTest.cpp b/llvm/unittests/Transforms/Utils/LocalTest.cpp
index be9d9e52da154..e1e401a425868 100644
--- a/llvm/unittests/Transforms/Utils/LocalTest.cpp
+++ b/llvm/unittests/Transforms/Utils/LocalTest.cpp
@@ -8,6 +8,7 @@
 
 #include "llvm/Transforms/Utils/Local.h"
 #include "llvm/Analysis/DomTreeUpdater.h"
+#include "llvm/Analysis/InstructionSimplify.h"
 #include "llvm/Analysis/PostDominators.h"
 #include "llvm/Analysis/TargetTransformInfo.h"
 #include "llvm/AsmParser/Parser.h"
@@ -586,6 +587,20 @@ TEST_F(SalvageDebugInfoTest, RecursiveBlockSimplification) {
   verifyDebugValuesAreSalvaged();
 }
 
+TEST(Local, SimplifyVScaleWithRange) {
+  LLVMContext C;
+  Module M("Module", C);
+
+  IntegerType *Ty = Type::getInt32Ty(C);
+  Function *VScale = Intrinsic::getDeclaration(&M, Intrinsic::vscale, {Ty});
+  auto *CI = CallInst::Create(VScale, {}, "vscale");
+
+  // Test that SimplifyCall won't try to query it's parent function for
+  // vscale_range attributes in order to simplify llvm.vscale -> constant.
+  EXPECT_EQ(SimplifyCall(CI, SimplifyQuery(M.getDataLayout())), nullptr);
+  delete CI;
+}
+
 TEST(Local, ChangeToUnreachable) {
   LLVMContext Ctx;
 


        


More information about the llvm-commits mailing list