24#include "llvm/ADT/SmallVector.h"
25#include "llvm/ADT/StringExtras.h"
26#include "llvm/ADT/StringMap.h"
27#include "llvm/Support/AMDGPUAddrSpace.h"
28#include "llvm/Support/AMDGPUAsyncStages.h"
29#include "llvm/Support/AtomicOrdering.h"
30#include "llvm/TargetParser/AMDGPUTargetParser.h"
31#include "llvm/TargetParser/AtomicScope.h"
42 const auto *FD =
SemaRef.getCurFunctionDecl(
true);
43 assert(FD &&
"AMDGPU builtins should not be used outside of a function");
44 llvm::StringMap<bool> CallerFeatureMap;
50 case AMDGPU::BI__builtin_amdgcn_raw_ptr_buffer_load_lds:
51 case AMDGPU::BI__builtin_amdgcn_raw_ptr_buffer_load_async_lds:
52 case AMDGPU::BI__builtin_amdgcn_struct_ptr_buffer_load_lds:
53 case AMDGPU::BI__builtin_amdgcn_struct_ptr_buffer_load_async_lds:
54 case AMDGPU::BI__builtin_amdgcn_load_to_lds:
55 case AMDGPU::BI__builtin_amdgcn_load_async_to_lds:
56 case AMDGPU::BI__builtin_amdgcn_global_load_lds:
57 case AMDGPU::BI__builtin_amdgcn_global_load_async_lds: {
58 constexpr const int SizeIdx = 2;
66 SemaRef.VerifyIntegerConstantExpression(ArgExpr, &Size);
67 assert(!R.isInvalid());
68 switch (Size.getSExtValue()) {
81 diag::err_amdgcn_load_lds_size_invalid_value)
84 diag::note_amdgcn_load_lds_size_valid_value)
89 case AMDGPU::BI__builtin_amdgcn_uicmp:
90 case AMDGPU::BI__builtin_amdgcn_uicmpl:
91 case AMDGPU::BI__builtin_amdgcn_sicmp:
92 case AMDGPU::BI__builtin_amdgcn_sicmpl:
93 case AMDGPU::BI__builtin_amdgcn_fcmp:
94 case AMDGPU::BI__builtin_amdgcn_fcmpf: {
102 << (IsWave32 ?
"__builtin_amdgcn_ballot_w32"
103 :
"__builtin_amdgcn_ballot_w64");
106 case AMDGPU::BI__builtin_amdgcn_get_fpenv:
107 case AMDGPU::BI__builtin_amdgcn_set_fpenv:
109 case AMDGPU::BI__builtin_amdgcn_atomic_inc32:
110 case AMDGPU::BI__builtin_amdgcn_atomic_inc64:
111 case AMDGPU::BI__builtin_amdgcn_atomic_dec32:
112 case AMDGPU::BI__builtin_amdgcn_atomic_dec64:
113 case AMDGPU::BI__builtin_amdgcn_fence: {
114 bool IsFence = BuiltinID == AMDGPU::BI__builtin_amdgcn_fence;
115 unsigned OrderIndex = IsFence ? 0 : 2;
116 unsigned ScopeIndex = IsFence ? 1 : 3;
117 Expr *OrderExpr = TheCall->
getArg(OrderIndex);
118 Expr *ScopeExpr = TheCall->
getArg(ScopeIndex);
127 return Diag(OrderExpr->
getExprLoc(), diag::err_typecheck_expect_int)
133 if (!llvm::isValidAtomicOrderingCABI(Ord))
135 diag::warn_atomic_op_has_invalid_memory_order)
137 switch (
static_cast<llvm::AtomicOrderingCABI
>(Ord)) {
138 case llvm::AtomicOrderingCABI::relaxed:
139 case llvm::AtomicOrderingCABI::consume:
142 diag::warn_atomic_op_has_invalid_memory_order)
145 case llvm::AtomicOrderingCABI::acquire:
146 case llvm::AtomicOrderingCABI::release:
147 case llvm::AtomicOrderingCABI::acq_rel:
148 case llvm::AtomicOrderingCABI::seq_cst:
155 return Diag(ScopeExpr->
getExprLoc(), diag::err_expr_not_string_literal)
159 std::optional<std::string> ScopeName =
162 if (ScopeName && !llvm::parseAtomicScopeIRString(TT, *ScopeName)) {
163 return Diag(ScopeExpr->
getExprLoc(), diag::err_invalid_sync_scope)
169 case AMDGPU::BI__builtin_amdgcn_flat_prefetch:
170 case AMDGPU::BI__builtin_amdgcn_global_prefetch: {
173 if (
SemaRef.BuiltinConstantArg(TheCall, 1, CPol))
175 if ((CPol.getZExtValue() & 0x7) != 0x7)
179 diag::err_amdgcn_prefetch_reserved_th)
183 case AMDGPU::BI__builtin_amdgcn_s_wait_event: {
189 "gfx12-insts", CallerFeatureMap);
194 if (((IsGFX12Plus && !
Result[1]) || (!IsGFX12Plus &&
Result[0])) ||
195 Result.getZExtValue() > 2) {
198 diag::warn_amdgpu_s_wait_event_mask_no_effect_target)
201 diag::note_amdgpu_s_wait_event_suggested_value)
207 case AMDGPU::BI__builtin_amdgcn_mov_dpp:
209 case AMDGPU::BI__builtin_amdgcn_mov_dpp8:
211 case AMDGPU::BI__builtin_amdgcn_update_dpp:
213 case AMDGPU::BI__builtin_amdgcn_asyncmark:
214 case AMDGPU::BI__builtin_amdgcn_wait_asyncmark: {
215 bool IsMark = BuiltinID == AMDGPU::BI__builtin_amdgcn_asyncmark;
218 llvm::APSInt NumMarks;
219 if (!IsMark &&
SemaRef.BuiltinConstantArg(TheCall, 0, NumMarks))
225 unsigned MaskArgNum = IsMark ? 0 : 1;
226 return SemaRef.BuiltinConstantArgRange(
227 TheCall, MaskArgNum, 0,
228 llvm::to_underlying(llvm::AMDGPU::AsyncStages::ALL));
230 case AMDGPU::BI__builtin_amdgcn_av_load_b8:
231 case AMDGPU::BI__builtin_amdgcn_av_load_b16:
232 case AMDGPU::BI__builtin_amdgcn_av_load_b32:
233 case AMDGPU::BI__builtin_amdgcn_av_load_b64:
234 case AMDGPU::BI__builtin_amdgcn_av_load_b128:
236 case AMDGPU::BI__builtin_amdgcn_av_store_b8:
237 case AMDGPU::BI__builtin_amdgcn_av_store_b16:
238 case AMDGPU::BI__builtin_amdgcn_av_store_b32:
239 case AMDGPU::BI__builtin_amdgcn_av_store_b64:
240 case AMDGPU::BI__builtin_amdgcn_av_store_b128:
242 case AMDGPU::BI__builtin_amdgcn_cooperative_atomic_load_32x4B:
243 case AMDGPU::BI__builtin_amdgcn_cooperative_atomic_load_16x8B:
244 case AMDGPU::BI__builtin_amdgcn_cooperative_atomic_load_8x16B:
246 case AMDGPU::BI__builtin_amdgcn_cooperative_atomic_store_32x4B:
247 case AMDGPU::BI__builtin_amdgcn_cooperative_atomic_store_16x8B:
248 case AMDGPU::BI__builtin_amdgcn_cooperative_atomic_store_8x16B:
250 case AMDGPU::BI__builtin_amdgcn_flat_load_monitor_b32:
251 case AMDGPU::BI__builtin_amdgcn_flat_load_monitor_b64:
252 case AMDGPU::BI__builtin_amdgcn_flat_load_monitor_b128:
253 case AMDGPU::BI__builtin_amdgcn_global_load_monitor_b32:
254 case AMDGPU::BI__builtin_amdgcn_global_load_monitor_b64:
255 case AMDGPU::BI__builtin_amdgcn_global_load_monitor_b128:
257 case AMDGPU::BI__builtin_amdgcn_image_load_1d_v4f32_i32:
258 case AMDGPU::BI__builtin_amdgcn_image_load_1darray_v4f32_i32:
259 case AMDGPU::BI__builtin_amdgcn_image_load_1d_v4f16_i32:
260 case AMDGPU::BI__builtin_amdgcn_image_load_1darray_v4f16_i32:
261 case AMDGPU::BI__builtin_amdgcn_image_load_2d_f32_i32:
262 case AMDGPU::BI__builtin_amdgcn_image_load_2d_v4f32_i32:
263 case AMDGPU::BI__builtin_amdgcn_image_load_2d_v4f16_i32:
264 case AMDGPU::BI__builtin_amdgcn_image_load_2darray_f32_i32:
265 case AMDGPU::BI__builtin_amdgcn_image_load_2darray_v4f32_i32:
266 case AMDGPU::BI__builtin_amdgcn_image_load_2darray_v4f16_i32:
267 case AMDGPU::BI__builtin_amdgcn_image_load_3d_v4f32_i32:
268 case AMDGPU::BI__builtin_amdgcn_image_load_3d_v4f16_i32:
269 case AMDGPU::BI__builtin_amdgcn_image_load_cube_v4f32_i32:
270 case AMDGPU::BI__builtin_amdgcn_image_load_cube_v4f16_i32:
271 case AMDGPU::BI__builtin_amdgcn_image_load_mip_1d_v4f32_i32:
272 case AMDGPU::BI__builtin_amdgcn_image_load_mip_1d_v4f16_i32:
273 case AMDGPU::BI__builtin_amdgcn_image_load_mip_1darray_v4f32_i32:
274 case AMDGPU::BI__builtin_amdgcn_image_load_mip_1darray_v4f16_i32:
275 case AMDGPU::BI__builtin_amdgcn_image_load_mip_2d_f32_i32:
276 case AMDGPU::BI__builtin_amdgcn_image_load_mip_2d_v4f32_i32:
277 case AMDGPU::BI__builtin_amdgcn_image_load_mip_2d_v4f16_i32:
278 case AMDGPU::BI__builtin_amdgcn_image_load_mip_2darray_f32_i32:
279 case AMDGPU::BI__builtin_amdgcn_image_load_mip_2darray_v4f32_i32:
280 case AMDGPU::BI__builtin_amdgcn_image_load_mip_2darray_v4f16_i32:
281 case AMDGPU::BI__builtin_amdgcn_image_load_mip_3d_v4f32_i32:
282 case AMDGPU::BI__builtin_amdgcn_image_load_mip_3d_v4f16_i32:
283 case AMDGPU::BI__builtin_amdgcn_image_load_mip_cube_v4f32_i32:
284 case AMDGPU::BI__builtin_amdgcn_image_load_mip_cube_v4f16_i32:
285 case AMDGPU::BI__builtin_amdgcn_image_sample_1d_v4f32_f32:
286 case AMDGPU::BI__builtin_amdgcn_image_sample_1darray_v4f32_f32:
287 case AMDGPU::BI__builtin_amdgcn_image_sample_1d_v4f16_f32:
288 case AMDGPU::BI__builtin_amdgcn_image_sample_1darray_v4f16_f32:
289 case AMDGPU::BI__builtin_amdgcn_image_sample_2d_f32_f32:
290 case AMDGPU::BI__builtin_amdgcn_image_sample_2d_v4f32_f32:
291 case AMDGPU::BI__builtin_amdgcn_image_sample_2d_v4f16_f32:
292 case AMDGPU::BI__builtin_amdgcn_image_sample_2darray_f32_f32:
293 case AMDGPU::BI__builtin_amdgcn_image_sample_2darray_v4f32_f32:
294 case AMDGPU::BI__builtin_amdgcn_image_sample_2darray_v4f16_f32:
295 case AMDGPU::BI__builtin_amdgcn_image_sample_3d_v4f32_f32:
296 case AMDGPU::BI__builtin_amdgcn_image_sample_3d_v4f16_f32:
297 case AMDGPU::BI__builtin_amdgcn_image_sample_cube_v4f32_f32:
298 case AMDGPU::BI__builtin_amdgcn_image_sample_cube_v4f16_f32:
299 case AMDGPU::BI__builtin_amdgcn_image_sample_lz_1d_v4f32_f32:
300 case AMDGPU::BI__builtin_amdgcn_image_sample_lz_1d_v4f16_f32:
301 case AMDGPU::BI__builtin_amdgcn_image_sample_lz_1darray_v4f32_f32:
302 case AMDGPU::BI__builtin_amdgcn_image_sample_lz_1darray_v4f16_f32:
303 case AMDGPU::BI__builtin_amdgcn_image_sample_lz_2d_f32_f32:
304 case AMDGPU::BI__builtin_amdgcn_image_sample_lz_2d_v4f32_f32:
305 case AMDGPU::BI__builtin_amdgcn_image_sample_lz_2d_v4f16_f32:
306 case AMDGPU::BI__builtin_amdgcn_image_sample_lz_2darray_f32_f32:
307 case AMDGPU::BI__builtin_amdgcn_image_sample_lz_2darray_v4f32_f32:
308 case AMDGPU::BI__builtin_amdgcn_image_sample_lz_2darray_v4f16_f32:
309 case AMDGPU::BI__builtin_amdgcn_image_sample_lz_3d_v4f32_f32:
310 case AMDGPU::BI__builtin_amdgcn_image_sample_lz_3d_v4f16_f32:
311 case AMDGPU::BI__builtin_amdgcn_image_sample_lz_cube_v4f32_f32:
312 case AMDGPU::BI__builtin_amdgcn_image_sample_lz_cube_v4f16_f32:
313 case AMDGPU::BI__builtin_amdgcn_image_sample_l_1d_v4f32_f32:
314 case AMDGPU::BI__builtin_amdgcn_image_sample_l_1d_v4f16_f32:
315 case AMDGPU::BI__builtin_amdgcn_image_sample_l_1darray_v4f32_f32:
316 case AMDGPU::BI__builtin_amdgcn_image_sample_l_1darray_v4f16_f32:
317 case AMDGPU::BI__builtin_amdgcn_image_sample_l_2d_f32_f32:
318 case AMDGPU::BI__builtin_amdgcn_image_sample_l_2d_v4f16_f32:
319 case AMDGPU::BI__builtin_amdgcn_image_sample_l_2d_v4f32_f32:
320 case AMDGPU::BI__builtin_amdgcn_image_sample_l_2darray_f32_f32:
321 case AMDGPU::BI__builtin_amdgcn_image_sample_l_2darray_v4f32_f32:
322 case AMDGPU::BI__builtin_amdgcn_image_sample_l_2darray_v4f16_f32:
323 case AMDGPU::BI__builtin_amdgcn_image_sample_l_3d_v4f32_f32:
324 case AMDGPU::BI__builtin_amdgcn_image_sample_l_3d_v4f16_f32:
325 case AMDGPU::BI__builtin_amdgcn_image_sample_l_cube_v4f32_f32:
326 case AMDGPU::BI__builtin_amdgcn_image_sample_l_cube_v4f16_f32:
327 case AMDGPU::BI__builtin_amdgcn_image_sample_d_1d_v4f32_f32:
328 case AMDGPU::BI__builtin_amdgcn_image_sample_d_1d_v4f16_f32:
329 case AMDGPU::BI__builtin_amdgcn_image_sample_d_1darray_v4f32_f32:
330 case AMDGPU::BI__builtin_amdgcn_image_sample_d_1darray_v4f16_f32:
331 case AMDGPU::BI__builtin_amdgcn_image_sample_d_2d_f32_f32:
332 case AMDGPU::BI__builtin_amdgcn_image_sample_d_2d_v4f32_f32:
333 case AMDGPU::BI__builtin_amdgcn_image_sample_d_2d_v4f16_f32:
334 case AMDGPU::BI__builtin_amdgcn_image_sample_d_2darray_f32_f32:
335 case AMDGPU::BI__builtin_amdgcn_image_sample_d_2darray_v4f32_f32:
336 case AMDGPU::BI__builtin_amdgcn_image_sample_d_2darray_v4f16_f32:
337 case AMDGPU::BI__builtin_amdgcn_image_sample_d_3d_v4f32_f32:
338 case AMDGPU::BI__builtin_amdgcn_image_sample_d_3d_v4f16_f32:
339 case AMDGPU::BI__builtin_amdgcn_image_gather4_lz_2d_v4f32_f32:
340 case AMDGPU::BI__builtin_amdgcn_image_gather4_lz_2d_v4f16_f32: {
342 unsigned ArgCount = TheCall->
getNumArgs() - 1;
347 constexpr unsigned DMaskArgNo = 0;
348 constexpr int Low = 0;
349 constexpr int High = 15;
350 if (
SemaRef.BuiltinConstantArg(TheCall, DMaskArgNo,
Result) ||
351 SemaRef.BuiltinConstantArgRange(TheCall, DMaskArgNo, Low, High,
357 int NumElementsInRetTy = 1;
359 if (
auto *VTy = dyn_cast<VectorType>(RetTy))
360 NumElementsInRetTy = VTy->getNumElements();
361 int NumActiveBitsInDMask =
363 if (NumActiveBitsInDMask > NumElementsInRetTy) {
365 diag::err_amdgcn_dmask_has_too_many_bits_set);
371 bool ExtraGatherChecks =
373 AMDGPU::BI__builtin_amdgcn_image_gather4_lz_2d_v4f32_f32 ||
375 AMDGPU::BI__builtin_amdgcn_image_gather4_lz_2d_v4f16_f32) &&
376 SemaRef.BuiltinConstantArgPower2(TheCall, 0);
378 return ExtraGatherChecks ||
380 (
SemaRef.BuiltinConstantArg(TheCall, (ArgCount - 1),
Result));
382 case AMDGPU::BI__builtin_amdgcn_image_store_1d_v4f32_i32:
383 case AMDGPU::BI__builtin_amdgcn_image_store_1darray_v4f32_i32:
384 case AMDGPU::BI__builtin_amdgcn_image_store_1d_v4f16_i32:
385 case AMDGPU::BI__builtin_amdgcn_image_store_1darray_v4f16_i32:
386 case AMDGPU::BI__builtin_amdgcn_image_store_2d_f32_i32:
387 case AMDGPU::BI__builtin_amdgcn_image_store_2d_v4f32_i32:
388 case AMDGPU::BI__builtin_amdgcn_image_store_2d_v4f16_i32:
389 case AMDGPU::BI__builtin_amdgcn_image_store_2darray_f32_i32:
390 case AMDGPU::BI__builtin_amdgcn_image_store_2darray_v4f32_i32:
391 case AMDGPU::BI__builtin_amdgcn_image_store_2darray_v4f16_i32:
392 case AMDGPU::BI__builtin_amdgcn_image_store_3d_v4f32_i32:
393 case AMDGPU::BI__builtin_amdgcn_image_store_3d_v4f16_i32:
394 case AMDGPU::BI__builtin_amdgcn_image_store_cube_v4f32_i32:
395 case AMDGPU::BI__builtin_amdgcn_image_store_cube_v4f16_i32:
396 case AMDGPU::BI__builtin_amdgcn_image_store_mip_1d_v4f32_i32:
397 case AMDGPU::BI__builtin_amdgcn_image_store_mip_1d_v4f16_i32:
398 case AMDGPU::BI__builtin_amdgcn_image_store_mip_1darray_v4f32_i32:
399 case AMDGPU::BI__builtin_amdgcn_image_store_mip_1darray_v4f16_i32:
400 case AMDGPU::BI__builtin_amdgcn_image_store_mip_2d_f32_i32:
401 case AMDGPU::BI__builtin_amdgcn_image_store_mip_2d_v4f32_i32:
402 case AMDGPU::BI__builtin_amdgcn_image_store_mip_2d_v4f16_i32:
403 case AMDGPU::BI__builtin_amdgcn_image_store_mip_2darray_f32_i32:
404 case AMDGPU::BI__builtin_amdgcn_image_store_mip_2darray_v4f32_i32:
405 case AMDGPU::BI__builtin_amdgcn_image_store_mip_2darray_v4f16_i32:
406 case AMDGPU::BI__builtin_amdgcn_image_store_mip_3d_v4f32_i32:
407 case AMDGPU::BI__builtin_amdgcn_image_store_mip_3d_v4f16_i32:
408 case AMDGPU::BI__builtin_amdgcn_image_store_mip_cube_v4f32_i32:
409 case AMDGPU::BI__builtin_amdgcn_image_store_mip_cube_v4f16_i32: {
411 unsigned ArgCount = TheCall->
getNumArgs() - 1;
416 constexpr unsigned DMaskArgNo = 1;
417 return SemaRef.BuiltinConstantArgRange(TheCall, DMaskArgNo, 0,
423 case AMDGPU::BI__builtin_amdgcn_wmma_i32_16x16x64_iu8:
424 case AMDGPU::BI__builtin_amdgcn_swmmac_i32_16x16x128_iu8: {
425 if (BuiltinID == AMDGPU::BI__builtin_amdgcn_wmma_i32_16x16x64_iu8) {
426 if (
SemaRef.checkArgCountRange(TheCall, 7, 8))
430 }
else if (BuiltinID ==
431 AMDGPU::BI__builtin_amdgcn_swmmac_i32_16x16x128_iu8) {
432 if (
SemaRef.checkArgCountRange(TheCall, 8, 9))
441 llvm::APSInt ClampValue;
442 if (!
SemaRef.VerifyIntegerConstantExpression(ClampArg, &ClampValue)
451 SemaRef.CheckSingleAssignmentConstraints(BoolTy, ClampExpr);
466 llvm_unreachable(
"Intrinsic requires imm for atomic ordering argument!");
468 llvm::AtomicOrderingCABI(AtomicOrdArgRes.
Val.
getInt().getZExtValue());
472 if (!llvm::isValidAtomicOrderingCABI((
unsigned)Ord) ||
473 (!(MayLoad && MayStore) && (Ord == llvm::AtomicOrderingCABI::acq_rel)) ||
474 (!MayLoad && Ord == llvm::AtomicOrderingCABI::acquire) ||
475 (!MayStore && Ord == llvm::AtomicOrderingCABI::release)) {
476 return Diag(E->
getBeginLoc(), diag::warn_atomic_op_has_invalid_memory_order)
489 if (AS != llvm::AMDGPUAS::FLAT_ADDRESS &&
490 AS != llvm::AMDGPUAS::GLOBAL_ADDRESS) {
492 diag::err_amdgcn_global_or_flat_pointer_required)
499 if (
Scope->isValueDependent())
502 if (std::optional<llvm::APSInt>
Result =
504 if (!ScopeModel->isValid(
Result->getZExtValue())) {
506 diag::err_atomic_op_has_invalid_sync_scope)
507 <<
Scope->getSourceRange();
538 <<
Scope->getSourceRange();
559 unsigned NumDataArgs) {
560 assert(NumDataArgs <= 2);
561 if (
SemaRef.checkArgCountRange(TheCall, NumArgs, NumArgs))
565 for (
unsigned I = 0; I != NumDataArgs; ++I) {
566 Args[I] = TheCall->
getArg(I);
567 ArgTys[I] = Args[I]->
getType();
569 if (!ArgTys[I]->isArithmeticType() || ArgTys[I]->isAnyComplexType()) {
570 SemaRef.Diag(Args[I]->getBeginLoc(),
571 diag::err_typecheck_cond_expect_int_float)
579 if (
getASTContext().hasSameUnqualifiedType(ArgTys[0], ArgTys[1]))
582 if (((ArgTys[0]->isUnsignedIntegerType() &&
583 ArgTys[1]->isSignedIntegerType()) ||
584 (ArgTys[0]->isSignedIntegerType() &&
585 ArgTys[1]->isUnsignedIntegerType())) &&
590 SemaRef.Diag(Args[1]->getBeginLoc(),
591 diag::err_typecheck_call_different_arg_types)
592 << ArgTys[0] << ArgTys[1];
598 const AMDGPUFlatWorkGroupSizeAttr &
Attr) {
612 if (
Min == 0 &&
Max != 0) {
626AMDGPUFlatWorkGroupSizeAttr *
630 AMDGPUFlatWorkGroupSizeAttr TmpAttr(Context, CI, MinExpr, MaxExpr);
634 return ::new (Context)
635 AMDGPUFlatWorkGroupSizeAttr(Context, CI, MinExpr, MaxExpr);
655 const AMDGPUWavesPerEUAttr &
Attr) {
673 if (
Min == 0 &&
Max != 0) {
687AMDGPUWavesPerEUAttr *
691 AMDGPUWavesPerEUAttr TmpAttr(Context, CI, MinExpr, MaxExpr);
696 return ::new (Context) AMDGPUWavesPerEUAttr(Context, CI, MinExpr, MaxExpr);
716 Diag(AL.
getLoc(), diag::warn_amdgpu_num_reg_attr_deprecated) << AL;
720 if (!
SemaRef.checkUInt32Argument(AL, NumSGPRExpr, NumSGPR))
728 Diag(AL.
getLoc(), diag::warn_amdgpu_num_reg_attr_deprecated) << AL;
732 if (!
SemaRef.checkUInt32Argument(AL, NumVGPRExpr, NumVGPR))
742 const AMDGPUMaxNumWorkGroupsAttr &
Attr) {
755 Expr *Exprs[3] = {XExpr, YExpr, ZExpr};
756 for (
int i = 0; i < 3; i++) {
775 AMDGPUMaxNumWorkGroupsAttr TmpAttr(Context, CI, XExpr, YExpr, ZExpr);
776 assert(!
SemaRef.isSFINAEContext() &&
777 "Can't produce SFINAE diagnostic pointing to temporary attribute");
783 return ::new (Context)
784 AMDGPUMaxNumWorkGroupsAttr(Context, CI, XExpr, YExpr, ZExpr);
809 return *ExpandedPredicates
810 .insert(
SemaRef.BuildBoolLiteral(Loc,
false).get())
818 if (BI == AMDGPU::BI__builtin_amdgcn_processor_is) {
821 Diag(Loc, diag::err_amdgcn_processor_is_arg_not_literal);
825 StringRef N = GFX->getString();
827 if (llvm::AMDGPU::parseArchAMDGCN(N) == llvm::AMDGPU::GK_NONE) {
828 Diag(Loc, diag::err_amdgcn_processor_is_arg_invalid_value) << N;
830 llvm::AMDGPU::fillValidArchListAMDGCN(ValidList);
831 if (!ValidList.empty())
832 Diag(Loc, diag::note_amdgcn_processor_is_valid_options)
833 << llvm::join(ValidList,
", ");
838 return *ExpandedPredicates.insert(CE).first;
845 Diag(Loc, diag::err_amdgcn_is_invocable_arg_invalid_value) << Arg;
851 return *ExpandedPredicates.insert(CE).first;
857 llvm::StringMap<bool> CF;
863 return *ExpandedPredicates.insert(
SemaRef.BuildBoolLiteral(Loc, P).get())
868 return ExpandedPredicates.contains(E);
872 PotentiallyUnguardedBuiltinUsers.insert(FD);
876 return PotentiallyUnguardedBuiltinUsers.contains(FD);
896 static Expr *FindPredicate(
Expr *Cond) {
897 if (
auto *CE = dyn_cast<CallExpr>(Cond)) {
898 if (CE->getBuiltinCallee() == AMDGPU::BI__builtin_amdgcn_is_invocable ||
899 CE->getBuiltinCallee() == AMDGPU::BI__builtin_amdgcn_processor_is)
901 }
else if (
auto *UO = dyn_cast<UnaryOperator>(Cond)) {
902 return FindPredicate(UO->getSubExpr());
903 }
else if (
auto *BO = dyn_cast<BinaryOperator>(Cond)) {
904 if ((Cond = FindPredicate(BO->getLHS())))
906 return FindPredicate(BO->getRHS());
911 bool EnterPredicateGuardedContext(CallExpr *P);
912 void ExitPredicateGuardedContext(
bool WasProcessorCheck);
913 bool TraverseGuardedStmt(Stmt *S, CallExpr *P);
916 DiagnoseUnguardedBuiltins(Sema &SemaRef) : SemaRef(SemaRef) {
917 if (
auto *TAT = SemaRef.getCurFunctionDecl(
true)->getAttr<TargetAttr>()) {
920 TAT->getX86AddedFeatures(TargetFeatures);
921 if (auto GFXIP = TAT->getX86Architecture())
922 CurrentGFXIP.emplace_back(TAT->getLocation(), *GFXIP);
926 bool TraverseLambdaExpr(
LambdaExpr *LE)
override {
927 if (SemaRef.AMDGPU().HasPotentiallyUnguardedBuiltinUsage(
928 LE->getCallOperator()))
930 return DynamicRecursiveASTVisitor::TraverseLambdaExpr(LE);
933 bool TraverseStmt(Stmt *S)
override {
939 void IssueDiagnostics(Stmt *S) { TraverseStmt(S); }
941 bool TraverseIfStmt(IfStmt *
If)
override {
942 if (
auto *CE = dyn_cast_or_null<CallExpr>(FindPredicate(
If->getCond())))
943 return TraverseGuardedStmt(
If, CE);
944 return DynamicRecursiveASTVisitor::TraverseIfStmt(
If);
947 bool TraverseCaseStmt(CaseStmt *CS)
override {
948 return TraverseStmt(CS->getSubStmt());
951 bool TraverseConditionalOperator(ConditionalOperator *CO)
override {
952 if (
auto *CE = dyn_cast_or_null<CallExpr>(FindPredicate(CO->getCond())))
953 return TraverseGuardedStmt(CO, CE);
954 return DynamicRecursiveASTVisitor::TraverseConditionalOperator(CO);
957 bool VisitAsmStmt(AsmStmt *ASM)
override;
958 bool VisitCallExpr(CallExpr *CE)
override;
961bool DiagnoseUnguardedBuiltins::EnterPredicateGuardedContext(
CallExpr *P) {
962 bool IsProcessorCheck =
963 P->getBuiltinCallee() == AMDGPU::BI__builtin_amdgcn_processor_is;
965 if (IsProcessorCheck) {
966 StringRef G = cast<clang::StringLiteral>(P->getArg(0))->getString();
968 if (!CurrentGFXIP.empty() && G != CurrentGFXIP.back().second) {
969 SemaRef.Diag(P->getExprLoc(),
970 diag::err_amdgcn_conflicting_is_processor_options)
972 SemaRef.Diag(CurrentGFXIP.back().first,
973 diag::note_amdgcn_previous_is_processor_guard);
975 CurrentGFXIP.emplace_back(P->getExprLoc(), G);
977 auto *FD = cast<FunctionDecl>(
978 cast<DeclRefExpr>(P->getArg(0))->getReferencedDeclOfCallee());
979 GuardedBuiltins.push_back(FD->getBuiltinID());
982 return IsProcessorCheck;
985void DiagnoseUnguardedBuiltins::ExitPredicateGuardedContext(
bool WasProcCheck) {
987 CurrentGFXIP.pop_back();
989 GuardedBuiltins.pop_back();
992inline std::pair<Stmt *, Stmt *> GetTraversalOrder(Stmt *S) {
993 std::pair<Stmt *, Stmt *> Ordered;
996 if (
auto *CO = dyn_cast<ConditionalOperator>(S)) {
998 Ordered = {CO->getTrueExpr(), CO->getFalseExpr()};
999 }
else if (
auto *
If = dyn_cast<IfStmt>(S)) {
1001 Ordered = {
If->getThen(),
If->getElse()};
1004 if (
auto *UO = dyn_cast<UnaryOperator>(
Condition))
1005 if (UO->getOpcode() == UnaryOperatorKind::UO_LNot)
1006 std::swap(Ordered.first, Ordered.second);
1011bool DiagnoseUnguardedBuiltins::TraverseGuardedStmt(Stmt *S, CallExpr *P) {
1012 assert(S &&
"Unexpected missing Statement!");
1013 assert(P &&
"Unexpected missing Predicate!");
1015 auto [Guarded, Unguarded] = GetTraversalOrder(S);
1017 bool WasProcessorCheck = EnterPredicateGuardedContext(P);
1019 bool Continue = TraverseStmt(Guarded);
1021 ExitPredicateGuardedContext(WasProcessorCheck);
1023 return Continue && TraverseStmt(Unguarded);
1026bool DiagnoseUnguardedBuiltins::VisitAsmStmt(AsmStmt *ASM) {
1028 if (!CurrentGFXIP.empty())
1031 std::string S = ASM->generateAsmString(SemaRef.getASTContext());
1032 SemaRef.Diag(ASM->getAsmLoc(), diag::warn_amdgcn_unguarded_asm_stmt) << S;
1033 SemaRef.Diag(ASM->getAsmLoc(), diag::note_amdgcn_unguarded_asm_silence) << S;
1038bool DiagnoseUnguardedBuiltins::VisitCallExpr(CallExpr *CE) {
1039 unsigned ID = CE->getBuiltinCallee();
1040 Builtin::Context &BInfo = SemaRef.getASTContext().BuiltinInfo;
1044 if (!BInfo.isTSBuiltin(ID))
1046 if (ID == AMDGPU::BI__builtin_amdgcn_processor_is ||
1047 ID == AMDGPU::BI__builtin_amdgcn_is_invocable)
1049 if (llvm::find(GuardedBuiltins, ID) != GuardedBuiltins.end())
1052 StringRef FL(BInfo.getRequiredFeatures(ID));
1053 llvm::StringMap<bool> FeatureMap;
1054 if (CurrentGFXIP.empty()) {
1055 for (
auto &&F : TargetFeatures)
1056 FeatureMap[F] =
true;
1057 for (
auto &&GID : GuardedBuiltins)
1058 for (
auto &&F : llvm::split(BInfo.getRequiredFeatures(GID),
','))
1059 FeatureMap[F] =
true;
1061 static const llvm::Triple AMDGCN(llvm::Triple::amdgpu,
1062 llvm::Triple::NoSubArch, llvm::Triple::AMD,
1063 llvm::Triple::AMDHSA);
1064 llvm::AMDGPU::fillAMDGPUFeatureMap(CurrentGFXIP.back().second, AMDGCN,
1068 FunctionDecl *BI = CE->getDirectCallee();
1069 SourceLocation BICallLoc = CE->getExprLoc();
1070 if (Builtin::evaluateRequiredTargetFeatures(FL, FeatureMap)) {
1071 SemaRef.Diag(BICallLoc, diag::warn_amdgcn_unguarded_builtin) << BI;
1072 SemaRef.Diag(BICallLoc, diag::note_amdgcn_unguarded_builtin_silence) << BI;
1074 StringRef GFXIP = CurrentGFXIP.empty() ?
"" : CurrentGFXIP.back().second;
1075 SemaRef.Diag(BICallLoc, diag::err_amdgcn_incompatible_builtin)
1076 << BI << FL << !CurrentGFXIP.empty() << GFXIP;
1077 if (!CurrentGFXIP.empty())
1078 SemaRef.Diag(CurrentGFXIP.back().first,
1079 diag::note_amdgcn_previous_is_processor_guard);
1087 DiagnoseUnguardedBuiltins(
SemaRef).IssueDiagnostics(FD->
getBody());
1099 SemaRef.Diag(Loc, diag::err_amdgpu_target_ext_type_unsupported)
1120 if (R->isInvalidDecl())
1123 if (!Context.getTargetInfo().hasAMDGPUTypes() &&
1124 (!Context.getAuxTargetInfo() ||
1125 !Context.getAuxTargetInfo()->hasAMDGPUTypes()))
1128 bool IsWrapper =
false;
1136 if (R->getNumFields() > 1) {
1137 SemaRef.Diag(NamedBarrField->getLocation(),
1138 diag::err_amdgpu_invalid_field_not_a_wrapper)
1139 << NamedBarrField->getType();
1142 diag::note_amdgpu_not_a_named_barrier_wrapper_too_many_fields)
1143 << R->getDeclName();
1148 DiagWrapperNote = [
this, R, NamedBarrField]() {
1149 SemaRef.Diag(NamedBarrField->getLocation(),
1150 diag::note_amdgpu_named_barrier_reason_field)
1151 << R->getDeclName() << NamedBarrField->getDeclName();
1157 const auto *CxxR = dyn_cast<CXXRecordDecl>(R);
1158 if (CxxR && !IsWrapper) {
1161 if (!
Base || !
Base->hasAttr<AMDGPUNamedBarrierWrapperAttr>())
1165 DiagWrapperNote = [
this, BS, R]() {
1168 SemaRef.Diag(BS.getBeginLoc(),
1169 diag::note_amdgpu_named_barrier_reason_inherited)
1170 << R->getDeclName() << BS.getType();
1180 AMDGPUNamedBarrierWrapperAttr::CreateImplicit(Context,
SourceRange()));
1183 if (CxxR && !CxxR->isCXX11StandardLayout()) {
1184 SemaRef.Diag(R->getLocation(),
1185 diag::err_amdgpu_named_barrier_wrapper_non_standard_layout)
1186 << R->getDeclName();
1187 assert(DiagWrapperNote &&
1188 "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)
bool checkAtomicOrderingCABIArg(Expr *E, bool MayLoad, bool MayStore)
Emits a diagnostic if the E is not an atomic ordering encoded in the C ABI format,...
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)
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.