Frank Derry Wanye | 156b127 | 2020-09-08 09:35:14 -0400 | [diff] [blame] | 1 | //===--- StructPackAlignCheck.cpp - clang-tidy ----------------------------===// |
| 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 | #include "StructPackAlignCheck.h" |
| 10 | #include "clang/AST/ASTContext.h" |
| 11 | #include "clang/AST/RecordLayout.h" |
| 12 | #include "clang/ASTMatchers/ASTMatchFinder.h" |
Piotr Zegar | c8644b1 | 2023-08-26 17:50:50 +0000 | [diff] [blame] | 13 | #include <cmath> |
Frank Derry Wanye | 156b127 | 2020-09-08 09:35:14 -0400 | [diff] [blame] | 14 | |
| 15 | using namespace clang::ast_matchers; |
| 16 | |
Carlos Galvez | 7d2ea6c | 2023-01-14 17:40:54 +0000 | [diff] [blame] | 17 | namespace clang::tidy::altera { |
Frank Derry Wanye | 156b127 | 2020-09-08 09:35:14 -0400 | [diff] [blame] | 18 | |
| 19 | void StructPackAlignCheck::registerMatchers(MatchFinder *Finder) { |
Matheus Izvekov | ee1f132 | 2022-10-31 19:34:23 +0100 | [diff] [blame] | 20 | Finder->addMatcher(recordDecl(isStruct(), isDefinition(), |
| 21 | unless(isExpansionInSystemHeader())) |
| 22 | .bind("struct"), |
| 23 | this); |
Frank Derry Wanye | 156b127 | 2020-09-08 09:35:14 -0400 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | CharUnits |
Piotr Zegar | 24a7587 | 2023-08-26 19:00:25 +0000 | [diff] [blame] | 27 | StructPackAlignCheck::computeRecommendedAlignment(CharUnits MinByteSize) const { |
Frank Derry Wanye | 156b127 | 2020-09-08 09:35:14 -0400 | [diff] [blame] | 28 | CharUnits NewAlign = CharUnits::fromQuantity(1); |
| 29 | if (!MinByteSize.isPowerOfTwo()) { |
Piotr Zegar | 44b38fe | 2023-08-26 19:25:37 +0000 | [diff] [blame] | 30 | CharUnits::QuantityType MSB = MinByteSize.getQuantity(); |
Frank Derry Wanye | 156b127 | 2020-09-08 09:35:14 -0400 | [diff] [blame] | 31 | for (; MSB > 0; MSB /= 2) { |
Piotr Zegar | 44b38fe | 2023-08-26 19:25:37 +0000 | [diff] [blame] | 32 | NewAlign = |
| 33 | NewAlign.alignTo(CharUnits::fromQuantity(NewAlign.getQuantity() * 2)); |
Frank Derry Wanye | 156b127 | 2020-09-08 09:35:14 -0400 | [diff] [blame] | 34 | // Abort if the computed alignment meets the maximum configured alignment. |
| 35 | if (NewAlign.getQuantity() >= MaxConfiguredAlignment) |
| 36 | break; |
| 37 | } |
| 38 | } else { |
| 39 | NewAlign = MinByteSize; |
| 40 | } |
| 41 | return NewAlign; |
| 42 | } |
| 43 | |
| 44 | void StructPackAlignCheck::check(const MatchFinder::MatchResult &Result) { |
| 45 | const auto *Struct = Result.Nodes.getNodeAs<RecordDecl>("struct"); |
| 46 | |
| 47 | // Do not trigger on templated struct declarations because the packing and |
| 48 | // alignment requirements are unknown. |
| 49 | if (Struct->isTemplated()) |
| 50 | return; |
| 51 | |
Balazs Benics | e1d0673 | 2021-11-29 09:56:43 +0100 | [diff] [blame] | 52 | // Packing and alignment requirements for invalid decls are meaningless. |
| 53 | if (Struct->isInvalidDecl()) |
| 54 | return; |
| 55 | |
Frank Derry Wanye | 156b127 | 2020-09-08 09:35:14 -0400 | [diff] [blame] | 56 | // Get sizing info for the struct. |
| 57 | llvm::SmallVector<std::pair<unsigned int, unsigned int>, 10> FieldSizes; |
| 58 | unsigned int TotalBitSize = 0; |
| 59 | for (const FieldDecl *StructField : Struct->fields()) { |
| 60 | // For each StructField, record how big it is (in bits). |
| 61 | // Would be good to use a pair of <offset, size> to advise a better |
| 62 | // packing order. |
Georgy Komarov | ab92a4c | 2021-05-16 08:27:46 +0300 | [diff] [blame] | 63 | QualType StructFieldTy = StructField->getType(); |
| 64 | if (StructFieldTy->isIncompleteType()) |
| 65 | return; |
Frank Derry Wanye | 156b127 | 2020-09-08 09:35:14 -0400 | [diff] [blame] | 66 | unsigned int StructFieldWidth = |
Georgy Komarov | ab92a4c | 2021-05-16 08:27:46 +0300 | [diff] [blame] | 67 | (unsigned int)Result.Context->getTypeInfo(StructFieldTy.getTypePtr()) |
Frank Derry Wanye | 156b127 | 2020-09-08 09:35:14 -0400 | [diff] [blame] | 68 | .Width; |
| 69 | FieldSizes.emplace_back(StructFieldWidth, StructField->getFieldIndex()); |
| 70 | // FIXME: Recommend a reorganization of the struct (sort by StructField |
| 71 | // size, largest to smallest). |
| 72 | TotalBitSize += StructFieldWidth; |
| 73 | } |
| 74 | |
| 75 | uint64_t CharSize = Result.Context->getCharWidth(); |
| 76 | CharUnits CurrSize = Result.Context->getASTRecordLayout(Struct).getSize(); |
| 77 | CharUnits MinByteSize = |
Fabian Wolff | f25935a | 2022-04-20 16:52:23 +0200 | [diff] [blame] | 78 | CharUnits::fromQuantity(std::max<clang::CharUnits::QuantityType>( |
| 79 | ceil(static_cast<float>(TotalBitSize) / CharSize), 1)); |
Frank Derry Wanye | 156b127 | 2020-09-08 09:35:14 -0400 | [diff] [blame] | 80 | CharUnits MaxAlign = CharUnits::fromQuantity( |
| 81 | ceil((float)Struct->getMaxAlignment() / CharSize)); |
| 82 | CharUnits CurrAlign = |
| 83 | Result.Context->getASTRecordLayout(Struct).getAlignment(); |
| 84 | CharUnits NewAlign = computeRecommendedAlignment(MinByteSize); |
| 85 | |
| 86 | bool IsPacked = Struct->hasAttr<PackedAttr>(); |
| 87 | bool NeedsPacking = (MinByteSize < CurrSize) && (MaxAlign != NewAlign) && |
| 88 | (CurrSize != NewAlign); |
| 89 | bool NeedsAlignment = CurrAlign.getQuantity() != NewAlign.getQuantity(); |
| 90 | |
| 91 | if (!NeedsAlignment && !NeedsPacking) |
| 92 | return; |
| 93 | |
| 94 | // If it's using much more space than it needs, suggest packing. |
| 95 | // (Do not suggest packing if it is currently explicitly aligned to what the |
| 96 | // minimum byte size would suggest as the new alignment.) |
| 97 | if (NeedsPacking && !IsPacked) { |
| 98 | diag(Struct->getLocation(), |
| 99 | "accessing fields in struct %0 is inefficient due to padding; only " |
| 100 | "needs %1 bytes but is using %2 bytes") |
| 101 | << Struct << (int)MinByteSize.getQuantity() |
| 102 | << (int)CurrSize.getQuantity() |
| 103 | << FixItHint::CreateInsertion(Struct->getEndLoc().getLocWithOffset(1), |
| 104 | " __attribute__((packed))"); |
| 105 | diag(Struct->getLocation(), |
| 106 | "use \"__attribute__((packed))\" to reduce the amount of padding " |
| 107 | "applied to struct %0", |
| 108 | DiagnosticIDs::Note) |
| 109 | << Struct; |
| 110 | } |
| 111 | |
| 112 | FixItHint FixIt; |
Piotr Zegar | fc2a9ad | 2023-08-27 09:32:28 +0000 | [diff] [blame] | 113 | auto *Attribute = Struct->getAttr<AlignedAttr>(); |
Frank Derry Wanye | 156b127 | 2020-09-08 09:35:14 -0400 | [diff] [blame] | 114 | std::string NewAlignQuantity = std::to_string((int)NewAlign.getQuantity()); |
| 115 | if (Attribute) { |
Nathan James | 00c7d66 | 2021-03-02 21:57:16 +0000 | [diff] [blame] | 116 | FixIt = FixItHint::CreateReplacement( |
| 117 | Attribute->getRange(), |
| 118 | (Twine("aligned(") + NewAlignQuantity + ")").str()); |
Frank Derry Wanye | 156b127 | 2020-09-08 09:35:14 -0400 | [diff] [blame] | 119 | } else { |
Nathan James | 00c7d66 | 2021-03-02 21:57:16 +0000 | [diff] [blame] | 120 | FixIt = FixItHint::CreateInsertion( |
| 121 | Struct->getEndLoc().getLocWithOffset(1), |
| 122 | (Twine(" __attribute__((aligned(") + NewAlignQuantity + ")))").str()); |
Frank Derry Wanye | 156b127 | 2020-09-08 09:35:14 -0400 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | // And suggest the minimum power-of-two alignment for the struct as a whole |
| 126 | // (with and without packing). |
| 127 | if (NeedsAlignment) { |
| 128 | diag(Struct->getLocation(), |
| 129 | "accessing fields in struct %0 is inefficient due to poor alignment; " |
| 130 | "currently aligned to %1 bytes, but recommended alignment is %2 bytes") |
| 131 | << Struct << (int)CurrAlign.getQuantity() << NewAlignQuantity << FixIt; |
| 132 | |
| 133 | diag(Struct->getLocation(), |
| 134 | "use \"__attribute__((aligned(%0)))\" to align struct %1 to %0 bytes", |
| 135 | DiagnosticIDs::Note) |
| 136 | << NewAlignQuantity << Struct; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | void StructPackAlignCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { |
| 141 | Options.store(Opts, "MaxConfiguredAlignment", MaxConfiguredAlignment); |
| 142 | } |
| 143 | |
Carlos Galvez | 7d2ea6c | 2023-01-14 17:40:54 +0000 | [diff] [blame] | 144 | } // namespace clang::tidy::altera |