Skip to content

Commit 2bbc9bc

Browse files
committed
[OpenCL] Support template parameters for as_type
Implement the TreeTransform for AsTypeExpr. Split `BuildAsTypeExpr` out of `ActOnAsTypeExpr`, such that we can call the Build method from the TreeTransform. Fixes PR47979. Differential Revision: https://reviews.llvm.org/D98855
1 parent f71404c commit 2bbc9bc

File tree

4 files changed

+49
-10
lines changed

4 files changed

+49
-10
lines changed

clang/include/clang/Sema/Sema.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5581,6 +5581,9 @@ class Sema final {
55815581
ExprResult ActOnAsTypeExpr(Expr *E, ParsedType ParsedDestTy,
55825582
SourceLocation BuiltinLoc,
55835583
SourceLocation RParenLoc);
5584+
ExprResult BuildAsTypeExpr(Expr *E, QualType DestTy,
5585+
SourceLocation BuiltinLoc,
5586+
SourceLocation RParenLoc);
55845587

55855588
//===---------------------------- C++ Features --------------------------===//
55865589

clang/lib/Sema/SemaExpr.cpp

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6497,24 +6497,30 @@ ExprResult Sema::BuildCallExpr(Scope *Scope, Expr *Fn, SourceLocation LParenLoc,
64976497
ExecConfig, IsExecConfig);
64986498
}
64996499

6500-
/// ActOnAsTypeExpr - create a new asType (bitcast) from the arguments.
6500+
/// Parse a __builtin_astype expression.
65016501
///
65026502
/// __builtin_astype( value, dst type )
65036503
///
65046504
ExprResult Sema::ActOnAsTypeExpr(Expr *E, ParsedType ParsedDestTy,
65056505
SourceLocation BuiltinLoc,
65066506
SourceLocation RParenLoc) {
6507+
QualType DstTy = GetTypeFromParser(ParsedDestTy);
6508+
return BuildAsTypeExpr(E, DstTy, BuiltinLoc, RParenLoc);
6509+
}
6510+
6511+
/// Create a new AsTypeExpr node (bitcast) from the arguments.
6512+
ExprResult Sema::BuildAsTypeExpr(Expr *E, QualType DestTy,
6513+
SourceLocation BuiltinLoc,
6514+
SourceLocation RParenLoc) {
65076515
ExprValueKind VK = VK_RValue;
65086516
ExprObjectKind OK = OK_Ordinary;
6509-
QualType DstTy = GetTypeFromParser(ParsedDestTy);
65106517
QualType SrcTy = E->getType();
6511-
if (Context.getTypeSize(DstTy) != Context.getTypeSize(SrcTy))
6512-
return ExprError(Diag(BuiltinLoc,
6513-
diag::err_invalid_astype_of_different_size)
6514-
<< DstTy
6515-
<< SrcTy
6516-
<< E->getSourceRange());
6517-
return new (Context) AsTypeExpr(E, DstTy, VK, OK, BuiltinLoc, RParenLoc);
6518+
if (!SrcTy->isDependentType() &&
6519+
Context.getTypeSize(DestTy) != Context.getTypeSize(SrcTy))
6520+
return ExprError(
6521+
Diag(BuiltinLoc, diag::err_invalid_astype_of_different_size)
6522+
<< DestTy << SrcTy << E->getSourceRange());
6523+
return new (Context) AsTypeExpr(E, DestTy, VK, OK, BuiltinLoc, RParenLoc);
65186524
}
65196525

65206526
/// ActOnConvertVectorExpr - create a new convert-vector expression from the

clang/lib/Sema/TreeTransform.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13971,7 +13971,14 @@ TreeTransform<Derived>::TransformBlockExpr(BlockExpr *E) {
1397113971
template<typename Derived>
1397213972
ExprResult
1397313973
TreeTransform<Derived>::TransformAsTypeExpr(AsTypeExpr *E) {
13974-
llvm_unreachable("Cannot transform asType expressions yet");
13974+
ExprResult SrcExpr = getDerived().TransformExpr(E->getSrcExpr());
13975+
if (SrcExpr.isInvalid())
13976+
return ExprError();
13977+
13978+
QualType Type = getDerived().TransformType(E->getType());
13979+
13980+
return SemaRef.BuildAsTypeExpr(SrcExpr.get(), Type, E->getBuiltinLoc(),
13981+
E->getRParenLoc());
1397513982
}
1397613983

1397713984
template<typename Derived>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// RUN: %clang_cc1 -fdeclare-opencl-builtins -finclude-default-header %s -cl-std=clc++ -verify
2+
3+
// Test as_type, which is defined in terms of __builtin_astype.
4+
template <typename T>
5+
auto templated_astype(T x) {
6+
return as_int2(x);
7+
// expected-error@-1{{invalid reinterpretation: sizes of 'int2' (vector of 2 'int' values) and '__private int' must match}}
8+
}
9+
10+
auto test_long(long x) { return templated_astype(x); }
11+
12+
auto neg_test_int(int x) { return templated_astype(x); }
13+
// expected-note@-1{{in instantiation of function template specialization 'templated_astype<int>' requested here}}
14+
15+
auto test_short4(short4 x) { return templated_astype(x); }
16+
17+
// Test __builtin_astype.
18+
template <typename T>
19+
auto templated_builtin_astype(T x) {
20+
return __builtin_astype(x, int2);
21+
}
22+
23+
auto test_builtin(char8 x) { return templated_builtin_astype(x); }

0 commit comments

Comments
 (0)