Skip to content

Commit 371bbef

Browse files
committed
address PR comments DXIL defaults to exp2 for opcode will need to handle exp via instruction expansion
1 parent 57abc69 commit 371bbef

File tree

5 files changed

+103
-18
lines changed

5 files changed

+103
-18
lines changed

clang/lib/Headers/hlsl/hlsl_intrinsics.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,38 @@ float3 exp(float3);
309309
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_exp)
310310
float4 exp(float4);
311311

312+
//===----------------------------------------------------------------------===//
313+
// exp2 builtins
314+
//===----------------------------------------------------------------------===//
315+
316+
/// \fn T exp2(T x)
317+
/// \brief Returns the base 2 exponential, or \a 2**x, of the specified value.
318+
/// \param x The specified input value.
319+
///
320+
/// The base 2 exponential of the \a x parameter.
321+
322+
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
323+
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_exp2)
324+
half exp2(half);
325+
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
326+
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_exp2)
327+
half2 exp2(half2);
328+
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
329+
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_exp2)
330+
half3 exp2(half3);
331+
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
332+
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_exp2)
333+
half4 exp2(half4);
334+
335+
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_exp2)
336+
float exp2(float);
337+
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_exp2)
338+
float2 exp2(float2);
339+
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_exp2)
340+
float3 exp2(float3);
341+
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_exp2)
342+
float4 exp2(float4);
343+
312344
//===----------------------------------------------------------------------===//
313345
// floor builtins
314346
//===----------------------------------------------------------------------===//
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple \
2+
// RUN: dxil-pc-shadermodel6.3-library %s -fnative-half-type \
3+
// RUN: -emit-llvm -disable-llvm-passes -o - | FileCheck %s \
4+
// RUN: --check-prefixes=CHECK,NATIVE_HALF
5+
// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple \
6+
// RUN: dxil-pc-shadermodel6.3-library %s -emit-llvm -disable-llvm-passes \
7+
// RUN: -o - | FileCheck %s --check-prefixes=CHECK,NO_HALF
8+
9+
// NATIVE_HALF: define noundef half @
10+
// NATIVE_HALF: %elt.exp2 = call half @llvm.exp2.f16(
11+
// NATIVE_HALF: ret half %elt.exp2
12+
// NO_HALF: define noundef float @"?test_exp2_half@@YA$halff@$halff@@Z"(
13+
// NO_HALF: %elt.exp2 = call float @llvm.exp2.f32(
14+
// NO_HALF: ret float %elt.exp2
15+
half test_exp2_half(half p0) { return exp2(p0); }
16+
// NATIVE_HALF: define noundef <2 x half> @
17+
// NATIVE_HALF: %elt.exp2 = call <2 x half> @llvm.exp2.v2f16
18+
// NATIVE_HALF: ret <2 x half> %elt.exp2
19+
// NO_HALF: define noundef <2 x float> @
20+
// NO_HALF: %elt.exp2 = call <2 x float> @llvm.exp2.v2f32(
21+
// NO_HALF: ret <2 x float> %elt.exp2
22+
half2 test_exp2_half2(half2 p0) { return exp2(p0); }
23+
// NATIVE_HALF: define noundef <3 x half> @
24+
// NATIVE_HALF: %elt.exp2 = call <3 x half> @llvm.exp2.v3f16
25+
// NATIVE_HALF: ret <3 x half> %elt.exp2
26+
// NO_HALF: define noundef <3 x float> @
27+
// NO_HALF: %elt.exp2 = call <3 x float> @llvm.exp2.v3f32(
28+
// NO_HALF: ret <3 x float> %elt.exp2
29+
half3 test_exp2_half3(half3 p0) { return exp2(p0); }
30+
// NATIVE_HALF: define noundef <4 x half> @
31+
// NATIVE_HALF: %elt.exp2 = call <4 x half> @llvm.exp2.v4f16
32+
// NATIVE_HALF: ret <4 x half> %elt.exp2
33+
// NO_HALF: define noundef <4 x float> @
34+
// NO_HALF: %elt.exp2 = call <4 x float> @llvm.exp2.v4f32(
35+
// NO_HALF: ret <4 x float> %elt.exp2
36+
half4 test_exp2_half4(half4 p0) { return exp2(p0); }
37+
38+
// CHECK: define noundef float @
39+
// CHECK: %elt.exp2 = call float @llvm.exp2.f32(
40+
// CHECK: ret float %elt.exp2
41+
float test_exp2_float(float p0) { return exp2(p0); }
42+
// CHECK: define noundef <2 x float> @
43+
// CHECK: %elt.exp2 = call <2 x float> @llvm.exp2.v2f32
44+
// CHECK: ret <2 x float> %elt.exp2
45+
float2 test_exp2_float2(float2 p0) { return exp2(p0); }
46+
// CHECK: define noundef <3 x float> @
47+
// CHECK: %elt.exp2 = call <3 x float> @llvm.exp2.v3f32
48+
// CHECK: ret <3 x float> %elt.exp2
49+
float3 test_exp2_float3(float3 p0) { return exp2(p0); }
50+
// CHECK: define noundef <4 x float> @
51+
// CHECK: %elt.exp2 = call <4 x float> @llvm.exp2.v4f32
52+
// CHECK: ret <4 x float> %elt.exp2
53+
float4 test_exp2_float4(float4 p0) { return exp2(p0); }
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11

