Skip to content

Commit 86e57e6

Browse files
glandiumsylvestre
authored andcommitted
[InstCombine] Bail out of casting calls when a conversion from/to byval is involved.
Fixes #58307 Reviewed By: nikic Differential Revision: https://reviews.llvm.org/D135738
1 parent 00c2eb2 commit 86e57e6

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3395,6 +3395,10 @@ bool InstCombinerImpl::transformConstExprCastCall(CallBase &Call) {
33953395
if (CallerPAL.hasParamAttr(i, Attribute::SwiftError))
33963396
return false;
33973397

3398+
if (CallerPAL.hasParamAttr(i, Attribute::ByVal) !=
3399+
Callee->getAttributes().hasParamAttr(i, Attribute::ByVal))
3400+
return false; // Cannot transform to or from byval.
3401+
33983402
// If the parameter is passed as a byval argument, then we have to have a
33993403
// sized type and the sized type has to have the same size as the old type.
34003404
if (ParamTy != ActTy && CallerPAL.hasParamAttr(i, Attribute::ByVal)) {

llvm/test/Transforms/InstCombine/byval.ll

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ declare void @add_byval_callee_2(double* byval(double))
77

88
define void @add_byval(i64* %in) {
99
; CHECK-LABEL: @add_byval(
10-
; CHECK-NEXT: [[TMP1:%.*]] = bitcast i64* [[IN:%.*]] to double*
11-
; CHECK-NEXT: call void @add_byval_callee(double* byval(double) [[TMP1]])
10+
; CHECK-NEXT: call void bitcast (void (double*)* @add_byval_callee to void (i64*)*)(i64* byval(i64) [[IN:%.*]])
1211
; CHECK-NEXT: ret void
1312
;
1413
%tmp = bitcast void (double*)* @add_byval_callee to void (i64*)*
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2+
; Check that function calls involving conversion from/to byval aren't transformed.
3+
; RUN: opt < %s -passes=instcombine -S | FileCheck %s
4+
5+
%Foo = type { i64 }
6+
define i64 @foo (ptr byval(%Foo) %foo) {
7+
; CHECK-LABEL: @foo(
8+
; CHECK-NEXT: [[TMP1:%.*]] = load i64, ptr [[FOO:%.*]], align 4
9+
; CHECK-NEXT: ret i64 [[TMP1]]
10+
;
11+
%1 = load i64, ptr %foo, align 4
12+
ret i64 %1
13+
}
14+
15+
define i64 @bar(i64 %0) {
16+
; CHECK-LABEL: @bar(
17+
; CHECK-NEXT: [[TMP2:%.*]] = tail call i64 @foo(i64 [[TMP0:%.*]])
18+
; CHECK-NEXT: ret i64 [[TMP2]]
19+
;
20+
%2 = tail call i64 @foo(i64 %0)
21+
ret i64 %2
22+
}
23+
24+
define i64 @qux(ptr byval(%Foo) %qux) {
25+
; CHECK-LABEL: @qux(
26+
; CHECK-NEXT: [[TMP1:%.*]] = tail call i64 @bar(ptr nonnull byval([[FOO:%.*]]) [[QUX:%.*]])
27+
; CHECK-NEXT: ret i64 [[TMP1]]
28+
;
29+
%1 = tail call i64 @bar(ptr byval(%Foo) %qux)
30+
ret i64 %1
31+
}

0 commit comments

Comments
 (0)