blob: 39b4bd00ad8d4e7e7a033fc7992643b00a3332f3 [file] [log] [blame]
alan-bakerfec0a472018-11-08 18:09:40 -05001// Copyright 2018 The Clspv Authors. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#include "clang/AST/RecordLayout.h"
16#include "clang/AST/RecursiveASTVisitor.h"
17#include "clang/Basic/TargetInfo.h"
18#include "clang/CodeGen/CodeGenAction.h"
19#include "clang/Frontend/CompilerInstance.h"
20#include "clang/Frontend/FrontendPluginRegistry.h"
21#include "clang/Frontend/TextDiagnosticPrinter.h"
22
23#include "clspv/Option.h"
24
25#include "FrontendPlugin.h"
26
Kévin Petit0fc88042019-04-09 23:25:02 +010027#include <unordered_set>
28
alan-bakerfec0a472018-11-08 18:09:40 -050029using namespace clang;
30
31namespace {
32struct ExtraValidationConsumer final : public ASTConsumer {
33private:
34 CompilerInstance &Instance;
35 llvm::StringRef InFile;
36
Diego Novillo3cc8d7a2019-04-10 13:30:34 -040037 enum Layout { UBO, SSBO };
alan-baker9bb09792019-03-25 11:25:13 -040038
alan-bakerfec0a472018-11-08 18:09:40 -050039 enum CustomDiagnosticType {
40 CustomDiagnosticVectorsMoreThan4Elements = 0,
41 CustomDiagnosticVoidPointer = 1,
alan-baker9bb09792019-03-25 11:25:13 -040042 CustomDiagnosticUnalignedScalar = 2,
43 CustomDiagnosticUnalignedVec2 = 3,
44 CustomDiagnosticUnalignedVec4 = 4,
alan-bakerfec0a472018-11-08 18:09:40 -050045 CustomDiagnosticUBOUnalignedArray = 5,
46 CustomDiagnosticUBOUnalignedStruct = 6,
alan-baker9bb09792019-03-25 11:25:13 -040047 CustomDiagnosticSmallStraddle = 7,
48 CustomDiagnosticLargeStraddle = 8,
49 CustomDiagnosticUnalignedStructMember = 9,
alan-bakerfec0a472018-11-08 18:09:40 -050050 CustomDiagnosticUBORestrictedSize = 10,
51 CustomDiagnosticUBORestrictedStruct = 11,
alan-baker3d9e2012019-01-11 14:55:30 -050052 CustomDiagnosticUBOArrayStride = 12,
53 CustomDiagnosticLocationInfo = 13,
alan-baker9bb09792019-03-25 11:25:13 -040054 CustomDiagnosticSSBOUnalignedArray = 14,
55 CustomDiagnosticSSBOUnalignedStruct = 15,
Kévin Petit0fc88042019-04-09 23:25:02 +010056 CustomDiagnosticOverloadedKernel = 16,
alan-bakerfec0a472018-11-08 18:09:40 -050057 CustomDiagnosticTotal
58 };
59 std::vector<unsigned> CustomDiagnosticsIDMap;
60
61 bool IsSupportedType(QualType QT, SourceRange SR) {
62 auto *Ty = QT.getTypePtr();
63
64 // First check if we have a pointer type.
65 if (Ty->isPointerType()) {
66 const Type *pointeeTy = Ty->getPointeeType().getTypePtr();
67 if (pointeeTy && pointeeTy->isVoidType()) {
68 // We don't support void pointers.
69 Instance.getDiagnostics().Report(
70 SR.getBegin(), CustomDiagnosticsIDMap[CustomDiagnosticVoidPointer]);
71 return false;
72 }
73 // Otherwise check recursively.
74 return IsSupportedType(Ty->getPointeeType(), SR);
75 }
76
77 const auto &canonicalType = QT.getCanonicalType();
78 if (auto *VT = llvm::dyn_cast<ExtVectorType>(canonicalType)) {
79 // We don't support vectors with more than 4 elements.
80 if (4 < VT->getNumElements()) {
81 Instance.getDiagnostics().Report(
82 SR.getBegin(),
83 CustomDiagnosticsIDMap[CustomDiagnosticVectorsMoreThan4Elements]);
84 return false;
85 }
86 }
87
88 return true;
89 }
90
alan-baker3d9e2012019-01-11 14:55:30 -050091 // Report a diagnostic using |diag|. If |arg_range| and |specific_range|
92 // differ, also issue a note with the specific location of the error.
93 void Report(const CustomDiagnosticType &diag, SourceRange arg_range,
94 SourceRange specific_range) {
95 Instance.getDiagnostics().Report(arg_range.getBegin(),
96 CustomDiagnosticsIDMap[diag]);
97 if (arg_range != specific_range) {
98 Instance.getDiagnostics().Report(
99 specific_range.getBegin(),
100 CustomDiagnosticsIDMap[CustomDiagnosticLocationInfo]);
101 }
102 }
103
alan-baker9bb09792019-03-25 11:25:13 -0400104 // Returns the alignment of |QT| to satisfy |layout|'s rules.
105 uint64_t GetAlignment(const QualType QT, const Layout &layout,
106 const ASTContext &context) const {
alan-bakerfec0a472018-11-08 18:09:40 -0500107 const auto canonical = QT.getCanonicalType();
108 uint64_t alignment = context.getTypeAlignInChars(canonical).getQuantity();
alan-baker9bb09792019-03-25 11:25:13 -0400109 if (layout == UBO &&
110 (canonical->isRecordType() || canonical->isArrayType())) {
alan-bakerfec0a472018-11-08 18:09:40 -0500111 return llvm::alignTo(alignment, 16);
112 }
113 return alignment;
114 }
115
116 // Returns true if |QT| is a valid layout for a Uniform buffer. Refer to
117 // 14.5.4 in the Vulkan specification.
alan-baker9bb09792019-03-25 11:25:13 -0400118 bool IsSupportedLayout(QualType QT, uint64_t offset, const Layout &layout,
119 ASTContext &context, SourceRange arg_range,
120 SourceRange specific_range) {
alan-bakerfec0a472018-11-08 18:09:40 -0500121 const auto canonical = QT.getCanonicalType();
122 if (canonical->isScalarType()) {
alan-baker9bb09792019-03-25 11:25:13 -0400123 if (!IsSupportedScalarLayout(canonical, offset, layout, context,
124 arg_range, specific_range))
alan-bakerfec0a472018-11-08 18:09:40 -0500125 return false;
126 } else if (canonical->isExtVectorType()) {
alan-baker9bb09792019-03-25 11:25:13 -0400127 if (!IsSupportedVectorLayout(canonical, offset, layout, context,
128 arg_range, specific_range))
alan-bakerfec0a472018-11-08 18:09:40 -0500129 return false;
130 } else if (canonical->isArrayType()) {
alan-baker9bb09792019-03-25 11:25:13 -0400131 if (!IsSupportedArrayLayout(canonical, offset, layout, context, arg_range,
132 specific_range))
alan-bakerfec0a472018-11-08 18:09:40 -0500133 return false;
134 } else if (canonical->isRecordType()) {
alan-baker9bb09792019-03-25 11:25:13 -0400135 if (!IsSupportedRecordLayout(canonical, offset, layout, context,
136 arg_range, specific_range))
alan-bakerfec0a472018-11-08 18:09:40 -0500137 return false;
138 }
139
140 // TODO(alan-baker): Find a way to avoid this restriction.
141 // Don't allow padding. This prevents structs like:
142 // struct {
143 // int x[2];
144 // int y __attribute((aligned(16)));
145 // };
146 //
147 // This would map in LLVM to { [2 x i32], [8 x i8], i32, [12 xi8] }.
148 // There is no easy way to manipulate the padding after the array to
149 // satisfy the standard Uniform buffer layout rules in this case. The usual
150 // trick is replacing the i8 arrays with an i32 element, but the i32 would
151 // still be laid out too close to the array.
152 const auto type_size = context.getTypeSizeInChars(canonical).getQuantity();
alan-baker9bb09792019-03-25 11:25:13 -0400153 const auto type_align = GetAlignment(canonical, layout, context);
154 if (layout == UBO && (type_size % type_align != 0)) {
alan-baker3d9e2012019-01-11 14:55:30 -0500155 Report(CustomDiagnosticUBORestrictedSize, arg_range, specific_range);
alan-bakerfec0a472018-11-08 18:09:40 -0500156 return false;
157 }
158
159 return true;
160 }
161
alan-baker9bb09792019-03-25 11:25:13 -0400162 bool IsSupportedScalarLayout(QualType QT, uint64_t offset,
163 const Layout & /*layout*/, ASTContext &context,
164 SourceRange arg_range,
165 SourceRange specific_range) {
alan-bakerfec0a472018-11-08 18:09:40 -0500166 // A scalar type of size N has a base alignment on N.
167 const unsigned type_size = context.getTypeSizeInChars(QT).getQuantity();
168 if (offset % type_size != 0) {
alan-baker9bb09792019-03-25 11:25:13 -0400169 Report(CustomDiagnosticUnalignedScalar, arg_range, specific_range);
alan-bakerfec0a472018-11-08 18:09:40 -0500170 return false;
171 }
172
173 return true;
174 }
175
alan-baker9bb09792019-03-25 11:25:13 -0400176 bool IsSupportedVectorLayout(QualType QT, uint64_t offset,
177 const Layout &layout, ASTContext &context,
178 SourceRange arg_range,
179 SourceRange specific_range) {
alan-bakerfec0a472018-11-08 18:09:40 -0500180 // 2-component vectors have a base alignment of 2 * (size of element).
181 // 3- and 4-component vectors hae a base alignment of 4 * (size of
182 // element).
183 const auto *VT = llvm::cast<VectorType>(QT);
184 const auto ele_size =
185 context.getTypeSizeInChars(VT->getElementType()).getQuantity();
186 if (VT->getNumElements() == 2) {
187 if (offset % (ele_size * 2) != 0) {
alan-baker9bb09792019-03-25 11:25:13 -0400188 Report(CustomDiagnosticUnalignedVec2, arg_range, specific_range);
alan-bakerfec0a472018-11-08 18:09:40 -0500189 return false;
190 }
191 } else if (offset % (ele_size * 4) != 0) {
192 // Other vector sizes cause errors elsewhere.
alan-baker9bb09792019-03-25 11:25:13 -0400193 Report(CustomDiagnosticUnalignedVec4, arg_range, specific_range);
alan-bakerfec0a472018-11-08 18:09:40 -0500194 return false;
195 }
196
197 // Straddling rules:
198 // * If total vector size is less than 16 bytes, the offset must place the
199 // entire vector within the same 16 bytes.
200 // * If total vector size is greater than 16 bytes, the offset must be a
201 // multiple of 16.
202 const auto size = context.getTypeSizeInChars(QT).getQuantity();
203 if (size <= 16 && (offset / 16 != (offset + size - 1) / 16)) {
alan-baker9bb09792019-03-25 11:25:13 -0400204 Report(CustomDiagnosticSmallStraddle, arg_range, specific_range);
alan-bakerfec0a472018-11-08 18:09:40 -0500205 return false;
206 } else if (size > 16 && (offset % 16 != 0)) {
alan-baker9bb09792019-03-25 11:25:13 -0400207 Report(CustomDiagnosticLargeStraddle, arg_range, specific_range);
alan-bakerfec0a472018-11-08 18:09:40 -0500208 return false;
209 }
210
alan-baker9bb09792019-03-25 11:25:13 -0400211 return IsSupportedLayout(VT->getElementType(), offset, layout, context,
212 arg_range, specific_range);
alan-bakerfec0a472018-11-08 18:09:40 -0500213 }
214
alan-baker9bb09792019-03-25 11:25:13 -0400215 bool IsSupportedArrayLayout(QualType QT, uint64_t offset,
216 const Layout &layout, ASTContext &context,
217 SourceRange arg_range,
218 SourceRange specific_range) {
219 // An array has a base alignment of is element type.
220 // If the layout is UBO, the alignment is rounded up to a multiple of 16.
alan-bakerfec0a472018-11-08 18:09:40 -0500221 const auto *AT = llvm::cast<ArrayType>(QT);
Diego Novillo3cc8d7a2019-04-10 13:30:34 -0400222 const auto element_align =
223 GetAlignment(AT->getElementType(), layout, context);
alan-baker9bb09792019-03-25 11:25:13 -0400224 const auto type_align =
225 layout == UBO ? llvm::alignTo(element_align, 16) : element_align;
alan-bakerfec0a472018-11-08 18:09:40 -0500226 if (offset % type_align != 0) {
alan-baker9bb09792019-03-25 11:25:13 -0400227 auto diag_id = layout == UBO ? CustomDiagnosticUBOUnalignedArray
228 : CustomDiagnosticSSBOUnalignedArray;
229 Report(diag_id, arg_range, specific_range);
alan-bakerfec0a472018-11-08 18:09:40 -0500230 return false;
231 }
alan-baker9bb09792019-03-25 11:25:13 -0400232 if (layout == UBO && !clspv::Option::RelaxedUniformBufferLayout()) {
alan-baker3d9e2012019-01-11 14:55:30 -0500233 // The ArrayStride must be a multiple of the base alignment of the array
234 // (i.e. a multiple of 16). This means that the element size must be
235 // restricted to be the base alignment of the array.
236 const auto element_size =
237 context.getTypeSizeInChars(AT->getElementType()).getQuantity();
238 if (element_size % type_align != 0) {
239 Report(CustomDiagnosticUBOArrayStride, arg_range, specific_range);
240 return false;
241 }
242 }
alan-bakerfec0a472018-11-08 18:09:40 -0500243
alan-baker9bb09792019-03-25 11:25:13 -0400244 return IsSupportedLayout(AT->getElementType(), offset, layout, context,
245 arg_range, specific_range);
alan-bakerfec0a472018-11-08 18:09:40 -0500246 }
247
alan-baker9bb09792019-03-25 11:25:13 -0400248 bool IsSupportedRecordLayout(QualType QT, uint64_t offset,
249 const Layout &layout, ASTContext &context,
250 SourceRange arg_range,
251 SourceRange specific_range) {
252 // A structure has a base alignment of its largest member. For UBO layouts,
253 // alignment is rounded up to a multiple of 16.
alan-bakerfec0a472018-11-08 18:09:40 -0500254 const auto *RT = llvm::cast<RecordType>(QT);
alan-baker9bb09792019-03-25 11:25:13 -0400255 auto type_alignment = GetAlignment(QT, layout, context);
Diego Novillo3cc8d7a2019-04-10 13:30:34 -0400256 if (layout == UBO)
257 llvm::alignTo(type_alignment, 16);
alan-bakerfec0a472018-11-08 18:09:40 -0500258 if (offset % type_alignment != 0) {
alan-baker9bb09792019-03-25 11:25:13 -0400259 auto diag_id = layout == UBO ? CustomDiagnosticUBOUnalignedStruct
260 : CustomDiagnosticSSBOUnalignedStruct;
261 Report(diag_id, arg_range, specific_range);
alan-bakerfec0a472018-11-08 18:09:40 -0500262 return false;
263 }
264
alan-baker9bb09792019-03-25 11:25:13 -0400265 const auto &record_layout = context.getASTRecordLayout(RT->getDecl());
alan-bakerfec0a472018-11-08 18:09:40 -0500266 const FieldDecl *prev = nullptr;
267 for (auto field_decl : RT->getDecl()->fields()) {
268 const auto field_type = field_decl->getType();
alan-baker9bb09792019-03-25 11:25:13 -0400269 const auto field_alignment = GetAlignment(field_type, layout, context);
alan-bakerfec0a472018-11-08 18:09:40 -0500270 const unsigned field_no = field_decl->getFieldIndex();
271 const uint64_t field_offset =
alan-baker9bb09792019-03-25 11:25:13 -0400272 record_layout.getFieldOffset(field_no) / context.getCharWidth();
alan-bakerfec0a472018-11-08 18:09:40 -0500273
274 // Rules must be checked recursively.
alan-baker9bb09792019-03-25 11:25:13 -0400275 if (!IsSupportedLayout(field_type, field_offset + offset, layout, context,
276 arg_range, field_decl->getSourceRange())) {
alan-bakerfec0a472018-11-08 18:09:40 -0500277 return false;
278 }
279
280 if (prev) {
281 const auto prev_canonical = prev->getType().getCanonicalType();
282 const uint64_t prev_offset =
alan-baker9bb09792019-03-25 11:25:13 -0400283 record_layout.getFieldOffset(field_no - 1) / context.getCharWidth();
alan-bakerfec0a472018-11-08 18:09:40 -0500284 const auto prev_size =
285 context.getTypeSizeInChars(prev_canonical).getQuantity();
Diego Novillo3cc8d7a2019-04-10 13:30:34 -0400286 const auto prev_alignment =
287 GetAlignment(prev_canonical, layout, context);
alan-bakerfec0a472018-11-08 18:09:40 -0500288 const auto next_available =
289 prev_offset + llvm::alignTo(prev_size, prev_alignment);
290 if (prev_canonical->isArrayType() || prev_canonical->isRecordType()) {
291 // The next element after an array or struct must be placed on or
292 // after the next multiple of the alignment of that array or
293 // struct.
alan-baker9bb09792019-03-25 11:25:13 -0400294 // For UBO layouts, both arrays and structs must be aligned to a
295 // multiple of 16 bytes.
296 const uint64_t final_align = layout == UBO
297 ? llvm::alignTo(next_available, 16)
298 : next_available;
299 if (final_align > field_offset) {
300 Report(CustomDiagnosticUnalignedStructMember, arg_range,
alan-baker3d9e2012019-01-11 14:55:30 -0500301 field_decl->getSourceRange());
alan-bakerfec0a472018-11-08 18:09:40 -0500302 return false;
303 }
304 }
305 }
306
307 prev = field_decl;
308 }
309
310 return true;
311 }
312
313 // This will be used to check the inside of function bodies.
314 class DeclVisitor : public RecursiveASTVisitor<DeclVisitor> {
315 private:
316 ExtraValidationConsumer &consumer;
317
318 public:
319 explicit DeclVisitor(ExtraValidationConsumer &VC) : consumer(VC) {}
320
321 // Visits a declaration. Emits a diagnostic and returns false if the
322 // declaration represents an unsupported vector value or vector type.
323 // Otherwise returns true.
324 bool VisitDecl(Decl *D) {
325 // Looking at the Decl class hierarchy, it seems ValueDecl and TypeDecl
326 // are the only two that might represent an unsupported vector type.
327 if (auto *VD = dyn_cast<ValueDecl>(D)) {
328 return consumer.IsSupportedType(VD->getType(), D->getSourceRange());
329 } else if (auto *TD = dyn_cast<TypeDecl>(D)) {
330 QualType DefinedType = TD->getASTContext().getTypeDeclType(TD);
331 return consumer.IsSupportedType(DefinedType, TD->getSourceRange());
332 }
333 return true;
334 }
335 };
336
337 DeclVisitor Visitor;
Kévin Petit0fc88042019-04-09 23:25:02 +0100338 std::unordered_set<std::string> Kernels;
alan-bakerfec0a472018-11-08 18:09:40 -0500339
340public:
341 explicit ExtraValidationConsumer(CompilerInstance &Instance,
342 llvm::StringRef InFile)
343 : Instance(Instance), InFile(InFile),
344 CustomDiagnosticsIDMap(CustomDiagnosticTotal), Visitor(*this) {
345 auto &DE = Instance.getDiagnostics();
346
347 CustomDiagnosticsIDMap[CustomDiagnosticVectorsMoreThan4Elements] =
348 DE.getCustomDiagID(
349 DiagnosticsEngine::Error,
350 "vectors with more than 4 elements are not supported");
351 CustomDiagnosticsIDMap[CustomDiagnosticVoidPointer] = DE.getCustomDiagID(
352 DiagnosticsEngine::Error, "pointer-to-void is not supported");
alan-baker9bb09792019-03-25 11:25:13 -0400353 CustomDiagnosticsIDMap[CustomDiagnosticUnalignedScalar] =
Diego Novillo3cc8d7a2019-04-10 13:30:34 -0400354 DE.getCustomDiagID(DiagnosticsEngine::Error,
355 "scalar elements must be aligned to their size");
alan-baker9bb09792019-03-25 11:25:13 -0400356 CustomDiagnosticsIDMap[CustomDiagnosticUnalignedVec2] = DE.getCustomDiagID(
357 DiagnosticsEngine::Error,
358 "two-component vectors must be aligned to 2 times their element size");
359 CustomDiagnosticsIDMap[CustomDiagnosticUnalignedVec4] =
alan-bakerfec0a472018-11-08 18:09:40 -0500360 DE.getCustomDiagID(DiagnosticsEngine::Error,
alan-baker9bb09792019-03-25 11:25:13 -0400361 "three- and four-component vectors must be aligned "
362 "to 4 times their element size");
alan-bakerfec0a472018-11-08 18:09:40 -0500363 CustomDiagnosticsIDMap[CustomDiagnosticUBOUnalignedArray] =
364 DE.getCustomDiagID(DiagnosticsEngine::Error,
365 "in an UBO, arrays must be aligned to their element "
366 "alignment, rounded up to a multiple of 16 bytes");
367 CustomDiagnosticsIDMap[CustomDiagnosticUBOUnalignedStruct] =
368 DE.getCustomDiagID(
369 DiagnosticsEngine::Error,
370 "in an UBO, structs must be aligned to their "
371 "largest element alignment, rounded up to a multiple of "
372 "16 bytes");
alan-baker9bb09792019-03-25 11:25:13 -0400373 CustomDiagnosticsIDMap[CustomDiagnosticSmallStraddle] =
alan-bakerfec0a472018-11-08 18:09:40 -0500374 DE.getCustomDiagID(DiagnosticsEngine::Error,
alan-baker9bb09792019-03-25 11:25:13 -0400375 "vectors with a total size less than or equal to 16 "
376 "bytes must be placed entirely within a 16 byte "
377 "aligned region");
378 CustomDiagnosticsIDMap[CustomDiagnosticLargeStraddle] =
alan-bakerfec0a472018-11-08 18:09:40 -0500379 DE.getCustomDiagID(DiagnosticsEngine::Error,
alan-baker9bb09792019-03-25 11:25:13 -0400380 "vectors with a total size greater than 16 bytes "
381 "must aligned to 16 bytes");
382 CustomDiagnosticsIDMap[CustomDiagnosticUnalignedStructMember] =
alan-bakerfec0a472018-11-08 18:09:40 -0500383 DE.getCustomDiagID(DiagnosticsEngine::Error,
alan-baker9bb09792019-03-25 11:25:13 -0400384 "a structure member must not be placed between the "
385 "end of a structure or array and the next multiple "
386 "of the base alignment of that structure or array");
alan-bakerfec0a472018-11-08 18:09:40 -0500387 CustomDiagnosticsIDMap[CustomDiagnosticUBORestrictedSize] =
388 DE.getCustomDiagID(DiagnosticsEngine::Error,
389 "clspv restriction: UBO element size must be a "
390 "multiple of that element's alignment");
391 CustomDiagnosticsIDMap[CustomDiagnosticUBORestrictedStruct] =
392 DE.getCustomDiagID(
393 DiagnosticsEngine::Error,
394 "clspv restriction: UBO structures may not have implicit padding");
alan-baker3d9e2012019-01-11 14:55:30 -0500395 CustomDiagnosticsIDMap[CustomDiagnosticUBOArrayStride] = DE.getCustomDiagID(
396 DiagnosticsEngine::Error,
397 "clspv restriction: to satisfy UBO ArrayStride restrictions, element "
398 "size must be a multiple of array alignment");
399 CustomDiagnosticsIDMap[CustomDiagnosticLocationInfo] =
400 DE.getCustomDiagID(DiagnosticsEngine::Note, "here");
alan-baker9bb09792019-03-25 11:25:13 -0400401 CustomDiagnosticsIDMap[CustomDiagnosticSSBOUnalignedArray] =
402 DE.getCustomDiagID(
403 DiagnosticsEngine::Error,
404 "in a SSBO, arrays must be aligned to their element alignment");
405 CustomDiagnosticsIDMap[CustomDiagnosticSSBOUnalignedStruct] =
406 DE.getCustomDiagID(DiagnosticsEngine::Error,
407 "in a SSBO, structs must be aligned to their "
408 "largest element alignment");
Kévin Petit0fc88042019-04-09 23:25:02 +0100409 CustomDiagnosticsIDMap[CustomDiagnosticOverloadedKernel] =
410 DE.getCustomDiagID(DiagnosticsEngine::Error,
411 "kernel functions can't be overloaded");
alan-bakerfec0a472018-11-08 18:09:40 -0500412 }
413
414 virtual bool HandleTopLevelDecl(DeclGroupRef DG) override {
415 for (auto *D : DG) {
416 if (auto *FD = llvm::dyn_cast<FunctionDecl>(D)) {
417 // If the function has a body it means we are not an OpenCL builtin
418 // function.
419 if (FD->hasBody()) {
420 if (!IsSupportedType(FD->getReturnType(),
421 FD->getReturnTypeSourceRange())) {
422 return false;
423 }
424
425 bool is_opencl_kernel = false;
426 if (FD->hasAttrs()) {
427 for (auto *attr : FD->attrs()) {
428 if (attr->getKind() == attr::Kind::OpenCLKernel) {
429 is_opencl_kernel = true;
430 }
431 }
432 }
433
Kévin Petit0fc88042019-04-09 23:25:02 +0100434 if (is_opencl_kernel) {
435 if (Kernels.count(FD->getName()) != 0) {
436 auto srcRange = FD->getSourceRange();
437 Report(CustomDiagnosticOverloadedKernel, srcRange, srcRange);
438 } else {
439 Kernels.insert(FD->getName());
440 }
441 }
442
alan-bakerfec0a472018-11-08 18:09:40 -0500443 for (auto *P : FD->parameters()) {
444 auto type = P->getType();
445 if (!IsSupportedType(P->getOriginalType(), P->getSourceRange())) {
446 return false;
447 }
448
alan-baker9bb09792019-03-25 11:25:13 -0400449 if (is_opencl_kernel && type->isPointerType() &&
450 ((type->getPointeeType().getAddressSpace() ==
451 LangAS::opencl_constant) ||
452 (type->getPointeeType().getAddressSpace() ==
453 LangAS::opencl_global))) {
alan-baker3d9e2012019-01-11 14:55:30 -0500454 // The argument will be generated as an array within a block.
455 // Generate an array type to check the validity for the generated
456 // case.
alan-baker9bb09792019-03-25 11:25:13 -0400457 Layout layout = SSBO;
458 if (clspv::Option::ConstantArgsInUniformBuffer() &&
459 !clspv::Option::Std430UniformBufferLayout() &&
460 type->getPointeeType().getAddressSpace() ==
461 LangAS::opencl_constant) {
462 layout = UBO;
463 }
alan-baker3d9e2012019-01-11 14:55:30 -0500464 auto array_type = FD->getASTContext().getIncompleteArrayType(
465 type->getPointeeType(), clang::ArrayType::Normal, 0);
alan-baker9bb09792019-03-25 11:25:13 -0400466 if (!IsSupportedLayout(array_type, 0, layout, FD->getASTContext(),
467 P->getSourceRange(),
468 P->getSourceRange())) {
alan-bakerfec0a472018-11-08 18:09:40 -0500469 return false;
470 }
471 }
alan-baker038e9242019-04-19 22:14:41 -0400472
473 if (is_opencl_kernel && !type->isPointerType()) {
474 Layout layout = SSBO;
475 if (clspv::Option::PodArgsInUniformBuffer() &&
476 !clspv::Option::Std430UniformBufferLayout())
477 layout = UBO;
478
479 if (!IsSupportedLayout(type, 0, layout, FD->getASTContext(),
480 P->getSourceRange(),
481 P->getSourceRange())) {
482 return false;
483 }
484 }
alan-bakerfec0a472018-11-08 18:09:40 -0500485 }
486
487 // Check for unsupported vector types.
488 Visitor.TraverseDecl(FD);
489 }
490 }
491 }
492
493 return true;
494 }
495};
496} // namespace
497
498namespace clspv {
499std::unique_ptr<ASTConsumer>
500ExtraValidationASTAction::CreateASTConsumer(CompilerInstance &CI,
501 llvm::StringRef InFile) {
502 return std::unique_ptr<ASTConsumer>(new ExtraValidationConsumer(CI, InFile));
503}
504} // namespace clspv