23#include "llvm/ADT/SmallVector.h"
24#include "llvm/ADT/StringExtras.h"
25#include "llvm/ADT/StringMap.h"
26#include "llvm/Support/AMDGPUAddrSpace.h"
27#include "llvm/Support/AMDGPUAsyncStages.h"
28#include "llvm/Support/AtomicOrdering.h"
29#include "llvm/TargetParser/AMDGPUTargetParser.h"
30#include "llvm/TargetParser/AtomicScope.h"
41 const auto *FD =
SemaRef.getCurFunctionDecl(
true);
42 assert(FD &&
"AMDGPU builtins should not be used outside of a function");
43 llvm::StringMap<bool> CallerFeatureMap;
49 case AMDGPU::BI__builtin_amdgcn_raw_ptr_buffer_load_lds:
50 case AMDGPU::BI__builtin_amdgcn_raw_ptr_buffer_load_async_lds:
51 case AMDGPU::BI__builtin_amdgcn_struct_ptr_buffer_load_lds:
52 case AMDGPU::BI__builtin_amdgcn_struct_ptr_buffer_load_async_lds:
53 case AMDGPU::BI__builtin_amdgcn_load_to_lds:
54 case AMDGPU::BI__builtin_amdgcn_load_async_to_lds:
55 case AMDGPU::BI__builtin_amdgcn_global_load_lds:
56 case AMDGPU::BI__builtin_amdgcn_global_load_async_lds: {
57 constexpr const int SizeIdx = 2;
65 SemaRef.VerifyIntegerConstantExpression(ArgExpr, &Size);
66 assert(!R.isInvalid());
67 switch (Size.getSExtValue()) {
80 diag::err_amdgcn_load_lds_size_invalid_value)
83 diag::note_amdgcn_load_lds_size_valid_value)
88 case AMDGPU::BI__builtin_amdgcn_uicmp:
89 case AMDGPU::BI__builtin_amdgcn_uicmpl:
90 case AMDGPU::BI__builtin_amdgcn_sicmp:
91 case AMDGPU::BI__builtin_amdgcn_sicmpl:
92 case AMDGPU::BI__builtin_amdgcn_fcmp:
93 case AMDGPU::BI__builtin_amdgcn_fcmpf: {
101 << (IsWave32 ?
"__builtin_amdgcn_ballot_w32"
102 :
"__builtin_amdgcn_ballot_w64");
105 case AMDGPU::BI__builtin_amdgcn_get_fpenv:
106 case AMDGPU::BI__builtin_amdgcn_set_fpenv:
108 case AMDGPU::BI__builtin_amdgcn_atomic_inc32:
109 case AMDGPU::BI__builtin_amdgcn_atomic_inc64:
110 case AMDGPU::BI__builtin_amdgcn_atomic_dec32:
111 case AMDGPU::BI__builtin_amdgcn_atomic_dec64:
112 case AMDGPU::BI__builtin_amdgcn_fence: {
113 bool IsFence = BuiltinID == AMDGPU::BI__builtin_amdgcn_fence;
114 unsigned OrderIndex = IsFence ? 0 : 2;
115 unsigned ScopeIndex = IsFence ? 1 : 3;
116 Expr *OrderExpr = TheCall->
getArg(OrderIndex);
117 Expr *ScopeExpr = TheCall->
getArg(ScopeIndex);
126 return Diag(OrderExpr->
getExprLoc(), diag::err_typecheck_expect_int)
134 if (!llvm::isValidAtomicOrderingCABI(Ord) ||
135 (IsFence && (Ord == (
uint64_t)llvm::AtomicOrderingCABI::relaxed ||
136 Ord == (
uint64_t)llvm::AtomicOrderingCABI::consume)))
138 diag::warn_atomic_op_has_invalid_memory_order)
144 return Diag(ScopeExpr->
getExprLoc(), diag::err_expr_not_string_literal)
148 std::optional<std::string> ScopeName =
151 if (ScopeName && !llvm::parseAtomicScopeIRString(TT, *ScopeName)) {
152 return Diag(ScopeExpr->
getExprLoc(), diag::err_invalid_sync_scope)
158 case AMDGPU::BI__builtin_amdgcn_flat_prefetch:
159 case AMDGPU::BI__builtin_amdgcn_global_prefetch: {
162 if (
SemaRef.BuiltinConstantArg(TheCall, 1, CPol))
164 if ((CPol.getZExtValue() & 0x7) != 0x7)
168 diag::err_amdgcn_prefetch_reserved_th)
172 case AMDGPU::BI__builtin_amdgcn_s_wait_event: {
178 "gfx12-insts", CallerFeatureMap);
183 if (((IsGFX12Plus && !
Result[1]) || (!IsGFX12Plus &&
Result[0])) ||
184 Result.getZExtValue() > 2) {
187 diag::warn_amdgpu_s_wait_event_mask_no_effect_target)
190 diag::note_amdgpu_s_wait_event_suggested_value)
196 case AMDGPU::BI__builtin_amdgcn_mov_dpp:
198 case AMDGPU::BI__builtin_amdgcn_mov_dpp8:
200 case AMDGPU::BI__builtin_amdgcn_update_dpp:
202 case AMDGPU::BI__builtin_amdgcn_asyncmark:
203 case AMDGPU::BI__builtin_amdgcn_wait_asyncmark: {
204 bool IsMark = BuiltinID == AMDGPU::BI__builtin_amdgcn_asyncmark;
207 llvm::APSInt NumMarks;
208 if (!IsMark &&
SemaRef.BuiltinConstantArg(TheCall, 0, NumMarks))
214 unsigned MaskArgNum = IsMark ? 0 : 1;
215 return SemaRef.BuiltinConstantArgRange(
216 TheCall, MaskArgNum, 0,
217 llvm::to_underlying(llvm::AMDGPU::AsyncStages::ALL));
219 case AMDGPU::BI__builtin_amdgcn_av_load_b8:
220 case AMDGPU::BI__builtin_amdgcn_av_load_b16:
221 case AMDGPU::BI__builtin_amdgcn_av_load_b32:
222 case AMDGPU::BI__builtin_amdgcn_av_load_b64:
223 case AMDGPU::BI__builtin_amdgcn_av_load_b128:
225 case AMDGPU::BI__builtin_amdgcn_av_store_b8:
226 case AMDGPU::BI__builtin_amdgcn_av_store_b16:
227 case AMDGPU::BI__builtin_amdgcn_av_store_b32:
228 case AMDGPU::BI__builtin_amdgcn_av_store_b64:
229 case AMDGPU::BI__builtin_amdgcn_av_store_b128:
231 case AMDGPU::BI__builtin_amdgcn_cooperative_atomic_load_32x4B:
232 case AMDGPU::BI__builtin_amdgcn_cooperative_atomic_load_16x8B:
233 case AMDGPU::BI__builtin_amdgcn_cooperative_atomic_load_8x16B:
235 case AMDGPU::BI__builtin_amdgcn_cooperative_atomic_store_32x4B:
236 case AMDGPU::BI__builtin_amdgcn_cooperative_atomic_store_16x8B:
237 case AMDGPU::BI__builtin_amdgcn_cooperative_atomic_store_8x16B:
239 case AMDGPU::BI__builtin_amdgcn_flat_load_monitor_b32:
240 case AMDGPU::BI__builtin_amdgcn_flat_load_monitor_b64:
241 case AMDGPU::BI__builtin_amdgcn_flat_load_monitor_b128:
242 case AMDGPU::BI__builtin_amdgcn_global_load_monitor_b32:
243 case AMDGPU::BI__builtin_amdgcn_global_load_monitor_b64:
244 case AMDGPU::BI__builtin_amdgcn_global_load_monitor_b128:
246 case AMDGPU::BI__builtin_amdgcn_image_load_1d_v4f32_i32:
247 case AMDGPU::BI__builtin_amdgcn_image_load_1darray_v4f32_i32:
248 case AMDGPU::BI__builtin_amdgcn_image_load_1d_v4f16_i32:
249 case AMDGPU::BI__builtin_amdgcn_image_load_1darray_v4f16_i32:
250 case AMDGPU::BI__builtin_amdgcn_image_load_2d_f32_i32:
251 case AMDGPU::BI__builtin_amdgcn_image_load_2d_v4f32_i32:
252 case AMDGPU::BI__builtin_amdgcn_image_load_2d_v4f16_i32:
253 case AMDGPU::BI__builtin_amdgcn_image_load_2darray_f32_i32:
254 case AMDGPU::BI__builtin_amdgcn_image_load_2darray_v4f32_i32:
255 case AMDGPU::BI__builtin_amdgcn_image_load_2darray_v4f16_i32:
256 case AMDGPU::BI__builtin_amdgcn_image_load_3d_v4f32_i32:
257 case AMDGPU::BI__builtin_amdgcn_image_load_3d_v4f16_i32:
258 case AMDGPU::BI__builtin_amdgcn_image_load_cube_v4f32_i32:
259 case AMDGPU::BI__builtin_amdgcn_image_load_cube_v4f16_i32:
260 case AMDGPU::BI__builtin_amdgcn_image_load_mip_1d_v4f32_i32:
261 case AMDGPU::BI__builtin_amdgcn_image_load_mip_1d_v4f16_i32:
262 case AMDGPU::BI__builtin_amdgcn_image_load_mip_1darray_v4f32_i32:
263 case AMDGPU::BI__builtin_amdgcn_image_load_mip_1darray_v4f16_i32:
264 case AMDGPU::BI__builtin_amdgcn_image_load_mip_2d_f32_i32:
265 case AMDGPU::BI__builtin_amdgcn_image_load_mip_2d_v4f32_i32:
266 case AMDGPU::BI__builtin_amdgcn_image_load_mip_2d_v4f16_i32:
267 case AMDGPU::BI__builtin_amdgcn_image_load_mip_2darray_f32_i32:
268 case AMDGPU::BI__builtin_amdgcn_image_load_mip_2darray_v4f32_i32:
269 case AMDGPU::BI__builtin_amdgcn_image_load_mip_2darray_v4f16_i32:
270 case AMDGPU::BI__builtin_amdgcn_image_load_mip_3d_v4f32_i32:
271 case AMDGPU::BI__builtin_amdgcn_image_load_mip_3d_v4f16_i32:
272 case AMDGPU::BI__builtin_amdgcn_image_load_mip_cube_v4f32_i32:
273 case AMDGPU::BI__builtin_amdgcn_image_load_mip_cube_v4f16_i32:
274 case AMDGPU::BI__builtin_amdgcn_image_sample_1d_v4f32_f32:
275 case AMDGPU::BI__builtin_amdgcn_image_sample_1darray_v4f32_f32:
276 case AMDGPU::BI__builtin_amdgcn_image_sample_1d_v4f16_f32:
277 case AMDGPU::BI__builtin_amdgcn_image_sample_1darray_v4f16_f32:
278 case AMDGPU::BI__builtin_amdgcn_image_sample_2d_f32_f32:
279 case AMDGPU::BI__builtin_amdgcn_image_sample_2d_v4f32_f32:
280 case AMDGPU::BI__builtin_amdgcn_image_sample_2d_v4f16_f32:
281 case AMDGPU::BI__builtin_amdgcn_image_sample_2darray_f32_f32:
282 case AMDGPU::BI__builtin_amdgcn_image_sample_2darray_v4f32_f32:
283 case AMDGPU::BI__builtin_amdgcn_image_sample_2darray_v4f16_f32:
284 case AMDGPU::BI__builtin_amdgcn_image_sample_3d_v4f32_f32:
285 case AMDGPU::BI__builtin_amdgcn_image_sample_3d_v4f16_f32:
286 case AMDGPU::BI__builtin_amdgcn_image_sample_cube_v4f32_f32:
287 case AMDGPU::BI__builtin_amdgcn_image_sample_cube_v4f16_f32:
288 case AMDGPU::BI__builtin_amdgcn_image_sample_lz_1d_v4f32_f32:
289 case AMDGPU::BI__builtin_amdgcn_image_sample_lz_1d_v4f16_f32:
290 case AMDGPU::BI__builtin_amdgcn_image_sample_lz_1darray_v4f32_f32:
291 case AMDGPU::BI__builtin_amdgcn_image_sample_lz_1darray_v4f16_f32:
292 case AMDGPU::BI__builtin_amdgcn_image_sample_lz_2d_f32_f32:
293 case AMDGPU::BI__builtin_amdgcn_image_sample_lz_2d_v4f32_f32:
294 case AMDGPU::BI__builtin_amdgcn_image_sample_lz_2d_v4f16_f32:
295 case AMDGPU::BI__builtin_amdgcn_image_sample_lz_2darray_f32_f32:
296 case AMDGPU::BI__builtin_amdgcn_image_sample_lz_2darray_v4f32_f32:
297 case AMDGPU::BI__builtin_amdgcn_image_sample_lz_2darray_v4f16_f32:
298 case AMDGPU::BI__builtin_amdgcn_image_sample_lz_3d_v4f32_f32:
299 case AMDGPU::BI__builtin_amdgcn_image_sample_lz_3d_v4f16_f32:
300 case AMDGPU::BI__builtin_amdgcn_image_sample_lz_cube_v4f32_f32:
301 case AMDGPU::BI__builtin_amdgcn_image_sample_lz_cube_v4f16_f32:
302 case AMDGPU::BI__builtin_amdgcn_image_sample_l_1d_v4f32_f32:
303 case AMDGPU::BI__builtin_amdgcn_image_sample_l_1d_v4f16_f32:
304 case AMDGPU::BI__builtin_amdgcn_image_sample_l_1darray_v4f32_f32:
305 case AMDGPU::BI__builtin_amdgcn_image_sample_l_1darray_v4f16_f32:
306 case AMDGPU::BI__builtin_amdgcn_image_sample_l_2d_f32_f32:
307 case AMDGPU::BI__builtin_amdgcn_image_sample_l_2d_v4f16_f32:
308 case AMDGPU::BI__builtin_amdgcn_image_sample_l_2d_v4f32_f32:
309 case AMDGPU::BI__builtin_amdgcn_image_sample_l_2darray_f32_f32:
310 case AMDGPU::BI__builtin_amdgcn_image_sample_l_2darray_v4f32_f32:
311 case AMDGPU::BI__builtin_amdgcn_image_sample_l_2darray_v4f16_f32:
312 case AMDGPU::BI__builtin_amdgcn_image_sample_l_3d_v4f32_f32:
313 case AMDGPU::BI__builtin_amdgcn_image_sample_l_3d_v4f16_f32:
314 case AMDGPU::BI__builtin_amdgcn_image_sample_l_cube_v4f32_f32:
315 case AMDGPU::BI__builtin_amdgcn_image_sample_l_cube_v4f16_f32:
316 case AMDGPU::BI__builtin_amdgcn_image_sample_d_1d_v4f32_f32:
317 case AMDGPU::BI__builtin_amdgcn_image_sample_d_1d_v4f16_f32:
318 case AMDGPU::BI__builtin_amdgcn_image_sample_d_1darray_v4f32_f32:
319 case AMDGPU::BI__builtin_amdgcn_image_sample_d_1darray_v4f16_f32:
320 case AMDGPU::BI__builtin_amdgcn_image_sample_d_2d_f32_f32:
321 case AMDGPU::BI__builtin_amdgcn_image_sample_d_2d_v4f32_f32:
322 case AMDGPU::BI__builtin_amdgcn_image_sample_d_2d_v4f16_f32:
323 case AMDGPU::BI__builtin_amdgcn_image_sample_d_2darray_f32_f32:
324 case AMDGPU::BI__builtin_amdgcn_image_sample_d_2darray_v4f32_f32:
325 case AMDGPU::BI__builtin_amdgcn_image_sample_d_2darray_v4f16_f32:
326 case AMDGPU::BI__builtin_amdgcn_image_sample_d_3d_v4f32_f32:
327 case AMDGPU::BI__builtin_amdgcn_image_sample_d_3d_v4f16_f32:
328 case AMDGPU::BI__builtin_amdgcn_image_gather4_lz_2d_v4f32_f32:
329 case AMDGPU::BI__builtin_amdgcn_image_gather4_lz_2d_v4f16_f32: {
331 unsigned ArgCount = TheCall->
getNumArgs() - 1;
336 constexpr unsigned DMaskArgNo = 0;
337 constexpr int Low = 0;
338 constexpr int High = 15;
339 if (
SemaRef.BuiltinConstantArg(TheCall, DMaskArgNo,
Result) ||
340 SemaRef.BuiltinConstantArgRange(TheCall, DMaskArgNo, Low, High,
346 int NumElementsInRetTy = 1;
348 if (
auto *VTy = dyn_cast<VectorType>(RetTy))
349 NumElementsInRetTy = VTy->getNumElements();
350 int NumActiveBitsInDMask =
352 if (NumActiveBitsInDMask > NumElementsInRetTy) {
354 diag::err_amdgcn_dmask_has_too_many_bits_set);
360 bool ExtraGatherChecks =
362 AMDGPU::BI__builtin_amdgcn_image_gather4_lz_2d_v4f32_f32 ||
364 AMDGPU::BI__builtin_amdgcn_image_gather4_lz_2d_v4f16_f32) &&
365 SemaRef.BuiltinConstantArgPower2(TheCall, 0);
367 return ExtraGatherChecks ||
369 (
SemaRef.BuiltinConstantArg(TheCall, (ArgCount - 1),
Result));
371 case AMDGPU::BI__builtin_amdgcn_image_store_1d_v4f32_i32:
372 case AMDGPU::BI__builtin_amdgcn_image_store_1darray_v4f32_i32:
373 case AMDGPU::BI__builtin_amdgcn_image_store_1d_v4f16_i32:
374 case AMDGPU::BI__builtin_amdgcn_image_store_1darray_v4f16_i32:
375 case AMDGPU::BI__builtin_amdgcn_image_store_2d_f32_i32:
376 case AMDGPU::BI__builtin_amdgcn_image_store_2d_v4f32_i32:
377 case AMDGPU::BI__builtin_amdgcn_image_store_2d_v4f16_i32:
378 case AMDGPU::BI__builtin_amdgcn_image_store_2darray_f32_i32:
379 case AMDGPU::BI__builtin_amdgcn_image_store_2darray_v4f32_i32:
380 case AMDGPU::BI__builtin_amdgcn_image_store_2darray_v4f16_i32:
381 case AMDGPU::BI__builtin_amdgcn_image_store_3d_v4f32_i32:
382 case AMDGPU::BI__builtin_amdgcn_image_store_3d_v4f16_i32:
383 case AMDGPU::BI__builtin_amdgcn_image_store_cube_v4f32_i32:
384 case AMDGPU::BI__builtin_amdgcn_image_store_cube_v4f16_i32:
385 case AMDGPU::BI__builtin_amdgcn_image_store_mip_1d_v4f32_i32:
386 case AMDGPU::BI__builtin_amdgcn_image_store_mip_1d_v4f16_i32:
387 case AMDGPU::BI__builtin_amdgcn_image_store_mip_1darray_v4f32_i32:
388 case AMDGPU::BI__builtin_amdgcn_image_store_mip_1darray_v4f16_i32:
389 case AMDGPU::BI__builtin_amdgcn_image_store_mip_2d_f32_i32:
390 case AMDGPU::BI__builtin_amdgcn_image_store_mip_2d_v4f32_i32:
391 case AMDGPU::BI__builtin_amdgcn_image_store_mip_2d_v4f16_i32:
392 case AMDGPU::BI__builtin_amdgcn_image_store_mip_2darray_f32_i32:
393 case AMDGPU::BI__builtin_amdgcn_image_store_mip_2darray_v4f32_i32:
394 case AMDGPU::BI__builtin_amdgcn_image_store_mip_2darray_v4f16_i32:
395 case AMDGPU::BI__builtin_amdgcn_image_store_mip_3d_v4f32_i32:
396 case AMDGPU::BI__builtin_amdgcn_image_store_mip_3d_v4f16_i32:
397 case AMDGPU::BI__builtin_amdgcn_image_store_mip_cube_v4f32_i32:
398 case AMDGPU::BI__builtin_amdgcn_image_store_mip_cube_v4f16_i32: {
400 unsigned ArgCount = TheCall->
getNumArgs() - 1;
405 constexpr unsigned DMaskArgNo = 1;
406 return SemaRef.BuiltinConstantArgRange(TheCall, DMaskArgNo, 0,
412 case AMDGPU::BI__builtin_amdgcn_wmma_i32_16x16x64_iu8:
413 case AMDGPU::BI__builtin_amdgcn_swmmac_i32_16x16x128_iu8: {
414 if (BuiltinID == AMDGPU::BI__builtin_amdgcn_wmma_i32_16x16x64_iu8) {
415 if (
SemaRef.checkArgCountRange(TheCall, 7, 8))
419 }
else if (BuiltinID ==
420 AMDGPU::BI__builtin_amdgcn_swmmac_i32_16x16x128_iu8) {
421 if (
SemaRef.checkArgCountRange(TheCall, 8, 9))
430 llvm::APSInt ClampValue;
431 if (!
SemaRef.VerifyIntegerConstantExpression(ClampArg, &ClampValue)
440 SemaRef.CheckSingleAssignmentConstraints(BoolTy, ClampExpr);
455 llvm_unreachable(
"Intrinsic requires imm for atomic ordering argument!");
457 llvm::AtomicOrderingCABI(AtomicOrdArgRes.
Val.
getInt().getZExtValue());
463 if (!llvm::isValidAtomicOrderingCABI((
unsigned)Ord) ||
464 (!(MayLoad && MayStore) && (Ord == llvm::AtomicOrderingCABI::acq_rel)) ||
465 (!MayLoad && (Ord == llvm::AtomicOrderingCABI::acquire ||
466 Ord == llvm::AtomicOrderingCABI::consume)) ||
467 (!MayStore && Ord == llvm::AtomicOrderingCABI::release)) {
479 if (AS != llvm::AMDGPUAS::FLAT_ADDRESS &&
480 AS != llvm::AMDGPUAS::GLOBAL_ADDRESS) {
482 diag::err_amdgcn_global_or_flat_pointer_required)
489 if (
Scope->isValueDependent())
492 if (std::optional<llvm::APSInt>
Result =
494 if (!ScopeModel->isValid(
Result->getZExtValue())) {
496 diag::err_atomic_op_has_invalid_sync_scope)
497 <<
Scope->getSourceRange();
526 <<
Scope->getSourceRange();
545 unsigned NumDataArgs) {
546 assert(NumDataArgs <= 2);
547 if (
SemaRef.checkArgCountRange(TheCall, NumArgs, NumArgs))
551 for (
unsigned I = 0; I != NumDataArgs; ++I) {
552 Args[I] = TheCall->
getArg(I);
553 ArgTys[I] = Args[I]->
getType();
555 if (!ArgTys[I]->isArithmeticType() || ArgTys[I]->isAnyComplexType()) {
556 SemaRef.Diag(Args[I]->getBeginLoc(),
557 diag::err_typecheck_cond_expect_int_float)
565 if (
getASTContext().hasSameUnqualifiedType(ArgTys[0], ArgTys[1]))
568 if (((ArgTys[0]->isUnsignedIntegerType() &&
569 ArgTys[1]->isSignedIntegerType()) ||
570 (ArgTys[0]->isSignedIntegerType() &&
571 ArgTys[1]->isUnsignedIntegerType())) &&
576 SemaRef.Diag(Args[1]->getBeginLoc(),
577 diag::err_typecheck_call_different_arg_types)
578 << ArgTys[0] << ArgTys[1];
584 const AMDGPUFlatWorkGroupSizeAttr &
Attr) {
598 if (
Min == 0 &&
Max != 0) {
612AMDGPUFlatWorkGroupSizeAttr *
616 AMDGPUFlatWorkGroupSizeAttr TmpAttr(Context, CI, MinExpr, MaxExpr);
620 return ::new (Context)
621 AMDGPUFlatWorkGroupSizeAttr(Context, CI, MinExpr, MaxExpr);
641 const AMDGPUWavesPerEUAttr &
Attr) {
659 if (
Min == 0 &&
Max != 0) {
673AMDGPUWavesPerEUAttr *
677 AMDGPUWavesPerEUAttr TmpAttr(Context, CI, MinExpr, MaxExpr);
682 return ::new (Context) AMDGPUWavesPerEUAttr(Context, CI, MinExpr, MaxExpr);
702 Diag(AL.
getLoc(), diag::warn_amdgpu_num_reg_attr_deprecated) << AL;
706 if (!
SemaRef.checkUInt32Argument(AL, NumSGPRExpr, NumSGPR))
714 Diag(AL.
getLoc(), diag::warn_amdgpu_num_reg_attr_deprecated) << AL;
718 if (!
SemaRef.checkUInt32Argument(AL, NumVGPRExpr, NumVGPR))
728 const AMDGPUMaxNumWorkGroupsAttr &
Attr) {
741 Expr *Exprs[3] = {XExpr, YExpr, ZExpr};
742 for (
int i = 0; i < 3; i++) {
761 AMDGPUMaxNumWorkGroupsAttr TmpAttr(Context, CI, XExpr, YExpr, ZExpr);
762 assert(!
SemaRef.isSFINAEContext() &&
763 "Can't produce SFINAE diagnostic pointing to temporary attribute");
769 return ::new (Context)
770 AMDGPUMaxNumWorkGroupsAttr(Context, CI, XExpr, YExpr, ZExpr);
795 return *ExpandedPredicates
796 .insert(
SemaRef.BuildBoolLiteral(Loc,
false).get())
804 if (BI == AMDGPU::BI__builtin_amdgcn_processor_is) {
807 Diag(Loc, diag::err_amdgcn_processor_is_arg_not_literal);
811 StringRef N = GFX->getString();
813 if (llvm::AMDGPU::parseArchAMDGCN(N) == llvm::AMDGPU::GK_NONE) {
814 Diag(Loc, diag::err_amdgcn_processor_is_arg_invalid_value) << N;
816 llvm::AMDGPU::fillValidArchListAMDGCN(ValidList);
817 if (!ValidList.empty())
818 Diag(Loc, diag::note_amdgcn_processor_is_valid_options)
819 << llvm::join(ValidList,
", ");
824 return *ExpandedPredicates.insert(CE).first;
831 Diag(Loc, diag::err_amdgcn_is_invocable_arg_invalid_value) << Arg;
837 return *ExpandedPredicates.insert(CE).first;
843 llvm::StringMap<bool> CF;
849 return *ExpandedPredicates.insert(
SemaRef.BuildBoolLiteral(Loc, P).get())
854 return ExpandedPredicates.contains(E);
858 PotentiallyUnguardedBuiltinUsers.insert(FD);
862 return PotentiallyUnguardedBuiltinUsers.contains(FD);
882 static Expr *FindPredicate(
Expr *Cond) {
883 if (
auto *CE = dyn_cast<CallExpr>(Cond)) {
884 if (CE->getBuiltinCallee() == AMDGPU::BI__builtin_amdgcn_is_invocable ||
885 CE->getBuiltinCallee() == AMDGPU::BI__builtin_amdgcn_processor_is)
887 }
else if (
auto *UO = dyn_cast<UnaryOperator>(Cond)) {
888 return FindPredicate(UO->getSubExpr());
889 }
else if (
auto *BO = dyn_cast<BinaryOperator>(Cond)) {
890 if ((Cond = FindPredicate(BO->getLHS())))
892 return FindPredicate(BO->getRHS());
897 bool EnterPredicateGuardedContext(CallExpr *P);
898 void ExitPredicateGuardedContext(
bool WasProcessorCheck);
899 bool TraverseGuardedStmt(Stmt *S, CallExpr *P);
902 DiagnoseUnguardedBuiltins(Sema &SemaRef) : SemaRef(SemaRef) {
903 if (
auto *TAT = SemaRef.getCurFunctionDecl(
true)->getAttr<TargetAttr>()) {
906 TAT->getX86AddedFeatures(TargetFeatures);
907 if (auto GFXIP = TAT->getX86Architecture())
908 CurrentGFXIP.emplace_back(TAT->getLocation(), *GFXIP);
912 bool TraverseLambdaExpr(
LambdaExpr *LE)
override {
913 if (SemaRef.AMDGPU().HasPotentiallyUnguardedBuiltinUsage(
914 LE->getCallOperator()))
916 return DynamicRecursiveASTVisitor::TraverseLambdaExpr(LE);
919 bool TraverseStmt(Stmt *S)
override {
925 void IssueDiagnostics(Stmt *S) { TraverseStmt(S); }
927 bool TraverseIfStmt(IfStmt *
If)
override {
928 if (
auto *CE = dyn_cast_or_null<CallExpr>(FindPredicate(
If->getCond())))
929 return TraverseGuardedStmt(
If, CE);
930 return DynamicRecursiveASTVisitor::TraverseIfStmt(
If);
933 bool TraverseCaseStmt(CaseStmt *CS)
override {
934 return TraverseStmt(CS->getSubStmt());
937 bool TraverseConditionalOperator(ConditionalOperator *CO)
override {
938 if (
auto *CE = dyn_cast_or_null<CallExpr>(FindPredicate(CO->getCond())))
939 return TraverseGuardedStmt(CO, CE);
940 return DynamicRecursiveASTVisitor::TraverseConditionalOperator(CO);
943 bool VisitAsmStmt(AsmStmt *ASM)
override;
944 bool VisitCallExpr(CallExpr *CE)
override;
947bool DiagnoseUnguardedBuiltins::EnterPredicateGuardedContext(
CallExpr *P) {
948 bool IsProcessorCheck =
949 P->getBuiltinCallee() == AMDGPU::BI__builtin_amdgcn_processor_is;
951 if (IsProcessorCheck) {
952 StringRef G = cast<clang::StringLiteral>(P->getArg(0))->getString();
954 if (!CurrentGFXIP.empty() && G != CurrentGFXIP.back().second) {
955 SemaRef.Diag(P->getExprLoc(),
956 diag::err_amdgcn_conflicting_is_processor_options)
958 SemaRef.Diag(CurrentGFXIP.back().first,
959 diag::note_amdgcn_previous_is_processor_guard);
961 CurrentGFXIP.emplace_back(P->getExprLoc(), G);
963 auto *FD = cast<FunctionDecl>(
964 cast<DeclRefExpr>(P->getArg(0))->getReferencedDeclOfCallee());
965 GuardedBuiltins.push_back(FD->getBuiltinID());
968 return IsProcessorCheck;
971void DiagnoseUnguardedBuiltins::ExitPredicateGuardedContext(
bool WasProcCheck) {
973 CurrentGFXIP.pop_back();
975 GuardedBuiltins.pop_back();
978inline std::pair<Stmt *, Stmt *> GetTraversalOrder(Stmt *S) {
979 std::pair<Stmt *, Stmt *> Ordered;
982 if (
auto *CO = dyn_cast<ConditionalOperator>(S)) {
984 Ordered = {CO->getTrueExpr(), CO->getFalseExpr()};
985 }
else if (
auto *
If = dyn_cast<IfStmt>(S)) {
987 Ordered = {
If->getThen(),
If->getElse()};
990 if (
auto *UO = dyn_cast<UnaryOperator>(
Condition))
991 if (UO->getOpcode() == UnaryOperatorKind::UO_LNot)
992 std::swap(Ordered.first, Ordered.second);
997bool DiagnoseUnguardedBuiltins::TraverseGuardedStmt(Stmt *S, CallExpr *P) {
998 assert(S &&
"Unexpected missing Statement!");
999 assert(P &&
"Unexpected missing Predicate!");
1001 auto [Guarded, Unguarded] = GetTraversalOrder(S);
1003 bool WasProcessorCheck = EnterPredicateGuardedContext(P);
1005 bool Continue = TraverseStmt(Guarded);
1007 ExitPredicateGuardedContext(WasProcessorCheck);
1009 return Continue && TraverseStmt(Unguarded);
1012bool DiagnoseUnguardedBuiltins::VisitAsmStmt(AsmStmt *ASM) {
1014 if (!CurrentGFXIP.empty())
1017 std::string S = ASM->generateAsmString(SemaRef.getASTContext());
1018 SemaRef.Diag(ASM->getAsmLoc(), diag::warn_amdgcn_unguarded_asm_stmt) << S;
1019 SemaRef.Diag(ASM->getAsmLoc(), diag::note_amdgcn_unguarded_asm_silence) << S;
1024bool DiagnoseUnguardedBuiltins::VisitCallExpr(CallExpr *CE) {
1025 unsigned ID = CE->getBuiltinCallee();
1026 Builtin::Context &BInfo = SemaRef.getASTContext().BuiltinInfo;
1030 if (!BInfo.isTSBuiltin(ID))
1032 if (ID == AMDGPU::BI__builtin_amdgcn_processor_is ||
1033 ID == AMDGPU::BI__builtin_amdgcn_is_invocable)
1035 if (llvm::find(GuardedBuiltins, ID) != GuardedBuiltins.end())
1038 StringRef FL(BInfo.getRequiredFeatures(ID));
1039 llvm::StringMap<bool> FeatureMap;
1040 if (CurrentGFXIP.empty()) {
1041 for (
auto &&F : TargetFeatures)
1042 FeatureMap[F] =
true;
1043 for (
auto &&GID : GuardedBuiltins)
1044 for (
auto &&F : llvm::split(BInfo.getRequiredFeatures(GID),
','))
1045 FeatureMap[F] =
true;
1047 static const llvm::Triple AMDGCN(llvm::Triple::amdgpu,
1048 llvm::Triple::NoSubArch, llvm::Triple::AMD,
1049 llvm::Triple::AMDHSA);
1050 llvm::AMDGPU::fillAMDGPUFeatureMap(CurrentGFXIP.back().second, AMDGCN,
1054 FunctionDecl *BI = CE->getDirectCallee();
1055 SourceLocation BICallLoc = CE->getExprLoc();
1056 if (Builtin::evaluateRequiredTargetFeatures(FL, FeatureMap)) {
1057 SemaRef.Diag(BICallLoc, diag::warn_amdgcn_unguarded_builtin) << BI;
1058 SemaRef.Diag(BICallLoc, diag::note_amdgcn_unguarded_builtin_silence) << BI;
1060 StringRef GFXIP = CurrentGFXIP.empty() ?
"" : CurrentGFXIP.back().second;
1061 SemaRef.Diag(BICallLoc, diag::err_amdgcn_incompatible_builtin)
1062 << BI << FL << !CurrentGFXIP.empty() << GFXIP;
1063 if (!CurrentGFXIP.empty())
1064 SemaRef.Diag(CurrentGFXIP.back().first,
1065 diag::note_amdgcn_previous_is_processor_guard);
1073 DiagnoseUnguardedBuiltins(
SemaRef).IssueDiagnostics(FD->
getBody());
1085 SemaRef.Diag(Loc, diag::err_amdgpu_target_ext_type_unsupported)
1106 if (R->isInvalidDecl())
1109 if (!Context.getTargetInfo().hasAMDGPUTypes() &&
1110 (!Context.getAuxTargetInfo() ||
1111 !Context.getAuxTargetInfo()->hasAMDGPUTypes()))
1114 bool IsWrapper =
false;
1122 if (R->getNumFields() > 1) {
1123 SemaRef.Diag(NamedBarrField->getLocation(),
1124 diag::err_amdgpu_invalid_field_not_a_wrapper)
1125 << NamedBarrField->getType();
1128 diag::note_amdgpu_not_a_named_barrier_wrapper_too_many_fields)
1129 << R->getDeclName();
1134 DiagWrapperNote = [
this, R, NamedBarrField]() {
1135 SemaRef.Diag(NamedBarrField->getLocation(),
1136 diag::note_amdgpu_named_barrier_reason_field)
1137 << R->getDeclName() << NamedBarrField->getDeclName();
1143 const auto *CxxR = dyn_cast<CXXRecordDecl>(R);
1144 if (CxxR && !IsWrapper) {
1147 if (!
Base || !
Base->hasAttr<AMDGPUNamedBarrierWrapperAttr>())
1151 DiagWrapperNote = [
this, BS, R]() {
1154 SemaRef.Diag(BS.getBeginLoc(),
1155 diag::note_amdgpu_named_barrier_reason_inherited)
1156 << R->getDeclName() << BS.getType();
1166 AMDGPUNamedBarrierWrapperAttr::CreateImplicit(Context,
SourceRange()));
1169 if (CxxR && !CxxR->isCXX11StandardLayout()) {
1170 SemaRef.Diag(R->getLocation(),
1171 diag::err_amdgpu_named_barrier_wrapper_non_standard_layout)
1172 << R->getDeclName();
1173 assert(DiagWrapperNote &&
1174 "IsWrapper is set but no context diagnostic provided");
This file declares semantic analysis functions specific to AMDGPU.
Enumerates target-specific builtins in their own namespaces within namespace clang.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
const llvm::StringMap< bool > & getFunctionFeatureMap(const FunctionDecl *FD) const
Builtin::Context & BuiltinInfo
CanQualType getLogicalOperationType() const
The result type of logical operations, '<', '>', '!=', etc.
const TargetInfo & getTargetInfo() const
unsigned getTargetAddressSpace(LangAS AS) const
static std::unique_ptr< AtomicScopeModel > create(AtomicScopeModelKind K)
Create an atomic scope model by AtomicScopeModelKind.
Attr - This represents one attribute.
SourceLocation getLocation() const
SourceLocation getLoc() const
llvm::SmallString< 64 > getQuotedName(unsigned ID) const
Return the identifier name for the specified builtin inside single quotes for a diagnostic,...
bool isAuxBuiltinID(unsigned ID) const
Return true if the builtin ID belongs exclusively to the AuxTarget, and false if it belongs to both p...
unsigned getAuxBuiltinID(unsigned ID) const
Return real builtin ID (i.e.
const char * getRequiredFeatures(unsigned ID) const
Represents a base class of a C++ class.
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Expr * getArg(unsigned Arg)
getArg - Return the specified argument.
SourceLocation getBeginLoc() const
unsigned getBuiltinCallee() const
getBuiltinCallee - If this is a call to a builtin, return the builtin ID of the callee.
unsigned getNumArgs() const
getNumArgs - Return the number of actual arguments to this call.
Decl - This represents one declaration (or definition), e.g.
virtual bool TraverseStmt(MaybeConst< Stmt > *S)
This represents one expression.
bool EvaluateAsInt(EvalResult &Result, const ASTContext &Ctx, SideEffectsKind AllowSideEffects=SE_NoSideEffects, bool InConstantContext=false) const
EvaluateAsInt - Return true if this is a constant which we can fold and convert to an integer,...
Expr * IgnoreParenCasts() LLVM_READONLY
Skip past any parentheses and casts which might surround this expression until reaching a fixed point...
bool isValueDependent() const
Determines whether the value of this expression depends on.
Expr * IgnoreParens() LLVM_READONLY
Skip past any parentheses which might surround this expression until reaching a fixed point.
Decl * getReferencedDeclOfCallee()
bool EvaluateAsConstantExpr(EvalResult &Result, const ASTContext &Ctx, ConstantExprKind Kind=ConstantExprKind::Normal) const
Evaluate an expression that is required to be a constant expression.
bool isInstantiationDependent() const
Whether this expression is instantiation-dependent, meaning that it depends in some way on.
std::optional< std::string > tryEvaluateString(ASTContext &Ctx) const
If the current Expr can be evaluated to a pointer to a null-terminated constant string,...
SourceLocation getExprLoc() const LLVM_READONLY
getExprLoc - Return the preferred location for the arrow when diagnosing a problem with a generic exp...
Represents a member of a struct/union/class.
Represents a function declaration or definition.
Stmt * getBody(const FunctionDecl *&Definition) const
Retrieve the body (definition) of the function.
ParsedAttr - Represents a syntactic attribute.
unsigned getNumArgs() const
getNumArgs - Return the number of actual arguments to this attribute.
Expr * getArgAsExpr(unsigned Arg) const
bool checkAtLeastNumArgs(class Sema &S, unsigned Num) const
Check if the attribute has at least as many args as Num.
bool checkAtMostNumArgs(class Sema &S, unsigned Num) const
Check if the attribute has at most as many args as Num.
A (possibly-)qualified type.
const Type * getTypePtr() const
Retrieves a pointer to the underlying (unqualified) type.
Represents a struct/union/class.
Scope - A scope is a transient data structure that is used while parsing the program.
void handleAMDGPUMaxNumWorkGroupsAttr(Decl *D, const ParsedAttr &AL)
void addAMDGPUFlatWorkGroupSizeAttr(Decl *D, const AttributeCommonInfo &CI, Expr *Min, Expr *Max)
addAMDGPUFlatWorkGroupSizeAttr - Adds an amdgpu_flat_work_group_size attribute to a particular declar...
bool checkCoopAtomicFunctionCall(CallExpr *TheCall, bool IsStore)
bool HasPotentiallyUnguardedBuiltinUsage(FunctionDecl *FD) const
void handleAMDGPUFlatWorkGroupSizeAttr(Decl *D, const ParsedAttr &AL)
bool checkAVLoadStore(CallExpr *TheCall, bool IsStore)
bool checkAtomicMonitorLoad(CallExpr *TheCall)
void handleAMDGPUNumSGPRAttr(Decl *D, const ParsedAttr &AL)
AMDGPUMaxNumWorkGroupsAttr * CreateAMDGPUMaxNumWorkGroupsAttr(const AttributeCommonInfo &CI, Expr *XExpr, Expr *YExpr, Expr *ZExpr)
Create an AMDGPUMaxNumWorkGroupsAttr attribute.
Expr * ExpandAMDGPUPredicateBuiltIn(Expr *CE)
Expand a valid use of the feature identification builtins into its corresponding sequence of instruct...
AMDGPUWavesPerEUAttr * CreateAMDGPUWavesPerEUAttr(const AttributeCommonInfo &CI, Expr *Min, Expr *Max)
Create an AMDGPUWavesPerEUAttr attribute.
bool CheckAMDGCNBuiltinFunctionCall(const TargetInfo &TI, unsigned BuiltinID, CallExpr *TheCall)
void DiagnoseUnguardedBuiltinUsage(FunctionDecl *FD)
bool checkAMDGPUTypeSupport(QualType Ty, SourceLocation Loc)
Check if Ty is supported on this AMDGPU target.
void handleAMDGPUNumVGPRAttr(Decl *D, const ParsedAttr &AL)
void checkAtomicOrderingCABIArg(Expr *E, bool MayLoad, bool MayStore)
Emits a diagnostic if the E is not an atomic ordering encoded in the C ABI format,...
AMDGPUFlatWorkGroupSizeAttr * CreateAMDGPUFlatWorkGroupSizeAttr(const AttributeCommonInfo &CI, Expr *Min, Expr *Max)
Create an AMDGPUWavesPerEUAttr attribute.
void AddPotentiallyUnguardedBuiltinUser(FunctionDecl *FD)
Diagnose unguarded usages of AMDGPU builtins and recommend guarding with __builtin_amdgcn_is_invocabl...
bool checkMovDPPFunctionCall(CallExpr *TheCall, unsigned NumArgs, unsigned NumDataArgs)
void handleAMDGPUWavesPerEUAttr(Decl *D, const ParsedAttr &AL)
void checkNamedBarrierWrapper(RecordDecl *R)
Called in ActOnFields - whenever a C/C++ Record is being finalized.
bool IsPredicate(Expr *E) const
void addAMDGPUWavesPerEUAttr(Decl *D, const AttributeCommonInfo &CI, Expr *Min, Expr *Max)
addAMDGPUWavePersEUAttr - Adds an amdgpu_waves_per_eu attribute to a particular declaration.
void addAMDGPUMaxNumWorkGroupsAttr(Decl *D, const AttributeCommonInfo &CI, Expr *XExpr, Expr *YExpr, Expr *ZExpr)
addAMDGPUMaxNumWorkGroupsAttr - Adds an amdgpu_max_num_work_groups attribute to a particular declarat...
ASTContext & getASTContext() const
SemaDiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID)
Emit a diagnostic.
Sema - This implements semantic analysis and AST building for C.
bool DiagnoseUnexpandedParameterPack(SourceLocation Loc, TypeSourceInfo *T, UnexpandedParameterPackContext UPPC)
If the given type contains an unexpanded parameter pack, diagnose the error.
bool checkUInt32Argument(const AttrInfo &AI, const Expr *Expr, uint32_t &Val, unsigned Idx=UINT_MAX, bool StrictlyUnsigned=false)
If Expr is a valid integer constant, get the value of the integer expression and return success or fa...
Encodes a location in the source.
A trivial tuple used to represent a source range.
SourceRange getSourceRange() const LLVM_READONLY
SourceLocation tokens are not useful in isolation - they are low level value objects created/interpre...
SourceLocation getBeginLoc() const LLVM_READONLY
Exposes information about the current target.
const llvm::Triple & getTriple() const
Returns the target triple of the primary target.
virtual bool isProcessorName(StringRef Name) const
Returns true if the target's processor is compatible with the processor named by Name,...
The base class of the type hierarchy.
const Type * getPointeeOrArrayElementType() const
If this is a pointer type, return the pointee type.
bool isAMDGPUNamedBarrierTypeOrWrapper() const
Check if the type is the AMDGPU named barrier type/a RecordType of a named barrier wrapper,...
QualType getPointeeType() const
If this is a pointer, ObjC object pointer, or block pointer, this returns the respective pointee.
bool isAMDGPUNamedBarrierType() const
Check if the type is the AMDGPU named barrier type, or an array thereof.
Defines the clang::TargetInfo interface.
bool evaluateRequiredTargetFeatures(llvm::StringRef RequiredFatures, const llvm::StringMap< bool > &TargetFetureMap)
Returns true if the required target features of a builtin function are enabled.
bool LE(InterpState &S, CodePtr OpPC)
Top level wrappers for InstallAPI frontend operations.
bool isa(CodeGen::Address addr)
static FieldDecl * getNamedBarrierField(const RecordDecl *R)
static bool checkScopeAsInt(SemaAMDGPU &S, Expr *Scope)
@ If
'if' clause, allowed on all the Compute Constructs, Data Constructs, Executable Constructs,...
@ Result
The result type of a method or function.
static bool checkAMDGPUMaxNumWorkGroupsArguments(Sema &S, Expr *XExpr, Expr *YExpr, Expr *ZExpr, const AMDGPUMaxNumWorkGroupsAttr &Attr)
DynamicRecursiveASTVisitorBase< false > DynamicRecursiveASTVisitor
U cast(CodeGen::Address addr)
static bool checkAMDGPUFlatWorkGroupSizeArguments(Sema &S, Expr *MinExpr, Expr *MaxExpr, const AMDGPUFlatWorkGroupSizeAttr &Attr)
static bool checkGlobalOrFlatPointerArg(SemaAMDGPU &S, CallExpr *TheCall)
ActionResult< Expr * > ExprResult
static bool checkAMDGPUWavesPerEUArguments(Sema &S, Expr *MinExpr, Expr *MaxExpr, const AMDGPUWavesPerEUAttr &Attr)
int const char * function
__builtin_elementwise_add_sat __builtin_elementwise_sub_sat uint32_t uint64_t __builtin_riscv_ssha_i32 __builtin_riscv_sshl_u32 uint64_t
int32_t uint32_t uint32_t __builtin_riscv_mhracc_i32 __builtin_riscv_mhraccu_u32 __builtin_riscv_mhraccsu_i32 __builtin_riscv_mhacc_h1_i32 __builtin_riscv_mhaccsu_h1_i32 __builtin_riscv_mulqr_i32 __builtin_riscv_mqrwacc_i64 __packed_splat4 __packed_splat2 __packed_splat8 __packed_splat4 __packed_splat2 uint8_t
EvalResult is a struct with detailed info about an evaluated expression.
APValue Val
Val - This is the value the expression can be folded to.