Skip to content

Commit 0877737

Browse files
authored
Handle log10 with float16_t parameter (microsoft#5659)
When the spirv backend sees the log10 intrinsic, it will generate a multiple with a constant. The type of that constant is hard coded to be a float. However, if the type of the parameter is not a float, there is a type mismatch on the multiple instruction. This commit changes that so the type of the contanst matches the scalar type of the parameter. That is, it will be a type T if the parameter is type T, a vector of type T, or a matrix of type T. Fixes microsoft#5608
1 parent 4fc141c commit 0877737

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

tools/clang/lib/SPIRV/SpirvEmitter.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11186,11 +11186,14 @@ SpirvEmitter::processIntrinsicLog10(const CallExpr *callExpr) {
1118611186
// 1 / log2(10) = 0.30103
1118711187
auto loc = callExpr->getExprLoc();
1118811188
auto range = callExpr->getSourceRange();
11189+
11190+
const auto returnType = callExpr->getType();
11191+
auto scalarType = getElementType(astContext, returnType);
11192+
1118911193
auto *scale =
11190-
spvBuilder.getConstantFloat(astContext.FloatTy, llvm::APFloat(0.30103f));
11194+
spvBuilder.getConstantFloat(scalarType, llvm::APFloat(0.30103f));
1119111195
auto *log2 = processIntrinsicUsingGLSLInst(
1119211196
callExpr, GLSLstd450::GLSLstd450Log2, true, loc, range);
11193-
const auto returnType = callExpr->getType();
1119411197
spv::Op scaleOp = isScalarType(returnType) ? spv::Op::OpFMul
1119511198
: isVectorType(returnType) ? spv::Op::OpVectorTimesScalar
1119611199
: spv::Op::OpMatrixTimesScalar;

tools/clang/test/CodeGenSPIRV/intrinsics.log10.hlsl

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %dxc -T vs_6_0 -E main
1+
// RUN: %dxc -T vs_6_2 -E main -enable-16bit-types
22

33
// According to HLSL reference:
44
// The 'log10' function can only operate on float, vector of float, and matrix of floats.
@@ -7,9 +7,10 @@
77
// CHECK: %float_0_30103001 = OpConstant %float 0.30103001
88

99
void main() {
10-
float a, log10_a;
11-
float4 b, log10_b;
12-
float2x3 c, log10_c;
10+
float a, log10_a;
11+
float4 b, log10_b;
12+
float2x3 c, log10_c;
13+
float16_t d, log10_d;
1314

1415
// CHECK: [[a:%\d+]] = OpLoad %float %a
1516
// CHECK-NEXT: [[log2_a:%\d+]] = OpExtInst %float [[glsl]] Log2 [[a]]
@@ -32,4 +33,9 @@ void main() {
3233
// CHECK-NEXT: [[log10_c:%\d+]] = OpMatrixTimesScalar %mat2v3float [[log2_c]] %float_0_30103
3334
// CHECK-NEXT: OpStore %log10_c [[log10_c]]
3435
log10_c = log10(c);
36+
37+
// CHECK: [[d:%\d+]] = OpLoad %half %d
38+
// CHECK-NEXT: [[log2_d:%\d+]] = OpExtInst %half [[glsl]] Log2 [[d]]
39+
// CHECK-NEXT:[[log10_d:%\d+]] = OpFMul %half [[log2_d]] %half_0x1_344pn2
40+
log10_d = log10(d);
3541
}

0 commit comments

Comments
 (0)