Skip to content

Commit 9a08afa

Browse files
committed
Add a check for integer bit count. Replace vector size check with bit count check
1 parent 7714dc5 commit 9a08afa

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
lines changed

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10655,10 +10655,13 @@ def err_second_argument_to_cwsc_not_pointer : Error<
1065510655

1065610656
def err_vector_incorrect_num_elements : Error<
1065710657
"%select{too many|too few}0 elements in vector %select{initialization|operand}3 (expected %1 elements, have %2)">;
10658-
def err_invalid_even_odd_vector_element_count : Error<
10659-
"invalid element count of %0 in vector %select{initialization|operand}4 (expected an %select{even|odd}3 element count in the range of %1 and %2)">;
1066010658
def err_altivec_empty_initializer : Error<"expected initializer">;
1066110659

10660+
def err_vector_incorrect_bit_count : Error<
10661+
"incorrect number of bits in vector operand (expected %select{|a multiple of}0 %1 bits, have %2)">;
10662+
def err_integer_incorrect_bit_count : Error<
10663+
"incorrect number of bits in integer (expected %0 bits, have %1)">;
10664+
1066210665
def err_invalid_neon_type_code : Error<
1066310666
"incompatible constant for this __builtin_neon function">;
1066410667
def err_argument_invalid_range : Error<

clang/lib/Sema/SemaHLSL.cpp

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "clang/AST/Type.h"
2222
#include "clang/AST/TypeLoc.h"
2323
#include "clang/Basic/Builtins.h"
24+
#include "clang/Basic/DiagnosticFrontend.h"
2425
#include "clang/Basic/DiagnosticSema.h"
2526
#include "clang/Basic/IdentifierTable.h"
2627
#include "clang/Basic/LLVM.h"
@@ -36,6 +37,7 @@
3637
#include "llvm/ADT/StringExtras.h"
3738
#include "llvm/ADT/StringRef.h"
3839
#include "llvm/ADT/Twine.h"
40+
#include "llvm/IR/DataLayout.h"
3941
#include "llvm/Support/Casting.h"
4042
#include "llvm/Support/DXILABI.h"
4143
#include "llvm/Support/ErrorHandling.h"
@@ -2261,12 +2263,23 @@ bool SemaHLSL::CheckBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
22612263
return true;
22622264
}
22632265

2264-
// ensure both args have 2 elements, or both args have 4 elements
2266+
// ensure arg integers are 32-bits
2267+
uint64_t ElementBitCount = getASTContext()
2268+
.getTypeSizeInChars(VTy->getElementType())
2269+
.getQuantity() *
2270+
8;
2271+
if (ElementBitCount != 32) {
2272+
SemaRef.Diag(TheCall->getBeginLoc(),
2273+
diag::err_integer_incorrect_bit_count)
2274+
<< 32 << ElementBitCount;
2275+
return true;
2276+
}
2277+
2278+
// ensure both args are vectors of total bit size of a multiple of 64
22652279
int NumElementsArg = VTy->getNumElements();
22662280
if (NumElementsArg != 2 && NumElementsArg != 4) {
2267-
SemaRef.Diag(TheCall->getBeginLoc(),
2268-
diag::err_invalid_even_odd_vector_element_count)
2269-
<< NumElementsArg << 2 << 4 << /*even*/ 0 << /*operand*/ 1;
2281+
SemaRef.Diag(TheCall->getBeginLoc(), diag::err_vector_incorrect_bit_count)
2282+
<< 1 /*a multiple of*/ << 64 << NumElementsArg * ElementBitCount;
22702283
return true;
22712284
}
22722285

clang/test/SemaHLSL/BuiltIns/AddUint64-errors.hlsl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,19 @@ uint2 test_mismatched_arg_types(uint2 a, uint4 b) {
1717

1818
uint2 test_bad_num_arg_elements(uint3 a, uint3 b) {
1919
return __builtin_hlsl_adduint64(a, b);
20-
// expected-error@-1 {{invalid element count of 3 in vector operand (expected an even element count in the range of 2 and 4)}}
20+
// expected-error@-1 {{incorrect number of bits in vector operand (expected a multiple of 64 bits, have 96)}}
2121
}
2222

2323
uint2 test_scalar_arg_type(uint a) {
2424
return __builtin_hlsl_adduint64(a, a);
2525
// expected-error@-1 {{all arguments to '__builtin_hlsl_adduint64' must be vectors}}
2626
}
2727

28+
uint2 test_uint64_args(uint16_t2 a) {
29+
return __builtin_hlsl_adduint64(a, a);
30+
// expected-error@-1 {{incorrect number of bits in integer (expected 32 bits, have 16)}}
31+
}
32+
2833
uint2 test_signed_integer_args(int2 a, int2 b) {
2934
return __builtin_hlsl_adduint64(a, b);
3035
// expected-error@-1 {{passing 'int2' (aka 'vector<int, 2>') to parameter of incompatible type '__attribute__((__vector_size__(2 * sizeof(unsigned int)))) unsigned int' (vector of 2 'unsigned int' values)}}

0 commit comments

Comments
 (0)