clang 24.0.0git
SemaAMDGPU.cpp
Go to the documentation of this file.
1//===------ SemaAMDGPU.cpp ------- AMDGPU target-specific routines --------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements semantic analysis functions specific to AMDGPU.
10//
11//===----------------------------------------------------------------------===//
12
14#include "clang/AST/Decl.h"
16#include "clang/AST/Expr.h"
22#include "clang/Sema/Scope.h"
23#include "clang/Sema/Sema.h"
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"
32#include <cstdint>
33#include <utility>
34
35namespace clang {
36
38
40 unsigned BuiltinID,
41 CallExpr *TheCall) {
42 const auto *FD = SemaRef.getCurFunctionDecl(/*AllowLambda=*/true);
43 assert(FD && "AMDGPU builtins should not be used outside of a function");
44 llvm::StringMap<bool> CallerFeatureMap;
45 getASTContext().getFunctionFeatureMap(CallerFeatureMap, FD);
46 bool HasGFX950Insts =
47 Builtin::evaluateRequiredTargetFeatures("gfx950-insts", CallerFeatureMap);
48
49 switch (BuiltinID) {
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;
59 llvm::APSInt Size;
60 Expr *ArgExpr = TheCall->getArg(SizeIdx);
61 // Check for instantiation-dependent expressions (e.g., involving template
62 // parameters). These will be checked again during template instantiation.
63 if (ArgExpr->isInstantiationDependent())
64 return false;
65 [[maybe_unused]] ExprResult R =
66 SemaRef.VerifyIntegerConstantExpression(ArgExpr, &Size);
67 assert(!R.isInvalid());
68 switch (Size.getSExtValue()) {
69 case 1:
70 case 2:
71 case 4:
72 return false;
73 case 12:
74 case 16: {
75 if (HasGFX950Insts)
76 return false;
77 [[fallthrough]];
78 }
79 default:
80 SemaRef.targetDiag(ArgExpr->getExprLoc(),
81 diag::err_amdgcn_load_lds_size_invalid_value)
82 << ArgExpr->getSourceRange();
83 SemaRef.targetDiag(ArgExpr->getExprLoc(),
84 diag::note_amdgcn_load_lds_size_valid_value)
85 << HasGFX950Insts << ArgExpr->getSourceRange();
86 return true;
87 }
88 }
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: {
95 // These builtins are deprecated in favor of
96 // __builtin_amdgcn_ballot_{w32|w64}. Suggest the replacement matching the
97 // wavefront size of the calling function.
98 bool IsWave32 = Builtin::evaluateRequiredTargetFeatures("wavefrontsize32",
99 CallerFeatureMap);
100 Diag(TheCall->getBeginLoc(), diag::warn_deprecated_builtin)
102 << (IsWave32 ? "__builtin_amdgcn_ballot_w32"
103 : "__builtin_amdgcn_ballot_w64");
104 return false;
105 }
106 case AMDGPU::BI__builtin_amdgcn_get_fpenv:
107 case AMDGPU::BI__builtin_amdgcn_set_fpenv:
108 return false;
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);
119
120 // Checks requiring constant evaluation are deferred until instantiation.
121 if (OrderExpr->isInstantiationDependent() ||
122 ScopeExpr->isInstantiationDependent())
123 return false;
124
125 Expr::EvalResult OrderResult;
126 if (!OrderExpr->EvaluateAsInt(OrderResult, getASTContext()))
127 return Diag(OrderExpr->getExprLoc(), diag::err_typecheck_expect_int)
128 << OrderExpr->getType();
129 uint64_t Ord = OrderResult.Val.getInt().getZExtValue();
130
131 // Check validity of memory ordering as per C11 / C++11's memory model.
132 // Only fence needs check. Atomic dec/inc allow all memory orders.
133 if (!llvm::isValidAtomicOrderingCABI(Ord))
134 return Diag(OrderExpr->getBeginLoc(),
135 diag::warn_atomic_op_has_invalid_memory_order)
136 << 0 << OrderExpr->getSourceRange();
137 switch (static_cast<llvm::AtomicOrderingCABI>(Ord)) {
138 case llvm::AtomicOrderingCABI::relaxed:
139 case llvm::AtomicOrderingCABI::consume:
140 if (IsFence)
141 return Diag(OrderExpr->getBeginLoc(),
142 diag::warn_atomic_op_has_invalid_memory_order)
143 << 0 << OrderExpr->getSourceRange();
144 break;
145 case llvm::AtomicOrderingCABI::acquire:
146 case llvm::AtomicOrderingCABI::release:
147 case llvm::AtomicOrderingCABI::acq_rel:
148 case llvm::AtomicOrderingCABI::seq_cst:
149 break;
150 }
151
152 Expr::EvalResult ScopeResult;
153 // Check that sync scope is a constant literal
154 if (!ScopeExpr->EvaluateAsConstantExpr(ScopeResult, getASTContext()))
155 return Diag(ScopeExpr->getExprLoc(), diag::err_expr_not_string_literal)
156 << ScopeExpr->getType();
157
158 // Reject any string that is not a valid synchronization scope name.
159 std::optional<std::string> ScopeName =
160 ScopeExpr->tryEvaluateString(getASTContext());
161 const llvm::Triple &TT = TI.getTriple();
162 if (ScopeName && !llvm::parseAtomicScopeIRString(TT, *ScopeName)) {
163 return Diag(ScopeExpr->getExprLoc(), diag::err_invalid_sync_scope)
164 << *ScopeName << ScopeExpr->getSourceRange();
165 }
166
167 return false;
168 }
169 case AMDGPU::BI__builtin_amdgcn_flat_prefetch:
170 case AMDGPU::BI__builtin_amdgcn_global_prefetch: {
171 // Only th (bits 0-2, not 7), scope (bits 3-4) and nv (bit 5) may be set.
172 llvm::APSInt CPol;
173 if (SemaRef.BuiltinConstantArg(TheCall, /*ArgNum=*/1, CPol))
174 return true;
175 if ((CPol.getZExtValue() & 0x7) != 0x7)
176 return false;
177 Expr *ArgExpr = TheCall->getArg(1);
178 SemaRef.targetDiag(ArgExpr->getExprLoc(),
179 diag::err_amdgcn_prefetch_reserved_th)
180 << ArgExpr->getSourceRange();
181 return true;
182 }
183 case AMDGPU::BI__builtin_amdgcn_s_wait_event: {
184 llvm::APSInt Result;
185 if (SemaRef.BuiltinConstantArg(TheCall, 0, Result))
186 return true;
187
189 "gfx12-insts", CallerFeatureMap);
190
191 // gfx11 -> gfx12 changed the interpretation of the bitmask. gfx12 inverted
192 // the intepretation for export_ready, but shifted the used bit by 1. Thus
193 // waiting for the export_ready event can use a value of 2 universally.
194 if (((IsGFX12Plus && !Result[1]) || (!IsGFX12Plus && Result[0])) ||
195 Result.getZExtValue() > 2) {
196 Expr *ArgExpr = TheCall->getArg(0);
197 SemaRef.targetDiag(ArgExpr->getExprLoc(),
198 diag::warn_amdgpu_s_wait_event_mask_no_effect_target)
199 << ArgExpr->getSourceRange();
200 SemaRef.targetDiag(ArgExpr->getExprLoc(),
201 diag::note_amdgpu_s_wait_event_suggested_value)
202 << ArgExpr->getSourceRange();
203 }
204
205 return false;
206 }
207 case AMDGPU::BI__builtin_amdgcn_mov_dpp:
208 return checkMovDPPFunctionCall(TheCall, 5, 1);
209 case AMDGPU::BI__builtin_amdgcn_mov_dpp8:
210 return checkMovDPPFunctionCall(TheCall, 2, 1);
211 case AMDGPU::BI__builtin_amdgcn_update_dpp:
212 return checkMovDPPFunctionCall(TheCall, 6, 2);
213 case AMDGPU::BI__builtin_amdgcn_asyncmark:
214 case AMDGPU::BI__builtin_amdgcn_wait_asyncmark: {
215 bool IsMark = BuiltinID == AMDGPU::BI__builtin_amdgcn_asyncmark;
216
217 // Check the sequence length limit is a constant.
218 llvm::APSInt NumMarks;
219 if (!IsMark && SemaRef.BuiltinConstantArg(TheCall, 0, NumMarks))
220 return true;
221
222 // The stage mask names the stages to act on, so every combination of known
223 // stage bits is meaningful, including none of them: the zero mask names
224 // every stage.
225 unsigned MaskArgNum = IsMark ? 0 : 1;
226 return SemaRef.BuiltinConstantArgRange(
227 TheCall, MaskArgNum, 0,
228 llvm::to_underlying(llvm::AMDGPU::AsyncStages::ALL));
229 }
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:
235 return checkAVLoadStore(TheCall, /*IsStore=*/false);
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:
241 return checkAVLoadStore(TheCall, /*IsStore=*/true);
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:
245 return checkCoopAtomicFunctionCall(TheCall, /*IsStore=*/false);
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:
249 return checkCoopAtomicFunctionCall(TheCall, /*IsStore=*/true);
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:
256 return checkAtomicMonitorLoad(TheCall);
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: {
341
342 unsigned ArgCount = TheCall->getNumArgs() - 1;
343 llvm::APSInt Result;
344
345 // Compilain about dmask values which are too huge to fully fit into 4 bits
346 // (which is the actual size of the dmask in corresponding HW instructions).
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,
352 /* RangeIsError = */ true))
353 return true;
354
355 // Dmask indicates which elements should be returned and it is not possible
356 // to return more values than there are elements in return type.
357 int NumElementsInRetTy = 1;
358 const Type *RetTy = TheCall->getType().getTypePtr();
359 if (auto *VTy = dyn_cast<VectorType>(RetTy))
360 NumElementsInRetTy = VTy->getNumElements();
361 int NumActiveBitsInDMask =
362 llvm::popcount(static_cast<uint8_t>(Result.getExtValue()));
363 if (NumActiveBitsInDMask > NumElementsInRetTy) {
364 Diag(TheCall->getBeginLoc(),
365 diag::err_amdgcn_dmask_has_too_many_bits_set);
366 return true;
367 }
368
369 // For gather, only one bit can be set indicating which exact component to
370 // return.
371 bool ExtraGatherChecks =
372 (BuiltinID ==
373 AMDGPU::BI__builtin_amdgcn_image_gather4_lz_2d_v4f32_f32 ||
374 BuiltinID ==
375 AMDGPU::BI__builtin_amdgcn_image_gather4_lz_2d_v4f16_f32) &&
376 SemaRef.BuiltinConstantArgPower2(TheCall, 0);
377
378 return ExtraGatherChecks ||
379 (SemaRef.BuiltinConstantArg(TheCall, ArgCount, Result)) ||
380 (SemaRef.BuiltinConstantArg(TheCall, (ArgCount - 1), Result));
381 }
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: {
410
411 unsigned ArgCount = TheCall->getNumArgs() - 1;
412 llvm::APSInt Result;
413
414 // Complain about dmask values which are too huge to fully fit into 4 bits
415 // (which is the actual size of the dmask in corresponding HW instructions).
416 constexpr unsigned DMaskArgNo = 1;
417 return SemaRef.BuiltinConstantArgRange(TheCall, DMaskArgNo, /*Low=*/0,
418 /*High=*/15,
419 /*RangeIsError=*/true) ||
420 SemaRef.BuiltinConstantArg(TheCall, ArgCount, Result) ||
421 SemaRef.BuiltinConstantArg(TheCall, (ArgCount - 1), Result);
422 }
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))
427 return true;
428 if (TheCall->getNumArgs() == 7)
429 return false;
430 } else if (BuiltinID ==
431 AMDGPU::BI__builtin_amdgcn_swmmac_i32_16x16x128_iu8) {
432 if (SemaRef.checkArgCountRange(TheCall, 8, 9))
433 return true;
434 if (TheCall->getNumArgs() == 8)
435 return false;
436 }
437 // Check if the last argument (clamp operand) is a constant and is
438 // convertible to bool.
439 Expr *ClampArg = TheCall->getArg(TheCall->getNumArgs() - 1);
440 // 1) Ensure clamp argument is a constant expression
441 llvm::APSInt ClampValue;
442 if (!SemaRef.VerifyIntegerConstantExpression(ClampArg, &ClampValue)
443 .isUsable())
444 return true;
445 // 2) Check if the argument can be converted to bool type
446 if (!SemaRef.Context.hasSameType(ClampArg->getType(),
447 SemaRef.Context.BoolTy)) {
448 // Try to convert to bool
449 QualType BoolTy = SemaRef.Context.BoolTy;
450 ExprResult ClampExpr(ClampArg);
451 SemaRef.CheckSingleAssignmentConstraints(BoolTy, ClampExpr);
452 if (ClampExpr.isInvalid())
453 return true;
454 }
455 return false;
456 }
457 default:
458 return false;
459 }
460}
461
463 bool MayStore) {
464 Expr::EvalResult AtomicOrdArgRes;
465 if (!E->EvaluateAsInt(AtomicOrdArgRes, getASTContext()))
466 llvm_unreachable("Intrinsic requires imm for atomic ordering argument!");
467 auto Ord =
468 llvm::AtomicOrderingCABI(AtomicOrdArgRes.Val.getInt().getZExtValue());
469
470 // Atomic ordering cannot be acq_rel in any case, acquire for stores or
471 // release for loads.
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)
477 << 0 << E->getSourceRange();
478 }
479
480 return false;
481}
482
483// Check that the first argument to TheCall is a global or generic pointer.
485 Expr *PtrArg = TheCall->getArg(0);
486 QualType PtrTy = PtrArg->getType()->getPointeeType();
487 unsigned AS =
488 S.getASTContext().getTargetAddressSpace(PtrTy.getAddressSpace());
489 if (AS != llvm::AMDGPUAS::FLAT_ADDRESS &&
490 AS != llvm::AMDGPUAS::GLOBAL_ADDRESS) {
491 return S.Diag(TheCall->getBeginLoc(),
492 diag::err_amdgcn_global_or_flat_pointer_required)
493 << PtrArg->getSourceRange();
494 }
495 return false;
496}
497
499 if (Scope->isValueDependent())
500 return false;
502 if (std::optional<llvm::APSInt> Result =
503 Scope->getIntegerConstantExpr(S.SemaRef.Context)) {
504 if (!ScopeModel->isValid(Result->getZExtValue())) {
505 return S.Diag(Scope->getBeginLoc(),
506 diag::err_atomic_op_has_invalid_sync_scope)
507 << Scope->getSourceRange();
508 }
509 }
510 return false;
511}
512
513bool SemaAMDGPU::checkAVLoadStore(CallExpr *TheCall, bool IsStore) {
514 if (checkGlobalOrFlatPointerArg(*this, TheCall))
515 return true;
516
517 Expr *Scope = TheCall->getArg(TheCall->getNumArgs() - 1);
518 return checkScopeAsInt(*this, Scope);
519}
520
522 bool Fail = checkGlobalOrFlatPointerArg(*this, TheCall);
523
524 Expr *AO = TheCall->getArg(IsStore ? 2 : 1);
525 Expr *Scope = TheCall->getArg(TheCall->getNumArgs() - 1);
526
527 if (AO->isValueDependent() || Scope->isValueDependent())
528 return false;
529
530 // Check atomic ordering
531 Fail |=
532 checkAtomicOrderingCABIArg(TheCall->getArg(IsStore ? 2 : 1),
533 /*MayLoad=*/!IsStore, /*MayStore=*/IsStore);
534
535 // Last argument is the syncscope as a string literal.
536 if (!isa<StringLiteral>(Scope->IgnoreParenImpCasts())) {
537 Diag(TheCall->getBeginLoc(), diag::err_expr_not_string_literal)
538 << Scope->getSourceRange();
539 Fail = true;
540 }
541
542 return Fail;
543}
544
546 Expr *AO = TheCall->getArg(1);
547 Expr *Scope = TheCall->getArg(TheCall->getNumArgs() - 1);
548
549 if (AO->isValueDependent() || Scope->isValueDependent())
550 return false;
551
552 bool Fail = checkAtomicOrderingCABIArg(AO, /*MayLoad=*/true,
553 /*MayStore=*/false);
554 Fail |= checkScopeAsInt(*this, Scope);
555 return Fail;
556}
557
558bool SemaAMDGPU::checkMovDPPFunctionCall(CallExpr *TheCall, unsigned NumArgs,
559 unsigned NumDataArgs) {
560 assert(NumDataArgs <= 2);
561 if (SemaRef.checkArgCountRange(TheCall, NumArgs, NumArgs))
562 return true;
563 Expr *Args[2];
564 QualType ArgTys[2];
565 for (unsigned I = 0; I != NumDataArgs; ++I) {
566 Args[I] = TheCall->getArg(I);
567 ArgTys[I] = Args[I]->getType();
568 // TODO: Vectors can also be supported.
569 if (!ArgTys[I]->isArithmeticType() || ArgTys[I]->isAnyComplexType()) {
570 SemaRef.Diag(Args[I]->getBeginLoc(),
571 diag::err_typecheck_cond_expect_int_float)
572 << ArgTys[I] << Args[I]->getSourceRange();
573 return true;
574 }
575 }
576 if (NumDataArgs < 2)
577 return false;
578
579 if (getASTContext().hasSameUnqualifiedType(ArgTys[0], ArgTys[1]))
580 return false;
581
582 if (((ArgTys[0]->isUnsignedIntegerType() &&
583 ArgTys[1]->isSignedIntegerType()) ||
584 (ArgTys[0]->isSignedIntegerType() &&
585 ArgTys[1]->isUnsignedIntegerType())) &&
586 getASTContext().getTypeSize(ArgTys[0]) ==
587 getASTContext().getTypeSize(ArgTys[1]))
588 return false;
589
590 SemaRef.Diag(Args[1]->getBeginLoc(),
591 diag::err_typecheck_call_different_arg_types)
592 << ArgTys[0] << ArgTys[1];
593 return true;
594}
595
596static bool
598 const AMDGPUFlatWorkGroupSizeAttr &Attr) {
599 // Accept template arguments for now as they depend on something else.
600 // We'll get to check them when they eventually get instantiated.
601 if (MinExpr->isValueDependent() || MaxExpr->isValueDependent())
602 return false;
603
604 uint32_t Min = 0;
605 if (!S.checkUInt32Argument(Attr, MinExpr, Min, 0))
606 return true;
607
608 uint32_t Max = 0;
609 if (!S.checkUInt32Argument(Attr, MaxExpr, Max, 1))
610 return true;
611
612 if (Min == 0 && Max != 0) {
613 S.Diag(Attr.getLocation(), diag::err_attribute_argument_invalid)
614 << &Attr << 0;
615 return true;
616 }
617 if (Min > Max) {
618 S.Diag(Attr.getLocation(), diag::err_attribute_argument_invalid)
619 << &Attr << 1;
620 return true;
621 }
622
623 return false;
624}
625
626AMDGPUFlatWorkGroupSizeAttr *
628 Expr *MinExpr, Expr *MaxExpr) {
629 ASTContext &Context = getASTContext();
630 AMDGPUFlatWorkGroupSizeAttr TmpAttr(Context, CI, MinExpr, MaxExpr);
631
632 if (checkAMDGPUFlatWorkGroupSizeArguments(SemaRef, MinExpr, MaxExpr, TmpAttr))
633 return nullptr;
634 return ::new (Context)
635 AMDGPUFlatWorkGroupSizeAttr(Context, CI, MinExpr, MaxExpr);
636}
637
639 const AttributeCommonInfo &CI,
640 Expr *MinExpr, Expr *MaxExpr) {
641 if (auto *Attr = CreateAMDGPUFlatWorkGroupSizeAttr(CI, MinExpr, MaxExpr))
642 D->addAttr(Attr);
643}
644
646 const ParsedAttr &AL) {
647 Expr *MinExpr = AL.getArgAsExpr(0);
648 Expr *MaxExpr = AL.getArgAsExpr(1);
649
650 addAMDGPUFlatWorkGroupSizeAttr(D, AL, MinExpr, MaxExpr);
651}
652
653static bool checkAMDGPUWavesPerEUArguments(Sema &S, Expr *MinExpr,
654 Expr *MaxExpr,
655 const AMDGPUWavesPerEUAttr &Attr) {
656 if (S.DiagnoseUnexpandedParameterPack(MinExpr) ||
657 (MaxExpr && S.DiagnoseUnexpandedParameterPack(MaxExpr)))
658 return true;
659
660 // Accept template arguments for now as they depend on something else.
661 // We'll get to check them when they eventually get instantiated.
662 if (MinExpr->isValueDependent() || (MaxExpr && MaxExpr->isValueDependent()))
663 return false;
664
665 uint32_t Min = 0;
666 if (!S.checkUInt32Argument(Attr, MinExpr, Min, 0))
667 return true;
668
669 uint32_t Max = 0;
670 if (MaxExpr && !S.checkUInt32Argument(Attr, MaxExpr, Max, 1))
671 return true;
672
673 if (Min == 0 && Max != 0) {
674 S.Diag(Attr.getLocation(), diag::err_attribute_argument_invalid)
675 << &Attr << 0;
676 return true;
677 }
678 if (Max != 0 && Min > Max) {
679 S.Diag(Attr.getLocation(), diag::err_attribute_argument_invalid)
680 << &Attr << 1;
681 return true;
682 }
683
684 return false;
685}
686
687AMDGPUWavesPerEUAttr *
689 Expr *MinExpr, Expr *MaxExpr) {
690 ASTContext &Context = getASTContext();
691 AMDGPUWavesPerEUAttr TmpAttr(Context, CI, MinExpr, MaxExpr);
692
693 if (checkAMDGPUWavesPerEUArguments(SemaRef, MinExpr, MaxExpr, TmpAttr))
694 return nullptr;
695
696 return ::new (Context) AMDGPUWavesPerEUAttr(Context, CI, MinExpr, MaxExpr);
697}
698
700 Expr *MinExpr, Expr *MaxExpr) {
701 if (auto *Attr = CreateAMDGPUWavesPerEUAttr(CI, MinExpr, MaxExpr))
702 D->addAttr(Attr);
703}
704
707 return;
708
709 Expr *MinExpr = AL.getArgAsExpr(0);
710 Expr *MaxExpr = (AL.getNumArgs() > 1) ? AL.getArgAsExpr(1) : nullptr;
711
712 addAMDGPUWavesPerEUAttr(D, AL, MinExpr, MaxExpr);
713}
714
716 Diag(AL.getLoc(), diag::warn_amdgpu_num_reg_attr_deprecated) << AL;
717
718 uint32_t NumSGPR = 0;
719 Expr *NumSGPRExpr = AL.getArgAsExpr(0);
720 if (!SemaRef.checkUInt32Argument(AL, NumSGPRExpr, NumSGPR))
721 return;
722
723 D->addAttr(::new (getASTContext())
724 AMDGPUNumSGPRAttr(getASTContext(), AL, NumSGPR));
725}
726
728 Diag(AL.getLoc(), diag::warn_amdgpu_num_reg_attr_deprecated) << AL;
729
730 uint32_t NumVGPR = 0;
731 Expr *NumVGPRExpr = AL.getArgAsExpr(0);
732 if (!SemaRef.checkUInt32Argument(AL, NumVGPRExpr, NumVGPR))
733 return;
734
735 D->addAttr(::new (getASTContext())
736 AMDGPUNumVGPRAttr(getASTContext(), AL, NumVGPR));
737}
738
739static bool
741 Expr *ZExpr,
742 const AMDGPUMaxNumWorkGroupsAttr &Attr) {
743 if (S.DiagnoseUnexpandedParameterPack(XExpr) ||
744 (YExpr && S.DiagnoseUnexpandedParameterPack(YExpr)) ||
745 (ZExpr && S.DiagnoseUnexpandedParameterPack(ZExpr)))
746 return true;
747
748 // Accept template arguments for now as they depend on something else.
749 // We'll get to check them when they eventually get instantiated.
750 if (XExpr->isValueDependent() || (YExpr && YExpr->isValueDependent()) ||
751 (ZExpr && ZExpr->isValueDependent()))
752 return false;
753
754 uint32_t NumWG = 0;
755 Expr *Exprs[3] = {XExpr, YExpr, ZExpr};
756 for (int i = 0; i < 3; i++) {
757 if (Exprs[i]) {
758 if (!S.checkUInt32Argument(Attr, Exprs[i], NumWG, i,
759 /*StrictlyUnsigned=*/true))
760 return true;
761 if (NumWG == 0) {
762 S.Diag(Attr.getLoc(), diag::err_attribute_argument_is_zero)
763 << &Attr << Exprs[i]->getSourceRange();
764 return true;
765 }
766 }
767 }
768
769 return false;
770}
771
773 const AttributeCommonInfo &CI, Expr *XExpr, Expr *YExpr, Expr *ZExpr) {
774 ASTContext &Context = getASTContext();
775 AMDGPUMaxNumWorkGroupsAttr TmpAttr(Context, CI, XExpr, YExpr, ZExpr);
776 assert(!SemaRef.isSFINAEContext() &&
777 "Can't produce SFINAE diagnostic pointing to temporary attribute");
778
779 if (checkAMDGPUMaxNumWorkGroupsArguments(SemaRef, XExpr, YExpr, ZExpr,
780 TmpAttr))
781 return nullptr;
782
783 return ::new (Context)
784 AMDGPUMaxNumWorkGroupsAttr(Context, CI, XExpr, YExpr, ZExpr);
785}
786
788 const AttributeCommonInfo &CI,
789 Expr *XExpr, Expr *YExpr,
790 Expr *ZExpr) {
791 if (auto *Attr = CreateAMDGPUMaxNumWorkGroupsAttr(CI, XExpr, YExpr, ZExpr))
792 D->addAttr(Attr);
793}
794
796 const ParsedAttr &AL) {
797 Expr *YExpr = (AL.getNumArgs() > 1) ? AL.getArgAsExpr(1) : nullptr;
798 Expr *ZExpr = (AL.getNumArgs() > 2) ? AL.getArgAsExpr(2) : nullptr;
799 addAMDGPUMaxNumWorkGroupsAttr(D, AL, AL.getArgAsExpr(0), YExpr, ZExpr);
800}
801
804 ASTContext &Ctx = getASTContext();
805 QualType BoolTy = Ctx.getLogicalOperationType();
806 SourceLocation Loc = CE->getExprLoc();
807
808 if (!CE->getBuiltinCallee())
809 return *ExpandedPredicates
810 .insert(SemaRef.BuildBoolLiteral(Loc, false).get())
811 .first;
812
813 bool P = false;
814 unsigned BI = CE->getBuiltinCallee();
815 if (Ctx.BuiltinInfo.isAuxBuiltinID(BI))
816 BI = Ctx.BuiltinInfo.getAuxBuiltinID(BI);
817
818 if (BI == AMDGPU::BI__builtin_amdgcn_processor_is) {
819 auto *GFX = dyn_cast<StringLiteral>(CE->getArg(0)->IgnoreParenCasts());
820 if (!GFX) {
821 Diag(Loc, diag::err_amdgcn_processor_is_arg_not_literal);
822 return nullptr;
823 }
824
825 StringRef N = GFX->getString();
826 const TargetInfo &TI = Ctx.getTargetInfo();
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, ", ");
834 return nullptr;
835 }
836 if (TI.getTriple().isSPIRV()) {
837 CE->setType(BoolTy);
838 return *ExpandedPredicates.insert(CE).first;
839 }
840
841 P = TI.isProcessorName(N);
842 } else {
843 Expr *Arg = CE->getArg(0);
844 if (!Arg || Arg->getType() != Ctx.BuiltinFnTy) {
845 Diag(Loc, diag::err_amdgcn_is_invocable_arg_invalid_value) << Arg;
846 return nullptr;
847 }
848
849 if (Ctx.getTargetInfo().getTriple().isSPIRV()) {
850 CE->setType(BoolTy);
851 return *ExpandedPredicates.insert(CE).first;
852 }
853
855
856 StringRef RF = Ctx.BuiltinInfo.getRequiredFeatures(FD->getBuiltinID());
857 llvm::StringMap<bool> CF;
858 Ctx.getFunctionFeatureMap(CF, FD);
859
861 }
862
863 return *ExpandedPredicates.insert(SemaRef.BuildBoolLiteral(Loc, P).get())
864 .first;
865}
866
868 return ExpandedPredicates.contains(E);
869}
870
872 PotentiallyUnguardedBuiltinUsers.insert(FD);
873}
874
876 return PotentiallyUnguardedBuiltinUsers.contains(FD);
877}
878
879namespace {
880/// This class implements -Wamdgpu-unguarded-builtin-usage.
881///
882/// This is done with a traversal of the AST of a function that includes a
883/// call to a target specific builtin. Whenever we encounter an \c if of the
884/// form: \c if(__builtin_amdgcn_is_invocable), we consider the then statement
885/// guarded.
886class DiagnoseUnguardedBuiltins : public DynamicRecursiveASTVisitor {
887 // TODO: this could eventually be extended to consider what happens when there
888 // are multiple target architectures specified via target("arch=gfxXXX")
889 // target("arch=gfxyyy") etc., as well as feature disabling via "-XXX".
890 Sema &SemaRef;
891
892 SmallVector<StringRef> TargetFeatures;
894 SmallVector<unsigned> GuardedBuiltins;
895
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)
900 return Cond;
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())))
905 return Cond;
906 return FindPredicate(BO->getRHS());
907 }
908 return nullptr;
909 }
910
911 bool EnterPredicateGuardedContext(CallExpr *P);
912 void ExitPredicateGuardedContext(bool WasProcessorCheck);
913 bool TraverseGuardedStmt(Stmt *S, CallExpr *P);
914
915public:
916 DiagnoseUnguardedBuiltins(Sema &SemaRef) : SemaRef(SemaRef) {
917 if (auto *TAT = SemaRef.getCurFunctionDecl(true)->getAttr<TargetAttr>()) {
918 // We use the somewhat misnamed x86 accessors because they provide exactly
919 // what we require.
920 TAT->getX86AddedFeatures(TargetFeatures);
921 if (auto GFXIP = TAT->getX86Architecture())
922 CurrentGFXIP.emplace_back(TAT->getLocation(), *GFXIP);
923 }
924 }
925
926 bool TraverseLambdaExpr(LambdaExpr *LE) override {
927 if (SemaRef.AMDGPU().HasPotentiallyUnguardedBuiltinUsage(
928 LE->getCallOperator()))
929 return true; // We have already handled this.
930 return DynamicRecursiveASTVisitor::TraverseLambdaExpr(LE);
931 }
932
933 bool TraverseStmt(Stmt *S) override {
934 if (!S)
935 return true;
937 }
938
939 void IssueDiagnostics(Stmt *S) { TraverseStmt(S); }
940
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);
945 }
946
947 bool TraverseCaseStmt(CaseStmt *CS) override {
948 return TraverseStmt(CS->getSubStmt());
949 }
950
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);
955 }
956
957 bool VisitAsmStmt(AsmStmt *ASM) override;
958 bool VisitCallExpr(CallExpr *CE) override;
959};
960
961bool DiagnoseUnguardedBuiltins::EnterPredicateGuardedContext(CallExpr *P) {
962 bool IsProcessorCheck =
963 P->getBuiltinCallee() == AMDGPU::BI__builtin_amdgcn_processor_is;
964
965 if (IsProcessorCheck) {
966 StringRef G = cast<clang::StringLiteral>(P->getArg(0))->getString();
967 // TODO: handle generic ISAs.
968 if (!CurrentGFXIP.empty() && G != CurrentGFXIP.back().second) {
969 SemaRef.Diag(P->getExprLoc(),
970 diag::err_amdgcn_conflicting_is_processor_options)
971 << P;
972 SemaRef.Diag(CurrentGFXIP.back().first,
973 diag::note_amdgcn_previous_is_processor_guard);
974 }
975 CurrentGFXIP.emplace_back(P->getExprLoc(), G);
976 } else {
977 auto *FD = cast<FunctionDecl>(
978 cast<DeclRefExpr>(P->getArg(0))->getReferencedDeclOfCallee());
979 GuardedBuiltins.push_back(FD->getBuiltinID());
980 }
981
982 return IsProcessorCheck;
983}
984
985void DiagnoseUnguardedBuiltins::ExitPredicateGuardedContext(bool WasProcCheck) {
986 if (WasProcCheck)
987 CurrentGFXIP.pop_back();
988 else
989 GuardedBuiltins.pop_back();
990}
991
992inline std::pair<Stmt *, Stmt *> GetTraversalOrder(Stmt *S) {
993 std::pair<Stmt *, Stmt *> Ordered;
994 Expr *Condition = nullptr;
995
996 if (auto *CO = dyn_cast<ConditionalOperator>(S)) {
997 Condition = CO->getCond();
998 Ordered = {CO->getTrueExpr(), CO->getFalseExpr()};
999 } else if (auto *If = dyn_cast<IfStmt>(S)) {
1000 Condition = If->getCond();
1001 Ordered = {If->getThen(), If->getElse()};
1002 }
1003
1004 if (auto *UO = dyn_cast<UnaryOperator>(Condition))
1005 if (UO->getOpcode() == UnaryOperatorKind::UO_LNot)
1006 std::swap(Ordered.first, Ordered.second);
1007
1008 return Ordered;
1009}
1010
1011bool DiagnoseUnguardedBuiltins::TraverseGuardedStmt(Stmt *S, CallExpr *P) {
1012 assert(S && "Unexpected missing Statement!");
1013 assert(P && "Unexpected missing Predicate!");
1014
1015 auto [Guarded, Unguarded] = GetTraversalOrder(S);
1016
1017 bool WasProcessorCheck = EnterPredicateGuardedContext(P);
1018
1019 bool Continue = TraverseStmt(Guarded);
1020
1021 ExitPredicateGuardedContext(WasProcessorCheck);
1022
1023 return Continue && TraverseStmt(Unguarded);
1024}
1025
1026bool DiagnoseUnguardedBuiltins::VisitAsmStmt(AsmStmt *ASM) {
1027 // TODO: should we check if the ASM is valid for the target? Can we?
1028 if (!CurrentGFXIP.empty())
1029 return true;
1030
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;
1034
1035 return true;
1036}
1037
1038bool DiagnoseUnguardedBuiltins::VisitCallExpr(CallExpr *CE) {
1039 unsigned ID = CE->getBuiltinCallee();
1040 Builtin::Context &BInfo = SemaRef.getASTContext().BuiltinInfo;
1041
1042 if (!ID)
1043 return true;
1044 if (!BInfo.isTSBuiltin(ID))
1045 return true;
1046 if (ID == AMDGPU::BI__builtin_amdgcn_processor_is ||
1047 ID == AMDGPU::BI__builtin_amdgcn_is_invocable)
1048 return true;
1049 if (llvm::find(GuardedBuiltins, ID) != GuardedBuiltins.end())
1050 return true;
1051
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;
1060 } else {
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,
1065 FeatureMap);
1066 }
1067
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;
1073 } else {
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);
1080 }
1081
1082 return true;
1083}
1084} // Unnamed namespace
1085
1087 DiagnoseUnguardedBuiltins(SemaRef).IssueDiagnostics(FD->getBody());
1088}
1089
1091 ASTContext &Ctx = getASTContext();
1092 llvm::Triple TT = Ctx.getTargetInfo().getTriple();
1093 const Type *BaseTy = Ty->getPointeeOrArrayElementType();
1094
1095 if (Ctx.getTargetInfo().getTriple().isSPIRV()) {
1096 // The AMDGPU named barrier type requires special handling in the back-end
1097 // and is not supported for SPIR-V
1098 if (BaseTy->isAMDGPUNamedBarrierType()) {
1099 SemaRef.Diag(Loc, diag::err_amdgpu_target_ext_type_unsupported)
1100 << Ty << TT.str();
1101 return false;
1102 }
1103 }
1104
1105 return true;
1106}
1107
1109 for (FieldDecl *FD : R->fields()) {
1110 QualType FDTy = FD->getType();
1112 return FD;
1113 }
1114
1115 return nullptr;
1116}
1117
1119 ASTContext &Context = getASTContext();
1120 if (R->isInvalidDecl())
1121 return;
1122
1123 if (!Context.getTargetInfo().hasAMDGPUTypes() &&
1124 (!Context.getAuxTargetInfo() ||
1125 !Context.getAuxTargetInfo()->hasAMDGPUTypes()))
1126 return;
1127
1128 bool IsWrapper = false;
1129 std::function<void()> DiagWrapperNote;
1130
1131 // First, check if this is a named barrier wrapper by virtue of the class
1132 // declaring a named barrier field. This covers both C and C++.
1133 if (FieldDecl *NamedBarrField = getNamedBarrierField(R)) {
1134 // If this record contains a named barrier field, it must have only one
1135 // field.
1136 if (R->getNumFields() > 1) {
1137 SemaRef.Diag(NamedBarrField->getLocation(),
1138 diag::err_amdgpu_invalid_field_not_a_wrapper)
1139 << NamedBarrField->getType();
1140 SemaRef.Diag(
1141 R->getLocation(),
1142 diag::note_amdgpu_not_a_named_barrier_wrapper_too_many_fields)
1143 << R->getDeclName();
1144 return;
1145 }
1146
1147 IsWrapper = true;
1148 DiagWrapperNote = [this, R, NamedBarrField]() {
1149 SemaRef.Diag(NamedBarrField->getLocation(),
1150 diag::note_amdgpu_named_barrier_reason_field)
1151 << R->getDeclName() << NamedBarrField->getDeclName();
1152 };
1153 }
1154
1155 // Then, for C++ classes, check if this is a named barrier wrapper by virtue
1156 // of inheriting one.
1157 const auto *CxxR = dyn_cast<CXXRecordDecl>(R);
1158 if (CxxR && !IsWrapper) {
1159 for (CXXBaseSpecifier BS : CxxR->bases()) {
1160 const RecordDecl *Base = BS.getType()->getAsRecordDecl();
1161 if (!Base || !Base->hasAttr<AMDGPUNamedBarrierWrapperAttr>())
1162 continue;
1163
1164 IsWrapper = true;
1165 DiagWrapperNote = [this, BS, R]() {
1166 // Print using the CXXBaseSpecifier type as it includes the template
1167 // parameters.
1168 SemaRef.Diag(BS.getBeginLoc(),
1169 diag::note_amdgpu_named_barrier_reason_inherited)
1170 << R->getDeclName() << BS.getType();
1171 };
1172 }
1173 }
1174
1175 if (!IsWrapper)
1176 return;
1177
1178 // Set the attribute even if the wrapper may be found to be invalid later.
1179 R->addAttr(
1180 AMDGPUNamedBarrierWrapperAttr::CreateImplicit(Context, SourceRange()));
1181
1182 // This is a wrapper CXXRecordDecl, it must have a C++11 standard layout.
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");
1189 DiagWrapperNote();
1190 }
1191}
1192} // namespace clang
This file declares semantic analysis functions specific to AMDGPU.
Enumerates target-specific builtins in their own namespaces within namespace clang.
APSInt & getInt()
Definition APValue.h:577
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:245
const llvm::StringMap< bool > & getFunctionFeatureMap(const FunctionDecl *FD) const
Builtin::Context & BuiltinInfo
Definition ASTContext.h:857
CanQualType getLogicalOperationType() const
The result type of logical operations, '<', '>', '!=', etc.
CanQualType BuiltinFnTy
const TargetInfo & getTargetInfo() const
Definition ASTContext.h:974
unsigned getTargetAddressSpace(LangAS AS) const
bool isInvalid() const
Definition Ownership.h:167
static std::unique_ptr< AtomicScopeModel > create(AtomicScopeModelKind K)
Create an atomic scope model by AtomicScopeModelKind.
Definition SyncScope.h:299
Attr - This represents one attribute.
Definition Attr.h:47
SourceLocation getLocation() const
Definition Attr.h:100
SourceLocation getLoc() const
llvm::SmallString< 64 > getQuotedName(unsigned ID) const
Return the identifier name for the specified builtin inside single quotes for a diagnostic,...
Definition Builtins.cpp:99
bool isAuxBuiltinID(unsigned ID) const
Return true if the builtin ID belongs exclusively to the AuxTarget, and false if it belongs to both p...
Definition Builtins.h:455
unsigned getAuxBuiltinID(unsigned ID) const
Return real builtin ID (i.e.
Definition Builtins.h:461
const char * getRequiredFeatures(unsigned ID) const
Definition Builtins.cpp:118
Represents a base class of a C++ class.
Definition DeclCXX.h:146
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Definition Expr.h:3012
Expr * getArg(unsigned Arg)
getArg - Return the specified argument.
Definition Expr.h:3216
SourceLocation getBeginLoc() const
Definition Expr.h:3346
unsigned getBuiltinCallee() const
getBuiltinCallee - If this is a call to a builtin, return the builtin ID of the callee.
Definition Expr.cpp:1624
unsigned getNumArgs() const
getNumArgs - Return the number of actual arguments to this call.
Definition Expr.h:3203
Decl - This represents one declaration (or definition), e.g.
Definition DeclBase.h:86
void addAttr(Attr *A)
virtual bool TraverseStmt(MaybeConst< Stmt > *S)
This represents one expression.
Definition Expr.h:114
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...
Definition Expr.cpp:3134
void setType(QualType t)
Definition Expr.h:147
bool isValueDependent() const
Determines whether the value of this expression depends on.
Definition Expr.h:179
Expr * IgnoreParens() LLVM_READONLY
Skip past any parentheses which might surround this expression until reaching a fixed point.
Definition Expr.cpp:3125
Decl * getReferencedDeclOfCallee()
Definition Expr.cpp:1578
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.
Definition Expr.h:225
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...
Definition Expr.cpp:283
QualType getType() const
Definition Expr.h:146
Represents a member of a struct/union/class.
Definition Decl.h:3341
Represents a function declaration or definition.
Definition Decl.h:2082
Stmt * getBody(const FunctionDecl *&Definition) const
Retrieve the body (definition) of the function.
Definition Decl.cpp:3285
ParsedAttr - Represents a syntactic attribute.
Definition ParsedAttr.h:119
unsigned getNumArgs() const
getNumArgs - Return the number of actual arguments to this attribute.
Definition ParsedAttr.h:371
Expr * getArgAsExpr(unsigned Arg) const
Definition ParsedAttr.h:383
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.
Definition TypeBase.h:938
const Type * getTypePtr() const
Retrieves a pointer to the underlying (unqualified) type.
Definition TypeBase.h:8578
Represents a struct/union/class.
Definition Decl.h:4506
Scope - A scope is a transient data structure that is used while parsing the program.
Definition Scope.h:41
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...
SemaBase(Sema &S)
Definition SemaBase.cpp:7
ASTContext & getASTContext() const
Definition SemaBase.cpp:9
Sema & SemaRef
Definition SemaBase.h:49
SemaDiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID)
Emit a diagnostic.
Definition SemaBase.cpp:61
Sema - This implements semantic analysis and AST building for C.
Definition Sema.h:863
ASTContext & Context
Definition Sema.h:1332
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...
Definition Sema.h:5055
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...
Definition Stmt.cpp:343
SourceLocation getBeginLoc() const LLVM_READONLY
Definition Stmt.cpp:355
Exposes information about the current target.
Definition TargetInfo.h:228
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.
Definition TypeBase.h:1879
const Type * getPointeeOrArrayElementType() const
If this is a pointer type, return the pointee type.
Definition TypeBase.h:9391
bool isAMDGPUNamedBarrierTypeOrWrapper() const
Check if the type is the AMDGPU named barrier type/a RecordType of a named barrier wrapper,...
Definition Type.cpp:5692
QualType getPointeeType() const
If this is a pointer, ObjC object pointer, or block pointer, this returns the respective pointee.
Definition Type.cpp:885
bool isAMDGPUNamedBarrierType() const
Check if the type is the AMDGPU named barrier type, or an array thereof.
Definition Type.cpp:5688
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)
Definition Interp.h:1512
Top level wrappers for InstallAPI frontend operations.
bool isa(CodeGen::Address addr)
Definition Address.h:330
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.
Definition TypeBase.h:906
static bool checkAMDGPUMaxNumWorkGroupsArguments(Sema &S, Expr *XExpr, Expr *YExpr, Expr *ZExpr, const AMDGPUMaxNumWorkGroupsAttr &Attr)
DynamicRecursiveASTVisitorBase< false > DynamicRecursiveASTVisitor
U cast(CodeGen::Address addr)
Definition Address.h:327
static bool checkAMDGPUFlatWorkGroupSizeArguments(Sema &S, Expr *MinExpr, Expr *MaxExpr, const AMDGPUFlatWorkGroupSizeAttr &Attr)
static bool checkGlobalOrFlatPointerArg(SemaAMDGPU &S, CallExpr *TheCall)
ActionResult< Expr * > ExprResult
Definition Ownership.h:249
static bool checkAMDGPUWavesPerEUArguments(Sema &S, Expr *MinExpr, Expr *MaxExpr, const AMDGPUWavesPerEUAttr &Attr)
int const char * function
Definition c++config.h:31
__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.
Definition Expr.h:667
APValue Val
Val - This is the value the expression can be folded to.
Definition Expr.h:669