alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 1 | // 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 Petit | 0fc8804 | 2019-04-09 23:25:02 +0100 | [diff] [blame] | 27 | #include <unordered_set> |
| 28 | |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 29 | using namespace clang; |
| 30 | |
| 31 | namespace { |
| 32 | struct ExtraValidationConsumer final : public ASTConsumer { |
| 33 | private: |
| 34 | CompilerInstance &Instance; |
| 35 | llvm::StringRef InFile; |
| 36 | |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 37 | enum Layout { UBO, SSBO }; |
alan-baker | 9bb0979 | 2019-03-25 11:25:13 -0400 | [diff] [blame] | 38 | |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 39 | enum CustomDiagnosticType { |
| 40 | CustomDiagnosticVectorsMoreThan4Elements = 0, |
| 41 | CustomDiagnosticVoidPointer = 1, |
alan-baker | 9bb0979 | 2019-03-25 11:25:13 -0400 | [diff] [blame] | 42 | CustomDiagnosticUnalignedScalar = 2, |
| 43 | CustomDiagnosticUnalignedVec2 = 3, |
| 44 | CustomDiagnosticUnalignedVec4 = 4, |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 45 | CustomDiagnosticUBOUnalignedArray = 5, |
| 46 | CustomDiagnosticUBOUnalignedStruct = 6, |
alan-baker | 9bb0979 | 2019-03-25 11:25:13 -0400 | [diff] [blame] | 47 | CustomDiagnosticSmallStraddle = 7, |
| 48 | CustomDiagnosticLargeStraddle = 8, |
| 49 | CustomDiagnosticUnalignedStructMember = 9, |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 50 | CustomDiagnosticUBORestrictedSize = 10, |
| 51 | CustomDiagnosticUBORestrictedStruct = 11, |
alan-baker | 3d9e201 | 2019-01-11 14:55:30 -0500 | [diff] [blame] | 52 | CustomDiagnosticUBOArrayStride = 12, |
| 53 | CustomDiagnosticLocationInfo = 13, |
alan-baker | 9bb0979 | 2019-03-25 11:25:13 -0400 | [diff] [blame] | 54 | CustomDiagnosticSSBOUnalignedArray = 14, |
| 55 | CustomDiagnosticSSBOUnalignedStruct = 15, |
Kévin Petit | 0fc8804 | 2019-04-09 23:25:02 +0100 | [diff] [blame] | 56 | CustomDiagnosticOverloadedKernel = 16, |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 57 | 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-baker | 3d9e201 | 2019-01-11 14:55:30 -0500 | [diff] [blame] | 91 | // 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-baker | 9bb0979 | 2019-03-25 11:25:13 -0400 | [diff] [blame] | 104 | // 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-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 107 | const auto canonical = QT.getCanonicalType(); |
| 108 | uint64_t alignment = context.getTypeAlignInChars(canonical).getQuantity(); |
alan-baker | 9bb0979 | 2019-03-25 11:25:13 -0400 | [diff] [blame] | 109 | if (layout == UBO && |
| 110 | (canonical->isRecordType() || canonical->isArrayType())) { |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 111 | 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-baker | 9bb0979 | 2019-03-25 11:25:13 -0400 | [diff] [blame] | 118 | bool IsSupportedLayout(QualType QT, uint64_t offset, const Layout &layout, |
| 119 | ASTContext &context, SourceRange arg_range, |
| 120 | SourceRange specific_range) { |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 121 | const auto canonical = QT.getCanonicalType(); |
| 122 | if (canonical->isScalarType()) { |
alan-baker | 9bb0979 | 2019-03-25 11:25:13 -0400 | [diff] [blame] | 123 | if (!IsSupportedScalarLayout(canonical, offset, layout, context, |
| 124 | arg_range, specific_range)) |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 125 | return false; |
| 126 | } else if (canonical->isExtVectorType()) { |
alan-baker | 9bb0979 | 2019-03-25 11:25:13 -0400 | [diff] [blame] | 127 | if (!IsSupportedVectorLayout(canonical, offset, layout, context, |
| 128 | arg_range, specific_range)) |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 129 | return false; |
| 130 | } else if (canonical->isArrayType()) { |
alan-baker | 9bb0979 | 2019-03-25 11:25:13 -0400 | [diff] [blame] | 131 | if (!IsSupportedArrayLayout(canonical, offset, layout, context, arg_range, |
| 132 | specific_range)) |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 133 | return false; |
| 134 | } else if (canonical->isRecordType()) { |
alan-baker | 9bb0979 | 2019-03-25 11:25:13 -0400 | [diff] [blame] | 135 | if (!IsSupportedRecordLayout(canonical, offset, layout, context, |
| 136 | arg_range, specific_range)) |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 137 | 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-baker | 9bb0979 | 2019-03-25 11:25:13 -0400 | [diff] [blame] | 153 | const auto type_align = GetAlignment(canonical, layout, context); |
| 154 | if (layout == UBO && (type_size % type_align != 0)) { |
alan-baker | 3d9e201 | 2019-01-11 14:55:30 -0500 | [diff] [blame] | 155 | Report(CustomDiagnosticUBORestrictedSize, arg_range, specific_range); |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 156 | return false; |
| 157 | } |
| 158 | |
| 159 | return true; |
| 160 | } |
| 161 | |
alan-baker | 9bb0979 | 2019-03-25 11:25:13 -0400 | [diff] [blame] | 162 | bool IsSupportedScalarLayout(QualType QT, uint64_t offset, |
| 163 | const Layout & /*layout*/, ASTContext &context, |
| 164 | SourceRange arg_range, |
| 165 | SourceRange specific_range) { |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 166 | // 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-baker | 9bb0979 | 2019-03-25 11:25:13 -0400 | [diff] [blame] | 169 | Report(CustomDiagnosticUnalignedScalar, arg_range, specific_range); |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 170 | return false; |
| 171 | } |
| 172 | |
| 173 | return true; |
| 174 | } |
| 175 | |
alan-baker | 9bb0979 | 2019-03-25 11:25:13 -0400 | [diff] [blame] | 176 | bool IsSupportedVectorLayout(QualType QT, uint64_t offset, |
| 177 | const Layout &layout, ASTContext &context, |
| 178 | SourceRange arg_range, |
| 179 | SourceRange specific_range) { |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 180 | // 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-baker | 9bb0979 | 2019-03-25 11:25:13 -0400 | [diff] [blame] | 188 | Report(CustomDiagnosticUnalignedVec2, arg_range, specific_range); |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 189 | return false; |
| 190 | } |
| 191 | } else if (offset % (ele_size * 4) != 0) { |
| 192 | // Other vector sizes cause errors elsewhere. |
alan-baker | 9bb0979 | 2019-03-25 11:25:13 -0400 | [diff] [blame] | 193 | Report(CustomDiagnosticUnalignedVec4, arg_range, specific_range); |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 194 | 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-baker | 9bb0979 | 2019-03-25 11:25:13 -0400 | [diff] [blame] | 204 | Report(CustomDiagnosticSmallStraddle, arg_range, specific_range); |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 205 | return false; |
| 206 | } else if (size > 16 && (offset % 16 != 0)) { |
alan-baker | 9bb0979 | 2019-03-25 11:25:13 -0400 | [diff] [blame] | 207 | Report(CustomDiagnosticLargeStraddle, arg_range, specific_range); |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 208 | return false; |
| 209 | } |
| 210 | |
alan-baker | 9bb0979 | 2019-03-25 11:25:13 -0400 | [diff] [blame] | 211 | return IsSupportedLayout(VT->getElementType(), offset, layout, context, |
| 212 | arg_range, specific_range); |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 213 | } |
| 214 | |
alan-baker | 9bb0979 | 2019-03-25 11:25:13 -0400 | [diff] [blame] | 215 | 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-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 221 | const auto *AT = llvm::cast<ArrayType>(QT); |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 222 | const auto element_align = |
| 223 | GetAlignment(AT->getElementType(), layout, context); |
alan-baker | 9bb0979 | 2019-03-25 11:25:13 -0400 | [diff] [blame] | 224 | const auto type_align = |
| 225 | layout == UBO ? llvm::alignTo(element_align, 16) : element_align; |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 226 | if (offset % type_align != 0) { |
alan-baker | 9bb0979 | 2019-03-25 11:25:13 -0400 | [diff] [blame] | 227 | auto diag_id = layout == UBO ? CustomDiagnosticUBOUnalignedArray |
| 228 | : CustomDiagnosticSSBOUnalignedArray; |
| 229 | Report(diag_id, arg_range, specific_range); |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 230 | return false; |
| 231 | } |
alan-baker | 9bb0979 | 2019-03-25 11:25:13 -0400 | [diff] [blame] | 232 | if (layout == UBO && !clspv::Option::RelaxedUniformBufferLayout()) { |
alan-baker | 3d9e201 | 2019-01-11 14:55:30 -0500 | [diff] [blame] | 233 | // 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-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 243 | |
alan-baker | 9bb0979 | 2019-03-25 11:25:13 -0400 | [diff] [blame] | 244 | return IsSupportedLayout(AT->getElementType(), offset, layout, context, |
| 245 | arg_range, specific_range); |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 246 | } |
| 247 | |
alan-baker | 9bb0979 | 2019-03-25 11:25:13 -0400 | [diff] [blame] | 248 | 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-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 254 | const auto *RT = llvm::cast<RecordType>(QT); |
alan-baker | 9bb0979 | 2019-03-25 11:25:13 -0400 | [diff] [blame] | 255 | auto type_alignment = GetAlignment(QT, layout, context); |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 256 | if (layout == UBO) |
| 257 | llvm::alignTo(type_alignment, 16); |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 258 | if (offset % type_alignment != 0) { |
alan-baker | 9bb0979 | 2019-03-25 11:25:13 -0400 | [diff] [blame] | 259 | auto diag_id = layout == UBO ? CustomDiagnosticUBOUnalignedStruct |
| 260 | : CustomDiagnosticSSBOUnalignedStruct; |
| 261 | Report(diag_id, arg_range, specific_range); |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 262 | return false; |
| 263 | } |
| 264 | |
alan-baker | 9bb0979 | 2019-03-25 11:25:13 -0400 | [diff] [blame] | 265 | const auto &record_layout = context.getASTRecordLayout(RT->getDecl()); |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 266 | const FieldDecl *prev = nullptr; |
| 267 | for (auto field_decl : RT->getDecl()->fields()) { |
| 268 | const auto field_type = field_decl->getType(); |
alan-baker | 9bb0979 | 2019-03-25 11:25:13 -0400 | [diff] [blame] | 269 | const auto field_alignment = GetAlignment(field_type, layout, context); |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 270 | const unsigned field_no = field_decl->getFieldIndex(); |
| 271 | const uint64_t field_offset = |
alan-baker | 9bb0979 | 2019-03-25 11:25:13 -0400 | [diff] [blame] | 272 | record_layout.getFieldOffset(field_no) / context.getCharWidth(); |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 273 | |
| 274 | // Rules must be checked recursively. |
alan-baker | 9bb0979 | 2019-03-25 11:25:13 -0400 | [diff] [blame] | 275 | if (!IsSupportedLayout(field_type, field_offset + offset, layout, context, |
| 276 | arg_range, field_decl->getSourceRange())) { |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 277 | return false; |
| 278 | } |
| 279 | |
| 280 | if (prev) { |
| 281 | const auto prev_canonical = prev->getType().getCanonicalType(); |
| 282 | const uint64_t prev_offset = |
alan-baker | 9bb0979 | 2019-03-25 11:25:13 -0400 | [diff] [blame] | 283 | record_layout.getFieldOffset(field_no - 1) / context.getCharWidth(); |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 284 | const auto prev_size = |
| 285 | context.getTypeSizeInChars(prev_canonical).getQuantity(); |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 286 | const auto prev_alignment = |
| 287 | GetAlignment(prev_canonical, layout, context); |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 288 | 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-baker | 9bb0979 | 2019-03-25 11:25:13 -0400 | [diff] [blame] | 294 | // 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-baker | 3d9e201 | 2019-01-11 14:55:30 -0500 | [diff] [blame] | 301 | field_decl->getSourceRange()); |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 302 | 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 Petit | 0fc8804 | 2019-04-09 23:25:02 +0100 | [diff] [blame] | 338 | std::unordered_set<std::string> Kernels; |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 339 | |
| 340 | public: |
| 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-baker | 9bb0979 | 2019-03-25 11:25:13 -0400 | [diff] [blame] | 353 | CustomDiagnosticsIDMap[CustomDiagnosticUnalignedScalar] = |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 354 | DE.getCustomDiagID(DiagnosticsEngine::Error, |
| 355 | "scalar elements must be aligned to their size"); |
alan-baker | 9bb0979 | 2019-03-25 11:25:13 -0400 | [diff] [blame] | 356 | CustomDiagnosticsIDMap[CustomDiagnosticUnalignedVec2] = DE.getCustomDiagID( |
| 357 | DiagnosticsEngine::Error, |
| 358 | "two-component vectors must be aligned to 2 times their element size"); |
| 359 | CustomDiagnosticsIDMap[CustomDiagnosticUnalignedVec4] = |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 360 | DE.getCustomDiagID(DiagnosticsEngine::Error, |
alan-baker | 9bb0979 | 2019-03-25 11:25:13 -0400 | [diff] [blame] | 361 | "three- and four-component vectors must be aligned " |
| 362 | "to 4 times their element size"); |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 363 | 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-baker | 9bb0979 | 2019-03-25 11:25:13 -0400 | [diff] [blame] | 373 | CustomDiagnosticsIDMap[CustomDiagnosticSmallStraddle] = |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 374 | DE.getCustomDiagID(DiagnosticsEngine::Error, |
alan-baker | 9bb0979 | 2019-03-25 11:25:13 -0400 | [diff] [blame] | 375 | "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-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 379 | DE.getCustomDiagID(DiagnosticsEngine::Error, |
alan-baker | 9bb0979 | 2019-03-25 11:25:13 -0400 | [diff] [blame] | 380 | "vectors with a total size greater than 16 bytes " |
| 381 | "must aligned to 16 bytes"); |
| 382 | CustomDiagnosticsIDMap[CustomDiagnosticUnalignedStructMember] = |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 383 | DE.getCustomDiagID(DiagnosticsEngine::Error, |
alan-baker | 9bb0979 | 2019-03-25 11:25:13 -0400 | [diff] [blame] | 384 | "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-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 387 | 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-baker | 3d9e201 | 2019-01-11 14:55:30 -0500 | [diff] [blame] | 395 | 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-baker | 9bb0979 | 2019-03-25 11:25:13 -0400 | [diff] [blame] | 401 | 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 Petit | 0fc8804 | 2019-04-09 23:25:02 +0100 | [diff] [blame] | 409 | CustomDiagnosticsIDMap[CustomDiagnosticOverloadedKernel] = |
| 410 | DE.getCustomDiagID(DiagnosticsEngine::Error, |
| 411 | "kernel functions can't be overloaded"); |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 412 | } |
| 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 Petit | 0fc8804 | 2019-04-09 23:25:02 +0100 | [diff] [blame] | 434 | 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-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 443 | for (auto *P : FD->parameters()) { |
| 444 | auto type = P->getType(); |
| 445 | if (!IsSupportedType(P->getOriginalType(), P->getSourceRange())) { |
| 446 | return false; |
| 447 | } |
| 448 | |
alan-baker | 9bb0979 | 2019-03-25 11:25:13 -0400 | [diff] [blame] | 449 | if (is_opencl_kernel && type->isPointerType() && |
| 450 | ((type->getPointeeType().getAddressSpace() == |
| 451 | LangAS::opencl_constant) || |
| 452 | (type->getPointeeType().getAddressSpace() == |
| 453 | LangAS::opencl_global))) { |
alan-baker | 3d9e201 | 2019-01-11 14:55:30 -0500 | [diff] [blame] | 454 | // 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-baker | 9bb0979 | 2019-03-25 11:25:13 -0400 | [diff] [blame] | 457 | 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-baker | 3d9e201 | 2019-01-11 14:55:30 -0500 | [diff] [blame] | 464 | auto array_type = FD->getASTContext().getIncompleteArrayType( |
| 465 | type->getPointeeType(), clang::ArrayType::Normal, 0); |
alan-baker | 9bb0979 | 2019-03-25 11:25:13 -0400 | [diff] [blame] | 466 | if (!IsSupportedLayout(array_type, 0, layout, FD->getASTContext(), |
| 467 | P->getSourceRange(), |
| 468 | P->getSourceRange())) { |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 469 | return false; |
| 470 | } |
| 471 | } |
alan-baker | 038e924 | 2019-04-19 22:14:41 -0400 | [diff] [blame^] | 472 | |
| 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-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | // Check for unsupported vector types. |
| 488 | Visitor.TraverseDecl(FD); |
| 489 | } |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | return true; |
| 494 | } |
| 495 | }; |
| 496 | } // namespace |
| 497 | |
| 498 | namespace clspv { |
| 499 | std::unique_ptr<ASTConsumer> |
| 500 | ExtraValidationASTAction::CreateASTConsumer(CompilerInstance &CI, |
| 501 | llvm::StringRef InFile) { |
| 502 | return std::unique_ptr<ASTConsumer>(new ExtraValidationConsumer(CI, InFile)); |
| 503 | } |
| 504 | } // namespace clspv |