Skip to content

[HLSL] Update __builtin_hlsl_dot builtin Sema Checking to fix error when passed an array literal 1u.xxxx #133941

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 4 additions & 12 deletions clang/lib/CodeGen/CGHLSLBuiltins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -368,20 +368,12 @@ Value *CodeGenFunction::EmitHLSLBuiltinExpr(unsigned BuiltinID,
"Scalar dot product is only supported on ints and floats.");
}
// For vectors, validate types and emit the appropriate intrinsic

// A VectorSplat should have happened
assert(T0->isVectorTy() && T1->isVectorTy() &&
"Dot product of vector and scalar is not supported.");
assert(CGM.getContext().hasSameUnqualifiedType(E->getArg(0)->getType(),
E->getArg(1)->getType()) &&
"Dot product operands must have the same type.");

auto *VecTy0 = E->getArg(0)->getType()->castAs<VectorType>();
[[maybe_unused]] auto *VecTy1 =
E->getArg(1)->getType()->castAs<VectorType>();

assert(VecTy0->getElementType() == VecTy1->getElementType() &&
"Dot product of vectors need the same element types.");

assert(VecTy0->getNumElements() == VecTy1->getNumElements() &&
"Dot product requires vectors to be of the same size.");
assert(VecTy0 && "Dot product argument must be a vector.");

return Builder.CreateIntrinsic(
/*ReturnType=*/T0->getScalarType(),
Expand Down
3 changes: 2 additions & 1 deletion clang/lib/Sema/SemaHLSL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2015,7 +2015,8 @@ static bool CheckVectorElementCallArgs(Sema *S, CallExpr *TheCall) {
}
if (VecTyA && VecTyB) {
bool retValue = false;
if (VecTyA->getElementType() != VecTyB->getElementType()) {
if (!S->Context.hasSameUnqualifiedType(VecTyA->getElementType(),
VecTyB->getElementType())) {
// Note: type promotion is intended to be handeled via the intrinsics
// and not the builtin itself.
S->Diag(TheCall->getBeginLoc(),
Expand Down
8 changes: 8 additions & 0 deletions clang/test/CodeGenHLSL/builtins/dot.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,11 @@ float test_dot_float4(float4 p0, float4 p1) { return dot(p0, p1); }
// CHECK: %hlsl.dot = fmul reassoc nnan ninf nsz arcp afn double
// CHECK: ret double %hlsl.dot
double test_dot_double(double p0, double p1) { return dot(p0, p1); }

// CHECK-LABEL: test_dot_literal
// CHECK: [[X:%.*]] = shufflevector <1 x i32> {{.*}}, <1 x i32> poison, <4 x i32> zeroinitializer
// CHECK-NEXT: %hlsl.dot = call i32 @llvm.[[ICF]].udot.v4i32(<4 x i32> {{.*}}, <4 x i32> [[X]])
// CHECK-NEXT: [[S1:%.*]] = insertelement <4 x i32> poison, i32 %hlsl.dot, i64 0
// CHECK-NEXT: [[S2:%.*]] = shufflevector <4 x i32> [[S1]], <4 x i32> poison, <4 x i32> zeroinitializer
// CHECK-NEXT: ret <4 x i32> [[S2]]
uint4 test_dot_literal( uint4 p0) { return dot(p0, 1u.xxxx); }
Loading