2-
// RUN: %clang_cc1 -finclude-default-header -triple dxil-pc-shadermodel6.6-library %s -fnative-half-type -emit-llvm -disable-llvm-passes -verify -verify-ignore-unexpected
3-
2+
// RUN: %clang_cc1 -finclude-default-header -triple dxil-pc-shadermodel6.6-library %s -fnative-half-type -emit-llvm -disable-llvm-passes -verify -verify-ignore-unexpected -DTEST_FUNC=__builtin_elementwise_exp
3+
// RUN: %clang_cc1 -finclude-default-header -triple dxil-pc-shadermodel6.6-library %s -fnative-half-type -emit-llvm -disable-llvm-passes -verify -verify-ignore-unexpected -DTEST_FUNC=__builtin_elementwise_exp2
44
float test_too_few_arg() {
5-
return __builtin_elementwise_exp();
5+
return TEST_FUNC();
66
// expected-error@-1 {{too few arguments to function call, expected 1, have 0}}
77
}
88

99
float2 test_too_many_arg(float2 p0) {
10-
return __builtin_elementwise_exp(p0, p0);
10+
return TEST_FUNC(p0, p0);
1111
// expected-error@-1 {{too many arguments to function call, expected 1, have 2}}
1212
}
1313

1414
float builtin_bool_to_float_type_promotion(bool p1) {
15-
return __builtin_elementwise_exp(p1);
15+
return TEST_FUNC(p1);
1616
// expected-error@-1 {{1st argument must be a vector, integer or floating point type (was 'bool')}}
1717
}
1818

1919
float builtin_exp_int_to_float_promotion(int p1) {
20-
return __builtin_elementwise_exp(p1);
20+
return TEST_FUNC(p1);
2121
// expected-error@-1 {{1st argument must be a floating point type (was 'int')}}
2222
}
2323

2424
float2 builtin_exp_int2_to_float2_promotion(int2 p1) {
25-
return __builtin_elementwise_exp(p1);
25+
return TEST_FUNC(p1);
2626
// expected-error@-1 {{1st argument must be a floating point type (was 'int2' (aka 'vector<int, 2>'))}}
2727
}

llvm/lib/Target/DirectX/DXIL.td

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,9 +218,9 @@ class DXILOpMapping<int opCode, DXILOpClass opClass, Intrinsic intrinsic, string
218218
// Concrete definition of DXIL Operation mapping to corresponding LLVM intrinsic
219219
def Sin : DXILOpMapping<13, unary, int_sin,
220220
"Returns sine(theta) for theta in radians.">;
221-
def Exp : DXILOpMapping<21, unary, int_exp,
222-
"Returns the base-e exponential of the x parameter."
223-
"exp(x) = e**x.">;
221+
def Exp2 : DXILOpMapping<21, unary, int_exp2,
222+
"Returns the base 2 exponential, or 2**x, of the specified value."
223+
"exp2(x) = 2**x.">;
224224
def Frac : DXILOpMapping<22, unary, int_dx_frac,
225225
"Returns a fraction from 0 to 1 that represents the "
226226
"decimal part of the input.">;
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11
; RUN: opt -S -dxil-op-lower < %s | FileCheck %s
22

3-
; Make sure dxil operation function calls for exp are generated for float and half.
3+
; Make sure dxil operation function calls for exp2 are generated for float and half.
44
; CHECK:call float @dx.op.unary.f32(i32 21, float %{{.*}})
55
; CHECK:call half @dx.op.unary.f16(i32 21, half %{{.*}})
66

77
target datalayout = "e-m:e-p:32:32-i1:32-i8:8-i16:16-i32:32-i64:64-f16:16-f32:32-f64:64-n8:16:32:64"
88
target triple = "dxil-pc-shadermodel6.7-library"
99

1010
; Function Attrs: noinline nounwind optnone
11-
define noundef float @exp_float(float noundef %a) #0 {
11+
define noundef float @exp2_float(float noundef %a) #0 {
1212
entry:
1313
%a.addr = alloca float, align 4
1414
store float %a, ptr %a.addr, align 4
1515
%0 = load float, ptr %a.addr, align 4
16-
%elt.exp = call float @llvm.exp.f32(float %0)
17-
ret float %elt.exp
16+
%elt.exp2 = call float @llvm.exp2.f32(float %0)
17+
ret float %elt.exp2
1818
}
1919

2020
; Function Attrs: nocallback nofree nosync nounwind readnone speculatable willreturn
21-
declare float @llvm.exp.f32(float) #1
21+
declare float @llvm.exp2.f32(float) #1
2222

2323
; Function Attrs: noinline nounwind optnone
24-
define noundef half @exp_half(half noundef %a) #0 {
24+
define noundef half @exp2_half(half noundef %a) #0 {
2525
entry:
2626
%a.addr = alloca half, align 2
2727
store half %a, ptr %a.addr, align 2
2828
%0 = load half, ptr %a.addr, align 2
29-
%elt.exp = call half @llvm.exp.f16(half %0)
30-
ret half %elt.exp
29+
%elt.exp2 = call half @llvm.exp2.f16(half %0)
30+
ret half %elt.exp2
3131
}

0 commit comments

Comments
 (0)