Skip to content

Commit 362b64d

Browse files
committed
Allow arrays to be returned by value in HLSL + test
1 parent c9ff839 commit 362b64d

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

clang/lib/Sema/SemaExpr.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20681,7 +20681,8 @@ ExprResult RebuildUnknownAnyExpr::VisitCallExpr(CallExpr *E) {
2068120681
const FunctionType *FnType = CalleeType->castAs<FunctionType>();
2068220682

2068320683
// Verify that this is a legal result type of a function.
20684-
if (DestType->isArrayType() || DestType->isFunctionType()) {
20684+
if ((DestType->isArrayType() && !S.getLangOpts().HLSL) ||
20685+
DestType->isFunctionType()) {
2068520686
unsigned diagID = diag::err_func_returning_array_function;
2068620687
if (Kind == FK_BlockPointer)
2068720688
diagID = diag::err_block_returning_array_function;

clang/lib/Sema/SemaType.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2530,7 +2530,7 @@ QualType Sema::BuildMatrixType(QualType ElementTy, Expr *NumRows, Expr *NumCols,
25302530
}
25312531

25322532
bool Sema::CheckFunctionReturnType(QualType T, SourceLocation Loc) {
2533-
if (T->isArrayType() || T->isFunctionType()) {
2533+
if ((T->isArrayType() && !getLangOpts().HLSL) || T->isFunctionType()) {
25342534
Diag(Loc, diag::err_func_returning_array_function)
25352535
<< T->isFunctionType() << T;
25362536
return true;
@@ -4934,7 +4934,9 @@ static TypeSourceInfo *GetFullTypeForDeclarator(TypeProcessingState &state,
49344934

49354935
// C99 6.7.5.3p1: The return type may not be a function or array type.
49364936
// For conversion functions, we'll diagnose this particular error later.
4937-
if (!D.isInvalidType() && (T->isArrayType() || T->isFunctionType()) &&
4937+
if (!D.isInvalidType() &&
4938+
((T->isArrayType() && !S.getLangOpts().HLSL) ||
4939+
T->isFunctionType()) &&
49384940
(D.getName().getKind() !=
49394941
UnqualifiedIdKind::IK_ConversionFunctionId)) {
49404942
unsigned diagID = diag::err_func_returning_array_function;
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-library -disable-llvm-passes -emit-llvm -finclude-default-header -o - %s | FileCheck %s
2+
3+
typedef int Foo[2];
4+
5+
// CHECK-LABEL: define void {{.*}}boop{{.*}}(ptr dead_on_unwind noalias writable sret([2 x i32]) align 4 %agg.result)
6+
// CHECK: [[G:%.*]] = alloca [2 x i32], align 4
7+
// CHECK-NEXT: call void @llvm.memcpy.p0.p0.i32(ptr align 4 [[G]], ptr align 4 {{.*}}, i32 8, i1 false)
8+
// CHECK-NEXT: [[AIB:%.*]] = getelementptr inbounds [2 x i32], ptr %agg.result, i32 0, i32 0
9+
// CHECK-NEXT: br label %arrayinit.body
10+
// CHECK: arrayinit.body:
11+
// CHECK-NEXT: [[AII:%.*]] = phi i32 [ 0, %entry ], [ %arrayinit.next, %arrayinit.body ]
12+
// CHECK-NEXT: [[X:%.*]] = getelementptr inbounds i32, ptr [[AIB]], i32 [[AII]]
13+
// CHECK-NEXT: [[AI:%.*]] = getelementptr inbounds nuw [2 x i32], ptr [[G]], i32 0, i32 [[AII]]
14+
// CHECK-NEXT: [[Y:%.*]] = load i32, ptr [[AI]], align 4
15+
// CHECK-NEXT: store i32 [[Y]], ptr [[X]], align 4
16+
// CHECK-NEXT: [[AIN:%.*]] = add nuw i32 [[AII]], 1
17+
// CHECK-NEXT: [[AID:%.*]] = icmp eq i32 [[AIN]], 2
18+
// CHECK-NEXT: br i1 [[AID]], label %arrayinit.end, label %arrayinit.body
19+
// CHECK: arrayinit.end:
20+
// CHECK-NEXT: ret void
21+
export Foo boop() {
22+
Foo G = {1,2};
23+
return G;
24+
}
25+
26+
// CHECK-LABEL: define void {{.*}}foo{{.*}}(ptr dead_on_unwind noalias writable sret([2 x i32]) align 4 %agg.result)
27+
// CHECK: store i32 1, ptr %agg.result, align 4
28+
// CHECK-NEXT: [[E:%.*]] = getelementptr inbounds i32, ptr %agg.result, i32 1
29+
// CHECK-NEXT: store i32 2, ptr [[E]], align 4
30+
// CHECK-NEXT: ret void
31+
export int foo()[2] {
32+
return {1,2};
33+
}

0 commit comments

Comments
 (0)