Files
TheRock/compiler/amd-llvm/clang-tools-extra/clang-tidy/altera/StructPackAlignCheck.cpp
T
derek.holloway 7502bf64a4
CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (python) (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 0s
Multi-Arch CI / setup (push) Canceled after 0s
Publish build_manylinux_x86_64 images / publish_build_manylinux_x86_64 (push) Canceled after 0s
Setup (Multi-Arch) / setup (push) Canceled after 0s
Multi-Arch CI / Linux::${{ fromJSON(needs.setup.outputs.linux_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubi10 image / publish_no_rocm_image_ubi10 (push) Canceled after 0s
Multi-Arch CI / Windows::${{ fromJSON(needs.setup.outputs.windows_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Multi-Arch CI / Manifest Diff (push) Canceled after 0s
TheRock Manifest Diff Report / Generate Manifest Diff Report (push) Canceled after 0s
Multi-Arch CI / CI Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_ocl_rt images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_rocgdb images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Unit Tests / Unit Tests :: ubuntu-24.04 (push) Canceled after 0s
Unit Tests / Unit Tests :: windows-2022 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_media (push) Canceled after 0s
Unit Tests / Unit Tests Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_openmpi (push) Canceled after 0s
Flatten subprojects into a single base monorepo
2026-09-02 16:34:47 -07:00

142 lines
5.5 KiB
C++

//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "StructPackAlignCheck.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/RecordLayout.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include <cmath>
using namespace clang::ast_matchers;
namespace clang::tidy::altera {
void StructPackAlignCheck::registerMatchers(MatchFinder *Finder) {
Finder->addMatcher(recordDecl(isStruct(), isDefinition()).bind("struct"),
this);
}
CharUnits
StructPackAlignCheck::computeRecommendedAlignment(CharUnits MinByteSize) const {
CharUnits NewAlign = CharUnits::fromQuantity(1);
if (!MinByteSize.isPowerOfTwo()) {
CharUnits::QuantityType MSB = MinByteSize.getQuantity();
for (; MSB > 0; MSB /= 2) {
NewAlign =
NewAlign.alignTo(CharUnits::fromQuantity(NewAlign.getQuantity() * 2));
// Abort if the computed alignment meets the maximum configured alignment.
if (NewAlign.getQuantity() >= MaxConfiguredAlignment)
break;
}
} else {
NewAlign = MinByteSize;
}
return NewAlign;
}
void StructPackAlignCheck::check(const MatchFinder::MatchResult &Result) {
const auto *Struct = Result.Nodes.getNodeAs<RecordDecl>("struct");
// Do not trigger on templated struct declarations because the packing and
// alignment requirements are unknown.
if (Struct->isTemplated())
return;
// Packing and alignment requirements for invalid decls are meaningless.
if (Struct->isInvalidDecl())
return;
// Get sizing info for the struct.
SmallVector<std::pair<unsigned int, unsigned int>, 10> FieldSizes;
unsigned int TotalBitSize = 0;
for (const FieldDecl *StructField : Struct->fields()) {
// For each StructField, record how big it is (in bits).
// Would be good to use a pair of <offset, size> to advise a better
// packing order.
const QualType StructFieldTy = StructField->getType();
if (StructFieldTy->isIncompleteType())
return;
const unsigned int StructFieldWidth = static_cast<unsigned int>(
Result.Context->getTypeInfo(StructFieldTy.getTypePtr()).Width);
FieldSizes.emplace_back(StructFieldWidth, StructField->getFieldIndex());
// FIXME: Recommend a reorganization of the struct (sort by StructField
// size, largest to smallest).
TotalBitSize += StructFieldWidth;
}
const uint64_t CharSize = Result.Context->getCharWidth();
const CharUnits CurrSize =
Result.Context->getASTRecordLayout(Struct).getSize();
const CharUnits MinByteSize =
CharUnits::fromQuantity(std::max<CharUnits::QuantityType>(
std::ceil(static_cast<float>(TotalBitSize) / CharSize), 1));
const CharUnits MaxAlign = CharUnits::fromQuantity(
std::ceil(static_cast<float>(Struct->getMaxAlignment()) / CharSize));
const CharUnits CurrAlign =
Result.Context->getASTRecordLayout(Struct).getAlignment();
const CharUnits NewAlign = computeRecommendedAlignment(MinByteSize);
const bool IsPacked = Struct->hasAttr<PackedAttr>();
const bool NeedsPacking = (MinByteSize < CurrSize) &&
(MaxAlign != NewAlign) && (CurrSize != NewAlign);
const bool NeedsAlignment = CurrAlign.getQuantity() != NewAlign.getQuantity();
if (!NeedsAlignment && !NeedsPacking)
return;
// If it's using much more space than it needs, suggest packing.
// (Do not suggest packing if it is currently explicitly aligned to what the
// minimum byte size would suggest as the new alignment.)
if (NeedsPacking && !IsPacked) {
diag(Struct->getLocation(),
"accessing fields in struct %0 is inefficient due to padding; only "
"needs %1 bytes but is using %2 bytes")
<< Struct << MinByteSize.getQuantity() << CurrSize.getQuantity()
<< FixItHint::CreateInsertion(Struct->getEndLoc().getLocWithOffset(1),
" __attribute__((packed))");
diag(Struct->getLocation(),
"use \"__attribute__((packed))\" to reduce the amount of padding "
"applied to struct %0",
DiagnosticIDs::Note)
<< Struct;
}
FixItHint FixIt;
auto *Attribute = Struct->getAttr<AlignedAttr>();
const std::string NewAlignQuantity = std::to_string(NewAlign.getQuantity());
if (Attribute) {
FixIt = FixItHint::CreateReplacement(
Attribute->getRange(),
(Twine("aligned(") + NewAlignQuantity + ")").str());
} else {
FixIt = FixItHint::CreateInsertion(
Struct->getEndLoc().getLocWithOffset(1),
(Twine(" __attribute__((aligned(") + NewAlignQuantity + ")))").str());
}
// And suggest the minimum power-of-two alignment for the struct as a whole
// (with and without packing).
if (NeedsAlignment) {
diag(Struct->getLocation(),
"accessing fields in struct %0 is inefficient due to poor alignment; "
"currently aligned to %1 bytes, but recommended alignment is %2 bytes")
<< Struct << CurrAlign.getQuantity() << NewAlignQuantity << FixIt;
diag(Struct->getLocation(),
"use \"__attribute__((aligned(%0)))\" to align struct %1 to %0 bytes",
DiagnosticIDs::Note)
<< NewAlignQuantity << Struct;
}
}
void StructPackAlignCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
Options.store(Opts, "MaxConfiguredAlignment", MaxConfiguredAlignment);
}
} // namespace clang::tidy::altera