[llvm] [Inliner] Prevent adding pointer attributes to non-pointer arguments (PR #115569)
Harald van Dijk via llvm-commits
llvm-commits at lists.llvm.org
Fri Nov 8 15:56:34 PST 2024
https://github.com/hvdijk created https://github.com/llvm/llvm-project/pull/115569
Fixes a crash seen after #114311
>From 48f6814b2c85f8b519955a9ef604c0e6479b25ba Mon Sep 17 00:00:00 2001
From: Harald van Dijk <harald.vandijk at codeplay.com>
Date: Fri, 8 Nov 2024 23:41:01 +0000
Subject: [PATCH] [Inliner] Prevent adding pointer attributes to non-pointer
arguments
Fixes a crash seen after #114311
---
llvm/lib/Transforms/Utils/InlineFunction.cpp | 9 +++++++
.../Transforms/Inline/arg-attr-propagation.ll | 26 +++++++++++++++++++
2 files changed, 35 insertions(+)
diff --git a/llvm/lib/Transforms/Utils/InlineFunction.cpp b/llvm/lib/Transforms/Utils/InlineFunction.cpp
index a27cb4dd219c30..cbad66ed9dfe5e 100644
--- a/llvm/lib/Transforms/Utils/InlineFunction.cpp
+++ b/llvm/lib/Transforms/Utils/InlineFunction.cpp
@@ -1477,6 +1477,15 @@ static void AddParamAndFnBasicAttributes(const CallBase &CB,
// If so, propagate its access attributes.
AL = AL.addParamAttributes(Context, I, ValidObjParamAttrs[ArgNo]);
+
+ // If the argument is not a pointer type, remove attributes which only
+ // apply to pointer types.
+ if (!NewInnerCB->getArgOperand(I)->getType()->isPointerTy()) {
+ AL = AL.removeParamAttribute(Context, I, Attribute::ReadNone);
+ AL = AL.removeParamAttribute(Context, I, Attribute::ReadOnly);
+ continue;
+ }
+
// We can have conflicting attributes from the inner callsite and
// to-be-inlined callsite. In that case, choose the most
// restrictive.
diff --git a/llvm/test/Transforms/Inline/arg-attr-propagation.ll b/llvm/test/Transforms/Inline/arg-attr-propagation.ll
index 7b096539e7e1b1..fad6c7ced2edd2 100644
--- a/llvm/test/Transforms/Inline/arg-attr-propagation.ll
+++ b/llvm/test/Transforms/Inline/arg-attr-propagation.ll
@@ -76,3 +76,29 @@ define i32 @caller3(ptr dereferenceable(33) %t1) {
ret i32 %t2
}
+; Make sure that we don't propagate a pointer-only attribute to a vector of pointers.
+
+declare void @helper4(<4 x ptr> %ptr)
+
+define void @callee4(ptr readonly %ptr, <4 x i64> %idx) {
+; CHECK-LABEL: define {{[^@]+}}@callee4
+; CHECK-SAME: (ptr readonly [[PTR:%.*]], <4 x i64> [[IDX:%.*]]) {
+; CHECK-NEXT: [[PTRS:%.*]] = getelementptr inbounds i8, ptr [[PTR]], <4 x i64> [[IDX]]
+; CHECK-NEXT: call void @helper4(<4 x ptr> [[PTRS]])
+; CHECK-NEXT: ret void
+;
+ %ptrs = getelementptr inbounds i8, ptr %ptr, <4 x i64> %idx
+ call void @helper4(<4 x ptr> %ptrs)
+ ret void
+}
+
+define void @caller4(ptr readonly %ptr, <4 x i64> %idx) {
+; CHECK-LABEL: define {{[^@]+}}@caller4
+; CHECK-SAME: (ptr readonly [[PTR:%.*]], <4 x i64> [[IDX:%.*]]) {
+; CHECK-NEXT: [[PTRS_I:%.*]] = getelementptr inbounds i8, ptr [[PTR]], <4 x i64> [[IDX]]
+; CHECK-NEXT: call void @helper4(<4 x ptr> [[PTRS_I]])
+; CHECK-NEXT: ret void
+;
+ call void @callee4(ptr readonly %ptr, <4 x i64> %idx)
+ ret void
+}
More information about the llvm-commits
mailing list