Skip to content

Commit ec4575c

Browse files
committed
address PR comments - removed faceforward_vec_impl, only check first arg is float in sema spirv checks, add half tests to codegen spirv test
1 parent b36dc04 commit ec4575c

File tree

8 files changed

+64
-39
lines changed

8 files changed

+64
-39
lines changed

clang/lib/Headers/hlsl/hlsl_intrinsic_helpers.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -127,16 +127,6 @@ template <typename T> constexpr vector<T, 4> lit_impl(T NDotL, T NDotH, T M) {
127127
}
128128

129129
template <typename T> constexpr T faceforward_impl(T N, T I, T Ng) {
130-
#if (__has_builtin(__builtin_spirv_faceforward))
131-
return __builtin_spirv_faceforward(N, I, Ng);
132-
#else
133-
return select(I * Ng < 0, N, -N);
134-
#endif
135-
}
136-
137-
template <typename T, int L>
138-
constexpr vector<T, L> faceforward_vec_impl(vector<T, L> N, vector<T, L> I,
139-
vector<T, L> Ng) {
140130
#if (__has_builtin(__builtin_spirv_faceforward))
141131
return __builtin_spirv_faceforward(N, I, Ng);
142132
#else

clang/lib/Headers/hlsl/hlsl_intrinsics.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,15 +250,15 @@ const inline __detail::HLSL_FIXED_VECTOR<half, L> faceforward(
250250
__detail::HLSL_FIXED_VECTOR<half, L> N,
251251
__detail::HLSL_FIXED_VECTOR<half, L> I,
252252
__detail::HLSL_FIXED_VECTOR<half, L> Ng) {
253-
return __detail::faceforward_vec_impl(N, I, Ng);
253+
return __detail::faceforward_impl(N, I, Ng);
254254
}
255255

256256
template <int L>
257257
const inline __detail::HLSL_FIXED_VECTOR<float, L>
258258
faceforward(__detail::HLSL_FIXED_VECTOR<float, L> N,
259259
__detail::HLSL_FIXED_VECTOR<float, L> I,
260260
__detail::HLSL_FIXED_VECTOR<float, L> Ng) {
261-
return __detail::faceforward_vec_impl(N, I, Ng);
261+
return __detail::faceforward_impl(N, I, Ng);
262262
}
263263

264264
//===----------------------------------------------------------------------===//

clang/lib/Sema/SemaSPIRV.cpp

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,6 @@ 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-
3319
static bool CheckAllArgsHaveSameType(Sema *S, CallExpr *TheCall) {
3420
assert(TheCall->getNumArgs() > 1);
3521
QualType ArgTy0 = TheCall->getArg(0)->getType();
@@ -136,27 +122,41 @@ bool SemaSPIRV::CheckSPIRVBuiltinFunctionCall(unsigned BuiltinID,
136122
if (SemaRef.checkArgCount(TheCall, 3))
137123
return true;
138124

139-
if (CheckAllArgsHaveFloatRepresentation(&SemaRef, TheCall))
125+
// Check if first argument has floating representation
126+
ExprResult A = TheCall->getArg(0);
127+
QualType ArgTyA = A.get()->getType();
128+
if (!ArgTyA->hasFloatingRepresentation()) {
129+
SemaRef.Diag(A.get()->getBeginLoc(), diag::err_builtin_invalid_arg_type)
130+
<< /* ordinal */ 1 << /* scalar or vector */ 5 << /* no int */ 0
131+
<< /* fp */ 1 << ArgTyA;
140132
return true;
133+
}
141134

142135
if (CheckAllArgsHaveSameType(&SemaRef, TheCall))
143136
return true;
144137

145-
QualType RetTy = TheCall->getArg(0)->getType();
138+
QualType RetTy = ArgTyA;
146139
TheCall->setType(RetTy);
147140
break;
148141
}
149142
case SPIRV::BI__builtin_spirv_faceforward: {
150143
if (SemaRef.checkArgCount(TheCall, 3))
151144
return true;
152145

153-
if (CheckAllArgsHaveFloatRepresentation(&SemaRef, TheCall))
146+
// Check if first argument has floating representation
147+
ExprResult A = TheCall->getArg(0);
148+
QualType ArgTyA = A.get()->getType();
149+
if (!ArgTyA->hasFloatingRepresentation()) {
150+
SemaRef.Diag(A.get()->getBeginLoc(), diag::err_builtin_invalid_arg_type)
151+
<< /* ordinal */ 1 << /* scalar or vector */ 5 << /* no int */ 0
152+
<< /* fp */ 1 << ArgTyA;
154153
return true;
154+
}
155155

156156
if (CheckAllArgsHaveSameType(&SemaRef, TheCall))
157157
return true;
158158

159-
QualType RetTy = TheCall->getArg(0)->getType();
159+
QualType RetTy = ArgTyA;
160160
TheCall->setType(RetTy);
161161
break;
162162
}

clang/test/CodeGenHLSL/builtins/faceforward.hlsl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
// RUN: -emit-llvm -o - | FileCheck %s --check-prefix=SPVCHECK
77

88
// CHECK-LABEL: test_faceforward_half
9-
// CHECK: %mul.i = fmul reassoc nnan ninf nsz arcp afn half %{{.*}}, %{{.*}}
10-
// CHECK: %cmp.i = fcmp reassoc nnan ninf nsz arcp afn olt half %mul.i, 0xH0000
9+
// CHECK: %hlsl.dot.i = fmul reassoc nnan ninf nsz arcp afn half %{{.*}}, %{{.*}}
10+
// CHECK: %cmp.i = fcmp reassoc nnan ninf nsz arcp afn olt half %hlsl.dot.i, 0xH0000
1111
// CHECK: %fneg.i = fneg reassoc nnan ninf nsz arcp afn half %{{.*}}
1212
// CHECK: %hlsl.select.i = select reassoc nnan ninf nsz arcp afn i1 %cmp.i, half %{{.*}}, half %fneg.i
1313
// CHECK: ret half %hlsl.select.i
@@ -50,8 +50,8 @@ half3 test_faceforward_half3(half3 N, half3 I, half3 Ng) { return faceforward(N,
5050
half4 test_faceforward_half4(half4 N, half4 I, half4 Ng) { return faceforward(N, I, Ng); }
5151

5252
// CHECK-LABEL: test_faceforward_float
53-
// CHECK: %mul.i = fmul reassoc nnan ninf nsz arcp afn float %{{.*}}, %{{.*}}
54-
// CHECK: %cmp.i = fcmp reassoc nnan ninf nsz arcp afn olt float %mul.i, 0.000000e+00
53+
// CHECK: %hlsl.dot.i = fmul reassoc nnan ninf nsz arcp afn float %{{.*}}, %{{.*}}
54+
// CHECK: %cmp.i = fcmp reassoc nnan ninf nsz arcp afn olt float %hlsl.dot.i, 0.000000e+00
5555
// CHECK: %fneg.i = fneg reassoc nnan ninf nsz arcp afn float %{{.*}}
5656
// CHECK: %hlsl.select.i = select reassoc nnan ninf nsz arcp afn i1 %cmp.i, float %{{.*}}, float %fneg.i
5757
// CHECK: ret float %hlsl.select.i

clang/test/CodeGenSPIRV/Builtins/faceforward.c

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,44 @@
22

33
// RUN: %clang_cc1 -O1 -triple spirv-pc-vulkan-compute %s -emit-llvm -o - | FileCheck %s
44

5+
typedef _Float16 half;
6+
typedef half half2 __attribute__((ext_vector_type(2)));
7+
typedef half half3 __attribute__((ext_vector_type(3)));
8+
typedef half half4 __attribute__((ext_vector_type(4)));
59
typedef float float2 __attribute__((ext_vector_type(2)));
610
typedef float float3 __attribute__((ext_vector_type(3)));
711
typedef float float4 __attribute__((ext_vector_type(4)));
812

13+
// CHECK-LABEL: define spir_func half @test_faceforward_half(
14+
// CHECK-SAME: half noundef [[N:%.*]], half noundef [[I:%.*]], half noundef [[NG:%.*]]) local_unnamed_addr #[[ATTR0:[0-9]+]] {
15+
// CHECK-NEXT: [[ENTRY:.*:]]
16+
// CHECK-NEXT: [[SPV_FACEFORWARD:%.*]] = tail call half @llvm.spv.smoothstep.f16(half [[N]], half [[I]], half [[NG]])
17+
// CHECK-NEXT: ret half [[SPV_FACEFORWARD]]
18+
half test_faceforward_half(half N, half I, half Ng) { return __builtin_spirv_faceforward(N, I, Ng); }
19+
20+
// CHECK-LABEL: define spir_func <2 x half> @test_faceforward_half2(
21+
// CHECK-SAME: <2 x half> noundef [[N:%.*]], <2 x half> noundef [[I:%.*]], <2 x half> noundef [[NG:%.*]]) local_unnamed_addr #[[ATTR0]] {
22+
// CHECK-NEXT: [[ENTRY:.*:]]
23+
// CHECK-NEXT: [[SPV_FACEFORWARD:%.*]] = tail call <2 x half> @llvm.spv.smoothstep.v2f16(<2 x half> [[N]], <2 x half> [[I]], <2 x half> [[NG]])
24+
// CHECK-NEXT: ret <2 x half> [[SPV_FACEFORWARD]]
25+
half2 test_faceforward_half2(half2 N, half2 I, half2 Ng) { return __builtin_spirv_faceforward(N, I, Ng); }
26+
27+
// CHECK-LABEL: define spir_func <3 x half> @test_faceforward_half3(
28+
// CHECK-SAME: <3 x half> noundef [[N:%.*]], <3 x half> noundef [[I:%.*]], <3 x half> noundef [[NG:%.*]]) local_unnamed_addr #[[ATTR0]] {
29+
// CHECK-NEXT: [[ENTRY:.*:]]
30+
// CHECK-NEXT: [[SPV_FACEFORWARD:%.*]] = tail call <3 x half> @llvm.spv.smoothstep.v3f16(<3 x half> [[N]], <3 x half> [[I]], <3 x half> [[NG]])
31+
// CHECK-NEXT: ret <3 x half> [[SPV_FACEFORWARD]]
32+
half3 test_faceforward_half3(half3 N, half3 I, half3 Ng) { return __builtin_spirv_faceforward(N, I, Ng); }
33+
34+
// CHECK-LABEL: define spir_func <4 x half> @test_faceforward_half4(
35+
// CHECK-SAME: <4 x half> noundef [[N:%.*]], <4 x half> noundef [[I:%.*]], <4 x half> noundef [[NG:%.*]]) local_unnamed_addr #[[ATTR0]] {
36+
// CHECK-NEXT: [[ENTRY:.*:]]
37+
// CHECK-NEXT: [[SPV_FACEFORWARD:%.*]] = tail call <4 x half> @llvm.spv.smoothstep.v4f16(<4 x half> [[N]], <4 x half> [[I]], <4 x half> [[NG]])
38+
// CHECK-NEXT: ret <4 x half> [[SPV_FACEFORWARD]]
39+
half4 test_faceforward_half4(half4 N, half4 I, half4 Ng) { return __builtin_spirv_faceforward(N, I, Ng); }
40+
941
// CHECK-LABEL: define spir_func float @test_faceforward_float(
10-
// CHECK-SAME: float noundef [[N:%.*]], float noundef [[I:%.*]], float noundef [[NG:%.*]]) local_unnamed_addr #[[ATTR0:[0-9]+]] {
42+
// CHECK-SAME: float noundef [[N:%.*]], float noundef [[I:%.*]], float noundef [[NG:%.*]]) local_unnamed_addr #[[ATTR0]] {
1143
// CHECK-NEXT: [[ENTRY:.*:]]
1244
// CHECK-NEXT: [[SPV_FACEFORWARD:%.*]] = tail call float @llvm.spv.smoothstep.f32(float [[N]], float [[I]], float [[NG]])
1345
// CHECK-NEXT: ret float [[SPV_FACEFORWARD]]

clang/test/SemaSPIRV/BuiltIns/faceforward-errors.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ int test_int_scalar_inputs(int p0) {
2525

2626
float test_int_scalar_inputs2(float p0, int p1) {
2727
return __builtin_spirv_faceforward(p0, p1, p1);
28-
// expected-error@-1 {{2nd argument must be a scalar or vector of floating-point types (was 'int')}}
28+
// expected-error@-1 {{all arguments to '__builtin_spirv_faceforward' must have the same type}}
2929
}
3030

3131
float test_int_scalar_inputs3(float p0, int p1) {
3232
return __builtin_spirv_faceforward(p0, p0, p1);
33-
// expected-error@-1 {{3rd argument must be a scalar or vector of floating-point types (was 'int')}}
33+
// expected-error@-1 {{all arguments to '__builtin_spirv_faceforward' must have the same type}}
3434
}
3535

3636
float test_mismatched_arg(float p0, float2 p1) {

clang/test/SemaSPIRV/BuiltIns/smoothstep-errors.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ int test_int_scalar_inputs(int p0) {
2525

2626
float test_int_scalar_inputs2(float p0, int p1) {
2727
return __builtin_spirv_smoothstep(p0, p1, p1);
28-
// expected-error@-1 {{2nd argument must be a scalar or vector of floating-point types (was 'int')}}
28+
// expected-error@-1 {{all arguments to '__builtin_spirv_smoothstep' must have the same type}}
2929
}
3030

3131
float test_int_scalar_inputs3(float p0, int p1) {
3232
return __builtin_spirv_smoothstep(p0, p0, p1);
33-
// expected-error@-1 {{3rd argument must be a scalar or vector of floating-point types (was 'int')}}
33+
// expected-error@-1 {{all arguments to '__builtin_spirv_smoothstep' must have the same type}}
3434
}
3535

3636
float test_mismatched_arg(float p0, float2 p1) {

llvm/test/CodeGen/SPIRV/hlsl-intrinsics/faceforward.ll

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
; RUN: llc -O0 -verify-machineinstrs -mtriple=spirv-unknown-unknown %s -o - | FileCheck %s
22
; RUN: %if spirv-tools %{ llc -O0 -mtriple=spirv-unknown-unknown %s -o - -filetype=obj | spirv-val --target-env vulkan1.3 %}
33

4+
; TODO(#136344): This test currently fails when --target-env vulkan1.3 is specified.
5+
; XFAIL: spirv-tools
6+
47
; Make sure SPIRV operation function calls for faceforward are lowered correctly.
58

69
; CHECK-DAG: %[[#op_ext_glsl:]] = OpExtInstImport "GLSL.std.450"

0 commit comments

Comments
 (0)