Skip to content

Commit 20b66ea

Browse files
committed
C++: Support __builtin_shuffle builtin
While here write gcc instead of GNU, which is more accurate.
1 parent 81e687e commit 20b66ea

File tree

5 files changed

+41
-2
lines changed

5 files changed

+41
-2
lines changed

cpp/ql/lib/semmle/code/cpp/exprs/BuiltInOperations.qll

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ class BuiltInNoOp extends BuiltInOperation, @noopexpr {
121121
/**
122122
* A C/C++ `__builtin_offsetof` built-in operation (used by some implementations
123123
* of `offsetof`). The operation retains its semantics even in the presence
124-
* of an overloaded `operator &`). This is a GNU/Clang extension.
124+
* of an overloaded `operator &`). This is a gcc/clang extension.
125125
* ```
126126
* struct S {
127127
* int a, b;
@@ -494,6 +494,23 @@ class BuiltInOperationBuiltInShuffleVector extends BuiltInOperation, @builtinshu
494494
override string getAPrimaryQlClass() { result = "BuiltInOperationBuiltInShuffleVector" }
495495
}
496496

497+
/**
498+
* A gcc `__builtin_shuffle` expression.
499+
*
500+
* It outputs a permutation of elements from one or two input vectors.
501+
* Please see https://gcc.gnu.org/onlinedocs/gcc/Vector-Extensions.html
502+
* for more information.
503+
* ```
504+
* // Concatenate every other element of 4-element vectors V1 and V2.
505+
* V3 = __builtin_shufflevector(V1, V2, {0, 2, 4, 6});
506+
* ```
507+
*/
508+
class BuiltInOperationBuiltInShuffle extends BuiltInOperation, @builtinshuffle {
509+
override string toString() { result = "__builtin_shuffle" }
510+
511+
override string getAPrimaryQlClass() { result = "BuiltInOperationBuiltInShuffle" }
512+
}
513+
497514
/**
498515
* A clang `__builtin_convertvector` expression.
499516
*
@@ -946,7 +963,7 @@ class BuiltInOperationIsFinal extends BuiltInOperation, @isfinalexpr {
946963
}
947964

948965
/**
949-
* The `__builtin_choose_expr` expression. This is a GNU/Clang extension.
966+
* The `__builtin_choose_expr` expression. This is a gcc/clang extension.
950967
*
951968
* The expression functions similarly to the ternary `?:` operator, except
952969
* that it is evaluated at compile-time.

cpp/ql/lib/semmlecode.cpp.dbscheme

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1654,6 +1654,7 @@ case @expr.kind of
16541654
| 331 = @isaggregate
16551655
| 332 = @hasuniqueobjectrepresentations
16561656
| 333 = @builtinbitcast
1657+
| 334 = @builtinshuffle
16571658
;
16581659

16591660
@var_args_expr = @vastartexpr
@@ -1719,6 +1720,7 @@ case @expr.kind of
17191720
| @isaggregate
17201721
| @hasuniqueobjectrepresentations
17211722
| @builtinbitcast
1723+
| @builtinshuffle
17221724
;
17231725

17241726
new_allocated_type(
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1+
| vector_types2.cpp:10:15:10:42 | __builtin_shuffle |
2+
| vector_types2.cpp:11:15:11:45 | __builtin_shuffle |
13
| vector_types.cpp:31:13:31:49 | __builtin_shufflevector |
24
| vector_types.cpp:58:10:58:52 | __builtin_convertvector |

cpp/ql/test/library-tests/vector_types/variables.expected

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@
1313
| file://:0:0:0:0 | gp_offset | gp_offset | file://:0:0:0:0 | unsigned int | 4 |
1414
| file://:0:0:0:0 | overflow_arg_area | overflow_arg_area | file://:0:0:0:0 | void * | 8 |
1515
| file://:0:0:0:0 | reg_save_area | reg_save_area | file://:0:0:0:0 | void * | 8 |
16+
| vector_types2.cpp:5:7:5:7 | a | a | vector_types2.cpp:2:13:2:15 | v4i | 16 |
17+
| vector_types2.cpp:6:7:6:7 | b | b | vector_types2.cpp:2:13:2:15 | v4i | 16 |
18+
| vector_types2.cpp:7:7:7:12 | mask_1 | mask_1 | vector_types2.cpp:2:13:2:15 | v4i | 16 |
19+
| vector_types2.cpp:8:7:8:12 | mask_2 | mask_2 | vector_types2.cpp:2:13:2:15 | v4i | 16 |
20+
| vector_types2.cpp:10:7:10:11 | res_1 | res_1 | vector_types2.cpp:2:13:2:15 | v4i | 16 |
21+
| vector_types2.cpp:11:7:11:11 | res_2 | res_2 | vector_types2.cpp:2:13:2:15 | v4i | 16 |
1622
| vector_types.cpp:9:21:9:21 | x | x | vector_types.cpp:6:15:6:17 | v4f | 16 |
1723
| vector_types.cpp:14:18:14:20 | lhs | lhs | vector_types.cpp:6:15:6:17 | v4f | 16 |
1824
| vector_types.cpp:14:27:14:29 | rhs | rhs | vector_types.cpp:6:15:6:17 | v4f | 16 |
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// semmle-extractor-options: --gnu --gnu_version 80000
2+
typedef int v4i __attribute__((vector_size (16)));
3+
4+
void f() {
5+
v4i a = {1,2,3,4};
6+
v4i b = {5,6,7,8};
7+
v4i mask_1 = {3,0,1,2};
8+
v4i mask_2 = {3,5,4,2};
9+
10+
v4i res_1 = __builtin_shuffle(a, mask_1);
11+
v4i res_2 = __builtin_shuffle(a, b, mask_2);
12+
}

0 commit comments

Comments
 (0)