Skip to content

Commit b36dc04

Browse files
committed
address PR comments - move repeated code to helper function, remove -x specification in tests, clarify faceforward desc
1 parent 9966b0c commit b36dc04

File tree

3 files changed

+41
-54
lines changed

3 files changed

+41
-54
lines changed

clang/lib/Headers/hlsl/hlsl_intrinsics.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ const inline double4 dst(double4 Src0, double4 Src1) {
220220

221221
/// \fn T faceforward(T N, T I, T Ng)
222222
/// \brief Flips the surface-normal (if needed) to face in a direction opposite
223-
/// to \a I. Returns the result in \a N.
223+
/// to \a I. Returns the result in terms of \a N.
224224
/// \param N The resulting floating-point surface-normal vector.
225225
/// \param I A floating-point, incident vector that points from the view
226226
/// position to the shading position.

clang/lib/Sema/SemaSPIRV.cpp

Lines changed: 39 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,37 @@ namespace clang {
1616

1717
SemaSPIRV::SemaSPIRV(Sema &S) : SemaBase(S) {}
1818

19+
static bool CheckAllArgsHaveFloatRepresentation(Sema *S, CallExpr *TheCall) {
20+
for (unsigned I = 0, N = TheCall->getNumArgs(); I < N; ++I) {
21+
ExprResult Arg = TheCall->getArg(I);
22+
QualType ArgTy = Arg.get()->getType();
23+
if (!ArgTy->hasFloatingRepresentation()) {
24+
S->Diag(Arg.get()->getBeginLoc(), diag::err_builtin_invalid_arg_type)
25+
<< /* ordinal */ I + 1 << /* scalar or vector */ 5 << /* no int */ 0
26+
<< /* fp */ 1 << ArgTy;
27+
return true;
28+
}
29+
}
30+
return false;
31+
}
32+
33+
static bool CheckAllArgsHaveSameType(Sema *S, CallExpr *TheCall) {
34+
assert(TheCall->getNumArgs() > 1);
35+
QualType ArgTy0 = TheCall->getArg(0)->getType();
36+
37+
for (unsigned I = 1, N = TheCall->getNumArgs(); I < N; ++I) {
38+
if (!S->getASTContext().hasSameUnqualifiedType(
39+
ArgTy0, TheCall->getArg(I)->getType())) {
40+
S->Diag(TheCall->getBeginLoc(), diag::err_vec_builtin_incompatible_vector)
41+
<< TheCall->getDirectCallee() << /*useAllTerminology*/ true
42+
<< SourceRange(TheCall->getArg(0)->getBeginLoc(),
43+
TheCall->getArg(N - 1)->getEndLoc());
44+
return true;
45+
}
46+
}
47+
return false;
48+
}
49+
1950
bool SemaSPIRV::CheckSPIRVBuiltinFunctionCall(unsigned BuiltinID,
2051
CallExpr *TheCall) {
2152
switch (BuiltinID) {
@@ -105,71 +136,27 @@ bool SemaSPIRV::CheckSPIRVBuiltinFunctionCall(unsigned BuiltinID,
105136
if (SemaRef.checkArgCount(TheCall, 3))
106137
return true;
107138

108-
// check if the all arguments have floating representation
109-
for (unsigned i = 0; i < TheCall->getNumArgs(); ++i) {
110-
ExprResult Arg = TheCall->getArg(i);
111-
QualType ArgTy = Arg.get()->getType();
112-
if (!ArgTy->hasFloatingRepresentation()) {
113-
SemaRef.Diag(Arg.get()->getBeginLoc(),
114-
diag::err_builtin_invalid_arg_type)
115-
<< i + 1 << /* scalar or vector */ 5 << /* no int */ 0 << /* fp */ 1
116-
<< ArgTy;
117-
return true;
118-
}
119-
}
139+
if (CheckAllArgsHaveFloatRepresentation(&SemaRef, TheCall))
140+
return true;
120141

121-
// check if all arguments are of the same type
122-
ExprResult A = TheCall->getArg(0);
123-
ExprResult B = TheCall->getArg(1);
124-
ExprResult C = TheCall->getArg(2);
125-
if (!(SemaRef.getASTContext().hasSameUnqualifiedType(A.get()->getType(),
126-
B.get()->getType()) &&
127-
SemaRef.getASTContext().hasSameUnqualifiedType(A.get()->getType(),
128-
C.get()->getType()))) {
129-
SemaRef.Diag(TheCall->getBeginLoc(),
130-
diag::err_vec_builtin_incompatible_vector)
131-
<< TheCall->getDirectCallee() << /*useAllTerminology*/ true
132-
<< SourceRange(A.get()->getBeginLoc(), C.get()->getEndLoc());
142+
if (CheckAllArgsHaveSameType(&SemaRef, TheCall))
133143
return true;
134-
}
135144

136-
QualType RetTy = A.get()->getType();
145+
QualType RetTy = TheCall->getArg(0)->getType();
137146
TheCall->setType(RetTy);
138147
break;
139148
}
140149
case SPIRV::BI__builtin_spirv_faceforward: {
141150
if (SemaRef.checkArgCount(TheCall, 3))
142151
return true;
143152

144-
// check if all arguments have floating representation
145-
for (unsigned i = 0; i < TheCall->getNumArgs(); ++i) {
146-
ExprResult Arg = TheCall->getArg(i);
147-
QualType ArgTy = Arg.get()->getType();
148-
if (!ArgTy->hasFloatingRepresentation()) {
149-
SemaRef.Diag(Arg.get()->getBeginLoc(),
150-
diag::err_builtin_invalid_arg_type)
151-
<< i + 1 << /* scalar or vector */ 5 << /* no int */ 0 << /* fp */ 1
152-
<< ArgTy;
153-
return true;
154-
}
155-
}
153+
if (CheckAllArgsHaveFloatRepresentation(&SemaRef, TheCall))
154+
return true;
156155

157-
// check if all arguments are of the same type
158-
ExprResult A = TheCall->getArg(0);
159-
ExprResult B = TheCall->getArg(1);
160-
ExprResult C = TheCall->getArg(2);
161-
if (!(SemaRef.getASTContext().hasSameUnqualifiedType(A.get()->getType(),
162-
B.get()->getType()) &&
163-
SemaRef.getASTContext().hasSameUnqualifiedType(A.get()->getType(),
164-
C.get()->getType()))) {
165-
SemaRef.Diag(TheCall->getBeginLoc(),
166-
diag::err_vec_builtin_incompatible_vector)
167-
<< TheCall->getDirectCallee() << /*useAllTerminology*/ true
168-
<< SourceRange(A.get()->getBeginLoc(), C.get()->getEndLoc());
156+
if (CheckAllArgsHaveSameType(&SemaRef, TheCall))
169157
return true;
170-
}
171158

172-
QualType RetTy = A.get()->getType();
159+
QualType RetTy = TheCall->getArg(0)->getType();
173160
TheCall->setType(RetTy);
174161
break;
175162
}

clang/test/CodeGenHLSL/builtins/faceforward.hlsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple \
1+
// RUN: %clang_cc1 -finclude-default-header -triple \
22
// RUN: dxil-pc-shadermodel6.3-library %s -fnative-half-type \
33
// RUN: -emit-llvm -o - | FileCheck %s
44
// RUN: %clang_cc1 -finclude-default-header -triple \

0 commit comments

Comments
 (0)