Skip to content

Commit

Permalink
don't use elementwise, rename some vars
Browse files Browse the repository at this point in the history
  • Loading branch information
bob80905 committed Aug 1, 2024
1 parent b620bab commit 3eab70e
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 38 deletions.
2 changes: 1 addition & 1 deletion clang/include/clang/Basic/Builtins.td
Original file line number Diff line number Diff line change
Expand Up @@ -4708,7 +4708,7 @@ def HLSLIsinf : LangBuiltin<"HLSL_LANG"> {
}

def HLSLLength : LangBuiltin<"HLSL_LANG"> {
let Spellings = ["__builtin_hlsl_elementwise_length"];
let Spellings = ["__builtin_hlsl_length"];
let Attributes = [NoThrow, Const];
let Prototype = "void(...)";
}
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/CodeGen/CGBuiltin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18460,7 +18460,7 @@ Value *CodeGenFunction::EmitHLSLBuiltinExpr(unsigned BuiltinID,
/*ReturnType=*/X->getType(), CGM.getHLSLRuntime().getLerpIntrinsic(),
ArrayRef<Value *>{X, Y, S}, nullptr, "hlsl.lerp");
}
case Builtin::BI__builtin_hlsl_elementwise_length: {
case Builtin::BI__builtin_hlsl_length: {
Value *X = EmitScalarExpr(E->getArg(0));

assert(E->getArg(0)->getType()->hasFloatingRepresentation() &&
Expand Down
16 changes: 8 additions & 8 deletions clang/lib/Headers/hlsl/hlsl_intrinsics.h
Original file line number Diff line number Diff line change
Expand Up @@ -919,25 +919,25 @@ float4 lerp(float4, float4, float4);
/// Length is based on the following formula: sqrt(x[0]^2 + x[1]^2 + …).

_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_length)
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_length)
half length(half);
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_length)
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_length)
half length(half2);
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_length)
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_length)
half length(half3);
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_length)
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_length)
half length(half4);

_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_length)
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_length)
float length(float);
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_length)
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_length)
float length(float2);
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_length)
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_length)
float length(float3);
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_length)
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_length)
float length(float4);

//===----------------------------------------------------------------------===//
Expand Down
4 changes: 1 addition & 3 deletions clang/lib/Sema/SemaHLSL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1079,13 +1079,11 @@ bool SemaHLSL::CheckBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
return true;
break;
}
case Builtin::BI__builtin_hlsl_elementwise_length: {
case Builtin::BI__builtin_hlsl_length: {
if (CheckFloatOrHalfRepresentations(&SemaRef, TheCall))
return true;
if (SemaRef.checkArgCount(TheCall, 1))
return true;
if (SemaRef.PrepareBuiltinElementwiseMathOneArgCall(TheCall))
return true;

ExprResult A = TheCall->getArg(0);
QualType ArgTyA = A.get()->getType();
Expand Down
10 changes: 5 additions & 5 deletions clang/test/SemaHLSL/BuiltIns/length-errors.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,30 @@

void test_too_few_arg()
{
return __builtin_hlsl_elementwise_length();
return __builtin_hlsl_length();
// expected-error@-1 {{too few arguments to function call, expected 1, have 0}}
}

void test_too_many_arg(float2 p0)
{
return __builtin_hlsl_elementwise_length(p0, p0);
return __builtin_hlsl_length(p0, p0);
// expected-error@-1 {{too many arguments to function call, expected 1, have 2}}
}

bool builtin_bool_to_float_type_promotion(bool p1)
{
return __builtin_hlsl_elementwise_length(p1);
return __builtin_hlsl_length(p1);
// expected-error@-1 {passing 'bool' to parameter of incompatible type 'float'}}
}

bool builtin_length_int_to_float_promotion(int p1)
{
return __builtin_hlsl_elementwise_length(p1);
return __builtin_hlsl_length(p1);
// expected-error@-1 {{passing 'int' to parameter of incompatible type 'float'}}
}

bool2 builtin_length_int2_to_float2_promotion(int2 p1)
{
return __builtin_hlsl_elementwise_length(p1);
return __builtin_hlsl_length(p1);
// expected-error@-1 {{passing 'int2' (aka 'vector<int, 2>') to parameter of incompatible type '__attribute__((__vector_size__(2 * sizeof(float)))) float' (vector of 2 'float' values)}}
}
7 changes: 4 additions & 3 deletions llvm/lib/Target/DirectX/DXILIntrinsicExpansion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,12 @@ static bool expandLengthIntrinsic(CallInst *Orig) {
// CGBuiltin.cpp should have emitted a fabs call.
Value *Elt = Builder.CreateExtractElement(X, (uint64_t)0);
auto *XVec = dyn_cast<FixedVectorType>(Ty);
unsigned size = XVec->getNumElements();
assert(Ty->isVectorTy() && size > 1 && "dx.length only works on vector type");
unsigned XVecSize = XVec->getNumElements();
assert(Ty->isVectorTy() && XVecSize > 1 &&
"dx.length requires a vector of length 2 or more");

Value *Sum = Builder.CreateFMul(Elt, Elt);
for (unsigned I = 1; I < size; I++) {
for (unsigned I = 1; I < XVecSize; I++) {
Elt = Builder.CreateExtractElement(X, I);
Value *Mul = Builder.CreateFMul(Elt, Elt);
Sum = Builder.CreateFAdd(Sum, Mul);
Expand Down
17 changes: 0 additions & 17 deletions llvm/test/CodeGen/DirectX/length.ll
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,6 @@ declare float @llvm.dx.length.v2f32(<2 x float>)
declare float @llvm.dx.length.v3f32(<3 x float>)
declare float @llvm.dx.length.v4f32(<4 x float>)

define noundef half @test_length_half(half noundef %p0) {
entry:
; EXPCHECK: call half @llvm.fabs.f16(half %{{.*}})
; DOPCHECK: call half @dx.op.unary.f16(i32 6, half %{{.*}})
%0 = call half @llvm.fabs.f16(half %p0)
ret half %0
}

define noundef half @test_length_half2(<2 x half> noundef %p0) {
entry:
; CHECK: extractelement <2 x half> %{{.*}}, i64 0
Expand Down Expand Up @@ -72,15 +64,6 @@ entry:
ret half %hlsl.length
}

define noundef float @test_length_float(float noundef %p0) {
entry:
; EXPCHECK: call float @llvm.fabs.f32(float %p0)
; DOPCHECK: call float @dx.op.unary.f32(i32 6, float %{{.*}})

%0 = call float @llvm.fabs.f32(float %p0)
ret float %0
}

define noundef float @test_length_float2(<2 x float> noundef %p0) {
entry:
; CHECK: extractelement <2 x float> %{{.*}}, i64 0
Expand Down

0 comments on commit 3eab70e

Please sign in to comment.